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>
57 #include <linux/iversion.h>
60 #include "delegation.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_POLL_RETRY_MIN (HZ/10)
75 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 /* file attributes which can be mapped to nfs attributes */
78 #define NFS4_VALID_ATTRS (ATTR_MODE \
89 static int _nfs4_proc_open(struct nfs4_opendata *data);
90 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
91 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
92 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
93 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
95 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
97 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
99 #ifdef CONFIG_NFS_V4_1
100 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
101 struct rpc_cred *cred,
102 struct nfs4_slot *slot,
104 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
106 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
107 struct rpc_cred *, bool);
110 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
111 static inline struct nfs4_label *
112 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
113 struct iattr *sattr, struct nfs4_label *label)
120 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
123 err = security_dentry_init_security(dentry, sattr->ia_mode,
124 &dentry->d_name, (void **)&label->label, &label->len);
131 nfs4_label_release_security(struct nfs4_label *label)
134 security_release_secctx(label->label, label->len);
136 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139 return server->attr_bitmask;
141 return server->attr_bitmask_nl;
144 static inline struct nfs4_label *
145 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
146 struct iattr *sattr, struct nfs4_label *l)
149 nfs4_label_release_security(struct nfs4_label *label)
152 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
153 { return server->attr_bitmask; }
156 /* Prevent leaks of NFSv4 errors into userland */
157 static int nfs4_map_errors(int err)
162 case -NFS4ERR_RESOURCE:
163 case -NFS4ERR_LAYOUTTRYLATER:
164 case -NFS4ERR_RECALLCONFLICT:
166 case -NFS4ERR_WRONGSEC:
167 case -NFS4ERR_WRONG_CRED:
169 case -NFS4ERR_BADOWNER:
170 case -NFS4ERR_BADNAME:
172 case -NFS4ERR_SHARE_DENIED:
174 case -NFS4ERR_MINOR_VERS_MISMATCH:
175 return -EPROTONOSUPPORT;
176 case -NFS4ERR_FILE_OPEN:
179 dprintk("%s could not handle NFSv4 error %d\n",
187 * This is our standard bitmap for GETATTR requests.
189 const u32 nfs4_fattr_bitmap[3] = {
191 | FATTR4_WORD0_CHANGE
194 | FATTR4_WORD0_FILEID,
196 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID,
205 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
206 FATTR4_WORD2_SECURITY_LABEL
210 static const u32 nfs4_pnfs_open_bitmap[3] = {
212 | FATTR4_WORD0_CHANGE
215 | FATTR4_WORD0_FILEID,
217 | FATTR4_WORD1_NUMLINKS
219 | FATTR4_WORD1_OWNER_GROUP
220 | FATTR4_WORD1_RAWDEV
221 | FATTR4_WORD1_SPACE_USED
222 | FATTR4_WORD1_TIME_ACCESS
223 | FATTR4_WORD1_TIME_METADATA
224 | FATTR4_WORD1_TIME_MODIFY,
225 FATTR4_WORD2_MDSTHRESHOLD
226 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
227 | FATTR4_WORD2_SECURITY_LABEL
231 static const u32 nfs4_open_noattr_bitmap[3] = {
233 | FATTR4_WORD0_FILEID,
236 const u32 nfs4_statfs_bitmap[3] = {
237 FATTR4_WORD0_FILES_AVAIL
238 | FATTR4_WORD0_FILES_FREE
239 | FATTR4_WORD0_FILES_TOTAL,
240 FATTR4_WORD1_SPACE_AVAIL
241 | FATTR4_WORD1_SPACE_FREE
242 | FATTR4_WORD1_SPACE_TOTAL
245 const u32 nfs4_pathconf_bitmap[3] = {
247 | FATTR4_WORD0_MAXNAME,
251 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
252 | FATTR4_WORD0_MAXREAD
253 | FATTR4_WORD0_MAXWRITE
254 | FATTR4_WORD0_LEASE_TIME,
255 FATTR4_WORD1_TIME_DELTA
256 | FATTR4_WORD1_FS_LAYOUT_TYPES,
257 FATTR4_WORD2_LAYOUT_BLKSIZE
258 | FATTR4_WORD2_CLONE_BLKSIZE
261 const u32 nfs4_fs_locations_bitmap[3] = {
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
277 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
278 struct nfs4_readdir_arg *readdir)
280 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
284 readdir->cookie = cookie;
285 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
290 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
295 * NFSv4 servers do not return entries for '.' and '..'
296 * Therefore, we fake these entries here. We let '.'
297 * have cookie 0 and '..' have cookie 1. Note that
298 * when talking to the server, we always send cookie 0
301 start = p = kmap_atomic(*readdir->pages);
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_one; /* cookie, second word */
307 *p++ = xdr_one; /* entry len */
308 memcpy(p, ".\0\0\0", 4); /* entry */
310 *p++ = xdr_one; /* bitmap length */
311 *p++ = htonl(attrs); /* bitmap */
312 *p++ = htonl(12); /* attribute buffer length */
313 *p++ = htonl(NF4DIR);
314 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
317 *p++ = xdr_one; /* next */
318 *p++ = xdr_zero; /* cookie, first word */
319 *p++ = xdr_two; /* cookie, second word */
320 *p++ = xdr_two; /* entry len */
321 memcpy(p, "..\0\0", 4); /* entry */
323 *p++ = xdr_one; /* bitmap length */
324 *p++ = htonl(attrs); /* bitmap */
325 *p++ = htonl(12); /* attribute buffer length */
326 *p++ = htonl(NF4DIR);
327 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
329 readdir->pgbase = (char *)p - (char *)start;
330 readdir->count -= readdir->pgbase;
331 kunmap_atomic(start);
334 static void nfs4_test_and_free_stateid(struct nfs_server *server,
335 nfs4_stateid *stateid,
336 struct rpc_cred *cred)
338 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
340 ops->test_and_free_expired(server, stateid, cred);
343 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
344 nfs4_stateid *stateid,
345 struct rpc_cred *cred)
347 stateid->type = NFS4_REVOKED_STATEID_TYPE;
348 nfs4_test_and_free_stateid(server, stateid, cred);
351 static void nfs4_free_revoked_stateid(struct nfs_server *server,
352 const nfs4_stateid *stateid,
353 struct rpc_cred *cred)
357 nfs4_stateid_copy(&tmp, stateid);
358 __nfs4_free_revoked_stateid(server, &tmp, cred);
361 static long nfs4_update_delay(long *timeout)
365 return NFS4_POLL_RETRY_MAX;
367 *timeout = NFS4_POLL_RETRY_MIN;
368 if (*timeout > NFS4_POLL_RETRY_MAX)
369 *timeout = NFS4_POLL_RETRY_MAX;
375 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
381 freezable_schedule_timeout_killable_unsafe(
382 nfs4_update_delay(timeout));
383 if (fatal_signal_pending(current))
388 /* This is the error handling routine for processes that are allowed
391 static int nfs4_do_handle_exception(struct nfs_server *server,
392 int errorcode, struct nfs4_exception *exception)
394 struct nfs_client *clp = server->nfs_client;
395 struct nfs4_state *state = exception->state;
396 const nfs4_stateid *stateid = exception->stateid;
397 struct inode *inode = exception->inode;
400 exception->delay = 0;
401 exception->recovering = 0;
402 exception->retry = 0;
404 if (stateid == NULL && state != NULL)
405 stateid = &state->stateid;
410 case -NFS4ERR_DELEG_REVOKED:
411 case -NFS4ERR_ADMIN_REVOKED:
412 case -NFS4ERR_EXPIRED:
413 case -NFS4ERR_BAD_STATEID:
414 if (inode != NULL && stateid != NULL) {
415 nfs_inode_find_state_and_recover(inode,
417 goto wait_on_recovery;
419 case -NFS4ERR_OPENMODE:
423 err = nfs_async_inode_return_delegation(inode,
426 goto wait_on_recovery;
427 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
428 exception->retry = 1;
434 ret = nfs4_schedule_stateid_recovery(server, state);
437 goto wait_on_recovery;
438 case -NFS4ERR_STALE_STATEID:
439 case -NFS4ERR_STALE_CLIENTID:
440 nfs4_schedule_lease_recovery(clp);
441 goto wait_on_recovery;
443 ret = nfs4_schedule_migration_recovery(server);
446 goto wait_on_recovery;
447 case -NFS4ERR_LEASE_MOVED:
448 nfs4_schedule_lease_moved_recovery(clp);
449 goto wait_on_recovery;
450 #if defined(CONFIG_NFS_V4_1)
451 case -NFS4ERR_BADSESSION:
452 case -NFS4ERR_BADSLOT:
453 case -NFS4ERR_BAD_HIGH_SLOT:
454 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
455 case -NFS4ERR_DEADSESSION:
456 case -NFS4ERR_SEQ_FALSE_RETRY:
457 case -NFS4ERR_SEQ_MISORDERED:
458 dprintk("%s ERROR: %d Reset session\n", __func__,
460 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
461 goto wait_on_recovery;
462 #endif /* defined(CONFIG_NFS_V4_1) */
463 case -NFS4ERR_FILE_OPEN:
464 if (exception->timeout > HZ) {
465 /* We have retried a decent amount, time to
472 nfs_inc_server_stats(server, NFSIOS_DELAY);
474 case -NFS4ERR_LAYOUTTRYLATER:
475 case -NFS4ERR_RECALLCONFLICT:
476 exception->delay = 1;
479 case -NFS4ERR_RETRY_UNCACHED_REP:
480 case -NFS4ERR_OLD_STATEID:
481 exception->retry = 1;
483 case -NFS4ERR_BADOWNER:
484 /* The following works around a Linux server bug! */
485 case -NFS4ERR_BADNAME:
486 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
487 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
488 exception->retry = 1;
489 printk(KERN_WARNING "NFS: v4 server %s "
490 "does not accept raw "
492 "Reenabling the idmapper.\n",
493 server->nfs_client->cl_hostname);
496 /* We failed to handle the error */
497 return nfs4_map_errors(ret);
499 exception->recovering = 1;
503 /* This is the error handling routine for processes that are allowed
506 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
508 struct nfs_client *clp = server->nfs_client;
511 ret = nfs4_do_handle_exception(server, errorcode, exception);
512 if (exception->delay) {
513 ret = nfs4_delay(server->client, &exception->timeout);
516 if (exception->recovering) {
517 ret = nfs4_wait_clnt_recover(clp);
518 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
525 exception->retry = 1;
530 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
531 int errorcode, struct nfs4_exception *exception)
533 struct nfs_client *clp = server->nfs_client;
536 ret = nfs4_do_handle_exception(server, errorcode, exception);
537 if (exception->delay) {
538 rpc_delay(task, nfs4_update_delay(&exception->timeout));
541 if (exception->recovering) {
542 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
543 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
544 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
547 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
552 exception->retry = 1;
557 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
558 struct nfs4_state *state, long *timeout)
560 struct nfs4_exception exception = {
564 if (task->tk_status >= 0)
567 exception.timeout = *timeout;
568 task->tk_status = nfs4_async_handle_exception(task, server,
571 if (exception.delay && timeout)
572 *timeout = exception.timeout;
579 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
580 * or 'false' otherwise.
582 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
584 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
585 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
588 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
590 spin_lock(&clp->cl_lock);
591 if (time_before(clp->cl_last_renewal,timestamp))
592 clp->cl_last_renewal = timestamp;
593 spin_unlock(&clp->cl_lock);
596 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
598 struct nfs_client *clp = server->nfs_client;
600 if (!nfs4_has_session(clp))
601 do_renew_lease(clp, timestamp);
604 struct nfs4_call_sync_data {
605 const struct nfs_server *seq_server;
606 struct nfs4_sequence_args *seq_args;
607 struct nfs4_sequence_res *seq_res;
610 void nfs4_init_sequence(struct nfs4_sequence_args *args,
611 struct nfs4_sequence_res *res, int cache_reply)
613 args->sa_slot = NULL;
614 args->sa_cache_this = cache_reply;
615 args->sa_privileged = 0;
620 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
622 args->sa_privileged = 1;
625 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
627 struct nfs4_slot *slot = res->sr_slot;
628 struct nfs4_slot_table *tbl;
631 spin_lock(&tbl->slot_tbl_lock);
632 if (!nfs41_wake_and_assign_slot(tbl, slot))
633 nfs4_free_slot(tbl, slot);
634 spin_unlock(&tbl->slot_tbl_lock);
639 static int nfs40_sequence_done(struct rpc_task *task,
640 struct nfs4_sequence_res *res)
642 if (res->sr_slot != NULL)
643 nfs40_sequence_free_slot(res);
647 #if defined(CONFIG_NFS_V4_1)
649 static void nfs41_release_slot(struct nfs4_slot *slot)
651 struct nfs4_session *session;
652 struct nfs4_slot_table *tbl;
653 bool send_new_highest_used_slotid = false;
658 session = tbl->session;
660 /* Bump the slot sequence number */
665 spin_lock(&tbl->slot_tbl_lock);
666 /* Be nice to the server: try to ensure that the last transmitted
667 * value for highest_user_slotid <= target_highest_slotid
669 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
670 send_new_highest_used_slotid = true;
672 if (nfs41_wake_and_assign_slot(tbl, slot)) {
673 send_new_highest_used_slotid = false;
676 nfs4_free_slot(tbl, slot);
678 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
679 send_new_highest_used_slotid = false;
681 spin_unlock(&tbl->slot_tbl_lock);
682 if (send_new_highest_used_slotid)
683 nfs41_notify_server(session->clp);
684 if (waitqueue_active(&tbl->slot_waitq))
685 wake_up_all(&tbl->slot_waitq);
688 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
690 nfs41_release_slot(res->sr_slot);
694 static int nfs41_sequence_process(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
697 struct nfs4_session *session;
698 struct nfs4_slot *slot = res->sr_slot;
699 struct nfs_client *clp;
700 bool interrupted = false;
705 /* don't increment the sequence number if the task wasn't sent */
706 if (!RPC_WAS_SENT(task))
709 session = slot->table->session;
711 if (slot->interrupted) {
712 if (res->sr_status != -NFS4ERR_DELAY)
713 slot->interrupted = 0;
717 trace_nfs4_sequence_done(session, res);
718 /* Check the SEQUENCE operation status */
719 switch (res->sr_status) {
721 /* Update the slot's sequence and clientid lease timer */
724 do_renew_lease(clp, res->sr_timestamp);
725 /* Check sequence flags */
726 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
728 nfs41_update_target_slotid(slot->table, slot, res);
732 * sr_status remains 1 if an RPC level error occurred.
733 * The server may or may not have processed the sequence
735 * Mark the slot as having hosted an interrupted RPC call.
737 slot->interrupted = 1;
740 /* The server detected a resend of the RPC call and
741 * returned NFS4ERR_DELAY as per Section 2.10.6.2
744 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
749 case -NFS4ERR_BADSLOT:
751 * The slot id we used was probably retired. Try again
752 * using a different slot id.
754 if (slot->seq_nr < slot->table->target_highest_slotid)
755 goto session_recover;
757 case -NFS4ERR_SEQ_MISORDERED:
759 * Was the last operation on this sequence interrupted?
760 * If so, retry after bumping the sequence number.
765 * Could this slot have been previously retired?
766 * If so, then the server may be expecting seq_nr = 1!
768 if (slot->seq_nr != 1) {
772 goto session_recover;
773 case -NFS4ERR_SEQ_FALSE_RETRY:
776 goto session_recover;
778 /* Just update the slot sequence no. */
782 /* The session may be reset by one of the error handlers. */
783 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
787 nfs4_schedule_session_recovery(session, res->sr_status);
792 if (rpc_restart_call_prepare(task)) {
793 nfs41_sequence_free_slot(res);
799 if (!rpc_restart_call(task))
801 rpc_delay(task, NFS4_POLL_RETRY_MAX);
805 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
807 if (!nfs41_sequence_process(task, res))
809 if (res->sr_slot != NULL)
810 nfs41_sequence_free_slot(res);
814 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
816 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
818 if (res->sr_slot == NULL)
820 if (res->sr_slot->table->session != NULL)
821 return nfs41_sequence_process(task, res);
822 return nfs40_sequence_done(task, res);
825 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
827 if (res->sr_slot != NULL) {
828 if (res->sr_slot->table->session != NULL)
829 nfs41_sequence_free_slot(res);
831 nfs40_sequence_free_slot(res);
835 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
837 if (res->sr_slot == NULL)
839 if (!res->sr_slot->table->session)
840 return nfs40_sequence_done(task, res);
841 return nfs41_sequence_done(task, res);
843 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
845 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
847 struct nfs4_call_sync_data *data = calldata;
849 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
851 nfs4_setup_sequence(data->seq_server->nfs_client,
852 data->seq_args, data->seq_res, task);
855 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
857 struct nfs4_call_sync_data *data = calldata;
859 nfs41_sequence_done(task, data->seq_res);
862 static const struct rpc_call_ops nfs41_call_sync_ops = {
863 .rpc_call_prepare = nfs41_call_sync_prepare,
864 .rpc_call_done = nfs41_call_sync_done,
868 nfs4_sequence_process_interrupted(struct nfs_client *client,
869 struct nfs4_slot *slot, struct rpc_cred *cred)
871 struct rpc_task *task;
873 task = _nfs41_proc_sequence(client, cred, slot, true);
875 rpc_put_task_async(task);
878 #else /* !CONFIG_NFS_V4_1 */
880 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
882 return nfs40_sequence_done(task, res);
885 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
887 if (res->sr_slot != NULL)
888 nfs40_sequence_free_slot(res);
891 int nfs4_sequence_done(struct rpc_task *task,
892 struct nfs4_sequence_res *res)
894 return nfs40_sequence_done(task, res);
896 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
899 nfs4_sequence_process_interrupted(struct nfs_client *client,
900 struct nfs4_slot *slot, struct rpc_cred *cred)
903 slot->interrupted = 0;
906 #endif /* !CONFIG_NFS_V4_1 */
909 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
910 struct nfs4_sequence_res *res,
911 struct nfs4_slot *slot)
915 slot->privileged = args->sa_privileged ? 1 : 0;
916 args->sa_slot = slot;
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
925 int nfs4_setup_sequence(struct nfs_client *client,
926 struct nfs4_sequence_args *args,
927 struct nfs4_sequence_res *res,
928 struct rpc_task *task)
930 struct nfs4_session *session = nfs4_get_session(client);
931 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
932 struct nfs4_slot *slot;
934 /* slot already allocated? */
935 if (res->sr_slot != NULL)
939 tbl = &session->fc_slot_table;
940 task->tk_timeout = 0;
944 spin_lock(&tbl->slot_tbl_lock);
945 /* The state manager will wait until the slot table is empty */
946 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
949 slot = nfs4_alloc_slot(tbl);
951 /* Try again in 1/4 second */
952 if (slot == ERR_PTR(-ENOMEM))
953 task->tk_timeout = HZ >> 2;
956 spin_unlock(&tbl->slot_tbl_lock);
958 if (likely(!slot->interrupted))
960 nfs4_sequence_process_interrupted(client,
961 slot, task->tk_msg.rpc_cred);
964 nfs4_sequence_attach_slot(args, res, slot);
966 trace_nfs4_setup_sequence(session, args);
968 rpc_call_start(task);
972 if (args->sa_privileged)
973 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
974 NULL, RPC_PRIORITY_PRIVILEGED);
976 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
977 spin_unlock(&tbl->slot_tbl_lock);
980 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
982 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
984 struct nfs4_call_sync_data *data = calldata;
985 nfs4_setup_sequence(data->seq_server->nfs_client,
986 data->seq_args, data->seq_res, task);
989 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
991 struct nfs4_call_sync_data *data = calldata;
992 nfs4_sequence_done(task, data->seq_res);
995 static const struct rpc_call_ops nfs40_call_sync_ops = {
996 .rpc_call_prepare = nfs40_call_sync_prepare,
997 .rpc_call_done = nfs40_call_sync_done,
1000 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1001 struct nfs_server *server,
1002 struct rpc_message *msg,
1003 struct nfs4_sequence_args *args,
1004 struct nfs4_sequence_res *res)
1007 struct rpc_task *task;
1008 struct nfs_client *clp = server->nfs_client;
1009 struct nfs4_call_sync_data data = {
1010 .seq_server = server,
1014 struct rpc_task_setup task_setup = {
1017 .callback_ops = clp->cl_mvops->call_sync_ops,
1018 .callback_data = &data
1021 task = rpc_run_task(&task_setup);
1023 ret = PTR_ERR(task);
1025 ret = task->tk_status;
1031 int nfs4_call_sync(struct rpc_clnt *clnt,
1032 struct nfs_server *server,
1033 struct rpc_message *msg,
1034 struct nfs4_sequence_args *args,
1035 struct nfs4_sequence_res *res,
1038 nfs4_init_sequence(args, res, cache_reply);
1039 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1042 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1043 unsigned long timestamp)
1045 struct nfs_inode *nfsi = NFS_I(dir);
1047 spin_lock(&dir->i_lock);
1048 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1049 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1050 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1051 nfsi->attrtimeo_timestamp = jiffies;
1053 nfs_force_lookup_revalidate(dir);
1054 if (cinfo->before != inode_peek_iversion_raw(dir))
1055 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1056 NFS_INO_INVALID_ACL;
1058 inode_set_iversion_raw(dir, cinfo->after);
1059 nfsi->read_cache_jiffies = timestamp;
1060 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1061 nfs_fscache_invalidate(dir);
1062 spin_unlock(&dir->i_lock);
1065 struct nfs4_opendata {
1067 struct nfs_openargs o_arg;
1068 struct nfs_openres o_res;
1069 struct nfs_open_confirmargs c_arg;
1070 struct nfs_open_confirmres c_res;
1071 struct nfs4_string owner_name;
1072 struct nfs4_string group_name;
1073 struct nfs4_label *a_label;
1074 struct nfs_fattr f_attr;
1075 struct nfs4_label *f_label;
1077 struct dentry *dentry;
1078 struct nfs4_state_owner *owner;
1079 struct nfs4_state *state;
1081 unsigned long timestamp;
1089 struct nfs4_open_createattrs {
1090 struct nfs4_label *label;
1091 struct iattr *sattr;
1092 const __u32 verf[2];
1095 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1096 int err, struct nfs4_exception *exception)
1100 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1102 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1103 exception->retry = 1;
1108 nfs4_map_atomic_open_share(struct nfs_server *server,
1109 fmode_t fmode, int openflags)
1113 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1115 res = NFS4_SHARE_ACCESS_READ;
1118 res = NFS4_SHARE_ACCESS_WRITE;
1120 case FMODE_READ|FMODE_WRITE:
1121 res = NFS4_SHARE_ACCESS_BOTH;
1123 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1125 /* Want no delegation if we're using O_DIRECT */
1126 if (openflags & O_DIRECT)
1127 res |= NFS4_SHARE_WANT_NO_DELEG;
1132 static enum open_claim_type4
1133 nfs4_map_atomic_open_claim(struct nfs_server *server,
1134 enum open_claim_type4 claim)
1136 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1141 case NFS4_OPEN_CLAIM_FH:
1142 return NFS4_OPEN_CLAIM_NULL;
1143 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1144 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1145 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1146 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1150 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1152 p->o_res.f_attr = &p->f_attr;
1153 p->o_res.f_label = p->f_label;
1154 p->o_res.seqid = p->o_arg.seqid;
1155 p->c_res.seqid = p->c_arg.seqid;
1156 p->o_res.server = p->o_arg.server;
1157 p->o_res.access_request = p->o_arg.access;
1158 nfs_fattr_init(&p->f_attr);
1159 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1162 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1163 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1164 const struct nfs4_open_createattrs *c,
1165 enum open_claim_type4 claim,
1168 struct dentry *parent = dget_parent(dentry);
1169 struct inode *dir = d_inode(parent);
1170 struct nfs_server *server = NFS_SERVER(dir);
1171 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1172 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1173 struct nfs4_opendata *p;
1175 p = kzalloc(sizeof(*p), gfp_mask);
1179 p->f_label = nfs4_label_alloc(server, gfp_mask);
1180 if (IS_ERR(p->f_label))
1183 p->a_label = nfs4_label_alloc(server, gfp_mask);
1184 if (IS_ERR(p->a_label))
1187 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1188 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1189 if (IS_ERR(p->o_arg.seqid))
1190 goto err_free_label;
1191 nfs_sb_active(dentry->d_sb);
1192 p->dentry = dget(dentry);
1195 atomic_inc(&sp->so_count);
1196 p->o_arg.open_flags = flags;
1197 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1198 p->o_arg.umask = current_umask();
1199 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1200 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1202 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1203 * will return permission denied for all bits until close */
1204 if (!(flags & O_EXCL)) {
1205 /* ask server to check for all possible rights as results
1207 switch (p->o_arg.claim) {
1210 case NFS4_OPEN_CLAIM_NULL:
1211 case NFS4_OPEN_CLAIM_FH:
1212 p->o_arg.access = NFS4_ACCESS_READ |
1213 NFS4_ACCESS_MODIFY |
1214 NFS4_ACCESS_EXTEND |
1215 NFS4_ACCESS_EXECUTE;
1218 p->o_arg.clientid = server->nfs_client->cl_clientid;
1219 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1220 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1221 p->o_arg.name = &dentry->d_name;
1222 p->o_arg.server = server;
1223 p->o_arg.bitmask = nfs4_bitmask(server, label);
1224 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1225 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1226 switch (p->o_arg.claim) {
1227 case NFS4_OPEN_CLAIM_NULL:
1228 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1229 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1230 p->o_arg.fh = NFS_FH(dir);
1232 case NFS4_OPEN_CLAIM_PREVIOUS:
1233 case NFS4_OPEN_CLAIM_FH:
1234 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1235 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1236 p->o_arg.fh = NFS_FH(d_inode(dentry));
1238 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
1239 p->o_arg.u.attrs = &p->attrs;
1240 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1242 memcpy(p->o_arg.u.verifier.data, c->verf,
1243 sizeof(p->o_arg.u.verifier.data));
1245 p->c_arg.fh = &p->o_res.fh;
1246 p->c_arg.stateid = &p->o_res.stateid;
1247 p->c_arg.seqid = p->o_arg.seqid;
1248 nfs4_init_opendata_res(p);
1249 kref_init(&p->kref);
1253 nfs4_label_free(p->a_label);
1255 nfs4_label_free(p->f_label);
1263 static void nfs4_opendata_free(struct kref *kref)
1265 struct nfs4_opendata *p = container_of(kref,
1266 struct nfs4_opendata, kref);
1267 struct super_block *sb = p->dentry->d_sb;
1269 nfs_free_seqid(p->o_arg.seqid);
1270 nfs4_sequence_free_slot(&p->o_res.seq_res);
1271 if (p->state != NULL)
1272 nfs4_put_open_state(p->state);
1273 nfs4_put_state_owner(p->owner);
1275 nfs4_label_free(p->a_label);
1276 nfs4_label_free(p->f_label);
1280 nfs_sb_deactive(sb);
1281 nfs_fattr_free_names(&p->f_attr);
1282 kfree(p->f_attr.mdsthreshold);
1286 static void nfs4_opendata_put(struct nfs4_opendata *p)
1289 kref_put(&p->kref, nfs4_opendata_free);
1292 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1295 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1296 case FMODE_READ|FMODE_WRITE:
1297 return state->n_rdwr != 0;
1299 return state->n_wronly != 0;
1301 return state->n_rdonly != 0;
1307 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1311 if (open_mode & (O_EXCL|O_TRUNC))
1313 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1315 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1316 && state->n_rdonly != 0;
1319 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1320 && state->n_wronly != 0;
1322 case FMODE_READ|FMODE_WRITE:
1323 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1324 && state->n_rdwr != 0;
1330 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1331 enum open_claim_type4 claim)
1333 if (delegation == NULL)
1335 if ((delegation->type & fmode) != fmode)
1337 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1340 case NFS4_OPEN_CLAIM_NULL:
1341 case NFS4_OPEN_CLAIM_FH:
1343 case NFS4_OPEN_CLAIM_PREVIOUS:
1344 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1349 nfs_mark_delegation_referenced(delegation);
1353 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1362 case FMODE_READ|FMODE_WRITE:
1365 nfs4_state_set_mode_locked(state, state->state | fmode);
1368 #ifdef CONFIG_NFS_V4_1
1369 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1371 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1373 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1375 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1379 #endif /* CONFIG_NFS_V4_1 */
1381 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1383 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1384 wake_up_all(&state->waitq);
1387 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1388 const nfs4_stateid *stateid)
1390 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1391 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1393 if (stateid_seqid == state_seqid + 1U ||
1394 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1395 nfs_state_log_update_open_stateid(state);
1397 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1400 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1402 struct nfs_client *clp = state->owner->so_server->nfs_client;
1403 bool need_recover = false;
1405 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1406 need_recover = true;
1407 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1408 need_recover = true;
1409 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1410 need_recover = true;
1412 nfs4_state_mark_reclaim_nograce(clp, state);
1416 * Check for whether or not the caller may update the open stateid
1417 * to the value passed in by stateid.
1419 * Note: This function relies heavily on the server implementing
1420 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1422 * i.e. The stateid seqids have to be initialised to 1, and
1423 * are then incremented on every state transition.
1425 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1426 const nfs4_stateid *stateid)
1428 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1429 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1430 if (stateid->seqid == cpu_to_be32(1))
1431 nfs_state_log_update_open_stateid(state);
1433 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1437 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1438 nfs_state_log_out_of_order_open_stateid(state, stateid);
1444 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1446 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1448 if (state->n_wronly)
1449 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1450 if (state->n_rdonly)
1451 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1453 set_bit(NFS_O_RDWR_STATE, &state->flags);
1454 set_bit(NFS_OPEN_STATE, &state->flags);
1457 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1458 nfs4_stateid *stateid, fmode_t fmode)
1460 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1461 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1463 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1469 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1470 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1471 clear_bit(NFS_OPEN_STATE, &state->flags);
1473 if (stateid == NULL)
1475 /* Handle OPEN+OPEN_DOWNGRADE races */
1476 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1477 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1478 nfs_resync_open_stateid_locked(state);
1481 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1482 nfs4_stateid_copy(&state->stateid, stateid);
1483 nfs4_stateid_copy(&state->open_stateid, stateid);
1484 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1486 nfs_state_log_update_open_stateid(state);
1489 static void nfs_clear_open_stateid(struct nfs4_state *state,
1490 nfs4_stateid *arg_stateid,
1491 nfs4_stateid *stateid, fmode_t fmode)
1493 write_seqlock(&state->seqlock);
1494 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1495 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1496 nfs_clear_open_stateid_locked(state, stateid, fmode);
1497 write_sequnlock(&state->seqlock);
1498 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1499 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1502 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1503 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1509 if (!nfs_need_update_open_stateid(state, stateid))
1511 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1515 /* Rely on seqids for serialisation with NFSv4.0 */
1516 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1519 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1521 * Ensure we process the state changes in the same order
1522 * in which the server processed them by delaying the
1523 * update of the stateid until we are in sequence.
1525 write_sequnlock(&state->seqlock);
1526 spin_unlock(&state->owner->so_lock);
1528 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1529 if (!signal_pending(current)) {
1530 if (schedule_timeout(5*HZ) == 0)
1536 finish_wait(&state->waitq, &wait);
1538 spin_lock(&state->owner->so_lock);
1539 write_seqlock(&state->seqlock);
1542 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1543 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1544 nfs4_stateid_copy(freeme, &state->open_stateid);
1545 nfs_test_and_clear_all_open_stateid(state);
1548 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1549 nfs4_stateid_copy(&state->stateid, stateid);
1550 nfs4_stateid_copy(&state->open_stateid, stateid);
1551 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1552 nfs_state_log_update_open_stateid(state);
1555 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1556 const nfs4_stateid *open_stateid,
1558 nfs4_stateid *freeme)
1561 * Protect the call to nfs4_state_set_mode_locked and
1562 * serialise the stateid update
1564 write_seqlock(&state->seqlock);
1565 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1568 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1571 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1573 case FMODE_READ|FMODE_WRITE:
1574 set_bit(NFS_O_RDWR_STATE, &state->flags);
1576 set_bit(NFS_OPEN_STATE, &state->flags);
1577 write_sequnlock(&state->seqlock);
1580 static void nfs_state_set_delegation(struct nfs4_state *state,
1581 const nfs4_stateid *deleg_stateid,
1585 * Protect the call to nfs4_state_set_mode_locked and
1586 * serialise the stateid update
1588 write_seqlock(&state->seqlock);
1589 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1590 set_bit(NFS_DELEGATED_STATE, &state->flags);
1591 write_sequnlock(&state->seqlock);
1594 static int update_open_stateid(struct nfs4_state *state,
1595 const nfs4_stateid *open_stateid,
1596 const nfs4_stateid *delegation,
1599 struct nfs_server *server = NFS_SERVER(state->inode);
1600 struct nfs_client *clp = server->nfs_client;
1601 struct nfs_inode *nfsi = NFS_I(state->inode);
1602 struct nfs_delegation *deleg_cur;
1603 nfs4_stateid freeme = { };
1606 fmode &= (FMODE_READ|FMODE_WRITE);
1609 spin_lock(&state->owner->so_lock);
1610 if (open_stateid != NULL) {
1611 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1615 deleg_cur = rcu_dereference(nfsi->delegation);
1616 if (deleg_cur == NULL)
1619 spin_lock(&deleg_cur->lock);
1620 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1621 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1622 (deleg_cur->type & fmode) != fmode)
1623 goto no_delegation_unlock;
1625 if (delegation == NULL)
1626 delegation = &deleg_cur->stateid;
1627 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1628 goto no_delegation_unlock;
1630 nfs_mark_delegation_referenced(deleg_cur);
1631 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1633 no_delegation_unlock:
1634 spin_unlock(&deleg_cur->lock);
1637 update_open_stateflags(state, fmode);
1638 spin_unlock(&state->owner->so_lock);
1641 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1642 nfs4_schedule_state_manager(clp);
1643 if (freeme.type != 0)
1644 nfs4_test_and_free_stateid(server, &freeme,
1645 state->owner->so_cred);
1650 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1651 const nfs4_stateid *stateid)
1653 struct nfs4_state *state = lsp->ls_state;
1656 spin_lock(&state->state_lock);
1657 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1659 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1661 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1664 spin_unlock(&state->state_lock);
1668 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1670 struct nfs_delegation *delegation;
1673 delegation = rcu_dereference(NFS_I(inode)->delegation);
1674 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1679 nfs4_inode_return_delegation(inode);
1682 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1684 struct nfs4_state *state = opendata->state;
1685 struct nfs_inode *nfsi = NFS_I(state->inode);
1686 struct nfs_delegation *delegation;
1687 int open_mode = opendata->o_arg.open_flags;
1688 fmode_t fmode = opendata->o_arg.fmode;
1689 enum open_claim_type4 claim = opendata->o_arg.claim;
1690 nfs4_stateid stateid;
1694 spin_lock(&state->owner->so_lock);
1695 if (can_open_cached(state, fmode, open_mode)) {
1696 update_open_stateflags(state, fmode);
1697 spin_unlock(&state->owner->so_lock);
1698 goto out_return_state;
1700 spin_unlock(&state->owner->so_lock);
1702 delegation = rcu_dereference(nfsi->delegation);
1703 if (!can_open_delegated(delegation, fmode, claim)) {
1707 /* Save the delegation */
1708 nfs4_stateid_copy(&stateid, &delegation->stateid);
1710 nfs_release_seqid(opendata->o_arg.seqid);
1711 if (!opendata->is_recover) {
1712 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1718 /* Try to update the stateid using the delegation */
1719 if (update_open_stateid(state, NULL, &stateid, fmode))
1720 goto out_return_state;
1723 return ERR_PTR(ret);
1725 atomic_inc(&state->count);
1730 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1732 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1733 struct nfs_delegation *delegation;
1734 int delegation_flags = 0;
1737 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1739 delegation_flags = delegation->flags;
1741 switch (data->o_arg.claim) {
1744 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1745 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1746 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1747 "returning a delegation for "
1748 "OPEN(CLAIM_DELEGATE_CUR)\n",
1752 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1753 nfs_inode_set_delegation(state->inode,
1754 data->owner->so_cred,
1757 nfs_inode_reclaim_delegation(state->inode,
1758 data->owner->so_cred,
1763 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1764 * and update the nfs4_state.
1766 static struct nfs4_state *
1767 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1769 struct inode *inode = data->state->inode;
1770 struct nfs4_state *state = data->state;
1773 if (!data->rpc_done) {
1774 if (data->rpc_status)
1775 return ERR_PTR(data->rpc_status);
1776 /* cached opens have already been processed */
1780 ret = nfs_refresh_inode(inode, &data->f_attr);
1782 return ERR_PTR(ret);
1784 if (data->o_res.delegation_type != 0)
1785 nfs4_opendata_check_deleg(data, state);
1787 update_open_stateid(state, &data->o_res.stateid, NULL,
1789 atomic_inc(&state->count);
1794 static struct inode *
1795 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1797 struct inode *inode;
1799 switch (data->o_arg.claim) {
1800 case NFS4_OPEN_CLAIM_NULL:
1801 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1802 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1803 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1804 return ERR_PTR(-EAGAIN);
1805 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1806 &data->f_attr, data->f_label);
1809 inode = d_inode(data->dentry);
1811 nfs_refresh_inode(inode, &data->f_attr);
1816 static struct nfs4_state *
1817 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1819 struct nfs4_state *state;
1820 struct inode *inode;
1822 inode = nfs4_opendata_get_inode(data);
1824 return ERR_CAST(inode);
1825 if (data->state != NULL && data->state->inode == inode) {
1826 state = data->state;
1827 atomic_inc(&state->count);
1829 state = nfs4_get_open_state(inode, data->owner);
1832 state = ERR_PTR(-ENOMEM);
1836 static struct nfs4_state *
1837 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1839 struct nfs4_state *state;
1841 if (!data->rpc_done) {
1842 state = nfs4_try_open_cached(data);
1843 trace_nfs4_cached_open(data->state);
1847 state = nfs4_opendata_find_nfs4_state(data);
1851 if (data->o_res.delegation_type != 0)
1852 nfs4_opendata_check_deleg(data, state);
1853 update_open_stateid(state, &data->o_res.stateid, NULL,
1856 nfs_release_seqid(data->o_arg.seqid);
1860 static struct nfs4_state *
1861 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1863 struct nfs4_state *ret;
1865 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1866 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1868 ret = _nfs4_opendata_to_nfs4_state(data);
1869 nfs4_sequence_free_slot(&data->o_res.seq_res);
1873 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1875 struct nfs_inode *nfsi = NFS_I(state->inode);
1876 struct nfs_open_context *ctx;
1878 spin_lock(&state->inode->i_lock);
1879 list_for_each_entry(ctx, &nfsi->open_files, list) {
1880 if (ctx->state != state)
1882 get_nfs_open_context(ctx);
1883 spin_unlock(&state->inode->i_lock);
1886 spin_unlock(&state->inode->i_lock);
1887 return ERR_PTR(-ENOENT);
1890 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1891 struct nfs4_state *state, enum open_claim_type4 claim)
1893 struct nfs4_opendata *opendata;
1895 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1896 NULL, claim, GFP_NOFS);
1897 if (opendata == NULL)
1898 return ERR_PTR(-ENOMEM);
1899 opendata->state = state;
1900 atomic_inc(&state->count);
1904 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1907 struct nfs4_state *newstate;
1910 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1912 opendata->o_arg.open_flags = 0;
1913 opendata->o_arg.fmode = fmode;
1914 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1915 NFS_SB(opendata->dentry->d_sb),
1917 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1918 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1919 nfs4_init_opendata_res(opendata);
1920 ret = _nfs4_recover_proc_open(opendata);
1923 newstate = nfs4_opendata_to_nfs4_state(opendata);
1924 if (IS_ERR(newstate))
1925 return PTR_ERR(newstate);
1926 if (newstate != opendata->state)
1928 nfs4_close_state(newstate, fmode);
1932 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1936 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1937 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1938 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1939 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1940 /* memory barrier prior to reading state->n_* */
1941 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1942 clear_bit(NFS_OPEN_STATE, &state->flags);
1944 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1947 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1950 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1954 * We may have performed cached opens for all three recoveries.
1955 * Check if we need to update the current stateid.
1957 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1958 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1959 write_seqlock(&state->seqlock);
1960 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1961 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1962 write_sequnlock(&state->seqlock);
1969 * reclaim state on the server after a reboot.
1971 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1973 struct nfs_delegation *delegation;
1974 struct nfs4_opendata *opendata;
1975 fmode_t delegation_type = 0;
1978 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1979 NFS4_OPEN_CLAIM_PREVIOUS);
1980 if (IS_ERR(opendata))
1981 return PTR_ERR(opendata);
1983 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1984 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1985 delegation_type = delegation->type;
1987 opendata->o_arg.u.delegation_type = delegation_type;
1988 status = nfs4_open_recover(opendata, state);
1989 nfs4_opendata_put(opendata);
1993 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1995 struct nfs_server *server = NFS_SERVER(state->inode);
1996 struct nfs4_exception exception = { };
1999 err = _nfs4_do_open_reclaim(ctx, state);
2000 trace_nfs4_open_reclaim(ctx, 0, err);
2001 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2003 if (err != -NFS4ERR_DELAY)
2005 nfs4_handle_exception(server, err, &exception);
2006 } while (exception.retry);
2010 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2012 struct nfs_open_context *ctx;
2015 ctx = nfs4_state_find_open_context(state);
2018 ret = nfs4_do_open_reclaim(ctx, state);
2019 put_nfs_open_context(ctx);
2023 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
2027 printk(KERN_ERR "NFS: %s: unhandled error "
2028 "%d.\n", __func__, err);
2034 case -NFS4ERR_BADSESSION:
2035 case -NFS4ERR_BADSLOT:
2036 case -NFS4ERR_BAD_HIGH_SLOT:
2037 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2038 case -NFS4ERR_DEADSESSION:
2039 set_bit(NFS_DELEGATED_STATE, &state->flags);
2040 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2042 case -NFS4ERR_STALE_CLIENTID:
2043 case -NFS4ERR_STALE_STATEID:
2044 set_bit(NFS_DELEGATED_STATE, &state->flags);
2045 /* Don't recall a delegation if it was lost */
2046 nfs4_schedule_lease_recovery(server->nfs_client);
2048 case -NFS4ERR_MOVED:
2049 nfs4_schedule_migration_recovery(server);
2051 case -NFS4ERR_LEASE_MOVED:
2052 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2054 case -NFS4ERR_DELEG_REVOKED:
2055 case -NFS4ERR_ADMIN_REVOKED:
2056 case -NFS4ERR_EXPIRED:
2057 case -NFS4ERR_BAD_STATEID:
2058 case -NFS4ERR_OPENMODE:
2059 nfs_inode_find_state_and_recover(state->inode,
2061 nfs4_schedule_stateid_recovery(server, state);
2063 case -NFS4ERR_DELAY:
2064 case -NFS4ERR_GRACE:
2065 set_bit(NFS_DELEGATED_STATE, &state->flags);
2069 case -NFS4ERR_DENIED:
2070 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2076 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2077 struct nfs4_state *state, const nfs4_stateid *stateid,
2080 struct nfs_server *server = NFS_SERVER(state->inode);
2081 struct nfs4_opendata *opendata;
2084 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2085 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2086 if (IS_ERR(opendata))
2087 return PTR_ERR(opendata);
2088 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2089 write_seqlock(&state->seqlock);
2090 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2091 write_sequnlock(&state->seqlock);
2092 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2093 switch (type & (FMODE_READ|FMODE_WRITE)) {
2094 case FMODE_READ|FMODE_WRITE:
2096 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2099 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2103 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2105 nfs4_opendata_put(opendata);
2106 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2109 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2111 struct nfs4_opendata *data = calldata;
2113 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2114 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2117 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2119 struct nfs4_opendata *data = calldata;
2121 nfs40_sequence_done(task, &data->c_res.seq_res);
2123 data->rpc_status = task->tk_status;
2124 if (data->rpc_status == 0) {
2125 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2126 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2127 renew_lease(data->o_res.server, data->timestamp);
2128 data->rpc_done = true;
2132 static void nfs4_open_confirm_release(void *calldata)
2134 struct nfs4_opendata *data = calldata;
2135 struct nfs4_state *state = NULL;
2137 /* If this request hasn't been cancelled, do nothing */
2138 if (!data->cancelled)
2140 /* In case of error, no cleanup! */
2141 if (!data->rpc_done)
2143 state = nfs4_opendata_to_nfs4_state(data);
2145 nfs4_close_state(state, data->o_arg.fmode);
2147 nfs4_opendata_put(data);
2150 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2151 .rpc_call_prepare = nfs4_open_confirm_prepare,
2152 .rpc_call_done = nfs4_open_confirm_done,
2153 .rpc_release = nfs4_open_confirm_release,
2157 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2159 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2161 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2162 struct rpc_task *task;
2163 struct rpc_message msg = {
2164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2165 .rpc_argp = &data->c_arg,
2166 .rpc_resp = &data->c_res,
2167 .rpc_cred = data->owner->so_cred,
2169 struct rpc_task_setup task_setup_data = {
2170 .rpc_client = server->client,
2171 .rpc_message = &msg,
2172 .callback_ops = &nfs4_open_confirm_ops,
2173 .callback_data = data,
2174 .workqueue = nfsiod_workqueue,
2175 .flags = RPC_TASK_ASYNC,
2179 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2180 kref_get(&data->kref);
2181 data->rpc_done = false;
2182 data->rpc_status = 0;
2183 data->timestamp = jiffies;
2184 if (data->is_recover)
2185 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2186 task = rpc_run_task(&task_setup_data);
2188 return PTR_ERR(task);
2189 status = rpc_wait_for_completion_task(task);
2191 data->cancelled = true;
2194 status = data->rpc_status;
2199 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2201 struct nfs4_opendata *data = calldata;
2202 struct nfs4_state_owner *sp = data->owner;
2203 struct nfs_client *clp = sp->so_server->nfs_client;
2204 enum open_claim_type4 claim = data->o_arg.claim;
2206 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2209 * Check if we still need to send an OPEN call, or if we can use
2210 * a delegation instead.
2212 if (data->state != NULL) {
2213 struct nfs_delegation *delegation;
2215 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2218 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2219 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2220 goto unlock_no_action;
2223 /* Update client id. */
2224 data->o_arg.clientid = clp->cl_clientid;
2228 case NFS4_OPEN_CLAIM_PREVIOUS:
2229 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2230 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2231 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2232 case NFS4_OPEN_CLAIM_FH:
2233 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2235 data->timestamp = jiffies;
2236 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2237 &data->o_arg.seq_args,
2238 &data->o_res.seq_res,
2240 nfs_release_seqid(data->o_arg.seqid);
2242 /* Set the create mode (note dependency on the session type) */
2243 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2244 if (data->o_arg.open_flags & O_EXCL) {
2245 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2246 if (nfs4_has_persistent_session(clp))
2247 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2248 else if (clp->cl_mvops->minor_version > 0)
2249 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2253 trace_nfs4_cached_open(data->state);
2256 task->tk_action = NULL;
2258 nfs4_sequence_done(task, &data->o_res.seq_res);
2261 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2263 struct nfs4_opendata *data = calldata;
2265 data->rpc_status = task->tk_status;
2267 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2270 if (task->tk_status == 0) {
2271 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2272 switch (data->o_res.f_attr->mode & S_IFMT) {
2276 data->rpc_status = -ELOOP;
2279 data->rpc_status = -EISDIR;
2282 data->rpc_status = -ENOTDIR;
2285 renew_lease(data->o_res.server, data->timestamp);
2286 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2287 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2289 data->rpc_done = true;
2292 static void nfs4_open_release(void *calldata)
2294 struct nfs4_opendata *data = calldata;
2295 struct nfs4_state *state = NULL;
2297 /* If this request hasn't been cancelled, do nothing */
2298 if (!data->cancelled)
2300 /* In case of error, no cleanup! */
2301 if (data->rpc_status != 0 || !data->rpc_done)
2303 /* In case we need an open_confirm, no cleanup! */
2304 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2306 state = nfs4_opendata_to_nfs4_state(data);
2308 nfs4_close_state(state, data->o_arg.fmode);
2310 nfs4_opendata_put(data);
2313 static const struct rpc_call_ops nfs4_open_ops = {
2314 .rpc_call_prepare = nfs4_open_prepare,
2315 .rpc_call_done = nfs4_open_done,
2316 .rpc_release = nfs4_open_release,
2319 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2321 struct inode *dir = d_inode(data->dir);
2322 struct nfs_server *server = NFS_SERVER(dir);
2323 struct nfs_openargs *o_arg = &data->o_arg;
2324 struct nfs_openres *o_res = &data->o_res;
2325 struct rpc_task *task;
2326 struct rpc_message msg = {
2327 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2330 .rpc_cred = data->owner->so_cred,
2332 struct rpc_task_setup task_setup_data = {
2333 .rpc_client = server->client,
2334 .rpc_message = &msg,
2335 .callback_ops = &nfs4_open_ops,
2336 .callback_data = data,
2337 .workqueue = nfsiod_workqueue,
2338 .flags = RPC_TASK_ASYNC,
2342 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2343 kref_get(&data->kref);
2344 data->rpc_done = false;
2345 data->rpc_status = 0;
2346 data->cancelled = false;
2347 data->is_recover = false;
2349 nfs4_set_sequence_privileged(&o_arg->seq_args);
2350 data->is_recover = true;
2352 task = rpc_run_task(&task_setup_data);
2354 return PTR_ERR(task);
2355 status = rpc_wait_for_completion_task(task);
2357 data->cancelled = true;
2360 status = data->rpc_status;
2366 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2368 struct inode *dir = d_inode(data->dir);
2369 struct nfs_openres *o_res = &data->o_res;
2372 status = nfs4_run_open_task(data, 1);
2373 if (status != 0 || !data->rpc_done)
2376 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2378 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2379 status = _nfs4_proc_open_confirm(data);
2385 * Additional permission checks in order to distinguish between an
2386 * open for read, and an open for execute. This works around the
2387 * fact that NFSv4 OPEN treats read and execute permissions as being
2389 * Note that in the non-execute case, we want to turn off permission
2390 * checking if we just created a new file (POSIX open() semantics).
2392 static int nfs4_opendata_access(struct rpc_cred *cred,
2393 struct nfs4_opendata *opendata,
2394 struct nfs4_state *state, fmode_t fmode,
2397 struct nfs_access_entry cache;
2400 /* access call failed or for some reason the server doesn't
2401 * support any access modes -- defer access call until later */
2402 if (opendata->o_res.access_supported == 0)
2407 * Use openflags to check for exec, because fmode won't
2408 * always have FMODE_EXEC set when file open for exec.
2410 if (openflags & __FMODE_EXEC) {
2411 /* ONLY check for exec rights */
2412 if (S_ISDIR(state->inode->i_mode))
2413 mask = NFS4_ACCESS_LOOKUP;
2415 mask = NFS4_ACCESS_EXECUTE;
2416 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2417 mask = NFS4_ACCESS_READ;
2420 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2421 nfs_access_add_cache(state->inode, &cache);
2423 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2424 if ((mask & ~cache.mask & flags) == 0)
2431 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2433 static int _nfs4_proc_open(struct nfs4_opendata *data)
2435 struct inode *dir = d_inode(data->dir);
2436 struct nfs_server *server = NFS_SERVER(dir);
2437 struct nfs_openargs *o_arg = &data->o_arg;
2438 struct nfs_openres *o_res = &data->o_res;
2441 status = nfs4_run_open_task(data, 0);
2442 if (!data->rpc_done)
2445 if (status == -NFS4ERR_BADNAME &&
2446 !(o_arg->open_flags & O_CREAT))
2451 nfs_fattr_map_and_free_names(server, &data->f_attr);
2453 if (o_arg->open_flags & O_CREAT) {
2454 if (o_arg->open_flags & O_EXCL)
2455 data->file_created = true;
2456 else if (o_res->cinfo.before != o_res->cinfo.after)
2457 data->file_created = true;
2458 if (data->file_created ||
2459 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2460 update_changeattr(dir, &o_res->cinfo,
2461 o_res->f_attr->time_start);
2463 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2464 server->caps &= ~NFS_CAP_POSIX_LOCK;
2465 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2466 status = _nfs4_proc_open_confirm(data);
2470 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2471 nfs4_sequence_free_slot(&o_res->seq_res);
2472 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2479 * reclaim state on the server after a network partition.
2480 * Assumes caller holds the appropriate lock
2482 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2484 struct nfs4_opendata *opendata;
2487 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2488 NFS4_OPEN_CLAIM_FH);
2489 if (IS_ERR(opendata))
2490 return PTR_ERR(opendata);
2491 ret = nfs4_open_recover(opendata, state);
2493 d_drop(ctx->dentry);
2494 nfs4_opendata_put(opendata);
2498 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2500 struct nfs_server *server = NFS_SERVER(state->inode);
2501 struct nfs4_exception exception = { };
2505 err = _nfs4_open_expired(ctx, state);
2506 trace_nfs4_open_expired(ctx, 0, err);
2507 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2512 case -NFS4ERR_GRACE:
2513 case -NFS4ERR_DELAY:
2514 nfs4_handle_exception(server, err, &exception);
2517 } while (exception.retry);
2522 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2524 struct nfs_open_context *ctx;
2527 ctx = nfs4_state_find_open_context(state);
2530 ret = nfs4_do_open_expired(ctx, state);
2531 put_nfs_open_context(ctx);
2535 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2536 const nfs4_stateid *stateid)
2538 nfs_remove_bad_delegation(state->inode, stateid);
2539 write_seqlock(&state->seqlock);
2540 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2541 write_sequnlock(&state->seqlock);
2542 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2545 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2547 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2548 nfs_finish_clear_delegation_stateid(state, NULL);
2551 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2553 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2554 nfs40_clear_delegation_stateid(state);
2555 return nfs4_open_expired(sp, state);
2558 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2559 nfs4_stateid *stateid,
2560 struct rpc_cred *cred)
2562 return -NFS4ERR_BAD_STATEID;
2565 #if defined(CONFIG_NFS_V4_1)
2566 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2567 nfs4_stateid *stateid,
2568 struct rpc_cred *cred)
2572 switch (stateid->type) {
2575 case NFS4_INVALID_STATEID_TYPE:
2576 case NFS4_SPECIAL_STATEID_TYPE:
2577 return -NFS4ERR_BAD_STATEID;
2578 case NFS4_REVOKED_STATEID_TYPE:
2582 status = nfs41_test_stateid(server, stateid, cred);
2584 case -NFS4ERR_EXPIRED:
2585 case -NFS4ERR_ADMIN_REVOKED:
2586 case -NFS4ERR_DELEG_REVOKED:
2592 /* Ack the revoked state to the server */
2593 nfs41_free_stateid(server, stateid, cred, true);
2594 return -NFS4ERR_EXPIRED;
2597 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2599 struct nfs_server *server = NFS_SERVER(state->inode);
2600 nfs4_stateid stateid;
2601 struct nfs_delegation *delegation;
2602 struct rpc_cred *cred;
2605 /* Get the delegation credential for use by test/free_stateid */
2607 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2608 if (delegation == NULL) {
2613 nfs4_stateid_copy(&stateid, &delegation->stateid);
2614 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2615 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2616 &delegation->flags)) {
2618 nfs_finish_clear_delegation_stateid(state, &stateid);
2622 cred = get_rpccred(delegation->cred);
2624 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2625 trace_nfs4_test_delegation_stateid(state, NULL, status);
2626 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2627 nfs_finish_clear_delegation_stateid(state, &stateid);
2633 * nfs41_check_expired_locks - possibly free a lock stateid
2635 * @state: NFSv4 state for an inode
2637 * Returns NFS_OK if recovery for this stateid is now finished.
2638 * Otherwise a negative NFS4ERR value is returned.
2640 static int nfs41_check_expired_locks(struct nfs4_state *state)
2642 int status, ret = NFS_OK;
2643 struct nfs4_lock_state *lsp, *prev = NULL;
2644 struct nfs_server *server = NFS_SERVER(state->inode);
2646 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2649 spin_lock(&state->state_lock);
2650 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2651 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2652 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2654 refcount_inc(&lsp->ls_count);
2655 spin_unlock(&state->state_lock);
2657 nfs4_put_lock_state(prev);
2660 status = nfs41_test_and_free_expired_stateid(server,
2663 trace_nfs4_test_lock_stateid(state, lsp, status);
2664 if (status == -NFS4ERR_EXPIRED ||
2665 status == -NFS4ERR_BAD_STATEID) {
2666 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2667 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2668 if (!recover_lost_locks)
2669 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2670 } else if (status != NFS_OK) {
2672 nfs4_put_lock_state(prev);
2675 spin_lock(&state->state_lock);
2678 spin_unlock(&state->state_lock);
2679 nfs4_put_lock_state(prev);
2685 * nfs41_check_open_stateid - possibly free an open stateid
2687 * @state: NFSv4 state for an inode
2689 * Returns NFS_OK if recovery for this stateid is now finished.
2690 * Otherwise a negative NFS4ERR value is returned.
2692 static int nfs41_check_open_stateid(struct nfs4_state *state)
2694 struct nfs_server *server = NFS_SERVER(state->inode);
2695 nfs4_stateid *stateid = &state->open_stateid;
2696 struct rpc_cred *cred = state->owner->so_cred;
2699 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2700 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2701 if (nfs4_have_delegation(state->inode, state->state))
2703 return -NFS4ERR_OPENMODE;
2705 return -NFS4ERR_BAD_STATEID;
2707 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2708 trace_nfs4_test_open_stateid(state, NULL, status);
2709 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2710 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2711 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2712 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2713 clear_bit(NFS_OPEN_STATE, &state->flags);
2714 stateid->type = NFS4_INVALID_STATEID_TYPE;
2717 if (nfs_open_stateid_recover_openmode(state))
2718 return -NFS4ERR_OPENMODE;
2722 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2726 nfs41_check_delegation_stateid(state);
2727 status = nfs41_check_expired_locks(state);
2728 if (status != NFS_OK)
2730 status = nfs41_check_open_stateid(state);
2731 if (status != NFS_OK)
2732 status = nfs4_open_expired(sp, state);
2738 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2739 * fields corresponding to attributes that were used to store the verifier.
2740 * Make sure we clobber those fields in the later setattr call
2742 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2743 struct iattr *sattr, struct nfs4_label **label)
2745 const u32 *attrset = opendata->o_res.attrset;
2747 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2748 !(sattr->ia_valid & ATTR_ATIME_SET))
2749 sattr->ia_valid |= ATTR_ATIME;
2751 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2752 !(sattr->ia_valid & ATTR_MTIME_SET))
2753 sattr->ia_valid |= ATTR_MTIME;
2755 /* Except MODE, it seems harmless of setting twice. */
2756 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2757 (attrset[1] & FATTR4_WORD1_MODE ||
2758 attrset[2] & FATTR4_WORD2_MODE_UMASK))
2759 sattr->ia_valid &= ~ATTR_MODE;
2761 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2765 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2768 struct nfs_open_context *ctx)
2770 struct nfs4_state_owner *sp = opendata->owner;
2771 struct nfs_server *server = sp->so_server;
2772 struct dentry *dentry;
2773 struct nfs4_state *state;
2777 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2779 ret = _nfs4_proc_open(opendata);
2783 state = nfs4_opendata_to_nfs4_state(opendata);
2784 ret = PTR_ERR(state);
2788 if (server->caps & NFS_CAP_POSIX_LOCK)
2789 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2790 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2791 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2793 dentry = opendata->dentry;
2794 if (d_really_is_negative(dentry)) {
2795 struct dentry *alias;
2797 alias = d_exact_alias(dentry, state->inode);
2799 alias = d_splice_alias(igrab(state->inode), dentry);
2800 /* d_splice_alias() can't fail here - it's a non-directory */
2803 ctx->dentry = dentry = alias;
2805 nfs_set_verifier(dentry,
2806 nfs_save_change_attribute(d_inode(opendata->dir)));
2809 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2813 if (d_inode(dentry) == state->inode) {
2814 nfs_inode_attach_open_context(ctx);
2815 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2816 nfs4_schedule_stateid_recovery(server, state);
2823 * Returns a referenced nfs4_state
2825 static int _nfs4_do_open(struct inode *dir,
2826 struct nfs_open_context *ctx,
2828 const struct nfs4_open_createattrs *c,
2831 struct nfs4_state_owner *sp;
2832 struct nfs4_state *state = NULL;
2833 struct nfs_server *server = NFS_SERVER(dir);
2834 struct nfs4_opendata *opendata;
2835 struct dentry *dentry = ctx->dentry;
2836 struct rpc_cred *cred = ctx->cred;
2837 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2838 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2839 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2840 struct iattr *sattr = c->sattr;
2841 struct nfs4_label *label = c->label;
2842 struct nfs4_label *olabel = NULL;
2845 /* Protect against reboot recovery conflicts */
2847 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2849 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2852 status = nfs4_client_recover_expired_lease(server->nfs_client);
2854 goto err_put_state_owner;
2855 if (d_really_is_positive(dentry))
2856 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2858 if (d_really_is_positive(dentry))
2859 claim = NFS4_OPEN_CLAIM_FH;
2860 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2861 c, claim, GFP_KERNEL);
2862 if (opendata == NULL)
2863 goto err_put_state_owner;
2866 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2867 if (IS_ERR(olabel)) {
2868 status = PTR_ERR(olabel);
2869 goto err_opendata_put;
2873 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2874 if (!opendata->f_attr.mdsthreshold) {
2875 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2876 if (!opendata->f_attr.mdsthreshold)
2877 goto err_free_label;
2879 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2881 if (d_really_is_positive(dentry))
2882 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2884 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2886 goto err_free_label;
2889 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2890 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2891 nfs4_exclusive_attrset(opendata, sattr, &label);
2893 * send create attributes which was not set by open
2894 * with an extra setattr.
2896 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2897 nfs_fattr_init(opendata->o_res.f_attr);
2898 status = nfs4_do_setattr(state->inode, cred,
2899 opendata->o_res.f_attr, sattr,
2900 ctx, label, olabel);
2902 nfs_setattr_update_inode(state->inode, sattr,
2903 opendata->o_res.f_attr);
2904 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2908 if (opened && opendata->file_created)
2909 *opened |= FILE_CREATED;
2911 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2912 *ctx_th = opendata->f_attr.mdsthreshold;
2913 opendata->f_attr.mdsthreshold = NULL;
2916 nfs4_label_free(olabel);
2918 nfs4_opendata_put(opendata);
2919 nfs4_put_state_owner(sp);
2922 nfs4_label_free(olabel);
2924 nfs4_opendata_put(opendata);
2925 err_put_state_owner:
2926 nfs4_put_state_owner(sp);
2932 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2933 struct nfs_open_context *ctx,
2935 struct iattr *sattr,
2936 struct nfs4_label *label,
2939 struct nfs_server *server = NFS_SERVER(dir);
2940 struct nfs4_exception exception = { };
2941 struct nfs4_state *res;
2942 struct nfs4_open_createattrs c = {
2946 [0] = (__u32)jiffies,
2947 [1] = (__u32)current->pid,
2953 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
2955 trace_nfs4_open_file(ctx, flags, status);
2958 /* NOTE: BAD_SEQID means the server and client disagree about the
2959 * book-keeping w.r.t. state-changing operations
2960 * (OPEN/CLOSE/LOCK/LOCKU...)
2961 * It is actually a sign of a bug on the client or on the server.
2963 * If we receive a BAD_SEQID error in the particular case of
2964 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2965 * have unhashed the old state_owner for us, and that we can
2966 * therefore safely retry using a new one. We should still warn
2967 * the user though...
2969 if (status == -NFS4ERR_BAD_SEQID) {
2970 pr_warn_ratelimited("NFS: v4 server %s "
2971 " returned a bad sequence-id error!\n",
2972 NFS_SERVER(dir)->nfs_client->cl_hostname);
2973 exception.retry = 1;
2977 * BAD_STATEID on OPEN means that the server cancelled our
2978 * state before it received the OPEN_CONFIRM.
2979 * Recover by retrying the request as per the discussion
2980 * on Page 181 of RFC3530.
2982 if (status == -NFS4ERR_BAD_STATEID) {
2983 exception.retry = 1;
2986 if (status == -EAGAIN) {
2987 /* We must have found a delegation */
2988 exception.retry = 1;
2991 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2993 res = ERR_PTR(nfs4_handle_exception(server,
2994 status, &exception));
2995 } while (exception.retry);
2999 static int _nfs4_do_setattr(struct inode *inode,
3000 struct nfs_setattrargs *arg,
3001 struct nfs_setattrres *res,
3002 struct rpc_cred *cred,
3003 struct nfs_open_context *ctx)
3005 struct nfs_server *server = NFS_SERVER(inode);
3006 struct rpc_message msg = {
3007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3012 struct rpc_cred *delegation_cred = NULL;
3013 unsigned long timestamp = jiffies;
3018 nfs_fattr_init(res->fattr);
3020 /* Servers should only apply open mode checks for file size changes */
3021 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3022 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3024 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3025 /* Use that stateid */
3026 } else if (truncate && ctx != NULL) {
3027 struct nfs_lock_context *l_ctx;
3028 if (!nfs4_valid_open_stateid(ctx->state))
3030 l_ctx = nfs_get_lock_context(ctx);
3032 return PTR_ERR(l_ctx);
3033 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3034 &arg->stateid, &delegation_cred);
3035 nfs_put_lock_context(l_ctx);
3039 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3040 if (delegation_cred)
3041 msg.rpc_cred = delegation_cred;
3043 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3045 put_rpccred(delegation_cred);
3046 if (status == 0 && ctx != NULL)
3047 renew_lease(server, timestamp);
3048 trace_nfs4_setattr(inode, &arg->stateid, status);
3052 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3053 struct nfs_fattr *fattr, struct iattr *sattr,
3054 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3055 struct nfs4_label *olabel)
3057 struct nfs_server *server = NFS_SERVER(inode);
3058 struct nfs4_state *state = ctx ? ctx->state : NULL;
3059 struct nfs_setattrargs arg = {
3060 .fh = NFS_FH(inode),
3063 .bitmask = server->attr_bitmask,
3066 struct nfs_setattrres res = {
3071 struct nfs4_exception exception = {
3074 .stateid = &arg.stateid,
3078 arg.bitmask = nfs4_bitmask(server, ilabel);
3080 arg.bitmask = nfs4_bitmask(server, olabel);
3083 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3085 case -NFS4ERR_OPENMODE:
3086 if (!(sattr->ia_valid & ATTR_SIZE)) {
3087 pr_warn_once("NFSv4: server %s is incorrectly "
3088 "applying open mode checks to "
3089 "a SETATTR that is not "
3090 "changing file size.\n",
3091 server->nfs_client->cl_hostname);
3093 if (state && !(state->state & FMODE_WRITE)) {
3095 if (sattr->ia_valid & ATTR_OPEN)
3100 err = nfs4_handle_exception(server, err, &exception);
3101 } while (exception.retry);
3107 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3109 if (inode == NULL || !nfs_have_layout(inode))
3112 return pnfs_wait_on_layoutreturn(inode, task);
3115 struct nfs4_closedata {
3116 struct inode *inode;
3117 struct nfs4_state *state;
3118 struct nfs_closeargs arg;
3119 struct nfs_closeres res;
3121 struct nfs4_layoutreturn_args arg;
3122 struct nfs4_layoutreturn_res res;
3123 struct nfs4_xdr_opaque_data ld_private;
3127 struct nfs_fattr fattr;
3128 unsigned long timestamp;
3131 static void nfs4_free_closedata(void *data)
3133 struct nfs4_closedata *calldata = data;
3134 struct nfs4_state_owner *sp = calldata->state->owner;
3135 struct super_block *sb = calldata->state->inode->i_sb;
3137 if (calldata->lr.roc)
3138 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3139 calldata->res.lr_ret);
3140 nfs4_put_open_state(calldata->state);
3141 nfs_free_seqid(calldata->arg.seqid);
3142 nfs4_put_state_owner(sp);
3143 nfs_sb_deactive(sb);
3147 static void nfs4_close_done(struct rpc_task *task, void *data)
3149 struct nfs4_closedata *calldata = data;
3150 struct nfs4_state *state = calldata->state;
3151 struct nfs_server *server = NFS_SERVER(calldata->inode);
3152 nfs4_stateid *res_stateid = NULL;
3154 dprintk("%s: begin!\n", __func__);
3155 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3157 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3159 /* Handle Layoutreturn errors */
3160 if (calldata->arg.lr_args && task->tk_status != 0) {
3161 switch (calldata->res.lr_ret) {
3163 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3166 calldata->arg.lr_args = NULL;
3167 calldata->res.lr_res = NULL;
3169 case -NFS4ERR_OLD_STATEID:
3170 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
3174 case -NFS4ERR_ADMIN_REVOKED:
3175 case -NFS4ERR_DELEG_REVOKED:
3176 case -NFS4ERR_EXPIRED:
3177 case -NFS4ERR_BAD_STATEID:
3178 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3179 case -NFS4ERR_WRONG_CRED:
3180 calldata->arg.lr_args = NULL;
3181 calldata->res.lr_res = NULL;
3186 /* hmm. we are done with the inode, and in the process of freeing
3187 * the state_owner. we keep this around to process errors
3189 switch (task->tk_status) {
3191 res_stateid = &calldata->res.stateid;
3192 renew_lease(server, calldata->timestamp);
3194 case -NFS4ERR_ACCESS:
3195 if (calldata->arg.bitmask != NULL) {
3196 calldata->arg.bitmask = NULL;
3197 calldata->res.fattr = NULL;
3202 case -NFS4ERR_OLD_STATEID:
3203 /* Did we race with OPEN? */
3204 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3208 case -NFS4ERR_ADMIN_REVOKED:
3209 case -NFS4ERR_STALE_STATEID:
3210 case -NFS4ERR_EXPIRED:
3211 nfs4_free_revoked_stateid(server,
3212 &calldata->arg.stateid,
3213 task->tk_msg.rpc_cred);
3215 case -NFS4ERR_BAD_STATEID:
3218 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
3221 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3222 res_stateid, calldata->arg.fmode);
3224 task->tk_status = 0;
3225 nfs_release_seqid(calldata->arg.seqid);
3226 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3227 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3230 calldata->res.lr_ret = 0;
3232 task->tk_status = 0;
3233 rpc_restart_call_prepare(task);
3237 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3239 struct nfs4_closedata *calldata = data;
3240 struct nfs4_state *state = calldata->state;
3241 struct inode *inode = calldata->inode;
3242 bool is_rdonly, is_wronly, is_rdwr;
3245 dprintk("%s: begin!\n", __func__);
3246 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3249 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3250 spin_lock(&state->owner->so_lock);
3251 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3252 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3253 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3254 /* Calculate the change in open mode */
3255 calldata->arg.fmode = 0;
3256 if (state->n_rdwr == 0) {
3257 if (state->n_rdonly == 0)
3258 call_close |= is_rdonly;
3260 calldata->arg.fmode |= FMODE_READ;
3261 if (state->n_wronly == 0)
3262 call_close |= is_wronly;
3264 calldata->arg.fmode |= FMODE_WRITE;
3265 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3266 call_close |= is_rdwr;
3268 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3270 if (!nfs4_valid_open_stateid(state) ||
3271 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3273 spin_unlock(&state->owner->so_lock);
3276 /* Note: exit _without_ calling nfs4_close_done */
3280 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3281 nfs_release_seqid(calldata->arg.seqid);
3285 if (calldata->arg.fmode == 0)
3286 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3288 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3289 /* Close-to-open cache consistency revalidation */
3290 if (!nfs4_have_delegation(inode, FMODE_READ))
3291 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3293 calldata->arg.bitmask = NULL;
3296 calldata->arg.share_access =
3297 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3298 calldata->arg.fmode, 0);
3300 if (calldata->res.fattr == NULL)
3301 calldata->arg.bitmask = NULL;
3302 else if (calldata->arg.bitmask == NULL)
3303 calldata->res.fattr = NULL;
3304 calldata->timestamp = jiffies;
3305 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3306 &calldata->arg.seq_args,
3307 &calldata->res.seq_res,
3309 nfs_release_seqid(calldata->arg.seqid);
3310 dprintk("%s: done!\n", __func__);
3313 task->tk_action = NULL;
3315 nfs4_sequence_done(task, &calldata->res.seq_res);
3318 static const struct rpc_call_ops nfs4_close_ops = {
3319 .rpc_call_prepare = nfs4_close_prepare,
3320 .rpc_call_done = nfs4_close_done,
3321 .rpc_release = nfs4_free_closedata,
3325 * It is possible for data to be read/written from a mem-mapped file
3326 * after the sys_close call (which hits the vfs layer as a flush).
3327 * This means that we can't safely call nfsv4 close on a file until
3328 * the inode is cleared. This in turn means that we are not good
3329 * NFSv4 citizens - we do not indicate to the server to update the file's
3330 * share state even when we are done with one of the three share
3331 * stateid's in the inode.
3333 * NOTE: Caller must be holding the sp->so_owner semaphore!
3335 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3337 struct nfs_server *server = NFS_SERVER(state->inode);
3338 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3339 struct nfs4_closedata *calldata;
3340 struct nfs4_state_owner *sp = state->owner;
3341 struct rpc_task *task;
3342 struct rpc_message msg = {
3343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3344 .rpc_cred = state->owner->so_cred,
3346 struct rpc_task_setup task_setup_data = {
3347 .rpc_client = server->client,
3348 .rpc_message = &msg,
3349 .callback_ops = &nfs4_close_ops,
3350 .workqueue = nfsiod_workqueue,
3351 .flags = RPC_TASK_ASYNC,
3353 int status = -ENOMEM;
3355 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3356 &task_setup_data.rpc_client, &msg);
3358 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3359 if (calldata == NULL)
3361 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3362 calldata->inode = state->inode;
3363 calldata->state = state;
3364 calldata->arg.fh = NFS_FH(state->inode);
3365 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3366 goto out_free_calldata;
3367 /* Serialization for the sequence id */
3368 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3369 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3370 if (IS_ERR(calldata->arg.seqid))
3371 goto out_free_calldata;
3372 nfs_fattr_init(&calldata->fattr);
3373 calldata->arg.fmode = 0;
3374 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3375 calldata->res.fattr = &calldata->fattr;
3376 calldata->res.seqid = calldata->arg.seqid;
3377 calldata->res.server = server;
3378 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3379 calldata->lr.roc = pnfs_roc(state->inode,
3380 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3381 if (calldata->lr.roc) {
3382 calldata->arg.lr_args = &calldata->lr.arg;
3383 calldata->res.lr_res = &calldata->lr.res;
3385 nfs_sb_active(calldata->inode->i_sb);
3387 msg.rpc_argp = &calldata->arg;
3388 msg.rpc_resp = &calldata->res;
3389 task_setup_data.callback_data = calldata;
3390 task = rpc_run_task(&task_setup_data);
3392 return PTR_ERR(task);
3395 status = rpc_wait_for_completion_task(task);
3401 nfs4_put_open_state(state);
3402 nfs4_put_state_owner(sp);
3406 static struct inode *
3407 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3408 int open_flags, struct iattr *attr, int *opened)
3410 struct nfs4_state *state;
3411 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3413 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3415 /* Protect against concurrent sillydeletes */
3416 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3418 nfs4_label_release_security(label);
3421 return ERR_CAST(state);
3422 return state->inode;
3425 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3427 if (ctx->state == NULL)
3430 nfs4_close_sync(ctx->state, ctx->mode);
3432 nfs4_close_state(ctx->state, ctx->mode);
3435 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3436 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3437 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3439 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3441 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3442 struct nfs4_server_caps_arg args = {
3446 struct nfs4_server_caps_res res = {};
3447 struct rpc_message msg = {
3448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3455 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3456 FATTR4_WORD0_FH_EXPIRE_TYPE |
3457 FATTR4_WORD0_LINK_SUPPORT |
3458 FATTR4_WORD0_SYMLINK_SUPPORT |
3459 FATTR4_WORD0_ACLSUPPORT;
3461 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3463 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3465 /* Sanity check the server answers */
3466 switch (minorversion) {
3468 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3469 res.attr_bitmask[2] = 0;
3472 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3475 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3477 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3478 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3479 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3480 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3481 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3482 NFS_CAP_CTIME|NFS_CAP_MTIME|
3483 NFS_CAP_SECURITY_LABEL);
3484 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3485 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3486 server->caps |= NFS_CAP_ACLS;
3487 if (res.has_links != 0)
3488 server->caps |= NFS_CAP_HARDLINKS;
3489 if (res.has_symlinks != 0)
3490 server->caps |= NFS_CAP_SYMLINKS;
3491 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3492 server->caps |= NFS_CAP_FILEID;
3493 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3494 server->caps |= NFS_CAP_MODE;
3495 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3496 server->caps |= NFS_CAP_NLINK;
3497 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3498 server->caps |= NFS_CAP_OWNER;
3499 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3500 server->caps |= NFS_CAP_OWNER_GROUP;
3501 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3502 server->caps |= NFS_CAP_ATIME;
3503 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3504 server->caps |= NFS_CAP_CTIME;
3505 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3506 server->caps |= NFS_CAP_MTIME;
3507 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3508 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3509 server->caps |= NFS_CAP_SECURITY_LABEL;
3511 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3512 sizeof(server->attr_bitmask));
3513 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3515 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3516 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3517 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3518 server->cache_consistency_bitmask[2] = 0;
3520 /* Avoid a regression due to buggy server */
3521 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3522 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3523 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3524 sizeof(server->exclcreat_bitmask));
3526 server->acl_bitmask = res.acl_bitmask;
3527 server->fh_expire_type = res.fh_expire_type;
3533 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3535 struct nfs4_exception exception = { };
3538 err = nfs4_handle_exception(server,
3539 _nfs4_server_capabilities(server, fhandle),
3541 } while (exception.retry);
3545 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3546 struct nfs_fsinfo *info)
3549 struct nfs4_lookup_root_arg args = {
3552 struct nfs4_lookup_res res = {
3554 .fattr = info->fattr,
3557 struct rpc_message msg = {
3558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3563 bitmask[0] = nfs4_fattr_bitmap[0];
3564 bitmask[1] = nfs4_fattr_bitmap[1];
3566 * Process the label in the upcoming getfattr
3568 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3570 nfs_fattr_init(info->fattr);
3571 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3574 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3575 struct nfs_fsinfo *info)
3577 struct nfs4_exception exception = { };
3580 err = _nfs4_lookup_root(server, fhandle, info);
3581 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3584 case -NFS4ERR_WRONGSEC:
3587 err = nfs4_handle_exception(server, err, &exception);
3589 } while (exception.retry);
3594 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3595 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3597 struct rpc_auth_create_args auth_args = {
3598 .pseudoflavor = flavor,
3600 struct rpc_auth *auth;
3602 auth = rpcauth_create(&auth_args, server->client);
3605 return nfs4_lookup_root(server, fhandle, info);
3609 * Retry pseudoroot lookup with various security flavors. We do this when:
3611 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3612 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3614 * Returns zero on success, or a negative NFS4ERR value, or a
3615 * negative errno value.
3617 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3618 struct nfs_fsinfo *info)
3620 /* Per 3530bis 15.33.5 */
3621 static const rpc_authflavor_t flav_array[] = {
3625 RPC_AUTH_UNIX, /* courtesy */
3628 int status = -EPERM;
3631 if (server->auth_info.flavor_len > 0) {
3632 /* try each flavor specified by user */
3633 for (i = 0; i < server->auth_info.flavor_len; i++) {
3634 status = nfs4_lookup_root_sec(server, fhandle, info,
3635 server->auth_info.flavors[i]);
3636 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3641 /* no flavors specified by user, try default list */
3642 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3643 status = nfs4_lookup_root_sec(server, fhandle, info,
3645 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3652 * -EACCESS could mean that the user doesn't have correct permissions
3653 * to access the mount. It could also mean that we tried to mount
3654 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3655 * existing mount programs don't handle -EACCES very well so it should
3656 * be mapped to -EPERM instead.
3658 if (status == -EACCES)
3664 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3665 * @server: initialized nfs_server handle
3666 * @fhandle: we fill in the pseudo-fs root file handle
3667 * @info: we fill in an FSINFO struct
3668 * @auth_probe: probe the auth flavours
3670 * Returns zero on success, or a negative errno.
3672 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3673 struct nfs_fsinfo *info,
3679 status = nfs4_lookup_root(server, fhandle, info);
3681 if (auth_probe || status == NFS4ERR_WRONGSEC)
3682 status = server->nfs_client->cl_mvops->find_root_sec(server,
3686 status = nfs4_server_capabilities(server, fhandle);
3688 status = nfs4_do_fsinfo(server, fhandle, info);
3690 return nfs4_map_errors(status);
3693 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3694 struct nfs_fsinfo *info)
3697 struct nfs_fattr *fattr = info->fattr;
3698 struct nfs4_label *label = NULL;
3700 error = nfs4_server_capabilities(server, mntfh);
3702 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3706 label = nfs4_label_alloc(server, GFP_KERNEL);
3708 return PTR_ERR(label);
3710 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3712 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3713 goto err_free_label;
3716 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3717 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3718 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3721 nfs4_label_free(label);
3727 * Get locations and (maybe) other attributes of a referral.
3728 * Note that we'll actually follow the referral later when
3729 * we detect fsid mismatch in inode revalidation
3731 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3732 const struct qstr *name, struct nfs_fattr *fattr,
3733 struct nfs_fh *fhandle)
3735 int status = -ENOMEM;
3736 struct page *page = NULL;
3737 struct nfs4_fs_locations *locations = NULL;
3739 page = alloc_page(GFP_KERNEL);
3742 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3743 if (locations == NULL)
3746 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3751 * If the fsid didn't change, this is a migration event, not a
3752 * referral. Cause us to drop into the exception handler, which
3753 * will kick off migration recovery.
3755 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3756 dprintk("%s: server did not return a different fsid for"
3757 " a referral at %s\n", __func__, name->name);
3758 status = -NFS4ERR_MOVED;
3761 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3762 nfs_fixup_referral_attributes(&locations->fattr);
3764 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3765 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3766 memset(fhandle, 0, sizeof(struct nfs_fh));
3774 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3775 struct nfs_fattr *fattr, struct nfs4_label *label)
3777 struct nfs4_getattr_arg args = {
3779 .bitmask = server->attr_bitmask,
3781 struct nfs4_getattr_res res = {
3786 struct rpc_message msg = {
3787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3792 args.bitmask = nfs4_bitmask(server, label);
3794 nfs_fattr_init(fattr);
3795 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3798 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3799 struct nfs_fattr *fattr, struct nfs4_label *label)
3801 struct nfs4_exception exception = { };
3804 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3805 trace_nfs4_getattr(server, fhandle, fattr, err);
3806 err = nfs4_handle_exception(server, err,
3808 } while (exception.retry);
3813 * The file is not closed if it is opened due to the a request to change
3814 * the size of the file. The open call will not be needed once the
3815 * VFS layer lookup-intents are implemented.
3817 * Close is called when the inode is destroyed.
3818 * If we haven't opened the file for O_WRONLY, we
3819 * need to in the size_change case to obtain a stateid.
3822 * Because OPEN is always done by name in nfsv4, it is
3823 * possible that we opened a different file by the same
3824 * name. We can recognize this race condition, but we
3825 * can't do anything about it besides returning an error.
3827 * This will be fixed with VFS changes (lookup-intent).
3830 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3831 struct iattr *sattr)
3833 struct inode *inode = d_inode(dentry);
3834 struct rpc_cred *cred = NULL;
3835 struct nfs_open_context *ctx = NULL;
3836 struct nfs4_label *label = NULL;
3839 if (pnfs_ld_layoutret_on_setattr(inode) &&
3840 sattr->ia_valid & ATTR_SIZE &&
3841 sattr->ia_size < i_size_read(inode))
3842 pnfs_commit_and_return_layout(inode);
3844 nfs_fattr_init(fattr);
3846 /* Deal with open(O_TRUNC) */
3847 if (sattr->ia_valid & ATTR_OPEN)
3848 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3850 /* Optimization: if the end result is no change, don't RPC */
3851 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3854 /* Search for an existing open(O_WRITE) file */
3855 if (sattr->ia_valid & ATTR_FILE) {
3857 ctx = nfs_file_open_context(sattr->ia_file);
3862 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3864 return PTR_ERR(label);
3866 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3868 nfs_setattr_update_inode(inode, sattr, fattr);
3869 nfs_setsecurity(inode, fattr, label);
3871 nfs4_label_free(label);
3875 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3876 const struct qstr *name, struct nfs_fh *fhandle,
3877 struct nfs_fattr *fattr, struct nfs4_label *label)
3879 struct nfs_server *server = NFS_SERVER(dir);
3881 struct nfs4_lookup_arg args = {
3882 .bitmask = server->attr_bitmask,
3883 .dir_fh = NFS_FH(dir),
3886 struct nfs4_lookup_res res = {
3892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3898 args.bitmask = nfs4_bitmask(server, label);
3900 nfs_fattr_init(fattr);
3902 dprintk("NFS call lookup %s\n", name->name);
3903 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3904 dprintk("NFS reply lookup: %d\n", status);
3908 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3910 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3911 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3912 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3916 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3917 const struct qstr *name, struct nfs_fh *fhandle,
3918 struct nfs_fattr *fattr, struct nfs4_label *label)
3920 struct nfs4_exception exception = { };
3921 struct rpc_clnt *client = *clnt;
3924 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3925 trace_nfs4_lookup(dir, name, err);
3927 case -NFS4ERR_BADNAME:
3930 case -NFS4ERR_MOVED:
3931 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3932 if (err == -NFS4ERR_MOVED)
3933 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3935 case -NFS4ERR_WRONGSEC:
3937 if (client != *clnt)
3939 client = nfs4_negotiate_security(client, dir, name);
3941 return PTR_ERR(client);
3943 exception.retry = 1;
3946 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3948 } while (exception.retry);
3953 else if (client != *clnt)
3954 rpc_shutdown_client(client);
3959 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3960 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3961 struct nfs4_label *label)
3964 struct rpc_clnt *client = NFS_CLIENT(dir);
3966 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3967 if (client != NFS_CLIENT(dir)) {
3968 rpc_shutdown_client(client);
3969 nfs_fixup_secinfo_attributes(fattr);
3975 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3976 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3978 struct rpc_clnt *client = NFS_CLIENT(dir);
3981 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3983 return ERR_PTR(status);
3984 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3987 static int _nfs4_proc_lookupp(struct inode *inode,
3988 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3989 struct nfs4_label *label)
3991 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3992 struct nfs_server *server = NFS_SERVER(inode);
3994 struct nfs4_lookupp_arg args = {
3995 .bitmask = server->attr_bitmask,
3996 .fh = NFS_FH(inode),
3998 struct nfs4_lookupp_res res = {
4004 struct rpc_message msg = {
4005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4010 args.bitmask = nfs4_bitmask(server, label);
4012 nfs_fattr_init(fattr);
4014 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4015 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4017 dprintk("NFS reply lookupp: %d\n", status);
4021 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4022 struct nfs_fattr *fattr, struct nfs4_label *label)
4024 struct nfs4_exception exception = { };
4027 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4028 trace_nfs4_lookupp(inode, err);
4029 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4031 } while (exception.retry);
4035 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4037 struct nfs_server *server = NFS_SERVER(inode);
4038 struct nfs4_accessargs args = {
4039 .fh = NFS_FH(inode),
4040 .bitmask = server->cache_consistency_bitmask,
4041 .access = entry->mask,
4043 struct nfs4_accessres res = {
4046 struct rpc_message msg = {
4047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4050 .rpc_cred = entry->cred,
4054 res.fattr = nfs_alloc_fattr();
4055 if (res.fattr == NULL)
4058 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4060 nfs_access_set_mask(entry, res.access);
4061 nfs_refresh_inode(inode, res.fattr);
4063 nfs_free_fattr(res.fattr);
4067 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4069 struct nfs4_exception exception = { };
4072 err = _nfs4_proc_access(inode, entry);
4073 trace_nfs4_access(inode, err);
4074 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4076 } while (exception.retry);
4081 * TODO: For the time being, we don't try to get any attributes
4082 * along with any of the zero-copy operations READ, READDIR,
4085 * In the case of the first three, we want to put the GETATTR
4086 * after the read-type operation -- this is because it is hard
4087 * to predict the length of a GETATTR response in v4, and thus
4088 * align the READ data correctly. This means that the GETATTR
4089 * may end up partially falling into the page cache, and we should
4090 * shift it into the 'tail' of the xdr_buf before processing.
4091 * To do this efficiently, we need to know the total length
4092 * of data received, which doesn't seem to be available outside
4095 * In the case of WRITE, we also want to put the GETATTR after
4096 * the operation -- in this case because we want to make sure
4097 * we get the post-operation mtime and size.
4099 * Both of these changes to the XDR layer would in fact be quite
4100 * minor, but I decided to leave them for a subsequent patch.
4102 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4103 unsigned int pgbase, unsigned int pglen)
4105 struct nfs4_readlink args = {
4106 .fh = NFS_FH(inode),
4111 struct nfs4_readlink_res res;
4112 struct rpc_message msg = {
4113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4118 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4121 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4122 unsigned int pgbase, unsigned int pglen)
4124 struct nfs4_exception exception = { };
4127 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4128 trace_nfs4_readlink(inode, err);
4129 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4131 } while (exception.retry);
4136 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4139 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4142 struct nfs_server *server = NFS_SERVER(dir);
4143 struct nfs4_label l, *ilabel = NULL;
4144 struct nfs_open_context *ctx;
4145 struct nfs4_state *state;
4148 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4150 return PTR_ERR(ctx);
4152 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4154 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4155 sattr->ia_mode &= ~current_umask();
4156 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4157 if (IS_ERR(state)) {
4158 status = PTR_ERR(state);
4162 nfs4_label_release_security(ilabel);
4163 put_nfs_open_context(ctx);
4167 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4169 struct nfs_server *server = NFS_SERVER(dir);
4170 struct nfs_removeargs args = {
4174 struct nfs_removeres res = {
4177 struct rpc_message msg = {
4178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4182 unsigned long timestamp = jiffies;
4185 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4187 update_changeattr(dir, &res.cinfo, timestamp);
4191 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4193 struct nfs4_exception exception = { };
4196 err = _nfs4_proc_remove(dir, name);
4197 trace_nfs4_remove(dir, name, err);
4198 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4200 } while (exception.retry);
4204 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4206 struct nfs_server *server = NFS_SERVER(dir);
4207 struct nfs_removeargs *args = msg->rpc_argp;
4208 struct nfs_removeres *res = msg->rpc_resp;
4210 res->server = server;
4211 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4212 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4214 nfs_fattr_init(res->dir_attr);
4217 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4219 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4220 &data->args.seq_args,
4225 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4227 struct nfs_unlinkdata *data = task->tk_calldata;
4228 struct nfs_removeres *res = &data->res;
4230 if (!nfs4_sequence_done(task, &res->seq_res))
4232 if (nfs4_async_handle_error(task, res->server, NULL,
4233 &data->timeout) == -EAGAIN)
4235 if (task->tk_status == 0)
4236 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
4240 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4242 struct nfs_server *server = NFS_SERVER(dir);
4243 struct nfs_renameargs *arg = msg->rpc_argp;
4244 struct nfs_renameres *res = msg->rpc_resp;
4246 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4247 res->server = server;
4248 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4251 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4253 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4254 &data->args.seq_args,
4259 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4260 struct inode *new_dir)
4262 struct nfs_renamedata *data = task->tk_calldata;
4263 struct nfs_renameres *res = &data->res;
4265 if (!nfs4_sequence_done(task, &res->seq_res))
4267 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4270 if (task->tk_status == 0) {
4271 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
4272 if (new_dir != old_dir)
4273 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
4278 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4280 struct nfs_server *server = NFS_SERVER(inode);
4281 struct nfs4_link_arg arg = {
4282 .fh = NFS_FH(inode),
4283 .dir_fh = NFS_FH(dir),
4285 .bitmask = server->attr_bitmask,
4287 struct nfs4_link_res res = {
4291 struct rpc_message msg = {
4292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4296 int status = -ENOMEM;
4298 res.fattr = nfs_alloc_fattr();
4299 if (res.fattr == NULL)
4302 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4303 if (IS_ERR(res.label)) {
4304 status = PTR_ERR(res.label);
4307 arg.bitmask = nfs4_bitmask(server, res.label);
4309 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4311 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
4312 status = nfs_post_op_update_inode(inode, res.fattr);
4314 nfs_setsecurity(inode, res.fattr, res.label);
4318 nfs4_label_free(res.label);
4321 nfs_free_fattr(res.fattr);
4325 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4327 struct nfs4_exception exception = { };
4330 err = nfs4_handle_exception(NFS_SERVER(inode),
4331 _nfs4_proc_link(inode, dir, name),
4333 } while (exception.retry);
4337 struct nfs4_createdata {
4338 struct rpc_message msg;
4339 struct nfs4_create_arg arg;
4340 struct nfs4_create_res res;
4342 struct nfs_fattr fattr;
4343 struct nfs4_label *label;
4346 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4347 const struct qstr *name, struct iattr *sattr, u32 ftype)
4349 struct nfs4_createdata *data;
4351 data = kzalloc(sizeof(*data), GFP_KERNEL);
4353 struct nfs_server *server = NFS_SERVER(dir);
4355 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4356 if (IS_ERR(data->label))
4359 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4360 data->msg.rpc_argp = &data->arg;
4361 data->msg.rpc_resp = &data->res;
4362 data->arg.dir_fh = NFS_FH(dir);
4363 data->arg.server = server;
4364 data->arg.name = name;
4365 data->arg.attrs = sattr;
4366 data->arg.ftype = ftype;
4367 data->arg.bitmask = nfs4_bitmask(server, data->label);
4368 data->arg.umask = current_umask();
4369 data->res.server = server;
4370 data->res.fh = &data->fh;
4371 data->res.fattr = &data->fattr;
4372 data->res.label = data->label;
4373 nfs_fattr_init(data->res.fattr);
4381 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4383 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4384 &data->arg.seq_args, &data->res.seq_res, 1);
4386 update_changeattr(dir, &data->res.dir_cinfo,
4387 data->res.fattr->time_start);
4388 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4393 static void nfs4_free_createdata(struct nfs4_createdata *data)
4395 nfs4_label_free(data->label);
4399 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4400 struct page *page, unsigned int len, struct iattr *sattr,
4401 struct nfs4_label *label)
4403 struct nfs4_createdata *data;
4404 int status = -ENAMETOOLONG;
4406 if (len > NFS4_MAXPATHLEN)
4410 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4414 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4415 data->arg.u.symlink.pages = &page;
4416 data->arg.u.symlink.len = len;
4417 data->arg.label = label;
4419 status = nfs4_do_create(dir, dentry, data);
4421 nfs4_free_createdata(data);
4426 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4427 struct page *page, unsigned int len, struct iattr *sattr)
4429 struct nfs4_exception exception = { };
4430 struct nfs4_label l, *label = NULL;
4433 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4436 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4437 trace_nfs4_symlink(dir, &dentry->d_name, err);
4438 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4440 } while (exception.retry);
4442 nfs4_label_release_security(label);
4446 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4447 struct iattr *sattr, struct nfs4_label *label)
4449 struct nfs4_createdata *data;
4450 int status = -ENOMEM;
4452 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4456 data->arg.label = label;
4457 status = nfs4_do_create(dir, dentry, data);
4459 nfs4_free_createdata(data);
4464 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4465 struct iattr *sattr)
4467 struct nfs_server *server = NFS_SERVER(dir);
4468 struct nfs4_exception exception = { };
4469 struct nfs4_label l, *label = NULL;
4472 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4474 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4475 sattr->ia_mode &= ~current_umask();
4477 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4478 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4479 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4481 } while (exception.retry);
4482 nfs4_label_release_security(label);
4487 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4488 u64 cookie, struct page **pages, unsigned int count, bool plus)
4490 struct inode *dir = d_inode(dentry);
4491 struct nfs4_readdir_arg args = {
4496 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4499 struct nfs4_readdir_res res;
4500 struct rpc_message msg = {
4501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4508 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4510 (unsigned long long)cookie);
4511 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4512 res.pgbase = args.pgbase;
4513 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4515 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4516 status += args.pgbase;
4519 nfs_invalidate_atime(dir);
4521 dprintk("%s: returns %d\n", __func__, status);
4525 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4526 u64 cookie, struct page **pages, unsigned int count, bool plus)
4528 struct nfs4_exception exception = { };
4531 err = _nfs4_proc_readdir(dentry, cred, cookie,
4532 pages, count, plus);
4533 trace_nfs4_readdir(d_inode(dentry), err);
4534 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4536 } while (exception.retry);
4540 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4541 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4543 struct nfs4_createdata *data;
4544 int mode = sattr->ia_mode;
4545 int status = -ENOMEM;
4547 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4552 data->arg.ftype = NF4FIFO;
4553 else if (S_ISBLK(mode)) {
4554 data->arg.ftype = NF4BLK;
4555 data->arg.u.device.specdata1 = MAJOR(rdev);
4556 data->arg.u.device.specdata2 = MINOR(rdev);
4558 else if (S_ISCHR(mode)) {
4559 data->arg.ftype = NF4CHR;
4560 data->arg.u.device.specdata1 = MAJOR(rdev);
4561 data->arg.u.device.specdata2 = MINOR(rdev);
4562 } else if (!S_ISSOCK(mode)) {
4567 data->arg.label = label;
4568 status = nfs4_do_create(dir, dentry, data);
4570 nfs4_free_createdata(data);
4575 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4576 struct iattr *sattr, dev_t rdev)
4578 struct nfs_server *server = NFS_SERVER(dir);
4579 struct nfs4_exception exception = { };
4580 struct nfs4_label l, *label = NULL;
4583 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4585 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4586 sattr->ia_mode &= ~current_umask();
4588 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4589 trace_nfs4_mknod(dir, &dentry->d_name, err);
4590 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4592 } while (exception.retry);
4594 nfs4_label_release_security(label);
4599 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4600 struct nfs_fsstat *fsstat)
4602 struct nfs4_statfs_arg args = {
4604 .bitmask = server->attr_bitmask,
4606 struct nfs4_statfs_res res = {
4609 struct rpc_message msg = {
4610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4615 nfs_fattr_init(fsstat->fattr);
4616 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4619 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4621 struct nfs4_exception exception = { };
4624 err = nfs4_handle_exception(server,
4625 _nfs4_proc_statfs(server, fhandle, fsstat),
4627 } while (exception.retry);
4631 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4632 struct nfs_fsinfo *fsinfo)
4634 struct nfs4_fsinfo_arg args = {
4636 .bitmask = server->attr_bitmask,
4638 struct nfs4_fsinfo_res res = {
4641 struct rpc_message msg = {
4642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4647 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4650 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4652 struct nfs4_exception exception = { };
4653 unsigned long now = jiffies;
4657 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4658 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4660 nfs4_set_lease_period(server->nfs_client,
4661 fsinfo->lease_time * HZ,
4665 err = nfs4_handle_exception(server, err, &exception);
4666 } while (exception.retry);
4670 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4674 nfs_fattr_init(fsinfo->fattr);
4675 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4677 /* block layout checks this! */
4678 server->pnfs_blksize = fsinfo->blksize;
4679 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4685 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4686 struct nfs_pathconf *pathconf)
4688 struct nfs4_pathconf_arg args = {
4690 .bitmask = server->attr_bitmask,
4692 struct nfs4_pathconf_res res = {
4693 .pathconf = pathconf,
4695 struct rpc_message msg = {
4696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4701 /* None of the pathconf attributes are mandatory to implement */
4702 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4703 memset(pathconf, 0, sizeof(*pathconf));
4707 nfs_fattr_init(pathconf->fattr);
4708 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4711 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4712 struct nfs_pathconf *pathconf)
4714 struct nfs4_exception exception = { };
4718 err = nfs4_handle_exception(server,
4719 _nfs4_proc_pathconf(server, fhandle, pathconf),
4721 } while (exception.retry);
4725 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4726 const struct nfs_open_context *ctx,
4727 const struct nfs_lock_context *l_ctx,
4730 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4732 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4734 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4735 const struct nfs_open_context *ctx,
4736 const struct nfs_lock_context *l_ctx,
4739 nfs4_stateid current_stateid;
4741 /* If the current stateid represents a lost lock, then exit */
4742 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
4744 return nfs4_stateid_match(stateid, ¤t_stateid);
4747 static bool nfs4_error_stateid_expired(int err)
4750 case -NFS4ERR_DELEG_REVOKED:
4751 case -NFS4ERR_ADMIN_REVOKED:
4752 case -NFS4ERR_BAD_STATEID:
4753 case -NFS4ERR_STALE_STATEID:
4754 case -NFS4ERR_OLD_STATEID:
4755 case -NFS4ERR_OPENMODE:
4756 case -NFS4ERR_EXPIRED:
4762 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4764 struct nfs_server *server = NFS_SERVER(hdr->inode);
4766 trace_nfs4_read(hdr, task->tk_status);
4767 if (task->tk_status < 0) {
4768 struct nfs4_exception exception = {
4769 .inode = hdr->inode,
4770 .state = hdr->args.context->state,
4771 .stateid = &hdr->args.stateid,
4773 task->tk_status = nfs4_async_handle_exception(task,
4774 server, task->tk_status, &exception);
4775 if (exception.retry) {
4776 rpc_restart_call_prepare(task);
4781 if (task->tk_status > 0)
4782 renew_lease(server, hdr->timestamp);
4786 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4787 struct nfs_pgio_args *args)
4790 if (!nfs4_error_stateid_expired(task->tk_status) ||
4791 nfs4_stateid_is_current(&args->stateid,
4796 rpc_restart_call_prepare(task);
4800 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4803 dprintk("--> %s\n", __func__);
4805 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4807 if (nfs4_read_stateid_changed(task, &hdr->args))
4809 if (task->tk_status > 0)
4810 nfs_invalidate_atime(hdr->inode);
4811 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4812 nfs4_read_done_cb(task, hdr);
4815 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4816 struct rpc_message *msg)
4818 hdr->timestamp = jiffies;
4819 if (!hdr->pgio_done_cb)
4820 hdr->pgio_done_cb = nfs4_read_done_cb;
4821 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4822 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4825 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4826 struct nfs_pgio_header *hdr)
4828 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
4829 &hdr->args.seq_args,
4833 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4834 hdr->args.lock_context,
4835 hdr->rw_mode) == -EIO)
4837 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4842 static int nfs4_write_done_cb(struct rpc_task *task,
4843 struct nfs_pgio_header *hdr)
4845 struct inode *inode = hdr->inode;
4847 trace_nfs4_write(hdr, task->tk_status);
4848 if (task->tk_status < 0) {
4849 struct nfs4_exception exception = {
4850 .inode = hdr->inode,
4851 .state = hdr->args.context->state,
4852 .stateid = &hdr->args.stateid,
4854 task->tk_status = nfs4_async_handle_exception(task,
4855 NFS_SERVER(inode), task->tk_status,
4857 if (exception.retry) {
4858 rpc_restart_call_prepare(task);
4862 if (task->tk_status >= 0) {
4863 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4864 nfs_writeback_update_inode(hdr);
4869 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4870 struct nfs_pgio_args *args)
4873 if (!nfs4_error_stateid_expired(task->tk_status) ||
4874 nfs4_stateid_is_current(&args->stateid,
4879 rpc_restart_call_prepare(task);
4883 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4885 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4887 if (nfs4_write_stateid_changed(task, &hdr->args))
4889 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4890 nfs4_write_done_cb(task, hdr);
4894 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4896 /* Don't request attributes for pNFS or O_DIRECT writes */
4897 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4899 /* Otherwise, request attributes if and only if we don't hold
4902 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4905 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4906 struct rpc_message *msg)
4908 struct nfs_server *server = NFS_SERVER(hdr->inode);
4910 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4911 hdr->args.bitmask = NULL;
4912 hdr->res.fattr = NULL;
4914 hdr->args.bitmask = server->cache_consistency_bitmask;
4916 if (!hdr->pgio_done_cb)
4917 hdr->pgio_done_cb = nfs4_write_done_cb;
4918 hdr->res.server = server;
4919 hdr->timestamp = jiffies;
4921 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4922 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4925 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4927 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
4928 &data->args.seq_args,
4933 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4935 struct inode *inode = data->inode;
4937 trace_nfs4_commit(data, task->tk_status);
4938 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4939 NULL, NULL) == -EAGAIN) {
4940 rpc_restart_call_prepare(task);
4946 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4948 if (!nfs4_sequence_done(task, &data->res.seq_res))
4950 return data->commit_done_cb(task, data);
4953 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4955 struct nfs_server *server = NFS_SERVER(data->inode);
4957 if (data->commit_done_cb == NULL)
4958 data->commit_done_cb = nfs4_commit_done_cb;
4959 data->res.server = server;
4960 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4961 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4964 struct nfs4_renewdata {
4965 struct nfs_client *client;
4966 unsigned long timestamp;
4970 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4971 * standalone procedure for queueing an asynchronous RENEW.
4973 static void nfs4_renew_release(void *calldata)
4975 struct nfs4_renewdata *data = calldata;
4976 struct nfs_client *clp = data->client;
4978 if (refcount_read(&clp->cl_count) > 1)
4979 nfs4_schedule_state_renewal(clp);
4980 nfs_put_client(clp);
4984 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4986 struct nfs4_renewdata *data = calldata;
4987 struct nfs_client *clp = data->client;
4988 unsigned long timestamp = data->timestamp;
4990 trace_nfs4_renew_async(clp, task->tk_status);
4991 switch (task->tk_status) {
4994 case -NFS4ERR_LEASE_MOVED:
4995 nfs4_schedule_lease_moved_recovery(clp);
4998 /* Unless we're shutting down, schedule state recovery! */
4999 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5001 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5002 nfs4_schedule_lease_recovery(clp);
5005 nfs4_schedule_path_down_recovery(clp);
5007 do_renew_lease(clp, timestamp);
5010 static const struct rpc_call_ops nfs4_renew_ops = {
5011 .rpc_call_done = nfs4_renew_done,
5012 .rpc_release = nfs4_renew_release,
5015 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5017 struct rpc_message msg = {
5018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5022 struct nfs4_renewdata *data;
5024 if (renew_flags == 0)
5026 if (!refcount_inc_not_zero(&clp->cl_count))
5028 data = kmalloc(sizeof(*data), GFP_NOFS);
5030 nfs_put_client(clp);
5034 data->timestamp = jiffies;
5035 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5036 &nfs4_renew_ops, data);
5039 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
5041 struct rpc_message msg = {
5042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5046 unsigned long now = jiffies;
5049 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5052 do_renew_lease(clp, now);
5056 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5058 return server->caps & NFS_CAP_ACLS;
5061 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5062 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5065 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5067 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5068 struct page **pages)
5070 struct page *newpage, **spages;
5076 len = min_t(size_t, PAGE_SIZE, buflen);
5077 newpage = alloc_page(GFP_KERNEL);
5079 if (newpage == NULL)
5081 memcpy(page_address(newpage), buf, len);
5086 } while (buflen != 0);
5092 __free_page(spages[rc-1]);
5096 struct nfs4_cached_acl {
5102 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5104 struct nfs_inode *nfsi = NFS_I(inode);
5106 spin_lock(&inode->i_lock);
5107 kfree(nfsi->nfs4_acl);
5108 nfsi->nfs4_acl = acl;
5109 spin_unlock(&inode->i_lock);
5112 static void nfs4_zap_acl_attr(struct inode *inode)
5114 nfs4_set_cached_acl(inode, NULL);
5117 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5119 struct nfs_inode *nfsi = NFS_I(inode);
5120 struct nfs4_cached_acl *acl;
5123 spin_lock(&inode->i_lock);
5124 acl = nfsi->nfs4_acl;
5127 if (buf == NULL) /* user is just asking for length */
5129 if (acl->cached == 0)
5131 ret = -ERANGE; /* see getxattr(2) man page */
5132 if (acl->len > buflen)
5134 memcpy(buf, acl->data, acl->len);
5138 spin_unlock(&inode->i_lock);
5142 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5144 struct nfs4_cached_acl *acl;
5145 size_t buflen = sizeof(*acl) + acl_len;
5147 if (buflen <= PAGE_SIZE) {
5148 acl = kmalloc(buflen, GFP_KERNEL);
5152 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5154 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5161 nfs4_set_cached_acl(inode, acl);
5165 * The getxattr API returns the required buffer length when called with a
5166 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5167 * the required buf. On a NULL buf, we send a page of data to the server
5168 * guessing that the ACL request can be serviced by a page. If so, we cache
5169 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5170 * the cache. If not so, we throw away the page, and cache the required
5171 * length. The next getxattr call will then produce another round trip to
5172 * the server, this time with the input buf of the required size.
5174 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5176 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5177 struct nfs_getaclargs args = {
5178 .fh = NFS_FH(inode),
5182 struct nfs_getaclres res = {
5185 struct rpc_message msg = {
5186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5190 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5191 int ret = -ENOMEM, i;
5193 if (npages > ARRAY_SIZE(pages))
5196 for (i = 0; i < npages; i++) {
5197 pages[i] = alloc_page(GFP_KERNEL);
5202 /* for decoding across pages */
5203 res.acl_scratch = alloc_page(GFP_KERNEL);
5204 if (!res.acl_scratch)
5207 args.acl_len = npages * PAGE_SIZE;
5209 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5210 __func__, buf, buflen, npages, args.acl_len);
5211 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5212 &msg, &args.seq_args, &res.seq_res, 0);
5216 /* Handle the case where the passed-in buffer is too short */
5217 if (res.acl_flags & NFS4_ACL_TRUNC) {
5218 /* Did the user only issue a request for the acl length? */
5224 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5226 if (res.acl_len > buflen) {
5230 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5235 for (i = 0; i < npages; i++)
5237 __free_page(pages[i]);
5238 if (res.acl_scratch)
5239 __free_page(res.acl_scratch);
5243 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5245 struct nfs4_exception exception = { };
5248 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5249 trace_nfs4_get_acl(inode, ret);
5252 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5253 } while (exception.retry);
5257 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5259 struct nfs_server *server = NFS_SERVER(inode);
5262 if (!nfs4_server_supports_acls(server))
5264 ret = nfs_revalidate_inode(server, inode);
5267 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5268 nfs_zap_acl_cache(inode);
5269 ret = nfs4_read_cached_acl(inode, buf, buflen);
5271 /* -ENOENT is returned if there is no ACL or if there is an ACL
5272 * but no cached acl data, just the acl length */
5274 return nfs4_get_acl_uncached(inode, buf, buflen);
5277 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5279 struct nfs_server *server = NFS_SERVER(inode);
5280 struct page *pages[NFS4ACL_MAXPAGES];
5281 struct nfs_setaclargs arg = {
5282 .fh = NFS_FH(inode),
5286 struct nfs_setaclres res;
5287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5292 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5295 if (!nfs4_server_supports_acls(server))
5297 if (npages > ARRAY_SIZE(pages))
5299 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5302 nfs4_inode_return_delegation(inode);
5303 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5306 * Free each page after tx, so the only ref left is
5307 * held by the network stack
5310 put_page(pages[i-1]);
5313 * Acl update can result in inode attribute update.
5314 * so mark the attribute cache invalid.
5316 spin_lock(&inode->i_lock);
5317 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5318 spin_unlock(&inode->i_lock);
5319 nfs_access_zap_cache(inode);
5320 nfs_zap_acl_cache(inode);
5324 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5326 struct nfs4_exception exception = { };
5329 err = __nfs4_proc_set_acl(inode, buf, buflen);
5330 trace_nfs4_set_acl(inode, err);
5331 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5333 } while (exception.retry);
5337 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5338 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5341 struct nfs_server *server = NFS_SERVER(inode);
5342 struct nfs_fattr fattr;
5343 struct nfs4_label label = {0, 0, buflen, buf};
5345 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5346 struct nfs4_getattr_arg arg = {
5347 .fh = NFS_FH(inode),
5350 struct nfs4_getattr_res res = {
5355 struct rpc_message msg = {
5356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5362 nfs_fattr_init(&fattr);
5364 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5367 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5369 if (buflen < label.len)
5374 static int nfs4_get_security_label(struct inode *inode, void *buf,
5377 struct nfs4_exception exception = { };
5380 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5384 err = _nfs4_get_security_label(inode, buf, buflen);
5385 trace_nfs4_get_security_label(inode, err);
5386 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5388 } while (exception.retry);
5392 static int _nfs4_do_set_security_label(struct inode *inode,
5393 struct nfs4_label *ilabel,
5394 struct nfs_fattr *fattr,
5395 struct nfs4_label *olabel)
5398 struct iattr sattr = {0};
5399 struct nfs_server *server = NFS_SERVER(inode);
5400 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5401 struct nfs_setattrargs arg = {
5402 .fh = NFS_FH(inode),
5408 struct nfs_setattrres res = {
5413 struct rpc_message msg = {
5414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5420 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5422 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5424 dprintk("%s failed: %d\n", __func__, status);
5429 static int nfs4_do_set_security_label(struct inode *inode,
5430 struct nfs4_label *ilabel,
5431 struct nfs_fattr *fattr,
5432 struct nfs4_label *olabel)
5434 struct nfs4_exception exception = { };
5438 err = _nfs4_do_set_security_label(inode, ilabel,
5440 trace_nfs4_set_security_label(inode, err);
5441 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5443 } while (exception.retry);
5448 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5450 struct nfs4_label ilabel, *olabel = NULL;
5451 struct nfs_fattr fattr;
5452 struct rpc_cred *cred;
5455 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5458 nfs_fattr_init(&fattr);
5462 ilabel.label = (char *)buf;
5463 ilabel.len = buflen;
5465 cred = rpc_lookup_cred();
5467 return PTR_ERR(cred);
5469 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5470 if (IS_ERR(olabel)) {
5471 status = -PTR_ERR(olabel);
5475 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5477 nfs_setsecurity(inode, &fattr, olabel);
5479 nfs4_label_free(olabel);
5484 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5487 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5488 nfs4_verifier *bootverf)
5492 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5493 /* An impossible timestamp guarantees this value
5494 * will never match a generated boot time. */
5495 verf[0] = cpu_to_be32(U32_MAX);
5496 verf[1] = cpu_to_be32(U32_MAX);
5498 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5499 u64 ns = ktime_to_ns(nn->boot_time);
5501 verf[0] = cpu_to_be32(ns >> 32);
5502 verf[1] = cpu_to_be32(ns);
5504 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5508 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5513 if (clp->cl_owner_id != NULL)
5517 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5518 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5520 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5524 if (len > NFS4_OPAQUE_LIMIT + 1)
5528 * Since this string is allocated at mount time, and held until the
5529 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5530 * about a memory-reclaim deadlock.
5532 str = kmalloc(len, GFP_KERNEL);
5537 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5539 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5540 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5543 clp->cl_owner_id = str;
5548 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5553 len = 10 + 10 + 1 + 10 + 1 +
5554 strlen(nfs4_client_id_uniquifier) + 1 +
5555 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5557 if (len > NFS4_OPAQUE_LIMIT + 1)
5561 * Since this string is allocated at mount time, and held until the
5562 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5563 * about a memory-reclaim deadlock.
5565 str = kmalloc(len, GFP_KERNEL);
5569 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5570 clp->rpc_ops->version, clp->cl_minorversion,
5571 nfs4_client_id_uniquifier,
5572 clp->cl_rpcclient->cl_nodename);
5573 clp->cl_owner_id = str;
5578 nfs4_init_uniform_client_string(struct nfs_client *clp)
5583 if (clp->cl_owner_id != NULL)
5586 if (nfs4_client_id_uniquifier[0] != '\0')
5587 return nfs4_init_uniquifier_client_string(clp);
5589 len = 10 + 10 + 1 + 10 + 1 +
5590 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5592 if (len > NFS4_OPAQUE_LIMIT + 1)
5596 * Since this string is allocated at mount time, and held until the
5597 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5598 * about a memory-reclaim deadlock.
5600 str = kmalloc(len, GFP_KERNEL);
5604 scnprintf(str, len, "Linux NFSv%u.%u %s",
5605 clp->rpc_ops->version, clp->cl_minorversion,
5606 clp->cl_rpcclient->cl_nodename);
5607 clp->cl_owner_id = str;
5612 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5613 * services. Advertise one based on the address family of the
5617 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5619 if (strchr(clp->cl_ipaddr, ':') != NULL)
5620 return scnprintf(buf, len, "tcp6");
5622 return scnprintf(buf, len, "tcp");
5625 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5627 struct nfs4_setclientid *sc = calldata;
5629 if (task->tk_status == 0)
5630 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5633 static const struct rpc_call_ops nfs4_setclientid_ops = {
5634 .rpc_call_done = nfs4_setclientid_done,
5638 * nfs4_proc_setclientid - Negotiate client ID
5639 * @clp: state data structure
5640 * @program: RPC program for NFSv4 callback service
5641 * @port: IP port number for NFS4 callback service
5642 * @cred: RPC credential to use for this call
5643 * @res: where to place the result
5645 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5647 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5648 unsigned short port, struct rpc_cred *cred,
5649 struct nfs4_setclientid_res *res)
5651 nfs4_verifier sc_verifier;
5652 struct nfs4_setclientid setclientid = {
5653 .sc_verifier = &sc_verifier,
5657 struct rpc_message msg = {
5658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5659 .rpc_argp = &setclientid,
5663 struct rpc_task *task;
5664 struct rpc_task_setup task_setup_data = {
5665 .rpc_client = clp->cl_rpcclient,
5666 .rpc_message = &msg,
5667 .callback_ops = &nfs4_setclientid_ops,
5668 .callback_data = &setclientid,
5669 .flags = RPC_TASK_TIMEOUT,
5673 /* nfs_client_id4 */
5674 nfs4_init_boot_verifier(clp, &sc_verifier);
5676 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5677 status = nfs4_init_uniform_client_string(clp);
5679 status = nfs4_init_nonuniform_client_string(clp);
5685 setclientid.sc_netid_len =
5686 nfs4_init_callback_netid(clp,
5687 setclientid.sc_netid,
5688 sizeof(setclientid.sc_netid));
5689 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5690 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5691 clp->cl_ipaddr, port >> 8, port & 255);
5693 dprintk("NFS call setclientid auth=%s, '%s'\n",
5694 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5696 task = rpc_run_task(&task_setup_data);
5698 status = PTR_ERR(task);
5701 status = task->tk_status;
5702 if (setclientid.sc_cred) {
5703 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5704 put_rpccred(setclientid.sc_cred);
5708 trace_nfs4_setclientid(clp, status);
5709 dprintk("NFS reply setclientid: %d\n", status);
5714 * nfs4_proc_setclientid_confirm - Confirm client ID
5715 * @clp: state data structure
5716 * @res: result of a previous SETCLIENTID
5717 * @cred: RPC credential to use for this call
5719 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5721 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5722 struct nfs4_setclientid_res *arg,
5723 struct rpc_cred *cred)
5725 struct rpc_message msg = {
5726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5732 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5733 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5735 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5736 trace_nfs4_setclientid_confirm(clp, status);
5737 dprintk("NFS reply setclientid_confirm: %d\n", status);
5741 struct nfs4_delegreturndata {
5742 struct nfs4_delegreturnargs args;
5743 struct nfs4_delegreturnres res;
5745 nfs4_stateid stateid;
5746 unsigned long timestamp;
5748 struct nfs4_layoutreturn_args arg;
5749 struct nfs4_layoutreturn_res res;
5750 struct nfs4_xdr_opaque_data ld_private;
5754 struct nfs_fattr fattr;
5756 struct inode *inode;
5759 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5761 struct nfs4_delegreturndata *data = calldata;
5763 if (!nfs4_sequence_done(task, &data->res.seq_res))
5766 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5768 /* Handle Layoutreturn errors */
5769 if (data->args.lr_args && task->tk_status != 0) {
5770 switch(data->res.lr_ret) {
5772 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5775 data->args.lr_args = NULL;
5776 data->res.lr_res = NULL;
5778 case -NFS4ERR_OLD_STATEID:
5779 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
5783 case -NFS4ERR_ADMIN_REVOKED:
5784 case -NFS4ERR_DELEG_REVOKED:
5785 case -NFS4ERR_EXPIRED:
5786 case -NFS4ERR_BAD_STATEID:
5787 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5788 case -NFS4ERR_WRONG_CRED:
5789 data->args.lr_args = NULL;
5790 data->res.lr_res = NULL;
5795 switch (task->tk_status) {
5797 renew_lease(data->res.server, data->timestamp);
5799 case -NFS4ERR_ADMIN_REVOKED:
5800 case -NFS4ERR_DELEG_REVOKED:
5801 case -NFS4ERR_EXPIRED:
5802 nfs4_free_revoked_stateid(data->res.server,
5804 task->tk_msg.rpc_cred);
5806 case -NFS4ERR_BAD_STATEID:
5807 case -NFS4ERR_STALE_STATEID:
5808 task->tk_status = 0;
5810 case -NFS4ERR_OLD_STATEID:
5811 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5813 task->tk_status = 0;
5815 case -NFS4ERR_ACCESS:
5816 if (data->args.bitmask) {
5817 data->args.bitmask = NULL;
5818 data->res.fattr = NULL;
5823 if (nfs4_async_handle_error(task, data->res.server,
5824 NULL, NULL) == -EAGAIN) {
5828 data->rpc_status = task->tk_status;
5831 data->res.lr_ret = 0;
5833 task->tk_status = 0;
5834 rpc_restart_call_prepare(task);
5837 static void nfs4_delegreturn_release(void *calldata)
5839 struct nfs4_delegreturndata *data = calldata;
5840 struct inode *inode = data->inode;
5844 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5846 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5847 nfs_iput_and_deactive(inode);
5852 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5854 struct nfs4_delegreturndata *d_data;
5856 d_data = (struct nfs4_delegreturndata *)data;
5858 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5861 nfs4_setup_sequence(d_data->res.server->nfs_client,
5862 &d_data->args.seq_args,
5863 &d_data->res.seq_res,
5867 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5868 .rpc_call_prepare = nfs4_delegreturn_prepare,
5869 .rpc_call_done = nfs4_delegreturn_done,
5870 .rpc_release = nfs4_delegreturn_release,
5873 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5875 struct nfs4_delegreturndata *data;
5876 struct nfs_server *server = NFS_SERVER(inode);
5877 struct rpc_task *task;
5878 struct rpc_message msg = {
5879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5882 struct rpc_task_setup task_setup_data = {
5883 .rpc_client = server->client,
5884 .rpc_message = &msg,
5885 .callback_ops = &nfs4_delegreturn_ops,
5886 .flags = RPC_TASK_ASYNC,
5890 data = kzalloc(sizeof(*data), GFP_NOFS);
5893 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5895 nfs4_state_protect(server->nfs_client,
5896 NFS_SP4_MACH_CRED_CLEANUP,
5897 &task_setup_data.rpc_client, &msg);
5899 data->args.fhandle = &data->fh;
5900 data->args.stateid = &data->stateid;
5901 data->args.bitmask = server->cache_consistency_bitmask;
5902 nfs_copy_fh(&data->fh, NFS_FH(inode));
5903 nfs4_stateid_copy(&data->stateid, stateid);
5904 data->res.fattr = &data->fattr;
5905 data->res.server = server;
5906 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5907 data->lr.arg.ld_private = &data->lr.ld_private;
5908 nfs_fattr_init(data->res.fattr);
5909 data->timestamp = jiffies;
5910 data->rpc_status = 0;
5911 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5912 data->inode = nfs_igrab_and_active(inode);
5915 data->args.lr_args = &data->lr.arg;
5916 data->res.lr_res = &data->lr.res;
5918 } else if (data->lr.roc) {
5919 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5920 data->lr.roc = false;
5923 task_setup_data.callback_data = data;
5924 msg.rpc_argp = &data->args;
5925 msg.rpc_resp = &data->res;
5926 task = rpc_run_task(&task_setup_data);
5928 return PTR_ERR(task);
5931 status = rpc_wait_for_completion_task(task);
5934 status = data->rpc_status;
5940 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5942 struct nfs_server *server = NFS_SERVER(inode);
5943 struct nfs4_exception exception = { };
5946 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5947 trace_nfs4_delegreturn(inode, stateid, err);
5949 case -NFS4ERR_STALE_STATEID:
5950 case -NFS4ERR_EXPIRED:
5954 err = nfs4_handle_exception(server, err, &exception);
5955 } while (exception.retry);
5959 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5961 struct inode *inode = state->inode;
5962 struct nfs_server *server = NFS_SERVER(inode);
5963 struct nfs_client *clp = server->nfs_client;
5964 struct nfs_lockt_args arg = {
5965 .fh = NFS_FH(inode),
5968 struct nfs_lockt_res res = {
5971 struct rpc_message msg = {
5972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5975 .rpc_cred = state->owner->so_cred,
5977 struct nfs4_lock_state *lsp;
5980 arg.lock_owner.clientid = clp->cl_clientid;
5981 status = nfs4_set_lock_state(state, request);
5984 lsp = request->fl_u.nfs4_fl.owner;
5985 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5986 arg.lock_owner.s_dev = server->s_dev;
5987 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5990 request->fl_type = F_UNLCK;
5992 case -NFS4ERR_DENIED:
5995 request->fl_ops->fl_release_private(request);
5996 request->fl_ops = NULL;
6001 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6003 struct nfs4_exception exception = { };
6007 err = _nfs4_proc_getlk(state, cmd, request);
6008 trace_nfs4_get_lock(request, state, cmd, err);
6009 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6011 } while (exception.retry);
6015 struct nfs4_unlockdata {
6016 struct nfs_locku_args arg;
6017 struct nfs_locku_res res;
6018 struct nfs4_lock_state *lsp;
6019 struct nfs_open_context *ctx;
6020 struct nfs_lock_context *l_ctx;
6021 struct file_lock fl;
6022 struct nfs_server *server;
6023 unsigned long timestamp;
6026 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6027 struct nfs_open_context *ctx,
6028 struct nfs4_lock_state *lsp,
6029 struct nfs_seqid *seqid)
6031 struct nfs4_unlockdata *p;
6032 struct inode *inode = lsp->ls_state->inode;
6034 p = kzalloc(sizeof(*p), GFP_NOFS);
6037 p->arg.fh = NFS_FH(inode);
6039 p->arg.seqid = seqid;
6040 p->res.seqid = seqid;
6042 refcount_inc(&lsp->ls_count);
6043 /* Ensure we don't close file until we're done freeing locks! */
6044 p->ctx = get_nfs_open_context(ctx);
6045 p->l_ctx = nfs_get_lock_context(ctx);
6046 memcpy(&p->fl, fl, sizeof(p->fl));
6047 p->server = NFS_SERVER(inode);
6051 static void nfs4_locku_release_calldata(void *data)
6053 struct nfs4_unlockdata *calldata = data;
6054 nfs_free_seqid(calldata->arg.seqid);
6055 nfs4_put_lock_state(calldata->lsp);
6056 nfs_put_lock_context(calldata->l_ctx);
6057 put_nfs_open_context(calldata->ctx);
6061 static void nfs4_locku_done(struct rpc_task *task, void *data)
6063 struct nfs4_unlockdata *calldata = data;
6065 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6067 switch (task->tk_status) {
6069 renew_lease(calldata->server, calldata->timestamp);
6070 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6071 if (nfs4_update_lock_stateid(calldata->lsp,
6072 &calldata->res.stateid))
6074 case -NFS4ERR_ADMIN_REVOKED:
6075 case -NFS4ERR_EXPIRED:
6076 nfs4_free_revoked_stateid(calldata->server,
6077 &calldata->arg.stateid,
6078 task->tk_msg.rpc_cred);
6079 case -NFS4ERR_BAD_STATEID:
6080 case -NFS4ERR_OLD_STATEID:
6081 case -NFS4ERR_STALE_STATEID:
6082 if (!nfs4_stateid_match(&calldata->arg.stateid,
6083 &calldata->lsp->ls_stateid))
6084 rpc_restart_call_prepare(task);
6087 if (nfs4_async_handle_error(task, calldata->server,
6088 NULL, NULL) == -EAGAIN)
6089 rpc_restart_call_prepare(task);
6091 nfs_release_seqid(calldata->arg.seqid);
6094 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6096 struct nfs4_unlockdata *calldata = data;
6098 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6099 nfs_async_iocounter_wait(task, calldata->l_ctx))
6102 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6104 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6105 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6106 /* Note: exit _without_ running nfs4_locku_done */
6109 calldata->timestamp = jiffies;
6110 if (nfs4_setup_sequence(calldata->server->nfs_client,
6111 &calldata->arg.seq_args,
6112 &calldata->res.seq_res,
6114 nfs_release_seqid(calldata->arg.seqid);
6117 task->tk_action = NULL;
6119 nfs4_sequence_done(task, &calldata->res.seq_res);
6122 static const struct rpc_call_ops nfs4_locku_ops = {
6123 .rpc_call_prepare = nfs4_locku_prepare,
6124 .rpc_call_done = nfs4_locku_done,
6125 .rpc_release = nfs4_locku_release_calldata,
6128 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6129 struct nfs_open_context *ctx,
6130 struct nfs4_lock_state *lsp,
6131 struct nfs_seqid *seqid)
6133 struct nfs4_unlockdata *data;
6134 struct rpc_message msg = {
6135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6136 .rpc_cred = ctx->cred,
6138 struct rpc_task_setup task_setup_data = {
6139 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6140 .rpc_message = &msg,
6141 .callback_ops = &nfs4_locku_ops,
6142 .workqueue = nfsiod_workqueue,
6143 .flags = RPC_TASK_ASYNC,
6146 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6147 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6149 /* Ensure this is an unlock - when canceling a lock, the
6150 * canceled lock is passed in, and it won't be an unlock.
6152 fl->fl_type = F_UNLCK;
6153 if (fl->fl_flags & FL_CLOSE)
6154 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6156 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6158 nfs_free_seqid(seqid);
6159 return ERR_PTR(-ENOMEM);
6162 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6163 msg.rpc_argp = &data->arg;
6164 msg.rpc_resp = &data->res;
6165 task_setup_data.callback_data = data;
6166 return rpc_run_task(&task_setup_data);
6169 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6171 struct inode *inode = state->inode;
6172 struct nfs4_state_owner *sp = state->owner;
6173 struct nfs_inode *nfsi = NFS_I(inode);
6174 struct nfs_seqid *seqid;
6175 struct nfs4_lock_state *lsp;
6176 struct rpc_task *task;
6177 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6179 unsigned char fl_flags = request->fl_flags;
6181 status = nfs4_set_lock_state(state, request);
6182 /* Unlock _before_ we do the RPC call */
6183 request->fl_flags |= FL_EXISTS;
6184 /* Exclude nfs_delegation_claim_locks() */
6185 mutex_lock(&sp->so_delegreturn_mutex);
6186 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6187 down_read(&nfsi->rwsem);
6188 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6189 up_read(&nfsi->rwsem);
6190 mutex_unlock(&sp->so_delegreturn_mutex);
6193 up_read(&nfsi->rwsem);
6194 mutex_unlock(&sp->so_delegreturn_mutex);
6197 /* Is this a delegated lock? */
6198 lsp = request->fl_u.nfs4_fl.owner;
6199 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6201 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6202 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6206 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6207 status = PTR_ERR(task);
6210 status = rpc_wait_for_completion_task(task);
6213 request->fl_flags = fl_flags;
6214 trace_nfs4_unlock(request, state, F_SETLK, status);
6218 struct nfs4_lockdata {
6219 struct nfs_lock_args arg;
6220 struct nfs_lock_res res;
6221 struct nfs4_lock_state *lsp;
6222 struct nfs_open_context *ctx;
6223 struct file_lock fl;
6224 unsigned long timestamp;
6227 struct nfs_server *server;
6230 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6231 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6234 struct nfs4_lockdata *p;
6235 struct inode *inode = lsp->ls_state->inode;
6236 struct nfs_server *server = NFS_SERVER(inode);
6237 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6239 p = kzalloc(sizeof(*p), gfp_mask);
6243 p->arg.fh = NFS_FH(inode);
6245 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6246 if (IS_ERR(p->arg.open_seqid))
6248 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6249 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6250 if (IS_ERR(p->arg.lock_seqid))
6251 goto out_free_seqid;
6252 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6253 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6254 p->arg.lock_owner.s_dev = server->s_dev;
6255 p->res.lock_seqid = p->arg.lock_seqid;
6258 refcount_inc(&lsp->ls_count);
6259 p->ctx = get_nfs_open_context(ctx);
6260 memcpy(&p->fl, fl, sizeof(p->fl));
6263 nfs_free_seqid(p->arg.open_seqid);
6269 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6271 struct nfs4_lockdata *data = calldata;
6272 struct nfs4_state *state = data->lsp->ls_state;
6274 dprintk("%s: begin!\n", __func__);
6275 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6277 /* Do we need to do an open_to_lock_owner? */
6278 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6279 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6280 goto out_release_lock_seqid;
6282 nfs4_stateid_copy(&data->arg.open_stateid,
6283 &state->open_stateid);
6284 data->arg.new_lock_owner = 1;
6285 data->res.open_seqid = data->arg.open_seqid;
6287 data->arg.new_lock_owner = 0;
6288 nfs4_stateid_copy(&data->arg.lock_stateid,
6289 &data->lsp->ls_stateid);
6291 if (!nfs4_valid_open_stateid(state)) {
6292 data->rpc_status = -EBADF;
6293 task->tk_action = NULL;
6294 goto out_release_open_seqid;
6296 data->timestamp = jiffies;
6297 if (nfs4_setup_sequence(data->server->nfs_client,
6298 &data->arg.seq_args,
6302 out_release_open_seqid:
6303 nfs_release_seqid(data->arg.open_seqid);
6304 out_release_lock_seqid:
6305 nfs_release_seqid(data->arg.lock_seqid);
6307 nfs4_sequence_done(task, &data->res.seq_res);
6308 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6311 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6313 struct nfs4_lockdata *data = calldata;
6314 struct nfs4_lock_state *lsp = data->lsp;
6316 dprintk("%s: begin!\n", __func__);
6318 if (!nfs4_sequence_done(task, &data->res.seq_res))
6321 data->rpc_status = task->tk_status;
6322 switch (task->tk_status) {
6324 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6326 if (data->arg.new_lock) {
6327 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6328 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6329 rpc_restart_call_prepare(task);
6333 if (data->arg.new_lock_owner != 0) {
6334 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6335 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6336 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6337 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6338 rpc_restart_call_prepare(task);
6340 case -NFS4ERR_BAD_STATEID:
6341 case -NFS4ERR_OLD_STATEID:
6342 case -NFS4ERR_STALE_STATEID:
6343 case -NFS4ERR_EXPIRED:
6344 if (data->arg.new_lock_owner != 0) {
6345 if (!nfs4_stateid_match(&data->arg.open_stateid,
6346 &lsp->ls_state->open_stateid))
6347 rpc_restart_call_prepare(task);
6348 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6350 rpc_restart_call_prepare(task);
6352 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6355 static void nfs4_lock_release(void *calldata)
6357 struct nfs4_lockdata *data = calldata;
6359 dprintk("%s: begin!\n", __func__);
6360 nfs_free_seqid(data->arg.open_seqid);
6361 if (data->cancelled) {
6362 struct rpc_task *task;
6363 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6364 data->arg.lock_seqid);
6366 rpc_put_task_async(task);
6367 dprintk("%s: cancelling lock!\n", __func__);
6369 nfs_free_seqid(data->arg.lock_seqid);
6370 nfs4_put_lock_state(data->lsp);
6371 put_nfs_open_context(data->ctx);
6373 dprintk("%s: done!\n", __func__);
6376 static const struct rpc_call_ops nfs4_lock_ops = {
6377 .rpc_call_prepare = nfs4_lock_prepare,
6378 .rpc_call_done = nfs4_lock_done,
6379 .rpc_release = nfs4_lock_release,
6382 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6385 case -NFS4ERR_ADMIN_REVOKED:
6386 case -NFS4ERR_EXPIRED:
6387 case -NFS4ERR_BAD_STATEID:
6388 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6389 if (new_lock_owner != 0 ||
6390 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6391 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6393 case -NFS4ERR_STALE_STATEID:
6394 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6395 nfs4_schedule_lease_recovery(server->nfs_client);
6399 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6401 struct nfs4_lockdata *data;
6402 struct rpc_task *task;
6403 struct rpc_message msg = {
6404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6405 .rpc_cred = state->owner->so_cred,
6407 struct rpc_task_setup task_setup_data = {
6408 .rpc_client = NFS_CLIENT(state->inode),
6409 .rpc_message = &msg,
6410 .callback_ops = &nfs4_lock_ops,
6411 .workqueue = nfsiod_workqueue,
6412 .flags = RPC_TASK_ASYNC,
6416 dprintk("%s: begin!\n", __func__);
6417 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6418 fl->fl_u.nfs4_fl.owner,
6419 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6423 data->arg.block = 1;
6424 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6425 msg.rpc_argp = &data->arg;
6426 msg.rpc_resp = &data->res;
6427 task_setup_data.callback_data = data;
6428 if (recovery_type > NFS_LOCK_NEW) {
6429 if (recovery_type == NFS_LOCK_RECLAIM)
6430 data->arg.reclaim = NFS_LOCK_RECLAIM;
6431 nfs4_set_sequence_privileged(&data->arg.seq_args);
6433 data->arg.new_lock = 1;
6434 task = rpc_run_task(&task_setup_data);
6436 return PTR_ERR(task);
6437 ret = rpc_wait_for_completion_task(task);
6439 ret = data->rpc_status;
6441 nfs4_handle_setlk_error(data->server, data->lsp,
6442 data->arg.new_lock_owner, ret);
6444 data->cancelled = true;
6446 dprintk("%s: done, ret = %d!\n", __func__, ret);
6447 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6451 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6453 struct nfs_server *server = NFS_SERVER(state->inode);
6454 struct nfs4_exception exception = {
6455 .inode = state->inode,
6460 /* Cache the lock if possible... */
6461 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6463 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6464 if (err != -NFS4ERR_DELAY)
6466 nfs4_handle_exception(server, err, &exception);
6467 } while (exception.retry);
6471 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6473 struct nfs_server *server = NFS_SERVER(state->inode);
6474 struct nfs4_exception exception = {
6475 .inode = state->inode,
6479 err = nfs4_set_lock_state(state, request);
6482 if (!recover_lost_locks) {
6483 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6487 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6489 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6493 case -NFS4ERR_GRACE:
6494 case -NFS4ERR_DELAY:
6495 nfs4_handle_exception(server, err, &exception);
6498 } while (exception.retry);
6503 #if defined(CONFIG_NFS_V4_1)
6504 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6506 struct nfs4_lock_state *lsp;
6509 status = nfs4_set_lock_state(state, request);
6512 lsp = request->fl_u.nfs4_fl.owner;
6513 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6514 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6516 return nfs4_lock_expired(state, request);
6520 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6522 struct nfs_inode *nfsi = NFS_I(state->inode);
6523 struct nfs4_state_owner *sp = state->owner;
6524 unsigned char fl_flags = request->fl_flags;
6527 request->fl_flags |= FL_ACCESS;
6528 status = locks_lock_inode_wait(state->inode, request);
6531 mutex_lock(&sp->so_delegreturn_mutex);
6532 down_read(&nfsi->rwsem);
6533 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6534 /* Yes: cache locks! */
6535 /* ...but avoid races with delegation recall... */
6536 request->fl_flags = fl_flags & ~FL_SLEEP;
6537 status = locks_lock_inode_wait(state->inode, request);
6538 up_read(&nfsi->rwsem);
6539 mutex_unlock(&sp->so_delegreturn_mutex);
6542 up_read(&nfsi->rwsem);
6543 mutex_unlock(&sp->so_delegreturn_mutex);
6544 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6546 request->fl_flags = fl_flags;
6550 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6552 struct nfs4_exception exception = {
6554 .inode = state->inode,
6559 err = _nfs4_proc_setlk(state, cmd, request);
6560 if (err == -NFS4ERR_DENIED)
6562 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6564 } while (exception.retry);
6568 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6569 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6572 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6573 struct file_lock *request)
6575 int status = -ERESTARTSYS;
6576 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6578 while(!signalled()) {
6579 status = nfs4_proc_setlk(state, cmd, request);
6580 if ((status != -EAGAIN) || IS_SETLK(cmd))
6582 freezable_schedule_timeout_interruptible(timeout);
6584 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6585 status = -ERESTARTSYS;
6590 #ifdef CONFIG_NFS_V4_1
6591 struct nfs4_lock_waiter {
6592 struct task_struct *task;
6593 struct inode *inode;
6594 struct nfs_lowner *owner;
6599 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6602 struct cb_notify_lock_args *cbnl = key;
6603 struct nfs4_lock_waiter *waiter = wait->private;
6604 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6605 *wowner = waiter->owner;
6607 /* Only wake if the callback was for the same owner */
6608 if (lowner->clientid != wowner->clientid ||
6609 lowner->id != wowner->id ||
6610 lowner->s_dev != wowner->s_dev)
6613 /* Make sure it's for the right inode */
6614 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6617 waiter->notified = true;
6619 /* override "private" so we can use default_wake_function */
6620 wait->private = waiter->task;
6621 ret = autoremove_wake_function(wait, mode, flags, key);
6622 wait->private = waiter;
6627 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6629 int status = -ERESTARTSYS;
6630 unsigned long flags;
6631 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6632 struct nfs_server *server = NFS_SERVER(state->inode);
6633 struct nfs_client *clp = server->nfs_client;
6634 wait_queue_head_t *q = &clp->cl_lock_waitq;
6635 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6636 .id = lsp->ls_seqid.owner_id,
6637 .s_dev = server->s_dev };
6638 struct nfs4_lock_waiter waiter = { .task = current,
6639 .inode = state->inode,
6641 .notified = false };
6642 wait_queue_entry_t wait;
6644 /* Don't bother with waitqueue if we don't expect a callback */
6645 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6646 return nfs4_retry_setlk_simple(state, cmd, request);
6649 wait.private = &waiter;
6650 wait.func = nfs4_wake_lock_waiter;
6651 add_wait_queue(q, &wait);
6653 while(!signalled()) {
6654 status = nfs4_proc_setlk(state, cmd, request);
6655 if ((status != -EAGAIN) || IS_SETLK(cmd))
6658 status = -ERESTARTSYS;
6659 spin_lock_irqsave(&q->lock, flags);
6660 if (waiter.notified) {
6661 spin_unlock_irqrestore(&q->lock, flags);
6664 set_current_state(TASK_INTERRUPTIBLE);
6665 spin_unlock_irqrestore(&q->lock, flags);
6667 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6670 finish_wait(q, &wait);
6673 #else /* !CONFIG_NFS_V4_1 */
6675 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6677 return nfs4_retry_setlk_simple(state, cmd, request);
6682 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6684 struct nfs_open_context *ctx;
6685 struct nfs4_state *state;
6688 /* verify open state */
6689 ctx = nfs_file_open_context(filp);
6692 if (IS_GETLK(cmd)) {
6694 return nfs4_proc_getlk(state, F_GETLK, request);
6698 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6701 if (request->fl_type == F_UNLCK) {
6703 return nfs4_proc_unlck(state, cmd, request);
6710 if ((request->fl_flags & FL_POSIX) &&
6711 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6715 * Don't rely on the VFS having checked the file open mode,
6716 * since it won't do this for flock() locks.
6718 switch (request->fl_type) {
6720 if (!(filp->f_mode & FMODE_READ))
6724 if (!(filp->f_mode & FMODE_WRITE))
6728 status = nfs4_set_lock_state(state, request);
6732 return nfs4_retry_setlk(state, cmd, request);
6735 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6737 struct nfs_server *server = NFS_SERVER(state->inode);
6740 err = nfs4_set_lock_state(state, fl);
6743 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6744 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6747 struct nfs_release_lockowner_data {
6748 struct nfs4_lock_state *lsp;
6749 struct nfs_server *server;
6750 struct nfs_release_lockowner_args args;
6751 struct nfs_release_lockowner_res res;
6752 unsigned long timestamp;
6755 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6757 struct nfs_release_lockowner_data *data = calldata;
6758 struct nfs_server *server = data->server;
6759 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6760 &data->res.seq_res, task);
6761 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6762 data->timestamp = jiffies;
6765 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6767 struct nfs_release_lockowner_data *data = calldata;
6768 struct nfs_server *server = data->server;
6770 nfs40_sequence_done(task, &data->res.seq_res);
6772 switch (task->tk_status) {
6774 renew_lease(server, data->timestamp);
6776 case -NFS4ERR_STALE_CLIENTID:
6777 case -NFS4ERR_EXPIRED:
6778 nfs4_schedule_lease_recovery(server->nfs_client);
6780 case -NFS4ERR_LEASE_MOVED:
6781 case -NFS4ERR_DELAY:
6782 if (nfs4_async_handle_error(task, server,
6783 NULL, NULL) == -EAGAIN)
6784 rpc_restart_call_prepare(task);
6788 static void nfs4_release_lockowner_release(void *calldata)
6790 struct nfs_release_lockowner_data *data = calldata;
6791 nfs4_free_lock_state(data->server, data->lsp);
6795 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6796 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6797 .rpc_call_done = nfs4_release_lockowner_done,
6798 .rpc_release = nfs4_release_lockowner_release,
6802 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6804 struct nfs_release_lockowner_data *data;
6805 struct rpc_message msg = {
6806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6809 if (server->nfs_client->cl_mvops->minor_version != 0)
6812 data = kmalloc(sizeof(*data), GFP_NOFS);
6816 data->server = server;
6817 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6818 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6819 data->args.lock_owner.s_dev = server->s_dev;
6821 msg.rpc_argp = &data->args;
6822 msg.rpc_resp = &data->res;
6823 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6824 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6827 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6829 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6830 struct dentry *unused, struct inode *inode,
6831 const char *key, const void *buf,
6832 size_t buflen, int flags)
6834 return nfs4_proc_set_acl(inode, buf, buflen);
6837 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6838 struct dentry *unused, struct inode *inode,
6839 const char *key, void *buf, size_t buflen)
6841 return nfs4_proc_get_acl(inode, buf, buflen);
6844 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6846 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6849 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6851 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6852 struct dentry *unused, struct inode *inode,
6853 const char *key, const void *buf,
6854 size_t buflen, int flags)
6856 if (security_ismaclabel(key))
6857 return nfs4_set_security_label(inode, buf, buflen);
6862 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6863 struct dentry *unused, struct inode *inode,
6864 const char *key, void *buf, size_t buflen)
6866 if (security_ismaclabel(key))
6867 return nfs4_get_security_label(inode, buf, buflen);
6872 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6876 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6877 len = security_inode_listsecurity(inode, list, list_len);
6878 if (list_len && len > list_len)
6884 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6885 .prefix = XATTR_SECURITY_PREFIX,
6886 .get = nfs4_xattr_get_nfs4_label,
6887 .set = nfs4_xattr_set_nfs4_label,
6893 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6901 * nfs_fhget will use either the mounted_on_fileid or the fileid
6903 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6905 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6906 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6907 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6908 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6911 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6912 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6913 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6917 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6918 const struct qstr *name,
6919 struct nfs4_fs_locations *fs_locations,
6922 struct nfs_server *server = NFS_SERVER(dir);
6924 struct nfs4_fs_locations_arg args = {
6925 .dir_fh = NFS_FH(dir),
6930 struct nfs4_fs_locations_res res = {
6931 .fs_locations = fs_locations,
6933 struct rpc_message msg = {
6934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6940 dprintk("%s: start\n", __func__);
6942 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6943 bitmask[1] = nfs4_fattr_bitmap[1];
6945 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6946 * is not supported */
6947 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6948 bitmask[0] &= ~FATTR4_WORD0_FILEID;
6950 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
6952 nfs_fattr_init(&fs_locations->fattr);
6953 fs_locations->server = server;
6954 fs_locations->nlocations = 0;
6955 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6956 dprintk("%s: returned status = %d\n", __func__, status);
6960 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6961 const struct qstr *name,
6962 struct nfs4_fs_locations *fs_locations,
6965 struct nfs4_exception exception = { };
6968 err = _nfs4_proc_fs_locations(client, dir, name,
6969 fs_locations, page);
6970 trace_nfs4_get_fs_locations(dir, name, err);
6971 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6973 } while (exception.retry);
6978 * This operation also signals the server that this client is
6979 * performing migration recovery. The server can stop returning
6980 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6981 * appended to this compound to identify the client ID which is
6982 * performing recovery.
6984 static int _nfs40_proc_get_locations(struct inode *inode,
6985 struct nfs4_fs_locations *locations,
6986 struct page *page, struct rpc_cred *cred)
6988 struct nfs_server *server = NFS_SERVER(inode);
6989 struct rpc_clnt *clnt = server->client;
6991 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6993 struct nfs4_fs_locations_arg args = {
6994 .clientid = server->nfs_client->cl_clientid,
6995 .fh = NFS_FH(inode),
6998 .migration = 1, /* skip LOOKUP */
6999 .renew = 1, /* append RENEW */
7001 struct nfs4_fs_locations_res res = {
7002 .fs_locations = locations,
7006 struct rpc_message msg = {
7007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7012 unsigned long now = jiffies;
7015 nfs_fattr_init(&locations->fattr);
7016 locations->server = server;
7017 locations->nlocations = 0;
7019 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7020 nfs4_set_sequence_privileged(&args.seq_args);
7021 status = nfs4_call_sync_sequence(clnt, server, &msg,
7022 &args.seq_args, &res.seq_res);
7026 renew_lease(server, now);
7030 #ifdef CONFIG_NFS_V4_1
7033 * This operation also signals the server that this client is
7034 * performing migration recovery. The server can stop asserting
7035 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7036 * performing this operation is identified in the SEQUENCE
7037 * operation in this compound.
7039 * When the client supports GETATTR(fs_locations_info), it can
7040 * be plumbed in here.
7042 static int _nfs41_proc_get_locations(struct inode *inode,
7043 struct nfs4_fs_locations *locations,
7044 struct page *page, struct rpc_cred *cred)
7046 struct nfs_server *server = NFS_SERVER(inode);
7047 struct rpc_clnt *clnt = server->client;
7049 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7051 struct nfs4_fs_locations_arg args = {
7052 .fh = NFS_FH(inode),
7055 .migration = 1, /* skip LOOKUP */
7057 struct nfs4_fs_locations_res res = {
7058 .fs_locations = locations,
7061 struct rpc_message msg = {
7062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7069 nfs_fattr_init(&locations->fattr);
7070 locations->server = server;
7071 locations->nlocations = 0;
7073 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7074 nfs4_set_sequence_privileged(&args.seq_args);
7075 status = nfs4_call_sync_sequence(clnt, server, &msg,
7076 &args.seq_args, &res.seq_res);
7077 if (status == NFS4_OK &&
7078 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7079 status = -NFS4ERR_LEASE_MOVED;
7083 #endif /* CONFIG_NFS_V4_1 */
7086 * nfs4_proc_get_locations - discover locations for a migrated FSID
7087 * @inode: inode on FSID that is migrating
7088 * @locations: result of query
7090 * @cred: credential to use for this operation
7092 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7093 * operation failed, or a negative errno if a local error occurred.
7095 * On success, "locations" is filled in, but if the server has
7096 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7099 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7100 * from this client that require migration recovery.
7102 int nfs4_proc_get_locations(struct inode *inode,
7103 struct nfs4_fs_locations *locations,
7104 struct page *page, struct rpc_cred *cred)
7106 struct nfs_server *server = NFS_SERVER(inode);
7107 struct nfs_client *clp = server->nfs_client;
7108 const struct nfs4_mig_recovery_ops *ops =
7109 clp->cl_mvops->mig_recovery_ops;
7110 struct nfs4_exception exception = { };
7113 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7114 (unsigned long long)server->fsid.major,
7115 (unsigned long long)server->fsid.minor,
7117 nfs_display_fhandle(NFS_FH(inode), __func__);
7120 status = ops->get_locations(inode, locations, page, cred);
7121 if (status != -NFS4ERR_DELAY)
7123 nfs4_handle_exception(server, status, &exception);
7124 } while (exception.retry);
7129 * This operation also signals the server that this client is
7130 * performing "lease moved" recovery. The server can stop
7131 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7132 * is appended to this compound to identify the client ID which is
7133 * performing recovery.
7135 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7137 struct nfs_server *server = NFS_SERVER(inode);
7138 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7139 struct rpc_clnt *clnt = server->client;
7140 struct nfs4_fsid_present_arg args = {
7141 .fh = NFS_FH(inode),
7142 .clientid = clp->cl_clientid,
7143 .renew = 1, /* append RENEW */
7145 struct nfs4_fsid_present_res res = {
7148 struct rpc_message msg = {
7149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7154 unsigned long now = jiffies;
7157 res.fh = nfs_alloc_fhandle();
7161 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7162 nfs4_set_sequence_privileged(&args.seq_args);
7163 status = nfs4_call_sync_sequence(clnt, server, &msg,
7164 &args.seq_args, &res.seq_res);
7165 nfs_free_fhandle(res.fh);
7169 do_renew_lease(clp, now);
7173 #ifdef CONFIG_NFS_V4_1
7176 * This operation also signals the server that this client is
7177 * performing "lease moved" recovery. The server can stop asserting
7178 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7179 * this operation is identified in the SEQUENCE operation in this
7182 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7184 struct nfs_server *server = NFS_SERVER(inode);
7185 struct rpc_clnt *clnt = server->client;
7186 struct nfs4_fsid_present_arg args = {
7187 .fh = NFS_FH(inode),
7189 struct nfs4_fsid_present_res res = {
7191 struct rpc_message msg = {
7192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7199 res.fh = nfs_alloc_fhandle();
7203 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7204 nfs4_set_sequence_privileged(&args.seq_args);
7205 status = nfs4_call_sync_sequence(clnt, server, &msg,
7206 &args.seq_args, &res.seq_res);
7207 nfs_free_fhandle(res.fh);
7208 if (status == NFS4_OK &&
7209 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7210 status = -NFS4ERR_LEASE_MOVED;
7214 #endif /* CONFIG_NFS_V4_1 */
7217 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7218 * @inode: inode on FSID to check
7219 * @cred: credential to use for this operation
7221 * Server indicates whether the FSID is present, moved, or not
7222 * recognized. This operation is necessary to clear a LEASE_MOVED
7223 * condition for this client ID.
7225 * Returns NFS4_OK if the FSID is present on this server,
7226 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7227 * NFS4ERR code if some error occurred on the server, or a
7228 * negative errno if a local failure occurred.
7230 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7232 struct nfs_server *server = NFS_SERVER(inode);
7233 struct nfs_client *clp = server->nfs_client;
7234 const struct nfs4_mig_recovery_ops *ops =
7235 clp->cl_mvops->mig_recovery_ops;
7236 struct nfs4_exception exception = { };
7239 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7240 (unsigned long long)server->fsid.major,
7241 (unsigned long long)server->fsid.minor,
7243 nfs_display_fhandle(NFS_FH(inode), __func__);
7246 status = ops->fsid_present(inode, cred);
7247 if (status != -NFS4ERR_DELAY)
7249 nfs4_handle_exception(server, status, &exception);
7250 } while (exception.retry);
7255 * If 'use_integrity' is true and the state managment nfs_client
7256 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7257 * and the machine credential as per RFC3530bis and RFC5661 Security
7258 * Considerations sections. Otherwise, just use the user cred with the
7259 * filesystem's rpc_client.
7261 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7264 struct nfs4_secinfo_arg args = {
7265 .dir_fh = NFS_FH(dir),
7268 struct nfs4_secinfo_res res = {
7271 struct rpc_message msg = {
7272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7276 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7277 struct rpc_cred *cred = NULL;
7279 if (use_integrity) {
7280 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7281 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7282 msg.rpc_cred = cred;
7285 dprintk("NFS call secinfo %s\n", name->name);
7287 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7288 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7290 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7292 dprintk("NFS reply secinfo: %d\n", status);
7300 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7301 struct nfs4_secinfo_flavors *flavors)
7303 struct nfs4_exception exception = { };
7306 err = -NFS4ERR_WRONGSEC;
7308 /* try to use integrity protection with machine cred */
7309 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7310 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7313 * if unable to use integrity protection, or SECINFO with
7314 * integrity protection returns NFS4ERR_WRONGSEC (which is
7315 * disallowed by spec, but exists in deployed servers) use
7316 * the current filesystem's rpc_client and the user cred.
7318 if (err == -NFS4ERR_WRONGSEC)
7319 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7321 trace_nfs4_secinfo(dir, name, err);
7322 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7324 } while (exception.retry);
7328 #ifdef CONFIG_NFS_V4_1
7330 * Check the exchange flags returned by the server for invalid flags, having
7331 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7334 static int nfs4_check_cl_exchange_flags(u32 flags)
7336 if (flags & ~EXCHGID4_FLAG_MASK_R)
7338 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7339 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7341 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7345 return -NFS4ERR_INVAL;
7349 nfs41_same_server_scope(struct nfs41_server_scope *a,
7350 struct nfs41_server_scope *b)
7352 if (a->server_scope_sz != b->server_scope_sz)
7354 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7358 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7362 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7363 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7367 * nfs4_proc_bind_one_conn_to_session()
7369 * The 4.1 client currently uses the same TCP connection for the
7370 * fore and backchannel.
7373 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7374 struct rpc_xprt *xprt,
7375 struct nfs_client *clp,
7376 struct rpc_cred *cred)
7379 struct nfs41_bind_conn_to_session_args args = {
7381 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7383 struct nfs41_bind_conn_to_session_res res;
7384 struct rpc_message msg = {
7386 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7391 struct rpc_task_setup task_setup_data = {
7394 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7395 .rpc_message = &msg,
7396 .flags = RPC_TASK_TIMEOUT,
7398 struct rpc_task *task;
7400 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7401 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7402 args.dir = NFS4_CDFC4_FORE;
7404 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7405 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7406 args.dir = NFS4_CDFC4_FORE;
7408 task = rpc_run_task(&task_setup_data);
7409 if (!IS_ERR(task)) {
7410 status = task->tk_status;
7413 status = PTR_ERR(task);
7414 trace_nfs4_bind_conn_to_session(clp, status);
7416 if (memcmp(res.sessionid.data,
7417 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7418 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7421 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7422 dprintk("NFS: %s: Unexpected direction from server\n",
7426 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7427 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7436 struct rpc_bind_conn_calldata {
7437 struct nfs_client *clp;
7438 struct rpc_cred *cred;
7442 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7443 struct rpc_xprt *xprt,
7446 struct rpc_bind_conn_calldata *p = calldata;
7448 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7451 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7453 struct rpc_bind_conn_calldata data = {
7457 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7458 nfs4_proc_bind_conn_to_session_callback, &data);
7462 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7463 * and operations we'd like to see to enable certain features in the allow map
7465 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7466 .how = SP4_MACH_CRED,
7467 .enforce.u.words = {
7468 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7469 1 << (OP_EXCHANGE_ID - 32) |
7470 1 << (OP_CREATE_SESSION - 32) |
7471 1 << (OP_DESTROY_SESSION - 32) |
7472 1 << (OP_DESTROY_CLIENTID - 32)
7475 [0] = 1 << (OP_CLOSE) |
7476 1 << (OP_OPEN_DOWNGRADE) |
7478 1 << (OP_DELEGRETURN) |
7480 [1] = 1 << (OP_SECINFO - 32) |
7481 1 << (OP_SECINFO_NO_NAME - 32) |
7482 1 << (OP_LAYOUTRETURN - 32) |
7483 1 << (OP_TEST_STATEID - 32) |
7484 1 << (OP_FREE_STATEID - 32) |
7485 1 << (OP_WRITE - 32)
7490 * Select the state protection mode for client `clp' given the server results
7491 * from exchange_id in `sp'.
7493 * Returns 0 on success, negative errno otherwise.
7495 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7496 struct nfs41_state_protection *sp)
7498 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7499 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7500 1 << (OP_EXCHANGE_ID - 32) |
7501 1 << (OP_CREATE_SESSION - 32) |
7502 1 << (OP_DESTROY_SESSION - 32) |
7503 1 << (OP_DESTROY_CLIENTID - 32)
7505 unsigned long flags = 0;
7509 if (sp->how == SP4_MACH_CRED) {
7510 /* Print state protect result */
7511 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7512 for (i = 0; i <= LAST_NFS4_OP; i++) {
7513 if (test_bit(i, sp->enforce.u.longs))
7514 dfprintk(MOUNT, " enforce op %d\n", i);
7515 if (test_bit(i, sp->allow.u.longs))
7516 dfprintk(MOUNT, " allow op %d\n", i);
7519 /* make sure nothing is on enforce list that isn't supported */
7520 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7521 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7522 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7529 * Minimal mode - state operations are allowed to use machine
7530 * credential. Note this already happens by default, so the
7531 * client doesn't have to do anything more than the negotiation.
7533 * NOTE: we don't care if EXCHANGE_ID is in the list -
7534 * we're already using the machine cred for exchange_id
7535 * and will never use a different cred.
7537 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7538 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7539 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7540 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7541 dfprintk(MOUNT, "sp4_mach_cred:\n");
7542 dfprintk(MOUNT, " minimal mode enabled\n");
7543 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7545 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7550 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7551 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7552 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7553 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7554 dfprintk(MOUNT, " cleanup mode enabled\n");
7555 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7558 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7559 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7560 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7563 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7564 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7565 dfprintk(MOUNT, " secinfo mode enabled\n");
7566 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7569 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7570 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7571 dfprintk(MOUNT, " stateid mode enabled\n");
7572 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7575 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7576 dfprintk(MOUNT, " write mode enabled\n");
7577 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7580 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7581 dfprintk(MOUNT, " commit mode enabled\n");
7582 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7586 clp->cl_sp4_flags = flags;
7590 struct nfs41_exchange_id_data {
7591 struct nfs41_exchange_id_res res;
7592 struct nfs41_exchange_id_args args;
7595 static void nfs4_exchange_id_release(void *data)
7597 struct nfs41_exchange_id_data *cdata =
7598 (struct nfs41_exchange_id_data *)data;
7600 nfs_put_client(cdata->args.client);
7601 kfree(cdata->res.impl_id);
7602 kfree(cdata->res.server_scope);
7603 kfree(cdata->res.server_owner);
7607 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7608 .rpc_release = nfs4_exchange_id_release,
7612 * _nfs4_proc_exchange_id()
7614 * Wrapper for EXCHANGE_ID operation.
7616 static struct rpc_task *
7617 nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7618 u32 sp4_how, struct rpc_xprt *xprt)
7620 struct rpc_message msg = {
7621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7624 struct rpc_task_setup task_setup_data = {
7625 .rpc_client = clp->cl_rpcclient,
7626 .callback_ops = &nfs4_exchange_id_call_ops,
7627 .rpc_message = &msg,
7628 .flags = RPC_TASK_TIMEOUT,
7630 struct nfs41_exchange_id_data *calldata;
7633 if (!refcount_inc_not_zero(&clp->cl_count))
7634 return ERR_PTR(-EIO);
7637 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7641 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7643 status = nfs4_init_uniform_client_string(clp);
7647 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7650 if (unlikely(calldata->res.server_owner == NULL))
7653 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7655 if (unlikely(calldata->res.server_scope == NULL))
7656 goto out_server_owner;
7658 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7659 if (unlikely(calldata->res.impl_id == NULL))
7660 goto out_server_scope;
7664 calldata->args.state_protect.how = SP4_NONE;
7668 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7678 task_setup_data.rpc_xprt = xprt;
7679 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7680 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7681 sizeof(calldata->args.verifier.data));
7683 calldata->args.client = clp;
7684 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7685 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7686 #ifdef CONFIG_NFS_V4_1_MIGRATION
7687 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7689 msg.rpc_argp = &calldata->args;
7690 msg.rpc_resp = &calldata->res;
7691 task_setup_data.callback_data = calldata;
7693 return rpc_run_task(&task_setup_data);
7696 kfree(calldata->res.impl_id);
7698 kfree(calldata->res.server_scope);
7700 kfree(calldata->res.server_owner);
7704 nfs_put_client(clp);
7705 return ERR_PTR(status);
7709 * _nfs4_proc_exchange_id()
7711 * Wrapper for EXCHANGE_ID operation.
7713 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7716 struct rpc_task *task;
7717 struct nfs41_exchange_id_args *argp;
7718 struct nfs41_exchange_id_res *resp;
7721 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7723 return PTR_ERR(task);
7725 argp = task->tk_msg.rpc_argp;
7726 resp = task->tk_msg.rpc_resp;
7727 status = task->tk_status;
7731 status = nfs4_check_cl_exchange_flags(resp->flags);
7735 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7739 clp->cl_clientid = resp->clientid;
7740 clp->cl_exchange_flags = resp->flags;
7741 clp->cl_seqid = resp->seqid;
7742 /* Client ID is not confirmed */
7743 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7744 clear_bit(NFS4_SESSION_ESTABLISHED,
7745 &clp->cl_session->session_state);
7747 if (clp->cl_serverscope != NULL &&
7748 !nfs41_same_server_scope(clp->cl_serverscope,
7749 resp->server_scope)) {
7750 dprintk("%s: server_scope mismatch detected\n",
7752 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7755 swap(clp->cl_serverowner, resp->server_owner);
7756 swap(clp->cl_serverscope, resp->server_scope);
7757 swap(clp->cl_implid, resp->impl_id);
7759 /* Save the EXCHANGE_ID verifier session trunk tests */
7760 memcpy(clp->cl_confirm.data, argp->verifier.data,
7761 sizeof(clp->cl_confirm.data));
7763 trace_nfs4_exchange_id(clp, status);
7769 * nfs4_proc_exchange_id()
7771 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7773 * Since the clientid has expired, all compounds using sessions
7774 * associated with the stale clientid will be returning
7775 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7776 * be in some phase of session reset.
7778 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7780 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7782 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7785 /* try SP4_MACH_CRED if krb5i/p */
7786 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7787 authflavor == RPC_AUTH_GSS_KRB5P) {
7788 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7794 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7798 * nfs4_test_session_trunk
7800 * This is an add_xprt_test() test function called from
7801 * rpc_clnt_setup_test_and_add_xprt.
7803 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7804 * and is dereferrenced in nfs4_exchange_id_release
7806 * Upon success, add the new transport to the rpc_clnt
7808 * @clnt: struct rpc_clnt to get new transport
7809 * @xprt: the rpc_xprt to test
7810 * @data: call data for _nfs4_proc_exchange_id.
7812 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7815 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7816 struct rpc_task *task;
7821 dprintk("--> %s try %s\n", __func__,
7822 xprt->address_strings[RPC_DISPLAY_ADDR]);
7824 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7826 /* Test connection for session trunking. Async exchange_id call */
7827 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7829 return PTR_ERR(task);
7831 status = task->tk_status;
7833 status = nfs4_detect_session_trunking(adata->clp,
7834 task->tk_msg.rpc_resp, xprt);
7839 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7841 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7842 struct rpc_cred *cred)
7844 struct rpc_message msg = {
7845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7851 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7852 trace_nfs4_destroy_clientid(clp, status);
7854 dprintk("NFS: Got error %d from the server %s on "
7855 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7859 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7860 struct rpc_cred *cred)
7865 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7866 ret = _nfs4_proc_destroy_clientid(clp, cred);
7868 case -NFS4ERR_DELAY:
7869 case -NFS4ERR_CLIENTID_BUSY:
7879 int nfs4_destroy_clientid(struct nfs_client *clp)
7881 struct rpc_cred *cred;
7884 if (clp->cl_mvops->minor_version < 1)
7886 if (clp->cl_exchange_flags == 0)
7888 if (clp->cl_preserve_clid)
7890 cred = nfs4_get_clid_cred(clp);
7891 ret = nfs4_proc_destroy_clientid(clp, cred);
7896 case -NFS4ERR_STALE_CLIENTID:
7897 clp->cl_exchange_flags = 0;
7903 struct nfs4_get_lease_time_data {
7904 struct nfs4_get_lease_time_args *args;
7905 struct nfs4_get_lease_time_res *res;
7906 struct nfs_client *clp;
7909 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7912 struct nfs4_get_lease_time_data *data =
7913 (struct nfs4_get_lease_time_data *)calldata;
7915 dprintk("--> %s\n", __func__);
7916 /* just setup sequence, do not trigger session recovery
7917 since we're invoked within one */
7918 nfs4_setup_sequence(data->clp,
7919 &data->args->la_seq_args,
7920 &data->res->lr_seq_res,
7922 dprintk("<-- %s\n", __func__);
7926 * Called from nfs4_state_manager thread for session setup, so don't recover
7927 * from sequence operation or clientid errors.
7929 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7931 struct nfs4_get_lease_time_data *data =
7932 (struct nfs4_get_lease_time_data *)calldata;
7934 dprintk("--> %s\n", __func__);
7935 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7937 switch (task->tk_status) {
7938 case -NFS4ERR_DELAY:
7939 case -NFS4ERR_GRACE:
7940 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7941 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7942 task->tk_status = 0;
7944 case -NFS4ERR_RETRY_UNCACHED_REP:
7945 rpc_restart_call_prepare(task);
7948 dprintk("<-- %s\n", __func__);
7951 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7952 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7953 .rpc_call_done = nfs4_get_lease_time_done,
7956 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7958 struct rpc_task *task;
7959 struct nfs4_get_lease_time_args args;
7960 struct nfs4_get_lease_time_res res = {
7961 .lr_fsinfo = fsinfo,
7963 struct nfs4_get_lease_time_data data = {
7968 struct rpc_message msg = {
7969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7973 struct rpc_task_setup task_setup = {
7974 .rpc_client = clp->cl_rpcclient,
7975 .rpc_message = &msg,
7976 .callback_ops = &nfs4_get_lease_time_ops,
7977 .callback_data = &data,
7978 .flags = RPC_TASK_TIMEOUT,
7982 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7983 nfs4_set_sequence_privileged(&args.la_seq_args);
7984 task = rpc_run_task(&task_setup);
7987 return PTR_ERR(task);
7989 status = task->tk_status;
7995 * Initialize the values to be used by the client in CREATE_SESSION
7996 * If nfs4_init_session set the fore channel request and response sizes,
7999 * Set the back channel max_resp_sz_cached to zero to force the client to
8000 * always set csa_cachethis to FALSE because the current implementation
8001 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8003 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8004 struct rpc_clnt *clnt)
8006 unsigned int max_rqst_sz, max_resp_sz;
8007 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8009 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8010 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8012 /* Fore channel attributes */
8013 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8014 args->fc_attrs.max_resp_sz = max_resp_sz;
8015 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8016 args->fc_attrs.max_reqs = max_session_slots;
8018 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8019 "max_ops=%u max_reqs=%u\n",
8021 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8022 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8024 /* Back channel attributes */
8025 args->bc_attrs.max_rqst_sz = max_bc_payload;
8026 args->bc_attrs.max_resp_sz = max_bc_payload;
8027 args->bc_attrs.max_resp_sz_cached = 0;
8028 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8029 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
8031 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8032 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8034 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8035 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8036 args->bc_attrs.max_reqs);
8039 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8040 struct nfs41_create_session_res *res)
8042 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8043 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8045 if (rcvd->max_resp_sz > sent->max_resp_sz)
8048 * Our requested max_ops is the minimum we need; we're not
8049 * prepared to break up compounds into smaller pieces than that.
8050 * So, no point even trying to continue if the server won't
8053 if (rcvd->max_ops < sent->max_ops)
8055 if (rcvd->max_reqs == 0)
8057 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8058 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8062 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8063 struct nfs41_create_session_res *res)
8065 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8066 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8068 if (!(res->flags & SESSION4_BACK_CHAN))
8070 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8072 if (rcvd->max_resp_sz < sent->max_resp_sz)
8074 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8076 if (rcvd->max_ops > sent->max_ops)
8078 if (rcvd->max_reqs > sent->max_reqs)
8084 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8085 struct nfs41_create_session_res *res)
8089 ret = nfs4_verify_fore_channel_attrs(args, res);
8092 return nfs4_verify_back_channel_attrs(args, res);
8095 static void nfs4_update_session(struct nfs4_session *session,
8096 struct nfs41_create_session_res *res)
8098 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8099 /* Mark client id and session as being confirmed */
8100 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8101 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8102 session->flags = res->flags;
8103 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8104 if (res->flags & SESSION4_BACK_CHAN)
8105 memcpy(&session->bc_attrs, &res->bc_attrs,
8106 sizeof(session->bc_attrs));
8109 static int _nfs4_proc_create_session(struct nfs_client *clp,
8110 struct rpc_cred *cred)
8112 struct nfs4_session *session = clp->cl_session;
8113 struct nfs41_create_session_args args = {
8115 .clientid = clp->cl_clientid,
8116 .seqid = clp->cl_seqid,
8117 .cb_program = NFS4_CALLBACK,
8119 struct nfs41_create_session_res res;
8121 struct rpc_message msg = {
8122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8129 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8130 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8132 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8133 trace_nfs4_create_session(clp, status);
8136 case -NFS4ERR_STALE_CLIENTID:
8137 case -NFS4ERR_DELAY:
8146 /* Verify the session's negotiated channel_attrs values */
8147 status = nfs4_verify_channel_attrs(&args, &res);
8148 /* Increment the clientid slot sequence id */
8151 nfs4_update_session(session, &res);
8158 * Issues a CREATE_SESSION operation to the server.
8159 * It is the responsibility of the caller to verify the session is
8160 * expired before calling this routine.
8162 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8166 struct nfs4_session *session = clp->cl_session;
8168 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8170 status = _nfs4_proc_create_session(clp, cred);
8174 /* Init or reset the session slot tables */
8175 status = nfs4_setup_session_slot_tables(session);
8176 dprintk("slot table setup returned %d\n", status);
8180 ptr = (unsigned *)&session->sess_id.data[0];
8181 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8182 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8184 dprintk("<-- %s\n", __func__);
8189 * Issue the over-the-wire RPC DESTROY_SESSION.
8190 * The caller must serialize access to this routine.
8192 int nfs4_proc_destroy_session(struct nfs4_session *session,
8193 struct rpc_cred *cred)
8195 struct rpc_message msg = {
8196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8197 .rpc_argp = session,
8202 dprintk("--> nfs4_proc_destroy_session\n");
8204 /* session is still being setup */
8205 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8208 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8209 trace_nfs4_destroy_session(session->clp, status);
8212 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8213 "Session has been destroyed regardless...\n", status);
8215 dprintk("<-- nfs4_proc_destroy_session\n");
8220 * Renew the cl_session lease.
8222 struct nfs4_sequence_data {
8223 struct nfs_client *clp;
8224 struct nfs4_sequence_args args;
8225 struct nfs4_sequence_res res;
8228 static void nfs41_sequence_release(void *data)
8230 struct nfs4_sequence_data *calldata = data;
8231 struct nfs_client *clp = calldata->clp;
8233 if (refcount_read(&clp->cl_count) > 1)
8234 nfs4_schedule_state_renewal(clp);
8235 nfs_put_client(clp);
8239 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8241 switch(task->tk_status) {
8242 case -NFS4ERR_DELAY:
8243 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8246 nfs4_schedule_lease_recovery(clp);
8251 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8253 struct nfs4_sequence_data *calldata = data;
8254 struct nfs_client *clp = calldata->clp;
8256 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8259 trace_nfs4_sequence(clp, task->tk_status);
8260 if (task->tk_status < 0) {
8261 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8262 if (refcount_read(&clp->cl_count) == 1)
8265 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8266 rpc_restart_call_prepare(task);
8270 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8272 dprintk("<-- %s\n", __func__);
8275 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8277 struct nfs4_sequence_data *calldata = data;
8278 struct nfs_client *clp = calldata->clp;
8279 struct nfs4_sequence_args *args;
8280 struct nfs4_sequence_res *res;
8282 args = task->tk_msg.rpc_argp;
8283 res = task->tk_msg.rpc_resp;
8285 nfs4_setup_sequence(clp, args, res, task);
8288 static const struct rpc_call_ops nfs41_sequence_ops = {
8289 .rpc_call_done = nfs41_sequence_call_done,
8290 .rpc_call_prepare = nfs41_sequence_prepare,
8291 .rpc_release = nfs41_sequence_release,
8294 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8295 struct rpc_cred *cred,
8296 struct nfs4_slot *slot,
8299 struct nfs4_sequence_data *calldata;
8300 struct rpc_message msg = {
8301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8304 struct rpc_task_setup task_setup_data = {
8305 .rpc_client = clp->cl_rpcclient,
8306 .rpc_message = &msg,
8307 .callback_ops = &nfs41_sequence_ops,
8308 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8310 struct rpc_task *ret;
8312 ret = ERR_PTR(-EIO);
8313 if (!refcount_inc_not_zero(&clp->cl_count))
8316 ret = ERR_PTR(-ENOMEM);
8317 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8318 if (calldata == NULL)
8320 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8321 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8323 nfs4_set_sequence_privileged(&calldata->args);
8324 msg.rpc_argp = &calldata->args;
8325 msg.rpc_resp = &calldata->res;
8326 calldata->clp = clp;
8327 task_setup_data.callback_data = calldata;
8329 ret = rpc_run_task(&task_setup_data);
8334 nfs_put_client(clp);
8336 nfs41_release_slot(slot);
8340 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8342 struct rpc_task *task;
8345 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8347 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8349 ret = PTR_ERR(task);
8351 rpc_put_task_async(task);
8352 dprintk("<-- %s status=%d\n", __func__, ret);
8356 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8358 struct rpc_task *task;
8361 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8363 ret = PTR_ERR(task);
8366 ret = rpc_wait_for_completion_task(task);
8368 ret = task->tk_status;
8371 dprintk("<-- %s status=%d\n", __func__, ret);
8375 struct nfs4_reclaim_complete_data {
8376 struct nfs_client *clp;
8377 struct nfs41_reclaim_complete_args arg;
8378 struct nfs41_reclaim_complete_res res;
8381 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8383 struct nfs4_reclaim_complete_data *calldata = data;
8385 nfs4_setup_sequence(calldata->clp,
8386 &calldata->arg.seq_args,
8387 &calldata->res.seq_res,
8391 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8393 switch(task->tk_status) {
8395 case -NFS4ERR_COMPLETE_ALREADY:
8396 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8398 case -NFS4ERR_DELAY:
8399 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8401 case -NFS4ERR_RETRY_UNCACHED_REP:
8403 case -NFS4ERR_BADSESSION:
8404 case -NFS4ERR_DEADSESSION:
8405 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8406 nfs4_schedule_session_recovery(clp->cl_session,
8410 nfs4_schedule_lease_recovery(clp);
8415 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8417 struct nfs4_reclaim_complete_data *calldata = data;
8418 struct nfs_client *clp = calldata->clp;
8419 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8421 dprintk("--> %s\n", __func__);
8422 if (!nfs41_sequence_done(task, res))
8425 trace_nfs4_reclaim_complete(clp, task->tk_status);
8426 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8427 rpc_restart_call_prepare(task);
8430 dprintk("<-- %s\n", __func__);
8433 static void nfs4_free_reclaim_complete_data(void *data)
8435 struct nfs4_reclaim_complete_data *calldata = data;
8440 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8441 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8442 .rpc_call_done = nfs4_reclaim_complete_done,
8443 .rpc_release = nfs4_free_reclaim_complete_data,
8447 * Issue a global reclaim complete.
8449 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8450 struct rpc_cred *cred)
8452 struct nfs4_reclaim_complete_data *calldata;
8453 struct rpc_task *task;
8454 struct rpc_message msg = {
8455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8458 struct rpc_task_setup task_setup_data = {
8459 .rpc_client = clp->cl_rpcclient,
8460 .rpc_message = &msg,
8461 .callback_ops = &nfs4_reclaim_complete_call_ops,
8462 .flags = RPC_TASK_ASYNC,
8464 int status = -ENOMEM;
8466 dprintk("--> %s\n", __func__);
8467 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8468 if (calldata == NULL)
8470 calldata->clp = clp;
8471 calldata->arg.one_fs = 0;
8473 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8474 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8475 msg.rpc_argp = &calldata->arg;
8476 msg.rpc_resp = &calldata->res;
8477 task_setup_data.callback_data = calldata;
8478 task = rpc_run_task(&task_setup_data);
8480 status = PTR_ERR(task);
8483 status = rpc_wait_for_completion_task(task);
8485 status = task->tk_status;
8488 dprintk("<-- %s status=%d\n", __func__, status);
8493 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8495 struct nfs4_layoutget *lgp = calldata;
8496 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8498 dprintk("--> %s\n", __func__);
8499 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8500 &lgp->res.seq_res, task);
8501 dprintk("<-- %s\n", __func__);
8504 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8506 struct nfs4_layoutget *lgp = calldata;
8508 dprintk("--> %s\n", __func__);
8509 nfs41_sequence_process(task, &lgp->res.seq_res);
8510 dprintk("<-- %s\n", __func__);
8514 nfs4_layoutget_handle_exception(struct rpc_task *task,
8515 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8517 struct inode *inode = lgp->args.inode;
8518 struct nfs_server *server = NFS_SERVER(inode);
8519 struct pnfs_layout_hdr *lo;
8520 int nfs4err = task->tk_status;
8521 int err, status = 0;
8524 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8531 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8532 * on the file. set tk_status to -ENODATA to tell upper layer to
8535 case -NFS4ERR_LAYOUTUNAVAILABLE:
8539 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8540 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8542 case -NFS4ERR_BADLAYOUT:
8543 status = -EOVERFLOW;
8546 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8547 * (or clients) writing to the same RAID stripe except when
8548 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8550 * Treat it like we would RECALLCONFLICT -- we retry for a little
8551 * while, and then eventually give up.
8553 case -NFS4ERR_LAYOUTTRYLATER:
8554 if (lgp->args.minlength == 0) {
8555 status = -EOVERFLOW;
8560 case -NFS4ERR_RECALLCONFLICT:
8561 status = -ERECALLCONFLICT;
8563 case -NFS4ERR_DELEG_REVOKED:
8564 case -NFS4ERR_ADMIN_REVOKED:
8565 case -NFS4ERR_EXPIRED:
8566 case -NFS4ERR_BAD_STATEID:
8567 exception->timeout = 0;
8568 spin_lock(&inode->i_lock);
8569 lo = NFS_I(inode)->layout;
8570 /* If the open stateid was bad, then recover it. */
8571 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8572 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8573 spin_unlock(&inode->i_lock);
8574 exception->state = lgp->args.ctx->state;
8575 exception->stateid = &lgp->args.stateid;
8580 * Mark the bad layout state as invalid, then retry
8582 pnfs_mark_layout_stateid_invalid(lo, &head);
8583 spin_unlock(&inode->i_lock);
8584 nfs_commit_inode(inode, 0);
8585 pnfs_free_lseg_list(&head);
8590 nfs4_sequence_free_slot(&lgp->res.seq_res);
8591 err = nfs4_handle_exception(server, nfs4err, exception);
8593 if (exception->retry)
8599 dprintk("<-- %s\n", __func__);
8603 static size_t max_response_pages(struct nfs_server *server)
8605 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8606 return nfs_page_array_len(0, max_resp_sz);
8609 static void nfs4_free_pages(struct page **pages, size_t size)
8616 for (i = 0; i < size; i++) {
8619 __free_page(pages[i]);
8624 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8626 struct page **pages;
8629 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8631 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8635 for (i = 0; i < size; i++) {
8636 pages[i] = alloc_page(gfp_flags);
8638 dprintk("%s: failed to allocate page\n", __func__);
8639 nfs4_free_pages(pages, size);
8647 static void nfs4_layoutget_release(void *calldata)
8649 struct nfs4_layoutget *lgp = calldata;
8650 struct inode *inode = lgp->args.inode;
8651 struct nfs_server *server = NFS_SERVER(inode);
8652 size_t max_pages = max_response_pages(server);
8654 dprintk("--> %s\n", __func__);
8655 nfs4_sequence_free_slot(&lgp->res.seq_res);
8656 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8657 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8658 put_nfs_open_context(lgp->args.ctx);
8660 dprintk("<-- %s\n", __func__);
8663 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8664 .rpc_call_prepare = nfs4_layoutget_prepare,
8665 .rpc_call_done = nfs4_layoutget_done,
8666 .rpc_release = nfs4_layoutget_release,
8669 struct pnfs_layout_segment *
8670 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8672 struct inode *inode = lgp->args.inode;
8673 struct nfs_server *server = NFS_SERVER(inode);
8674 size_t max_pages = max_response_pages(server);
8675 struct rpc_task *task;
8676 struct rpc_message msg = {
8677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8678 .rpc_argp = &lgp->args,
8679 .rpc_resp = &lgp->res,
8680 .rpc_cred = lgp->cred,
8682 struct rpc_task_setup task_setup_data = {
8683 .rpc_client = server->client,
8684 .rpc_message = &msg,
8685 .callback_ops = &nfs4_layoutget_call_ops,
8686 .callback_data = lgp,
8687 .flags = RPC_TASK_ASYNC,
8689 struct pnfs_layout_segment *lseg = NULL;
8690 struct nfs4_exception exception = {
8692 .timeout = *timeout,
8696 dprintk("--> %s\n", __func__);
8698 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8699 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8701 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8702 if (!lgp->args.layout.pages) {
8703 nfs4_layoutget_release(lgp);
8704 return ERR_PTR(-ENOMEM);
8706 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8708 lgp->res.layoutp = &lgp->args.layout;
8709 lgp->res.seq_res.sr_slot = NULL;
8710 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8712 task = rpc_run_task(&task_setup_data);
8714 return ERR_CAST(task);
8715 status = rpc_wait_for_completion_task(task);
8717 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8718 *timeout = exception.timeout;
8721 trace_nfs4_layoutget(lgp->args.ctx,
8727 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8728 if (status == 0 && lgp->res.layoutp->len)
8729 lseg = pnfs_layout_process(lgp);
8731 dprintk("<-- %s status=%d\n", __func__, status);
8733 return ERR_PTR(status);
8738 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8740 struct nfs4_layoutreturn *lrp = calldata;
8742 dprintk("--> %s\n", __func__);
8743 nfs4_setup_sequence(lrp->clp,
8744 &lrp->args.seq_args,
8749 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8751 struct nfs4_layoutreturn *lrp = calldata;
8752 struct nfs_server *server;
8754 dprintk("--> %s\n", __func__);
8756 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8759 server = NFS_SERVER(lrp->args.inode);
8760 switch (task->tk_status) {
8761 case -NFS4ERR_OLD_STATEID:
8762 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8767 task->tk_status = 0;
8771 case -NFS4ERR_DELAY:
8772 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8776 dprintk("<-- %s\n", __func__);
8779 task->tk_status = 0;
8780 nfs4_sequence_free_slot(&lrp->res.seq_res);
8781 rpc_restart_call_prepare(task);
8784 static void nfs4_layoutreturn_release(void *calldata)
8786 struct nfs4_layoutreturn *lrp = calldata;
8787 struct pnfs_layout_hdr *lo = lrp->args.layout;
8789 dprintk("--> %s\n", __func__);
8790 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8791 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8792 nfs4_sequence_free_slot(&lrp->res.seq_res);
8793 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8794 lrp->ld_private.ops->free(&lrp->ld_private);
8795 pnfs_put_layout_hdr(lrp->args.layout);
8796 nfs_iput_and_deactive(lrp->inode);
8798 dprintk("<-- %s\n", __func__);
8801 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8802 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8803 .rpc_call_done = nfs4_layoutreturn_done,
8804 .rpc_release = nfs4_layoutreturn_release,
8807 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8809 struct rpc_task *task;
8810 struct rpc_message msg = {
8811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8812 .rpc_argp = &lrp->args,
8813 .rpc_resp = &lrp->res,
8814 .rpc_cred = lrp->cred,
8816 struct rpc_task_setup task_setup_data = {
8817 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8818 .rpc_message = &msg,
8819 .callback_ops = &nfs4_layoutreturn_call_ops,
8820 .callback_data = lrp,
8824 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8825 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8826 &task_setup_data.rpc_client, &msg);
8828 dprintk("--> %s\n", __func__);
8830 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8832 nfs4_layoutreturn_release(lrp);
8835 task_setup_data.flags |= RPC_TASK_ASYNC;
8837 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8838 task = rpc_run_task(&task_setup_data);
8840 return PTR_ERR(task);
8842 status = task->tk_status;
8843 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8844 dprintk("<-- %s status=%d\n", __func__, status);
8850 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8851 struct pnfs_device *pdev,
8852 struct rpc_cred *cred)
8854 struct nfs4_getdeviceinfo_args args = {
8856 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8857 NOTIFY_DEVICEID4_DELETE,
8859 struct nfs4_getdeviceinfo_res res = {
8862 struct rpc_message msg = {
8863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8870 dprintk("--> %s\n", __func__);
8871 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8872 if (res.notification & ~args.notify_types)
8873 dprintk("%s: unsupported notification\n", __func__);
8874 if (res.notification != args.notify_types)
8877 dprintk("<-- %s status=%d\n", __func__, status);
8882 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8883 struct pnfs_device *pdev,
8884 struct rpc_cred *cred)
8886 struct nfs4_exception exception = { };
8890 err = nfs4_handle_exception(server,
8891 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8893 } while (exception.retry);
8896 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8898 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8900 struct nfs4_layoutcommit_data *data = calldata;
8901 struct nfs_server *server = NFS_SERVER(data->args.inode);
8903 nfs4_setup_sequence(server->nfs_client,
8904 &data->args.seq_args,
8910 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8912 struct nfs4_layoutcommit_data *data = calldata;
8913 struct nfs_server *server = NFS_SERVER(data->args.inode);
8915 if (!nfs41_sequence_done(task, &data->res.seq_res))
8918 switch (task->tk_status) { /* Just ignore these failures */
8919 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8920 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8921 case -NFS4ERR_BADLAYOUT: /* no layout */
8922 case -NFS4ERR_GRACE: /* loca_recalim always false */
8923 task->tk_status = 0;
8927 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8928 rpc_restart_call_prepare(task);
8934 static void nfs4_layoutcommit_release(void *calldata)
8936 struct nfs4_layoutcommit_data *data = calldata;
8938 pnfs_cleanup_layoutcommit(data);
8939 nfs_post_op_update_inode_force_wcc(data->args.inode,
8941 put_rpccred(data->cred);
8942 nfs_iput_and_deactive(data->inode);
8946 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8947 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8948 .rpc_call_done = nfs4_layoutcommit_done,
8949 .rpc_release = nfs4_layoutcommit_release,
8953 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8955 struct rpc_message msg = {
8956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8957 .rpc_argp = &data->args,
8958 .rpc_resp = &data->res,
8959 .rpc_cred = data->cred,
8961 struct rpc_task_setup task_setup_data = {
8962 .task = &data->task,
8963 .rpc_client = NFS_CLIENT(data->args.inode),
8964 .rpc_message = &msg,
8965 .callback_ops = &nfs4_layoutcommit_ops,
8966 .callback_data = data,
8968 struct rpc_task *task;
8971 dprintk("NFS: initiating layoutcommit call. sync %d "
8972 "lbw: %llu inode %lu\n", sync,
8973 data->args.lastbytewritten,
8974 data->args.inode->i_ino);
8977 data->inode = nfs_igrab_and_active(data->args.inode);
8978 if (data->inode == NULL) {
8979 nfs4_layoutcommit_release(data);
8982 task_setup_data.flags = RPC_TASK_ASYNC;
8984 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8985 task = rpc_run_task(&task_setup_data);
8987 return PTR_ERR(task);
8989 status = task->tk_status;
8990 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8991 dprintk("%s: status %d\n", __func__, status);
8997 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8998 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9001 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9002 struct nfs_fsinfo *info,
9003 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9005 struct nfs41_secinfo_no_name_args args = {
9006 .style = SECINFO_STYLE_CURRENT_FH,
9008 struct nfs4_secinfo_res res = {
9011 struct rpc_message msg = {
9012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9016 struct rpc_clnt *clnt = server->client;
9017 struct rpc_cred *cred = NULL;
9020 if (use_integrity) {
9021 clnt = server->nfs_client->cl_rpcclient;
9022 cred = nfs4_get_clid_cred(server->nfs_client);
9023 msg.rpc_cred = cred;
9026 dprintk("--> %s\n", __func__);
9027 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9029 dprintk("<-- %s status=%d\n", __func__, status);
9038 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9039 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9041 struct nfs4_exception exception = { };
9044 /* first try using integrity protection */
9045 err = -NFS4ERR_WRONGSEC;
9047 /* try to use integrity protection with machine cred */
9048 if (_nfs4_is_integrity_protected(server->nfs_client))
9049 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9053 * if unable to use integrity protection, or SECINFO with
9054 * integrity protection returns NFS4ERR_WRONGSEC (which is
9055 * disallowed by spec, but exists in deployed servers) use
9056 * the current filesystem's rpc_client and the user cred.
9058 if (err == -NFS4ERR_WRONGSEC)
9059 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9064 case -NFS4ERR_WRONGSEC:
9068 err = nfs4_handle_exception(server, err, &exception);
9070 } while (exception.retry);
9076 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9077 struct nfs_fsinfo *info)
9081 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9082 struct nfs4_secinfo_flavors *flavors;
9083 struct nfs4_secinfo4 *secinfo;
9086 page = alloc_page(GFP_KERNEL);
9092 flavors = page_address(page);
9093 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9096 * Fall back on "guess and check" method if
9097 * the server doesn't support SECINFO_NO_NAME
9099 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9100 err = nfs4_find_root_sec(server, fhandle, info);
9106 for (i = 0; i < flavors->num_flavors; i++) {
9107 secinfo = &flavors->flavors[i];
9109 switch (secinfo->flavor) {
9113 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9114 &secinfo->flavor_info);
9117 flavor = RPC_AUTH_MAXFLAVOR;
9121 if (!nfs_auth_info_match(&server->auth_info, flavor))
9122 flavor = RPC_AUTH_MAXFLAVOR;
9124 if (flavor != RPC_AUTH_MAXFLAVOR) {
9125 err = nfs4_lookup_root_sec(server, fhandle,
9132 if (flavor == RPC_AUTH_MAXFLAVOR)
9143 static int _nfs41_test_stateid(struct nfs_server *server,
9144 nfs4_stateid *stateid,
9145 struct rpc_cred *cred)
9148 struct nfs41_test_stateid_args args = {
9151 struct nfs41_test_stateid_res res;
9152 struct rpc_message msg = {
9153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9158 struct rpc_clnt *rpc_client = server->client;
9160 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9163 dprintk("NFS call test_stateid %p\n", stateid);
9164 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
9165 nfs4_set_sequence_privileged(&args.seq_args);
9166 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9167 &args.seq_args, &res.seq_res);
9168 if (status != NFS_OK) {
9169 dprintk("NFS reply test_stateid: failed, %d\n", status);
9172 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9176 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9177 int err, struct nfs4_exception *exception)
9179 exception->retry = 0;
9181 case -NFS4ERR_DELAY:
9182 case -NFS4ERR_RETRY_UNCACHED_REP:
9183 nfs4_handle_exception(server, err, exception);
9185 case -NFS4ERR_BADSESSION:
9186 case -NFS4ERR_BADSLOT:
9187 case -NFS4ERR_BAD_HIGH_SLOT:
9188 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9189 case -NFS4ERR_DEADSESSION:
9190 nfs4_do_handle_exception(server, err, exception);
9195 * nfs41_test_stateid - perform a TEST_STATEID operation
9197 * @server: server / transport on which to perform the operation
9198 * @stateid: state ID to test
9201 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9202 * Otherwise a negative NFS4ERR value is returned if the operation
9203 * failed or the state ID is not currently valid.
9205 static int nfs41_test_stateid(struct nfs_server *server,
9206 nfs4_stateid *stateid,
9207 struct rpc_cred *cred)
9209 struct nfs4_exception exception = { };
9212 err = _nfs41_test_stateid(server, stateid, cred);
9213 nfs4_handle_delay_or_session_error(server, err, &exception);
9214 } while (exception.retry);
9218 struct nfs_free_stateid_data {
9219 struct nfs_server *server;
9220 struct nfs41_free_stateid_args args;
9221 struct nfs41_free_stateid_res res;
9224 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9226 struct nfs_free_stateid_data *data = calldata;
9227 nfs4_setup_sequence(data->server->nfs_client,
9228 &data->args.seq_args,
9233 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9235 struct nfs_free_stateid_data *data = calldata;
9237 nfs41_sequence_done(task, &data->res.seq_res);
9239 switch (task->tk_status) {
9240 case -NFS4ERR_DELAY:
9241 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9242 rpc_restart_call_prepare(task);
9246 static void nfs41_free_stateid_release(void *calldata)
9251 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9252 .rpc_call_prepare = nfs41_free_stateid_prepare,
9253 .rpc_call_done = nfs41_free_stateid_done,
9254 .rpc_release = nfs41_free_stateid_release,
9257 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9258 const nfs4_stateid *stateid,
9259 struct rpc_cred *cred,
9262 struct rpc_message msg = {
9263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9266 struct rpc_task_setup task_setup = {
9267 .rpc_client = server->client,
9268 .rpc_message = &msg,
9269 .callback_ops = &nfs41_free_stateid_ops,
9270 .flags = RPC_TASK_ASYNC,
9272 struct nfs_free_stateid_data *data;
9274 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9275 &task_setup.rpc_client, &msg);
9277 dprintk("NFS call free_stateid %p\n", stateid);
9278 data = kmalloc(sizeof(*data), GFP_NOFS);
9280 return ERR_PTR(-ENOMEM);
9281 data->server = server;
9282 nfs4_stateid_copy(&data->args.stateid, stateid);
9284 task_setup.callback_data = data;
9286 msg.rpc_argp = &data->args;
9287 msg.rpc_resp = &data->res;
9288 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9290 nfs4_set_sequence_privileged(&data->args.seq_args);
9292 return rpc_run_task(&task_setup);
9296 * nfs41_free_stateid - perform a FREE_STATEID operation
9298 * @server: server / transport on which to perform the operation
9299 * @stateid: state ID to release
9301 * @is_recovery: set to true if this call needs to be privileged
9303 * Note: this function is always asynchronous.
9305 static int nfs41_free_stateid(struct nfs_server *server,
9306 const nfs4_stateid *stateid,
9307 struct rpc_cred *cred,
9310 struct rpc_task *task;
9312 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9314 return PTR_ERR(task);
9320 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9322 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9324 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9325 nfs4_free_lock_state(server, lsp);
9328 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9329 const nfs4_stateid *s2)
9331 if (s1->type != s2->type)
9334 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9337 if (s1->seqid == s2->seqid)
9340 return s1->seqid == 0 || s2->seqid == 0;
9343 #endif /* CONFIG_NFS_V4_1 */
9345 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9346 const nfs4_stateid *s2)
9348 return nfs4_stateid_match(s1, s2);
9352 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9353 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9354 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9355 .recover_open = nfs4_open_reclaim,
9356 .recover_lock = nfs4_lock_reclaim,
9357 .establish_clid = nfs4_init_clientid,
9358 .detect_trunking = nfs40_discover_server_trunking,
9361 #if defined(CONFIG_NFS_V4_1)
9362 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9363 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9364 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9365 .recover_open = nfs4_open_reclaim,
9366 .recover_lock = nfs4_lock_reclaim,
9367 .establish_clid = nfs41_init_clientid,
9368 .reclaim_complete = nfs41_proc_reclaim_complete,
9369 .detect_trunking = nfs41_discover_server_trunking,
9371 #endif /* CONFIG_NFS_V4_1 */
9373 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9374 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9375 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9376 .recover_open = nfs40_open_expired,
9377 .recover_lock = nfs4_lock_expired,
9378 .establish_clid = nfs4_init_clientid,
9381 #if defined(CONFIG_NFS_V4_1)
9382 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9383 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9384 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9385 .recover_open = nfs41_open_expired,
9386 .recover_lock = nfs41_lock_expired,
9387 .establish_clid = nfs41_init_clientid,
9389 #endif /* CONFIG_NFS_V4_1 */
9391 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9392 .sched_state_renewal = nfs4_proc_async_renew,
9393 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9394 .renew_lease = nfs4_proc_renew,
9397 #if defined(CONFIG_NFS_V4_1)
9398 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9399 .sched_state_renewal = nfs41_proc_async_sequence,
9400 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9401 .renew_lease = nfs4_proc_sequence,
9405 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9406 .get_locations = _nfs40_proc_get_locations,
9407 .fsid_present = _nfs40_proc_fsid_present,
9410 #if defined(CONFIG_NFS_V4_1)
9411 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9412 .get_locations = _nfs41_proc_get_locations,
9413 .fsid_present = _nfs41_proc_fsid_present,
9415 #endif /* CONFIG_NFS_V4_1 */
9417 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9419 .init_caps = NFS_CAP_READDIRPLUS
9420 | NFS_CAP_ATOMIC_OPEN
9421 | NFS_CAP_POSIX_LOCK,
9422 .init_client = nfs40_init_client,
9423 .shutdown_client = nfs40_shutdown_client,
9424 .match_stateid = nfs4_match_stateid,
9425 .find_root_sec = nfs4_find_root_sec,
9426 .free_lock_state = nfs4_release_lockowner,
9427 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9428 .alloc_seqid = nfs_alloc_seqid,
9429 .call_sync_ops = &nfs40_call_sync_ops,
9430 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9431 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9432 .state_renewal_ops = &nfs40_state_renewal_ops,
9433 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9436 #if defined(CONFIG_NFS_V4_1)
9437 static struct nfs_seqid *
9438 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9443 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9445 .init_caps = NFS_CAP_READDIRPLUS
9446 | NFS_CAP_ATOMIC_OPEN
9447 | NFS_CAP_POSIX_LOCK
9448 | NFS_CAP_STATEID_NFSV41
9449 | NFS_CAP_ATOMIC_OPEN_V1,
9450 .init_client = nfs41_init_client,
9451 .shutdown_client = nfs41_shutdown_client,
9452 .match_stateid = nfs41_match_stateid,
9453 .find_root_sec = nfs41_find_root_sec,
9454 .free_lock_state = nfs41_free_lock_state,
9455 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9456 .alloc_seqid = nfs_alloc_no_seqid,
9457 .session_trunk = nfs4_test_session_trunk,
9458 .call_sync_ops = &nfs41_call_sync_ops,
9459 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9460 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9461 .state_renewal_ops = &nfs41_state_renewal_ops,
9462 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9466 #if defined(CONFIG_NFS_V4_2)
9467 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9469 .init_caps = NFS_CAP_READDIRPLUS
9470 | NFS_CAP_ATOMIC_OPEN
9471 | NFS_CAP_POSIX_LOCK
9472 | NFS_CAP_STATEID_NFSV41
9473 | NFS_CAP_ATOMIC_OPEN_V1
9476 | NFS_CAP_DEALLOCATE
9478 | NFS_CAP_LAYOUTSTATS
9480 .init_client = nfs41_init_client,
9481 .shutdown_client = nfs41_shutdown_client,
9482 .match_stateid = nfs41_match_stateid,
9483 .find_root_sec = nfs41_find_root_sec,
9484 .free_lock_state = nfs41_free_lock_state,
9485 .call_sync_ops = &nfs41_call_sync_ops,
9486 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9487 .alloc_seqid = nfs_alloc_no_seqid,
9488 .session_trunk = nfs4_test_session_trunk,
9489 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9490 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9491 .state_renewal_ops = &nfs41_state_renewal_ops,
9492 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9496 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9497 [0] = &nfs_v4_0_minor_ops,
9498 #if defined(CONFIG_NFS_V4_1)
9499 [1] = &nfs_v4_1_minor_ops,
9501 #if defined(CONFIG_NFS_V4_2)
9502 [2] = &nfs_v4_2_minor_ops,
9506 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9508 ssize_t error, error2;
9510 error = generic_listxattr(dentry, list, size);
9518 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9521 return error + error2;
9524 static const struct inode_operations nfs4_dir_inode_operations = {
9525 .create = nfs_create,
9526 .lookup = nfs_lookup,
9527 .atomic_open = nfs_atomic_open,
9529 .unlink = nfs_unlink,
9530 .symlink = nfs_symlink,
9534 .rename = nfs_rename,
9535 .permission = nfs_permission,
9536 .getattr = nfs_getattr,
9537 .setattr = nfs_setattr,
9538 .listxattr = nfs4_listxattr,
9541 static const struct inode_operations nfs4_file_inode_operations = {
9542 .permission = nfs_permission,
9543 .getattr = nfs_getattr,
9544 .setattr = nfs_setattr,
9545 .listxattr = nfs4_listxattr,
9548 const struct nfs_rpc_ops nfs_v4_clientops = {
9549 .version = 4, /* protocol version */
9550 .dentry_ops = &nfs4_dentry_operations,
9551 .dir_inode_ops = &nfs4_dir_inode_operations,
9552 .file_inode_ops = &nfs4_file_inode_operations,
9553 .file_ops = &nfs4_file_operations,
9554 .getroot = nfs4_proc_get_root,
9555 .submount = nfs4_submount,
9556 .try_mount = nfs4_try_mount,
9557 .getattr = nfs4_proc_getattr,
9558 .setattr = nfs4_proc_setattr,
9559 .lookup = nfs4_proc_lookup,
9560 .lookupp = nfs4_proc_lookupp,
9561 .access = nfs4_proc_access,
9562 .readlink = nfs4_proc_readlink,
9563 .create = nfs4_proc_create,
9564 .remove = nfs4_proc_remove,
9565 .unlink_setup = nfs4_proc_unlink_setup,
9566 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9567 .unlink_done = nfs4_proc_unlink_done,
9568 .rename_setup = nfs4_proc_rename_setup,
9569 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9570 .rename_done = nfs4_proc_rename_done,
9571 .link = nfs4_proc_link,
9572 .symlink = nfs4_proc_symlink,
9573 .mkdir = nfs4_proc_mkdir,
9574 .rmdir = nfs4_proc_remove,
9575 .readdir = nfs4_proc_readdir,
9576 .mknod = nfs4_proc_mknod,
9577 .statfs = nfs4_proc_statfs,
9578 .fsinfo = nfs4_proc_fsinfo,
9579 .pathconf = nfs4_proc_pathconf,
9580 .set_capabilities = nfs4_server_capabilities,
9581 .decode_dirent = nfs4_decode_dirent,
9582 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9583 .read_setup = nfs4_proc_read_setup,
9584 .read_done = nfs4_read_done,
9585 .write_setup = nfs4_proc_write_setup,
9586 .write_done = nfs4_write_done,
9587 .commit_setup = nfs4_proc_commit_setup,
9588 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9589 .commit_done = nfs4_commit_done,
9590 .lock = nfs4_proc_lock,
9591 .clear_acl_cache = nfs4_zap_acl_attr,
9592 .close_context = nfs4_close_context,
9593 .open_context = nfs4_atomic_open,
9594 .have_delegation = nfs4_have_delegation,
9595 .return_delegation = nfs4_inode_return_delegation,
9596 .alloc_client = nfs4_alloc_client,
9597 .init_client = nfs4_init_client,
9598 .free_client = nfs4_free_client,
9599 .create_server = nfs4_create_server,
9600 .clone_server = nfs_clone_server,
9603 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9604 .name = XATTR_NAME_NFSV4_ACL,
9605 .list = nfs4_xattr_list_nfs4_acl,
9606 .get = nfs4_xattr_get_nfs4_acl,
9607 .set = nfs4_xattr_set_nfs4_acl,
9610 const struct xattr_handler *nfs4_xattr_handlers[] = {
9611 &nfs4_xattr_nfs4_acl_handler,
9612 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9613 &nfs4_xattr_nfs4_label_handler,