4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
59 #include "delegation.h"
65 #include "nfs4idmap.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
76 /* file attributes which can be mapped to nfs attributes */
77 #define NFS4_VALID_ATTRS (ATTR_MODE \
88 static int _nfs4_proc_open(struct nfs4_opendata *data);
89 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
90 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
91 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
92 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
94 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
96 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
97 struct nfs4_label *olabel);
98 #ifdef CONFIG_NFS_V4_1
99 static 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] = {
260 | FATTR4_WORD0_FILEID
261 | FATTR4_WORD0_FS_LOCATIONS,
263 | FATTR4_WORD1_OWNER_GROUP
264 | FATTR4_WORD1_RAWDEV
265 | FATTR4_WORD1_SPACE_USED
266 | FATTR4_WORD1_TIME_ACCESS
267 | FATTR4_WORD1_TIME_METADATA
268 | FATTR4_WORD1_TIME_MODIFY
269 | FATTR4_WORD1_MOUNTED_ON_FILEID,
272 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
273 struct nfs4_readdir_arg *readdir)
275 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
279 readdir->cookie = cookie;
280 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 * NFSv4 servers do not return entries for '.' and '..'
291 * Therefore, we fake these entries here. We let '.'
292 * have cookie 0 and '..' have cookie 1. Note that
293 * when talking to the server, we always send cookie 0
296 start = p = kmap_atomic(*readdir->pages);
299 *p++ = xdr_one; /* next */
300 *p++ = xdr_zero; /* cookie, first word */
301 *p++ = xdr_one; /* cookie, second word */
302 *p++ = xdr_one; /* entry len */
303 memcpy(p, ".\0\0\0", 4); /* entry */
305 *p++ = xdr_one; /* bitmap length */
306 *p++ = htonl(attrs); /* bitmap */
307 *p++ = htonl(12); /* attribute buffer length */
308 *p++ = htonl(NF4DIR);
309 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
312 *p++ = xdr_one; /* next */
313 *p++ = xdr_zero; /* cookie, first word */
314 *p++ = xdr_two; /* cookie, second word */
315 *p++ = xdr_two; /* entry len */
316 memcpy(p, "..\0\0", 4); /* entry */
318 *p++ = xdr_one; /* bitmap length */
319 *p++ = htonl(attrs); /* bitmap */
320 *p++ = htonl(12); /* attribute buffer length */
321 *p++ = htonl(NF4DIR);
322 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
324 readdir->pgbase = (char *)p - (char *)start;
325 readdir->count -= readdir->pgbase;
326 kunmap_atomic(start);
329 static void nfs4_test_and_free_stateid(struct nfs_server *server,
330 nfs4_stateid *stateid,
331 struct rpc_cred *cred)
333 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
335 ops->test_and_free_expired(server, stateid, cred);
338 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
339 nfs4_stateid *stateid,
340 struct rpc_cred *cred)
342 stateid->type = NFS4_REVOKED_STATEID_TYPE;
343 nfs4_test_and_free_stateid(server, stateid, cred);
346 static void nfs4_free_revoked_stateid(struct nfs_server *server,
347 const nfs4_stateid *stateid,
348 struct rpc_cred *cred)
352 nfs4_stateid_copy(&tmp, stateid);
353 __nfs4_free_revoked_stateid(server, &tmp, cred);
356 static long nfs4_update_delay(long *timeout)
360 return NFS4_POLL_RETRY_MAX;
362 *timeout = NFS4_POLL_RETRY_MIN;
363 if (*timeout > NFS4_POLL_RETRY_MAX)
364 *timeout = NFS4_POLL_RETRY_MAX;
370 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
376 freezable_schedule_timeout_killable_unsafe(
377 nfs4_update_delay(timeout));
378 if (fatal_signal_pending(current))
383 /* This is the error handling routine for processes that are allowed
386 static int nfs4_do_handle_exception(struct nfs_server *server,
387 int errorcode, struct nfs4_exception *exception)
389 struct nfs_client *clp = server->nfs_client;
390 struct nfs4_state *state = exception->state;
391 const nfs4_stateid *stateid = exception->stateid;
392 struct inode *inode = exception->inode;
395 exception->delay = 0;
396 exception->recovering = 0;
397 exception->retry = 0;
399 if (stateid == NULL && state != NULL)
400 stateid = &state->stateid;
405 case -NFS4ERR_DELEG_REVOKED:
406 case -NFS4ERR_ADMIN_REVOKED:
407 case -NFS4ERR_EXPIRED:
408 case -NFS4ERR_BAD_STATEID:
409 if (inode != NULL && stateid != NULL) {
410 nfs_inode_find_state_and_recover(inode,
412 goto wait_on_recovery;
414 case -NFS4ERR_OPENMODE:
418 err = nfs_async_inode_return_delegation(inode,
421 goto wait_on_recovery;
422 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
423 exception->retry = 1;
429 ret = nfs4_schedule_stateid_recovery(server, state);
432 goto wait_on_recovery;
433 case -NFS4ERR_STALE_STATEID:
434 case -NFS4ERR_STALE_CLIENTID:
435 nfs4_schedule_lease_recovery(clp);
436 goto wait_on_recovery;
438 ret = nfs4_schedule_migration_recovery(server);
441 goto wait_on_recovery;
442 case -NFS4ERR_LEASE_MOVED:
443 nfs4_schedule_lease_moved_recovery(clp);
444 goto wait_on_recovery;
445 #if defined(CONFIG_NFS_V4_1)
446 case -NFS4ERR_BADSESSION:
447 case -NFS4ERR_BADSLOT:
448 case -NFS4ERR_BAD_HIGH_SLOT:
449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
450 case -NFS4ERR_DEADSESSION:
451 case -NFS4ERR_SEQ_FALSE_RETRY:
452 case -NFS4ERR_SEQ_MISORDERED:
453 dprintk("%s ERROR: %d Reset session\n", __func__,
455 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
456 goto wait_on_recovery;
457 #endif /* defined(CONFIG_NFS_V4_1) */
458 case -NFS4ERR_FILE_OPEN:
459 if (exception->timeout > HZ) {
460 /* We have retried a decent amount, time to
467 nfs_inc_server_stats(server, NFSIOS_DELAY);
469 case -NFS4ERR_LAYOUTTRYLATER:
470 case -NFS4ERR_RECALLCONFLICT:
471 exception->delay = 1;
474 case -NFS4ERR_RETRY_UNCACHED_REP:
475 case -NFS4ERR_OLD_STATEID:
476 exception->retry = 1;
478 case -NFS4ERR_BADOWNER:
479 /* The following works around a Linux server bug! */
480 case -NFS4ERR_BADNAME:
481 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
482 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
483 exception->retry = 1;
484 printk(KERN_WARNING "NFS: v4 server %s "
485 "does not accept raw "
487 "Reenabling the idmapper.\n",
488 server->nfs_client->cl_hostname);
491 /* We failed to handle the error */
492 return nfs4_map_errors(ret);
494 exception->recovering = 1;
498 /* This is the error handling routine for processes that are allowed
501 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
503 struct nfs_client *clp = server->nfs_client;
506 ret = nfs4_do_handle_exception(server, errorcode, exception);
507 if (exception->delay) {
508 ret = nfs4_delay(server->client, &exception->timeout);
511 if (exception->recovering) {
512 ret = nfs4_wait_clnt_recover(clp);
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
520 exception->retry = 1;
525 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
526 int errorcode, struct nfs4_exception *exception)
528 struct nfs_client *clp = server->nfs_client;
531 ret = nfs4_do_handle_exception(server, errorcode, exception);
532 if (exception->delay) {
533 rpc_delay(task, nfs4_update_delay(&exception->timeout));
536 if (exception->recovering) {
537 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
538 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
539 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
542 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
547 exception->retry = 1;
552 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
553 struct nfs4_state *state, long *timeout)
555 struct nfs4_exception exception = {
559 if (task->tk_status >= 0)
562 exception.timeout = *timeout;
563 task->tk_status = nfs4_async_handle_exception(task, server,
566 if (exception.delay && timeout)
567 *timeout = exception.timeout;
574 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
575 * or 'false' otherwise.
577 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
579 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
580 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
583 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
585 spin_lock(&clp->cl_lock);
586 if (time_before(clp->cl_last_renewal,timestamp))
587 clp->cl_last_renewal = timestamp;
588 spin_unlock(&clp->cl_lock);
591 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
593 struct nfs_client *clp = server->nfs_client;
595 if (!nfs4_has_session(clp))
596 do_renew_lease(clp, timestamp);
599 struct nfs4_call_sync_data {
600 const struct nfs_server *seq_server;
601 struct nfs4_sequence_args *seq_args;
602 struct nfs4_sequence_res *seq_res;
605 void nfs4_init_sequence(struct nfs4_sequence_args *args,
606 struct nfs4_sequence_res *res, int cache_reply)
608 args->sa_slot = NULL;
609 args->sa_cache_this = cache_reply;
610 args->sa_privileged = 0;
615 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
617 args->sa_privileged = 1;
620 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
622 struct nfs4_slot *slot = res->sr_slot;
623 struct nfs4_slot_table *tbl;
626 spin_lock(&tbl->slot_tbl_lock);
627 if (!nfs41_wake_and_assign_slot(tbl, slot))
628 nfs4_free_slot(tbl, slot);
629 spin_unlock(&tbl->slot_tbl_lock);
634 static int nfs40_sequence_done(struct rpc_task *task,
635 struct nfs4_sequence_res *res)
637 if (res->sr_slot != NULL)
638 nfs40_sequence_free_slot(res);
642 #if defined(CONFIG_NFS_V4_1)
644 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
646 struct nfs4_session *session;
647 struct nfs4_slot_table *tbl;
648 struct nfs4_slot *slot = res->sr_slot;
649 bool send_new_highest_used_slotid = false;
652 session = tbl->session;
654 /* Bump the slot sequence number */
659 spin_lock(&tbl->slot_tbl_lock);
660 /* Be nice to the server: try to ensure that the last transmitted
661 * value for highest_user_slotid <= target_highest_slotid
663 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
664 send_new_highest_used_slotid = true;
666 if (nfs41_wake_and_assign_slot(tbl, slot)) {
667 send_new_highest_used_slotid = false;
670 nfs4_free_slot(tbl, slot);
672 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
673 send_new_highest_used_slotid = false;
675 spin_unlock(&tbl->slot_tbl_lock);
677 if (send_new_highest_used_slotid)
678 nfs41_notify_server(session->clp);
679 if (waitqueue_active(&tbl->slot_waitq))
680 wake_up_all(&tbl->slot_waitq);
683 static int nfs41_sequence_process(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
686 struct nfs4_session *session;
687 struct nfs4_slot *slot = res->sr_slot;
688 struct nfs_client *clp;
689 bool interrupted = false;
694 /* don't increment the sequence number if the task wasn't sent */
695 if (!RPC_WAS_SENT(task))
698 session = slot->table->session;
700 if (slot->interrupted) {
701 if (res->sr_status != -NFS4ERR_DELAY)
702 slot->interrupted = 0;
706 trace_nfs4_sequence_done(session, res);
707 /* Check the SEQUENCE operation status */
708 switch (res->sr_status) {
710 /* If previous op on slot was interrupted and we reused
711 * the seq# and got a reply from the cache, then retry
713 if (task->tk_status == -EREMOTEIO && interrupted) {
717 /* Update the slot's sequence and clientid lease timer */
720 do_renew_lease(clp, res->sr_timestamp);
721 /* Check sequence flags */
722 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
724 nfs41_update_target_slotid(slot->table, slot, res);
728 * sr_status remains 1 if an RPC level error occurred.
729 * The server may or may not have processed the sequence
731 * Mark the slot as having hosted an interrupted RPC call.
733 slot->interrupted = 1;
736 /* The server detected a resend of the RPC call and
737 * returned NFS4ERR_DELAY as per Section 2.10.6.2
740 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
745 case -NFS4ERR_BADSLOT:
747 * The slot id we used was probably retired. Try again
748 * using a different slot id.
751 case -NFS4ERR_SEQ_MISORDERED:
753 * Was the last operation on this sequence interrupted?
754 * If so, retry after bumping the sequence number.
761 * Could this slot have been previously retired?
762 * If so, then the server may be expecting seq_nr = 1!
764 if (slot->seq_nr != 1) {
769 case -NFS4ERR_SEQ_FALSE_RETRY:
773 /* Just update the slot sequence no. */
777 /* The session may be reset by one of the error handlers. */
778 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
782 if (rpc_restart_call_prepare(task)) {
783 nfs41_sequence_free_slot(res);
789 if (!rpc_restart_call(task))
791 rpc_delay(task, NFS4_POLL_RETRY_MAX);
795 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
797 if (!nfs41_sequence_process(task, res))
799 if (res->sr_slot != NULL)
800 nfs41_sequence_free_slot(res);
804 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
806 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
808 if (res->sr_slot == NULL)
810 if (res->sr_slot->table->session != NULL)
811 return nfs41_sequence_process(task, res);
812 return nfs40_sequence_done(task, res);
815 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
817 if (res->sr_slot != NULL) {
818 if (res->sr_slot->table->session != NULL)
819 nfs41_sequence_free_slot(res);
821 nfs40_sequence_free_slot(res);
825 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
827 if (res->sr_slot == NULL)
829 if (!res->sr_slot->table->session)
830 return nfs40_sequence_done(task, res);
831 return nfs41_sequence_done(task, res);
833 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
835 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
837 struct nfs4_call_sync_data *data = calldata;
839 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
841 nfs4_setup_sequence(data->seq_server->nfs_client,
842 data->seq_args, data->seq_res, task);
845 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
847 struct nfs4_call_sync_data *data = calldata;
849 nfs41_sequence_done(task, data->seq_res);
852 static const struct rpc_call_ops nfs41_call_sync_ops = {
853 .rpc_call_prepare = nfs41_call_sync_prepare,
854 .rpc_call_done = nfs41_call_sync_done,
857 #else /* !CONFIG_NFS_V4_1 */
859 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
861 return nfs40_sequence_done(task, res);
864 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
866 if (res->sr_slot != NULL)
867 nfs40_sequence_free_slot(res);
870 int nfs4_sequence_done(struct rpc_task *task,
871 struct nfs4_sequence_res *res)
873 return nfs40_sequence_done(task, res);
875 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
877 #endif /* !CONFIG_NFS_V4_1 */
879 int nfs4_setup_sequence(const struct nfs_client *client,
880 struct nfs4_sequence_args *args,
881 struct nfs4_sequence_res *res,
882 struct rpc_task *task)
884 struct nfs4_session *session = nfs4_get_session(client);
885 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
886 struct nfs4_slot *slot;
888 /* slot already allocated? */
889 if (res->sr_slot != NULL)
893 tbl = &session->fc_slot_table;
894 task->tk_timeout = 0;
897 spin_lock(&tbl->slot_tbl_lock);
898 /* The state manager will wait until the slot table is empty */
899 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
902 slot = nfs4_alloc_slot(tbl);
904 /* Try again in 1/4 second */
905 if (slot == ERR_PTR(-ENOMEM))
906 task->tk_timeout = HZ >> 2;
909 spin_unlock(&tbl->slot_tbl_lock);
911 slot->privileged = args->sa_privileged ? 1 : 0;
912 args->sa_slot = slot;
916 res->sr_timestamp = jiffies;
917 res->sr_status_flags = 0;
921 trace_nfs4_setup_sequence(session, args);
923 rpc_call_start(task);
927 if (args->sa_privileged)
928 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
929 NULL, RPC_PRIORITY_PRIVILEGED);
931 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
932 spin_unlock(&tbl->slot_tbl_lock);
935 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
937 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
939 struct nfs4_call_sync_data *data = calldata;
940 nfs4_setup_sequence(data->seq_server->nfs_client,
941 data->seq_args, data->seq_res, task);
944 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
946 struct nfs4_call_sync_data *data = calldata;
947 nfs4_sequence_done(task, data->seq_res);
950 static const struct rpc_call_ops nfs40_call_sync_ops = {
951 .rpc_call_prepare = nfs40_call_sync_prepare,
952 .rpc_call_done = nfs40_call_sync_done,
955 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
956 struct nfs_server *server,
957 struct rpc_message *msg,
958 struct nfs4_sequence_args *args,
959 struct nfs4_sequence_res *res)
962 struct rpc_task *task;
963 struct nfs_client *clp = server->nfs_client;
964 struct nfs4_call_sync_data data = {
965 .seq_server = server,
969 struct rpc_task_setup task_setup = {
972 .callback_ops = clp->cl_mvops->call_sync_ops,
973 .callback_data = &data
976 task = rpc_run_task(&task_setup);
980 ret = task->tk_status;
986 int nfs4_call_sync(struct rpc_clnt *clnt,
987 struct nfs_server *server,
988 struct rpc_message *msg,
989 struct nfs4_sequence_args *args,
990 struct nfs4_sequence_res *res,
993 nfs4_init_sequence(args, res, cache_reply);
994 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
997 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
998 unsigned long timestamp)
1000 struct nfs_inode *nfsi = NFS_I(dir);
1002 spin_lock(&dir->i_lock);
1003 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1004 if (cinfo->atomic && cinfo->before == dir->i_version) {
1005 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1006 nfsi->attrtimeo_timestamp = jiffies;
1008 nfs_force_lookup_revalidate(dir);
1009 if (cinfo->before != dir->i_version)
1010 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1011 NFS_INO_INVALID_ACL;
1013 dir->i_version = cinfo->after;
1014 nfsi->read_cache_jiffies = timestamp;
1015 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1016 nfs_fscache_invalidate(dir);
1017 spin_unlock(&dir->i_lock);
1020 struct nfs4_opendata {
1022 struct nfs_openargs o_arg;
1023 struct nfs_openres o_res;
1024 struct nfs_open_confirmargs c_arg;
1025 struct nfs_open_confirmres c_res;
1026 struct nfs4_string owner_name;
1027 struct nfs4_string group_name;
1028 struct nfs4_label *a_label;
1029 struct nfs_fattr f_attr;
1030 struct nfs4_label *f_label;
1032 struct dentry *dentry;
1033 struct nfs4_state_owner *owner;
1034 struct nfs4_state *state;
1036 unsigned long timestamp;
1044 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1045 int err, struct nfs4_exception *exception)
1049 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1051 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1052 exception->retry = 1;
1057 nfs4_map_atomic_open_share(struct nfs_server *server,
1058 fmode_t fmode, int openflags)
1062 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1064 res = NFS4_SHARE_ACCESS_READ;
1067 res = NFS4_SHARE_ACCESS_WRITE;
1069 case FMODE_READ|FMODE_WRITE:
1070 res = NFS4_SHARE_ACCESS_BOTH;
1072 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1074 /* Want no delegation if we're using O_DIRECT */
1075 if (openflags & O_DIRECT)
1076 res |= NFS4_SHARE_WANT_NO_DELEG;
1081 static enum open_claim_type4
1082 nfs4_map_atomic_open_claim(struct nfs_server *server,
1083 enum open_claim_type4 claim)
1085 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1090 case NFS4_OPEN_CLAIM_FH:
1091 return NFS4_OPEN_CLAIM_NULL;
1092 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1093 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1094 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1095 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1099 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1101 p->o_res.f_attr = &p->f_attr;
1102 p->o_res.f_label = p->f_label;
1103 p->o_res.seqid = p->o_arg.seqid;
1104 p->c_res.seqid = p->c_arg.seqid;
1105 p->o_res.server = p->o_arg.server;
1106 p->o_res.access_request = p->o_arg.access;
1107 nfs_fattr_init(&p->f_attr);
1108 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1111 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1112 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1113 const struct iattr *attrs,
1114 struct nfs4_label *label,
1115 enum open_claim_type4 claim,
1118 struct dentry *parent = dget_parent(dentry);
1119 struct inode *dir = d_inode(parent);
1120 struct nfs_server *server = NFS_SERVER(dir);
1121 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1122 struct nfs4_opendata *p;
1124 p = kzalloc(sizeof(*p), gfp_mask);
1128 p->f_label = nfs4_label_alloc(server, gfp_mask);
1129 if (IS_ERR(p->f_label))
1132 p->a_label = nfs4_label_alloc(server, gfp_mask);
1133 if (IS_ERR(p->a_label))
1136 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1137 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1138 if (IS_ERR(p->o_arg.seqid))
1139 goto err_free_label;
1140 nfs_sb_active(dentry->d_sb);
1141 p->dentry = dget(dentry);
1144 atomic_inc(&sp->so_count);
1145 p->o_arg.open_flags = flags;
1146 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1147 p->o_arg.umask = current_umask();
1148 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1149 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1151 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1152 * will return permission denied for all bits until close */
1153 if (!(flags & O_EXCL)) {
1154 /* ask server to check for all possible rights as results
1156 switch (p->o_arg.claim) {
1159 case NFS4_OPEN_CLAIM_NULL:
1160 case NFS4_OPEN_CLAIM_FH:
1161 p->o_arg.access = NFS4_ACCESS_READ |
1162 NFS4_ACCESS_MODIFY |
1163 NFS4_ACCESS_EXTEND |
1164 NFS4_ACCESS_EXECUTE;
1167 p->o_arg.clientid = server->nfs_client->cl_clientid;
1168 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1169 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1170 p->o_arg.name = &dentry->d_name;
1171 p->o_arg.server = server;
1172 p->o_arg.bitmask = nfs4_bitmask(server, label);
1173 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1174 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1175 switch (p->o_arg.claim) {
1176 case NFS4_OPEN_CLAIM_NULL:
1177 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1178 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1179 p->o_arg.fh = NFS_FH(dir);
1181 case NFS4_OPEN_CLAIM_PREVIOUS:
1182 case NFS4_OPEN_CLAIM_FH:
1183 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1184 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1185 p->o_arg.fh = NFS_FH(d_inode(dentry));
1187 if (attrs != NULL && attrs->ia_valid != 0) {
1190 p->o_arg.u.attrs = &p->attrs;
1191 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1194 verf[1] = current->pid;
1195 memcpy(p->o_arg.u.verifier.data, verf,
1196 sizeof(p->o_arg.u.verifier.data));
1198 p->c_arg.fh = &p->o_res.fh;
1199 p->c_arg.stateid = &p->o_res.stateid;
1200 p->c_arg.seqid = p->o_arg.seqid;
1201 nfs4_init_opendata_res(p);
1202 kref_init(&p->kref);
1206 nfs4_label_free(p->a_label);
1208 nfs4_label_free(p->f_label);
1216 static void nfs4_opendata_free(struct kref *kref)
1218 struct nfs4_opendata *p = container_of(kref,
1219 struct nfs4_opendata, kref);
1220 struct super_block *sb = p->dentry->d_sb;
1222 nfs_free_seqid(p->o_arg.seqid);
1223 nfs4_sequence_free_slot(&p->o_res.seq_res);
1224 if (p->state != NULL)
1225 nfs4_put_open_state(p->state);
1226 nfs4_put_state_owner(p->owner);
1228 nfs4_label_free(p->a_label);
1229 nfs4_label_free(p->f_label);
1233 nfs_sb_deactive(sb);
1234 nfs_fattr_free_names(&p->f_attr);
1235 kfree(p->f_attr.mdsthreshold);
1239 static void nfs4_opendata_put(struct nfs4_opendata *p)
1242 kref_put(&p->kref, nfs4_opendata_free);
1245 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1248 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1249 case FMODE_READ|FMODE_WRITE:
1250 return state->n_rdwr != 0;
1252 return state->n_wronly != 0;
1254 return state->n_rdonly != 0;
1260 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1264 if (open_mode & (O_EXCL|O_TRUNC))
1266 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1268 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1269 && state->n_rdonly != 0;
1272 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1273 && state->n_wronly != 0;
1275 case FMODE_READ|FMODE_WRITE:
1276 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1277 && state->n_rdwr != 0;
1283 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1284 enum open_claim_type4 claim)
1286 if (delegation == NULL)
1288 if ((delegation->type & fmode) != fmode)
1290 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1293 case NFS4_OPEN_CLAIM_NULL:
1294 case NFS4_OPEN_CLAIM_FH:
1296 case NFS4_OPEN_CLAIM_PREVIOUS:
1297 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1302 nfs_mark_delegation_referenced(delegation);
1306 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1315 case FMODE_READ|FMODE_WRITE:
1318 nfs4_state_set_mode_locked(state, state->state | fmode);
1321 #ifdef CONFIG_NFS_V4_1
1322 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1324 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1326 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1328 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1332 #endif /* CONFIG_NFS_V4_1 */
1334 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1336 struct nfs_client *clp = state->owner->so_server->nfs_client;
1337 bool need_recover = false;
1339 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1340 need_recover = true;
1341 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1342 need_recover = true;
1343 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1344 need_recover = true;
1346 nfs4_state_mark_reclaim_nograce(clp, state);
1349 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1350 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1352 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1354 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1355 nfs4_stateid_copy(freeme, &state->open_stateid);
1356 nfs_test_and_clear_all_open_stateid(state);
1359 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1364 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1366 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1368 if (state->n_wronly)
1369 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1370 if (state->n_rdonly)
1371 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1373 set_bit(NFS_O_RDWR_STATE, &state->flags);
1374 set_bit(NFS_OPEN_STATE, &state->flags);
1377 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1378 nfs4_stateid *stateid, fmode_t fmode)
1380 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1381 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1383 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1386 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1389 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1390 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1391 clear_bit(NFS_OPEN_STATE, &state->flags);
1393 if (stateid == NULL)
1395 /* Handle OPEN+OPEN_DOWNGRADE races */
1396 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1397 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1398 nfs_resync_open_stateid_locked(state);
1401 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1402 nfs4_stateid_copy(&state->stateid, stateid);
1403 nfs4_stateid_copy(&state->open_stateid, stateid);
1406 static void nfs_clear_open_stateid(struct nfs4_state *state,
1407 nfs4_stateid *arg_stateid,
1408 nfs4_stateid *stateid, fmode_t fmode)
1410 write_seqlock(&state->seqlock);
1411 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1412 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1413 nfs_clear_open_stateid_locked(state, stateid, fmode);
1414 write_sequnlock(&state->seqlock);
1415 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1416 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1419 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1420 const nfs4_stateid *stateid, fmode_t fmode,
1421 nfs4_stateid *freeme)
1425 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1428 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1430 case FMODE_READ|FMODE_WRITE:
1431 set_bit(NFS_O_RDWR_STATE, &state->flags);
1433 if (!nfs_need_update_open_stateid(state, stateid, freeme))
1435 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1436 nfs4_stateid_copy(&state->stateid, stateid);
1437 nfs4_stateid_copy(&state->open_stateid, stateid);
1440 static void __update_open_stateid(struct nfs4_state *state,
1441 const nfs4_stateid *open_stateid,
1442 const nfs4_stateid *deleg_stateid,
1444 nfs4_stateid *freeme)
1447 * Protect the call to nfs4_state_set_mode_locked and
1448 * serialise the stateid update
1450 spin_lock(&state->owner->so_lock);
1451 write_seqlock(&state->seqlock);
1452 if (deleg_stateid != NULL) {
1453 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1454 set_bit(NFS_DELEGATED_STATE, &state->flags);
1456 if (open_stateid != NULL)
1457 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
1458 write_sequnlock(&state->seqlock);
1459 update_open_stateflags(state, fmode);
1460 spin_unlock(&state->owner->so_lock);
1463 static int update_open_stateid(struct nfs4_state *state,
1464 const nfs4_stateid *open_stateid,
1465 const nfs4_stateid *delegation,
1468 struct nfs_server *server = NFS_SERVER(state->inode);
1469 struct nfs_client *clp = server->nfs_client;
1470 struct nfs_inode *nfsi = NFS_I(state->inode);
1471 struct nfs_delegation *deleg_cur;
1472 nfs4_stateid freeme = { };
1475 fmode &= (FMODE_READ|FMODE_WRITE);
1478 deleg_cur = rcu_dereference(nfsi->delegation);
1479 if (deleg_cur == NULL)
1482 spin_lock(&deleg_cur->lock);
1483 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1484 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1485 (deleg_cur->type & fmode) != fmode)
1486 goto no_delegation_unlock;
1488 if (delegation == NULL)
1489 delegation = &deleg_cur->stateid;
1490 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1491 goto no_delegation_unlock;
1493 nfs_mark_delegation_referenced(deleg_cur);
1494 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1497 no_delegation_unlock:
1498 spin_unlock(&deleg_cur->lock);
1502 if (!ret && open_stateid != NULL) {
1503 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
1506 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1507 nfs4_schedule_state_manager(clp);
1508 if (freeme.type != 0)
1509 nfs4_test_and_free_stateid(server, &freeme,
1510 state->owner->so_cred);
1515 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1516 const nfs4_stateid *stateid)
1518 struct nfs4_state *state = lsp->ls_state;
1521 spin_lock(&state->state_lock);
1522 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1524 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1526 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1529 spin_unlock(&state->state_lock);
1533 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1535 struct nfs_delegation *delegation;
1538 delegation = rcu_dereference(NFS_I(inode)->delegation);
1539 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1544 nfs4_inode_return_delegation(inode);
1547 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1549 struct nfs4_state *state = opendata->state;
1550 struct nfs_inode *nfsi = NFS_I(state->inode);
1551 struct nfs_delegation *delegation;
1552 int open_mode = opendata->o_arg.open_flags;
1553 fmode_t fmode = opendata->o_arg.fmode;
1554 enum open_claim_type4 claim = opendata->o_arg.claim;
1555 nfs4_stateid stateid;
1559 spin_lock(&state->owner->so_lock);
1560 if (can_open_cached(state, fmode, open_mode)) {
1561 update_open_stateflags(state, fmode);
1562 spin_unlock(&state->owner->so_lock);
1563 goto out_return_state;
1565 spin_unlock(&state->owner->so_lock);
1567 delegation = rcu_dereference(nfsi->delegation);
1568 if (!can_open_delegated(delegation, fmode, claim)) {
1572 /* Save the delegation */
1573 nfs4_stateid_copy(&stateid, &delegation->stateid);
1575 nfs_release_seqid(opendata->o_arg.seqid);
1576 if (!opendata->is_recover) {
1577 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1583 /* Try to update the stateid using the delegation */
1584 if (update_open_stateid(state, NULL, &stateid, fmode))
1585 goto out_return_state;
1588 return ERR_PTR(ret);
1590 atomic_inc(&state->count);
1595 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1597 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1598 struct nfs_delegation *delegation;
1599 int delegation_flags = 0;
1602 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1604 delegation_flags = delegation->flags;
1606 switch (data->o_arg.claim) {
1609 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1610 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1611 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1612 "returning a delegation for "
1613 "OPEN(CLAIM_DELEGATE_CUR)\n",
1617 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1618 nfs_inode_set_delegation(state->inode,
1619 data->owner->so_cred,
1622 nfs_inode_reclaim_delegation(state->inode,
1623 data->owner->so_cred,
1628 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1629 * and update the nfs4_state.
1631 static struct nfs4_state *
1632 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1634 struct inode *inode = data->state->inode;
1635 struct nfs4_state *state = data->state;
1638 if (!data->rpc_done) {
1639 if (data->rpc_status)
1640 return ERR_PTR(data->rpc_status);
1641 /* cached opens have already been processed */
1645 ret = nfs_refresh_inode(inode, &data->f_attr);
1647 return ERR_PTR(ret);
1649 if (data->o_res.delegation_type != 0)
1650 nfs4_opendata_check_deleg(data, state);
1652 update_open_stateid(state, &data->o_res.stateid, NULL,
1654 atomic_inc(&state->count);
1659 static struct inode *
1660 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1662 struct inode *inode;
1664 switch (data->o_arg.claim) {
1665 case NFS4_OPEN_CLAIM_NULL:
1666 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1667 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1668 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1669 return ERR_PTR(-EAGAIN);
1670 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1671 &data->f_attr, data->f_label);
1674 inode = d_inode(data->dentry);
1676 nfs_refresh_inode(inode, &data->f_attr);
1681 static struct nfs4_state *
1682 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1684 struct nfs4_state *state;
1685 struct inode *inode;
1687 inode = nfs4_opendata_get_inode(data);
1689 return ERR_CAST(inode);
1690 if (data->state != NULL && data->state->inode == inode) {
1691 state = data->state;
1692 atomic_inc(&state->count);
1694 state = nfs4_get_open_state(inode, data->owner);
1697 state = ERR_PTR(-ENOMEM);
1701 static struct nfs4_state *
1702 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1704 struct nfs4_state *state;
1706 if (!data->rpc_done) {
1707 state = nfs4_try_open_cached(data);
1708 trace_nfs4_cached_open(data->state);
1712 state = nfs4_opendata_find_nfs4_state(data);
1716 if (data->o_res.delegation_type != 0)
1717 nfs4_opendata_check_deleg(data, state);
1718 update_open_stateid(state, &data->o_res.stateid, NULL,
1721 nfs_release_seqid(data->o_arg.seqid);
1725 static struct nfs4_state *
1726 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1728 struct nfs4_state *ret;
1730 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1731 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1733 ret = _nfs4_opendata_to_nfs4_state(data);
1734 nfs4_sequence_free_slot(&data->o_res.seq_res);
1738 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1740 struct nfs_inode *nfsi = NFS_I(state->inode);
1741 struct nfs_open_context *ctx;
1743 spin_lock(&state->inode->i_lock);
1744 list_for_each_entry(ctx, &nfsi->open_files, list) {
1745 if (ctx->state != state)
1747 get_nfs_open_context(ctx);
1748 spin_unlock(&state->inode->i_lock);
1751 spin_unlock(&state->inode->i_lock);
1752 return ERR_PTR(-ENOENT);
1755 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1756 struct nfs4_state *state, enum open_claim_type4 claim)
1758 struct nfs4_opendata *opendata;
1760 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1761 NULL, NULL, claim, GFP_NOFS);
1762 if (opendata == NULL)
1763 return ERR_PTR(-ENOMEM);
1764 opendata->state = state;
1765 atomic_inc(&state->count);
1769 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1772 struct nfs4_state *newstate;
1775 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1777 opendata->o_arg.open_flags = 0;
1778 opendata->o_arg.fmode = fmode;
1779 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1780 NFS_SB(opendata->dentry->d_sb),
1782 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1783 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1784 nfs4_init_opendata_res(opendata);
1785 ret = _nfs4_recover_proc_open(opendata);
1788 newstate = nfs4_opendata_to_nfs4_state(opendata);
1789 if (IS_ERR(newstate))
1790 return PTR_ERR(newstate);
1791 if (newstate != opendata->state)
1793 nfs4_close_state(newstate, fmode);
1797 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1801 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1802 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1803 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1804 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1805 /* memory barrier prior to reading state->n_* */
1806 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1807 clear_bit(NFS_OPEN_STATE, &state->flags);
1809 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1812 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1815 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1819 * We may have performed cached opens for all three recoveries.
1820 * Check if we need to update the current stateid.
1822 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1823 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1824 write_seqlock(&state->seqlock);
1825 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1826 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1827 write_sequnlock(&state->seqlock);
1834 * reclaim state on the server after a reboot.
1836 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1838 struct nfs_delegation *delegation;
1839 struct nfs4_opendata *opendata;
1840 fmode_t delegation_type = 0;
1843 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1844 NFS4_OPEN_CLAIM_PREVIOUS);
1845 if (IS_ERR(opendata))
1846 return PTR_ERR(opendata);
1848 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1849 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1850 delegation_type = delegation->type;
1852 opendata->o_arg.u.delegation_type = delegation_type;
1853 status = nfs4_open_recover(opendata, state);
1854 nfs4_opendata_put(opendata);
1858 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1860 struct nfs_server *server = NFS_SERVER(state->inode);
1861 struct nfs4_exception exception = { };
1864 err = _nfs4_do_open_reclaim(ctx, state);
1865 trace_nfs4_open_reclaim(ctx, 0, err);
1866 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1868 if (err != -NFS4ERR_DELAY)
1870 nfs4_handle_exception(server, err, &exception);
1871 } while (exception.retry);
1875 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1877 struct nfs_open_context *ctx;
1880 ctx = nfs4_state_find_open_context(state);
1883 ret = nfs4_do_open_reclaim(ctx, state);
1884 put_nfs_open_context(ctx);
1888 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1892 printk(KERN_ERR "NFS: %s: unhandled error "
1893 "%d.\n", __func__, err);
1899 case -NFS4ERR_BADSESSION:
1900 case -NFS4ERR_BADSLOT:
1901 case -NFS4ERR_BAD_HIGH_SLOT:
1902 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1903 case -NFS4ERR_DEADSESSION:
1904 set_bit(NFS_DELEGATED_STATE, &state->flags);
1905 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1907 case -NFS4ERR_STALE_CLIENTID:
1908 case -NFS4ERR_STALE_STATEID:
1909 set_bit(NFS_DELEGATED_STATE, &state->flags);
1910 /* Don't recall a delegation if it was lost */
1911 nfs4_schedule_lease_recovery(server->nfs_client);
1913 case -NFS4ERR_MOVED:
1914 nfs4_schedule_migration_recovery(server);
1916 case -NFS4ERR_LEASE_MOVED:
1917 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1919 case -NFS4ERR_DELEG_REVOKED:
1920 case -NFS4ERR_ADMIN_REVOKED:
1921 case -NFS4ERR_EXPIRED:
1922 case -NFS4ERR_BAD_STATEID:
1923 case -NFS4ERR_OPENMODE:
1924 nfs_inode_find_state_and_recover(state->inode,
1926 nfs4_schedule_stateid_recovery(server, state);
1928 case -NFS4ERR_DELAY:
1929 case -NFS4ERR_GRACE:
1930 set_bit(NFS_DELEGATED_STATE, &state->flags);
1934 case -NFS4ERR_DENIED:
1935 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1941 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1942 struct nfs4_state *state, const nfs4_stateid *stateid,
1945 struct nfs_server *server = NFS_SERVER(state->inode);
1946 struct nfs4_opendata *opendata;
1949 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1950 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1951 if (IS_ERR(opendata))
1952 return PTR_ERR(opendata);
1953 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1954 write_seqlock(&state->seqlock);
1955 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1956 write_sequnlock(&state->seqlock);
1957 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1958 switch (type & (FMODE_READ|FMODE_WRITE)) {
1959 case FMODE_READ|FMODE_WRITE:
1961 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1964 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1968 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1970 nfs4_opendata_put(opendata);
1971 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1974 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1976 struct nfs4_opendata *data = calldata;
1978 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1979 &data->c_arg.seq_args, &data->c_res.seq_res, task);
1982 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1984 struct nfs4_opendata *data = calldata;
1986 nfs40_sequence_done(task, &data->c_res.seq_res);
1988 data->rpc_status = task->tk_status;
1989 if (data->rpc_status == 0) {
1990 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1991 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1992 renew_lease(data->o_res.server, data->timestamp);
1993 data->rpc_done = true;
1997 static void nfs4_open_confirm_release(void *calldata)
1999 struct nfs4_opendata *data = calldata;
2000 struct nfs4_state *state = NULL;
2002 /* If this request hasn't been cancelled, do nothing */
2003 if (!data->cancelled)
2005 /* In case of error, no cleanup! */
2006 if (!data->rpc_done)
2008 state = nfs4_opendata_to_nfs4_state(data);
2010 nfs4_close_state(state, data->o_arg.fmode);
2012 nfs4_opendata_put(data);
2015 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2016 .rpc_call_prepare = nfs4_open_confirm_prepare,
2017 .rpc_call_done = nfs4_open_confirm_done,
2018 .rpc_release = nfs4_open_confirm_release,
2022 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2024 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2026 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2027 struct rpc_task *task;
2028 struct rpc_message msg = {
2029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2030 .rpc_argp = &data->c_arg,
2031 .rpc_resp = &data->c_res,
2032 .rpc_cred = data->owner->so_cred,
2034 struct rpc_task_setup task_setup_data = {
2035 .rpc_client = server->client,
2036 .rpc_message = &msg,
2037 .callback_ops = &nfs4_open_confirm_ops,
2038 .callback_data = data,
2039 .workqueue = nfsiod_workqueue,
2040 .flags = RPC_TASK_ASYNC,
2044 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2045 kref_get(&data->kref);
2046 data->rpc_done = false;
2047 data->rpc_status = 0;
2048 data->timestamp = jiffies;
2049 if (data->is_recover)
2050 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2051 task = rpc_run_task(&task_setup_data);
2053 return PTR_ERR(task);
2054 status = rpc_wait_for_completion_task(task);
2056 data->cancelled = true;
2059 status = data->rpc_status;
2064 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2066 struct nfs4_opendata *data = calldata;
2067 struct nfs4_state_owner *sp = data->owner;
2068 struct nfs_client *clp = sp->so_server->nfs_client;
2069 enum open_claim_type4 claim = data->o_arg.claim;
2071 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2074 * Check if we still need to send an OPEN call, or if we can use
2075 * a delegation instead.
2077 if (data->state != NULL) {
2078 struct nfs_delegation *delegation;
2080 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2083 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2084 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2085 goto unlock_no_action;
2088 /* Update client id. */
2089 data->o_arg.clientid = clp->cl_clientid;
2093 case NFS4_OPEN_CLAIM_PREVIOUS:
2094 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2095 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2096 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2097 case NFS4_OPEN_CLAIM_FH:
2098 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2100 data->timestamp = jiffies;
2101 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2102 &data->o_arg.seq_args,
2103 &data->o_res.seq_res,
2105 nfs_release_seqid(data->o_arg.seqid);
2107 /* Set the create mode (note dependency on the session type) */
2108 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2109 if (data->o_arg.open_flags & O_EXCL) {
2110 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2111 if (nfs4_has_persistent_session(clp))
2112 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2113 else if (clp->cl_mvops->minor_version > 0)
2114 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2118 trace_nfs4_cached_open(data->state);
2121 task->tk_action = NULL;
2123 nfs4_sequence_done(task, &data->o_res.seq_res);
2126 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2128 struct nfs4_opendata *data = calldata;
2130 data->rpc_status = task->tk_status;
2132 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2135 if (task->tk_status == 0) {
2136 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2137 switch (data->o_res.f_attr->mode & S_IFMT) {
2141 data->rpc_status = -ELOOP;
2144 data->rpc_status = -EISDIR;
2147 data->rpc_status = -ENOTDIR;
2150 renew_lease(data->o_res.server, data->timestamp);
2151 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2152 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2154 data->rpc_done = true;
2157 static void nfs4_open_release(void *calldata)
2159 struct nfs4_opendata *data = calldata;
2160 struct nfs4_state *state = NULL;
2162 /* If this request hasn't been cancelled, do nothing */
2163 if (!data->cancelled)
2165 /* In case of error, no cleanup! */
2166 if (data->rpc_status != 0 || !data->rpc_done)
2168 /* In case we need an open_confirm, no cleanup! */
2169 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2171 state = nfs4_opendata_to_nfs4_state(data);
2173 nfs4_close_state(state, data->o_arg.fmode);
2175 nfs4_opendata_put(data);
2178 static const struct rpc_call_ops nfs4_open_ops = {
2179 .rpc_call_prepare = nfs4_open_prepare,
2180 .rpc_call_done = nfs4_open_done,
2181 .rpc_release = nfs4_open_release,
2184 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2186 struct inode *dir = d_inode(data->dir);
2187 struct nfs_server *server = NFS_SERVER(dir);
2188 struct nfs_openargs *o_arg = &data->o_arg;
2189 struct nfs_openres *o_res = &data->o_res;
2190 struct rpc_task *task;
2191 struct rpc_message msg = {
2192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2195 .rpc_cred = data->owner->so_cred,
2197 struct rpc_task_setup task_setup_data = {
2198 .rpc_client = server->client,
2199 .rpc_message = &msg,
2200 .callback_ops = &nfs4_open_ops,
2201 .callback_data = data,
2202 .workqueue = nfsiod_workqueue,
2203 .flags = RPC_TASK_ASYNC,
2207 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2208 kref_get(&data->kref);
2209 data->rpc_done = false;
2210 data->rpc_status = 0;
2211 data->cancelled = false;
2212 data->is_recover = false;
2214 nfs4_set_sequence_privileged(&o_arg->seq_args);
2215 data->is_recover = true;
2217 task = rpc_run_task(&task_setup_data);
2219 return PTR_ERR(task);
2220 status = rpc_wait_for_completion_task(task);
2222 data->cancelled = true;
2225 status = data->rpc_status;
2231 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2233 struct inode *dir = d_inode(data->dir);
2234 struct nfs_openres *o_res = &data->o_res;
2237 status = nfs4_run_open_task(data, 1);
2238 if (status != 0 || !data->rpc_done)
2241 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2243 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2244 status = _nfs4_proc_open_confirm(data);
2250 * Additional permission checks in order to distinguish between an
2251 * open for read, and an open for execute. This works around the
2252 * fact that NFSv4 OPEN treats read and execute permissions as being
2254 * Note that in the non-execute case, we want to turn off permission
2255 * checking if we just created a new file (POSIX open() semantics).
2257 static int nfs4_opendata_access(struct rpc_cred *cred,
2258 struct nfs4_opendata *opendata,
2259 struct nfs4_state *state, fmode_t fmode,
2262 struct nfs_access_entry cache;
2265 /* access call failed or for some reason the server doesn't
2266 * support any access modes -- defer access call until later */
2267 if (opendata->o_res.access_supported == 0)
2272 * Use openflags to check for exec, because fmode won't
2273 * always have FMODE_EXEC set when file open for exec.
2275 if (openflags & __FMODE_EXEC) {
2276 /* ONLY check for exec rights */
2277 if (S_ISDIR(state->inode->i_mode))
2278 mask = NFS4_ACCESS_LOOKUP;
2280 mask = NFS4_ACCESS_EXECUTE;
2281 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2282 mask = NFS4_ACCESS_READ;
2285 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2286 nfs_access_add_cache(state->inode, &cache);
2288 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2289 if ((mask & ~cache.mask & flags) == 0)
2296 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2298 static int _nfs4_proc_open(struct nfs4_opendata *data)
2300 struct inode *dir = d_inode(data->dir);
2301 struct nfs_server *server = NFS_SERVER(dir);
2302 struct nfs_openargs *o_arg = &data->o_arg;
2303 struct nfs_openres *o_res = &data->o_res;
2306 status = nfs4_run_open_task(data, 0);
2307 if (!data->rpc_done)
2310 if (status == -NFS4ERR_BADNAME &&
2311 !(o_arg->open_flags & O_CREAT))
2316 nfs_fattr_map_and_free_names(server, &data->f_attr);
2318 if (o_arg->open_flags & O_CREAT) {
2319 if (o_arg->open_flags & O_EXCL)
2320 data->file_created = true;
2321 else if (o_res->cinfo.before != o_res->cinfo.after)
2322 data->file_created = true;
2323 if (data->file_created || dir->i_version != o_res->cinfo.after)
2324 update_changeattr(dir, &o_res->cinfo,
2325 o_res->f_attr->time_start);
2327 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2328 server->caps &= ~NFS_CAP_POSIX_LOCK;
2329 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2330 status = _nfs4_proc_open_confirm(data);
2334 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2335 nfs4_sequence_free_slot(&o_res->seq_res);
2336 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2343 * reclaim state on the server after a network partition.
2344 * Assumes caller holds the appropriate lock
2346 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2348 struct nfs4_opendata *opendata;
2351 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2352 NFS4_OPEN_CLAIM_FH);
2353 if (IS_ERR(opendata))
2354 return PTR_ERR(opendata);
2355 ret = nfs4_open_recover(opendata, state);
2357 d_drop(ctx->dentry);
2358 nfs4_opendata_put(opendata);
2362 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2364 struct nfs_server *server = NFS_SERVER(state->inode);
2365 struct nfs4_exception exception = { };
2369 err = _nfs4_open_expired(ctx, state);
2370 trace_nfs4_open_expired(ctx, 0, err);
2371 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2376 case -NFS4ERR_GRACE:
2377 case -NFS4ERR_DELAY:
2378 nfs4_handle_exception(server, err, &exception);
2381 } while (exception.retry);
2386 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2388 struct nfs_open_context *ctx;
2391 ctx = nfs4_state_find_open_context(state);
2394 ret = nfs4_do_open_expired(ctx, state);
2395 put_nfs_open_context(ctx);
2399 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2400 const nfs4_stateid *stateid)
2402 nfs_remove_bad_delegation(state->inode, stateid);
2403 write_seqlock(&state->seqlock);
2404 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2405 write_sequnlock(&state->seqlock);
2406 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2409 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2411 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2412 nfs_finish_clear_delegation_stateid(state, NULL);
2415 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2417 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2418 nfs40_clear_delegation_stateid(state);
2419 return nfs4_open_expired(sp, state);
2422 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2423 nfs4_stateid *stateid,
2424 struct rpc_cred *cred)
2426 return -NFS4ERR_BAD_STATEID;
2429 #if defined(CONFIG_NFS_V4_1)
2430 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2431 nfs4_stateid *stateid,
2432 struct rpc_cred *cred)
2436 switch (stateid->type) {
2439 case NFS4_INVALID_STATEID_TYPE:
2440 case NFS4_SPECIAL_STATEID_TYPE:
2441 return -NFS4ERR_BAD_STATEID;
2442 case NFS4_REVOKED_STATEID_TYPE:
2446 status = nfs41_test_stateid(server, stateid, cred);
2448 case -NFS4ERR_EXPIRED:
2449 case -NFS4ERR_ADMIN_REVOKED:
2450 case -NFS4ERR_DELEG_REVOKED:
2456 /* Ack the revoked state to the server */
2457 nfs41_free_stateid(server, stateid, cred, true);
2458 return -NFS4ERR_EXPIRED;
2461 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2463 struct nfs_server *server = NFS_SERVER(state->inode);
2464 nfs4_stateid stateid;
2465 struct nfs_delegation *delegation;
2466 struct rpc_cred *cred;
2469 /* Get the delegation credential for use by test/free_stateid */
2471 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2472 if (delegation == NULL) {
2477 nfs4_stateid_copy(&stateid, &delegation->stateid);
2478 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2479 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2480 &delegation->flags)) {
2482 nfs_finish_clear_delegation_stateid(state, &stateid);
2486 cred = get_rpccred(delegation->cred);
2488 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2489 trace_nfs4_test_delegation_stateid(state, NULL, status);
2490 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2491 nfs_finish_clear_delegation_stateid(state, &stateid);
2497 * nfs41_check_expired_locks - possibly free a lock stateid
2499 * @state: NFSv4 state for an inode
2501 * Returns NFS_OK if recovery for this stateid is now finished.
2502 * Otherwise a negative NFS4ERR value is returned.
2504 static int nfs41_check_expired_locks(struct nfs4_state *state)
2506 int status, ret = NFS_OK;
2507 struct nfs4_lock_state *lsp, *prev = NULL;
2508 struct nfs_server *server = NFS_SERVER(state->inode);
2510 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2513 spin_lock(&state->state_lock);
2514 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2515 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2516 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2518 atomic_inc(&lsp->ls_count);
2519 spin_unlock(&state->state_lock);
2521 nfs4_put_lock_state(prev);
2524 status = nfs41_test_and_free_expired_stateid(server,
2527 trace_nfs4_test_lock_stateid(state, lsp, status);
2528 if (status == -NFS4ERR_EXPIRED ||
2529 status == -NFS4ERR_BAD_STATEID) {
2530 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2531 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2532 if (!recover_lost_locks)
2533 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2534 } else if (status != NFS_OK) {
2536 nfs4_put_lock_state(prev);
2539 spin_lock(&state->state_lock);
2542 spin_unlock(&state->state_lock);
2543 nfs4_put_lock_state(prev);
2549 * nfs41_check_open_stateid - possibly free an open stateid
2551 * @state: NFSv4 state for an inode
2553 * Returns NFS_OK if recovery for this stateid is now finished.
2554 * Otherwise a negative NFS4ERR value is returned.
2556 static int nfs41_check_open_stateid(struct nfs4_state *state)
2558 struct nfs_server *server = NFS_SERVER(state->inode);
2559 nfs4_stateid *stateid = &state->open_stateid;
2560 struct rpc_cred *cred = state->owner->so_cred;
2563 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2564 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2565 if (nfs4_have_delegation(state->inode, state->state))
2567 return -NFS4ERR_OPENMODE;
2569 return -NFS4ERR_BAD_STATEID;
2571 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2572 trace_nfs4_test_open_stateid(state, NULL, status);
2573 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2574 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2575 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2576 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2577 clear_bit(NFS_OPEN_STATE, &state->flags);
2578 stateid->type = NFS4_INVALID_STATEID_TYPE;
2581 if (nfs_open_stateid_recover_openmode(state))
2582 return -NFS4ERR_OPENMODE;
2586 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2590 nfs41_check_delegation_stateid(state);
2591 status = nfs41_check_expired_locks(state);
2592 if (status != NFS_OK)
2594 status = nfs41_check_open_stateid(state);
2595 if (status != NFS_OK)
2596 status = nfs4_open_expired(sp, state);
2602 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2603 * fields corresponding to attributes that were used to store the verifier.
2604 * Make sure we clobber those fields in the later setattr call
2606 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2607 struct iattr *sattr, struct nfs4_label **label)
2609 const u32 *attrset = opendata->o_res.attrset;
2611 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2612 !(sattr->ia_valid & ATTR_ATIME_SET))
2613 sattr->ia_valid |= ATTR_ATIME;
2615 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2616 !(sattr->ia_valid & ATTR_MTIME_SET))
2617 sattr->ia_valid |= ATTR_MTIME;
2619 /* Except MODE, it seems harmless of setting twice. */
2620 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2621 (attrset[1] & FATTR4_WORD1_MODE ||
2622 attrset[2] & FATTR4_WORD2_MODE_UMASK))
2623 sattr->ia_valid &= ~ATTR_MODE;
2625 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2629 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2632 struct nfs_open_context *ctx)
2634 struct nfs4_state_owner *sp = opendata->owner;
2635 struct nfs_server *server = sp->so_server;
2636 struct dentry *dentry;
2637 struct nfs4_state *state;
2641 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2643 ret = _nfs4_proc_open(opendata);
2647 state = nfs4_opendata_to_nfs4_state(opendata);
2648 ret = PTR_ERR(state);
2652 if (server->caps & NFS_CAP_POSIX_LOCK)
2653 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2654 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2655 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2657 dentry = opendata->dentry;
2658 if (d_really_is_negative(dentry)) {
2659 struct dentry *alias;
2661 alias = d_exact_alias(dentry, state->inode);
2663 alias = d_splice_alias(igrab(state->inode), dentry);
2664 /* d_splice_alias() can't fail here - it's a non-directory */
2667 ctx->dentry = dentry = alias;
2669 nfs_set_verifier(dentry,
2670 nfs_save_change_attribute(d_inode(opendata->dir)));
2673 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2677 if (d_inode(dentry) == state->inode) {
2678 nfs_inode_attach_open_context(ctx);
2679 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2680 nfs4_schedule_stateid_recovery(server, state);
2687 * Returns a referenced nfs4_state
2689 static int _nfs4_do_open(struct inode *dir,
2690 struct nfs_open_context *ctx,
2692 struct iattr *sattr,
2693 struct nfs4_label *label,
2696 struct nfs4_state_owner *sp;
2697 struct nfs4_state *state = NULL;
2698 struct nfs_server *server = NFS_SERVER(dir);
2699 struct nfs4_opendata *opendata;
2700 struct dentry *dentry = ctx->dentry;
2701 struct rpc_cred *cred = ctx->cred;
2702 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2703 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2704 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2705 struct nfs4_label *olabel = NULL;
2708 /* Protect against reboot recovery conflicts */
2710 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2712 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2715 status = nfs4_client_recover_expired_lease(server->nfs_client);
2717 goto err_put_state_owner;
2718 if (d_really_is_positive(dentry))
2719 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2721 if (d_really_is_positive(dentry))
2722 claim = NFS4_OPEN_CLAIM_FH;
2723 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2724 label, claim, GFP_KERNEL);
2725 if (opendata == NULL)
2726 goto err_put_state_owner;
2729 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2730 if (IS_ERR(olabel)) {
2731 status = PTR_ERR(olabel);
2732 goto err_opendata_put;
2736 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2737 if (!opendata->f_attr.mdsthreshold) {
2738 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2739 if (!opendata->f_attr.mdsthreshold)
2740 goto err_free_label;
2742 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2744 if (d_really_is_positive(dentry))
2745 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2747 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2749 goto err_free_label;
2752 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2753 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2754 nfs4_exclusive_attrset(opendata, sattr, &label);
2756 * send create attributes which was not set by open
2757 * with an extra setattr.
2759 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2760 nfs_fattr_init(opendata->o_res.f_attr);
2761 status = nfs4_do_setattr(state->inode, cred,
2762 opendata->o_res.f_attr, sattr,
2763 ctx, label, olabel);
2765 nfs_setattr_update_inode(state->inode, sattr,
2766 opendata->o_res.f_attr);
2767 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2771 if (opened && opendata->file_created)
2772 *opened |= FILE_CREATED;
2774 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2775 *ctx_th = opendata->f_attr.mdsthreshold;
2776 opendata->f_attr.mdsthreshold = NULL;
2779 nfs4_label_free(olabel);
2781 nfs4_opendata_put(opendata);
2782 nfs4_put_state_owner(sp);
2785 nfs4_label_free(olabel);
2787 nfs4_opendata_put(opendata);
2788 err_put_state_owner:
2789 nfs4_put_state_owner(sp);
2795 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2796 struct nfs_open_context *ctx,
2798 struct iattr *sattr,
2799 struct nfs4_label *label,
2802 struct nfs_server *server = NFS_SERVER(dir);
2803 struct nfs4_exception exception = { };
2804 struct nfs4_state *res;
2808 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2810 trace_nfs4_open_file(ctx, flags, status);
2813 /* NOTE: BAD_SEQID means the server and client disagree about the
2814 * book-keeping w.r.t. state-changing operations
2815 * (OPEN/CLOSE/LOCK/LOCKU...)
2816 * It is actually a sign of a bug on the client or on the server.
2818 * If we receive a BAD_SEQID error in the particular case of
2819 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2820 * have unhashed the old state_owner for us, and that we can
2821 * therefore safely retry using a new one. We should still warn
2822 * the user though...
2824 if (status == -NFS4ERR_BAD_SEQID) {
2825 pr_warn_ratelimited("NFS: v4 server %s "
2826 " returned a bad sequence-id error!\n",
2827 NFS_SERVER(dir)->nfs_client->cl_hostname);
2828 exception.retry = 1;
2832 * BAD_STATEID on OPEN means that the server cancelled our
2833 * state before it received the OPEN_CONFIRM.
2834 * Recover by retrying the request as per the discussion
2835 * on Page 181 of RFC3530.
2837 if (status == -NFS4ERR_BAD_STATEID) {
2838 exception.retry = 1;
2841 if (status == -EAGAIN) {
2842 /* We must have found a delegation */
2843 exception.retry = 1;
2846 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2848 res = ERR_PTR(nfs4_handle_exception(server,
2849 status, &exception));
2850 } while (exception.retry);
2854 static int _nfs4_do_setattr(struct inode *inode,
2855 struct nfs_setattrargs *arg,
2856 struct nfs_setattrres *res,
2857 struct rpc_cred *cred,
2858 struct nfs_open_context *ctx)
2860 struct nfs_server *server = NFS_SERVER(inode);
2861 struct rpc_message msg = {
2862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2867 struct rpc_cred *delegation_cred = NULL;
2868 unsigned long timestamp = jiffies;
2873 nfs_fattr_init(res->fattr);
2875 /* Servers should only apply open mode checks for file size changes */
2876 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2877 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2879 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2880 /* Use that stateid */
2881 } else if (truncate && ctx != NULL) {
2882 struct nfs_lock_context *l_ctx;
2883 if (!nfs4_valid_open_stateid(ctx->state))
2885 l_ctx = nfs_get_lock_context(ctx);
2887 return PTR_ERR(l_ctx);
2888 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2889 &arg->stateid, &delegation_cred);
2890 nfs_put_lock_context(l_ctx);
2894 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2895 if (delegation_cred)
2896 msg.rpc_cred = delegation_cred;
2898 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2900 put_rpccred(delegation_cred);
2901 if (status == 0 && ctx != NULL)
2902 renew_lease(server, timestamp);
2903 trace_nfs4_setattr(inode, &arg->stateid, status);
2907 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2908 struct nfs_fattr *fattr, struct iattr *sattr,
2909 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
2910 struct nfs4_label *olabel)
2912 struct nfs_server *server = NFS_SERVER(inode);
2913 struct nfs4_state *state = ctx ? ctx->state : NULL;
2914 struct nfs_setattrargs arg = {
2915 .fh = NFS_FH(inode),
2918 .bitmask = server->attr_bitmask,
2921 struct nfs_setattrres res = {
2926 struct nfs4_exception exception = {
2929 .stateid = &arg.stateid,
2933 arg.bitmask = nfs4_bitmask(server, ilabel);
2935 arg.bitmask = nfs4_bitmask(server, olabel);
2938 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
2940 case -NFS4ERR_OPENMODE:
2941 if (!(sattr->ia_valid & ATTR_SIZE)) {
2942 pr_warn_once("NFSv4: server %s is incorrectly "
2943 "applying open mode checks to "
2944 "a SETATTR that is not "
2945 "changing file size.\n",
2946 server->nfs_client->cl_hostname);
2948 if (state && !(state->state & FMODE_WRITE)) {
2950 if (sattr->ia_valid & ATTR_OPEN)
2955 err = nfs4_handle_exception(server, err, &exception);
2956 } while (exception.retry);
2962 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2964 if (inode == NULL || !nfs_have_layout(inode))
2967 return pnfs_wait_on_layoutreturn(inode, task);
2970 struct nfs4_closedata {
2971 struct inode *inode;
2972 struct nfs4_state *state;
2973 struct nfs_closeargs arg;
2974 struct nfs_closeres res;
2976 struct nfs4_layoutreturn_args arg;
2977 struct nfs4_layoutreturn_res res;
2978 struct nfs4_xdr_opaque_data ld_private;
2982 struct nfs_fattr fattr;
2983 unsigned long timestamp;
2986 static void nfs4_free_closedata(void *data)
2988 struct nfs4_closedata *calldata = data;
2989 struct nfs4_state_owner *sp = calldata->state->owner;
2990 struct super_block *sb = calldata->state->inode->i_sb;
2992 if (calldata->lr.roc)
2993 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2994 calldata->res.lr_ret);
2995 nfs4_put_open_state(calldata->state);
2996 nfs_free_seqid(calldata->arg.seqid);
2997 nfs4_put_state_owner(sp);
2998 nfs_sb_deactive(sb);
3002 static void nfs4_close_done(struct rpc_task *task, void *data)
3004 struct nfs4_closedata *calldata = data;
3005 struct nfs4_state *state = calldata->state;
3006 struct nfs_server *server = NFS_SERVER(calldata->inode);
3007 nfs4_stateid *res_stateid = NULL;
3009 dprintk("%s: begin!\n", __func__);
3010 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3012 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3014 /* Handle Layoutreturn errors */
3015 if (calldata->arg.lr_args && task->tk_status != 0) {
3016 switch (calldata->res.lr_ret) {
3018 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3021 calldata->arg.lr_args = NULL;
3022 calldata->res.lr_res = NULL;
3024 case -NFS4ERR_ADMIN_REVOKED:
3025 case -NFS4ERR_DELEG_REVOKED:
3026 case -NFS4ERR_EXPIRED:
3027 case -NFS4ERR_BAD_STATEID:
3028 case -NFS4ERR_OLD_STATEID:
3029 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3030 case -NFS4ERR_WRONG_CRED:
3031 calldata->arg.lr_args = NULL;
3032 calldata->res.lr_res = NULL;
3033 calldata->res.lr_ret = 0;
3034 rpc_restart_call_prepare(task);
3039 /* hmm. we are done with the inode, and in the process of freeing
3040 * the state_owner. we keep this around to process errors
3042 switch (task->tk_status) {
3044 res_stateid = &calldata->res.stateid;
3045 renew_lease(server, calldata->timestamp);
3047 case -NFS4ERR_ACCESS:
3048 if (calldata->arg.bitmask != NULL) {
3049 calldata->arg.bitmask = NULL;
3050 calldata->res.fattr = NULL;
3051 task->tk_status = 0;
3052 rpc_restart_call_prepare(task);
3057 case -NFS4ERR_ADMIN_REVOKED:
3058 case -NFS4ERR_STALE_STATEID:
3059 case -NFS4ERR_EXPIRED:
3060 nfs4_free_revoked_stateid(server,
3061 &calldata->arg.stateid,
3062 task->tk_msg.rpc_cred);
3063 case -NFS4ERR_OLD_STATEID:
3064 case -NFS4ERR_BAD_STATEID:
3065 if (!nfs4_stateid_match(&calldata->arg.stateid,
3066 &state->open_stateid)) {
3067 rpc_restart_call_prepare(task);
3070 if (calldata->arg.fmode == 0)
3073 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3074 rpc_restart_call_prepare(task);
3078 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3079 res_stateid, calldata->arg.fmode);
3081 nfs_release_seqid(calldata->arg.seqid);
3082 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3083 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3086 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3088 struct nfs4_closedata *calldata = data;
3089 struct nfs4_state *state = calldata->state;
3090 struct inode *inode = calldata->inode;
3091 bool is_rdonly, is_wronly, is_rdwr;
3094 dprintk("%s: begin!\n", __func__);
3095 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3098 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3099 spin_lock(&state->owner->so_lock);
3100 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3101 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3102 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3103 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3104 /* Calculate the change in open mode */
3105 calldata->arg.fmode = 0;
3106 if (state->n_rdwr == 0) {
3107 if (state->n_rdonly == 0)
3108 call_close |= is_rdonly;
3110 calldata->arg.fmode |= FMODE_READ;
3111 if (state->n_wronly == 0)
3112 call_close |= is_wronly;
3114 calldata->arg.fmode |= FMODE_WRITE;
3115 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3116 call_close |= is_rdwr;
3118 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3120 if (!nfs4_valid_open_stateid(state) ||
3121 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3123 spin_unlock(&state->owner->so_lock);
3126 /* Note: exit _without_ calling nfs4_close_done */
3130 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3131 nfs_release_seqid(calldata->arg.seqid);
3135 if (calldata->arg.fmode == 0)
3136 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3138 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3139 /* Close-to-open cache consistency revalidation */
3140 if (!nfs4_have_delegation(inode, FMODE_READ))
3141 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3143 calldata->arg.bitmask = NULL;
3146 calldata->arg.share_access =
3147 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3148 calldata->arg.fmode, 0);
3150 if (calldata->res.fattr == NULL)
3151 calldata->arg.bitmask = NULL;
3152 else if (calldata->arg.bitmask == NULL)
3153 calldata->res.fattr = NULL;
3154 calldata->timestamp = jiffies;
3155 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3156 &calldata->arg.seq_args,
3157 &calldata->res.seq_res,
3159 nfs_release_seqid(calldata->arg.seqid);
3160 dprintk("%s: done!\n", __func__);
3163 task->tk_action = NULL;
3165 nfs4_sequence_done(task, &calldata->res.seq_res);
3168 static const struct rpc_call_ops nfs4_close_ops = {
3169 .rpc_call_prepare = nfs4_close_prepare,
3170 .rpc_call_done = nfs4_close_done,
3171 .rpc_release = nfs4_free_closedata,
3175 * It is possible for data to be read/written from a mem-mapped file
3176 * after the sys_close call (which hits the vfs layer as a flush).
3177 * This means that we can't safely call nfsv4 close on a file until
3178 * the inode is cleared. This in turn means that we are not good
3179 * NFSv4 citizens - we do not indicate to the server to update the file's
3180 * share state even when we are done with one of the three share
3181 * stateid's in the inode.
3183 * NOTE: Caller must be holding the sp->so_owner semaphore!
3185 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3187 struct nfs_server *server = NFS_SERVER(state->inode);
3188 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3189 struct nfs4_closedata *calldata;
3190 struct nfs4_state_owner *sp = state->owner;
3191 struct rpc_task *task;
3192 struct rpc_message msg = {
3193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3194 .rpc_cred = state->owner->so_cred,
3196 struct rpc_task_setup task_setup_data = {
3197 .rpc_client = server->client,
3198 .rpc_message = &msg,
3199 .callback_ops = &nfs4_close_ops,
3200 .workqueue = nfsiod_workqueue,
3201 .flags = RPC_TASK_ASYNC,
3203 int status = -ENOMEM;
3205 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3206 &task_setup_data.rpc_client, &msg);
3208 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3209 if (calldata == NULL)
3211 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3212 calldata->inode = state->inode;
3213 calldata->state = state;
3214 calldata->arg.fh = NFS_FH(state->inode);
3215 /* Serialization for the sequence id */
3216 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3217 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3218 if (IS_ERR(calldata->arg.seqid))
3219 goto out_free_calldata;
3220 nfs_fattr_init(&calldata->fattr);
3221 calldata->arg.fmode = 0;
3222 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3223 calldata->res.fattr = &calldata->fattr;
3224 calldata->res.seqid = calldata->arg.seqid;
3225 calldata->res.server = server;
3226 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3227 calldata->lr.roc = pnfs_roc(state->inode,
3228 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3229 if (calldata->lr.roc) {
3230 calldata->arg.lr_args = &calldata->lr.arg;
3231 calldata->res.lr_res = &calldata->lr.res;
3233 nfs_sb_active(calldata->inode->i_sb);
3235 msg.rpc_argp = &calldata->arg;
3236 msg.rpc_resp = &calldata->res;
3237 task_setup_data.callback_data = calldata;
3238 task = rpc_run_task(&task_setup_data);
3240 return PTR_ERR(task);
3243 status = rpc_wait_for_completion_task(task);
3249 nfs4_put_open_state(state);
3250 nfs4_put_state_owner(sp);
3254 static struct inode *
3255 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3256 int open_flags, struct iattr *attr, int *opened)
3258 struct nfs4_state *state;
3259 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3261 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3263 /* Protect against concurrent sillydeletes */
3264 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3266 nfs4_label_release_security(label);
3269 return ERR_CAST(state);
3270 return state->inode;
3273 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3275 if (ctx->state == NULL)
3278 nfs4_close_sync(ctx->state, ctx->mode);
3280 nfs4_close_state(ctx->state, ctx->mode);
3283 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3284 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3285 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3287 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3289 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3290 struct nfs4_server_caps_arg args = {
3294 struct nfs4_server_caps_res res = {};
3295 struct rpc_message msg = {
3296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3303 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3304 FATTR4_WORD0_FH_EXPIRE_TYPE |
3305 FATTR4_WORD0_LINK_SUPPORT |
3306 FATTR4_WORD0_SYMLINK_SUPPORT |
3307 FATTR4_WORD0_ACLSUPPORT;
3309 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3311 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3313 /* Sanity check the server answers */
3314 switch (minorversion) {
3316 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3317 res.attr_bitmask[2] = 0;
3320 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3323 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3325 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3326 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3327 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3328 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3329 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3330 NFS_CAP_CTIME|NFS_CAP_MTIME|
3331 NFS_CAP_SECURITY_LABEL);
3332 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3333 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3334 server->caps |= NFS_CAP_ACLS;
3335 if (res.has_links != 0)
3336 server->caps |= NFS_CAP_HARDLINKS;
3337 if (res.has_symlinks != 0)
3338 server->caps |= NFS_CAP_SYMLINKS;
3339 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3340 server->caps |= NFS_CAP_FILEID;
3341 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3342 server->caps |= NFS_CAP_MODE;
3343 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3344 server->caps |= NFS_CAP_NLINK;
3345 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3346 server->caps |= NFS_CAP_OWNER;
3347 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3348 server->caps |= NFS_CAP_OWNER_GROUP;
3349 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3350 server->caps |= NFS_CAP_ATIME;
3351 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3352 server->caps |= NFS_CAP_CTIME;
3353 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3354 server->caps |= NFS_CAP_MTIME;
3355 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3356 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3357 server->caps |= NFS_CAP_SECURITY_LABEL;
3359 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3360 sizeof(server->attr_bitmask));
3361 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3363 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3364 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3365 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3366 server->cache_consistency_bitmask[2] = 0;
3368 /* Avoid a regression due to buggy server */
3369 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3370 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3371 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3372 sizeof(server->exclcreat_bitmask));
3374 server->acl_bitmask = res.acl_bitmask;
3375 server->fh_expire_type = res.fh_expire_type;
3381 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3383 struct nfs4_exception exception = { };
3386 err = nfs4_handle_exception(server,
3387 _nfs4_server_capabilities(server, fhandle),
3389 } while (exception.retry);
3393 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3394 struct nfs_fsinfo *info)
3397 struct nfs4_lookup_root_arg args = {
3400 struct nfs4_lookup_res res = {
3402 .fattr = info->fattr,
3405 struct rpc_message msg = {
3406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3411 bitmask[0] = nfs4_fattr_bitmap[0];
3412 bitmask[1] = nfs4_fattr_bitmap[1];
3414 * Process the label in the upcoming getfattr
3416 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3418 nfs_fattr_init(info->fattr);
3419 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3422 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3423 struct nfs_fsinfo *info)
3425 struct nfs4_exception exception = { };
3428 err = _nfs4_lookup_root(server, fhandle, info);
3429 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3432 case -NFS4ERR_WRONGSEC:
3435 err = nfs4_handle_exception(server, err, &exception);
3437 } while (exception.retry);
3442 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3443 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3445 struct rpc_auth_create_args auth_args = {
3446 .pseudoflavor = flavor,
3448 struct rpc_auth *auth;
3450 auth = rpcauth_create(&auth_args, server->client);
3453 return nfs4_lookup_root(server, fhandle, info);
3457 * Retry pseudoroot lookup with various security flavors. We do this when:
3459 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3460 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3462 * Returns zero on success, or a negative NFS4ERR value, or a
3463 * negative errno value.
3465 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3466 struct nfs_fsinfo *info)
3468 /* Per 3530bis 15.33.5 */
3469 static const rpc_authflavor_t flav_array[] = {
3473 RPC_AUTH_UNIX, /* courtesy */
3476 int status = -EPERM;
3479 if (server->auth_info.flavor_len > 0) {
3480 /* try each flavor specified by user */
3481 for (i = 0; i < server->auth_info.flavor_len; i++) {
3482 status = nfs4_lookup_root_sec(server, fhandle, info,
3483 server->auth_info.flavors[i]);
3484 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3489 /* no flavors specified by user, try default list */
3490 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3491 status = nfs4_lookup_root_sec(server, fhandle, info,
3493 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3500 * -EACCESS could mean that the user doesn't have correct permissions
3501 * to access the mount. It could also mean that we tried to mount
3502 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3503 * existing mount programs don't handle -EACCES very well so it should
3504 * be mapped to -EPERM instead.
3506 if (status == -EACCES)
3512 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3513 * @server: initialized nfs_server handle
3514 * @fhandle: we fill in the pseudo-fs root file handle
3515 * @info: we fill in an FSINFO struct
3516 * @auth_probe: probe the auth flavours
3518 * Returns zero on success, or a negative errno.
3520 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3521 struct nfs_fsinfo *info,
3527 status = nfs4_lookup_root(server, fhandle, info);
3529 if (auth_probe || status == NFS4ERR_WRONGSEC)
3530 status = server->nfs_client->cl_mvops->find_root_sec(server,
3534 status = nfs4_server_capabilities(server, fhandle);
3536 status = nfs4_do_fsinfo(server, fhandle, info);
3538 return nfs4_map_errors(status);
3541 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3542 struct nfs_fsinfo *info)
3545 struct nfs_fattr *fattr = info->fattr;
3546 struct nfs4_label *label = NULL;
3548 error = nfs4_server_capabilities(server, mntfh);
3550 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3554 label = nfs4_label_alloc(server, GFP_KERNEL);
3556 return PTR_ERR(label);
3558 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3560 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3561 goto err_free_label;
3564 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3565 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3566 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3569 nfs4_label_free(label);
3575 * Get locations and (maybe) other attributes of a referral.
3576 * Note that we'll actually follow the referral later when
3577 * we detect fsid mismatch in inode revalidation
3579 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3580 const struct qstr *name, struct nfs_fattr *fattr,
3581 struct nfs_fh *fhandle)
3583 int status = -ENOMEM;
3584 struct page *page = NULL;
3585 struct nfs4_fs_locations *locations = NULL;
3587 page = alloc_page(GFP_KERNEL);
3590 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3591 if (locations == NULL)
3594 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3599 * If the fsid didn't change, this is a migration event, not a
3600 * referral. Cause us to drop into the exception handler, which
3601 * will kick off migration recovery.
3603 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3604 dprintk("%s: server did not return a different fsid for"
3605 " a referral at %s\n", __func__, name->name);
3606 status = -NFS4ERR_MOVED;
3609 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3610 nfs_fixup_referral_attributes(&locations->fattr);
3612 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3613 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3614 memset(fhandle, 0, sizeof(struct nfs_fh));
3622 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3623 struct nfs_fattr *fattr, struct nfs4_label *label)
3625 struct nfs4_getattr_arg args = {
3627 .bitmask = server->attr_bitmask,
3629 struct nfs4_getattr_res res = {
3634 struct rpc_message msg = {
3635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3640 args.bitmask = nfs4_bitmask(server, label);
3642 nfs_fattr_init(fattr);
3643 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3646 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3647 struct nfs_fattr *fattr, struct nfs4_label *label)
3649 struct nfs4_exception exception = { };
3652 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3653 trace_nfs4_getattr(server, fhandle, fattr, err);
3654 err = nfs4_handle_exception(server, err,
3656 } while (exception.retry);
3661 * The file is not closed if it is opened due to the a request to change
3662 * the size of the file. The open call will not be needed once the
3663 * VFS layer lookup-intents are implemented.
3665 * Close is called when the inode is destroyed.
3666 * If we haven't opened the file for O_WRONLY, we
3667 * need to in the size_change case to obtain a stateid.
3670 * Because OPEN is always done by name in nfsv4, it is
3671 * possible that we opened a different file by the same
3672 * name. We can recognize this race condition, but we
3673 * can't do anything about it besides returning an error.
3675 * This will be fixed with VFS changes (lookup-intent).
3678 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3679 struct iattr *sattr)
3681 struct inode *inode = d_inode(dentry);
3682 struct rpc_cred *cred = NULL;
3683 struct nfs_open_context *ctx = NULL;
3684 struct nfs4_label *label = NULL;
3687 if (pnfs_ld_layoutret_on_setattr(inode) &&
3688 sattr->ia_valid & ATTR_SIZE &&
3689 sattr->ia_size < i_size_read(inode))
3690 pnfs_commit_and_return_layout(inode);
3692 nfs_fattr_init(fattr);
3694 /* Deal with open(O_TRUNC) */
3695 if (sattr->ia_valid & ATTR_OPEN)
3696 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3698 /* Optimization: if the end result is no change, don't RPC */
3699 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3702 /* Search for an existing open(O_WRITE) file */
3703 if (sattr->ia_valid & ATTR_FILE) {
3705 ctx = nfs_file_open_context(sattr->ia_file);
3710 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3712 return PTR_ERR(label);
3714 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3716 nfs_setattr_update_inode(inode, sattr, fattr);
3717 nfs_setsecurity(inode, fattr, label);
3719 nfs4_label_free(label);
3723 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3724 const struct qstr *name, struct nfs_fh *fhandle,
3725 struct nfs_fattr *fattr, struct nfs4_label *label)
3727 struct nfs_server *server = NFS_SERVER(dir);
3729 struct nfs4_lookup_arg args = {
3730 .bitmask = server->attr_bitmask,
3731 .dir_fh = NFS_FH(dir),
3734 struct nfs4_lookup_res res = {
3740 struct rpc_message msg = {
3741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3746 args.bitmask = nfs4_bitmask(server, label);
3748 nfs_fattr_init(fattr);
3750 dprintk("NFS call lookup %s\n", name->name);
3751 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3752 dprintk("NFS reply lookup: %d\n", status);
3756 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3758 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3759 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3760 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3764 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3765 const struct qstr *name, struct nfs_fh *fhandle,
3766 struct nfs_fattr *fattr, struct nfs4_label *label)
3768 struct nfs4_exception exception = { };
3769 struct rpc_clnt *client = *clnt;
3772 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3773 trace_nfs4_lookup(dir, name, err);
3775 case -NFS4ERR_BADNAME:
3778 case -NFS4ERR_MOVED:
3779 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3780 if (err == -NFS4ERR_MOVED)
3781 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3783 case -NFS4ERR_WRONGSEC:
3785 if (client != *clnt)
3787 client = nfs4_negotiate_security(client, dir, name);
3789 return PTR_ERR(client);
3791 exception.retry = 1;
3794 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3796 } while (exception.retry);
3801 else if (client != *clnt)
3802 rpc_shutdown_client(client);
3807 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3808 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3809 struct nfs4_label *label)
3812 struct rpc_clnt *client = NFS_CLIENT(dir);
3814 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3815 if (client != NFS_CLIENT(dir)) {
3816 rpc_shutdown_client(client);
3817 nfs_fixup_secinfo_attributes(fattr);
3823 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3824 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3826 struct rpc_clnt *client = NFS_CLIENT(dir);
3829 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3831 return ERR_PTR(status);
3832 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3835 static int _nfs4_proc_lookupp(struct inode *inode,
3836 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3837 struct nfs4_label *label)
3839 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3840 struct nfs_server *server = NFS_SERVER(inode);
3842 struct nfs4_lookupp_arg args = {
3843 .bitmask = server->attr_bitmask,
3844 .fh = NFS_FH(inode),
3846 struct nfs4_lookupp_res res = {
3852 struct rpc_message msg = {
3853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
3858 args.bitmask = nfs4_bitmask(server, label);
3860 nfs_fattr_init(fattr);
3862 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
3863 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
3865 dprintk("NFS reply lookupp: %d\n", status);
3869 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
3870 struct nfs_fattr *fattr, struct nfs4_label *label)
3872 struct nfs4_exception exception = { };
3875 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
3876 trace_nfs4_lookupp(inode, err);
3877 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3879 } while (exception.retry);
3883 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3885 struct nfs_server *server = NFS_SERVER(inode);
3886 struct nfs4_accessargs args = {
3887 .fh = NFS_FH(inode),
3888 .bitmask = server->cache_consistency_bitmask,
3890 struct nfs4_accessres res = {
3893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3897 .rpc_cred = entry->cred,
3899 int mode = entry->mask;
3903 * Determine which access bits we want to ask for...
3905 if (mode & MAY_READ)
3906 args.access |= NFS4_ACCESS_READ;
3907 if (S_ISDIR(inode->i_mode)) {
3908 if (mode & MAY_WRITE)
3909 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3910 if (mode & MAY_EXEC)
3911 args.access |= NFS4_ACCESS_LOOKUP;
3913 if (mode & MAY_WRITE)
3914 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3915 if (mode & MAY_EXEC)
3916 args.access |= NFS4_ACCESS_EXECUTE;
3919 res.fattr = nfs_alloc_fattr();
3920 if (res.fattr == NULL)
3923 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3925 nfs_access_set_mask(entry, res.access);
3926 nfs_refresh_inode(inode, res.fattr);
3928 nfs_free_fattr(res.fattr);
3932 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3934 struct nfs4_exception exception = { };
3937 err = _nfs4_proc_access(inode, entry);
3938 trace_nfs4_access(inode, err);
3939 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3941 } while (exception.retry);
3946 * TODO: For the time being, we don't try to get any attributes
3947 * along with any of the zero-copy operations READ, READDIR,
3950 * In the case of the first three, we want to put the GETATTR
3951 * after the read-type operation -- this is because it is hard
3952 * to predict the length of a GETATTR response in v4, and thus
3953 * align the READ data correctly. This means that the GETATTR
3954 * may end up partially falling into the page cache, and we should
3955 * shift it into the 'tail' of the xdr_buf before processing.
3956 * To do this efficiently, we need to know the total length
3957 * of data received, which doesn't seem to be available outside
3960 * In the case of WRITE, we also want to put the GETATTR after
3961 * the operation -- in this case because we want to make sure
3962 * we get the post-operation mtime and size.
3964 * Both of these changes to the XDR layer would in fact be quite
3965 * minor, but I decided to leave them for a subsequent patch.
3967 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3968 unsigned int pgbase, unsigned int pglen)
3970 struct nfs4_readlink args = {
3971 .fh = NFS_FH(inode),
3976 struct nfs4_readlink_res res;
3977 struct rpc_message msg = {
3978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3983 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3986 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3987 unsigned int pgbase, unsigned int pglen)
3989 struct nfs4_exception exception = { };
3992 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3993 trace_nfs4_readlink(inode, err);
3994 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3996 } while (exception.retry);
4001 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4004 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4007 struct nfs_server *server = NFS_SERVER(dir);
4008 struct nfs4_label l, *ilabel = NULL;
4009 struct nfs_open_context *ctx;
4010 struct nfs4_state *state;
4013 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4015 return PTR_ERR(ctx);
4017 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4019 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4020 sattr->ia_mode &= ~current_umask();
4021 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4022 if (IS_ERR(state)) {
4023 status = PTR_ERR(state);
4027 nfs4_label_release_security(ilabel);
4028 put_nfs_open_context(ctx);
4032 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4034 struct nfs_server *server = NFS_SERVER(dir);
4035 struct nfs_removeargs args = {
4039 struct nfs_removeres res = {
4042 struct rpc_message msg = {
4043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4047 unsigned long timestamp = jiffies;
4050 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4052 update_changeattr(dir, &res.cinfo, timestamp);
4056 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4058 struct nfs4_exception exception = { };
4061 err = _nfs4_proc_remove(dir, name);
4062 trace_nfs4_remove(dir, name, err);
4063 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4065 } while (exception.retry);
4069 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4071 struct nfs_server *server = NFS_SERVER(dir);
4072 struct nfs_removeargs *args = msg->rpc_argp;
4073 struct nfs_removeres *res = msg->rpc_resp;
4075 res->server = server;
4076 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4077 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4079 nfs_fattr_init(res->dir_attr);
4082 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4084 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4085 &data->args.seq_args,
4090 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4092 struct nfs_unlinkdata *data = task->tk_calldata;
4093 struct nfs_removeres *res = &data->res;
4095 if (!nfs4_sequence_done(task, &res->seq_res))
4097 if (nfs4_async_handle_error(task, res->server, NULL,
4098 &data->timeout) == -EAGAIN)
4100 if (task->tk_status == 0)
4101 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
4105 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4107 struct nfs_server *server = NFS_SERVER(dir);
4108 struct nfs_renameargs *arg = msg->rpc_argp;
4109 struct nfs_renameres *res = msg->rpc_resp;
4111 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4112 res->server = server;
4113 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4116 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4118 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4119 &data->args.seq_args,
4124 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4125 struct inode *new_dir)
4127 struct nfs_renamedata *data = task->tk_calldata;
4128 struct nfs_renameres *res = &data->res;
4130 if (!nfs4_sequence_done(task, &res->seq_res))
4132 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4135 if (task->tk_status == 0) {
4136 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
4137 if (new_dir != old_dir)
4138 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
4143 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4145 struct nfs_server *server = NFS_SERVER(inode);
4146 struct nfs4_link_arg arg = {
4147 .fh = NFS_FH(inode),
4148 .dir_fh = NFS_FH(dir),
4150 .bitmask = server->attr_bitmask,
4152 struct nfs4_link_res res = {
4156 struct rpc_message msg = {
4157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4161 int status = -ENOMEM;
4163 res.fattr = nfs_alloc_fattr();
4164 if (res.fattr == NULL)
4167 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4168 if (IS_ERR(res.label)) {
4169 status = PTR_ERR(res.label);
4172 arg.bitmask = nfs4_bitmask(server, res.label);
4174 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4176 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
4177 status = nfs_post_op_update_inode(inode, res.fattr);
4179 nfs_setsecurity(inode, res.fattr, res.label);
4183 nfs4_label_free(res.label);
4186 nfs_free_fattr(res.fattr);
4190 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4192 struct nfs4_exception exception = { };
4195 err = nfs4_handle_exception(NFS_SERVER(inode),
4196 _nfs4_proc_link(inode, dir, name),
4198 } while (exception.retry);
4202 struct nfs4_createdata {
4203 struct rpc_message msg;
4204 struct nfs4_create_arg arg;
4205 struct nfs4_create_res res;
4207 struct nfs_fattr fattr;
4208 struct nfs4_label *label;
4211 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4212 const struct qstr *name, struct iattr *sattr, u32 ftype)
4214 struct nfs4_createdata *data;
4216 data = kzalloc(sizeof(*data), GFP_KERNEL);
4218 struct nfs_server *server = NFS_SERVER(dir);
4220 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4221 if (IS_ERR(data->label))
4224 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4225 data->msg.rpc_argp = &data->arg;
4226 data->msg.rpc_resp = &data->res;
4227 data->arg.dir_fh = NFS_FH(dir);
4228 data->arg.server = server;
4229 data->arg.name = name;
4230 data->arg.attrs = sattr;
4231 data->arg.ftype = ftype;
4232 data->arg.bitmask = nfs4_bitmask(server, data->label);
4233 data->arg.umask = current_umask();
4234 data->res.server = server;
4235 data->res.fh = &data->fh;
4236 data->res.fattr = &data->fattr;
4237 data->res.label = data->label;
4238 nfs_fattr_init(data->res.fattr);
4246 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4248 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4249 &data->arg.seq_args, &data->res.seq_res, 1);
4251 update_changeattr(dir, &data->res.dir_cinfo,
4252 data->res.fattr->time_start);
4253 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4258 static void nfs4_free_createdata(struct nfs4_createdata *data)
4260 nfs4_label_free(data->label);
4264 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4265 struct page *page, unsigned int len, struct iattr *sattr,
4266 struct nfs4_label *label)
4268 struct nfs4_createdata *data;
4269 int status = -ENAMETOOLONG;
4271 if (len > NFS4_MAXPATHLEN)
4275 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4279 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4280 data->arg.u.symlink.pages = &page;
4281 data->arg.u.symlink.len = len;
4282 data->arg.label = label;
4284 status = nfs4_do_create(dir, dentry, data);
4286 nfs4_free_createdata(data);
4291 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4292 struct page *page, unsigned int len, struct iattr *sattr)
4294 struct nfs4_exception exception = { };
4295 struct nfs4_label l, *label = NULL;
4298 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4301 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4302 trace_nfs4_symlink(dir, &dentry->d_name, err);
4303 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4305 } while (exception.retry);
4307 nfs4_label_release_security(label);
4311 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4312 struct iattr *sattr, struct nfs4_label *label)
4314 struct nfs4_createdata *data;
4315 int status = -ENOMEM;
4317 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4321 data->arg.label = label;
4322 status = nfs4_do_create(dir, dentry, data);
4324 nfs4_free_createdata(data);
4329 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4330 struct iattr *sattr)
4332 struct nfs_server *server = NFS_SERVER(dir);
4333 struct nfs4_exception exception = { };
4334 struct nfs4_label l, *label = NULL;
4337 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4339 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4340 sattr->ia_mode &= ~current_umask();
4342 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4343 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4344 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4346 } while (exception.retry);
4347 nfs4_label_release_security(label);
4352 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4353 u64 cookie, struct page **pages, unsigned int count, bool plus)
4355 struct inode *dir = d_inode(dentry);
4356 struct nfs4_readdir_arg args = {
4361 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4364 struct nfs4_readdir_res res;
4365 struct rpc_message msg = {
4366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4373 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4375 (unsigned long long)cookie);
4376 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4377 res.pgbase = args.pgbase;
4378 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4380 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4381 status += args.pgbase;
4384 nfs_invalidate_atime(dir);
4386 dprintk("%s: returns %d\n", __func__, status);
4390 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4391 u64 cookie, struct page **pages, unsigned int count, bool plus)
4393 struct nfs4_exception exception = { };
4396 err = _nfs4_proc_readdir(dentry, cred, cookie,
4397 pages, count, plus);
4398 trace_nfs4_readdir(d_inode(dentry), err);
4399 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4401 } while (exception.retry);
4405 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4406 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4408 struct nfs4_createdata *data;
4409 int mode = sattr->ia_mode;
4410 int status = -ENOMEM;
4412 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4417 data->arg.ftype = NF4FIFO;
4418 else if (S_ISBLK(mode)) {
4419 data->arg.ftype = NF4BLK;
4420 data->arg.u.device.specdata1 = MAJOR(rdev);
4421 data->arg.u.device.specdata2 = MINOR(rdev);
4423 else if (S_ISCHR(mode)) {
4424 data->arg.ftype = NF4CHR;
4425 data->arg.u.device.specdata1 = MAJOR(rdev);
4426 data->arg.u.device.specdata2 = MINOR(rdev);
4427 } else if (!S_ISSOCK(mode)) {
4432 data->arg.label = label;
4433 status = nfs4_do_create(dir, dentry, data);
4435 nfs4_free_createdata(data);
4440 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4441 struct iattr *sattr, dev_t rdev)
4443 struct nfs_server *server = NFS_SERVER(dir);
4444 struct nfs4_exception exception = { };
4445 struct nfs4_label l, *label = NULL;
4448 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4450 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4451 sattr->ia_mode &= ~current_umask();
4453 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4454 trace_nfs4_mknod(dir, &dentry->d_name, err);
4455 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4457 } while (exception.retry);
4459 nfs4_label_release_security(label);
4464 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4465 struct nfs_fsstat *fsstat)
4467 struct nfs4_statfs_arg args = {
4469 .bitmask = server->attr_bitmask,
4471 struct nfs4_statfs_res res = {
4474 struct rpc_message msg = {
4475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4480 nfs_fattr_init(fsstat->fattr);
4481 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4484 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4486 struct nfs4_exception exception = { };
4489 err = nfs4_handle_exception(server,
4490 _nfs4_proc_statfs(server, fhandle, fsstat),
4492 } while (exception.retry);
4496 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4497 struct nfs_fsinfo *fsinfo)
4499 struct nfs4_fsinfo_arg args = {
4501 .bitmask = server->attr_bitmask,
4503 struct nfs4_fsinfo_res res = {
4506 struct rpc_message msg = {
4507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4512 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4515 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4517 struct nfs4_exception exception = { };
4518 unsigned long now = jiffies;
4522 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4523 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4525 nfs4_set_lease_period(server->nfs_client,
4526 fsinfo->lease_time * HZ,
4530 err = nfs4_handle_exception(server, err, &exception);
4531 } while (exception.retry);
4535 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4539 nfs_fattr_init(fsinfo->fattr);
4540 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4542 /* block layout checks this! */
4543 server->pnfs_blksize = fsinfo->blksize;
4544 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4550 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4551 struct nfs_pathconf *pathconf)
4553 struct nfs4_pathconf_arg args = {
4555 .bitmask = server->attr_bitmask,
4557 struct nfs4_pathconf_res res = {
4558 .pathconf = pathconf,
4560 struct rpc_message msg = {
4561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4566 /* None of the pathconf attributes are mandatory to implement */
4567 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4568 memset(pathconf, 0, sizeof(*pathconf));
4572 nfs_fattr_init(pathconf->fattr);
4573 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4576 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4577 struct nfs_pathconf *pathconf)
4579 struct nfs4_exception exception = { };
4583 err = nfs4_handle_exception(server,
4584 _nfs4_proc_pathconf(server, fhandle, pathconf),
4586 } while (exception.retry);
4590 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4591 const struct nfs_open_context *ctx,
4592 const struct nfs_lock_context *l_ctx,
4595 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4597 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4599 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4600 const struct nfs_open_context *ctx,
4601 const struct nfs_lock_context *l_ctx,
4604 nfs4_stateid current_stateid;
4606 /* If the current stateid represents a lost lock, then exit */
4607 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4609 return nfs4_stateid_match(stateid, ¤t_stateid);
4612 static bool nfs4_error_stateid_expired(int err)
4615 case -NFS4ERR_DELEG_REVOKED:
4616 case -NFS4ERR_ADMIN_REVOKED:
4617 case -NFS4ERR_BAD_STATEID:
4618 case -NFS4ERR_STALE_STATEID:
4619 case -NFS4ERR_OLD_STATEID:
4620 case -NFS4ERR_OPENMODE:
4621 case -NFS4ERR_EXPIRED:
4627 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4629 struct nfs_server *server = NFS_SERVER(hdr->inode);
4631 trace_nfs4_read(hdr, task->tk_status);
4632 if (task->tk_status < 0) {
4633 struct nfs4_exception exception = {
4634 .inode = hdr->inode,
4635 .state = hdr->args.context->state,
4636 .stateid = &hdr->args.stateid,
4638 task->tk_status = nfs4_async_handle_exception(task,
4639 server, task->tk_status, &exception);
4640 if (exception.retry) {
4641 rpc_restart_call_prepare(task);
4646 if (task->tk_status > 0)
4647 renew_lease(server, hdr->timestamp);
4651 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4652 struct nfs_pgio_args *args)
4655 if (!nfs4_error_stateid_expired(task->tk_status) ||
4656 nfs4_stateid_is_current(&args->stateid,
4661 rpc_restart_call_prepare(task);
4665 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4668 dprintk("--> %s\n", __func__);
4670 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4672 if (nfs4_read_stateid_changed(task, &hdr->args))
4674 if (task->tk_status > 0)
4675 nfs_invalidate_atime(hdr->inode);
4676 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4677 nfs4_read_done_cb(task, hdr);
4680 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4681 struct rpc_message *msg)
4683 hdr->timestamp = jiffies;
4684 if (!hdr->pgio_done_cb)
4685 hdr->pgio_done_cb = nfs4_read_done_cb;
4686 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4687 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4690 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4691 struct nfs_pgio_header *hdr)
4693 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
4694 &hdr->args.seq_args,
4698 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4699 hdr->args.lock_context,
4700 hdr->rw_mode) == -EIO)
4702 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4707 static int nfs4_write_done_cb(struct rpc_task *task,
4708 struct nfs_pgio_header *hdr)
4710 struct inode *inode = hdr->inode;
4712 trace_nfs4_write(hdr, task->tk_status);
4713 if (task->tk_status < 0) {
4714 struct nfs4_exception exception = {
4715 .inode = hdr->inode,
4716 .state = hdr->args.context->state,
4717 .stateid = &hdr->args.stateid,
4719 task->tk_status = nfs4_async_handle_exception(task,
4720 NFS_SERVER(inode), task->tk_status,
4722 if (exception.retry) {
4723 rpc_restart_call_prepare(task);
4727 if (task->tk_status >= 0) {
4728 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4729 nfs_writeback_update_inode(hdr);
4734 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4735 struct nfs_pgio_args *args)
4738 if (!nfs4_error_stateid_expired(task->tk_status) ||
4739 nfs4_stateid_is_current(&args->stateid,
4744 rpc_restart_call_prepare(task);
4748 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4750 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4752 if (nfs4_write_stateid_changed(task, &hdr->args))
4754 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4755 nfs4_write_done_cb(task, hdr);
4759 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4761 /* Don't request attributes for pNFS or O_DIRECT writes */
4762 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4764 /* Otherwise, request attributes if and only if we don't hold
4767 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4770 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4771 struct rpc_message *msg)
4773 struct nfs_server *server = NFS_SERVER(hdr->inode);
4775 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4776 hdr->args.bitmask = NULL;
4777 hdr->res.fattr = NULL;
4779 hdr->args.bitmask = server->cache_consistency_bitmask;
4781 if (!hdr->pgio_done_cb)
4782 hdr->pgio_done_cb = nfs4_write_done_cb;
4783 hdr->res.server = server;
4784 hdr->timestamp = jiffies;
4786 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4787 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4790 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4792 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
4793 &data->args.seq_args,
4798 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4800 struct inode *inode = data->inode;
4802 trace_nfs4_commit(data, task->tk_status);
4803 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4804 NULL, NULL) == -EAGAIN) {
4805 rpc_restart_call_prepare(task);
4811 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4813 if (!nfs4_sequence_done(task, &data->res.seq_res))
4815 return data->commit_done_cb(task, data);
4818 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4820 struct nfs_server *server = NFS_SERVER(data->inode);
4822 if (data->commit_done_cb == NULL)
4823 data->commit_done_cb = nfs4_commit_done_cb;
4824 data->res.server = server;
4825 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4826 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4829 struct nfs4_renewdata {
4830 struct nfs_client *client;
4831 unsigned long timestamp;
4835 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4836 * standalone procedure for queueing an asynchronous RENEW.
4838 static void nfs4_renew_release(void *calldata)
4840 struct nfs4_renewdata *data = calldata;
4841 struct nfs_client *clp = data->client;
4843 if (atomic_read(&clp->cl_count) > 1)
4844 nfs4_schedule_state_renewal(clp);
4845 nfs_put_client(clp);
4849 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4851 struct nfs4_renewdata *data = calldata;
4852 struct nfs_client *clp = data->client;
4853 unsigned long timestamp = data->timestamp;
4855 trace_nfs4_renew_async(clp, task->tk_status);
4856 switch (task->tk_status) {
4859 case -NFS4ERR_LEASE_MOVED:
4860 nfs4_schedule_lease_moved_recovery(clp);
4863 /* Unless we're shutting down, schedule state recovery! */
4864 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4866 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4867 nfs4_schedule_lease_recovery(clp);
4870 nfs4_schedule_path_down_recovery(clp);
4872 do_renew_lease(clp, timestamp);
4875 static const struct rpc_call_ops nfs4_renew_ops = {
4876 .rpc_call_done = nfs4_renew_done,
4877 .rpc_release = nfs4_renew_release,
4880 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4882 struct rpc_message msg = {
4883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4887 struct nfs4_renewdata *data;
4889 if (renew_flags == 0)
4891 if (!atomic_inc_not_zero(&clp->cl_count))
4893 data = kmalloc(sizeof(*data), GFP_NOFS);
4895 nfs_put_client(clp);
4899 data->timestamp = jiffies;
4900 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4901 &nfs4_renew_ops, data);
4904 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4906 struct rpc_message msg = {
4907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4911 unsigned long now = jiffies;
4914 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4917 do_renew_lease(clp, now);
4921 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4923 return server->caps & NFS_CAP_ACLS;
4926 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4927 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4930 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4932 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4933 struct page **pages)
4935 struct page *newpage, **spages;
4941 len = min_t(size_t, PAGE_SIZE, buflen);
4942 newpage = alloc_page(GFP_KERNEL);
4944 if (newpage == NULL)
4946 memcpy(page_address(newpage), buf, len);
4951 } while (buflen != 0);
4957 __free_page(spages[rc-1]);
4961 struct nfs4_cached_acl {
4967 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4969 struct nfs_inode *nfsi = NFS_I(inode);
4971 spin_lock(&inode->i_lock);
4972 kfree(nfsi->nfs4_acl);
4973 nfsi->nfs4_acl = acl;
4974 spin_unlock(&inode->i_lock);
4977 static void nfs4_zap_acl_attr(struct inode *inode)
4979 nfs4_set_cached_acl(inode, NULL);
4982 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4984 struct nfs_inode *nfsi = NFS_I(inode);
4985 struct nfs4_cached_acl *acl;
4988 spin_lock(&inode->i_lock);
4989 acl = nfsi->nfs4_acl;
4992 if (buf == NULL) /* user is just asking for length */
4994 if (acl->cached == 0)
4996 ret = -ERANGE; /* see getxattr(2) man page */
4997 if (acl->len > buflen)
4999 memcpy(buf, acl->data, acl->len);
5003 spin_unlock(&inode->i_lock);
5007 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5009 struct nfs4_cached_acl *acl;
5010 size_t buflen = sizeof(*acl) + acl_len;
5012 if (buflen <= PAGE_SIZE) {
5013 acl = kmalloc(buflen, GFP_KERNEL);
5017 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5019 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5026 nfs4_set_cached_acl(inode, acl);
5030 * The getxattr API returns the required buffer length when called with a
5031 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5032 * the required buf. On a NULL buf, we send a page of data to the server
5033 * guessing that the ACL request can be serviced by a page. If so, we cache
5034 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5035 * the cache. If not so, we throw away the page, and cache the required
5036 * length. The next getxattr call will then produce another round trip to
5037 * the server, this time with the input buf of the required size.
5039 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5041 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5042 struct nfs_getaclargs args = {
5043 .fh = NFS_FH(inode),
5047 struct nfs_getaclres res = {
5050 struct rpc_message msg = {
5051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5055 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5056 int ret = -ENOMEM, i;
5058 if (npages > ARRAY_SIZE(pages))
5061 for (i = 0; i < npages; i++) {
5062 pages[i] = alloc_page(GFP_KERNEL);
5067 /* for decoding across pages */
5068 res.acl_scratch = alloc_page(GFP_KERNEL);
5069 if (!res.acl_scratch)
5072 args.acl_len = npages * PAGE_SIZE;
5074 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5075 __func__, buf, buflen, npages, args.acl_len);
5076 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5077 &msg, &args.seq_args, &res.seq_res, 0);
5081 /* Handle the case where the passed-in buffer is too short */
5082 if (res.acl_flags & NFS4_ACL_TRUNC) {
5083 /* Did the user only issue a request for the acl length? */
5089 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5091 if (res.acl_len > buflen) {
5095 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5100 for (i = 0; i < npages; i++)
5102 __free_page(pages[i]);
5103 if (res.acl_scratch)
5104 __free_page(res.acl_scratch);
5108 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5110 struct nfs4_exception exception = { };
5113 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5114 trace_nfs4_get_acl(inode, ret);
5117 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5118 } while (exception.retry);
5122 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5124 struct nfs_server *server = NFS_SERVER(inode);
5127 if (!nfs4_server_supports_acls(server))
5129 ret = nfs_revalidate_inode(server, inode);
5132 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5133 nfs_zap_acl_cache(inode);
5134 ret = nfs4_read_cached_acl(inode, buf, buflen);
5136 /* -ENOENT is returned if there is no ACL or if there is an ACL
5137 * but no cached acl data, just the acl length */
5139 return nfs4_get_acl_uncached(inode, buf, buflen);
5142 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5144 struct nfs_server *server = NFS_SERVER(inode);
5145 struct page *pages[NFS4ACL_MAXPAGES];
5146 struct nfs_setaclargs arg = {
5147 .fh = NFS_FH(inode),
5151 struct nfs_setaclres res;
5152 struct rpc_message msg = {
5153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5157 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5160 if (!nfs4_server_supports_acls(server))
5162 if (npages > ARRAY_SIZE(pages))
5164 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5167 nfs4_inode_return_delegation(inode);
5168 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5171 * Free each page after tx, so the only ref left is
5172 * held by the network stack
5175 put_page(pages[i-1]);
5178 * Acl update can result in inode attribute update.
5179 * so mark the attribute cache invalid.
5181 spin_lock(&inode->i_lock);
5182 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5183 spin_unlock(&inode->i_lock);
5184 nfs_access_zap_cache(inode);
5185 nfs_zap_acl_cache(inode);
5189 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5191 struct nfs4_exception exception = { };
5194 err = __nfs4_proc_set_acl(inode, buf, buflen);
5195 trace_nfs4_set_acl(inode, err);
5196 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5198 } while (exception.retry);
5202 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5203 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5206 struct nfs_server *server = NFS_SERVER(inode);
5207 struct nfs_fattr fattr;
5208 struct nfs4_label label = {0, 0, buflen, buf};
5210 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5211 struct nfs4_getattr_arg arg = {
5212 .fh = NFS_FH(inode),
5215 struct nfs4_getattr_res res = {
5220 struct rpc_message msg = {
5221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5227 nfs_fattr_init(&fattr);
5229 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5232 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5234 if (buflen < label.len)
5239 static int nfs4_get_security_label(struct inode *inode, void *buf,
5242 struct nfs4_exception exception = { };
5245 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5249 err = _nfs4_get_security_label(inode, buf, buflen);
5250 trace_nfs4_get_security_label(inode, err);
5251 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5253 } while (exception.retry);
5257 static int _nfs4_do_set_security_label(struct inode *inode,
5258 struct nfs4_label *ilabel,
5259 struct nfs_fattr *fattr,
5260 struct nfs4_label *olabel)
5263 struct iattr sattr = {0};
5264 struct nfs_server *server = NFS_SERVER(inode);
5265 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5266 struct nfs_setattrargs arg = {
5267 .fh = NFS_FH(inode),
5273 struct nfs_setattrres res = {
5278 struct rpc_message msg = {
5279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5285 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5287 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5289 dprintk("%s failed: %d\n", __func__, status);
5294 static int nfs4_do_set_security_label(struct inode *inode,
5295 struct nfs4_label *ilabel,
5296 struct nfs_fattr *fattr,
5297 struct nfs4_label *olabel)
5299 struct nfs4_exception exception = { };
5303 err = _nfs4_do_set_security_label(inode, ilabel,
5305 trace_nfs4_set_security_label(inode, err);
5306 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5308 } while (exception.retry);
5313 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5315 struct nfs4_label ilabel, *olabel = NULL;
5316 struct nfs_fattr fattr;
5317 struct rpc_cred *cred;
5320 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5323 nfs_fattr_init(&fattr);
5327 ilabel.label = (char *)buf;
5328 ilabel.len = buflen;
5330 cred = rpc_lookup_cred();
5332 return PTR_ERR(cred);
5334 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5335 if (IS_ERR(olabel)) {
5336 status = -PTR_ERR(olabel);
5340 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5342 nfs_setsecurity(inode, &fattr, olabel);
5344 nfs4_label_free(olabel);
5349 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5352 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5353 nfs4_verifier *bootverf)
5357 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5358 /* An impossible timestamp guarantees this value
5359 * will never match a generated boot time. */
5360 verf[0] = cpu_to_be32(U32_MAX);
5361 verf[1] = cpu_to_be32(U32_MAX);
5363 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5364 u64 ns = ktime_to_ns(nn->boot_time);
5366 verf[0] = cpu_to_be32(ns >> 32);
5367 verf[1] = cpu_to_be32(ns);
5369 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5373 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5378 if (clp->cl_owner_id != NULL)
5382 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5383 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5385 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5389 if (len > NFS4_OPAQUE_LIMIT + 1)
5393 * Since this string is allocated at mount time, and held until the
5394 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5395 * about a memory-reclaim deadlock.
5397 str = kmalloc(len, GFP_KERNEL);
5402 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5404 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5405 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5408 clp->cl_owner_id = str;
5413 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5418 len = 10 + 10 + 1 + 10 + 1 +
5419 strlen(nfs4_client_id_uniquifier) + 1 +
5420 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5422 if (len > NFS4_OPAQUE_LIMIT + 1)
5426 * Since this string is allocated at mount time, and held until the
5427 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5428 * about a memory-reclaim deadlock.
5430 str = kmalloc(len, GFP_KERNEL);
5434 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5435 clp->rpc_ops->version, clp->cl_minorversion,
5436 nfs4_client_id_uniquifier,
5437 clp->cl_rpcclient->cl_nodename);
5438 clp->cl_owner_id = str;
5443 nfs4_init_uniform_client_string(struct nfs_client *clp)
5448 if (clp->cl_owner_id != NULL)
5451 if (nfs4_client_id_uniquifier[0] != '\0')
5452 return nfs4_init_uniquifier_client_string(clp);
5454 len = 10 + 10 + 1 + 10 + 1 +
5455 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5457 if (len > NFS4_OPAQUE_LIMIT + 1)
5461 * Since this string is allocated at mount time, and held until the
5462 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5463 * about a memory-reclaim deadlock.
5465 str = kmalloc(len, GFP_KERNEL);
5469 scnprintf(str, len, "Linux NFSv%u.%u %s",
5470 clp->rpc_ops->version, clp->cl_minorversion,
5471 clp->cl_rpcclient->cl_nodename);
5472 clp->cl_owner_id = str;
5477 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5478 * services. Advertise one based on the address family of the
5482 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5484 if (strchr(clp->cl_ipaddr, ':') != NULL)
5485 return scnprintf(buf, len, "tcp6");
5487 return scnprintf(buf, len, "tcp");
5490 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5492 struct nfs4_setclientid *sc = calldata;
5494 if (task->tk_status == 0)
5495 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5498 static const struct rpc_call_ops nfs4_setclientid_ops = {
5499 .rpc_call_done = nfs4_setclientid_done,
5503 * nfs4_proc_setclientid - Negotiate client ID
5504 * @clp: state data structure
5505 * @program: RPC program for NFSv4 callback service
5506 * @port: IP port number for NFS4 callback service
5507 * @cred: RPC credential to use for this call
5508 * @res: where to place the result
5510 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5512 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5513 unsigned short port, struct rpc_cred *cred,
5514 struct nfs4_setclientid_res *res)
5516 nfs4_verifier sc_verifier;
5517 struct nfs4_setclientid setclientid = {
5518 .sc_verifier = &sc_verifier,
5522 struct rpc_message msg = {
5523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5524 .rpc_argp = &setclientid,
5528 struct rpc_task *task;
5529 struct rpc_task_setup task_setup_data = {
5530 .rpc_client = clp->cl_rpcclient,
5531 .rpc_message = &msg,
5532 .callback_ops = &nfs4_setclientid_ops,
5533 .callback_data = &setclientid,
5534 .flags = RPC_TASK_TIMEOUT,
5538 /* nfs_client_id4 */
5539 nfs4_init_boot_verifier(clp, &sc_verifier);
5541 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5542 status = nfs4_init_uniform_client_string(clp);
5544 status = nfs4_init_nonuniform_client_string(clp);
5550 setclientid.sc_netid_len =
5551 nfs4_init_callback_netid(clp,
5552 setclientid.sc_netid,
5553 sizeof(setclientid.sc_netid));
5554 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5555 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5556 clp->cl_ipaddr, port >> 8, port & 255);
5558 dprintk("NFS call setclientid auth=%s, '%s'\n",
5559 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5561 task = rpc_run_task(&task_setup_data);
5563 status = PTR_ERR(task);
5566 status = task->tk_status;
5567 if (setclientid.sc_cred) {
5568 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5569 put_rpccred(setclientid.sc_cred);
5573 trace_nfs4_setclientid(clp, status);
5574 dprintk("NFS reply setclientid: %d\n", status);
5579 * nfs4_proc_setclientid_confirm - Confirm client ID
5580 * @clp: state data structure
5581 * @res: result of a previous SETCLIENTID
5582 * @cred: RPC credential to use for this call
5584 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5586 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5587 struct nfs4_setclientid_res *arg,
5588 struct rpc_cred *cred)
5590 struct rpc_message msg = {
5591 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5597 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5598 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5600 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5601 trace_nfs4_setclientid_confirm(clp, status);
5602 dprintk("NFS reply setclientid_confirm: %d\n", status);
5606 struct nfs4_delegreturndata {
5607 struct nfs4_delegreturnargs args;
5608 struct nfs4_delegreturnres res;
5610 nfs4_stateid stateid;
5611 unsigned long timestamp;
5613 struct nfs4_layoutreturn_args arg;
5614 struct nfs4_layoutreturn_res res;
5615 struct nfs4_xdr_opaque_data ld_private;
5619 struct nfs_fattr fattr;
5621 struct inode *inode;
5624 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5626 struct nfs4_delegreturndata *data = calldata;
5628 if (!nfs4_sequence_done(task, &data->res.seq_res))
5631 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5633 /* Handle Layoutreturn errors */
5634 if (data->args.lr_args && task->tk_status != 0) {
5635 switch(data->res.lr_ret) {
5637 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5640 data->args.lr_args = NULL;
5641 data->res.lr_res = NULL;
5643 case -NFS4ERR_ADMIN_REVOKED:
5644 case -NFS4ERR_DELEG_REVOKED:
5645 case -NFS4ERR_EXPIRED:
5646 case -NFS4ERR_BAD_STATEID:
5647 case -NFS4ERR_OLD_STATEID:
5648 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5649 case -NFS4ERR_WRONG_CRED:
5650 data->args.lr_args = NULL;
5651 data->res.lr_res = NULL;
5652 data->res.lr_ret = 0;
5653 rpc_restart_call_prepare(task);
5658 switch (task->tk_status) {
5660 renew_lease(data->res.server, data->timestamp);
5662 case -NFS4ERR_ADMIN_REVOKED:
5663 case -NFS4ERR_DELEG_REVOKED:
5664 case -NFS4ERR_EXPIRED:
5665 nfs4_free_revoked_stateid(data->res.server,
5667 task->tk_msg.rpc_cred);
5668 case -NFS4ERR_BAD_STATEID:
5669 case -NFS4ERR_OLD_STATEID:
5670 case -NFS4ERR_STALE_STATEID:
5671 task->tk_status = 0;
5673 case -NFS4ERR_ACCESS:
5674 if (data->args.bitmask) {
5675 data->args.bitmask = NULL;
5676 data->res.fattr = NULL;
5677 task->tk_status = 0;
5678 rpc_restart_call_prepare(task);
5682 if (nfs4_async_handle_error(task, data->res.server,
5683 NULL, NULL) == -EAGAIN) {
5684 rpc_restart_call_prepare(task);
5688 data->rpc_status = task->tk_status;
5691 static void nfs4_delegreturn_release(void *calldata)
5693 struct nfs4_delegreturndata *data = calldata;
5694 struct inode *inode = data->inode;
5698 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5700 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5701 nfs_iput_and_deactive(inode);
5706 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5708 struct nfs4_delegreturndata *d_data;
5710 d_data = (struct nfs4_delegreturndata *)data;
5712 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5715 nfs4_setup_sequence(d_data->res.server->nfs_client,
5716 &d_data->args.seq_args,
5717 &d_data->res.seq_res,
5721 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5722 .rpc_call_prepare = nfs4_delegreturn_prepare,
5723 .rpc_call_done = nfs4_delegreturn_done,
5724 .rpc_release = nfs4_delegreturn_release,
5727 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5729 struct nfs4_delegreturndata *data;
5730 struct nfs_server *server = NFS_SERVER(inode);
5731 struct rpc_task *task;
5732 struct rpc_message msg = {
5733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5736 struct rpc_task_setup task_setup_data = {
5737 .rpc_client = server->client,
5738 .rpc_message = &msg,
5739 .callback_ops = &nfs4_delegreturn_ops,
5740 .flags = RPC_TASK_ASYNC,
5744 data = kzalloc(sizeof(*data), GFP_NOFS);
5747 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5749 nfs4_state_protect(server->nfs_client,
5750 NFS_SP4_MACH_CRED_CLEANUP,
5751 &task_setup_data.rpc_client, &msg);
5753 data->args.fhandle = &data->fh;
5754 data->args.stateid = &data->stateid;
5755 data->args.bitmask = server->cache_consistency_bitmask;
5756 nfs_copy_fh(&data->fh, NFS_FH(inode));
5757 nfs4_stateid_copy(&data->stateid, stateid);
5758 data->res.fattr = &data->fattr;
5759 data->res.server = server;
5760 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5761 data->lr.arg.ld_private = &data->lr.ld_private;
5762 nfs_fattr_init(data->res.fattr);
5763 data->timestamp = jiffies;
5764 data->rpc_status = 0;
5765 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5766 data->inode = nfs_igrab_and_active(inode);
5769 data->args.lr_args = &data->lr.arg;
5770 data->res.lr_res = &data->lr.res;
5772 } else if (data->lr.roc) {
5773 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5774 data->lr.roc = false;
5777 task_setup_data.callback_data = data;
5778 msg.rpc_argp = &data->args;
5779 msg.rpc_resp = &data->res;
5780 task = rpc_run_task(&task_setup_data);
5782 return PTR_ERR(task);
5785 status = rpc_wait_for_completion_task(task);
5788 status = data->rpc_status;
5794 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5796 struct nfs_server *server = NFS_SERVER(inode);
5797 struct nfs4_exception exception = { };
5800 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5801 trace_nfs4_delegreturn(inode, stateid, err);
5803 case -NFS4ERR_STALE_STATEID:
5804 case -NFS4ERR_EXPIRED:
5808 err = nfs4_handle_exception(server, err, &exception);
5809 } while (exception.retry);
5813 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5815 struct inode *inode = state->inode;
5816 struct nfs_server *server = NFS_SERVER(inode);
5817 struct nfs_client *clp = server->nfs_client;
5818 struct nfs_lockt_args arg = {
5819 .fh = NFS_FH(inode),
5822 struct nfs_lockt_res res = {
5825 struct rpc_message msg = {
5826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5829 .rpc_cred = state->owner->so_cred,
5831 struct nfs4_lock_state *lsp;
5834 arg.lock_owner.clientid = clp->cl_clientid;
5835 status = nfs4_set_lock_state(state, request);
5838 lsp = request->fl_u.nfs4_fl.owner;
5839 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5840 arg.lock_owner.s_dev = server->s_dev;
5841 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5844 request->fl_type = F_UNLCK;
5846 case -NFS4ERR_DENIED:
5849 request->fl_ops->fl_release_private(request);
5850 request->fl_ops = NULL;
5855 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5857 struct nfs4_exception exception = { };
5861 err = _nfs4_proc_getlk(state, cmd, request);
5862 trace_nfs4_get_lock(request, state, cmd, err);
5863 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5865 } while (exception.retry);
5869 struct nfs4_unlockdata {
5870 struct nfs_locku_args arg;
5871 struct nfs_locku_res res;
5872 struct nfs4_lock_state *lsp;
5873 struct nfs_open_context *ctx;
5874 struct nfs_lock_context *l_ctx;
5875 struct file_lock fl;
5876 struct nfs_server *server;
5877 unsigned long timestamp;
5880 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5881 struct nfs_open_context *ctx,
5882 struct nfs4_lock_state *lsp,
5883 struct nfs_seqid *seqid)
5885 struct nfs4_unlockdata *p;
5886 struct inode *inode = lsp->ls_state->inode;
5888 p = kzalloc(sizeof(*p), GFP_NOFS);
5891 p->arg.fh = NFS_FH(inode);
5893 p->arg.seqid = seqid;
5894 p->res.seqid = seqid;
5896 atomic_inc(&lsp->ls_count);
5897 /* Ensure we don't close file until we're done freeing locks! */
5898 p->ctx = get_nfs_open_context(ctx);
5899 p->l_ctx = nfs_get_lock_context(ctx);
5900 memcpy(&p->fl, fl, sizeof(p->fl));
5901 p->server = NFS_SERVER(inode);
5905 static void nfs4_locku_release_calldata(void *data)
5907 struct nfs4_unlockdata *calldata = data;
5908 nfs_free_seqid(calldata->arg.seqid);
5909 nfs4_put_lock_state(calldata->lsp);
5910 nfs_put_lock_context(calldata->l_ctx);
5911 put_nfs_open_context(calldata->ctx);
5915 static void nfs4_locku_done(struct rpc_task *task, void *data)
5917 struct nfs4_unlockdata *calldata = data;
5919 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5921 switch (task->tk_status) {
5923 renew_lease(calldata->server, calldata->timestamp);
5924 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5925 if (nfs4_update_lock_stateid(calldata->lsp,
5926 &calldata->res.stateid))
5928 case -NFS4ERR_ADMIN_REVOKED:
5929 case -NFS4ERR_EXPIRED:
5930 nfs4_free_revoked_stateid(calldata->server,
5931 &calldata->arg.stateid,
5932 task->tk_msg.rpc_cred);
5933 case -NFS4ERR_BAD_STATEID:
5934 case -NFS4ERR_OLD_STATEID:
5935 case -NFS4ERR_STALE_STATEID:
5936 if (!nfs4_stateid_match(&calldata->arg.stateid,
5937 &calldata->lsp->ls_stateid))
5938 rpc_restart_call_prepare(task);
5941 if (nfs4_async_handle_error(task, calldata->server,
5942 NULL, NULL) == -EAGAIN)
5943 rpc_restart_call_prepare(task);
5945 nfs_release_seqid(calldata->arg.seqid);
5948 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5950 struct nfs4_unlockdata *calldata = data;
5952 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
5953 nfs_async_iocounter_wait(task, calldata->l_ctx))
5956 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5958 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5959 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5960 /* Note: exit _without_ running nfs4_locku_done */
5963 calldata->timestamp = jiffies;
5964 if (nfs4_setup_sequence(calldata->server->nfs_client,
5965 &calldata->arg.seq_args,
5966 &calldata->res.seq_res,
5968 nfs_release_seqid(calldata->arg.seqid);
5971 task->tk_action = NULL;
5973 nfs4_sequence_done(task, &calldata->res.seq_res);
5976 static const struct rpc_call_ops nfs4_locku_ops = {
5977 .rpc_call_prepare = nfs4_locku_prepare,
5978 .rpc_call_done = nfs4_locku_done,
5979 .rpc_release = nfs4_locku_release_calldata,
5982 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5983 struct nfs_open_context *ctx,
5984 struct nfs4_lock_state *lsp,
5985 struct nfs_seqid *seqid)
5987 struct nfs4_unlockdata *data;
5988 struct rpc_message msg = {
5989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5990 .rpc_cred = ctx->cred,
5992 struct rpc_task_setup task_setup_data = {
5993 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5994 .rpc_message = &msg,
5995 .callback_ops = &nfs4_locku_ops,
5996 .workqueue = nfsiod_workqueue,
5997 .flags = RPC_TASK_ASYNC,
6000 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6001 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6003 /* Ensure this is an unlock - when canceling a lock, the
6004 * canceled lock is passed in, and it won't be an unlock.
6006 fl->fl_type = F_UNLCK;
6007 if (fl->fl_flags & FL_CLOSE)
6008 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6010 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6012 nfs_free_seqid(seqid);
6013 return ERR_PTR(-ENOMEM);
6016 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6017 msg.rpc_argp = &data->arg;
6018 msg.rpc_resp = &data->res;
6019 task_setup_data.callback_data = data;
6020 return rpc_run_task(&task_setup_data);
6023 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6025 struct inode *inode = state->inode;
6026 struct nfs4_state_owner *sp = state->owner;
6027 struct nfs_inode *nfsi = NFS_I(inode);
6028 struct nfs_seqid *seqid;
6029 struct nfs4_lock_state *lsp;
6030 struct rpc_task *task;
6031 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6033 unsigned char fl_flags = request->fl_flags;
6035 status = nfs4_set_lock_state(state, request);
6036 /* Unlock _before_ we do the RPC call */
6037 request->fl_flags |= FL_EXISTS;
6038 /* Exclude nfs_delegation_claim_locks() */
6039 mutex_lock(&sp->so_delegreturn_mutex);
6040 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6041 down_read(&nfsi->rwsem);
6042 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6043 up_read(&nfsi->rwsem);
6044 mutex_unlock(&sp->so_delegreturn_mutex);
6047 up_read(&nfsi->rwsem);
6048 mutex_unlock(&sp->so_delegreturn_mutex);
6051 /* Is this a delegated lock? */
6052 lsp = request->fl_u.nfs4_fl.owner;
6053 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6055 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6056 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6060 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6061 status = PTR_ERR(task);
6064 status = rpc_wait_for_completion_task(task);
6067 request->fl_flags = fl_flags;
6068 trace_nfs4_unlock(request, state, F_SETLK, status);
6072 struct nfs4_lockdata {
6073 struct nfs_lock_args arg;
6074 struct nfs_lock_res res;
6075 struct nfs4_lock_state *lsp;
6076 struct nfs_open_context *ctx;
6077 struct file_lock fl;
6078 unsigned long timestamp;
6081 struct nfs_server *server;
6084 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6085 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6088 struct nfs4_lockdata *p;
6089 struct inode *inode = lsp->ls_state->inode;
6090 struct nfs_server *server = NFS_SERVER(inode);
6091 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6093 p = kzalloc(sizeof(*p), gfp_mask);
6097 p->arg.fh = NFS_FH(inode);
6099 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6100 if (IS_ERR(p->arg.open_seqid))
6102 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6103 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6104 if (IS_ERR(p->arg.lock_seqid))
6105 goto out_free_seqid;
6106 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6107 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6108 p->arg.lock_owner.s_dev = server->s_dev;
6109 p->res.lock_seqid = p->arg.lock_seqid;
6112 atomic_inc(&lsp->ls_count);
6113 p->ctx = get_nfs_open_context(ctx);
6114 memcpy(&p->fl, fl, sizeof(p->fl));
6117 nfs_free_seqid(p->arg.open_seqid);
6123 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6125 struct nfs4_lockdata *data = calldata;
6126 struct nfs4_state *state = data->lsp->ls_state;
6128 dprintk("%s: begin!\n", __func__);
6129 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6131 /* Do we need to do an open_to_lock_owner? */
6132 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6133 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6134 goto out_release_lock_seqid;
6136 nfs4_stateid_copy(&data->arg.open_stateid,
6137 &state->open_stateid);
6138 data->arg.new_lock_owner = 1;
6139 data->res.open_seqid = data->arg.open_seqid;
6141 data->arg.new_lock_owner = 0;
6142 nfs4_stateid_copy(&data->arg.lock_stateid,
6143 &data->lsp->ls_stateid);
6145 if (!nfs4_valid_open_stateid(state)) {
6146 data->rpc_status = -EBADF;
6147 task->tk_action = NULL;
6148 goto out_release_open_seqid;
6150 data->timestamp = jiffies;
6151 if (nfs4_setup_sequence(data->server->nfs_client,
6152 &data->arg.seq_args,
6156 out_release_open_seqid:
6157 nfs_release_seqid(data->arg.open_seqid);
6158 out_release_lock_seqid:
6159 nfs_release_seqid(data->arg.lock_seqid);
6161 nfs4_sequence_done(task, &data->res.seq_res);
6162 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6165 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6167 struct nfs4_lockdata *data = calldata;
6168 struct nfs4_lock_state *lsp = data->lsp;
6170 dprintk("%s: begin!\n", __func__);
6172 if (!nfs4_sequence_done(task, &data->res.seq_res))
6175 data->rpc_status = task->tk_status;
6176 switch (task->tk_status) {
6178 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6180 if (data->arg.new_lock) {
6181 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6182 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6183 rpc_restart_call_prepare(task);
6187 if (data->arg.new_lock_owner != 0) {
6188 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6189 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6190 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6191 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6192 rpc_restart_call_prepare(task);
6194 case -NFS4ERR_BAD_STATEID:
6195 case -NFS4ERR_OLD_STATEID:
6196 case -NFS4ERR_STALE_STATEID:
6197 case -NFS4ERR_EXPIRED:
6198 if (data->arg.new_lock_owner != 0) {
6199 if (!nfs4_stateid_match(&data->arg.open_stateid,
6200 &lsp->ls_state->open_stateid))
6201 rpc_restart_call_prepare(task);
6202 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6204 rpc_restart_call_prepare(task);
6206 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6209 static void nfs4_lock_release(void *calldata)
6211 struct nfs4_lockdata *data = calldata;
6213 dprintk("%s: begin!\n", __func__);
6214 nfs_free_seqid(data->arg.open_seqid);
6215 if (data->cancelled) {
6216 struct rpc_task *task;
6217 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6218 data->arg.lock_seqid);
6220 rpc_put_task_async(task);
6221 dprintk("%s: cancelling lock!\n", __func__);
6223 nfs_free_seqid(data->arg.lock_seqid);
6224 nfs4_put_lock_state(data->lsp);
6225 put_nfs_open_context(data->ctx);
6227 dprintk("%s: done!\n", __func__);
6230 static const struct rpc_call_ops nfs4_lock_ops = {
6231 .rpc_call_prepare = nfs4_lock_prepare,
6232 .rpc_call_done = nfs4_lock_done,
6233 .rpc_release = nfs4_lock_release,
6236 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6239 case -NFS4ERR_ADMIN_REVOKED:
6240 case -NFS4ERR_EXPIRED:
6241 case -NFS4ERR_BAD_STATEID:
6242 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6243 if (new_lock_owner != 0 ||
6244 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6245 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6247 case -NFS4ERR_STALE_STATEID:
6248 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6249 nfs4_schedule_lease_recovery(server->nfs_client);
6253 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6255 struct nfs4_lockdata *data;
6256 struct rpc_task *task;
6257 struct rpc_message msg = {
6258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6259 .rpc_cred = state->owner->so_cred,
6261 struct rpc_task_setup task_setup_data = {
6262 .rpc_client = NFS_CLIENT(state->inode),
6263 .rpc_message = &msg,
6264 .callback_ops = &nfs4_lock_ops,
6265 .workqueue = nfsiod_workqueue,
6266 .flags = RPC_TASK_ASYNC,
6270 dprintk("%s: begin!\n", __func__);
6271 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6272 fl->fl_u.nfs4_fl.owner,
6273 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6277 data->arg.block = 1;
6278 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6279 msg.rpc_argp = &data->arg;
6280 msg.rpc_resp = &data->res;
6281 task_setup_data.callback_data = data;
6282 if (recovery_type > NFS_LOCK_NEW) {
6283 if (recovery_type == NFS_LOCK_RECLAIM)
6284 data->arg.reclaim = NFS_LOCK_RECLAIM;
6285 nfs4_set_sequence_privileged(&data->arg.seq_args);
6287 data->arg.new_lock = 1;
6288 task = rpc_run_task(&task_setup_data);
6290 return PTR_ERR(task);
6291 ret = rpc_wait_for_completion_task(task);
6293 ret = data->rpc_status;
6295 nfs4_handle_setlk_error(data->server, data->lsp,
6296 data->arg.new_lock_owner, ret);
6298 data->cancelled = true;
6300 dprintk("%s: done, ret = %d!\n", __func__, ret);
6301 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6305 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6307 struct nfs_server *server = NFS_SERVER(state->inode);
6308 struct nfs4_exception exception = {
6309 .inode = state->inode,
6314 /* Cache the lock if possible... */
6315 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6317 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6318 if (err != -NFS4ERR_DELAY)
6320 nfs4_handle_exception(server, err, &exception);
6321 } while (exception.retry);
6325 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6327 struct nfs_server *server = NFS_SERVER(state->inode);
6328 struct nfs4_exception exception = {
6329 .inode = state->inode,
6333 err = nfs4_set_lock_state(state, request);
6336 if (!recover_lost_locks) {
6337 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6341 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6343 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6347 case -NFS4ERR_GRACE:
6348 case -NFS4ERR_DELAY:
6349 nfs4_handle_exception(server, err, &exception);
6352 } while (exception.retry);
6357 #if defined(CONFIG_NFS_V4_1)
6358 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6360 struct nfs4_lock_state *lsp;
6363 status = nfs4_set_lock_state(state, request);
6366 lsp = request->fl_u.nfs4_fl.owner;
6367 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6368 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6370 return nfs4_lock_expired(state, request);
6374 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6376 struct nfs_inode *nfsi = NFS_I(state->inode);
6377 struct nfs4_state_owner *sp = state->owner;
6378 unsigned char fl_flags = request->fl_flags;
6381 request->fl_flags |= FL_ACCESS;
6382 status = locks_lock_inode_wait(state->inode, request);
6385 mutex_lock(&sp->so_delegreturn_mutex);
6386 down_read(&nfsi->rwsem);
6387 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6388 /* Yes: cache locks! */
6389 /* ...but avoid races with delegation recall... */
6390 request->fl_flags = fl_flags & ~FL_SLEEP;
6391 status = locks_lock_inode_wait(state->inode, request);
6392 up_read(&nfsi->rwsem);
6393 mutex_unlock(&sp->so_delegreturn_mutex);
6396 up_read(&nfsi->rwsem);
6397 mutex_unlock(&sp->so_delegreturn_mutex);
6398 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6400 request->fl_flags = fl_flags;
6404 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6406 struct nfs4_exception exception = {
6408 .inode = state->inode,
6413 err = _nfs4_proc_setlk(state, cmd, request);
6414 if (err == -NFS4ERR_DENIED)
6416 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6418 } while (exception.retry);
6422 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6423 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6426 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6427 struct file_lock *request)
6429 int status = -ERESTARTSYS;
6430 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6432 while(!signalled()) {
6433 status = nfs4_proc_setlk(state, cmd, request);
6434 if ((status != -EAGAIN) || IS_SETLK(cmd))
6436 freezable_schedule_timeout_interruptible(timeout);
6438 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6439 status = -ERESTARTSYS;
6444 #ifdef CONFIG_NFS_V4_1
6445 struct nfs4_lock_waiter {
6446 struct task_struct *task;
6447 struct inode *inode;
6448 struct nfs_lowner *owner;
6453 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6456 struct cb_notify_lock_args *cbnl = key;
6457 struct nfs4_lock_waiter *waiter = wait->private;
6458 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6459 *wowner = waiter->owner;
6461 /* Only wake if the callback was for the same owner */
6462 if (lowner->clientid != wowner->clientid ||
6463 lowner->id != wowner->id ||
6464 lowner->s_dev != wowner->s_dev)
6467 /* Make sure it's for the right inode */
6468 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6471 waiter->notified = true;
6473 /* override "private" so we can use default_wake_function */
6474 wait->private = waiter->task;
6475 ret = autoremove_wake_function(wait, mode, flags, key);
6476 wait->private = waiter;
6481 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6483 int status = -ERESTARTSYS;
6484 unsigned long flags;
6485 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6486 struct nfs_server *server = NFS_SERVER(state->inode);
6487 struct nfs_client *clp = server->nfs_client;
6488 wait_queue_head_t *q = &clp->cl_lock_waitq;
6489 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6490 .id = lsp->ls_seqid.owner_id,
6491 .s_dev = server->s_dev };
6492 struct nfs4_lock_waiter waiter = { .task = current,
6493 .inode = state->inode,
6495 .notified = false };
6496 wait_queue_entry_t wait;
6498 /* Don't bother with waitqueue if we don't expect a callback */
6499 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6500 return nfs4_retry_setlk_simple(state, cmd, request);
6503 wait.private = &waiter;
6504 wait.func = nfs4_wake_lock_waiter;
6505 add_wait_queue(q, &wait);
6507 while(!signalled()) {
6508 status = nfs4_proc_setlk(state, cmd, request);
6509 if ((status != -EAGAIN) || IS_SETLK(cmd))
6512 status = -ERESTARTSYS;
6513 spin_lock_irqsave(&q->lock, flags);
6514 if (waiter.notified) {
6515 spin_unlock_irqrestore(&q->lock, flags);
6518 set_current_state(TASK_INTERRUPTIBLE);
6519 spin_unlock_irqrestore(&q->lock, flags);
6521 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6524 finish_wait(q, &wait);
6527 #else /* !CONFIG_NFS_V4_1 */
6529 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6531 return nfs4_retry_setlk_simple(state, cmd, request);
6536 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6538 struct nfs_open_context *ctx;
6539 struct nfs4_state *state;
6542 /* verify open state */
6543 ctx = nfs_file_open_context(filp);
6546 if (IS_GETLK(cmd)) {
6548 return nfs4_proc_getlk(state, F_GETLK, request);
6552 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6555 if (request->fl_type == F_UNLCK) {
6557 return nfs4_proc_unlck(state, cmd, request);
6564 if ((request->fl_flags & FL_POSIX) &&
6565 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6569 * Don't rely on the VFS having checked the file open mode,
6570 * since it won't do this for flock() locks.
6572 switch (request->fl_type) {
6574 if (!(filp->f_mode & FMODE_READ))
6578 if (!(filp->f_mode & FMODE_WRITE))
6582 status = nfs4_set_lock_state(state, request);
6586 return nfs4_retry_setlk(state, cmd, request);
6589 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6591 struct nfs_server *server = NFS_SERVER(state->inode);
6594 err = nfs4_set_lock_state(state, fl);
6597 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6598 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6601 struct nfs_release_lockowner_data {
6602 struct nfs4_lock_state *lsp;
6603 struct nfs_server *server;
6604 struct nfs_release_lockowner_args args;
6605 struct nfs_release_lockowner_res res;
6606 unsigned long timestamp;
6609 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6611 struct nfs_release_lockowner_data *data = calldata;
6612 struct nfs_server *server = data->server;
6613 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6614 &data->res.seq_res, task);
6615 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6616 data->timestamp = jiffies;
6619 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6621 struct nfs_release_lockowner_data *data = calldata;
6622 struct nfs_server *server = data->server;
6624 nfs40_sequence_done(task, &data->res.seq_res);
6626 switch (task->tk_status) {
6628 renew_lease(server, data->timestamp);
6630 case -NFS4ERR_STALE_CLIENTID:
6631 case -NFS4ERR_EXPIRED:
6632 nfs4_schedule_lease_recovery(server->nfs_client);
6634 case -NFS4ERR_LEASE_MOVED:
6635 case -NFS4ERR_DELAY:
6636 if (nfs4_async_handle_error(task, server,
6637 NULL, NULL) == -EAGAIN)
6638 rpc_restart_call_prepare(task);
6642 static void nfs4_release_lockowner_release(void *calldata)
6644 struct nfs_release_lockowner_data *data = calldata;
6645 nfs4_free_lock_state(data->server, data->lsp);
6649 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6650 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6651 .rpc_call_done = nfs4_release_lockowner_done,
6652 .rpc_release = nfs4_release_lockowner_release,
6656 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6658 struct nfs_release_lockowner_data *data;
6659 struct rpc_message msg = {
6660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6663 if (server->nfs_client->cl_mvops->minor_version != 0)
6666 data = kmalloc(sizeof(*data), GFP_NOFS);
6670 data->server = server;
6671 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6672 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6673 data->args.lock_owner.s_dev = server->s_dev;
6675 msg.rpc_argp = &data->args;
6676 msg.rpc_resp = &data->res;
6677 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6678 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6681 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6683 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6684 struct dentry *unused, struct inode *inode,
6685 const char *key, const void *buf,
6686 size_t buflen, int flags)
6688 return nfs4_proc_set_acl(inode, buf, buflen);
6691 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6692 struct dentry *unused, struct inode *inode,
6693 const char *key, void *buf, size_t buflen)
6695 return nfs4_proc_get_acl(inode, buf, buflen);
6698 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6700 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6703 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6705 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6706 struct dentry *unused, struct inode *inode,
6707 const char *key, const void *buf,
6708 size_t buflen, int flags)
6710 if (security_ismaclabel(key))
6711 return nfs4_set_security_label(inode, buf, buflen);
6716 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6717 struct dentry *unused, struct inode *inode,
6718 const char *key, void *buf, size_t buflen)
6720 if (security_ismaclabel(key))
6721 return nfs4_get_security_label(inode, buf, buflen);
6726 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6730 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6731 len = security_inode_listsecurity(inode, list, list_len);
6732 if (list_len && len > list_len)
6738 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6739 .prefix = XATTR_SECURITY_PREFIX,
6740 .get = nfs4_xattr_get_nfs4_label,
6741 .set = nfs4_xattr_set_nfs4_label,
6747 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6755 * nfs_fhget will use either the mounted_on_fileid or the fileid
6757 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6759 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6760 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6761 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6762 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6765 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6766 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6767 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6771 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6772 const struct qstr *name,
6773 struct nfs4_fs_locations *fs_locations,
6776 struct nfs_server *server = NFS_SERVER(dir);
6778 struct nfs4_fs_locations_arg args = {
6779 .dir_fh = NFS_FH(dir),
6784 struct nfs4_fs_locations_res res = {
6785 .fs_locations = fs_locations,
6787 struct rpc_message msg = {
6788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6794 dprintk("%s: start\n", __func__);
6796 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6797 bitmask[1] = nfs4_fattr_bitmap[1];
6799 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6800 * is not supported */
6801 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6802 bitmask[0] &= ~FATTR4_WORD0_FILEID;
6804 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
6806 nfs_fattr_init(&fs_locations->fattr);
6807 fs_locations->server = server;
6808 fs_locations->nlocations = 0;
6809 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6810 dprintk("%s: returned status = %d\n", __func__, status);
6814 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6815 const struct qstr *name,
6816 struct nfs4_fs_locations *fs_locations,
6819 struct nfs4_exception exception = { };
6822 err = _nfs4_proc_fs_locations(client, dir, name,
6823 fs_locations, page);
6824 trace_nfs4_get_fs_locations(dir, name, err);
6825 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6827 } while (exception.retry);
6832 * This operation also signals the server that this client is
6833 * performing migration recovery. The server can stop returning
6834 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6835 * appended to this compound to identify the client ID which is
6836 * performing recovery.
6838 static int _nfs40_proc_get_locations(struct inode *inode,
6839 struct nfs4_fs_locations *locations,
6840 struct page *page, struct rpc_cred *cred)
6842 struct nfs_server *server = NFS_SERVER(inode);
6843 struct rpc_clnt *clnt = server->client;
6845 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6847 struct nfs4_fs_locations_arg args = {
6848 .clientid = server->nfs_client->cl_clientid,
6849 .fh = NFS_FH(inode),
6852 .migration = 1, /* skip LOOKUP */
6853 .renew = 1, /* append RENEW */
6855 struct nfs4_fs_locations_res res = {
6856 .fs_locations = locations,
6860 struct rpc_message msg = {
6861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6866 unsigned long now = jiffies;
6869 nfs_fattr_init(&locations->fattr);
6870 locations->server = server;
6871 locations->nlocations = 0;
6873 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6874 nfs4_set_sequence_privileged(&args.seq_args);
6875 status = nfs4_call_sync_sequence(clnt, server, &msg,
6876 &args.seq_args, &res.seq_res);
6880 renew_lease(server, now);
6884 #ifdef CONFIG_NFS_V4_1
6887 * This operation also signals the server that this client is
6888 * performing migration recovery. The server can stop asserting
6889 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6890 * performing this operation is identified in the SEQUENCE
6891 * operation in this compound.
6893 * When the client supports GETATTR(fs_locations_info), it can
6894 * be plumbed in here.
6896 static int _nfs41_proc_get_locations(struct inode *inode,
6897 struct nfs4_fs_locations *locations,
6898 struct page *page, struct rpc_cred *cred)
6900 struct nfs_server *server = NFS_SERVER(inode);
6901 struct rpc_clnt *clnt = server->client;
6903 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6905 struct nfs4_fs_locations_arg args = {
6906 .fh = NFS_FH(inode),
6909 .migration = 1, /* skip LOOKUP */
6911 struct nfs4_fs_locations_res res = {
6912 .fs_locations = locations,
6915 struct rpc_message msg = {
6916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6923 nfs_fattr_init(&locations->fattr);
6924 locations->server = server;
6925 locations->nlocations = 0;
6927 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6928 nfs4_set_sequence_privileged(&args.seq_args);
6929 status = nfs4_call_sync_sequence(clnt, server, &msg,
6930 &args.seq_args, &res.seq_res);
6931 if (status == NFS4_OK &&
6932 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6933 status = -NFS4ERR_LEASE_MOVED;
6937 #endif /* CONFIG_NFS_V4_1 */
6940 * nfs4_proc_get_locations - discover locations for a migrated FSID
6941 * @inode: inode on FSID that is migrating
6942 * @locations: result of query
6944 * @cred: credential to use for this operation
6946 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6947 * operation failed, or a negative errno if a local error occurred.
6949 * On success, "locations" is filled in, but if the server has
6950 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6953 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6954 * from this client that require migration recovery.
6956 int nfs4_proc_get_locations(struct inode *inode,
6957 struct nfs4_fs_locations *locations,
6958 struct page *page, struct rpc_cred *cred)
6960 struct nfs_server *server = NFS_SERVER(inode);
6961 struct nfs_client *clp = server->nfs_client;
6962 const struct nfs4_mig_recovery_ops *ops =
6963 clp->cl_mvops->mig_recovery_ops;
6964 struct nfs4_exception exception = { };
6967 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6968 (unsigned long long)server->fsid.major,
6969 (unsigned long long)server->fsid.minor,
6971 nfs_display_fhandle(NFS_FH(inode), __func__);
6974 status = ops->get_locations(inode, locations, page, cred);
6975 if (status != -NFS4ERR_DELAY)
6977 nfs4_handle_exception(server, status, &exception);
6978 } while (exception.retry);
6983 * This operation also signals the server that this client is
6984 * performing "lease moved" recovery. The server can stop
6985 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6986 * is appended to this compound to identify the client ID which is
6987 * performing recovery.
6989 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6991 struct nfs_server *server = NFS_SERVER(inode);
6992 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6993 struct rpc_clnt *clnt = server->client;
6994 struct nfs4_fsid_present_arg args = {
6995 .fh = NFS_FH(inode),
6996 .clientid = clp->cl_clientid,
6997 .renew = 1, /* append RENEW */
6999 struct nfs4_fsid_present_res res = {
7002 struct rpc_message msg = {
7003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7008 unsigned long now = jiffies;
7011 res.fh = nfs_alloc_fhandle();
7015 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7016 nfs4_set_sequence_privileged(&args.seq_args);
7017 status = nfs4_call_sync_sequence(clnt, server, &msg,
7018 &args.seq_args, &res.seq_res);
7019 nfs_free_fhandle(res.fh);
7023 do_renew_lease(clp, now);
7027 #ifdef CONFIG_NFS_V4_1
7030 * This operation also signals the server that this client is
7031 * performing "lease moved" recovery. The server can stop asserting
7032 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7033 * this operation is identified in the SEQUENCE operation in this
7036 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7038 struct nfs_server *server = NFS_SERVER(inode);
7039 struct rpc_clnt *clnt = server->client;
7040 struct nfs4_fsid_present_arg args = {
7041 .fh = NFS_FH(inode),
7043 struct nfs4_fsid_present_res res = {
7045 struct rpc_message msg = {
7046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7053 res.fh = nfs_alloc_fhandle();
7057 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7058 nfs4_set_sequence_privileged(&args.seq_args);
7059 status = nfs4_call_sync_sequence(clnt, server, &msg,
7060 &args.seq_args, &res.seq_res);
7061 nfs_free_fhandle(res.fh);
7062 if (status == NFS4_OK &&
7063 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7064 status = -NFS4ERR_LEASE_MOVED;
7068 #endif /* CONFIG_NFS_V4_1 */
7071 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7072 * @inode: inode on FSID to check
7073 * @cred: credential to use for this operation
7075 * Server indicates whether the FSID is present, moved, or not
7076 * recognized. This operation is necessary to clear a LEASE_MOVED
7077 * condition for this client ID.
7079 * Returns NFS4_OK if the FSID is present on this server,
7080 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7081 * NFS4ERR code if some error occurred on the server, or a
7082 * negative errno if a local failure occurred.
7084 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7086 struct nfs_server *server = NFS_SERVER(inode);
7087 struct nfs_client *clp = server->nfs_client;
7088 const struct nfs4_mig_recovery_ops *ops =
7089 clp->cl_mvops->mig_recovery_ops;
7090 struct nfs4_exception exception = { };
7093 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7094 (unsigned long long)server->fsid.major,
7095 (unsigned long long)server->fsid.minor,
7097 nfs_display_fhandle(NFS_FH(inode), __func__);
7100 status = ops->fsid_present(inode, cred);
7101 if (status != -NFS4ERR_DELAY)
7103 nfs4_handle_exception(server, status, &exception);
7104 } while (exception.retry);
7109 * If 'use_integrity' is true and the state managment nfs_client
7110 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7111 * and the machine credential as per RFC3530bis and RFC5661 Security
7112 * Considerations sections. Otherwise, just use the user cred with the
7113 * filesystem's rpc_client.
7115 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7118 struct nfs4_secinfo_arg args = {
7119 .dir_fh = NFS_FH(dir),
7122 struct nfs4_secinfo_res res = {
7125 struct rpc_message msg = {
7126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7130 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7131 struct rpc_cred *cred = NULL;
7133 if (use_integrity) {
7134 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7135 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7136 msg.rpc_cred = cred;
7139 dprintk("NFS call secinfo %s\n", name->name);
7141 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7142 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7144 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7146 dprintk("NFS reply secinfo: %d\n", status);
7154 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7155 struct nfs4_secinfo_flavors *flavors)
7157 struct nfs4_exception exception = { };
7160 err = -NFS4ERR_WRONGSEC;
7162 /* try to use integrity protection with machine cred */
7163 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7164 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7167 * if unable to use integrity protection, or SECINFO with
7168 * integrity protection returns NFS4ERR_WRONGSEC (which is
7169 * disallowed by spec, but exists in deployed servers) use
7170 * the current filesystem's rpc_client and the user cred.
7172 if (err == -NFS4ERR_WRONGSEC)
7173 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7175 trace_nfs4_secinfo(dir, name, err);
7176 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7178 } while (exception.retry);
7182 #ifdef CONFIG_NFS_V4_1
7184 * Check the exchange flags returned by the server for invalid flags, having
7185 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7188 static int nfs4_check_cl_exchange_flags(u32 flags)
7190 if (flags & ~EXCHGID4_FLAG_MASK_R)
7192 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7193 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7195 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7199 return -NFS4ERR_INVAL;
7203 nfs41_same_server_scope(struct nfs41_server_scope *a,
7204 struct nfs41_server_scope *b)
7206 if (a->server_scope_sz != b->server_scope_sz)
7208 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7212 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7216 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7217 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7221 * nfs4_proc_bind_one_conn_to_session()
7223 * The 4.1 client currently uses the same TCP connection for the
7224 * fore and backchannel.
7227 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7228 struct rpc_xprt *xprt,
7229 struct nfs_client *clp,
7230 struct rpc_cred *cred)
7233 struct nfs41_bind_conn_to_session_args args = {
7235 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7237 struct nfs41_bind_conn_to_session_res res;
7238 struct rpc_message msg = {
7240 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7245 struct rpc_task_setup task_setup_data = {
7248 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7249 .rpc_message = &msg,
7250 .flags = RPC_TASK_TIMEOUT,
7252 struct rpc_task *task;
7254 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7255 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7256 args.dir = NFS4_CDFC4_FORE;
7258 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7259 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7260 args.dir = NFS4_CDFC4_FORE;
7262 task = rpc_run_task(&task_setup_data);
7263 if (!IS_ERR(task)) {
7264 status = task->tk_status;
7267 status = PTR_ERR(task);
7268 trace_nfs4_bind_conn_to_session(clp, status);
7270 if (memcmp(res.sessionid.data,
7271 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7272 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7275 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7276 dprintk("NFS: %s: Unexpected direction from server\n",
7280 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7281 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7290 struct rpc_bind_conn_calldata {
7291 struct nfs_client *clp;
7292 struct rpc_cred *cred;
7296 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7297 struct rpc_xprt *xprt,
7300 struct rpc_bind_conn_calldata *p = calldata;
7302 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7305 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7307 struct rpc_bind_conn_calldata data = {
7311 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7312 nfs4_proc_bind_conn_to_session_callback, &data);
7316 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7317 * and operations we'd like to see to enable certain features in the allow map
7319 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7320 .how = SP4_MACH_CRED,
7321 .enforce.u.words = {
7322 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7323 1 << (OP_EXCHANGE_ID - 32) |
7324 1 << (OP_CREATE_SESSION - 32) |
7325 1 << (OP_DESTROY_SESSION - 32) |
7326 1 << (OP_DESTROY_CLIENTID - 32)
7329 [0] = 1 << (OP_CLOSE) |
7330 1 << (OP_OPEN_DOWNGRADE) |
7332 1 << (OP_DELEGRETURN) |
7334 [1] = 1 << (OP_SECINFO - 32) |
7335 1 << (OP_SECINFO_NO_NAME - 32) |
7336 1 << (OP_LAYOUTRETURN - 32) |
7337 1 << (OP_TEST_STATEID - 32) |
7338 1 << (OP_FREE_STATEID - 32) |
7339 1 << (OP_WRITE - 32)
7344 * Select the state protection mode for client `clp' given the server results
7345 * from exchange_id in `sp'.
7347 * Returns 0 on success, negative errno otherwise.
7349 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7350 struct nfs41_state_protection *sp)
7352 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7353 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7354 1 << (OP_EXCHANGE_ID - 32) |
7355 1 << (OP_CREATE_SESSION - 32) |
7356 1 << (OP_DESTROY_SESSION - 32) |
7357 1 << (OP_DESTROY_CLIENTID - 32)
7359 unsigned long flags = 0;
7363 if (sp->how == SP4_MACH_CRED) {
7364 /* Print state protect result */
7365 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7366 for (i = 0; i <= LAST_NFS4_OP; i++) {
7367 if (test_bit(i, sp->enforce.u.longs))
7368 dfprintk(MOUNT, " enforce op %d\n", i);
7369 if (test_bit(i, sp->allow.u.longs))
7370 dfprintk(MOUNT, " allow op %d\n", i);
7373 /* make sure nothing is on enforce list that isn't supported */
7374 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7375 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7376 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7383 * Minimal mode - state operations are allowed to use machine
7384 * credential. Note this already happens by default, so the
7385 * client doesn't have to do anything more than the negotiation.
7387 * NOTE: we don't care if EXCHANGE_ID is in the list -
7388 * we're already using the machine cred for exchange_id
7389 * and will never use a different cred.
7391 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7392 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7393 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7394 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7395 dfprintk(MOUNT, "sp4_mach_cred:\n");
7396 dfprintk(MOUNT, " minimal mode enabled\n");
7397 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7399 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7404 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7405 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7406 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7407 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7408 dfprintk(MOUNT, " cleanup mode enabled\n");
7409 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7412 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7413 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7414 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7417 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7418 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7419 dfprintk(MOUNT, " secinfo mode enabled\n");
7420 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7423 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7424 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7425 dfprintk(MOUNT, " stateid mode enabled\n");
7426 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7429 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7430 dfprintk(MOUNT, " write mode enabled\n");
7431 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7434 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7435 dfprintk(MOUNT, " commit mode enabled\n");
7436 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7440 clp->cl_sp4_flags = flags;
7444 struct nfs41_exchange_id_data {
7445 struct nfs41_exchange_id_res res;
7446 struct nfs41_exchange_id_args args;
7449 static void nfs4_exchange_id_release(void *data)
7451 struct nfs41_exchange_id_data *cdata =
7452 (struct nfs41_exchange_id_data *)data;
7454 nfs_put_client(cdata->args.client);
7455 kfree(cdata->res.impl_id);
7456 kfree(cdata->res.server_scope);
7457 kfree(cdata->res.server_owner);
7461 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7462 .rpc_release = nfs4_exchange_id_release,
7466 * _nfs4_proc_exchange_id()
7468 * Wrapper for EXCHANGE_ID operation.
7470 static struct rpc_task *
7471 nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7472 u32 sp4_how, struct rpc_xprt *xprt)
7474 struct rpc_message msg = {
7475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7478 struct rpc_task_setup task_setup_data = {
7479 .rpc_client = clp->cl_rpcclient,
7480 .callback_ops = &nfs4_exchange_id_call_ops,
7481 .rpc_message = &msg,
7482 .flags = RPC_TASK_TIMEOUT,
7484 struct nfs41_exchange_id_data *calldata;
7487 if (!atomic_inc_not_zero(&clp->cl_count))
7488 return ERR_PTR(-EIO);
7491 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7495 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7497 status = nfs4_init_uniform_client_string(clp);
7501 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7504 if (unlikely(calldata->res.server_owner == NULL))
7507 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7509 if (unlikely(calldata->res.server_scope == NULL))
7510 goto out_server_owner;
7512 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7513 if (unlikely(calldata->res.impl_id == NULL))
7514 goto out_server_scope;
7518 calldata->args.state_protect.how = SP4_NONE;
7522 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7532 task_setup_data.rpc_xprt = xprt;
7533 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7534 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7535 sizeof(calldata->args.verifier.data));
7537 calldata->args.client = clp;
7538 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7539 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7540 #ifdef CONFIG_NFS_V4_1_MIGRATION
7541 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7543 msg.rpc_argp = &calldata->args;
7544 msg.rpc_resp = &calldata->res;
7545 task_setup_data.callback_data = calldata;
7547 return rpc_run_task(&task_setup_data);
7550 kfree(calldata->res.impl_id);
7552 kfree(calldata->res.server_scope);
7554 kfree(calldata->res.server_owner);
7558 nfs_put_client(clp);
7559 return ERR_PTR(status);
7563 * _nfs4_proc_exchange_id()
7565 * Wrapper for EXCHANGE_ID operation.
7567 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7570 struct rpc_task *task;
7571 struct nfs41_exchange_id_args *argp;
7572 struct nfs41_exchange_id_res *resp;
7575 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7577 return PTR_ERR(task);
7579 argp = task->tk_msg.rpc_argp;
7580 resp = task->tk_msg.rpc_resp;
7581 status = task->tk_status;
7585 status = nfs4_check_cl_exchange_flags(resp->flags);
7589 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7593 clp->cl_clientid = resp->clientid;
7594 clp->cl_exchange_flags = resp->flags;
7595 clp->cl_seqid = resp->seqid;
7596 /* Client ID is not confirmed */
7597 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7598 clear_bit(NFS4_SESSION_ESTABLISHED,
7599 &clp->cl_session->session_state);
7601 if (clp->cl_serverscope != NULL &&
7602 !nfs41_same_server_scope(clp->cl_serverscope,
7603 resp->server_scope)) {
7604 dprintk("%s: server_scope mismatch detected\n",
7606 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7609 swap(clp->cl_serverowner, resp->server_owner);
7610 swap(clp->cl_serverscope, resp->server_scope);
7611 swap(clp->cl_implid, resp->impl_id);
7613 /* Save the EXCHANGE_ID verifier session trunk tests */
7614 memcpy(clp->cl_confirm.data, argp->verifier.data,
7615 sizeof(clp->cl_confirm.data));
7617 trace_nfs4_exchange_id(clp, status);
7623 * nfs4_proc_exchange_id()
7625 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7627 * Since the clientid has expired, all compounds using sessions
7628 * associated with the stale clientid will be returning
7629 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7630 * be in some phase of session reset.
7632 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7634 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7636 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7639 /* try SP4_MACH_CRED if krb5i/p */
7640 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7641 authflavor == RPC_AUTH_GSS_KRB5P) {
7642 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7648 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7652 * nfs4_test_session_trunk
7654 * This is an add_xprt_test() test function called from
7655 * rpc_clnt_setup_test_and_add_xprt.
7657 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7658 * and is dereferrenced in nfs4_exchange_id_release
7660 * Upon success, add the new transport to the rpc_clnt
7662 * @clnt: struct rpc_clnt to get new transport
7663 * @xprt: the rpc_xprt to test
7664 * @data: call data for _nfs4_proc_exchange_id.
7666 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7669 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7670 struct rpc_task *task;
7675 dprintk("--> %s try %s\n", __func__,
7676 xprt->address_strings[RPC_DISPLAY_ADDR]);
7678 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7680 /* Test connection for session trunking. Async exchange_id call */
7681 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7683 return PTR_ERR(task);
7685 status = task->tk_status;
7687 status = nfs4_detect_session_trunking(adata->clp,
7688 task->tk_msg.rpc_resp, xprt);
7693 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7695 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7696 struct rpc_cred *cred)
7698 struct rpc_message msg = {
7699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7705 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7706 trace_nfs4_destroy_clientid(clp, status);
7708 dprintk("NFS: Got error %d from the server %s on "
7709 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7713 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7714 struct rpc_cred *cred)
7719 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7720 ret = _nfs4_proc_destroy_clientid(clp, cred);
7722 case -NFS4ERR_DELAY:
7723 case -NFS4ERR_CLIENTID_BUSY:
7733 int nfs4_destroy_clientid(struct nfs_client *clp)
7735 struct rpc_cred *cred;
7738 if (clp->cl_mvops->minor_version < 1)
7740 if (clp->cl_exchange_flags == 0)
7742 if (clp->cl_preserve_clid)
7744 cred = nfs4_get_clid_cred(clp);
7745 ret = nfs4_proc_destroy_clientid(clp, cred);
7750 case -NFS4ERR_STALE_CLIENTID:
7751 clp->cl_exchange_flags = 0;
7757 struct nfs4_get_lease_time_data {
7758 struct nfs4_get_lease_time_args *args;
7759 struct nfs4_get_lease_time_res *res;
7760 struct nfs_client *clp;
7763 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7766 struct nfs4_get_lease_time_data *data =
7767 (struct nfs4_get_lease_time_data *)calldata;
7769 dprintk("--> %s\n", __func__);
7770 /* just setup sequence, do not trigger session recovery
7771 since we're invoked within one */
7772 nfs4_setup_sequence(data->clp,
7773 &data->args->la_seq_args,
7774 &data->res->lr_seq_res,
7776 dprintk("<-- %s\n", __func__);
7780 * Called from nfs4_state_manager thread for session setup, so don't recover
7781 * from sequence operation or clientid errors.
7783 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7785 struct nfs4_get_lease_time_data *data =
7786 (struct nfs4_get_lease_time_data *)calldata;
7788 dprintk("--> %s\n", __func__);
7789 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7791 switch (task->tk_status) {
7792 case -NFS4ERR_DELAY:
7793 case -NFS4ERR_GRACE:
7794 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7795 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7796 task->tk_status = 0;
7798 case -NFS4ERR_RETRY_UNCACHED_REP:
7799 rpc_restart_call_prepare(task);
7802 dprintk("<-- %s\n", __func__);
7805 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7806 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7807 .rpc_call_done = nfs4_get_lease_time_done,
7810 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7812 struct rpc_task *task;
7813 struct nfs4_get_lease_time_args args;
7814 struct nfs4_get_lease_time_res res = {
7815 .lr_fsinfo = fsinfo,
7817 struct nfs4_get_lease_time_data data = {
7822 struct rpc_message msg = {
7823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7827 struct rpc_task_setup task_setup = {
7828 .rpc_client = clp->cl_rpcclient,
7829 .rpc_message = &msg,
7830 .callback_ops = &nfs4_get_lease_time_ops,
7831 .callback_data = &data,
7832 .flags = RPC_TASK_TIMEOUT,
7836 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7837 nfs4_set_sequence_privileged(&args.la_seq_args);
7838 task = rpc_run_task(&task_setup);
7841 return PTR_ERR(task);
7843 status = task->tk_status;
7849 * Initialize the values to be used by the client in CREATE_SESSION
7850 * If nfs4_init_session set the fore channel request and response sizes,
7853 * Set the back channel max_resp_sz_cached to zero to force the client to
7854 * always set csa_cachethis to FALSE because the current implementation
7855 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7857 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7858 struct rpc_clnt *clnt)
7860 unsigned int max_rqst_sz, max_resp_sz;
7861 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7863 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7864 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7866 /* Fore channel attributes */
7867 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7868 args->fc_attrs.max_resp_sz = max_resp_sz;
7869 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7870 args->fc_attrs.max_reqs = max_session_slots;
7872 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7873 "max_ops=%u max_reqs=%u\n",
7875 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7876 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7878 /* Back channel attributes */
7879 args->bc_attrs.max_rqst_sz = max_bc_payload;
7880 args->bc_attrs.max_resp_sz = max_bc_payload;
7881 args->bc_attrs.max_resp_sz_cached = 0;
7882 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7883 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7885 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7886 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7888 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7889 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7890 args->bc_attrs.max_reqs);
7893 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7894 struct nfs41_create_session_res *res)
7896 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7897 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7899 if (rcvd->max_resp_sz > sent->max_resp_sz)
7902 * Our requested max_ops is the minimum we need; we're not
7903 * prepared to break up compounds into smaller pieces than that.
7904 * So, no point even trying to continue if the server won't
7907 if (rcvd->max_ops < sent->max_ops)
7909 if (rcvd->max_reqs == 0)
7911 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7912 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7916 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7917 struct nfs41_create_session_res *res)
7919 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7920 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7922 if (!(res->flags & SESSION4_BACK_CHAN))
7924 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7926 if (rcvd->max_resp_sz < sent->max_resp_sz)
7928 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7930 if (rcvd->max_ops > sent->max_ops)
7932 if (rcvd->max_reqs > sent->max_reqs)
7938 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7939 struct nfs41_create_session_res *res)
7943 ret = nfs4_verify_fore_channel_attrs(args, res);
7946 return nfs4_verify_back_channel_attrs(args, res);
7949 static void nfs4_update_session(struct nfs4_session *session,
7950 struct nfs41_create_session_res *res)
7952 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7953 /* Mark client id and session as being confirmed */
7954 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7955 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7956 session->flags = res->flags;
7957 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7958 if (res->flags & SESSION4_BACK_CHAN)
7959 memcpy(&session->bc_attrs, &res->bc_attrs,
7960 sizeof(session->bc_attrs));
7963 static int _nfs4_proc_create_session(struct nfs_client *clp,
7964 struct rpc_cred *cred)
7966 struct nfs4_session *session = clp->cl_session;
7967 struct nfs41_create_session_args args = {
7969 .clientid = clp->cl_clientid,
7970 .seqid = clp->cl_seqid,
7971 .cb_program = NFS4_CALLBACK,
7973 struct nfs41_create_session_res res;
7975 struct rpc_message msg = {
7976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7983 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
7984 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7986 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7987 trace_nfs4_create_session(clp, status);
7990 case -NFS4ERR_STALE_CLIENTID:
7991 case -NFS4ERR_DELAY:
8000 /* Verify the session's negotiated channel_attrs values */
8001 status = nfs4_verify_channel_attrs(&args, &res);
8002 /* Increment the clientid slot sequence id */
8005 nfs4_update_session(session, &res);
8012 * Issues a CREATE_SESSION operation to the server.
8013 * It is the responsibility of the caller to verify the session is
8014 * expired before calling this routine.
8016 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8020 struct nfs4_session *session = clp->cl_session;
8022 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8024 status = _nfs4_proc_create_session(clp, cred);
8028 /* Init or reset the session slot tables */
8029 status = nfs4_setup_session_slot_tables(session);
8030 dprintk("slot table setup returned %d\n", status);
8034 ptr = (unsigned *)&session->sess_id.data[0];
8035 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8036 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8038 dprintk("<-- %s\n", __func__);
8043 * Issue the over-the-wire RPC DESTROY_SESSION.
8044 * The caller must serialize access to this routine.
8046 int nfs4_proc_destroy_session(struct nfs4_session *session,
8047 struct rpc_cred *cred)
8049 struct rpc_message msg = {
8050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8051 .rpc_argp = session,
8056 dprintk("--> nfs4_proc_destroy_session\n");
8058 /* session is still being setup */
8059 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8062 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8063 trace_nfs4_destroy_session(session->clp, status);
8066 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8067 "Session has been destroyed regardless...\n", status);
8069 dprintk("<-- nfs4_proc_destroy_session\n");
8074 * Renew the cl_session lease.
8076 struct nfs4_sequence_data {
8077 struct nfs_client *clp;
8078 struct nfs4_sequence_args args;
8079 struct nfs4_sequence_res res;
8082 static void nfs41_sequence_release(void *data)
8084 struct nfs4_sequence_data *calldata = data;
8085 struct nfs_client *clp = calldata->clp;
8087 if (atomic_read(&clp->cl_count) > 1)
8088 nfs4_schedule_state_renewal(clp);
8089 nfs_put_client(clp);
8093 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8095 switch(task->tk_status) {
8096 case -NFS4ERR_DELAY:
8097 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8100 nfs4_schedule_lease_recovery(clp);
8105 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8107 struct nfs4_sequence_data *calldata = data;
8108 struct nfs_client *clp = calldata->clp;
8110 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8113 trace_nfs4_sequence(clp, task->tk_status);
8114 if (task->tk_status < 0) {
8115 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8116 if (atomic_read(&clp->cl_count) == 1)
8119 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8120 rpc_restart_call_prepare(task);
8124 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8126 dprintk("<-- %s\n", __func__);
8129 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8131 struct nfs4_sequence_data *calldata = data;
8132 struct nfs_client *clp = calldata->clp;
8133 struct nfs4_sequence_args *args;
8134 struct nfs4_sequence_res *res;
8136 args = task->tk_msg.rpc_argp;
8137 res = task->tk_msg.rpc_resp;
8139 nfs4_setup_sequence(clp, args, res, task);
8142 static const struct rpc_call_ops nfs41_sequence_ops = {
8143 .rpc_call_done = nfs41_sequence_call_done,
8144 .rpc_call_prepare = nfs41_sequence_prepare,
8145 .rpc_release = nfs41_sequence_release,
8148 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8149 struct rpc_cred *cred,
8152 struct nfs4_sequence_data *calldata;
8153 struct rpc_message msg = {
8154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8157 struct rpc_task_setup task_setup_data = {
8158 .rpc_client = clp->cl_rpcclient,
8159 .rpc_message = &msg,
8160 .callback_ops = &nfs41_sequence_ops,
8161 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8164 if (!atomic_inc_not_zero(&clp->cl_count))
8165 return ERR_PTR(-EIO);
8166 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8167 if (calldata == NULL) {
8168 nfs_put_client(clp);
8169 return ERR_PTR(-ENOMEM);
8171 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8173 nfs4_set_sequence_privileged(&calldata->args);
8174 msg.rpc_argp = &calldata->args;
8175 msg.rpc_resp = &calldata->res;
8176 calldata->clp = clp;
8177 task_setup_data.callback_data = calldata;
8179 return rpc_run_task(&task_setup_data);
8182 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8184 struct rpc_task *task;
8187 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8189 task = _nfs41_proc_sequence(clp, cred, false);
8191 ret = PTR_ERR(task);
8193 rpc_put_task_async(task);
8194 dprintk("<-- %s status=%d\n", __func__, ret);
8198 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8200 struct rpc_task *task;
8203 task = _nfs41_proc_sequence(clp, cred, true);
8205 ret = PTR_ERR(task);
8208 ret = rpc_wait_for_completion_task(task);
8210 ret = task->tk_status;
8213 dprintk("<-- %s status=%d\n", __func__, ret);
8217 struct nfs4_reclaim_complete_data {
8218 struct nfs_client *clp;
8219 struct nfs41_reclaim_complete_args arg;
8220 struct nfs41_reclaim_complete_res res;
8223 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8225 struct nfs4_reclaim_complete_data *calldata = data;
8227 nfs4_setup_sequence(calldata->clp,
8228 &calldata->arg.seq_args,
8229 &calldata->res.seq_res,
8233 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8235 switch(task->tk_status) {
8237 case -NFS4ERR_COMPLETE_ALREADY:
8238 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8240 case -NFS4ERR_DELAY:
8241 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8243 case -NFS4ERR_RETRY_UNCACHED_REP:
8245 case -NFS4ERR_BADSESSION:
8246 case -NFS4ERR_DEADSESSION:
8247 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8248 nfs4_schedule_session_recovery(clp->cl_session,
8252 nfs4_schedule_lease_recovery(clp);
8257 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8259 struct nfs4_reclaim_complete_data *calldata = data;
8260 struct nfs_client *clp = calldata->clp;
8261 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8263 dprintk("--> %s\n", __func__);
8264 if (!nfs41_sequence_done(task, res))
8267 trace_nfs4_reclaim_complete(clp, task->tk_status);
8268 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8269 rpc_restart_call_prepare(task);
8272 dprintk("<-- %s\n", __func__);
8275 static void nfs4_free_reclaim_complete_data(void *data)
8277 struct nfs4_reclaim_complete_data *calldata = data;
8282 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8283 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8284 .rpc_call_done = nfs4_reclaim_complete_done,
8285 .rpc_release = nfs4_free_reclaim_complete_data,
8289 * Issue a global reclaim complete.
8291 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8292 struct rpc_cred *cred)
8294 struct nfs4_reclaim_complete_data *calldata;
8295 struct rpc_task *task;
8296 struct rpc_message msg = {
8297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8300 struct rpc_task_setup task_setup_data = {
8301 .rpc_client = clp->cl_rpcclient,
8302 .rpc_message = &msg,
8303 .callback_ops = &nfs4_reclaim_complete_call_ops,
8304 .flags = RPC_TASK_ASYNC,
8306 int status = -ENOMEM;
8308 dprintk("--> %s\n", __func__);
8309 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8310 if (calldata == NULL)
8312 calldata->clp = clp;
8313 calldata->arg.one_fs = 0;
8315 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8316 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8317 msg.rpc_argp = &calldata->arg;
8318 msg.rpc_resp = &calldata->res;
8319 task_setup_data.callback_data = calldata;
8320 task = rpc_run_task(&task_setup_data);
8322 status = PTR_ERR(task);
8325 status = rpc_wait_for_completion_task(task);
8327 status = task->tk_status;
8330 dprintk("<-- %s status=%d\n", __func__, status);
8335 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8337 struct nfs4_layoutget *lgp = calldata;
8338 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8340 dprintk("--> %s\n", __func__);
8341 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8342 &lgp->res.seq_res, task);
8343 dprintk("<-- %s\n", __func__);
8346 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8348 struct nfs4_layoutget *lgp = calldata;
8350 dprintk("--> %s\n", __func__);
8351 nfs41_sequence_process(task, &lgp->res.seq_res);
8352 dprintk("<-- %s\n", __func__);
8356 nfs4_layoutget_handle_exception(struct rpc_task *task,
8357 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8359 struct inode *inode = lgp->args.inode;
8360 struct nfs_server *server = NFS_SERVER(inode);
8361 struct pnfs_layout_hdr *lo;
8362 int nfs4err = task->tk_status;
8363 int err, status = 0;
8366 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8373 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8374 * on the file. set tk_status to -ENODATA to tell upper layer to
8377 case -NFS4ERR_LAYOUTUNAVAILABLE:
8381 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8382 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8384 case -NFS4ERR_BADLAYOUT:
8385 status = -EOVERFLOW;
8388 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8389 * (or clients) writing to the same RAID stripe except when
8390 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8392 * Treat it like we would RECALLCONFLICT -- we retry for a little
8393 * while, and then eventually give up.
8395 case -NFS4ERR_LAYOUTTRYLATER:
8396 if (lgp->args.minlength == 0) {
8397 status = -EOVERFLOW;
8402 case -NFS4ERR_RECALLCONFLICT:
8403 status = -ERECALLCONFLICT;
8405 case -NFS4ERR_DELEG_REVOKED:
8406 case -NFS4ERR_ADMIN_REVOKED:
8407 case -NFS4ERR_EXPIRED:
8408 case -NFS4ERR_BAD_STATEID:
8409 exception->timeout = 0;
8410 spin_lock(&inode->i_lock);
8411 lo = NFS_I(inode)->layout;
8412 /* If the open stateid was bad, then recover it. */
8413 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8414 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8415 spin_unlock(&inode->i_lock);
8416 exception->state = lgp->args.ctx->state;
8417 exception->stateid = &lgp->args.stateid;
8422 * Mark the bad layout state as invalid, then retry
8424 pnfs_mark_layout_stateid_invalid(lo, &head);
8425 spin_unlock(&inode->i_lock);
8426 nfs_commit_inode(inode, 0);
8427 pnfs_free_lseg_list(&head);
8432 nfs4_sequence_free_slot(&lgp->res.seq_res);
8433 err = nfs4_handle_exception(server, nfs4err, exception);
8435 if (exception->retry)
8441 dprintk("<-- %s\n", __func__);
8445 static size_t max_response_pages(struct nfs_server *server)
8447 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8448 return nfs_page_array_len(0, max_resp_sz);
8451 static void nfs4_free_pages(struct page **pages, size_t size)
8458 for (i = 0; i < size; i++) {
8461 __free_page(pages[i]);
8466 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8468 struct page **pages;
8471 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8473 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8477 for (i = 0; i < size; i++) {
8478 pages[i] = alloc_page(gfp_flags);
8480 dprintk("%s: failed to allocate page\n", __func__);
8481 nfs4_free_pages(pages, size);
8489 static void nfs4_layoutget_release(void *calldata)
8491 struct nfs4_layoutget *lgp = calldata;
8492 struct inode *inode = lgp->args.inode;
8493 struct nfs_server *server = NFS_SERVER(inode);
8494 size_t max_pages = max_response_pages(server);
8496 dprintk("--> %s\n", __func__);
8497 nfs4_sequence_free_slot(&lgp->res.seq_res);
8498 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8499 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8500 put_nfs_open_context(lgp->args.ctx);
8502 dprintk("<-- %s\n", __func__);
8505 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8506 .rpc_call_prepare = nfs4_layoutget_prepare,
8507 .rpc_call_done = nfs4_layoutget_done,
8508 .rpc_release = nfs4_layoutget_release,
8511 struct pnfs_layout_segment *
8512 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8514 struct inode *inode = lgp->args.inode;
8515 struct nfs_server *server = NFS_SERVER(inode);
8516 size_t max_pages = max_response_pages(server);
8517 struct rpc_task *task;
8518 struct rpc_message msg = {
8519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8520 .rpc_argp = &lgp->args,
8521 .rpc_resp = &lgp->res,
8522 .rpc_cred = lgp->cred,
8524 struct rpc_task_setup task_setup_data = {
8525 .rpc_client = server->client,
8526 .rpc_message = &msg,
8527 .callback_ops = &nfs4_layoutget_call_ops,
8528 .callback_data = lgp,
8529 .flags = RPC_TASK_ASYNC,
8531 struct pnfs_layout_segment *lseg = NULL;
8532 struct nfs4_exception exception = {
8534 .timeout = *timeout,
8538 dprintk("--> %s\n", __func__);
8540 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8541 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8543 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8544 if (!lgp->args.layout.pages) {
8545 nfs4_layoutget_release(lgp);
8546 return ERR_PTR(-ENOMEM);
8548 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8550 lgp->res.layoutp = &lgp->args.layout;
8551 lgp->res.seq_res.sr_slot = NULL;
8552 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8554 task = rpc_run_task(&task_setup_data);
8556 return ERR_CAST(task);
8557 status = rpc_wait_for_completion_task(task);
8559 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8560 *timeout = exception.timeout;
8563 trace_nfs4_layoutget(lgp->args.ctx,
8569 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8570 if (status == 0 && lgp->res.layoutp->len)
8571 lseg = pnfs_layout_process(lgp);
8573 dprintk("<-- %s status=%d\n", __func__, status);
8575 return ERR_PTR(status);
8580 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8582 struct nfs4_layoutreturn *lrp = calldata;
8584 dprintk("--> %s\n", __func__);
8585 nfs4_setup_sequence(lrp->clp,
8586 &lrp->args.seq_args,
8591 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8593 struct nfs4_layoutreturn *lrp = calldata;
8594 struct nfs_server *server;
8596 dprintk("--> %s\n", __func__);
8598 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8601 server = NFS_SERVER(lrp->args.inode);
8602 switch (task->tk_status) {
8604 task->tk_status = 0;
8607 case -NFS4ERR_DELAY:
8608 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8610 nfs4_sequence_free_slot(&lrp->res.seq_res);
8611 rpc_restart_call_prepare(task);
8614 dprintk("<-- %s\n", __func__);
8617 static void nfs4_layoutreturn_release(void *calldata)
8619 struct nfs4_layoutreturn *lrp = calldata;
8620 struct pnfs_layout_hdr *lo = lrp->args.layout;
8622 dprintk("--> %s\n", __func__);
8623 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8624 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8625 nfs4_sequence_free_slot(&lrp->res.seq_res);
8626 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8627 lrp->ld_private.ops->free(&lrp->ld_private);
8628 pnfs_put_layout_hdr(lrp->args.layout);
8629 nfs_iput_and_deactive(lrp->inode);
8631 dprintk("<-- %s\n", __func__);
8634 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8635 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8636 .rpc_call_done = nfs4_layoutreturn_done,
8637 .rpc_release = nfs4_layoutreturn_release,
8640 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8642 struct rpc_task *task;
8643 struct rpc_message msg = {
8644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8645 .rpc_argp = &lrp->args,
8646 .rpc_resp = &lrp->res,
8647 .rpc_cred = lrp->cred,
8649 struct rpc_task_setup task_setup_data = {
8650 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8651 .rpc_message = &msg,
8652 .callback_ops = &nfs4_layoutreturn_call_ops,
8653 .callback_data = lrp,
8657 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8658 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8659 &task_setup_data.rpc_client, &msg);
8661 dprintk("--> %s\n", __func__);
8663 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8665 nfs4_layoutreturn_release(lrp);
8668 task_setup_data.flags |= RPC_TASK_ASYNC;
8670 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8671 task = rpc_run_task(&task_setup_data);
8673 return PTR_ERR(task);
8675 status = task->tk_status;
8676 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8677 dprintk("<-- %s status=%d\n", __func__, status);
8683 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8684 struct pnfs_device *pdev,
8685 struct rpc_cred *cred)
8687 struct nfs4_getdeviceinfo_args args = {
8689 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8690 NOTIFY_DEVICEID4_DELETE,
8692 struct nfs4_getdeviceinfo_res res = {
8695 struct rpc_message msg = {
8696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8703 dprintk("--> %s\n", __func__);
8704 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8705 if (res.notification & ~args.notify_types)
8706 dprintk("%s: unsupported notification\n", __func__);
8707 if (res.notification != args.notify_types)
8710 dprintk("<-- %s status=%d\n", __func__, status);
8715 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8716 struct pnfs_device *pdev,
8717 struct rpc_cred *cred)
8719 struct nfs4_exception exception = { };
8723 err = nfs4_handle_exception(server,
8724 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8726 } while (exception.retry);
8729 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8731 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8733 struct nfs4_layoutcommit_data *data = calldata;
8734 struct nfs_server *server = NFS_SERVER(data->args.inode);
8736 nfs4_setup_sequence(server->nfs_client,
8737 &data->args.seq_args,
8743 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8745 struct nfs4_layoutcommit_data *data = calldata;
8746 struct nfs_server *server = NFS_SERVER(data->args.inode);
8748 if (!nfs41_sequence_done(task, &data->res.seq_res))
8751 switch (task->tk_status) { /* Just ignore these failures */
8752 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8753 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8754 case -NFS4ERR_BADLAYOUT: /* no layout */
8755 case -NFS4ERR_GRACE: /* loca_recalim always false */
8756 task->tk_status = 0;
8760 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8761 rpc_restart_call_prepare(task);
8767 static void nfs4_layoutcommit_release(void *calldata)
8769 struct nfs4_layoutcommit_data *data = calldata;
8771 pnfs_cleanup_layoutcommit(data);
8772 nfs_post_op_update_inode_force_wcc(data->args.inode,
8774 put_rpccred(data->cred);
8775 nfs_iput_and_deactive(data->inode);
8779 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8780 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8781 .rpc_call_done = nfs4_layoutcommit_done,
8782 .rpc_release = nfs4_layoutcommit_release,
8786 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8788 struct rpc_message msg = {
8789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8790 .rpc_argp = &data->args,
8791 .rpc_resp = &data->res,
8792 .rpc_cred = data->cred,
8794 struct rpc_task_setup task_setup_data = {
8795 .task = &data->task,
8796 .rpc_client = NFS_CLIENT(data->args.inode),
8797 .rpc_message = &msg,
8798 .callback_ops = &nfs4_layoutcommit_ops,
8799 .callback_data = data,
8801 struct rpc_task *task;
8804 dprintk("NFS: initiating layoutcommit call. sync %d "
8805 "lbw: %llu inode %lu\n", sync,
8806 data->args.lastbytewritten,
8807 data->args.inode->i_ino);
8810 data->inode = nfs_igrab_and_active(data->args.inode);
8811 if (data->inode == NULL) {
8812 nfs4_layoutcommit_release(data);
8815 task_setup_data.flags = RPC_TASK_ASYNC;
8817 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8818 task = rpc_run_task(&task_setup_data);
8820 return PTR_ERR(task);
8822 status = task->tk_status;
8823 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8824 dprintk("%s: status %d\n", __func__, status);
8830 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8831 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8834 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8835 struct nfs_fsinfo *info,
8836 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8838 struct nfs41_secinfo_no_name_args args = {
8839 .style = SECINFO_STYLE_CURRENT_FH,
8841 struct nfs4_secinfo_res res = {
8844 struct rpc_message msg = {
8845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8849 struct rpc_clnt *clnt = server->client;
8850 struct rpc_cred *cred = NULL;
8853 if (use_integrity) {
8854 clnt = server->nfs_client->cl_rpcclient;
8855 cred = nfs4_get_clid_cred(server->nfs_client);
8856 msg.rpc_cred = cred;
8859 dprintk("--> %s\n", __func__);
8860 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8862 dprintk("<-- %s status=%d\n", __func__, status);
8871 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8872 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8874 struct nfs4_exception exception = { };
8877 /* first try using integrity protection */
8878 err = -NFS4ERR_WRONGSEC;
8880 /* try to use integrity protection with machine cred */
8881 if (_nfs4_is_integrity_protected(server->nfs_client))
8882 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8886 * if unable to use integrity protection, or SECINFO with
8887 * integrity protection returns NFS4ERR_WRONGSEC (which is
8888 * disallowed by spec, but exists in deployed servers) use
8889 * the current filesystem's rpc_client and the user cred.
8891 if (err == -NFS4ERR_WRONGSEC)
8892 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8897 case -NFS4ERR_WRONGSEC:
8901 err = nfs4_handle_exception(server, err, &exception);
8903 } while (exception.retry);
8909 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8910 struct nfs_fsinfo *info)
8914 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8915 struct nfs4_secinfo_flavors *flavors;
8916 struct nfs4_secinfo4 *secinfo;
8919 page = alloc_page(GFP_KERNEL);
8925 flavors = page_address(page);
8926 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8929 * Fall back on "guess and check" method if
8930 * the server doesn't support SECINFO_NO_NAME
8932 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8933 err = nfs4_find_root_sec(server, fhandle, info);
8939 for (i = 0; i < flavors->num_flavors; i++) {
8940 secinfo = &flavors->flavors[i];
8942 switch (secinfo->flavor) {
8946 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8947 &secinfo->flavor_info);
8950 flavor = RPC_AUTH_MAXFLAVOR;
8954 if (!nfs_auth_info_match(&server->auth_info, flavor))
8955 flavor = RPC_AUTH_MAXFLAVOR;
8957 if (flavor != RPC_AUTH_MAXFLAVOR) {
8958 err = nfs4_lookup_root_sec(server, fhandle,
8965 if (flavor == RPC_AUTH_MAXFLAVOR)
8976 static int _nfs41_test_stateid(struct nfs_server *server,
8977 nfs4_stateid *stateid,
8978 struct rpc_cred *cred)
8981 struct nfs41_test_stateid_args args = {
8984 struct nfs41_test_stateid_res res;
8985 struct rpc_message msg = {
8986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8991 struct rpc_clnt *rpc_client = server->client;
8993 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8996 dprintk("NFS call test_stateid %p\n", stateid);
8997 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8998 nfs4_set_sequence_privileged(&args.seq_args);
8999 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9000 &args.seq_args, &res.seq_res);
9001 if (status != NFS_OK) {
9002 dprintk("NFS reply test_stateid: failed, %d\n", status);
9005 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9009 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9010 int err, struct nfs4_exception *exception)
9012 exception->retry = 0;
9014 case -NFS4ERR_DELAY:
9015 case -NFS4ERR_RETRY_UNCACHED_REP:
9016 nfs4_handle_exception(server, err, exception);
9018 case -NFS4ERR_BADSESSION:
9019 case -NFS4ERR_BADSLOT:
9020 case -NFS4ERR_BAD_HIGH_SLOT:
9021 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9022 case -NFS4ERR_DEADSESSION:
9023 nfs4_do_handle_exception(server, err, exception);
9028 * nfs41_test_stateid - perform a TEST_STATEID operation
9030 * @server: server / transport on which to perform the operation
9031 * @stateid: state ID to test
9034 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9035 * Otherwise a negative NFS4ERR value is returned if the operation
9036 * failed or the state ID is not currently valid.
9038 static int nfs41_test_stateid(struct nfs_server *server,
9039 nfs4_stateid *stateid,
9040 struct rpc_cred *cred)
9042 struct nfs4_exception exception = { };
9045 err = _nfs41_test_stateid(server, stateid, cred);
9046 nfs4_handle_delay_or_session_error(server, err, &exception);
9047 } while (exception.retry);
9051 struct nfs_free_stateid_data {
9052 struct nfs_server *server;
9053 struct nfs41_free_stateid_args args;
9054 struct nfs41_free_stateid_res res;
9057 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9059 struct nfs_free_stateid_data *data = calldata;
9060 nfs4_setup_sequence(data->server->nfs_client,
9061 &data->args.seq_args,
9066 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9068 struct nfs_free_stateid_data *data = calldata;
9070 nfs41_sequence_done(task, &data->res.seq_res);
9072 switch (task->tk_status) {
9073 case -NFS4ERR_DELAY:
9074 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9075 rpc_restart_call_prepare(task);
9079 static void nfs41_free_stateid_release(void *calldata)
9084 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9085 .rpc_call_prepare = nfs41_free_stateid_prepare,
9086 .rpc_call_done = nfs41_free_stateid_done,
9087 .rpc_release = nfs41_free_stateid_release,
9090 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9091 const nfs4_stateid *stateid,
9092 struct rpc_cred *cred,
9095 struct rpc_message msg = {
9096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9099 struct rpc_task_setup task_setup = {
9100 .rpc_client = server->client,
9101 .rpc_message = &msg,
9102 .callback_ops = &nfs41_free_stateid_ops,
9103 .flags = RPC_TASK_ASYNC,
9105 struct nfs_free_stateid_data *data;
9107 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9108 &task_setup.rpc_client, &msg);
9110 dprintk("NFS call free_stateid %p\n", stateid);
9111 data = kmalloc(sizeof(*data), GFP_NOFS);
9113 return ERR_PTR(-ENOMEM);
9114 data->server = server;
9115 nfs4_stateid_copy(&data->args.stateid, stateid);
9117 task_setup.callback_data = data;
9119 msg.rpc_argp = &data->args;
9120 msg.rpc_resp = &data->res;
9121 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9123 nfs4_set_sequence_privileged(&data->args.seq_args);
9125 return rpc_run_task(&task_setup);
9129 * nfs41_free_stateid - perform a FREE_STATEID operation
9131 * @server: server / transport on which to perform the operation
9132 * @stateid: state ID to release
9134 * @is_recovery: set to true if this call needs to be privileged
9136 * Note: this function is always asynchronous.
9138 static int nfs41_free_stateid(struct nfs_server *server,
9139 const nfs4_stateid *stateid,
9140 struct rpc_cred *cred,
9143 struct rpc_task *task;
9145 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9147 return PTR_ERR(task);
9153 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9155 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9157 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9158 nfs4_free_lock_state(server, lsp);
9161 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9162 const nfs4_stateid *s2)
9164 if (s1->type != s2->type)
9167 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9170 if (s1->seqid == s2->seqid)
9173 return s1->seqid == 0 || s2->seqid == 0;
9176 #endif /* CONFIG_NFS_V4_1 */
9178 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9179 const nfs4_stateid *s2)
9181 return nfs4_stateid_match(s1, s2);
9185 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9186 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9187 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9188 .recover_open = nfs4_open_reclaim,
9189 .recover_lock = nfs4_lock_reclaim,
9190 .establish_clid = nfs4_init_clientid,
9191 .detect_trunking = nfs40_discover_server_trunking,
9194 #if defined(CONFIG_NFS_V4_1)
9195 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9196 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9197 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9198 .recover_open = nfs4_open_reclaim,
9199 .recover_lock = nfs4_lock_reclaim,
9200 .establish_clid = nfs41_init_clientid,
9201 .reclaim_complete = nfs41_proc_reclaim_complete,
9202 .detect_trunking = nfs41_discover_server_trunking,
9204 #endif /* CONFIG_NFS_V4_1 */
9206 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9207 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9208 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9209 .recover_open = nfs40_open_expired,
9210 .recover_lock = nfs4_lock_expired,
9211 .establish_clid = nfs4_init_clientid,
9214 #if defined(CONFIG_NFS_V4_1)
9215 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9216 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9217 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9218 .recover_open = nfs41_open_expired,
9219 .recover_lock = nfs41_lock_expired,
9220 .establish_clid = nfs41_init_clientid,
9222 #endif /* CONFIG_NFS_V4_1 */
9224 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9225 .sched_state_renewal = nfs4_proc_async_renew,
9226 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9227 .renew_lease = nfs4_proc_renew,
9230 #if defined(CONFIG_NFS_V4_1)
9231 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9232 .sched_state_renewal = nfs41_proc_async_sequence,
9233 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9234 .renew_lease = nfs4_proc_sequence,
9238 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9239 .get_locations = _nfs40_proc_get_locations,
9240 .fsid_present = _nfs40_proc_fsid_present,
9243 #if defined(CONFIG_NFS_V4_1)
9244 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9245 .get_locations = _nfs41_proc_get_locations,
9246 .fsid_present = _nfs41_proc_fsid_present,
9248 #endif /* CONFIG_NFS_V4_1 */
9250 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9252 .init_caps = NFS_CAP_READDIRPLUS
9253 | NFS_CAP_ATOMIC_OPEN
9254 | NFS_CAP_POSIX_LOCK,
9255 .init_client = nfs40_init_client,
9256 .shutdown_client = nfs40_shutdown_client,
9257 .match_stateid = nfs4_match_stateid,
9258 .find_root_sec = nfs4_find_root_sec,
9259 .free_lock_state = nfs4_release_lockowner,
9260 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9261 .alloc_seqid = nfs_alloc_seqid,
9262 .call_sync_ops = &nfs40_call_sync_ops,
9263 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9264 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9265 .state_renewal_ops = &nfs40_state_renewal_ops,
9266 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9269 #if defined(CONFIG_NFS_V4_1)
9270 static struct nfs_seqid *
9271 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9276 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9278 .init_caps = NFS_CAP_READDIRPLUS
9279 | NFS_CAP_ATOMIC_OPEN
9280 | NFS_CAP_POSIX_LOCK
9281 | NFS_CAP_STATEID_NFSV41
9282 | NFS_CAP_ATOMIC_OPEN_V1,
9283 .init_client = nfs41_init_client,
9284 .shutdown_client = nfs41_shutdown_client,
9285 .match_stateid = nfs41_match_stateid,
9286 .find_root_sec = nfs41_find_root_sec,
9287 .free_lock_state = nfs41_free_lock_state,
9288 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9289 .alloc_seqid = nfs_alloc_no_seqid,
9290 .session_trunk = nfs4_test_session_trunk,
9291 .call_sync_ops = &nfs41_call_sync_ops,
9292 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9293 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9294 .state_renewal_ops = &nfs41_state_renewal_ops,
9295 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9299 #if defined(CONFIG_NFS_V4_2)
9300 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9302 .init_caps = NFS_CAP_READDIRPLUS
9303 | NFS_CAP_ATOMIC_OPEN
9304 | NFS_CAP_POSIX_LOCK
9305 | NFS_CAP_STATEID_NFSV41
9306 | NFS_CAP_ATOMIC_OPEN_V1
9309 | NFS_CAP_DEALLOCATE
9311 | NFS_CAP_LAYOUTSTATS
9313 .init_client = nfs41_init_client,
9314 .shutdown_client = nfs41_shutdown_client,
9315 .match_stateid = nfs41_match_stateid,
9316 .find_root_sec = nfs41_find_root_sec,
9317 .free_lock_state = nfs41_free_lock_state,
9318 .call_sync_ops = &nfs41_call_sync_ops,
9319 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9320 .alloc_seqid = nfs_alloc_no_seqid,
9321 .session_trunk = nfs4_test_session_trunk,
9322 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9323 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9324 .state_renewal_ops = &nfs41_state_renewal_ops,
9325 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9329 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9330 [0] = &nfs_v4_0_minor_ops,
9331 #if defined(CONFIG_NFS_V4_1)
9332 [1] = &nfs_v4_1_minor_ops,
9334 #if defined(CONFIG_NFS_V4_2)
9335 [2] = &nfs_v4_2_minor_ops,
9339 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9341 ssize_t error, error2;
9343 error = generic_listxattr(dentry, list, size);
9351 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9354 return error + error2;
9357 static const struct inode_operations nfs4_dir_inode_operations = {
9358 .create = nfs_create,
9359 .lookup = nfs_lookup,
9360 .atomic_open = nfs_atomic_open,
9362 .unlink = nfs_unlink,
9363 .symlink = nfs_symlink,
9367 .rename = nfs_rename,
9368 .permission = nfs_permission,
9369 .getattr = nfs_getattr,
9370 .setattr = nfs_setattr,
9371 .listxattr = nfs4_listxattr,
9374 static const struct inode_operations nfs4_file_inode_operations = {
9375 .permission = nfs_permission,
9376 .getattr = nfs_getattr,
9377 .setattr = nfs_setattr,
9378 .listxattr = nfs4_listxattr,
9381 const struct nfs_rpc_ops nfs_v4_clientops = {
9382 .version = 4, /* protocol version */
9383 .dentry_ops = &nfs4_dentry_operations,
9384 .dir_inode_ops = &nfs4_dir_inode_operations,
9385 .file_inode_ops = &nfs4_file_inode_operations,
9386 .file_ops = &nfs4_file_operations,
9387 .getroot = nfs4_proc_get_root,
9388 .submount = nfs4_submount,
9389 .try_mount = nfs4_try_mount,
9390 .getattr = nfs4_proc_getattr,
9391 .setattr = nfs4_proc_setattr,
9392 .lookup = nfs4_proc_lookup,
9393 .lookupp = nfs4_proc_lookupp,
9394 .access = nfs4_proc_access,
9395 .readlink = nfs4_proc_readlink,
9396 .create = nfs4_proc_create,
9397 .remove = nfs4_proc_remove,
9398 .unlink_setup = nfs4_proc_unlink_setup,
9399 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9400 .unlink_done = nfs4_proc_unlink_done,
9401 .rename_setup = nfs4_proc_rename_setup,
9402 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9403 .rename_done = nfs4_proc_rename_done,
9404 .link = nfs4_proc_link,
9405 .symlink = nfs4_proc_symlink,
9406 .mkdir = nfs4_proc_mkdir,
9407 .rmdir = nfs4_proc_remove,
9408 .readdir = nfs4_proc_readdir,
9409 .mknod = nfs4_proc_mknod,
9410 .statfs = nfs4_proc_statfs,
9411 .fsinfo = nfs4_proc_fsinfo,
9412 .pathconf = nfs4_proc_pathconf,
9413 .set_capabilities = nfs4_server_capabilities,
9414 .decode_dirent = nfs4_decode_dirent,
9415 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9416 .read_setup = nfs4_proc_read_setup,
9417 .read_done = nfs4_read_done,
9418 .write_setup = nfs4_proc_write_setup,
9419 .write_done = nfs4_write_done,
9420 .commit_setup = nfs4_proc_commit_setup,
9421 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9422 .commit_done = nfs4_commit_done,
9423 .lock = nfs4_proc_lock,
9424 .clear_acl_cache = nfs4_zap_acl_attr,
9425 .close_context = nfs4_close_context,
9426 .open_context = nfs4_atomic_open,
9427 .have_delegation = nfs4_have_delegation,
9428 .return_delegation = nfs4_inode_return_delegation,
9429 .alloc_client = nfs4_alloc_client,
9430 .init_client = nfs4_init_client,
9431 .free_client = nfs4_free_client,
9432 .create_server = nfs4_create_server,
9433 .clone_server = nfs_clone_server,
9436 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9437 .name = XATTR_NAME_NFSV4_ACL,
9438 .list = nfs4_xattr_list_nfs4_acl,
9439 .get = nfs4_xattr_get_nfs4_acl,
9440 .set = nfs4_xattr_set_nfs4_acl,
9443 const struct xattr_handler *nfs4_xattr_handlers[] = {
9444 &nfs4_xattr_nfs4_acl_handler,
9445 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9446 &nfs4_xattr_nfs4_label_handler,