4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
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 int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
105 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
106 static inline struct nfs4_label *
107 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
126 nfs4_label_release_security(struct nfs4_label *label)
129 security_release_secctx(label->label, label->len);
131 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
134 return server->attr_bitmask;
136 return server->attr_bitmask_nl;
139 static inline struct nfs4_label *
140 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
144 nfs4_label_release_security(struct nfs4_label *label)
147 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148 { return server->attr_bitmask; }
151 /* Prevent leaks of NFSv4 errors into userland */
152 static int nfs4_map_errors(int err)
157 case -NFS4ERR_RESOURCE:
158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
161 case -NFS4ERR_WRONGSEC:
162 case -NFS4ERR_WRONG_CRED:
164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
167 case -NFS4ERR_SHARE_DENIED:
169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
171 case -NFS4ERR_FILE_OPEN:
174 dprintk("%s could not handle NFSv4 error %d\n",
182 * This is our standard bitmap for GETATTR requests.
184 const u32 nfs4_fattr_bitmap[3] = {
186 | FATTR4_WORD0_CHANGE
189 | FATTR4_WORD0_FILEID,
191 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
200 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
205 static const u32 nfs4_pnfs_open_bitmap[3] = {
207 | FATTR4_WORD0_CHANGE
210 | FATTR4_WORD0_FILEID,
212 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
221 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
226 static const u32 nfs4_open_noattr_bitmap[3] = {
228 | FATTR4_WORD0_FILEID,
231 const u32 nfs4_statfs_bitmap[3] = {
232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
240 const u32 nfs4_pathconf_bitmap[3] = {
242 | FATTR4_WORD0_MAXNAME,
246 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME,
250 FATTR4_WORD1_TIME_DELTA
251 | FATTR4_WORD1_FS_LAYOUT_TYPES,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
253 | FATTR4_WORD2_CLONE_BLKSIZE
256 const u32 nfs4_fs_locations_bitmap[3] = {
258 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
264 | FATTR4_WORD1_NUMLINKS
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
272 | FATTR4_WORD1_MOUNTED_ON_FILEID,
275 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
276 struct nfs4_readdir_arg *readdir)
278 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
282 readdir->cookie = cookie;
283 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
288 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
293 * NFSv4 servers do not return entries for '.' and '..'
294 * Therefore, we fake these entries here. We let '.'
295 * have cookie 0 and '..' have cookie 1. Note that
296 * when talking to the server, we always send cookie 0
299 start = p = kmap_atomic(*readdir->pages);
302 *p++ = xdr_one; /* next */
303 *p++ = xdr_zero; /* cookie, first word */
304 *p++ = xdr_one; /* cookie, second word */
305 *p++ = xdr_one; /* entry len */
306 memcpy(p, ".\0\0\0", 4); /* entry */
308 *p++ = xdr_one; /* bitmap length */
309 *p++ = htonl(attrs); /* bitmap */
310 *p++ = htonl(12); /* attribute buffer length */
311 *p++ = htonl(NF4DIR);
312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(attrs); /* bitmap */
323 *p++ = htonl(12); /* attribute buffer length */
324 *p++ = htonl(NF4DIR);
325 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
327 readdir->pgbase = (char *)p - (char *)start;
328 readdir->count -= readdir->pgbase;
329 kunmap_atomic(start);
332 static void nfs4_test_and_free_stateid(struct nfs_server *server,
333 nfs4_stateid *stateid,
334 struct rpc_cred *cred)
336 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
338 ops->test_and_free_expired(server, stateid, cred);
341 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
342 nfs4_stateid *stateid,
343 struct rpc_cred *cred)
345 stateid->type = NFS4_REVOKED_STATEID_TYPE;
346 nfs4_test_and_free_stateid(server, stateid, cred);
349 static void nfs4_free_revoked_stateid(struct nfs_server *server,
350 const nfs4_stateid *stateid,
351 struct rpc_cred *cred)
355 nfs4_stateid_copy(&tmp, stateid);
356 __nfs4_free_revoked_stateid(server, &tmp, cred);
359 static long nfs4_update_delay(long *timeout)
363 return NFS4_POLL_RETRY_MAX;
365 *timeout = NFS4_POLL_RETRY_MIN;
366 if (*timeout > NFS4_POLL_RETRY_MAX)
367 *timeout = NFS4_POLL_RETRY_MAX;
373 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
379 freezable_schedule_timeout_killable_unsafe(
380 nfs4_update_delay(timeout));
381 if (fatal_signal_pending(current))
386 /* This is the error handling routine for processes that are allowed
389 static int nfs4_do_handle_exception(struct nfs_server *server,
390 int errorcode, struct nfs4_exception *exception)
392 struct nfs_client *clp = server->nfs_client;
393 struct nfs4_state *state = exception->state;
394 const nfs4_stateid *stateid = exception->stateid;
395 struct inode *inode = exception->inode;
398 exception->delay = 0;
399 exception->recovering = 0;
400 exception->retry = 0;
402 if (stateid == NULL && state != NULL)
403 stateid = &state->stateid;
408 case -NFS4ERR_DELEG_REVOKED:
409 case -NFS4ERR_ADMIN_REVOKED:
410 case -NFS4ERR_EXPIRED:
411 case -NFS4ERR_BAD_STATEID:
412 if (inode != NULL && stateid != NULL) {
413 nfs_inode_find_state_and_recover(inode,
415 goto wait_on_recovery;
417 case -NFS4ERR_OPENMODE:
421 err = nfs_async_inode_return_delegation(inode,
424 goto wait_on_recovery;
425 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
426 exception->retry = 1;
432 ret = nfs4_schedule_stateid_recovery(server, state);
435 goto wait_on_recovery;
436 case -NFS4ERR_STALE_STATEID:
437 case -NFS4ERR_STALE_CLIENTID:
438 nfs4_schedule_lease_recovery(clp);
439 goto wait_on_recovery;
441 ret = nfs4_schedule_migration_recovery(server);
444 goto wait_on_recovery;
445 case -NFS4ERR_LEASE_MOVED:
446 nfs4_schedule_lease_moved_recovery(clp);
447 goto wait_on_recovery;
448 #if defined(CONFIG_NFS_V4_1)
449 case -NFS4ERR_BADSESSION:
450 case -NFS4ERR_BADSLOT:
451 case -NFS4ERR_BAD_HIGH_SLOT:
452 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
453 case -NFS4ERR_DEADSESSION:
454 case -NFS4ERR_SEQ_FALSE_RETRY:
455 case -NFS4ERR_SEQ_MISORDERED:
456 dprintk("%s ERROR: %d Reset session\n", __func__,
458 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
459 goto wait_on_recovery;
460 #endif /* defined(CONFIG_NFS_V4_1) */
461 case -NFS4ERR_FILE_OPEN:
462 if (exception->timeout > HZ) {
463 /* We have retried a decent amount, time to
470 nfs_inc_server_stats(server, NFSIOS_DELAY);
472 case -NFS4ERR_LAYOUTTRYLATER:
473 case -NFS4ERR_RECALLCONFLICT:
474 exception->delay = 1;
477 case -NFS4ERR_RETRY_UNCACHED_REP:
478 case -NFS4ERR_OLD_STATEID:
479 exception->retry = 1;
481 case -NFS4ERR_BADOWNER:
482 /* The following works around a Linux server bug! */
483 case -NFS4ERR_BADNAME:
484 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
485 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
486 exception->retry = 1;
487 printk(KERN_WARNING "NFS: v4 server %s "
488 "does not accept raw "
490 "Reenabling the idmapper.\n",
491 server->nfs_client->cl_hostname);
494 /* We failed to handle the error */
495 return nfs4_map_errors(ret);
497 exception->recovering = 1;
501 /* This is the error handling routine for processes that are allowed
504 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
506 struct nfs_client *clp = server->nfs_client;
509 ret = nfs4_do_handle_exception(server, errorcode, exception);
510 if (exception->delay) {
511 ret = nfs4_delay(server->client, &exception->timeout);
514 if (exception->recovering) {
515 ret = nfs4_wait_clnt_recover(clp);
516 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
523 exception->retry = 1;
528 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
529 int errorcode, struct nfs4_exception *exception)
531 struct nfs_client *clp = server->nfs_client;
534 ret = nfs4_do_handle_exception(server, errorcode, exception);
535 if (exception->delay) {
536 rpc_delay(task, nfs4_update_delay(&exception->timeout));
539 if (exception->recovering) {
540 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
541 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
542 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
545 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
550 exception->retry = 1;
555 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
556 struct nfs4_state *state, long *timeout)
558 struct nfs4_exception exception = {
562 if (task->tk_status >= 0)
565 exception.timeout = *timeout;
566 task->tk_status = nfs4_async_handle_exception(task, server,
569 if (exception.delay && timeout)
570 *timeout = exception.timeout;
577 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
578 * or 'false' otherwise.
580 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
582 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
583 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
586 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
588 spin_lock(&clp->cl_lock);
589 if (time_before(clp->cl_last_renewal,timestamp))
590 clp->cl_last_renewal = timestamp;
591 spin_unlock(&clp->cl_lock);
594 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
596 struct nfs_client *clp = server->nfs_client;
598 if (!nfs4_has_session(clp))
599 do_renew_lease(clp, timestamp);
602 struct nfs4_call_sync_data {
603 const struct nfs_server *seq_server;
604 struct nfs4_sequence_args *seq_args;
605 struct nfs4_sequence_res *seq_res;
608 void nfs4_init_sequence(struct nfs4_sequence_args *args,
609 struct nfs4_sequence_res *res, int cache_reply)
611 args->sa_slot = NULL;
612 args->sa_cache_this = cache_reply;
613 args->sa_privileged = 0;
618 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
620 args->sa_privileged = 1;
623 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
625 struct nfs4_slot *slot = res->sr_slot;
626 struct nfs4_slot_table *tbl;
629 spin_lock(&tbl->slot_tbl_lock);
630 if (!nfs41_wake_and_assign_slot(tbl, slot))
631 nfs4_free_slot(tbl, slot);
632 spin_unlock(&tbl->slot_tbl_lock);
637 static int nfs40_sequence_done(struct rpc_task *task,
638 struct nfs4_sequence_res *res)
640 if (res->sr_slot != NULL)
641 nfs40_sequence_free_slot(res);
645 #if defined(CONFIG_NFS_V4_1)
647 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
649 struct nfs4_session *session;
650 struct nfs4_slot_table *tbl;
651 struct nfs4_slot *slot = res->sr_slot;
652 bool send_new_highest_used_slotid = false;
655 session = tbl->session;
657 /* Bump the slot sequence number */
662 spin_lock(&tbl->slot_tbl_lock);
663 /* Be nice to the server: try to ensure that the last transmitted
664 * value for highest_user_slotid <= target_highest_slotid
666 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
667 send_new_highest_used_slotid = true;
669 if (nfs41_wake_and_assign_slot(tbl, slot)) {
670 send_new_highest_used_slotid = false;
673 nfs4_free_slot(tbl, slot);
675 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
676 send_new_highest_used_slotid = false;
678 spin_unlock(&tbl->slot_tbl_lock);
680 if (send_new_highest_used_slotid)
681 nfs41_notify_server(session->clp);
682 if (waitqueue_active(&tbl->slot_waitq))
683 wake_up_all(&tbl->slot_waitq);
686 static int nfs41_sequence_process(struct rpc_task *task,
687 struct nfs4_sequence_res *res)
689 struct nfs4_session *session;
690 struct nfs4_slot *slot = res->sr_slot;
691 struct nfs_client *clp;
692 bool interrupted = false;
697 /* don't increment the sequence number if the task wasn't sent */
698 if (!RPC_WAS_SENT(task))
701 session = slot->table->session;
703 if (slot->interrupted) {
704 if (res->sr_status != -NFS4ERR_DELAY)
705 slot->interrupted = 0;
709 trace_nfs4_sequence_done(session, res);
710 /* Check the SEQUENCE operation status */
711 switch (res->sr_status) {
713 /* If previous op on slot was interrupted and we reused
714 * the seq# and got a reply from the cache, then retry
716 if (task->tk_status == -EREMOTEIO && interrupted) {
720 /* Update the slot's sequence and clientid lease timer */
723 do_renew_lease(clp, res->sr_timestamp);
724 /* Check sequence flags */
725 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
727 nfs41_update_target_slotid(slot->table, slot, res);
731 * sr_status remains 1 if an RPC level error occurred.
732 * The server may or may not have processed the sequence
734 * Mark the slot as having hosted an interrupted RPC call.
736 slot->interrupted = 1;
739 /* The server detected a resend of the RPC call and
740 * returned NFS4ERR_DELAY as per Section 2.10.6.2
743 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
748 case -NFS4ERR_BADSLOT:
750 * The slot id we used was probably retired. Try again
751 * using a different slot id.
754 case -NFS4ERR_SEQ_MISORDERED:
756 * Was the last operation on this sequence interrupted?
757 * If so, retry after bumping the sequence number.
764 * Could this slot have been previously retired?
765 * If so, then the server may be expecting seq_nr = 1!
767 if (slot->seq_nr != 1) {
772 case -NFS4ERR_SEQ_FALSE_RETRY:
776 /* Just update the slot sequence no. */
780 /* The session may be reset by one of the error handlers. */
781 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
785 if (rpc_restart_call_prepare(task)) {
786 nfs41_sequence_free_slot(res);
792 if (!rpc_restart_call(task))
794 rpc_delay(task, NFS4_POLL_RETRY_MAX);
798 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
800 if (!nfs41_sequence_process(task, res))
802 if (res->sr_slot != NULL)
803 nfs41_sequence_free_slot(res);
807 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
809 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
811 if (res->sr_slot == NULL)
813 if (res->sr_slot->table->session != NULL)
814 return nfs41_sequence_process(task, res);
815 return nfs40_sequence_done(task, res);
818 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
820 if (res->sr_slot != NULL) {
821 if (res->sr_slot->table->session != NULL)
822 nfs41_sequence_free_slot(res);
824 nfs40_sequence_free_slot(res);
828 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
830 if (res->sr_slot == NULL)
832 if (!res->sr_slot->table->session)
833 return nfs40_sequence_done(task, res);
834 return nfs41_sequence_done(task, res);
836 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
838 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
840 struct nfs4_call_sync_data *data = calldata;
842 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
844 nfs4_setup_sequence(data->seq_server->nfs_client,
845 data->seq_args, data->seq_res, task);
848 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
850 struct nfs4_call_sync_data *data = calldata;
852 nfs41_sequence_done(task, data->seq_res);
855 static const struct rpc_call_ops nfs41_call_sync_ops = {
856 .rpc_call_prepare = nfs41_call_sync_prepare,
857 .rpc_call_done = nfs41_call_sync_done,
860 #else /* !CONFIG_NFS_V4_1 */
862 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
864 return nfs40_sequence_done(task, res);
867 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
869 if (res->sr_slot != NULL)
870 nfs40_sequence_free_slot(res);
873 int nfs4_sequence_done(struct rpc_task *task,
874 struct nfs4_sequence_res *res)
876 return nfs40_sequence_done(task, res);
878 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
880 #endif /* !CONFIG_NFS_V4_1 */
882 int nfs4_setup_sequence(const struct nfs_client *client,
883 struct nfs4_sequence_args *args,
884 struct nfs4_sequence_res *res,
885 struct rpc_task *task)
887 struct nfs4_session *session = nfs4_get_session(client);
888 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
889 struct nfs4_slot *slot;
891 /* slot already allocated? */
892 if (res->sr_slot != NULL)
896 tbl = &session->fc_slot_table;
897 task->tk_timeout = 0;
900 spin_lock(&tbl->slot_tbl_lock);
901 /* The state manager will wait until the slot table is empty */
902 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
905 slot = nfs4_alloc_slot(tbl);
907 /* Try again in 1/4 second */
908 if (slot == ERR_PTR(-ENOMEM))
909 task->tk_timeout = HZ >> 2;
912 spin_unlock(&tbl->slot_tbl_lock);
914 slot->privileged = args->sa_privileged ? 1 : 0;
915 args->sa_slot = slot;
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
924 trace_nfs4_setup_sequence(session, args);
926 rpc_call_start(task);
930 if (args->sa_privileged)
931 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
932 NULL, RPC_PRIORITY_PRIVILEGED);
934 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
935 spin_unlock(&tbl->slot_tbl_lock);
938 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
940 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
942 struct nfs4_call_sync_data *data = calldata;
943 nfs4_setup_sequence(data->seq_server->nfs_client,
944 data->seq_args, data->seq_res, task);
947 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
949 struct nfs4_call_sync_data *data = calldata;
950 nfs4_sequence_done(task, data->seq_res);
953 static const struct rpc_call_ops nfs40_call_sync_ops = {
954 .rpc_call_prepare = nfs40_call_sync_prepare,
955 .rpc_call_done = nfs40_call_sync_done,
958 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
959 struct nfs_server *server,
960 struct rpc_message *msg,
961 struct nfs4_sequence_args *args,
962 struct nfs4_sequence_res *res)
965 struct rpc_task *task;
966 struct nfs_client *clp = server->nfs_client;
967 struct nfs4_call_sync_data data = {
968 .seq_server = server,
972 struct rpc_task_setup task_setup = {
975 .callback_ops = clp->cl_mvops->call_sync_ops,
976 .callback_data = &data
979 task = rpc_run_task(&task_setup);
983 ret = task->tk_status;
989 int nfs4_call_sync(struct rpc_clnt *clnt,
990 struct nfs_server *server,
991 struct rpc_message *msg,
992 struct nfs4_sequence_args *args,
993 struct nfs4_sequence_res *res,
996 nfs4_init_sequence(args, res, cache_reply);
997 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1000 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1001 unsigned long timestamp)
1003 struct nfs_inode *nfsi = NFS_I(dir);
1005 spin_lock(&dir->i_lock);
1006 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1007 if (cinfo->atomic && cinfo->before == dir->i_version) {
1008 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1009 nfsi->attrtimeo_timestamp = jiffies;
1011 nfs_force_lookup_revalidate(dir);
1012 if (cinfo->before != dir->i_version)
1013 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1014 NFS_INO_INVALID_ACL;
1016 dir->i_version = cinfo->after;
1017 nfsi->read_cache_jiffies = timestamp;
1018 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1019 nfs_fscache_invalidate(dir);
1020 spin_unlock(&dir->i_lock);
1023 struct nfs4_opendata {
1025 struct nfs_openargs o_arg;
1026 struct nfs_openres o_res;
1027 struct nfs_open_confirmargs c_arg;
1028 struct nfs_open_confirmres c_res;
1029 struct nfs4_string owner_name;
1030 struct nfs4_string group_name;
1031 struct nfs4_label *a_label;
1032 struct nfs_fattr f_attr;
1033 struct nfs4_label *f_label;
1035 struct dentry *dentry;
1036 struct nfs4_state_owner *owner;
1037 struct nfs4_state *state;
1039 unsigned long timestamp;
1047 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1048 int err, struct nfs4_exception *exception)
1052 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1054 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1055 exception->retry = 1;
1060 nfs4_map_atomic_open_share(struct nfs_server *server,
1061 fmode_t fmode, int openflags)
1065 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1067 res = NFS4_SHARE_ACCESS_READ;
1070 res = NFS4_SHARE_ACCESS_WRITE;
1072 case FMODE_READ|FMODE_WRITE:
1073 res = NFS4_SHARE_ACCESS_BOTH;
1075 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1077 /* Want no delegation if we're using O_DIRECT */
1078 if (openflags & O_DIRECT)
1079 res |= NFS4_SHARE_WANT_NO_DELEG;
1084 static enum open_claim_type4
1085 nfs4_map_atomic_open_claim(struct nfs_server *server,
1086 enum open_claim_type4 claim)
1088 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1093 case NFS4_OPEN_CLAIM_FH:
1094 return NFS4_OPEN_CLAIM_NULL;
1095 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1096 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1097 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1098 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1102 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1104 p->o_res.f_attr = &p->f_attr;
1105 p->o_res.f_label = p->f_label;
1106 p->o_res.seqid = p->o_arg.seqid;
1107 p->c_res.seqid = p->c_arg.seqid;
1108 p->o_res.server = p->o_arg.server;
1109 p->o_res.access_request = p->o_arg.access;
1110 nfs_fattr_init(&p->f_attr);
1111 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1114 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1115 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1116 const struct iattr *attrs,
1117 struct nfs4_label *label,
1118 enum open_claim_type4 claim,
1121 struct dentry *parent = dget_parent(dentry);
1122 struct inode *dir = d_inode(parent);
1123 struct nfs_server *server = NFS_SERVER(dir);
1124 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1125 struct nfs4_opendata *p;
1127 p = kzalloc(sizeof(*p), gfp_mask);
1131 p->f_label = nfs4_label_alloc(server, gfp_mask);
1132 if (IS_ERR(p->f_label))
1135 p->a_label = nfs4_label_alloc(server, gfp_mask);
1136 if (IS_ERR(p->a_label))
1139 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1140 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1141 if (IS_ERR(p->o_arg.seqid))
1142 goto err_free_label;
1143 nfs_sb_active(dentry->d_sb);
1144 p->dentry = dget(dentry);
1147 atomic_inc(&sp->so_count);
1148 p->o_arg.open_flags = flags;
1149 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1150 p->o_arg.umask = current_umask();
1151 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1152 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1154 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1155 * will return permission denied for all bits until close */
1156 if (!(flags & O_EXCL)) {
1157 /* ask server to check for all possible rights as results
1159 switch (p->o_arg.claim) {
1162 case NFS4_OPEN_CLAIM_NULL:
1163 case NFS4_OPEN_CLAIM_FH:
1164 p->o_arg.access = NFS4_ACCESS_READ |
1165 NFS4_ACCESS_MODIFY |
1166 NFS4_ACCESS_EXTEND |
1167 NFS4_ACCESS_EXECUTE;
1170 p->o_arg.clientid = server->nfs_client->cl_clientid;
1171 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1172 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1173 p->o_arg.name = &dentry->d_name;
1174 p->o_arg.server = server;
1175 p->o_arg.bitmask = nfs4_bitmask(server, label);
1176 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1177 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1178 switch (p->o_arg.claim) {
1179 case NFS4_OPEN_CLAIM_NULL:
1180 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1181 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1182 p->o_arg.fh = NFS_FH(dir);
1184 case NFS4_OPEN_CLAIM_PREVIOUS:
1185 case NFS4_OPEN_CLAIM_FH:
1186 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1187 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1188 p->o_arg.fh = NFS_FH(d_inode(dentry));
1190 if (attrs != NULL && attrs->ia_valid != 0) {
1193 p->o_arg.u.attrs = &p->attrs;
1194 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1197 verf[1] = current->pid;
1198 memcpy(p->o_arg.u.verifier.data, verf,
1199 sizeof(p->o_arg.u.verifier.data));
1201 p->c_arg.fh = &p->o_res.fh;
1202 p->c_arg.stateid = &p->o_res.stateid;
1203 p->c_arg.seqid = p->o_arg.seqid;
1204 nfs4_init_opendata_res(p);
1205 kref_init(&p->kref);
1209 nfs4_label_free(p->a_label);
1211 nfs4_label_free(p->f_label);
1219 static void nfs4_opendata_free(struct kref *kref)
1221 struct nfs4_opendata *p = container_of(kref,
1222 struct nfs4_opendata, kref);
1223 struct super_block *sb = p->dentry->d_sb;
1225 nfs_free_seqid(p->o_arg.seqid);
1226 nfs4_sequence_free_slot(&p->o_res.seq_res);
1227 if (p->state != NULL)
1228 nfs4_put_open_state(p->state);
1229 nfs4_put_state_owner(p->owner);
1231 nfs4_label_free(p->a_label);
1232 nfs4_label_free(p->f_label);
1236 nfs_sb_deactive(sb);
1237 nfs_fattr_free_names(&p->f_attr);
1238 kfree(p->f_attr.mdsthreshold);
1242 static void nfs4_opendata_put(struct nfs4_opendata *p)
1245 kref_put(&p->kref, nfs4_opendata_free);
1248 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1251 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1252 case FMODE_READ|FMODE_WRITE:
1253 return state->n_rdwr != 0;
1255 return state->n_wronly != 0;
1257 return state->n_rdonly != 0;
1263 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1267 if (open_mode & (O_EXCL|O_TRUNC))
1269 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1271 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1272 && state->n_rdonly != 0;
1275 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1276 && state->n_wronly != 0;
1278 case FMODE_READ|FMODE_WRITE:
1279 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1280 && state->n_rdwr != 0;
1286 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1287 enum open_claim_type4 claim)
1289 if (delegation == NULL)
1291 if ((delegation->type & fmode) != fmode)
1293 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1296 case NFS4_OPEN_CLAIM_NULL:
1297 case NFS4_OPEN_CLAIM_FH:
1299 case NFS4_OPEN_CLAIM_PREVIOUS:
1300 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1305 nfs_mark_delegation_referenced(delegation);
1309 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1318 case FMODE_READ|FMODE_WRITE:
1321 nfs4_state_set_mode_locked(state, state->state | fmode);
1324 #ifdef CONFIG_NFS_V4_1
1325 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1327 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1329 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1331 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1335 #endif /* CONFIG_NFS_V4_1 */
1337 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1339 struct nfs_client *clp = state->owner->so_server->nfs_client;
1340 bool need_recover = false;
1342 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1343 need_recover = true;
1344 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1345 need_recover = true;
1346 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1347 need_recover = true;
1349 nfs4_state_mark_reclaim_nograce(clp, state);
1352 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1353 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1355 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1357 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1358 nfs4_stateid_copy(freeme, &state->open_stateid);
1359 nfs_test_and_clear_all_open_stateid(state);
1362 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1367 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1369 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1371 if (state->n_wronly)
1372 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1373 if (state->n_rdonly)
1374 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1376 set_bit(NFS_O_RDWR_STATE, &state->flags);
1377 set_bit(NFS_OPEN_STATE, &state->flags);
1380 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1381 nfs4_stateid *stateid, fmode_t fmode)
1383 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1384 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1386 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1389 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1392 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1393 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 clear_bit(NFS_OPEN_STATE, &state->flags);
1396 if (stateid == NULL)
1398 /* Handle OPEN+OPEN_DOWNGRADE races */
1399 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1400 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1401 nfs_resync_open_stateid_locked(state);
1404 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1405 nfs4_stateid_copy(&state->stateid, stateid);
1406 nfs4_stateid_copy(&state->open_stateid, stateid);
1409 static void nfs_clear_open_stateid(struct nfs4_state *state,
1410 nfs4_stateid *arg_stateid,
1411 nfs4_stateid *stateid, fmode_t fmode)
1413 write_seqlock(&state->seqlock);
1414 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1415 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1416 nfs_clear_open_stateid_locked(state, stateid, fmode);
1417 write_sequnlock(&state->seqlock);
1418 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1419 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1422 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1423 const nfs4_stateid *stateid, fmode_t fmode,
1424 nfs4_stateid *freeme)
1428 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1431 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1433 case FMODE_READ|FMODE_WRITE:
1434 set_bit(NFS_O_RDWR_STATE, &state->flags);
1436 if (!nfs_need_update_open_stateid(state, stateid, freeme))
1438 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1439 nfs4_stateid_copy(&state->stateid, stateid);
1440 nfs4_stateid_copy(&state->open_stateid, stateid);
1443 static void __update_open_stateid(struct nfs4_state *state,
1444 const nfs4_stateid *open_stateid,
1445 const nfs4_stateid *deleg_stateid,
1447 nfs4_stateid *freeme)
1450 * Protect the call to nfs4_state_set_mode_locked and
1451 * serialise the stateid update
1453 spin_lock(&state->owner->so_lock);
1454 write_seqlock(&state->seqlock);
1455 if (deleg_stateid != NULL) {
1456 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1457 set_bit(NFS_DELEGATED_STATE, &state->flags);
1459 if (open_stateid != NULL)
1460 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
1461 write_sequnlock(&state->seqlock);
1462 update_open_stateflags(state, fmode);
1463 spin_unlock(&state->owner->so_lock);
1466 static int update_open_stateid(struct nfs4_state *state,
1467 const nfs4_stateid *open_stateid,
1468 const nfs4_stateid *delegation,
1471 struct nfs_server *server = NFS_SERVER(state->inode);
1472 struct nfs_client *clp = server->nfs_client;
1473 struct nfs_inode *nfsi = NFS_I(state->inode);
1474 struct nfs_delegation *deleg_cur;
1475 nfs4_stateid freeme = { };
1478 fmode &= (FMODE_READ|FMODE_WRITE);
1481 deleg_cur = rcu_dereference(nfsi->delegation);
1482 if (deleg_cur == NULL)
1485 spin_lock(&deleg_cur->lock);
1486 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1487 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1488 (deleg_cur->type & fmode) != fmode)
1489 goto no_delegation_unlock;
1491 if (delegation == NULL)
1492 delegation = &deleg_cur->stateid;
1493 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1494 goto no_delegation_unlock;
1496 nfs_mark_delegation_referenced(deleg_cur);
1497 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1500 no_delegation_unlock:
1501 spin_unlock(&deleg_cur->lock);
1505 if (!ret && open_stateid != NULL) {
1506 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
1509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1510 nfs4_schedule_state_manager(clp);
1511 if (freeme.type != 0)
1512 nfs4_test_and_free_stateid(server, &freeme,
1513 state->owner->so_cred);
1518 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1519 const nfs4_stateid *stateid)
1521 struct nfs4_state *state = lsp->ls_state;
1524 spin_lock(&state->state_lock);
1525 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1527 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1529 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1532 spin_unlock(&state->state_lock);
1536 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1538 struct nfs_delegation *delegation;
1541 delegation = rcu_dereference(NFS_I(inode)->delegation);
1542 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1547 nfs4_inode_return_delegation(inode);
1550 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1552 struct nfs4_state *state = opendata->state;
1553 struct nfs_inode *nfsi = NFS_I(state->inode);
1554 struct nfs_delegation *delegation;
1555 int open_mode = opendata->o_arg.open_flags;
1556 fmode_t fmode = opendata->o_arg.fmode;
1557 enum open_claim_type4 claim = opendata->o_arg.claim;
1558 nfs4_stateid stateid;
1562 spin_lock(&state->owner->so_lock);
1563 if (can_open_cached(state, fmode, open_mode)) {
1564 update_open_stateflags(state, fmode);
1565 spin_unlock(&state->owner->so_lock);
1566 goto out_return_state;
1568 spin_unlock(&state->owner->so_lock);
1570 delegation = rcu_dereference(nfsi->delegation);
1571 if (!can_open_delegated(delegation, fmode, claim)) {
1575 /* Save the delegation */
1576 nfs4_stateid_copy(&stateid, &delegation->stateid);
1578 nfs_release_seqid(opendata->o_arg.seqid);
1579 if (!opendata->is_recover) {
1580 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1586 /* Try to update the stateid using the delegation */
1587 if (update_open_stateid(state, NULL, &stateid, fmode))
1588 goto out_return_state;
1591 return ERR_PTR(ret);
1593 atomic_inc(&state->count);
1598 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1600 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1601 struct nfs_delegation *delegation;
1602 int delegation_flags = 0;
1605 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1607 delegation_flags = delegation->flags;
1609 switch (data->o_arg.claim) {
1612 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1613 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1614 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1615 "returning a delegation for "
1616 "OPEN(CLAIM_DELEGATE_CUR)\n",
1620 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1621 nfs_inode_set_delegation(state->inode,
1622 data->owner->so_cred,
1625 nfs_inode_reclaim_delegation(state->inode,
1626 data->owner->so_cred,
1631 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1632 * and update the nfs4_state.
1634 static struct nfs4_state *
1635 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1637 struct inode *inode = data->state->inode;
1638 struct nfs4_state *state = data->state;
1641 if (!data->rpc_done) {
1642 if (data->rpc_status)
1643 return ERR_PTR(data->rpc_status);
1644 /* cached opens have already been processed */
1648 ret = nfs_refresh_inode(inode, &data->f_attr);
1650 return ERR_PTR(ret);
1652 if (data->o_res.delegation_type != 0)
1653 nfs4_opendata_check_deleg(data, state);
1655 update_open_stateid(state, &data->o_res.stateid, NULL,
1657 atomic_inc(&state->count);
1662 static struct inode *
1663 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1665 struct inode *inode;
1667 switch (data->o_arg.claim) {
1668 case NFS4_OPEN_CLAIM_NULL:
1669 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1670 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1671 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1672 return ERR_PTR(-EAGAIN);
1673 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1674 &data->f_attr, data->f_label);
1677 inode = d_inode(data->dentry);
1679 nfs_refresh_inode(inode, &data->f_attr);
1684 static struct nfs4_state *
1685 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1687 struct nfs4_state *state;
1688 struct inode *inode;
1690 inode = nfs4_opendata_get_inode(data);
1692 return ERR_CAST(inode);
1693 if (data->state != NULL && data->state->inode == inode) {
1694 state = data->state;
1695 atomic_inc(&state->count);
1697 state = nfs4_get_open_state(inode, data->owner);
1700 state = ERR_PTR(-ENOMEM);
1704 static struct nfs4_state *
1705 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1707 struct nfs4_state *state;
1709 if (!data->rpc_done) {
1710 state = nfs4_try_open_cached(data);
1711 trace_nfs4_cached_open(data->state);
1715 state = nfs4_opendata_find_nfs4_state(data);
1719 if (data->o_res.delegation_type != 0)
1720 nfs4_opendata_check_deleg(data, state);
1721 update_open_stateid(state, &data->o_res.stateid, NULL,
1724 nfs_release_seqid(data->o_arg.seqid);
1728 static struct nfs4_state *
1729 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1731 struct nfs4_state *ret;
1733 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1734 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1736 ret = _nfs4_opendata_to_nfs4_state(data);
1737 nfs4_sequence_free_slot(&data->o_res.seq_res);
1741 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1743 struct nfs_inode *nfsi = NFS_I(state->inode);
1744 struct nfs_open_context *ctx;
1746 spin_lock(&state->inode->i_lock);
1747 list_for_each_entry(ctx, &nfsi->open_files, list) {
1748 if (ctx->state != state)
1750 get_nfs_open_context(ctx);
1751 spin_unlock(&state->inode->i_lock);
1754 spin_unlock(&state->inode->i_lock);
1755 return ERR_PTR(-ENOENT);
1758 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1759 struct nfs4_state *state, enum open_claim_type4 claim)
1761 struct nfs4_opendata *opendata;
1763 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1764 NULL, NULL, claim, GFP_NOFS);
1765 if (opendata == NULL)
1766 return ERR_PTR(-ENOMEM);
1767 opendata->state = state;
1768 atomic_inc(&state->count);
1772 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1775 struct nfs4_state *newstate;
1778 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1780 opendata->o_arg.open_flags = 0;
1781 opendata->o_arg.fmode = fmode;
1782 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1783 NFS_SB(opendata->dentry->d_sb),
1785 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1786 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1787 nfs4_init_opendata_res(opendata);
1788 ret = _nfs4_recover_proc_open(opendata);
1791 newstate = nfs4_opendata_to_nfs4_state(opendata);
1792 if (IS_ERR(newstate))
1793 return PTR_ERR(newstate);
1794 if (newstate != opendata->state)
1796 nfs4_close_state(newstate, fmode);
1800 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1804 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1805 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1806 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1807 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1808 /* memory barrier prior to reading state->n_* */
1809 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1810 clear_bit(NFS_OPEN_STATE, &state->flags);
1812 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1815 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1818 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1822 * We may have performed cached opens for all three recoveries.
1823 * Check if we need to update the current stateid.
1825 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1826 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1827 write_seqlock(&state->seqlock);
1828 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1829 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1830 write_sequnlock(&state->seqlock);
1837 * reclaim state on the server after a reboot.
1839 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1841 struct nfs_delegation *delegation;
1842 struct nfs4_opendata *opendata;
1843 fmode_t delegation_type = 0;
1846 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1847 NFS4_OPEN_CLAIM_PREVIOUS);
1848 if (IS_ERR(opendata))
1849 return PTR_ERR(opendata);
1851 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1852 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1853 delegation_type = delegation->type;
1855 opendata->o_arg.u.delegation_type = delegation_type;
1856 status = nfs4_open_recover(opendata, state);
1857 nfs4_opendata_put(opendata);
1861 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1863 struct nfs_server *server = NFS_SERVER(state->inode);
1864 struct nfs4_exception exception = { };
1867 err = _nfs4_do_open_reclaim(ctx, state);
1868 trace_nfs4_open_reclaim(ctx, 0, err);
1869 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1871 if (err != -NFS4ERR_DELAY)
1873 nfs4_handle_exception(server, err, &exception);
1874 } while (exception.retry);
1878 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1880 struct nfs_open_context *ctx;
1883 ctx = nfs4_state_find_open_context(state);
1886 ret = nfs4_do_open_reclaim(ctx, state);
1887 put_nfs_open_context(ctx);
1891 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1895 printk(KERN_ERR "NFS: %s: unhandled error "
1896 "%d.\n", __func__, err);
1902 case -NFS4ERR_BADSESSION:
1903 case -NFS4ERR_BADSLOT:
1904 case -NFS4ERR_BAD_HIGH_SLOT:
1905 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1906 case -NFS4ERR_DEADSESSION:
1907 set_bit(NFS_DELEGATED_STATE, &state->flags);
1908 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1910 case -NFS4ERR_STALE_CLIENTID:
1911 case -NFS4ERR_STALE_STATEID:
1912 set_bit(NFS_DELEGATED_STATE, &state->flags);
1913 /* Don't recall a delegation if it was lost */
1914 nfs4_schedule_lease_recovery(server->nfs_client);
1916 case -NFS4ERR_MOVED:
1917 nfs4_schedule_migration_recovery(server);
1919 case -NFS4ERR_LEASE_MOVED:
1920 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1922 case -NFS4ERR_DELEG_REVOKED:
1923 case -NFS4ERR_ADMIN_REVOKED:
1924 case -NFS4ERR_EXPIRED:
1925 case -NFS4ERR_BAD_STATEID:
1926 case -NFS4ERR_OPENMODE:
1927 nfs_inode_find_state_and_recover(state->inode,
1929 nfs4_schedule_stateid_recovery(server, state);
1931 case -NFS4ERR_DELAY:
1932 case -NFS4ERR_GRACE:
1933 set_bit(NFS_DELEGATED_STATE, &state->flags);
1937 case -NFS4ERR_DENIED:
1938 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1944 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1945 struct nfs4_state *state, const nfs4_stateid *stateid,
1948 struct nfs_server *server = NFS_SERVER(state->inode);
1949 struct nfs4_opendata *opendata;
1952 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1953 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1954 if (IS_ERR(opendata))
1955 return PTR_ERR(opendata);
1956 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1957 write_seqlock(&state->seqlock);
1958 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1959 write_sequnlock(&state->seqlock);
1960 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1961 switch (type & (FMODE_READ|FMODE_WRITE)) {
1962 case FMODE_READ|FMODE_WRITE:
1964 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1967 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1971 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1973 nfs4_opendata_put(opendata);
1974 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1977 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1979 struct nfs4_opendata *data = calldata;
1981 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1982 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1985 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1987 struct nfs4_opendata *data = calldata;
1989 nfs40_sequence_done(task, &data->c_res.seq_res);
1991 data->rpc_status = task->tk_status;
1992 if (data->rpc_status == 0) {
1993 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1994 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1995 renew_lease(data->o_res.server, data->timestamp);
1996 data->rpc_done = true;
2000 static void nfs4_open_confirm_release(void *calldata)
2002 struct nfs4_opendata *data = calldata;
2003 struct nfs4_state *state = NULL;
2005 /* If this request hasn't been cancelled, do nothing */
2006 if (!data->cancelled)
2008 /* In case of error, no cleanup! */
2009 if (!data->rpc_done)
2011 state = nfs4_opendata_to_nfs4_state(data);
2013 nfs4_close_state(state, data->o_arg.fmode);
2015 nfs4_opendata_put(data);
2018 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2019 .rpc_call_prepare = nfs4_open_confirm_prepare,
2020 .rpc_call_done = nfs4_open_confirm_done,
2021 .rpc_release = nfs4_open_confirm_release,
2025 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2027 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2029 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2030 struct rpc_task *task;
2031 struct rpc_message msg = {
2032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2033 .rpc_argp = &data->c_arg,
2034 .rpc_resp = &data->c_res,
2035 .rpc_cred = data->owner->so_cred,
2037 struct rpc_task_setup task_setup_data = {
2038 .rpc_client = server->client,
2039 .rpc_message = &msg,
2040 .callback_ops = &nfs4_open_confirm_ops,
2041 .callback_data = data,
2042 .workqueue = nfsiod_workqueue,
2043 .flags = RPC_TASK_ASYNC,
2047 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2048 kref_get(&data->kref);
2049 data->rpc_done = false;
2050 data->rpc_status = 0;
2051 data->timestamp = jiffies;
2052 if (data->is_recover)
2053 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2054 task = rpc_run_task(&task_setup_data);
2056 return PTR_ERR(task);
2057 status = rpc_wait_for_completion_task(task);
2059 data->cancelled = true;
2062 status = data->rpc_status;
2067 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2069 struct nfs4_opendata *data = calldata;
2070 struct nfs4_state_owner *sp = data->owner;
2071 struct nfs_client *clp = sp->so_server->nfs_client;
2072 enum open_claim_type4 claim = data->o_arg.claim;
2074 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2077 * Check if we still need to send an OPEN call, or if we can use
2078 * a delegation instead.
2080 if (data->state != NULL) {
2081 struct nfs_delegation *delegation;
2083 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2086 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2087 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2088 goto unlock_no_action;
2091 /* Update client id. */
2092 data->o_arg.clientid = clp->cl_clientid;
2096 case NFS4_OPEN_CLAIM_PREVIOUS:
2097 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2098 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2099 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2100 case NFS4_OPEN_CLAIM_FH:
2101 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2103 data->timestamp = jiffies;
2104 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2105 &data->o_arg.seq_args,
2106 &data->o_res.seq_res,
2108 nfs_release_seqid(data->o_arg.seqid);
2110 /* Set the create mode (note dependency on the session type) */
2111 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2112 if (data->o_arg.open_flags & O_EXCL) {
2113 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2114 if (nfs4_has_persistent_session(clp))
2115 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2116 else if (clp->cl_mvops->minor_version > 0)
2117 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2121 trace_nfs4_cached_open(data->state);
2124 task->tk_action = NULL;
2126 nfs4_sequence_done(task, &data->o_res.seq_res);
2129 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2131 struct nfs4_opendata *data = calldata;
2133 data->rpc_status = task->tk_status;
2135 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2138 if (task->tk_status == 0) {
2139 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2140 switch (data->o_res.f_attr->mode & S_IFMT) {
2144 data->rpc_status = -ELOOP;
2147 data->rpc_status = -EISDIR;
2150 data->rpc_status = -ENOTDIR;
2153 renew_lease(data->o_res.server, data->timestamp);
2154 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2155 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2157 data->rpc_done = true;
2160 static void nfs4_open_release(void *calldata)
2162 struct nfs4_opendata *data = calldata;
2163 struct nfs4_state *state = NULL;
2165 /* If this request hasn't been cancelled, do nothing */
2166 if (!data->cancelled)
2168 /* In case of error, no cleanup! */
2169 if (data->rpc_status != 0 || !data->rpc_done)
2171 /* In case we need an open_confirm, no cleanup! */
2172 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2174 state = nfs4_opendata_to_nfs4_state(data);
2176 nfs4_close_state(state, data->o_arg.fmode);
2178 nfs4_opendata_put(data);
2181 static const struct rpc_call_ops nfs4_open_ops = {
2182 .rpc_call_prepare = nfs4_open_prepare,
2183 .rpc_call_done = nfs4_open_done,
2184 .rpc_release = nfs4_open_release,
2187 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2189 struct inode *dir = d_inode(data->dir);
2190 struct nfs_server *server = NFS_SERVER(dir);
2191 struct nfs_openargs *o_arg = &data->o_arg;
2192 struct nfs_openres *o_res = &data->o_res;
2193 struct rpc_task *task;
2194 struct rpc_message msg = {
2195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2198 .rpc_cred = data->owner->so_cred,
2200 struct rpc_task_setup task_setup_data = {
2201 .rpc_client = server->client,
2202 .rpc_message = &msg,
2203 .callback_ops = &nfs4_open_ops,
2204 .callback_data = data,
2205 .workqueue = nfsiod_workqueue,
2206 .flags = RPC_TASK_ASYNC,
2210 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2211 kref_get(&data->kref);
2212 data->rpc_done = false;
2213 data->rpc_status = 0;
2214 data->cancelled = false;
2215 data->is_recover = false;
2217 nfs4_set_sequence_privileged(&o_arg->seq_args);
2218 data->is_recover = true;
2220 task = rpc_run_task(&task_setup_data);
2222 return PTR_ERR(task);
2223 status = rpc_wait_for_completion_task(task);
2225 data->cancelled = true;
2228 status = data->rpc_status;
2234 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2236 struct inode *dir = d_inode(data->dir);
2237 struct nfs_openres *o_res = &data->o_res;
2240 status = nfs4_run_open_task(data, 1);
2241 if (status != 0 || !data->rpc_done)
2244 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2246 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2247 status = _nfs4_proc_open_confirm(data);
2253 * Additional permission checks in order to distinguish between an
2254 * open for read, and an open for execute. This works around the
2255 * fact that NFSv4 OPEN treats read and execute permissions as being
2257 * Note that in the non-execute case, we want to turn off permission
2258 * checking if we just created a new file (POSIX open() semantics).
2260 static int nfs4_opendata_access(struct rpc_cred *cred,
2261 struct nfs4_opendata *opendata,
2262 struct nfs4_state *state, fmode_t fmode,
2265 struct nfs_access_entry cache;
2268 /* access call failed or for some reason the server doesn't
2269 * support any access modes -- defer access call until later */
2270 if (opendata->o_res.access_supported == 0)
2275 * Use openflags to check for exec, because fmode won't
2276 * always have FMODE_EXEC set when file open for exec.
2278 if (openflags & __FMODE_EXEC) {
2279 /* ONLY check for exec rights */
2280 if (S_ISDIR(state->inode->i_mode))
2281 mask = NFS4_ACCESS_LOOKUP;
2283 mask = NFS4_ACCESS_EXECUTE;
2284 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2285 mask = NFS4_ACCESS_READ;
2288 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2289 nfs_access_add_cache(state->inode, &cache);
2291 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2292 if ((mask & ~cache.mask & flags) == 0)
2299 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2301 static int _nfs4_proc_open(struct nfs4_opendata *data)
2303 struct inode *dir = d_inode(data->dir);
2304 struct nfs_server *server = NFS_SERVER(dir);
2305 struct nfs_openargs *o_arg = &data->o_arg;
2306 struct nfs_openres *o_res = &data->o_res;
2309 status = nfs4_run_open_task(data, 0);
2310 if (!data->rpc_done)
2313 if (status == -NFS4ERR_BADNAME &&
2314 !(o_arg->open_flags & O_CREAT))
2319 nfs_fattr_map_and_free_names(server, &data->f_attr);
2321 if (o_arg->open_flags & O_CREAT) {
2322 if (o_arg->open_flags & O_EXCL)
2323 data->file_created = true;
2324 else if (o_res->cinfo.before != o_res->cinfo.after)
2325 data->file_created = true;
2326 if (data->file_created || dir->i_version != o_res->cinfo.after)
2327 update_changeattr(dir, &o_res->cinfo,
2328 o_res->f_attr->time_start);
2330 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2331 server->caps &= ~NFS_CAP_POSIX_LOCK;
2332 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2333 status = _nfs4_proc_open_confirm(data);
2337 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2338 nfs4_sequence_free_slot(&o_res->seq_res);
2339 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2346 * reclaim state on the server after a network partition.
2347 * Assumes caller holds the appropriate lock
2349 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2351 struct nfs4_opendata *opendata;
2354 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2355 NFS4_OPEN_CLAIM_FH);
2356 if (IS_ERR(opendata))
2357 return PTR_ERR(opendata);
2358 ret = nfs4_open_recover(opendata, state);
2360 d_drop(ctx->dentry);
2361 nfs4_opendata_put(opendata);
2365 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2367 struct nfs_server *server = NFS_SERVER(state->inode);
2368 struct nfs4_exception exception = { };
2372 err = _nfs4_open_expired(ctx, state);
2373 trace_nfs4_open_expired(ctx, 0, err);
2374 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2379 case -NFS4ERR_GRACE:
2380 case -NFS4ERR_DELAY:
2381 nfs4_handle_exception(server, err, &exception);
2384 } while (exception.retry);
2389 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2391 struct nfs_open_context *ctx;
2394 ctx = nfs4_state_find_open_context(state);
2397 ret = nfs4_do_open_expired(ctx, state);
2398 put_nfs_open_context(ctx);
2402 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2403 const nfs4_stateid *stateid)
2405 nfs_remove_bad_delegation(state->inode, stateid);
2406 write_seqlock(&state->seqlock);
2407 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2408 write_sequnlock(&state->seqlock);
2409 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2412 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2414 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2415 nfs_finish_clear_delegation_stateid(state, NULL);
2418 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2420 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2421 nfs40_clear_delegation_stateid(state);
2422 return nfs4_open_expired(sp, state);
2425 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2426 nfs4_stateid *stateid,
2427 struct rpc_cred *cred)
2429 return -NFS4ERR_BAD_STATEID;
2432 #if defined(CONFIG_NFS_V4_1)
2433 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2434 nfs4_stateid *stateid,
2435 struct rpc_cred *cred)
2439 switch (stateid->type) {
2442 case NFS4_INVALID_STATEID_TYPE:
2443 case NFS4_SPECIAL_STATEID_TYPE:
2444 return -NFS4ERR_BAD_STATEID;
2445 case NFS4_REVOKED_STATEID_TYPE:
2449 status = nfs41_test_stateid(server, stateid, cred);
2451 case -NFS4ERR_EXPIRED:
2452 case -NFS4ERR_ADMIN_REVOKED:
2453 case -NFS4ERR_DELEG_REVOKED:
2459 /* Ack the revoked state to the server */
2460 nfs41_free_stateid(server, stateid, cred, true);
2461 return -NFS4ERR_EXPIRED;
2464 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2466 struct nfs_server *server = NFS_SERVER(state->inode);
2467 nfs4_stateid stateid;
2468 struct nfs_delegation *delegation;
2469 struct rpc_cred *cred;
2472 /* Get the delegation credential for use by test/free_stateid */
2474 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2475 if (delegation == NULL) {
2480 nfs4_stateid_copy(&stateid, &delegation->stateid);
2481 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2482 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2483 &delegation->flags)) {
2485 nfs_finish_clear_delegation_stateid(state, &stateid);
2489 cred = get_rpccred(delegation->cred);
2491 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2492 trace_nfs4_test_delegation_stateid(state, NULL, status);
2493 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2494 nfs_finish_clear_delegation_stateid(state, &stateid);
2500 * nfs41_check_expired_locks - possibly free a lock stateid
2502 * @state: NFSv4 state for an inode
2504 * Returns NFS_OK if recovery for this stateid is now finished.
2505 * Otherwise a negative NFS4ERR value is returned.
2507 static int nfs41_check_expired_locks(struct nfs4_state *state)
2509 int status, ret = NFS_OK;
2510 struct nfs4_lock_state *lsp, *prev = NULL;
2511 struct nfs_server *server = NFS_SERVER(state->inode);
2513 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2516 spin_lock(&state->state_lock);
2517 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2518 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2519 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2521 atomic_inc(&lsp->ls_count);
2522 spin_unlock(&state->state_lock);
2524 nfs4_put_lock_state(prev);
2527 status = nfs41_test_and_free_expired_stateid(server,
2530 trace_nfs4_test_lock_stateid(state, lsp, status);
2531 if (status == -NFS4ERR_EXPIRED ||
2532 status == -NFS4ERR_BAD_STATEID) {
2533 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2534 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2535 if (!recover_lost_locks)
2536 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2537 } else if (status != NFS_OK) {
2539 nfs4_put_lock_state(prev);
2542 spin_lock(&state->state_lock);
2545 spin_unlock(&state->state_lock);
2546 nfs4_put_lock_state(prev);
2552 * nfs41_check_open_stateid - possibly free an open stateid
2554 * @state: NFSv4 state for an inode
2556 * Returns NFS_OK if recovery for this stateid is now finished.
2557 * Otherwise a negative NFS4ERR value is returned.
2559 static int nfs41_check_open_stateid(struct nfs4_state *state)
2561 struct nfs_server *server = NFS_SERVER(state->inode);
2562 nfs4_stateid *stateid = &state->open_stateid;
2563 struct rpc_cred *cred = state->owner->so_cred;
2566 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2567 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2568 if (nfs4_have_delegation(state->inode, state->state))
2570 return -NFS4ERR_OPENMODE;
2572 return -NFS4ERR_BAD_STATEID;
2574 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2575 trace_nfs4_test_open_stateid(state, NULL, status);
2576 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2577 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2578 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2579 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2580 clear_bit(NFS_OPEN_STATE, &state->flags);
2581 stateid->type = NFS4_INVALID_STATEID_TYPE;
2584 if (nfs_open_stateid_recover_openmode(state))
2585 return -NFS4ERR_OPENMODE;
2589 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2593 nfs41_check_delegation_stateid(state);
2594 status = nfs41_check_expired_locks(state);
2595 if (status != NFS_OK)
2597 status = nfs41_check_open_stateid(state);
2598 if (status != NFS_OK)
2599 status = nfs4_open_expired(sp, state);
2605 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2606 * fields corresponding to attributes that were used to store the verifier.
2607 * Make sure we clobber those fields in the later setattr call
2609 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2610 struct iattr *sattr, struct nfs4_label **label)
2612 const u32 *attrset = opendata->o_res.attrset;
2614 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2615 !(sattr->ia_valid & ATTR_ATIME_SET))
2616 sattr->ia_valid |= ATTR_ATIME;
2618 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2619 !(sattr->ia_valid & ATTR_MTIME_SET))
2620 sattr->ia_valid |= ATTR_MTIME;
2622 /* Except MODE, it seems harmless of setting twice. */
2623 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2624 (attrset[1] & FATTR4_WORD1_MODE ||
2625 attrset[2] & FATTR4_WORD2_MODE_UMASK))
2626 sattr->ia_valid &= ~ATTR_MODE;
2628 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2632 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2635 struct nfs_open_context *ctx)
2637 struct nfs4_state_owner *sp = opendata->owner;
2638 struct nfs_server *server = sp->so_server;
2639 struct dentry *dentry;
2640 struct nfs4_state *state;
2644 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2646 ret = _nfs4_proc_open(opendata);
2650 state = nfs4_opendata_to_nfs4_state(opendata);
2651 ret = PTR_ERR(state);
2655 if (server->caps & NFS_CAP_POSIX_LOCK)
2656 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2657 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2658 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2660 dentry = opendata->dentry;
2661 if (d_really_is_negative(dentry)) {
2662 struct dentry *alias;
2664 alias = d_exact_alias(dentry, state->inode);
2666 alias = d_splice_alias(igrab(state->inode), dentry);
2667 /* d_splice_alias() can't fail here - it's a non-directory */
2670 ctx->dentry = dentry = alias;
2672 nfs_set_verifier(dentry,
2673 nfs_save_change_attribute(d_inode(opendata->dir)));
2676 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2680 if (d_inode(dentry) == state->inode) {
2681 nfs_inode_attach_open_context(ctx);
2682 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2683 nfs4_schedule_stateid_recovery(server, state);
2690 * Returns a referenced nfs4_state
2692 static int _nfs4_do_open(struct inode *dir,
2693 struct nfs_open_context *ctx,
2695 struct iattr *sattr,
2696 struct nfs4_label *label,
2699 struct nfs4_state_owner *sp;
2700 struct nfs4_state *state = NULL;
2701 struct nfs_server *server = NFS_SERVER(dir);
2702 struct nfs4_opendata *opendata;
2703 struct dentry *dentry = ctx->dentry;
2704 struct rpc_cred *cred = ctx->cred;
2705 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2706 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2707 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2708 struct nfs4_label *olabel = NULL;
2711 /* Protect against reboot recovery conflicts */
2713 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2715 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2718 status = nfs4_client_recover_expired_lease(server->nfs_client);
2720 goto err_put_state_owner;
2721 if (d_really_is_positive(dentry))
2722 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2724 if (d_really_is_positive(dentry))
2725 claim = NFS4_OPEN_CLAIM_FH;
2726 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2727 label, claim, GFP_KERNEL);
2728 if (opendata == NULL)
2729 goto err_put_state_owner;
2732 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2733 if (IS_ERR(olabel)) {
2734 status = PTR_ERR(olabel);
2735 goto err_opendata_put;
2739 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2740 if (!opendata->f_attr.mdsthreshold) {
2741 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2742 if (!opendata->f_attr.mdsthreshold)
2743 goto err_free_label;
2745 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2747 if (d_really_is_positive(dentry))
2748 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2750 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2752 goto err_free_label;
2755 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2756 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2757 nfs4_exclusive_attrset(opendata, sattr, &label);
2759 * send create attributes which was not set by open
2760 * with an extra setattr.
2762 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2763 nfs_fattr_init(opendata->o_res.f_attr);
2764 status = nfs4_do_setattr(state->inode, cred,
2765 opendata->o_res.f_attr, sattr,
2766 ctx, label, olabel);
2768 nfs_setattr_update_inode(state->inode, sattr,
2769 opendata->o_res.f_attr);
2770 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2774 if (opened && opendata->file_created)
2775 *opened |= FILE_CREATED;
2777 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2778 *ctx_th = opendata->f_attr.mdsthreshold;
2779 opendata->f_attr.mdsthreshold = NULL;
2782 nfs4_label_free(olabel);
2784 nfs4_opendata_put(opendata);
2785 nfs4_put_state_owner(sp);
2788 nfs4_label_free(olabel);
2790 nfs4_opendata_put(opendata);
2791 err_put_state_owner:
2792 nfs4_put_state_owner(sp);
2798 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2799 struct nfs_open_context *ctx,
2801 struct iattr *sattr,
2802 struct nfs4_label *label,
2805 struct nfs_server *server = NFS_SERVER(dir);
2806 struct nfs4_exception exception = { };
2807 struct nfs4_state *res;
2811 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2813 trace_nfs4_open_file(ctx, flags, status);
2816 /* NOTE: BAD_SEQID means the server and client disagree about the
2817 * book-keeping w.r.t. state-changing operations
2818 * (OPEN/CLOSE/LOCK/LOCKU...)
2819 * It is actually a sign of a bug on the client or on the server.
2821 * If we receive a BAD_SEQID error in the particular case of
2822 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2823 * have unhashed the old state_owner for us, and that we can
2824 * therefore safely retry using a new one. We should still warn
2825 * the user though...
2827 if (status == -NFS4ERR_BAD_SEQID) {
2828 pr_warn_ratelimited("NFS: v4 server %s "
2829 " returned a bad sequence-id error!\n",
2830 NFS_SERVER(dir)->nfs_client->cl_hostname);
2831 exception.retry = 1;
2835 * BAD_STATEID on OPEN means that the server cancelled our
2836 * state before it received the OPEN_CONFIRM.
2837 * Recover by retrying the request as per the discussion
2838 * on Page 181 of RFC3530.
2840 if (status == -NFS4ERR_BAD_STATEID) {
2841 exception.retry = 1;
2844 if (status == -EAGAIN) {
2845 /* We must have found a delegation */
2846 exception.retry = 1;
2849 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2851 res = ERR_PTR(nfs4_handle_exception(server,
2852 status, &exception));
2853 } while (exception.retry);
2857 static int _nfs4_do_setattr(struct inode *inode,
2858 struct nfs_setattrargs *arg,
2859 struct nfs_setattrres *res,
2860 struct rpc_cred *cred,
2861 struct nfs_open_context *ctx)
2863 struct nfs_server *server = NFS_SERVER(inode);
2864 struct rpc_message msg = {
2865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2870 struct rpc_cred *delegation_cred = NULL;
2871 unsigned long timestamp = jiffies;
2876 nfs_fattr_init(res->fattr);
2878 /* Servers should only apply open mode checks for file size changes */
2879 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2880 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2882 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2883 /* Use that stateid */
2884 } else if (truncate && ctx != NULL) {
2885 struct nfs_lock_context *l_ctx;
2886 if (!nfs4_valid_open_stateid(ctx->state))
2888 l_ctx = nfs_get_lock_context(ctx);
2890 return PTR_ERR(l_ctx);
2891 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2892 &arg->stateid, &delegation_cred);
2893 nfs_put_lock_context(l_ctx);
2897 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2898 if (delegation_cred)
2899 msg.rpc_cred = delegation_cred;
2901 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2903 put_rpccred(delegation_cred);
2904 if (status == 0 && ctx != NULL)
2905 renew_lease(server, timestamp);
2906 trace_nfs4_setattr(inode, &arg->stateid, status);
2910 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2911 struct nfs_fattr *fattr, struct iattr *sattr,
2912 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
2913 struct nfs4_label *olabel)
2915 struct nfs_server *server = NFS_SERVER(inode);
2916 struct nfs4_state *state = ctx ? ctx->state : NULL;
2917 struct nfs_setattrargs arg = {
2918 .fh = NFS_FH(inode),
2921 .bitmask = server->attr_bitmask,
2924 struct nfs_setattrres res = {
2929 struct nfs4_exception exception = {
2932 .stateid = &arg.stateid,
2936 arg.bitmask = nfs4_bitmask(server, ilabel);
2938 arg.bitmask = nfs4_bitmask(server, olabel);
2941 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
2943 case -NFS4ERR_OPENMODE:
2944 if (!(sattr->ia_valid & ATTR_SIZE)) {
2945 pr_warn_once("NFSv4: server %s is incorrectly "
2946 "applying open mode checks to "
2947 "a SETATTR that is not "
2948 "changing file size.\n",
2949 server->nfs_client->cl_hostname);
2951 if (state && !(state->state & FMODE_WRITE)) {
2953 if (sattr->ia_valid & ATTR_OPEN)
2958 err = nfs4_handle_exception(server, err, &exception);
2959 } while (exception.retry);
2965 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2967 if (inode == NULL || !nfs_have_layout(inode))
2970 return pnfs_wait_on_layoutreturn(inode, task);
2973 struct nfs4_closedata {
2974 struct inode *inode;
2975 struct nfs4_state *state;
2976 struct nfs_closeargs arg;
2977 struct nfs_closeres res;
2979 struct nfs4_layoutreturn_args arg;
2980 struct nfs4_layoutreturn_res res;
2981 struct nfs4_xdr_opaque_data ld_private;
2985 struct nfs_fattr fattr;
2986 unsigned long timestamp;
2989 static void nfs4_free_closedata(void *data)
2991 struct nfs4_closedata *calldata = data;
2992 struct nfs4_state_owner *sp = calldata->state->owner;
2993 struct super_block *sb = calldata->state->inode->i_sb;
2995 if (calldata->lr.roc)
2996 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2997 calldata->res.lr_ret);
2998 nfs4_put_open_state(calldata->state);
2999 nfs_free_seqid(calldata->arg.seqid);
3000 nfs4_put_state_owner(sp);
3001 nfs_sb_deactive(sb);
3005 static void nfs4_close_done(struct rpc_task *task, void *data)
3007 struct nfs4_closedata *calldata = data;
3008 struct nfs4_state *state = calldata->state;
3009 struct nfs_server *server = NFS_SERVER(calldata->inode);
3010 nfs4_stateid *res_stateid = NULL;
3012 dprintk("%s: begin!\n", __func__);
3013 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3015 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3017 /* Handle Layoutreturn errors */
3018 if (calldata->arg.lr_args && task->tk_status != 0) {
3019 switch (calldata->res.lr_ret) {
3021 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3024 calldata->arg.lr_args = NULL;
3025 calldata->res.lr_res = NULL;
3027 case -NFS4ERR_ADMIN_REVOKED:
3028 case -NFS4ERR_DELEG_REVOKED:
3029 case -NFS4ERR_EXPIRED:
3030 case -NFS4ERR_BAD_STATEID:
3031 case -NFS4ERR_OLD_STATEID:
3032 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3033 case -NFS4ERR_WRONG_CRED:
3034 calldata->arg.lr_args = NULL;
3035 calldata->res.lr_res = NULL;
3036 calldata->res.lr_ret = 0;
3037 rpc_restart_call_prepare(task);
3042 /* hmm. we are done with the inode, and in the process of freeing
3043 * the state_owner. we keep this around to process errors
3045 switch (task->tk_status) {
3047 res_stateid = &calldata->res.stateid;
3048 renew_lease(server, calldata->timestamp);
3050 case -NFS4ERR_ACCESS:
3051 if (calldata->arg.bitmask != NULL) {
3052 calldata->arg.bitmask = NULL;
3053 calldata->res.fattr = NULL;
3054 task->tk_status = 0;
3055 rpc_restart_call_prepare(task);
3060 case -NFS4ERR_ADMIN_REVOKED:
3061 case -NFS4ERR_STALE_STATEID:
3062 case -NFS4ERR_EXPIRED:
3063 nfs4_free_revoked_stateid(server,
3064 &calldata->arg.stateid,
3065 task->tk_msg.rpc_cred);
3066 case -NFS4ERR_OLD_STATEID:
3067 case -NFS4ERR_BAD_STATEID:
3068 if (!nfs4_stateid_match(&calldata->arg.stateid,
3069 &state->open_stateid)) {
3070 rpc_restart_call_prepare(task);
3073 if (calldata->arg.fmode == 0)
3076 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3077 rpc_restart_call_prepare(task);
3081 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3082 res_stateid, calldata->arg.fmode);
3084 nfs_release_seqid(calldata->arg.seqid);
3085 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3086 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3089 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3091 struct nfs4_closedata *calldata = data;
3092 struct nfs4_state *state = calldata->state;
3093 struct inode *inode = calldata->inode;
3094 bool is_rdonly, is_wronly, is_rdwr;
3097 dprintk("%s: begin!\n", __func__);
3098 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3101 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3102 spin_lock(&state->owner->so_lock);
3103 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3104 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3105 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3106 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3107 /* Calculate the change in open mode */
3108 calldata->arg.fmode = 0;
3109 if (state->n_rdwr == 0) {
3110 if (state->n_rdonly == 0)
3111 call_close |= is_rdonly;
3113 calldata->arg.fmode |= FMODE_READ;
3114 if (state->n_wronly == 0)
3115 call_close |= is_wronly;
3117 calldata->arg.fmode |= FMODE_WRITE;
3118 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3119 call_close |= is_rdwr;
3121 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3123 if (!nfs4_valid_open_stateid(state) ||
3124 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3126 spin_unlock(&state->owner->so_lock);
3129 /* Note: exit _without_ calling nfs4_close_done */
3133 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3134 nfs_release_seqid(calldata->arg.seqid);
3138 if (calldata->arg.fmode == 0)
3139 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3141 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3142 /* Close-to-open cache consistency revalidation */
3143 if (!nfs4_have_delegation(inode, FMODE_READ))
3144 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3146 calldata->arg.bitmask = NULL;
3149 calldata->arg.share_access =
3150 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3151 calldata->arg.fmode, 0);
3153 if (calldata->res.fattr == NULL)
3154 calldata->arg.bitmask = NULL;
3155 else if (calldata->arg.bitmask == NULL)
3156 calldata->res.fattr = NULL;
3157 calldata->timestamp = jiffies;
3158 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3159 &calldata->arg.seq_args,
3160 &calldata->res.seq_res,
3162 nfs_release_seqid(calldata->arg.seqid);
3163 dprintk("%s: done!\n", __func__);
3166 task->tk_action = NULL;
3168 nfs4_sequence_done(task, &calldata->res.seq_res);
3171 static const struct rpc_call_ops nfs4_close_ops = {
3172 .rpc_call_prepare = nfs4_close_prepare,
3173 .rpc_call_done = nfs4_close_done,
3174 .rpc_release = nfs4_free_closedata,
3178 * It is possible for data to be read/written from a mem-mapped file
3179 * after the sys_close call (which hits the vfs layer as a flush).
3180 * This means that we can't safely call nfsv4 close on a file until
3181 * the inode is cleared. This in turn means that we are not good
3182 * NFSv4 citizens - we do not indicate to the server to update the file's
3183 * share state even when we are done with one of the three share
3184 * stateid's in the inode.
3186 * NOTE: Caller must be holding the sp->so_owner semaphore!
3188 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3190 struct nfs_server *server = NFS_SERVER(state->inode);
3191 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3192 struct nfs4_closedata *calldata;
3193 struct nfs4_state_owner *sp = state->owner;
3194 struct rpc_task *task;
3195 struct rpc_message msg = {
3196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3197 .rpc_cred = state->owner->so_cred,
3199 struct rpc_task_setup task_setup_data = {
3200 .rpc_client = server->client,
3201 .rpc_message = &msg,
3202 .callback_ops = &nfs4_close_ops,
3203 .workqueue = nfsiod_workqueue,
3204 .flags = RPC_TASK_ASYNC,
3206 int status = -ENOMEM;
3208 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3209 &task_setup_data.rpc_client, &msg);
3211 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3212 if (calldata == NULL)
3214 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3215 calldata->inode = state->inode;
3216 calldata->state = state;
3217 calldata->arg.fh = NFS_FH(state->inode);
3218 /* Serialization for the sequence id */
3219 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3220 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3221 if (IS_ERR(calldata->arg.seqid))
3222 goto out_free_calldata;
3223 nfs_fattr_init(&calldata->fattr);
3224 calldata->arg.fmode = 0;
3225 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3226 calldata->res.fattr = &calldata->fattr;
3227 calldata->res.seqid = calldata->arg.seqid;
3228 calldata->res.server = server;
3229 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3230 calldata->lr.roc = pnfs_roc(state->inode,
3231 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3232 if (calldata->lr.roc) {
3233 calldata->arg.lr_args = &calldata->lr.arg;
3234 calldata->res.lr_res = &calldata->lr.res;
3236 nfs_sb_active(calldata->inode->i_sb);
3238 msg.rpc_argp = &calldata->arg;
3239 msg.rpc_resp = &calldata->res;
3240 task_setup_data.callback_data = calldata;
3241 task = rpc_run_task(&task_setup_data);
3243 return PTR_ERR(task);
3246 status = rpc_wait_for_completion_task(task);
3252 nfs4_put_open_state(state);
3253 nfs4_put_state_owner(sp);
3257 static struct inode *
3258 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3259 int open_flags, struct iattr *attr, int *opened)
3261 struct nfs4_state *state;
3262 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3264 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3266 /* Protect against concurrent sillydeletes */
3267 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3269 nfs4_label_release_security(label);
3272 return ERR_CAST(state);
3273 return state->inode;
3276 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3278 if (ctx->state == NULL)
3281 nfs4_close_sync(ctx->state, ctx->mode);
3283 nfs4_close_state(ctx->state, ctx->mode);
3286 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3287 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3288 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3290 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3292 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3293 struct nfs4_server_caps_arg args = {
3297 struct nfs4_server_caps_res res = {};
3298 struct rpc_message msg = {
3299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3306 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3307 FATTR4_WORD0_FH_EXPIRE_TYPE |
3308 FATTR4_WORD0_LINK_SUPPORT |
3309 FATTR4_WORD0_SYMLINK_SUPPORT |
3310 FATTR4_WORD0_ACLSUPPORT;
3312 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3314 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3316 /* Sanity check the server answers */
3317 switch (minorversion) {
3319 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3320 res.attr_bitmask[2] = 0;
3323 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3326 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3328 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3329 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3330 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3331 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3332 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3333 NFS_CAP_CTIME|NFS_CAP_MTIME|
3334 NFS_CAP_SECURITY_LABEL);
3335 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3336 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3337 server->caps |= NFS_CAP_ACLS;
3338 if (res.has_links != 0)
3339 server->caps |= NFS_CAP_HARDLINKS;
3340 if (res.has_symlinks != 0)
3341 server->caps |= NFS_CAP_SYMLINKS;
3342 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3343 server->caps |= NFS_CAP_FILEID;
3344 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3345 server->caps |= NFS_CAP_MODE;
3346 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3347 server->caps |= NFS_CAP_NLINK;
3348 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3349 server->caps |= NFS_CAP_OWNER;
3350 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3351 server->caps |= NFS_CAP_OWNER_GROUP;
3352 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3353 server->caps |= NFS_CAP_ATIME;
3354 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3355 server->caps |= NFS_CAP_CTIME;
3356 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3357 server->caps |= NFS_CAP_MTIME;
3358 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3359 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3360 server->caps |= NFS_CAP_SECURITY_LABEL;
3362 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3363 sizeof(server->attr_bitmask));
3364 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3366 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3367 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3368 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3369 server->cache_consistency_bitmask[2] = 0;
3371 /* Avoid a regression due to buggy server */
3372 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3373 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3374 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3375 sizeof(server->exclcreat_bitmask));
3377 server->acl_bitmask = res.acl_bitmask;
3378 server->fh_expire_type = res.fh_expire_type;
3384 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3386 struct nfs4_exception exception = { };
3389 err = nfs4_handle_exception(server,
3390 _nfs4_server_capabilities(server, fhandle),
3392 } while (exception.retry);
3396 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3397 struct nfs_fsinfo *info)
3400 struct nfs4_lookup_root_arg args = {
3403 struct nfs4_lookup_res res = {
3405 .fattr = info->fattr,
3408 struct rpc_message msg = {
3409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3414 bitmask[0] = nfs4_fattr_bitmap[0];
3415 bitmask[1] = nfs4_fattr_bitmap[1];
3417 * Process the label in the upcoming getfattr
3419 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3421 nfs_fattr_init(info->fattr);
3422 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3425 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3426 struct nfs_fsinfo *info)
3428 struct nfs4_exception exception = { };
3431 err = _nfs4_lookup_root(server, fhandle, info);
3432 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3435 case -NFS4ERR_WRONGSEC:
3438 err = nfs4_handle_exception(server, err, &exception);
3440 } while (exception.retry);
3445 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3446 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3448 struct rpc_auth_create_args auth_args = {
3449 .pseudoflavor = flavor,
3451 struct rpc_auth *auth;
3453 auth = rpcauth_create(&auth_args, server->client);
3456 return nfs4_lookup_root(server, fhandle, info);
3460 * Retry pseudoroot lookup with various security flavors. We do this when:
3462 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3463 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3465 * Returns zero on success, or a negative NFS4ERR value, or a
3466 * negative errno value.
3468 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3469 struct nfs_fsinfo *info)
3471 /* Per 3530bis 15.33.5 */
3472 static const rpc_authflavor_t flav_array[] = {
3476 RPC_AUTH_UNIX, /* courtesy */
3479 int status = -EPERM;
3482 if (server->auth_info.flavor_len > 0) {
3483 /* try each flavor specified by user */
3484 for (i = 0; i < server->auth_info.flavor_len; i++) {
3485 status = nfs4_lookup_root_sec(server, fhandle, info,
3486 server->auth_info.flavors[i]);
3487 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3492 /* no flavors specified by user, try default list */
3493 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3494 status = nfs4_lookup_root_sec(server, fhandle, info,
3496 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3503 * -EACCESS could mean that the user doesn't have correct permissions
3504 * to access the mount. It could also mean that we tried to mount
3505 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3506 * existing mount programs don't handle -EACCES very well so it should
3507 * be mapped to -EPERM instead.
3509 if (status == -EACCES)
3515 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3516 * @server: initialized nfs_server handle
3517 * @fhandle: we fill in the pseudo-fs root file handle
3518 * @info: we fill in an FSINFO struct
3519 * @auth_probe: probe the auth flavours
3521 * Returns zero on success, or a negative errno.
3523 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3524 struct nfs_fsinfo *info,
3530 status = nfs4_lookup_root(server, fhandle, info);
3532 if (auth_probe || status == NFS4ERR_WRONGSEC)
3533 status = server->nfs_client->cl_mvops->find_root_sec(server,
3537 status = nfs4_server_capabilities(server, fhandle);
3539 status = nfs4_do_fsinfo(server, fhandle, info);
3541 return nfs4_map_errors(status);
3544 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3545 struct nfs_fsinfo *info)
3548 struct nfs_fattr *fattr = info->fattr;
3549 struct nfs4_label *label = NULL;
3551 error = nfs4_server_capabilities(server, mntfh);
3553 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3557 label = nfs4_label_alloc(server, GFP_KERNEL);
3559 return PTR_ERR(label);
3561 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3563 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3564 goto err_free_label;
3567 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3568 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3569 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3572 nfs4_label_free(label);
3578 * Get locations and (maybe) other attributes of a referral.
3579 * Note that we'll actually follow the referral later when
3580 * we detect fsid mismatch in inode revalidation
3582 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3583 const struct qstr *name, struct nfs_fattr *fattr,
3584 struct nfs_fh *fhandle)
3586 int status = -ENOMEM;
3587 struct page *page = NULL;
3588 struct nfs4_fs_locations *locations = NULL;
3590 page = alloc_page(GFP_KERNEL);
3593 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3594 if (locations == NULL)
3597 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3602 * If the fsid didn't change, this is a migration event, not a
3603 * referral. Cause us to drop into the exception handler, which
3604 * will kick off migration recovery.
3606 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3607 dprintk("%s: server did not return a different fsid for"
3608 " a referral at %s\n", __func__, name->name);
3609 status = -NFS4ERR_MOVED;
3612 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3613 nfs_fixup_referral_attributes(&locations->fattr);
3615 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3616 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3617 memset(fhandle, 0, sizeof(struct nfs_fh));
3625 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3626 struct nfs_fattr *fattr, struct nfs4_label *label)
3628 struct nfs4_getattr_arg args = {
3630 .bitmask = server->attr_bitmask,
3632 struct nfs4_getattr_res res = {
3637 struct rpc_message msg = {
3638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3643 args.bitmask = nfs4_bitmask(server, label);
3645 nfs_fattr_init(fattr);
3646 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3649 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3650 struct nfs_fattr *fattr, struct nfs4_label *label)
3652 struct nfs4_exception exception = { };
3655 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3656 trace_nfs4_getattr(server, fhandle, fattr, err);
3657 err = nfs4_handle_exception(server, err,
3659 } while (exception.retry);
3664 * The file is not closed if it is opened due to the a request to change
3665 * the size of the file. The open call will not be needed once the
3666 * VFS layer lookup-intents are implemented.
3668 * Close is called when the inode is destroyed.
3669 * If we haven't opened the file for O_WRONLY, we
3670 * need to in the size_change case to obtain a stateid.
3673 * Because OPEN is always done by name in nfsv4, it is
3674 * possible that we opened a different file by the same
3675 * name. We can recognize this race condition, but we
3676 * can't do anything about it besides returning an error.
3678 * This will be fixed with VFS changes (lookup-intent).
3681 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3682 struct iattr *sattr)
3684 struct inode *inode = d_inode(dentry);
3685 struct rpc_cred *cred = NULL;
3686 struct nfs_open_context *ctx = NULL;
3687 struct nfs4_label *label = NULL;
3690 if (pnfs_ld_layoutret_on_setattr(inode) &&
3691 sattr->ia_valid & ATTR_SIZE &&
3692 sattr->ia_size < i_size_read(inode))
3693 pnfs_commit_and_return_layout(inode);
3695 nfs_fattr_init(fattr);
3697 /* Deal with open(O_TRUNC) */
3698 if (sattr->ia_valid & ATTR_OPEN)
3699 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3701 /* Optimization: if the end result is no change, don't RPC */
3702 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3705 /* Search for an existing open(O_WRITE) file */
3706 if (sattr->ia_valid & ATTR_FILE) {
3708 ctx = nfs_file_open_context(sattr->ia_file);
3713 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3715 return PTR_ERR(label);
3717 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3719 nfs_setattr_update_inode(inode, sattr, fattr);
3720 nfs_setsecurity(inode, fattr, label);
3722 nfs4_label_free(label);
3726 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3727 const struct qstr *name, struct nfs_fh *fhandle,
3728 struct nfs_fattr *fattr, struct nfs4_label *label)
3730 struct nfs_server *server = NFS_SERVER(dir);
3732 struct nfs4_lookup_arg args = {
3733 .bitmask = server->attr_bitmask,
3734 .dir_fh = NFS_FH(dir),
3737 struct nfs4_lookup_res res = {
3743 struct rpc_message msg = {
3744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3749 args.bitmask = nfs4_bitmask(server, label);
3751 nfs_fattr_init(fattr);
3753 dprintk("NFS call lookup %s\n", name->name);
3754 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3755 dprintk("NFS reply lookup: %d\n", status);
3759 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3761 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3762 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3763 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3767 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3768 const struct qstr *name, struct nfs_fh *fhandle,
3769 struct nfs_fattr *fattr, struct nfs4_label *label)
3771 struct nfs4_exception exception = { };
3772 struct rpc_clnt *client = *clnt;
3775 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3776 trace_nfs4_lookup(dir, name, err);
3778 case -NFS4ERR_BADNAME:
3781 case -NFS4ERR_MOVED:
3782 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3783 if (err == -NFS4ERR_MOVED)
3784 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3786 case -NFS4ERR_WRONGSEC:
3788 if (client != *clnt)
3790 client = nfs4_negotiate_security(client, dir, name);
3792 return PTR_ERR(client);
3794 exception.retry = 1;
3797 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3799 } while (exception.retry);
3804 else if (client != *clnt)
3805 rpc_shutdown_client(client);
3810 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3811 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3812 struct nfs4_label *label)
3815 struct rpc_clnt *client = NFS_CLIENT(dir);
3817 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3818 if (client != NFS_CLIENT(dir)) {
3819 rpc_shutdown_client(client);
3820 nfs_fixup_secinfo_attributes(fattr);
3826 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3827 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3829 struct rpc_clnt *client = NFS_CLIENT(dir);
3832 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3834 return ERR_PTR(status);
3835 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3838 static int _nfs4_proc_lookupp(struct inode *inode,
3839 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3840 struct nfs4_label *label)
3842 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3843 struct nfs_server *server = NFS_SERVER(inode);
3845 struct nfs4_lookupp_arg args = {
3846 .bitmask = server->attr_bitmask,
3847 .fh = NFS_FH(inode),
3849 struct nfs4_lookupp_res res = {
3855 struct rpc_message msg = {
3856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
3861 args.bitmask = nfs4_bitmask(server, label);
3863 nfs_fattr_init(fattr);
3865 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
3866 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
3868 dprintk("NFS reply lookupp: %d\n", status);
3872 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
3873 struct nfs_fattr *fattr, struct nfs4_label *label)
3875 struct nfs4_exception exception = { };
3878 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
3879 trace_nfs4_lookupp(inode, err);
3880 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3882 } while (exception.retry);
3886 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3888 struct nfs_server *server = NFS_SERVER(inode);
3889 struct nfs4_accessargs args = {
3890 .fh = NFS_FH(inode),
3891 .bitmask = server->cache_consistency_bitmask,
3893 struct nfs4_accessres res = {
3896 struct rpc_message msg = {
3897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3900 .rpc_cred = entry->cred,
3902 int mode = entry->mask;
3906 * Determine which access bits we want to ask for...
3908 if (mode & MAY_READ)
3909 args.access |= NFS4_ACCESS_READ;
3910 if (S_ISDIR(inode->i_mode)) {
3911 if (mode & MAY_WRITE)
3912 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3913 if (mode & MAY_EXEC)
3914 args.access |= NFS4_ACCESS_LOOKUP;
3916 if (mode & MAY_WRITE)
3917 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3918 if (mode & MAY_EXEC)
3919 args.access |= NFS4_ACCESS_EXECUTE;
3922 res.fattr = nfs_alloc_fattr();
3923 if (res.fattr == NULL)
3926 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3928 nfs_access_set_mask(entry, res.access);
3929 nfs_refresh_inode(inode, res.fattr);
3931 nfs_free_fattr(res.fattr);
3935 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3937 struct nfs4_exception exception = { };
3940 err = _nfs4_proc_access(inode, entry);
3941 trace_nfs4_access(inode, err);
3942 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3944 } while (exception.retry);
3949 * TODO: For the time being, we don't try to get any attributes
3950 * along with any of the zero-copy operations READ, READDIR,
3953 * In the case of the first three, we want to put the GETATTR
3954 * after the read-type operation -- this is because it is hard
3955 * to predict the length of a GETATTR response in v4, and thus
3956 * align the READ data correctly. This means that the GETATTR
3957 * may end up partially falling into the page cache, and we should
3958 * shift it into the 'tail' of the xdr_buf before processing.
3959 * To do this efficiently, we need to know the total length
3960 * of data received, which doesn't seem to be available outside
3963 * In the case of WRITE, we also want to put the GETATTR after
3964 * the operation -- in this case because we want to make sure
3965 * we get the post-operation mtime and size.
3967 * Both of these changes to the XDR layer would in fact be quite
3968 * minor, but I decided to leave them for a subsequent patch.
3970 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3971 unsigned int pgbase, unsigned int pglen)
3973 struct nfs4_readlink args = {
3974 .fh = NFS_FH(inode),
3979 struct nfs4_readlink_res res;
3980 struct rpc_message msg = {
3981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3986 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3989 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3990 unsigned int pgbase, unsigned int pglen)
3992 struct nfs4_exception exception = { };
3995 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3996 trace_nfs4_readlink(inode, err);
3997 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3999 } while (exception.retry);
4004 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4007 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4010 struct nfs_server *server = NFS_SERVER(dir);
4011 struct nfs4_label l, *ilabel = NULL;
4012 struct nfs_open_context *ctx;
4013 struct nfs4_state *state;
4016 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4018 return PTR_ERR(ctx);
4020 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4022 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4023 sattr->ia_mode &= ~current_umask();
4024 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4025 if (IS_ERR(state)) {
4026 status = PTR_ERR(state);
4030 nfs4_label_release_security(ilabel);
4031 put_nfs_open_context(ctx);
4035 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4037 struct nfs_server *server = NFS_SERVER(dir);
4038 struct nfs_removeargs args = {
4042 struct nfs_removeres res = {
4045 struct rpc_message msg = {
4046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4050 unsigned long timestamp = jiffies;
4053 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4055 update_changeattr(dir, &res.cinfo, timestamp);
4059 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4061 struct nfs4_exception exception = { };
4064 err = _nfs4_proc_remove(dir, name);
4065 trace_nfs4_remove(dir, name, err);
4066 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4068 } while (exception.retry);
4072 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4074 struct nfs_server *server = NFS_SERVER(dir);
4075 struct nfs_removeargs *args = msg->rpc_argp;
4076 struct nfs_removeres *res = msg->rpc_resp;
4078 res->server = server;
4079 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4080 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4082 nfs_fattr_init(res->dir_attr);
4085 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4087 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4088 &data->args.seq_args,
4093 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4095 struct nfs_unlinkdata *data = task->tk_calldata;
4096 struct nfs_removeres *res = &data->res;
4098 if (!nfs4_sequence_done(task, &res->seq_res))
4100 if (nfs4_async_handle_error(task, res->server, NULL,
4101 &data->timeout) == -EAGAIN)
4103 if (task->tk_status == 0)
4104 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
4108 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4110 struct nfs_server *server = NFS_SERVER(dir);
4111 struct nfs_renameargs *arg = msg->rpc_argp;
4112 struct nfs_renameres *res = msg->rpc_resp;
4114 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4115 res->server = server;
4116 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4119 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4121 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4122 &data->args.seq_args,
4127 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4128 struct inode *new_dir)
4130 struct nfs_renamedata *data = task->tk_calldata;
4131 struct nfs_renameres *res = &data->res;
4133 if (!nfs4_sequence_done(task, &res->seq_res))
4135 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4138 if (task->tk_status == 0) {
4139 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
4140 if (new_dir != old_dir)
4141 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
4146 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4148 struct nfs_server *server = NFS_SERVER(inode);
4149 struct nfs4_link_arg arg = {
4150 .fh = NFS_FH(inode),
4151 .dir_fh = NFS_FH(dir),
4153 .bitmask = server->attr_bitmask,
4155 struct nfs4_link_res res = {
4159 struct rpc_message msg = {
4160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4164 int status = -ENOMEM;
4166 res.fattr = nfs_alloc_fattr();
4167 if (res.fattr == NULL)
4170 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4171 if (IS_ERR(res.label)) {
4172 status = PTR_ERR(res.label);
4175 arg.bitmask = nfs4_bitmask(server, res.label);
4177 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4179 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
4180 status = nfs_post_op_update_inode(inode, res.fattr);
4182 nfs_setsecurity(inode, res.fattr, res.label);
4186 nfs4_label_free(res.label);
4189 nfs_free_fattr(res.fattr);
4193 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4195 struct nfs4_exception exception = { };
4198 err = nfs4_handle_exception(NFS_SERVER(inode),
4199 _nfs4_proc_link(inode, dir, name),
4201 } while (exception.retry);
4205 struct nfs4_createdata {
4206 struct rpc_message msg;
4207 struct nfs4_create_arg arg;
4208 struct nfs4_create_res res;
4210 struct nfs_fattr fattr;
4211 struct nfs4_label *label;
4214 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4215 const struct qstr *name, struct iattr *sattr, u32 ftype)
4217 struct nfs4_createdata *data;
4219 data = kzalloc(sizeof(*data), GFP_KERNEL);
4221 struct nfs_server *server = NFS_SERVER(dir);
4223 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4224 if (IS_ERR(data->label))
4227 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4228 data->msg.rpc_argp = &data->arg;
4229 data->msg.rpc_resp = &data->res;
4230 data->arg.dir_fh = NFS_FH(dir);
4231 data->arg.server = server;
4232 data->arg.name = name;
4233 data->arg.attrs = sattr;
4234 data->arg.ftype = ftype;
4235 data->arg.bitmask = nfs4_bitmask(server, data->label);
4236 data->arg.umask = current_umask();
4237 data->res.server = server;
4238 data->res.fh = &data->fh;
4239 data->res.fattr = &data->fattr;
4240 data->res.label = data->label;
4241 nfs_fattr_init(data->res.fattr);
4249 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4251 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4252 &data->arg.seq_args, &data->res.seq_res, 1);
4254 update_changeattr(dir, &data->res.dir_cinfo,
4255 data->res.fattr->time_start);
4256 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4261 static void nfs4_free_createdata(struct nfs4_createdata *data)
4263 nfs4_label_free(data->label);
4267 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4268 struct page *page, unsigned int len, struct iattr *sattr,
4269 struct nfs4_label *label)
4271 struct nfs4_createdata *data;
4272 int status = -ENAMETOOLONG;
4274 if (len > NFS4_MAXPATHLEN)
4278 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4282 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4283 data->arg.u.symlink.pages = &page;
4284 data->arg.u.symlink.len = len;
4285 data->arg.label = label;
4287 status = nfs4_do_create(dir, dentry, data);
4289 nfs4_free_createdata(data);
4294 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4295 struct page *page, unsigned int len, struct iattr *sattr)
4297 struct nfs4_exception exception = { };
4298 struct nfs4_label l, *label = NULL;
4301 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4304 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4305 trace_nfs4_symlink(dir, &dentry->d_name, err);
4306 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4308 } while (exception.retry);
4310 nfs4_label_release_security(label);
4314 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4315 struct iattr *sattr, struct nfs4_label *label)
4317 struct nfs4_createdata *data;
4318 int status = -ENOMEM;
4320 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4324 data->arg.label = label;
4325 status = nfs4_do_create(dir, dentry, data);
4327 nfs4_free_createdata(data);
4332 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4333 struct iattr *sattr)
4335 struct nfs_server *server = NFS_SERVER(dir);
4336 struct nfs4_exception exception = { };
4337 struct nfs4_label l, *label = NULL;
4340 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4342 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4343 sattr->ia_mode &= ~current_umask();
4345 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4346 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4347 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4349 } while (exception.retry);
4350 nfs4_label_release_security(label);
4355 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4356 u64 cookie, struct page **pages, unsigned int count, bool plus)
4358 struct inode *dir = d_inode(dentry);
4359 struct nfs4_readdir_arg args = {
4364 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4367 struct nfs4_readdir_res res;
4368 struct rpc_message msg = {
4369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4376 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4378 (unsigned long long)cookie);
4379 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4380 res.pgbase = args.pgbase;
4381 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4383 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4384 status += args.pgbase;
4387 nfs_invalidate_atime(dir);
4389 dprintk("%s: returns %d\n", __func__, status);
4393 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4394 u64 cookie, struct page **pages, unsigned int count, bool plus)
4396 struct nfs4_exception exception = { };
4399 err = _nfs4_proc_readdir(dentry, cred, cookie,
4400 pages, count, plus);
4401 trace_nfs4_readdir(d_inode(dentry), err);
4402 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4404 } while (exception.retry);
4408 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4409 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4411 struct nfs4_createdata *data;
4412 int mode = sattr->ia_mode;
4413 int status = -ENOMEM;
4415 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4420 data->arg.ftype = NF4FIFO;
4421 else if (S_ISBLK(mode)) {
4422 data->arg.ftype = NF4BLK;
4423 data->arg.u.device.specdata1 = MAJOR(rdev);
4424 data->arg.u.device.specdata2 = MINOR(rdev);
4426 else if (S_ISCHR(mode)) {
4427 data->arg.ftype = NF4CHR;
4428 data->arg.u.device.specdata1 = MAJOR(rdev);
4429 data->arg.u.device.specdata2 = MINOR(rdev);
4430 } else if (!S_ISSOCK(mode)) {
4435 data->arg.label = label;
4436 status = nfs4_do_create(dir, dentry, data);
4438 nfs4_free_createdata(data);
4443 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4444 struct iattr *sattr, dev_t rdev)
4446 struct nfs_server *server = NFS_SERVER(dir);
4447 struct nfs4_exception exception = { };
4448 struct nfs4_label l, *label = NULL;
4451 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4453 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4454 sattr->ia_mode &= ~current_umask();
4456 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4457 trace_nfs4_mknod(dir, &dentry->d_name, err);
4458 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4460 } while (exception.retry);
4462 nfs4_label_release_security(label);
4467 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4468 struct nfs_fsstat *fsstat)
4470 struct nfs4_statfs_arg args = {
4472 .bitmask = server->attr_bitmask,
4474 struct nfs4_statfs_res res = {
4477 struct rpc_message msg = {
4478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4483 nfs_fattr_init(fsstat->fattr);
4484 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4487 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4489 struct nfs4_exception exception = { };
4492 err = nfs4_handle_exception(server,
4493 _nfs4_proc_statfs(server, fhandle, fsstat),
4495 } while (exception.retry);
4499 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4500 struct nfs_fsinfo *fsinfo)
4502 struct nfs4_fsinfo_arg args = {
4504 .bitmask = server->attr_bitmask,
4506 struct nfs4_fsinfo_res res = {
4509 struct rpc_message msg = {
4510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4515 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4518 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4520 struct nfs4_exception exception = { };
4521 unsigned long now = jiffies;
4525 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4526 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4528 nfs4_set_lease_period(server->nfs_client,
4529 fsinfo->lease_time * HZ,
4533 err = nfs4_handle_exception(server, err, &exception);
4534 } while (exception.retry);
4538 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4542 nfs_fattr_init(fsinfo->fattr);
4543 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4545 /* block layout checks this! */
4546 server->pnfs_blksize = fsinfo->blksize;
4547 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4553 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4554 struct nfs_pathconf *pathconf)
4556 struct nfs4_pathconf_arg args = {
4558 .bitmask = server->attr_bitmask,
4560 struct nfs4_pathconf_res res = {
4561 .pathconf = pathconf,
4563 struct rpc_message msg = {
4564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4569 /* None of the pathconf attributes are mandatory to implement */
4570 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4571 memset(pathconf, 0, sizeof(*pathconf));
4575 nfs_fattr_init(pathconf->fattr);
4576 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4579 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4580 struct nfs_pathconf *pathconf)
4582 struct nfs4_exception exception = { };
4586 err = nfs4_handle_exception(server,
4587 _nfs4_proc_pathconf(server, fhandle, pathconf),
4589 } while (exception.retry);
4593 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4594 const struct nfs_open_context *ctx,
4595 const struct nfs_lock_context *l_ctx,
4598 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4600 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4602 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4603 const struct nfs_open_context *ctx,
4604 const struct nfs_lock_context *l_ctx,
4607 nfs4_stateid current_stateid;
4609 /* If the current stateid represents a lost lock, then exit */
4610 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4612 return nfs4_stateid_match(stateid, ¤t_stateid);
4615 static bool nfs4_error_stateid_expired(int err)
4618 case -NFS4ERR_DELEG_REVOKED:
4619 case -NFS4ERR_ADMIN_REVOKED:
4620 case -NFS4ERR_BAD_STATEID:
4621 case -NFS4ERR_STALE_STATEID:
4622 case -NFS4ERR_OLD_STATEID:
4623 case -NFS4ERR_OPENMODE:
4624 case -NFS4ERR_EXPIRED:
4630 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4632 struct nfs_server *server = NFS_SERVER(hdr->inode);
4634 trace_nfs4_read(hdr, task->tk_status);
4635 if (task->tk_status < 0) {
4636 struct nfs4_exception exception = {
4637 .inode = hdr->inode,
4638 .state = hdr->args.context->state,
4639 .stateid = &hdr->args.stateid,
4641 task->tk_status = nfs4_async_handle_exception(task,
4642 server, task->tk_status, &exception);
4643 if (exception.retry) {
4644 rpc_restart_call_prepare(task);
4649 if (task->tk_status > 0)
4650 renew_lease(server, hdr->timestamp);
4654 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4655 struct nfs_pgio_args *args)
4658 if (!nfs4_error_stateid_expired(task->tk_status) ||
4659 nfs4_stateid_is_current(&args->stateid,
4664 rpc_restart_call_prepare(task);
4668 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4671 dprintk("--> %s\n", __func__);
4673 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4675 if (nfs4_read_stateid_changed(task, &hdr->args))
4677 if (task->tk_status > 0)
4678 nfs_invalidate_atime(hdr->inode);
4679 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4680 nfs4_read_done_cb(task, hdr);
4683 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4684 struct rpc_message *msg)
4686 hdr->timestamp = jiffies;
4687 if (!hdr->pgio_done_cb)
4688 hdr->pgio_done_cb = nfs4_read_done_cb;
4689 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4690 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4693 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4694 struct nfs_pgio_header *hdr)
4696 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
4697 &hdr->args.seq_args,
4701 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4702 hdr->args.lock_context,
4703 hdr->rw_mode) == -EIO)
4705 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4710 static int nfs4_write_done_cb(struct rpc_task *task,
4711 struct nfs_pgio_header *hdr)
4713 struct inode *inode = hdr->inode;
4715 trace_nfs4_write(hdr, task->tk_status);
4716 if (task->tk_status < 0) {
4717 struct nfs4_exception exception = {
4718 .inode = hdr->inode,
4719 .state = hdr->args.context->state,
4720 .stateid = &hdr->args.stateid,
4722 task->tk_status = nfs4_async_handle_exception(task,
4723 NFS_SERVER(inode), task->tk_status,
4725 if (exception.retry) {
4726 rpc_restart_call_prepare(task);
4730 if (task->tk_status >= 0) {
4731 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4732 nfs_writeback_update_inode(hdr);
4737 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4738 struct nfs_pgio_args *args)
4741 if (!nfs4_error_stateid_expired(task->tk_status) ||
4742 nfs4_stateid_is_current(&args->stateid,
4747 rpc_restart_call_prepare(task);
4751 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4753 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4755 if (nfs4_write_stateid_changed(task, &hdr->args))
4757 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4758 nfs4_write_done_cb(task, hdr);
4762 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4764 /* Don't request attributes for pNFS or O_DIRECT writes */
4765 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4767 /* Otherwise, request attributes if and only if we don't hold
4770 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4773 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4774 struct rpc_message *msg)
4776 struct nfs_server *server = NFS_SERVER(hdr->inode);
4778 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4779 hdr->args.bitmask = NULL;
4780 hdr->res.fattr = NULL;
4782 hdr->args.bitmask = server->cache_consistency_bitmask;
4784 if (!hdr->pgio_done_cb)
4785 hdr->pgio_done_cb = nfs4_write_done_cb;
4786 hdr->res.server = server;
4787 hdr->timestamp = jiffies;
4789 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4790 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4793 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4795 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
4796 &data->args.seq_args,
4801 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4803 struct inode *inode = data->inode;
4805 trace_nfs4_commit(data, task->tk_status);
4806 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4807 NULL, NULL) == -EAGAIN) {
4808 rpc_restart_call_prepare(task);
4814 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4816 if (!nfs4_sequence_done(task, &data->res.seq_res))
4818 return data->commit_done_cb(task, data);
4821 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4823 struct nfs_server *server = NFS_SERVER(data->inode);
4825 if (data->commit_done_cb == NULL)
4826 data->commit_done_cb = nfs4_commit_done_cb;
4827 data->res.server = server;
4828 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4829 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4832 struct nfs4_renewdata {
4833 struct nfs_client *client;
4834 unsigned long timestamp;
4838 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4839 * standalone procedure for queueing an asynchronous RENEW.
4841 static void nfs4_renew_release(void *calldata)
4843 struct nfs4_renewdata *data = calldata;
4844 struct nfs_client *clp = data->client;
4846 if (atomic_read(&clp->cl_count) > 1)
4847 nfs4_schedule_state_renewal(clp);
4848 nfs_put_client(clp);
4852 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4854 struct nfs4_renewdata *data = calldata;
4855 struct nfs_client *clp = data->client;
4856 unsigned long timestamp = data->timestamp;
4858 trace_nfs4_renew_async(clp, task->tk_status);
4859 switch (task->tk_status) {
4862 case -NFS4ERR_LEASE_MOVED:
4863 nfs4_schedule_lease_moved_recovery(clp);
4866 /* Unless we're shutting down, schedule state recovery! */
4867 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4869 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4870 nfs4_schedule_lease_recovery(clp);
4873 nfs4_schedule_path_down_recovery(clp);
4875 do_renew_lease(clp, timestamp);
4878 static const struct rpc_call_ops nfs4_renew_ops = {
4879 .rpc_call_done = nfs4_renew_done,
4880 .rpc_release = nfs4_renew_release,
4883 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4885 struct rpc_message msg = {
4886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4890 struct nfs4_renewdata *data;
4892 if (renew_flags == 0)
4894 if (!atomic_inc_not_zero(&clp->cl_count))
4896 data = kmalloc(sizeof(*data), GFP_NOFS);
4898 nfs_put_client(clp);
4902 data->timestamp = jiffies;
4903 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4904 &nfs4_renew_ops, data);
4907 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4909 struct rpc_message msg = {
4910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4914 unsigned long now = jiffies;
4917 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4920 do_renew_lease(clp, now);
4924 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4926 return server->caps & NFS_CAP_ACLS;
4929 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4930 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4933 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4935 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4936 struct page **pages)
4938 struct page *newpage, **spages;
4944 len = min_t(size_t, PAGE_SIZE, buflen);
4945 newpage = alloc_page(GFP_KERNEL);
4947 if (newpage == NULL)
4949 memcpy(page_address(newpage), buf, len);
4954 } while (buflen != 0);
4960 __free_page(spages[rc-1]);
4964 struct nfs4_cached_acl {
4970 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4972 struct nfs_inode *nfsi = NFS_I(inode);
4974 spin_lock(&inode->i_lock);
4975 kfree(nfsi->nfs4_acl);
4976 nfsi->nfs4_acl = acl;
4977 spin_unlock(&inode->i_lock);
4980 static void nfs4_zap_acl_attr(struct inode *inode)
4982 nfs4_set_cached_acl(inode, NULL);
4985 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4987 struct nfs_inode *nfsi = NFS_I(inode);
4988 struct nfs4_cached_acl *acl;
4991 spin_lock(&inode->i_lock);
4992 acl = nfsi->nfs4_acl;
4995 if (buf == NULL) /* user is just asking for length */
4997 if (acl->cached == 0)
4999 ret = -ERANGE; /* see getxattr(2) man page */
5000 if (acl->len > buflen)
5002 memcpy(buf, acl->data, acl->len);
5006 spin_unlock(&inode->i_lock);
5010 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5012 struct nfs4_cached_acl *acl;
5013 size_t buflen = sizeof(*acl) + acl_len;
5015 if (buflen <= PAGE_SIZE) {
5016 acl = kmalloc(buflen, GFP_KERNEL);
5020 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5022 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5029 nfs4_set_cached_acl(inode, acl);
5033 * The getxattr API returns the required buffer length when called with a
5034 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5035 * the required buf. On a NULL buf, we send a page of data to the server
5036 * guessing that the ACL request can be serviced by a page. If so, we cache
5037 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5038 * the cache. If not so, we throw away the page, and cache the required
5039 * length. The next getxattr call will then produce another round trip to
5040 * the server, this time with the input buf of the required size.
5042 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5044 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5045 struct nfs_getaclargs args = {
5046 .fh = NFS_FH(inode),
5050 struct nfs_getaclres res = {
5053 struct rpc_message msg = {
5054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5058 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5059 int ret = -ENOMEM, i;
5061 if (npages > ARRAY_SIZE(pages))
5064 for (i = 0; i < npages; i++) {
5065 pages[i] = alloc_page(GFP_KERNEL);
5070 /* for decoding across pages */
5071 res.acl_scratch = alloc_page(GFP_KERNEL);
5072 if (!res.acl_scratch)
5075 args.acl_len = npages * PAGE_SIZE;
5077 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5078 __func__, buf, buflen, npages, args.acl_len);
5079 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5080 &msg, &args.seq_args, &res.seq_res, 0);
5084 /* Handle the case where the passed-in buffer is too short */
5085 if (res.acl_flags & NFS4_ACL_TRUNC) {
5086 /* Did the user only issue a request for the acl length? */
5092 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5094 if (res.acl_len > buflen) {
5098 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5103 for (i = 0; i < npages; i++)
5105 __free_page(pages[i]);
5106 if (res.acl_scratch)
5107 __free_page(res.acl_scratch);
5111 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5113 struct nfs4_exception exception = { };
5116 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5117 trace_nfs4_get_acl(inode, ret);
5120 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5121 } while (exception.retry);
5125 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5127 struct nfs_server *server = NFS_SERVER(inode);
5130 if (!nfs4_server_supports_acls(server))
5132 ret = nfs_revalidate_inode(server, inode);
5135 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5136 nfs_zap_acl_cache(inode);
5137 ret = nfs4_read_cached_acl(inode, buf, buflen);
5139 /* -ENOENT is returned if there is no ACL or if there is an ACL
5140 * but no cached acl data, just the acl length */
5142 return nfs4_get_acl_uncached(inode, buf, buflen);
5145 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5147 struct nfs_server *server = NFS_SERVER(inode);
5148 struct page *pages[NFS4ACL_MAXPAGES];
5149 struct nfs_setaclargs arg = {
5150 .fh = NFS_FH(inode),
5154 struct nfs_setaclres res;
5155 struct rpc_message msg = {
5156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5160 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5163 if (!nfs4_server_supports_acls(server))
5165 if (npages > ARRAY_SIZE(pages))
5167 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5170 nfs4_inode_return_delegation(inode);
5171 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5174 * Free each page after tx, so the only ref left is
5175 * held by the network stack
5178 put_page(pages[i-1]);
5181 * Acl update can result in inode attribute update.
5182 * so mark the attribute cache invalid.
5184 spin_lock(&inode->i_lock);
5185 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5186 spin_unlock(&inode->i_lock);
5187 nfs_access_zap_cache(inode);
5188 nfs_zap_acl_cache(inode);
5192 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5194 struct nfs4_exception exception = { };
5197 err = __nfs4_proc_set_acl(inode, buf, buflen);
5198 trace_nfs4_set_acl(inode, err);
5199 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5201 } while (exception.retry);
5205 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5206 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5209 struct nfs_server *server = NFS_SERVER(inode);
5210 struct nfs_fattr fattr;
5211 struct nfs4_label label = {0, 0, buflen, buf};
5213 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5214 struct nfs4_getattr_arg arg = {
5215 .fh = NFS_FH(inode),
5218 struct nfs4_getattr_res res = {
5223 struct rpc_message msg = {
5224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5230 nfs_fattr_init(&fattr);
5232 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5235 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5237 if (buflen < label.len)
5242 static int nfs4_get_security_label(struct inode *inode, void *buf,
5245 struct nfs4_exception exception = { };
5248 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5252 err = _nfs4_get_security_label(inode, buf, buflen);
5253 trace_nfs4_get_security_label(inode, err);
5254 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5256 } while (exception.retry);
5260 static int _nfs4_do_set_security_label(struct inode *inode,
5261 struct nfs4_label *ilabel,
5262 struct nfs_fattr *fattr,
5263 struct nfs4_label *olabel)
5266 struct iattr sattr = {0};
5267 struct nfs_server *server = NFS_SERVER(inode);
5268 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5269 struct nfs_setattrargs arg = {
5270 .fh = NFS_FH(inode),
5276 struct nfs_setattrres res = {
5281 struct rpc_message msg = {
5282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5288 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5290 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5292 dprintk("%s failed: %d\n", __func__, status);
5297 static int nfs4_do_set_security_label(struct inode *inode,
5298 struct nfs4_label *ilabel,
5299 struct nfs_fattr *fattr,
5300 struct nfs4_label *olabel)
5302 struct nfs4_exception exception = { };
5306 err = _nfs4_do_set_security_label(inode, ilabel,
5308 trace_nfs4_set_security_label(inode, err);
5309 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5311 } while (exception.retry);
5316 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5318 struct nfs4_label ilabel, *olabel = NULL;
5319 struct nfs_fattr fattr;
5320 struct rpc_cred *cred;
5323 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5326 nfs_fattr_init(&fattr);
5330 ilabel.label = (char *)buf;
5331 ilabel.len = buflen;
5333 cred = rpc_lookup_cred();
5335 return PTR_ERR(cred);
5337 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5338 if (IS_ERR(olabel)) {
5339 status = -PTR_ERR(olabel);
5343 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5345 nfs_setsecurity(inode, &fattr, olabel);
5347 nfs4_label_free(olabel);
5352 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5355 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5356 nfs4_verifier *bootverf)
5360 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5361 /* An impossible timestamp guarantees this value
5362 * will never match a generated boot time. */
5363 verf[0] = cpu_to_be32(U32_MAX);
5364 verf[1] = cpu_to_be32(U32_MAX);
5366 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5367 u64 ns = ktime_to_ns(nn->boot_time);
5369 verf[0] = cpu_to_be32(ns >> 32);
5370 verf[1] = cpu_to_be32(ns);
5372 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5376 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5381 if (clp->cl_owner_id != NULL)
5385 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5386 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5388 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5392 if (len > NFS4_OPAQUE_LIMIT + 1)
5396 * Since this string is allocated at mount time, and held until the
5397 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5398 * about a memory-reclaim deadlock.
5400 str = kmalloc(len, GFP_KERNEL);
5405 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5407 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5408 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5411 clp->cl_owner_id = str;
5416 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5421 len = 10 + 10 + 1 + 10 + 1 +
5422 strlen(nfs4_client_id_uniquifier) + 1 +
5423 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5425 if (len > NFS4_OPAQUE_LIMIT + 1)
5429 * Since this string is allocated at mount time, and held until the
5430 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5431 * about a memory-reclaim deadlock.
5433 str = kmalloc(len, GFP_KERNEL);
5437 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5438 clp->rpc_ops->version, clp->cl_minorversion,
5439 nfs4_client_id_uniquifier,
5440 clp->cl_rpcclient->cl_nodename);
5441 clp->cl_owner_id = str;
5446 nfs4_init_uniform_client_string(struct nfs_client *clp)
5451 if (clp->cl_owner_id != NULL)
5454 if (nfs4_client_id_uniquifier[0] != '\0')
5455 return nfs4_init_uniquifier_client_string(clp);
5457 len = 10 + 10 + 1 + 10 + 1 +
5458 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5460 if (len > NFS4_OPAQUE_LIMIT + 1)
5464 * Since this string is allocated at mount time, and held until the
5465 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5466 * about a memory-reclaim deadlock.
5468 str = kmalloc(len, GFP_KERNEL);
5472 scnprintf(str, len, "Linux NFSv%u.%u %s",
5473 clp->rpc_ops->version, clp->cl_minorversion,
5474 clp->cl_rpcclient->cl_nodename);
5475 clp->cl_owner_id = str;
5480 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5481 * services. Advertise one based on the address family of the
5485 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5487 if (strchr(clp->cl_ipaddr, ':') != NULL)
5488 return scnprintf(buf, len, "tcp6");
5490 return scnprintf(buf, len, "tcp");
5493 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5495 struct nfs4_setclientid *sc = calldata;
5497 if (task->tk_status == 0)
5498 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5501 static const struct rpc_call_ops nfs4_setclientid_ops = {
5502 .rpc_call_done = nfs4_setclientid_done,
5506 * nfs4_proc_setclientid - Negotiate client ID
5507 * @clp: state data structure
5508 * @program: RPC program for NFSv4 callback service
5509 * @port: IP port number for NFS4 callback service
5510 * @cred: RPC credential to use for this call
5511 * @res: where to place the result
5513 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5515 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5516 unsigned short port, struct rpc_cred *cred,
5517 struct nfs4_setclientid_res *res)
5519 nfs4_verifier sc_verifier;
5520 struct nfs4_setclientid setclientid = {
5521 .sc_verifier = &sc_verifier,
5525 struct rpc_message msg = {
5526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5527 .rpc_argp = &setclientid,
5531 struct rpc_task *task;
5532 struct rpc_task_setup task_setup_data = {
5533 .rpc_client = clp->cl_rpcclient,
5534 .rpc_message = &msg,
5535 .callback_ops = &nfs4_setclientid_ops,
5536 .callback_data = &setclientid,
5537 .flags = RPC_TASK_TIMEOUT,
5541 /* nfs_client_id4 */
5542 nfs4_init_boot_verifier(clp, &sc_verifier);
5544 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5545 status = nfs4_init_uniform_client_string(clp);
5547 status = nfs4_init_nonuniform_client_string(clp);
5553 setclientid.sc_netid_len =
5554 nfs4_init_callback_netid(clp,
5555 setclientid.sc_netid,
5556 sizeof(setclientid.sc_netid));
5557 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5558 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5559 clp->cl_ipaddr, port >> 8, port & 255);
5561 dprintk("NFS call setclientid auth=%s, '%s'\n",
5562 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5564 task = rpc_run_task(&task_setup_data);
5566 status = PTR_ERR(task);
5569 status = task->tk_status;
5570 if (setclientid.sc_cred) {
5571 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5572 put_rpccred(setclientid.sc_cred);
5576 trace_nfs4_setclientid(clp, status);
5577 dprintk("NFS reply setclientid: %d\n", status);
5582 * nfs4_proc_setclientid_confirm - Confirm client ID
5583 * @clp: state data structure
5584 * @res: result of a previous SETCLIENTID
5585 * @cred: RPC credential to use for this call
5587 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5589 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5590 struct nfs4_setclientid_res *arg,
5591 struct rpc_cred *cred)
5593 struct rpc_message msg = {
5594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5600 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5601 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5603 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5604 trace_nfs4_setclientid_confirm(clp, status);
5605 dprintk("NFS reply setclientid_confirm: %d\n", status);
5609 struct nfs4_delegreturndata {
5610 struct nfs4_delegreturnargs args;
5611 struct nfs4_delegreturnres res;
5613 nfs4_stateid stateid;
5614 unsigned long timestamp;
5616 struct nfs4_layoutreturn_args arg;
5617 struct nfs4_layoutreturn_res res;
5618 struct nfs4_xdr_opaque_data ld_private;
5622 struct nfs_fattr fattr;
5624 struct inode *inode;
5627 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5629 struct nfs4_delegreturndata *data = calldata;
5631 if (!nfs4_sequence_done(task, &data->res.seq_res))
5634 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5636 /* Handle Layoutreturn errors */
5637 if (data->args.lr_args && task->tk_status != 0) {
5638 switch(data->res.lr_ret) {
5640 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5643 data->args.lr_args = NULL;
5644 data->res.lr_res = NULL;
5646 case -NFS4ERR_ADMIN_REVOKED:
5647 case -NFS4ERR_DELEG_REVOKED:
5648 case -NFS4ERR_EXPIRED:
5649 case -NFS4ERR_BAD_STATEID:
5650 case -NFS4ERR_OLD_STATEID:
5651 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5652 case -NFS4ERR_WRONG_CRED:
5653 data->args.lr_args = NULL;
5654 data->res.lr_res = NULL;
5655 data->res.lr_ret = 0;
5656 rpc_restart_call_prepare(task);
5661 switch (task->tk_status) {
5663 renew_lease(data->res.server, data->timestamp);
5665 case -NFS4ERR_ADMIN_REVOKED:
5666 case -NFS4ERR_DELEG_REVOKED:
5667 case -NFS4ERR_EXPIRED:
5668 nfs4_free_revoked_stateid(data->res.server,
5670 task->tk_msg.rpc_cred);
5671 case -NFS4ERR_BAD_STATEID:
5672 case -NFS4ERR_OLD_STATEID:
5673 case -NFS4ERR_STALE_STATEID:
5674 task->tk_status = 0;
5676 case -NFS4ERR_ACCESS:
5677 if (data->args.bitmask) {
5678 data->args.bitmask = NULL;
5679 data->res.fattr = NULL;
5680 task->tk_status = 0;
5681 rpc_restart_call_prepare(task);
5685 if (nfs4_async_handle_error(task, data->res.server,
5686 NULL, NULL) == -EAGAIN) {
5687 rpc_restart_call_prepare(task);
5691 data->rpc_status = task->tk_status;
5694 static void nfs4_delegreturn_release(void *calldata)
5696 struct nfs4_delegreturndata *data = calldata;
5697 struct inode *inode = data->inode;
5701 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5703 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5704 nfs_iput_and_deactive(inode);
5709 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5711 struct nfs4_delegreturndata *d_data;
5713 d_data = (struct nfs4_delegreturndata *)data;
5715 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5718 nfs4_setup_sequence(d_data->res.server->nfs_client,
5719 &d_data->args.seq_args,
5720 &d_data->res.seq_res,
5724 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5725 .rpc_call_prepare = nfs4_delegreturn_prepare,
5726 .rpc_call_done = nfs4_delegreturn_done,
5727 .rpc_release = nfs4_delegreturn_release,
5730 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5732 struct nfs4_delegreturndata *data;
5733 struct nfs_server *server = NFS_SERVER(inode);
5734 struct rpc_task *task;
5735 struct rpc_message msg = {
5736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5739 struct rpc_task_setup task_setup_data = {
5740 .rpc_client = server->client,
5741 .rpc_message = &msg,
5742 .callback_ops = &nfs4_delegreturn_ops,
5743 .flags = RPC_TASK_ASYNC,
5747 data = kzalloc(sizeof(*data), GFP_NOFS);
5750 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5752 nfs4_state_protect(server->nfs_client,
5753 NFS_SP4_MACH_CRED_CLEANUP,
5754 &task_setup_data.rpc_client, &msg);
5756 data->args.fhandle = &data->fh;
5757 data->args.stateid = &data->stateid;
5758 data->args.bitmask = server->cache_consistency_bitmask;
5759 nfs_copy_fh(&data->fh, NFS_FH(inode));
5760 nfs4_stateid_copy(&data->stateid, stateid);
5761 data->res.fattr = &data->fattr;
5762 data->res.server = server;
5763 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5764 data->lr.arg.ld_private = &data->lr.ld_private;
5765 nfs_fattr_init(data->res.fattr);
5766 data->timestamp = jiffies;
5767 data->rpc_status = 0;
5768 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5769 data->inode = nfs_igrab_and_active(inode);
5772 data->args.lr_args = &data->lr.arg;
5773 data->res.lr_res = &data->lr.res;
5775 } else if (data->lr.roc) {
5776 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5777 data->lr.roc = false;
5780 task_setup_data.callback_data = data;
5781 msg.rpc_argp = &data->args;
5782 msg.rpc_resp = &data->res;
5783 task = rpc_run_task(&task_setup_data);
5785 return PTR_ERR(task);
5788 status = rpc_wait_for_completion_task(task);
5791 status = data->rpc_status;
5797 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5799 struct nfs_server *server = NFS_SERVER(inode);
5800 struct nfs4_exception exception = { };
5803 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5804 trace_nfs4_delegreturn(inode, stateid, err);
5806 case -NFS4ERR_STALE_STATEID:
5807 case -NFS4ERR_EXPIRED:
5811 err = nfs4_handle_exception(server, err, &exception);
5812 } while (exception.retry);
5816 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5818 struct inode *inode = state->inode;
5819 struct nfs_server *server = NFS_SERVER(inode);
5820 struct nfs_client *clp = server->nfs_client;
5821 struct nfs_lockt_args arg = {
5822 .fh = NFS_FH(inode),
5825 struct nfs_lockt_res res = {
5828 struct rpc_message msg = {
5829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5832 .rpc_cred = state->owner->so_cred,
5834 struct nfs4_lock_state *lsp;
5837 arg.lock_owner.clientid = clp->cl_clientid;
5838 status = nfs4_set_lock_state(state, request);
5841 lsp = request->fl_u.nfs4_fl.owner;
5842 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5843 arg.lock_owner.s_dev = server->s_dev;
5844 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5847 request->fl_type = F_UNLCK;
5849 case -NFS4ERR_DENIED:
5852 request->fl_ops->fl_release_private(request);
5853 request->fl_ops = NULL;
5858 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5860 struct nfs4_exception exception = { };
5864 err = _nfs4_proc_getlk(state, cmd, request);
5865 trace_nfs4_get_lock(request, state, cmd, err);
5866 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5868 } while (exception.retry);
5872 struct nfs4_unlockdata {
5873 struct nfs_locku_args arg;
5874 struct nfs_locku_res res;
5875 struct nfs4_lock_state *lsp;
5876 struct nfs_open_context *ctx;
5877 struct nfs_lock_context *l_ctx;
5878 struct file_lock fl;
5879 struct nfs_server *server;
5880 unsigned long timestamp;
5883 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5884 struct nfs_open_context *ctx,
5885 struct nfs4_lock_state *lsp,
5886 struct nfs_seqid *seqid)
5888 struct nfs4_unlockdata *p;
5889 struct inode *inode = lsp->ls_state->inode;
5891 p = kzalloc(sizeof(*p), GFP_NOFS);
5894 p->arg.fh = NFS_FH(inode);
5896 p->arg.seqid = seqid;
5897 p->res.seqid = seqid;
5899 atomic_inc(&lsp->ls_count);
5900 /* Ensure we don't close file until we're done freeing locks! */
5901 p->ctx = get_nfs_open_context(ctx);
5902 p->l_ctx = nfs_get_lock_context(ctx);
5903 memcpy(&p->fl, fl, sizeof(p->fl));
5904 p->server = NFS_SERVER(inode);
5908 static void nfs4_locku_release_calldata(void *data)
5910 struct nfs4_unlockdata *calldata = data;
5911 nfs_free_seqid(calldata->arg.seqid);
5912 nfs4_put_lock_state(calldata->lsp);
5913 nfs_put_lock_context(calldata->l_ctx);
5914 put_nfs_open_context(calldata->ctx);
5918 static void nfs4_locku_done(struct rpc_task *task, void *data)
5920 struct nfs4_unlockdata *calldata = data;
5922 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5924 switch (task->tk_status) {
5926 renew_lease(calldata->server, calldata->timestamp);
5927 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5928 if (nfs4_update_lock_stateid(calldata->lsp,
5929 &calldata->res.stateid))
5931 case -NFS4ERR_ADMIN_REVOKED:
5932 case -NFS4ERR_EXPIRED:
5933 nfs4_free_revoked_stateid(calldata->server,
5934 &calldata->arg.stateid,
5935 task->tk_msg.rpc_cred);
5936 case -NFS4ERR_BAD_STATEID:
5937 case -NFS4ERR_OLD_STATEID:
5938 case -NFS4ERR_STALE_STATEID:
5939 if (!nfs4_stateid_match(&calldata->arg.stateid,
5940 &calldata->lsp->ls_stateid))
5941 rpc_restart_call_prepare(task);
5944 if (nfs4_async_handle_error(task, calldata->server,
5945 NULL, NULL) == -EAGAIN)
5946 rpc_restart_call_prepare(task);
5948 nfs_release_seqid(calldata->arg.seqid);
5951 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5953 struct nfs4_unlockdata *calldata = data;
5955 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
5956 nfs_async_iocounter_wait(task, calldata->l_ctx))
5959 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5961 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5962 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5963 /* Note: exit _without_ running nfs4_locku_done */
5966 calldata->timestamp = jiffies;
5967 if (nfs4_setup_sequence(calldata->server->nfs_client,
5968 &calldata->arg.seq_args,
5969 &calldata->res.seq_res,
5971 nfs_release_seqid(calldata->arg.seqid);
5974 task->tk_action = NULL;
5976 nfs4_sequence_done(task, &calldata->res.seq_res);
5979 static const struct rpc_call_ops nfs4_locku_ops = {
5980 .rpc_call_prepare = nfs4_locku_prepare,
5981 .rpc_call_done = nfs4_locku_done,
5982 .rpc_release = nfs4_locku_release_calldata,
5985 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5986 struct nfs_open_context *ctx,
5987 struct nfs4_lock_state *lsp,
5988 struct nfs_seqid *seqid)
5990 struct nfs4_unlockdata *data;
5991 struct rpc_message msg = {
5992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5993 .rpc_cred = ctx->cred,
5995 struct rpc_task_setup task_setup_data = {
5996 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5997 .rpc_message = &msg,
5998 .callback_ops = &nfs4_locku_ops,
5999 .workqueue = nfsiod_workqueue,
6000 .flags = RPC_TASK_ASYNC,
6003 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6004 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6006 /* Ensure this is an unlock - when canceling a lock, the
6007 * canceled lock is passed in, and it won't be an unlock.
6009 fl->fl_type = F_UNLCK;
6010 if (fl->fl_flags & FL_CLOSE)
6011 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6013 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6015 nfs_free_seqid(seqid);
6016 return ERR_PTR(-ENOMEM);
6019 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6020 msg.rpc_argp = &data->arg;
6021 msg.rpc_resp = &data->res;
6022 task_setup_data.callback_data = data;
6023 return rpc_run_task(&task_setup_data);
6026 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6028 struct inode *inode = state->inode;
6029 struct nfs4_state_owner *sp = state->owner;
6030 struct nfs_inode *nfsi = NFS_I(inode);
6031 struct nfs_seqid *seqid;
6032 struct nfs4_lock_state *lsp;
6033 struct rpc_task *task;
6034 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6036 unsigned char fl_flags = request->fl_flags;
6038 status = nfs4_set_lock_state(state, request);
6039 /* Unlock _before_ we do the RPC call */
6040 request->fl_flags |= FL_EXISTS;
6041 /* Exclude nfs_delegation_claim_locks() */
6042 mutex_lock(&sp->so_delegreturn_mutex);
6043 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6044 down_read(&nfsi->rwsem);
6045 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6046 up_read(&nfsi->rwsem);
6047 mutex_unlock(&sp->so_delegreturn_mutex);
6050 up_read(&nfsi->rwsem);
6051 mutex_unlock(&sp->so_delegreturn_mutex);
6054 /* Is this a delegated lock? */
6055 lsp = request->fl_u.nfs4_fl.owner;
6056 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6058 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6059 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6063 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6064 status = PTR_ERR(task);
6067 status = rpc_wait_for_completion_task(task);
6070 request->fl_flags = fl_flags;
6071 trace_nfs4_unlock(request, state, F_SETLK, status);
6075 struct nfs4_lockdata {
6076 struct nfs_lock_args arg;
6077 struct nfs_lock_res res;
6078 struct nfs4_lock_state *lsp;
6079 struct nfs_open_context *ctx;
6080 struct file_lock fl;
6081 unsigned long timestamp;
6084 struct nfs_server *server;
6087 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6088 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6091 struct nfs4_lockdata *p;
6092 struct inode *inode = lsp->ls_state->inode;
6093 struct nfs_server *server = NFS_SERVER(inode);
6094 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6096 p = kzalloc(sizeof(*p), gfp_mask);
6100 p->arg.fh = NFS_FH(inode);
6102 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6103 if (IS_ERR(p->arg.open_seqid))
6105 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6106 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6107 if (IS_ERR(p->arg.lock_seqid))
6108 goto out_free_seqid;
6109 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6110 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6111 p->arg.lock_owner.s_dev = server->s_dev;
6112 p->res.lock_seqid = p->arg.lock_seqid;
6115 atomic_inc(&lsp->ls_count);
6116 p->ctx = get_nfs_open_context(ctx);
6117 memcpy(&p->fl, fl, sizeof(p->fl));
6120 nfs_free_seqid(p->arg.open_seqid);
6126 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6128 struct nfs4_lockdata *data = calldata;
6129 struct nfs4_state *state = data->lsp->ls_state;
6131 dprintk("%s: begin!\n", __func__);
6132 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6134 /* Do we need to do an open_to_lock_owner? */
6135 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6136 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6137 goto out_release_lock_seqid;
6139 nfs4_stateid_copy(&data->arg.open_stateid,
6140 &state->open_stateid);
6141 data->arg.new_lock_owner = 1;
6142 data->res.open_seqid = data->arg.open_seqid;
6144 data->arg.new_lock_owner = 0;
6145 nfs4_stateid_copy(&data->arg.lock_stateid,
6146 &data->lsp->ls_stateid);
6148 if (!nfs4_valid_open_stateid(state)) {
6149 data->rpc_status = -EBADF;
6150 task->tk_action = NULL;
6151 goto out_release_open_seqid;
6153 data->timestamp = jiffies;
6154 if (nfs4_setup_sequence(data->server->nfs_client,
6155 &data->arg.seq_args,
6159 out_release_open_seqid:
6160 nfs_release_seqid(data->arg.open_seqid);
6161 out_release_lock_seqid:
6162 nfs_release_seqid(data->arg.lock_seqid);
6164 nfs4_sequence_done(task, &data->res.seq_res);
6165 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6168 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6170 struct nfs4_lockdata *data = calldata;
6171 struct nfs4_lock_state *lsp = data->lsp;
6173 dprintk("%s: begin!\n", __func__);
6175 if (!nfs4_sequence_done(task, &data->res.seq_res))
6178 data->rpc_status = task->tk_status;
6179 switch (task->tk_status) {
6181 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6183 if (data->arg.new_lock) {
6184 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6185 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6186 rpc_restart_call_prepare(task);
6190 if (data->arg.new_lock_owner != 0) {
6191 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6192 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6193 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6194 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6195 rpc_restart_call_prepare(task);
6197 case -NFS4ERR_BAD_STATEID:
6198 case -NFS4ERR_OLD_STATEID:
6199 case -NFS4ERR_STALE_STATEID:
6200 case -NFS4ERR_EXPIRED:
6201 if (data->arg.new_lock_owner != 0) {
6202 if (!nfs4_stateid_match(&data->arg.open_stateid,
6203 &lsp->ls_state->open_stateid))
6204 rpc_restart_call_prepare(task);
6205 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6207 rpc_restart_call_prepare(task);
6209 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6212 static void nfs4_lock_release(void *calldata)
6214 struct nfs4_lockdata *data = calldata;
6216 dprintk("%s: begin!\n", __func__);
6217 nfs_free_seqid(data->arg.open_seqid);
6218 if (data->cancelled) {
6219 struct rpc_task *task;
6220 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6221 data->arg.lock_seqid);
6223 rpc_put_task_async(task);
6224 dprintk("%s: cancelling lock!\n", __func__);
6226 nfs_free_seqid(data->arg.lock_seqid);
6227 nfs4_put_lock_state(data->lsp);
6228 put_nfs_open_context(data->ctx);
6230 dprintk("%s: done!\n", __func__);
6233 static const struct rpc_call_ops nfs4_lock_ops = {
6234 .rpc_call_prepare = nfs4_lock_prepare,
6235 .rpc_call_done = nfs4_lock_done,
6236 .rpc_release = nfs4_lock_release,
6239 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6242 case -NFS4ERR_ADMIN_REVOKED:
6243 case -NFS4ERR_EXPIRED:
6244 case -NFS4ERR_BAD_STATEID:
6245 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6246 if (new_lock_owner != 0 ||
6247 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6248 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6250 case -NFS4ERR_STALE_STATEID:
6251 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6252 nfs4_schedule_lease_recovery(server->nfs_client);
6256 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6258 struct nfs4_lockdata *data;
6259 struct rpc_task *task;
6260 struct rpc_message msg = {
6261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6262 .rpc_cred = state->owner->so_cred,
6264 struct rpc_task_setup task_setup_data = {
6265 .rpc_client = NFS_CLIENT(state->inode),
6266 .rpc_message = &msg,
6267 .callback_ops = &nfs4_lock_ops,
6268 .workqueue = nfsiod_workqueue,
6269 .flags = RPC_TASK_ASYNC,
6273 dprintk("%s: begin!\n", __func__);
6274 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6275 fl->fl_u.nfs4_fl.owner,
6276 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6280 data->arg.block = 1;
6281 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6282 msg.rpc_argp = &data->arg;
6283 msg.rpc_resp = &data->res;
6284 task_setup_data.callback_data = data;
6285 if (recovery_type > NFS_LOCK_NEW) {
6286 if (recovery_type == NFS_LOCK_RECLAIM)
6287 data->arg.reclaim = NFS_LOCK_RECLAIM;
6288 nfs4_set_sequence_privileged(&data->arg.seq_args);
6290 data->arg.new_lock = 1;
6291 task = rpc_run_task(&task_setup_data);
6293 return PTR_ERR(task);
6294 ret = rpc_wait_for_completion_task(task);
6296 ret = data->rpc_status;
6298 nfs4_handle_setlk_error(data->server, data->lsp,
6299 data->arg.new_lock_owner, ret);
6301 data->cancelled = true;
6303 dprintk("%s: done, ret = %d!\n", __func__, ret);
6304 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6308 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6310 struct nfs_server *server = NFS_SERVER(state->inode);
6311 struct nfs4_exception exception = {
6312 .inode = state->inode,
6317 /* Cache the lock if possible... */
6318 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6320 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6321 if (err != -NFS4ERR_DELAY)
6323 nfs4_handle_exception(server, err, &exception);
6324 } while (exception.retry);
6328 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6330 struct nfs_server *server = NFS_SERVER(state->inode);
6331 struct nfs4_exception exception = {
6332 .inode = state->inode,
6336 err = nfs4_set_lock_state(state, request);
6339 if (!recover_lost_locks) {
6340 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6344 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6346 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6350 case -NFS4ERR_GRACE:
6351 case -NFS4ERR_DELAY:
6352 nfs4_handle_exception(server, err, &exception);
6355 } while (exception.retry);
6360 #if defined(CONFIG_NFS_V4_1)
6361 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6363 struct nfs4_lock_state *lsp;
6366 status = nfs4_set_lock_state(state, request);
6369 lsp = request->fl_u.nfs4_fl.owner;
6370 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6371 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6373 return nfs4_lock_expired(state, request);
6377 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6379 struct nfs_inode *nfsi = NFS_I(state->inode);
6380 struct nfs4_state_owner *sp = state->owner;
6381 unsigned char fl_flags = request->fl_flags;
6384 request->fl_flags |= FL_ACCESS;
6385 status = locks_lock_inode_wait(state->inode, request);
6388 mutex_lock(&sp->so_delegreturn_mutex);
6389 down_read(&nfsi->rwsem);
6390 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6391 /* Yes: cache locks! */
6392 /* ...but avoid races with delegation recall... */
6393 request->fl_flags = fl_flags & ~FL_SLEEP;
6394 status = locks_lock_inode_wait(state->inode, request);
6395 up_read(&nfsi->rwsem);
6396 mutex_unlock(&sp->so_delegreturn_mutex);
6399 up_read(&nfsi->rwsem);
6400 mutex_unlock(&sp->so_delegreturn_mutex);
6401 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6403 request->fl_flags = fl_flags;
6407 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6409 struct nfs4_exception exception = {
6411 .inode = state->inode,
6416 err = _nfs4_proc_setlk(state, cmd, request);
6417 if (err == -NFS4ERR_DENIED)
6419 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6421 } while (exception.retry);
6425 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6426 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6429 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6430 struct file_lock *request)
6432 int status = -ERESTARTSYS;
6433 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6435 while(!signalled()) {
6436 status = nfs4_proc_setlk(state, cmd, request);
6437 if ((status != -EAGAIN) || IS_SETLK(cmd))
6439 freezable_schedule_timeout_interruptible(timeout);
6441 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6442 status = -ERESTARTSYS;
6447 #ifdef CONFIG_NFS_V4_1
6448 struct nfs4_lock_waiter {
6449 struct task_struct *task;
6450 struct inode *inode;
6451 struct nfs_lowner *owner;
6456 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6459 struct cb_notify_lock_args *cbnl = key;
6460 struct nfs4_lock_waiter *waiter = wait->private;
6461 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6462 *wowner = waiter->owner;
6464 /* Only wake if the callback was for the same owner */
6465 if (lowner->clientid != wowner->clientid ||
6466 lowner->id != wowner->id ||
6467 lowner->s_dev != wowner->s_dev)
6470 /* Make sure it's for the right inode */
6471 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6474 waiter->notified = true;
6476 /* override "private" so we can use default_wake_function */
6477 wait->private = waiter->task;
6478 ret = autoremove_wake_function(wait, mode, flags, key);
6479 wait->private = waiter;
6484 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6486 int status = -ERESTARTSYS;
6487 unsigned long flags;
6488 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6489 struct nfs_server *server = NFS_SERVER(state->inode);
6490 struct nfs_client *clp = server->nfs_client;
6491 wait_queue_head_t *q = &clp->cl_lock_waitq;
6492 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6493 .id = lsp->ls_seqid.owner_id,
6494 .s_dev = server->s_dev };
6495 struct nfs4_lock_waiter waiter = { .task = current,
6496 .inode = state->inode,
6498 .notified = false };
6499 wait_queue_entry_t wait;
6501 /* Don't bother with waitqueue if we don't expect a callback */
6502 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6503 return nfs4_retry_setlk_simple(state, cmd, request);
6506 wait.private = &waiter;
6507 wait.func = nfs4_wake_lock_waiter;
6508 add_wait_queue(q, &wait);
6510 while(!signalled()) {
6511 status = nfs4_proc_setlk(state, cmd, request);
6512 if ((status != -EAGAIN) || IS_SETLK(cmd))
6515 status = -ERESTARTSYS;
6516 spin_lock_irqsave(&q->lock, flags);
6517 if (waiter.notified) {
6518 spin_unlock_irqrestore(&q->lock, flags);
6521 set_current_state(TASK_INTERRUPTIBLE);
6522 spin_unlock_irqrestore(&q->lock, flags);
6524 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6527 finish_wait(q, &wait);
6530 #else /* !CONFIG_NFS_V4_1 */
6532 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6534 return nfs4_retry_setlk_simple(state, cmd, request);
6539 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6541 struct nfs_open_context *ctx;
6542 struct nfs4_state *state;
6545 /* verify open state */
6546 ctx = nfs_file_open_context(filp);
6549 if (IS_GETLK(cmd)) {
6551 return nfs4_proc_getlk(state, F_GETLK, request);
6555 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6558 if (request->fl_type == F_UNLCK) {
6560 return nfs4_proc_unlck(state, cmd, request);
6567 if ((request->fl_flags & FL_POSIX) &&
6568 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6571 status = nfs4_set_lock_state(state, request);
6575 return nfs4_retry_setlk(state, cmd, request);
6578 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6580 struct nfs_server *server = NFS_SERVER(state->inode);
6583 err = nfs4_set_lock_state(state, fl);
6586 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6587 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6590 struct nfs_release_lockowner_data {
6591 struct nfs4_lock_state *lsp;
6592 struct nfs_server *server;
6593 struct nfs_release_lockowner_args args;
6594 struct nfs_release_lockowner_res res;
6595 unsigned long timestamp;
6598 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6600 struct nfs_release_lockowner_data *data = calldata;
6601 struct nfs_server *server = data->server;
6602 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6603 &data->res.seq_res, task);
6604 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6605 data->timestamp = jiffies;
6608 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6610 struct nfs_release_lockowner_data *data = calldata;
6611 struct nfs_server *server = data->server;
6613 nfs40_sequence_done(task, &data->res.seq_res);
6615 switch (task->tk_status) {
6617 renew_lease(server, data->timestamp);
6619 case -NFS4ERR_STALE_CLIENTID:
6620 case -NFS4ERR_EXPIRED:
6621 nfs4_schedule_lease_recovery(server->nfs_client);
6623 case -NFS4ERR_LEASE_MOVED:
6624 case -NFS4ERR_DELAY:
6625 if (nfs4_async_handle_error(task, server,
6626 NULL, NULL) == -EAGAIN)
6627 rpc_restart_call_prepare(task);
6631 static void nfs4_release_lockowner_release(void *calldata)
6633 struct nfs_release_lockowner_data *data = calldata;
6634 nfs4_free_lock_state(data->server, data->lsp);
6638 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6639 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6640 .rpc_call_done = nfs4_release_lockowner_done,
6641 .rpc_release = nfs4_release_lockowner_release,
6645 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6647 struct nfs_release_lockowner_data *data;
6648 struct rpc_message msg = {
6649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6652 if (server->nfs_client->cl_mvops->minor_version != 0)
6655 data = kmalloc(sizeof(*data), GFP_NOFS);
6659 data->server = server;
6660 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6661 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6662 data->args.lock_owner.s_dev = server->s_dev;
6664 msg.rpc_argp = &data->args;
6665 msg.rpc_resp = &data->res;
6666 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6667 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6670 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6672 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6673 struct dentry *unused, struct inode *inode,
6674 const char *key, const void *buf,
6675 size_t buflen, int flags)
6677 return nfs4_proc_set_acl(inode, buf, buflen);
6680 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6681 struct dentry *unused, struct inode *inode,
6682 const char *key, void *buf, size_t buflen)
6684 return nfs4_proc_get_acl(inode, buf, buflen);
6687 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6689 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6692 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6694 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6695 struct dentry *unused, struct inode *inode,
6696 const char *key, const void *buf,
6697 size_t buflen, int flags)
6699 if (security_ismaclabel(key))
6700 return nfs4_set_security_label(inode, buf, buflen);
6705 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6706 struct dentry *unused, struct inode *inode,
6707 const char *key, void *buf, size_t buflen)
6709 if (security_ismaclabel(key))
6710 return nfs4_get_security_label(inode, buf, buflen);
6715 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6719 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6720 len = security_inode_listsecurity(inode, list, list_len);
6721 if (list_len && len > list_len)
6727 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6728 .prefix = XATTR_SECURITY_PREFIX,
6729 .get = nfs4_xattr_get_nfs4_label,
6730 .set = nfs4_xattr_set_nfs4_label,
6736 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6744 * nfs_fhget will use either the mounted_on_fileid or the fileid
6746 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6748 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6749 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6750 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6751 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6754 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6755 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6756 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6760 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6761 const struct qstr *name,
6762 struct nfs4_fs_locations *fs_locations,
6765 struct nfs_server *server = NFS_SERVER(dir);
6767 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6769 struct nfs4_fs_locations_arg args = {
6770 .dir_fh = NFS_FH(dir),
6775 struct nfs4_fs_locations_res res = {
6776 .fs_locations = fs_locations,
6778 struct rpc_message msg = {
6779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6785 dprintk("%s: start\n", __func__);
6787 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6788 * is not supported */
6789 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6790 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6792 bitmask[0] |= FATTR4_WORD0_FILEID;
6794 nfs_fattr_init(&fs_locations->fattr);
6795 fs_locations->server = server;
6796 fs_locations->nlocations = 0;
6797 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6798 dprintk("%s: returned status = %d\n", __func__, status);
6802 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6803 const struct qstr *name,
6804 struct nfs4_fs_locations *fs_locations,
6807 struct nfs4_exception exception = { };
6810 err = _nfs4_proc_fs_locations(client, dir, name,
6811 fs_locations, page);
6812 trace_nfs4_get_fs_locations(dir, name, err);
6813 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6815 } while (exception.retry);
6820 * This operation also signals the server that this client is
6821 * performing migration recovery. The server can stop returning
6822 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6823 * appended to this compound to identify the client ID which is
6824 * performing recovery.
6826 static int _nfs40_proc_get_locations(struct inode *inode,
6827 struct nfs4_fs_locations *locations,
6828 struct page *page, struct rpc_cred *cred)
6830 struct nfs_server *server = NFS_SERVER(inode);
6831 struct rpc_clnt *clnt = server->client;
6833 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6835 struct nfs4_fs_locations_arg args = {
6836 .clientid = server->nfs_client->cl_clientid,
6837 .fh = NFS_FH(inode),
6840 .migration = 1, /* skip LOOKUP */
6841 .renew = 1, /* append RENEW */
6843 struct nfs4_fs_locations_res res = {
6844 .fs_locations = locations,
6848 struct rpc_message msg = {
6849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6854 unsigned long now = jiffies;
6857 nfs_fattr_init(&locations->fattr);
6858 locations->server = server;
6859 locations->nlocations = 0;
6861 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6862 nfs4_set_sequence_privileged(&args.seq_args);
6863 status = nfs4_call_sync_sequence(clnt, server, &msg,
6864 &args.seq_args, &res.seq_res);
6868 renew_lease(server, now);
6872 #ifdef CONFIG_NFS_V4_1
6875 * This operation also signals the server that this client is
6876 * performing migration recovery. The server can stop asserting
6877 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6878 * performing this operation is identified in the SEQUENCE
6879 * operation in this compound.
6881 * When the client supports GETATTR(fs_locations_info), it can
6882 * be plumbed in here.
6884 static int _nfs41_proc_get_locations(struct inode *inode,
6885 struct nfs4_fs_locations *locations,
6886 struct page *page, struct rpc_cred *cred)
6888 struct nfs_server *server = NFS_SERVER(inode);
6889 struct rpc_clnt *clnt = server->client;
6891 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6893 struct nfs4_fs_locations_arg args = {
6894 .fh = NFS_FH(inode),
6897 .migration = 1, /* skip LOOKUP */
6899 struct nfs4_fs_locations_res res = {
6900 .fs_locations = locations,
6903 struct rpc_message msg = {
6904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6911 nfs_fattr_init(&locations->fattr);
6912 locations->server = server;
6913 locations->nlocations = 0;
6915 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6916 nfs4_set_sequence_privileged(&args.seq_args);
6917 status = nfs4_call_sync_sequence(clnt, server, &msg,
6918 &args.seq_args, &res.seq_res);
6919 if (status == NFS4_OK &&
6920 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6921 status = -NFS4ERR_LEASE_MOVED;
6925 #endif /* CONFIG_NFS_V4_1 */
6928 * nfs4_proc_get_locations - discover locations for a migrated FSID
6929 * @inode: inode on FSID that is migrating
6930 * @locations: result of query
6932 * @cred: credential to use for this operation
6934 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6935 * operation failed, or a negative errno if a local error occurred.
6937 * On success, "locations" is filled in, but if the server has
6938 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6941 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6942 * from this client that require migration recovery.
6944 int nfs4_proc_get_locations(struct inode *inode,
6945 struct nfs4_fs_locations *locations,
6946 struct page *page, struct rpc_cred *cred)
6948 struct nfs_server *server = NFS_SERVER(inode);
6949 struct nfs_client *clp = server->nfs_client;
6950 const struct nfs4_mig_recovery_ops *ops =
6951 clp->cl_mvops->mig_recovery_ops;
6952 struct nfs4_exception exception = { };
6955 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6956 (unsigned long long)server->fsid.major,
6957 (unsigned long long)server->fsid.minor,
6959 nfs_display_fhandle(NFS_FH(inode), __func__);
6962 status = ops->get_locations(inode, locations, page, cred);
6963 if (status != -NFS4ERR_DELAY)
6965 nfs4_handle_exception(server, status, &exception);
6966 } while (exception.retry);
6971 * This operation also signals the server that this client is
6972 * performing "lease moved" recovery. The server can stop
6973 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6974 * is appended to this compound to identify the client ID which is
6975 * performing recovery.
6977 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6979 struct nfs_server *server = NFS_SERVER(inode);
6980 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6981 struct rpc_clnt *clnt = server->client;
6982 struct nfs4_fsid_present_arg args = {
6983 .fh = NFS_FH(inode),
6984 .clientid = clp->cl_clientid,
6985 .renew = 1, /* append RENEW */
6987 struct nfs4_fsid_present_res res = {
6990 struct rpc_message msg = {
6991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6996 unsigned long now = jiffies;
6999 res.fh = nfs_alloc_fhandle();
7003 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7004 nfs4_set_sequence_privileged(&args.seq_args);
7005 status = nfs4_call_sync_sequence(clnt, server, &msg,
7006 &args.seq_args, &res.seq_res);
7007 nfs_free_fhandle(res.fh);
7011 do_renew_lease(clp, now);
7015 #ifdef CONFIG_NFS_V4_1
7018 * This operation also signals the server that this client is
7019 * performing "lease moved" recovery. The server can stop asserting
7020 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7021 * this operation is identified in the SEQUENCE operation in this
7024 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7026 struct nfs_server *server = NFS_SERVER(inode);
7027 struct rpc_clnt *clnt = server->client;
7028 struct nfs4_fsid_present_arg args = {
7029 .fh = NFS_FH(inode),
7031 struct nfs4_fsid_present_res res = {
7033 struct rpc_message msg = {
7034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7041 res.fh = nfs_alloc_fhandle();
7045 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7046 nfs4_set_sequence_privileged(&args.seq_args);
7047 status = nfs4_call_sync_sequence(clnt, server, &msg,
7048 &args.seq_args, &res.seq_res);
7049 nfs_free_fhandle(res.fh);
7050 if (status == NFS4_OK &&
7051 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7052 status = -NFS4ERR_LEASE_MOVED;
7056 #endif /* CONFIG_NFS_V4_1 */
7059 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7060 * @inode: inode on FSID to check
7061 * @cred: credential to use for this operation
7063 * Server indicates whether the FSID is present, moved, or not
7064 * recognized. This operation is necessary to clear a LEASE_MOVED
7065 * condition for this client ID.
7067 * Returns NFS4_OK if the FSID is present on this server,
7068 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7069 * NFS4ERR code if some error occurred on the server, or a
7070 * negative errno if a local failure occurred.
7072 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7074 struct nfs_server *server = NFS_SERVER(inode);
7075 struct nfs_client *clp = server->nfs_client;
7076 const struct nfs4_mig_recovery_ops *ops =
7077 clp->cl_mvops->mig_recovery_ops;
7078 struct nfs4_exception exception = { };
7081 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7082 (unsigned long long)server->fsid.major,
7083 (unsigned long long)server->fsid.minor,
7085 nfs_display_fhandle(NFS_FH(inode), __func__);
7088 status = ops->fsid_present(inode, cred);
7089 if (status != -NFS4ERR_DELAY)
7091 nfs4_handle_exception(server, status, &exception);
7092 } while (exception.retry);
7097 * If 'use_integrity' is true and the state managment nfs_client
7098 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7099 * and the machine credential as per RFC3530bis and RFC5661 Security
7100 * Considerations sections. Otherwise, just use the user cred with the
7101 * filesystem's rpc_client.
7103 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7106 struct nfs4_secinfo_arg args = {
7107 .dir_fh = NFS_FH(dir),
7110 struct nfs4_secinfo_res res = {
7113 struct rpc_message msg = {
7114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7118 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7119 struct rpc_cred *cred = NULL;
7121 if (use_integrity) {
7122 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7123 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7124 msg.rpc_cred = cred;
7127 dprintk("NFS call secinfo %s\n", name->name);
7129 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7130 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7132 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7134 dprintk("NFS reply secinfo: %d\n", status);
7142 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7143 struct nfs4_secinfo_flavors *flavors)
7145 struct nfs4_exception exception = { };
7148 err = -NFS4ERR_WRONGSEC;
7150 /* try to use integrity protection with machine cred */
7151 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7152 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7155 * if unable to use integrity protection, or SECINFO with
7156 * integrity protection returns NFS4ERR_WRONGSEC (which is
7157 * disallowed by spec, but exists in deployed servers) use
7158 * the current filesystem's rpc_client and the user cred.
7160 if (err == -NFS4ERR_WRONGSEC)
7161 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7163 trace_nfs4_secinfo(dir, name, err);
7164 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7166 } while (exception.retry);
7170 #ifdef CONFIG_NFS_V4_1
7172 * Check the exchange flags returned by the server for invalid flags, having
7173 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7176 static int nfs4_check_cl_exchange_flags(u32 flags)
7178 if (flags & ~EXCHGID4_FLAG_MASK_R)
7180 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7181 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7183 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7187 return -NFS4ERR_INVAL;
7191 nfs41_same_server_scope(struct nfs41_server_scope *a,
7192 struct nfs41_server_scope *b)
7194 if (a->server_scope_sz != b->server_scope_sz)
7196 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7200 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7204 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7205 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7209 * nfs4_proc_bind_one_conn_to_session()
7211 * The 4.1 client currently uses the same TCP connection for the
7212 * fore and backchannel.
7215 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7216 struct rpc_xprt *xprt,
7217 struct nfs_client *clp,
7218 struct rpc_cred *cred)
7221 struct nfs41_bind_conn_to_session_args args = {
7223 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7225 struct nfs41_bind_conn_to_session_res res;
7226 struct rpc_message msg = {
7228 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7233 struct rpc_task_setup task_setup_data = {
7236 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7237 .rpc_message = &msg,
7238 .flags = RPC_TASK_TIMEOUT,
7240 struct rpc_task *task;
7242 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7243 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7244 args.dir = NFS4_CDFC4_FORE;
7246 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7247 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7248 args.dir = NFS4_CDFC4_FORE;
7250 task = rpc_run_task(&task_setup_data);
7251 if (!IS_ERR(task)) {
7252 status = task->tk_status;
7255 status = PTR_ERR(task);
7256 trace_nfs4_bind_conn_to_session(clp, status);
7258 if (memcmp(res.sessionid.data,
7259 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7260 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7263 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7264 dprintk("NFS: %s: Unexpected direction from server\n",
7268 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7269 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7278 struct rpc_bind_conn_calldata {
7279 struct nfs_client *clp;
7280 struct rpc_cred *cred;
7284 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7285 struct rpc_xprt *xprt,
7288 struct rpc_bind_conn_calldata *p = calldata;
7290 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7293 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7295 struct rpc_bind_conn_calldata data = {
7299 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7300 nfs4_proc_bind_conn_to_session_callback, &data);
7304 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7305 * and operations we'd like to see to enable certain features in the allow map
7307 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7308 .how = SP4_MACH_CRED,
7309 .enforce.u.words = {
7310 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7311 1 << (OP_EXCHANGE_ID - 32) |
7312 1 << (OP_CREATE_SESSION - 32) |
7313 1 << (OP_DESTROY_SESSION - 32) |
7314 1 << (OP_DESTROY_CLIENTID - 32)
7317 [0] = 1 << (OP_CLOSE) |
7318 1 << (OP_OPEN_DOWNGRADE) |
7320 1 << (OP_DELEGRETURN) |
7322 [1] = 1 << (OP_SECINFO - 32) |
7323 1 << (OP_SECINFO_NO_NAME - 32) |
7324 1 << (OP_LAYOUTRETURN - 32) |
7325 1 << (OP_TEST_STATEID - 32) |
7326 1 << (OP_FREE_STATEID - 32) |
7327 1 << (OP_WRITE - 32)
7332 * Select the state protection mode for client `clp' given the server results
7333 * from exchange_id in `sp'.
7335 * Returns 0 on success, negative errno otherwise.
7337 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7338 struct nfs41_state_protection *sp)
7340 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7341 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7342 1 << (OP_EXCHANGE_ID - 32) |
7343 1 << (OP_CREATE_SESSION - 32) |
7344 1 << (OP_DESTROY_SESSION - 32) |
7345 1 << (OP_DESTROY_CLIENTID - 32)
7347 unsigned long flags = 0;
7351 if (sp->how == SP4_MACH_CRED) {
7352 /* Print state protect result */
7353 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7354 for (i = 0; i <= LAST_NFS4_OP; i++) {
7355 if (test_bit(i, sp->enforce.u.longs))
7356 dfprintk(MOUNT, " enforce op %d\n", i);
7357 if (test_bit(i, sp->allow.u.longs))
7358 dfprintk(MOUNT, " allow op %d\n", i);
7361 /* make sure nothing is on enforce list that isn't supported */
7362 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7363 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7364 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7371 * Minimal mode - state operations are allowed to use machine
7372 * credential. Note this already happens by default, so the
7373 * client doesn't have to do anything more than the negotiation.
7375 * NOTE: we don't care if EXCHANGE_ID is in the list -
7376 * we're already using the machine cred for exchange_id
7377 * and will never use a different cred.
7379 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7380 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7381 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7382 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7383 dfprintk(MOUNT, "sp4_mach_cred:\n");
7384 dfprintk(MOUNT, " minimal mode enabled\n");
7385 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7387 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7392 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7393 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7394 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7395 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7396 dfprintk(MOUNT, " cleanup mode enabled\n");
7397 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7400 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7401 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7402 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7405 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7406 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7407 dfprintk(MOUNT, " secinfo mode enabled\n");
7408 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7411 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7412 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7413 dfprintk(MOUNT, " stateid mode enabled\n");
7414 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7417 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7418 dfprintk(MOUNT, " write mode enabled\n");
7419 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7422 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7423 dfprintk(MOUNT, " commit mode enabled\n");
7424 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7428 clp->cl_sp4_flags = flags;
7432 struct nfs41_exchange_id_data {
7433 struct nfs41_exchange_id_res res;
7434 struct nfs41_exchange_id_args args;
7437 static void nfs4_exchange_id_release(void *data)
7439 struct nfs41_exchange_id_data *cdata =
7440 (struct nfs41_exchange_id_data *)data;
7442 nfs_put_client(cdata->args.client);
7443 kfree(cdata->res.impl_id);
7444 kfree(cdata->res.server_scope);
7445 kfree(cdata->res.server_owner);
7449 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7450 .rpc_release = nfs4_exchange_id_release,
7454 * _nfs4_proc_exchange_id()
7456 * Wrapper for EXCHANGE_ID operation.
7458 static struct rpc_task *
7459 nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7460 u32 sp4_how, struct rpc_xprt *xprt)
7462 struct rpc_message msg = {
7463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7466 struct rpc_task_setup task_setup_data = {
7467 .rpc_client = clp->cl_rpcclient,
7468 .callback_ops = &nfs4_exchange_id_call_ops,
7469 .rpc_message = &msg,
7470 .flags = RPC_TASK_TIMEOUT,
7472 struct nfs41_exchange_id_data *calldata;
7475 if (!atomic_inc_not_zero(&clp->cl_count))
7476 return ERR_PTR(-EIO);
7479 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7483 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7485 status = nfs4_init_uniform_client_string(clp);
7489 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7492 if (unlikely(calldata->res.server_owner == NULL))
7495 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7497 if (unlikely(calldata->res.server_scope == NULL))
7498 goto out_server_owner;
7500 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7501 if (unlikely(calldata->res.impl_id == NULL))
7502 goto out_server_scope;
7506 calldata->args.state_protect.how = SP4_NONE;
7510 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7520 task_setup_data.rpc_xprt = xprt;
7521 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7522 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7523 sizeof(calldata->args.verifier.data));
7525 calldata->args.client = clp;
7526 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7527 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7528 #ifdef CONFIG_NFS_V4_1_MIGRATION
7529 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7531 msg.rpc_argp = &calldata->args;
7532 msg.rpc_resp = &calldata->res;
7533 task_setup_data.callback_data = calldata;
7535 return rpc_run_task(&task_setup_data);
7538 kfree(calldata->res.impl_id);
7540 kfree(calldata->res.server_scope);
7542 kfree(calldata->res.server_owner);
7546 nfs_put_client(clp);
7547 return ERR_PTR(status);
7551 * _nfs4_proc_exchange_id()
7553 * Wrapper for EXCHANGE_ID operation.
7555 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7558 struct rpc_task *task;
7559 struct nfs41_exchange_id_args *argp;
7560 struct nfs41_exchange_id_res *resp;
7563 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7565 return PTR_ERR(task);
7567 argp = task->tk_msg.rpc_argp;
7568 resp = task->tk_msg.rpc_resp;
7569 status = task->tk_status;
7573 status = nfs4_check_cl_exchange_flags(resp->flags);
7577 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7581 clp->cl_clientid = resp->clientid;
7582 clp->cl_exchange_flags = resp->flags;
7583 clp->cl_seqid = resp->seqid;
7584 /* Client ID is not confirmed */
7585 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7586 clear_bit(NFS4_SESSION_ESTABLISHED,
7587 &clp->cl_session->session_state);
7589 if (clp->cl_serverscope != NULL &&
7590 !nfs41_same_server_scope(clp->cl_serverscope,
7591 resp->server_scope)) {
7592 dprintk("%s: server_scope mismatch detected\n",
7594 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7597 swap(clp->cl_serverowner, resp->server_owner);
7598 swap(clp->cl_serverscope, resp->server_scope);
7599 swap(clp->cl_implid, resp->impl_id);
7601 /* Save the EXCHANGE_ID verifier session trunk tests */
7602 memcpy(clp->cl_confirm.data, argp->verifier.data,
7603 sizeof(clp->cl_confirm.data));
7605 trace_nfs4_exchange_id(clp, status);
7611 * nfs4_proc_exchange_id()
7613 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7615 * Since the clientid has expired, all compounds using sessions
7616 * associated with the stale clientid will be returning
7617 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7618 * be in some phase of session reset.
7620 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7622 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7624 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7627 /* try SP4_MACH_CRED if krb5i/p */
7628 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7629 authflavor == RPC_AUTH_GSS_KRB5P) {
7630 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7636 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7640 * nfs4_test_session_trunk
7642 * This is an add_xprt_test() test function called from
7643 * rpc_clnt_setup_test_and_add_xprt.
7645 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7646 * and is dereferrenced in nfs4_exchange_id_release
7648 * Upon success, add the new transport to the rpc_clnt
7650 * @clnt: struct rpc_clnt to get new transport
7651 * @xprt: the rpc_xprt to test
7652 * @data: call data for _nfs4_proc_exchange_id.
7654 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7657 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7658 struct rpc_task *task;
7663 dprintk("--> %s try %s\n", __func__,
7664 xprt->address_strings[RPC_DISPLAY_ADDR]);
7666 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7668 /* Test connection for session trunking. Async exchange_id call */
7669 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7671 return PTR_ERR(task);
7673 status = task->tk_status;
7675 status = nfs4_detect_session_trunking(adata->clp,
7676 task->tk_msg.rpc_resp, xprt);
7681 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7683 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7684 struct rpc_cred *cred)
7686 struct rpc_message msg = {
7687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7693 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7694 trace_nfs4_destroy_clientid(clp, status);
7696 dprintk("NFS: Got error %d from the server %s on "
7697 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7701 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7702 struct rpc_cred *cred)
7707 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7708 ret = _nfs4_proc_destroy_clientid(clp, cred);
7710 case -NFS4ERR_DELAY:
7711 case -NFS4ERR_CLIENTID_BUSY:
7721 int nfs4_destroy_clientid(struct nfs_client *clp)
7723 struct rpc_cred *cred;
7726 if (clp->cl_mvops->minor_version < 1)
7728 if (clp->cl_exchange_flags == 0)
7730 if (clp->cl_preserve_clid)
7732 cred = nfs4_get_clid_cred(clp);
7733 ret = nfs4_proc_destroy_clientid(clp, cred);
7738 case -NFS4ERR_STALE_CLIENTID:
7739 clp->cl_exchange_flags = 0;
7745 struct nfs4_get_lease_time_data {
7746 struct nfs4_get_lease_time_args *args;
7747 struct nfs4_get_lease_time_res *res;
7748 struct nfs_client *clp;
7751 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7754 struct nfs4_get_lease_time_data *data =
7755 (struct nfs4_get_lease_time_data *)calldata;
7757 dprintk("--> %s\n", __func__);
7758 /* just setup sequence, do not trigger session recovery
7759 since we're invoked within one */
7760 nfs4_setup_sequence(data->clp,
7761 &data->args->la_seq_args,
7762 &data->res->lr_seq_res,
7764 dprintk("<-- %s\n", __func__);
7768 * Called from nfs4_state_manager thread for session setup, so don't recover
7769 * from sequence operation or clientid errors.
7771 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7773 struct nfs4_get_lease_time_data *data =
7774 (struct nfs4_get_lease_time_data *)calldata;
7776 dprintk("--> %s\n", __func__);
7777 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7779 switch (task->tk_status) {
7780 case -NFS4ERR_DELAY:
7781 case -NFS4ERR_GRACE:
7782 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7783 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7784 task->tk_status = 0;
7786 case -NFS4ERR_RETRY_UNCACHED_REP:
7787 rpc_restart_call_prepare(task);
7790 dprintk("<-- %s\n", __func__);
7793 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7794 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7795 .rpc_call_done = nfs4_get_lease_time_done,
7798 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7800 struct rpc_task *task;
7801 struct nfs4_get_lease_time_args args;
7802 struct nfs4_get_lease_time_res res = {
7803 .lr_fsinfo = fsinfo,
7805 struct nfs4_get_lease_time_data data = {
7810 struct rpc_message msg = {
7811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7815 struct rpc_task_setup task_setup = {
7816 .rpc_client = clp->cl_rpcclient,
7817 .rpc_message = &msg,
7818 .callback_ops = &nfs4_get_lease_time_ops,
7819 .callback_data = &data,
7820 .flags = RPC_TASK_TIMEOUT,
7824 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7825 nfs4_set_sequence_privileged(&args.la_seq_args);
7826 task = rpc_run_task(&task_setup);
7829 return PTR_ERR(task);
7831 status = task->tk_status;
7837 * Initialize the values to be used by the client in CREATE_SESSION
7838 * If nfs4_init_session set the fore channel request and response sizes,
7841 * Set the back channel max_resp_sz_cached to zero to force the client to
7842 * always set csa_cachethis to FALSE because the current implementation
7843 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7845 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7846 struct rpc_clnt *clnt)
7848 unsigned int max_rqst_sz, max_resp_sz;
7849 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7851 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7852 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7854 /* Fore channel attributes */
7855 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7856 args->fc_attrs.max_resp_sz = max_resp_sz;
7857 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7858 args->fc_attrs.max_reqs = max_session_slots;
7860 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7861 "max_ops=%u max_reqs=%u\n",
7863 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7864 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7866 /* Back channel attributes */
7867 args->bc_attrs.max_rqst_sz = max_bc_payload;
7868 args->bc_attrs.max_resp_sz = max_bc_payload;
7869 args->bc_attrs.max_resp_sz_cached = 0;
7870 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7871 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7873 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7874 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7876 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7877 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7878 args->bc_attrs.max_reqs);
7881 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7882 struct nfs41_create_session_res *res)
7884 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7885 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7887 if (rcvd->max_resp_sz > sent->max_resp_sz)
7890 * Our requested max_ops is the minimum we need; we're not
7891 * prepared to break up compounds into smaller pieces than that.
7892 * So, no point even trying to continue if the server won't
7895 if (rcvd->max_ops < sent->max_ops)
7897 if (rcvd->max_reqs == 0)
7899 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7900 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7904 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7905 struct nfs41_create_session_res *res)
7907 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7908 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7910 if (!(res->flags & SESSION4_BACK_CHAN))
7912 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7914 if (rcvd->max_resp_sz < sent->max_resp_sz)
7916 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7918 if (rcvd->max_ops > sent->max_ops)
7920 if (rcvd->max_reqs > sent->max_reqs)
7926 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7927 struct nfs41_create_session_res *res)
7931 ret = nfs4_verify_fore_channel_attrs(args, res);
7934 return nfs4_verify_back_channel_attrs(args, res);
7937 static void nfs4_update_session(struct nfs4_session *session,
7938 struct nfs41_create_session_res *res)
7940 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7941 /* Mark client id and session as being confirmed */
7942 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7943 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7944 session->flags = res->flags;
7945 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7946 if (res->flags & SESSION4_BACK_CHAN)
7947 memcpy(&session->bc_attrs, &res->bc_attrs,
7948 sizeof(session->bc_attrs));
7951 static int _nfs4_proc_create_session(struct nfs_client *clp,
7952 struct rpc_cred *cred)
7954 struct nfs4_session *session = clp->cl_session;
7955 struct nfs41_create_session_args args = {
7957 .clientid = clp->cl_clientid,
7958 .seqid = clp->cl_seqid,
7959 .cb_program = NFS4_CALLBACK,
7961 struct nfs41_create_session_res res;
7963 struct rpc_message msg = {
7964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7971 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
7972 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7974 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7975 trace_nfs4_create_session(clp, status);
7978 case -NFS4ERR_STALE_CLIENTID:
7979 case -NFS4ERR_DELAY:
7988 /* Verify the session's negotiated channel_attrs values */
7989 status = nfs4_verify_channel_attrs(&args, &res);
7990 /* Increment the clientid slot sequence id */
7993 nfs4_update_session(session, &res);
8000 * Issues a CREATE_SESSION operation to the server.
8001 * It is the responsibility of the caller to verify the session is
8002 * expired before calling this routine.
8004 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8008 struct nfs4_session *session = clp->cl_session;
8010 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8012 status = _nfs4_proc_create_session(clp, cred);
8016 /* Init or reset the session slot tables */
8017 status = nfs4_setup_session_slot_tables(session);
8018 dprintk("slot table setup returned %d\n", status);
8022 ptr = (unsigned *)&session->sess_id.data[0];
8023 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8024 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8026 dprintk("<-- %s\n", __func__);
8031 * Issue the over-the-wire RPC DESTROY_SESSION.
8032 * The caller must serialize access to this routine.
8034 int nfs4_proc_destroy_session(struct nfs4_session *session,
8035 struct rpc_cred *cred)
8037 struct rpc_message msg = {
8038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8039 .rpc_argp = session,
8044 dprintk("--> nfs4_proc_destroy_session\n");
8046 /* session is still being setup */
8047 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8050 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8051 trace_nfs4_destroy_session(session->clp, status);
8054 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8055 "Session has been destroyed regardless...\n", status);
8057 dprintk("<-- nfs4_proc_destroy_session\n");
8062 * Renew the cl_session lease.
8064 struct nfs4_sequence_data {
8065 struct nfs_client *clp;
8066 struct nfs4_sequence_args args;
8067 struct nfs4_sequence_res res;
8070 static void nfs41_sequence_release(void *data)
8072 struct nfs4_sequence_data *calldata = data;
8073 struct nfs_client *clp = calldata->clp;
8075 if (atomic_read(&clp->cl_count) > 1)
8076 nfs4_schedule_state_renewal(clp);
8077 nfs_put_client(clp);
8081 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8083 switch(task->tk_status) {
8084 case -NFS4ERR_DELAY:
8085 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8088 nfs4_schedule_lease_recovery(clp);
8093 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8095 struct nfs4_sequence_data *calldata = data;
8096 struct nfs_client *clp = calldata->clp;
8098 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8101 trace_nfs4_sequence(clp, task->tk_status);
8102 if (task->tk_status < 0) {
8103 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8104 if (atomic_read(&clp->cl_count) == 1)
8107 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8108 rpc_restart_call_prepare(task);
8112 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8114 dprintk("<-- %s\n", __func__);
8117 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8119 struct nfs4_sequence_data *calldata = data;
8120 struct nfs_client *clp = calldata->clp;
8121 struct nfs4_sequence_args *args;
8122 struct nfs4_sequence_res *res;
8124 args = task->tk_msg.rpc_argp;
8125 res = task->tk_msg.rpc_resp;
8127 nfs4_setup_sequence(clp, args, res, task);
8130 static const struct rpc_call_ops nfs41_sequence_ops = {
8131 .rpc_call_done = nfs41_sequence_call_done,
8132 .rpc_call_prepare = nfs41_sequence_prepare,
8133 .rpc_release = nfs41_sequence_release,
8136 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8137 struct rpc_cred *cred,
8140 struct nfs4_sequence_data *calldata;
8141 struct rpc_message msg = {
8142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8145 struct rpc_task_setup task_setup_data = {
8146 .rpc_client = clp->cl_rpcclient,
8147 .rpc_message = &msg,
8148 .callback_ops = &nfs41_sequence_ops,
8149 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8152 if (!atomic_inc_not_zero(&clp->cl_count))
8153 return ERR_PTR(-EIO);
8154 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8155 if (calldata == NULL) {
8156 nfs_put_client(clp);
8157 return ERR_PTR(-ENOMEM);
8159 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8161 nfs4_set_sequence_privileged(&calldata->args);
8162 msg.rpc_argp = &calldata->args;
8163 msg.rpc_resp = &calldata->res;
8164 calldata->clp = clp;
8165 task_setup_data.callback_data = calldata;
8167 return rpc_run_task(&task_setup_data);
8170 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8172 struct rpc_task *task;
8175 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8177 task = _nfs41_proc_sequence(clp, cred, false);
8179 ret = PTR_ERR(task);
8181 rpc_put_task_async(task);
8182 dprintk("<-- %s status=%d\n", __func__, ret);
8186 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8188 struct rpc_task *task;
8191 task = _nfs41_proc_sequence(clp, cred, true);
8193 ret = PTR_ERR(task);
8196 ret = rpc_wait_for_completion_task(task);
8198 ret = task->tk_status;
8201 dprintk("<-- %s status=%d\n", __func__, ret);
8205 struct nfs4_reclaim_complete_data {
8206 struct nfs_client *clp;
8207 struct nfs41_reclaim_complete_args arg;
8208 struct nfs41_reclaim_complete_res res;
8211 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8213 struct nfs4_reclaim_complete_data *calldata = data;
8215 nfs4_setup_sequence(calldata->clp,
8216 &calldata->arg.seq_args,
8217 &calldata->res.seq_res,
8221 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8223 switch(task->tk_status) {
8225 case -NFS4ERR_COMPLETE_ALREADY:
8226 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8228 case -NFS4ERR_DELAY:
8229 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8231 case -NFS4ERR_RETRY_UNCACHED_REP:
8233 case -NFS4ERR_BADSESSION:
8234 case -NFS4ERR_DEADSESSION:
8235 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8236 nfs4_schedule_session_recovery(clp->cl_session,
8240 nfs4_schedule_lease_recovery(clp);
8245 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8247 struct nfs4_reclaim_complete_data *calldata = data;
8248 struct nfs_client *clp = calldata->clp;
8249 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8251 dprintk("--> %s\n", __func__);
8252 if (!nfs41_sequence_done(task, res))
8255 trace_nfs4_reclaim_complete(clp, task->tk_status);
8256 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8257 rpc_restart_call_prepare(task);
8260 dprintk("<-- %s\n", __func__);
8263 static void nfs4_free_reclaim_complete_data(void *data)
8265 struct nfs4_reclaim_complete_data *calldata = data;
8270 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8271 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8272 .rpc_call_done = nfs4_reclaim_complete_done,
8273 .rpc_release = nfs4_free_reclaim_complete_data,
8277 * Issue a global reclaim complete.
8279 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8280 struct rpc_cred *cred)
8282 struct nfs4_reclaim_complete_data *calldata;
8283 struct rpc_task *task;
8284 struct rpc_message msg = {
8285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8288 struct rpc_task_setup task_setup_data = {
8289 .rpc_client = clp->cl_rpcclient,
8290 .rpc_message = &msg,
8291 .callback_ops = &nfs4_reclaim_complete_call_ops,
8292 .flags = RPC_TASK_ASYNC,
8294 int status = -ENOMEM;
8296 dprintk("--> %s\n", __func__);
8297 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8298 if (calldata == NULL)
8300 calldata->clp = clp;
8301 calldata->arg.one_fs = 0;
8303 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8304 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8305 msg.rpc_argp = &calldata->arg;
8306 msg.rpc_resp = &calldata->res;
8307 task_setup_data.callback_data = calldata;
8308 task = rpc_run_task(&task_setup_data);
8310 status = PTR_ERR(task);
8313 status = rpc_wait_for_completion_task(task);
8315 status = task->tk_status;
8318 dprintk("<-- %s status=%d\n", __func__, status);
8323 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8325 struct nfs4_layoutget *lgp = calldata;
8326 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8328 dprintk("--> %s\n", __func__);
8329 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8330 &lgp->res.seq_res, task);
8331 dprintk("<-- %s\n", __func__);
8334 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8336 struct nfs4_layoutget *lgp = calldata;
8338 dprintk("--> %s\n", __func__);
8339 nfs41_sequence_process(task, &lgp->res.seq_res);
8340 dprintk("<-- %s\n", __func__);
8344 nfs4_layoutget_handle_exception(struct rpc_task *task,
8345 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8347 struct inode *inode = lgp->args.inode;
8348 struct nfs_server *server = NFS_SERVER(inode);
8349 struct pnfs_layout_hdr *lo;
8350 int nfs4err = task->tk_status;
8351 int err, status = 0;
8354 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8361 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8362 * on the file. set tk_status to -ENODATA to tell upper layer to
8365 case -NFS4ERR_LAYOUTUNAVAILABLE:
8369 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8370 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8372 case -NFS4ERR_BADLAYOUT:
8373 status = -EOVERFLOW;
8376 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8377 * (or clients) writing to the same RAID stripe except when
8378 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8380 * Treat it like we would RECALLCONFLICT -- we retry for a little
8381 * while, and then eventually give up.
8383 case -NFS4ERR_LAYOUTTRYLATER:
8384 if (lgp->args.minlength == 0) {
8385 status = -EOVERFLOW;
8390 case -NFS4ERR_RECALLCONFLICT:
8391 status = -ERECALLCONFLICT;
8393 case -NFS4ERR_DELEG_REVOKED:
8394 case -NFS4ERR_ADMIN_REVOKED:
8395 case -NFS4ERR_EXPIRED:
8396 case -NFS4ERR_BAD_STATEID:
8397 exception->timeout = 0;
8398 spin_lock(&inode->i_lock);
8399 lo = NFS_I(inode)->layout;
8400 /* If the open stateid was bad, then recover it. */
8401 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8402 nfs4_stateid_match_other(&lgp->args.stateid,
8403 &lgp->args.ctx->state->stateid)) {
8404 spin_unlock(&inode->i_lock);
8405 exception->state = lgp->args.ctx->state;
8406 exception->stateid = &lgp->args.stateid;
8411 * Mark the bad layout state as invalid, then retry
8413 pnfs_mark_layout_stateid_invalid(lo, &head);
8414 spin_unlock(&inode->i_lock);
8415 nfs_commit_inode(inode, 0);
8416 pnfs_free_lseg_list(&head);
8421 nfs4_sequence_free_slot(&lgp->res.seq_res);
8422 err = nfs4_handle_exception(server, nfs4err, exception);
8424 if (exception->retry)
8430 dprintk("<-- %s\n", __func__);
8434 static size_t max_response_pages(struct nfs_server *server)
8436 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8437 return nfs_page_array_len(0, max_resp_sz);
8440 static void nfs4_free_pages(struct page **pages, size_t size)
8447 for (i = 0; i < size; i++) {
8450 __free_page(pages[i]);
8455 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8457 struct page **pages;
8460 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8462 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8466 for (i = 0; i < size; i++) {
8467 pages[i] = alloc_page(gfp_flags);
8469 dprintk("%s: failed to allocate page\n", __func__);
8470 nfs4_free_pages(pages, size);
8478 static void nfs4_layoutget_release(void *calldata)
8480 struct nfs4_layoutget *lgp = calldata;
8481 struct inode *inode = lgp->args.inode;
8482 struct nfs_server *server = NFS_SERVER(inode);
8483 size_t max_pages = max_response_pages(server);
8485 dprintk("--> %s\n", __func__);
8486 nfs4_sequence_free_slot(&lgp->res.seq_res);
8487 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8488 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8489 put_nfs_open_context(lgp->args.ctx);
8491 dprintk("<-- %s\n", __func__);
8494 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8495 .rpc_call_prepare = nfs4_layoutget_prepare,
8496 .rpc_call_done = nfs4_layoutget_done,
8497 .rpc_release = nfs4_layoutget_release,
8500 struct pnfs_layout_segment *
8501 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8503 struct inode *inode = lgp->args.inode;
8504 struct nfs_server *server = NFS_SERVER(inode);
8505 size_t max_pages = max_response_pages(server);
8506 struct rpc_task *task;
8507 struct rpc_message msg = {
8508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8509 .rpc_argp = &lgp->args,
8510 .rpc_resp = &lgp->res,
8511 .rpc_cred = lgp->cred,
8513 struct rpc_task_setup task_setup_data = {
8514 .rpc_client = server->client,
8515 .rpc_message = &msg,
8516 .callback_ops = &nfs4_layoutget_call_ops,
8517 .callback_data = lgp,
8518 .flags = RPC_TASK_ASYNC,
8520 struct pnfs_layout_segment *lseg = NULL;
8521 struct nfs4_exception exception = {
8523 .timeout = *timeout,
8527 dprintk("--> %s\n", __func__);
8529 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8530 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8532 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8533 if (!lgp->args.layout.pages) {
8534 nfs4_layoutget_release(lgp);
8535 return ERR_PTR(-ENOMEM);
8537 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8539 lgp->res.layoutp = &lgp->args.layout;
8540 lgp->res.seq_res.sr_slot = NULL;
8541 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8543 task = rpc_run_task(&task_setup_data);
8545 return ERR_CAST(task);
8546 status = rpc_wait_for_completion_task(task);
8548 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8549 *timeout = exception.timeout;
8552 trace_nfs4_layoutget(lgp->args.ctx,
8558 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8559 if (status == 0 && lgp->res.layoutp->len)
8560 lseg = pnfs_layout_process(lgp);
8562 dprintk("<-- %s status=%d\n", __func__, status);
8564 return ERR_PTR(status);
8569 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8571 struct nfs4_layoutreturn *lrp = calldata;
8573 dprintk("--> %s\n", __func__);
8574 nfs4_setup_sequence(lrp->clp,
8575 &lrp->args.seq_args,
8580 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8582 struct nfs4_layoutreturn *lrp = calldata;
8583 struct nfs_server *server;
8585 dprintk("--> %s\n", __func__);
8587 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8590 server = NFS_SERVER(lrp->args.inode);
8591 switch (task->tk_status) {
8593 task->tk_status = 0;
8596 case -NFS4ERR_DELAY:
8597 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8599 nfs4_sequence_free_slot(&lrp->res.seq_res);
8600 rpc_restart_call_prepare(task);
8603 dprintk("<-- %s\n", __func__);
8606 static void nfs4_layoutreturn_release(void *calldata)
8608 struct nfs4_layoutreturn *lrp = calldata;
8609 struct pnfs_layout_hdr *lo = lrp->args.layout;
8611 dprintk("--> %s\n", __func__);
8612 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8613 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8614 nfs4_sequence_free_slot(&lrp->res.seq_res);
8615 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8616 lrp->ld_private.ops->free(&lrp->ld_private);
8617 pnfs_put_layout_hdr(lrp->args.layout);
8618 nfs_iput_and_deactive(lrp->inode);
8620 dprintk("<-- %s\n", __func__);
8623 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8624 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8625 .rpc_call_done = nfs4_layoutreturn_done,
8626 .rpc_release = nfs4_layoutreturn_release,
8629 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8631 struct rpc_task *task;
8632 struct rpc_message msg = {
8633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8634 .rpc_argp = &lrp->args,
8635 .rpc_resp = &lrp->res,
8636 .rpc_cred = lrp->cred,
8638 struct rpc_task_setup task_setup_data = {
8639 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8640 .rpc_message = &msg,
8641 .callback_ops = &nfs4_layoutreturn_call_ops,
8642 .callback_data = lrp,
8646 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8647 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8648 &task_setup_data.rpc_client, &msg);
8650 dprintk("--> %s\n", __func__);
8652 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8654 nfs4_layoutreturn_release(lrp);
8657 task_setup_data.flags |= RPC_TASK_ASYNC;
8659 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8660 task = rpc_run_task(&task_setup_data);
8662 return PTR_ERR(task);
8664 status = task->tk_status;
8665 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8666 dprintk("<-- %s status=%d\n", __func__, status);
8672 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8673 struct pnfs_device *pdev,
8674 struct rpc_cred *cred)
8676 struct nfs4_getdeviceinfo_args args = {
8678 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8679 NOTIFY_DEVICEID4_DELETE,
8681 struct nfs4_getdeviceinfo_res res = {
8684 struct rpc_message msg = {
8685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8692 dprintk("--> %s\n", __func__);
8693 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8694 if (res.notification & ~args.notify_types)
8695 dprintk("%s: unsupported notification\n", __func__);
8696 if (res.notification != args.notify_types)
8699 dprintk("<-- %s status=%d\n", __func__, status);
8704 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8705 struct pnfs_device *pdev,
8706 struct rpc_cred *cred)
8708 struct nfs4_exception exception = { };
8712 err = nfs4_handle_exception(server,
8713 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8715 } while (exception.retry);
8718 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8720 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8722 struct nfs4_layoutcommit_data *data = calldata;
8723 struct nfs_server *server = NFS_SERVER(data->args.inode);
8725 nfs4_setup_sequence(server->nfs_client,
8726 &data->args.seq_args,
8732 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8734 struct nfs4_layoutcommit_data *data = calldata;
8735 struct nfs_server *server = NFS_SERVER(data->args.inode);
8737 if (!nfs41_sequence_done(task, &data->res.seq_res))
8740 switch (task->tk_status) { /* Just ignore these failures */
8741 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8742 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8743 case -NFS4ERR_BADLAYOUT: /* no layout */
8744 case -NFS4ERR_GRACE: /* loca_recalim always false */
8745 task->tk_status = 0;
8749 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8750 rpc_restart_call_prepare(task);
8756 static void nfs4_layoutcommit_release(void *calldata)
8758 struct nfs4_layoutcommit_data *data = calldata;
8760 pnfs_cleanup_layoutcommit(data);
8761 nfs_post_op_update_inode_force_wcc(data->args.inode,
8763 put_rpccred(data->cred);
8764 nfs_iput_and_deactive(data->inode);
8768 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8769 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8770 .rpc_call_done = nfs4_layoutcommit_done,
8771 .rpc_release = nfs4_layoutcommit_release,
8775 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8777 struct rpc_message msg = {
8778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8779 .rpc_argp = &data->args,
8780 .rpc_resp = &data->res,
8781 .rpc_cred = data->cred,
8783 struct rpc_task_setup task_setup_data = {
8784 .task = &data->task,
8785 .rpc_client = NFS_CLIENT(data->args.inode),
8786 .rpc_message = &msg,
8787 .callback_ops = &nfs4_layoutcommit_ops,
8788 .callback_data = data,
8790 struct rpc_task *task;
8793 dprintk("NFS: initiating layoutcommit call. sync %d "
8794 "lbw: %llu inode %lu\n", sync,
8795 data->args.lastbytewritten,
8796 data->args.inode->i_ino);
8799 data->inode = nfs_igrab_and_active(data->args.inode);
8800 if (data->inode == NULL) {
8801 nfs4_layoutcommit_release(data);
8804 task_setup_data.flags = RPC_TASK_ASYNC;
8806 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8807 task = rpc_run_task(&task_setup_data);
8809 return PTR_ERR(task);
8811 status = task->tk_status;
8812 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8813 dprintk("%s: status %d\n", __func__, status);
8819 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8820 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8823 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8824 struct nfs_fsinfo *info,
8825 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8827 struct nfs41_secinfo_no_name_args args = {
8828 .style = SECINFO_STYLE_CURRENT_FH,
8830 struct nfs4_secinfo_res res = {
8833 struct rpc_message msg = {
8834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8838 struct rpc_clnt *clnt = server->client;
8839 struct rpc_cred *cred = NULL;
8842 if (use_integrity) {
8843 clnt = server->nfs_client->cl_rpcclient;
8844 cred = nfs4_get_clid_cred(server->nfs_client);
8845 msg.rpc_cred = cred;
8848 dprintk("--> %s\n", __func__);
8849 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8851 dprintk("<-- %s status=%d\n", __func__, status);
8860 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8861 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8863 struct nfs4_exception exception = { };
8866 /* first try using integrity protection */
8867 err = -NFS4ERR_WRONGSEC;
8869 /* try to use integrity protection with machine cred */
8870 if (_nfs4_is_integrity_protected(server->nfs_client))
8871 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8875 * if unable to use integrity protection, or SECINFO with
8876 * integrity protection returns NFS4ERR_WRONGSEC (which is
8877 * disallowed by spec, but exists in deployed servers) use
8878 * the current filesystem's rpc_client and the user cred.
8880 if (err == -NFS4ERR_WRONGSEC)
8881 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8886 case -NFS4ERR_WRONGSEC:
8890 err = nfs4_handle_exception(server, err, &exception);
8892 } while (exception.retry);
8898 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8899 struct nfs_fsinfo *info)
8903 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8904 struct nfs4_secinfo_flavors *flavors;
8905 struct nfs4_secinfo4 *secinfo;
8908 page = alloc_page(GFP_KERNEL);
8914 flavors = page_address(page);
8915 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8918 * Fall back on "guess and check" method if
8919 * the server doesn't support SECINFO_NO_NAME
8921 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8922 err = nfs4_find_root_sec(server, fhandle, info);
8928 for (i = 0; i < flavors->num_flavors; i++) {
8929 secinfo = &flavors->flavors[i];
8931 switch (secinfo->flavor) {
8935 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8936 &secinfo->flavor_info);
8939 flavor = RPC_AUTH_MAXFLAVOR;
8943 if (!nfs_auth_info_match(&server->auth_info, flavor))
8944 flavor = RPC_AUTH_MAXFLAVOR;
8946 if (flavor != RPC_AUTH_MAXFLAVOR) {
8947 err = nfs4_lookup_root_sec(server, fhandle,
8954 if (flavor == RPC_AUTH_MAXFLAVOR)
8965 static int _nfs41_test_stateid(struct nfs_server *server,
8966 nfs4_stateid *stateid,
8967 struct rpc_cred *cred)
8970 struct nfs41_test_stateid_args args = {
8973 struct nfs41_test_stateid_res res;
8974 struct rpc_message msg = {
8975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8980 struct rpc_clnt *rpc_client = server->client;
8982 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8985 dprintk("NFS call test_stateid %p\n", stateid);
8986 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8987 nfs4_set_sequence_privileged(&args.seq_args);
8988 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8989 &args.seq_args, &res.seq_res);
8990 if (status != NFS_OK) {
8991 dprintk("NFS reply test_stateid: failed, %d\n", status);
8994 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8998 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8999 int err, struct nfs4_exception *exception)
9001 exception->retry = 0;
9003 case -NFS4ERR_DELAY:
9004 case -NFS4ERR_RETRY_UNCACHED_REP:
9005 nfs4_handle_exception(server, err, exception);
9007 case -NFS4ERR_BADSESSION:
9008 case -NFS4ERR_BADSLOT:
9009 case -NFS4ERR_BAD_HIGH_SLOT:
9010 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9011 case -NFS4ERR_DEADSESSION:
9012 nfs4_do_handle_exception(server, err, exception);
9017 * nfs41_test_stateid - perform a TEST_STATEID operation
9019 * @server: server / transport on which to perform the operation
9020 * @stateid: state ID to test
9023 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9024 * Otherwise a negative NFS4ERR value is returned if the operation
9025 * failed or the state ID is not currently valid.
9027 static int nfs41_test_stateid(struct nfs_server *server,
9028 nfs4_stateid *stateid,
9029 struct rpc_cred *cred)
9031 struct nfs4_exception exception = { };
9034 err = _nfs41_test_stateid(server, stateid, cred);
9035 nfs4_handle_delay_or_session_error(server, err, &exception);
9036 } while (exception.retry);
9040 struct nfs_free_stateid_data {
9041 struct nfs_server *server;
9042 struct nfs41_free_stateid_args args;
9043 struct nfs41_free_stateid_res res;
9046 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9048 struct nfs_free_stateid_data *data = calldata;
9049 nfs4_setup_sequence(data->server->nfs_client,
9050 &data->args.seq_args,
9055 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9057 struct nfs_free_stateid_data *data = calldata;
9059 nfs41_sequence_done(task, &data->res.seq_res);
9061 switch (task->tk_status) {
9062 case -NFS4ERR_DELAY:
9063 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9064 rpc_restart_call_prepare(task);
9068 static void nfs41_free_stateid_release(void *calldata)
9073 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9074 .rpc_call_prepare = nfs41_free_stateid_prepare,
9075 .rpc_call_done = nfs41_free_stateid_done,
9076 .rpc_release = nfs41_free_stateid_release,
9079 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9080 const nfs4_stateid *stateid,
9081 struct rpc_cred *cred,
9084 struct rpc_message msg = {
9085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9088 struct rpc_task_setup task_setup = {
9089 .rpc_client = server->client,
9090 .rpc_message = &msg,
9091 .callback_ops = &nfs41_free_stateid_ops,
9092 .flags = RPC_TASK_ASYNC,
9094 struct nfs_free_stateid_data *data;
9096 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9097 &task_setup.rpc_client, &msg);
9099 dprintk("NFS call free_stateid %p\n", stateid);
9100 data = kmalloc(sizeof(*data), GFP_NOFS);
9102 return ERR_PTR(-ENOMEM);
9103 data->server = server;
9104 nfs4_stateid_copy(&data->args.stateid, stateid);
9106 task_setup.callback_data = data;
9108 msg.rpc_argp = &data->args;
9109 msg.rpc_resp = &data->res;
9110 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9112 nfs4_set_sequence_privileged(&data->args.seq_args);
9114 return rpc_run_task(&task_setup);
9118 * nfs41_free_stateid - perform a FREE_STATEID operation
9120 * @server: server / transport on which to perform the operation
9121 * @stateid: state ID to release
9123 * @is_recovery: set to true if this call needs to be privileged
9125 * Note: this function is always asynchronous.
9127 static int nfs41_free_stateid(struct nfs_server *server,
9128 const nfs4_stateid *stateid,
9129 struct rpc_cred *cred,
9132 struct rpc_task *task;
9134 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9136 return PTR_ERR(task);
9142 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9144 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9146 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9147 nfs4_free_lock_state(server, lsp);
9150 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9151 const nfs4_stateid *s2)
9153 if (s1->type != s2->type)
9156 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9159 if (s1->seqid == s2->seqid)
9162 return s1->seqid == 0 || s2->seqid == 0;
9165 #endif /* CONFIG_NFS_V4_1 */
9167 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9168 const nfs4_stateid *s2)
9170 return nfs4_stateid_match(s1, s2);
9174 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9175 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9176 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9177 .recover_open = nfs4_open_reclaim,
9178 .recover_lock = nfs4_lock_reclaim,
9179 .establish_clid = nfs4_init_clientid,
9180 .detect_trunking = nfs40_discover_server_trunking,
9183 #if defined(CONFIG_NFS_V4_1)
9184 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9185 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9186 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9187 .recover_open = nfs4_open_reclaim,
9188 .recover_lock = nfs4_lock_reclaim,
9189 .establish_clid = nfs41_init_clientid,
9190 .reclaim_complete = nfs41_proc_reclaim_complete,
9191 .detect_trunking = nfs41_discover_server_trunking,
9193 #endif /* CONFIG_NFS_V4_1 */
9195 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9196 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9197 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9198 .recover_open = nfs40_open_expired,
9199 .recover_lock = nfs4_lock_expired,
9200 .establish_clid = nfs4_init_clientid,
9203 #if defined(CONFIG_NFS_V4_1)
9204 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9205 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9206 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9207 .recover_open = nfs41_open_expired,
9208 .recover_lock = nfs41_lock_expired,
9209 .establish_clid = nfs41_init_clientid,
9211 #endif /* CONFIG_NFS_V4_1 */
9213 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9214 .sched_state_renewal = nfs4_proc_async_renew,
9215 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9216 .renew_lease = nfs4_proc_renew,
9219 #if defined(CONFIG_NFS_V4_1)
9220 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9221 .sched_state_renewal = nfs41_proc_async_sequence,
9222 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9223 .renew_lease = nfs4_proc_sequence,
9227 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9228 .get_locations = _nfs40_proc_get_locations,
9229 .fsid_present = _nfs40_proc_fsid_present,
9232 #if defined(CONFIG_NFS_V4_1)
9233 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9234 .get_locations = _nfs41_proc_get_locations,
9235 .fsid_present = _nfs41_proc_fsid_present,
9237 #endif /* CONFIG_NFS_V4_1 */
9239 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9241 .init_caps = NFS_CAP_READDIRPLUS
9242 | NFS_CAP_ATOMIC_OPEN
9243 | NFS_CAP_POSIX_LOCK,
9244 .init_client = nfs40_init_client,
9245 .shutdown_client = nfs40_shutdown_client,
9246 .match_stateid = nfs4_match_stateid,
9247 .find_root_sec = nfs4_find_root_sec,
9248 .free_lock_state = nfs4_release_lockowner,
9249 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9250 .alloc_seqid = nfs_alloc_seqid,
9251 .call_sync_ops = &nfs40_call_sync_ops,
9252 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9253 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9254 .state_renewal_ops = &nfs40_state_renewal_ops,
9255 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9258 #if defined(CONFIG_NFS_V4_1)
9259 static struct nfs_seqid *
9260 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9265 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9267 .init_caps = NFS_CAP_READDIRPLUS
9268 | NFS_CAP_ATOMIC_OPEN
9269 | NFS_CAP_POSIX_LOCK
9270 | NFS_CAP_STATEID_NFSV41
9271 | NFS_CAP_ATOMIC_OPEN_V1,
9272 .init_client = nfs41_init_client,
9273 .shutdown_client = nfs41_shutdown_client,
9274 .match_stateid = nfs41_match_stateid,
9275 .find_root_sec = nfs41_find_root_sec,
9276 .free_lock_state = nfs41_free_lock_state,
9277 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9278 .alloc_seqid = nfs_alloc_no_seqid,
9279 .session_trunk = nfs4_test_session_trunk,
9280 .call_sync_ops = &nfs41_call_sync_ops,
9281 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9282 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9283 .state_renewal_ops = &nfs41_state_renewal_ops,
9284 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9288 #if defined(CONFIG_NFS_V4_2)
9289 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9291 .init_caps = NFS_CAP_READDIRPLUS
9292 | NFS_CAP_ATOMIC_OPEN
9293 | NFS_CAP_POSIX_LOCK
9294 | NFS_CAP_STATEID_NFSV41
9295 | NFS_CAP_ATOMIC_OPEN_V1
9298 | NFS_CAP_DEALLOCATE
9300 | NFS_CAP_LAYOUTSTATS
9302 .init_client = nfs41_init_client,
9303 .shutdown_client = nfs41_shutdown_client,
9304 .match_stateid = nfs41_match_stateid,
9305 .find_root_sec = nfs41_find_root_sec,
9306 .free_lock_state = nfs41_free_lock_state,
9307 .call_sync_ops = &nfs41_call_sync_ops,
9308 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9309 .alloc_seqid = nfs_alloc_no_seqid,
9310 .session_trunk = nfs4_test_session_trunk,
9311 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9312 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9313 .state_renewal_ops = &nfs41_state_renewal_ops,
9314 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9318 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9319 [0] = &nfs_v4_0_minor_ops,
9320 #if defined(CONFIG_NFS_V4_1)
9321 [1] = &nfs_v4_1_minor_ops,
9323 #if defined(CONFIG_NFS_V4_2)
9324 [2] = &nfs_v4_2_minor_ops,
9328 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9330 ssize_t error, error2;
9332 error = generic_listxattr(dentry, list, size);
9340 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9343 return error + error2;
9346 static const struct inode_operations nfs4_dir_inode_operations = {
9347 .create = nfs_create,
9348 .lookup = nfs_lookup,
9349 .atomic_open = nfs_atomic_open,
9351 .unlink = nfs_unlink,
9352 .symlink = nfs_symlink,
9356 .rename = nfs_rename,
9357 .permission = nfs_permission,
9358 .getattr = nfs_getattr,
9359 .setattr = nfs_setattr,
9360 .listxattr = nfs4_listxattr,
9363 static const struct inode_operations nfs4_file_inode_operations = {
9364 .permission = nfs_permission,
9365 .getattr = nfs_getattr,
9366 .setattr = nfs_setattr,
9367 .listxattr = nfs4_listxattr,
9370 const struct nfs_rpc_ops nfs_v4_clientops = {
9371 .version = 4, /* protocol version */
9372 .dentry_ops = &nfs4_dentry_operations,
9373 .dir_inode_ops = &nfs4_dir_inode_operations,
9374 .file_inode_ops = &nfs4_file_inode_operations,
9375 .file_ops = &nfs4_file_operations,
9376 .getroot = nfs4_proc_get_root,
9377 .submount = nfs4_submount,
9378 .try_mount = nfs4_try_mount,
9379 .getattr = nfs4_proc_getattr,
9380 .setattr = nfs4_proc_setattr,
9381 .lookup = nfs4_proc_lookup,
9382 .lookupp = nfs4_proc_lookupp,
9383 .access = nfs4_proc_access,
9384 .readlink = nfs4_proc_readlink,
9385 .create = nfs4_proc_create,
9386 .remove = nfs4_proc_remove,
9387 .unlink_setup = nfs4_proc_unlink_setup,
9388 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9389 .unlink_done = nfs4_proc_unlink_done,
9390 .rename_setup = nfs4_proc_rename_setup,
9391 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9392 .rename_done = nfs4_proc_rename_done,
9393 .link = nfs4_proc_link,
9394 .symlink = nfs4_proc_symlink,
9395 .mkdir = nfs4_proc_mkdir,
9396 .rmdir = nfs4_proc_remove,
9397 .readdir = nfs4_proc_readdir,
9398 .mknod = nfs4_proc_mknod,
9399 .statfs = nfs4_proc_statfs,
9400 .fsinfo = nfs4_proc_fsinfo,
9401 .pathconf = nfs4_proc_pathconf,
9402 .set_capabilities = nfs4_server_capabilities,
9403 .decode_dirent = nfs4_decode_dirent,
9404 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9405 .read_setup = nfs4_proc_read_setup,
9406 .read_done = nfs4_read_done,
9407 .write_setup = nfs4_proc_write_setup,
9408 .write_done = nfs4_write_done,
9409 .commit_setup = nfs4_proc_commit_setup,
9410 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9411 .commit_done = nfs4_commit_done,
9412 .lock = nfs4_proc_lock,
9413 .clear_acl_cache = nfs4_zap_acl_attr,
9414 .close_context = nfs4_close_context,
9415 .open_context = nfs4_atomic_open,
9416 .have_delegation = nfs4_have_delegation,
9417 .return_delegation = nfs4_inode_return_delegation,
9418 .alloc_client = nfs4_alloc_client,
9419 .init_client = nfs4_init_client,
9420 .free_client = nfs4_free_client,
9421 .create_server = nfs4_create_server,
9422 .clone_server = nfs_clone_server,
9425 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9426 .name = XATTR_NAME_NFSV4_ACL,
9427 .list = nfs4_xattr_list_nfs4_acl,
9428 .get = nfs4_xattr_get_nfs4_acl,
9429 .set = nfs4_xattr_set_nfs4_acl,
9432 const struct xattr_handler *nfs4_xattr_handlers[] = {
9433 &nfs4_xattr_nfs4_acl_handler,
9434 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9435 &nfs4_xattr_nfs4_label_handler,