2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
45 #include <linux/string_helpers.h>
46 #include <linux/fsnotify.h>
47 #include <linux/rhashtable.h>
48 #include <linux/nfs_ssc.h>
53 #include "current_stateid.h"
57 #include "filecache.h"
60 #define NFSDDBG_FACILITY NFSDDBG_PROC
62 #define all_ones {{ ~0, ~0}, ~0}
63 static const stateid_t one_stateid = {
65 .si_opaque = all_ones,
67 static const stateid_t zero_stateid = {
70 static const stateid_t currentstateid = {
73 static const stateid_t close_stateid = {
74 .si_generation = 0xffffffffU,
77 static u64 current_sessionid = 1;
79 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
80 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
81 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
82 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
84 /* forward declarations */
85 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
86 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
87 void nfsd4_end_grace(struct nfsd_net *nn);
88 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
89 static void nfsd4_file_hash_remove(struct nfs4_file *fi);
94 * Currently used for the del_recall_lru and file hash table. In an
95 * effort to decrease the scope of the client_mutex, this spinlock may
96 * eventually cover more:
98 static DEFINE_SPINLOCK(state_lock);
100 enum nfsd4_st_mutex_lock_subclass {
101 OPEN_STATEID_MUTEX = 0,
102 LOCK_STATEID_MUTEX = 1,
106 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
107 * the refcount on the open stateid to drop.
109 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
112 * A waitqueue where a writer to clients/#/ctl destroying a client can
113 * wait for cl_rpc_users to drop to 0 and then for the client to be
116 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
118 static struct kmem_cache *client_slab;
119 static struct kmem_cache *openowner_slab;
120 static struct kmem_cache *lockowner_slab;
121 static struct kmem_cache *file_slab;
122 static struct kmem_cache *stateid_slab;
123 static struct kmem_cache *deleg_slab;
124 static struct kmem_cache *odstate_slab;
126 static void free_session(struct nfsd4_session *);
128 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
129 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
131 static struct workqueue_struct *laundry_wq;
133 int nfsd4_create_laundry_wq(void)
137 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
138 if (laundry_wq == NULL)
143 void nfsd4_destroy_laundry_wq(void)
145 destroy_workqueue(laundry_wq);
148 static bool is_session_dead(struct nfsd4_session *ses)
150 return ses->se_flags & NFS4_SESSION_DEAD;
153 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
155 if (atomic_read(&ses->se_ref) > ref_held_by_me)
156 return nfserr_jukebox;
157 ses->se_flags |= NFS4_SESSION_DEAD;
161 static bool is_client_expired(struct nfs4_client *clp)
163 return clp->cl_time == 0;
166 static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn,
167 struct nfs4_client *clp)
169 if (clp->cl_state != NFSD4_ACTIVE)
170 atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0);
173 static __be32 get_client_locked(struct nfs4_client *clp)
175 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
177 lockdep_assert_held(&nn->client_lock);
179 if (is_client_expired(clp))
180 return nfserr_expired;
181 atomic_inc(&clp->cl_rpc_users);
182 nfsd4_dec_courtesy_client_count(nn, clp);
183 clp->cl_state = NFSD4_ACTIVE;
187 /* must be called under the client_lock */
189 renew_client_locked(struct nfs4_client *clp)
191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
193 if (is_client_expired(clp)) {
195 printk("%s: client (clientid %08x/%08x) already expired\n",
197 clp->cl_clientid.cl_boot,
198 clp->cl_clientid.cl_id);
202 list_move_tail(&clp->cl_lru, &nn->client_lru);
203 clp->cl_time = ktime_get_boottime_seconds();
204 nfsd4_dec_courtesy_client_count(nn, clp);
205 clp->cl_state = NFSD4_ACTIVE;
208 static void put_client_renew_locked(struct nfs4_client *clp)
210 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
212 lockdep_assert_held(&nn->client_lock);
214 if (!atomic_dec_and_test(&clp->cl_rpc_users))
216 if (!is_client_expired(clp))
217 renew_client_locked(clp);
219 wake_up_all(&expiry_wq);
222 static void put_client_renew(struct nfs4_client *clp)
224 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
226 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
228 if (!is_client_expired(clp))
229 renew_client_locked(clp);
231 wake_up_all(&expiry_wq);
232 spin_unlock(&nn->client_lock);
235 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
239 if (is_session_dead(ses))
240 return nfserr_badsession;
241 status = get_client_locked(ses->se_client);
244 atomic_inc(&ses->se_ref);
248 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
250 struct nfs4_client *clp = ses->se_client;
251 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
253 lockdep_assert_held(&nn->client_lock);
255 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
257 put_client_renew_locked(clp);
260 static void nfsd4_put_session(struct nfsd4_session *ses)
262 struct nfs4_client *clp = ses->se_client;
263 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
265 spin_lock(&nn->client_lock);
266 nfsd4_put_session_locked(ses);
267 spin_unlock(&nn->client_lock);
270 static struct nfsd4_blocked_lock *
271 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
274 struct nfsd4_blocked_lock *cur, *found = NULL;
276 spin_lock(&nn->blocked_locks_lock);
277 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
278 if (fh_match(fh, &cur->nbl_fh)) {
279 list_del_init(&cur->nbl_list);
280 WARN_ON(list_empty(&cur->nbl_lru));
281 list_del_init(&cur->nbl_lru);
286 spin_unlock(&nn->blocked_locks_lock);
288 locks_delete_block(&found->nbl_lock);
292 static struct nfsd4_blocked_lock *
293 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
296 struct nfsd4_blocked_lock *nbl;
298 nbl = find_blocked_lock(lo, fh, nn);
300 nbl = kmalloc(sizeof(*nbl), GFP_KERNEL);
302 INIT_LIST_HEAD(&nbl->nbl_list);
303 INIT_LIST_HEAD(&nbl->nbl_lru);
304 fh_copy_shallow(&nbl->nbl_fh, fh);
305 locks_init_lock(&nbl->nbl_lock);
306 kref_init(&nbl->nbl_kref);
307 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
308 &nfsd4_cb_notify_lock_ops,
309 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
316 free_nbl(struct kref *kref)
318 struct nfsd4_blocked_lock *nbl;
320 nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
325 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
327 locks_delete_block(&nbl->nbl_lock);
328 locks_release_private(&nbl->nbl_lock);
329 kref_put(&nbl->nbl_kref, free_nbl);
333 remove_blocked_locks(struct nfs4_lockowner *lo)
335 struct nfs4_client *clp = lo->lo_owner.so_client;
336 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
337 struct nfsd4_blocked_lock *nbl;
340 /* Dequeue all blocked locks */
341 spin_lock(&nn->blocked_locks_lock);
342 while (!list_empty(&lo->lo_blocked)) {
343 nbl = list_first_entry(&lo->lo_blocked,
344 struct nfsd4_blocked_lock,
346 list_del_init(&nbl->nbl_list);
347 WARN_ON(list_empty(&nbl->nbl_lru));
348 list_move(&nbl->nbl_lru, &reaplist);
350 spin_unlock(&nn->blocked_locks_lock);
353 while (!list_empty(&reaplist)) {
354 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
356 list_del_init(&nbl->nbl_lru);
357 free_blocked_lock(nbl);
362 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
364 struct nfsd4_blocked_lock *nbl = container_of(cb,
365 struct nfsd4_blocked_lock, nbl_cb);
366 locks_delete_block(&nbl->nbl_lock);
370 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
372 trace_nfsd_cb_notify_lock_done(&zero_stateid, task);
375 * Since this is just an optimization, we don't try very hard if it
376 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
377 * just quit trying on anything else.
379 switch (task->tk_status) {
381 rpc_delay(task, 1 * HZ);
389 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
391 struct nfsd4_blocked_lock *nbl = container_of(cb,
392 struct nfsd4_blocked_lock, nbl_cb);
394 free_blocked_lock(nbl);
397 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
398 .prepare = nfsd4_cb_notify_lock_prepare,
399 .done = nfsd4_cb_notify_lock_done,
400 .release = nfsd4_cb_notify_lock_release,
404 * We store the NONE, READ, WRITE, and BOTH bits separately in the
405 * st_{access,deny}_bmap field of the stateid, in order to track not
406 * only what share bits are currently in force, but also what
407 * combinations of share bits previous opens have used. This allows us
408 * to enforce the recommendation in
409 * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
410 * the server return an error if the client attempt to downgrade to a
411 * combination of share bits not explicable by closing some of its
414 * This enforcement is arguably incomplete, since we don't keep
415 * track of access/deny bit combinations; so, e.g., we allow:
417 * OPEN allow read, deny write
418 * OPEN allow both, deny none
419 * DOWNGRADE allow read, deny none
421 * which we should reject.
423 * But you could also argue that our current code is already overkill,
424 * since it only exists to return NFS4ERR_INVAL on incorrect client
428 bmap_to_share_mode(unsigned long bmap)
431 unsigned int access = 0;
433 for (i = 1; i < 4; i++) {
434 if (test_bit(i, &bmap))
440 /* set share access for a given stateid */
442 set_access(u32 access, struct nfs4_ol_stateid *stp)
444 unsigned char mask = 1 << access;
446 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
447 stp->st_access_bmap |= mask;
450 /* clear share access for a given stateid */
452 clear_access(u32 access, struct nfs4_ol_stateid *stp)
454 unsigned char mask = 1 << access;
456 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
457 stp->st_access_bmap &= ~mask;
460 /* test whether a given stateid has access */
462 test_access(u32 access, struct nfs4_ol_stateid *stp)
464 unsigned char mask = 1 << access;
466 return (bool)(stp->st_access_bmap & mask);
469 /* set share deny for a given stateid */
471 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
473 unsigned char mask = 1 << deny;
475 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
476 stp->st_deny_bmap |= mask;
479 /* clear share deny for a given stateid */
481 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
483 unsigned char mask = 1 << deny;
485 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
486 stp->st_deny_bmap &= ~mask;
489 /* test whether a given stateid is denying specific access */
491 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
493 unsigned char mask = 1 << deny;
495 return (bool)(stp->st_deny_bmap & mask);
498 static int nfs4_access_to_omode(u32 access)
500 switch (access & NFS4_SHARE_ACCESS_BOTH) {
501 case NFS4_SHARE_ACCESS_READ:
503 case NFS4_SHARE_ACCESS_WRITE:
505 case NFS4_SHARE_ACCESS_BOTH:
513 access_permit_read(struct nfs4_ol_stateid *stp)
515 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
516 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
517 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
521 access_permit_write(struct nfs4_ol_stateid *stp)
523 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
524 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
527 static inline struct nfs4_stateowner *
528 nfs4_get_stateowner(struct nfs4_stateowner *sop)
530 atomic_inc(&sop->so_count);
535 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
537 return (sop->so_owner.len == owner->len) &&
538 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
541 static struct nfs4_openowner *
542 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
543 struct nfs4_client *clp)
545 struct nfs4_stateowner *so;
547 lockdep_assert_held(&clp->cl_lock);
549 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
551 if (!so->so_is_open_owner)
553 if (same_owner_str(so, &open->op_owner))
554 return openowner(nfs4_get_stateowner(so));
559 static struct nfs4_openowner *
560 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
561 struct nfs4_client *clp)
563 struct nfs4_openowner *oo;
565 spin_lock(&clp->cl_lock);
566 oo = find_openstateowner_str_locked(hashval, open, clp);
567 spin_unlock(&clp->cl_lock);
572 opaque_hashval(const void *ptr, int nbytes)
574 unsigned char *cptr = (unsigned char *) ptr;
584 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
586 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
588 kmem_cache_free(file_slab, fp);
592 put_nfs4_file(struct nfs4_file *fi)
594 if (refcount_dec_and_test(&fi->fi_ref)) {
595 nfsd4_file_hash_remove(fi);
596 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
597 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
598 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
602 static struct nfsd_file *
603 find_writeable_file_locked(struct nfs4_file *f)
605 struct nfsd_file *ret;
607 lockdep_assert_held(&f->fi_lock);
609 ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
611 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
615 static struct nfsd_file *
616 find_writeable_file(struct nfs4_file *f)
618 struct nfsd_file *ret;
620 spin_lock(&f->fi_lock);
621 ret = find_writeable_file_locked(f);
622 spin_unlock(&f->fi_lock);
627 static struct nfsd_file *
628 find_readable_file_locked(struct nfs4_file *f)
630 struct nfsd_file *ret;
632 lockdep_assert_held(&f->fi_lock);
634 ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
636 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
640 static struct nfsd_file *
641 find_readable_file(struct nfs4_file *f)
643 struct nfsd_file *ret;
645 spin_lock(&f->fi_lock);
646 ret = find_readable_file_locked(f);
647 spin_unlock(&f->fi_lock);
652 static struct nfsd_file *
653 find_rw_file(struct nfs4_file *f)
655 struct nfsd_file *ret;
657 spin_lock(&f->fi_lock);
658 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
659 spin_unlock(&f->fi_lock);
665 find_any_file(struct nfs4_file *f)
667 struct nfsd_file *ret;
671 spin_lock(&f->fi_lock);
672 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
674 ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
676 ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
678 spin_unlock(&f->fi_lock);
682 static struct nfsd_file *find_any_file_locked(struct nfs4_file *f)
684 lockdep_assert_held(&f->fi_lock);
686 if (f->fi_fds[O_RDWR])
687 return f->fi_fds[O_RDWR];
688 if (f->fi_fds[O_WRONLY])
689 return f->fi_fds[O_WRONLY];
690 if (f->fi_fds[O_RDONLY])
691 return f->fi_fds[O_RDONLY];
695 static atomic_long_t num_delegations;
696 unsigned long max_delegations;
699 * Open owner state (share locks)
702 /* hash tables for lock and open owners */
703 #define OWNER_HASH_BITS 8
704 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
705 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
707 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
711 ret = opaque_hashval(ownername->data, ownername->len);
712 return ret & OWNER_HASH_MASK;
715 static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp;
717 static const struct rhashtable_params nfs4_file_rhash_params = {
718 .key_len = sizeof_field(struct nfs4_file, fi_inode),
719 .key_offset = offsetof(struct nfs4_file, fi_inode),
720 .head_offset = offsetof(struct nfs4_file, fi_rlist),
723 * Start with a single page hash table to reduce resizing churn
724 * on light workloads.
727 .automatic_shrinking = true,
731 * Check if courtesy clients have conflicting access and resolve it if possible
733 * access: is op_share_access if share_access is true.
734 * Check if access mode, op_share_access, would conflict with
735 * the current deny mode of the file 'fp'.
736 * access: is op_share_deny if share_access is false.
737 * Check if the deny mode, op_share_deny, would conflict with
738 * current access of the file 'fp'.
739 * stp: skip checking this entry.
740 * new_stp: normal open, not open upgrade.
743 * false - access/deny mode conflict with normal client.
744 * true - no conflict or conflict with courtesy client(s) is resolved.
747 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
748 struct nfs4_ol_stateid *stp, u32 access, bool share_access)
750 struct nfs4_ol_stateid *st;
751 bool resolvable = true;
754 struct nfs4_client *clp;
756 lockdep_assert_held(&fp->fi_lock);
757 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
758 /* ignore lock stateid */
761 if (st == stp && new_stp)
763 /* check file access against deny mode or vice versa */
764 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
765 if (!(access & bmap_to_share_mode(bmap)))
767 clp = st->st_stid.sc_client;
768 if (try_to_expire_client(clp))
774 clp = stp->st_stid.sc_client;
775 nn = net_generic(clp->net, nfsd_net_id);
776 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
782 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
784 lockdep_assert_held(&fp->fi_lock);
786 if (access & NFS4_SHARE_ACCESS_WRITE)
787 atomic_inc(&fp->fi_access[O_WRONLY]);
788 if (access & NFS4_SHARE_ACCESS_READ)
789 atomic_inc(&fp->fi_access[O_RDONLY]);
793 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
795 lockdep_assert_held(&fp->fi_lock);
797 /* Does this access mode make sense? */
798 if (access & ~NFS4_SHARE_ACCESS_BOTH)
801 /* Does it conflict with a deny mode already set? */
802 if ((access & fp->fi_share_deny) != 0)
803 return nfserr_share_denied;
805 __nfs4_file_get_access(fp, access);
809 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
811 /* Common case is that there is no deny mode. */
813 /* Does this deny mode make sense? */
814 if (deny & ~NFS4_SHARE_DENY_BOTH)
817 if ((deny & NFS4_SHARE_DENY_READ) &&
818 atomic_read(&fp->fi_access[O_RDONLY]))
819 return nfserr_share_denied;
821 if ((deny & NFS4_SHARE_DENY_WRITE) &&
822 atomic_read(&fp->fi_access[O_WRONLY]))
823 return nfserr_share_denied;
828 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
830 might_lock(&fp->fi_lock);
832 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
833 struct nfsd_file *f1 = NULL;
834 struct nfsd_file *f2 = NULL;
836 swap(f1, fp->fi_fds[oflag]);
837 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
838 swap(f2, fp->fi_fds[O_RDWR]);
839 spin_unlock(&fp->fi_lock);
847 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
849 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
851 if (access & NFS4_SHARE_ACCESS_WRITE)
852 __nfs4_file_put_access(fp, O_WRONLY);
853 if (access & NFS4_SHARE_ACCESS_READ)
854 __nfs4_file_put_access(fp, O_RDONLY);
858 * Allocate a new open/delegation state counter. This is needed for
859 * pNFS for proper return on close semantics.
861 * Note that we only allocate it for pNFS-enabled exports, otherwise
862 * all pointers to struct nfs4_clnt_odstate are always NULL.
864 static struct nfs4_clnt_odstate *
865 alloc_clnt_odstate(struct nfs4_client *clp)
867 struct nfs4_clnt_odstate *co;
869 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
872 refcount_set(&co->co_odcount, 1);
878 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
880 struct nfs4_file *fp = co->co_file;
882 lockdep_assert_held(&fp->fi_lock);
883 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
887 get_clnt_odstate(struct nfs4_clnt_odstate *co)
890 refcount_inc(&co->co_odcount);
894 put_clnt_odstate(struct nfs4_clnt_odstate *co)
896 struct nfs4_file *fp;
902 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
903 list_del(&co->co_perfile);
904 spin_unlock(&fp->fi_lock);
906 nfsd4_return_all_file_layouts(co->co_client, fp);
907 kmem_cache_free(odstate_slab, co);
911 static struct nfs4_clnt_odstate *
912 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
914 struct nfs4_clnt_odstate *co;
915 struct nfs4_client *cl;
922 spin_lock(&fp->fi_lock);
923 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
924 if (co->co_client == cl) {
925 get_clnt_odstate(co);
931 hash_clnt_odstate_locked(new);
933 spin_unlock(&fp->fi_lock);
937 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
938 void (*sc_free)(struct nfs4_stid *))
940 struct nfs4_stid *stid;
943 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
947 idr_preload(GFP_KERNEL);
948 spin_lock(&cl->cl_lock);
949 /* Reserving 0 for start of file in nfsdfs "states" file: */
950 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
951 spin_unlock(&cl->cl_lock);
956 stid->sc_free = sc_free;
957 stid->sc_client = cl;
958 stid->sc_stateid.si_opaque.so_id = new_id;
959 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
960 /* Will be incremented before return to client: */
961 refcount_set(&stid->sc_count, 1);
962 spin_lock_init(&stid->sc_lock);
963 INIT_LIST_HEAD(&stid->sc_cp_list);
966 * It shouldn't be a problem to reuse an opaque stateid value.
967 * I don't think it is for 4.1. But with 4.0 I worry that, for
968 * example, a stray write retransmission could be accepted by
969 * the server when it should have been rejected. Therefore,
970 * adopt a trick from the sctp code to attempt to maximize the
971 * amount of time until an id is reused, by ensuring they always
972 * "increase" (mod INT_MAX):
976 kmem_cache_free(slab, stid);
981 * Create a unique stateid_t to represent each COPY.
983 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
984 unsigned char cs_type)
988 stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
989 stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
991 idr_preload(GFP_KERNEL);
992 spin_lock(&nn->s2s_cp_lock);
993 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
994 stid->cs_stid.si_opaque.so_id = new_id;
995 stid->cs_stid.si_generation = 1;
996 spin_unlock(&nn->s2s_cp_lock);
1000 stid->cs_type = cs_type;
1004 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
1006 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID);
1009 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
1010 struct nfs4_stid *p_stid)
1012 struct nfs4_cpntf_state *cps;
1014 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
1017 cps->cpntf_time = ktime_get_boottime_seconds();
1018 refcount_set(&cps->cp_stateid.cs_count, 1);
1019 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
1021 spin_lock(&nn->s2s_cp_lock);
1022 list_add(&cps->cp_list, &p_stid->sc_cp_list);
1023 spin_unlock(&nn->s2s_cp_lock);
1030 void nfs4_free_copy_state(struct nfsd4_copy *copy)
1032 struct nfsd_net *nn;
1034 if (copy->cp_stateid.cs_type != NFS4_COPY_STID)
1036 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
1037 spin_lock(&nn->s2s_cp_lock);
1038 idr_remove(&nn->s2s_cp_stateids,
1039 copy->cp_stateid.cs_stid.si_opaque.so_id);
1040 spin_unlock(&nn->s2s_cp_lock);
1043 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1045 struct nfs4_cpntf_state *cps;
1046 struct nfsd_net *nn;
1048 nn = net_generic(net, nfsd_net_id);
1049 spin_lock(&nn->s2s_cp_lock);
1050 while (!list_empty(&stid->sc_cp_list)) {
1051 cps = list_first_entry(&stid->sc_cp_list,
1052 struct nfs4_cpntf_state, cp_list);
1053 _free_cpntf_state_locked(nn, cps);
1055 spin_unlock(&nn->s2s_cp_lock);
1058 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
1060 struct nfs4_stid *stid;
1062 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
1066 return openlockstateid(stid);
1069 static void nfs4_free_deleg(struct nfs4_stid *stid)
1071 struct nfs4_delegation *dp = delegstateid(stid);
1073 WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
1074 WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
1075 WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
1076 WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
1077 kmem_cache_free(deleg_slab, stid);
1078 atomic_long_dec(&num_delegations);
1082 * When we recall a delegation, we should be careful not to hand it
1083 * out again straight away.
1084 * To ensure this we keep a pair of bloom filters ('new' and 'old')
1085 * in which the filehandles of recalled delegations are "stored".
1086 * If a filehandle appear in either filter, a delegation is blocked.
1087 * When a delegation is recalled, the filehandle is stored in the "new"
1089 * Every 30 seconds we swap the filters and clear the "new" one,
1090 * unless both are empty of course.
1092 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
1093 * low 3 bytes as hash-table indices.
1095 * 'blocked_delegations_lock', which is always taken in block_delegations(),
1096 * is used to manage concurrent access. Testing does not need the lock
1097 * except when swapping the two filters.
1099 static DEFINE_SPINLOCK(blocked_delegations_lock);
1100 static struct bloom_pair {
1101 int entries, old_entries;
1103 int new; /* index into 'set' */
1104 DECLARE_BITMAP(set[2], 256);
1105 } blocked_delegations;
1107 static int delegation_blocked(struct knfsd_fh *fh)
1110 struct bloom_pair *bd = &blocked_delegations;
1112 if (bd->entries == 0)
1114 if (ktime_get_seconds() - bd->swap_time > 30) {
1115 spin_lock(&blocked_delegations_lock);
1116 if (ktime_get_seconds() - bd->swap_time > 30) {
1117 bd->entries -= bd->old_entries;
1118 bd->old_entries = bd->entries;
1119 memset(bd->set[bd->new], 0,
1120 sizeof(bd->set[0]));
1121 bd->new = 1-bd->new;
1122 bd->swap_time = ktime_get_seconds();
1124 spin_unlock(&blocked_delegations_lock);
1126 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1127 if (test_bit(hash&255, bd->set[0]) &&
1128 test_bit((hash>>8)&255, bd->set[0]) &&
1129 test_bit((hash>>16)&255, bd->set[0]))
1132 if (test_bit(hash&255, bd->set[1]) &&
1133 test_bit((hash>>8)&255, bd->set[1]) &&
1134 test_bit((hash>>16)&255, bd->set[1]))
1140 static void block_delegations(struct knfsd_fh *fh)
1143 struct bloom_pair *bd = &blocked_delegations;
1145 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1147 spin_lock(&blocked_delegations_lock);
1148 __set_bit(hash&255, bd->set[bd->new]);
1149 __set_bit((hash>>8)&255, bd->set[bd->new]);
1150 __set_bit((hash>>16)&255, bd->set[bd->new]);
1151 if (bd->entries == 0)
1152 bd->swap_time = ktime_get_seconds();
1154 spin_unlock(&blocked_delegations_lock);
1157 static struct nfs4_delegation *
1158 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1159 struct nfs4_clnt_odstate *odstate, u32 dl_type)
1161 struct nfs4_delegation *dp;
1162 struct nfs4_stid *stid;
1165 dprintk("NFSD alloc_init_deleg\n");
1166 n = atomic_long_inc_return(&num_delegations);
1167 if (n < 0 || n > max_delegations)
1169 if (delegation_blocked(&fp->fi_fhandle))
1171 stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
1174 dp = delegstateid(stid);
1177 * delegation seqid's are never incremented. The 4.1 special
1178 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1179 * 0 anyway just for consistency and use 1:
1181 dp->dl_stid.sc_stateid.si_generation = 1;
1182 INIT_LIST_HEAD(&dp->dl_perfile);
1183 INIT_LIST_HEAD(&dp->dl_perclnt);
1184 INIT_LIST_HEAD(&dp->dl_recall_lru);
1185 dp->dl_clnt_odstate = odstate;
1186 get_clnt_odstate(odstate);
1187 dp->dl_type = dl_type;
1189 dp->dl_recalled = false;
1190 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1191 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1193 dp->dl_stid.sc_file = fp;
1196 atomic_long_dec(&num_delegations);
1201 nfs4_put_stid(struct nfs4_stid *s)
1203 struct nfs4_file *fp = s->sc_file;
1204 struct nfs4_client *clp = s->sc_client;
1206 might_lock(&clp->cl_lock);
1208 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1209 wake_up_all(&close_wq);
1212 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1213 nfs4_free_cpntf_statelist(clp->net, s);
1214 spin_unlock(&clp->cl_lock);
1221 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1223 stateid_t *src = &stid->sc_stateid;
1225 spin_lock(&stid->sc_lock);
1226 if (unlikely(++src->si_generation == 0))
1227 src->si_generation = 1;
1228 memcpy(dst, src, sizeof(*dst));
1229 spin_unlock(&stid->sc_lock);
1232 static void put_deleg_file(struct nfs4_file *fp)
1234 struct nfsd_file *nf = NULL;
1236 spin_lock(&fp->fi_lock);
1237 if (--fp->fi_delegees == 0)
1238 swap(nf, fp->fi_deleg_file);
1239 spin_unlock(&fp->fi_lock);
1245 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1247 struct nfs4_file *fp = dp->dl_stid.sc_file;
1248 struct nfsd_file *nf = fp->fi_deleg_file;
1250 WARN_ON_ONCE(!fp->fi_delegees);
1252 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1256 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1258 put_clnt_odstate(dp->dl_clnt_odstate);
1259 nfs4_unlock_deleg_lease(dp);
1260 nfs4_put_stid(&dp->dl_stid);
1263 void nfs4_unhash_stid(struct nfs4_stid *s)
1269 * nfs4_delegation_exists - Discover if this delegation already exists
1270 * @clp: a pointer to the nfs4_client we're granting a delegation to
1271 * @fp: a pointer to the nfs4_file we're granting a delegation on
1274 * On success: true iff an existing delegation is found
1278 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1280 struct nfs4_delegation *searchdp = NULL;
1281 struct nfs4_client *searchclp = NULL;
1283 lockdep_assert_held(&state_lock);
1284 lockdep_assert_held(&fp->fi_lock);
1286 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1287 searchclp = searchdp->dl_stid.sc_client;
1288 if (clp == searchclp) {
1296 * hash_delegation_locked - Add a delegation to the appropriate lists
1297 * @dp: a pointer to the nfs4_delegation we are adding.
1298 * @fp: a pointer to the nfs4_file we're granting a delegation on
1301 * On success: NULL if the delegation was successfully hashed.
1303 * On error: -EAGAIN if one was previously granted to this
1304 * nfs4_client for this nfs4_file. Delegation is not hashed.
1309 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1311 struct nfs4_client *clp = dp->dl_stid.sc_client;
1313 lockdep_assert_held(&state_lock);
1314 lockdep_assert_held(&fp->fi_lock);
1316 if (nfs4_delegation_exists(clp, fp))
1318 refcount_inc(&dp->dl_stid.sc_count);
1319 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1320 list_add(&dp->dl_perfile, &fp->fi_delegations);
1321 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1325 static bool delegation_hashed(struct nfs4_delegation *dp)
1327 return !(list_empty(&dp->dl_perfile));
1331 unhash_delegation_locked(struct nfs4_delegation *dp)
1333 struct nfs4_file *fp = dp->dl_stid.sc_file;
1335 lockdep_assert_held(&state_lock);
1337 if (!delegation_hashed(dp))
1340 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1341 /* Ensure that deleg break won't try to requeue it */
1343 spin_lock(&fp->fi_lock);
1344 list_del_init(&dp->dl_perclnt);
1345 list_del_init(&dp->dl_recall_lru);
1346 list_del_init(&dp->dl_perfile);
1347 spin_unlock(&fp->fi_lock);
1351 static void destroy_delegation(struct nfs4_delegation *dp)
1355 spin_lock(&state_lock);
1356 unhashed = unhash_delegation_locked(dp);
1357 spin_unlock(&state_lock);
1359 destroy_unhashed_deleg(dp);
1362 static void revoke_delegation(struct nfs4_delegation *dp)
1364 struct nfs4_client *clp = dp->dl_stid.sc_client;
1366 WARN_ON(!list_empty(&dp->dl_recall_lru));
1368 trace_nfsd_stid_revoke(&dp->dl_stid);
1370 if (clp->cl_minorversion) {
1371 spin_lock(&clp->cl_lock);
1372 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1373 refcount_inc(&dp->dl_stid.sc_count);
1374 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1375 spin_unlock(&clp->cl_lock);
1377 destroy_unhashed_deleg(dp);
1384 static unsigned int clientid_hashval(u32 id)
1386 return id & CLIENT_HASH_MASK;
1389 static unsigned int clientstr_hashval(struct xdr_netobj name)
1391 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1395 * A stateid that had a deny mode associated with it is being released
1396 * or downgraded. Recalculate the deny mode on the file.
1399 recalculate_deny_mode(struct nfs4_file *fp)
1401 struct nfs4_ol_stateid *stp;
1403 spin_lock(&fp->fi_lock);
1404 fp->fi_share_deny = 0;
1405 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1406 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1407 spin_unlock(&fp->fi_lock);
1411 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1414 bool change = false;
1416 for (i = 1; i < 4; i++) {
1417 if ((i & deny) != i) {
1423 /* Recalculate per-file deny mode if there was a change */
1425 recalculate_deny_mode(stp->st_stid.sc_file);
1428 /* release all access and file references for a given stateid */
1430 release_all_access(struct nfs4_ol_stateid *stp)
1433 struct nfs4_file *fp = stp->st_stid.sc_file;
1435 if (fp && stp->st_deny_bmap != 0)
1436 recalculate_deny_mode(fp);
1438 for (i = 1; i < 4; i++) {
1439 if (test_access(i, stp))
1440 nfs4_file_put_access(stp->st_stid.sc_file, i);
1441 clear_access(i, stp);
1445 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1447 kfree(sop->so_owner.data);
1448 sop->so_ops->so_free(sop);
1451 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1453 struct nfs4_client *clp = sop->so_client;
1455 might_lock(&clp->cl_lock);
1457 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1459 sop->so_ops->so_unhash(sop);
1460 spin_unlock(&clp->cl_lock);
1461 nfs4_free_stateowner(sop);
1465 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1467 return list_empty(&stp->st_perfile);
1470 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1472 struct nfs4_file *fp = stp->st_stid.sc_file;
1474 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1476 if (list_empty(&stp->st_perfile))
1479 spin_lock(&fp->fi_lock);
1480 list_del_init(&stp->st_perfile);
1481 spin_unlock(&fp->fi_lock);
1482 list_del(&stp->st_perstateowner);
1486 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1488 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1490 put_clnt_odstate(stp->st_clnt_odstate);
1491 release_all_access(stp);
1492 if (stp->st_stateowner)
1493 nfs4_put_stateowner(stp->st_stateowner);
1494 WARN_ON(!list_empty(&stid->sc_cp_list));
1495 kmem_cache_free(stateid_slab, stid);
1498 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1500 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1501 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1502 struct nfsd_file *nf;
1504 nf = find_any_file(stp->st_stid.sc_file);
1506 get_file(nf->nf_file);
1507 filp_close(nf->nf_file, (fl_owner_t)lo);
1510 nfs4_free_ol_stateid(stid);
1514 * Put the persistent reference to an already unhashed generic stateid, while
1515 * holding the cl_lock. If it's the last reference, then put it onto the
1516 * reaplist for later destruction.
1518 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1519 struct list_head *reaplist)
1521 struct nfs4_stid *s = &stp->st_stid;
1522 struct nfs4_client *clp = s->sc_client;
1524 lockdep_assert_held(&clp->cl_lock);
1526 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1528 if (!refcount_dec_and_test(&s->sc_count)) {
1529 wake_up_all(&close_wq);
1533 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1534 list_add(&stp->st_locks, reaplist);
1537 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1539 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1541 if (!unhash_ol_stateid(stp))
1543 list_del_init(&stp->st_locks);
1544 nfs4_unhash_stid(&stp->st_stid);
1548 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1550 struct nfs4_client *clp = stp->st_stid.sc_client;
1553 spin_lock(&clp->cl_lock);
1554 unhashed = unhash_lock_stateid(stp);
1555 spin_unlock(&clp->cl_lock);
1557 nfs4_put_stid(&stp->st_stid);
1560 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1562 struct nfs4_client *clp = lo->lo_owner.so_client;
1564 lockdep_assert_held(&clp->cl_lock);
1566 list_del_init(&lo->lo_owner.so_strhash);
1570 * Free a list of generic stateids that were collected earlier after being
1574 free_ol_stateid_reaplist(struct list_head *reaplist)
1576 struct nfs4_ol_stateid *stp;
1577 struct nfs4_file *fp;
1581 while (!list_empty(reaplist)) {
1582 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1584 list_del(&stp->st_locks);
1585 fp = stp->st_stid.sc_file;
1586 stp->st_stid.sc_free(&stp->st_stid);
1592 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1593 struct list_head *reaplist)
1595 struct nfs4_ol_stateid *stp;
1597 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1599 while (!list_empty(&open_stp->st_locks)) {
1600 stp = list_entry(open_stp->st_locks.next,
1601 struct nfs4_ol_stateid, st_locks);
1602 WARN_ON(!unhash_lock_stateid(stp));
1603 put_ol_stateid_locked(stp, reaplist);
1607 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1608 struct list_head *reaplist)
1610 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1612 if (!unhash_ol_stateid(stp))
1614 release_open_stateid_locks(stp, reaplist);
1618 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1620 LIST_HEAD(reaplist);
1622 spin_lock(&stp->st_stid.sc_client->cl_lock);
1623 if (unhash_open_stateid(stp, &reaplist))
1624 put_ol_stateid_locked(stp, &reaplist);
1625 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1626 free_ol_stateid_reaplist(&reaplist);
1629 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1631 struct nfs4_client *clp = oo->oo_owner.so_client;
1633 lockdep_assert_held(&clp->cl_lock);
1635 list_del_init(&oo->oo_owner.so_strhash);
1636 list_del_init(&oo->oo_perclient);
1639 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1641 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1643 struct nfs4_ol_stateid *s;
1645 spin_lock(&nn->client_lock);
1646 s = oo->oo_last_closed_stid;
1648 list_del_init(&oo->oo_close_lru);
1649 oo->oo_last_closed_stid = NULL;
1651 spin_unlock(&nn->client_lock);
1653 nfs4_put_stid(&s->st_stid);
1656 static void release_openowner(struct nfs4_openowner *oo)
1658 struct nfs4_ol_stateid *stp;
1659 struct nfs4_client *clp = oo->oo_owner.so_client;
1660 struct list_head reaplist;
1662 INIT_LIST_HEAD(&reaplist);
1664 spin_lock(&clp->cl_lock);
1665 unhash_openowner_locked(oo);
1666 while (!list_empty(&oo->oo_owner.so_stateids)) {
1667 stp = list_first_entry(&oo->oo_owner.so_stateids,
1668 struct nfs4_ol_stateid, st_perstateowner);
1669 if (unhash_open_stateid(stp, &reaplist))
1670 put_ol_stateid_locked(stp, &reaplist);
1672 spin_unlock(&clp->cl_lock);
1673 free_ol_stateid_reaplist(&reaplist);
1674 release_last_closed_stateid(oo);
1675 nfs4_put_stateowner(&oo->oo_owner);
1679 hash_sessionid(struct nfs4_sessionid *sessionid)
1681 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1683 return sid->sequence % SESSION_HASH_SIZE;
1686 #ifdef CONFIG_SUNRPC_DEBUG
1688 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1690 u32 *ptr = (u32 *)(&sessionid->data[0]);
1691 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1695 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1701 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1702 * won't be used for replay.
1704 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1706 struct nfs4_stateowner *so = cstate->replay_owner;
1708 if (nfserr == nfserr_replay_me)
1711 if (!seqid_mutating_err(ntohl(nfserr))) {
1712 nfsd4_cstate_clear_replay(cstate);
1717 if (so->so_is_open_owner)
1718 release_last_closed_stateid(openowner(so));
1724 gen_sessionid(struct nfsd4_session *ses)
1726 struct nfs4_client *clp = ses->se_client;
1727 struct nfsd4_sessionid *sid;
1729 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1730 sid->clientid = clp->cl_clientid;
1731 sid->sequence = current_sessionid++;
1736 * The protocol defines ca_maxresponssize_cached to include the size of
1737 * the rpc header, but all we need to cache is the data starting after
1738 * the end of the initial SEQUENCE operation--the rest we regenerate
1739 * each time. Therefore we can advertise a ca_maxresponssize_cached
1740 * value that is the number of bytes in our cache plus a few additional
1741 * bytes. In order to stay on the safe side, and not promise more than
1742 * we can cache, those additional bytes must be the minimum possible: 24
1743 * bytes of rpc header (xid through accept state, with AUTH_NULL
1744 * verifier), 12 for the compound header (with zero-length tag), and 44
1745 * for the SEQUENCE op response:
1747 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1750 free_session_slots(struct nfsd4_session *ses)
1754 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1755 free_svc_cred(&ses->se_slots[i]->sl_cred);
1756 kfree(ses->se_slots[i]);
1761 * We don't actually need to cache the rpc and session headers, so we
1762 * can allocate a little less for each slot:
1764 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1768 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1771 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1772 return size + sizeof(struct nfsd4_slot);
1776 * XXX: If we run out of reserved DRC memory we could (up to a point)
1777 * re-negotiate active sessions and reduce their slot usage to make
1778 * room for new connections. For now we just fail the create session.
1780 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1782 u32 slotsize = slot_bytes(ca);
1783 u32 num = ca->maxreqs;
1784 unsigned long avail, total_avail;
1785 unsigned int scale_factor;
1787 spin_lock(&nfsd_drc_lock);
1788 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1789 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1791 /* We have handed out more space than we chose in
1792 * set_max_drc() to allow. That isn't really a
1793 * problem as long as that doesn't make us think we
1794 * have lots more due to integer overflow.
1797 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1799 * Never use more than a fraction of the remaining memory,
1800 * unless it's the only way to give this client a slot.
1801 * The chosen fraction is either 1/8 or 1/number of threads,
1802 * whichever is smaller. This ensures there are adequate
1803 * slots to support multiple clients per thread.
1804 * Give the client one slot even if that would require
1805 * over-allocation--it is better than failure.
1807 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1809 avail = clamp_t(unsigned long, avail, slotsize,
1810 total_avail/scale_factor);
1811 num = min_t(int, num, avail / slotsize);
1812 num = max_t(int, num, 1);
1813 nfsd_drc_mem_used += num * slotsize;
1814 spin_unlock(&nfsd_drc_lock);
1819 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1821 int slotsize = slot_bytes(ca);
1823 spin_lock(&nfsd_drc_lock);
1824 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1825 spin_unlock(&nfsd_drc_lock);
1828 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1829 struct nfsd4_channel_attrs *battrs)
1831 int numslots = fattrs->maxreqs;
1832 int slotsize = slot_bytes(fattrs);
1833 struct nfsd4_session *new;
1836 BUILD_BUG_ON(struct_size(new, se_slots, NFSD_MAX_SLOTS_PER_SESSION)
1839 new = kzalloc(struct_size(new, se_slots, numslots), GFP_KERNEL);
1842 /* allocate each struct nfsd4_slot and data cache in one piece */
1843 for (i = 0; i < numslots; i++) {
1844 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1845 if (!new->se_slots[i])
1849 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1850 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1855 kfree(new->se_slots[i]);
1860 static void free_conn(struct nfsd4_conn *c)
1862 svc_xprt_put(c->cn_xprt);
1866 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1868 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1869 struct nfs4_client *clp = c->cn_session->se_client;
1871 trace_nfsd_cb_lost(clp);
1873 spin_lock(&clp->cl_lock);
1874 if (!list_empty(&c->cn_persession)) {
1875 list_del(&c->cn_persession);
1878 nfsd4_probe_callback(clp);
1879 spin_unlock(&clp->cl_lock);
1882 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1884 struct nfsd4_conn *conn;
1886 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1889 svc_xprt_get(rqstp->rq_xprt);
1890 conn->cn_xprt = rqstp->rq_xprt;
1891 conn->cn_flags = flags;
1892 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1896 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1898 conn->cn_session = ses;
1899 list_add(&conn->cn_persession, &ses->se_conns);
1902 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1904 struct nfs4_client *clp = ses->se_client;
1906 spin_lock(&clp->cl_lock);
1907 __nfsd4_hash_conn(conn, ses);
1908 spin_unlock(&clp->cl_lock);
1911 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1913 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1914 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1917 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1921 nfsd4_hash_conn(conn, ses);
1922 ret = nfsd4_register_conn(conn);
1924 /* oops; xprt is already down: */
1925 nfsd4_conn_lost(&conn->cn_xpt_user);
1926 /* We may have gained or lost a callback channel: */
1927 nfsd4_probe_callback_sync(ses->se_client);
1930 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1932 u32 dir = NFS4_CDFC4_FORE;
1934 if (cses->flags & SESSION4_BACK_CHAN)
1935 dir |= NFS4_CDFC4_BACK;
1936 return alloc_conn(rqstp, dir);
1939 /* must be called under client_lock */
1940 static void nfsd4_del_conns(struct nfsd4_session *s)
1942 struct nfs4_client *clp = s->se_client;
1943 struct nfsd4_conn *c;
1945 spin_lock(&clp->cl_lock);
1946 while (!list_empty(&s->se_conns)) {
1947 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1948 list_del_init(&c->cn_persession);
1949 spin_unlock(&clp->cl_lock);
1951 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1954 spin_lock(&clp->cl_lock);
1956 spin_unlock(&clp->cl_lock);
1959 static void __free_session(struct nfsd4_session *ses)
1961 free_session_slots(ses);
1965 static void free_session(struct nfsd4_session *ses)
1967 nfsd4_del_conns(ses);
1968 nfsd4_put_drc_mem(&ses->se_fchannel);
1969 __free_session(ses);
1972 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1975 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1977 new->se_client = clp;
1980 INIT_LIST_HEAD(&new->se_conns);
1982 new->se_cb_seq_nr = 1;
1983 new->se_flags = cses->flags;
1984 new->se_cb_prog = cses->callback_prog;
1985 new->se_cb_sec = cses->cb_sec;
1986 atomic_set(&new->se_ref, 0);
1987 idx = hash_sessionid(&new->se_sessionid);
1988 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1989 spin_lock(&clp->cl_lock);
1990 list_add(&new->se_perclnt, &clp->cl_sessions);
1991 spin_unlock(&clp->cl_lock);
1994 struct sockaddr *sa = svc_addr(rqstp);
1996 * This is a little silly; with sessions there's no real
1997 * use for the callback address. Use the peer address
1998 * as a reasonable default for now, but consider fixing
1999 * the rpc client not to require an address in the
2002 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2003 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2007 /* caller must hold client_lock */
2008 static struct nfsd4_session *
2009 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2011 struct nfsd4_session *elem;
2013 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2015 lockdep_assert_held(&nn->client_lock);
2017 dump_sessionid(__func__, sessionid);
2018 idx = hash_sessionid(sessionid);
2019 /* Search in the appropriate list */
2020 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2021 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2022 NFS4_MAX_SESSIONID_LEN)) {
2027 dprintk("%s: session not found\n", __func__);
2031 static struct nfsd4_session *
2032 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2035 struct nfsd4_session *session;
2036 __be32 status = nfserr_badsession;
2038 session = __find_in_sessionid_hashtbl(sessionid, net);
2041 status = nfsd4_get_session_locked(session);
2049 /* caller must hold client_lock */
2051 unhash_session(struct nfsd4_session *ses)
2053 struct nfs4_client *clp = ses->se_client;
2054 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2056 lockdep_assert_held(&nn->client_lock);
2058 list_del(&ses->se_hash);
2059 spin_lock(&ses->se_client->cl_lock);
2060 list_del(&ses->se_perclnt);
2061 spin_unlock(&ses->se_client->cl_lock);
2064 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2066 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2069 * We're assuming the clid was not given out from a boot
2070 * precisely 2^32 (about 136 years) before this one. That seems
2071 * a safe assumption:
2073 if (clid->cl_boot == (u32)nn->boot_time)
2075 trace_nfsd_clid_stale(clid);
2080 * XXX Should we use a slab cache ?
2081 * This type of memory management is somewhat inefficient, but we use it
2082 * anyway since SETCLIENTID is not a common operation.
2084 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2085 struct nfsd_net *nn)
2087 struct nfs4_client *clp;
2090 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
2091 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2094 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2097 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2098 if (clp->cl_name.data == NULL)
2100 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2101 sizeof(struct list_head),
2103 if (!clp->cl_ownerstr_hashtbl)
2104 goto err_no_hashtbl;
2105 for (i = 0; i < OWNER_HASH_SIZE; i++)
2106 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2107 INIT_LIST_HEAD(&clp->cl_sessions);
2108 idr_init(&clp->cl_stateids);
2109 atomic_set(&clp->cl_rpc_users, 0);
2110 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2111 clp->cl_state = NFSD4_ACTIVE;
2112 atomic_inc(&nn->nfs4_client_count);
2113 atomic_set(&clp->cl_delegs_in_recall, 0);
2114 INIT_LIST_HEAD(&clp->cl_idhash);
2115 INIT_LIST_HEAD(&clp->cl_openowners);
2116 INIT_LIST_HEAD(&clp->cl_delegations);
2117 INIT_LIST_HEAD(&clp->cl_lru);
2118 INIT_LIST_HEAD(&clp->cl_revoked);
2119 #ifdef CONFIG_NFSD_PNFS
2120 INIT_LIST_HEAD(&clp->cl_lo_states);
2122 INIT_LIST_HEAD(&clp->async_copies);
2123 spin_lock_init(&clp->async_lock);
2124 spin_lock_init(&clp->cl_lock);
2125 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2128 kfree(clp->cl_name.data);
2130 kmem_cache_free(client_slab, clp);
2134 static void __free_client(struct kref *k)
2136 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2137 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2139 free_svc_cred(&clp->cl_cred);
2140 kfree(clp->cl_ownerstr_hashtbl);
2141 kfree(clp->cl_name.data);
2142 kfree(clp->cl_nii_domain.data);
2143 kfree(clp->cl_nii_name.data);
2144 idr_destroy(&clp->cl_stateids);
2146 kmem_cache_free(client_slab, clp);
2149 static void drop_client(struct nfs4_client *clp)
2151 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2155 free_client(struct nfs4_client *clp)
2157 while (!list_empty(&clp->cl_sessions)) {
2158 struct nfsd4_session *ses;
2159 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2161 list_del(&ses->se_perclnt);
2162 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2165 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2166 if (clp->cl_nfsd_dentry) {
2167 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2168 clp->cl_nfsd_dentry = NULL;
2169 wake_up_all(&expiry_wq);
2174 /* must be called under the client_lock */
2176 unhash_client_locked(struct nfs4_client *clp)
2178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2179 struct nfsd4_session *ses;
2181 lockdep_assert_held(&nn->client_lock);
2183 /* Mark the client as expired! */
2185 /* Make it invisible */
2186 if (!list_empty(&clp->cl_idhash)) {
2187 list_del_init(&clp->cl_idhash);
2188 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2189 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2191 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2193 list_del_init(&clp->cl_lru);
2194 spin_lock(&clp->cl_lock);
2195 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2196 list_del_init(&ses->se_hash);
2197 spin_unlock(&clp->cl_lock);
2201 unhash_client(struct nfs4_client *clp)
2203 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2205 spin_lock(&nn->client_lock);
2206 unhash_client_locked(clp);
2207 spin_unlock(&nn->client_lock);
2210 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2212 if (atomic_read(&clp->cl_rpc_users))
2213 return nfserr_jukebox;
2214 unhash_client_locked(clp);
2219 __destroy_client(struct nfs4_client *clp)
2221 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2223 struct nfs4_openowner *oo;
2224 struct nfs4_delegation *dp;
2225 struct list_head reaplist;
2227 INIT_LIST_HEAD(&reaplist);
2228 spin_lock(&state_lock);
2229 while (!list_empty(&clp->cl_delegations)) {
2230 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2231 WARN_ON(!unhash_delegation_locked(dp));
2232 list_add(&dp->dl_recall_lru, &reaplist);
2234 spin_unlock(&state_lock);
2235 while (!list_empty(&reaplist)) {
2236 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2237 list_del_init(&dp->dl_recall_lru);
2238 destroy_unhashed_deleg(dp);
2240 while (!list_empty(&clp->cl_revoked)) {
2241 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2242 list_del_init(&dp->dl_recall_lru);
2243 nfs4_put_stid(&dp->dl_stid);
2245 while (!list_empty(&clp->cl_openowners)) {
2246 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2247 nfs4_get_stateowner(&oo->oo_owner);
2248 release_openowner(oo);
2250 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2251 struct nfs4_stateowner *so, *tmp;
2253 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2255 /* Should be no openowners at this point */
2256 WARN_ON_ONCE(so->so_is_open_owner);
2257 remove_blocked_locks(lockowner(so));
2260 nfsd4_return_all_client_layouts(clp);
2261 nfsd4_shutdown_copy(clp);
2262 nfsd4_shutdown_callback(clp);
2263 if (clp->cl_cb_conn.cb_xprt)
2264 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2265 atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2266 nfsd4_dec_courtesy_client_count(nn, clp);
2268 wake_up_all(&expiry_wq);
2272 destroy_client(struct nfs4_client *clp)
2275 __destroy_client(clp);
2278 static void inc_reclaim_complete(struct nfs4_client *clp)
2280 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2282 if (!nn->track_reclaim_completes)
2284 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2286 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2287 nn->reclaim_str_hashtbl_size) {
2288 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2290 nfsd4_end_grace(nn);
2294 static void expire_client(struct nfs4_client *clp)
2297 nfsd4_client_record_remove(clp);
2298 __destroy_client(clp);
2301 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2303 memcpy(target->cl_verifier.data, source->data,
2304 sizeof(target->cl_verifier.data));
2307 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2309 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2310 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2313 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2315 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2316 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2318 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2319 if ((source->cr_principal && !target->cr_principal) ||
2320 (source->cr_raw_principal && !target->cr_raw_principal) ||
2321 (source->cr_targ_princ && !target->cr_targ_princ))
2324 target->cr_flavor = source->cr_flavor;
2325 target->cr_uid = source->cr_uid;
2326 target->cr_gid = source->cr_gid;
2327 target->cr_group_info = source->cr_group_info;
2328 get_group_info(target->cr_group_info);
2329 target->cr_gss_mech = source->cr_gss_mech;
2330 if (source->cr_gss_mech)
2331 gss_mech_get(source->cr_gss_mech);
2336 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2338 if (o1->len < o2->len)
2340 if (o1->len > o2->len)
2342 return memcmp(o1->data, o2->data, o1->len);
2346 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2348 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2352 same_clid(clientid_t *cl1, clientid_t *cl2)
2354 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2357 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2361 if (g1->ngroups != g2->ngroups)
2363 for (i=0; i<g1->ngroups; i++)
2364 if (!gid_eq(g1->gid[i], g2->gid[i]))
2370 * RFC 3530 language requires clid_inuse be returned when the
2371 * "principal" associated with a requests differs from that previously
2372 * used. We use uid, gid's, and gss principal string as our best
2373 * approximation. We also don't want to allow non-gss use of a client
2374 * established using gss: in theory cr_principal should catch that
2375 * change, but in practice cr_principal can be null even in the gss case
2376 * since gssd doesn't always pass down a principal string.
2378 static bool is_gss_cred(struct svc_cred *cr)
2380 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2381 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2386 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2388 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2389 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2390 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2391 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2393 /* XXX: check that cr_targ_princ fields match ? */
2394 if (cr1->cr_principal == cr2->cr_principal)
2396 if (!cr1->cr_principal || !cr2->cr_principal)
2398 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2401 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2403 struct svc_cred *cr = &rqstp->rq_cred;
2406 if (!cr->cr_gss_mech)
2408 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2409 return service == RPC_GSS_SVC_INTEGRITY ||
2410 service == RPC_GSS_SVC_PRIVACY;
2413 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2415 struct svc_cred *cr = &rqstp->rq_cred;
2417 if (!cl->cl_mach_cred)
2419 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2421 if (!svc_rqst_integrity_protected(rqstp))
2423 if (cl->cl_cred.cr_raw_principal)
2424 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2425 cr->cr_raw_principal);
2426 if (!cr->cr_principal)
2428 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2431 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2436 * This is opaque to client, so no need to byte-swap. Use
2437 * __force to keep sparse happy
2439 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2440 verf[1] = (__force __be32)nn->clverifier_counter++;
2441 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2444 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2446 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2447 clp->cl_clientid.cl_id = nn->clientid_counter++;
2448 gen_confirm(clp, nn);
2451 static struct nfs4_stid *
2452 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2454 struct nfs4_stid *ret;
2456 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2457 if (!ret || !ret->sc_type)
2462 static struct nfs4_stid *
2463 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2465 struct nfs4_stid *s;
2467 spin_lock(&cl->cl_lock);
2468 s = find_stateid_locked(cl, t);
2470 if (typemask & s->sc_type)
2471 refcount_inc(&s->sc_count);
2475 spin_unlock(&cl->cl_lock);
2479 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2481 struct nfsdfs_client *nc;
2482 nc = get_nfsdfs_client(inode);
2485 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2488 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2490 seq_printf(m, "\"");
2491 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2492 seq_printf(m, "\"");
2495 static const char *cb_state2str(int state)
2500 case NFSD4_CB_UNKNOWN:
2504 case NFSD4_CB_FAULT:
2510 static int client_info_show(struct seq_file *m, void *v)
2512 struct inode *inode = file_inode(m->file);
2513 struct nfs4_client *clp;
2516 clp = get_nfsdfs_clp(inode);
2519 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2520 seq_printf(m, "clientid: 0x%llx\n", clid);
2521 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2523 if (clp->cl_state == NFSD4_COURTESY)
2524 seq_puts(m, "status: courtesy\n");
2525 else if (clp->cl_state == NFSD4_EXPIRABLE)
2526 seq_puts(m, "status: expirable\n");
2527 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2528 seq_puts(m, "status: confirmed\n");
2530 seq_puts(m, "status: unconfirmed\n");
2531 seq_printf(m, "seconds from last renew: %lld\n",
2532 ktime_get_boottime_seconds() - clp->cl_time);
2533 seq_printf(m, "name: ");
2534 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2535 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2536 if (clp->cl_nii_domain.data) {
2537 seq_printf(m, "Implementation domain: ");
2538 seq_quote_mem(m, clp->cl_nii_domain.data,
2539 clp->cl_nii_domain.len);
2540 seq_printf(m, "\nImplementation name: ");
2541 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2542 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2543 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2545 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2546 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
2552 DEFINE_SHOW_ATTRIBUTE(client_info);
2554 static void *states_start(struct seq_file *s, loff_t *pos)
2555 __acquires(&clp->cl_lock)
2557 struct nfs4_client *clp = s->private;
2558 unsigned long id = *pos;
2561 spin_lock(&clp->cl_lock);
2562 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2567 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2569 struct nfs4_client *clp = s->private;
2570 unsigned long id = *pos;
2575 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2580 static void states_stop(struct seq_file *s, void *v)
2581 __releases(&clp->cl_lock)
2583 struct nfs4_client *clp = s->private;
2585 spin_unlock(&clp->cl_lock);
2588 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2590 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2593 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2595 struct inode *inode = file_inode(f->nf_file);
2597 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2598 MAJOR(inode->i_sb->s_dev),
2599 MINOR(inode->i_sb->s_dev),
2603 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2605 seq_printf(s, "owner: ");
2606 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2609 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2611 seq_printf(s, "0x%.8x", stid->si_generation);
2612 seq_printf(s, "%12phN", &stid->si_opaque);
2615 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2617 struct nfs4_ol_stateid *ols;
2618 struct nfs4_file *nf;
2619 struct nfsd_file *file;
2620 struct nfs4_stateowner *oo;
2621 unsigned int access, deny;
2623 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2624 return 0; /* XXX: or SEQ_SKIP? */
2625 ols = openlockstateid(st);
2626 oo = ols->st_stateowner;
2629 spin_lock(&nf->fi_lock);
2630 file = find_any_file_locked(nf);
2634 seq_printf(s, "- ");
2635 nfs4_show_stateid(s, &st->sc_stateid);
2636 seq_printf(s, ": { type: open, ");
2638 access = bmap_to_share_mode(ols->st_access_bmap);
2639 deny = bmap_to_share_mode(ols->st_deny_bmap);
2641 seq_printf(s, "access: %s%s, ",
2642 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2643 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2644 seq_printf(s, "deny: %s%s, ",
2645 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2646 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2648 nfs4_show_superblock(s, file);
2649 seq_printf(s, ", ");
2650 nfs4_show_fname(s, file);
2651 seq_printf(s, ", ");
2652 nfs4_show_owner(s, oo);
2653 seq_printf(s, " }\n");
2655 spin_unlock(&nf->fi_lock);
2659 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2661 struct nfs4_ol_stateid *ols;
2662 struct nfs4_file *nf;
2663 struct nfsd_file *file;
2664 struct nfs4_stateowner *oo;
2666 ols = openlockstateid(st);
2667 oo = ols->st_stateowner;
2669 spin_lock(&nf->fi_lock);
2670 file = find_any_file_locked(nf);
2674 seq_printf(s, "- ");
2675 nfs4_show_stateid(s, &st->sc_stateid);
2676 seq_printf(s, ": { type: lock, ");
2679 * Note: a lock stateid isn't really the same thing as a lock,
2680 * it's the locking state held by one owner on a file, and there
2681 * may be multiple (or no) lock ranges associated with it.
2682 * (Same for the matter is true of open stateids.)
2685 nfs4_show_superblock(s, file);
2686 /* XXX: open stateid? */
2687 seq_printf(s, ", ");
2688 nfs4_show_fname(s, file);
2689 seq_printf(s, ", ");
2690 nfs4_show_owner(s, oo);
2691 seq_printf(s, " }\n");
2693 spin_unlock(&nf->fi_lock);
2697 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2699 struct nfs4_delegation *ds;
2700 struct nfs4_file *nf;
2701 struct nfsd_file *file;
2703 ds = delegstateid(st);
2705 spin_lock(&nf->fi_lock);
2706 file = nf->fi_deleg_file;
2710 seq_printf(s, "- ");
2711 nfs4_show_stateid(s, &st->sc_stateid);
2712 seq_printf(s, ": { type: deleg, ");
2714 /* Kinda dead code as long as we only support read delegs: */
2715 seq_printf(s, "access: %s, ",
2716 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2718 /* XXX: lease time, whether it's being recalled. */
2720 nfs4_show_superblock(s, file);
2721 seq_printf(s, ", ");
2722 nfs4_show_fname(s, file);
2723 seq_printf(s, " }\n");
2725 spin_unlock(&nf->fi_lock);
2729 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2731 struct nfs4_layout_stateid *ls;
2732 struct nfsd_file *file;
2734 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2737 seq_printf(s, "- ");
2738 nfs4_show_stateid(s, &st->sc_stateid);
2739 seq_printf(s, ": { type: layout, ");
2741 /* XXX: What else would be useful? */
2743 nfs4_show_superblock(s, file);
2744 seq_printf(s, ", ");
2745 nfs4_show_fname(s, file);
2746 seq_printf(s, " }\n");
2751 static int states_show(struct seq_file *s, void *v)
2753 struct nfs4_stid *st = v;
2755 switch (st->sc_type) {
2756 case NFS4_OPEN_STID:
2757 return nfs4_show_open(s, st);
2758 case NFS4_LOCK_STID:
2759 return nfs4_show_lock(s, st);
2760 case NFS4_DELEG_STID:
2761 return nfs4_show_deleg(s, st);
2762 case NFS4_LAYOUT_STID:
2763 return nfs4_show_layout(s, st);
2765 return 0; /* XXX: or SEQ_SKIP? */
2767 /* XXX: copy stateids? */
2770 static struct seq_operations states_seq_ops = {
2771 .start = states_start,
2772 .next = states_next,
2773 .stop = states_stop,
2777 static int client_states_open(struct inode *inode, struct file *file)
2780 struct nfs4_client *clp;
2783 clp = get_nfsdfs_clp(inode);
2787 ret = seq_open(file, &states_seq_ops);
2790 s = file->private_data;
2795 static int client_opens_release(struct inode *inode, struct file *file)
2797 struct seq_file *m = file->private_data;
2798 struct nfs4_client *clp = m->private;
2800 /* XXX: alternatively, we could get/drop in seq start/stop */
2802 return seq_release(inode, file);
2805 static const struct file_operations client_states_fops = {
2806 .open = client_states_open,
2808 .llseek = seq_lseek,
2809 .release = client_opens_release,
2813 * Normally we refuse to destroy clients that are in use, but here the
2814 * administrator is telling us to just do it. We also want to wait
2815 * so the caller has a guarantee that the client's locks are gone by
2816 * the time the write returns:
2818 static void force_expire_client(struct nfs4_client *clp)
2820 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2821 bool already_expired;
2823 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2825 spin_lock(&nn->client_lock);
2827 spin_unlock(&nn->client_lock);
2829 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2830 spin_lock(&nn->client_lock);
2831 already_expired = list_empty(&clp->cl_lru);
2832 if (!already_expired)
2833 unhash_client_locked(clp);
2834 spin_unlock(&nn->client_lock);
2836 if (!already_expired)
2839 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2842 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2843 size_t size, loff_t *pos)
2846 struct nfs4_client *clp;
2848 data = simple_transaction_get(file, buf, size);
2850 return PTR_ERR(data);
2851 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2853 clp = get_nfsdfs_clp(file_inode(file));
2856 force_expire_client(clp);
2861 static const struct file_operations client_ctl_fops = {
2862 .write = client_ctl_write,
2863 .release = simple_transaction_release,
2866 static const struct tree_descr client_files[] = {
2867 [0] = {"info", &client_info_fops, S_IRUSR},
2868 [1] = {"states", &client_states_fops, S_IRUSR},
2869 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
2874 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb,
2875 struct rpc_task *task)
2877 trace_nfsd_cb_recall_any_done(cb, task);
2878 switch (task->tk_status) {
2879 case -NFS4ERR_DELAY:
2880 rpc_delay(task, 2 * HZ);
2888 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb)
2890 struct nfs4_client *clp = cb->cb_clp;
2891 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2893 spin_lock(&nn->client_lock);
2894 clear_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
2895 put_client_renew_locked(clp);
2896 spin_unlock(&nn->client_lock);
2899 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
2900 .done = nfsd4_cb_recall_any_done,
2901 .release = nfsd4_cb_recall_any_release,
2904 static struct nfs4_client *create_client(struct xdr_netobj name,
2905 struct svc_rqst *rqstp, nfs4_verifier *verf)
2907 struct nfs4_client *clp;
2908 struct sockaddr *sa = svc_addr(rqstp);
2910 struct net *net = SVC_NET(rqstp);
2911 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2912 struct dentry *dentries[ARRAY_SIZE(client_files)];
2914 clp = alloc_client(name, nn);
2918 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2924 kref_init(&clp->cl_nfsdfs.cl_ref);
2925 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2926 clp->cl_time = ktime_get_boottime_seconds();
2927 clear_bit(0, &clp->cl_cb_slot_busy);
2928 copy_verf(clp, verf);
2929 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2930 clp->cl_cb_session = NULL;
2932 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2933 nn, &clp->cl_nfsdfs,
2934 clp->cl_clientid.cl_id - nn->clientid_base,
2935 client_files, dentries);
2936 clp->cl_nfsd_info_dentry = dentries[0];
2937 if (!clp->cl_nfsd_dentry) {
2941 clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL);
2946 clp->cl_ra_time = 0;
2947 nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops,
2948 NFSPROC4_CLNT_CB_RECALL_ANY);
2953 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2955 struct rb_node **new = &(root->rb_node), *parent = NULL;
2956 struct nfs4_client *clp;
2959 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2962 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2963 new = &((*new)->rb_left);
2965 new = &((*new)->rb_right);
2968 rb_link_node(&new_clp->cl_namenode, parent, new);
2969 rb_insert_color(&new_clp->cl_namenode, root);
2972 static struct nfs4_client *
2973 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2976 struct rb_node *node = root->rb_node;
2977 struct nfs4_client *clp;
2980 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2981 cmp = compare_blob(&clp->cl_name, name);
2983 node = node->rb_left;
2985 node = node->rb_right;
2993 add_to_unconfirmed(struct nfs4_client *clp)
2995 unsigned int idhashval;
2996 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2998 lockdep_assert_held(&nn->client_lock);
3000 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3001 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
3002 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3003 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3004 renew_client_locked(clp);
3008 move_to_confirmed(struct nfs4_client *clp)
3010 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3011 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3013 lockdep_assert_held(&nn->client_lock);
3015 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
3016 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
3017 add_clp_to_name_tree(clp, &nn->conf_name_tree);
3018 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3019 trace_nfsd_clid_confirmed(&clp->cl_clientid);
3020 renew_client_locked(clp);
3023 static struct nfs4_client *
3024 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
3026 struct nfs4_client *clp;
3027 unsigned int idhashval = clientid_hashval(clid->cl_id);
3029 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
3030 if (same_clid(&clp->cl_clientid, clid)) {
3031 if ((bool)clp->cl_minorversion != sessions)
3033 renew_client_locked(clp);
3040 static struct nfs4_client *
3041 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3043 struct list_head *tbl = nn->conf_id_hashtbl;
3045 lockdep_assert_held(&nn->client_lock);
3046 return find_client_in_id_table(tbl, clid, sessions);
3049 static struct nfs4_client *
3050 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3052 struct list_head *tbl = nn->unconf_id_hashtbl;
3054 lockdep_assert_held(&nn->client_lock);
3055 return find_client_in_id_table(tbl, clid, sessions);
3058 static bool clp_used_exchangeid(struct nfs4_client *clp)
3060 return clp->cl_exchange_flags != 0;
3063 static struct nfs4_client *
3064 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3066 lockdep_assert_held(&nn->client_lock);
3067 return find_clp_in_name_tree(name, &nn->conf_name_tree);
3070 static struct nfs4_client *
3071 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3073 lockdep_assert_held(&nn->client_lock);
3074 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3078 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3080 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3081 struct sockaddr *sa = svc_addr(rqstp);
3082 u32 scopeid = rpc_get_scope_id(sa);
3083 unsigned short expected_family;
3085 /* Currently, we only support tcp and tcp6 for the callback channel */
3086 if (se->se_callback_netid_len == 3 &&
3087 !memcmp(se->se_callback_netid_val, "tcp", 3))
3088 expected_family = AF_INET;
3089 else if (se->se_callback_netid_len == 4 &&
3090 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3091 expected_family = AF_INET6;
3095 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3096 se->se_callback_addr_len,
3097 (struct sockaddr *)&conn->cb_addr,
3098 sizeof(conn->cb_addr));
3100 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3103 if (conn->cb_addr.ss_family == AF_INET6)
3104 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3106 conn->cb_prog = se->se_callback_prog;
3107 conn->cb_ident = se->se_callback_ident;
3108 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3109 trace_nfsd_cb_args(clp, conn);
3112 conn->cb_addr.ss_family = AF_UNSPEC;
3113 conn->cb_addrlen = 0;
3114 trace_nfsd_cb_nodelegs(clp);
3119 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3122 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3124 struct xdr_buf *buf = resp->xdr->buf;
3125 struct nfsd4_slot *slot = resp->cstate.slot;
3128 dprintk("--> %s slot %p\n", __func__, slot);
3130 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3131 slot->sl_opcnt = resp->opcnt;
3132 slot->sl_status = resp->cstate.status;
3133 free_svc_cred(&slot->sl_cred);
3134 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3136 if (!nfsd4_cache_this(resp)) {
3137 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3140 slot->sl_flags |= NFSD4_SLOT_CACHED;
3142 base = resp->cstate.data_offset;
3143 slot->sl_datalen = buf->len - base;
3144 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3145 WARN(1, "%s: sessions DRC could not cache compound\n",
3151 * Encode the replay sequence operation from the slot values.
3152 * If cachethis is FALSE encode the uncached rep error on the next
3153 * operation which sets resp->p and increments resp->opcnt for
3154 * nfs4svc_encode_compoundres.
3158 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3159 struct nfsd4_compoundres *resp)
3161 struct nfsd4_op *op;
3162 struct nfsd4_slot *slot = resp->cstate.slot;
3164 /* Encode the replayed sequence operation */
3165 op = &args->ops[resp->opcnt - 1];
3166 nfsd4_encode_operation(resp, op);
3168 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3170 if (args->opcnt == 1) {
3172 * The original operation wasn't a solo sequence--we
3173 * always cache those--so this retry must not match the
3176 op->status = nfserr_seq_false_retry;
3178 op = &args->ops[resp->opcnt++];
3179 op->status = nfserr_retry_uncached_rep;
3180 nfsd4_encode_operation(resp, op);
3186 * The sequence operation is not cached because we can use the slot and
3190 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3191 struct nfsd4_sequence *seq)
3193 struct nfsd4_slot *slot = resp->cstate.slot;
3194 struct xdr_stream *xdr = resp->xdr;
3198 dprintk("--> %s slot %p\n", __func__, slot);
3200 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3204 p = xdr_reserve_space(xdr, slot->sl_datalen);
3207 return nfserr_serverfault;
3209 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3210 xdr_commit_encode(xdr);
3212 resp->opcnt = slot->sl_opcnt;
3213 return slot->sl_status;
3217 * Set the exchange_id flags returned by the server.
3220 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3222 #ifdef CONFIG_NFSD_PNFS
3223 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3225 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3228 /* Referrals are supported, Migration is not. */
3229 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3231 /* set the wire flags to return to client. */
3232 clid->flags = new->cl_exchange_flags;
3235 static bool client_has_openowners(struct nfs4_client *clp)
3237 struct nfs4_openowner *oo;
3239 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3240 if (!list_empty(&oo->oo_owner.so_stateids))
3246 static bool client_has_state(struct nfs4_client *clp)
3248 return client_has_openowners(clp)
3249 #ifdef CONFIG_NFSD_PNFS
3250 || !list_empty(&clp->cl_lo_states)
3252 || !list_empty(&clp->cl_delegations)
3253 || !list_empty(&clp->cl_sessions)
3254 || !list_empty(&clp->async_copies);
3257 static __be32 copy_impl_id(struct nfs4_client *clp,
3258 struct nfsd4_exchange_id *exid)
3260 if (!exid->nii_domain.data)
3262 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3263 if (!clp->cl_nii_domain.data)
3264 return nfserr_jukebox;
3265 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3266 if (!clp->cl_nii_name.data)
3267 return nfserr_jukebox;
3268 clp->cl_nii_time = exid->nii_time;
3273 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3274 union nfsd4_op_u *u)
3276 struct nfsd4_exchange_id *exid = &u->exchange_id;
3277 struct nfs4_client *conf, *new;
3278 struct nfs4_client *unconf = NULL;
3280 char addr_str[INET6_ADDRSTRLEN];
3281 nfs4_verifier verf = exid->verifier;
3282 struct sockaddr *sa = svc_addr(rqstp);
3283 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3284 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3286 rpc_ntop(sa, addr_str, sizeof(addr_str));
3287 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3288 "ip_addr=%s flags %x, spa_how %u\n",
3289 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3290 addr_str, exid->flags, exid->spa_how);
3292 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3293 return nfserr_inval;
3295 new = create_client(exid->clname, rqstp, &verf);
3297 return nfserr_jukebox;
3298 status = copy_impl_id(new, exid);
3302 switch (exid->spa_how) {
3304 exid->spo_must_enforce[0] = 0;
3305 exid->spo_must_enforce[1] = (
3306 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3307 1 << (OP_EXCHANGE_ID - 32) |
3308 1 << (OP_CREATE_SESSION - 32) |
3309 1 << (OP_DESTROY_SESSION - 32) |
3310 1 << (OP_DESTROY_CLIENTID - 32));
3312 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3313 1 << (OP_OPEN_DOWNGRADE) |
3315 1 << (OP_DELEGRETURN));
3317 exid->spo_must_allow[1] &= (
3318 1 << (OP_TEST_STATEID - 32) |
3319 1 << (OP_FREE_STATEID - 32));
3320 if (!svc_rqst_integrity_protected(rqstp)) {
3321 status = nfserr_inval;
3325 * Sometimes userspace doesn't give us a principal.
3326 * Which is a bug, really. Anyway, we can't enforce
3327 * MACH_CRED in that case, better to give up now:
3329 if (!new->cl_cred.cr_principal &&
3330 !new->cl_cred.cr_raw_principal) {
3331 status = nfserr_serverfault;
3334 new->cl_mach_cred = true;
3338 default: /* checked by xdr code */
3342 status = nfserr_encr_alg_unsupp;
3346 /* Cases below refer to rfc 5661 section 18.35.4: */
3347 spin_lock(&nn->client_lock);
3348 conf = find_confirmed_client_by_name(&exid->clname, nn);
3350 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3351 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3354 if (!clp_used_exchangeid(conf)) { /* buggy client */
3355 status = nfserr_inval;
3358 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3359 status = nfserr_wrong_cred;
3362 if (!creds_match) { /* case 9 */
3363 status = nfserr_perm;
3366 if (!verfs_match) { /* case 8 */
3367 status = nfserr_not_same;
3371 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3372 trace_nfsd_clid_confirmed_r(conf);
3375 if (!creds_match) { /* case 3 */
3376 if (client_has_state(conf)) {
3377 status = nfserr_clid_inuse;
3378 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3383 if (verfs_match) { /* case 2 */
3384 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3385 trace_nfsd_clid_confirmed_r(conf);
3388 /* case 5, client reboot */
3389 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3394 if (update) { /* case 7 */
3395 status = nfserr_noent;
3399 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3400 if (unconf) /* case 4, possible retry or client restart */
3401 unhash_client_locked(unconf);
3403 /* case 1, new owner ID */
3404 trace_nfsd_clid_fresh(new);
3408 status = mark_client_expired_locked(conf);
3411 trace_nfsd_clid_replaced(&conf->cl_clientid);
3413 new->cl_minorversion = cstate->minorversion;
3414 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3415 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3417 add_to_unconfirmed(new);
3420 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3421 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3423 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3424 nfsd4_set_ex_flags(conf, exid);
3426 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3427 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3431 spin_unlock(&nn->client_lock);
3436 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3437 expire_client(unconf);
3443 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3445 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3448 /* The slot is in use, and no response has been sent. */
3450 if (seqid == slot_seqid)
3451 return nfserr_jukebox;
3453 return nfserr_seq_misordered;
3455 /* Note unsigned 32-bit arithmetic handles wraparound: */
3456 if (likely(seqid == slot_seqid + 1))
3458 if (seqid == slot_seqid)
3459 return nfserr_replay_cache;
3460 return nfserr_seq_misordered;
3464 * Cache the create session result into the create session single DRC
3465 * slot cache by saving the xdr structure. sl_seqid has been set.
3466 * Do this for solo or embedded create session operations.
3469 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3470 struct nfsd4_clid_slot *slot, __be32 nfserr)
3472 slot->sl_status = nfserr;
3473 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3477 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3478 struct nfsd4_clid_slot *slot)
3480 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3481 return slot->sl_status;
3484 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3485 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3486 1 + /* MIN tag is length with zero, only length */ \
3487 3 + /* version, opcount, opcode */ \
3488 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3489 /* seqid, slotID, slotID, cache */ \
3490 4 ) * sizeof(__be32))
3492 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3493 2 + /* verifier: AUTH_NULL, length 0 */\
3495 1 + /* MIN tag is length with zero, only length */ \
3496 3 + /* opcount, opcode, opstatus*/ \
3497 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3498 /* seqid, slotID, slotID, slotID, status */ \
3499 5 ) * sizeof(__be32))
3501 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3503 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3505 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3506 return nfserr_toosmall;
3507 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3508 return nfserr_toosmall;
3509 ca->headerpadsz = 0;
3510 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3511 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3512 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3513 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3514 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3515 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3517 * Note decreasing slot size below client's request may make it
3518 * difficult for client to function correctly, whereas
3519 * decreasing the number of slots will (just?) affect
3520 * performance. When short on memory we therefore prefer to
3521 * decrease number of slots instead of their size. Clients that
3522 * request larger slots than they need will get poor results:
3523 * Note that we always allow at least one slot, because our
3524 * accounting is soft and provides no guarantees either way.
3526 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3532 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3533 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3535 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3536 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3538 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3539 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3541 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3542 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3543 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3544 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3547 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3549 ca->headerpadsz = 0;
3551 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3552 return nfserr_toosmall;
3553 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3554 return nfserr_toosmall;
3555 ca->maxresp_cached = 0;
3557 return nfserr_toosmall;
3562 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3564 switch (cbs->flavor) {
3570 * GSS case: the spec doesn't allow us to return this
3571 * error. But it also doesn't allow us not to support
3573 * I'd rather this fail hard than return some error the
3574 * client might think it can already handle:
3576 return nfserr_encr_alg_unsupp;
3581 nfsd4_create_session(struct svc_rqst *rqstp,
3582 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3584 struct nfsd4_create_session *cr_ses = &u->create_session;
3585 struct sockaddr *sa = svc_addr(rqstp);
3586 struct nfs4_client *conf, *unconf;
3587 struct nfs4_client *old = NULL;
3588 struct nfsd4_session *new;
3589 struct nfsd4_conn *conn;
3590 struct nfsd4_clid_slot *cs_slot = NULL;
3592 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3594 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3595 return nfserr_inval;
3596 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3599 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3602 status = check_backchannel_attrs(&cr_ses->back_channel);
3604 goto out_release_drc_mem;
3605 status = nfserr_jukebox;
3606 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3608 goto out_release_drc_mem;
3609 conn = alloc_conn_from_crses(rqstp, cr_ses);
3611 goto out_free_session;
3613 spin_lock(&nn->client_lock);
3614 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3615 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3616 WARN_ON_ONCE(conf && unconf);
3619 status = nfserr_wrong_cred;
3620 if (!nfsd4_mach_creds_match(conf, rqstp))
3622 cs_slot = &conf->cl_cs_slot;
3623 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3625 if (status == nfserr_replay_cache)
3626 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3629 } else if (unconf) {
3630 status = nfserr_clid_inuse;
3631 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3632 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3633 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
3636 status = nfserr_wrong_cred;
3637 if (!nfsd4_mach_creds_match(unconf, rqstp))
3639 cs_slot = &unconf->cl_cs_slot;
3640 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3642 /* an unconfirmed replay returns misordered */
3643 status = nfserr_seq_misordered;
3646 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3648 status = mark_client_expired_locked(old);
3653 trace_nfsd_clid_replaced(&old->cl_clientid);
3655 move_to_confirmed(unconf);
3658 status = nfserr_stale_clientid;
3662 /* Persistent sessions are not supported */
3663 cr_ses->flags &= ~SESSION4_PERSIST;
3664 /* Upshifting from TCP to RDMA is not supported */
3665 cr_ses->flags &= ~SESSION4_RDMA;
3667 init_session(rqstp, new, conf, cr_ses);
3668 nfsd4_get_session_locked(new);
3670 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3671 NFS4_MAX_SESSIONID_LEN);
3672 cs_slot->sl_seqid++;
3673 cr_ses->seqid = cs_slot->sl_seqid;
3675 /* cache solo and embedded create sessions under the client_lock */
3676 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3677 spin_unlock(&nn->client_lock);
3679 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3680 /* init connection and backchannel */
3681 nfsd4_init_conn(rqstp, conn, new);
3682 nfsd4_put_session(new);
3687 spin_unlock(&nn->client_lock);
3692 __free_session(new);
3693 out_release_drc_mem:
3694 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3698 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3701 case NFS4_CDFC4_FORE:
3702 case NFS4_CDFC4_BACK:
3704 case NFS4_CDFC4_FORE_OR_BOTH:
3705 case NFS4_CDFC4_BACK_OR_BOTH:
3706 *dir = NFS4_CDFC4_BOTH;
3709 return nfserr_inval;
3712 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3713 struct nfsd4_compound_state *cstate,
3714 union nfsd4_op_u *u)
3716 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3717 struct nfsd4_session *session = cstate->session;
3718 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3721 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3724 spin_lock(&nn->client_lock);
3725 session->se_cb_prog = bc->bc_cb_program;
3726 session->se_cb_sec = bc->bc_cb_sec;
3727 spin_unlock(&nn->client_lock);
3729 nfsd4_probe_callback(session->se_client);
3734 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3736 struct nfsd4_conn *c;
3738 list_for_each_entry(c, &s->se_conns, cn_persession) {
3739 if (c->cn_xprt == xpt) {
3746 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3747 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3749 struct nfs4_client *clp = session->se_client;
3750 struct svc_xprt *xpt = rqst->rq_xprt;
3751 struct nfsd4_conn *c;
3754 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3755 spin_lock(&clp->cl_lock);
3756 c = __nfsd4_find_conn(xpt, session);
3758 status = nfserr_noent;
3759 else if (req == c->cn_flags)
3761 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3762 c->cn_flags != NFS4_CDFC4_BACK)
3764 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3765 c->cn_flags != NFS4_CDFC4_FORE)
3768 status = nfserr_inval;
3769 spin_unlock(&clp->cl_lock);
3770 if (status == nfs_ok && conn)
3775 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3776 struct nfsd4_compound_state *cstate,
3777 union nfsd4_op_u *u)
3779 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3781 struct nfsd4_conn *conn;
3782 struct nfsd4_session *session;
3783 struct net *net = SVC_NET(rqstp);
3784 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3786 if (!nfsd4_last_compound_op(rqstp))
3787 return nfserr_not_only_op;
3788 spin_lock(&nn->client_lock);
3789 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3790 spin_unlock(&nn->client_lock);
3792 goto out_no_session;
3793 status = nfserr_wrong_cred;
3794 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3796 status = nfsd4_match_existing_connection(rqstp, session,
3798 if (status == nfs_ok) {
3799 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3800 bcts->dir == NFS4_CDFC4_BACK)
3801 conn->cn_flags |= NFS4_CDFC4_BACK;
3802 nfsd4_probe_callback(session->se_client);
3805 if (status == nfserr_inval)
3807 status = nfsd4_map_bcts_dir(&bcts->dir);
3810 conn = alloc_conn(rqstp, bcts->dir);
3811 status = nfserr_jukebox;
3814 nfsd4_init_conn(rqstp, conn, session);
3817 nfsd4_put_session(session);
3822 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3824 if (!cstate->session)
3826 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3830 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3831 union nfsd4_op_u *u)
3833 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3834 struct nfsd4_session *ses;
3836 int ref_held_by_me = 0;
3837 struct net *net = SVC_NET(r);
3838 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3840 status = nfserr_not_only_op;
3841 if (nfsd4_compound_in_session(cstate, sessionid)) {
3842 if (!nfsd4_last_compound_op(r))
3846 dump_sessionid(__func__, sessionid);
3847 spin_lock(&nn->client_lock);
3848 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3850 goto out_client_lock;
3851 status = nfserr_wrong_cred;
3852 if (!nfsd4_mach_creds_match(ses->se_client, r))
3853 goto out_put_session;
3854 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3856 goto out_put_session;
3857 unhash_session(ses);
3858 spin_unlock(&nn->client_lock);
3860 nfsd4_probe_callback_sync(ses->se_client);
3862 spin_lock(&nn->client_lock);
3865 nfsd4_put_session_locked(ses);
3867 spin_unlock(&nn->client_lock);
3872 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3874 struct nfs4_client *clp = ses->se_client;
3875 struct nfsd4_conn *c;
3876 __be32 status = nfs_ok;
3879 spin_lock(&clp->cl_lock);
3880 c = __nfsd4_find_conn(new->cn_xprt, ses);
3883 status = nfserr_conn_not_bound_to_session;
3884 if (clp->cl_mach_cred)
3886 __nfsd4_hash_conn(new, ses);
3887 spin_unlock(&clp->cl_lock);
3888 ret = nfsd4_register_conn(new);
3890 /* oops; xprt is already down: */
3891 nfsd4_conn_lost(&new->cn_xpt_user);
3894 spin_unlock(&clp->cl_lock);
3899 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3901 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3903 return args->opcnt > session->se_fchannel.maxops;
3906 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3907 struct nfsd4_session *session)
3909 struct xdr_buf *xb = &rqstp->rq_arg;
3911 return xb->len > session->se_fchannel.maxreq_sz;
3914 static bool replay_matches_cache(struct svc_rqst *rqstp,
3915 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3917 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3919 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3920 (bool)seq->cachethis)
3923 * If there's an error then the reply can have fewer ops than
3926 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3929 * But if we cached a reply with *more* ops than the call you're
3930 * sending us now, then this new call is clearly not really a
3931 * replay of the old one:
3933 if (slot->sl_opcnt > argp->opcnt)
3935 /* This is the only check explicitly called by spec: */
3936 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3939 * There may be more comparisons we could actually do, but the
3940 * spec doesn't require us to catch every case where the calls
3941 * don't match (that would require caching the call as well as
3942 * the reply), so we don't bother.
3948 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3949 union nfsd4_op_u *u)
3951 struct nfsd4_sequence *seq = &u->sequence;
3952 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3953 struct xdr_stream *xdr = resp->xdr;
3954 struct nfsd4_session *session;
3955 struct nfs4_client *clp;
3956 struct nfsd4_slot *slot;
3957 struct nfsd4_conn *conn;
3960 struct net *net = SVC_NET(rqstp);
3961 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3963 if (resp->opcnt != 1)
3964 return nfserr_sequence_pos;
3967 * Will be either used or freed by nfsd4_sequence_check_conn
3970 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3972 return nfserr_jukebox;
3974 spin_lock(&nn->client_lock);
3975 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3977 goto out_no_session;
3978 clp = session->se_client;
3980 status = nfserr_too_many_ops;
3981 if (nfsd4_session_too_many_ops(rqstp, session))
3982 goto out_put_session;
3984 status = nfserr_req_too_big;
3985 if (nfsd4_request_too_big(rqstp, session))
3986 goto out_put_session;
3988 status = nfserr_badslot;
3989 if (seq->slotid >= session->se_fchannel.maxreqs)
3990 goto out_put_session;
3992 slot = session->se_slots[seq->slotid];
3993 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3995 /* We do not negotiate the number of slots yet, so set the
3996 * maxslots to the session maxreqs which is used to encode
3997 * sr_highest_slotid and the sr_target_slot id to maxslots */
3998 seq->maxslots = session->se_fchannel.maxreqs;
4000 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
4001 slot->sl_flags & NFSD4_SLOT_INUSE);
4002 if (status == nfserr_replay_cache) {
4003 status = nfserr_seq_misordered;
4004 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
4005 goto out_put_session;
4006 status = nfserr_seq_false_retry;
4007 if (!replay_matches_cache(rqstp, seq, slot))
4008 goto out_put_session;
4009 cstate->slot = slot;
4010 cstate->session = session;
4012 /* Return the cached reply status and set cstate->status
4013 * for nfsd4_proc_compound processing */
4014 status = nfsd4_replay_cache_entry(resp, seq);
4015 cstate->status = nfserr_replay_cache;
4019 goto out_put_session;
4021 status = nfsd4_sequence_check_conn(conn, session);
4024 goto out_put_session;
4026 buflen = (seq->cachethis) ?
4027 session->se_fchannel.maxresp_cached :
4028 session->se_fchannel.maxresp_sz;
4029 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
4031 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
4032 goto out_put_session;
4033 svc_reserve(rqstp, buflen);
4036 /* Success! bump slot seqid */
4037 slot->sl_seqid = seq->seqid;
4038 slot->sl_flags |= NFSD4_SLOT_INUSE;
4040 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4042 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4044 cstate->slot = slot;
4045 cstate->session = session;
4049 switch (clp->cl_cb_state) {
4051 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
4053 case NFSD4_CB_FAULT:
4054 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4057 seq->status_flags = 0;
4059 if (!list_empty(&clp->cl_revoked))
4060 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4064 spin_unlock(&nn->client_lock);
4067 nfsd4_put_session_locked(session);
4068 goto out_no_session;
4072 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4074 struct nfsd4_compound_state *cs = &resp->cstate;
4076 if (nfsd4_has_session(cs)) {
4077 if (cs->status != nfserr_replay_cache) {
4078 nfsd4_store_cache_entry(resp);
4079 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4081 /* Drop session reference that was taken in nfsd4_sequence() */
4082 nfsd4_put_session(cs->session);
4084 put_client_renew(cs->clp);
4088 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4089 struct nfsd4_compound_state *cstate,
4090 union nfsd4_op_u *u)
4092 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4093 struct nfs4_client *conf, *unconf;
4094 struct nfs4_client *clp = NULL;
4096 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4098 spin_lock(&nn->client_lock);
4099 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4100 conf = find_confirmed_client(&dc->clientid, true, nn);
4101 WARN_ON_ONCE(conf && unconf);
4104 if (client_has_state(conf)) {
4105 status = nfserr_clientid_busy;
4108 status = mark_client_expired_locked(conf);
4115 status = nfserr_stale_clientid;
4118 if (!nfsd4_mach_creds_match(clp, rqstp)) {
4120 status = nfserr_wrong_cred;
4123 trace_nfsd_clid_destroyed(&clp->cl_clientid);
4124 unhash_client_locked(clp);
4126 spin_unlock(&nn->client_lock);
4133 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4134 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4136 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4137 struct nfs4_client *clp = cstate->clp;
4140 if (rc->rca_one_fs) {
4141 if (!cstate->current_fh.fh_dentry)
4142 return nfserr_nofilehandle;
4144 * We don't take advantage of the rca_one_fs case.
4145 * That's OK, it's optional, we can safely ignore it.
4150 status = nfserr_complete_already;
4151 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4154 status = nfserr_stale_clientid;
4155 if (is_client_expired(clp))
4157 * The following error isn't really legal.
4158 * But we only get here if the client just explicitly
4159 * destroyed the client. Surely it no longer cares what
4160 * error it gets back on an operation for the dead
4166 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4167 nfsd4_client_record_create(clp);
4168 inc_reclaim_complete(clp);
4174 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4175 union nfsd4_op_u *u)
4177 struct nfsd4_setclientid *setclid = &u->setclientid;
4178 struct xdr_netobj clname = setclid->se_name;
4179 nfs4_verifier clverifier = setclid->se_verf;
4180 struct nfs4_client *conf, *new;
4181 struct nfs4_client *unconf = NULL;
4183 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4185 new = create_client(clname, rqstp, &clverifier);
4187 return nfserr_jukebox;
4188 spin_lock(&nn->client_lock);
4189 conf = find_confirmed_client_by_name(&clname, nn);
4190 if (conf && client_has_state(conf)) {
4191 status = nfserr_clid_inuse;
4192 if (clp_used_exchangeid(conf))
4194 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4195 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4199 unconf = find_unconfirmed_client_by_name(&clname, nn);
4201 unhash_client_locked(unconf);
4203 if (same_verf(&conf->cl_verifier, &clverifier)) {
4204 copy_clid(new, conf);
4205 gen_confirm(new, nn);
4207 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4210 trace_nfsd_clid_fresh(new);
4211 new->cl_minorversion = 0;
4212 gen_callback(new, setclid, rqstp);
4213 add_to_unconfirmed(new);
4214 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4215 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4216 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4220 spin_unlock(&nn->client_lock);
4224 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4225 expire_client(unconf);
4231 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4232 struct nfsd4_compound_state *cstate,
4233 union nfsd4_op_u *u)
4235 struct nfsd4_setclientid_confirm *setclientid_confirm =
4236 &u->setclientid_confirm;
4237 struct nfs4_client *conf, *unconf;
4238 struct nfs4_client *old = NULL;
4239 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4240 clientid_t * clid = &setclientid_confirm->sc_clientid;
4242 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4244 if (STALE_CLIENTID(clid, nn))
4245 return nfserr_stale_clientid;
4247 spin_lock(&nn->client_lock);
4248 conf = find_confirmed_client(clid, false, nn);
4249 unconf = find_unconfirmed_client(clid, false, nn);
4251 * We try hard to give out unique clientid's, so if we get an
4252 * attempt to confirm the same clientid with a different cred,
4253 * the client may be buggy; this should never happen.
4255 * Nevertheless, RFC 7530 recommends INUSE for this case:
4257 status = nfserr_clid_inuse;
4258 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4259 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4262 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4263 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4266 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4267 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4270 status = nfserr_stale_clientid;
4276 unhash_client_locked(old);
4277 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4279 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4281 status = nfserr_clid_inuse;
4282 if (client_has_state(old)
4283 && !same_creds(&unconf->cl_cred,
4288 status = mark_client_expired_locked(old);
4293 trace_nfsd_clid_replaced(&old->cl_clientid);
4295 move_to_confirmed(unconf);
4298 get_client_locked(conf);
4299 spin_unlock(&nn->client_lock);
4301 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4302 nfsd4_probe_callback(conf);
4303 spin_lock(&nn->client_lock);
4304 put_client_renew_locked(conf);
4306 spin_unlock(&nn->client_lock);
4312 static struct nfs4_file *nfsd4_alloc_file(void)
4314 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4317 /* OPEN Share state helper functions */
4319 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
4321 refcount_set(&fp->fi_ref, 1);
4322 spin_lock_init(&fp->fi_lock);
4323 INIT_LIST_HEAD(&fp->fi_stateids);
4324 INIT_LIST_HEAD(&fp->fi_delegations);
4325 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4326 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4327 fp->fi_deleg_file = NULL;
4328 fp->fi_had_conflict = false;
4329 fp->fi_share_deny = 0;
4330 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4331 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4332 fp->fi_aliased = false;
4333 fp->fi_inode = d_inode(fh->fh_dentry);
4334 #ifdef CONFIG_NFSD_PNFS
4335 INIT_LIST_HEAD(&fp->fi_lo_states);
4336 atomic_set(&fp->fi_lo_recalls, 0);
4341 nfsd4_free_slabs(void)
4343 kmem_cache_destroy(client_slab);
4344 kmem_cache_destroy(openowner_slab);
4345 kmem_cache_destroy(lockowner_slab);
4346 kmem_cache_destroy(file_slab);
4347 kmem_cache_destroy(stateid_slab);
4348 kmem_cache_destroy(deleg_slab);
4349 kmem_cache_destroy(odstate_slab);
4353 nfsd4_init_slabs(void)
4355 client_slab = kmem_cache_create("nfsd4_clients",
4356 sizeof(struct nfs4_client), 0, 0, NULL);
4357 if (client_slab == NULL)
4359 openowner_slab = kmem_cache_create("nfsd4_openowners",
4360 sizeof(struct nfs4_openowner), 0, 0, NULL);
4361 if (openowner_slab == NULL)
4362 goto out_free_client_slab;
4363 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4364 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4365 if (lockowner_slab == NULL)
4366 goto out_free_openowner_slab;
4367 file_slab = kmem_cache_create("nfsd4_files",
4368 sizeof(struct nfs4_file), 0, 0, NULL);
4369 if (file_slab == NULL)
4370 goto out_free_lockowner_slab;
4371 stateid_slab = kmem_cache_create("nfsd4_stateids",
4372 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4373 if (stateid_slab == NULL)
4374 goto out_free_file_slab;
4375 deleg_slab = kmem_cache_create("nfsd4_delegations",
4376 sizeof(struct nfs4_delegation), 0, 0, NULL);
4377 if (deleg_slab == NULL)
4378 goto out_free_stateid_slab;
4379 odstate_slab = kmem_cache_create("nfsd4_odstate",
4380 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4381 if (odstate_slab == NULL)
4382 goto out_free_deleg_slab;
4385 out_free_deleg_slab:
4386 kmem_cache_destroy(deleg_slab);
4387 out_free_stateid_slab:
4388 kmem_cache_destroy(stateid_slab);
4390 kmem_cache_destroy(file_slab);
4391 out_free_lockowner_slab:
4392 kmem_cache_destroy(lockowner_slab);
4393 out_free_openowner_slab:
4394 kmem_cache_destroy(openowner_slab);
4395 out_free_client_slab:
4396 kmem_cache_destroy(client_slab);
4401 static unsigned long
4402 nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
4405 struct nfsd_net *nn = shrink->private_data;
4407 count = atomic_read(&nn->nfsd_courtesy_clients);
4409 count = atomic_long_read(&num_delegations);
4411 queue_work(laundry_wq, &nn->nfsd_shrinker_work);
4412 return (unsigned long)count;
4415 static unsigned long
4416 nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
4422 nfsd4_init_leases_net(struct nfsd_net *nn)
4427 nn->nfsd4_lease = 90; /* default lease time */
4428 nn->nfsd4_grace = 90;
4429 nn->somebody_reclaimed = false;
4430 nn->track_reclaim_completes = false;
4431 nn->clverifier_counter = get_random_u32();
4432 nn->clientid_base = get_random_u32();
4433 nn->clientid_counter = nn->clientid_base + 1;
4434 nn->s2s_cp_cl_id = nn->clientid_counter++;
4436 atomic_set(&nn->nfs4_client_count, 0);
4438 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4439 max_clients *= NFS4_CLIENTS_PER_GB;
4440 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4442 atomic_set(&nn->nfsd_courtesy_clients, 0);
4445 static void init_nfs4_replay(struct nfs4_replay *rp)
4447 rp->rp_status = nfserr_serverfault;
4449 rp->rp_buf = rp->rp_ibuf;
4450 mutex_init(&rp->rp_mutex);
4453 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4454 struct nfs4_stateowner *so)
4456 if (!nfsd4_has_session(cstate)) {
4457 mutex_lock(&so->so_replay.rp_mutex);
4458 cstate->replay_owner = nfs4_get_stateowner(so);
4462 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4464 struct nfs4_stateowner *so = cstate->replay_owner;
4467 cstate->replay_owner = NULL;
4468 mutex_unlock(&so->so_replay.rp_mutex);
4469 nfs4_put_stateowner(so);
4473 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4475 struct nfs4_stateowner *sop;
4477 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4481 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4482 if (!sop->so_owner.data) {
4483 kmem_cache_free(slab, sop);
4487 INIT_LIST_HEAD(&sop->so_stateids);
4488 sop->so_client = clp;
4489 init_nfs4_replay(&sop->so_replay);
4490 atomic_set(&sop->so_count, 1);
4494 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4496 lockdep_assert_held(&clp->cl_lock);
4498 list_add(&oo->oo_owner.so_strhash,
4499 &clp->cl_ownerstr_hashtbl[strhashval]);
4500 list_add(&oo->oo_perclient, &clp->cl_openowners);
4503 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4505 unhash_openowner_locked(openowner(so));
4508 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4510 struct nfs4_openowner *oo = openowner(so);
4512 kmem_cache_free(openowner_slab, oo);
4515 static const struct nfs4_stateowner_operations openowner_ops = {
4516 .so_unhash = nfs4_unhash_openowner,
4517 .so_free = nfs4_free_openowner,
4520 static struct nfs4_ol_stateid *
4521 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4523 struct nfs4_ol_stateid *local, *ret = NULL;
4524 struct nfs4_openowner *oo = open->op_openowner;
4526 lockdep_assert_held(&fp->fi_lock);
4528 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4529 /* ignore lock owners */
4530 if (local->st_stateowner->so_is_open_owner == 0)
4532 if (local->st_stateowner != &oo->oo_owner)
4534 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4536 refcount_inc(&ret->st_stid.sc_count);
4544 nfsd4_verify_open_stid(struct nfs4_stid *s)
4546 __be32 ret = nfs_ok;
4548 switch (s->sc_type) {
4552 case NFS4_CLOSED_STID:
4553 case NFS4_CLOSED_DELEG_STID:
4554 ret = nfserr_bad_stateid;
4556 case NFS4_REVOKED_DELEG_STID:
4557 ret = nfserr_deleg_revoked;
4562 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4564 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4568 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4569 ret = nfsd4_verify_open_stid(&stp->st_stid);
4571 mutex_unlock(&stp->st_mutex);
4575 static struct nfs4_ol_stateid *
4576 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4578 struct nfs4_ol_stateid *stp;
4580 spin_lock(&fp->fi_lock);
4581 stp = nfsd4_find_existing_open(fp, open);
4582 spin_unlock(&fp->fi_lock);
4583 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4585 nfs4_put_stid(&stp->st_stid);
4590 static struct nfs4_openowner *
4591 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4592 struct nfsd4_compound_state *cstate)
4594 struct nfs4_client *clp = cstate->clp;
4595 struct nfs4_openowner *oo, *ret;
4597 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4600 oo->oo_owner.so_ops = &openowner_ops;
4601 oo->oo_owner.so_is_open_owner = 1;
4602 oo->oo_owner.so_seqid = open->op_seqid;
4604 if (nfsd4_has_session(cstate))
4605 oo->oo_flags |= NFS4_OO_CONFIRMED;
4607 oo->oo_last_closed_stid = NULL;
4608 INIT_LIST_HEAD(&oo->oo_close_lru);
4609 spin_lock(&clp->cl_lock);
4610 ret = find_openstateowner_str_locked(strhashval, open, clp);
4612 hash_openowner(oo, clp, strhashval);
4615 nfs4_free_stateowner(&oo->oo_owner);
4617 spin_unlock(&clp->cl_lock);
4621 static struct nfs4_ol_stateid *
4622 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4625 struct nfs4_openowner *oo = open->op_openowner;
4626 struct nfs4_ol_stateid *retstp = NULL;
4627 struct nfs4_ol_stateid *stp;
4630 /* We are moving these outside of the spinlocks to avoid the warnings */
4631 mutex_init(&stp->st_mutex);
4632 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4635 spin_lock(&oo->oo_owner.so_client->cl_lock);
4636 spin_lock(&fp->fi_lock);
4638 retstp = nfsd4_find_existing_open(fp, open);
4642 open->op_stp = NULL;
4643 refcount_inc(&stp->st_stid.sc_count);
4644 stp->st_stid.sc_type = NFS4_OPEN_STID;
4645 INIT_LIST_HEAD(&stp->st_locks);
4646 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4648 stp->st_stid.sc_file = fp;
4649 stp->st_access_bmap = 0;
4650 stp->st_deny_bmap = 0;
4651 stp->st_openstp = NULL;
4652 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4653 list_add(&stp->st_perfile, &fp->fi_stateids);
4656 spin_unlock(&fp->fi_lock);
4657 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4659 /* Handle races with CLOSE */
4660 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4661 nfs4_put_stid(&retstp->st_stid);
4664 /* To keep mutex tracking happy */
4665 mutex_unlock(&stp->st_mutex);
4672 * In the 4.0 case we need to keep the owners around a little while to handle
4673 * CLOSE replay. We still do need to release any file access that is held by
4674 * them before returning however.
4677 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4679 struct nfs4_ol_stateid *last;
4680 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4681 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4684 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4687 * We know that we hold one reference via nfsd4_close, and another
4688 * "persistent" reference for the client. If the refcount is higher
4689 * than 2, then there are still calls in progress that are using this
4690 * stateid. We can't put the sc_file reference until they are finished.
4691 * Wait for the refcount to drop to 2. Since it has been unhashed,
4692 * there should be no danger of the refcount going back up again at
4695 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4697 release_all_access(s);
4698 if (s->st_stid.sc_file) {
4699 put_nfs4_file(s->st_stid.sc_file);
4700 s->st_stid.sc_file = NULL;
4703 spin_lock(&nn->client_lock);
4704 last = oo->oo_last_closed_stid;
4705 oo->oo_last_closed_stid = s;
4706 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4707 oo->oo_time = ktime_get_boottime_seconds();
4708 spin_unlock(&nn->client_lock);
4710 nfs4_put_stid(&last->st_stid);
4713 static noinline_for_stack struct nfs4_file *
4714 nfsd4_file_hash_lookup(const struct svc_fh *fhp)
4716 struct inode *inode = d_inode(fhp->fh_dentry);
4717 struct rhlist_head *tmp, *list;
4718 struct nfs4_file *fi;
4721 list = rhltable_lookup(&nfs4_file_rhltable, &inode,
4722 nfs4_file_rhash_params);
4723 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
4724 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
4725 if (refcount_inc_not_zero(&fi->fi_ref)) {
4736 * On hash insertion, identify entries with the same inode but
4737 * distinct filehandles. They will all be on the list returned
4738 * by rhltable_lookup().
4740 * inode->i_lock prevents racing insertions from adding an entry
4741 * for the same inode/fhp pair twice.
4743 static noinline_for_stack struct nfs4_file *
4744 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
4746 struct inode *inode = d_inode(fhp->fh_dentry);
4747 struct rhlist_head *tmp, *list;
4748 struct nfs4_file *ret = NULL;
4749 bool alias_found = false;
4750 struct nfs4_file *fi;
4754 spin_lock(&inode->i_lock);
4756 list = rhltable_lookup(&nfs4_file_rhltable, &inode,
4757 nfs4_file_rhash_params);
4758 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
4759 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
4760 if (refcount_inc_not_zero(&fi->fi_ref))
4763 fi->fi_aliased = alias_found = true;
4768 nfsd4_file_init(fhp, new);
4769 err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist,
4770 nfs4_file_rhash_params);
4774 new->fi_aliased = alias_found;
4778 spin_unlock(&inode->i_lock);
4783 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
4785 rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist,
4786 nfs4_file_rhash_params);
4790 * Called to check deny when READ with all zero stateid or
4791 * WRITE with all zero or all one stateid
4794 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4796 struct nfs4_file *fp;
4797 __be32 ret = nfs_ok;
4799 fp = nfsd4_file_hash_lookup(current_fh);
4803 /* Check for conflicting share reservations */
4804 spin_lock(&fp->fi_lock);
4805 if (fp->fi_share_deny & deny_type)
4806 ret = nfserr_locked;
4807 spin_unlock(&fp->fi_lock);
4812 static bool nfsd4_deleg_present(const struct inode *inode)
4814 struct file_lock_context *ctx = locks_inode_context(inode);
4816 return ctx && !list_empty_careful(&ctx->flc_lease);
4820 * nfsd_wait_for_delegreturn - wait for delegations to be returned
4821 * @rqstp: the RPC transaction being executed
4822 * @inode: in-core inode of the file being waited for
4824 * The timeout prevents deadlock if all nfsd threads happen to be
4825 * tied up waiting for returning delegations.
4828 * %true: delegation was returned
4829 * %false: timed out waiting for delegreturn
4831 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
4833 long __maybe_unused timeo;
4835 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
4836 NFSD_DELEGRETURN_TIMEOUT);
4837 trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
4841 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4843 struct nfs4_delegation *dp = cb_to_delegation(cb);
4844 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4847 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4850 * We can't do this in nfsd_break_deleg_cb because it is
4851 * already holding inode->i_lock.
4853 * If the dl_time != 0, then we know that it has already been
4854 * queued for a lease break. Don't queue it again.
4856 spin_lock(&state_lock);
4857 if (delegation_hashed(dp) && dp->dl_time == 0) {
4858 dp->dl_time = ktime_get_boottime_seconds();
4859 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4861 spin_unlock(&state_lock);
4864 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4865 struct rpc_task *task)
4867 struct nfs4_delegation *dp = cb_to_delegation(cb);
4869 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
4871 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4872 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4875 switch (task->tk_status) {
4878 case -NFS4ERR_DELAY:
4879 rpc_delay(task, 2 * HZ);
4882 case -NFS4ERR_BAD_STATEID:
4884 * Race: client probably got cb_recall before open reply
4885 * granting delegation.
4887 if (dp->dl_retries--) {
4888 rpc_delay(task, 2 * HZ);
4897 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4899 struct nfs4_delegation *dp = cb_to_delegation(cb);
4901 nfs4_put_stid(&dp->dl_stid);
4904 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4905 .prepare = nfsd4_cb_recall_prepare,
4906 .done = nfsd4_cb_recall_done,
4907 .release = nfsd4_cb_recall_release,
4910 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4913 * We're assuming the state code never drops its reference
4914 * without first removing the lease. Since we're in this lease
4915 * callback (and since the lease code is serialized by the
4916 * flc_lock) we know the server hasn't removed the lease yet, and
4917 * we know it's safe to take a reference.
4919 refcount_inc(&dp->dl_stid.sc_count);
4920 WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall));
4923 /* Called from break_lease() with flc_lock held. */
4925 nfsd_break_deleg_cb(struct file_lock *fl)
4927 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4928 struct nfs4_file *fp = dp->dl_stid.sc_file;
4929 struct nfs4_client *clp = dp->dl_stid.sc_client;
4930 struct nfsd_net *nn;
4932 trace_nfsd_cb_recall(&dp->dl_stid);
4934 dp->dl_recalled = true;
4935 atomic_inc(&clp->cl_delegs_in_recall);
4936 if (try_to_expire_client(clp)) {
4937 nn = net_generic(clp->net, nfsd_net_id);
4938 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
4942 * We don't want the locks code to timeout the lease for us;
4943 * we'll remove it ourself if a delegation isn't returned
4946 fl->fl_break_time = 0;
4948 spin_lock(&fp->fi_lock);
4949 fp->fi_had_conflict = true;
4950 nfsd_break_one_deleg(dp);
4951 spin_unlock(&fp->fi_lock);
4956 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
4957 * @fl: Lock state to check
4960 * %true: Lease conflict was resolved
4961 * %false: Lease conflict was not resolved.
4963 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4965 struct nfs4_delegation *dl = fl->fl_owner;
4966 struct svc_rqst *rqst;
4967 struct nfs4_client *clp;
4971 rqst = kthread_data(current);
4972 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4973 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
4975 clp = *(rqst->rq_lease_breaker);
4976 return dl->dl_stid.sc_client == clp;
4980 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4981 struct list_head *dispose)
4983 struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
4984 struct nfs4_client *clp = dp->dl_stid.sc_client;
4986 if (arg & F_UNLCK) {
4987 if (dp->dl_recalled)
4988 atomic_dec(&clp->cl_delegs_in_recall);
4989 return lease_modify(onlist, arg, dispose);
4994 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4995 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
4996 .lm_break = nfsd_break_deleg_cb,
4997 .lm_change = nfsd_change_deleg_cb,
5000 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
5002 if (nfsd4_has_session(cstate))
5004 if (seqid == so->so_seqid - 1)
5005 return nfserr_replay_me;
5006 if (seqid == so->so_seqid)
5008 return nfserr_bad_seqid;
5011 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
5012 struct nfsd_net *nn)
5014 struct nfs4_client *found;
5016 spin_lock(&nn->client_lock);
5017 found = find_confirmed_client(clid, sessions, nn);
5019 atomic_inc(&found->cl_rpc_users);
5020 spin_unlock(&nn->client_lock);
5024 static __be32 set_client(clientid_t *clid,
5025 struct nfsd4_compound_state *cstate,
5026 struct nfsd_net *nn)
5029 if (!same_clid(&cstate->clp->cl_clientid, clid))
5030 return nfserr_stale_clientid;
5033 if (STALE_CLIENTID(clid, nn))
5034 return nfserr_stale_clientid;
5036 * We're in the 4.0 case (otherwise the SEQUENCE op would have
5037 * set cstate->clp), so session = false:
5039 cstate->clp = lookup_clientid(clid, false, nn);
5041 return nfserr_expired;
5046 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
5047 struct nfsd4_open *open, struct nfsd_net *nn)
5049 clientid_t *clientid = &open->op_clientid;
5050 struct nfs4_client *clp = NULL;
5051 unsigned int strhashval;
5052 struct nfs4_openowner *oo = NULL;
5056 * In case we need it later, after we've already created the
5057 * file and don't want to risk a further failure:
5059 open->op_file = nfsd4_alloc_file();
5060 if (open->op_file == NULL)
5061 return nfserr_jukebox;
5063 status = set_client(clientid, cstate, nn);
5068 strhashval = ownerstr_hashval(&open->op_owner);
5069 oo = find_openstateowner_str(strhashval, open, clp);
5070 open->op_openowner = oo;
5074 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5075 /* Replace unconfirmed owners without checking for replay. */
5076 release_openowner(oo);
5077 open->op_openowner = NULL;
5080 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5085 oo = alloc_init_open_stateowner(strhashval, open, cstate);
5087 return nfserr_jukebox;
5088 open->op_openowner = oo;
5090 open->op_stp = nfs4_alloc_open_stateid(clp);
5092 return nfserr_jukebox;
5094 if (nfsd4_has_session(cstate) &&
5095 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5096 open->op_odstate = alloc_clnt_odstate(clp);
5097 if (!open->op_odstate)
5098 return nfserr_jukebox;
5104 static inline __be32
5105 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5107 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
5108 return nfserr_openmode;
5113 static int share_access_to_flags(u32 share_access)
5115 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5118 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
5120 struct nfs4_stid *ret;
5122 ret = find_stateid_by_type(cl, s,
5123 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
5126 return delegstateid(ret);
5129 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5131 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5132 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5136 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5137 struct nfs4_delegation **dp)
5140 __be32 status = nfserr_bad_stateid;
5141 struct nfs4_delegation *deleg;
5143 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5146 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
5147 nfs4_put_stid(&deleg->dl_stid);
5148 if (cl->cl_minorversion)
5149 status = nfserr_deleg_revoked;
5152 flags = share_access_to_flags(open->op_share_access);
5153 status = nfs4_check_delegmode(deleg, flags);
5155 nfs4_put_stid(&deleg->dl_stid);
5160 if (!nfsd4_is_deleg_cur(open))
5164 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5168 static inline int nfs4_access_to_access(u32 nfs4_access)
5172 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5173 flags |= NFSD_MAY_READ;
5174 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5175 flags |= NFSD_MAY_WRITE;
5179 static inline __be32
5180 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5181 struct nfsd4_open *open)
5183 struct iattr iattr = {
5184 .ia_valid = ATTR_SIZE,
5187 struct nfsd_attrs attrs = {
5190 if (!open->op_truncate)
5192 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5193 return nfserr_inval;
5194 return nfsd_setattr(rqstp, fh, &attrs, 0, (time64_t)0);
5197 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5198 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5199 struct nfsd4_open *open, bool new_stp)
5201 struct nfsd_file *nf = NULL;
5203 int oflag = nfs4_access_to_omode(open->op_share_access);
5204 int access = nfs4_access_to_access(open->op_share_access);
5205 unsigned char old_access_bmap, old_deny_bmap;
5207 spin_lock(&fp->fi_lock);
5210 * Are we trying to set a deny mode that would conflict with
5213 status = nfs4_file_check_deny(fp, open->op_share_deny);
5214 if (status != nfs_ok) {
5215 if (status != nfserr_share_denied) {
5216 spin_unlock(&fp->fi_lock);
5219 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5220 stp, open->op_share_deny, false))
5221 status = nfserr_jukebox;
5222 spin_unlock(&fp->fi_lock);
5226 /* set access to the file */
5227 status = nfs4_file_get_access(fp, open->op_share_access);
5228 if (status != nfs_ok) {
5229 if (status != nfserr_share_denied) {
5230 spin_unlock(&fp->fi_lock);
5233 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5234 stp, open->op_share_access, true))
5235 status = nfserr_jukebox;
5236 spin_unlock(&fp->fi_lock);
5240 /* Set access bits in stateid */
5241 old_access_bmap = stp->st_access_bmap;
5242 set_access(open->op_share_access, stp);
5244 /* Set new deny mask */
5245 old_deny_bmap = stp->st_deny_bmap;
5246 set_deny(open->op_share_deny, stp);
5247 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5249 if (!fp->fi_fds[oflag]) {
5250 spin_unlock(&fp->fi_lock);
5252 status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
5253 open->op_filp, &nf);
5254 if (status != nfs_ok)
5255 goto out_put_access;
5257 spin_lock(&fp->fi_lock);
5258 if (!fp->fi_fds[oflag]) {
5259 fp->fi_fds[oflag] = nf;
5263 spin_unlock(&fp->fi_lock);
5267 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5270 goto out_put_access;
5272 status = nfsd4_truncate(rqstp, cur_fh, open);
5274 goto out_put_access;
5278 stp->st_access_bmap = old_access_bmap;
5279 nfs4_file_put_access(fp, open->op_share_access);
5280 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5285 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5286 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5287 struct nfsd4_open *open)
5290 unsigned char old_deny_bmap = stp->st_deny_bmap;
5292 if (!test_access(open->op_share_access, stp))
5293 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5295 /* test and set deny mode */
5296 spin_lock(&fp->fi_lock);
5297 status = nfs4_file_check_deny(fp, open->op_share_deny);
5300 set_deny(open->op_share_deny, stp);
5301 fp->fi_share_deny |=
5302 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5304 case nfserr_share_denied:
5305 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5306 stp, open->op_share_deny, false))
5307 status = nfserr_jukebox;
5310 spin_unlock(&fp->fi_lock);
5312 if (status != nfs_ok)
5315 status = nfsd4_truncate(rqstp, cur_fh, open);
5316 if (status != nfs_ok)
5317 reset_union_bmap_deny(old_deny_bmap, stp);
5321 /* Should we give out recallable state?: */
5322 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5324 if (clp->cl_cb_state == NFSD4_CB_UP)
5327 * In the sessions case, since we don't have to establish a
5328 * separate connection for callbacks, we assume it's OK
5329 * until we hear otherwise:
5331 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5334 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5337 struct file_lock *fl;
5339 fl = locks_alloc_lock();
5342 fl->fl_lmops = &nfsd_lease_mng_ops;
5343 fl->fl_flags = FL_DELEG;
5344 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5345 fl->fl_end = OFFSET_MAX;
5346 fl->fl_owner = (fl_owner_t)dp;
5347 fl->fl_pid = current->tgid;
5348 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5352 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5353 struct nfs4_file *fp)
5355 struct nfs4_ol_stateid *st;
5356 struct file *f = fp->fi_deleg_file->nf_file;
5357 struct inode *ino = file_inode(f);
5360 writes = atomic_read(&ino->i_writecount);
5364 * There could be multiple filehandles (hence multiple
5365 * nfs4_files) referencing this file, but that's not too
5366 * common; let's just give up in that case rather than
5367 * trying to go look up all the clients using that other
5368 * nfs4_file as well:
5373 * If there's a close in progress, make sure that we see it
5374 * clear any fi_fds[] entries before we see it decrement
5377 smp_mb__after_atomic();
5379 if (fp->fi_fds[O_WRONLY])
5381 if (fp->fi_fds[O_RDWR])
5384 return -EAGAIN; /* There may be non-NFSv4 writers */
5386 * It's possible there are non-NFSv4 write opens in progress,
5387 * but if they haven't incremented i_writecount yet then they
5388 * also haven't called break lease yet; so, they'll break this
5389 * lease soon enough. So, all that's left to check for is NFSv4
5392 spin_lock(&fp->fi_lock);
5393 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5394 if (st->st_openstp == NULL /* it's an open */ &&
5395 access_permit_write(st) &&
5396 st->st_stid.sc_client != clp) {
5397 spin_unlock(&fp->fi_lock);
5401 spin_unlock(&fp->fi_lock);
5403 * There's a small chance that we could be racing with another
5404 * NFSv4 open. However, any open that hasn't added itself to
5405 * the fi_stateids list also hasn't called break_lease yet; so,
5406 * they'll break this lease soon enough.
5412 * It's possible that between opening the dentry and setting the delegation,
5413 * that it has been renamed or unlinked. Redo the lookup to verify that this
5417 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5418 struct svc_fh *parent)
5420 struct svc_export *exp;
5421 struct dentry *child;
5424 err = nfsd_lookup_dentry(open->op_rqstp, parent,
5425 open->op_fname, open->op_fnamelen,
5433 if (child != file_dentry(fp->fi_deleg_file->nf_file))
5440 * We avoid breaking delegations held by a client due to its own activity, but
5441 * clearing setuid/setgid bits on a write is an implicit activity and the client
5442 * may not notice and continue using the old mode. Avoid giving out a delegation
5443 * on setuid/setgid files when the client is requesting an open for write.
5446 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
5448 struct inode *inode = file_inode(nf->nf_file);
5450 if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
5451 (inode->i_mode & (S_ISUID|S_ISGID)))
5456 static struct nfs4_delegation *
5457 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5458 struct svc_fh *parent)
5461 struct nfs4_client *clp = stp->st_stid.sc_client;
5462 struct nfs4_file *fp = stp->st_stid.sc_file;
5463 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
5464 struct nfs4_delegation *dp;
5465 struct nfsd_file *nf = NULL;
5466 struct file_lock *fl;
5470 * The fi_had_conflict and nfs_get_existing_delegation checks
5471 * here are just optimizations; we'll need to recheck them at
5474 if (fp->fi_had_conflict)
5475 return ERR_PTR(-EAGAIN);
5478 * Try for a write delegation first. RFC8881 section 10.4 says:
5480 * "An OPEN_DELEGATE_WRITE delegation allows the client to handle,
5481 * on its own, all opens."
5483 * Furthermore the client can use a write delegation for most READ
5484 * operations as well, so we require a O_RDWR file here.
5486 * Offer a write delegation in the case of a BOTH open, and ensure
5487 * we get the O_RDWR descriptor.
5489 if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == NFS4_SHARE_ACCESS_BOTH) {
5490 nf = find_rw_file(fp);
5491 dl_type = NFS4_OPEN_DELEGATE_WRITE;
5495 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
5496 * file for some reason, then try for a read delegation instead.
5498 if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
5499 nf = find_readable_file(fp);
5500 dl_type = NFS4_OPEN_DELEGATE_READ;
5504 return ERR_PTR(-EAGAIN);
5506 spin_lock(&state_lock);
5507 spin_lock(&fp->fi_lock);
5508 if (nfs4_delegation_exists(clp, fp))
5510 else if (nfsd4_verify_setuid_write(open, nf))
5512 else if (!fp->fi_deleg_file) {
5513 fp->fi_deleg_file = nf;
5514 /* increment early to prevent fi_deleg_file from being
5516 fp->fi_delegees = 1;
5520 spin_unlock(&fp->fi_lock);
5521 spin_unlock(&state_lock);
5525 return ERR_PTR(status);
5528 dp = alloc_init_deleg(clp, fp, odstate, dl_type);
5532 fl = nfs4_alloc_init_lease(dp, dl_type);
5534 goto out_clnt_odstate;
5536 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5538 locks_free_lock(fl);
5540 goto out_clnt_odstate;
5543 status = nfsd4_verify_deleg_dentry(open, fp, parent);
5548 status = nfsd4_check_conflicting_opens(clp, fp);
5553 * Now that the deleg is set, check again to ensure that nothing
5554 * raced in and changed the mode while we weren't lookng.
5556 status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file);
5560 spin_lock(&state_lock);
5561 spin_lock(&fp->fi_lock);
5562 if (fp->fi_had_conflict)
5565 status = hash_delegation_locked(dp, fp);
5566 spin_unlock(&fp->fi_lock);
5567 spin_unlock(&state_lock);
5574 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5576 put_clnt_odstate(dp->dl_clnt_odstate);
5577 nfs4_put_stid(&dp->dl_stid);
5580 return ERR_PTR(status);
5583 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5585 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5586 if (status == -EAGAIN)
5587 open->op_why_no_deleg = WND4_CONTENTION;
5589 open->op_why_no_deleg = WND4_RESOURCE;
5590 switch (open->op_deleg_want) {
5591 case NFS4_SHARE_WANT_READ_DELEG:
5592 case NFS4_SHARE_WANT_WRITE_DELEG:
5593 case NFS4_SHARE_WANT_ANY_DELEG:
5595 case NFS4_SHARE_WANT_CANCEL:
5596 open->op_why_no_deleg = WND4_CANCELLED;
5598 case NFS4_SHARE_WANT_NO_DELEG:
5605 * The Linux NFS server does not offer write delegations to NFSv4.0
5606 * clients in order to avoid conflicts between write delegations and
5607 * GETATTRs requesting CHANGE or SIZE attributes.
5609 * With NFSv4.1 and later minorversions, the SEQUENCE operation that
5610 * begins each COMPOUND contains a client ID. Delegation recall can
5611 * be avoided when the server recognizes the client sending a
5612 * GETATTR also holds write delegation it conflicts with.
5614 * However, the NFSv4.0 protocol does not enable a server to
5615 * determine that a GETATTR originated from the client holding the
5616 * conflicting delegation versus coming from some other client. Per
5617 * RFC 7530 Section 16.7.5, the server must recall or send a
5618 * CB_GETATTR even when the GETATTR originates from the client that
5619 * holds the conflicting delegation.
5621 * An NFSv4.0 client can trigger a pathological situation if it
5622 * always sends a DELEGRETURN preceded by a conflicting GETATTR in
5623 * the same COMPOUND. COMPOUND execution will always stop at the
5624 * GETATTR and the DELEGRETURN will never get executed. The server
5625 * eventually revokes the delegation, which can result in loss of
5626 * open or lock state.
5629 nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5630 struct svc_fh *currentfh)
5632 struct nfs4_delegation *dp;
5633 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5634 struct nfs4_client *clp = stp->st_stid.sc_client;
5635 struct svc_fh *parent = NULL;
5639 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5640 open->op_recall = false;
5641 switch (open->op_claim_type) {
5642 case NFS4_OPEN_CLAIM_PREVIOUS:
5644 open->op_recall = true;
5646 case NFS4_OPEN_CLAIM_NULL:
5649 case NFS4_OPEN_CLAIM_FH:
5651 * Let's not give out any delegations till everyone's
5652 * had the chance to reclaim theirs, *and* until
5653 * NLM locks have all been reclaimed:
5655 if (locks_in_grace(clp->net))
5657 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5659 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
5660 !clp->cl_minorversion)
5666 dp = nfs4_set_delegation(open, stp, parent);
5670 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5672 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
5673 open->op_delegate_type = NFS4_OPEN_DELEGATE_WRITE;
5674 trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
5676 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5677 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5679 nfs4_put_stid(&dp->dl_stid);
5682 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5683 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5684 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5685 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5686 open->op_recall = true;
5689 /* 4.1 client asking for a delegation? */
5690 if (open->op_deleg_want)
5691 nfsd4_open_deleg_none_ext(open, status);
5695 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5696 struct nfs4_delegation *dp)
5698 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5699 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5700 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5701 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5702 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5703 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5704 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5705 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5707 /* Otherwise the client must be confused wanting a delegation
5708 * it already has, therefore we don't return
5709 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5714 * nfsd4_process_open2 - finish open processing
5715 * @rqstp: the RPC transaction being executed
5716 * @current_fh: NFSv4 COMPOUND's current filehandle
5717 * @open: OPEN arguments
5719 * If successful, (1) truncate the file if open->op_truncate was
5720 * set, (2) set open->op_stateid, (3) set open->op_delegation.
5722 * Returns %nfs_ok on success; otherwise an nfs4stat value in
5723 * network byte order is returned.
5726 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5728 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5729 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5730 struct nfs4_file *fp = NULL;
5731 struct nfs4_ol_stateid *stp = NULL;
5732 struct nfs4_delegation *dp = NULL;
5734 bool new_stp = false;
5737 * Lookup file; if found, lookup stateid and check open request,
5738 * and check for delegations in the process of being recalled.
5739 * If not found, create the nfs4_file struct
5741 fp = nfsd4_file_hash_insert(open->op_file, current_fh);
5743 return nfserr_jukebox;
5744 if (fp != open->op_file) {
5745 status = nfs4_check_deleg(cl, open, &dp);
5748 stp = nfsd4_find_and_lock_existing_open(fp, open);
5750 open->op_file = NULL;
5751 status = nfserr_bad_stateid;
5752 if (nfsd4_is_deleg_cur(open))
5757 stp = init_open_stateid(fp, open);
5763 * OPEN the file, or upgrade an existing OPEN.
5764 * If truncate fails, the OPEN fails.
5766 * stp is already locked.
5769 /* Stateid was found, this is an OPEN upgrade */
5770 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5772 mutex_unlock(&stp->st_mutex);
5776 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
5778 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5779 release_open_stateid(stp);
5780 mutex_unlock(&stp->st_mutex);
5784 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5786 if (stp->st_clnt_odstate == open->op_odstate)
5787 open->op_odstate = NULL;
5790 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5791 mutex_unlock(&stp->st_mutex);
5793 if (nfsd4_has_session(&resp->cstate)) {
5794 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5795 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5796 open->op_why_no_deleg = WND4_NOT_WANTED;
5802 * Attempt to hand out a delegation. No error return, because the
5803 * OPEN succeeds even if we fail.
5805 nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
5808 trace_nfsd_open(&stp->st_stid.sc_stateid);
5810 /* 4.1 client trying to upgrade/downgrade delegation? */
5811 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5812 open->op_deleg_want)
5813 nfsd4_deleg_xgrade_none_ext(open, dp);
5817 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5818 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5820 * To finish the open response, we just need to set the rflags.
5822 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5823 if (nfsd4_has_session(&resp->cstate))
5824 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5825 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5826 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5829 nfs4_put_stid(&dp->dl_stid);
5831 nfs4_put_stid(&stp->st_stid);
5836 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5837 struct nfsd4_open *open)
5839 if (open->op_openowner) {
5840 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5842 nfsd4_cstate_assign_replay(cstate, so);
5843 nfs4_put_stateowner(so);
5846 kmem_cache_free(file_slab, open->op_file);
5848 nfs4_put_stid(&open->op_stp->st_stid);
5849 if (open->op_odstate)
5850 kmem_cache_free(odstate_slab, open->op_odstate);
5854 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5855 union nfsd4_op_u *u)
5857 clientid_t *clid = &u->renew;
5858 struct nfs4_client *clp;
5860 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5862 trace_nfsd_clid_renew(clid);
5863 status = set_client(clid, cstate, nn);
5867 if (!list_empty(&clp->cl_delegations)
5868 && clp->cl_cb_state != NFSD4_CB_UP)
5869 return nfserr_cb_path_down;
5874 nfsd4_end_grace(struct nfsd_net *nn)
5876 /* do nothing if grace period already ended */
5877 if (nn->grace_ended)
5880 trace_nfsd_grace_complete(nn);
5881 nn->grace_ended = true;
5883 * If the server goes down again right now, an NFSv4
5884 * client will still be allowed to reclaim after it comes back up,
5885 * even if it hasn't yet had a chance to reclaim state this time.
5888 nfsd4_record_grace_done(nn);
5890 * At this point, NFSv4 clients can still reclaim. But if the
5891 * server crashes, any that have not yet reclaimed will be out
5892 * of luck on the next boot.
5894 * (NFSv4.1+ clients are considered to have reclaimed once they
5895 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5896 * have reclaimed after their first OPEN.)
5898 locks_end_grace(&nn->nfsd4_manager);
5900 * At this point, and once lockd and/or any other containers
5901 * exit their grace period, further reclaims will fail and
5902 * regular locking can resume.
5907 * If we've waited a lease period but there are still clients trying to
5908 * reclaim, wait a little longer to give them a chance to finish.
5910 static bool clients_still_reclaiming(struct nfsd_net *nn)
5912 time64_t double_grace_period_end = nn->boot_time +
5913 2 * nn->nfsd4_lease;
5915 if (nn->track_reclaim_completes &&
5916 atomic_read(&nn->nr_reclaim_complete) ==
5917 nn->reclaim_str_hashtbl_size)
5919 if (!nn->somebody_reclaimed)
5921 nn->somebody_reclaimed = false;
5923 * If we've given them *two* lease times to reclaim, and they're
5924 * still not done, give up:
5926 if (ktime_get_boottime_seconds() > double_grace_period_end)
5931 struct laundry_time {
5936 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5938 time64_t time_remaining;
5940 if (last_refresh < lt->cutoff)
5942 time_remaining = last_refresh - lt->cutoff;
5943 lt->new_timeo = min(lt->new_timeo, time_remaining);
5947 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
5948 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5950 spin_lock_init(&nn->nfsd_ssc_lock);
5951 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5952 init_waitqueue_head(&nn->nfsd_ssc_waitq);
5954 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5957 * This is called when nfsd is being shutdown, after all inter_ssc
5958 * cleanup were done, to destroy the ssc delayed unmount list.
5960 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5962 struct nfsd4_ssc_umount_item *ni = NULL;
5963 struct nfsd4_ssc_umount_item *tmp;
5965 spin_lock(&nn->nfsd_ssc_lock);
5966 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5967 list_del(&ni->nsui_list);
5968 spin_unlock(&nn->nfsd_ssc_lock);
5969 mntput(ni->nsui_vfsmount);
5971 spin_lock(&nn->nfsd_ssc_lock);
5973 spin_unlock(&nn->nfsd_ssc_lock);
5976 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5978 bool do_wakeup = false;
5979 struct nfsd4_ssc_umount_item *ni = NULL;
5980 struct nfsd4_ssc_umount_item *tmp;
5982 spin_lock(&nn->nfsd_ssc_lock);
5983 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5984 if (time_after(jiffies, ni->nsui_expire)) {
5985 if (refcount_read(&ni->nsui_refcnt) > 1)
5988 /* mark being unmount */
5989 ni->nsui_busy = true;
5990 spin_unlock(&nn->nfsd_ssc_lock);
5991 mntput(ni->nsui_vfsmount);
5992 spin_lock(&nn->nfsd_ssc_lock);
5994 /* waiters need to start from begin of list */
5995 list_del(&ni->nsui_list);
5998 /* wakeup ssc_connect waiters */
6005 wake_up_all(&nn->nfsd_ssc_waitq);
6006 spin_unlock(&nn->nfsd_ssc_lock);
6010 /* Check if any lock belonging to this lockowner has any blockers */
6012 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
6014 struct file_lock_context *ctx;
6015 struct nfs4_ol_stateid *stp;
6016 struct nfs4_file *nf;
6018 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
6019 nf = stp->st_stid.sc_file;
6020 ctx = locks_inode_context(nf->fi_inode);
6023 if (locks_owner_has_blockers(ctx, lo))
6030 nfs4_anylock_blockers(struct nfs4_client *clp)
6033 struct nfs4_stateowner *so;
6034 struct nfs4_lockowner *lo;
6036 if (atomic_read(&clp->cl_delegs_in_recall))
6038 spin_lock(&clp->cl_lock);
6039 for (i = 0; i < OWNER_HASH_SIZE; i++) {
6040 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
6042 if (so->so_is_open_owner)
6045 if (nfs4_lockowner_has_blockers(lo)) {
6046 spin_unlock(&clp->cl_lock);
6051 spin_unlock(&clp->cl_lock);
6056 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
6057 struct laundry_time *lt)
6059 unsigned int maxreap, reapcnt = 0;
6060 struct list_head *pos, *next;
6061 struct nfs4_client *clp;
6063 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
6064 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
6065 INIT_LIST_HEAD(reaplist);
6066 spin_lock(&nn->client_lock);
6067 list_for_each_safe(pos, next, &nn->client_lru) {
6068 clp = list_entry(pos, struct nfs4_client, cl_lru);
6069 if (clp->cl_state == NFSD4_EXPIRABLE)
6071 if (!state_expired(lt, clp->cl_time))
6073 if (!atomic_read(&clp->cl_rpc_users)) {
6074 if (clp->cl_state == NFSD4_ACTIVE)
6075 atomic_inc(&nn->nfsd_courtesy_clients);
6076 clp->cl_state = NFSD4_COURTESY;
6078 if (!client_has_state(clp))
6080 if (!nfs4_anylock_blockers(clp))
6081 if (reapcnt >= maxreap)
6084 if (!mark_client_expired_locked(clp)) {
6085 list_add(&clp->cl_lru, reaplist);
6089 spin_unlock(&nn->client_lock);
6093 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
6094 struct list_head *reaplist)
6096 unsigned int maxreap = 0, reapcnt = 0;
6097 struct list_head *pos, *next;
6098 struct nfs4_client *clp;
6100 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
6101 INIT_LIST_HEAD(reaplist);
6103 spin_lock(&nn->client_lock);
6104 list_for_each_safe(pos, next, &nn->client_lru) {
6105 clp = list_entry(pos, struct nfs4_client, cl_lru);
6106 if (clp->cl_state == NFSD4_ACTIVE)
6108 if (reapcnt >= maxreap)
6110 if (!mark_client_expired_locked(clp)) {
6111 list_add(&clp->cl_lru, reaplist);
6115 spin_unlock(&nn->client_lock);
6119 nfs4_process_client_reaplist(struct list_head *reaplist)
6121 struct list_head *pos, *next;
6122 struct nfs4_client *clp;
6124 list_for_each_safe(pos, next, reaplist) {
6125 clp = list_entry(pos, struct nfs4_client, cl_lru);
6126 trace_nfsd_clid_purged(&clp->cl_clientid);
6127 list_del_init(&clp->cl_lru);
6133 nfs4_laundromat(struct nfsd_net *nn)
6135 struct nfs4_openowner *oo;
6136 struct nfs4_delegation *dp;
6137 struct nfs4_ol_stateid *stp;
6138 struct nfsd4_blocked_lock *nbl;
6139 struct list_head *pos, *next, reaplist;
6140 struct laundry_time lt = {
6141 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6142 .new_timeo = nn->nfsd4_lease
6144 struct nfs4_cpntf_state *cps;
6145 copy_stateid_t *cps_t;
6148 if (clients_still_reclaiming(nn)) {
6152 nfsd4_end_grace(nn);
6154 spin_lock(&nn->s2s_cp_lock);
6155 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6156 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6157 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
6158 state_expired(<, cps->cpntf_time))
6159 _free_cpntf_state_locked(nn, cps);
6161 spin_unlock(&nn->s2s_cp_lock);
6162 nfs4_get_client_reaplist(nn, &reaplist, <);
6163 nfs4_process_client_reaplist(&reaplist);
6165 spin_lock(&state_lock);
6166 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6167 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6168 if (!state_expired(<, dp->dl_time))
6170 WARN_ON(!unhash_delegation_locked(dp));
6171 list_add(&dp->dl_recall_lru, &reaplist);
6173 spin_unlock(&state_lock);
6174 while (!list_empty(&reaplist)) {
6175 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6177 list_del_init(&dp->dl_recall_lru);
6178 revoke_delegation(dp);
6181 spin_lock(&nn->client_lock);
6182 while (!list_empty(&nn->close_lru)) {
6183 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6185 if (!state_expired(<, oo->oo_time))
6187 list_del_init(&oo->oo_close_lru);
6188 stp = oo->oo_last_closed_stid;
6189 oo->oo_last_closed_stid = NULL;
6190 spin_unlock(&nn->client_lock);
6191 nfs4_put_stid(&stp->st_stid);
6192 spin_lock(&nn->client_lock);
6194 spin_unlock(&nn->client_lock);
6197 * It's possible for a client to try and acquire an already held lock
6198 * that is being held for a long time, and then lose interest in it.
6199 * So, we clean out any un-revisited request after a lease period
6200 * under the assumption that the client is no longer interested.
6202 * RFC5661, sec. 9.6 states that the client must not rely on getting
6203 * notifications and must continue to poll for locks, even when the
6204 * server supports them. Thus this shouldn't lead to clients blocking
6205 * indefinitely once the lock does become free.
6207 BUG_ON(!list_empty(&reaplist));
6208 spin_lock(&nn->blocked_locks_lock);
6209 while (!list_empty(&nn->blocked_locks_lru)) {
6210 nbl = list_first_entry(&nn->blocked_locks_lru,
6211 struct nfsd4_blocked_lock, nbl_lru);
6212 if (!state_expired(<, nbl->nbl_time))
6214 list_move(&nbl->nbl_lru, &reaplist);
6215 list_del_init(&nbl->nbl_list);
6217 spin_unlock(&nn->blocked_locks_lock);
6219 while (!list_empty(&reaplist)) {
6220 nbl = list_first_entry(&reaplist,
6221 struct nfsd4_blocked_lock, nbl_lru);
6222 list_del_init(&nbl->nbl_lru);
6223 free_blocked_lock(nbl);
6225 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6226 /* service the server-to-server copy delayed unmount list */
6227 nfsd4_ssc_expire_umount(nn);
6230 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
6233 static void laundromat_main(struct work_struct *);
6236 laundromat_main(struct work_struct *laundry)
6239 struct delayed_work *dwork = to_delayed_work(laundry);
6240 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6243 t = nfs4_laundromat(nn);
6244 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
6248 courtesy_client_reaper(struct nfsd_net *nn)
6250 struct list_head reaplist;
6252 nfs4_get_courtesy_client_reaplist(nn, &reaplist);
6253 nfs4_process_client_reaplist(&reaplist);
6257 deleg_reaper(struct nfsd_net *nn)
6259 struct list_head *pos, *next;
6260 struct nfs4_client *clp;
6261 struct list_head cblist;
6263 INIT_LIST_HEAD(&cblist);
6264 spin_lock(&nn->client_lock);
6265 list_for_each_safe(pos, next, &nn->client_lru) {
6266 clp = list_entry(pos, struct nfs4_client, cl_lru);
6267 if (clp->cl_state != NFSD4_ACTIVE ||
6268 list_empty(&clp->cl_delegations) ||
6269 atomic_read(&clp->cl_delegs_in_recall) ||
6270 test_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags) ||
6271 (ktime_get_boottime_seconds() -
6272 clp->cl_ra_time < 5)) {
6275 list_add(&clp->cl_ra_cblist, &cblist);
6277 /* release in nfsd4_cb_recall_any_release */
6278 atomic_inc(&clp->cl_rpc_users);
6279 set_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
6280 clp->cl_ra_time = ktime_get_boottime_seconds();
6282 spin_unlock(&nn->client_lock);
6284 while (!list_empty(&cblist)) {
6285 clp = list_first_entry(&cblist, struct nfs4_client,
6287 list_del_init(&clp->cl_ra_cblist);
6288 clp->cl_ra->ra_keep = 0;
6289 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG);
6290 trace_nfsd_cb_recall_any(clp->cl_ra);
6291 nfsd4_run_cb(&clp->cl_ra->ra_cb);
6296 nfsd4_state_shrinker_worker(struct work_struct *work)
6298 struct nfsd_net *nn = container_of(work, struct nfsd_net,
6299 nfsd_shrinker_work);
6301 courtesy_client_reaper(nn);
6305 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
6307 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
6308 return nfserr_bad_stateid;
6313 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
6315 __be32 status = nfserr_openmode;
6317 /* For lock stateid's, we test the parent open, not the lock: */
6318 if (stp->st_openstp)
6319 stp = stp->st_openstp;
6320 if ((flags & WR_STATE) && !access_permit_write(stp))
6322 if ((flags & RD_STATE) && !access_permit_read(stp))
6329 static inline __be32
6330 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
6332 if (ONE_STATEID(stateid) && (flags & RD_STATE))
6334 else if (opens_in_grace(net)) {
6335 /* Answer in remaining cases depends on existence of
6336 * conflicting state; so we must wait out the grace period. */
6337 return nfserr_grace;
6338 } else if (flags & WR_STATE)
6339 return nfs4_share_conflict(current_fh,
6340 NFS4_SHARE_DENY_WRITE);
6341 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
6342 return nfs4_share_conflict(current_fh,
6343 NFS4_SHARE_DENY_READ);
6346 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
6349 * When sessions are used the stateid generation number is ignored
6352 if (has_session && in->si_generation == 0)
6355 if (in->si_generation == ref->si_generation)
6358 /* If the client sends us a stateid from the future, it's buggy: */
6359 if (nfsd4_stateid_generation_after(in, ref))
6360 return nfserr_bad_stateid;
6362 * However, we could see a stateid from the past, even from a
6363 * non-buggy client. For example, if the client sends a lock
6364 * while some IO is outstanding, the lock may bump si_generation
6365 * while the IO is still in flight. The client could avoid that
6366 * situation by waiting for responses on all the IO requests,
6367 * but better performance may result in retrying IO that
6368 * receives an old_stateid error if requests are rarely
6369 * reordered in flight:
6371 return nfserr_old_stateid;
6374 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
6378 spin_lock(&s->sc_lock);
6379 ret = nfsd4_verify_open_stid(s);
6381 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
6382 spin_unlock(&s->sc_lock);
6386 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6388 if (ols->st_stateowner->so_is_open_owner &&
6389 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6390 return nfserr_bad_stateid;
6394 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
6396 struct nfs4_stid *s;
6397 __be32 status = nfserr_bad_stateid;
6399 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6400 CLOSE_STATEID(stateid))
6402 spin_lock(&cl->cl_lock);
6403 s = find_stateid_locked(cl, stateid);
6406 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
6409 switch (s->sc_type) {
6410 case NFS4_DELEG_STID:
6413 case NFS4_REVOKED_DELEG_STID:
6414 status = nfserr_deleg_revoked;
6416 case NFS4_OPEN_STID:
6417 case NFS4_LOCK_STID:
6418 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
6421 printk("unknown stateid type %x\n", s->sc_type);
6423 case NFS4_CLOSED_STID:
6424 case NFS4_CLOSED_DELEG_STID:
6425 status = nfserr_bad_stateid;
6428 spin_unlock(&cl->cl_lock);
6433 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6434 stateid_t *stateid, unsigned char typemask,
6435 struct nfs4_stid **s, struct nfsd_net *nn)
6438 struct nfs4_stid *stid;
6439 bool return_revoked = false;
6442 * only return revoked delegations if explicitly asked.
6443 * otherwise we report revoked or bad_stateid status.
6445 if (typemask & NFS4_REVOKED_DELEG_STID)
6446 return_revoked = true;
6447 else if (typemask & NFS4_DELEG_STID)
6448 typemask |= NFS4_REVOKED_DELEG_STID;
6450 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6451 CLOSE_STATEID(stateid))
6452 return nfserr_bad_stateid;
6453 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
6454 if (status == nfserr_stale_clientid) {
6455 if (cstate->session)
6456 return nfserr_bad_stateid;
6457 return nfserr_stale_stateid;
6461 stid = find_stateid_by_type(cstate->clp, stateid, typemask);
6463 return nfserr_bad_stateid;
6464 if ((stid->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
6465 nfs4_put_stid(stid);
6466 if (cstate->minorversion)
6467 return nfserr_deleg_revoked;
6468 return nfserr_bad_stateid;
6474 static struct nfsd_file *
6475 nfs4_find_file(struct nfs4_stid *s, int flags)
6477 struct nfsd_file *ret = NULL;
6482 switch (s->sc_type) {
6483 case NFS4_DELEG_STID:
6484 spin_lock(&s->sc_file->fi_lock);
6485 ret = nfsd_file_get(s->sc_file->fi_deleg_file);
6486 spin_unlock(&s->sc_file->fi_lock);
6488 case NFS4_OPEN_STID:
6489 case NFS4_LOCK_STID:
6490 if (flags & RD_STATE)
6491 ret = find_readable_file(s->sc_file);
6493 ret = find_writeable_file(s->sc_file);
6500 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
6504 status = nfsd4_check_openowner_confirmed(ols);
6507 return nfs4_check_openmode(ols, flags);
6511 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
6512 struct nfsd_file **nfp, int flags)
6514 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6515 struct nfsd_file *nf;
6518 nf = nfs4_find_file(s, flags);
6520 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6521 acc | NFSD_MAY_OWNER_OVERRIDE);
6527 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
6536 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6538 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
6539 if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
6541 list_del(&cps->cp_list);
6542 idr_remove(&nn->s2s_cp_stateids,
6543 cps->cp_stateid.cs_stid.si_opaque.so_id);
6547 * A READ from an inter server to server COPY will have a
6548 * copy stateid. Look up the copy notify stateid from the
6549 * idr structure and take a reference on it.
6551 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6552 struct nfs4_client *clp,
6553 struct nfs4_cpntf_state **cps)
6555 copy_stateid_t *cps_t;
6556 struct nfs4_cpntf_state *state = NULL;
6558 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6559 return nfserr_bad_stateid;
6560 spin_lock(&nn->s2s_cp_lock);
6561 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6563 state = container_of(cps_t, struct nfs4_cpntf_state,
6565 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
6570 refcount_inc(&state->cp_stateid.cs_count);
6572 _free_cpntf_state_locked(nn, state);
6575 spin_unlock(&nn->s2s_cp_lock);
6577 return nfserr_bad_stateid;
6583 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6584 struct nfs4_stid **stid)
6587 struct nfs4_cpntf_state *cps = NULL;
6588 struct nfs4_client *found;
6590 status = manage_cpntf_state(nn, st, NULL, &cps);
6594 cps->cpntf_time = ktime_get_boottime_seconds();
6596 status = nfserr_expired;
6597 found = lookup_clientid(&cps->cp_p_clid, true, nn);
6601 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6602 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6606 status = nfserr_bad_stateid;
6608 put_client_renew(found);
6610 nfs4_put_cpntf_state(nn, cps);
6614 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6616 spin_lock(&nn->s2s_cp_lock);
6617 _free_cpntf_state_locked(nn, cps);
6618 spin_unlock(&nn->s2s_cp_lock);
6622 * nfs4_preprocess_stateid_op - find and prep stateid for an operation
6623 * @rqstp: incoming request from client
6624 * @cstate: current compound state
6625 * @fhp: filehandle associated with requested stateid
6626 * @stateid: stateid (provided by client)
6627 * @flags: flags describing type of operation to be done
6628 * @nfp: optional nfsd_file return pointer (may be NULL)
6629 * @cstid: optional returned nfs4_stid pointer (may be NULL)
6631 * Given info from the client, look up a nfs4_stid for the operation. On
6632 * success, it returns a reference to the nfs4_stid and/or the nfsd_file
6633 * associated with it.
6636 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
6637 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
6638 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6639 struct nfs4_stid **cstid)
6641 struct net *net = SVC_NET(rqstp);
6642 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6643 struct nfs4_stid *s = NULL;
6649 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6651 status = nfserr_bad_stateid;
6653 status = check_special_stateids(net, fhp, stateid,
6658 status = nfsd4_lookup_stateid(cstate, stateid,
6659 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
6661 if (status == nfserr_bad_stateid)
6662 status = find_cpntf_state(nn, stateid, &s);
6665 status = nfsd4_stid_check_stateid_generation(stateid, s,
6666 nfsd4_has_session(cstate));
6670 switch (s->sc_type) {
6671 case NFS4_DELEG_STID:
6672 status = nfs4_check_delegmode(delegstateid(s), flags);
6674 case NFS4_OPEN_STID:
6675 case NFS4_LOCK_STID:
6676 status = nfs4_check_olstateid(openlockstateid(s), flags);
6679 status = nfserr_bad_stateid;
6684 status = nfs4_check_fh(fhp, s);
6687 if (status == nfs_ok && nfp)
6688 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
6691 if (!status && cstid)
6700 * Test if the stateid is valid
6703 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6704 union nfsd4_op_u *u)
6706 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
6707 struct nfsd4_test_stateid_id *stateid;
6708 struct nfs4_client *cl = cstate->clp;
6710 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
6711 stateid->ts_id_status =
6712 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
6718 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6720 struct nfs4_ol_stateid *stp = openlockstateid(s);
6723 ret = nfsd4_lock_ol_stateid(stp);
6727 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6731 ret = nfserr_locks_held;
6732 if (check_for_locks(stp->st_stid.sc_file,
6733 lockowner(stp->st_stateowner)))
6736 release_lock_stateid(stp);
6740 mutex_unlock(&stp->st_mutex);
6747 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6748 union nfsd4_op_u *u)
6750 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
6751 stateid_t *stateid = &free_stateid->fr_stateid;
6752 struct nfs4_stid *s;
6753 struct nfs4_delegation *dp;
6754 struct nfs4_client *cl = cstate->clp;
6755 __be32 ret = nfserr_bad_stateid;
6757 spin_lock(&cl->cl_lock);
6758 s = find_stateid_locked(cl, stateid);
6761 spin_lock(&s->sc_lock);
6762 switch (s->sc_type) {
6763 case NFS4_DELEG_STID:
6764 ret = nfserr_locks_held;
6766 case NFS4_OPEN_STID:
6767 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6770 ret = nfserr_locks_held;
6772 case NFS4_LOCK_STID:
6773 spin_unlock(&s->sc_lock);
6774 refcount_inc(&s->sc_count);
6775 spin_unlock(&cl->cl_lock);
6776 ret = nfsd4_free_lock_stateid(stateid, s);
6778 case NFS4_REVOKED_DELEG_STID:
6779 spin_unlock(&s->sc_lock);
6780 dp = delegstateid(s);
6781 list_del_init(&dp->dl_recall_lru);
6782 spin_unlock(&cl->cl_lock);
6786 /* Default falls through and returns nfserr_bad_stateid */
6788 spin_unlock(&s->sc_lock);
6790 spin_unlock(&cl->cl_lock);
6798 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6799 RD_STATE : WR_STATE;
6802 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6804 struct svc_fh *current_fh = &cstate->current_fh;
6805 struct nfs4_stateowner *sop = stp->st_stateowner;
6808 status = nfsd4_check_seqid(cstate, sop, seqid);
6811 status = nfsd4_lock_ol_stateid(stp);
6812 if (status != nfs_ok)
6814 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6815 if (status == nfs_ok)
6816 status = nfs4_check_fh(current_fh, &stp->st_stid);
6817 if (status != nfs_ok)
6818 mutex_unlock(&stp->st_mutex);
6823 * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op
6824 * @cstate: compund state
6825 * @seqid: seqid (provided by client)
6826 * @stateid: stateid (provided by client)
6827 * @typemask: mask of allowable types for this operation
6828 * @stpp: return pointer for the stateid found
6829 * @nn: net namespace for request
6831 * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and
6832 * return it in @stpp. On a nfs_ok return, the returned stateid will
6833 * have its st_mutex locked.
6836 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6837 stateid_t *stateid, char typemask,
6838 struct nfs4_ol_stateid **stpp,
6839 struct nfsd_net *nn)
6842 struct nfs4_stid *s;
6843 struct nfs4_ol_stateid *stp = NULL;
6845 trace_nfsd_preprocess(seqid, stateid);
6848 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6851 stp = openlockstateid(s);
6852 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6854 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6858 nfs4_put_stid(&stp->st_stid);
6862 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6863 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6866 struct nfs4_openowner *oo;
6867 struct nfs4_ol_stateid *stp;
6869 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6870 NFS4_OPEN_STID, &stp, nn);
6873 oo = openowner(stp->st_stateowner);
6874 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6875 mutex_unlock(&stp->st_mutex);
6876 nfs4_put_stid(&stp->st_stid);
6877 return nfserr_bad_stateid;
6884 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6885 union nfsd4_op_u *u)
6887 struct nfsd4_open_confirm *oc = &u->open_confirm;
6889 struct nfs4_openowner *oo;
6890 struct nfs4_ol_stateid *stp;
6891 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6893 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6894 cstate->current_fh.fh_dentry);
6896 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6900 status = nfs4_preprocess_seqid_op(cstate,
6901 oc->oc_seqid, &oc->oc_req_stateid,
6902 NFS4_OPEN_STID, &stp, nn);
6905 oo = openowner(stp->st_stateowner);
6906 status = nfserr_bad_stateid;
6907 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6908 mutex_unlock(&stp->st_mutex);
6911 oo->oo_flags |= NFS4_OO_CONFIRMED;
6912 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6913 mutex_unlock(&stp->st_mutex);
6914 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6915 nfsd4_client_record_create(oo->oo_owner.so_client);
6918 nfs4_put_stid(&stp->st_stid);
6920 nfsd4_bump_seqid(cstate, status);
6924 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6926 if (!test_access(access, stp))
6928 nfs4_file_put_access(stp->st_stid.sc_file, access);
6929 clear_access(access, stp);
6932 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6934 switch (to_access) {
6935 case NFS4_SHARE_ACCESS_READ:
6936 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6937 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6939 case NFS4_SHARE_ACCESS_WRITE:
6940 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6941 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6943 case NFS4_SHARE_ACCESS_BOTH:
6951 nfsd4_open_downgrade(struct svc_rqst *rqstp,
6952 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
6954 struct nfsd4_open_downgrade *od = &u->open_downgrade;
6956 struct nfs4_ol_stateid *stp;
6957 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6959 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6960 cstate->current_fh.fh_dentry);
6962 /* We don't yet support WANT bits: */
6963 if (od->od_deleg_want)
6964 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6967 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
6968 &od->od_stateid, &stp, nn);
6971 status = nfserr_inval;
6972 if (!test_access(od->od_share_access, stp)) {
6973 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6974 stp->st_access_bmap, od->od_share_access);
6977 if (!test_deny(od->od_share_deny, stp)) {
6978 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6979 stp->st_deny_bmap, od->od_share_deny);
6982 nfs4_stateid_downgrade(stp, od->od_share_access);
6983 reset_union_bmap_deny(od->od_share_deny, stp);
6984 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
6987 mutex_unlock(&stp->st_mutex);
6988 nfs4_put_stid(&stp->st_stid);
6990 nfsd4_bump_seqid(cstate, status);
6994 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6996 struct nfs4_client *clp = s->st_stid.sc_client;
6998 LIST_HEAD(reaplist);
6999 struct nfs4_ol_stateid *stp;
7001 spin_lock(&clp->cl_lock);
7002 unhashed = unhash_open_stateid(s, &reaplist);
7004 if (clp->cl_minorversion) {
7006 put_ol_stateid_locked(s, &reaplist);
7007 spin_unlock(&clp->cl_lock);
7008 list_for_each_entry(stp, &reaplist, st_locks)
7009 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
7010 free_ol_stateid_reaplist(&reaplist);
7012 spin_unlock(&clp->cl_lock);
7013 free_ol_stateid_reaplist(&reaplist);
7015 move_to_close_lru(s, clp->net);
7020 * nfs4_unlock_state() called after encode
7023 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7024 union nfsd4_op_u *u)
7026 struct nfsd4_close *close = &u->close;
7028 struct nfs4_ol_stateid *stp;
7029 struct net *net = SVC_NET(rqstp);
7030 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7032 dprintk("NFSD: nfsd4_close on file %pd\n",
7033 cstate->current_fh.fh_dentry);
7035 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
7037 NFS4_OPEN_STID|NFS4_CLOSED_STID,
7039 nfsd4_bump_seqid(cstate, status);
7043 stp->st_stid.sc_type = NFS4_CLOSED_STID;
7046 * Technically we don't _really_ have to increment or copy it, since
7047 * it should just be gone after this operation and we clobber the
7048 * copied value below, but we continue to do so here just to ensure
7049 * that racing ops see that there was a state change.
7051 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
7053 nfsd4_close_open_stateid(stp);
7054 mutex_unlock(&stp->st_mutex);
7056 /* v4.1+ suggests that we send a special stateid in here, since the
7057 * clients should just ignore this anyway. Since this is not useful
7058 * for v4.0 clients either, we set it to the special close_stateid
7061 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
7063 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
7065 /* put reference from nfs4_preprocess_seqid_op */
7066 nfs4_put_stid(&stp->st_stid);
7072 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7073 union nfsd4_op_u *u)
7075 struct nfsd4_delegreturn *dr = &u->delegreturn;
7076 struct nfs4_delegation *dp;
7077 stateid_t *stateid = &dr->dr_stateid;
7078 struct nfs4_stid *s;
7080 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7082 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7085 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
7088 dp = delegstateid(s);
7089 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
7093 trace_nfsd_deleg_return(stateid);
7094 wake_up_var(d_inode(cstate->current_fh.fh_dentry));
7095 destroy_delegation(dp);
7097 nfs4_put_stid(&dp->dl_stid);
7102 /* last octet in a range */
7104 last_byte_offset(u64 start, u64 len)
7110 return end > start ? end - 1: NFS4_MAX_UINT64;
7114 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
7115 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
7116 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
7117 * locking, this prevents us from being completely protocol-compliant. The
7118 * real solution to this problem is to start using unsigned file offsets in
7119 * the VFS, but this is a very deep change!
7122 nfs4_transform_lock_offset(struct file_lock *lock)
7124 if (lock->fl_start < 0)
7125 lock->fl_start = OFFSET_MAX;
7126 if (lock->fl_end < 0)
7127 lock->fl_end = OFFSET_MAX;
7131 nfsd4_lm_get_owner(fl_owner_t owner)
7133 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7135 nfs4_get_stateowner(&lo->lo_owner);
7140 nfsd4_lm_put_owner(fl_owner_t owner)
7142 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7145 nfs4_put_stateowner(&lo->lo_owner);
7148 /* return pointer to struct nfs4_client if client is expirable */
7150 nfsd4_lm_lock_expirable(struct file_lock *cfl)
7152 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
7153 struct nfs4_client *clp = lo->lo_owner.so_client;
7154 struct nfsd_net *nn;
7156 if (try_to_expire_client(clp)) {
7157 nn = net_generic(clp->net, nfsd_net_id);
7158 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
7164 /* schedule laundromat to run immediately and wait for it to complete */
7166 nfsd4_lm_expire_lock(void)
7168 flush_workqueue(laundry_wq);
7172 nfsd4_lm_notify(struct file_lock *fl)
7174 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
7175 struct net *net = lo->lo_owner.so_client->net;
7176 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7177 struct nfsd4_blocked_lock *nbl = container_of(fl,
7178 struct nfsd4_blocked_lock, nbl_lock);
7181 /* An empty list means that something else is going to be using it */
7182 spin_lock(&nn->blocked_locks_lock);
7183 if (!list_empty(&nbl->nbl_list)) {
7184 list_del_init(&nbl->nbl_list);
7185 list_del_init(&nbl->nbl_lru);
7188 spin_unlock(&nn->blocked_locks_lock);
7191 trace_nfsd_cb_notify_lock(lo, nbl);
7192 nfsd4_run_cb(&nbl->nbl_cb);
7196 static const struct lock_manager_operations nfsd_posix_mng_ops = {
7197 .lm_mod_owner = THIS_MODULE,
7198 .lm_notify = nfsd4_lm_notify,
7199 .lm_get_owner = nfsd4_lm_get_owner,
7200 .lm_put_owner = nfsd4_lm_put_owner,
7201 .lm_lock_expirable = nfsd4_lm_lock_expirable,
7202 .lm_expire_lock = nfsd4_lm_expire_lock,
7206 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
7208 struct nfs4_lockowner *lo;
7210 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
7211 lo = (struct nfs4_lockowner *) fl->fl_owner;
7212 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
7214 if (!deny->ld_owner.data)
7215 /* We just don't care that much */
7217 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
7220 deny->ld_owner.len = 0;
7221 deny->ld_owner.data = NULL;
7222 deny->ld_clientid.cl_boot = 0;
7223 deny->ld_clientid.cl_id = 0;
7225 deny->ld_start = fl->fl_start;
7226 deny->ld_length = NFS4_MAX_UINT64;
7227 if (fl->fl_end != NFS4_MAX_UINT64)
7228 deny->ld_length = fl->fl_end - fl->fl_start + 1;
7229 deny->ld_type = NFS4_READ_LT;
7230 if (fl->fl_type != F_RDLCK)
7231 deny->ld_type = NFS4_WRITE_LT;
7234 static struct nfs4_lockowner *
7235 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
7237 unsigned int strhashval = ownerstr_hashval(owner);
7238 struct nfs4_stateowner *so;
7240 lockdep_assert_held(&clp->cl_lock);
7242 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7244 if (so->so_is_open_owner)
7246 if (same_owner_str(so, owner))
7247 return lockowner(nfs4_get_stateowner(so));
7252 static struct nfs4_lockowner *
7253 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
7255 struct nfs4_lockowner *lo;
7257 spin_lock(&clp->cl_lock);
7258 lo = find_lockowner_str_locked(clp, owner);
7259 spin_unlock(&clp->cl_lock);
7263 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
7265 unhash_lockowner_locked(lockowner(sop));
7268 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
7270 struct nfs4_lockowner *lo = lockowner(sop);
7272 kmem_cache_free(lockowner_slab, lo);
7275 static const struct nfs4_stateowner_operations lockowner_ops = {
7276 .so_unhash = nfs4_unhash_lockowner,
7277 .so_free = nfs4_free_lockowner,
7281 * Alloc a lock owner structure.
7282 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
7285 * strhashval = ownerstr_hashval
7287 static struct nfs4_lockowner *
7288 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
7289 struct nfs4_ol_stateid *open_stp,
7290 struct nfsd4_lock *lock)
7292 struct nfs4_lockowner *lo, *ret;
7294 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
7297 INIT_LIST_HEAD(&lo->lo_blocked);
7298 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
7299 lo->lo_owner.so_is_open_owner = 0;
7300 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
7301 lo->lo_owner.so_ops = &lockowner_ops;
7302 spin_lock(&clp->cl_lock);
7303 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
7305 list_add(&lo->lo_owner.so_strhash,
7306 &clp->cl_ownerstr_hashtbl[strhashval]);
7309 nfs4_free_stateowner(&lo->lo_owner);
7311 spin_unlock(&clp->cl_lock);
7315 static struct nfs4_ol_stateid *
7316 find_lock_stateid(const struct nfs4_lockowner *lo,
7317 const struct nfs4_ol_stateid *ost)
7319 struct nfs4_ol_stateid *lst;
7321 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
7323 /* If ost is not hashed, ost->st_locks will not be valid */
7324 if (!nfs4_ol_stateid_unhashed(ost))
7325 list_for_each_entry(lst, &ost->st_locks, st_locks) {
7326 if (lst->st_stateowner == &lo->lo_owner) {
7327 refcount_inc(&lst->st_stid.sc_count);
7334 static struct nfs4_ol_stateid *
7335 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7336 struct nfs4_file *fp, struct inode *inode,
7337 struct nfs4_ol_stateid *open_stp)
7339 struct nfs4_client *clp = lo->lo_owner.so_client;
7340 struct nfs4_ol_stateid *retstp;
7342 mutex_init(&stp->st_mutex);
7343 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
7345 spin_lock(&clp->cl_lock);
7346 if (nfs4_ol_stateid_unhashed(open_stp))
7348 retstp = find_lock_stateid(lo, open_stp);
7351 refcount_inc(&stp->st_stid.sc_count);
7352 stp->st_stid.sc_type = NFS4_LOCK_STID;
7353 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
7355 stp->st_stid.sc_file = fp;
7356 stp->st_access_bmap = 0;
7357 stp->st_deny_bmap = open_stp->st_deny_bmap;
7358 stp->st_openstp = open_stp;
7359 spin_lock(&fp->fi_lock);
7360 list_add(&stp->st_locks, &open_stp->st_locks);
7361 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
7362 list_add(&stp->st_perfile, &fp->fi_stateids);
7363 spin_unlock(&fp->fi_lock);
7364 spin_unlock(&clp->cl_lock);
7367 spin_unlock(&clp->cl_lock);
7368 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
7369 nfs4_put_stid(&retstp->st_stid);
7372 /* To keep mutex tracking happy */
7373 mutex_unlock(&stp->st_mutex);
7376 spin_unlock(&clp->cl_lock);
7377 mutex_unlock(&stp->st_mutex);
7381 static struct nfs4_ol_stateid *
7382 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7383 struct inode *inode, struct nfs4_ol_stateid *ost,
7386 struct nfs4_stid *ns = NULL;
7387 struct nfs4_ol_stateid *lst;
7388 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7389 struct nfs4_client *clp = oo->oo_owner.so_client;
7392 spin_lock(&clp->cl_lock);
7393 lst = find_lock_stateid(lo, ost);
7394 spin_unlock(&clp->cl_lock);
7396 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7398 nfs4_put_stid(&lst->st_stid);
7400 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7404 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7405 if (lst == openlockstateid(ns))
7414 check_lock_length(u64 offset, u64 length)
7416 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7417 (length > ~offset)));
7420 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
7422 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
7424 lockdep_assert_held(&fp->fi_lock);
7426 if (test_access(access, lock_stp))
7428 __nfs4_file_get_access(fp, access);
7429 set_access(access, lock_stp);
7433 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7434 struct nfs4_ol_stateid *ost,
7435 struct nfsd4_lock *lock,
7436 struct nfs4_ol_stateid **plst, bool *new)
7439 struct nfs4_file *fi = ost->st_stid.sc_file;
7440 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7441 struct nfs4_client *cl = oo->oo_owner.so_client;
7442 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
7443 struct nfs4_lockowner *lo;
7444 struct nfs4_ol_stateid *lst;
7445 unsigned int strhashval;
7447 lo = find_lockowner_str(cl, &lock->lk_new_owner);
7449 strhashval = ownerstr_hashval(&lock->lk_new_owner);
7450 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7452 return nfserr_jukebox;
7454 /* with an existing lockowner, seqids must be the same */
7455 status = nfserr_bad_seqid;
7456 if (!cstate->minorversion &&
7457 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
7461 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7463 status = nfserr_jukebox;
7470 nfs4_put_stateowner(&lo->lo_owner);
7478 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7479 union nfsd4_op_u *u)
7481 struct nfsd4_lock *lock = &u->lock;
7482 struct nfs4_openowner *open_sop = NULL;
7483 struct nfs4_lockowner *lock_sop = NULL;
7484 struct nfs4_ol_stateid *lock_stp = NULL;
7485 struct nfs4_ol_stateid *open_stp = NULL;
7486 struct nfs4_file *fp;
7487 struct nfsd_file *nf = NULL;
7488 struct nfsd4_blocked_lock *nbl = NULL;
7489 struct file_lock *file_lock = NULL;
7490 struct file_lock *conflock = NULL;
7491 struct super_block *sb;
7496 unsigned char fl_type;
7497 unsigned int fl_flags = FL_POSIX;
7498 struct net *net = SVC_NET(rqstp);
7499 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7501 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7502 (long long) lock->lk_offset,
7503 (long long) lock->lk_length);
7505 if (check_lock_length(lock->lk_offset, lock->lk_length))
7506 return nfserr_inval;
7508 if ((status = fh_verify(rqstp, &cstate->current_fh,
7509 S_IFREG, NFSD_MAY_LOCK))) {
7510 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7513 sb = cstate->current_fh.fh_dentry->d_sb;
7515 if (lock->lk_is_new) {
7516 if (nfsd4_has_session(cstate))
7517 /* See rfc 5661 18.10.3: given clientid is ignored: */
7518 memcpy(&lock->lk_new_clientid,
7519 &cstate->clp->cl_clientid,
7520 sizeof(clientid_t));
7522 /* validate and update open stateid and open seqid */
7523 status = nfs4_preprocess_confirmed_seqid_op(cstate,
7524 lock->lk_new_open_seqid,
7525 &lock->lk_new_open_stateid,
7529 mutex_unlock(&open_stp->st_mutex);
7530 open_sop = openowner(open_stp->st_stateowner);
7531 status = nfserr_bad_stateid;
7532 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
7533 &lock->lk_new_clientid))
7535 status = lookup_or_create_lock_state(cstate, open_stp, lock,
7538 status = nfs4_preprocess_seqid_op(cstate,
7539 lock->lk_old_lock_seqid,
7540 &lock->lk_old_lock_stateid,
7541 NFS4_LOCK_STID, &lock_stp, nn);
7545 lock_sop = lockowner(lock_stp->st_stateowner);
7547 lkflg = setlkflg(lock->lk_type);
7548 status = nfs4_check_openmode(lock_stp, lkflg);
7552 status = nfserr_grace;
7553 if (locks_in_grace(net) && !lock->lk_reclaim)
7555 status = nfserr_no_grace;
7556 if (!locks_in_grace(net) && lock->lk_reclaim)
7559 if (lock->lk_reclaim)
7560 fl_flags |= FL_RECLAIM;
7562 fp = lock_stp->st_stid.sc_file;
7563 switch (lock->lk_type) {
7565 if (nfsd4_has_session(cstate) ||
7566 exportfs_lock_op_is_async(sb->s_export_op))
7567 fl_flags |= FL_SLEEP;
7570 spin_lock(&fp->fi_lock);
7571 nf = find_readable_file_locked(fp);
7573 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7574 spin_unlock(&fp->fi_lock);
7577 case NFS4_WRITEW_LT:
7578 if (nfsd4_has_session(cstate) ||
7579 exportfs_lock_op_is_async(sb->s_export_op))
7580 fl_flags |= FL_SLEEP;
7583 spin_lock(&fp->fi_lock);
7584 nf = find_writeable_file_locked(fp);
7586 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7587 spin_unlock(&fp->fi_lock);
7591 status = nfserr_inval;
7596 status = nfserr_openmode;
7601 * Most filesystems with their own ->lock operations will block
7602 * the nfsd thread waiting to acquire the lock. That leads to
7603 * deadlocks (we don't want every nfsd thread tied up waiting
7604 * for file locks), so don't attempt blocking lock notifications
7605 * on those filesystems:
7607 if (!exportfs_lock_op_is_async(sb->s_export_op))
7608 fl_flags &= ~FL_SLEEP;
7610 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
7612 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
7613 status = nfserr_jukebox;
7617 file_lock = &nbl->nbl_lock;
7618 file_lock->fl_type = fl_type;
7619 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
7620 file_lock->fl_pid = current->tgid;
7621 file_lock->fl_file = nf->nf_file;
7622 file_lock->fl_flags = fl_flags;
7623 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7624 file_lock->fl_start = lock->lk_offset;
7625 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
7626 nfs4_transform_lock_offset(file_lock);
7628 conflock = locks_alloc_lock();
7630 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7631 status = nfserr_jukebox;
7635 if (fl_flags & FL_SLEEP) {
7636 nbl->nbl_time = ktime_get_boottime_seconds();
7637 spin_lock(&nn->blocked_locks_lock);
7638 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
7639 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
7640 kref_get(&nbl->nbl_kref);
7641 spin_unlock(&nn->blocked_locks_lock);
7644 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
7646 case 0: /* success! */
7647 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
7649 if (lock->lk_reclaim)
7650 nn->somebody_reclaimed = true;
7652 case FILE_LOCK_DEFERRED:
7653 kref_put(&nbl->nbl_kref, free_nbl);
7656 case -EAGAIN: /* conflock holds conflicting lock */
7657 status = nfserr_denied;
7658 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
7659 nfs4_set_lock_denied(conflock, &lock->lk_denied);
7662 status = nfserr_deadlock;
7665 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
7666 status = nfserrno(err);
7671 /* dequeue it if we queued it before */
7672 if (fl_flags & FL_SLEEP) {
7673 spin_lock(&nn->blocked_locks_lock);
7674 if (!list_empty(&nbl->nbl_list) &&
7675 !list_empty(&nbl->nbl_lru)) {
7676 list_del_init(&nbl->nbl_list);
7677 list_del_init(&nbl->nbl_lru);
7678 kref_put(&nbl->nbl_kref, free_nbl);
7680 /* nbl can use one of lists to be linked to reaplist */
7681 spin_unlock(&nn->blocked_locks_lock);
7683 free_blocked_lock(nbl);
7688 /* Bump seqid manually if the 4.0 replay owner is openowner */
7689 if (cstate->replay_owner &&
7690 cstate->replay_owner != &lock_sop->lo_owner &&
7691 seqid_mutating_err(ntohl(status)))
7692 lock_sop->lo_owner.so_seqid++;
7695 * If this is a new, never-before-used stateid, and we are
7696 * returning an error, then just go ahead and release it.
7699 release_lock_stateid(lock_stp);
7701 mutex_unlock(&lock_stp->st_mutex);
7703 nfs4_put_stid(&lock_stp->st_stid);
7706 nfs4_put_stid(&open_stp->st_stid);
7707 nfsd4_bump_seqid(cstate, status);
7709 locks_free_lock(conflock);
7713 void nfsd4_lock_release(union nfsd4_op_u *u)
7715 struct nfsd4_lock *lock = &u->lock;
7716 struct nfsd4_lock_denied *deny = &lock->lk_denied;
7718 kfree(deny->ld_owner.data);
7722 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7723 * so we do a temporary open here just to get an open file to pass to
7726 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
7728 struct nfsd_file *nf;
7729 struct inode *inode;
7732 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7735 inode = fhp->fh_dentry->d_inode;
7736 inode_lock(inode); /* to block new leases till after test_lock: */
7737 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
7740 lock->fl_file = nf->nf_file;
7741 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
7742 lock->fl_file = NULL;
7744 inode_unlock(inode);
7753 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7754 union nfsd4_op_u *u)
7756 struct nfsd4_lockt *lockt = &u->lockt;
7757 struct file_lock *file_lock = NULL;
7758 struct nfs4_lockowner *lo = NULL;
7760 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7762 if (locks_in_grace(SVC_NET(rqstp)))
7763 return nfserr_grace;
7765 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7766 return nfserr_inval;
7768 if (!nfsd4_has_session(cstate)) {
7769 status = set_client(&lockt->lt_clientid, cstate, nn);
7774 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7777 file_lock = locks_alloc_lock();
7779 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7780 status = nfserr_jukebox;
7784 switch (lockt->lt_type) {
7787 file_lock->fl_type = F_RDLCK;
7790 case NFS4_WRITEW_LT:
7791 file_lock->fl_type = F_WRLCK;
7794 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
7795 status = nfserr_inval;
7799 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
7801 file_lock->fl_owner = (fl_owner_t)lo;
7802 file_lock->fl_pid = current->tgid;
7803 file_lock->fl_flags = FL_POSIX;
7805 file_lock->fl_start = lockt->lt_offset;
7806 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
7808 nfs4_transform_lock_offset(file_lock);
7810 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
7814 if (file_lock->fl_type != F_UNLCK) {
7815 status = nfserr_denied;
7816 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
7820 nfs4_put_stateowner(&lo->lo_owner);
7822 locks_free_lock(file_lock);
7826 void nfsd4_lockt_release(union nfsd4_op_u *u)
7828 struct nfsd4_lockt *lockt = &u->lockt;
7829 struct nfsd4_lock_denied *deny = &lockt->lt_denied;
7831 kfree(deny->ld_owner.data);
7835 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7836 union nfsd4_op_u *u)
7838 struct nfsd4_locku *locku = &u->locku;
7839 struct nfs4_ol_stateid *stp;
7840 struct nfsd_file *nf = NULL;
7841 struct file_lock *file_lock = NULL;
7844 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7846 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7847 (long long) locku->lu_offset,
7848 (long long) locku->lu_length);
7850 if (check_lock_length(locku->lu_offset, locku->lu_length))
7851 return nfserr_inval;
7853 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
7854 &locku->lu_stateid, NFS4_LOCK_STID,
7858 nf = find_any_file(stp->st_stid.sc_file);
7860 status = nfserr_lock_range;
7863 file_lock = locks_alloc_lock();
7865 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7866 status = nfserr_jukebox;
7870 file_lock->fl_type = F_UNLCK;
7871 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7872 file_lock->fl_pid = current->tgid;
7873 file_lock->fl_file = nf->nf_file;
7874 file_lock->fl_flags = FL_POSIX;
7875 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7876 file_lock->fl_start = locku->lu_offset;
7878 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7880 nfs4_transform_lock_offset(file_lock);
7882 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7884 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7887 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7891 mutex_unlock(&stp->st_mutex);
7892 nfs4_put_stid(&stp->st_stid);
7894 nfsd4_bump_seqid(cstate, status);
7896 locks_free_lock(file_lock);
7900 status = nfserrno(err);
7906 * true: locks held by lockowner
7907 * false: no locks held by lockowner
7910 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7912 struct file_lock *fl;
7914 struct nfsd_file *nf;
7915 struct inode *inode;
7916 struct file_lock_context *flctx;
7918 spin_lock(&fp->fi_lock);
7919 nf = find_any_file_locked(fp);
7921 /* Any valid lock stateid should have some sort of access */
7926 inode = file_inode(nf->nf_file);
7927 flctx = locks_inode_context(inode);
7929 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7930 spin_lock(&flctx->flc_lock);
7931 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7932 if (fl->fl_owner == (fl_owner_t)lowner) {
7937 spin_unlock(&flctx->flc_lock);
7940 spin_unlock(&fp->fi_lock);
7945 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
7946 * @rqstp: RPC transaction
7947 * @cstate: NFSv4 COMPOUND state
7948 * @u: RELEASE_LOCKOWNER arguments
7950 * Check if theree are any locks still held and if not - free the lockowner
7951 * and any lock state that is owned.
7954 * %nfs_ok: lockowner released or not found
7955 * %nfserr_locks_held: lockowner still in use
7956 * %nfserr_stale_clientid: clientid no longer active
7957 * %nfserr_expired: clientid not recognized
7960 nfsd4_release_lockowner(struct svc_rqst *rqstp,
7961 struct nfsd4_compound_state *cstate,
7962 union nfsd4_op_u *u)
7964 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
7965 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7966 clientid_t *clid = &rlockowner->rl_clientid;
7967 struct nfs4_ol_stateid *stp;
7968 struct nfs4_lockowner *lo;
7969 struct nfs4_client *clp;
7970 LIST_HEAD(reaplist);
7973 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7974 clid->cl_boot, clid->cl_id);
7976 status = set_client(clid, cstate, nn);
7981 spin_lock(&clp->cl_lock);
7982 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
7984 spin_unlock(&clp->cl_lock);
7988 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
7989 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7990 spin_unlock(&clp->cl_lock);
7991 nfs4_put_stateowner(&lo->lo_owner);
7992 return nfserr_locks_held;
7995 unhash_lockowner_locked(lo);
7996 while (!list_empty(&lo->lo_owner.so_stateids)) {
7997 stp = list_first_entry(&lo->lo_owner.so_stateids,
7998 struct nfs4_ol_stateid,
8000 WARN_ON(!unhash_lock_stateid(stp));
8001 put_ol_stateid_locked(stp, &reaplist);
8003 spin_unlock(&clp->cl_lock);
8005 free_ol_stateid_reaplist(&reaplist);
8006 remove_blocked_locks(lo);
8007 nfs4_put_stateowner(&lo->lo_owner);
8011 static inline struct nfs4_client_reclaim *
8014 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
8018 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
8020 struct nfs4_client_reclaim *crp;
8022 crp = nfsd4_find_reclaim_client(name, nn);
8023 return (crp && crp->cr_clp);
8027 * failure => all reset bets are off, nfserr_no_grace...
8029 * The caller is responsible for freeing name.data if NULL is returned (it
8030 * will be freed in nfs4_remove_reclaim_record in the normal case).
8032 struct nfs4_client_reclaim *
8033 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
8034 struct nfsd_net *nn)
8036 unsigned int strhashval;
8037 struct nfs4_client_reclaim *crp;
8039 crp = alloc_reclaim();
8041 strhashval = clientstr_hashval(name);
8042 INIT_LIST_HEAD(&crp->cr_strhash);
8043 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
8044 crp->cr_name.data = name.data;
8045 crp->cr_name.len = name.len;
8046 crp->cr_princhash.data = princhash.data;
8047 crp->cr_princhash.len = princhash.len;
8049 nn->reclaim_str_hashtbl_size++;
8055 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
8057 list_del(&crp->cr_strhash);
8058 kfree(crp->cr_name.data);
8059 kfree(crp->cr_princhash.data);
8061 nn->reclaim_str_hashtbl_size--;
8065 nfs4_release_reclaim(struct nfsd_net *nn)
8067 struct nfs4_client_reclaim *crp = NULL;
8070 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8071 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
8072 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
8073 struct nfs4_client_reclaim, cr_strhash);
8074 nfs4_remove_reclaim_record(crp, nn);
8077 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
8081 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
8082 struct nfs4_client_reclaim *
8083 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
8085 unsigned int strhashval;
8086 struct nfs4_client_reclaim *crp = NULL;
8088 strhashval = clientstr_hashval(name);
8089 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
8090 if (compare_blob(&crp->cr_name, &name) == 0) {
8098 nfs4_check_open_reclaim(struct nfs4_client *clp)
8100 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
8101 return nfserr_no_grace;
8103 if (nfsd4_client_record_check(clp))
8104 return nfserr_reclaim_bad;
8110 * Since the lifetime of a delegation isn't limited to that of an open, a
8111 * client may quite reasonably hang on to a delegation as long as it has
8112 * the inode cached. This becomes an obvious problem the first time a
8113 * client's inode cache approaches the size of the server's total memory.
8115 * For now we avoid this problem by imposing a hard limit on the number
8116 * of delegations, which varies according to the server's memory size.
8119 set_max_delegations(void)
8122 * Allow at most 4 delegations per megabyte of RAM. Quick
8123 * estimates suggest that in the worst case (where every delegation
8124 * is for a different inode), a delegation could take about 1.5K,
8125 * giving a worst case usage of about 6% of memory.
8127 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
8130 static int nfs4_state_create_net(struct net *net)
8132 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8135 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8136 sizeof(struct list_head),
8138 if (!nn->conf_id_hashtbl)
8140 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8141 sizeof(struct list_head),
8143 if (!nn->unconf_id_hashtbl)
8145 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
8146 sizeof(struct list_head),
8148 if (!nn->sessionid_hashtbl)
8151 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8152 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
8153 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
8155 for (i = 0; i < SESSION_HASH_SIZE; i++)
8156 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
8157 nn->conf_name_tree = RB_ROOT;
8158 nn->unconf_name_tree = RB_ROOT;
8159 nn->boot_time = ktime_get_real_seconds();
8160 nn->grace_ended = false;
8161 nn->nfsd4_manager.block_opens = true;
8162 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
8163 INIT_LIST_HEAD(&nn->client_lru);
8164 INIT_LIST_HEAD(&nn->close_lru);
8165 INIT_LIST_HEAD(&nn->del_recall_lru);
8166 spin_lock_init(&nn->client_lock);
8167 spin_lock_init(&nn->s2s_cp_lock);
8168 idr_init(&nn->s2s_cp_stateids);
8170 spin_lock_init(&nn->blocked_locks_lock);
8171 INIT_LIST_HEAD(&nn->blocked_locks_lru);
8173 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
8174 INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker);
8177 nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client");
8178 if (!nn->nfsd_client_shrinker)
8181 nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan;
8182 nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count;
8183 nn->nfsd_client_shrinker->private_data = nn;
8185 shrinker_register(nn->nfsd_client_shrinker);
8191 kfree(nn->sessionid_hashtbl);
8193 kfree(nn->unconf_id_hashtbl);
8195 kfree(nn->conf_id_hashtbl);
8201 nfs4_state_destroy_net(struct net *net)
8204 struct nfs4_client *clp = NULL;
8205 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8207 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8208 while (!list_empty(&nn->conf_id_hashtbl[i])) {
8209 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8210 destroy_client(clp);
8214 WARN_ON(!list_empty(&nn->blocked_locks_lru));
8216 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8217 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
8218 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8219 destroy_client(clp);
8223 kfree(nn->sessionid_hashtbl);
8224 kfree(nn->unconf_id_hashtbl);
8225 kfree(nn->conf_id_hashtbl);
8230 nfs4_state_start_net(struct net *net)
8232 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8235 ret = nfs4_state_create_net(net);
8238 locks_start_grace(net, &nn->nfsd4_manager);
8239 nfsd4_client_tracking_init(net);
8240 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
8242 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
8243 nn->nfsd4_grace, net->ns.inum);
8244 trace_nfsd_grace_start(nn);
8245 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
8249 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
8251 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
8252 nfsd4_end_grace(nn);
8256 /* initialization to perform when the nfsd service is started: */
8259 nfs4_state_start(void)
8263 ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params);
8267 ret = nfsd4_create_callback_queue();
8269 rhltable_destroy(&nfs4_file_rhltable);
8273 set_max_delegations();
8278 nfs4_state_shutdown_net(struct net *net)
8280 struct nfs4_delegation *dp = NULL;
8281 struct list_head *pos, *next, reaplist;
8282 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8284 shrinker_free(nn->nfsd_client_shrinker);
8285 cancel_work(&nn->nfsd_shrinker_work);
8286 cancel_delayed_work_sync(&nn->laundromat_work);
8287 locks_end_grace(&nn->nfsd4_manager);
8289 INIT_LIST_HEAD(&reaplist);
8290 spin_lock(&state_lock);
8291 list_for_each_safe(pos, next, &nn->del_recall_lru) {
8292 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8293 WARN_ON(!unhash_delegation_locked(dp));
8294 list_add(&dp->dl_recall_lru, &reaplist);
8296 spin_unlock(&state_lock);
8297 list_for_each_safe(pos, next, &reaplist) {
8298 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8299 list_del_init(&dp->dl_recall_lru);
8300 destroy_unhashed_deleg(dp);
8303 nfsd4_client_tracking_exit(net);
8304 nfs4_state_destroy_net(net);
8305 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
8306 nfsd4_ssc_shutdown_umount(nn);
8311 nfs4_state_shutdown(void)
8313 nfsd4_destroy_callback_queue();
8314 rhltable_destroy(&nfs4_file_rhltable);
8318 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8320 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
8321 CURRENT_STATEID(stateid))
8322 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8326 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8328 if (cstate->minorversion) {
8329 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
8330 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8335 clear_current_stateid(struct nfsd4_compound_state *cstate)
8337 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8341 * functions to set current state id
8344 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
8345 union nfsd4_op_u *u)
8347 put_stateid(cstate, &u->open_downgrade.od_stateid);
8351 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
8352 union nfsd4_op_u *u)
8354 put_stateid(cstate, &u->open.op_stateid);
8358 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
8359 union nfsd4_op_u *u)
8361 put_stateid(cstate, &u->close.cl_stateid);
8365 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
8366 union nfsd4_op_u *u)
8368 put_stateid(cstate, &u->lock.lk_resp_stateid);
8372 * functions to consume current state id
8376 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
8377 union nfsd4_op_u *u)
8379 get_stateid(cstate, &u->open_downgrade.od_stateid);
8383 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8384 union nfsd4_op_u *u)
8386 get_stateid(cstate, &u->delegreturn.dr_stateid);
8390 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8391 union nfsd4_op_u *u)
8393 get_stateid(cstate, &u->free_stateid.fr_stateid);
8397 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8398 union nfsd4_op_u *u)
8400 get_stateid(cstate, &u->setattr.sa_stateid);
8404 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8405 union nfsd4_op_u *u)
8407 get_stateid(cstate, &u->close.cl_stateid);
8411 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8412 union nfsd4_op_u *u)
8414 get_stateid(cstate, &u->locku.lu_stateid);
8418 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8419 union nfsd4_op_u *u)
8421 get_stateid(cstate, &u->read.rd_stateid);
8425 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8426 union nfsd4_op_u *u)
8428 get_stateid(cstate, &u->write.wr_stateid);
8432 * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict
8433 * @rqstp: RPC transaction context
8434 * @inode: file to be checked for a conflict
8436 * This function is called when there is a conflict between a write
8437 * delegation and a change/size GETATTR from another client. The server
8438 * must either use the CB_GETATTR to get the current values of the
8439 * attributes from the client that holds the delegation or recall the
8440 * delegation before replying to the GETATTR. See RFC 8881 section
8443 * The current implementation does not support CB_GETATTR yet. However
8444 * this can avoid recalling the delegation could be added in follow up
8447 * Returns 0 if there is no conflict; otherwise an nfs_stat
8451 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct inode *inode)
8454 struct file_lock_context *ctx;
8455 struct file_lock *fl;
8456 struct nfs4_delegation *dp;
8458 ctx = locks_inode_context(inode);
8461 spin_lock(&ctx->flc_lock);
8462 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
8463 if (fl->fl_flags == FL_LAYOUT)
8465 if (fl->fl_lmops != &nfsd_lease_mng_ops) {
8467 * non-nfs lease, if it's a lease with F_RDLCK then
8468 * we are done; there isn't any write delegation
8471 if (fl->fl_type == F_RDLCK)
8475 if (fl->fl_type == F_WRLCK) {
8477 if (dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) {
8478 spin_unlock(&ctx->flc_lock);
8482 spin_unlock(&ctx->flc_lock);
8483 nfsd_stats_wdeleg_getattr_inc();
8484 status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
8485 if (status != nfserr_jukebox ||
8486 !nfsd_wait_for_delegreturn(rqstp, inode))
8492 spin_unlock(&ctx->flc_lock);