2 * Copyright (c) 2001 The Regents of the University of Michigan.
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;
1164 dprintk("NFSD alloc_init_deleg\n");
1165 n = atomic_long_inc_return(&num_delegations);
1166 if (n < 0 || n > max_delegations)
1168 if (delegation_blocked(&fp->fi_fhandle))
1170 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
1175 * delegation seqid's are never incremented. The 4.1 special
1176 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1177 * 0 anyway just for consistency and use 1:
1179 dp->dl_stid.sc_stateid.si_generation = 1;
1180 INIT_LIST_HEAD(&dp->dl_perfile);
1181 INIT_LIST_HEAD(&dp->dl_perclnt);
1182 INIT_LIST_HEAD(&dp->dl_recall_lru);
1183 dp->dl_clnt_odstate = odstate;
1184 get_clnt_odstate(odstate);
1185 dp->dl_type = dl_type;
1187 dp->dl_recalled = false;
1188 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1189 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1191 dp->dl_stid.sc_file = fp;
1194 atomic_long_dec(&num_delegations);
1199 nfs4_put_stid(struct nfs4_stid *s)
1201 struct nfs4_file *fp = s->sc_file;
1202 struct nfs4_client *clp = s->sc_client;
1204 might_lock(&clp->cl_lock);
1206 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1207 wake_up_all(&close_wq);
1210 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1211 nfs4_free_cpntf_statelist(clp->net, s);
1212 spin_unlock(&clp->cl_lock);
1219 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1221 stateid_t *src = &stid->sc_stateid;
1223 spin_lock(&stid->sc_lock);
1224 if (unlikely(++src->si_generation == 0))
1225 src->si_generation = 1;
1226 memcpy(dst, src, sizeof(*dst));
1227 spin_unlock(&stid->sc_lock);
1230 static void put_deleg_file(struct nfs4_file *fp)
1232 struct nfsd_file *nf = NULL;
1234 spin_lock(&fp->fi_lock);
1235 if (--fp->fi_delegees == 0)
1236 swap(nf, fp->fi_deleg_file);
1237 spin_unlock(&fp->fi_lock);
1243 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1245 struct nfs4_file *fp = dp->dl_stid.sc_file;
1246 struct nfsd_file *nf = fp->fi_deleg_file;
1248 WARN_ON_ONCE(!fp->fi_delegees);
1250 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1254 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1256 put_clnt_odstate(dp->dl_clnt_odstate);
1257 nfs4_unlock_deleg_lease(dp);
1258 nfs4_put_stid(&dp->dl_stid);
1261 void nfs4_unhash_stid(struct nfs4_stid *s)
1267 * nfs4_delegation_exists - Discover if this delegation already exists
1268 * @clp: a pointer to the nfs4_client we're granting a delegation to
1269 * @fp: a pointer to the nfs4_file we're granting a delegation on
1272 * On success: true iff an existing delegation is found
1276 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1278 struct nfs4_delegation *searchdp = NULL;
1279 struct nfs4_client *searchclp = NULL;
1281 lockdep_assert_held(&state_lock);
1282 lockdep_assert_held(&fp->fi_lock);
1284 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1285 searchclp = searchdp->dl_stid.sc_client;
1286 if (clp == searchclp) {
1294 * hash_delegation_locked - Add a delegation to the appropriate lists
1295 * @dp: a pointer to the nfs4_delegation we are adding.
1296 * @fp: a pointer to the nfs4_file we're granting a delegation on
1299 * On success: NULL if the delegation was successfully hashed.
1301 * On error: -EAGAIN if one was previously granted to this
1302 * nfs4_client for this nfs4_file. Delegation is not hashed.
1307 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1309 struct nfs4_client *clp = dp->dl_stid.sc_client;
1311 lockdep_assert_held(&state_lock);
1312 lockdep_assert_held(&fp->fi_lock);
1314 if (nfs4_delegation_exists(clp, fp))
1316 refcount_inc(&dp->dl_stid.sc_count);
1317 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1318 list_add(&dp->dl_perfile, &fp->fi_delegations);
1319 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1323 static bool delegation_hashed(struct nfs4_delegation *dp)
1325 return !(list_empty(&dp->dl_perfile));
1329 unhash_delegation_locked(struct nfs4_delegation *dp)
1331 struct nfs4_file *fp = dp->dl_stid.sc_file;
1333 lockdep_assert_held(&state_lock);
1335 if (!delegation_hashed(dp))
1338 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1339 /* Ensure that deleg break won't try to requeue it */
1341 spin_lock(&fp->fi_lock);
1342 list_del_init(&dp->dl_perclnt);
1343 list_del_init(&dp->dl_recall_lru);
1344 list_del_init(&dp->dl_perfile);
1345 spin_unlock(&fp->fi_lock);
1349 static void destroy_delegation(struct nfs4_delegation *dp)
1353 spin_lock(&state_lock);
1354 unhashed = unhash_delegation_locked(dp);
1355 spin_unlock(&state_lock);
1357 destroy_unhashed_deleg(dp);
1360 static void revoke_delegation(struct nfs4_delegation *dp)
1362 struct nfs4_client *clp = dp->dl_stid.sc_client;
1364 WARN_ON(!list_empty(&dp->dl_recall_lru));
1366 trace_nfsd_stid_revoke(&dp->dl_stid);
1368 if (clp->cl_minorversion) {
1369 spin_lock(&clp->cl_lock);
1370 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1371 refcount_inc(&dp->dl_stid.sc_count);
1372 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1373 spin_unlock(&clp->cl_lock);
1375 destroy_unhashed_deleg(dp);
1382 static unsigned int clientid_hashval(u32 id)
1384 return id & CLIENT_HASH_MASK;
1387 static unsigned int clientstr_hashval(struct xdr_netobj name)
1389 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1393 * A stateid that had a deny mode associated with it is being released
1394 * or downgraded. Recalculate the deny mode on the file.
1397 recalculate_deny_mode(struct nfs4_file *fp)
1399 struct nfs4_ol_stateid *stp;
1401 spin_lock(&fp->fi_lock);
1402 fp->fi_share_deny = 0;
1403 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1404 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1405 spin_unlock(&fp->fi_lock);
1409 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1412 bool change = false;
1414 for (i = 1; i < 4; i++) {
1415 if ((i & deny) != i) {
1421 /* Recalculate per-file deny mode if there was a change */
1423 recalculate_deny_mode(stp->st_stid.sc_file);
1426 /* release all access and file references for a given stateid */
1428 release_all_access(struct nfs4_ol_stateid *stp)
1431 struct nfs4_file *fp = stp->st_stid.sc_file;
1433 if (fp && stp->st_deny_bmap != 0)
1434 recalculate_deny_mode(fp);
1436 for (i = 1; i < 4; i++) {
1437 if (test_access(i, stp))
1438 nfs4_file_put_access(stp->st_stid.sc_file, i);
1439 clear_access(i, stp);
1443 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1445 kfree(sop->so_owner.data);
1446 sop->so_ops->so_free(sop);
1449 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1451 struct nfs4_client *clp = sop->so_client;
1453 might_lock(&clp->cl_lock);
1455 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1457 sop->so_ops->so_unhash(sop);
1458 spin_unlock(&clp->cl_lock);
1459 nfs4_free_stateowner(sop);
1463 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1465 return list_empty(&stp->st_perfile);
1468 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1470 struct nfs4_file *fp = stp->st_stid.sc_file;
1472 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1474 if (list_empty(&stp->st_perfile))
1477 spin_lock(&fp->fi_lock);
1478 list_del_init(&stp->st_perfile);
1479 spin_unlock(&fp->fi_lock);
1480 list_del(&stp->st_perstateowner);
1484 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1486 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1488 put_clnt_odstate(stp->st_clnt_odstate);
1489 release_all_access(stp);
1490 if (stp->st_stateowner)
1491 nfs4_put_stateowner(stp->st_stateowner);
1492 WARN_ON(!list_empty(&stid->sc_cp_list));
1493 kmem_cache_free(stateid_slab, stid);
1496 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1498 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1499 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1500 struct nfsd_file *nf;
1502 nf = find_any_file(stp->st_stid.sc_file);
1504 get_file(nf->nf_file);
1505 filp_close(nf->nf_file, (fl_owner_t)lo);
1508 nfs4_free_ol_stateid(stid);
1512 * Put the persistent reference to an already unhashed generic stateid, while
1513 * holding the cl_lock. If it's the last reference, then put it onto the
1514 * reaplist for later destruction.
1516 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1517 struct list_head *reaplist)
1519 struct nfs4_stid *s = &stp->st_stid;
1520 struct nfs4_client *clp = s->sc_client;
1522 lockdep_assert_held(&clp->cl_lock);
1524 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1526 if (!refcount_dec_and_test(&s->sc_count)) {
1527 wake_up_all(&close_wq);
1531 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1532 list_add(&stp->st_locks, reaplist);
1535 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1537 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1539 if (!unhash_ol_stateid(stp))
1541 list_del_init(&stp->st_locks);
1542 nfs4_unhash_stid(&stp->st_stid);
1546 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1548 struct nfs4_client *clp = stp->st_stid.sc_client;
1551 spin_lock(&clp->cl_lock);
1552 unhashed = unhash_lock_stateid(stp);
1553 spin_unlock(&clp->cl_lock);
1555 nfs4_put_stid(&stp->st_stid);
1558 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1560 struct nfs4_client *clp = lo->lo_owner.so_client;
1562 lockdep_assert_held(&clp->cl_lock);
1564 list_del_init(&lo->lo_owner.so_strhash);
1568 * Free a list of generic stateids that were collected earlier after being
1572 free_ol_stateid_reaplist(struct list_head *reaplist)
1574 struct nfs4_ol_stateid *stp;
1575 struct nfs4_file *fp;
1579 while (!list_empty(reaplist)) {
1580 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1582 list_del(&stp->st_locks);
1583 fp = stp->st_stid.sc_file;
1584 stp->st_stid.sc_free(&stp->st_stid);
1590 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1591 struct list_head *reaplist)
1593 struct nfs4_ol_stateid *stp;
1595 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1597 while (!list_empty(&open_stp->st_locks)) {
1598 stp = list_entry(open_stp->st_locks.next,
1599 struct nfs4_ol_stateid, st_locks);
1600 WARN_ON(!unhash_lock_stateid(stp));
1601 put_ol_stateid_locked(stp, reaplist);
1605 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1606 struct list_head *reaplist)
1608 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1610 if (!unhash_ol_stateid(stp))
1612 release_open_stateid_locks(stp, reaplist);
1616 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1618 LIST_HEAD(reaplist);
1620 spin_lock(&stp->st_stid.sc_client->cl_lock);
1621 if (unhash_open_stateid(stp, &reaplist))
1622 put_ol_stateid_locked(stp, &reaplist);
1623 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1624 free_ol_stateid_reaplist(&reaplist);
1627 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1629 struct nfs4_client *clp = oo->oo_owner.so_client;
1631 lockdep_assert_held(&clp->cl_lock);
1633 list_del_init(&oo->oo_owner.so_strhash);
1634 list_del_init(&oo->oo_perclient);
1637 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1639 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1641 struct nfs4_ol_stateid *s;
1643 spin_lock(&nn->client_lock);
1644 s = oo->oo_last_closed_stid;
1646 list_del_init(&oo->oo_close_lru);
1647 oo->oo_last_closed_stid = NULL;
1649 spin_unlock(&nn->client_lock);
1651 nfs4_put_stid(&s->st_stid);
1654 static void release_openowner(struct nfs4_openowner *oo)
1656 struct nfs4_ol_stateid *stp;
1657 struct nfs4_client *clp = oo->oo_owner.so_client;
1658 struct list_head reaplist;
1660 INIT_LIST_HEAD(&reaplist);
1662 spin_lock(&clp->cl_lock);
1663 unhash_openowner_locked(oo);
1664 while (!list_empty(&oo->oo_owner.so_stateids)) {
1665 stp = list_first_entry(&oo->oo_owner.so_stateids,
1666 struct nfs4_ol_stateid, st_perstateowner);
1667 if (unhash_open_stateid(stp, &reaplist))
1668 put_ol_stateid_locked(stp, &reaplist);
1670 spin_unlock(&clp->cl_lock);
1671 free_ol_stateid_reaplist(&reaplist);
1672 release_last_closed_stateid(oo);
1673 nfs4_put_stateowner(&oo->oo_owner);
1677 hash_sessionid(struct nfs4_sessionid *sessionid)
1679 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1681 return sid->sequence % SESSION_HASH_SIZE;
1684 #ifdef CONFIG_SUNRPC_DEBUG
1686 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1688 u32 *ptr = (u32 *)(&sessionid->data[0]);
1689 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1693 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1699 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1700 * won't be used for replay.
1702 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1704 struct nfs4_stateowner *so = cstate->replay_owner;
1706 if (nfserr == nfserr_replay_me)
1709 if (!seqid_mutating_err(ntohl(nfserr))) {
1710 nfsd4_cstate_clear_replay(cstate);
1715 if (so->so_is_open_owner)
1716 release_last_closed_stateid(openowner(so));
1722 gen_sessionid(struct nfsd4_session *ses)
1724 struct nfs4_client *clp = ses->se_client;
1725 struct nfsd4_sessionid *sid;
1727 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1728 sid->clientid = clp->cl_clientid;
1729 sid->sequence = current_sessionid++;
1734 * The protocol defines ca_maxresponssize_cached to include the size of
1735 * the rpc header, but all we need to cache is the data starting after
1736 * the end of the initial SEQUENCE operation--the rest we regenerate
1737 * each time. Therefore we can advertise a ca_maxresponssize_cached
1738 * value that is the number of bytes in our cache plus a few additional
1739 * bytes. In order to stay on the safe side, and not promise more than
1740 * we can cache, those additional bytes must be the minimum possible: 24
1741 * bytes of rpc header (xid through accept state, with AUTH_NULL
1742 * verifier), 12 for the compound header (with zero-length tag), and 44
1743 * for the SEQUENCE op response:
1745 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1748 free_session_slots(struct nfsd4_session *ses)
1752 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1753 free_svc_cred(&ses->se_slots[i]->sl_cred);
1754 kfree(ses->se_slots[i]);
1759 * We don't actually need to cache the rpc and session headers, so we
1760 * can allocate a little less for each slot:
1762 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1766 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1769 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1770 return size + sizeof(struct nfsd4_slot);
1774 * XXX: If we run out of reserved DRC memory we could (up to a point)
1775 * re-negotiate active sessions and reduce their slot usage to make
1776 * room for new connections. For now we just fail the create session.
1778 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1780 u32 slotsize = slot_bytes(ca);
1781 u32 num = ca->maxreqs;
1782 unsigned long avail, total_avail;
1783 unsigned int scale_factor;
1785 spin_lock(&nfsd_drc_lock);
1786 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1787 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1789 /* We have handed out more space than we chose in
1790 * set_max_drc() to allow. That isn't really a
1791 * problem as long as that doesn't make us think we
1792 * have lots more due to integer overflow.
1795 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1797 * Never use more than a fraction of the remaining memory,
1798 * unless it's the only way to give this client a slot.
1799 * The chosen fraction is either 1/8 or 1/number of threads,
1800 * whichever is smaller. This ensures there are adequate
1801 * slots to support multiple clients per thread.
1802 * Give the client one slot even if that would require
1803 * over-allocation--it is better than failure.
1805 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1807 avail = clamp_t(unsigned long, avail, slotsize,
1808 total_avail/scale_factor);
1809 num = min_t(int, num, avail / slotsize);
1810 num = max_t(int, num, 1);
1811 nfsd_drc_mem_used += num * slotsize;
1812 spin_unlock(&nfsd_drc_lock);
1817 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1819 int slotsize = slot_bytes(ca);
1821 spin_lock(&nfsd_drc_lock);
1822 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1823 spin_unlock(&nfsd_drc_lock);
1826 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1827 struct nfsd4_channel_attrs *battrs)
1829 int numslots = fattrs->maxreqs;
1830 int slotsize = slot_bytes(fattrs);
1831 struct nfsd4_session *new;
1834 BUILD_BUG_ON(struct_size(new, se_slots, NFSD_MAX_SLOTS_PER_SESSION)
1837 new = kzalloc(struct_size(new, se_slots, numslots), GFP_KERNEL);
1840 /* allocate each struct nfsd4_slot and data cache in one piece */
1841 for (i = 0; i < numslots; i++) {
1842 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1843 if (!new->se_slots[i])
1847 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1848 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1853 kfree(new->se_slots[i]);
1858 static void free_conn(struct nfsd4_conn *c)
1860 svc_xprt_put(c->cn_xprt);
1864 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1866 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1867 struct nfs4_client *clp = c->cn_session->se_client;
1869 trace_nfsd_cb_lost(clp);
1871 spin_lock(&clp->cl_lock);
1872 if (!list_empty(&c->cn_persession)) {
1873 list_del(&c->cn_persession);
1876 nfsd4_probe_callback(clp);
1877 spin_unlock(&clp->cl_lock);
1880 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1882 struct nfsd4_conn *conn;
1884 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1887 svc_xprt_get(rqstp->rq_xprt);
1888 conn->cn_xprt = rqstp->rq_xprt;
1889 conn->cn_flags = flags;
1890 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1894 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1896 conn->cn_session = ses;
1897 list_add(&conn->cn_persession, &ses->se_conns);
1900 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1902 struct nfs4_client *clp = ses->se_client;
1904 spin_lock(&clp->cl_lock);
1905 __nfsd4_hash_conn(conn, ses);
1906 spin_unlock(&clp->cl_lock);
1909 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1911 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1912 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1915 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1919 nfsd4_hash_conn(conn, ses);
1920 ret = nfsd4_register_conn(conn);
1922 /* oops; xprt is already down: */
1923 nfsd4_conn_lost(&conn->cn_xpt_user);
1924 /* We may have gained or lost a callback channel: */
1925 nfsd4_probe_callback_sync(ses->se_client);
1928 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1930 u32 dir = NFS4_CDFC4_FORE;
1932 if (cses->flags & SESSION4_BACK_CHAN)
1933 dir |= NFS4_CDFC4_BACK;
1934 return alloc_conn(rqstp, dir);
1937 /* must be called under client_lock */
1938 static void nfsd4_del_conns(struct nfsd4_session *s)
1940 struct nfs4_client *clp = s->se_client;
1941 struct nfsd4_conn *c;
1943 spin_lock(&clp->cl_lock);
1944 while (!list_empty(&s->se_conns)) {
1945 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1946 list_del_init(&c->cn_persession);
1947 spin_unlock(&clp->cl_lock);
1949 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1952 spin_lock(&clp->cl_lock);
1954 spin_unlock(&clp->cl_lock);
1957 static void __free_session(struct nfsd4_session *ses)
1959 free_session_slots(ses);
1963 static void free_session(struct nfsd4_session *ses)
1965 nfsd4_del_conns(ses);
1966 nfsd4_put_drc_mem(&ses->se_fchannel);
1967 __free_session(ses);
1970 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1973 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1975 new->se_client = clp;
1978 INIT_LIST_HEAD(&new->se_conns);
1980 new->se_cb_seq_nr = 1;
1981 new->se_flags = cses->flags;
1982 new->se_cb_prog = cses->callback_prog;
1983 new->se_cb_sec = cses->cb_sec;
1984 atomic_set(&new->se_ref, 0);
1985 idx = hash_sessionid(&new->se_sessionid);
1986 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1987 spin_lock(&clp->cl_lock);
1988 list_add(&new->se_perclnt, &clp->cl_sessions);
1989 spin_unlock(&clp->cl_lock);
1992 struct sockaddr *sa = svc_addr(rqstp);
1994 * This is a little silly; with sessions there's no real
1995 * use for the callback address. Use the peer address
1996 * as a reasonable default for now, but consider fixing
1997 * the rpc client not to require an address in the
2000 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2001 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2005 /* caller must hold client_lock */
2006 static struct nfsd4_session *
2007 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2009 struct nfsd4_session *elem;
2011 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2013 lockdep_assert_held(&nn->client_lock);
2015 dump_sessionid(__func__, sessionid);
2016 idx = hash_sessionid(sessionid);
2017 /* Search in the appropriate list */
2018 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2019 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2020 NFS4_MAX_SESSIONID_LEN)) {
2025 dprintk("%s: session not found\n", __func__);
2029 static struct nfsd4_session *
2030 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2033 struct nfsd4_session *session;
2034 __be32 status = nfserr_badsession;
2036 session = __find_in_sessionid_hashtbl(sessionid, net);
2039 status = nfsd4_get_session_locked(session);
2047 /* caller must hold client_lock */
2049 unhash_session(struct nfsd4_session *ses)
2051 struct nfs4_client *clp = ses->se_client;
2052 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2054 lockdep_assert_held(&nn->client_lock);
2056 list_del(&ses->se_hash);
2057 spin_lock(&ses->se_client->cl_lock);
2058 list_del(&ses->se_perclnt);
2059 spin_unlock(&ses->se_client->cl_lock);
2062 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2064 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2067 * We're assuming the clid was not given out from a boot
2068 * precisely 2^32 (about 136 years) before this one. That seems
2069 * a safe assumption:
2071 if (clid->cl_boot == (u32)nn->boot_time)
2073 trace_nfsd_clid_stale(clid);
2078 * XXX Should we use a slab cache ?
2079 * This type of memory management is somewhat inefficient, but we use it
2080 * anyway since SETCLIENTID is not a common operation.
2082 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2083 struct nfsd_net *nn)
2085 struct nfs4_client *clp;
2088 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
2089 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2092 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2095 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2096 if (clp->cl_name.data == NULL)
2098 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2099 sizeof(struct list_head),
2101 if (!clp->cl_ownerstr_hashtbl)
2102 goto err_no_hashtbl;
2103 for (i = 0; i < OWNER_HASH_SIZE; i++)
2104 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2105 INIT_LIST_HEAD(&clp->cl_sessions);
2106 idr_init(&clp->cl_stateids);
2107 atomic_set(&clp->cl_rpc_users, 0);
2108 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2109 clp->cl_state = NFSD4_ACTIVE;
2110 atomic_inc(&nn->nfs4_client_count);
2111 atomic_set(&clp->cl_delegs_in_recall, 0);
2112 INIT_LIST_HEAD(&clp->cl_idhash);
2113 INIT_LIST_HEAD(&clp->cl_openowners);
2114 INIT_LIST_HEAD(&clp->cl_delegations);
2115 INIT_LIST_HEAD(&clp->cl_lru);
2116 INIT_LIST_HEAD(&clp->cl_revoked);
2117 #ifdef CONFIG_NFSD_PNFS
2118 INIT_LIST_HEAD(&clp->cl_lo_states);
2120 INIT_LIST_HEAD(&clp->async_copies);
2121 spin_lock_init(&clp->async_lock);
2122 spin_lock_init(&clp->cl_lock);
2123 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2126 kfree(clp->cl_name.data);
2128 kmem_cache_free(client_slab, clp);
2132 static void __free_client(struct kref *k)
2134 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2135 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2137 free_svc_cred(&clp->cl_cred);
2138 kfree(clp->cl_ownerstr_hashtbl);
2139 kfree(clp->cl_name.data);
2140 kfree(clp->cl_nii_domain.data);
2141 kfree(clp->cl_nii_name.data);
2142 idr_destroy(&clp->cl_stateids);
2144 kmem_cache_free(client_slab, clp);
2147 static void drop_client(struct nfs4_client *clp)
2149 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2153 free_client(struct nfs4_client *clp)
2155 while (!list_empty(&clp->cl_sessions)) {
2156 struct nfsd4_session *ses;
2157 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2159 list_del(&ses->se_perclnt);
2160 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2163 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2164 if (clp->cl_nfsd_dentry) {
2165 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2166 clp->cl_nfsd_dentry = NULL;
2167 wake_up_all(&expiry_wq);
2172 /* must be called under the client_lock */
2174 unhash_client_locked(struct nfs4_client *clp)
2176 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2177 struct nfsd4_session *ses;
2179 lockdep_assert_held(&nn->client_lock);
2181 /* Mark the client as expired! */
2183 /* Make it invisible */
2184 if (!list_empty(&clp->cl_idhash)) {
2185 list_del_init(&clp->cl_idhash);
2186 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2187 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2189 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2191 list_del_init(&clp->cl_lru);
2192 spin_lock(&clp->cl_lock);
2193 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2194 list_del_init(&ses->se_hash);
2195 spin_unlock(&clp->cl_lock);
2199 unhash_client(struct nfs4_client *clp)
2201 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2203 spin_lock(&nn->client_lock);
2204 unhash_client_locked(clp);
2205 spin_unlock(&nn->client_lock);
2208 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2210 if (atomic_read(&clp->cl_rpc_users))
2211 return nfserr_jukebox;
2212 unhash_client_locked(clp);
2217 __destroy_client(struct nfs4_client *clp)
2219 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2221 struct nfs4_openowner *oo;
2222 struct nfs4_delegation *dp;
2223 struct list_head reaplist;
2225 INIT_LIST_HEAD(&reaplist);
2226 spin_lock(&state_lock);
2227 while (!list_empty(&clp->cl_delegations)) {
2228 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2229 WARN_ON(!unhash_delegation_locked(dp));
2230 list_add(&dp->dl_recall_lru, &reaplist);
2232 spin_unlock(&state_lock);
2233 while (!list_empty(&reaplist)) {
2234 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2235 list_del_init(&dp->dl_recall_lru);
2236 destroy_unhashed_deleg(dp);
2238 while (!list_empty(&clp->cl_revoked)) {
2239 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2240 list_del_init(&dp->dl_recall_lru);
2241 nfs4_put_stid(&dp->dl_stid);
2243 while (!list_empty(&clp->cl_openowners)) {
2244 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2245 nfs4_get_stateowner(&oo->oo_owner);
2246 release_openowner(oo);
2248 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2249 struct nfs4_stateowner *so, *tmp;
2251 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2253 /* Should be no openowners at this point */
2254 WARN_ON_ONCE(so->so_is_open_owner);
2255 remove_blocked_locks(lockowner(so));
2258 nfsd4_return_all_client_layouts(clp);
2259 nfsd4_shutdown_copy(clp);
2260 nfsd4_shutdown_callback(clp);
2261 if (clp->cl_cb_conn.cb_xprt)
2262 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2263 atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2264 nfsd4_dec_courtesy_client_count(nn, clp);
2266 wake_up_all(&expiry_wq);
2270 destroy_client(struct nfs4_client *clp)
2273 __destroy_client(clp);
2276 static void inc_reclaim_complete(struct nfs4_client *clp)
2278 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2280 if (!nn->track_reclaim_completes)
2282 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2284 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2285 nn->reclaim_str_hashtbl_size) {
2286 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2288 nfsd4_end_grace(nn);
2292 static void expire_client(struct nfs4_client *clp)
2295 nfsd4_client_record_remove(clp);
2296 __destroy_client(clp);
2299 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2301 memcpy(target->cl_verifier.data, source->data,
2302 sizeof(target->cl_verifier.data));
2305 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2307 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2308 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2311 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2313 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2314 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2316 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2317 if ((source->cr_principal && !target->cr_principal) ||
2318 (source->cr_raw_principal && !target->cr_raw_principal) ||
2319 (source->cr_targ_princ && !target->cr_targ_princ))
2322 target->cr_flavor = source->cr_flavor;
2323 target->cr_uid = source->cr_uid;
2324 target->cr_gid = source->cr_gid;
2325 target->cr_group_info = source->cr_group_info;
2326 get_group_info(target->cr_group_info);
2327 target->cr_gss_mech = source->cr_gss_mech;
2328 if (source->cr_gss_mech)
2329 gss_mech_get(source->cr_gss_mech);
2334 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2336 if (o1->len < o2->len)
2338 if (o1->len > o2->len)
2340 return memcmp(o1->data, o2->data, o1->len);
2344 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2346 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2350 same_clid(clientid_t *cl1, clientid_t *cl2)
2352 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2355 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2359 if (g1->ngroups != g2->ngroups)
2361 for (i=0; i<g1->ngroups; i++)
2362 if (!gid_eq(g1->gid[i], g2->gid[i]))
2368 * RFC 3530 language requires clid_inuse be returned when the
2369 * "principal" associated with a requests differs from that previously
2370 * used. We use uid, gid's, and gss principal string as our best
2371 * approximation. We also don't want to allow non-gss use of a client
2372 * established using gss: in theory cr_principal should catch that
2373 * change, but in practice cr_principal can be null even in the gss case
2374 * since gssd doesn't always pass down a principal string.
2376 static bool is_gss_cred(struct svc_cred *cr)
2378 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2379 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2384 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2386 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2387 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2388 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2389 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2391 /* XXX: check that cr_targ_princ fields match ? */
2392 if (cr1->cr_principal == cr2->cr_principal)
2394 if (!cr1->cr_principal || !cr2->cr_principal)
2396 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2399 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2401 struct svc_cred *cr = &rqstp->rq_cred;
2404 if (!cr->cr_gss_mech)
2406 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2407 return service == RPC_GSS_SVC_INTEGRITY ||
2408 service == RPC_GSS_SVC_PRIVACY;
2411 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2413 struct svc_cred *cr = &rqstp->rq_cred;
2415 if (!cl->cl_mach_cred)
2417 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2419 if (!svc_rqst_integrity_protected(rqstp))
2421 if (cl->cl_cred.cr_raw_principal)
2422 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2423 cr->cr_raw_principal);
2424 if (!cr->cr_principal)
2426 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2429 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2434 * This is opaque to client, so no need to byte-swap. Use
2435 * __force to keep sparse happy
2437 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2438 verf[1] = (__force __be32)nn->clverifier_counter++;
2439 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2442 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2444 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2445 clp->cl_clientid.cl_id = nn->clientid_counter++;
2446 gen_confirm(clp, nn);
2449 static struct nfs4_stid *
2450 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2452 struct nfs4_stid *ret;
2454 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2455 if (!ret || !ret->sc_type)
2460 static struct nfs4_stid *
2461 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2463 struct nfs4_stid *s;
2465 spin_lock(&cl->cl_lock);
2466 s = find_stateid_locked(cl, t);
2468 if (typemask & s->sc_type)
2469 refcount_inc(&s->sc_count);
2473 spin_unlock(&cl->cl_lock);
2477 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2479 struct nfsdfs_client *nc;
2480 nc = get_nfsdfs_client(inode);
2483 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2486 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2488 seq_printf(m, "\"");
2489 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2490 seq_printf(m, "\"");
2493 static const char *cb_state2str(int state)
2498 case NFSD4_CB_UNKNOWN:
2502 case NFSD4_CB_FAULT:
2508 static int client_info_show(struct seq_file *m, void *v)
2510 struct inode *inode = file_inode(m->file);
2511 struct nfs4_client *clp;
2514 clp = get_nfsdfs_clp(inode);
2517 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2518 seq_printf(m, "clientid: 0x%llx\n", clid);
2519 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2521 if (clp->cl_state == NFSD4_COURTESY)
2522 seq_puts(m, "status: courtesy\n");
2523 else if (clp->cl_state == NFSD4_EXPIRABLE)
2524 seq_puts(m, "status: expirable\n");
2525 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2526 seq_puts(m, "status: confirmed\n");
2528 seq_puts(m, "status: unconfirmed\n");
2529 seq_printf(m, "seconds from last renew: %lld\n",
2530 ktime_get_boottime_seconds() - clp->cl_time);
2531 seq_printf(m, "name: ");
2532 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2533 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2534 if (clp->cl_nii_domain.data) {
2535 seq_printf(m, "Implementation domain: ");
2536 seq_quote_mem(m, clp->cl_nii_domain.data,
2537 clp->cl_nii_domain.len);
2538 seq_printf(m, "\nImplementation name: ");
2539 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2540 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2541 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2543 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2544 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
2550 DEFINE_SHOW_ATTRIBUTE(client_info);
2552 static void *states_start(struct seq_file *s, loff_t *pos)
2553 __acquires(&clp->cl_lock)
2555 struct nfs4_client *clp = s->private;
2556 unsigned long id = *pos;
2559 spin_lock(&clp->cl_lock);
2560 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2565 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2567 struct nfs4_client *clp = s->private;
2568 unsigned long id = *pos;
2573 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2578 static void states_stop(struct seq_file *s, void *v)
2579 __releases(&clp->cl_lock)
2581 struct nfs4_client *clp = s->private;
2583 spin_unlock(&clp->cl_lock);
2586 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2588 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2591 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2593 struct inode *inode = file_inode(f->nf_file);
2595 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2596 MAJOR(inode->i_sb->s_dev),
2597 MINOR(inode->i_sb->s_dev),
2601 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2603 seq_printf(s, "owner: ");
2604 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2607 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2609 seq_printf(s, "0x%.8x", stid->si_generation);
2610 seq_printf(s, "%12phN", &stid->si_opaque);
2613 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2615 struct nfs4_ol_stateid *ols;
2616 struct nfs4_file *nf;
2617 struct nfsd_file *file;
2618 struct nfs4_stateowner *oo;
2619 unsigned int access, deny;
2621 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2622 return 0; /* XXX: or SEQ_SKIP? */
2623 ols = openlockstateid(st);
2624 oo = ols->st_stateowner;
2627 spin_lock(&nf->fi_lock);
2628 file = find_any_file_locked(nf);
2632 seq_printf(s, "- ");
2633 nfs4_show_stateid(s, &st->sc_stateid);
2634 seq_printf(s, ": { type: open, ");
2636 access = bmap_to_share_mode(ols->st_access_bmap);
2637 deny = bmap_to_share_mode(ols->st_deny_bmap);
2639 seq_printf(s, "access: %s%s, ",
2640 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2641 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2642 seq_printf(s, "deny: %s%s, ",
2643 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2644 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2646 nfs4_show_superblock(s, file);
2647 seq_printf(s, ", ");
2648 nfs4_show_fname(s, file);
2649 seq_printf(s, ", ");
2650 nfs4_show_owner(s, oo);
2651 seq_printf(s, " }\n");
2653 spin_unlock(&nf->fi_lock);
2657 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2659 struct nfs4_ol_stateid *ols;
2660 struct nfs4_file *nf;
2661 struct nfsd_file *file;
2662 struct nfs4_stateowner *oo;
2664 ols = openlockstateid(st);
2665 oo = ols->st_stateowner;
2667 spin_lock(&nf->fi_lock);
2668 file = find_any_file_locked(nf);
2672 seq_printf(s, "- ");
2673 nfs4_show_stateid(s, &st->sc_stateid);
2674 seq_printf(s, ": { type: lock, ");
2677 * Note: a lock stateid isn't really the same thing as a lock,
2678 * it's the locking state held by one owner on a file, and there
2679 * may be multiple (or no) lock ranges associated with it.
2680 * (Same for the matter is true of open stateids.)
2683 nfs4_show_superblock(s, file);
2684 /* XXX: open stateid? */
2685 seq_printf(s, ", ");
2686 nfs4_show_fname(s, file);
2687 seq_printf(s, ", ");
2688 nfs4_show_owner(s, oo);
2689 seq_printf(s, " }\n");
2691 spin_unlock(&nf->fi_lock);
2695 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2697 struct nfs4_delegation *ds;
2698 struct nfs4_file *nf;
2699 struct nfsd_file *file;
2701 ds = delegstateid(st);
2703 spin_lock(&nf->fi_lock);
2704 file = nf->fi_deleg_file;
2708 seq_printf(s, "- ");
2709 nfs4_show_stateid(s, &st->sc_stateid);
2710 seq_printf(s, ": { type: deleg, ");
2712 /* Kinda dead code as long as we only support read delegs: */
2713 seq_printf(s, "access: %s, ",
2714 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2716 /* XXX: lease time, whether it's being recalled. */
2718 nfs4_show_superblock(s, file);
2719 seq_printf(s, ", ");
2720 nfs4_show_fname(s, file);
2721 seq_printf(s, " }\n");
2723 spin_unlock(&nf->fi_lock);
2727 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2729 struct nfs4_layout_stateid *ls;
2730 struct nfsd_file *file;
2732 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2735 seq_printf(s, "- ");
2736 nfs4_show_stateid(s, &st->sc_stateid);
2737 seq_printf(s, ": { type: layout, ");
2739 /* XXX: What else would be useful? */
2741 nfs4_show_superblock(s, file);
2742 seq_printf(s, ", ");
2743 nfs4_show_fname(s, file);
2744 seq_printf(s, " }\n");
2749 static int states_show(struct seq_file *s, void *v)
2751 struct nfs4_stid *st = v;
2753 switch (st->sc_type) {
2754 case NFS4_OPEN_STID:
2755 return nfs4_show_open(s, st);
2756 case NFS4_LOCK_STID:
2757 return nfs4_show_lock(s, st);
2758 case NFS4_DELEG_STID:
2759 return nfs4_show_deleg(s, st);
2760 case NFS4_LAYOUT_STID:
2761 return nfs4_show_layout(s, st);
2763 return 0; /* XXX: or SEQ_SKIP? */
2765 /* XXX: copy stateids? */
2768 static struct seq_operations states_seq_ops = {
2769 .start = states_start,
2770 .next = states_next,
2771 .stop = states_stop,
2775 static int client_states_open(struct inode *inode, struct file *file)
2778 struct nfs4_client *clp;
2781 clp = get_nfsdfs_clp(inode);
2785 ret = seq_open(file, &states_seq_ops);
2788 s = file->private_data;
2793 static int client_opens_release(struct inode *inode, struct file *file)
2795 struct seq_file *m = file->private_data;
2796 struct nfs4_client *clp = m->private;
2798 /* XXX: alternatively, we could get/drop in seq start/stop */
2803 static const struct file_operations client_states_fops = {
2804 .open = client_states_open,
2806 .llseek = seq_lseek,
2807 .release = client_opens_release,
2811 * Normally we refuse to destroy clients that are in use, but here the
2812 * administrator is telling us to just do it. We also want to wait
2813 * so the caller has a guarantee that the client's locks are gone by
2814 * the time the write returns:
2816 static void force_expire_client(struct nfs4_client *clp)
2818 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2819 bool already_expired;
2821 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2823 spin_lock(&nn->client_lock);
2825 spin_unlock(&nn->client_lock);
2827 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2828 spin_lock(&nn->client_lock);
2829 already_expired = list_empty(&clp->cl_lru);
2830 if (!already_expired)
2831 unhash_client_locked(clp);
2832 spin_unlock(&nn->client_lock);
2834 if (!already_expired)
2837 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2840 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2841 size_t size, loff_t *pos)
2844 struct nfs4_client *clp;
2846 data = simple_transaction_get(file, buf, size);
2848 return PTR_ERR(data);
2849 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2851 clp = get_nfsdfs_clp(file_inode(file));
2854 force_expire_client(clp);
2859 static const struct file_operations client_ctl_fops = {
2860 .write = client_ctl_write,
2861 .release = simple_transaction_release,
2864 static const struct tree_descr client_files[] = {
2865 [0] = {"info", &client_info_fops, S_IRUSR},
2866 [1] = {"states", &client_states_fops, S_IRUSR},
2867 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
2872 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb,
2873 struct rpc_task *task)
2875 trace_nfsd_cb_recall_any_done(cb, task);
2876 switch (task->tk_status) {
2877 case -NFS4ERR_DELAY:
2878 rpc_delay(task, 2 * HZ);
2886 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb)
2888 struct nfs4_client *clp = cb->cb_clp;
2889 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2891 spin_lock(&nn->client_lock);
2892 clear_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
2893 put_client_renew_locked(clp);
2894 spin_unlock(&nn->client_lock);
2897 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
2898 .done = nfsd4_cb_recall_any_done,
2899 .release = nfsd4_cb_recall_any_release,
2902 static struct nfs4_client *create_client(struct xdr_netobj name,
2903 struct svc_rqst *rqstp, nfs4_verifier *verf)
2905 struct nfs4_client *clp;
2906 struct sockaddr *sa = svc_addr(rqstp);
2908 struct net *net = SVC_NET(rqstp);
2909 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2910 struct dentry *dentries[ARRAY_SIZE(client_files)];
2912 clp = alloc_client(name, nn);
2916 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2922 kref_init(&clp->cl_nfsdfs.cl_ref);
2923 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2924 clp->cl_time = ktime_get_boottime_seconds();
2925 clear_bit(0, &clp->cl_cb_slot_busy);
2926 copy_verf(clp, verf);
2927 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2928 clp->cl_cb_session = NULL;
2930 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2931 nn, &clp->cl_nfsdfs,
2932 clp->cl_clientid.cl_id - nn->clientid_base,
2933 client_files, dentries);
2934 clp->cl_nfsd_info_dentry = dentries[0];
2935 if (!clp->cl_nfsd_dentry) {
2939 clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL);
2944 clp->cl_ra_time = 0;
2945 nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops,
2946 NFSPROC4_CLNT_CB_RECALL_ANY);
2951 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2953 struct rb_node **new = &(root->rb_node), *parent = NULL;
2954 struct nfs4_client *clp;
2957 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2960 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2961 new = &((*new)->rb_left);
2963 new = &((*new)->rb_right);
2966 rb_link_node(&new_clp->cl_namenode, parent, new);
2967 rb_insert_color(&new_clp->cl_namenode, root);
2970 static struct nfs4_client *
2971 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2974 struct rb_node *node = root->rb_node;
2975 struct nfs4_client *clp;
2978 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2979 cmp = compare_blob(&clp->cl_name, name);
2981 node = node->rb_left;
2983 node = node->rb_right;
2991 add_to_unconfirmed(struct nfs4_client *clp)
2993 unsigned int idhashval;
2994 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2996 lockdep_assert_held(&nn->client_lock);
2998 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2999 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
3000 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3001 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3002 renew_client_locked(clp);
3006 move_to_confirmed(struct nfs4_client *clp)
3008 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3009 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3011 lockdep_assert_held(&nn->client_lock);
3013 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
3014 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
3015 add_clp_to_name_tree(clp, &nn->conf_name_tree);
3016 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3017 trace_nfsd_clid_confirmed(&clp->cl_clientid);
3018 renew_client_locked(clp);
3021 static struct nfs4_client *
3022 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
3024 struct nfs4_client *clp;
3025 unsigned int idhashval = clientid_hashval(clid->cl_id);
3027 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
3028 if (same_clid(&clp->cl_clientid, clid)) {
3029 if ((bool)clp->cl_minorversion != sessions)
3031 renew_client_locked(clp);
3038 static struct nfs4_client *
3039 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3041 struct list_head *tbl = nn->conf_id_hashtbl;
3043 lockdep_assert_held(&nn->client_lock);
3044 return find_client_in_id_table(tbl, clid, sessions);
3047 static struct nfs4_client *
3048 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3050 struct list_head *tbl = nn->unconf_id_hashtbl;
3052 lockdep_assert_held(&nn->client_lock);
3053 return find_client_in_id_table(tbl, clid, sessions);
3056 static bool clp_used_exchangeid(struct nfs4_client *clp)
3058 return clp->cl_exchange_flags != 0;
3061 static struct nfs4_client *
3062 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3064 lockdep_assert_held(&nn->client_lock);
3065 return find_clp_in_name_tree(name, &nn->conf_name_tree);
3068 static struct nfs4_client *
3069 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3071 lockdep_assert_held(&nn->client_lock);
3072 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3076 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3078 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3079 struct sockaddr *sa = svc_addr(rqstp);
3080 u32 scopeid = rpc_get_scope_id(sa);
3081 unsigned short expected_family;
3083 /* Currently, we only support tcp and tcp6 for the callback channel */
3084 if (se->se_callback_netid_len == 3 &&
3085 !memcmp(se->se_callback_netid_val, "tcp", 3))
3086 expected_family = AF_INET;
3087 else if (se->se_callback_netid_len == 4 &&
3088 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3089 expected_family = AF_INET6;
3093 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3094 se->se_callback_addr_len,
3095 (struct sockaddr *)&conn->cb_addr,
3096 sizeof(conn->cb_addr));
3098 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3101 if (conn->cb_addr.ss_family == AF_INET6)
3102 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3104 conn->cb_prog = se->se_callback_prog;
3105 conn->cb_ident = se->se_callback_ident;
3106 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3107 trace_nfsd_cb_args(clp, conn);
3110 conn->cb_addr.ss_family = AF_UNSPEC;
3111 conn->cb_addrlen = 0;
3112 trace_nfsd_cb_nodelegs(clp);
3117 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3120 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3122 struct xdr_buf *buf = resp->xdr->buf;
3123 struct nfsd4_slot *slot = resp->cstate.slot;
3126 dprintk("--> %s slot %p\n", __func__, slot);
3128 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3129 slot->sl_opcnt = resp->opcnt;
3130 slot->sl_status = resp->cstate.status;
3131 free_svc_cred(&slot->sl_cred);
3132 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3134 if (!nfsd4_cache_this(resp)) {
3135 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3138 slot->sl_flags |= NFSD4_SLOT_CACHED;
3140 base = resp->cstate.data_offset;
3141 slot->sl_datalen = buf->len - base;
3142 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3143 WARN(1, "%s: sessions DRC could not cache compound\n",
3149 * Encode the replay sequence operation from the slot values.
3150 * If cachethis is FALSE encode the uncached rep error on the next
3151 * operation which sets resp->p and increments resp->opcnt for
3152 * nfs4svc_encode_compoundres.
3156 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3157 struct nfsd4_compoundres *resp)
3159 struct nfsd4_op *op;
3160 struct nfsd4_slot *slot = resp->cstate.slot;
3162 /* Encode the replayed sequence operation */
3163 op = &args->ops[resp->opcnt - 1];
3164 nfsd4_encode_operation(resp, op);
3166 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3168 if (args->opcnt == 1) {
3170 * The original operation wasn't a solo sequence--we
3171 * always cache those--so this retry must not match the
3174 op->status = nfserr_seq_false_retry;
3176 op = &args->ops[resp->opcnt++];
3177 op->status = nfserr_retry_uncached_rep;
3178 nfsd4_encode_operation(resp, op);
3184 * The sequence operation is not cached because we can use the slot and
3188 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3189 struct nfsd4_sequence *seq)
3191 struct nfsd4_slot *slot = resp->cstate.slot;
3192 struct xdr_stream *xdr = resp->xdr;
3196 dprintk("--> %s slot %p\n", __func__, slot);
3198 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3202 p = xdr_reserve_space(xdr, slot->sl_datalen);
3205 return nfserr_serverfault;
3207 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3208 xdr_commit_encode(xdr);
3210 resp->opcnt = slot->sl_opcnt;
3211 return slot->sl_status;
3215 * Set the exchange_id flags returned by the server.
3218 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3220 #ifdef CONFIG_NFSD_PNFS
3221 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3223 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3226 /* Referrals are supported, Migration is not. */
3227 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3229 /* set the wire flags to return to client. */
3230 clid->flags = new->cl_exchange_flags;
3233 static bool client_has_openowners(struct nfs4_client *clp)
3235 struct nfs4_openowner *oo;
3237 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3238 if (!list_empty(&oo->oo_owner.so_stateids))
3244 static bool client_has_state(struct nfs4_client *clp)
3246 return client_has_openowners(clp)
3247 #ifdef CONFIG_NFSD_PNFS
3248 || !list_empty(&clp->cl_lo_states)
3250 || !list_empty(&clp->cl_delegations)
3251 || !list_empty(&clp->cl_sessions)
3252 || !list_empty(&clp->async_copies);
3255 static __be32 copy_impl_id(struct nfs4_client *clp,
3256 struct nfsd4_exchange_id *exid)
3258 if (!exid->nii_domain.data)
3260 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3261 if (!clp->cl_nii_domain.data)
3262 return nfserr_jukebox;
3263 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3264 if (!clp->cl_nii_name.data)
3265 return nfserr_jukebox;
3266 clp->cl_nii_time = exid->nii_time;
3271 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3272 union nfsd4_op_u *u)
3274 struct nfsd4_exchange_id *exid = &u->exchange_id;
3275 struct nfs4_client *conf, *new;
3276 struct nfs4_client *unconf = NULL;
3278 char addr_str[INET6_ADDRSTRLEN];
3279 nfs4_verifier verf = exid->verifier;
3280 struct sockaddr *sa = svc_addr(rqstp);
3281 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3282 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3284 rpc_ntop(sa, addr_str, sizeof(addr_str));
3285 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3286 "ip_addr=%s flags %x, spa_how %u\n",
3287 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3288 addr_str, exid->flags, exid->spa_how);
3290 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3291 return nfserr_inval;
3293 new = create_client(exid->clname, rqstp, &verf);
3295 return nfserr_jukebox;
3296 status = copy_impl_id(new, exid);
3300 switch (exid->spa_how) {
3302 exid->spo_must_enforce[0] = 0;
3303 exid->spo_must_enforce[1] = (
3304 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3305 1 << (OP_EXCHANGE_ID - 32) |
3306 1 << (OP_CREATE_SESSION - 32) |
3307 1 << (OP_DESTROY_SESSION - 32) |
3308 1 << (OP_DESTROY_CLIENTID - 32));
3310 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3311 1 << (OP_OPEN_DOWNGRADE) |
3313 1 << (OP_DELEGRETURN));
3315 exid->spo_must_allow[1] &= (
3316 1 << (OP_TEST_STATEID - 32) |
3317 1 << (OP_FREE_STATEID - 32));
3318 if (!svc_rqst_integrity_protected(rqstp)) {
3319 status = nfserr_inval;
3323 * Sometimes userspace doesn't give us a principal.
3324 * Which is a bug, really. Anyway, we can't enforce
3325 * MACH_CRED in that case, better to give up now:
3327 if (!new->cl_cred.cr_principal &&
3328 !new->cl_cred.cr_raw_principal) {
3329 status = nfserr_serverfault;
3332 new->cl_mach_cred = true;
3336 default: /* checked by xdr code */
3340 status = nfserr_encr_alg_unsupp;
3344 /* Cases below refer to rfc 5661 section 18.35.4: */
3345 spin_lock(&nn->client_lock);
3346 conf = find_confirmed_client_by_name(&exid->clname, nn);
3348 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3349 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3352 if (!clp_used_exchangeid(conf)) { /* buggy client */
3353 status = nfserr_inval;
3356 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3357 status = nfserr_wrong_cred;
3360 if (!creds_match) { /* case 9 */
3361 status = nfserr_perm;
3364 if (!verfs_match) { /* case 8 */
3365 status = nfserr_not_same;
3369 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3370 trace_nfsd_clid_confirmed_r(conf);
3373 if (!creds_match) { /* case 3 */
3374 if (client_has_state(conf)) {
3375 status = nfserr_clid_inuse;
3376 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3381 if (verfs_match) { /* case 2 */
3382 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3383 trace_nfsd_clid_confirmed_r(conf);
3386 /* case 5, client reboot */
3387 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3392 if (update) { /* case 7 */
3393 status = nfserr_noent;
3397 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3398 if (unconf) /* case 4, possible retry or client restart */
3399 unhash_client_locked(unconf);
3401 /* case 1, new owner ID */
3402 trace_nfsd_clid_fresh(new);
3406 status = mark_client_expired_locked(conf);
3409 trace_nfsd_clid_replaced(&conf->cl_clientid);
3411 new->cl_minorversion = cstate->minorversion;
3412 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3413 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3415 add_to_unconfirmed(new);
3418 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3419 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3421 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3422 nfsd4_set_ex_flags(conf, exid);
3424 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3425 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3429 spin_unlock(&nn->client_lock);
3434 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3435 expire_client(unconf);
3441 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3443 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3446 /* The slot is in use, and no response has been sent. */
3448 if (seqid == slot_seqid)
3449 return nfserr_jukebox;
3451 return nfserr_seq_misordered;
3453 /* Note unsigned 32-bit arithmetic handles wraparound: */
3454 if (likely(seqid == slot_seqid + 1))
3456 if (seqid == slot_seqid)
3457 return nfserr_replay_cache;
3458 return nfserr_seq_misordered;
3462 * Cache the create session result into the create session single DRC
3463 * slot cache by saving the xdr structure. sl_seqid has been set.
3464 * Do this for solo or embedded create session operations.
3467 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3468 struct nfsd4_clid_slot *slot, __be32 nfserr)
3470 slot->sl_status = nfserr;
3471 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3475 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3476 struct nfsd4_clid_slot *slot)
3478 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3479 return slot->sl_status;
3482 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3483 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3484 1 + /* MIN tag is length with zero, only length */ \
3485 3 + /* version, opcount, opcode */ \
3486 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3487 /* seqid, slotID, slotID, cache */ \
3488 4 ) * sizeof(__be32))
3490 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3491 2 + /* verifier: AUTH_NULL, length 0 */\
3493 1 + /* MIN tag is length with zero, only length */ \
3494 3 + /* opcount, opcode, opstatus*/ \
3495 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3496 /* seqid, slotID, slotID, slotID, status */ \
3497 5 ) * sizeof(__be32))
3499 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3501 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3503 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3504 return nfserr_toosmall;
3505 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3506 return nfserr_toosmall;
3507 ca->headerpadsz = 0;
3508 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3509 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3510 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3511 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3512 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3513 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3515 * Note decreasing slot size below client's request may make it
3516 * difficult for client to function correctly, whereas
3517 * decreasing the number of slots will (just?) affect
3518 * performance. When short on memory we therefore prefer to
3519 * decrease number of slots instead of their size. Clients that
3520 * request larger slots than they need will get poor results:
3521 * Note that we always allow at least one slot, because our
3522 * accounting is soft and provides no guarantees either way.
3524 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3530 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3531 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3533 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3534 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3536 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3537 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3539 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3540 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3541 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3542 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3545 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3547 ca->headerpadsz = 0;
3549 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3550 return nfserr_toosmall;
3551 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3552 return nfserr_toosmall;
3553 ca->maxresp_cached = 0;
3555 return nfserr_toosmall;
3560 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3562 switch (cbs->flavor) {
3568 * GSS case: the spec doesn't allow us to return this
3569 * error. But it also doesn't allow us not to support
3571 * I'd rather this fail hard than return some error the
3572 * client might think it can already handle:
3574 return nfserr_encr_alg_unsupp;
3579 nfsd4_create_session(struct svc_rqst *rqstp,
3580 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3582 struct nfsd4_create_session *cr_ses = &u->create_session;
3583 struct sockaddr *sa = svc_addr(rqstp);
3584 struct nfs4_client *conf, *unconf;
3585 struct nfs4_client *old = NULL;
3586 struct nfsd4_session *new;
3587 struct nfsd4_conn *conn;
3588 struct nfsd4_clid_slot *cs_slot = NULL;
3590 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3592 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3593 return nfserr_inval;
3594 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3597 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3600 status = check_backchannel_attrs(&cr_ses->back_channel);
3602 goto out_release_drc_mem;
3603 status = nfserr_jukebox;
3604 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3606 goto out_release_drc_mem;
3607 conn = alloc_conn_from_crses(rqstp, cr_ses);
3609 goto out_free_session;
3611 spin_lock(&nn->client_lock);
3612 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3613 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3614 WARN_ON_ONCE(conf && unconf);
3617 status = nfserr_wrong_cred;
3618 if (!nfsd4_mach_creds_match(conf, rqstp))
3620 cs_slot = &conf->cl_cs_slot;
3621 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3623 if (status == nfserr_replay_cache)
3624 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3627 } else if (unconf) {
3628 status = nfserr_clid_inuse;
3629 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3630 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3631 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
3634 status = nfserr_wrong_cred;
3635 if (!nfsd4_mach_creds_match(unconf, rqstp))
3637 cs_slot = &unconf->cl_cs_slot;
3638 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3640 /* an unconfirmed replay returns misordered */
3641 status = nfserr_seq_misordered;
3644 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3646 status = mark_client_expired_locked(old);
3651 trace_nfsd_clid_replaced(&old->cl_clientid);
3653 move_to_confirmed(unconf);
3656 status = nfserr_stale_clientid;
3660 /* Persistent sessions are not supported */
3661 cr_ses->flags &= ~SESSION4_PERSIST;
3662 /* Upshifting from TCP to RDMA is not supported */
3663 cr_ses->flags &= ~SESSION4_RDMA;
3665 init_session(rqstp, new, conf, cr_ses);
3666 nfsd4_get_session_locked(new);
3668 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3669 NFS4_MAX_SESSIONID_LEN);
3670 cs_slot->sl_seqid++;
3671 cr_ses->seqid = cs_slot->sl_seqid;
3673 /* cache solo and embedded create sessions under the client_lock */
3674 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3675 spin_unlock(&nn->client_lock);
3677 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3678 /* init connection and backchannel */
3679 nfsd4_init_conn(rqstp, conn, new);
3680 nfsd4_put_session(new);
3685 spin_unlock(&nn->client_lock);
3690 __free_session(new);
3691 out_release_drc_mem:
3692 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3696 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3699 case NFS4_CDFC4_FORE:
3700 case NFS4_CDFC4_BACK:
3702 case NFS4_CDFC4_FORE_OR_BOTH:
3703 case NFS4_CDFC4_BACK_OR_BOTH:
3704 *dir = NFS4_CDFC4_BOTH;
3707 return nfserr_inval;
3710 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3711 struct nfsd4_compound_state *cstate,
3712 union nfsd4_op_u *u)
3714 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3715 struct nfsd4_session *session = cstate->session;
3716 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3719 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3722 spin_lock(&nn->client_lock);
3723 session->se_cb_prog = bc->bc_cb_program;
3724 session->se_cb_sec = bc->bc_cb_sec;
3725 spin_unlock(&nn->client_lock);
3727 nfsd4_probe_callback(session->se_client);
3732 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3734 struct nfsd4_conn *c;
3736 list_for_each_entry(c, &s->se_conns, cn_persession) {
3737 if (c->cn_xprt == xpt) {
3744 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3745 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3747 struct nfs4_client *clp = session->se_client;
3748 struct svc_xprt *xpt = rqst->rq_xprt;
3749 struct nfsd4_conn *c;
3752 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3753 spin_lock(&clp->cl_lock);
3754 c = __nfsd4_find_conn(xpt, session);
3756 status = nfserr_noent;
3757 else if (req == c->cn_flags)
3759 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3760 c->cn_flags != NFS4_CDFC4_BACK)
3762 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3763 c->cn_flags != NFS4_CDFC4_FORE)
3766 status = nfserr_inval;
3767 spin_unlock(&clp->cl_lock);
3768 if (status == nfs_ok && conn)
3773 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3774 struct nfsd4_compound_state *cstate,
3775 union nfsd4_op_u *u)
3777 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3779 struct nfsd4_conn *conn;
3780 struct nfsd4_session *session;
3781 struct net *net = SVC_NET(rqstp);
3782 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3784 if (!nfsd4_last_compound_op(rqstp))
3785 return nfserr_not_only_op;
3786 spin_lock(&nn->client_lock);
3787 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3788 spin_unlock(&nn->client_lock);
3790 goto out_no_session;
3791 status = nfserr_wrong_cred;
3792 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3794 status = nfsd4_match_existing_connection(rqstp, session,
3796 if (status == nfs_ok) {
3797 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3798 bcts->dir == NFS4_CDFC4_BACK)
3799 conn->cn_flags |= NFS4_CDFC4_BACK;
3800 nfsd4_probe_callback(session->se_client);
3803 if (status == nfserr_inval)
3805 status = nfsd4_map_bcts_dir(&bcts->dir);
3808 conn = alloc_conn(rqstp, bcts->dir);
3809 status = nfserr_jukebox;
3812 nfsd4_init_conn(rqstp, conn, session);
3815 nfsd4_put_session(session);
3820 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3822 if (!cstate->session)
3824 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3828 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3829 union nfsd4_op_u *u)
3831 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3832 struct nfsd4_session *ses;
3834 int ref_held_by_me = 0;
3835 struct net *net = SVC_NET(r);
3836 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3838 status = nfserr_not_only_op;
3839 if (nfsd4_compound_in_session(cstate, sessionid)) {
3840 if (!nfsd4_last_compound_op(r))
3844 dump_sessionid(__func__, sessionid);
3845 spin_lock(&nn->client_lock);
3846 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3848 goto out_client_lock;
3849 status = nfserr_wrong_cred;
3850 if (!nfsd4_mach_creds_match(ses->se_client, r))
3851 goto out_put_session;
3852 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3854 goto out_put_session;
3855 unhash_session(ses);
3856 spin_unlock(&nn->client_lock);
3858 nfsd4_probe_callback_sync(ses->se_client);
3860 spin_lock(&nn->client_lock);
3863 nfsd4_put_session_locked(ses);
3865 spin_unlock(&nn->client_lock);
3870 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3872 struct nfs4_client *clp = ses->se_client;
3873 struct nfsd4_conn *c;
3874 __be32 status = nfs_ok;
3877 spin_lock(&clp->cl_lock);
3878 c = __nfsd4_find_conn(new->cn_xprt, ses);
3881 status = nfserr_conn_not_bound_to_session;
3882 if (clp->cl_mach_cred)
3884 __nfsd4_hash_conn(new, ses);
3885 spin_unlock(&clp->cl_lock);
3886 ret = nfsd4_register_conn(new);
3888 /* oops; xprt is already down: */
3889 nfsd4_conn_lost(&new->cn_xpt_user);
3892 spin_unlock(&clp->cl_lock);
3897 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3899 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3901 return args->opcnt > session->se_fchannel.maxops;
3904 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3905 struct nfsd4_session *session)
3907 struct xdr_buf *xb = &rqstp->rq_arg;
3909 return xb->len > session->se_fchannel.maxreq_sz;
3912 static bool replay_matches_cache(struct svc_rqst *rqstp,
3913 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3915 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3917 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3918 (bool)seq->cachethis)
3921 * If there's an error then the reply can have fewer ops than
3924 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3927 * But if we cached a reply with *more* ops than the call you're
3928 * sending us now, then this new call is clearly not really a
3929 * replay of the old one:
3931 if (slot->sl_opcnt > argp->opcnt)
3933 /* This is the only check explicitly called by spec: */
3934 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3937 * There may be more comparisons we could actually do, but the
3938 * spec doesn't require us to catch every case where the calls
3939 * don't match (that would require caching the call as well as
3940 * the reply), so we don't bother.
3946 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3947 union nfsd4_op_u *u)
3949 struct nfsd4_sequence *seq = &u->sequence;
3950 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3951 struct xdr_stream *xdr = resp->xdr;
3952 struct nfsd4_session *session;
3953 struct nfs4_client *clp;
3954 struct nfsd4_slot *slot;
3955 struct nfsd4_conn *conn;
3958 struct net *net = SVC_NET(rqstp);
3959 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3961 if (resp->opcnt != 1)
3962 return nfserr_sequence_pos;
3965 * Will be either used or freed by nfsd4_sequence_check_conn
3968 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3970 return nfserr_jukebox;
3972 spin_lock(&nn->client_lock);
3973 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3975 goto out_no_session;
3976 clp = session->se_client;
3978 status = nfserr_too_many_ops;
3979 if (nfsd4_session_too_many_ops(rqstp, session))
3980 goto out_put_session;
3982 status = nfserr_req_too_big;
3983 if (nfsd4_request_too_big(rqstp, session))
3984 goto out_put_session;
3986 status = nfserr_badslot;
3987 if (seq->slotid >= session->se_fchannel.maxreqs)
3988 goto out_put_session;
3990 slot = session->se_slots[seq->slotid];
3991 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3993 /* We do not negotiate the number of slots yet, so set the
3994 * maxslots to the session maxreqs which is used to encode
3995 * sr_highest_slotid and the sr_target_slot id to maxslots */
3996 seq->maxslots = session->se_fchannel.maxreqs;
3998 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3999 slot->sl_flags & NFSD4_SLOT_INUSE);
4000 if (status == nfserr_replay_cache) {
4001 status = nfserr_seq_misordered;
4002 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
4003 goto out_put_session;
4004 status = nfserr_seq_false_retry;
4005 if (!replay_matches_cache(rqstp, seq, slot))
4006 goto out_put_session;
4007 cstate->slot = slot;
4008 cstate->session = session;
4010 /* Return the cached reply status and set cstate->status
4011 * for nfsd4_proc_compound processing */
4012 status = nfsd4_replay_cache_entry(resp, seq);
4013 cstate->status = nfserr_replay_cache;
4017 goto out_put_session;
4019 status = nfsd4_sequence_check_conn(conn, session);
4022 goto out_put_session;
4024 buflen = (seq->cachethis) ?
4025 session->se_fchannel.maxresp_cached :
4026 session->se_fchannel.maxresp_sz;
4027 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
4029 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
4030 goto out_put_session;
4031 svc_reserve(rqstp, buflen);
4034 /* Success! bump slot seqid */
4035 slot->sl_seqid = seq->seqid;
4036 slot->sl_flags |= NFSD4_SLOT_INUSE;
4038 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4040 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4042 cstate->slot = slot;
4043 cstate->session = session;
4047 switch (clp->cl_cb_state) {
4049 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
4051 case NFSD4_CB_FAULT:
4052 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4055 seq->status_flags = 0;
4057 if (!list_empty(&clp->cl_revoked))
4058 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4062 spin_unlock(&nn->client_lock);
4065 nfsd4_put_session_locked(session);
4066 goto out_no_session;
4070 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4072 struct nfsd4_compound_state *cs = &resp->cstate;
4074 if (nfsd4_has_session(cs)) {
4075 if (cs->status != nfserr_replay_cache) {
4076 nfsd4_store_cache_entry(resp);
4077 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4079 /* Drop session reference that was taken in nfsd4_sequence() */
4080 nfsd4_put_session(cs->session);
4082 put_client_renew(cs->clp);
4086 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4087 struct nfsd4_compound_state *cstate,
4088 union nfsd4_op_u *u)
4090 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4091 struct nfs4_client *conf, *unconf;
4092 struct nfs4_client *clp = NULL;
4094 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4096 spin_lock(&nn->client_lock);
4097 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4098 conf = find_confirmed_client(&dc->clientid, true, nn);
4099 WARN_ON_ONCE(conf && unconf);
4102 if (client_has_state(conf)) {
4103 status = nfserr_clientid_busy;
4106 status = mark_client_expired_locked(conf);
4113 status = nfserr_stale_clientid;
4116 if (!nfsd4_mach_creds_match(clp, rqstp)) {
4118 status = nfserr_wrong_cred;
4121 trace_nfsd_clid_destroyed(&clp->cl_clientid);
4122 unhash_client_locked(clp);
4124 spin_unlock(&nn->client_lock);
4131 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4132 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4134 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4135 struct nfs4_client *clp = cstate->clp;
4138 if (rc->rca_one_fs) {
4139 if (!cstate->current_fh.fh_dentry)
4140 return nfserr_nofilehandle;
4142 * We don't take advantage of the rca_one_fs case.
4143 * That's OK, it's optional, we can safely ignore it.
4148 status = nfserr_complete_already;
4149 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4152 status = nfserr_stale_clientid;
4153 if (is_client_expired(clp))
4155 * The following error isn't really legal.
4156 * But we only get here if the client just explicitly
4157 * destroyed the client. Surely it no longer cares what
4158 * error it gets back on an operation for the dead
4164 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4165 nfsd4_client_record_create(clp);
4166 inc_reclaim_complete(clp);
4172 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4173 union nfsd4_op_u *u)
4175 struct nfsd4_setclientid *setclid = &u->setclientid;
4176 struct xdr_netobj clname = setclid->se_name;
4177 nfs4_verifier clverifier = setclid->se_verf;
4178 struct nfs4_client *conf, *new;
4179 struct nfs4_client *unconf = NULL;
4181 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4183 new = create_client(clname, rqstp, &clverifier);
4185 return nfserr_jukebox;
4186 spin_lock(&nn->client_lock);
4187 conf = find_confirmed_client_by_name(&clname, nn);
4188 if (conf && client_has_state(conf)) {
4189 status = nfserr_clid_inuse;
4190 if (clp_used_exchangeid(conf))
4192 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4193 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4197 unconf = find_unconfirmed_client_by_name(&clname, nn);
4199 unhash_client_locked(unconf);
4201 if (same_verf(&conf->cl_verifier, &clverifier)) {
4202 copy_clid(new, conf);
4203 gen_confirm(new, nn);
4205 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4208 trace_nfsd_clid_fresh(new);
4209 new->cl_minorversion = 0;
4210 gen_callback(new, setclid, rqstp);
4211 add_to_unconfirmed(new);
4212 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4213 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4214 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4218 spin_unlock(&nn->client_lock);
4222 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4223 expire_client(unconf);
4229 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4230 struct nfsd4_compound_state *cstate,
4231 union nfsd4_op_u *u)
4233 struct nfsd4_setclientid_confirm *setclientid_confirm =
4234 &u->setclientid_confirm;
4235 struct nfs4_client *conf, *unconf;
4236 struct nfs4_client *old = NULL;
4237 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4238 clientid_t * clid = &setclientid_confirm->sc_clientid;
4240 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4242 if (STALE_CLIENTID(clid, nn))
4243 return nfserr_stale_clientid;
4245 spin_lock(&nn->client_lock);
4246 conf = find_confirmed_client(clid, false, nn);
4247 unconf = find_unconfirmed_client(clid, false, nn);
4249 * We try hard to give out unique clientid's, so if we get an
4250 * attempt to confirm the same clientid with a different cred,
4251 * the client may be buggy; this should never happen.
4253 * Nevertheless, RFC 7530 recommends INUSE for this case:
4255 status = nfserr_clid_inuse;
4256 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4257 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4260 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4261 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4264 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4265 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4268 status = nfserr_stale_clientid;
4274 unhash_client_locked(old);
4275 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4277 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4279 status = nfserr_clid_inuse;
4280 if (client_has_state(old)
4281 && !same_creds(&unconf->cl_cred,
4286 status = mark_client_expired_locked(old);
4291 trace_nfsd_clid_replaced(&old->cl_clientid);
4293 move_to_confirmed(unconf);
4296 get_client_locked(conf);
4297 spin_unlock(&nn->client_lock);
4299 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4300 nfsd4_probe_callback(conf);
4301 spin_lock(&nn->client_lock);
4302 put_client_renew_locked(conf);
4304 spin_unlock(&nn->client_lock);
4310 static struct nfs4_file *nfsd4_alloc_file(void)
4312 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4315 /* OPEN Share state helper functions */
4317 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
4319 refcount_set(&fp->fi_ref, 1);
4320 spin_lock_init(&fp->fi_lock);
4321 INIT_LIST_HEAD(&fp->fi_stateids);
4322 INIT_LIST_HEAD(&fp->fi_delegations);
4323 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4324 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4325 fp->fi_deleg_file = NULL;
4326 fp->fi_had_conflict = false;
4327 fp->fi_share_deny = 0;
4328 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4329 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4330 fp->fi_aliased = false;
4331 fp->fi_inode = d_inode(fh->fh_dentry);
4332 #ifdef CONFIG_NFSD_PNFS
4333 INIT_LIST_HEAD(&fp->fi_lo_states);
4334 atomic_set(&fp->fi_lo_recalls, 0);
4339 nfsd4_free_slabs(void)
4341 kmem_cache_destroy(client_slab);
4342 kmem_cache_destroy(openowner_slab);
4343 kmem_cache_destroy(lockowner_slab);
4344 kmem_cache_destroy(file_slab);
4345 kmem_cache_destroy(stateid_slab);
4346 kmem_cache_destroy(deleg_slab);
4347 kmem_cache_destroy(odstate_slab);
4351 nfsd4_init_slabs(void)
4353 client_slab = kmem_cache_create("nfsd4_clients",
4354 sizeof(struct nfs4_client), 0, 0, NULL);
4355 if (client_slab == NULL)
4357 openowner_slab = kmem_cache_create("nfsd4_openowners",
4358 sizeof(struct nfs4_openowner), 0, 0, NULL);
4359 if (openowner_slab == NULL)
4360 goto out_free_client_slab;
4361 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4362 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4363 if (lockowner_slab == NULL)
4364 goto out_free_openowner_slab;
4365 file_slab = kmem_cache_create("nfsd4_files",
4366 sizeof(struct nfs4_file), 0, 0, NULL);
4367 if (file_slab == NULL)
4368 goto out_free_lockowner_slab;
4369 stateid_slab = kmem_cache_create("nfsd4_stateids",
4370 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4371 if (stateid_slab == NULL)
4372 goto out_free_file_slab;
4373 deleg_slab = kmem_cache_create("nfsd4_delegations",
4374 sizeof(struct nfs4_delegation), 0, 0, NULL);
4375 if (deleg_slab == NULL)
4376 goto out_free_stateid_slab;
4377 odstate_slab = kmem_cache_create("nfsd4_odstate",
4378 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4379 if (odstate_slab == NULL)
4380 goto out_free_deleg_slab;
4383 out_free_deleg_slab:
4384 kmem_cache_destroy(deleg_slab);
4385 out_free_stateid_slab:
4386 kmem_cache_destroy(stateid_slab);
4388 kmem_cache_destroy(file_slab);
4389 out_free_lockowner_slab:
4390 kmem_cache_destroy(lockowner_slab);
4391 out_free_openowner_slab:
4392 kmem_cache_destroy(openowner_slab);
4393 out_free_client_slab:
4394 kmem_cache_destroy(client_slab);
4399 static unsigned long
4400 nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
4403 struct nfsd_net *nn = shrink->private_data;
4405 count = atomic_read(&nn->nfsd_courtesy_clients);
4407 count = atomic_long_read(&num_delegations);
4409 queue_work(laundry_wq, &nn->nfsd_shrinker_work);
4410 return (unsigned long)count;
4413 static unsigned long
4414 nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
4420 nfsd4_init_leases_net(struct nfsd_net *nn)
4425 nn->nfsd4_lease = 90; /* default lease time */
4426 nn->nfsd4_grace = 90;
4427 nn->somebody_reclaimed = false;
4428 nn->track_reclaim_completes = false;
4429 nn->clverifier_counter = get_random_u32();
4430 nn->clientid_base = get_random_u32();
4431 nn->clientid_counter = nn->clientid_base + 1;
4432 nn->s2s_cp_cl_id = nn->clientid_counter++;
4434 atomic_set(&nn->nfs4_client_count, 0);
4436 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4437 max_clients *= NFS4_CLIENTS_PER_GB;
4438 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4440 atomic_set(&nn->nfsd_courtesy_clients, 0);
4443 static void init_nfs4_replay(struct nfs4_replay *rp)
4445 rp->rp_status = nfserr_serverfault;
4447 rp->rp_buf = rp->rp_ibuf;
4448 mutex_init(&rp->rp_mutex);
4451 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4452 struct nfs4_stateowner *so)
4454 if (!nfsd4_has_session(cstate)) {
4455 mutex_lock(&so->so_replay.rp_mutex);
4456 cstate->replay_owner = nfs4_get_stateowner(so);
4460 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4462 struct nfs4_stateowner *so = cstate->replay_owner;
4465 cstate->replay_owner = NULL;
4466 mutex_unlock(&so->so_replay.rp_mutex);
4467 nfs4_put_stateowner(so);
4471 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4473 struct nfs4_stateowner *sop;
4475 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4479 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4480 if (!sop->so_owner.data) {
4481 kmem_cache_free(slab, sop);
4485 INIT_LIST_HEAD(&sop->so_stateids);
4486 sop->so_client = clp;
4487 init_nfs4_replay(&sop->so_replay);
4488 atomic_set(&sop->so_count, 1);
4492 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4494 lockdep_assert_held(&clp->cl_lock);
4496 list_add(&oo->oo_owner.so_strhash,
4497 &clp->cl_ownerstr_hashtbl[strhashval]);
4498 list_add(&oo->oo_perclient, &clp->cl_openowners);
4501 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4503 unhash_openowner_locked(openowner(so));
4506 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4508 struct nfs4_openowner *oo = openowner(so);
4510 kmem_cache_free(openowner_slab, oo);
4513 static const struct nfs4_stateowner_operations openowner_ops = {
4514 .so_unhash = nfs4_unhash_openowner,
4515 .so_free = nfs4_free_openowner,
4518 static struct nfs4_ol_stateid *
4519 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4521 struct nfs4_ol_stateid *local, *ret = NULL;
4522 struct nfs4_openowner *oo = open->op_openowner;
4524 lockdep_assert_held(&fp->fi_lock);
4526 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4527 /* ignore lock owners */
4528 if (local->st_stateowner->so_is_open_owner == 0)
4530 if (local->st_stateowner != &oo->oo_owner)
4532 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4534 refcount_inc(&ret->st_stid.sc_count);
4542 nfsd4_verify_open_stid(struct nfs4_stid *s)
4544 __be32 ret = nfs_ok;
4546 switch (s->sc_type) {
4550 case NFS4_CLOSED_STID:
4551 case NFS4_CLOSED_DELEG_STID:
4552 ret = nfserr_bad_stateid;
4554 case NFS4_REVOKED_DELEG_STID:
4555 ret = nfserr_deleg_revoked;
4560 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4562 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4566 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4567 ret = nfsd4_verify_open_stid(&stp->st_stid);
4569 mutex_unlock(&stp->st_mutex);
4573 static struct nfs4_ol_stateid *
4574 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4576 struct nfs4_ol_stateid *stp;
4578 spin_lock(&fp->fi_lock);
4579 stp = nfsd4_find_existing_open(fp, open);
4580 spin_unlock(&fp->fi_lock);
4581 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4583 nfs4_put_stid(&stp->st_stid);
4588 static struct nfs4_openowner *
4589 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4590 struct nfsd4_compound_state *cstate)
4592 struct nfs4_client *clp = cstate->clp;
4593 struct nfs4_openowner *oo, *ret;
4595 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4598 oo->oo_owner.so_ops = &openowner_ops;
4599 oo->oo_owner.so_is_open_owner = 1;
4600 oo->oo_owner.so_seqid = open->op_seqid;
4602 if (nfsd4_has_session(cstate))
4603 oo->oo_flags |= NFS4_OO_CONFIRMED;
4605 oo->oo_last_closed_stid = NULL;
4606 INIT_LIST_HEAD(&oo->oo_close_lru);
4607 spin_lock(&clp->cl_lock);
4608 ret = find_openstateowner_str_locked(strhashval, open, clp);
4610 hash_openowner(oo, clp, strhashval);
4613 nfs4_free_stateowner(&oo->oo_owner);
4615 spin_unlock(&clp->cl_lock);
4619 static struct nfs4_ol_stateid *
4620 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4623 struct nfs4_openowner *oo = open->op_openowner;
4624 struct nfs4_ol_stateid *retstp = NULL;
4625 struct nfs4_ol_stateid *stp;
4628 /* We are moving these outside of the spinlocks to avoid the warnings */
4629 mutex_init(&stp->st_mutex);
4630 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4633 spin_lock(&oo->oo_owner.so_client->cl_lock);
4634 spin_lock(&fp->fi_lock);
4636 retstp = nfsd4_find_existing_open(fp, open);
4640 open->op_stp = NULL;
4641 refcount_inc(&stp->st_stid.sc_count);
4642 stp->st_stid.sc_type = NFS4_OPEN_STID;
4643 INIT_LIST_HEAD(&stp->st_locks);
4644 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4646 stp->st_stid.sc_file = fp;
4647 stp->st_access_bmap = 0;
4648 stp->st_deny_bmap = 0;
4649 stp->st_openstp = NULL;
4650 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4651 list_add(&stp->st_perfile, &fp->fi_stateids);
4654 spin_unlock(&fp->fi_lock);
4655 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4657 /* Handle races with CLOSE */
4658 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4659 nfs4_put_stid(&retstp->st_stid);
4662 /* To keep mutex tracking happy */
4663 mutex_unlock(&stp->st_mutex);
4670 * In the 4.0 case we need to keep the owners around a little while to handle
4671 * CLOSE replay. We still do need to release any file access that is held by
4672 * them before returning however.
4675 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4677 struct nfs4_ol_stateid *last;
4678 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4679 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4682 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4685 * We know that we hold one reference via nfsd4_close, and another
4686 * "persistent" reference for the client. If the refcount is higher
4687 * than 2, then there are still calls in progress that are using this
4688 * stateid. We can't put the sc_file reference until they are finished.
4689 * Wait for the refcount to drop to 2. Since it has been unhashed,
4690 * there should be no danger of the refcount going back up again at
4693 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4695 release_all_access(s);
4696 if (s->st_stid.sc_file) {
4697 put_nfs4_file(s->st_stid.sc_file);
4698 s->st_stid.sc_file = NULL;
4701 spin_lock(&nn->client_lock);
4702 last = oo->oo_last_closed_stid;
4703 oo->oo_last_closed_stid = s;
4704 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4705 oo->oo_time = ktime_get_boottime_seconds();
4706 spin_unlock(&nn->client_lock);
4708 nfs4_put_stid(&last->st_stid);
4711 static noinline_for_stack struct nfs4_file *
4712 nfsd4_file_hash_lookup(const struct svc_fh *fhp)
4714 struct inode *inode = d_inode(fhp->fh_dentry);
4715 struct rhlist_head *tmp, *list;
4716 struct nfs4_file *fi;
4719 list = rhltable_lookup(&nfs4_file_rhltable, &inode,
4720 nfs4_file_rhash_params);
4721 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
4722 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
4723 if (refcount_inc_not_zero(&fi->fi_ref)) {
4734 * On hash insertion, identify entries with the same inode but
4735 * distinct filehandles. They will all be on the list returned
4736 * by rhltable_lookup().
4738 * inode->i_lock prevents racing insertions from adding an entry
4739 * for the same inode/fhp pair twice.
4741 static noinline_for_stack struct nfs4_file *
4742 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
4744 struct inode *inode = d_inode(fhp->fh_dentry);
4745 struct rhlist_head *tmp, *list;
4746 struct nfs4_file *ret = NULL;
4747 bool alias_found = false;
4748 struct nfs4_file *fi;
4752 spin_lock(&inode->i_lock);
4754 list = rhltable_lookup(&nfs4_file_rhltable, &inode,
4755 nfs4_file_rhash_params);
4756 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
4757 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
4758 if (refcount_inc_not_zero(&fi->fi_ref))
4761 fi->fi_aliased = alias_found = true;
4766 nfsd4_file_init(fhp, new);
4767 err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist,
4768 nfs4_file_rhash_params);
4772 new->fi_aliased = alias_found;
4776 spin_unlock(&inode->i_lock);
4781 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
4783 rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist,
4784 nfs4_file_rhash_params);
4788 * Called to check deny when READ with all zero stateid or
4789 * WRITE with all zero or all one stateid
4792 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4794 struct nfs4_file *fp;
4795 __be32 ret = nfs_ok;
4797 fp = nfsd4_file_hash_lookup(current_fh);
4801 /* Check for conflicting share reservations */
4802 spin_lock(&fp->fi_lock);
4803 if (fp->fi_share_deny & deny_type)
4804 ret = nfserr_locked;
4805 spin_unlock(&fp->fi_lock);
4810 static bool nfsd4_deleg_present(const struct inode *inode)
4812 struct file_lock_context *ctx = locks_inode_context(inode);
4814 return ctx && !list_empty_careful(&ctx->flc_lease);
4818 * nfsd_wait_for_delegreturn - wait for delegations to be returned
4819 * @rqstp: the RPC transaction being executed
4820 * @inode: in-core inode of the file being waited for
4822 * The timeout prevents deadlock if all nfsd threads happen to be
4823 * tied up waiting for returning delegations.
4826 * %true: delegation was returned
4827 * %false: timed out waiting for delegreturn
4829 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
4831 long __maybe_unused timeo;
4833 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
4834 NFSD_DELEGRETURN_TIMEOUT);
4835 trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
4839 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4841 struct nfs4_delegation *dp = cb_to_delegation(cb);
4842 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4845 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4848 * We can't do this in nfsd_break_deleg_cb because it is
4849 * already holding inode->i_lock.
4851 * If the dl_time != 0, then we know that it has already been
4852 * queued for a lease break. Don't queue it again.
4854 spin_lock(&state_lock);
4855 if (delegation_hashed(dp) && dp->dl_time == 0) {
4856 dp->dl_time = ktime_get_boottime_seconds();
4857 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4859 spin_unlock(&state_lock);
4862 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4863 struct rpc_task *task)
4865 struct nfs4_delegation *dp = cb_to_delegation(cb);
4867 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
4869 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4870 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4873 switch (task->tk_status) {
4876 case -NFS4ERR_DELAY:
4877 rpc_delay(task, 2 * HZ);
4880 case -NFS4ERR_BAD_STATEID:
4882 * Race: client probably got cb_recall before open reply
4883 * granting delegation.
4885 if (dp->dl_retries--) {
4886 rpc_delay(task, 2 * HZ);
4895 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4897 struct nfs4_delegation *dp = cb_to_delegation(cb);
4899 nfs4_put_stid(&dp->dl_stid);
4902 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4903 .prepare = nfsd4_cb_recall_prepare,
4904 .done = nfsd4_cb_recall_done,
4905 .release = nfsd4_cb_recall_release,
4908 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4911 * We're assuming the state code never drops its reference
4912 * without first removing the lease. Since we're in this lease
4913 * callback (and since the lease code is serialized by the
4914 * flc_lock) we know the server hasn't removed the lease yet, and
4915 * we know it's safe to take a reference.
4917 refcount_inc(&dp->dl_stid.sc_count);
4918 WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall));
4921 /* Called from break_lease() with flc_lock held. */
4923 nfsd_break_deleg_cb(struct file_lock *fl)
4925 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4926 struct nfs4_file *fp = dp->dl_stid.sc_file;
4927 struct nfs4_client *clp = dp->dl_stid.sc_client;
4928 struct nfsd_net *nn;
4930 trace_nfsd_cb_recall(&dp->dl_stid);
4932 dp->dl_recalled = true;
4933 atomic_inc(&clp->cl_delegs_in_recall);
4934 if (try_to_expire_client(clp)) {
4935 nn = net_generic(clp->net, nfsd_net_id);
4936 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
4940 * We don't want the locks code to timeout the lease for us;
4941 * we'll remove it ourself if a delegation isn't returned
4944 fl->fl_break_time = 0;
4946 spin_lock(&fp->fi_lock);
4947 fp->fi_had_conflict = true;
4948 nfsd_break_one_deleg(dp);
4949 spin_unlock(&fp->fi_lock);
4954 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
4955 * @fl: Lock state to check
4958 * %true: Lease conflict was resolved
4959 * %false: Lease conflict was not resolved.
4961 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4963 struct nfs4_delegation *dl = fl->fl_owner;
4964 struct svc_rqst *rqst;
4965 struct nfs4_client *clp;
4969 rqst = kthread_data(current);
4970 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4971 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
4973 clp = *(rqst->rq_lease_breaker);
4974 return dl->dl_stid.sc_client == clp;
4978 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4979 struct list_head *dispose)
4981 struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
4982 struct nfs4_client *clp = dp->dl_stid.sc_client;
4984 if (arg & F_UNLCK) {
4985 if (dp->dl_recalled)
4986 atomic_dec(&clp->cl_delegs_in_recall);
4987 return lease_modify(onlist, arg, dispose);
4992 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4993 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
4994 .lm_break = nfsd_break_deleg_cb,
4995 .lm_change = nfsd_change_deleg_cb,
4998 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
5000 if (nfsd4_has_session(cstate))
5002 if (seqid == so->so_seqid - 1)
5003 return nfserr_replay_me;
5004 if (seqid == so->so_seqid)
5006 return nfserr_bad_seqid;
5009 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
5010 struct nfsd_net *nn)
5012 struct nfs4_client *found;
5014 spin_lock(&nn->client_lock);
5015 found = find_confirmed_client(clid, sessions, nn);
5017 atomic_inc(&found->cl_rpc_users);
5018 spin_unlock(&nn->client_lock);
5022 static __be32 set_client(clientid_t *clid,
5023 struct nfsd4_compound_state *cstate,
5024 struct nfsd_net *nn)
5027 if (!same_clid(&cstate->clp->cl_clientid, clid))
5028 return nfserr_stale_clientid;
5031 if (STALE_CLIENTID(clid, nn))
5032 return nfserr_stale_clientid;
5034 * We're in the 4.0 case (otherwise the SEQUENCE op would have
5035 * set cstate->clp), so session = false:
5037 cstate->clp = lookup_clientid(clid, false, nn);
5039 return nfserr_expired;
5044 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
5045 struct nfsd4_open *open, struct nfsd_net *nn)
5047 clientid_t *clientid = &open->op_clientid;
5048 struct nfs4_client *clp = NULL;
5049 unsigned int strhashval;
5050 struct nfs4_openowner *oo = NULL;
5054 * In case we need it later, after we've already created the
5055 * file and don't want to risk a further failure:
5057 open->op_file = nfsd4_alloc_file();
5058 if (open->op_file == NULL)
5059 return nfserr_jukebox;
5061 status = set_client(clientid, cstate, nn);
5066 strhashval = ownerstr_hashval(&open->op_owner);
5067 oo = find_openstateowner_str(strhashval, open, clp);
5068 open->op_openowner = oo;
5072 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5073 /* Replace unconfirmed owners without checking for replay. */
5074 release_openowner(oo);
5075 open->op_openowner = NULL;
5078 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5083 oo = alloc_init_open_stateowner(strhashval, open, cstate);
5085 return nfserr_jukebox;
5086 open->op_openowner = oo;
5088 open->op_stp = nfs4_alloc_open_stateid(clp);
5090 return nfserr_jukebox;
5092 if (nfsd4_has_session(cstate) &&
5093 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5094 open->op_odstate = alloc_clnt_odstate(clp);
5095 if (!open->op_odstate)
5096 return nfserr_jukebox;
5102 static inline __be32
5103 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5105 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
5106 return nfserr_openmode;
5111 static int share_access_to_flags(u32 share_access)
5113 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5116 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
5118 struct nfs4_stid *ret;
5120 ret = find_stateid_by_type(cl, s,
5121 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
5124 return delegstateid(ret);
5127 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5129 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5130 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5134 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5135 struct nfs4_delegation **dp)
5138 __be32 status = nfserr_bad_stateid;
5139 struct nfs4_delegation *deleg;
5141 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5144 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
5145 nfs4_put_stid(&deleg->dl_stid);
5146 if (cl->cl_minorversion)
5147 status = nfserr_deleg_revoked;
5150 flags = share_access_to_flags(open->op_share_access);
5151 status = nfs4_check_delegmode(deleg, flags);
5153 nfs4_put_stid(&deleg->dl_stid);
5158 if (!nfsd4_is_deleg_cur(open))
5162 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5166 static inline int nfs4_access_to_access(u32 nfs4_access)
5170 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5171 flags |= NFSD_MAY_READ;
5172 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5173 flags |= NFSD_MAY_WRITE;
5177 static inline __be32
5178 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5179 struct nfsd4_open *open)
5181 struct iattr iattr = {
5182 .ia_valid = ATTR_SIZE,
5185 struct nfsd_attrs attrs = {
5188 if (!open->op_truncate)
5190 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5191 return nfserr_inval;
5192 return nfsd_setattr(rqstp, fh, &attrs, 0, (time64_t)0);
5195 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5196 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5197 struct nfsd4_open *open, bool new_stp)
5199 struct nfsd_file *nf = NULL;
5201 int oflag = nfs4_access_to_omode(open->op_share_access);
5202 int access = nfs4_access_to_access(open->op_share_access);
5203 unsigned char old_access_bmap, old_deny_bmap;
5205 spin_lock(&fp->fi_lock);
5208 * Are we trying to set a deny mode that would conflict with
5211 status = nfs4_file_check_deny(fp, open->op_share_deny);
5212 if (status != nfs_ok) {
5213 if (status != nfserr_share_denied) {
5214 spin_unlock(&fp->fi_lock);
5217 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5218 stp, open->op_share_deny, false))
5219 status = nfserr_jukebox;
5220 spin_unlock(&fp->fi_lock);
5224 /* set access to the file */
5225 status = nfs4_file_get_access(fp, open->op_share_access);
5226 if (status != nfs_ok) {
5227 if (status != nfserr_share_denied) {
5228 spin_unlock(&fp->fi_lock);
5231 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5232 stp, open->op_share_access, true))
5233 status = nfserr_jukebox;
5234 spin_unlock(&fp->fi_lock);
5238 /* Set access bits in stateid */
5239 old_access_bmap = stp->st_access_bmap;
5240 set_access(open->op_share_access, stp);
5242 /* Set new deny mask */
5243 old_deny_bmap = stp->st_deny_bmap;
5244 set_deny(open->op_share_deny, stp);
5245 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5247 if (!fp->fi_fds[oflag]) {
5248 spin_unlock(&fp->fi_lock);
5250 status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
5251 open->op_filp, &nf);
5252 if (status != nfs_ok)
5253 goto out_put_access;
5255 spin_lock(&fp->fi_lock);
5256 if (!fp->fi_fds[oflag]) {
5257 fp->fi_fds[oflag] = nf;
5261 spin_unlock(&fp->fi_lock);
5265 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5268 goto out_put_access;
5270 status = nfsd4_truncate(rqstp, cur_fh, open);
5272 goto out_put_access;
5276 stp->st_access_bmap = old_access_bmap;
5277 nfs4_file_put_access(fp, open->op_share_access);
5278 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5283 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5284 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5285 struct nfsd4_open *open)
5288 unsigned char old_deny_bmap = stp->st_deny_bmap;
5290 if (!test_access(open->op_share_access, stp))
5291 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5293 /* test and set deny mode */
5294 spin_lock(&fp->fi_lock);
5295 status = nfs4_file_check_deny(fp, open->op_share_deny);
5298 set_deny(open->op_share_deny, stp);
5299 fp->fi_share_deny |=
5300 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5302 case nfserr_share_denied:
5303 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5304 stp, open->op_share_deny, false))
5305 status = nfserr_jukebox;
5308 spin_unlock(&fp->fi_lock);
5310 if (status != nfs_ok)
5313 status = nfsd4_truncate(rqstp, cur_fh, open);
5314 if (status != nfs_ok)
5315 reset_union_bmap_deny(old_deny_bmap, stp);
5319 /* Should we give out recallable state?: */
5320 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5322 if (clp->cl_cb_state == NFSD4_CB_UP)
5325 * In the sessions case, since we don't have to establish a
5326 * separate connection for callbacks, we assume it's OK
5327 * until we hear otherwise:
5329 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5332 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5335 struct file_lock *fl;
5337 fl = locks_alloc_lock();
5340 fl->fl_lmops = &nfsd_lease_mng_ops;
5341 fl->fl_flags = FL_DELEG;
5342 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5343 fl->fl_end = OFFSET_MAX;
5344 fl->fl_owner = (fl_owner_t)dp;
5345 fl->fl_pid = current->tgid;
5346 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5350 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5351 struct nfs4_file *fp)
5353 struct nfs4_ol_stateid *st;
5354 struct file *f = fp->fi_deleg_file->nf_file;
5355 struct inode *ino = file_inode(f);
5358 writes = atomic_read(&ino->i_writecount);
5362 * There could be multiple filehandles (hence multiple
5363 * nfs4_files) referencing this file, but that's not too
5364 * common; let's just give up in that case rather than
5365 * trying to go look up all the clients using that other
5366 * nfs4_file as well:
5371 * If there's a close in progress, make sure that we see it
5372 * clear any fi_fds[] entries before we see it decrement
5375 smp_mb__after_atomic();
5377 if (fp->fi_fds[O_WRONLY])
5379 if (fp->fi_fds[O_RDWR])
5382 return -EAGAIN; /* There may be non-NFSv4 writers */
5384 * It's possible there are non-NFSv4 write opens in progress,
5385 * but if they haven't incremented i_writecount yet then they
5386 * also haven't called break lease yet; so, they'll break this
5387 * lease soon enough. So, all that's left to check for is NFSv4
5390 spin_lock(&fp->fi_lock);
5391 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5392 if (st->st_openstp == NULL /* it's an open */ &&
5393 access_permit_write(st) &&
5394 st->st_stid.sc_client != clp) {
5395 spin_unlock(&fp->fi_lock);
5399 spin_unlock(&fp->fi_lock);
5401 * There's a small chance that we could be racing with another
5402 * NFSv4 open. However, any open that hasn't added itself to
5403 * the fi_stateids list also hasn't called break_lease yet; so,
5404 * they'll break this lease soon enough.
5410 * It's possible that between opening the dentry and setting the delegation,
5411 * that it has been renamed or unlinked. Redo the lookup to verify that this
5415 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5416 struct svc_fh *parent)
5418 struct svc_export *exp;
5419 struct dentry *child;
5422 err = nfsd_lookup_dentry(open->op_rqstp, parent,
5423 open->op_fname, open->op_fnamelen,
5431 if (child != file_dentry(fp->fi_deleg_file->nf_file))
5438 * We avoid breaking delegations held by a client due to its own activity, but
5439 * clearing setuid/setgid bits on a write is an implicit activity and the client
5440 * may not notice and continue using the old mode. Avoid giving out a delegation
5441 * on setuid/setgid files when the client is requesting an open for write.
5444 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
5446 struct inode *inode = file_inode(nf->nf_file);
5448 if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
5449 (inode->i_mode & (S_ISUID|S_ISGID)))
5454 static struct nfs4_delegation *
5455 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5456 struct svc_fh *parent)
5459 struct nfs4_client *clp = stp->st_stid.sc_client;
5460 struct nfs4_file *fp = stp->st_stid.sc_file;
5461 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
5462 struct nfs4_delegation *dp;
5463 struct nfsd_file *nf = NULL;
5464 struct file_lock *fl;
5468 * The fi_had_conflict and nfs_get_existing_delegation checks
5469 * here are just optimizations; we'll need to recheck them at
5472 if (fp->fi_had_conflict)
5473 return ERR_PTR(-EAGAIN);
5476 * Try for a write delegation first. RFC8881 section 10.4 says:
5478 * "An OPEN_DELEGATE_WRITE delegation allows the client to handle,
5479 * on its own, all opens."
5481 * Furthermore the client can use a write delegation for most READ
5482 * operations as well, so we require a O_RDWR file here.
5484 * Offer a write delegation in the case of a BOTH open, and ensure
5485 * we get the O_RDWR descriptor.
5487 if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == NFS4_SHARE_ACCESS_BOTH) {
5488 nf = find_rw_file(fp);
5489 dl_type = NFS4_OPEN_DELEGATE_WRITE;
5493 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
5494 * file for some reason, then try for a read delegation instead.
5496 if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
5497 nf = find_readable_file(fp);
5498 dl_type = NFS4_OPEN_DELEGATE_READ;
5502 return ERR_PTR(-EAGAIN);
5504 spin_lock(&state_lock);
5505 spin_lock(&fp->fi_lock);
5506 if (nfs4_delegation_exists(clp, fp))
5508 else if (nfsd4_verify_setuid_write(open, nf))
5510 else if (!fp->fi_deleg_file) {
5511 fp->fi_deleg_file = nf;
5512 /* increment early to prevent fi_deleg_file from being
5514 fp->fi_delegees = 1;
5518 spin_unlock(&fp->fi_lock);
5519 spin_unlock(&state_lock);
5523 return ERR_PTR(status);
5526 dp = alloc_init_deleg(clp, fp, odstate, dl_type);
5530 fl = nfs4_alloc_init_lease(dp, dl_type);
5532 goto out_clnt_odstate;
5534 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5536 locks_free_lock(fl);
5538 goto out_clnt_odstate;
5541 status = nfsd4_verify_deleg_dentry(open, fp, parent);
5546 status = nfsd4_check_conflicting_opens(clp, fp);
5551 * Now that the deleg is set, check again to ensure that nothing
5552 * raced in and changed the mode while we weren't lookng.
5554 status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file);
5558 spin_lock(&state_lock);
5559 spin_lock(&fp->fi_lock);
5560 if (fp->fi_had_conflict)
5563 status = hash_delegation_locked(dp, fp);
5564 spin_unlock(&fp->fi_lock);
5565 spin_unlock(&state_lock);
5572 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5574 put_clnt_odstate(dp->dl_clnt_odstate);
5575 nfs4_put_stid(&dp->dl_stid);
5578 return ERR_PTR(status);
5581 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5583 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5584 if (status == -EAGAIN)
5585 open->op_why_no_deleg = WND4_CONTENTION;
5587 open->op_why_no_deleg = WND4_RESOURCE;
5588 switch (open->op_deleg_want) {
5589 case NFS4_SHARE_WANT_READ_DELEG:
5590 case NFS4_SHARE_WANT_WRITE_DELEG:
5591 case NFS4_SHARE_WANT_ANY_DELEG:
5593 case NFS4_SHARE_WANT_CANCEL:
5594 open->op_why_no_deleg = WND4_CANCELLED;
5596 case NFS4_SHARE_WANT_NO_DELEG:
5603 * The Linux NFS server does not offer write delegations to NFSv4.0
5604 * clients in order to avoid conflicts between write delegations and
5605 * GETATTRs requesting CHANGE or SIZE attributes.
5607 * With NFSv4.1 and later minorversions, the SEQUENCE operation that
5608 * begins each COMPOUND contains a client ID. Delegation recall can
5609 * be avoided when the server recognizes the client sending a
5610 * GETATTR also holds write delegation it conflicts with.
5612 * However, the NFSv4.0 protocol does not enable a server to
5613 * determine that a GETATTR originated from the client holding the
5614 * conflicting delegation versus coming from some other client. Per
5615 * RFC 7530 Section 16.7.5, the server must recall or send a
5616 * CB_GETATTR even when the GETATTR originates from the client that
5617 * holds the conflicting delegation.
5619 * An NFSv4.0 client can trigger a pathological situation if it
5620 * always sends a DELEGRETURN preceded by a conflicting GETATTR in
5621 * the same COMPOUND. COMPOUND execution will always stop at the
5622 * GETATTR and the DELEGRETURN will never get executed. The server
5623 * eventually revokes the delegation, which can result in loss of
5624 * open or lock state.
5627 nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5628 struct svc_fh *currentfh)
5630 struct nfs4_delegation *dp;
5631 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5632 struct nfs4_client *clp = stp->st_stid.sc_client;
5633 struct svc_fh *parent = NULL;
5637 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5638 open->op_recall = 0;
5639 switch (open->op_claim_type) {
5640 case NFS4_OPEN_CLAIM_PREVIOUS:
5642 open->op_recall = 1;
5644 case NFS4_OPEN_CLAIM_NULL:
5647 case NFS4_OPEN_CLAIM_FH:
5649 * Let's not give out any delegations till everyone's
5650 * had the chance to reclaim theirs, *and* until
5651 * NLM locks have all been reclaimed:
5653 if (locks_in_grace(clp->net))
5655 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5657 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
5658 !clp->cl_minorversion)
5664 dp = nfs4_set_delegation(open, stp, parent);
5668 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5670 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
5671 open->op_delegate_type = NFS4_OPEN_DELEGATE_WRITE;
5672 trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
5674 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5675 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5677 nfs4_put_stid(&dp->dl_stid);
5680 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5681 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5682 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5683 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5684 open->op_recall = 1;
5687 /* 4.1 client asking for a delegation? */
5688 if (open->op_deleg_want)
5689 nfsd4_open_deleg_none_ext(open, status);
5693 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5694 struct nfs4_delegation *dp)
5696 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5697 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5698 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5699 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5700 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5701 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5702 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5703 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5705 /* Otherwise the client must be confused wanting a delegation
5706 * it already has, therefore we don't return
5707 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5712 * nfsd4_process_open2 - finish open processing
5713 * @rqstp: the RPC transaction being executed
5714 * @current_fh: NFSv4 COMPOUND's current filehandle
5715 * @open: OPEN arguments
5717 * If successful, (1) truncate the file if open->op_truncate was
5718 * set, (2) set open->op_stateid, (3) set open->op_delegation.
5720 * Returns %nfs_ok on success; otherwise an nfs4stat value in
5721 * network byte order is returned.
5724 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5726 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5727 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5728 struct nfs4_file *fp = NULL;
5729 struct nfs4_ol_stateid *stp = NULL;
5730 struct nfs4_delegation *dp = NULL;
5732 bool new_stp = false;
5735 * Lookup file; if found, lookup stateid and check open request,
5736 * and check for delegations in the process of being recalled.
5737 * If not found, create the nfs4_file struct
5739 fp = nfsd4_file_hash_insert(open->op_file, current_fh);
5741 return nfserr_jukebox;
5742 if (fp != open->op_file) {
5743 status = nfs4_check_deleg(cl, open, &dp);
5746 stp = nfsd4_find_and_lock_existing_open(fp, open);
5748 open->op_file = NULL;
5749 status = nfserr_bad_stateid;
5750 if (nfsd4_is_deleg_cur(open))
5755 stp = init_open_stateid(fp, open);
5761 * OPEN the file, or upgrade an existing OPEN.
5762 * If truncate fails, the OPEN fails.
5764 * stp is already locked.
5767 /* Stateid was found, this is an OPEN upgrade */
5768 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5770 mutex_unlock(&stp->st_mutex);
5774 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
5776 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5777 release_open_stateid(stp);
5778 mutex_unlock(&stp->st_mutex);
5782 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5784 if (stp->st_clnt_odstate == open->op_odstate)
5785 open->op_odstate = NULL;
5788 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5789 mutex_unlock(&stp->st_mutex);
5791 if (nfsd4_has_session(&resp->cstate)) {
5792 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5793 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5794 open->op_why_no_deleg = WND4_NOT_WANTED;
5800 * Attempt to hand out a delegation. No error return, because the
5801 * OPEN succeeds even if we fail.
5803 nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
5806 trace_nfsd_open(&stp->st_stid.sc_stateid);
5808 /* 4.1 client trying to upgrade/downgrade delegation? */
5809 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5810 open->op_deleg_want)
5811 nfsd4_deleg_xgrade_none_ext(open, dp);
5815 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5816 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5818 * To finish the open response, we just need to set the rflags.
5820 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5821 if (nfsd4_has_session(&resp->cstate))
5822 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5823 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5824 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5827 nfs4_put_stid(&dp->dl_stid);
5829 nfs4_put_stid(&stp->st_stid);
5834 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5835 struct nfsd4_open *open)
5837 if (open->op_openowner) {
5838 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5840 nfsd4_cstate_assign_replay(cstate, so);
5841 nfs4_put_stateowner(so);
5844 kmem_cache_free(file_slab, open->op_file);
5846 nfs4_put_stid(&open->op_stp->st_stid);
5847 if (open->op_odstate)
5848 kmem_cache_free(odstate_slab, open->op_odstate);
5852 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5853 union nfsd4_op_u *u)
5855 clientid_t *clid = &u->renew;
5856 struct nfs4_client *clp;
5858 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5860 trace_nfsd_clid_renew(clid);
5861 status = set_client(clid, cstate, nn);
5865 if (!list_empty(&clp->cl_delegations)
5866 && clp->cl_cb_state != NFSD4_CB_UP)
5867 return nfserr_cb_path_down;
5872 nfsd4_end_grace(struct nfsd_net *nn)
5874 /* do nothing if grace period already ended */
5875 if (nn->grace_ended)
5878 trace_nfsd_grace_complete(nn);
5879 nn->grace_ended = true;
5881 * If the server goes down again right now, an NFSv4
5882 * client will still be allowed to reclaim after it comes back up,
5883 * even if it hasn't yet had a chance to reclaim state this time.
5886 nfsd4_record_grace_done(nn);
5888 * At this point, NFSv4 clients can still reclaim. But if the
5889 * server crashes, any that have not yet reclaimed will be out
5890 * of luck on the next boot.
5892 * (NFSv4.1+ clients are considered to have reclaimed once they
5893 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5894 * have reclaimed after their first OPEN.)
5896 locks_end_grace(&nn->nfsd4_manager);
5898 * At this point, and once lockd and/or any other containers
5899 * exit their grace period, further reclaims will fail and
5900 * regular locking can resume.
5905 * If we've waited a lease period but there are still clients trying to
5906 * reclaim, wait a little longer to give them a chance to finish.
5908 static bool clients_still_reclaiming(struct nfsd_net *nn)
5910 time64_t double_grace_period_end = nn->boot_time +
5911 2 * nn->nfsd4_lease;
5913 if (nn->track_reclaim_completes &&
5914 atomic_read(&nn->nr_reclaim_complete) ==
5915 nn->reclaim_str_hashtbl_size)
5917 if (!nn->somebody_reclaimed)
5919 nn->somebody_reclaimed = false;
5921 * If we've given them *two* lease times to reclaim, and they're
5922 * still not done, give up:
5924 if (ktime_get_boottime_seconds() > double_grace_period_end)
5929 struct laundry_time {
5934 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5936 time64_t time_remaining;
5938 if (last_refresh < lt->cutoff)
5940 time_remaining = last_refresh - lt->cutoff;
5941 lt->new_timeo = min(lt->new_timeo, time_remaining);
5945 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
5946 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5948 spin_lock_init(&nn->nfsd_ssc_lock);
5949 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5950 init_waitqueue_head(&nn->nfsd_ssc_waitq);
5952 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5955 * This is called when nfsd is being shutdown, after all inter_ssc
5956 * cleanup were done, to destroy the ssc delayed unmount list.
5958 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5960 struct nfsd4_ssc_umount_item *ni = NULL;
5961 struct nfsd4_ssc_umount_item *tmp;
5963 spin_lock(&nn->nfsd_ssc_lock);
5964 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5965 list_del(&ni->nsui_list);
5966 spin_unlock(&nn->nfsd_ssc_lock);
5967 mntput(ni->nsui_vfsmount);
5969 spin_lock(&nn->nfsd_ssc_lock);
5971 spin_unlock(&nn->nfsd_ssc_lock);
5974 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5976 bool do_wakeup = false;
5977 struct nfsd4_ssc_umount_item *ni = NULL;
5978 struct nfsd4_ssc_umount_item *tmp;
5980 spin_lock(&nn->nfsd_ssc_lock);
5981 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5982 if (time_after(jiffies, ni->nsui_expire)) {
5983 if (refcount_read(&ni->nsui_refcnt) > 1)
5986 /* mark being unmount */
5987 ni->nsui_busy = true;
5988 spin_unlock(&nn->nfsd_ssc_lock);
5989 mntput(ni->nsui_vfsmount);
5990 spin_lock(&nn->nfsd_ssc_lock);
5992 /* waiters need to start from begin of list */
5993 list_del(&ni->nsui_list);
5996 /* wakeup ssc_connect waiters */
6003 wake_up_all(&nn->nfsd_ssc_waitq);
6004 spin_unlock(&nn->nfsd_ssc_lock);
6008 /* Check if any lock belonging to this lockowner has any blockers */
6010 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
6012 struct file_lock_context *ctx;
6013 struct nfs4_ol_stateid *stp;
6014 struct nfs4_file *nf;
6016 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
6017 nf = stp->st_stid.sc_file;
6018 ctx = locks_inode_context(nf->fi_inode);
6021 if (locks_owner_has_blockers(ctx, lo))
6028 nfs4_anylock_blockers(struct nfs4_client *clp)
6031 struct nfs4_stateowner *so;
6032 struct nfs4_lockowner *lo;
6034 if (atomic_read(&clp->cl_delegs_in_recall))
6036 spin_lock(&clp->cl_lock);
6037 for (i = 0; i < OWNER_HASH_SIZE; i++) {
6038 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
6040 if (so->so_is_open_owner)
6043 if (nfs4_lockowner_has_blockers(lo)) {
6044 spin_unlock(&clp->cl_lock);
6049 spin_unlock(&clp->cl_lock);
6054 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
6055 struct laundry_time *lt)
6057 unsigned int maxreap, reapcnt = 0;
6058 struct list_head *pos, *next;
6059 struct nfs4_client *clp;
6061 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
6062 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
6063 INIT_LIST_HEAD(reaplist);
6064 spin_lock(&nn->client_lock);
6065 list_for_each_safe(pos, next, &nn->client_lru) {
6066 clp = list_entry(pos, struct nfs4_client, cl_lru);
6067 if (clp->cl_state == NFSD4_EXPIRABLE)
6069 if (!state_expired(lt, clp->cl_time))
6071 if (!atomic_read(&clp->cl_rpc_users)) {
6072 if (clp->cl_state == NFSD4_ACTIVE)
6073 atomic_inc(&nn->nfsd_courtesy_clients);
6074 clp->cl_state = NFSD4_COURTESY;
6076 if (!client_has_state(clp))
6078 if (!nfs4_anylock_blockers(clp))
6079 if (reapcnt >= maxreap)
6082 if (!mark_client_expired_locked(clp)) {
6083 list_add(&clp->cl_lru, reaplist);
6087 spin_unlock(&nn->client_lock);
6091 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
6092 struct list_head *reaplist)
6094 unsigned int maxreap = 0, reapcnt = 0;
6095 struct list_head *pos, *next;
6096 struct nfs4_client *clp;
6098 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
6099 INIT_LIST_HEAD(reaplist);
6101 spin_lock(&nn->client_lock);
6102 list_for_each_safe(pos, next, &nn->client_lru) {
6103 clp = list_entry(pos, struct nfs4_client, cl_lru);
6104 if (clp->cl_state == NFSD4_ACTIVE)
6106 if (reapcnt >= maxreap)
6108 if (!mark_client_expired_locked(clp)) {
6109 list_add(&clp->cl_lru, reaplist);
6113 spin_unlock(&nn->client_lock);
6117 nfs4_process_client_reaplist(struct list_head *reaplist)
6119 struct list_head *pos, *next;
6120 struct nfs4_client *clp;
6122 list_for_each_safe(pos, next, reaplist) {
6123 clp = list_entry(pos, struct nfs4_client, cl_lru);
6124 trace_nfsd_clid_purged(&clp->cl_clientid);
6125 list_del_init(&clp->cl_lru);
6131 nfs4_laundromat(struct nfsd_net *nn)
6133 struct nfs4_openowner *oo;
6134 struct nfs4_delegation *dp;
6135 struct nfs4_ol_stateid *stp;
6136 struct nfsd4_blocked_lock *nbl;
6137 struct list_head *pos, *next, reaplist;
6138 struct laundry_time lt = {
6139 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6140 .new_timeo = nn->nfsd4_lease
6142 struct nfs4_cpntf_state *cps;
6143 copy_stateid_t *cps_t;
6146 if (clients_still_reclaiming(nn)) {
6150 nfsd4_end_grace(nn);
6152 spin_lock(&nn->s2s_cp_lock);
6153 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6154 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6155 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
6156 state_expired(<, cps->cpntf_time))
6157 _free_cpntf_state_locked(nn, cps);
6159 spin_unlock(&nn->s2s_cp_lock);
6160 nfs4_get_client_reaplist(nn, &reaplist, <);
6161 nfs4_process_client_reaplist(&reaplist);
6163 spin_lock(&state_lock);
6164 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6165 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6166 if (!state_expired(<, dp->dl_time))
6168 WARN_ON(!unhash_delegation_locked(dp));
6169 list_add(&dp->dl_recall_lru, &reaplist);
6171 spin_unlock(&state_lock);
6172 while (!list_empty(&reaplist)) {
6173 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6175 list_del_init(&dp->dl_recall_lru);
6176 revoke_delegation(dp);
6179 spin_lock(&nn->client_lock);
6180 while (!list_empty(&nn->close_lru)) {
6181 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6183 if (!state_expired(<, oo->oo_time))
6185 list_del_init(&oo->oo_close_lru);
6186 stp = oo->oo_last_closed_stid;
6187 oo->oo_last_closed_stid = NULL;
6188 spin_unlock(&nn->client_lock);
6189 nfs4_put_stid(&stp->st_stid);
6190 spin_lock(&nn->client_lock);
6192 spin_unlock(&nn->client_lock);
6195 * It's possible for a client to try and acquire an already held lock
6196 * that is being held for a long time, and then lose interest in it.
6197 * So, we clean out any un-revisited request after a lease period
6198 * under the assumption that the client is no longer interested.
6200 * RFC5661, sec. 9.6 states that the client must not rely on getting
6201 * notifications and must continue to poll for locks, even when the
6202 * server supports them. Thus this shouldn't lead to clients blocking
6203 * indefinitely once the lock does become free.
6205 BUG_ON(!list_empty(&reaplist));
6206 spin_lock(&nn->blocked_locks_lock);
6207 while (!list_empty(&nn->blocked_locks_lru)) {
6208 nbl = list_first_entry(&nn->blocked_locks_lru,
6209 struct nfsd4_blocked_lock, nbl_lru);
6210 if (!state_expired(<, nbl->nbl_time))
6212 list_move(&nbl->nbl_lru, &reaplist);
6213 list_del_init(&nbl->nbl_list);
6215 spin_unlock(&nn->blocked_locks_lock);
6217 while (!list_empty(&reaplist)) {
6218 nbl = list_first_entry(&reaplist,
6219 struct nfsd4_blocked_lock, nbl_lru);
6220 list_del_init(&nbl->nbl_lru);
6221 free_blocked_lock(nbl);
6223 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6224 /* service the server-to-server copy delayed unmount list */
6225 nfsd4_ssc_expire_umount(nn);
6228 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
6231 static void laundromat_main(struct work_struct *);
6234 laundromat_main(struct work_struct *laundry)
6237 struct delayed_work *dwork = to_delayed_work(laundry);
6238 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6241 t = nfs4_laundromat(nn);
6242 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
6246 courtesy_client_reaper(struct nfsd_net *nn)
6248 struct list_head reaplist;
6250 nfs4_get_courtesy_client_reaplist(nn, &reaplist);
6251 nfs4_process_client_reaplist(&reaplist);
6255 deleg_reaper(struct nfsd_net *nn)
6257 struct list_head *pos, *next;
6258 struct nfs4_client *clp;
6259 struct list_head cblist;
6261 INIT_LIST_HEAD(&cblist);
6262 spin_lock(&nn->client_lock);
6263 list_for_each_safe(pos, next, &nn->client_lru) {
6264 clp = list_entry(pos, struct nfs4_client, cl_lru);
6265 if (clp->cl_state != NFSD4_ACTIVE ||
6266 list_empty(&clp->cl_delegations) ||
6267 atomic_read(&clp->cl_delegs_in_recall) ||
6268 test_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags) ||
6269 (ktime_get_boottime_seconds() -
6270 clp->cl_ra_time < 5)) {
6273 list_add(&clp->cl_ra_cblist, &cblist);
6275 /* release in nfsd4_cb_recall_any_release */
6276 atomic_inc(&clp->cl_rpc_users);
6277 set_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
6278 clp->cl_ra_time = ktime_get_boottime_seconds();
6280 spin_unlock(&nn->client_lock);
6282 while (!list_empty(&cblist)) {
6283 clp = list_first_entry(&cblist, struct nfs4_client,
6285 list_del_init(&clp->cl_ra_cblist);
6286 clp->cl_ra->ra_keep = 0;
6287 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG);
6288 trace_nfsd_cb_recall_any(clp->cl_ra);
6289 nfsd4_run_cb(&clp->cl_ra->ra_cb);
6294 nfsd4_state_shrinker_worker(struct work_struct *work)
6296 struct nfsd_net *nn = container_of(work, struct nfsd_net,
6297 nfsd_shrinker_work);
6299 courtesy_client_reaper(nn);
6303 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
6305 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
6306 return nfserr_bad_stateid;
6311 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
6313 __be32 status = nfserr_openmode;
6315 /* For lock stateid's, we test the parent open, not the lock: */
6316 if (stp->st_openstp)
6317 stp = stp->st_openstp;
6318 if ((flags & WR_STATE) && !access_permit_write(stp))
6320 if ((flags & RD_STATE) && !access_permit_read(stp))
6327 static inline __be32
6328 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
6330 if (ONE_STATEID(stateid) && (flags & RD_STATE))
6332 else if (opens_in_grace(net)) {
6333 /* Answer in remaining cases depends on existence of
6334 * conflicting state; so we must wait out the grace period. */
6335 return nfserr_grace;
6336 } else if (flags & WR_STATE)
6337 return nfs4_share_conflict(current_fh,
6338 NFS4_SHARE_DENY_WRITE);
6339 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
6340 return nfs4_share_conflict(current_fh,
6341 NFS4_SHARE_DENY_READ);
6344 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
6347 * When sessions are used the stateid generation number is ignored
6350 if (has_session && in->si_generation == 0)
6353 if (in->si_generation == ref->si_generation)
6356 /* If the client sends us a stateid from the future, it's buggy: */
6357 if (nfsd4_stateid_generation_after(in, ref))
6358 return nfserr_bad_stateid;
6360 * However, we could see a stateid from the past, even from a
6361 * non-buggy client. For example, if the client sends a lock
6362 * while some IO is outstanding, the lock may bump si_generation
6363 * while the IO is still in flight. The client could avoid that
6364 * situation by waiting for responses on all the IO requests,
6365 * but better performance may result in retrying IO that
6366 * receives an old_stateid error if requests are rarely
6367 * reordered in flight:
6369 return nfserr_old_stateid;
6372 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
6376 spin_lock(&s->sc_lock);
6377 ret = nfsd4_verify_open_stid(s);
6379 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
6380 spin_unlock(&s->sc_lock);
6384 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6386 if (ols->st_stateowner->so_is_open_owner &&
6387 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6388 return nfserr_bad_stateid;
6392 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
6394 struct nfs4_stid *s;
6395 __be32 status = nfserr_bad_stateid;
6397 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6398 CLOSE_STATEID(stateid))
6400 spin_lock(&cl->cl_lock);
6401 s = find_stateid_locked(cl, stateid);
6404 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
6407 switch (s->sc_type) {
6408 case NFS4_DELEG_STID:
6411 case NFS4_REVOKED_DELEG_STID:
6412 status = nfserr_deleg_revoked;
6414 case NFS4_OPEN_STID:
6415 case NFS4_LOCK_STID:
6416 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
6419 printk("unknown stateid type %x\n", s->sc_type);
6421 case NFS4_CLOSED_STID:
6422 case NFS4_CLOSED_DELEG_STID:
6423 status = nfserr_bad_stateid;
6426 spin_unlock(&cl->cl_lock);
6431 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6432 stateid_t *stateid, unsigned char typemask,
6433 struct nfs4_stid **s, struct nfsd_net *nn)
6436 struct nfs4_stid *stid;
6437 bool return_revoked = false;
6440 * only return revoked delegations if explicitly asked.
6441 * otherwise we report revoked or bad_stateid status.
6443 if (typemask & NFS4_REVOKED_DELEG_STID)
6444 return_revoked = true;
6445 else if (typemask & NFS4_DELEG_STID)
6446 typemask |= NFS4_REVOKED_DELEG_STID;
6448 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6449 CLOSE_STATEID(stateid))
6450 return nfserr_bad_stateid;
6451 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
6452 if (status == nfserr_stale_clientid) {
6453 if (cstate->session)
6454 return nfserr_bad_stateid;
6455 return nfserr_stale_stateid;
6459 stid = find_stateid_by_type(cstate->clp, stateid, typemask);
6461 return nfserr_bad_stateid;
6462 if ((stid->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
6463 nfs4_put_stid(stid);
6464 if (cstate->minorversion)
6465 return nfserr_deleg_revoked;
6466 return nfserr_bad_stateid;
6472 static struct nfsd_file *
6473 nfs4_find_file(struct nfs4_stid *s, int flags)
6475 struct nfsd_file *ret = NULL;
6480 switch (s->sc_type) {
6481 case NFS4_DELEG_STID:
6482 spin_lock(&s->sc_file->fi_lock);
6483 ret = nfsd_file_get(s->sc_file->fi_deleg_file);
6484 spin_unlock(&s->sc_file->fi_lock);
6486 case NFS4_OPEN_STID:
6487 case NFS4_LOCK_STID:
6488 if (flags & RD_STATE)
6489 ret = find_readable_file(s->sc_file);
6491 ret = find_writeable_file(s->sc_file);
6498 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
6502 status = nfsd4_check_openowner_confirmed(ols);
6505 return nfs4_check_openmode(ols, flags);
6509 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
6510 struct nfsd_file **nfp, int flags)
6512 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6513 struct nfsd_file *nf;
6516 nf = nfs4_find_file(s, flags);
6518 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6519 acc | NFSD_MAY_OWNER_OVERRIDE);
6525 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
6534 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6536 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
6537 if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
6539 list_del(&cps->cp_list);
6540 idr_remove(&nn->s2s_cp_stateids,
6541 cps->cp_stateid.cs_stid.si_opaque.so_id);
6545 * A READ from an inter server to server COPY will have a
6546 * copy stateid. Look up the copy notify stateid from the
6547 * idr structure and take a reference on it.
6549 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6550 struct nfs4_client *clp,
6551 struct nfs4_cpntf_state **cps)
6553 copy_stateid_t *cps_t;
6554 struct nfs4_cpntf_state *state = NULL;
6556 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6557 return nfserr_bad_stateid;
6558 spin_lock(&nn->s2s_cp_lock);
6559 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6561 state = container_of(cps_t, struct nfs4_cpntf_state,
6563 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
6568 refcount_inc(&state->cp_stateid.cs_count);
6570 _free_cpntf_state_locked(nn, state);
6573 spin_unlock(&nn->s2s_cp_lock);
6575 return nfserr_bad_stateid;
6581 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6582 struct nfs4_stid **stid)
6585 struct nfs4_cpntf_state *cps = NULL;
6586 struct nfs4_client *found;
6588 status = manage_cpntf_state(nn, st, NULL, &cps);
6592 cps->cpntf_time = ktime_get_boottime_seconds();
6594 status = nfserr_expired;
6595 found = lookup_clientid(&cps->cp_p_clid, true, nn);
6599 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6600 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6604 status = nfserr_bad_stateid;
6606 put_client_renew(found);
6608 nfs4_put_cpntf_state(nn, cps);
6612 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6614 spin_lock(&nn->s2s_cp_lock);
6615 _free_cpntf_state_locked(nn, cps);
6616 spin_unlock(&nn->s2s_cp_lock);
6620 * nfs4_preprocess_stateid_op - find and prep stateid for an operation
6621 * @rqstp: incoming request from client
6622 * @cstate: current compound state
6623 * @fhp: filehandle associated with requested stateid
6624 * @stateid: stateid (provided by client)
6625 * @flags: flags describing type of operation to be done
6626 * @nfp: optional nfsd_file return pointer (may be NULL)
6627 * @cstid: optional returned nfs4_stid pointer (may be NULL)
6629 * Given info from the client, look up a nfs4_stid for the operation. On
6630 * success, it returns a reference to the nfs4_stid and/or the nfsd_file
6631 * associated with it.
6634 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
6635 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
6636 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6637 struct nfs4_stid **cstid)
6639 struct net *net = SVC_NET(rqstp);
6640 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6641 struct nfs4_stid *s = NULL;
6647 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6649 status = nfserr_bad_stateid;
6651 status = check_special_stateids(net, fhp, stateid,
6656 status = nfsd4_lookup_stateid(cstate, stateid,
6657 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
6659 if (status == nfserr_bad_stateid)
6660 status = find_cpntf_state(nn, stateid, &s);
6663 status = nfsd4_stid_check_stateid_generation(stateid, s,
6664 nfsd4_has_session(cstate));
6668 switch (s->sc_type) {
6669 case NFS4_DELEG_STID:
6670 status = nfs4_check_delegmode(delegstateid(s), flags);
6672 case NFS4_OPEN_STID:
6673 case NFS4_LOCK_STID:
6674 status = nfs4_check_olstateid(openlockstateid(s), flags);
6677 status = nfserr_bad_stateid;
6682 status = nfs4_check_fh(fhp, s);
6685 if (status == nfs_ok && nfp)
6686 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
6689 if (!status && cstid)
6698 * Test if the stateid is valid
6701 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6702 union nfsd4_op_u *u)
6704 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
6705 struct nfsd4_test_stateid_id *stateid;
6706 struct nfs4_client *cl = cstate->clp;
6708 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
6709 stateid->ts_id_status =
6710 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
6716 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6718 struct nfs4_ol_stateid *stp = openlockstateid(s);
6721 ret = nfsd4_lock_ol_stateid(stp);
6725 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6729 ret = nfserr_locks_held;
6730 if (check_for_locks(stp->st_stid.sc_file,
6731 lockowner(stp->st_stateowner)))
6734 release_lock_stateid(stp);
6738 mutex_unlock(&stp->st_mutex);
6745 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6746 union nfsd4_op_u *u)
6748 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
6749 stateid_t *stateid = &free_stateid->fr_stateid;
6750 struct nfs4_stid *s;
6751 struct nfs4_delegation *dp;
6752 struct nfs4_client *cl = cstate->clp;
6753 __be32 ret = nfserr_bad_stateid;
6755 spin_lock(&cl->cl_lock);
6756 s = find_stateid_locked(cl, stateid);
6759 spin_lock(&s->sc_lock);
6760 switch (s->sc_type) {
6761 case NFS4_DELEG_STID:
6762 ret = nfserr_locks_held;
6764 case NFS4_OPEN_STID:
6765 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6768 ret = nfserr_locks_held;
6770 case NFS4_LOCK_STID:
6771 spin_unlock(&s->sc_lock);
6772 refcount_inc(&s->sc_count);
6773 spin_unlock(&cl->cl_lock);
6774 ret = nfsd4_free_lock_stateid(stateid, s);
6776 case NFS4_REVOKED_DELEG_STID:
6777 spin_unlock(&s->sc_lock);
6778 dp = delegstateid(s);
6779 list_del_init(&dp->dl_recall_lru);
6780 spin_unlock(&cl->cl_lock);
6784 /* Default falls through and returns nfserr_bad_stateid */
6786 spin_unlock(&s->sc_lock);
6788 spin_unlock(&cl->cl_lock);
6796 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6797 RD_STATE : WR_STATE;
6800 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6802 struct svc_fh *current_fh = &cstate->current_fh;
6803 struct nfs4_stateowner *sop = stp->st_stateowner;
6806 status = nfsd4_check_seqid(cstate, sop, seqid);
6809 status = nfsd4_lock_ol_stateid(stp);
6810 if (status != nfs_ok)
6812 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6813 if (status == nfs_ok)
6814 status = nfs4_check_fh(current_fh, &stp->st_stid);
6815 if (status != nfs_ok)
6816 mutex_unlock(&stp->st_mutex);
6821 * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op
6822 * @cstate: compund state
6823 * @seqid: seqid (provided by client)
6824 * @stateid: stateid (provided by client)
6825 * @typemask: mask of allowable types for this operation
6826 * @stpp: return pointer for the stateid found
6827 * @nn: net namespace for request
6829 * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and
6830 * return it in @stpp. On a nfs_ok return, the returned stateid will
6831 * have its st_mutex locked.
6834 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6835 stateid_t *stateid, char typemask,
6836 struct nfs4_ol_stateid **stpp,
6837 struct nfsd_net *nn)
6840 struct nfs4_stid *s;
6841 struct nfs4_ol_stateid *stp = NULL;
6843 trace_nfsd_preprocess(seqid, stateid);
6846 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6849 stp = openlockstateid(s);
6850 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6852 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6856 nfs4_put_stid(&stp->st_stid);
6860 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6861 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6864 struct nfs4_openowner *oo;
6865 struct nfs4_ol_stateid *stp;
6867 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6868 NFS4_OPEN_STID, &stp, nn);
6871 oo = openowner(stp->st_stateowner);
6872 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6873 mutex_unlock(&stp->st_mutex);
6874 nfs4_put_stid(&stp->st_stid);
6875 return nfserr_bad_stateid;
6882 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6883 union nfsd4_op_u *u)
6885 struct nfsd4_open_confirm *oc = &u->open_confirm;
6887 struct nfs4_openowner *oo;
6888 struct nfs4_ol_stateid *stp;
6889 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6891 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6892 cstate->current_fh.fh_dentry);
6894 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6898 status = nfs4_preprocess_seqid_op(cstate,
6899 oc->oc_seqid, &oc->oc_req_stateid,
6900 NFS4_OPEN_STID, &stp, nn);
6903 oo = openowner(stp->st_stateowner);
6904 status = nfserr_bad_stateid;
6905 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6906 mutex_unlock(&stp->st_mutex);
6909 oo->oo_flags |= NFS4_OO_CONFIRMED;
6910 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6911 mutex_unlock(&stp->st_mutex);
6912 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6913 nfsd4_client_record_create(oo->oo_owner.so_client);
6916 nfs4_put_stid(&stp->st_stid);
6918 nfsd4_bump_seqid(cstate, status);
6922 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6924 if (!test_access(access, stp))
6926 nfs4_file_put_access(stp->st_stid.sc_file, access);
6927 clear_access(access, stp);
6930 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6932 switch (to_access) {
6933 case NFS4_SHARE_ACCESS_READ:
6934 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6935 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6937 case NFS4_SHARE_ACCESS_WRITE:
6938 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6939 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6941 case NFS4_SHARE_ACCESS_BOTH:
6949 nfsd4_open_downgrade(struct svc_rqst *rqstp,
6950 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
6952 struct nfsd4_open_downgrade *od = &u->open_downgrade;
6954 struct nfs4_ol_stateid *stp;
6955 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6957 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6958 cstate->current_fh.fh_dentry);
6960 /* We don't yet support WANT bits: */
6961 if (od->od_deleg_want)
6962 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6965 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
6966 &od->od_stateid, &stp, nn);
6969 status = nfserr_inval;
6970 if (!test_access(od->od_share_access, stp)) {
6971 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6972 stp->st_access_bmap, od->od_share_access);
6975 if (!test_deny(od->od_share_deny, stp)) {
6976 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6977 stp->st_deny_bmap, od->od_share_deny);
6980 nfs4_stateid_downgrade(stp, od->od_share_access);
6981 reset_union_bmap_deny(od->od_share_deny, stp);
6982 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
6985 mutex_unlock(&stp->st_mutex);
6986 nfs4_put_stid(&stp->st_stid);
6988 nfsd4_bump_seqid(cstate, status);
6992 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6994 struct nfs4_client *clp = s->st_stid.sc_client;
6996 LIST_HEAD(reaplist);
6997 struct nfs4_ol_stateid *stp;
6999 spin_lock(&clp->cl_lock);
7000 unhashed = unhash_open_stateid(s, &reaplist);
7002 if (clp->cl_minorversion) {
7004 put_ol_stateid_locked(s, &reaplist);
7005 spin_unlock(&clp->cl_lock);
7006 list_for_each_entry(stp, &reaplist, st_locks)
7007 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
7008 free_ol_stateid_reaplist(&reaplist);
7010 spin_unlock(&clp->cl_lock);
7011 free_ol_stateid_reaplist(&reaplist);
7013 move_to_close_lru(s, clp->net);
7018 * nfs4_unlock_state() called after encode
7021 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7022 union nfsd4_op_u *u)
7024 struct nfsd4_close *close = &u->close;
7026 struct nfs4_ol_stateid *stp;
7027 struct net *net = SVC_NET(rqstp);
7028 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7030 dprintk("NFSD: nfsd4_close on file %pd\n",
7031 cstate->current_fh.fh_dentry);
7033 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
7035 NFS4_OPEN_STID|NFS4_CLOSED_STID,
7037 nfsd4_bump_seqid(cstate, status);
7041 stp->st_stid.sc_type = NFS4_CLOSED_STID;
7044 * Technically we don't _really_ have to increment or copy it, since
7045 * it should just be gone after this operation and we clobber the
7046 * copied value below, but we continue to do so here just to ensure
7047 * that racing ops see that there was a state change.
7049 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
7051 nfsd4_close_open_stateid(stp);
7052 mutex_unlock(&stp->st_mutex);
7054 /* v4.1+ suggests that we send a special stateid in here, since the
7055 * clients should just ignore this anyway. Since this is not useful
7056 * for v4.0 clients either, we set it to the special close_stateid
7059 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
7061 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
7063 /* put reference from nfs4_preprocess_seqid_op */
7064 nfs4_put_stid(&stp->st_stid);
7070 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7071 union nfsd4_op_u *u)
7073 struct nfsd4_delegreturn *dr = &u->delegreturn;
7074 struct nfs4_delegation *dp;
7075 stateid_t *stateid = &dr->dr_stateid;
7076 struct nfs4_stid *s;
7078 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7080 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7083 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
7086 dp = delegstateid(s);
7087 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
7091 trace_nfsd_deleg_return(stateid);
7092 wake_up_var(d_inode(cstate->current_fh.fh_dentry));
7093 destroy_delegation(dp);
7095 nfs4_put_stid(&dp->dl_stid);
7100 /* last octet in a range */
7102 last_byte_offset(u64 start, u64 len)
7108 return end > start ? end - 1: NFS4_MAX_UINT64;
7112 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
7113 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
7114 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
7115 * locking, this prevents us from being completely protocol-compliant. The
7116 * real solution to this problem is to start using unsigned file offsets in
7117 * the VFS, but this is a very deep change!
7120 nfs4_transform_lock_offset(struct file_lock *lock)
7122 if (lock->fl_start < 0)
7123 lock->fl_start = OFFSET_MAX;
7124 if (lock->fl_end < 0)
7125 lock->fl_end = OFFSET_MAX;
7129 nfsd4_lm_get_owner(fl_owner_t owner)
7131 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7133 nfs4_get_stateowner(&lo->lo_owner);
7138 nfsd4_lm_put_owner(fl_owner_t owner)
7140 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7143 nfs4_put_stateowner(&lo->lo_owner);
7146 /* return pointer to struct nfs4_client if client is expirable */
7148 nfsd4_lm_lock_expirable(struct file_lock *cfl)
7150 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
7151 struct nfs4_client *clp = lo->lo_owner.so_client;
7152 struct nfsd_net *nn;
7154 if (try_to_expire_client(clp)) {
7155 nn = net_generic(clp->net, nfsd_net_id);
7156 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
7162 /* schedule laundromat to run immediately and wait for it to complete */
7164 nfsd4_lm_expire_lock(void)
7166 flush_workqueue(laundry_wq);
7170 nfsd4_lm_notify(struct file_lock *fl)
7172 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
7173 struct net *net = lo->lo_owner.so_client->net;
7174 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7175 struct nfsd4_blocked_lock *nbl = container_of(fl,
7176 struct nfsd4_blocked_lock, nbl_lock);
7179 /* An empty list means that something else is going to be using it */
7180 spin_lock(&nn->blocked_locks_lock);
7181 if (!list_empty(&nbl->nbl_list)) {
7182 list_del_init(&nbl->nbl_list);
7183 list_del_init(&nbl->nbl_lru);
7186 spin_unlock(&nn->blocked_locks_lock);
7189 trace_nfsd_cb_notify_lock(lo, nbl);
7190 nfsd4_run_cb(&nbl->nbl_cb);
7194 static const struct lock_manager_operations nfsd_posix_mng_ops = {
7195 .lm_mod_owner = THIS_MODULE,
7196 .lm_notify = nfsd4_lm_notify,
7197 .lm_get_owner = nfsd4_lm_get_owner,
7198 .lm_put_owner = nfsd4_lm_put_owner,
7199 .lm_lock_expirable = nfsd4_lm_lock_expirable,
7200 .lm_expire_lock = nfsd4_lm_expire_lock,
7204 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
7206 struct nfs4_lockowner *lo;
7208 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
7209 lo = (struct nfs4_lockowner *) fl->fl_owner;
7210 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
7212 if (!deny->ld_owner.data)
7213 /* We just don't care that much */
7215 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
7218 deny->ld_owner.len = 0;
7219 deny->ld_owner.data = NULL;
7220 deny->ld_clientid.cl_boot = 0;
7221 deny->ld_clientid.cl_id = 0;
7223 deny->ld_start = fl->fl_start;
7224 deny->ld_length = NFS4_MAX_UINT64;
7225 if (fl->fl_end != NFS4_MAX_UINT64)
7226 deny->ld_length = fl->fl_end - fl->fl_start + 1;
7227 deny->ld_type = NFS4_READ_LT;
7228 if (fl->fl_type != F_RDLCK)
7229 deny->ld_type = NFS4_WRITE_LT;
7232 static struct nfs4_lockowner *
7233 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
7235 unsigned int strhashval = ownerstr_hashval(owner);
7236 struct nfs4_stateowner *so;
7238 lockdep_assert_held(&clp->cl_lock);
7240 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7242 if (so->so_is_open_owner)
7244 if (same_owner_str(so, owner))
7245 return lockowner(nfs4_get_stateowner(so));
7250 static struct nfs4_lockowner *
7251 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
7253 struct nfs4_lockowner *lo;
7255 spin_lock(&clp->cl_lock);
7256 lo = find_lockowner_str_locked(clp, owner);
7257 spin_unlock(&clp->cl_lock);
7261 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
7263 unhash_lockowner_locked(lockowner(sop));
7266 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
7268 struct nfs4_lockowner *lo = lockowner(sop);
7270 kmem_cache_free(lockowner_slab, lo);
7273 static const struct nfs4_stateowner_operations lockowner_ops = {
7274 .so_unhash = nfs4_unhash_lockowner,
7275 .so_free = nfs4_free_lockowner,
7279 * Alloc a lock owner structure.
7280 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
7283 * strhashval = ownerstr_hashval
7285 static struct nfs4_lockowner *
7286 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
7287 struct nfs4_ol_stateid *open_stp,
7288 struct nfsd4_lock *lock)
7290 struct nfs4_lockowner *lo, *ret;
7292 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
7295 INIT_LIST_HEAD(&lo->lo_blocked);
7296 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
7297 lo->lo_owner.so_is_open_owner = 0;
7298 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
7299 lo->lo_owner.so_ops = &lockowner_ops;
7300 spin_lock(&clp->cl_lock);
7301 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
7303 list_add(&lo->lo_owner.so_strhash,
7304 &clp->cl_ownerstr_hashtbl[strhashval]);
7307 nfs4_free_stateowner(&lo->lo_owner);
7309 spin_unlock(&clp->cl_lock);
7313 static struct nfs4_ol_stateid *
7314 find_lock_stateid(const struct nfs4_lockowner *lo,
7315 const struct nfs4_ol_stateid *ost)
7317 struct nfs4_ol_stateid *lst;
7319 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
7321 /* If ost is not hashed, ost->st_locks will not be valid */
7322 if (!nfs4_ol_stateid_unhashed(ost))
7323 list_for_each_entry(lst, &ost->st_locks, st_locks) {
7324 if (lst->st_stateowner == &lo->lo_owner) {
7325 refcount_inc(&lst->st_stid.sc_count);
7332 static struct nfs4_ol_stateid *
7333 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7334 struct nfs4_file *fp, struct inode *inode,
7335 struct nfs4_ol_stateid *open_stp)
7337 struct nfs4_client *clp = lo->lo_owner.so_client;
7338 struct nfs4_ol_stateid *retstp;
7340 mutex_init(&stp->st_mutex);
7341 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
7343 spin_lock(&clp->cl_lock);
7344 if (nfs4_ol_stateid_unhashed(open_stp))
7346 retstp = find_lock_stateid(lo, open_stp);
7349 refcount_inc(&stp->st_stid.sc_count);
7350 stp->st_stid.sc_type = NFS4_LOCK_STID;
7351 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
7353 stp->st_stid.sc_file = fp;
7354 stp->st_access_bmap = 0;
7355 stp->st_deny_bmap = open_stp->st_deny_bmap;
7356 stp->st_openstp = open_stp;
7357 spin_lock(&fp->fi_lock);
7358 list_add(&stp->st_locks, &open_stp->st_locks);
7359 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
7360 list_add(&stp->st_perfile, &fp->fi_stateids);
7361 spin_unlock(&fp->fi_lock);
7362 spin_unlock(&clp->cl_lock);
7365 spin_unlock(&clp->cl_lock);
7366 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
7367 nfs4_put_stid(&retstp->st_stid);
7370 /* To keep mutex tracking happy */
7371 mutex_unlock(&stp->st_mutex);
7374 spin_unlock(&clp->cl_lock);
7375 mutex_unlock(&stp->st_mutex);
7379 static struct nfs4_ol_stateid *
7380 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7381 struct inode *inode, struct nfs4_ol_stateid *ost,
7384 struct nfs4_stid *ns = NULL;
7385 struct nfs4_ol_stateid *lst;
7386 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7387 struct nfs4_client *clp = oo->oo_owner.so_client;
7390 spin_lock(&clp->cl_lock);
7391 lst = find_lock_stateid(lo, ost);
7392 spin_unlock(&clp->cl_lock);
7394 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7396 nfs4_put_stid(&lst->st_stid);
7398 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7402 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7403 if (lst == openlockstateid(ns))
7412 check_lock_length(u64 offset, u64 length)
7414 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7415 (length > ~offset)));
7418 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
7420 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
7422 lockdep_assert_held(&fp->fi_lock);
7424 if (test_access(access, lock_stp))
7426 __nfs4_file_get_access(fp, access);
7427 set_access(access, lock_stp);
7431 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7432 struct nfs4_ol_stateid *ost,
7433 struct nfsd4_lock *lock,
7434 struct nfs4_ol_stateid **plst, bool *new)
7437 struct nfs4_file *fi = ost->st_stid.sc_file;
7438 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7439 struct nfs4_client *cl = oo->oo_owner.so_client;
7440 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
7441 struct nfs4_lockowner *lo;
7442 struct nfs4_ol_stateid *lst;
7443 unsigned int strhashval;
7445 lo = find_lockowner_str(cl, &lock->lk_new_owner);
7447 strhashval = ownerstr_hashval(&lock->lk_new_owner);
7448 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7450 return nfserr_jukebox;
7452 /* with an existing lockowner, seqids must be the same */
7453 status = nfserr_bad_seqid;
7454 if (!cstate->minorversion &&
7455 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
7459 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7461 status = nfserr_jukebox;
7468 nfs4_put_stateowner(&lo->lo_owner);
7476 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7477 union nfsd4_op_u *u)
7479 struct nfsd4_lock *lock = &u->lock;
7480 struct nfs4_openowner *open_sop = NULL;
7481 struct nfs4_lockowner *lock_sop = NULL;
7482 struct nfs4_ol_stateid *lock_stp = NULL;
7483 struct nfs4_ol_stateid *open_stp = NULL;
7484 struct nfs4_file *fp;
7485 struct nfsd_file *nf = NULL;
7486 struct nfsd4_blocked_lock *nbl = NULL;
7487 struct file_lock *file_lock = NULL;
7488 struct file_lock *conflock = NULL;
7493 unsigned char fl_type;
7494 unsigned int fl_flags = FL_POSIX;
7495 struct net *net = SVC_NET(rqstp);
7496 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7498 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7499 (long long) lock->lk_offset,
7500 (long long) lock->lk_length);
7502 if (check_lock_length(lock->lk_offset, lock->lk_length))
7503 return nfserr_inval;
7505 if ((status = fh_verify(rqstp, &cstate->current_fh,
7506 S_IFREG, NFSD_MAY_LOCK))) {
7507 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7511 if (lock->lk_is_new) {
7512 if (nfsd4_has_session(cstate))
7513 /* See rfc 5661 18.10.3: given clientid is ignored: */
7514 memcpy(&lock->lk_new_clientid,
7515 &cstate->clp->cl_clientid,
7516 sizeof(clientid_t));
7518 /* validate and update open stateid and open seqid */
7519 status = nfs4_preprocess_confirmed_seqid_op(cstate,
7520 lock->lk_new_open_seqid,
7521 &lock->lk_new_open_stateid,
7525 mutex_unlock(&open_stp->st_mutex);
7526 open_sop = openowner(open_stp->st_stateowner);
7527 status = nfserr_bad_stateid;
7528 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
7529 &lock->lk_new_clientid))
7531 status = lookup_or_create_lock_state(cstate, open_stp, lock,
7534 status = nfs4_preprocess_seqid_op(cstate,
7535 lock->lk_old_lock_seqid,
7536 &lock->lk_old_lock_stateid,
7537 NFS4_LOCK_STID, &lock_stp, nn);
7541 lock_sop = lockowner(lock_stp->st_stateowner);
7543 lkflg = setlkflg(lock->lk_type);
7544 status = nfs4_check_openmode(lock_stp, lkflg);
7548 status = nfserr_grace;
7549 if (locks_in_grace(net) && !lock->lk_reclaim)
7551 status = nfserr_no_grace;
7552 if (!locks_in_grace(net) && lock->lk_reclaim)
7555 if (lock->lk_reclaim)
7556 fl_flags |= FL_RECLAIM;
7558 fp = lock_stp->st_stid.sc_file;
7559 switch (lock->lk_type) {
7561 if (nfsd4_has_session(cstate))
7562 fl_flags |= FL_SLEEP;
7565 spin_lock(&fp->fi_lock);
7566 nf = find_readable_file_locked(fp);
7568 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7569 spin_unlock(&fp->fi_lock);
7572 case NFS4_WRITEW_LT:
7573 if (nfsd4_has_session(cstate))
7574 fl_flags |= FL_SLEEP;
7577 spin_lock(&fp->fi_lock);
7578 nf = find_writeable_file_locked(fp);
7580 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7581 spin_unlock(&fp->fi_lock);
7585 status = nfserr_inval;
7590 status = nfserr_openmode;
7595 * Most filesystems with their own ->lock operations will block
7596 * the nfsd thread waiting to acquire the lock. That leads to
7597 * deadlocks (we don't want every nfsd thread tied up waiting
7598 * for file locks), so don't attempt blocking lock notifications
7599 * on those filesystems:
7601 if (nf->nf_file->f_op->lock)
7602 fl_flags &= ~FL_SLEEP;
7604 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
7606 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
7607 status = nfserr_jukebox;
7611 file_lock = &nbl->nbl_lock;
7612 file_lock->fl_type = fl_type;
7613 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
7614 file_lock->fl_pid = current->tgid;
7615 file_lock->fl_file = nf->nf_file;
7616 file_lock->fl_flags = fl_flags;
7617 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7618 file_lock->fl_start = lock->lk_offset;
7619 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
7620 nfs4_transform_lock_offset(file_lock);
7622 conflock = locks_alloc_lock();
7624 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7625 status = nfserr_jukebox;
7629 if (fl_flags & FL_SLEEP) {
7630 nbl->nbl_time = ktime_get_boottime_seconds();
7631 spin_lock(&nn->blocked_locks_lock);
7632 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
7633 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
7634 kref_get(&nbl->nbl_kref);
7635 spin_unlock(&nn->blocked_locks_lock);
7638 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
7640 case 0: /* success! */
7641 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
7643 if (lock->lk_reclaim)
7644 nn->somebody_reclaimed = true;
7646 case FILE_LOCK_DEFERRED:
7647 kref_put(&nbl->nbl_kref, free_nbl);
7650 case -EAGAIN: /* conflock holds conflicting lock */
7651 status = nfserr_denied;
7652 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
7653 nfs4_set_lock_denied(conflock, &lock->lk_denied);
7656 status = nfserr_deadlock;
7659 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
7660 status = nfserrno(err);
7665 /* dequeue it if we queued it before */
7666 if (fl_flags & FL_SLEEP) {
7667 spin_lock(&nn->blocked_locks_lock);
7668 if (!list_empty(&nbl->nbl_list) &&
7669 !list_empty(&nbl->nbl_lru)) {
7670 list_del_init(&nbl->nbl_list);
7671 list_del_init(&nbl->nbl_lru);
7672 kref_put(&nbl->nbl_kref, free_nbl);
7674 /* nbl can use one of lists to be linked to reaplist */
7675 spin_unlock(&nn->blocked_locks_lock);
7677 free_blocked_lock(nbl);
7682 /* Bump seqid manually if the 4.0 replay owner is openowner */
7683 if (cstate->replay_owner &&
7684 cstate->replay_owner != &lock_sop->lo_owner &&
7685 seqid_mutating_err(ntohl(status)))
7686 lock_sop->lo_owner.so_seqid++;
7689 * If this is a new, never-before-used stateid, and we are
7690 * returning an error, then just go ahead and release it.
7693 release_lock_stateid(lock_stp);
7695 mutex_unlock(&lock_stp->st_mutex);
7697 nfs4_put_stid(&lock_stp->st_stid);
7700 nfs4_put_stid(&open_stp->st_stid);
7701 nfsd4_bump_seqid(cstate, status);
7703 locks_free_lock(conflock);
7708 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7709 * so we do a temporary open here just to get an open file to pass to
7712 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
7714 struct nfsd_file *nf;
7715 struct inode *inode;
7718 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7721 inode = fhp->fh_dentry->d_inode;
7722 inode_lock(inode); /* to block new leases till after test_lock: */
7723 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
7726 lock->fl_file = nf->nf_file;
7727 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
7728 lock->fl_file = NULL;
7730 inode_unlock(inode);
7739 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7740 union nfsd4_op_u *u)
7742 struct nfsd4_lockt *lockt = &u->lockt;
7743 struct file_lock *file_lock = NULL;
7744 struct nfs4_lockowner *lo = NULL;
7746 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7748 if (locks_in_grace(SVC_NET(rqstp)))
7749 return nfserr_grace;
7751 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7752 return nfserr_inval;
7754 if (!nfsd4_has_session(cstate)) {
7755 status = set_client(&lockt->lt_clientid, cstate, nn);
7760 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7763 file_lock = locks_alloc_lock();
7765 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7766 status = nfserr_jukebox;
7770 switch (lockt->lt_type) {
7773 file_lock->fl_type = F_RDLCK;
7776 case NFS4_WRITEW_LT:
7777 file_lock->fl_type = F_WRLCK;
7780 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
7781 status = nfserr_inval;
7785 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
7787 file_lock->fl_owner = (fl_owner_t)lo;
7788 file_lock->fl_pid = current->tgid;
7789 file_lock->fl_flags = FL_POSIX;
7791 file_lock->fl_start = lockt->lt_offset;
7792 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
7794 nfs4_transform_lock_offset(file_lock);
7796 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
7800 if (file_lock->fl_type != F_UNLCK) {
7801 status = nfserr_denied;
7802 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
7806 nfs4_put_stateowner(&lo->lo_owner);
7808 locks_free_lock(file_lock);
7813 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7814 union nfsd4_op_u *u)
7816 struct nfsd4_locku *locku = &u->locku;
7817 struct nfs4_ol_stateid *stp;
7818 struct nfsd_file *nf = NULL;
7819 struct file_lock *file_lock = NULL;
7822 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7824 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7825 (long long) locku->lu_offset,
7826 (long long) locku->lu_length);
7828 if (check_lock_length(locku->lu_offset, locku->lu_length))
7829 return nfserr_inval;
7831 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
7832 &locku->lu_stateid, NFS4_LOCK_STID,
7836 nf = find_any_file(stp->st_stid.sc_file);
7838 status = nfserr_lock_range;
7841 file_lock = locks_alloc_lock();
7843 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7844 status = nfserr_jukebox;
7848 file_lock->fl_type = F_UNLCK;
7849 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7850 file_lock->fl_pid = current->tgid;
7851 file_lock->fl_file = nf->nf_file;
7852 file_lock->fl_flags = FL_POSIX;
7853 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7854 file_lock->fl_start = locku->lu_offset;
7856 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7858 nfs4_transform_lock_offset(file_lock);
7860 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7862 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7865 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7869 mutex_unlock(&stp->st_mutex);
7870 nfs4_put_stid(&stp->st_stid);
7872 nfsd4_bump_seqid(cstate, status);
7874 locks_free_lock(file_lock);
7878 status = nfserrno(err);
7884 * true: locks held by lockowner
7885 * false: no locks held by lockowner
7888 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7890 struct file_lock *fl;
7892 struct nfsd_file *nf = find_any_file(fp);
7893 struct inode *inode;
7894 struct file_lock_context *flctx;
7897 /* Any valid lock stateid should have some sort of access */
7902 inode = file_inode(nf->nf_file);
7903 flctx = locks_inode_context(inode);
7905 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7906 spin_lock(&flctx->flc_lock);
7907 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7908 if (fl->fl_owner == (fl_owner_t)lowner) {
7913 spin_unlock(&flctx->flc_lock);
7920 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
7921 * @rqstp: RPC transaction
7922 * @cstate: NFSv4 COMPOUND state
7923 * @u: RELEASE_LOCKOWNER arguments
7925 * The lockowner's so_count is bumped when a lock record is added
7926 * or when copying a conflicting lock. The latter case is brief,
7927 * but can lead to fleeting false positives when looking for
7931 * %nfs_ok: lockowner released or not found
7932 * %nfserr_locks_held: lockowner still in use
7933 * %nfserr_stale_clientid: clientid no longer active
7934 * %nfserr_expired: clientid not recognized
7937 nfsd4_release_lockowner(struct svc_rqst *rqstp,
7938 struct nfsd4_compound_state *cstate,
7939 union nfsd4_op_u *u)
7941 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
7942 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7943 clientid_t *clid = &rlockowner->rl_clientid;
7944 struct nfs4_ol_stateid *stp;
7945 struct nfs4_lockowner *lo;
7946 struct nfs4_client *clp;
7947 LIST_HEAD(reaplist);
7950 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7951 clid->cl_boot, clid->cl_id);
7953 status = set_client(clid, cstate, nn);
7958 spin_lock(&clp->cl_lock);
7959 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
7961 spin_unlock(&clp->cl_lock);
7964 if (atomic_read(&lo->lo_owner.so_count) != 2) {
7965 spin_unlock(&clp->cl_lock);
7966 nfs4_put_stateowner(&lo->lo_owner);
7967 return nfserr_locks_held;
7969 unhash_lockowner_locked(lo);
7970 while (!list_empty(&lo->lo_owner.so_stateids)) {
7971 stp = list_first_entry(&lo->lo_owner.so_stateids,
7972 struct nfs4_ol_stateid,
7974 WARN_ON(!unhash_lock_stateid(stp));
7975 put_ol_stateid_locked(stp, &reaplist);
7977 spin_unlock(&clp->cl_lock);
7979 free_ol_stateid_reaplist(&reaplist);
7980 remove_blocked_locks(lo);
7981 nfs4_put_stateowner(&lo->lo_owner);
7985 static inline struct nfs4_client_reclaim *
7988 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
7992 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7994 struct nfs4_client_reclaim *crp;
7996 crp = nfsd4_find_reclaim_client(name, nn);
7997 return (crp && crp->cr_clp);
8001 * failure => all reset bets are off, nfserr_no_grace...
8003 * The caller is responsible for freeing name.data if NULL is returned (it
8004 * will be freed in nfs4_remove_reclaim_record in the normal case).
8006 struct nfs4_client_reclaim *
8007 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
8008 struct nfsd_net *nn)
8010 unsigned int strhashval;
8011 struct nfs4_client_reclaim *crp;
8013 crp = alloc_reclaim();
8015 strhashval = clientstr_hashval(name);
8016 INIT_LIST_HEAD(&crp->cr_strhash);
8017 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
8018 crp->cr_name.data = name.data;
8019 crp->cr_name.len = name.len;
8020 crp->cr_princhash.data = princhash.data;
8021 crp->cr_princhash.len = princhash.len;
8023 nn->reclaim_str_hashtbl_size++;
8029 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
8031 list_del(&crp->cr_strhash);
8032 kfree(crp->cr_name.data);
8033 kfree(crp->cr_princhash.data);
8035 nn->reclaim_str_hashtbl_size--;
8039 nfs4_release_reclaim(struct nfsd_net *nn)
8041 struct nfs4_client_reclaim *crp = NULL;
8044 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8045 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
8046 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
8047 struct nfs4_client_reclaim, cr_strhash);
8048 nfs4_remove_reclaim_record(crp, nn);
8051 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
8055 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
8056 struct nfs4_client_reclaim *
8057 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
8059 unsigned int strhashval;
8060 struct nfs4_client_reclaim *crp = NULL;
8062 strhashval = clientstr_hashval(name);
8063 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
8064 if (compare_blob(&crp->cr_name, &name) == 0) {
8072 nfs4_check_open_reclaim(struct nfs4_client *clp)
8074 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
8075 return nfserr_no_grace;
8077 if (nfsd4_client_record_check(clp))
8078 return nfserr_reclaim_bad;
8084 * Since the lifetime of a delegation isn't limited to that of an open, a
8085 * client may quite reasonably hang on to a delegation as long as it has
8086 * the inode cached. This becomes an obvious problem the first time a
8087 * client's inode cache approaches the size of the server's total memory.
8089 * For now we avoid this problem by imposing a hard limit on the number
8090 * of delegations, which varies according to the server's memory size.
8093 set_max_delegations(void)
8096 * Allow at most 4 delegations per megabyte of RAM. Quick
8097 * estimates suggest that in the worst case (where every delegation
8098 * is for a different inode), a delegation could take about 1.5K,
8099 * giving a worst case usage of about 6% of memory.
8101 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
8104 static int nfs4_state_create_net(struct net *net)
8106 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8109 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8110 sizeof(struct list_head),
8112 if (!nn->conf_id_hashtbl)
8114 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8115 sizeof(struct list_head),
8117 if (!nn->unconf_id_hashtbl)
8119 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
8120 sizeof(struct list_head),
8122 if (!nn->sessionid_hashtbl)
8125 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8126 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
8127 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
8129 for (i = 0; i < SESSION_HASH_SIZE; i++)
8130 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
8131 nn->conf_name_tree = RB_ROOT;
8132 nn->unconf_name_tree = RB_ROOT;
8133 nn->boot_time = ktime_get_real_seconds();
8134 nn->grace_ended = false;
8135 nn->nfsd4_manager.block_opens = true;
8136 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
8137 INIT_LIST_HEAD(&nn->client_lru);
8138 INIT_LIST_HEAD(&nn->close_lru);
8139 INIT_LIST_HEAD(&nn->del_recall_lru);
8140 spin_lock_init(&nn->client_lock);
8141 spin_lock_init(&nn->s2s_cp_lock);
8142 idr_init(&nn->s2s_cp_stateids);
8144 spin_lock_init(&nn->blocked_locks_lock);
8145 INIT_LIST_HEAD(&nn->blocked_locks_lru);
8147 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
8148 INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker);
8151 nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client");
8152 if (!nn->nfsd_client_shrinker)
8155 nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan;
8156 nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count;
8157 nn->nfsd_client_shrinker->private_data = nn;
8159 shrinker_register(nn->nfsd_client_shrinker);
8165 kfree(nn->sessionid_hashtbl);
8167 kfree(nn->unconf_id_hashtbl);
8169 kfree(nn->conf_id_hashtbl);
8175 nfs4_state_destroy_net(struct net *net)
8178 struct nfs4_client *clp = NULL;
8179 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8181 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8182 while (!list_empty(&nn->conf_id_hashtbl[i])) {
8183 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8184 destroy_client(clp);
8188 WARN_ON(!list_empty(&nn->blocked_locks_lru));
8190 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8191 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
8192 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8193 destroy_client(clp);
8197 kfree(nn->sessionid_hashtbl);
8198 kfree(nn->unconf_id_hashtbl);
8199 kfree(nn->conf_id_hashtbl);
8204 nfs4_state_start_net(struct net *net)
8206 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8209 ret = nfs4_state_create_net(net);
8212 locks_start_grace(net, &nn->nfsd4_manager);
8213 nfsd4_client_tracking_init(net);
8214 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
8216 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
8217 nn->nfsd4_grace, net->ns.inum);
8218 trace_nfsd_grace_start(nn);
8219 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
8223 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
8225 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
8226 nfsd4_end_grace(nn);
8230 /* initialization to perform when the nfsd service is started: */
8233 nfs4_state_start(void)
8237 ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params);
8241 ret = nfsd4_create_callback_queue();
8243 rhltable_destroy(&nfs4_file_rhltable);
8247 set_max_delegations();
8252 nfs4_state_shutdown_net(struct net *net)
8254 struct nfs4_delegation *dp = NULL;
8255 struct list_head *pos, *next, reaplist;
8256 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8258 shrinker_free(nn->nfsd_client_shrinker);
8259 cancel_work(&nn->nfsd_shrinker_work);
8260 cancel_delayed_work_sync(&nn->laundromat_work);
8261 locks_end_grace(&nn->nfsd4_manager);
8263 INIT_LIST_HEAD(&reaplist);
8264 spin_lock(&state_lock);
8265 list_for_each_safe(pos, next, &nn->del_recall_lru) {
8266 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8267 WARN_ON(!unhash_delegation_locked(dp));
8268 list_add(&dp->dl_recall_lru, &reaplist);
8270 spin_unlock(&state_lock);
8271 list_for_each_safe(pos, next, &reaplist) {
8272 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8273 list_del_init(&dp->dl_recall_lru);
8274 destroy_unhashed_deleg(dp);
8277 nfsd4_client_tracking_exit(net);
8278 nfs4_state_destroy_net(net);
8279 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
8280 nfsd4_ssc_shutdown_umount(nn);
8285 nfs4_state_shutdown(void)
8287 nfsd4_destroy_callback_queue();
8288 rhltable_destroy(&nfs4_file_rhltable);
8292 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8294 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
8295 CURRENT_STATEID(stateid))
8296 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8300 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8302 if (cstate->minorversion) {
8303 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
8304 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8309 clear_current_stateid(struct nfsd4_compound_state *cstate)
8311 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8315 * functions to set current state id
8318 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
8319 union nfsd4_op_u *u)
8321 put_stateid(cstate, &u->open_downgrade.od_stateid);
8325 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
8326 union nfsd4_op_u *u)
8328 put_stateid(cstate, &u->open.op_stateid);
8332 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
8333 union nfsd4_op_u *u)
8335 put_stateid(cstate, &u->close.cl_stateid);
8339 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
8340 union nfsd4_op_u *u)
8342 put_stateid(cstate, &u->lock.lk_resp_stateid);
8346 * functions to consume current state id
8350 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
8351 union nfsd4_op_u *u)
8353 get_stateid(cstate, &u->open_downgrade.od_stateid);
8357 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8358 union nfsd4_op_u *u)
8360 get_stateid(cstate, &u->delegreturn.dr_stateid);
8364 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8365 union nfsd4_op_u *u)
8367 get_stateid(cstate, &u->free_stateid.fr_stateid);
8371 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8372 union nfsd4_op_u *u)
8374 get_stateid(cstate, &u->setattr.sa_stateid);
8378 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8379 union nfsd4_op_u *u)
8381 get_stateid(cstate, &u->close.cl_stateid);
8385 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8386 union nfsd4_op_u *u)
8388 get_stateid(cstate, &u->locku.lu_stateid);
8392 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8393 union nfsd4_op_u *u)
8395 get_stateid(cstate, &u->read.rd_stateid);
8399 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8400 union nfsd4_op_u *u)
8402 get_stateid(cstate, &u->write.wr_stateid);
8406 * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict
8407 * @rqstp: RPC transaction context
8408 * @inode: file to be checked for a conflict
8410 * This function is called when there is a conflict between a write
8411 * delegation and a change/size GETATTR from another client. The server
8412 * must either use the CB_GETATTR to get the current values of the
8413 * attributes from the client that holds the delegation or recall the
8414 * delegation before replying to the GETATTR. See RFC 8881 section
8417 * The current implementation does not support CB_GETATTR yet. However
8418 * this can avoid recalling the delegation could be added in follow up
8421 * Returns 0 if there is no conflict; otherwise an nfs_stat
8425 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct inode *inode)
8428 struct file_lock_context *ctx;
8429 struct file_lock *fl;
8430 struct nfs4_delegation *dp;
8432 ctx = locks_inode_context(inode);
8435 spin_lock(&ctx->flc_lock);
8436 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
8437 if (fl->fl_flags == FL_LAYOUT)
8439 if (fl->fl_lmops != &nfsd_lease_mng_ops) {
8441 * non-nfs lease, if it's a lease with F_RDLCK then
8442 * we are done; there isn't any write delegation
8445 if (fl->fl_type == F_RDLCK)
8449 if (fl->fl_type == F_WRLCK) {
8451 if (dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) {
8452 spin_unlock(&ctx->flc_lock);
8456 spin_unlock(&ctx->flc_lock);
8457 nfsd_stats_wdeleg_getattr_inc();
8458 status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
8459 if (status != nfserr_jukebox ||
8460 !nfsd_wait_for_delegreturn(rqstp, inode))
8466 spin_unlock(&ctx->flc_lock);