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"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
72 #include "nfs4trace.h"
74 #define NFSDBG_FACILITY NFSDBG_PROC
76 #define NFS4_BITMASK_SZ 3
78 #define NFS4_POLL_RETRY_MIN (HZ/10)
79 #define NFS4_POLL_RETRY_MAX (15*HZ)
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 struct nfs_fattr *fattr, struct inode *inode);
98 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
99 struct nfs_fattr *fattr, struct iattr *sattr,
100 struct nfs_open_context *ctx, struct nfs4_label *ilabel);
101 #ifdef CONFIG_NFS_V4_1
102 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 const struct cred *cred,
104 struct nfs4_slot *slot,
106 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 const struct cred *);
108 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 const struct cred *, bool);
112 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
113 static inline struct nfs4_label *
114 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
115 struct iattr *sattr, struct nfs4_label *label)
122 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
130 err = security_dentry_init_security(dentry, sattr->ia_mode,
131 &dentry->d_name, NULL,
132 (void **)&label->label, &label->len);
139 nfs4_label_release_security(struct nfs4_label *label)
142 security_release_secctx(label->label, label->len);
144 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
147 return server->attr_bitmask;
149 return server->attr_bitmask_nl;
152 static inline struct nfs4_label *
153 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
154 struct iattr *sattr, struct nfs4_label *l)
157 nfs4_label_release_security(struct nfs4_label *label)
160 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
161 { return server->attr_bitmask; }
164 /* Prevent leaks of NFSv4 errors into userland */
165 static int nfs4_map_errors(int err)
170 case -NFS4ERR_RESOURCE:
171 case -NFS4ERR_LAYOUTTRYLATER:
172 case -NFS4ERR_RECALLCONFLICT:
174 case -NFS4ERR_WRONGSEC:
175 case -NFS4ERR_WRONG_CRED:
177 case -NFS4ERR_BADOWNER:
178 case -NFS4ERR_BADNAME:
180 case -NFS4ERR_SHARE_DENIED:
182 case -NFS4ERR_MINOR_VERS_MISMATCH:
183 return -EPROTONOSUPPORT;
184 case -NFS4ERR_FILE_OPEN:
186 case -NFS4ERR_NOT_SAME:
189 dprintk("%s could not handle NFSv4 error %d\n",
197 * This is our standard bitmap for GETATTR requests.
199 const u32 nfs4_fattr_bitmap[3] = {
201 | FATTR4_WORD0_CHANGE
204 | FATTR4_WORD0_FILEID,
206 | FATTR4_WORD1_NUMLINKS
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
213 | FATTR4_WORD1_TIME_MODIFY
214 | FATTR4_WORD1_MOUNTED_ON_FILEID,
215 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
216 FATTR4_WORD2_SECURITY_LABEL
220 static const u32 nfs4_pnfs_open_bitmap[3] = {
222 | FATTR4_WORD0_CHANGE
225 | FATTR4_WORD0_FILEID,
227 | FATTR4_WORD1_NUMLINKS
229 | FATTR4_WORD1_OWNER_GROUP
230 | FATTR4_WORD1_RAWDEV
231 | FATTR4_WORD1_SPACE_USED
232 | FATTR4_WORD1_TIME_ACCESS
233 | FATTR4_WORD1_TIME_METADATA
234 | FATTR4_WORD1_TIME_MODIFY,
235 FATTR4_WORD2_MDSTHRESHOLD
236 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
237 | FATTR4_WORD2_SECURITY_LABEL
241 static const u32 nfs4_open_noattr_bitmap[3] = {
243 | FATTR4_WORD0_FILEID,
246 const u32 nfs4_statfs_bitmap[3] = {
247 FATTR4_WORD0_FILES_AVAIL
248 | FATTR4_WORD0_FILES_FREE
249 | FATTR4_WORD0_FILES_TOTAL,
250 FATTR4_WORD1_SPACE_AVAIL
251 | FATTR4_WORD1_SPACE_FREE
252 | FATTR4_WORD1_SPACE_TOTAL
255 const u32 nfs4_pathconf_bitmap[3] = {
257 | FATTR4_WORD0_MAXNAME,
261 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
262 | FATTR4_WORD0_MAXREAD
263 | FATTR4_WORD0_MAXWRITE
264 | FATTR4_WORD0_LEASE_TIME,
265 FATTR4_WORD1_TIME_DELTA
266 | FATTR4_WORD1_FS_LAYOUT_TYPES,
267 FATTR4_WORD2_LAYOUT_BLKSIZE
268 | FATTR4_WORD2_CLONE_BLKSIZE
269 | FATTR4_WORD2_CHANGE_ATTR_TYPE
270 | FATTR4_WORD2_XATTR_SUPPORT
273 const u32 nfs4_fs_locations_bitmap[3] = {
277 | FATTR4_WORD0_FILEID
278 | FATTR4_WORD0_FS_LOCATIONS,
280 | FATTR4_WORD1_OWNER_GROUP
281 | FATTR4_WORD1_RAWDEV
282 | FATTR4_WORD1_SPACE_USED
283 | FATTR4_WORD1_TIME_ACCESS
284 | FATTR4_WORD1_TIME_METADATA
285 | FATTR4_WORD1_TIME_MODIFY
286 | FATTR4_WORD1_MOUNTED_ON_FILEID,
289 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
290 struct inode *inode, unsigned long flags)
292 unsigned long cache_validity;
294 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
295 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
298 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
300 /* Remove the attributes over which we have full control */
301 dst[1] &= ~FATTR4_WORD1_RAWDEV;
302 if (!(cache_validity & NFS_INO_INVALID_SIZE))
303 dst[0] &= ~FATTR4_WORD0_SIZE;
305 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
306 dst[0] &= ~FATTR4_WORD0_CHANGE;
308 if (!(cache_validity & NFS_INO_INVALID_MODE))
309 dst[1] &= ~FATTR4_WORD1_MODE;
310 if (!(cache_validity & NFS_INO_INVALID_OTHER))
311 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
314 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
315 struct nfs4_readdir_arg *readdir)
317 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
321 readdir->cookie = cookie;
322 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
327 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
332 * NFSv4 servers do not return entries for '.' and '..'
333 * Therefore, we fake these entries here. We let '.'
334 * have cookie 0 and '..' have cookie 1. Note that
335 * when talking to the server, we always send cookie 0
338 start = p = kmap_atomic(*readdir->pages);
341 *p++ = xdr_one; /* next */
342 *p++ = xdr_zero; /* cookie, first word */
343 *p++ = xdr_one; /* cookie, second word */
344 *p++ = xdr_one; /* entry len */
345 memcpy(p, ".\0\0\0", 4); /* entry */
347 *p++ = xdr_one; /* bitmap length */
348 *p++ = htonl(attrs); /* bitmap */
349 *p++ = htonl(12); /* attribute buffer length */
350 *p++ = htonl(NF4DIR);
351 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
354 *p++ = xdr_one; /* next */
355 *p++ = xdr_zero; /* cookie, first word */
356 *p++ = xdr_two; /* cookie, second word */
357 *p++ = xdr_two; /* entry len */
358 memcpy(p, "..\0\0", 4); /* entry */
360 *p++ = xdr_one; /* bitmap length */
361 *p++ = htonl(attrs); /* bitmap */
362 *p++ = htonl(12); /* attribute buffer length */
363 *p++ = htonl(NF4DIR);
364 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
366 readdir->pgbase = (char *)p - (char *)start;
367 readdir->count -= readdir->pgbase;
368 kunmap_atomic(start);
371 static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
373 if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
374 fattr->pre_change_attr = version;
375 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
379 static void nfs4_test_and_free_stateid(struct nfs_server *server,
380 nfs4_stateid *stateid,
381 const struct cred *cred)
383 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
385 ops->test_and_free_expired(server, stateid, cred);
388 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
389 nfs4_stateid *stateid,
390 const struct cred *cred)
392 stateid->type = NFS4_REVOKED_STATEID_TYPE;
393 nfs4_test_and_free_stateid(server, stateid, cred);
396 static void nfs4_free_revoked_stateid(struct nfs_server *server,
397 const nfs4_stateid *stateid,
398 const struct cred *cred)
402 nfs4_stateid_copy(&tmp, stateid);
403 __nfs4_free_revoked_stateid(server, &tmp, cred);
406 static long nfs4_update_delay(long *timeout)
410 return NFS4_POLL_RETRY_MAX;
412 *timeout = NFS4_POLL_RETRY_MIN;
413 if (*timeout > NFS4_POLL_RETRY_MAX)
414 *timeout = NFS4_POLL_RETRY_MAX;
420 static int nfs4_delay_killable(long *timeout)
424 __set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
425 schedule_timeout(nfs4_update_delay(timeout));
426 if (!__fatal_signal_pending(current))
431 static int nfs4_delay_interruptible(long *timeout)
435 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE_UNSAFE);
436 schedule_timeout(nfs4_update_delay(timeout));
437 if (!signal_pending(current))
439 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
442 static int nfs4_delay(long *timeout, bool interruptible)
445 return nfs4_delay_interruptible(timeout);
446 return nfs4_delay_killable(timeout);
449 static const nfs4_stateid *
450 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
454 switch (stateid->type) {
455 case NFS4_OPEN_STATEID_TYPE:
456 case NFS4_LOCK_STATEID_TYPE:
457 case NFS4_DELEGATION_STATEID_TYPE:
465 /* This is the error handling routine for processes that are allowed
468 static int nfs4_do_handle_exception(struct nfs_server *server,
469 int errorcode, struct nfs4_exception *exception)
471 struct nfs_client *clp = server->nfs_client;
472 struct nfs4_state *state = exception->state;
473 const nfs4_stateid *stateid;
474 struct inode *inode = exception->inode;
477 exception->delay = 0;
478 exception->recovering = 0;
479 exception->retry = 0;
481 stateid = nfs4_recoverable_stateid(exception->stateid);
482 if (stateid == NULL && state != NULL)
483 stateid = nfs4_recoverable_stateid(&state->stateid);
488 case -NFS4ERR_BADHANDLE:
490 if (inode != NULL && S_ISREG(inode->i_mode))
491 pnfs_destroy_layout(NFS_I(inode));
493 case -NFS4ERR_DELEG_REVOKED:
494 case -NFS4ERR_ADMIN_REVOKED:
495 case -NFS4ERR_EXPIRED:
496 case -NFS4ERR_BAD_STATEID:
497 case -NFS4ERR_PARTNER_NO_AUTH:
498 if (inode != NULL && stateid != NULL) {
499 nfs_inode_find_state_and_recover(inode,
501 goto wait_on_recovery;
504 case -NFS4ERR_OPENMODE:
508 err = nfs_async_inode_return_delegation(inode,
511 goto wait_on_recovery;
512 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
513 exception->retry = 1;
519 ret = nfs4_schedule_stateid_recovery(server, state);
522 goto wait_on_recovery;
523 case -NFS4ERR_STALE_STATEID:
524 case -NFS4ERR_STALE_CLIENTID:
525 nfs4_schedule_lease_recovery(clp);
526 goto wait_on_recovery;
528 ret = nfs4_schedule_migration_recovery(server);
531 goto wait_on_recovery;
532 case -NFS4ERR_LEASE_MOVED:
533 nfs4_schedule_lease_moved_recovery(clp);
534 goto wait_on_recovery;
535 #if defined(CONFIG_NFS_V4_1)
536 case -NFS4ERR_BADSESSION:
537 case -NFS4ERR_BADSLOT:
538 case -NFS4ERR_BAD_HIGH_SLOT:
539 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
540 case -NFS4ERR_DEADSESSION:
541 case -NFS4ERR_SEQ_FALSE_RETRY:
542 case -NFS4ERR_SEQ_MISORDERED:
543 /* Handled in nfs41_sequence_process() */
544 goto wait_on_recovery;
545 #endif /* defined(CONFIG_NFS_V4_1) */
546 case -NFS4ERR_FILE_OPEN:
547 if (exception->timeout > HZ) {
548 /* We have retried a decent amount, time to
556 nfs_inc_server_stats(server, NFSIOS_DELAY);
559 case -NFS4ERR_LAYOUTTRYLATER:
560 case -NFS4ERR_RECALLCONFLICT:
561 exception->delay = 1;
564 case -NFS4ERR_RETRY_UNCACHED_REP:
565 case -NFS4ERR_OLD_STATEID:
566 exception->retry = 1;
568 case -NFS4ERR_BADOWNER:
569 /* The following works around a Linux server bug! */
570 case -NFS4ERR_BADNAME:
571 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
572 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
573 exception->retry = 1;
574 printk(KERN_WARNING "NFS: v4 server %s "
575 "does not accept raw "
577 "Reenabling the idmapper.\n",
578 server->nfs_client->cl_hostname);
581 /* We failed to handle the error */
582 return nfs4_map_errors(ret);
584 exception->recovering = 1;
589 * Track the number of NFS4ERR_DELAY related retransmissions and return
590 * EAGAIN if the 'softerr' mount option is set, and we've exceeded the limit
591 * set by 'nfs_delay_retrans'.
593 static int nfs4_exception_should_retrans(const struct nfs_server *server,
594 struct nfs4_exception *exception)
596 if (server->flags & NFS_MOUNT_SOFTERR && nfs_delay_retrans >= 0) {
597 if (exception->retrans++ >= (unsigned short)nfs_delay_retrans)
603 /* This is the error handling routine for processes that are allowed
606 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
608 struct nfs_client *clp = server->nfs_client;
611 ret = nfs4_do_handle_exception(server, errorcode, exception);
612 if (exception->delay) {
613 int ret2 = nfs4_exception_should_retrans(server, exception);
615 exception->retry = 0;
618 ret = nfs4_delay(&exception->timeout,
619 exception->interruptible);
622 if (exception->recovering) {
623 if (exception->task_is_privileged)
625 ret = nfs4_wait_clnt_recover(clp);
626 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
633 exception->retry = 1;
638 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
639 int errorcode, struct nfs4_exception *exception)
641 struct nfs_client *clp = server->nfs_client;
644 ret = nfs4_do_handle_exception(server, errorcode, exception);
645 if (exception->delay) {
646 int ret2 = nfs4_exception_should_retrans(server, exception);
648 exception->retry = 0;
651 rpc_delay(task, nfs4_update_delay(&exception->timeout));
654 if (exception->recovering) {
655 if (exception->task_is_privileged)
657 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
658 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
659 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
662 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
667 exception->retry = 1;
669 * For NFS4ERR_MOVED, the client transport will need to
670 * be recomputed after migration recovery has completed.
672 if (errorcode == -NFS4ERR_MOVED)
673 rpc_task_release_transport(task);
679 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
680 struct nfs4_state *state, long *timeout)
682 struct nfs4_exception exception = {
686 if (task->tk_status >= 0)
689 exception.timeout = *timeout;
690 task->tk_status = nfs4_async_handle_exception(task, server,
693 if (exception.delay && timeout)
694 *timeout = exception.timeout;
701 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
702 * or 'false' otherwise.
704 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
706 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
707 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
710 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
712 spin_lock(&clp->cl_lock);
713 if (time_before(clp->cl_last_renewal,timestamp))
714 clp->cl_last_renewal = timestamp;
715 spin_unlock(&clp->cl_lock);
718 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
720 struct nfs_client *clp = server->nfs_client;
722 if (!nfs4_has_session(clp))
723 do_renew_lease(clp, timestamp);
726 struct nfs4_call_sync_data {
727 const struct nfs_server *seq_server;
728 struct nfs4_sequence_args *seq_args;
729 struct nfs4_sequence_res *seq_res;
732 void nfs4_init_sequence(struct nfs4_sequence_args *args,
733 struct nfs4_sequence_res *res, int cache_reply,
736 args->sa_slot = NULL;
737 args->sa_cache_this = cache_reply;
738 args->sa_privileged = privileged;
743 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
745 struct nfs4_slot *slot = res->sr_slot;
746 struct nfs4_slot_table *tbl;
749 spin_lock(&tbl->slot_tbl_lock);
750 if (!nfs41_wake_and_assign_slot(tbl, slot))
751 nfs4_free_slot(tbl, slot);
752 spin_unlock(&tbl->slot_tbl_lock);
757 static int nfs40_sequence_done(struct rpc_task *task,
758 struct nfs4_sequence_res *res)
760 if (res->sr_slot != NULL)
761 nfs40_sequence_free_slot(res);
765 #if defined(CONFIG_NFS_V4_1)
767 static void nfs41_release_slot(struct nfs4_slot *slot)
769 struct nfs4_session *session;
770 struct nfs4_slot_table *tbl;
771 bool send_new_highest_used_slotid = false;
776 session = tbl->session;
778 /* Bump the slot sequence number */
783 spin_lock(&tbl->slot_tbl_lock);
784 /* Be nice to the server: try to ensure that the last transmitted
785 * value for highest_user_slotid <= target_highest_slotid
787 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
788 send_new_highest_used_slotid = true;
790 if (nfs41_wake_and_assign_slot(tbl, slot)) {
791 send_new_highest_used_slotid = false;
794 nfs4_free_slot(tbl, slot);
796 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
797 send_new_highest_used_slotid = false;
799 spin_unlock(&tbl->slot_tbl_lock);
800 if (send_new_highest_used_slotid)
801 nfs41_notify_server(session->clp);
802 if (waitqueue_active(&tbl->slot_waitq))
803 wake_up_all(&tbl->slot_waitq);
806 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
808 nfs41_release_slot(res->sr_slot);
812 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
815 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
816 slot->seq_nr_highest_sent = seqnr;
818 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, u32 seqnr)
820 nfs4_slot_sequence_record_sent(slot, seqnr);
821 slot->seq_nr_last_acked = seqnr;
824 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
825 struct nfs4_slot *slot)
827 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
829 rpc_put_task_async(task);
832 static int nfs41_sequence_process(struct rpc_task *task,
833 struct nfs4_sequence_res *res)
835 struct nfs4_session *session;
836 struct nfs4_slot *slot = res->sr_slot;
837 struct nfs_client *clp;
843 /* don't increment the sequence number if the task wasn't sent */
844 if (!RPC_WAS_SENT(task) || slot->seq_done)
847 session = slot->table->session;
850 trace_nfs4_sequence_done(session, res);
852 status = res->sr_status;
853 if (task->tk_status == -NFS4ERR_DEADSESSION)
854 status = -NFS4ERR_DEADSESSION;
856 /* Check the SEQUENCE operation status */
859 /* Mark this sequence number as having been acked */
860 nfs4_slot_sequence_acked(slot, slot->seq_nr);
861 /* Update the slot's sequence and clientid lease timer */
863 do_renew_lease(clp, res->sr_timestamp);
864 /* Check sequence flags */
865 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
867 nfs41_update_target_slotid(slot->table, slot, res);
871 * sr_status remains 1 if an RPC level error occurred.
872 * The server may or may not have processed the sequence
875 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
879 /* The server detected a resend of the RPC call and
880 * returned NFS4ERR_DELAY as per Section 2.10.6.2
883 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
888 case -NFS4ERR_RETRY_UNCACHED_REP:
889 case -NFS4ERR_SEQ_FALSE_RETRY:
891 * The server thinks we tried to replay a request.
892 * Retry the call after bumping the sequence ID.
894 nfs4_slot_sequence_acked(slot, slot->seq_nr);
896 case -NFS4ERR_BADSLOT:
898 * The slot id we used was probably retired. Try again
899 * using a different slot id.
901 if (slot->slot_nr < slot->table->target_highest_slotid)
902 goto session_recover;
904 case -NFS4ERR_SEQ_MISORDERED:
905 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
907 * Were one or more calls using this slot interrupted?
908 * If the server never received the request, then our
909 * transmitted slot sequence number may be too high. However,
910 * if the server did receive the request then it might
911 * accidentally give us a reply with a mismatched operation.
912 * We can sort this out by sending a lone sequence operation
913 * to the server on the same slot.
915 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
917 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
918 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
925 * A retry might be sent while the original request is
926 * still in progress on the replier. The replier SHOULD
927 * deal with the issue by returning NFS4ERR_DELAY as the
928 * reply to SEQUENCE or CB_SEQUENCE operation, but
929 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
931 * Restart the search after a delay.
933 slot->seq_nr = slot->seq_nr_highest_sent;
935 case -NFS4ERR_BADSESSION:
936 case -NFS4ERR_DEADSESSION:
937 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
938 goto session_recover;
940 /* Just update the slot sequence no. */
944 /* The session may be reset by one of the error handlers. */
945 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
949 set_bit(NFS4_SLOT_TBL_DRAINING, &session->fc_slot_table.slot_tbl_state);
950 nfs4_schedule_session_recovery(session, status);
951 dprintk("%s ERROR: %d Reset session\n", __func__, status);
952 nfs41_sequence_free_slot(res);
957 if (rpc_restart_call_prepare(task)) {
958 nfs41_sequence_free_slot(res);
964 if (!rpc_restart_call(task))
966 rpc_delay(task, NFS4_POLL_RETRY_MAX);
970 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
972 if (!nfs41_sequence_process(task, res))
974 if (res->sr_slot != NULL)
975 nfs41_sequence_free_slot(res);
979 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
981 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
983 if (res->sr_slot == NULL)
985 if (res->sr_slot->table->session != NULL)
986 return nfs41_sequence_process(task, res);
987 return nfs40_sequence_done(task, res);
990 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
992 if (res->sr_slot != NULL) {
993 if (res->sr_slot->table->session != NULL)
994 nfs41_sequence_free_slot(res);
996 nfs40_sequence_free_slot(res);
1000 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
1002 if (res->sr_slot == NULL)
1004 if (!res->sr_slot->table->session)
1005 return nfs40_sequence_done(task, res);
1006 return nfs41_sequence_done(task, res);
1008 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1010 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
1012 struct nfs4_call_sync_data *data = calldata;
1014 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
1016 nfs4_setup_sequence(data->seq_server->nfs_client,
1017 data->seq_args, data->seq_res, task);
1020 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
1022 struct nfs4_call_sync_data *data = calldata;
1024 nfs41_sequence_done(task, data->seq_res);
1027 static const struct rpc_call_ops nfs41_call_sync_ops = {
1028 .rpc_call_prepare = nfs41_call_sync_prepare,
1029 .rpc_call_done = nfs41_call_sync_done,
1032 #else /* !CONFIG_NFS_V4_1 */
1034 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1036 return nfs40_sequence_done(task, res);
1039 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1041 if (res->sr_slot != NULL)
1042 nfs40_sequence_free_slot(res);
1045 int nfs4_sequence_done(struct rpc_task *task,
1046 struct nfs4_sequence_res *res)
1048 return nfs40_sequence_done(task, res);
1050 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1052 #endif /* !CONFIG_NFS_V4_1 */
1054 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1056 res->sr_timestamp = jiffies;
1057 res->sr_status_flags = 0;
1062 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1063 struct nfs4_sequence_res *res,
1064 struct nfs4_slot *slot)
1068 slot->privileged = args->sa_privileged ? 1 : 0;
1069 args->sa_slot = slot;
1071 res->sr_slot = slot;
1074 int nfs4_setup_sequence(struct nfs_client *client,
1075 struct nfs4_sequence_args *args,
1076 struct nfs4_sequence_res *res,
1077 struct rpc_task *task)
1079 struct nfs4_session *session = nfs4_get_session(client);
1080 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1081 struct nfs4_slot *slot;
1083 /* slot already allocated? */
1084 if (res->sr_slot != NULL)
1088 tbl = &session->fc_slot_table;
1090 spin_lock(&tbl->slot_tbl_lock);
1091 /* The state manager will wait until the slot table is empty */
1092 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1095 slot = nfs4_alloc_slot(tbl);
1097 if (slot == ERR_PTR(-ENOMEM))
1098 goto out_sleep_timeout;
1101 spin_unlock(&tbl->slot_tbl_lock);
1103 nfs4_sequence_attach_slot(args, res, slot);
1105 trace_nfs4_setup_sequence(session, args);
1107 nfs41_sequence_res_init(res);
1108 rpc_call_start(task);
1111 /* Try again in 1/4 second */
1112 if (args->sa_privileged)
1113 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1114 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1116 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1117 NULL, jiffies + (HZ >> 2));
1118 spin_unlock(&tbl->slot_tbl_lock);
1121 if (args->sa_privileged)
1122 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1123 RPC_PRIORITY_PRIVILEGED);
1125 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1126 spin_unlock(&tbl->slot_tbl_lock);
1129 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1131 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1133 struct nfs4_call_sync_data *data = calldata;
1134 nfs4_setup_sequence(data->seq_server->nfs_client,
1135 data->seq_args, data->seq_res, task);
1138 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1140 struct nfs4_call_sync_data *data = calldata;
1141 nfs4_sequence_done(task, data->seq_res);
1144 static const struct rpc_call_ops nfs40_call_sync_ops = {
1145 .rpc_call_prepare = nfs40_call_sync_prepare,
1146 .rpc_call_done = nfs40_call_sync_done,
1149 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1152 struct rpc_task *task;
1154 task = rpc_run_task(task_setup);
1156 return PTR_ERR(task);
1158 ret = task->tk_status;
1163 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1164 struct nfs_server *server,
1165 struct rpc_message *msg,
1166 struct nfs4_sequence_args *args,
1167 struct nfs4_sequence_res *res,
1168 unsigned short task_flags)
1170 struct nfs_client *clp = server->nfs_client;
1171 struct nfs4_call_sync_data data = {
1172 .seq_server = server,
1176 struct rpc_task_setup task_setup = {
1179 .callback_ops = clp->cl_mvops->call_sync_ops,
1180 .callback_data = &data,
1181 .flags = task_flags,
1184 return nfs4_call_sync_custom(&task_setup);
1187 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1188 struct nfs_server *server,
1189 struct rpc_message *msg,
1190 struct nfs4_sequence_args *args,
1191 struct nfs4_sequence_res *res)
1193 unsigned short task_flags = 0;
1195 if (server->caps & NFS_CAP_MOVEABLE)
1196 task_flags = RPC_TASK_MOVEABLE;
1197 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1201 int nfs4_call_sync(struct rpc_clnt *clnt,
1202 struct nfs_server *server,
1203 struct rpc_message *msg,
1204 struct nfs4_sequence_args *args,
1205 struct nfs4_sequence_res *res,
1208 nfs4_init_sequence(args, res, cache_reply, 0);
1209 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1213 nfs4_inc_nlink_locked(struct inode *inode)
1215 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1216 NFS_INO_INVALID_CTIME |
1217 NFS_INO_INVALID_NLINK);
1222 nfs4_inc_nlink(struct inode *inode)
1224 spin_lock(&inode->i_lock);
1225 nfs4_inc_nlink_locked(inode);
1226 spin_unlock(&inode->i_lock);
1230 nfs4_dec_nlink_locked(struct inode *inode)
1232 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1233 NFS_INO_INVALID_CTIME |
1234 NFS_INO_INVALID_NLINK);
1239 nfs4_update_changeattr_locked(struct inode *inode,
1240 struct nfs4_change_info *cinfo,
1241 unsigned long timestamp, unsigned long cache_validity)
1243 struct nfs_inode *nfsi = NFS_I(inode);
1244 u64 change_attr = inode_peek_iversion_raw(inode);
1246 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1247 if (S_ISDIR(inode->i_mode))
1248 cache_validity |= NFS_INO_INVALID_DATA;
1250 switch (NFS_SERVER(inode)->change_attr_type) {
1251 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1252 if (cinfo->after == change_attr)
1256 if ((s64)(change_attr - cinfo->after) >= 0)
1260 inode_set_iversion_raw(inode, cinfo->after);
1261 if (!cinfo->atomic || cinfo->before != change_attr) {
1262 if (S_ISDIR(inode->i_mode))
1263 nfs_force_lookup_revalidate(inode);
1265 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1267 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1268 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1269 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1270 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR;
1271 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1273 nfsi->attrtimeo_timestamp = jiffies;
1274 nfsi->read_cache_jiffies = timestamp;
1275 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1276 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1278 nfs_set_cache_invalid(inode, cache_validity);
1282 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1283 unsigned long timestamp, unsigned long cache_validity)
1285 spin_lock(&dir->i_lock);
1286 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1287 spin_unlock(&dir->i_lock);
1290 struct nfs4_open_createattrs {
1291 struct nfs4_label *label;
1292 struct iattr *sattr;
1293 const __u32 verf[2];
1296 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1297 int err, struct nfs4_exception *exception)
1301 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1303 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1304 exception->retry = 1;
1308 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1310 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1313 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1315 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1317 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1321 nfs4_map_atomic_open_share(struct nfs_server *server,
1322 fmode_t fmode, int openflags)
1326 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1328 res = NFS4_SHARE_ACCESS_READ;
1331 res = NFS4_SHARE_ACCESS_WRITE;
1333 case FMODE_READ|FMODE_WRITE:
1334 res = NFS4_SHARE_ACCESS_BOTH;
1336 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1338 /* Want no delegation if we're using O_DIRECT */
1339 if (openflags & O_DIRECT)
1340 res |= NFS4_SHARE_WANT_NO_DELEG;
1345 static enum open_claim_type4
1346 nfs4_map_atomic_open_claim(struct nfs_server *server,
1347 enum open_claim_type4 claim)
1349 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1354 case NFS4_OPEN_CLAIM_FH:
1355 return NFS4_OPEN_CLAIM_NULL;
1356 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1357 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1358 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1359 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1363 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1365 p->o_res.f_attr = &p->f_attr;
1366 p->o_res.seqid = p->o_arg.seqid;
1367 p->c_res.seqid = p->c_arg.seqid;
1368 p->o_res.server = p->o_arg.server;
1369 p->o_res.access_request = p->o_arg.access;
1370 nfs_fattr_init(&p->f_attr);
1371 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1374 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1375 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1376 const struct nfs4_open_createattrs *c,
1377 enum open_claim_type4 claim,
1380 struct dentry *parent = dget_parent(dentry);
1381 struct inode *dir = d_inode(parent);
1382 struct nfs_server *server = NFS_SERVER(dir);
1383 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1384 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1385 struct nfs4_opendata *p;
1387 p = kzalloc(sizeof(*p), gfp_mask);
1391 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1392 if (IS_ERR(p->f_attr.label))
1395 p->a_label = nfs4_label_alloc(server, gfp_mask);
1396 if (IS_ERR(p->a_label))
1399 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1400 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1401 if (IS_ERR(p->o_arg.seqid))
1402 goto err_free_label;
1403 nfs_sb_active(dentry->d_sb);
1404 p->dentry = dget(dentry);
1407 atomic_inc(&sp->so_count);
1408 p->o_arg.open_flags = flags;
1409 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1410 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1411 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1413 if (flags & O_CREAT) {
1414 p->o_arg.umask = current_umask();
1415 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1416 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1417 p->o_arg.u.attrs = &p->attrs;
1418 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1420 memcpy(p->o_arg.u.verifier.data, c->verf,
1421 sizeof(p->o_arg.u.verifier.data));
1424 /* ask server to check for all possible rights as results
1426 switch (p->o_arg.claim) {
1429 case NFS4_OPEN_CLAIM_NULL:
1430 case NFS4_OPEN_CLAIM_FH:
1431 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1432 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1433 NFS4_ACCESS_EXECUTE |
1434 nfs_access_xattr_mask(server);
1436 p->o_arg.clientid = server->nfs_client->cl_clientid;
1437 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1438 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1439 p->o_arg.name = &dentry->d_name;
1440 p->o_arg.server = server;
1441 p->o_arg.bitmask = nfs4_bitmask(server, label);
1442 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1443 switch (p->o_arg.claim) {
1444 case NFS4_OPEN_CLAIM_NULL:
1445 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1446 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1447 p->o_arg.fh = NFS_FH(dir);
1449 case NFS4_OPEN_CLAIM_PREVIOUS:
1450 case NFS4_OPEN_CLAIM_FH:
1451 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1452 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1453 p->o_arg.fh = NFS_FH(d_inode(dentry));
1455 p->c_arg.fh = &p->o_res.fh;
1456 p->c_arg.stateid = &p->o_res.stateid;
1457 p->c_arg.seqid = p->o_arg.seqid;
1458 nfs4_init_opendata_res(p);
1459 kref_init(&p->kref);
1463 nfs4_label_free(p->a_label);
1465 nfs4_label_free(p->f_attr.label);
1473 static void nfs4_opendata_free(struct kref *kref)
1475 struct nfs4_opendata *p = container_of(kref,
1476 struct nfs4_opendata, kref);
1477 struct super_block *sb = p->dentry->d_sb;
1479 nfs4_lgopen_release(p->lgp);
1480 nfs_free_seqid(p->o_arg.seqid);
1481 nfs4_sequence_free_slot(&p->o_res.seq_res);
1482 if (p->state != NULL)
1483 nfs4_put_open_state(p->state);
1484 nfs4_put_state_owner(p->owner);
1486 nfs4_label_free(p->a_label);
1487 nfs4_label_free(p->f_attr.label);
1491 nfs_sb_deactive(sb);
1492 nfs_fattr_free_names(&p->f_attr);
1493 kfree(p->f_attr.mdsthreshold);
1497 static void nfs4_opendata_put(struct nfs4_opendata *p)
1500 kref_put(&p->kref, nfs4_opendata_free);
1503 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1506 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1507 case FMODE_READ|FMODE_WRITE:
1508 return state->n_rdwr != 0;
1510 return state->n_wronly != 0;
1512 return state->n_rdonly != 0;
1518 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1519 int open_mode, enum open_claim_type4 claim)
1523 if (open_mode & (O_EXCL|O_TRUNC))
1526 case NFS4_OPEN_CLAIM_NULL:
1527 case NFS4_OPEN_CLAIM_FH:
1532 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1534 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1535 && state->n_rdonly != 0;
1538 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1539 && state->n_wronly != 0;
1541 case FMODE_READ|FMODE_WRITE:
1542 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1543 && state->n_rdwr != 0;
1549 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1550 enum open_claim_type4 claim)
1552 if (delegation == NULL)
1554 if ((delegation->type & fmode) != fmode)
1557 case NFS4_OPEN_CLAIM_NULL:
1558 case NFS4_OPEN_CLAIM_FH:
1560 case NFS4_OPEN_CLAIM_PREVIOUS:
1561 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1567 nfs_mark_delegation_referenced(delegation);
1571 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1580 case FMODE_READ|FMODE_WRITE:
1583 nfs4_state_set_mode_locked(state, state->state | fmode);
1586 #ifdef CONFIG_NFS_V4_1
1587 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1589 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1591 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1593 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1597 #endif /* CONFIG_NFS_V4_1 */
1599 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1601 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1602 wake_up_all(&state->waitq);
1605 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1607 struct nfs_client *clp = state->owner->so_server->nfs_client;
1608 bool need_recover = false;
1610 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1611 need_recover = true;
1612 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1613 need_recover = true;
1614 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1615 need_recover = true;
1617 nfs4_state_mark_reclaim_nograce(clp, state);
1621 * Check for whether or not the caller may update the open stateid
1622 * to the value passed in by stateid.
1624 * Note: This function relies heavily on the server implementing
1625 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1627 * i.e. The stateid seqids have to be initialised to 1, and
1628 * are then incremented on every state transition.
1630 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1631 const nfs4_stateid *stateid)
1633 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1634 /* The common case - we're updating to a new sequence number */
1635 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1636 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1640 /* The server returned a new stateid */
1642 /* This is the first OPEN in this generation */
1643 if (stateid->seqid == cpu_to_be32(1))
1648 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1650 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1652 if (state->n_wronly)
1653 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1654 if (state->n_rdonly)
1655 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1657 set_bit(NFS_O_RDWR_STATE, &state->flags);
1658 set_bit(NFS_OPEN_STATE, &state->flags);
1661 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1662 nfs4_stateid *stateid, fmode_t fmode)
1664 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1665 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1667 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1670 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1673 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1674 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1675 clear_bit(NFS_OPEN_STATE, &state->flags);
1677 if (stateid == NULL)
1679 /* Handle OPEN+OPEN_DOWNGRADE races */
1680 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1681 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1682 nfs_resync_open_stateid_locked(state);
1685 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1686 nfs4_stateid_copy(&state->stateid, stateid);
1687 nfs4_stateid_copy(&state->open_stateid, stateid);
1688 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1690 nfs_state_log_update_open_stateid(state);
1693 static void nfs_clear_open_stateid(struct nfs4_state *state,
1694 nfs4_stateid *arg_stateid,
1695 nfs4_stateid *stateid, fmode_t fmode)
1697 write_seqlock(&state->seqlock);
1698 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1699 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1700 nfs_clear_open_stateid_locked(state, stateid, fmode);
1701 write_sequnlock(&state->seqlock);
1702 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1703 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1706 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1707 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1708 __must_hold(&state->owner->so_lock)
1709 __must_hold(&state->seqlock)
1717 if (nfs_stateid_is_sequential(state, stateid))
1722 /* Rely on seqids for serialisation with NFSv4.0 */
1723 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1726 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1727 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1729 * Ensure we process the state changes in the same order
1730 * in which the server processed them by delaying the
1731 * update of the stateid until we are in sequence.
1733 write_sequnlock(&state->seqlock);
1734 spin_unlock(&state->owner->so_lock);
1736 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1738 if (!fatal_signal_pending(current)) {
1739 if (schedule_timeout(5*HZ) == 0)
1745 finish_wait(&state->waitq, &wait);
1747 spin_lock(&state->owner->so_lock);
1748 write_seqlock(&state->seqlock);
1751 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1752 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1753 nfs4_stateid_copy(freeme, &state->open_stateid);
1754 nfs_test_and_clear_all_open_stateid(state);
1757 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1758 nfs4_stateid_copy(&state->stateid, stateid);
1759 nfs4_stateid_copy(&state->open_stateid, stateid);
1760 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1761 nfs_state_log_update_open_stateid(state);
1764 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1765 const nfs4_stateid *open_stateid,
1767 nfs4_stateid *freeme)
1770 * Protect the call to nfs4_state_set_mode_locked and
1771 * serialise the stateid update
1773 write_seqlock(&state->seqlock);
1774 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1777 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1780 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1782 case FMODE_READ|FMODE_WRITE:
1783 set_bit(NFS_O_RDWR_STATE, &state->flags);
1785 set_bit(NFS_OPEN_STATE, &state->flags);
1786 write_sequnlock(&state->seqlock);
1789 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1791 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1792 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1793 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1794 clear_bit(NFS_OPEN_STATE, &state->flags);
1797 static void nfs_state_set_delegation(struct nfs4_state *state,
1798 const nfs4_stateid *deleg_stateid,
1802 * Protect the call to nfs4_state_set_mode_locked and
1803 * serialise the stateid update
1805 write_seqlock(&state->seqlock);
1806 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1807 set_bit(NFS_DELEGATED_STATE, &state->flags);
1808 write_sequnlock(&state->seqlock);
1811 static void nfs_state_clear_delegation(struct nfs4_state *state)
1813 write_seqlock(&state->seqlock);
1814 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1815 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1816 write_sequnlock(&state->seqlock);
1819 int update_open_stateid(struct nfs4_state *state,
1820 const nfs4_stateid *open_stateid,
1821 const nfs4_stateid *delegation,
1824 struct nfs_server *server = NFS_SERVER(state->inode);
1825 struct nfs_client *clp = server->nfs_client;
1826 struct nfs_inode *nfsi = NFS_I(state->inode);
1827 struct nfs_delegation *deleg_cur;
1828 nfs4_stateid freeme = { };
1831 fmode &= (FMODE_READ|FMODE_WRITE);
1834 spin_lock(&state->owner->so_lock);
1835 if (open_stateid != NULL) {
1836 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1840 deleg_cur = nfs4_get_valid_delegation(state->inode);
1841 if (deleg_cur == NULL)
1844 spin_lock(&deleg_cur->lock);
1845 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1846 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1847 (deleg_cur->type & fmode) != fmode)
1848 goto no_delegation_unlock;
1850 if (delegation == NULL)
1851 delegation = &deleg_cur->stateid;
1852 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1853 goto no_delegation_unlock;
1855 nfs_mark_delegation_referenced(deleg_cur);
1856 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1858 no_delegation_unlock:
1859 spin_unlock(&deleg_cur->lock);
1862 update_open_stateflags(state, fmode);
1863 spin_unlock(&state->owner->so_lock);
1866 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1867 nfs4_schedule_state_manager(clp);
1868 if (freeme.type != 0)
1869 nfs4_test_and_free_stateid(server, &freeme,
1870 state->owner->so_cred);
1875 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1876 const nfs4_stateid *stateid)
1878 struct nfs4_state *state = lsp->ls_state;
1881 spin_lock(&state->state_lock);
1882 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1884 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1886 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1889 spin_unlock(&state->state_lock);
1893 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1895 struct nfs_delegation *delegation;
1897 fmode &= FMODE_READ|FMODE_WRITE;
1899 delegation = nfs4_get_valid_delegation(inode);
1900 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1905 nfs4_inode_return_delegation(inode);
1908 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1910 struct nfs4_state *state = opendata->state;
1911 struct nfs_delegation *delegation;
1912 int open_mode = opendata->o_arg.open_flags;
1913 fmode_t fmode = opendata->o_arg.fmode;
1914 enum open_claim_type4 claim = opendata->o_arg.claim;
1915 nfs4_stateid stateid;
1919 spin_lock(&state->owner->so_lock);
1920 if (can_open_cached(state, fmode, open_mode, claim)) {
1921 update_open_stateflags(state, fmode);
1922 spin_unlock(&state->owner->so_lock);
1923 goto out_return_state;
1925 spin_unlock(&state->owner->so_lock);
1927 delegation = nfs4_get_valid_delegation(state->inode);
1928 if (!can_open_delegated(delegation, fmode, claim)) {
1932 /* Save the delegation */
1933 nfs4_stateid_copy(&stateid, &delegation->stateid);
1935 nfs_release_seqid(opendata->o_arg.seqid);
1936 if (!opendata->is_recover) {
1937 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1943 /* Try to update the stateid using the delegation */
1944 if (update_open_stateid(state, NULL, &stateid, fmode))
1945 goto out_return_state;
1948 return ERR_PTR(ret);
1950 refcount_inc(&state->count);
1955 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1957 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1958 struct nfs_delegation *delegation;
1959 int delegation_flags = 0;
1962 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1964 delegation_flags = delegation->flags;
1966 switch (data->o_arg.claim) {
1969 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1970 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1971 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1972 "returning a delegation for "
1973 "OPEN(CLAIM_DELEGATE_CUR)\n",
1977 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1978 nfs_inode_set_delegation(state->inode,
1979 data->owner->so_cred,
1980 data->o_res.delegation_type,
1981 &data->o_res.delegation,
1982 data->o_res.pagemod_limit);
1984 nfs_inode_reclaim_delegation(state->inode,
1985 data->owner->so_cred,
1986 data->o_res.delegation_type,
1987 &data->o_res.delegation,
1988 data->o_res.pagemod_limit);
1990 if (data->o_res.do_recall)
1991 nfs_async_inode_return_delegation(state->inode,
1992 &data->o_res.delegation);
1996 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1997 * and update the nfs4_state.
1999 static struct nfs4_state *
2000 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
2002 struct inode *inode = data->state->inode;
2003 struct nfs4_state *state = data->state;
2006 if (!data->rpc_done) {
2007 if (data->rpc_status)
2008 return ERR_PTR(data->rpc_status);
2009 return nfs4_try_open_cached(data);
2012 ret = nfs_refresh_inode(inode, &data->f_attr);
2014 return ERR_PTR(ret);
2016 if (data->o_res.delegation_type != 0)
2017 nfs4_opendata_check_deleg(data, state);
2019 if (!update_open_stateid(state, &data->o_res.stateid,
2020 NULL, data->o_arg.fmode))
2021 return ERR_PTR(-EAGAIN);
2022 refcount_inc(&state->count);
2027 static struct inode *
2028 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2030 struct inode *inode;
2032 switch (data->o_arg.claim) {
2033 case NFS4_OPEN_CLAIM_NULL:
2034 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2035 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2036 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2037 return ERR_PTR(-EAGAIN);
2038 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2042 inode = d_inode(data->dentry);
2044 nfs_refresh_inode(inode, &data->f_attr);
2049 static struct nfs4_state *
2050 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2052 struct nfs4_state *state;
2053 struct inode *inode;
2055 inode = nfs4_opendata_get_inode(data);
2057 return ERR_CAST(inode);
2058 if (data->state != NULL && data->state->inode == inode) {
2059 state = data->state;
2060 refcount_inc(&state->count);
2062 state = nfs4_get_open_state(inode, data->owner);
2065 state = ERR_PTR(-ENOMEM);
2069 static struct nfs4_state *
2070 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2072 struct nfs4_state *state;
2074 if (!data->rpc_done) {
2075 state = nfs4_try_open_cached(data);
2076 trace_nfs4_cached_open(data->state);
2080 state = nfs4_opendata_find_nfs4_state(data);
2084 if (data->o_res.delegation_type != 0)
2085 nfs4_opendata_check_deleg(data, state);
2086 if (!update_open_stateid(state, &data->o_res.stateid,
2087 NULL, data->o_arg.fmode)) {
2088 nfs4_put_open_state(state);
2089 state = ERR_PTR(-EAGAIN);
2092 nfs_release_seqid(data->o_arg.seqid);
2096 static struct nfs4_state *
2097 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2099 struct nfs4_state *ret;
2101 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2102 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2104 ret = _nfs4_opendata_to_nfs4_state(data);
2105 nfs4_sequence_free_slot(&data->o_res.seq_res);
2109 static struct nfs_open_context *
2110 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2112 struct nfs_inode *nfsi = NFS_I(state->inode);
2113 struct nfs_open_context *ctx;
2116 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2117 if (ctx->state != state)
2119 if ((ctx->mode & mode) != mode)
2121 if (!get_nfs_open_context(ctx))
2127 return ERR_PTR(-ENOENT);
2130 static struct nfs_open_context *
2131 nfs4_state_find_open_context(struct nfs4_state *state)
2133 struct nfs_open_context *ctx;
2135 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2138 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2141 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2144 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2145 struct nfs4_state *state, enum open_claim_type4 claim)
2147 struct nfs4_opendata *opendata;
2149 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2150 NULL, claim, GFP_NOFS);
2151 if (opendata == NULL)
2152 return ERR_PTR(-ENOMEM);
2153 opendata->state = state;
2154 refcount_inc(&state->count);
2158 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2161 struct nfs4_state *newstate;
2162 struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
2163 int openflags = opendata->o_arg.open_flags;
2166 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2168 opendata->o_arg.fmode = fmode;
2169 opendata->o_arg.share_access =
2170 nfs4_map_atomic_open_share(server, fmode, openflags);
2171 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2172 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2173 nfs4_init_opendata_res(opendata);
2174 ret = _nfs4_recover_proc_open(opendata);
2177 newstate = nfs4_opendata_to_nfs4_state(opendata);
2178 if (IS_ERR(newstate))
2179 return PTR_ERR(newstate);
2180 if (newstate != opendata->state)
2182 nfs4_close_state(newstate, fmode);
2186 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2190 /* memory barrier prior to reading state->n_* */
2192 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2195 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2198 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2202 * We may have performed cached opens for all three recoveries.
2203 * Check if we need to update the current stateid.
2205 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2206 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2207 write_seqlock(&state->seqlock);
2208 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2209 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2210 write_sequnlock(&state->seqlock);
2217 * reclaim state on the server after a reboot.
2219 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2221 struct nfs_delegation *delegation;
2222 struct nfs4_opendata *opendata;
2223 fmode_t delegation_type = 0;
2226 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2227 NFS4_OPEN_CLAIM_PREVIOUS);
2228 if (IS_ERR(opendata))
2229 return PTR_ERR(opendata);
2231 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2232 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2233 delegation_type = delegation->type;
2235 opendata->o_arg.u.delegation_type = delegation_type;
2236 status = nfs4_open_recover(opendata, state);
2237 nfs4_opendata_put(opendata);
2241 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2243 struct nfs_server *server = NFS_SERVER(state->inode);
2244 struct nfs4_exception exception = { };
2247 err = _nfs4_do_open_reclaim(ctx, state);
2248 trace_nfs4_open_reclaim(ctx, 0, err);
2249 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2251 if (err != -NFS4ERR_DELAY)
2253 nfs4_handle_exception(server, err, &exception);
2254 } while (exception.retry);
2258 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2260 struct nfs_open_context *ctx;
2263 ctx = nfs4_state_find_open_context(state);
2266 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2267 nfs_state_clear_open_state_flags(state);
2268 ret = nfs4_do_open_reclaim(ctx, state);
2269 put_nfs_open_context(ctx);
2273 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2277 printk(KERN_ERR "NFS: %s: unhandled error "
2278 "%d.\n", __func__, err);
2286 case -NFS4ERR_BADSESSION:
2287 case -NFS4ERR_BADSLOT:
2288 case -NFS4ERR_BAD_HIGH_SLOT:
2289 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2290 case -NFS4ERR_DEADSESSION:
2292 case -NFS4ERR_STALE_CLIENTID:
2293 case -NFS4ERR_STALE_STATEID:
2294 /* Don't recall a delegation if it was lost */
2295 nfs4_schedule_lease_recovery(server->nfs_client);
2297 case -NFS4ERR_MOVED:
2298 nfs4_schedule_migration_recovery(server);
2300 case -NFS4ERR_LEASE_MOVED:
2301 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2303 case -NFS4ERR_DELEG_REVOKED:
2304 case -NFS4ERR_ADMIN_REVOKED:
2305 case -NFS4ERR_EXPIRED:
2306 case -NFS4ERR_BAD_STATEID:
2307 case -NFS4ERR_OPENMODE:
2308 nfs_inode_find_state_and_recover(state->inode,
2310 nfs4_schedule_stateid_recovery(server, state);
2312 case -NFS4ERR_DELAY:
2313 case -NFS4ERR_GRACE:
2317 case -NFS4ERR_DENIED:
2319 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2321 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2328 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2329 struct nfs4_state *state, const nfs4_stateid *stateid)
2331 struct nfs_server *server = NFS_SERVER(state->inode);
2332 struct nfs4_opendata *opendata;
2335 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2336 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2337 if (IS_ERR(opendata))
2338 return PTR_ERR(opendata);
2339 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2340 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2341 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2345 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2346 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2350 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2351 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2355 nfs_state_clear_delegation(state);
2357 nfs4_opendata_put(opendata);
2358 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2361 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2363 struct nfs4_opendata *data = calldata;
2365 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2366 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2369 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2371 struct nfs4_opendata *data = calldata;
2373 nfs40_sequence_done(task, &data->c_res.seq_res);
2375 data->rpc_status = task->tk_status;
2376 if (data->rpc_status == 0) {
2377 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2378 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2379 renew_lease(data->o_res.server, data->timestamp);
2380 data->rpc_done = true;
2384 static void nfs4_open_confirm_release(void *calldata)
2386 struct nfs4_opendata *data = calldata;
2387 struct nfs4_state *state = NULL;
2389 /* If this request hasn't been cancelled, do nothing */
2390 if (!data->cancelled)
2392 /* In case of error, no cleanup! */
2393 if (!data->rpc_done)
2395 state = nfs4_opendata_to_nfs4_state(data);
2397 nfs4_close_state(state, data->o_arg.fmode);
2399 nfs4_opendata_put(data);
2402 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2403 .rpc_call_prepare = nfs4_open_confirm_prepare,
2404 .rpc_call_done = nfs4_open_confirm_done,
2405 .rpc_release = nfs4_open_confirm_release,
2409 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2411 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2413 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2414 struct rpc_task *task;
2415 struct rpc_message msg = {
2416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2417 .rpc_argp = &data->c_arg,
2418 .rpc_resp = &data->c_res,
2419 .rpc_cred = data->owner->so_cred,
2421 struct rpc_task_setup task_setup_data = {
2422 .rpc_client = server->client,
2423 .rpc_message = &msg,
2424 .callback_ops = &nfs4_open_confirm_ops,
2425 .callback_data = data,
2426 .workqueue = nfsiod_workqueue,
2427 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2431 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2433 kref_get(&data->kref);
2434 data->rpc_done = false;
2435 data->rpc_status = 0;
2436 data->timestamp = jiffies;
2437 task = rpc_run_task(&task_setup_data);
2439 return PTR_ERR(task);
2440 status = rpc_wait_for_completion_task(task);
2442 data->cancelled = true;
2445 status = data->rpc_status;
2450 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2452 struct nfs4_opendata *data = calldata;
2453 struct nfs4_state_owner *sp = data->owner;
2454 struct nfs_client *clp = sp->so_server->nfs_client;
2455 enum open_claim_type4 claim = data->o_arg.claim;
2457 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2460 * Check if we still need to send an OPEN call, or if we can use
2461 * a delegation instead.
2463 if (data->state != NULL) {
2464 struct nfs_delegation *delegation;
2466 if (can_open_cached(data->state, data->o_arg.fmode,
2467 data->o_arg.open_flags, claim))
2470 delegation = nfs4_get_valid_delegation(data->state->inode);
2471 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2472 goto unlock_no_action;
2475 /* Update client id. */
2476 data->o_arg.clientid = clp->cl_clientid;
2480 case NFS4_OPEN_CLAIM_PREVIOUS:
2481 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2482 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2483 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2485 case NFS4_OPEN_CLAIM_FH:
2486 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2488 data->timestamp = jiffies;
2489 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2490 &data->o_arg.seq_args,
2491 &data->o_res.seq_res,
2493 nfs_release_seqid(data->o_arg.seqid);
2495 /* Set the create mode (note dependency on the session type) */
2496 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2497 if (data->o_arg.open_flags & O_EXCL) {
2498 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2499 if (clp->cl_mvops->minor_version == 0) {
2500 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2501 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2502 * because ACCESS will return permission denied for
2503 * all bits until close */
2504 data->o_res.access_request = data->o_arg.access = 0;
2505 } else if (nfs4_has_persistent_session(clp))
2506 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2510 trace_nfs4_cached_open(data->state);
2513 task->tk_action = NULL;
2515 nfs4_sequence_done(task, &data->o_res.seq_res);
2518 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2520 struct nfs4_opendata *data = calldata;
2522 data->rpc_status = task->tk_status;
2524 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2527 if (task->tk_status == 0) {
2528 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2529 switch (data->o_res.f_attr->mode & S_IFMT) {
2533 data->rpc_status = -ELOOP;
2536 data->rpc_status = -EISDIR;
2539 data->rpc_status = -ENOTDIR;
2542 renew_lease(data->o_res.server, data->timestamp);
2543 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2544 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2546 data->rpc_done = true;
2549 static void nfs4_open_release(void *calldata)
2551 struct nfs4_opendata *data = calldata;
2552 struct nfs4_state *state = NULL;
2554 /* If this request hasn't been cancelled, do nothing */
2555 if (!data->cancelled)
2557 /* In case of error, no cleanup! */
2558 if (data->rpc_status != 0 || !data->rpc_done)
2560 /* In case we need an open_confirm, no cleanup! */
2561 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2563 state = nfs4_opendata_to_nfs4_state(data);
2565 nfs4_close_state(state, data->o_arg.fmode);
2567 nfs4_opendata_put(data);
2570 static const struct rpc_call_ops nfs4_open_ops = {
2571 .rpc_call_prepare = nfs4_open_prepare,
2572 .rpc_call_done = nfs4_open_done,
2573 .rpc_release = nfs4_open_release,
2576 static int nfs4_run_open_task(struct nfs4_opendata *data,
2577 struct nfs_open_context *ctx)
2579 struct inode *dir = d_inode(data->dir);
2580 struct nfs_server *server = NFS_SERVER(dir);
2581 struct nfs_openargs *o_arg = &data->o_arg;
2582 struct nfs_openres *o_res = &data->o_res;
2583 struct rpc_task *task;
2584 struct rpc_message msg = {
2585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2588 .rpc_cred = data->owner->so_cred,
2590 struct rpc_task_setup task_setup_data = {
2591 .rpc_client = server->client,
2592 .rpc_message = &msg,
2593 .callback_ops = &nfs4_open_ops,
2594 .callback_data = data,
2595 .workqueue = nfsiod_workqueue,
2596 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2600 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
2601 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2603 kref_get(&data->kref);
2604 data->rpc_done = false;
2605 data->rpc_status = 0;
2606 data->cancelled = false;
2607 data->is_recover = false;
2609 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2610 data->is_recover = true;
2611 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2613 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2614 pnfs_lgopen_prepare(data, ctx);
2616 task = rpc_run_task(&task_setup_data);
2618 return PTR_ERR(task);
2619 status = rpc_wait_for_completion_task(task);
2621 data->cancelled = true;
2624 status = data->rpc_status;
2630 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2632 struct inode *dir = d_inode(data->dir);
2633 struct nfs_openres *o_res = &data->o_res;
2636 status = nfs4_run_open_task(data, NULL);
2637 if (status != 0 || !data->rpc_done)
2640 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2642 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2643 status = _nfs4_proc_open_confirm(data);
2649 * Additional permission checks in order to distinguish between an
2650 * open for read, and an open for execute. This works around the
2651 * fact that NFSv4 OPEN treats read and execute permissions as being
2653 * Note that in the non-execute case, we want to turn off permission
2654 * checking if we just created a new file (POSIX open() semantics).
2656 static int nfs4_opendata_access(const struct cred *cred,
2657 struct nfs4_opendata *opendata,
2658 struct nfs4_state *state, fmode_t fmode)
2660 struct nfs_access_entry cache;
2663 /* access call failed or for some reason the server doesn't
2664 * support any access modes -- defer access call until later */
2665 if (opendata->o_res.access_supported == 0)
2669 if (fmode & FMODE_EXEC) {
2670 /* ONLY check for exec rights */
2671 if (S_ISDIR(state->inode->i_mode))
2672 mask = NFS4_ACCESS_LOOKUP;
2674 mask = NFS4_ACCESS_EXECUTE;
2675 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2676 mask = NFS4_ACCESS_READ;
2678 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2679 nfs_access_add_cache(state->inode, &cache, cred);
2681 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2682 if ((mask & ~cache.mask & flags) == 0)
2689 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2691 static int _nfs4_proc_open(struct nfs4_opendata *data,
2692 struct nfs_open_context *ctx)
2694 struct inode *dir = d_inode(data->dir);
2695 struct nfs_server *server = NFS_SERVER(dir);
2696 struct nfs_openargs *o_arg = &data->o_arg;
2697 struct nfs_openres *o_res = &data->o_res;
2700 status = nfs4_run_open_task(data, ctx);
2701 if (!data->rpc_done)
2704 if (status == -NFS4ERR_BADNAME &&
2705 !(o_arg->open_flags & O_CREAT))
2710 nfs_fattr_map_and_free_names(server, &data->f_attr);
2712 if (o_arg->open_flags & O_CREAT) {
2713 if (o_arg->open_flags & O_EXCL)
2714 data->file_created = true;
2715 else if (o_res->cinfo.before != o_res->cinfo.after)
2716 data->file_created = true;
2717 if (data->file_created ||
2718 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2719 nfs4_update_changeattr(dir, &o_res->cinfo,
2720 o_res->f_attr->time_start,
2721 NFS_INO_INVALID_DATA);
2723 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2724 server->caps &= ~NFS_CAP_POSIX_LOCK;
2725 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2726 status = _nfs4_proc_open_confirm(data);
2730 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2731 struct nfs_fh *fh = &o_res->fh;
2733 nfs4_sequence_free_slot(&o_res->seq_res);
2734 if (o_arg->claim == NFS4_OPEN_CLAIM_FH)
2735 fh = NFS_FH(d_inode(data->dentry));
2736 nfs4_proc_getattr(server, fh, o_res->f_attr, NULL);
2743 * reclaim state on the server after a network partition.
2744 * Assumes caller holds the appropriate lock
2746 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2748 struct nfs4_opendata *opendata;
2751 opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2752 if (IS_ERR(opendata))
2753 return PTR_ERR(opendata);
2755 * We're not recovering a delegation, so ask for no delegation.
2756 * Otherwise the recovery thread could deadlock with an outstanding
2757 * delegation return.
2759 opendata->o_arg.open_flags = O_DIRECT;
2760 ret = nfs4_open_recover(opendata, state);
2762 d_drop(ctx->dentry);
2763 nfs4_opendata_put(opendata);
2767 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2769 struct nfs_server *server = NFS_SERVER(state->inode);
2770 struct nfs4_exception exception = { };
2774 err = _nfs4_open_expired(ctx, state);
2775 trace_nfs4_open_expired(ctx, 0, err);
2776 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2781 case -NFS4ERR_GRACE:
2782 case -NFS4ERR_DELAY:
2783 nfs4_handle_exception(server, err, &exception);
2786 } while (exception.retry);
2791 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2793 struct nfs_open_context *ctx;
2796 ctx = nfs4_state_find_open_context(state);
2799 ret = nfs4_do_open_expired(ctx, state);
2800 put_nfs_open_context(ctx);
2804 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2805 const nfs4_stateid *stateid)
2807 nfs_remove_bad_delegation(state->inode, stateid);
2808 nfs_state_clear_delegation(state);
2811 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2813 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2814 nfs_finish_clear_delegation_stateid(state, NULL);
2817 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2819 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2820 nfs40_clear_delegation_stateid(state);
2821 nfs_state_clear_open_state_flags(state);
2822 return nfs4_open_expired(sp, state);
2825 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2826 nfs4_stateid *stateid,
2827 const struct cred *cred)
2829 return -NFS4ERR_BAD_STATEID;
2832 #if defined(CONFIG_NFS_V4_1)
2833 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2834 nfs4_stateid *stateid,
2835 const struct cred *cred)
2839 switch (stateid->type) {
2842 case NFS4_INVALID_STATEID_TYPE:
2843 case NFS4_SPECIAL_STATEID_TYPE:
2844 return -NFS4ERR_BAD_STATEID;
2845 case NFS4_REVOKED_STATEID_TYPE:
2849 status = nfs41_test_stateid(server, stateid, cred);
2851 case -NFS4ERR_EXPIRED:
2852 case -NFS4ERR_ADMIN_REVOKED:
2853 case -NFS4ERR_DELEG_REVOKED:
2859 /* Ack the revoked state to the server */
2860 nfs41_free_stateid(server, stateid, cred, true);
2861 return -NFS4ERR_EXPIRED;
2864 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2866 struct nfs_server *server = NFS_SERVER(state->inode);
2867 nfs4_stateid stateid;
2868 struct nfs_delegation *delegation;
2869 const struct cred *cred = NULL;
2870 int status, ret = NFS_OK;
2872 /* Get the delegation credential for use by test/free_stateid */
2874 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2875 if (delegation == NULL) {
2877 nfs_state_clear_delegation(state);
2881 spin_lock(&delegation->lock);
2882 nfs4_stateid_copy(&stateid, &delegation->stateid);
2884 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2885 &delegation->flags)) {
2886 spin_unlock(&delegation->lock);
2891 if (delegation->cred)
2892 cred = get_cred(delegation->cred);
2893 spin_unlock(&delegation->lock);
2895 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2896 trace_nfs4_test_delegation_stateid(state, NULL, status);
2897 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2898 nfs_finish_clear_delegation_stateid(state, &stateid);
2906 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2910 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2911 nfs4_copy_delegation_stateid(state->inode, state->state,
2913 nfs4_stateid_match_other(&state->stateid, &tmp))
2914 nfs_state_set_delegation(state, &tmp, state->state);
2916 nfs_state_clear_delegation(state);
2920 * nfs41_check_expired_locks - possibly free a lock stateid
2922 * @state: NFSv4 state for an inode
2924 * Returns NFS_OK if recovery for this stateid is now finished.
2925 * Otherwise a negative NFS4ERR value is returned.
2927 static int nfs41_check_expired_locks(struct nfs4_state *state)
2929 int status, ret = NFS_OK;
2930 struct nfs4_lock_state *lsp, *prev = NULL;
2931 struct nfs_server *server = NFS_SERVER(state->inode);
2933 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2936 spin_lock(&state->state_lock);
2937 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2938 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2939 const struct cred *cred = lsp->ls_state->owner->so_cred;
2941 refcount_inc(&lsp->ls_count);
2942 spin_unlock(&state->state_lock);
2944 nfs4_put_lock_state(prev);
2947 status = nfs41_test_and_free_expired_stateid(server,
2950 trace_nfs4_test_lock_stateid(state, lsp, status);
2951 if (status == -NFS4ERR_EXPIRED ||
2952 status == -NFS4ERR_BAD_STATEID) {
2953 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2954 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2955 if (!recover_lost_locks)
2956 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2957 } else if (status != NFS_OK) {
2959 nfs4_put_lock_state(prev);
2962 spin_lock(&state->state_lock);
2965 spin_unlock(&state->state_lock);
2966 nfs4_put_lock_state(prev);
2972 * nfs41_check_open_stateid - possibly free an open stateid
2974 * @state: NFSv4 state for an inode
2976 * Returns NFS_OK if recovery for this stateid is now finished.
2977 * Otherwise a negative NFS4ERR value is returned.
2979 static int nfs41_check_open_stateid(struct nfs4_state *state)
2981 struct nfs_server *server = NFS_SERVER(state->inode);
2982 nfs4_stateid *stateid = &state->open_stateid;
2983 const struct cred *cred = state->owner->so_cred;
2986 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2987 return -NFS4ERR_BAD_STATEID;
2988 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2989 trace_nfs4_test_open_stateid(state, NULL, status);
2990 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2991 nfs_state_clear_open_state_flags(state);
2992 stateid->type = NFS4_INVALID_STATEID_TYPE;
2995 if (nfs_open_stateid_recover_openmode(state))
2996 return -NFS4ERR_OPENMODE;
3000 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
3004 status = nfs41_check_delegation_stateid(state);
3005 if (status != NFS_OK)
3007 nfs41_delegation_recover_stateid(state);
3009 status = nfs41_check_expired_locks(state);
3010 if (status != NFS_OK)
3012 status = nfs41_check_open_stateid(state);
3013 if (status != NFS_OK)
3014 status = nfs4_open_expired(sp, state);
3020 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
3021 * fields corresponding to attributes that were used to store the verifier.
3022 * Make sure we clobber those fields in the later setattr call
3024 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
3025 struct iattr *sattr, struct nfs4_label **label)
3027 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
3032 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3033 attrset[i] = opendata->o_res.attrset[i];
3034 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3035 attrset[i] &= ~bitmask[i];
3038 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3039 sattr->ia_valid : 0;
3041 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3042 if (sattr->ia_valid & ATTR_ATIME_SET)
3043 ret |= ATTR_ATIME_SET;
3048 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3049 if (sattr->ia_valid & ATTR_MTIME_SET)
3050 ret |= ATTR_MTIME_SET;
3055 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3060 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3061 struct nfs_open_context *ctx)
3063 struct nfs4_state_owner *sp = opendata->owner;
3064 struct nfs_server *server = sp->so_server;
3065 struct dentry *dentry;
3066 struct nfs4_state *state;
3067 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3068 struct inode *dir = d_inode(opendata->dir);
3069 unsigned long dir_verifier;
3073 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3074 dir_verifier = nfs_save_change_attribute(dir);
3076 ret = _nfs4_proc_open(opendata, ctx);
3080 state = _nfs4_opendata_to_nfs4_state(opendata);
3081 ret = PTR_ERR(state);
3085 if (server->caps & NFS_CAP_POSIX_LOCK)
3086 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3087 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3088 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3089 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3090 set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3092 dentry = opendata->dentry;
3093 if (d_really_is_negative(dentry)) {
3094 struct dentry *alias;
3096 alias = d_exact_alias(dentry, state->inode);
3098 alias = d_splice_alias(igrab(state->inode), dentry);
3099 /* d_splice_alias() can't fail here - it's a non-directory */
3102 ctx->dentry = dentry = alias;
3106 switch(opendata->o_arg.claim) {
3109 case NFS4_OPEN_CLAIM_NULL:
3110 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3111 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3112 if (!opendata->rpc_done)
3114 if (opendata->o_res.delegation_type != 0)
3115 dir_verifier = nfs_save_change_attribute(dir);
3116 nfs_set_verifier(dentry, dir_verifier);
3119 /* Parse layoutget results before we check for access */
3120 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3122 ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode);
3126 if (d_inode(dentry) == state->inode) {
3127 nfs_inode_attach_open_context(ctx);
3128 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3129 nfs4_schedule_stateid_recovery(server, state);
3133 if (!opendata->cancelled) {
3134 if (opendata->lgp) {
3135 nfs4_lgopen_release(opendata->lgp);
3136 opendata->lgp = NULL;
3138 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3144 * Returns a referenced nfs4_state
3146 static int _nfs4_do_open(struct inode *dir,
3147 struct nfs_open_context *ctx,
3149 const struct nfs4_open_createattrs *c,
3152 struct nfs4_state_owner *sp;
3153 struct nfs4_state *state = NULL;
3154 struct nfs_server *server = NFS_SERVER(dir);
3155 struct nfs4_opendata *opendata;
3156 struct dentry *dentry = ctx->dentry;
3157 const struct cred *cred = ctx->cred;
3158 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3159 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3160 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3161 struct iattr *sattr = c->sattr;
3162 struct nfs4_label *label = c->label;
3165 /* Protect against reboot recovery conflicts */
3167 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3169 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3172 status = nfs4_client_recover_expired_lease(server->nfs_client);
3174 goto err_put_state_owner;
3175 if (d_really_is_positive(dentry))
3176 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3178 if (d_really_is_positive(dentry))
3179 claim = NFS4_OPEN_CLAIM_FH;
3180 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3181 c, claim, GFP_KERNEL);
3182 if (opendata == NULL)
3183 goto err_put_state_owner;
3185 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3186 if (!opendata->f_attr.mdsthreshold) {
3187 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3188 if (!opendata->f_attr.mdsthreshold)
3189 goto err_opendata_put;
3191 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3193 if (d_really_is_positive(dentry))
3194 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3196 status = _nfs4_open_and_get_state(opendata, ctx);
3198 goto err_opendata_put;
3201 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3202 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3203 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3205 * send create attributes which was not set by open
3206 * with an extra setattr.
3208 if (attrs || label) {
3209 unsigned ia_old = sattr->ia_valid;
3211 sattr->ia_valid = attrs;
3212 nfs_fattr_init(opendata->o_res.f_attr);
3213 status = nfs4_do_setattr(state->inode, cred,
3214 opendata->o_res.f_attr, sattr,
3217 nfs_setattr_update_inode(state->inode, sattr,
3218 opendata->o_res.f_attr);
3219 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3221 sattr->ia_valid = ia_old;
3224 if (opened && opendata->file_created)
3227 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3228 *ctx_th = opendata->f_attr.mdsthreshold;
3229 opendata->f_attr.mdsthreshold = NULL;
3232 nfs4_opendata_put(opendata);
3233 nfs4_put_state_owner(sp);
3236 nfs4_opendata_put(opendata);
3237 err_put_state_owner:
3238 nfs4_put_state_owner(sp);
3244 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3245 struct nfs_open_context *ctx,
3247 struct iattr *sattr,
3248 struct nfs4_label *label,
3251 struct nfs_server *server = NFS_SERVER(dir);
3252 struct nfs4_exception exception = {
3253 .interruptible = true,
3255 struct nfs4_state *res;
3256 struct nfs4_open_createattrs c = {
3260 [0] = (__u32)jiffies,
3261 [1] = (__u32)current->pid,
3267 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3269 trace_nfs4_open_file(ctx, flags, status);
3272 /* NOTE: BAD_SEQID means the server and client disagree about the
3273 * book-keeping w.r.t. state-changing operations
3274 * (OPEN/CLOSE/LOCK/LOCKU...)
3275 * It is actually a sign of a bug on the client or on the server.
3277 * If we receive a BAD_SEQID error in the particular case of
3278 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3279 * have unhashed the old state_owner for us, and that we can
3280 * therefore safely retry using a new one. We should still warn
3281 * the user though...
3283 if (status == -NFS4ERR_BAD_SEQID) {
3284 pr_warn_ratelimited("NFS: v4 server %s "
3285 " returned a bad sequence-id error!\n",
3286 NFS_SERVER(dir)->nfs_client->cl_hostname);
3287 exception.retry = 1;
3291 * BAD_STATEID on OPEN means that the server cancelled our
3292 * state before it received the OPEN_CONFIRM.
3293 * Recover by retrying the request as per the discussion
3294 * on Page 181 of RFC3530.
3296 if (status == -NFS4ERR_BAD_STATEID) {
3297 exception.retry = 1;
3300 if (status == -NFS4ERR_EXPIRED) {
3301 nfs4_schedule_lease_recovery(server->nfs_client);
3302 exception.retry = 1;
3305 if (status == -EAGAIN) {
3306 /* We must have found a delegation */
3307 exception.retry = 1;
3310 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3312 res = ERR_PTR(nfs4_handle_exception(server,
3313 status, &exception));
3314 } while (exception.retry);
3318 static int _nfs4_do_setattr(struct inode *inode,
3319 struct nfs_setattrargs *arg,
3320 struct nfs_setattrres *res,
3321 const struct cred *cred,
3322 struct nfs_open_context *ctx)
3324 struct nfs_server *server = NFS_SERVER(inode);
3325 struct rpc_message msg = {
3326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3331 const struct cred *delegation_cred = NULL;
3332 unsigned long timestamp = jiffies;
3336 nfs_fattr_init(res->fattr);
3338 /* Servers should only apply open mode checks for file size changes */
3339 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3341 nfs4_inode_make_writeable(inode);
3345 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3346 /* Use that stateid */
3347 } else if (ctx != NULL && ctx->state) {
3348 struct nfs_lock_context *l_ctx;
3349 if (!nfs4_valid_open_stateid(ctx->state))
3351 l_ctx = nfs_get_lock_context(ctx);
3353 return PTR_ERR(l_ctx);
3354 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3355 &arg->stateid, &delegation_cred);
3356 nfs_put_lock_context(l_ctx);
3359 else if (status == -EAGAIN)
3363 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3365 if (delegation_cred)
3366 msg.rpc_cred = delegation_cred;
3368 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3370 put_cred(delegation_cred);
3371 if (status == 0 && ctx != NULL)
3372 renew_lease(server, timestamp);
3373 trace_nfs4_setattr(inode, &arg->stateid, status);
3377 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3378 struct nfs_fattr *fattr, struct iattr *sattr,
3379 struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3381 struct nfs_server *server = NFS_SERVER(inode);
3382 __u32 bitmask[NFS4_BITMASK_SZ];
3383 struct nfs4_state *state = ctx ? ctx->state : NULL;
3384 struct nfs_setattrargs arg = {
3385 .fh = NFS_FH(inode),
3391 struct nfs_setattrres res = {
3395 struct nfs4_exception exception = {
3398 .stateid = &arg.stateid,
3400 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3403 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3404 adjust_flags |= NFS_INO_INVALID_MODE;
3405 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3406 adjust_flags |= NFS_INO_INVALID_OTHER;
3409 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3410 inode, adjust_flags);
3412 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3414 case -NFS4ERR_OPENMODE:
3415 if (!(sattr->ia_valid & ATTR_SIZE)) {
3416 pr_warn_once("NFSv4: server %s is incorrectly "
3417 "applying open mode checks to "
3418 "a SETATTR that is not "
3419 "changing file size.\n",
3420 server->nfs_client->cl_hostname);
3422 if (state && !(state->state & FMODE_WRITE)) {
3424 if (sattr->ia_valid & ATTR_OPEN)
3429 err = nfs4_handle_exception(server, err, &exception);
3430 } while (exception.retry);
3436 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3438 if (inode == NULL || !nfs_have_layout(inode))
3441 return pnfs_wait_on_layoutreturn(inode, task);
3445 * Update the seqid of an open stateid
3447 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3448 struct nfs4_state *state)
3455 if (!nfs4_valid_open_stateid(state))
3457 seq = read_seqbegin(&state->seqlock);
3458 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3459 nfs4_stateid_copy(dst, &state->open_stateid);
3460 if (read_seqretry(&state->seqlock, seq))
3464 seqid_open = state->open_stateid.seqid;
3465 if (read_seqretry(&state->seqlock, seq))
3468 dst_seqid = be32_to_cpu(dst->seqid);
3469 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3470 dst->seqid = seqid_open;
3476 * Update the seqid of an open stateid after receiving
3477 * NFS4ERR_OLD_STATEID
3479 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3480 struct nfs4_state *state)
3485 int seq, status = -EAGAIN;
3490 if (!nfs4_valid_open_stateid(state))
3492 seq = read_seqbegin(&state->seqlock);
3493 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3494 if (read_seqretry(&state->seqlock, seq))
3499 write_seqlock(&state->seqlock);
3500 seqid_open = state->open_stateid.seqid;
3502 dst_seqid = be32_to_cpu(dst->seqid);
3504 /* Did another OPEN bump the state's seqid? try again: */
3505 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3506 dst->seqid = seqid_open;
3507 write_sequnlock(&state->seqlock);
3512 /* server says we're behind but we haven't seen the update yet */
3513 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3514 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3515 write_sequnlock(&state->seqlock);
3516 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3518 if (fatal_signal_pending(current))
3521 if (schedule_timeout(5*HZ) != 0)
3524 finish_wait(&state->waitq, &wait);
3528 if (status == -EINTR)
3531 /* we slept the whole 5 seconds, we must have lost a seqid */
3532 dst->seqid = cpu_to_be32(dst_seqid + 1);
3540 struct nfs4_closedata {
3541 struct inode *inode;
3542 struct nfs4_state *state;
3543 struct nfs_closeargs arg;
3544 struct nfs_closeres res;
3546 struct nfs4_layoutreturn_args arg;
3547 struct nfs4_layoutreturn_res res;
3548 struct nfs4_xdr_opaque_data ld_private;
3552 struct nfs_fattr fattr;
3553 unsigned long timestamp;
3556 static void nfs4_free_closedata(void *data)
3558 struct nfs4_closedata *calldata = data;
3559 struct nfs4_state_owner *sp = calldata->state->owner;
3560 struct super_block *sb = calldata->state->inode->i_sb;
3562 if (calldata->lr.roc)
3563 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3564 calldata->res.lr_ret);
3565 nfs4_put_open_state(calldata->state);
3566 nfs_free_seqid(calldata->arg.seqid);
3567 nfs4_put_state_owner(sp);
3568 nfs_sb_deactive(sb);
3572 static void nfs4_close_done(struct rpc_task *task, void *data)
3574 struct nfs4_closedata *calldata = data;
3575 struct nfs4_state *state = calldata->state;
3576 struct nfs_server *server = NFS_SERVER(calldata->inode);
3577 nfs4_stateid *res_stateid = NULL;
3578 struct nfs4_exception exception = {
3580 .inode = calldata->inode,
3581 .stateid = &calldata->arg.stateid,
3584 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3586 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3588 /* Handle Layoutreturn errors */
3589 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3590 &calldata->res.lr_ret) == -EAGAIN)
3593 /* hmm. we are done with the inode, and in the process of freeing
3594 * the state_owner. we keep this around to process errors
3596 switch (task->tk_status) {
3598 res_stateid = &calldata->res.stateid;
3599 renew_lease(server, calldata->timestamp);
3601 case -NFS4ERR_ACCESS:
3602 if (calldata->arg.bitmask != NULL) {
3603 calldata->arg.bitmask = NULL;
3604 calldata->res.fattr = NULL;
3609 case -NFS4ERR_OLD_STATEID:
3610 /* Did we race with OPEN? */
3611 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3615 case -NFS4ERR_ADMIN_REVOKED:
3616 case -NFS4ERR_STALE_STATEID:
3617 case -NFS4ERR_EXPIRED:
3618 nfs4_free_revoked_stateid(server,
3619 &calldata->arg.stateid,
3620 task->tk_msg.rpc_cred);
3622 case -NFS4ERR_BAD_STATEID:
3623 if (calldata->arg.fmode == 0)
3627 task->tk_status = nfs4_async_handle_exception(task,
3628 server, task->tk_status, &exception);
3629 if (exception.retry)
3632 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3633 res_stateid, calldata->arg.fmode);
3635 task->tk_status = 0;
3636 nfs_release_seqid(calldata->arg.seqid);
3637 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3638 dprintk("%s: ret = %d\n", __func__, task->tk_status);
3641 task->tk_status = 0;
3642 rpc_restart_call_prepare(task);
3646 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3648 struct nfs4_closedata *calldata = data;
3649 struct nfs4_state *state = calldata->state;
3650 struct inode *inode = calldata->inode;
3651 struct nfs_server *server = NFS_SERVER(inode);
3652 struct pnfs_layout_hdr *lo;
3653 bool is_rdonly, is_wronly, is_rdwr;
3656 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3659 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3660 spin_lock(&state->owner->so_lock);
3661 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3662 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3663 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3664 /* Calculate the change in open mode */
3665 calldata->arg.fmode = 0;
3666 if (state->n_rdwr == 0) {
3667 if (state->n_rdonly == 0)
3668 call_close |= is_rdonly;
3670 calldata->arg.fmode |= FMODE_READ;
3671 if (state->n_wronly == 0)
3672 call_close |= is_wronly;
3674 calldata->arg.fmode |= FMODE_WRITE;
3675 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3676 call_close |= is_rdwr;
3678 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3680 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3681 if (!nfs4_valid_open_stateid(state))
3683 spin_unlock(&state->owner->so_lock);
3686 /* Note: exit _without_ calling nfs4_close_done */
3690 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3691 nfs_release_seqid(calldata->arg.seqid);
3695 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3696 if (lo && !pnfs_layout_is_valid(lo)) {
3697 calldata->arg.lr_args = NULL;
3698 calldata->res.lr_res = NULL;
3701 if (calldata->arg.fmode == 0)
3702 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3704 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3705 /* Close-to-open cache consistency revalidation */
3706 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3707 nfs4_bitmask_set(calldata->arg.bitmask_store,
3708 server->cache_consistency_bitmask,
3710 calldata->arg.bitmask = calldata->arg.bitmask_store;
3712 calldata->arg.bitmask = NULL;
3715 calldata->arg.share_access =
3716 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3717 calldata->arg.fmode, 0);
3719 if (calldata->res.fattr == NULL)
3720 calldata->arg.bitmask = NULL;
3721 else if (calldata->arg.bitmask == NULL)
3722 calldata->res.fattr = NULL;
3723 calldata->timestamp = jiffies;
3724 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3725 &calldata->arg.seq_args,
3726 &calldata->res.seq_res,
3728 nfs_release_seqid(calldata->arg.seqid);
3731 task->tk_action = NULL;
3733 nfs4_sequence_done(task, &calldata->res.seq_res);
3736 static const struct rpc_call_ops nfs4_close_ops = {
3737 .rpc_call_prepare = nfs4_close_prepare,
3738 .rpc_call_done = nfs4_close_done,
3739 .rpc_release = nfs4_free_closedata,
3743 * It is possible for data to be read/written from a mem-mapped file
3744 * after the sys_close call (which hits the vfs layer as a flush).
3745 * This means that we can't safely call nfsv4 close on a file until
3746 * the inode is cleared. This in turn means that we are not good
3747 * NFSv4 citizens - we do not indicate to the server to update the file's
3748 * share state even when we are done with one of the three share
3749 * stateid's in the inode.
3751 * NOTE: Caller must be holding the sp->so_owner semaphore!
3753 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3755 struct nfs_server *server = NFS_SERVER(state->inode);
3756 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3757 struct nfs4_closedata *calldata;
3758 struct nfs4_state_owner *sp = state->owner;
3759 struct rpc_task *task;
3760 struct rpc_message msg = {
3761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3762 .rpc_cred = state->owner->so_cred,
3764 struct rpc_task_setup task_setup_data = {
3765 .rpc_client = server->client,
3766 .rpc_message = &msg,
3767 .callback_ops = &nfs4_close_ops,
3768 .workqueue = nfsiod_workqueue,
3769 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3771 int status = -ENOMEM;
3773 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3774 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3776 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3777 &task_setup_data.rpc_client, &msg);
3779 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3780 if (calldata == NULL)
3782 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3783 calldata->inode = state->inode;
3784 calldata->state = state;
3785 calldata->arg.fh = NFS_FH(state->inode);
3786 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3787 goto out_free_calldata;
3788 /* Serialization for the sequence id */
3789 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3790 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3791 if (IS_ERR(calldata->arg.seqid))
3792 goto out_free_calldata;
3793 nfs_fattr_init(&calldata->fattr);
3794 calldata->arg.fmode = 0;
3795 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3796 calldata->res.fattr = &calldata->fattr;
3797 calldata->res.seqid = calldata->arg.seqid;
3798 calldata->res.server = server;
3799 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3800 calldata->lr.roc = pnfs_roc(state->inode,
3801 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3802 if (calldata->lr.roc) {
3803 calldata->arg.lr_args = &calldata->lr.arg;
3804 calldata->res.lr_res = &calldata->lr.res;
3806 nfs_sb_active(calldata->inode->i_sb);
3808 msg.rpc_argp = &calldata->arg;
3809 msg.rpc_resp = &calldata->res;
3810 task_setup_data.callback_data = calldata;
3811 task = rpc_run_task(&task_setup_data);
3813 return PTR_ERR(task);
3816 status = rpc_wait_for_completion_task(task);
3822 nfs4_put_open_state(state);
3823 nfs4_put_state_owner(sp);
3827 static struct inode *
3828 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3829 int open_flags, struct iattr *attr, int *opened)
3831 struct nfs4_state *state;
3832 struct nfs4_label l, *label;
3834 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3836 /* Protect against concurrent sillydeletes */
3837 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3839 nfs4_label_release_security(label);
3842 return ERR_CAST(state);
3843 return state->inode;
3846 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3848 if (ctx->state == NULL)
3851 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3853 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3856 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3857 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3858 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3860 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3862 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3863 struct nfs4_server_caps_arg args = {
3867 struct nfs4_server_caps_res res = {};
3868 struct rpc_message msg = {
3869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3876 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3877 FATTR4_WORD0_FH_EXPIRE_TYPE |
3878 FATTR4_WORD0_LINK_SUPPORT |
3879 FATTR4_WORD0_SYMLINK_SUPPORT |
3880 FATTR4_WORD0_ACLSUPPORT |
3881 FATTR4_WORD0_CASE_INSENSITIVE |
3882 FATTR4_WORD0_CASE_PRESERVING;
3884 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3886 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3888 /* Sanity check the server answers */
3889 switch (minorversion) {
3891 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3892 res.attr_bitmask[2] = 0;
3895 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3898 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3900 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3901 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3902 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3903 server->fattr_valid = NFS_ATTR_FATTR_V4;
3904 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3905 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3906 server->caps |= NFS_CAP_ACLS;
3907 if (res.has_links != 0)
3908 server->caps |= NFS_CAP_HARDLINKS;
3909 if (res.has_symlinks != 0)
3910 server->caps |= NFS_CAP_SYMLINKS;
3911 if (res.case_insensitive)
3912 server->caps |= NFS_CAP_CASE_INSENSITIVE;
3913 if (res.case_preserving)
3914 server->caps |= NFS_CAP_CASE_PRESERVING;
3915 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3916 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3917 server->caps |= NFS_CAP_SECURITY_LABEL;
3919 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3920 server->caps |= NFS_CAP_FS_LOCATIONS;
3921 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3922 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3923 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3924 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3925 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3926 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3927 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3928 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3929 NFS_ATTR_FATTR_OWNER_NAME);
3930 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3931 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3932 NFS_ATTR_FATTR_GROUP_NAME);
3933 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3934 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3935 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3936 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3937 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3938 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3939 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3940 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3941 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3942 sizeof(server->attr_bitmask));
3943 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3945 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3946 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3947 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3948 server->cache_consistency_bitmask[2] = 0;
3950 /* Avoid a regression due to buggy server */
3951 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3952 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3953 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3954 sizeof(server->exclcreat_bitmask));
3956 server->acl_bitmask = res.acl_bitmask;
3957 server->fh_expire_type = res.fh_expire_type;
3963 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3965 struct nfs4_exception exception = {
3966 .interruptible = true,
3970 nfs4_server_set_init_caps(server);
3972 err = nfs4_handle_exception(server,
3973 _nfs4_server_capabilities(server, fhandle),
3975 } while (exception.retry);
3979 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3980 struct nfs_client *clp,
3981 struct nfs_server *server)
3985 for (i = 0; i < location->nservers; i++) {
3986 struct nfs4_string *srv_loc = &location->servers[i];
3987 struct sockaddr_storage addr;
3989 struct xprt_create xprt_args = {
3993 struct nfs4_add_xprt_data xprtdata = {
3996 struct rpc_add_xprt_test rpcdata = {
3997 .add_xprt_test = clp->cl_mvops->session_trunk,
4000 char *servername = NULL;
4005 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
4006 &addr, sizeof(addr),
4007 clp->cl_net, server->port);
4010 xprt_args.dstaddr = (struct sockaddr *)&addr;
4011 xprt_args.addrlen = addrlen;
4012 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
4015 memcpy(servername, srv_loc->data, srv_loc->len);
4016 servername[srv_loc->len] = '\0';
4017 xprt_args.servername = servername;
4019 xprtdata.cred = nfs4_get_clid_cred(clp);
4020 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
4021 rpc_clnt_setup_test_and_add_xprt,
4024 put_cred(xprtdata.cred);
4029 static int _nfs4_discover_trunking(struct nfs_server *server,
4030 struct nfs_fh *fhandle)
4032 struct nfs4_fs_locations *locations = NULL;
4034 const struct cred *cred;
4035 struct nfs_client *clp = server->nfs_client;
4036 const struct nfs4_state_maintenance_ops *ops =
4037 clp->cl_mvops->state_renewal_ops;
4038 int status = -ENOMEM, i;
4040 cred = ops->get_state_renewal_cred(clp);
4042 cred = nfs4_get_clid_cred(clp);
4047 page = alloc_page(GFP_KERNEL);
4050 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4053 locations->fattr = nfs_alloc_fattr();
4054 if (!locations->fattr)
4057 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4062 for (i = 0; i < locations->nlocations; i++)
4063 test_fs_location_for_trunking(&locations->locations[i], clp,
4066 kfree(locations->fattr);
4076 static int nfs4_discover_trunking(struct nfs_server *server,
4077 struct nfs_fh *fhandle)
4079 struct nfs4_exception exception = {
4080 .interruptible = true,
4082 struct nfs_client *clp = server->nfs_client;
4085 if (!nfs4_has_session(clp))
4088 err = nfs4_handle_exception(server,
4089 _nfs4_discover_trunking(server, fhandle),
4091 } while (exception.retry);
4096 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4097 struct nfs_fsinfo *info)
4100 struct nfs4_lookup_root_arg args = {
4103 struct nfs4_lookup_res res = {
4105 .fattr = info->fattr,
4108 struct rpc_message msg = {
4109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4114 bitmask[0] = nfs4_fattr_bitmap[0];
4115 bitmask[1] = nfs4_fattr_bitmap[1];
4117 * Process the label in the upcoming getfattr
4119 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4121 nfs_fattr_init(info->fattr);
4122 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4125 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4126 struct nfs_fsinfo *info)
4128 struct nfs4_exception exception = {
4129 .interruptible = true,
4133 err = _nfs4_lookup_root(server, fhandle, info);
4134 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4137 case -NFS4ERR_WRONGSEC:
4140 err = nfs4_handle_exception(server, err, &exception);
4142 } while (exception.retry);
4147 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4148 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4150 struct rpc_auth_create_args auth_args = {
4151 .pseudoflavor = flavor,
4153 struct rpc_auth *auth;
4155 auth = rpcauth_create(&auth_args, server->client);
4158 return nfs4_lookup_root(server, fhandle, info);
4162 * Retry pseudoroot lookup with various security flavors. We do this when:
4164 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4165 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4167 * Returns zero on success, or a negative NFS4ERR value, or a
4168 * negative errno value.
4170 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4171 struct nfs_fsinfo *info)
4173 /* Per 3530bis 15.33.5 */
4174 static const rpc_authflavor_t flav_array[] = {
4178 RPC_AUTH_UNIX, /* courtesy */
4181 int status = -EPERM;
4184 if (server->auth_info.flavor_len > 0) {
4185 /* try each flavor specified by user */
4186 for (i = 0; i < server->auth_info.flavor_len; i++) {
4187 status = nfs4_lookup_root_sec(server, fhandle, info,
4188 server->auth_info.flavors[i]);
4189 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4194 /* no flavors specified by user, try default list */
4195 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4196 status = nfs4_lookup_root_sec(server, fhandle, info,
4198 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4205 * -EACCES could mean that the user doesn't have correct permissions
4206 * to access the mount. It could also mean that we tried to mount
4207 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4208 * existing mount programs don't handle -EACCES very well so it should
4209 * be mapped to -EPERM instead.
4211 if (status == -EACCES)
4217 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4218 * @server: initialized nfs_server handle
4219 * @fhandle: we fill in the pseudo-fs root file handle
4220 * @info: we fill in an FSINFO struct
4221 * @auth_probe: probe the auth flavours
4223 * Returns zero on success, or a negative errno.
4225 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4226 struct nfs_fsinfo *info,
4232 status = nfs4_lookup_root(server, fhandle, info);
4234 if (auth_probe || status == NFS4ERR_WRONGSEC)
4235 status = server->nfs_client->cl_mvops->find_root_sec(server,
4239 status = nfs4_server_capabilities(server, fhandle);
4241 status = nfs4_do_fsinfo(server, fhandle, info);
4243 return nfs4_map_errors(status);
4246 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4247 struct nfs_fsinfo *info)
4250 struct nfs_fattr *fattr = info->fattr;
4252 error = nfs4_server_capabilities(server, mntfh);
4254 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4258 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4260 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4264 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4265 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4266 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4273 * Get locations and (maybe) other attributes of a referral.
4274 * Note that we'll actually follow the referral later when
4275 * we detect fsid mismatch in inode revalidation
4277 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4278 const struct qstr *name, struct nfs_fattr *fattr,
4279 struct nfs_fh *fhandle)
4281 int status = -ENOMEM;
4282 struct page *page = NULL;
4283 struct nfs4_fs_locations *locations = NULL;
4285 page = alloc_page(GFP_KERNEL);
4288 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4289 if (locations == NULL)
4292 locations->fattr = fattr;
4294 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4299 * If the fsid didn't change, this is a migration event, not a
4300 * referral. Cause us to drop into the exception handler, which
4301 * will kick off migration recovery.
4303 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4304 dprintk("%s: server did not return a different fsid for"
4305 " a referral at %s\n", __func__, name->name);
4306 status = -NFS4ERR_MOVED;
4309 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4310 nfs_fixup_referral_attributes(fattr);
4311 memset(fhandle, 0, sizeof(struct nfs_fh));
4319 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4320 struct nfs_fattr *fattr, struct inode *inode)
4322 __u32 bitmask[NFS4_BITMASK_SZ];
4323 struct nfs4_getattr_arg args = {
4327 struct nfs4_getattr_res res = {
4331 struct rpc_message msg = {
4332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4336 unsigned short task_flags = 0;
4338 if (nfs4_has_session(server->nfs_client))
4339 task_flags = RPC_TASK_MOVEABLE;
4341 /* Is this is an attribute revalidation, subject to softreval? */
4342 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4343 task_flags |= RPC_TASK_TIMEOUT;
4345 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4346 nfs_fattr_init(fattr);
4347 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4348 return nfs4_do_call_sync(server->client, server, &msg,
4349 &args.seq_args, &res.seq_res, task_flags);
4352 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4353 struct nfs_fattr *fattr, struct inode *inode)
4355 struct nfs4_exception exception = {
4356 .interruptible = true,
4360 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4361 trace_nfs4_getattr(server, fhandle, fattr, err);
4362 err = nfs4_handle_exception(server, err,
4364 } while (exception.retry);
4369 * The file is not closed if it is opened due to the a request to change
4370 * the size of the file. The open call will not be needed once the
4371 * VFS layer lookup-intents are implemented.
4373 * Close is called when the inode is destroyed.
4374 * If we haven't opened the file for O_WRONLY, we
4375 * need to in the size_change case to obtain a stateid.
4378 * Because OPEN is always done by name in nfsv4, it is
4379 * possible that we opened a different file by the same
4380 * name. We can recognize this race condition, but we
4381 * can't do anything about it besides returning an error.
4383 * This will be fixed with VFS changes (lookup-intent).
4386 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4387 struct iattr *sattr)
4389 struct inode *inode = d_inode(dentry);
4390 const struct cred *cred = NULL;
4391 struct nfs_open_context *ctx = NULL;
4394 if (pnfs_ld_layoutret_on_setattr(inode) &&
4395 sattr->ia_valid & ATTR_SIZE &&
4396 sattr->ia_size < i_size_read(inode))
4397 pnfs_commit_and_return_layout(inode);
4399 nfs_fattr_init(fattr);
4401 /* Deal with open(O_TRUNC) */
4402 if (sattr->ia_valid & ATTR_OPEN)
4403 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4405 /* Optimization: if the end result is no change, don't RPC */
4406 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4409 /* Search for an existing open(O_WRITE) file */
4410 if (sattr->ia_valid & ATTR_FILE) {
4412 ctx = nfs_file_open_context(sattr->ia_file);
4417 /* Return any delegations if we're going to change ACLs */
4418 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4419 nfs4_inode_make_writeable(inode);
4421 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4423 nfs_setattr_update_inode(inode, sattr, fattr);
4424 nfs_setsecurity(inode, fattr);
4429 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4430 struct dentry *dentry, struct nfs_fh *fhandle,
4431 struct nfs_fattr *fattr)
4433 struct nfs_server *server = NFS_SERVER(dir);
4435 struct nfs4_lookup_arg args = {
4436 .bitmask = server->attr_bitmask,
4437 .dir_fh = NFS_FH(dir),
4438 .name = &dentry->d_name,
4440 struct nfs4_lookup_res res = {
4445 struct rpc_message msg = {
4446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4450 unsigned short task_flags = 0;
4452 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4453 task_flags = RPC_TASK_MOVEABLE;
4455 /* Is this is an attribute revalidation, subject to softreval? */
4456 if (nfs_lookup_is_soft_revalidate(dentry))
4457 task_flags |= RPC_TASK_TIMEOUT;
4459 args.bitmask = nfs4_bitmask(server, fattr->label);
4461 nfs_fattr_init(fattr);
4463 dprintk("NFS call lookup %pd2\n", dentry);
4464 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4465 status = nfs4_do_call_sync(clnt, server, &msg,
4466 &args.seq_args, &res.seq_res, task_flags);
4467 dprintk("NFS reply lookup: %d\n", status);
4471 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4473 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4474 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4475 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4479 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4480 struct dentry *dentry, struct nfs_fh *fhandle,
4481 struct nfs_fattr *fattr)
4483 struct nfs4_exception exception = {
4484 .interruptible = true,
4486 struct rpc_clnt *client = *clnt;
4487 const struct qstr *name = &dentry->d_name;
4490 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4491 trace_nfs4_lookup(dir, name, err);
4493 case -NFS4ERR_BADNAME:
4496 case -NFS4ERR_MOVED:
4497 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4498 if (err == -NFS4ERR_MOVED)
4499 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4501 case -NFS4ERR_WRONGSEC:
4503 if (client != *clnt)
4505 client = nfs4_negotiate_security(client, dir, name);
4507 return PTR_ERR(client);
4509 exception.retry = 1;
4512 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4514 } while (exception.retry);
4519 else if (client != *clnt)
4520 rpc_shutdown_client(client);
4525 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4526 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4529 struct rpc_clnt *client = NFS_CLIENT(dir);
4531 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4532 if (client != NFS_CLIENT(dir)) {
4533 rpc_shutdown_client(client);
4534 nfs_fixup_secinfo_attributes(fattr);
4540 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4541 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4543 struct rpc_clnt *client = NFS_CLIENT(dir);
4546 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4548 return ERR_PTR(status);
4549 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4552 static int _nfs4_proc_lookupp(struct inode *inode,
4553 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4555 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4556 struct nfs_server *server = NFS_SERVER(inode);
4558 struct nfs4_lookupp_arg args = {
4559 .bitmask = server->attr_bitmask,
4560 .fh = NFS_FH(inode),
4562 struct nfs4_lookupp_res res = {
4567 struct rpc_message msg = {
4568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4572 unsigned short task_flags = 0;
4574 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4575 task_flags |= RPC_TASK_TIMEOUT;
4577 args.bitmask = nfs4_bitmask(server, fattr->label);
4579 nfs_fattr_init(fattr);
4581 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4582 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4583 &res.seq_res, task_flags);
4584 dprintk("NFS reply lookupp: %d\n", status);
4588 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4589 struct nfs_fattr *fattr)
4591 struct nfs4_exception exception = {
4592 .interruptible = true,
4596 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4597 trace_nfs4_lookupp(inode, err);
4598 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4600 } while (exception.retry);
4604 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4605 const struct cred *cred)
4607 struct nfs_server *server = NFS_SERVER(inode);
4608 struct nfs4_accessargs args = {
4609 .fh = NFS_FH(inode),
4610 .access = entry->mask,
4612 struct nfs4_accessres res = {
4615 struct rpc_message msg = {
4616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4623 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4624 res.fattr = nfs_alloc_fattr();
4625 if (res.fattr == NULL)
4627 args.bitmask = server->cache_consistency_bitmask;
4629 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4631 nfs_access_set_mask(entry, res.access);
4633 nfs_refresh_inode(inode, res.fattr);
4635 nfs_free_fattr(res.fattr);
4639 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4640 const struct cred *cred)
4642 struct nfs4_exception exception = {
4643 .interruptible = true,
4647 err = _nfs4_proc_access(inode, entry, cred);
4648 trace_nfs4_access(inode, err);
4649 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4651 } while (exception.retry);
4656 * TODO: For the time being, we don't try to get any attributes
4657 * along with any of the zero-copy operations READ, READDIR,
4660 * In the case of the first three, we want to put the GETATTR
4661 * after the read-type operation -- this is because it is hard
4662 * to predict the length of a GETATTR response in v4, and thus
4663 * align the READ data correctly. This means that the GETATTR
4664 * may end up partially falling into the page cache, and we should
4665 * shift it into the 'tail' of the xdr_buf before processing.
4666 * To do this efficiently, we need to know the total length
4667 * of data received, which doesn't seem to be available outside
4670 * In the case of WRITE, we also want to put the GETATTR after
4671 * the operation -- in this case because we want to make sure
4672 * we get the post-operation mtime and size.
4674 * Both of these changes to the XDR layer would in fact be quite
4675 * minor, but I decided to leave them for a subsequent patch.
4677 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4678 unsigned int pgbase, unsigned int pglen)
4680 struct nfs4_readlink args = {
4681 .fh = NFS_FH(inode),
4686 struct nfs4_readlink_res res;
4687 struct rpc_message msg = {
4688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4693 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4696 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4697 unsigned int pgbase, unsigned int pglen)
4699 struct nfs4_exception exception = {
4700 .interruptible = true,
4704 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4705 trace_nfs4_readlink(inode, err);
4706 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4708 } while (exception.retry);
4713 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4716 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4719 struct nfs_server *server = NFS_SERVER(dir);
4720 struct nfs4_label l, *ilabel;
4721 struct nfs_open_context *ctx;
4722 struct nfs4_state *state;
4725 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4727 return PTR_ERR(ctx);
4729 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4731 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4732 sattr->ia_mode &= ~current_umask();
4733 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4734 if (IS_ERR(state)) {
4735 status = PTR_ERR(state);
4739 nfs4_label_release_security(ilabel);
4740 put_nfs_open_context(ctx);
4745 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4747 struct nfs_server *server = NFS_SERVER(dir);
4748 struct nfs_removeargs args = {
4752 struct nfs_removeres res = {
4755 struct rpc_message msg = {
4756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4760 unsigned long timestamp = jiffies;
4763 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4765 spin_lock(&dir->i_lock);
4766 /* Removing a directory decrements nlink in the parent */
4767 if (ftype == NF4DIR && dir->i_nlink > 2)
4768 nfs4_dec_nlink_locked(dir);
4769 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4770 NFS_INO_INVALID_DATA);
4771 spin_unlock(&dir->i_lock);
4776 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4778 struct nfs4_exception exception = {
4779 .interruptible = true,
4781 struct inode *inode = d_inode(dentry);
4785 if (inode->i_nlink == 1)
4786 nfs4_inode_return_delegation(inode);
4788 nfs4_inode_make_writeable(inode);
4791 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4792 trace_nfs4_remove(dir, &dentry->d_name, err);
4793 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4795 } while (exception.retry);
4799 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4801 struct nfs4_exception exception = {
4802 .interruptible = true,
4807 err = _nfs4_proc_remove(dir, name, NF4DIR);
4808 trace_nfs4_remove(dir, name, err);
4809 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4811 } while (exception.retry);
4815 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4816 struct dentry *dentry,
4817 struct inode *inode)
4819 struct nfs_removeargs *args = msg->rpc_argp;
4820 struct nfs_removeres *res = msg->rpc_resp;
4822 res->server = NFS_SB(dentry->d_sb);
4823 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4824 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4826 nfs_fattr_init(res->dir_attr);
4829 nfs4_inode_return_delegation(inode);
4830 nfs_d_prune_case_insensitive_aliases(inode);
4834 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4836 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4837 &data->args.seq_args,
4842 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4844 struct nfs_unlinkdata *data = task->tk_calldata;
4845 struct nfs_removeres *res = &data->res;
4847 if (!nfs4_sequence_done(task, &res->seq_res))
4849 if (nfs4_async_handle_error(task, res->server, NULL,
4850 &data->timeout) == -EAGAIN)
4852 if (task->tk_status == 0)
4853 nfs4_update_changeattr(dir, &res->cinfo,
4854 res->dir_attr->time_start,
4855 NFS_INO_INVALID_DATA);
4859 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4860 struct dentry *old_dentry,
4861 struct dentry *new_dentry)
4863 struct nfs_renameargs *arg = msg->rpc_argp;
4864 struct nfs_renameres *res = msg->rpc_resp;
4865 struct inode *old_inode = d_inode(old_dentry);
4866 struct inode *new_inode = d_inode(new_dentry);
4869 nfs4_inode_make_writeable(old_inode);
4871 nfs4_inode_return_delegation(new_inode);
4872 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4873 res->server = NFS_SB(old_dentry->d_sb);
4874 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4877 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4879 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4880 &data->args.seq_args,
4885 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4886 struct inode *new_dir)
4888 struct nfs_renamedata *data = task->tk_calldata;
4889 struct nfs_renameres *res = &data->res;
4891 if (!nfs4_sequence_done(task, &res->seq_res))
4893 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4896 if (task->tk_status == 0) {
4897 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4898 if (new_dir != old_dir) {
4899 /* Note: If we moved a directory, nlink will change */
4900 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4901 res->old_fattr->time_start,
4902 NFS_INO_INVALID_NLINK |
4903 NFS_INO_INVALID_DATA);
4904 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4905 res->new_fattr->time_start,
4906 NFS_INO_INVALID_NLINK |
4907 NFS_INO_INVALID_DATA);
4909 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4910 res->old_fattr->time_start,
4911 NFS_INO_INVALID_DATA);
4916 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4918 struct nfs_server *server = NFS_SERVER(inode);
4919 __u32 bitmask[NFS4_BITMASK_SZ];
4920 struct nfs4_link_arg arg = {
4921 .fh = NFS_FH(inode),
4922 .dir_fh = NFS_FH(dir),
4926 struct nfs4_link_res res = {
4929 struct rpc_message msg = {
4930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4934 int status = -ENOMEM;
4936 res.fattr = nfs_alloc_fattr_with_label(server);
4937 if (res.fattr == NULL)
4940 nfs4_inode_make_writeable(inode);
4941 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4942 NFS_INO_INVALID_CHANGE);
4943 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4945 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4946 NFS_INO_INVALID_DATA);
4947 nfs4_inc_nlink(inode);
4948 status = nfs_post_op_update_inode(inode, res.fattr);
4950 nfs_setsecurity(inode, res.fattr);
4954 nfs_free_fattr(res.fattr);
4958 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4960 struct nfs4_exception exception = {
4961 .interruptible = true,
4965 err = nfs4_handle_exception(NFS_SERVER(inode),
4966 _nfs4_proc_link(inode, dir, name),
4968 } while (exception.retry);
4972 struct nfs4_createdata {
4973 struct rpc_message msg;
4974 struct nfs4_create_arg arg;
4975 struct nfs4_create_res res;
4977 struct nfs_fattr fattr;
4980 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4981 const struct qstr *name, struct iattr *sattr, u32 ftype)
4983 struct nfs4_createdata *data;
4985 data = kzalloc(sizeof(*data), GFP_KERNEL);
4987 struct nfs_server *server = NFS_SERVER(dir);
4989 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4990 if (IS_ERR(data->fattr.label))
4993 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4994 data->msg.rpc_argp = &data->arg;
4995 data->msg.rpc_resp = &data->res;
4996 data->arg.dir_fh = NFS_FH(dir);
4997 data->arg.server = server;
4998 data->arg.name = name;
4999 data->arg.attrs = sattr;
5000 data->arg.ftype = ftype;
5001 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
5002 data->arg.umask = current_umask();
5003 data->res.server = server;
5004 data->res.fh = &data->fh;
5005 data->res.fattr = &data->fattr;
5006 nfs_fattr_init(data->res.fattr);
5014 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
5016 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
5017 &data->arg.seq_args, &data->res.seq_res, 1);
5019 spin_lock(&dir->i_lock);
5020 /* Creating a directory bumps nlink in the parent */
5021 if (data->arg.ftype == NF4DIR)
5022 nfs4_inc_nlink_locked(dir);
5023 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
5024 data->res.fattr->time_start,
5025 NFS_INO_INVALID_DATA);
5026 spin_unlock(&dir->i_lock);
5027 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
5032 static void nfs4_free_createdata(struct nfs4_createdata *data)
5034 nfs4_label_free(data->fattr.label);
5038 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5039 struct page *page, unsigned int len, struct iattr *sattr,
5040 struct nfs4_label *label)
5042 struct nfs4_createdata *data;
5043 int status = -ENAMETOOLONG;
5045 if (len > NFS4_MAXPATHLEN)
5049 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5053 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5054 data->arg.u.symlink.pages = &page;
5055 data->arg.u.symlink.len = len;
5056 data->arg.label = label;
5058 status = nfs4_do_create(dir, dentry, data);
5060 nfs4_free_createdata(data);
5065 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5066 struct page *page, unsigned int len, struct iattr *sattr)
5068 struct nfs4_exception exception = {
5069 .interruptible = true,
5071 struct nfs4_label l, *label;
5074 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5077 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5078 trace_nfs4_symlink(dir, &dentry->d_name, err);
5079 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5081 } while (exception.retry);
5083 nfs4_label_release_security(label);
5087 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5088 struct iattr *sattr, struct nfs4_label *label)
5090 struct nfs4_createdata *data;
5091 int status = -ENOMEM;
5093 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5097 data->arg.label = label;
5098 status = nfs4_do_create(dir, dentry, data);
5100 nfs4_free_createdata(data);
5105 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5106 struct iattr *sattr)
5108 struct nfs_server *server = NFS_SERVER(dir);
5109 struct nfs4_exception exception = {
5110 .interruptible = true,
5112 struct nfs4_label l, *label;
5115 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5117 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5118 sattr->ia_mode &= ~current_umask();
5120 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5121 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5122 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5124 } while (exception.retry);
5125 nfs4_label_release_security(label);
5130 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5131 struct nfs_readdir_res *nr_res)
5133 struct inode *dir = d_inode(nr_arg->dentry);
5134 struct nfs_server *server = NFS_SERVER(dir);
5135 struct nfs4_readdir_arg args = {
5137 .pages = nr_arg->pages,
5139 .count = nr_arg->page_len,
5140 .plus = nr_arg->plus,
5142 struct nfs4_readdir_res res;
5143 struct rpc_message msg = {
5144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5147 .rpc_cred = nr_arg->cred,
5151 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5152 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5153 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5154 args.bitmask = server->attr_bitmask_nl;
5156 args.bitmask = server->attr_bitmask;
5158 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5159 res.pgbase = args.pgbase;
5160 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5163 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5164 status += args.pgbase;
5167 nfs_invalidate_atime(dir);
5169 dprintk("%s: returns %d\n", __func__, status);
5173 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5174 struct nfs_readdir_res *res)
5176 struct nfs4_exception exception = {
5177 .interruptible = true,
5181 err = _nfs4_proc_readdir(arg, res);
5182 trace_nfs4_readdir(d_inode(arg->dentry), err);
5183 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5185 } while (exception.retry);
5189 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5190 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5192 struct nfs4_createdata *data;
5193 int mode = sattr->ia_mode;
5194 int status = -ENOMEM;
5196 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5201 data->arg.ftype = NF4FIFO;
5202 else if (S_ISBLK(mode)) {
5203 data->arg.ftype = NF4BLK;
5204 data->arg.u.device.specdata1 = MAJOR(rdev);
5205 data->arg.u.device.specdata2 = MINOR(rdev);
5207 else if (S_ISCHR(mode)) {
5208 data->arg.ftype = NF4CHR;
5209 data->arg.u.device.specdata1 = MAJOR(rdev);
5210 data->arg.u.device.specdata2 = MINOR(rdev);
5211 } else if (!S_ISSOCK(mode)) {
5216 data->arg.label = label;
5217 status = nfs4_do_create(dir, dentry, data);
5219 nfs4_free_createdata(data);
5224 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5225 struct iattr *sattr, dev_t rdev)
5227 struct nfs_server *server = NFS_SERVER(dir);
5228 struct nfs4_exception exception = {
5229 .interruptible = true,
5231 struct nfs4_label l, *label;
5234 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5236 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5237 sattr->ia_mode &= ~current_umask();
5239 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5240 trace_nfs4_mknod(dir, &dentry->d_name, err);
5241 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5243 } while (exception.retry);
5245 nfs4_label_release_security(label);
5250 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5251 struct nfs_fsstat *fsstat)
5253 struct nfs4_statfs_arg args = {
5255 .bitmask = server->attr_bitmask,
5257 struct nfs4_statfs_res res = {
5260 struct rpc_message msg = {
5261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5266 nfs_fattr_init(fsstat->fattr);
5267 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5270 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5272 struct nfs4_exception exception = {
5273 .interruptible = true,
5277 err = nfs4_handle_exception(server,
5278 _nfs4_proc_statfs(server, fhandle, fsstat),
5280 } while (exception.retry);
5284 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5285 struct nfs_fsinfo *fsinfo)
5287 struct nfs4_fsinfo_arg args = {
5289 .bitmask = server->attr_bitmask,
5291 struct nfs4_fsinfo_res res = {
5294 struct rpc_message msg = {
5295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5300 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5303 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5305 struct nfs4_exception exception = {
5306 .interruptible = true,
5311 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5312 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5314 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5317 err = nfs4_handle_exception(server, err, &exception);
5318 } while (exception.retry);
5322 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5326 nfs_fattr_init(fsinfo->fattr);
5327 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5329 /* block layout checks this! */
5330 server->pnfs_blksize = fsinfo->blksize;
5331 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5337 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5338 struct nfs_pathconf *pathconf)
5340 struct nfs4_pathconf_arg args = {
5342 .bitmask = server->attr_bitmask,
5344 struct nfs4_pathconf_res res = {
5345 .pathconf = pathconf,
5347 struct rpc_message msg = {
5348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5353 /* None of the pathconf attributes are mandatory to implement */
5354 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5355 memset(pathconf, 0, sizeof(*pathconf));
5359 nfs_fattr_init(pathconf->fattr);
5360 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5363 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5364 struct nfs_pathconf *pathconf)
5366 struct nfs4_exception exception = {
5367 .interruptible = true,
5372 err = nfs4_handle_exception(server,
5373 _nfs4_proc_pathconf(server, fhandle, pathconf),
5375 } while (exception.retry);
5379 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5380 const struct nfs_open_context *ctx,
5381 const struct nfs_lock_context *l_ctx,
5384 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5386 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5388 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5389 const struct nfs_open_context *ctx,
5390 const struct nfs_lock_context *l_ctx,
5393 nfs4_stateid _current_stateid;
5395 /* If the current stateid represents a lost lock, then exit */
5396 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5398 return nfs4_stateid_match(stateid, &_current_stateid);
5401 static bool nfs4_error_stateid_expired(int err)
5404 case -NFS4ERR_DELEG_REVOKED:
5405 case -NFS4ERR_ADMIN_REVOKED:
5406 case -NFS4ERR_BAD_STATEID:
5407 case -NFS4ERR_STALE_STATEID:
5408 case -NFS4ERR_OLD_STATEID:
5409 case -NFS4ERR_OPENMODE:
5410 case -NFS4ERR_EXPIRED:
5416 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5418 struct nfs_server *server = NFS_SERVER(hdr->inode);
5420 trace_nfs4_read(hdr, task->tk_status);
5421 if (task->tk_status < 0) {
5422 struct nfs4_exception exception = {
5423 .inode = hdr->inode,
5424 .state = hdr->args.context->state,
5425 .stateid = &hdr->args.stateid,
5427 task->tk_status = nfs4_async_handle_exception(task,
5428 server, task->tk_status, &exception);
5429 if (exception.retry) {
5430 rpc_restart_call_prepare(task);
5435 if (task->tk_status > 0)
5436 renew_lease(server, hdr->timestamp);
5440 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5441 struct nfs_pgio_args *args)
5444 if (!nfs4_error_stateid_expired(task->tk_status) ||
5445 nfs4_stateid_is_current(&args->stateid,
5450 rpc_restart_call_prepare(task);
5454 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5455 struct nfs_pgio_header *hdr)
5457 struct nfs_server *server = NFS_SERVER(hdr->inode);
5458 struct rpc_message *msg = &task->tk_msg;
5460 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5461 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5462 server->caps &= ~NFS_CAP_READ_PLUS;
5463 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5464 rpc_restart_call_prepare(task);
5470 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5472 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5474 if (nfs4_read_stateid_changed(task, &hdr->args))
5476 if (nfs4_read_plus_not_supported(task, hdr))
5478 if (task->tk_status > 0)
5479 nfs_invalidate_atime(hdr->inode);
5480 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5481 nfs4_read_done_cb(task, hdr);
5484 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5485 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5486 struct rpc_message *msg)
5488 /* Note: We don't use READ_PLUS with pNFS yet */
5489 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) {
5490 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5491 return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE);
5496 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5497 struct rpc_message *msg)
5501 #endif /* CONFIG_NFS_V4_2 */
5503 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5504 struct rpc_message *msg)
5506 hdr->timestamp = jiffies;
5507 if (!hdr->pgio_done_cb)
5508 hdr->pgio_done_cb = nfs4_read_done_cb;
5509 if (!nfs42_read_plus_support(hdr, msg))
5510 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5511 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5514 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5515 struct nfs_pgio_header *hdr)
5517 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5518 &hdr->args.seq_args,
5522 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5523 hdr->args.lock_context,
5524 hdr->rw_mode) == -EIO)
5526 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5531 static int nfs4_write_done_cb(struct rpc_task *task,
5532 struct nfs_pgio_header *hdr)
5534 struct inode *inode = hdr->inode;
5536 trace_nfs4_write(hdr, task->tk_status);
5537 if (task->tk_status < 0) {
5538 struct nfs4_exception exception = {
5539 .inode = hdr->inode,
5540 .state = hdr->args.context->state,
5541 .stateid = &hdr->args.stateid,
5543 task->tk_status = nfs4_async_handle_exception(task,
5544 NFS_SERVER(inode), task->tk_status,
5546 if (exception.retry) {
5547 rpc_restart_call_prepare(task);
5551 if (task->tk_status >= 0) {
5552 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5553 nfs_writeback_update_inode(hdr);
5558 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5559 struct nfs_pgio_args *args)
5562 if (!nfs4_error_stateid_expired(task->tk_status) ||
5563 nfs4_stateid_is_current(&args->stateid,
5568 rpc_restart_call_prepare(task);
5572 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5574 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5576 if (nfs4_write_stateid_changed(task, &hdr->args))
5578 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5579 nfs4_write_done_cb(task, hdr);
5583 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5585 /* Don't request attributes for pNFS or O_DIRECT writes */
5586 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5588 /* Otherwise, request attributes if and only if we don't hold
5591 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5594 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5595 struct inode *inode, unsigned long cache_validity)
5597 struct nfs_server *server = NFS_SERVER(inode);
5600 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5601 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5603 if (cache_validity & NFS_INO_INVALID_CHANGE)
5604 bitmask[0] |= FATTR4_WORD0_CHANGE;
5605 if (cache_validity & NFS_INO_INVALID_ATIME)
5606 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5607 if (cache_validity & NFS_INO_INVALID_MODE)
5608 bitmask[1] |= FATTR4_WORD1_MODE;
5609 if (cache_validity & NFS_INO_INVALID_OTHER)
5610 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5611 if (cache_validity & NFS_INO_INVALID_NLINK)
5612 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5613 if (cache_validity & NFS_INO_INVALID_CTIME)
5614 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5615 if (cache_validity & NFS_INO_INVALID_MTIME)
5616 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5617 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5618 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5620 if (cache_validity & NFS_INO_INVALID_SIZE)
5621 bitmask[0] |= FATTR4_WORD0_SIZE;
5623 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5624 bitmask[i] &= server->attr_bitmask[i];
5627 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5628 struct rpc_message *msg,
5629 struct rpc_clnt **clnt)
5631 struct nfs_server *server = NFS_SERVER(hdr->inode);
5633 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5634 hdr->args.bitmask = NULL;
5635 hdr->res.fattr = NULL;
5637 nfs4_bitmask_set(hdr->args.bitmask_store,
5638 server->cache_consistency_bitmask,
5639 hdr->inode, NFS_INO_INVALID_BLOCKS);
5640 hdr->args.bitmask = hdr->args.bitmask_store;
5643 if (!hdr->pgio_done_cb)
5644 hdr->pgio_done_cb = nfs4_write_done_cb;
5645 hdr->res.server = server;
5646 hdr->timestamp = jiffies;
5648 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5649 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5650 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5653 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5655 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5656 &data->args.seq_args,
5661 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5663 struct inode *inode = data->inode;
5665 trace_nfs4_commit(data, task->tk_status);
5666 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5667 NULL, NULL) == -EAGAIN) {
5668 rpc_restart_call_prepare(task);
5674 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5676 if (!nfs4_sequence_done(task, &data->res.seq_res))
5678 return data->commit_done_cb(task, data);
5681 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5682 struct rpc_clnt **clnt)
5684 struct nfs_server *server = NFS_SERVER(data->inode);
5686 if (data->commit_done_cb == NULL)
5687 data->commit_done_cb = nfs4_commit_done_cb;
5688 data->res.server = server;
5689 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5690 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5691 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5694 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5695 struct nfs_commitres *res)
5697 struct inode *dst_inode = file_inode(dst);
5698 struct nfs_server *server = NFS_SERVER(dst_inode);
5699 struct rpc_message msg = {
5700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5705 args->fh = NFS_FH(dst_inode);
5706 return nfs4_call_sync(server->client, server, &msg,
5707 &args->seq_args, &res->seq_res, 1);
5710 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5712 struct nfs_commitargs args = {
5716 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5717 struct nfs4_exception exception = { };
5721 status = _nfs4_proc_commit(dst, &args, res);
5722 status = nfs4_handle_exception(dst_server, status, &exception);
5723 } while (exception.retry);
5728 struct nfs4_renewdata {
5729 struct nfs_client *client;
5730 unsigned long timestamp;
5734 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5735 * standalone procedure for queueing an asynchronous RENEW.
5737 static void nfs4_renew_release(void *calldata)
5739 struct nfs4_renewdata *data = calldata;
5740 struct nfs_client *clp = data->client;
5742 if (refcount_read(&clp->cl_count) > 1)
5743 nfs4_schedule_state_renewal(clp);
5744 nfs_put_client(clp);
5748 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5750 struct nfs4_renewdata *data = calldata;
5751 struct nfs_client *clp = data->client;
5752 unsigned long timestamp = data->timestamp;
5754 trace_nfs4_renew_async(clp, task->tk_status);
5755 switch (task->tk_status) {
5758 case -NFS4ERR_LEASE_MOVED:
5759 nfs4_schedule_lease_moved_recovery(clp);
5762 /* Unless we're shutting down, schedule state recovery! */
5763 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5765 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5766 nfs4_schedule_lease_recovery(clp);
5769 nfs4_schedule_path_down_recovery(clp);
5771 do_renew_lease(clp, timestamp);
5774 static const struct rpc_call_ops nfs4_renew_ops = {
5775 .rpc_call_done = nfs4_renew_done,
5776 .rpc_release = nfs4_renew_release,
5779 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5781 struct rpc_message msg = {
5782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5786 struct nfs4_renewdata *data;
5788 if (renew_flags == 0)
5790 if (!refcount_inc_not_zero(&clp->cl_count))
5792 data = kmalloc(sizeof(*data), GFP_NOFS);
5794 nfs_put_client(clp);
5798 data->timestamp = jiffies;
5799 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5800 &nfs4_renew_ops, data);
5803 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5805 struct rpc_message msg = {
5806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5810 unsigned long now = jiffies;
5813 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5816 do_renew_lease(clp, now);
5820 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5821 enum nfs4_acl_type type)
5825 return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5827 return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5829 return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5833 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5834 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5837 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5839 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5840 struct page **pages)
5842 struct page *newpage, **spages;
5848 len = min_t(size_t, PAGE_SIZE, buflen);
5849 newpage = alloc_page(GFP_KERNEL);
5851 if (newpage == NULL)
5853 memcpy(page_address(newpage), buf, len);
5858 } while (buflen != 0);
5864 __free_page(spages[rc-1]);
5868 struct nfs4_cached_acl {
5869 enum nfs4_acl_type type;
5875 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5877 struct nfs_inode *nfsi = NFS_I(inode);
5879 spin_lock(&inode->i_lock);
5880 kfree(nfsi->nfs4_acl);
5881 nfsi->nfs4_acl = acl;
5882 spin_unlock(&inode->i_lock);
5885 static void nfs4_zap_acl_attr(struct inode *inode)
5887 nfs4_set_cached_acl(inode, NULL);
5890 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5891 size_t buflen, enum nfs4_acl_type type)
5893 struct nfs_inode *nfsi = NFS_I(inode);
5894 struct nfs4_cached_acl *acl;
5897 spin_lock(&inode->i_lock);
5898 acl = nfsi->nfs4_acl;
5901 if (acl->type != type)
5903 if (buf == NULL) /* user is just asking for length */
5905 if (acl->cached == 0)
5907 ret = -ERANGE; /* see getxattr(2) man page */
5908 if (acl->len > buflen)
5910 memcpy(buf, acl->data, acl->len);
5914 spin_unlock(&inode->i_lock);
5918 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
5919 size_t pgbase, size_t acl_len,
5920 enum nfs4_acl_type type)
5922 struct nfs4_cached_acl *acl;
5923 size_t buflen = sizeof(*acl) + acl_len;
5925 if (buflen <= PAGE_SIZE) {
5926 acl = kmalloc(buflen, GFP_KERNEL);
5930 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5932 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5940 nfs4_set_cached_acl(inode, acl);
5944 * The getxattr API returns the required buffer length when called with a
5945 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5946 * the required buf. On a NULL buf, we send a page of data to the server
5947 * guessing that the ACL request can be serviced by a page. If so, we cache
5948 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5949 * the cache. If not so, we throw away the page, and cache the required
5950 * length. The next getxattr call will then produce another round trip to
5951 * the server, this time with the input buf of the required size.
5953 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
5954 size_t buflen, enum nfs4_acl_type type)
5956 struct page **pages;
5957 struct nfs_getaclargs args = {
5958 .fh = NFS_FH(inode),
5962 struct nfs_getaclres res = {
5966 struct rpc_message msg = {
5967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5971 unsigned int npages;
5972 int ret = -ENOMEM, i;
5973 struct nfs_server *server = NFS_SERVER(inode);
5976 buflen = server->rsize;
5978 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5979 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5983 args.acl_pages = pages;
5985 for (i = 0; i < npages; i++) {
5986 pages[i] = alloc_page(GFP_KERNEL);
5991 /* for decoding across pages */
5992 res.acl_scratch = alloc_page(GFP_KERNEL);
5993 if (!res.acl_scratch)
5996 args.acl_len = npages * PAGE_SIZE;
5998 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5999 __func__, buf, buflen, npages, args.acl_len);
6000 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
6001 &msg, &args.seq_args, &res.seq_res, 0);
6005 /* Handle the case where the passed-in buffer is too short */
6006 if (res.acl_flags & NFS4_ACL_TRUNC) {
6007 /* Did the user only issue a request for the acl length? */
6013 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
6016 if (res.acl_len > buflen) {
6020 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
6026 __free_page(pages[i]);
6027 if (res.acl_scratch)
6028 __free_page(res.acl_scratch);
6033 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
6034 size_t buflen, enum nfs4_acl_type type)
6036 struct nfs4_exception exception = {
6037 .interruptible = true,
6041 ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6042 trace_nfs4_get_acl(inode, ret);
6045 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6046 } while (exception.retry);
6050 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6051 enum nfs4_acl_type type)
6053 struct nfs_server *server = NFS_SERVER(inode);
6056 if (!nfs4_server_supports_acls(server, type))
6058 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6061 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6062 nfs_zap_acl_cache(inode);
6063 ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6065 /* -ENOENT is returned if there is no ACL or if there is an ACL
6066 * but no cached acl data, just the acl length */
6068 return nfs4_get_acl_uncached(inode, buf, buflen, type);
6071 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6072 size_t buflen, enum nfs4_acl_type type)
6074 struct nfs_server *server = NFS_SERVER(inode);
6075 struct page *pages[NFS4ACL_MAXPAGES];
6076 struct nfs_setaclargs arg = {
6077 .fh = NFS_FH(inode),
6082 struct nfs_setaclres res;
6083 struct rpc_message msg = {
6084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6088 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6091 /* You can't remove system.nfs4_acl: */
6094 if (!nfs4_server_supports_acls(server, type))
6096 if (npages > ARRAY_SIZE(pages))
6098 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6101 nfs4_inode_make_writeable(inode);
6102 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6105 * Free each page after tx, so the only ref left is
6106 * held by the network stack
6109 put_page(pages[i-1]);
6112 * Acl update can result in inode attribute update.
6113 * so mark the attribute cache invalid.
6115 spin_lock(&inode->i_lock);
6116 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6117 NFS_INO_INVALID_CTIME |
6118 NFS_INO_REVAL_FORCED);
6119 spin_unlock(&inode->i_lock);
6120 nfs_access_zap_cache(inode);
6121 nfs_zap_acl_cache(inode);
6125 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6126 size_t buflen, enum nfs4_acl_type type)
6128 struct nfs4_exception exception = { };
6131 err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6132 trace_nfs4_set_acl(inode, err);
6133 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6135 * no need to retry since the kernel
6136 * isn't involved in encoding the ACEs.
6141 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6143 } while (exception.retry);
6147 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6148 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6151 struct nfs_server *server = NFS_SERVER(inode);
6152 struct nfs4_label label = {0, 0, buflen, buf};
6154 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6155 struct nfs_fattr fattr = {
6158 struct nfs4_getattr_arg arg = {
6159 .fh = NFS_FH(inode),
6162 struct nfs4_getattr_res res = {
6166 struct rpc_message msg = {
6167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6173 nfs_fattr_init(&fattr);
6175 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6178 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6183 static int nfs4_get_security_label(struct inode *inode, void *buf,
6186 struct nfs4_exception exception = {
6187 .interruptible = true,
6191 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6195 err = _nfs4_get_security_label(inode, buf, buflen);
6196 trace_nfs4_get_security_label(inode, err);
6197 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6199 } while (exception.retry);
6203 static int _nfs4_do_set_security_label(struct inode *inode,
6204 struct nfs4_label *ilabel,
6205 struct nfs_fattr *fattr)
6208 struct iattr sattr = {0};
6209 struct nfs_server *server = NFS_SERVER(inode);
6210 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6211 struct nfs_setattrargs arg = {
6212 .fh = NFS_FH(inode),
6218 struct nfs_setattrres res = {
6222 struct rpc_message msg = {
6223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6229 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6231 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6233 dprintk("%s failed: %d\n", __func__, status);
6238 static int nfs4_do_set_security_label(struct inode *inode,
6239 struct nfs4_label *ilabel,
6240 struct nfs_fattr *fattr)
6242 struct nfs4_exception exception = { };
6246 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6247 trace_nfs4_set_security_label(inode, err);
6248 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6250 } while (exception.retry);
6255 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6257 struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6258 struct nfs_fattr *fattr;
6261 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6264 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6268 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6270 nfs_setsecurity(inode, fattr);
6274 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6277 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6278 nfs4_verifier *bootverf)
6282 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6283 /* An impossible timestamp guarantees this value
6284 * will never match a generated boot time. */
6285 verf[0] = cpu_to_be32(U32_MAX);
6286 verf[1] = cpu_to_be32(U32_MAX);
6288 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6289 u64 ns = ktime_to_ns(nn->boot_time);
6291 verf[0] = cpu_to_be32(ns >> 32);
6292 verf[1] = cpu_to_be32(ns);
6294 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6298 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6300 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6301 struct nfs_netns_client *nn_clp = nn->nfs_client;
6308 id = rcu_dereference(nn_clp->identifier);
6310 strscpy(buf, id, buflen);
6314 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6315 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6321 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6323 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6328 if (clp->cl_owner_id != NULL)
6333 strlen(clp->cl_rpcclient->cl_nodename) +
6335 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6339 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6343 if (len > NFS4_OPAQUE_LIMIT + 1)
6347 * Since this string is allocated at mount time, and held until the
6348 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6349 * about a memory-reclaim deadlock.
6351 str = kmalloc(len, GFP_KERNEL);
6357 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6358 clp->cl_rpcclient->cl_nodename, buf,
6359 rpc_peeraddr2str(clp->cl_rpcclient,
6362 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6363 clp->cl_rpcclient->cl_nodename,
6364 rpc_peeraddr2str(clp->cl_rpcclient,
6368 clp->cl_owner_id = str;
6373 nfs4_init_uniform_client_string(struct nfs_client *clp)
6375 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6380 if (clp->cl_owner_id != NULL)
6383 len = 10 + 10 + 1 + 10 + 1 +
6384 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6386 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6390 if (len > NFS4_OPAQUE_LIMIT + 1)
6394 * Since this string is allocated at mount time, and held until the
6395 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6396 * about a memory-reclaim deadlock.
6398 str = kmalloc(len, GFP_KERNEL);
6403 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6404 clp->rpc_ops->version, clp->cl_minorversion,
6405 buf, clp->cl_rpcclient->cl_nodename);
6407 scnprintf(str, len, "Linux NFSv%u.%u %s",
6408 clp->rpc_ops->version, clp->cl_minorversion,
6409 clp->cl_rpcclient->cl_nodename);
6410 clp->cl_owner_id = str;
6415 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6416 * services. Advertise one based on the address family of the
6420 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6422 if (strchr(clp->cl_ipaddr, ':') != NULL)
6423 return scnprintf(buf, len, "tcp6");
6425 return scnprintf(buf, len, "tcp");
6428 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6430 struct nfs4_setclientid *sc = calldata;
6432 if (task->tk_status == 0)
6433 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6436 static const struct rpc_call_ops nfs4_setclientid_ops = {
6437 .rpc_call_done = nfs4_setclientid_done,
6441 * nfs4_proc_setclientid - Negotiate client ID
6442 * @clp: state data structure
6443 * @program: RPC program for NFSv4 callback service
6444 * @port: IP port number for NFS4 callback service
6445 * @cred: credential to use for this call
6446 * @res: where to place the result
6448 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6450 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6451 unsigned short port, const struct cred *cred,
6452 struct nfs4_setclientid_res *res)
6454 nfs4_verifier sc_verifier;
6455 struct nfs4_setclientid setclientid = {
6456 .sc_verifier = &sc_verifier,
6460 struct rpc_message msg = {
6461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6462 .rpc_argp = &setclientid,
6466 struct rpc_task_setup task_setup_data = {
6467 .rpc_client = clp->cl_rpcclient,
6468 .rpc_message = &msg,
6469 .callback_ops = &nfs4_setclientid_ops,
6470 .callback_data = &setclientid,
6471 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6473 unsigned long now = jiffies;
6476 /* nfs_client_id4 */
6477 nfs4_init_boot_verifier(clp, &sc_verifier);
6479 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6480 status = nfs4_init_uniform_client_string(clp);
6482 status = nfs4_init_nonuniform_client_string(clp);
6488 setclientid.sc_netid_len =
6489 nfs4_init_callback_netid(clp,
6490 setclientid.sc_netid,
6491 sizeof(setclientid.sc_netid));
6492 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6493 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6494 clp->cl_ipaddr, port >> 8, port & 255);
6496 dprintk("NFS call setclientid auth=%s, '%s'\n",
6497 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6500 status = nfs4_call_sync_custom(&task_setup_data);
6501 if (setclientid.sc_cred) {
6502 kfree(clp->cl_acceptor);
6503 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6504 put_rpccred(setclientid.sc_cred);
6508 do_renew_lease(clp, now);
6510 trace_nfs4_setclientid(clp, status);
6511 dprintk("NFS reply setclientid: %d\n", status);
6516 * nfs4_proc_setclientid_confirm - Confirm client ID
6517 * @clp: state data structure
6518 * @arg: result of a previous SETCLIENTID
6519 * @cred: credential to use for this call
6521 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6523 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6524 struct nfs4_setclientid_res *arg,
6525 const struct cred *cred)
6527 struct rpc_message msg = {
6528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6534 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6535 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6537 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6538 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6539 trace_nfs4_setclientid_confirm(clp, status);
6540 dprintk("NFS reply setclientid_confirm: %d\n", status);
6544 struct nfs4_delegreturndata {
6545 struct nfs4_delegreturnargs args;
6546 struct nfs4_delegreturnres res;
6548 nfs4_stateid stateid;
6549 unsigned long timestamp;
6551 struct nfs4_layoutreturn_args arg;
6552 struct nfs4_layoutreturn_res res;
6553 struct nfs4_xdr_opaque_data ld_private;
6557 struct nfs_fattr fattr;
6559 struct inode *inode;
6562 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6564 struct nfs4_delegreturndata *data = calldata;
6565 struct nfs4_exception exception = {
6566 .inode = data->inode,
6567 .stateid = &data->stateid,
6568 .task_is_privileged = data->args.seq_args.sa_privileged,
6571 if (!nfs4_sequence_done(task, &data->res.seq_res))
6574 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6576 /* Handle Layoutreturn errors */
6577 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6578 &data->res.lr_ret) == -EAGAIN)
6581 switch (task->tk_status) {
6583 renew_lease(data->res.server, data->timestamp);
6585 case -NFS4ERR_ADMIN_REVOKED:
6586 case -NFS4ERR_DELEG_REVOKED:
6587 case -NFS4ERR_EXPIRED:
6588 nfs4_free_revoked_stateid(data->res.server,
6590 task->tk_msg.rpc_cred);
6592 case -NFS4ERR_BAD_STATEID:
6593 case -NFS4ERR_STALE_STATEID:
6595 task->tk_status = 0;
6597 case -NFS4ERR_OLD_STATEID:
6598 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6599 nfs4_stateid_seqid_inc(&data->stateid);
6600 if (data->args.bitmask) {
6601 data->args.bitmask = NULL;
6602 data->res.fattr = NULL;
6605 case -NFS4ERR_ACCESS:
6606 if (data->args.bitmask) {
6607 data->args.bitmask = NULL;
6608 data->res.fattr = NULL;
6613 task->tk_status = nfs4_async_handle_exception(task,
6614 data->res.server, task->tk_status,
6616 if (exception.retry)
6619 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6620 data->rpc_status = task->tk_status;
6623 task->tk_status = 0;
6624 rpc_restart_call_prepare(task);
6627 static void nfs4_delegreturn_release(void *calldata)
6629 struct nfs4_delegreturndata *data = calldata;
6630 struct inode *inode = data->inode;
6633 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6636 nfs4_fattr_set_prechange(&data->fattr,
6637 inode_peek_iversion_raw(inode));
6638 nfs_refresh_inode(inode, &data->fattr);
6639 nfs_iput_and_deactive(inode);
6644 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6646 struct nfs4_delegreturndata *d_data;
6647 struct pnfs_layout_hdr *lo;
6651 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6652 nfs4_sequence_done(task, &d_data->res.seq_res);
6656 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6657 if (lo && !pnfs_layout_is_valid(lo)) {
6658 d_data->args.lr_args = NULL;
6659 d_data->res.lr_res = NULL;
6662 nfs4_setup_sequence(d_data->res.server->nfs_client,
6663 &d_data->args.seq_args,
6664 &d_data->res.seq_res,
6668 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6669 .rpc_call_prepare = nfs4_delegreturn_prepare,
6670 .rpc_call_done = nfs4_delegreturn_done,
6671 .rpc_release = nfs4_delegreturn_release,
6674 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6676 struct nfs4_delegreturndata *data;
6677 struct nfs_server *server = NFS_SERVER(inode);
6678 struct rpc_task *task;
6679 struct rpc_message msg = {
6680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6683 struct rpc_task_setup task_setup_data = {
6684 .rpc_client = server->client,
6685 .rpc_message = &msg,
6686 .callback_ops = &nfs4_delegreturn_ops,
6687 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6691 if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6692 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6694 data = kzalloc(sizeof(*data), GFP_KERNEL);
6698 nfs4_state_protect(server->nfs_client,
6699 NFS_SP4_MACH_CRED_CLEANUP,
6700 &task_setup_data.rpc_client, &msg);
6702 data->args.fhandle = &data->fh;
6703 data->args.stateid = &data->stateid;
6704 nfs4_bitmask_set(data->args.bitmask_store,
6705 server->cache_consistency_bitmask, inode, 0);
6706 data->args.bitmask = data->args.bitmask_store;
6707 nfs_copy_fh(&data->fh, NFS_FH(inode));
6708 nfs4_stateid_copy(&data->stateid, stateid);
6709 data->res.fattr = &data->fattr;
6710 data->res.server = server;
6711 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6712 data->lr.arg.ld_private = &data->lr.ld_private;
6713 nfs_fattr_init(data->res.fattr);
6714 data->timestamp = jiffies;
6715 data->rpc_status = 0;
6716 data->inode = nfs_igrab_and_active(inode);
6717 if (data->inode || issync) {
6718 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6721 data->args.lr_args = &data->lr.arg;
6722 data->res.lr_res = &data->lr.res;
6727 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6730 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6732 task_setup_data.callback_data = data;
6733 msg.rpc_argp = &data->args;
6734 msg.rpc_resp = &data->res;
6735 task = rpc_run_task(&task_setup_data);
6737 return PTR_ERR(task);
6740 status = rpc_wait_for_completion_task(task);
6743 status = data->rpc_status;
6749 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6751 struct nfs_server *server = NFS_SERVER(inode);
6752 struct nfs4_exception exception = { };
6755 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6756 trace_nfs4_delegreturn(inode, stateid, err);
6758 case -NFS4ERR_STALE_STATEID:
6759 case -NFS4ERR_EXPIRED:
6763 err = nfs4_handle_exception(server, err, &exception);
6764 } while (exception.retry);
6768 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6770 struct inode *inode = state->inode;
6771 struct nfs_server *server = NFS_SERVER(inode);
6772 struct nfs_client *clp = server->nfs_client;
6773 struct nfs_lockt_args arg = {
6774 .fh = NFS_FH(inode),
6777 struct nfs_lockt_res res = {
6780 struct rpc_message msg = {
6781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6784 .rpc_cred = state->owner->so_cred,
6786 struct nfs4_lock_state *lsp;
6789 arg.lock_owner.clientid = clp->cl_clientid;
6790 status = nfs4_set_lock_state(state, request);
6793 lsp = request->fl_u.nfs4_fl.owner;
6794 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6795 arg.lock_owner.s_dev = server->s_dev;
6796 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6799 request->fl_type = F_UNLCK;
6801 case -NFS4ERR_DENIED:
6804 request->fl_ops->fl_release_private(request);
6805 request->fl_ops = NULL;
6810 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6812 struct nfs4_exception exception = {
6813 .interruptible = true,
6818 err = _nfs4_proc_getlk(state, cmd, request);
6819 trace_nfs4_get_lock(request, state, cmd, err);
6820 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6822 } while (exception.retry);
6827 * Update the seqid of a lock stateid after receiving
6828 * NFS4ERR_OLD_STATEID
6830 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6831 struct nfs4_lock_state *lsp)
6833 struct nfs4_state *state = lsp->ls_state;
6836 spin_lock(&state->state_lock);
6837 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6839 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6840 nfs4_stateid_seqid_inc(dst);
6842 dst->seqid = lsp->ls_stateid.seqid;
6845 spin_unlock(&state->state_lock);
6849 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6850 struct nfs4_lock_state *lsp)
6852 struct nfs4_state *state = lsp->ls_state;
6855 spin_lock(&state->state_lock);
6856 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6857 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6858 spin_unlock(&state->state_lock);
6862 struct nfs4_unlockdata {
6863 struct nfs_locku_args arg;
6864 struct nfs_locku_res res;
6865 struct nfs4_lock_state *lsp;
6866 struct nfs_open_context *ctx;
6867 struct nfs_lock_context *l_ctx;
6868 struct file_lock fl;
6869 struct nfs_server *server;
6870 unsigned long timestamp;
6873 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6874 struct nfs_open_context *ctx,
6875 struct nfs4_lock_state *lsp,
6876 struct nfs_seqid *seqid)
6878 struct nfs4_unlockdata *p;
6879 struct nfs4_state *state = lsp->ls_state;
6880 struct inode *inode = state->inode;
6882 p = kzalloc(sizeof(*p), GFP_KERNEL);
6885 p->arg.fh = NFS_FH(inode);
6887 p->arg.seqid = seqid;
6888 p->res.seqid = seqid;
6890 /* Ensure we don't close file until we're done freeing locks! */
6891 p->ctx = get_nfs_open_context(ctx);
6892 p->l_ctx = nfs_get_lock_context(ctx);
6893 locks_init_lock(&p->fl);
6894 locks_copy_lock(&p->fl, fl);
6895 p->server = NFS_SERVER(inode);
6896 spin_lock(&state->state_lock);
6897 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6898 spin_unlock(&state->state_lock);
6902 static void nfs4_locku_release_calldata(void *data)
6904 struct nfs4_unlockdata *calldata = data;
6905 nfs_free_seqid(calldata->arg.seqid);
6906 nfs4_put_lock_state(calldata->lsp);
6907 nfs_put_lock_context(calldata->l_ctx);
6908 put_nfs_open_context(calldata->ctx);
6912 static void nfs4_locku_done(struct rpc_task *task, void *data)
6914 struct nfs4_unlockdata *calldata = data;
6915 struct nfs4_exception exception = {
6916 .inode = calldata->lsp->ls_state->inode,
6917 .stateid = &calldata->arg.stateid,
6920 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6922 switch (task->tk_status) {
6924 renew_lease(calldata->server, calldata->timestamp);
6925 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6926 if (nfs4_update_lock_stateid(calldata->lsp,
6927 &calldata->res.stateid))
6930 case -NFS4ERR_ADMIN_REVOKED:
6931 case -NFS4ERR_EXPIRED:
6932 nfs4_free_revoked_stateid(calldata->server,
6933 &calldata->arg.stateid,
6934 task->tk_msg.rpc_cred);
6936 case -NFS4ERR_BAD_STATEID:
6937 case -NFS4ERR_STALE_STATEID:
6938 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6940 rpc_restart_call_prepare(task);
6942 case -NFS4ERR_OLD_STATEID:
6943 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6945 rpc_restart_call_prepare(task);
6948 task->tk_status = nfs4_async_handle_exception(task,
6949 calldata->server, task->tk_status,
6951 if (exception.retry)
6952 rpc_restart_call_prepare(task);
6954 nfs_release_seqid(calldata->arg.seqid);
6957 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6959 struct nfs4_unlockdata *calldata = data;
6961 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6962 nfs_async_iocounter_wait(task, calldata->l_ctx))
6965 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6967 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6968 /* Note: exit _without_ running nfs4_locku_done */
6971 calldata->timestamp = jiffies;
6972 if (nfs4_setup_sequence(calldata->server->nfs_client,
6973 &calldata->arg.seq_args,
6974 &calldata->res.seq_res,
6976 nfs_release_seqid(calldata->arg.seqid);
6979 task->tk_action = NULL;
6981 nfs4_sequence_done(task, &calldata->res.seq_res);
6984 static const struct rpc_call_ops nfs4_locku_ops = {
6985 .rpc_call_prepare = nfs4_locku_prepare,
6986 .rpc_call_done = nfs4_locku_done,
6987 .rpc_release = nfs4_locku_release_calldata,
6990 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6991 struct nfs_open_context *ctx,
6992 struct nfs4_lock_state *lsp,
6993 struct nfs_seqid *seqid)
6995 struct nfs4_unlockdata *data;
6996 struct rpc_message msg = {
6997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6998 .rpc_cred = ctx->cred,
7000 struct rpc_task_setup task_setup_data = {
7001 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
7002 .rpc_message = &msg,
7003 .callback_ops = &nfs4_locku_ops,
7004 .workqueue = nfsiod_workqueue,
7005 .flags = RPC_TASK_ASYNC,
7008 if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
7009 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7011 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
7012 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
7014 /* Ensure this is an unlock - when canceling a lock, the
7015 * canceled lock is passed in, and it won't be an unlock.
7017 fl->fl_type = F_UNLCK;
7018 if (fl->fl_flags & FL_CLOSE)
7019 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
7021 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
7023 nfs_free_seqid(seqid);
7024 return ERR_PTR(-ENOMEM);
7027 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
7028 msg.rpc_argp = &data->arg;
7029 msg.rpc_resp = &data->res;
7030 task_setup_data.callback_data = data;
7031 return rpc_run_task(&task_setup_data);
7034 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
7036 struct inode *inode = state->inode;
7037 struct nfs4_state_owner *sp = state->owner;
7038 struct nfs_inode *nfsi = NFS_I(inode);
7039 struct nfs_seqid *seqid;
7040 struct nfs4_lock_state *lsp;
7041 struct rpc_task *task;
7042 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7044 unsigned char fl_flags = request->fl_flags;
7046 status = nfs4_set_lock_state(state, request);
7047 /* Unlock _before_ we do the RPC call */
7048 request->fl_flags |= FL_EXISTS;
7049 /* Exclude nfs_delegation_claim_locks() */
7050 mutex_lock(&sp->so_delegreturn_mutex);
7051 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7052 down_read(&nfsi->rwsem);
7053 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7054 up_read(&nfsi->rwsem);
7055 mutex_unlock(&sp->so_delegreturn_mutex);
7058 lsp = request->fl_u.nfs4_fl.owner;
7059 set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
7060 up_read(&nfsi->rwsem);
7061 mutex_unlock(&sp->so_delegreturn_mutex);
7064 /* Is this a delegated lock? */
7065 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7067 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7068 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7072 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
7073 status = PTR_ERR(task);
7076 status = rpc_wait_for_completion_task(task);
7079 request->fl_flags = fl_flags;
7080 trace_nfs4_unlock(request, state, F_SETLK, status);
7084 struct nfs4_lockdata {
7085 struct nfs_lock_args arg;
7086 struct nfs_lock_res res;
7087 struct nfs4_lock_state *lsp;
7088 struct nfs_open_context *ctx;
7089 struct file_lock fl;
7090 unsigned long timestamp;
7093 struct nfs_server *server;
7096 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7097 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7100 struct nfs4_lockdata *p;
7101 struct inode *inode = lsp->ls_state->inode;
7102 struct nfs_server *server = NFS_SERVER(inode);
7103 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7105 p = kzalloc(sizeof(*p), gfp_mask);
7109 p->arg.fh = NFS_FH(inode);
7111 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7112 if (IS_ERR(p->arg.open_seqid))
7114 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7115 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7116 if (IS_ERR(p->arg.lock_seqid))
7117 goto out_free_seqid;
7118 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7119 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7120 p->arg.lock_owner.s_dev = server->s_dev;
7121 p->res.lock_seqid = p->arg.lock_seqid;
7124 p->ctx = get_nfs_open_context(ctx);
7125 locks_init_lock(&p->fl);
7126 locks_copy_lock(&p->fl, fl);
7129 nfs_free_seqid(p->arg.open_seqid);
7135 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7137 struct nfs4_lockdata *data = calldata;
7138 struct nfs4_state *state = data->lsp->ls_state;
7140 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7142 /* Do we need to do an open_to_lock_owner? */
7143 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7144 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7145 goto out_release_lock_seqid;
7147 nfs4_stateid_copy(&data->arg.open_stateid,
7148 &state->open_stateid);
7149 data->arg.new_lock_owner = 1;
7150 data->res.open_seqid = data->arg.open_seqid;
7152 data->arg.new_lock_owner = 0;
7153 nfs4_stateid_copy(&data->arg.lock_stateid,
7154 &data->lsp->ls_stateid);
7156 if (!nfs4_valid_open_stateid(state)) {
7157 data->rpc_status = -EBADF;
7158 task->tk_action = NULL;
7159 goto out_release_open_seqid;
7161 data->timestamp = jiffies;
7162 if (nfs4_setup_sequence(data->server->nfs_client,
7163 &data->arg.seq_args,
7167 out_release_open_seqid:
7168 nfs_release_seqid(data->arg.open_seqid);
7169 out_release_lock_seqid:
7170 nfs_release_seqid(data->arg.lock_seqid);
7172 nfs4_sequence_done(task, &data->res.seq_res);
7173 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7176 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7178 struct nfs4_lockdata *data = calldata;
7179 struct nfs4_lock_state *lsp = data->lsp;
7181 if (!nfs4_sequence_done(task, &data->res.seq_res))
7184 data->rpc_status = task->tk_status;
7185 switch (task->tk_status) {
7187 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7189 if (data->arg.new_lock && !data->cancelled) {
7190 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7191 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7194 if (data->arg.new_lock_owner != 0) {
7195 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7196 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7197 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7198 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7201 case -NFS4ERR_OLD_STATEID:
7202 if (data->arg.new_lock_owner != 0 &&
7203 nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7206 if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7209 case -NFS4ERR_BAD_STATEID:
7210 case -NFS4ERR_STALE_STATEID:
7211 case -NFS4ERR_EXPIRED:
7212 if (data->arg.new_lock_owner != 0) {
7213 if (!nfs4_stateid_match(&data->arg.open_stateid,
7214 &lsp->ls_state->open_stateid))
7216 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7221 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7224 if (!data->cancelled)
7225 rpc_restart_call_prepare(task);
7229 static void nfs4_lock_release(void *calldata)
7231 struct nfs4_lockdata *data = calldata;
7233 nfs_free_seqid(data->arg.open_seqid);
7234 if (data->cancelled && data->rpc_status == 0) {
7235 struct rpc_task *task;
7236 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7237 data->arg.lock_seqid);
7239 rpc_put_task_async(task);
7240 dprintk("%s: cancelling lock!\n", __func__);
7242 nfs_free_seqid(data->arg.lock_seqid);
7243 nfs4_put_lock_state(data->lsp);
7244 put_nfs_open_context(data->ctx);
7248 static const struct rpc_call_ops nfs4_lock_ops = {
7249 .rpc_call_prepare = nfs4_lock_prepare,
7250 .rpc_call_done = nfs4_lock_done,
7251 .rpc_release = nfs4_lock_release,
7254 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7257 case -NFS4ERR_ADMIN_REVOKED:
7258 case -NFS4ERR_EXPIRED:
7259 case -NFS4ERR_BAD_STATEID:
7260 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7261 if (new_lock_owner != 0 ||
7262 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7263 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7265 case -NFS4ERR_STALE_STATEID:
7266 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7267 nfs4_schedule_lease_recovery(server->nfs_client);
7271 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7273 struct nfs4_lockdata *data;
7274 struct rpc_task *task;
7275 struct rpc_message msg = {
7276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7277 .rpc_cred = state->owner->so_cred,
7279 struct rpc_task_setup task_setup_data = {
7280 .rpc_client = NFS_CLIENT(state->inode),
7281 .rpc_message = &msg,
7282 .callback_ops = &nfs4_lock_ops,
7283 .workqueue = nfsiod_workqueue,
7284 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7288 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7289 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7291 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7292 fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7296 data->arg.block = 1;
7297 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7298 recovery_type > NFS_LOCK_NEW);
7299 msg.rpc_argp = &data->arg;
7300 msg.rpc_resp = &data->res;
7301 task_setup_data.callback_data = data;
7302 if (recovery_type > NFS_LOCK_NEW) {
7303 if (recovery_type == NFS_LOCK_RECLAIM)
7304 data->arg.reclaim = NFS_LOCK_RECLAIM;
7306 data->arg.new_lock = 1;
7307 task = rpc_run_task(&task_setup_data);
7309 return PTR_ERR(task);
7310 ret = rpc_wait_for_completion_task(task);
7312 ret = data->rpc_status;
7314 nfs4_handle_setlk_error(data->server, data->lsp,
7315 data->arg.new_lock_owner, ret);
7317 data->cancelled = true;
7318 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7320 dprintk("%s: ret = %d\n", __func__, ret);
7324 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7326 struct nfs_server *server = NFS_SERVER(state->inode);
7327 struct nfs4_exception exception = {
7328 .inode = state->inode,
7333 /* Cache the lock if possible... */
7334 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7336 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7337 if (err != -NFS4ERR_DELAY)
7339 nfs4_handle_exception(server, err, &exception);
7340 } while (exception.retry);
7344 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7346 struct nfs_server *server = NFS_SERVER(state->inode);
7347 struct nfs4_exception exception = {
7348 .inode = state->inode,
7352 err = nfs4_set_lock_state(state, request);
7355 if (!recover_lost_locks) {
7356 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7360 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7362 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7366 case -NFS4ERR_GRACE:
7367 case -NFS4ERR_DELAY:
7368 nfs4_handle_exception(server, err, &exception);
7371 } while (exception.retry);
7376 #if defined(CONFIG_NFS_V4_1)
7377 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7379 struct nfs4_lock_state *lsp;
7382 status = nfs4_set_lock_state(state, request);
7385 lsp = request->fl_u.nfs4_fl.owner;
7386 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7387 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7389 return nfs4_lock_expired(state, request);
7393 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7395 struct nfs_inode *nfsi = NFS_I(state->inode);
7396 struct nfs4_state_owner *sp = state->owner;
7397 unsigned char fl_flags = request->fl_flags;
7400 request->fl_flags |= FL_ACCESS;
7401 status = locks_lock_inode_wait(state->inode, request);
7404 mutex_lock(&sp->so_delegreturn_mutex);
7405 down_read(&nfsi->rwsem);
7406 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7407 /* Yes: cache locks! */
7408 /* ...but avoid races with delegation recall... */
7409 request->fl_flags = fl_flags & ~FL_SLEEP;
7410 status = locks_lock_inode_wait(state->inode, request);
7411 up_read(&nfsi->rwsem);
7412 mutex_unlock(&sp->so_delegreturn_mutex);
7415 up_read(&nfsi->rwsem);
7416 mutex_unlock(&sp->so_delegreturn_mutex);
7417 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7419 request->fl_flags = fl_flags;
7423 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7425 struct nfs4_exception exception = {
7427 .inode = state->inode,
7428 .interruptible = true,
7433 err = _nfs4_proc_setlk(state, cmd, request);
7434 if (err == -NFS4ERR_DENIED)
7436 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7438 } while (exception.retry);
7442 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7443 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7446 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7447 struct file_lock *request)
7449 int status = -ERESTARTSYS;
7450 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7452 while(!signalled()) {
7453 status = nfs4_proc_setlk(state, cmd, request);
7454 if ((status != -EAGAIN) || IS_SETLK(cmd))
7456 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
7457 schedule_timeout(timeout);
7459 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7460 status = -ERESTARTSYS;
7465 #ifdef CONFIG_NFS_V4_1
7466 struct nfs4_lock_waiter {
7467 struct inode *inode;
7468 struct nfs_lowner owner;
7469 wait_queue_entry_t wait;
7473 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7475 struct nfs4_lock_waiter *waiter =
7476 container_of(wait, struct nfs4_lock_waiter, wait);
7478 /* NULL key means to wake up everyone */
7480 struct cb_notify_lock_args *cbnl = key;
7481 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7482 *wowner = &waiter->owner;
7484 /* Only wake if the callback was for the same owner. */
7485 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7488 /* Make sure it's for the right inode */
7489 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7493 return woken_wake_function(wait, mode, flags, key);
7497 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7499 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7500 struct nfs_server *server = NFS_SERVER(state->inode);
7501 struct nfs_client *clp = server->nfs_client;
7502 wait_queue_head_t *q = &clp->cl_lock_waitq;
7503 struct nfs4_lock_waiter waiter = {
7504 .inode = state->inode,
7505 .owner = { .clientid = clp->cl_clientid,
7506 .id = lsp->ls_seqid.owner_id,
7507 .s_dev = server->s_dev },
7511 /* Don't bother with waitqueue if we don't expect a callback */
7512 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7513 return nfs4_retry_setlk_simple(state, cmd, request);
7515 init_wait(&waiter.wait);
7516 waiter.wait.func = nfs4_wake_lock_waiter;
7517 add_wait_queue(q, &waiter.wait);
7520 status = nfs4_proc_setlk(state, cmd, request);
7521 if (status != -EAGAIN || IS_SETLK(cmd))
7524 status = -ERESTARTSYS;
7525 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
7526 NFS4_LOCK_MAXTIMEOUT);
7527 } while (!signalled());
7529 remove_wait_queue(q, &waiter.wait);
7533 #else /* !CONFIG_NFS_V4_1 */
7535 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7537 return nfs4_retry_setlk_simple(state, cmd, request);
7542 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7544 struct nfs_open_context *ctx;
7545 struct nfs4_state *state;
7548 /* verify open state */
7549 ctx = nfs_file_open_context(filp);
7552 if (IS_GETLK(cmd)) {
7554 return nfs4_proc_getlk(state, F_GETLK, request);
7558 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7561 if (request->fl_type == F_UNLCK) {
7563 return nfs4_proc_unlck(state, cmd, request);
7570 if ((request->fl_flags & FL_POSIX) &&
7571 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7575 * Don't rely on the VFS having checked the file open mode,
7576 * since it won't do this for flock() locks.
7578 switch (request->fl_type) {
7580 if (!(filp->f_mode & FMODE_READ))
7584 if (!(filp->f_mode & FMODE_WRITE))
7588 status = nfs4_set_lock_state(state, request);
7592 return nfs4_retry_setlk(state, cmd, request);
7595 static int nfs4_delete_lease(struct file *file, void **priv)
7597 return generic_setlease(file, F_UNLCK, NULL, priv);
7600 static int nfs4_add_lease(struct file *file, int arg, struct file_lock **lease,
7603 struct inode *inode = file_inode(file);
7604 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7607 /* No delegation, no lease */
7608 if (!nfs4_have_delegation(inode, type))
7610 ret = generic_setlease(file, arg, lease, priv);
7611 if (ret || nfs4_have_delegation(inode, type))
7613 /* We raced with a delegation return */
7614 nfs4_delete_lease(file, priv);
7618 int nfs4_proc_setlease(struct file *file, int arg, struct file_lock **lease,
7624 return nfs4_add_lease(file, arg, lease, priv);
7626 return nfs4_delete_lease(file, priv);
7632 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7634 struct nfs_server *server = NFS_SERVER(state->inode);
7637 err = nfs4_set_lock_state(state, fl);
7641 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7642 if (err != -NFS4ERR_DELAY)
7645 } while (err == -NFS4ERR_DELAY);
7646 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7649 struct nfs_release_lockowner_data {
7650 struct nfs4_lock_state *lsp;
7651 struct nfs_server *server;
7652 struct nfs_release_lockowner_args args;
7653 struct nfs_release_lockowner_res res;
7654 unsigned long timestamp;
7657 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7659 struct nfs_release_lockowner_data *data = calldata;
7660 struct nfs_server *server = data->server;
7661 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7662 &data->res.seq_res, task);
7663 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7664 data->timestamp = jiffies;
7667 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7669 struct nfs_release_lockowner_data *data = calldata;
7670 struct nfs_server *server = data->server;
7672 nfs40_sequence_done(task, &data->res.seq_res);
7674 switch (task->tk_status) {
7676 renew_lease(server, data->timestamp);
7678 case -NFS4ERR_STALE_CLIENTID:
7679 case -NFS4ERR_EXPIRED:
7680 nfs4_schedule_lease_recovery(server->nfs_client);
7682 case -NFS4ERR_LEASE_MOVED:
7683 case -NFS4ERR_DELAY:
7684 if (nfs4_async_handle_error(task, server,
7685 NULL, NULL) == -EAGAIN)
7686 rpc_restart_call_prepare(task);
7690 static void nfs4_release_lockowner_release(void *calldata)
7692 struct nfs_release_lockowner_data *data = calldata;
7693 nfs4_free_lock_state(data->server, data->lsp);
7697 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7698 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7699 .rpc_call_done = nfs4_release_lockowner_done,
7700 .rpc_release = nfs4_release_lockowner_release,
7704 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7706 struct nfs_release_lockowner_data *data;
7707 struct rpc_message msg = {
7708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7711 if (server->nfs_client->cl_mvops->minor_version != 0)
7714 data = kmalloc(sizeof(*data), GFP_KERNEL);
7718 data->server = server;
7719 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7720 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7721 data->args.lock_owner.s_dev = server->s_dev;
7723 msg.rpc_argp = &data->args;
7724 msg.rpc_resp = &data->res;
7725 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7726 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7729 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7731 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7732 struct mnt_idmap *idmap,
7733 struct dentry *unused, struct inode *inode,
7734 const char *key, const void *buf,
7735 size_t buflen, int flags)
7737 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7740 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7741 struct dentry *unused, struct inode *inode,
7742 const char *key, void *buf, size_t buflen)
7744 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7747 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7749 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7752 #if defined(CONFIG_NFS_V4_1)
7753 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7755 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7756 struct mnt_idmap *idmap,
7757 struct dentry *unused, struct inode *inode,
7758 const char *key, const void *buf,
7759 size_t buflen, int flags)
7761 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7764 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7765 struct dentry *unused, struct inode *inode,
7766 const char *key, void *buf, size_t buflen)
7768 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7771 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7773 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7776 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7778 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7779 struct mnt_idmap *idmap,
7780 struct dentry *unused, struct inode *inode,
7781 const char *key, const void *buf,
7782 size_t buflen, int flags)
7784 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7787 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7788 struct dentry *unused, struct inode *inode,
7789 const char *key, void *buf, size_t buflen)
7791 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7794 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7796 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7801 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7803 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7804 struct mnt_idmap *idmap,
7805 struct dentry *unused, struct inode *inode,
7806 const char *key, const void *buf,
7807 size_t buflen, int flags)
7809 if (security_ismaclabel(key))
7810 return nfs4_set_security_label(inode, buf, buflen);
7815 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7816 struct dentry *unused, struct inode *inode,
7817 const char *key, void *buf, size_t buflen)
7819 if (security_ismaclabel(key))
7820 return nfs4_get_security_label(inode, buf, buflen);
7825 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7829 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7830 len = security_inode_listsecurity(inode, list, list_len);
7831 if (len >= 0 && list_len && len > list_len)
7837 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7838 .prefix = XATTR_SECURITY_PREFIX,
7839 .get = nfs4_xattr_get_nfs4_label,
7840 .set = nfs4_xattr_set_nfs4_label,
7846 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7853 #ifdef CONFIG_NFS_V4_2
7854 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7855 struct mnt_idmap *idmap,
7856 struct dentry *unused, struct inode *inode,
7857 const char *key, const void *buf,
7858 size_t buflen, int flags)
7863 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7867 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7868 * flags right now. Handling of xattr operations use the normal
7869 * file read/write permissions.
7871 * Just in case the server has other ideas (which RFC 8276 allows),
7872 * do a cached access check for the XA* flags to possibly avoid
7873 * doing an RPC and getting EACCES back.
7875 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7876 if (!(mask & NFS_ACCESS_XAWRITE))
7881 ret = nfs42_proc_removexattr(inode, key);
7883 nfs4_xattr_cache_remove(inode, key);
7885 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7887 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7893 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7894 struct dentry *unused, struct inode *inode,
7895 const char *key, void *buf, size_t buflen)
7900 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7903 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7904 if (!(mask & NFS_ACCESS_XAREAD))
7908 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7912 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7913 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7916 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7922 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7931 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7934 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7935 if (!(mask & NFS_ACCESS_XALIST))
7939 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7943 ret = nfs4_xattr_cache_list(inode, list, list_len);
7944 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7949 buflen = list_len ? list_len : XATTR_LIST_MAX;
7950 buf = list_len ? list : NULL;
7954 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7967 nfs4_xattr_cache_set_list(inode, list, size);
7975 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7979 #endif /* CONFIG_NFS_V4_2 */
7982 * nfs_fhget will use either the mounted_on_fileid or the fileid
7984 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7986 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7987 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7988 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7989 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7992 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7993 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7994 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7998 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7999 const struct qstr *name,
8000 struct nfs4_fs_locations *fs_locations,
8003 struct nfs_server *server = NFS_SERVER(dir);
8005 struct nfs4_fs_locations_arg args = {
8006 .dir_fh = NFS_FH(dir),
8011 struct nfs4_fs_locations_res res = {
8012 .fs_locations = fs_locations,
8014 struct rpc_message msg = {
8015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8021 dprintk("%s: start\n", __func__);
8023 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
8024 bitmask[1] = nfs4_fattr_bitmap[1];
8026 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
8027 * is not supported */
8028 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
8029 bitmask[0] &= ~FATTR4_WORD0_FILEID;
8031 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
8033 nfs_fattr_init(fs_locations->fattr);
8034 fs_locations->server = server;
8035 fs_locations->nlocations = 0;
8036 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
8037 dprintk("%s: returned status = %d\n", __func__, status);
8041 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8042 const struct qstr *name,
8043 struct nfs4_fs_locations *fs_locations,
8046 struct nfs4_exception exception = {
8047 .interruptible = true,
8051 err = _nfs4_proc_fs_locations(client, dir, name,
8052 fs_locations, page);
8053 trace_nfs4_get_fs_locations(dir, name, err);
8054 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8056 } while (exception.retry);
8061 * This operation also signals the server that this client is
8062 * performing migration recovery. The server can stop returning
8063 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
8064 * appended to this compound to identify the client ID which is
8065 * performing recovery.
8067 static int _nfs40_proc_get_locations(struct nfs_server *server,
8068 struct nfs_fh *fhandle,
8069 struct nfs4_fs_locations *locations,
8070 struct page *page, const struct cred *cred)
8072 struct rpc_clnt *clnt = server->client;
8074 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8076 struct nfs4_fs_locations_arg args = {
8077 .clientid = server->nfs_client->cl_clientid,
8081 .migration = 1, /* skip LOOKUP */
8082 .renew = 1, /* append RENEW */
8084 struct nfs4_fs_locations_res res = {
8085 .fs_locations = locations,
8089 struct rpc_message msg = {
8090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8095 unsigned long now = jiffies;
8098 nfs_fattr_init(locations->fattr);
8099 locations->server = server;
8100 locations->nlocations = 0;
8102 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8103 status = nfs4_call_sync_sequence(clnt, server, &msg,
8104 &args.seq_args, &res.seq_res);
8108 renew_lease(server, now);
8112 #ifdef CONFIG_NFS_V4_1
8115 * This operation also signals the server that this client is
8116 * performing migration recovery. The server can stop asserting
8117 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
8118 * performing this operation is identified in the SEQUENCE
8119 * operation in this compound.
8121 * When the client supports GETATTR(fs_locations_info), it can
8122 * be plumbed in here.
8124 static int _nfs41_proc_get_locations(struct nfs_server *server,
8125 struct nfs_fh *fhandle,
8126 struct nfs4_fs_locations *locations,
8127 struct page *page, const struct cred *cred)
8129 struct rpc_clnt *clnt = server->client;
8131 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8133 struct nfs4_fs_locations_arg args = {
8137 .migration = 1, /* skip LOOKUP */
8139 struct nfs4_fs_locations_res res = {
8140 .fs_locations = locations,
8143 struct rpc_message msg = {
8144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8149 struct nfs4_call_sync_data data = {
8150 .seq_server = server,
8151 .seq_args = &args.seq_args,
8152 .seq_res = &res.seq_res,
8154 struct rpc_task_setup task_setup_data = {
8156 .rpc_message = &msg,
8157 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8158 .callback_data = &data,
8159 .flags = RPC_TASK_NO_ROUND_ROBIN,
8163 nfs_fattr_init(locations->fattr);
8164 locations->server = server;
8165 locations->nlocations = 0;
8167 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8168 status = nfs4_call_sync_custom(&task_setup_data);
8169 if (status == NFS4_OK &&
8170 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8171 status = -NFS4ERR_LEASE_MOVED;
8175 #endif /* CONFIG_NFS_V4_1 */
8178 * nfs4_proc_get_locations - discover locations for a migrated FSID
8179 * @server: pointer to nfs_server to process
8180 * @fhandle: pointer to the kernel NFS client file handle
8181 * @locations: result of query
8183 * @cred: credential to use for this operation
8185 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8186 * operation failed, or a negative errno if a local error occurred.
8188 * On success, "locations" is filled in, but if the server has
8189 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8192 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8193 * from this client that require migration recovery.
8195 int nfs4_proc_get_locations(struct nfs_server *server,
8196 struct nfs_fh *fhandle,
8197 struct nfs4_fs_locations *locations,
8198 struct page *page, const struct cred *cred)
8200 struct nfs_client *clp = server->nfs_client;
8201 const struct nfs4_mig_recovery_ops *ops =
8202 clp->cl_mvops->mig_recovery_ops;
8203 struct nfs4_exception exception = {
8204 .interruptible = true,
8208 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8209 (unsigned long long)server->fsid.major,
8210 (unsigned long long)server->fsid.minor,
8212 nfs_display_fhandle(fhandle, __func__);
8215 status = ops->get_locations(server, fhandle, locations, page,
8217 if (status != -NFS4ERR_DELAY)
8219 nfs4_handle_exception(server, status, &exception);
8220 } while (exception.retry);
8225 * This operation also signals the server that this client is
8226 * performing "lease moved" recovery. The server can stop
8227 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8228 * is appended to this compound to identify the client ID which is
8229 * performing recovery.
8231 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8233 struct nfs_server *server = NFS_SERVER(inode);
8234 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8235 struct rpc_clnt *clnt = server->client;
8236 struct nfs4_fsid_present_arg args = {
8237 .fh = NFS_FH(inode),
8238 .clientid = clp->cl_clientid,
8239 .renew = 1, /* append RENEW */
8241 struct nfs4_fsid_present_res res = {
8244 struct rpc_message msg = {
8245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8250 unsigned long now = jiffies;
8253 res.fh = nfs_alloc_fhandle();
8257 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8258 status = nfs4_call_sync_sequence(clnt, server, &msg,
8259 &args.seq_args, &res.seq_res);
8260 nfs_free_fhandle(res.fh);
8264 do_renew_lease(clp, now);
8268 #ifdef CONFIG_NFS_V4_1
8271 * This operation also signals the server that this client is
8272 * performing "lease moved" recovery. The server can stop asserting
8273 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8274 * this operation is identified in the SEQUENCE operation in this
8277 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8279 struct nfs_server *server = NFS_SERVER(inode);
8280 struct rpc_clnt *clnt = server->client;
8281 struct nfs4_fsid_present_arg args = {
8282 .fh = NFS_FH(inode),
8284 struct nfs4_fsid_present_res res = {
8286 struct rpc_message msg = {
8287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8294 res.fh = nfs_alloc_fhandle();
8298 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8299 status = nfs4_call_sync_sequence(clnt, server, &msg,
8300 &args.seq_args, &res.seq_res);
8301 nfs_free_fhandle(res.fh);
8302 if (status == NFS4_OK &&
8303 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8304 status = -NFS4ERR_LEASE_MOVED;
8308 #endif /* CONFIG_NFS_V4_1 */
8311 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8312 * @inode: inode on FSID to check
8313 * @cred: credential to use for this operation
8315 * Server indicates whether the FSID is present, moved, or not
8316 * recognized. This operation is necessary to clear a LEASE_MOVED
8317 * condition for this client ID.
8319 * Returns NFS4_OK if the FSID is present on this server,
8320 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8321 * NFS4ERR code if some error occurred on the server, or a
8322 * negative errno if a local failure occurred.
8324 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8326 struct nfs_server *server = NFS_SERVER(inode);
8327 struct nfs_client *clp = server->nfs_client;
8328 const struct nfs4_mig_recovery_ops *ops =
8329 clp->cl_mvops->mig_recovery_ops;
8330 struct nfs4_exception exception = {
8331 .interruptible = true,
8335 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8336 (unsigned long long)server->fsid.major,
8337 (unsigned long long)server->fsid.minor,
8339 nfs_display_fhandle(NFS_FH(inode), __func__);
8342 status = ops->fsid_present(inode, cred);
8343 if (status != -NFS4ERR_DELAY)
8345 nfs4_handle_exception(server, status, &exception);
8346 } while (exception.retry);
8351 * If 'use_integrity' is true and the state managment nfs_client
8352 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8353 * and the machine credential as per RFC3530bis and RFC5661 Security
8354 * Considerations sections. Otherwise, just use the user cred with the
8355 * filesystem's rpc_client.
8357 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8360 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8361 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8362 struct nfs4_secinfo_arg args = {
8363 .dir_fh = NFS_FH(dir),
8366 struct nfs4_secinfo_res res = {
8369 struct rpc_message msg = {
8370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8374 struct nfs4_call_sync_data data = {
8375 .seq_server = NFS_SERVER(dir),
8376 .seq_args = &args.seq_args,
8377 .seq_res = &res.seq_res,
8379 struct rpc_task_setup task_setup = {
8381 .rpc_message = &msg,
8382 .callback_ops = clp->cl_mvops->call_sync_ops,
8383 .callback_data = &data,
8384 .flags = RPC_TASK_NO_ROUND_ROBIN,
8386 const struct cred *cred = NULL;
8388 if (use_integrity) {
8389 clnt = clp->cl_rpcclient;
8390 task_setup.rpc_client = clnt;
8392 cred = nfs4_get_clid_cred(clp);
8393 msg.rpc_cred = cred;
8396 dprintk("NFS call secinfo %s\n", name->name);
8398 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8399 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8400 status = nfs4_call_sync_custom(&task_setup);
8402 dprintk("NFS reply secinfo: %d\n", status);
8408 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8409 struct nfs4_secinfo_flavors *flavors)
8411 struct nfs4_exception exception = {
8412 .interruptible = true,
8416 err = -NFS4ERR_WRONGSEC;
8418 /* try to use integrity protection with machine cred */
8419 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8420 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8423 * if unable to use integrity protection, or SECINFO with
8424 * integrity protection returns NFS4ERR_WRONGSEC (which is
8425 * disallowed by spec, but exists in deployed servers) use
8426 * the current filesystem's rpc_client and the user cred.
8428 if (err == -NFS4ERR_WRONGSEC)
8429 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8431 trace_nfs4_secinfo(dir, name, err);
8432 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8434 } while (exception.retry);
8438 #ifdef CONFIG_NFS_V4_1
8440 * Check the exchange flags returned by the server for invalid flags, having
8441 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8444 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8446 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8448 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8450 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8451 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8453 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8457 return -NFS4ERR_INVAL;
8461 nfs41_same_server_scope(struct nfs41_server_scope *a,
8462 struct nfs41_server_scope *b)
8464 if (a->server_scope_sz != b->server_scope_sz)
8466 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8470 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8472 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8473 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8474 struct nfs_client *clp = args->client;
8476 switch (task->tk_status) {
8477 case -NFS4ERR_BADSESSION:
8478 case -NFS4ERR_DEADSESSION:
8479 nfs4_schedule_session_recovery(clp->cl_session,
8483 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8484 res->dir != NFS4_CDFS4_BOTH) {
8485 rpc_task_close_connection(task);
8486 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8487 rpc_restart_call(task);
8491 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8492 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8496 * nfs4_proc_bind_one_conn_to_session()
8498 * The 4.1 client currently uses the same TCP connection for the
8499 * fore and backchannel.
8502 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8503 struct rpc_xprt *xprt,
8504 struct nfs_client *clp,
8505 const struct cred *cred)
8508 struct nfs41_bind_conn_to_session_args args = {
8510 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8513 struct nfs41_bind_conn_to_session_res res;
8514 struct rpc_message msg = {
8516 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8521 struct rpc_task_setup task_setup_data = {
8524 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8525 .rpc_message = &msg,
8526 .flags = RPC_TASK_TIMEOUT,
8528 struct rpc_task *task;
8530 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8531 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8532 args.dir = NFS4_CDFC4_FORE;
8534 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8535 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8536 args.dir = NFS4_CDFC4_FORE;
8538 task = rpc_run_task(&task_setup_data);
8539 if (!IS_ERR(task)) {
8540 status = task->tk_status;
8543 status = PTR_ERR(task);
8544 trace_nfs4_bind_conn_to_session(clp, status);
8546 if (memcmp(res.sessionid.data,
8547 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8548 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8551 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8552 dprintk("NFS: %s: Unexpected direction from server\n",
8556 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8557 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8566 struct rpc_bind_conn_calldata {
8567 struct nfs_client *clp;
8568 const struct cred *cred;
8572 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8573 struct rpc_xprt *xprt,
8576 struct rpc_bind_conn_calldata *p = calldata;
8578 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8581 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8583 struct rpc_bind_conn_calldata data = {
8587 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8588 nfs4_proc_bind_conn_to_session_callback, &data);
8592 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8593 * and operations we'd like to see to enable certain features in the allow map
8595 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8596 .how = SP4_MACH_CRED,
8597 .enforce.u.words = {
8598 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8599 1 << (OP_EXCHANGE_ID - 32) |
8600 1 << (OP_CREATE_SESSION - 32) |
8601 1 << (OP_DESTROY_SESSION - 32) |
8602 1 << (OP_DESTROY_CLIENTID - 32)
8605 [0] = 1 << (OP_CLOSE) |
8606 1 << (OP_OPEN_DOWNGRADE) |
8608 1 << (OP_DELEGRETURN) |
8610 [1] = 1 << (OP_SECINFO - 32) |
8611 1 << (OP_SECINFO_NO_NAME - 32) |
8612 1 << (OP_LAYOUTRETURN - 32) |
8613 1 << (OP_TEST_STATEID - 32) |
8614 1 << (OP_FREE_STATEID - 32) |
8615 1 << (OP_WRITE - 32)
8620 * Select the state protection mode for client `clp' given the server results
8621 * from exchange_id in `sp'.
8623 * Returns 0 on success, negative errno otherwise.
8625 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8626 struct nfs41_state_protection *sp)
8628 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8629 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8630 1 << (OP_EXCHANGE_ID - 32) |
8631 1 << (OP_CREATE_SESSION - 32) |
8632 1 << (OP_DESTROY_SESSION - 32) |
8633 1 << (OP_DESTROY_CLIENTID - 32)
8635 unsigned long flags = 0;
8639 if (sp->how == SP4_MACH_CRED) {
8640 /* Print state protect result */
8641 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8642 for (i = 0; i <= LAST_NFS4_OP; i++) {
8643 if (test_bit(i, sp->enforce.u.longs))
8644 dfprintk(MOUNT, " enforce op %d\n", i);
8645 if (test_bit(i, sp->allow.u.longs))
8646 dfprintk(MOUNT, " allow op %d\n", i);
8649 /* make sure nothing is on enforce list that isn't supported */
8650 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8651 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8652 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8659 * Minimal mode - state operations are allowed to use machine
8660 * credential. Note this already happens by default, so the
8661 * client doesn't have to do anything more than the negotiation.
8663 * NOTE: we don't care if EXCHANGE_ID is in the list -
8664 * we're already using the machine cred for exchange_id
8665 * and will never use a different cred.
8667 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8668 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8669 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8670 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8671 dfprintk(MOUNT, "sp4_mach_cred:\n");
8672 dfprintk(MOUNT, " minimal mode enabled\n");
8673 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8675 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8680 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8681 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8682 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8683 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8684 dfprintk(MOUNT, " cleanup mode enabled\n");
8685 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8688 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8689 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8690 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8693 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8694 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8695 dfprintk(MOUNT, " secinfo mode enabled\n");
8696 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8699 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8700 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8701 dfprintk(MOUNT, " stateid mode enabled\n");
8702 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8705 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8706 dfprintk(MOUNT, " write mode enabled\n");
8707 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8710 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8711 dfprintk(MOUNT, " commit mode enabled\n");
8712 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8716 clp->cl_sp4_flags = flags;
8720 struct nfs41_exchange_id_data {
8721 struct nfs41_exchange_id_res res;
8722 struct nfs41_exchange_id_args args;
8725 static void nfs4_exchange_id_release(void *data)
8727 struct nfs41_exchange_id_data *cdata =
8728 (struct nfs41_exchange_id_data *)data;
8730 nfs_put_client(cdata->args.client);
8731 kfree(cdata->res.impl_id);
8732 kfree(cdata->res.server_scope);
8733 kfree(cdata->res.server_owner);
8737 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8738 .rpc_release = nfs4_exchange_id_release,
8742 * _nfs4_proc_exchange_id()
8744 * Wrapper for EXCHANGE_ID operation.
8746 static struct rpc_task *
8747 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8748 u32 sp4_how, struct rpc_xprt *xprt)
8750 struct rpc_message msg = {
8751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8754 struct rpc_task_setup task_setup_data = {
8755 .rpc_client = clp->cl_rpcclient,
8756 .callback_ops = &nfs4_exchange_id_call_ops,
8757 .rpc_message = &msg,
8758 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8760 struct nfs41_exchange_id_data *calldata;
8763 if (!refcount_inc_not_zero(&clp->cl_count))
8764 return ERR_PTR(-EIO);
8767 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8771 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8773 status = nfs4_init_uniform_client_string(clp);
8777 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8780 if (unlikely(calldata->res.server_owner == NULL))
8783 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8785 if (unlikely(calldata->res.server_scope == NULL))
8786 goto out_server_owner;
8788 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8789 if (unlikely(calldata->res.impl_id == NULL))
8790 goto out_server_scope;
8794 calldata->args.state_protect.how = SP4_NONE;
8798 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8808 task_setup_data.rpc_xprt = xprt;
8809 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8810 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8811 sizeof(calldata->args.verifier.data));
8813 calldata->args.client = clp;
8814 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8815 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8816 #ifdef CONFIG_NFS_V4_1_MIGRATION
8817 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8819 if (test_bit(NFS_CS_DS, &clp->cl_flags))
8820 calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS;
8821 msg.rpc_argp = &calldata->args;
8822 msg.rpc_resp = &calldata->res;
8823 task_setup_data.callback_data = calldata;
8825 return rpc_run_task(&task_setup_data);
8828 kfree(calldata->res.impl_id);
8830 kfree(calldata->res.server_scope);
8832 kfree(calldata->res.server_owner);
8836 nfs_put_client(clp);
8837 return ERR_PTR(status);
8841 * _nfs4_proc_exchange_id()
8843 * Wrapper for EXCHANGE_ID operation.
8845 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8848 struct rpc_task *task;
8849 struct nfs41_exchange_id_args *argp;
8850 struct nfs41_exchange_id_res *resp;
8851 unsigned long now = jiffies;
8854 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8856 return PTR_ERR(task);
8858 argp = task->tk_msg.rpc_argp;
8859 resp = task->tk_msg.rpc_resp;
8860 status = task->tk_status;
8864 status = nfs4_check_cl_exchange_flags(resp->flags,
8865 clp->cl_mvops->minor_version);
8869 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8873 do_renew_lease(clp, now);
8875 clp->cl_clientid = resp->clientid;
8876 clp->cl_exchange_flags = resp->flags;
8877 clp->cl_seqid = resp->seqid;
8878 /* Client ID is not confirmed */
8879 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8880 clear_bit(NFS4_SESSION_ESTABLISHED,
8881 &clp->cl_session->session_state);
8883 if (clp->cl_serverscope != NULL &&
8884 !nfs41_same_server_scope(clp->cl_serverscope,
8885 resp->server_scope)) {
8886 dprintk("%s: server_scope mismatch detected\n",
8888 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8891 swap(clp->cl_serverowner, resp->server_owner);
8892 swap(clp->cl_serverscope, resp->server_scope);
8893 swap(clp->cl_implid, resp->impl_id);
8895 /* Save the EXCHANGE_ID verifier session trunk tests */
8896 memcpy(clp->cl_confirm.data, argp->verifier.data,
8897 sizeof(clp->cl_confirm.data));
8899 trace_nfs4_exchange_id(clp, status);
8905 * nfs4_proc_exchange_id()
8907 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8909 * Since the clientid has expired, all compounds using sessions
8910 * associated with the stale clientid will be returning
8911 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8912 * be in some phase of session reset.
8914 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8916 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8918 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8921 /* try SP4_MACH_CRED if krb5i/p */
8922 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8923 authflavor == RPC_AUTH_GSS_KRB5P) {
8924 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8930 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8934 * nfs4_test_session_trunk
8936 * This is an add_xprt_test() test function called from
8937 * rpc_clnt_setup_test_and_add_xprt.
8939 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8940 * and is dereferrenced in nfs4_exchange_id_release
8942 * Upon success, add the new transport to the rpc_clnt
8944 * @clnt: struct rpc_clnt to get new transport
8945 * @xprt: the rpc_xprt to test
8946 * @data: call data for _nfs4_proc_exchange_id.
8948 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8951 struct nfs4_add_xprt_data *adata = data;
8952 struct rpc_task *task;
8957 dprintk("--> %s try %s\n", __func__,
8958 xprt->address_strings[RPC_DISPLAY_ADDR]);
8960 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8962 /* Test connection for session trunking. Async exchange_id call */
8963 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8967 status = task->tk_status;
8969 status = nfs4_detect_session_trunking(adata->clp,
8970 task->tk_msg.rpc_resp, xprt);
8973 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8974 else if (rpc_clnt_xprt_switch_has_addr(clnt,
8975 (struct sockaddr *)&xprt->addr))
8976 rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
8980 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8982 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8983 const struct cred *cred)
8985 struct rpc_message msg = {
8986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8992 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8993 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8994 trace_nfs4_destroy_clientid(clp, status);
8996 dprintk("NFS: Got error %d from the server %s on "
8997 "DESTROY_CLIENTID.", status, clp->cl_hostname);
9001 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
9002 const struct cred *cred)
9007 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
9008 ret = _nfs4_proc_destroy_clientid(clp, cred);
9010 case -NFS4ERR_DELAY:
9011 case -NFS4ERR_CLIENTID_BUSY:
9021 int nfs4_destroy_clientid(struct nfs_client *clp)
9023 const struct cred *cred;
9026 if (clp->cl_mvops->minor_version < 1)
9028 if (clp->cl_exchange_flags == 0)
9030 if (clp->cl_preserve_clid)
9032 cred = nfs4_get_clid_cred(clp);
9033 ret = nfs4_proc_destroy_clientid(clp, cred);
9037 case -NFS4ERR_STALE_CLIENTID:
9038 clp->cl_exchange_flags = 0;
9044 #endif /* CONFIG_NFS_V4_1 */
9046 struct nfs4_get_lease_time_data {
9047 struct nfs4_get_lease_time_args *args;
9048 struct nfs4_get_lease_time_res *res;
9049 struct nfs_client *clp;
9052 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9055 struct nfs4_get_lease_time_data *data =
9056 (struct nfs4_get_lease_time_data *)calldata;
9058 /* just setup sequence, do not trigger session recovery
9059 since we're invoked within one */
9060 nfs4_setup_sequence(data->clp,
9061 &data->args->la_seq_args,
9062 &data->res->lr_seq_res,
9067 * Called from nfs4_state_manager thread for session setup, so don't recover
9068 * from sequence operation or clientid errors.
9070 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9072 struct nfs4_get_lease_time_data *data =
9073 (struct nfs4_get_lease_time_data *)calldata;
9075 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9077 switch (task->tk_status) {
9078 case -NFS4ERR_DELAY:
9079 case -NFS4ERR_GRACE:
9080 rpc_delay(task, NFS4_POLL_RETRY_MIN);
9081 task->tk_status = 0;
9083 case -NFS4ERR_RETRY_UNCACHED_REP:
9084 rpc_restart_call_prepare(task);
9089 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9090 .rpc_call_prepare = nfs4_get_lease_time_prepare,
9091 .rpc_call_done = nfs4_get_lease_time_done,
9094 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9096 struct nfs4_get_lease_time_args args;
9097 struct nfs4_get_lease_time_res res = {
9098 .lr_fsinfo = fsinfo,
9100 struct nfs4_get_lease_time_data data = {
9105 struct rpc_message msg = {
9106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9110 struct rpc_task_setup task_setup = {
9111 .rpc_client = clp->cl_rpcclient,
9112 .rpc_message = &msg,
9113 .callback_ops = &nfs4_get_lease_time_ops,
9114 .callback_data = &data,
9115 .flags = RPC_TASK_TIMEOUT,
9118 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9119 return nfs4_call_sync_custom(&task_setup);
9122 #ifdef CONFIG_NFS_V4_1
9125 * Initialize the values to be used by the client in CREATE_SESSION
9126 * If nfs4_init_session set the fore channel request and response sizes,
9129 * Set the back channel max_resp_sz_cached to zero to force the client to
9130 * always set csa_cachethis to FALSE because the current implementation
9131 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9133 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9134 struct rpc_clnt *clnt)
9136 unsigned int max_rqst_sz, max_resp_sz;
9137 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9138 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9140 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9141 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9143 /* Fore channel attributes */
9144 args->fc_attrs.max_rqst_sz = max_rqst_sz;
9145 args->fc_attrs.max_resp_sz = max_resp_sz;
9146 args->fc_attrs.max_ops = NFS4_MAX_OPS;
9147 args->fc_attrs.max_reqs = max_session_slots;
9149 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9150 "max_ops=%u max_reqs=%u\n",
9152 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9153 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9155 /* Back channel attributes */
9156 args->bc_attrs.max_rqst_sz = max_bc_payload;
9157 args->bc_attrs.max_resp_sz = max_bc_payload;
9158 args->bc_attrs.max_resp_sz_cached = 0;
9159 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9160 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9161 if (args->bc_attrs.max_reqs > max_bc_slots)
9162 args->bc_attrs.max_reqs = max_bc_slots;
9164 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9165 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9167 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9168 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9169 args->bc_attrs.max_reqs);
9172 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9173 struct nfs41_create_session_res *res)
9175 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9176 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9178 if (rcvd->max_resp_sz > sent->max_resp_sz)
9181 * Our requested max_ops is the minimum we need; we're not
9182 * prepared to break up compounds into smaller pieces than that.
9183 * So, no point even trying to continue if the server won't
9186 if (rcvd->max_ops < sent->max_ops)
9188 if (rcvd->max_reqs == 0)
9190 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9191 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9195 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9196 struct nfs41_create_session_res *res)
9198 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9199 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9201 if (!(res->flags & SESSION4_BACK_CHAN))
9203 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9205 if (rcvd->max_resp_sz < sent->max_resp_sz)
9207 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9209 if (rcvd->max_ops > sent->max_ops)
9211 if (rcvd->max_reqs > sent->max_reqs)
9217 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9218 struct nfs41_create_session_res *res)
9222 ret = nfs4_verify_fore_channel_attrs(args, res);
9225 return nfs4_verify_back_channel_attrs(args, res);
9228 static void nfs4_update_session(struct nfs4_session *session,
9229 struct nfs41_create_session_res *res)
9231 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9232 /* Mark client id and session as being confirmed */
9233 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9234 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9235 session->flags = res->flags;
9236 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9237 if (res->flags & SESSION4_BACK_CHAN)
9238 memcpy(&session->bc_attrs, &res->bc_attrs,
9239 sizeof(session->bc_attrs));
9242 static int _nfs4_proc_create_session(struct nfs_client *clp,
9243 const struct cred *cred)
9245 struct nfs4_session *session = clp->cl_session;
9246 struct nfs41_create_session_args args = {
9248 .clientid = clp->cl_clientid,
9249 .seqid = clp->cl_seqid,
9250 .cb_program = NFS4_CALLBACK,
9252 struct nfs41_create_session_res res;
9254 struct rpc_message msg = {
9255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9262 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9263 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9265 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9266 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9267 trace_nfs4_create_session(clp, status);
9270 case -NFS4ERR_STALE_CLIENTID:
9271 case -NFS4ERR_DELAY:
9280 /* Verify the session's negotiated channel_attrs values */
9281 status = nfs4_verify_channel_attrs(&args, &res);
9282 /* Increment the clientid slot sequence id */
9285 nfs4_update_session(session, &res);
9292 * Issues a CREATE_SESSION operation to the server.
9293 * It is the responsibility of the caller to verify the session is
9294 * expired before calling this routine.
9296 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9300 struct nfs4_session *session = clp->cl_session;
9301 struct nfs4_add_xprt_data xprtdata = {
9304 struct rpc_add_xprt_test rpcdata = {
9305 .add_xprt_test = clp->cl_mvops->session_trunk,
9309 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9311 status = _nfs4_proc_create_session(clp, cred);
9315 /* Init or reset the session slot tables */
9316 status = nfs4_setup_session_slot_tables(session);
9317 dprintk("slot table setup returned %d\n", status);
9321 ptr = (unsigned *)&session->sess_id.data[0];
9322 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9323 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9324 rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9330 * Issue the over-the-wire RPC DESTROY_SESSION.
9331 * The caller must serialize access to this routine.
9333 int nfs4_proc_destroy_session(struct nfs4_session *session,
9334 const struct cred *cred)
9336 struct rpc_message msg = {
9337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9338 .rpc_argp = session,
9343 /* session is still being setup */
9344 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9347 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9348 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9349 trace_nfs4_destroy_session(session->clp, status);
9352 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9353 "Session has been destroyed regardless...\n", status);
9354 rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9359 * Renew the cl_session lease.
9361 struct nfs4_sequence_data {
9362 struct nfs_client *clp;
9363 struct nfs4_sequence_args args;
9364 struct nfs4_sequence_res res;
9367 static void nfs41_sequence_release(void *data)
9369 struct nfs4_sequence_data *calldata = data;
9370 struct nfs_client *clp = calldata->clp;
9372 if (refcount_read(&clp->cl_count) > 1)
9373 nfs4_schedule_state_renewal(clp);
9374 nfs_put_client(clp);
9378 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9380 switch(task->tk_status) {
9381 case -NFS4ERR_DELAY:
9382 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9385 nfs4_schedule_lease_recovery(clp);
9390 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9392 struct nfs4_sequence_data *calldata = data;
9393 struct nfs_client *clp = calldata->clp;
9395 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9398 trace_nfs4_sequence(clp, task->tk_status);
9399 if (task->tk_status < 0 && !task->tk_client->cl_shutdown) {
9400 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9401 if (refcount_read(&clp->cl_count) == 1)
9404 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9405 rpc_restart_call_prepare(task);
9409 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9412 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9414 struct nfs4_sequence_data *calldata = data;
9415 struct nfs_client *clp = calldata->clp;
9416 struct nfs4_sequence_args *args;
9417 struct nfs4_sequence_res *res;
9419 args = task->tk_msg.rpc_argp;
9420 res = task->tk_msg.rpc_resp;
9422 nfs4_setup_sequence(clp, args, res, task);
9425 static const struct rpc_call_ops nfs41_sequence_ops = {
9426 .rpc_call_done = nfs41_sequence_call_done,
9427 .rpc_call_prepare = nfs41_sequence_prepare,
9428 .rpc_release = nfs41_sequence_release,
9431 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9432 const struct cred *cred,
9433 struct nfs4_slot *slot,
9436 struct nfs4_sequence_data *calldata;
9437 struct rpc_message msg = {
9438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9441 struct rpc_task_setup task_setup_data = {
9442 .rpc_client = clp->cl_rpcclient,
9443 .rpc_message = &msg,
9444 .callback_ops = &nfs41_sequence_ops,
9445 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9447 struct rpc_task *ret;
9449 ret = ERR_PTR(-EIO);
9450 if (!refcount_inc_not_zero(&clp->cl_count))
9453 ret = ERR_PTR(-ENOMEM);
9454 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9455 if (calldata == NULL)
9457 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9458 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9459 msg.rpc_argp = &calldata->args;
9460 msg.rpc_resp = &calldata->res;
9461 calldata->clp = clp;
9462 task_setup_data.callback_data = calldata;
9464 ret = rpc_run_task(&task_setup_data);
9469 nfs_put_client(clp);
9471 nfs41_release_slot(slot);
9475 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9477 struct rpc_task *task;
9480 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9482 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9484 ret = PTR_ERR(task);
9486 rpc_put_task_async(task);
9487 dprintk("<-- %s status=%d\n", __func__, ret);
9491 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9493 struct rpc_task *task;
9496 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9498 ret = PTR_ERR(task);
9501 ret = rpc_wait_for_completion_task(task);
9503 ret = task->tk_status;
9506 dprintk("<-- %s status=%d\n", __func__, ret);
9510 struct nfs4_reclaim_complete_data {
9511 struct nfs_client *clp;
9512 struct nfs41_reclaim_complete_args arg;
9513 struct nfs41_reclaim_complete_res res;
9516 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9518 struct nfs4_reclaim_complete_data *calldata = data;
9520 nfs4_setup_sequence(calldata->clp,
9521 &calldata->arg.seq_args,
9522 &calldata->res.seq_res,
9526 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9528 switch(task->tk_status) {
9530 wake_up_all(&clp->cl_lock_waitq);
9532 case -NFS4ERR_COMPLETE_ALREADY:
9533 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9535 case -NFS4ERR_DELAY:
9536 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9538 case -NFS4ERR_RETRY_UNCACHED_REP:
9540 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9541 __func__, task->tk_status, clp->cl_hostname);
9543 case -NFS4ERR_BADSESSION:
9544 case -NFS4ERR_DEADSESSION:
9545 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9548 nfs4_schedule_lease_recovery(clp);
9553 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9555 struct nfs4_reclaim_complete_data *calldata = data;
9556 struct nfs_client *clp = calldata->clp;
9557 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9559 if (!nfs41_sequence_done(task, res))
9562 trace_nfs4_reclaim_complete(clp, task->tk_status);
9563 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9564 rpc_restart_call_prepare(task);
9569 static void nfs4_free_reclaim_complete_data(void *data)
9571 struct nfs4_reclaim_complete_data *calldata = data;
9576 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9577 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9578 .rpc_call_done = nfs4_reclaim_complete_done,
9579 .rpc_release = nfs4_free_reclaim_complete_data,
9583 * Issue a global reclaim complete.
9585 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9586 const struct cred *cred)
9588 struct nfs4_reclaim_complete_data *calldata;
9589 struct rpc_message msg = {
9590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9593 struct rpc_task_setup task_setup_data = {
9594 .rpc_client = clp->cl_rpcclient,
9595 .rpc_message = &msg,
9596 .callback_ops = &nfs4_reclaim_complete_call_ops,
9597 .flags = RPC_TASK_NO_ROUND_ROBIN,
9599 int status = -ENOMEM;
9601 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9602 if (calldata == NULL)
9604 calldata->clp = clp;
9605 calldata->arg.one_fs = 0;
9607 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9608 msg.rpc_argp = &calldata->arg;
9609 msg.rpc_resp = &calldata->res;
9610 task_setup_data.callback_data = calldata;
9611 status = nfs4_call_sync_custom(&task_setup_data);
9613 dprintk("<-- %s status=%d\n", __func__, status);
9618 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9620 struct nfs4_layoutget *lgp = calldata;
9621 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9623 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9624 &lgp->res.seq_res, task);
9627 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9629 struct nfs4_layoutget *lgp = calldata;
9631 nfs41_sequence_process(task, &lgp->res.seq_res);
9635 nfs4_layoutget_handle_exception(struct rpc_task *task,
9636 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9638 struct inode *inode = lgp->args.inode;
9639 struct nfs_server *server = NFS_SERVER(inode);
9640 struct pnfs_layout_hdr *lo = lgp->lo;
9641 int nfs4err = task->tk_status;
9642 int err, status = 0;
9645 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9647 nfs4_sequence_free_slot(&lgp->res.seq_res);
9654 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9655 * on the file. set tk_status to -ENODATA to tell upper layer to
9658 case -NFS4ERR_LAYOUTUNAVAILABLE:
9662 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9663 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9665 case -NFS4ERR_BADLAYOUT:
9666 status = -EOVERFLOW;
9669 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9670 * (or clients) writing to the same RAID stripe except when
9671 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9673 * Treat it like we would RECALLCONFLICT -- we retry for a little
9674 * while, and then eventually give up.
9676 case -NFS4ERR_LAYOUTTRYLATER:
9677 if (lgp->args.minlength == 0) {
9678 status = -EOVERFLOW;
9683 case -NFS4ERR_RECALLCONFLICT:
9684 status = -ERECALLCONFLICT;
9686 case -NFS4ERR_DELEG_REVOKED:
9687 case -NFS4ERR_ADMIN_REVOKED:
9688 case -NFS4ERR_EXPIRED:
9689 case -NFS4ERR_BAD_STATEID:
9690 exception->timeout = 0;
9691 spin_lock(&inode->i_lock);
9692 /* If the open stateid was bad, then recover it. */
9693 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9694 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9695 spin_unlock(&inode->i_lock);
9696 exception->state = lgp->args.ctx->state;
9697 exception->stateid = &lgp->args.stateid;
9702 * Mark the bad layout state as invalid, then retry
9704 pnfs_mark_layout_stateid_invalid(lo, &head);
9705 spin_unlock(&inode->i_lock);
9706 nfs_commit_inode(inode, 0);
9707 pnfs_free_lseg_list(&head);
9712 err = nfs4_handle_exception(server, nfs4err, exception);
9714 if (exception->retry)
9723 size_t max_response_pages(struct nfs_server *server)
9725 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9726 return nfs_page_array_len(0, max_resp_sz);
9729 static void nfs4_layoutget_release(void *calldata)
9731 struct nfs4_layoutget *lgp = calldata;
9733 nfs4_sequence_free_slot(&lgp->res.seq_res);
9734 pnfs_layoutget_free(lgp);
9737 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9738 .rpc_call_prepare = nfs4_layoutget_prepare,
9739 .rpc_call_done = nfs4_layoutget_done,
9740 .rpc_release = nfs4_layoutget_release,
9743 struct pnfs_layout_segment *
9744 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9746 struct inode *inode = lgp->args.inode;
9747 struct nfs_server *server = NFS_SERVER(inode);
9748 struct rpc_task *task;
9749 struct rpc_message msg = {
9750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9751 .rpc_argp = &lgp->args,
9752 .rpc_resp = &lgp->res,
9753 .rpc_cred = lgp->cred,
9755 struct rpc_task_setup task_setup_data = {
9756 .rpc_client = server->client,
9757 .rpc_message = &msg,
9758 .callback_ops = &nfs4_layoutget_call_ops,
9759 .callback_data = lgp,
9760 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9763 struct pnfs_layout_segment *lseg = NULL;
9764 struct nfs4_exception exception = {
9766 .timeout = *timeout,
9770 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9772 task = rpc_run_task(&task_setup_data);
9774 return ERR_CAST(task);
9776 status = rpc_wait_for_completion_task(task);
9780 if (task->tk_status < 0) {
9781 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9782 *timeout = exception.timeout;
9783 } else if (lgp->res.layoutp->len == 0) {
9785 *timeout = nfs4_update_delay(&exception.timeout);
9787 lseg = pnfs_layout_process(lgp);
9789 trace_nfs4_layoutget(lgp->args.ctx,
9796 dprintk("<-- %s status=%d\n", __func__, status);
9798 return ERR_PTR(status);
9803 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9805 struct nfs4_layoutreturn *lrp = calldata;
9807 nfs4_setup_sequence(lrp->clp,
9808 &lrp->args.seq_args,
9811 if (!pnfs_layout_is_valid(lrp->args.layout))
9815 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9817 struct nfs4_layoutreturn *lrp = calldata;
9818 struct nfs_server *server;
9820 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9824 * Was there an RPC level error? Assume the call succeeded,
9825 * and that we need to release the layout
9827 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9828 lrp->res.lrs_present = 0;
9832 server = NFS_SERVER(lrp->args.inode);
9833 switch (task->tk_status) {
9834 case -NFS4ERR_OLD_STATEID:
9835 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9841 task->tk_status = 0;
9845 case -NFS4ERR_DELAY:
9846 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9852 task->tk_status = 0;
9853 nfs4_sequence_free_slot(&lrp->res.seq_res);
9854 rpc_restart_call_prepare(task);
9857 static void nfs4_layoutreturn_release(void *calldata)
9859 struct nfs4_layoutreturn *lrp = calldata;
9860 struct pnfs_layout_hdr *lo = lrp->args.layout;
9862 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9863 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9864 nfs4_sequence_free_slot(&lrp->res.seq_res);
9865 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9866 lrp->ld_private.ops->free(&lrp->ld_private);
9867 pnfs_put_layout_hdr(lrp->args.layout);
9868 nfs_iput_and_deactive(lrp->inode);
9869 put_cred(lrp->cred);
9873 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9874 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9875 .rpc_call_done = nfs4_layoutreturn_done,
9876 .rpc_release = nfs4_layoutreturn_release,
9879 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9881 struct rpc_task *task;
9882 struct rpc_message msg = {
9883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9884 .rpc_argp = &lrp->args,
9885 .rpc_resp = &lrp->res,
9886 .rpc_cred = lrp->cred,
9888 struct rpc_task_setup task_setup_data = {
9889 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9890 .rpc_message = &msg,
9891 .callback_ops = &nfs4_layoutreturn_call_ops,
9892 .callback_data = lrp,
9893 .flags = RPC_TASK_MOVEABLE,
9897 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9898 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9899 &task_setup_data.rpc_client, &msg);
9901 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9904 nfs4_layoutreturn_release(lrp);
9907 task_setup_data.flags |= RPC_TASK_ASYNC;
9910 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9913 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9915 task = rpc_run_task(&task_setup_data);
9917 return PTR_ERR(task);
9919 status = task->tk_status;
9920 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9921 dprintk("<-- %s status=%d\n", __func__, status);
9927 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9928 struct pnfs_device *pdev,
9929 const struct cred *cred)
9931 struct nfs4_getdeviceinfo_args args = {
9933 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9934 NOTIFY_DEVICEID4_DELETE,
9936 struct nfs4_getdeviceinfo_res res = {
9939 struct rpc_message msg = {
9940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9947 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9948 if (res.notification & ~args.notify_types)
9949 dprintk("%s: unsupported notification\n", __func__);
9950 if (res.notification != args.notify_types)
9953 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9955 dprintk("<-- %s status=%d\n", __func__, status);
9960 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9961 struct pnfs_device *pdev,
9962 const struct cred *cred)
9964 struct nfs4_exception exception = { };
9968 err = nfs4_handle_exception(server,
9969 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9971 } while (exception.retry);
9974 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9976 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9978 struct nfs4_layoutcommit_data *data = calldata;
9979 struct nfs_server *server = NFS_SERVER(data->args.inode);
9981 nfs4_setup_sequence(server->nfs_client,
9982 &data->args.seq_args,
9988 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9990 struct nfs4_layoutcommit_data *data = calldata;
9991 struct nfs_server *server = NFS_SERVER(data->args.inode);
9993 if (!nfs41_sequence_done(task, &data->res.seq_res))
9996 switch (task->tk_status) { /* Just ignore these failures */
9997 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9998 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9999 case -NFS4ERR_BADLAYOUT: /* no layout */
10000 case -NFS4ERR_GRACE: /* loca_recalim always false */
10001 task->tk_status = 0;
10006 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
10007 rpc_restart_call_prepare(task);
10013 static void nfs4_layoutcommit_release(void *calldata)
10015 struct nfs4_layoutcommit_data *data = calldata;
10017 pnfs_cleanup_layoutcommit(data);
10018 nfs_post_op_update_inode_force_wcc(data->args.inode,
10020 put_cred(data->cred);
10021 nfs_iput_and_deactive(data->inode);
10025 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
10026 .rpc_call_prepare = nfs4_layoutcommit_prepare,
10027 .rpc_call_done = nfs4_layoutcommit_done,
10028 .rpc_release = nfs4_layoutcommit_release,
10032 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
10034 struct rpc_message msg = {
10035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
10036 .rpc_argp = &data->args,
10037 .rpc_resp = &data->res,
10038 .rpc_cred = data->cred,
10040 struct rpc_task_setup task_setup_data = {
10041 .task = &data->task,
10042 .rpc_client = NFS_CLIENT(data->args.inode),
10043 .rpc_message = &msg,
10044 .callback_ops = &nfs4_layoutcommit_ops,
10045 .callback_data = data,
10046 .flags = RPC_TASK_MOVEABLE,
10048 struct rpc_task *task;
10051 dprintk("NFS: initiating layoutcommit call. sync %d "
10052 "lbw: %llu inode %lu\n", sync,
10053 data->args.lastbytewritten,
10054 data->args.inode->i_ino);
10057 data->inode = nfs_igrab_and_active(data->args.inode);
10058 if (data->inode == NULL) {
10059 nfs4_layoutcommit_release(data);
10062 task_setup_data.flags = RPC_TASK_ASYNC;
10064 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10065 task = rpc_run_task(&task_setup_data);
10067 return PTR_ERR(task);
10069 status = task->tk_status;
10070 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10071 dprintk("%s: status %d\n", __func__, status);
10072 rpc_put_task(task);
10077 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10078 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10081 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10082 struct nfs_fsinfo *info,
10083 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10085 struct nfs41_secinfo_no_name_args args = {
10086 .style = SECINFO_STYLE_CURRENT_FH,
10088 struct nfs4_secinfo_res res = {
10089 .flavors = flavors,
10091 struct rpc_message msg = {
10092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10096 struct nfs4_call_sync_data data = {
10097 .seq_server = server,
10098 .seq_args = &args.seq_args,
10099 .seq_res = &res.seq_res,
10101 struct rpc_task_setup task_setup = {
10102 .rpc_client = server->client,
10103 .rpc_message = &msg,
10104 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10105 .callback_data = &data,
10106 .flags = RPC_TASK_NO_ROUND_ROBIN,
10108 const struct cred *cred = NULL;
10111 if (use_integrity) {
10112 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10114 cred = nfs4_get_clid_cred(server->nfs_client);
10115 msg.rpc_cred = cred;
10118 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10119 status = nfs4_call_sync_custom(&task_setup);
10120 dprintk("<-- %s status=%d\n", __func__, status);
10128 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10129 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10131 struct nfs4_exception exception = {
10132 .interruptible = true,
10136 /* first try using integrity protection */
10137 err = -NFS4ERR_WRONGSEC;
10139 /* try to use integrity protection with machine cred */
10140 if (_nfs4_is_integrity_protected(server->nfs_client))
10141 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10145 * if unable to use integrity protection, or SECINFO with
10146 * integrity protection returns NFS4ERR_WRONGSEC (which is
10147 * disallowed by spec, but exists in deployed servers) use
10148 * the current filesystem's rpc_client and the user cred.
10150 if (err == -NFS4ERR_WRONGSEC)
10151 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10156 case -NFS4ERR_WRONGSEC:
10160 err = nfs4_handle_exception(server, err, &exception);
10162 } while (exception.retry);
10168 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10169 struct nfs_fsinfo *info)
10173 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10174 struct nfs4_secinfo_flavors *flavors;
10175 struct nfs4_secinfo4 *secinfo;
10178 page = alloc_page(GFP_KERNEL);
10184 flavors = page_address(page);
10185 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10188 * Fall back on "guess and check" method if
10189 * the server doesn't support SECINFO_NO_NAME
10191 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10192 err = nfs4_find_root_sec(server, fhandle, info);
10198 for (i = 0; i < flavors->num_flavors; i++) {
10199 secinfo = &flavors->flavors[i];
10201 switch (secinfo->flavor) {
10202 case RPC_AUTH_NULL:
10203 case RPC_AUTH_UNIX:
10205 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10206 &secinfo->flavor_info);
10209 flavor = RPC_AUTH_MAXFLAVOR;
10213 if (!nfs_auth_info_match(&server->auth_info, flavor))
10214 flavor = RPC_AUTH_MAXFLAVOR;
10216 if (flavor != RPC_AUTH_MAXFLAVOR) {
10217 err = nfs4_lookup_root_sec(server, fhandle,
10224 if (flavor == RPC_AUTH_MAXFLAVOR)
10229 if (err == -EACCES)
10235 static int _nfs41_test_stateid(struct nfs_server *server,
10236 nfs4_stateid *stateid,
10237 const struct cred *cred)
10240 struct nfs41_test_stateid_args args = {
10241 .stateid = stateid,
10243 struct nfs41_test_stateid_res res;
10244 struct rpc_message msg = {
10245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10250 struct rpc_clnt *rpc_client = server->client;
10252 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10253 &rpc_client, &msg);
10255 dprintk("NFS call test_stateid %p\n", stateid);
10256 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10257 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10258 &args.seq_args, &res.seq_res);
10259 if (status != NFS_OK) {
10260 dprintk("NFS reply test_stateid: failed, %d\n", status);
10263 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10264 return -res.status;
10267 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10268 int err, struct nfs4_exception *exception)
10270 exception->retry = 0;
10272 case -NFS4ERR_DELAY:
10273 case -NFS4ERR_RETRY_UNCACHED_REP:
10274 nfs4_handle_exception(server, err, exception);
10276 case -NFS4ERR_BADSESSION:
10277 case -NFS4ERR_BADSLOT:
10278 case -NFS4ERR_BAD_HIGH_SLOT:
10279 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10280 case -NFS4ERR_DEADSESSION:
10281 nfs4_do_handle_exception(server, err, exception);
10286 * nfs41_test_stateid - perform a TEST_STATEID operation
10288 * @server: server / transport on which to perform the operation
10289 * @stateid: state ID to test
10290 * @cred: credential
10292 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10293 * Otherwise a negative NFS4ERR value is returned if the operation
10294 * failed or the state ID is not currently valid.
10296 static int nfs41_test_stateid(struct nfs_server *server,
10297 nfs4_stateid *stateid,
10298 const struct cred *cred)
10300 struct nfs4_exception exception = {
10301 .interruptible = true,
10305 err = _nfs41_test_stateid(server, stateid, cred);
10306 nfs4_handle_delay_or_session_error(server, err, &exception);
10307 } while (exception.retry);
10311 struct nfs_free_stateid_data {
10312 struct nfs_server *server;
10313 struct nfs41_free_stateid_args args;
10314 struct nfs41_free_stateid_res res;
10317 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10319 struct nfs_free_stateid_data *data = calldata;
10320 nfs4_setup_sequence(data->server->nfs_client,
10321 &data->args.seq_args,
10322 &data->res.seq_res,
10326 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10328 struct nfs_free_stateid_data *data = calldata;
10330 nfs41_sequence_done(task, &data->res.seq_res);
10332 switch (task->tk_status) {
10333 case -NFS4ERR_DELAY:
10334 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10335 rpc_restart_call_prepare(task);
10339 static void nfs41_free_stateid_release(void *calldata)
10341 struct nfs_free_stateid_data *data = calldata;
10342 struct nfs_client *clp = data->server->nfs_client;
10344 nfs_put_client(clp);
10348 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10349 .rpc_call_prepare = nfs41_free_stateid_prepare,
10350 .rpc_call_done = nfs41_free_stateid_done,
10351 .rpc_release = nfs41_free_stateid_release,
10355 * nfs41_free_stateid - perform a FREE_STATEID operation
10357 * @server: server / transport on which to perform the operation
10358 * @stateid: state ID to release
10359 * @cred: credential
10360 * @privileged: set to true if this call needs to be privileged
10362 * Note: this function is always asynchronous.
10364 static int nfs41_free_stateid(struct nfs_server *server,
10365 const nfs4_stateid *stateid,
10366 const struct cred *cred,
10369 struct rpc_message msg = {
10370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10373 struct rpc_task_setup task_setup = {
10374 .rpc_client = server->client,
10375 .rpc_message = &msg,
10376 .callback_ops = &nfs41_free_stateid_ops,
10377 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10379 struct nfs_free_stateid_data *data;
10380 struct rpc_task *task;
10381 struct nfs_client *clp = server->nfs_client;
10383 if (!refcount_inc_not_zero(&clp->cl_count))
10386 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10387 &task_setup.rpc_client, &msg);
10389 dprintk("NFS call free_stateid %p\n", stateid);
10390 data = kmalloc(sizeof(*data), GFP_KERNEL);
10393 data->server = server;
10394 nfs4_stateid_copy(&data->args.stateid, stateid);
10396 task_setup.callback_data = data;
10398 msg.rpc_argp = &data->args;
10399 msg.rpc_resp = &data->res;
10400 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10401 task = rpc_run_task(&task_setup);
10403 return PTR_ERR(task);
10404 rpc_put_task(task);
10409 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10411 const struct cred *cred = lsp->ls_state->owner->so_cred;
10413 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10414 nfs4_free_lock_state(server, lsp);
10417 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10418 const nfs4_stateid *s2)
10420 if (s1->type != s2->type)
10423 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10426 if (s1->seqid == s2->seqid)
10429 return s1->seqid == 0 || s2->seqid == 0;
10432 #endif /* CONFIG_NFS_V4_1 */
10434 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10435 const nfs4_stateid *s2)
10437 return nfs4_stateid_match(s1, s2);
10441 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10442 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10443 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10444 .recover_open = nfs4_open_reclaim,
10445 .recover_lock = nfs4_lock_reclaim,
10446 .establish_clid = nfs4_init_clientid,
10447 .detect_trunking = nfs40_discover_server_trunking,
10450 #if defined(CONFIG_NFS_V4_1)
10451 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10452 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10453 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10454 .recover_open = nfs4_open_reclaim,
10455 .recover_lock = nfs4_lock_reclaim,
10456 .establish_clid = nfs41_init_clientid,
10457 .reclaim_complete = nfs41_proc_reclaim_complete,
10458 .detect_trunking = nfs41_discover_server_trunking,
10460 #endif /* CONFIG_NFS_V4_1 */
10462 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10463 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10464 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10465 .recover_open = nfs40_open_expired,
10466 .recover_lock = nfs4_lock_expired,
10467 .establish_clid = nfs4_init_clientid,
10470 #if defined(CONFIG_NFS_V4_1)
10471 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10472 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10473 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10474 .recover_open = nfs41_open_expired,
10475 .recover_lock = nfs41_lock_expired,
10476 .establish_clid = nfs41_init_clientid,
10478 #endif /* CONFIG_NFS_V4_1 */
10480 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10481 .sched_state_renewal = nfs4_proc_async_renew,
10482 .get_state_renewal_cred = nfs4_get_renew_cred,
10483 .renew_lease = nfs4_proc_renew,
10486 #if defined(CONFIG_NFS_V4_1)
10487 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10488 .sched_state_renewal = nfs41_proc_async_sequence,
10489 .get_state_renewal_cred = nfs4_get_machine_cred,
10490 .renew_lease = nfs4_proc_sequence,
10494 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10495 .get_locations = _nfs40_proc_get_locations,
10496 .fsid_present = _nfs40_proc_fsid_present,
10499 #if defined(CONFIG_NFS_V4_1)
10500 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10501 .get_locations = _nfs41_proc_get_locations,
10502 .fsid_present = _nfs41_proc_fsid_present,
10504 #endif /* CONFIG_NFS_V4_1 */
10506 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10507 .minor_version = 0,
10508 .init_caps = NFS_CAP_READDIRPLUS
10509 | NFS_CAP_ATOMIC_OPEN
10510 | NFS_CAP_POSIX_LOCK,
10511 .init_client = nfs40_init_client,
10512 .shutdown_client = nfs40_shutdown_client,
10513 .match_stateid = nfs4_match_stateid,
10514 .find_root_sec = nfs4_find_root_sec,
10515 .free_lock_state = nfs4_release_lockowner,
10516 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10517 .alloc_seqid = nfs_alloc_seqid,
10518 .call_sync_ops = &nfs40_call_sync_ops,
10519 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10520 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10521 .state_renewal_ops = &nfs40_state_renewal_ops,
10522 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10525 #if defined(CONFIG_NFS_V4_1)
10526 static struct nfs_seqid *
10527 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10532 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10533 .minor_version = 1,
10534 .init_caps = NFS_CAP_READDIRPLUS
10535 | NFS_CAP_ATOMIC_OPEN
10536 | NFS_CAP_POSIX_LOCK
10537 | NFS_CAP_STATEID_NFSV41
10538 | NFS_CAP_ATOMIC_OPEN_V1
10540 | NFS_CAP_MOVEABLE,
10541 .init_client = nfs41_init_client,
10542 .shutdown_client = nfs41_shutdown_client,
10543 .match_stateid = nfs41_match_stateid,
10544 .find_root_sec = nfs41_find_root_sec,
10545 .free_lock_state = nfs41_free_lock_state,
10546 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10547 .alloc_seqid = nfs_alloc_no_seqid,
10548 .session_trunk = nfs4_test_session_trunk,
10549 .call_sync_ops = &nfs41_call_sync_ops,
10550 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10551 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10552 .state_renewal_ops = &nfs41_state_renewal_ops,
10553 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10557 #if defined(CONFIG_NFS_V4_2)
10558 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10559 .minor_version = 2,
10560 .init_caps = NFS_CAP_READDIRPLUS
10561 | NFS_CAP_ATOMIC_OPEN
10562 | NFS_CAP_POSIX_LOCK
10563 | NFS_CAP_STATEID_NFSV41
10564 | NFS_CAP_ATOMIC_OPEN_V1
10568 | NFS_CAP_OFFLOAD_CANCEL
10569 | NFS_CAP_COPY_NOTIFY
10570 | NFS_CAP_DEALLOCATE
10572 | NFS_CAP_LAYOUTSTATS
10574 | NFS_CAP_LAYOUTERROR
10575 | NFS_CAP_READ_PLUS
10576 | NFS_CAP_MOVEABLE,
10577 .init_client = nfs41_init_client,
10578 .shutdown_client = nfs41_shutdown_client,
10579 .match_stateid = nfs41_match_stateid,
10580 .find_root_sec = nfs41_find_root_sec,
10581 .free_lock_state = nfs41_free_lock_state,
10582 .call_sync_ops = &nfs41_call_sync_ops,
10583 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10584 .alloc_seqid = nfs_alloc_no_seqid,
10585 .session_trunk = nfs4_test_session_trunk,
10586 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10587 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10588 .state_renewal_ops = &nfs41_state_renewal_ops,
10589 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10593 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10594 [0] = &nfs_v4_0_minor_ops,
10595 #if defined(CONFIG_NFS_V4_1)
10596 [1] = &nfs_v4_1_minor_ops,
10598 #if defined(CONFIG_NFS_V4_2)
10599 [2] = &nfs_v4_2_minor_ops,
10603 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10605 ssize_t error, error2, error3;
10607 error = generic_listxattr(dentry, list, size);
10615 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10624 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10628 return error + error2 + error3;
10631 static void nfs4_enable_swap(struct inode *inode)
10633 /* The state manager thread must always be running.
10634 * It will notice the client is a swapper, and stay put.
10636 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10638 nfs4_schedule_state_manager(clp);
10641 static void nfs4_disable_swap(struct inode *inode)
10643 /* The state manager thread will now exit once it is
10646 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10648 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10649 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
10650 wake_up_var(&clp->cl_state);
10653 static const struct inode_operations nfs4_dir_inode_operations = {
10654 .create = nfs_create,
10655 .lookup = nfs_lookup,
10656 .atomic_open = nfs_atomic_open,
10658 .unlink = nfs_unlink,
10659 .symlink = nfs_symlink,
10660 .mkdir = nfs_mkdir,
10661 .rmdir = nfs_rmdir,
10662 .mknod = nfs_mknod,
10663 .rename = nfs_rename,
10664 .permission = nfs_permission,
10665 .getattr = nfs_getattr,
10666 .setattr = nfs_setattr,
10667 .listxattr = nfs4_listxattr,
10670 static const struct inode_operations nfs4_file_inode_operations = {
10671 .permission = nfs_permission,
10672 .getattr = nfs_getattr,
10673 .setattr = nfs_setattr,
10674 .listxattr = nfs4_listxattr,
10677 const struct nfs_rpc_ops nfs_v4_clientops = {
10678 .version = 4, /* protocol version */
10679 .dentry_ops = &nfs4_dentry_operations,
10680 .dir_inode_ops = &nfs4_dir_inode_operations,
10681 .file_inode_ops = &nfs4_file_inode_operations,
10682 .file_ops = &nfs4_file_operations,
10683 .getroot = nfs4_proc_get_root,
10684 .submount = nfs4_submount,
10685 .try_get_tree = nfs4_try_get_tree,
10686 .getattr = nfs4_proc_getattr,
10687 .setattr = nfs4_proc_setattr,
10688 .lookup = nfs4_proc_lookup,
10689 .lookupp = nfs4_proc_lookupp,
10690 .access = nfs4_proc_access,
10691 .readlink = nfs4_proc_readlink,
10692 .create = nfs4_proc_create,
10693 .remove = nfs4_proc_remove,
10694 .unlink_setup = nfs4_proc_unlink_setup,
10695 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10696 .unlink_done = nfs4_proc_unlink_done,
10697 .rename_setup = nfs4_proc_rename_setup,
10698 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10699 .rename_done = nfs4_proc_rename_done,
10700 .link = nfs4_proc_link,
10701 .symlink = nfs4_proc_symlink,
10702 .mkdir = nfs4_proc_mkdir,
10703 .rmdir = nfs4_proc_rmdir,
10704 .readdir = nfs4_proc_readdir,
10705 .mknod = nfs4_proc_mknod,
10706 .statfs = nfs4_proc_statfs,
10707 .fsinfo = nfs4_proc_fsinfo,
10708 .pathconf = nfs4_proc_pathconf,
10709 .set_capabilities = nfs4_server_capabilities,
10710 .decode_dirent = nfs4_decode_dirent,
10711 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10712 .read_setup = nfs4_proc_read_setup,
10713 .read_done = nfs4_read_done,
10714 .write_setup = nfs4_proc_write_setup,
10715 .write_done = nfs4_write_done,
10716 .commit_setup = nfs4_proc_commit_setup,
10717 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10718 .commit_done = nfs4_commit_done,
10719 .lock = nfs4_proc_lock,
10720 .clear_acl_cache = nfs4_zap_acl_attr,
10721 .close_context = nfs4_close_context,
10722 .open_context = nfs4_atomic_open,
10723 .have_delegation = nfs4_have_delegation,
10724 .alloc_client = nfs4_alloc_client,
10725 .init_client = nfs4_init_client,
10726 .free_client = nfs4_free_client,
10727 .create_server = nfs4_create_server,
10728 .clone_server = nfs_clone_server,
10729 .discover_trunking = nfs4_discover_trunking,
10730 .enable_swap = nfs4_enable_swap,
10731 .disable_swap = nfs4_disable_swap,
10734 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10735 .name = XATTR_NAME_NFSV4_ACL,
10736 .list = nfs4_xattr_list_nfs4_acl,
10737 .get = nfs4_xattr_get_nfs4_acl,
10738 .set = nfs4_xattr_set_nfs4_acl,
10741 #if defined(CONFIG_NFS_V4_1)
10742 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10743 .name = XATTR_NAME_NFSV4_DACL,
10744 .list = nfs4_xattr_list_nfs4_dacl,
10745 .get = nfs4_xattr_get_nfs4_dacl,
10746 .set = nfs4_xattr_set_nfs4_dacl,
10749 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10750 .name = XATTR_NAME_NFSV4_SACL,
10751 .list = nfs4_xattr_list_nfs4_sacl,
10752 .get = nfs4_xattr_get_nfs4_sacl,
10753 .set = nfs4_xattr_set_nfs4_sacl,
10757 #ifdef CONFIG_NFS_V4_2
10758 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10759 .prefix = XATTR_USER_PREFIX,
10760 .get = nfs4_xattr_get_nfs4_user,
10761 .set = nfs4_xattr_set_nfs4_user,
10765 const struct xattr_handler *nfs4_xattr_handlers[] = {
10766 &nfs4_xattr_nfs4_acl_handler,
10767 #if defined(CONFIG_NFS_V4_1)
10768 &nfs4_xattr_nfs4_dacl_handler,
10769 &nfs4_xattr_nfs4_sacl_handler,
10771 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10772 &nfs4_xattr_nfs4_label_handler,
10774 #ifdef CONFIG_NFS_V4_2
10775 &nfs4_xattr_nfs4_user_handler,