2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
45 #include <linux/string_helpers.h>
46 #include <linux/fsnotify.h>
47 #include <linux/rhashtable.h>
48 #include <linux/nfs_ssc.h>
53 #include "current_stateid.h"
57 #include "filecache.h"
60 #define NFSDDBG_FACILITY NFSDDBG_PROC
62 #define all_ones {{ ~0, ~0}, ~0}
63 static const stateid_t one_stateid = {
65 .si_opaque = all_ones,
67 static const stateid_t zero_stateid = {
70 static const stateid_t currentstateid = {
73 static const stateid_t close_stateid = {
74 .si_generation = 0xffffffffU,
77 static u64 current_sessionid = 1;
79 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
80 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
81 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
82 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
84 /* forward declarations */
85 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
86 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
87 void nfsd4_end_grace(struct nfsd_net *nn);
88 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
89 static void nfsd4_file_hash_remove(struct nfs4_file *fi);
94 * Currently used for the del_recall_lru and file hash table. In an
95 * effort to decrease the scope of the client_mutex, this spinlock may
96 * eventually cover more:
98 static DEFINE_SPINLOCK(state_lock);
100 enum nfsd4_st_mutex_lock_subclass {
101 OPEN_STATEID_MUTEX = 0,
102 LOCK_STATEID_MUTEX = 1,
106 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
107 * the refcount on the open stateid to drop.
109 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
112 * A waitqueue where a writer to clients/#/ctl destroying a client can
113 * wait for cl_rpc_users to drop to 0 and then for the client to be
116 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
118 static struct kmem_cache *client_slab;
119 static struct kmem_cache *openowner_slab;
120 static struct kmem_cache *lockowner_slab;
121 static struct kmem_cache *file_slab;
122 static struct kmem_cache *stateid_slab;
123 static struct kmem_cache *deleg_slab;
124 static struct kmem_cache *odstate_slab;
126 static void free_session(struct nfsd4_session *);
128 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
129 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
130 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops;
132 static struct workqueue_struct *laundry_wq;
134 int nfsd4_create_laundry_wq(void)
138 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
139 if (laundry_wq == NULL)
144 void nfsd4_destroy_laundry_wq(void)
146 destroy_workqueue(laundry_wq);
149 static bool is_session_dead(struct nfsd4_session *ses)
151 return ses->se_flags & NFS4_SESSION_DEAD;
154 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
156 if (atomic_read(&ses->se_ref) > ref_held_by_me)
157 return nfserr_jukebox;
158 ses->se_flags |= NFS4_SESSION_DEAD;
162 static bool is_client_expired(struct nfs4_client *clp)
164 return clp->cl_time == 0;
167 static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn,
168 struct nfs4_client *clp)
170 if (clp->cl_state != NFSD4_ACTIVE)
171 atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0);
174 static __be32 get_client_locked(struct nfs4_client *clp)
176 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178 lockdep_assert_held(&nn->client_lock);
180 if (is_client_expired(clp))
181 return nfserr_expired;
182 atomic_inc(&clp->cl_rpc_users);
183 nfsd4_dec_courtesy_client_count(nn, clp);
184 clp->cl_state = NFSD4_ACTIVE;
188 /* must be called under the client_lock */
190 renew_client_locked(struct nfs4_client *clp)
192 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
194 if (is_client_expired(clp)) {
196 printk("%s: client (clientid %08x/%08x) already expired\n",
198 clp->cl_clientid.cl_boot,
199 clp->cl_clientid.cl_id);
203 list_move_tail(&clp->cl_lru, &nn->client_lru);
204 clp->cl_time = ktime_get_boottime_seconds();
205 nfsd4_dec_courtesy_client_count(nn, clp);
206 clp->cl_state = NFSD4_ACTIVE;
209 static void put_client_renew_locked(struct nfs4_client *clp)
211 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
213 lockdep_assert_held(&nn->client_lock);
215 if (!atomic_dec_and_test(&clp->cl_rpc_users))
217 if (!is_client_expired(clp))
218 renew_client_locked(clp);
220 wake_up_all(&expiry_wq);
223 static void put_client_renew(struct nfs4_client *clp)
225 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
227 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
229 if (!is_client_expired(clp))
230 renew_client_locked(clp);
232 wake_up_all(&expiry_wq);
233 spin_unlock(&nn->client_lock);
236 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
240 if (is_session_dead(ses))
241 return nfserr_badsession;
242 status = get_client_locked(ses->se_client);
245 atomic_inc(&ses->se_ref);
249 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
251 struct nfs4_client *clp = ses->se_client;
252 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
254 lockdep_assert_held(&nn->client_lock);
256 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
258 put_client_renew_locked(clp);
261 static void nfsd4_put_session(struct nfsd4_session *ses)
263 struct nfs4_client *clp = ses->se_client;
264 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
266 spin_lock(&nn->client_lock);
267 nfsd4_put_session_locked(ses);
268 spin_unlock(&nn->client_lock);
271 static struct nfsd4_blocked_lock *
272 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
275 struct nfsd4_blocked_lock *cur, *found = NULL;
277 spin_lock(&nn->blocked_locks_lock);
278 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
279 if (fh_match(fh, &cur->nbl_fh)) {
280 list_del_init(&cur->nbl_list);
281 WARN_ON(list_empty(&cur->nbl_lru));
282 list_del_init(&cur->nbl_lru);
287 spin_unlock(&nn->blocked_locks_lock);
289 locks_delete_block(&found->nbl_lock);
293 static struct nfsd4_blocked_lock *
294 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
297 struct nfsd4_blocked_lock *nbl;
299 nbl = find_blocked_lock(lo, fh, nn);
301 nbl = kmalloc(sizeof(*nbl), GFP_KERNEL);
303 INIT_LIST_HEAD(&nbl->nbl_list);
304 INIT_LIST_HEAD(&nbl->nbl_lru);
305 fh_copy_shallow(&nbl->nbl_fh, fh);
306 locks_init_lock(&nbl->nbl_lock);
307 kref_init(&nbl->nbl_kref);
308 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
309 &nfsd4_cb_notify_lock_ops,
310 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
317 free_nbl(struct kref *kref)
319 struct nfsd4_blocked_lock *nbl;
321 nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
326 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
328 locks_delete_block(&nbl->nbl_lock);
329 locks_release_private(&nbl->nbl_lock);
330 kref_put(&nbl->nbl_kref, free_nbl);
334 remove_blocked_locks(struct nfs4_lockowner *lo)
336 struct nfs4_client *clp = lo->lo_owner.so_client;
337 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
338 struct nfsd4_blocked_lock *nbl;
341 /* Dequeue all blocked locks */
342 spin_lock(&nn->blocked_locks_lock);
343 while (!list_empty(&lo->lo_blocked)) {
344 nbl = list_first_entry(&lo->lo_blocked,
345 struct nfsd4_blocked_lock,
347 list_del_init(&nbl->nbl_list);
348 WARN_ON(list_empty(&nbl->nbl_lru));
349 list_move(&nbl->nbl_lru, &reaplist);
351 spin_unlock(&nn->blocked_locks_lock);
354 while (!list_empty(&reaplist)) {
355 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
357 list_del_init(&nbl->nbl_lru);
358 free_blocked_lock(nbl);
363 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
365 struct nfsd4_blocked_lock *nbl = container_of(cb,
366 struct nfsd4_blocked_lock, nbl_cb);
367 locks_delete_block(&nbl->nbl_lock);
371 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
373 trace_nfsd_cb_notify_lock_done(&zero_stateid, task);
376 * Since this is just an optimization, we don't try very hard if it
377 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
378 * just quit trying on anything else.
380 switch (task->tk_status) {
382 rpc_delay(task, 1 * HZ);
390 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
392 struct nfsd4_blocked_lock *nbl = container_of(cb,
393 struct nfsd4_blocked_lock, nbl_cb);
395 free_blocked_lock(nbl);
398 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
399 .prepare = nfsd4_cb_notify_lock_prepare,
400 .done = nfsd4_cb_notify_lock_done,
401 .release = nfsd4_cb_notify_lock_release,
405 * We store the NONE, READ, WRITE, and BOTH bits separately in the
406 * st_{access,deny}_bmap field of the stateid, in order to track not
407 * only what share bits are currently in force, but also what
408 * combinations of share bits previous opens have used. This allows us
409 * to enforce the recommendation in
410 * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
411 * the server return an error if the client attempt to downgrade to a
412 * combination of share bits not explicable by closing some of its
415 * This enforcement is arguably incomplete, since we don't keep
416 * track of access/deny bit combinations; so, e.g., we allow:
418 * OPEN allow read, deny write
419 * OPEN allow both, deny none
420 * DOWNGRADE allow read, deny none
422 * which we should reject.
424 * But you could also argue that our current code is already overkill,
425 * since it only exists to return NFS4ERR_INVAL on incorrect client
429 bmap_to_share_mode(unsigned long bmap)
432 unsigned int access = 0;
434 for (i = 1; i < 4; i++) {
435 if (test_bit(i, &bmap))
441 /* set share access for a given stateid */
443 set_access(u32 access, struct nfs4_ol_stateid *stp)
445 unsigned char mask = 1 << access;
447 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
448 stp->st_access_bmap |= mask;
451 /* clear share access for a given stateid */
453 clear_access(u32 access, struct nfs4_ol_stateid *stp)
455 unsigned char mask = 1 << access;
457 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
458 stp->st_access_bmap &= ~mask;
461 /* test whether a given stateid has access */
463 test_access(u32 access, struct nfs4_ol_stateid *stp)
465 unsigned char mask = 1 << access;
467 return (bool)(stp->st_access_bmap & mask);
470 /* set share deny for a given stateid */
472 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
474 unsigned char mask = 1 << deny;
476 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
477 stp->st_deny_bmap |= mask;
480 /* clear share deny for a given stateid */
482 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
484 unsigned char mask = 1 << deny;
486 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
487 stp->st_deny_bmap &= ~mask;
490 /* test whether a given stateid is denying specific access */
492 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
494 unsigned char mask = 1 << deny;
496 return (bool)(stp->st_deny_bmap & mask);
499 static int nfs4_access_to_omode(u32 access)
501 switch (access & NFS4_SHARE_ACCESS_BOTH) {
502 case NFS4_SHARE_ACCESS_READ:
504 case NFS4_SHARE_ACCESS_WRITE:
506 case NFS4_SHARE_ACCESS_BOTH:
514 access_permit_read(struct nfs4_ol_stateid *stp)
516 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
517 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
518 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
522 access_permit_write(struct nfs4_ol_stateid *stp)
524 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
525 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
528 static inline struct nfs4_stateowner *
529 nfs4_get_stateowner(struct nfs4_stateowner *sop)
531 atomic_inc(&sop->so_count);
536 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
538 return (sop->so_owner.len == owner->len) &&
539 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
542 static struct nfs4_openowner *
543 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
544 struct nfs4_client *clp)
546 struct nfs4_stateowner *so;
548 lockdep_assert_held(&clp->cl_lock);
550 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
552 if (!so->so_is_open_owner)
554 if (same_owner_str(so, &open->op_owner))
555 return openowner(nfs4_get_stateowner(so));
560 static struct nfs4_openowner *
561 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
562 struct nfs4_client *clp)
564 struct nfs4_openowner *oo;
566 spin_lock(&clp->cl_lock);
567 oo = find_openstateowner_str_locked(hashval, open, clp);
568 spin_unlock(&clp->cl_lock);
573 opaque_hashval(const void *ptr, int nbytes)
575 unsigned char *cptr = (unsigned char *) ptr;
585 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
587 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
589 kmem_cache_free(file_slab, fp);
593 put_nfs4_file(struct nfs4_file *fi)
595 if (refcount_dec_and_test(&fi->fi_ref)) {
596 nfsd4_file_hash_remove(fi);
597 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
598 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
599 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
603 static struct nfsd_file *
604 find_writeable_file_locked(struct nfs4_file *f)
606 struct nfsd_file *ret;
608 lockdep_assert_held(&f->fi_lock);
610 ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
612 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
616 static struct nfsd_file *
617 find_writeable_file(struct nfs4_file *f)
619 struct nfsd_file *ret;
621 spin_lock(&f->fi_lock);
622 ret = find_writeable_file_locked(f);
623 spin_unlock(&f->fi_lock);
628 static struct nfsd_file *
629 find_readable_file_locked(struct nfs4_file *f)
631 struct nfsd_file *ret;
633 lockdep_assert_held(&f->fi_lock);
635 ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
637 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
641 static struct nfsd_file *
642 find_readable_file(struct nfs4_file *f)
644 struct nfsd_file *ret;
646 spin_lock(&f->fi_lock);
647 ret = find_readable_file_locked(f);
648 spin_unlock(&f->fi_lock);
653 static struct nfsd_file *
654 find_rw_file(struct nfs4_file *f)
656 struct nfsd_file *ret;
658 spin_lock(&f->fi_lock);
659 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
660 spin_unlock(&f->fi_lock);
666 find_any_file(struct nfs4_file *f)
668 struct nfsd_file *ret;
672 spin_lock(&f->fi_lock);
673 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
675 ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
677 ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
679 spin_unlock(&f->fi_lock);
683 static struct nfsd_file *find_any_file_locked(struct nfs4_file *f)
685 lockdep_assert_held(&f->fi_lock);
687 if (f->fi_fds[O_RDWR])
688 return f->fi_fds[O_RDWR];
689 if (f->fi_fds[O_WRONLY])
690 return f->fi_fds[O_WRONLY];
691 if (f->fi_fds[O_RDONLY])
692 return f->fi_fds[O_RDONLY];
696 static atomic_long_t num_delegations;
697 unsigned long max_delegations;
700 * Open owner state (share locks)
703 /* hash tables for lock and open owners */
704 #define OWNER_HASH_BITS 8
705 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
706 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
708 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
712 ret = opaque_hashval(ownername->data, ownername->len);
713 return ret & OWNER_HASH_MASK;
716 static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp;
718 static const struct rhashtable_params nfs4_file_rhash_params = {
719 .key_len = sizeof_field(struct nfs4_file, fi_inode),
720 .key_offset = offsetof(struct nfs4_file, fi_inode),
721 .head_offset = offsetof(struct nfs4_file, fi_rlist),
724 * Start with a single page hash table to reduce resizing churn
725 * on light workloads.
728 .automatic_shrinking = true,
732 * Check if courtesy clients have conflicting access and resolve it if possible
734 * access: is op_share_access if share_access is true.
735 * Check if access mode, op_share_access, would conflict with
736 * the current deny mode of the file 'fp'.
737 * access: is op_share_deny if share_access is false.
738 * Check if the deny mode, op_share_deny, would conflict with
739 * current access of the file 'fp'.
740 * stp: skip checking this entry.
741 * new_stp: normal open, not open upgrade.
744 * false - access/deny mode conflict with normal client.
745 * true - no conflict or conflict with courtesy client(s) is resolved.
748 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
749 struct nfs4_ol_stateid *stp, u32 access, bool share_access)
751 struct nfs4_ol_stateid *st;
752 bool resolvable = true;
755 struct nfs4_client *clp;
757 lockdep_assert_held(&fp->fi_lock);
758 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
759 /* ignore lock stateid */
762 if (st == stp && new_stp)
764 /* check file access against deny mode or vice versa */
765 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
766 if (!(access & bmap_to_share_mode(bmap)))
768 clp = st->st_stid.sc_client;
769 if (try_to_expire_client(clp))
775 clp = stp->st_stid.sc_client;
776 nn = net_generic(clp->net, nfsd_net_id);
777 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
783 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
785 lockdep_assert_held(&fp->fi_lock);
787 if (access & NFS4_SHARE_ACCESS_WRITE)
788 atomic_inc(&fp->fi_access[O_WRONLY]);
789 if (access & NFS4_SHARE_ACCESS_READ)
790 atomic_inc(&fp->fi_access[O_RDONLY]);
794 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
796 lockdep_assert_held(&fp->fi_lock);
798 /* Does this access mode make sense? */
799 if (access & ~NFS4_SHARE_ACCESS_BOTH)
802 /* Does it conflict with a deny mode already set? */
803 if ((access & fp->fi_share_deny) != 0)
804 return nfserr_share_denied;
806 __nfs4_file_get_access(fp, access);
810 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
812 /* Common case is that there is no deny mode. */
814 /* Does this deny mode make sense? */
815 if (deny & ~NFS4_SHARE_DENY_BOTH)
818 if ((deny & NFS4_SHARE_DENY_READ) &&
819 atomic_read(&fp->fi_access[O_RDONLY]))
820 return nfserr_share_denied;
822 if ((deny & NFS4_SHARE_DENY_WRITE) &&
823 atomic_read(&fp->fi_access[O_WRONLY]))
824 return nfserr_share_denied;
829 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
831 might_lock(&fp->fi_lock);
833 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
834 struct nfsd_file *f1 = NULL;
835 struct nfsd_file *f2 = NULL;
837 swap(f1, fp->fi_fds[oflag]);
838 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
839 swap(f2, fp->fi_fds[O_RDWR]);
840 spin_unlock(&fp->fi_lock);
848 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
850 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
852 if (access & NFS4_SHARE_ACCESS_WRITE)
853 __nfs4_file_put_access(fp, O_WRONLY);
854 if (access & NFS4_SHARE_ACCESS_READ)
855 __nfs4_file_put_access(fp, O_RDONLY);
859 * Allocate a new open/delegation state counter. This is needed for
860 * pNFS for proper return on close semantics.
862 * Note that we only allocate it for pNFS-enabled exports, otherwise
863 * all pointers to struct nfs4_clnt_odstate are always NULL.
865 static struct nfs4_clnt_odstate *
866 alloc_clnt_odstate(struct nfs4_client *clp)
868 struct nfs4_clnt_odstate *co;
870 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
873 refcount_set(&co->co_odcount, 1);
879 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
881 struct nfs4_file *fp = co->co_file;
883 lockdep_assert_held(&fp->fi_lock);
884 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
888 get_clnt_odstate(struct nfs4_clnt_odstate *co)
891 refcount_inc(&co->co_odcount);
895 put_clnt_odstate(struct nfs4_clnt_odstate *co)
897 struct nfs4_file *fp;
903 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
904 list_del(&co->co_perfile);
905 spin_unlock(&fp->fi_lock);
907 nfsd4_return_all_file_layouts(co->co_client, fp);
908 kmem_cache_free(odstate_slab, co);
912 static struct nfs4_clnt_odstate *
913 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
915 struct nfs4_clnt_odstate *co;
916 struct nfs4_client *cl;
923 spin_lock(&fp->fi_lock);
924 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
925 if (co->co_client == cl) {
926 get_clnt_odstate(co);
932 hash_clnt_odstate_locked(new);
934 spin_unlock(&fp->fi_lock);
938 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
939 void (*sc_free)(struct nfs4_stid *))
941 struct nfs4_stid *stid;
944 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
948 idr_preload(GFP_KERNEL);
949 spin_lock(&cl->cl_lock);
950 /* Reserving 0 for start of file in nfsdfs "states" file: */
951 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
952 spin_unlock(&cl->cl_lock);
957 stid->sc_free = sc_free;
958 stid->sc_client = cl;
959 stid->sc_stateid.si_opaque.so_id = new_id;
960 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
961 /* Will be incremented before return to client: */
962 refcount_set(&stid->sc_count, 1);
963 spin_lock_init(&stid->sc_lock);
964 INIT_LIST_HEAD(&stid->sc_cp_list);
967 * It shouldn't be a problem to reuse an opaque stateid value.
968 * I don't think it is for 4.1. But with 4.0 I worry that, for
969 * example, a stray write retransmission could be accepted by
970 * the server when it should have been rejected. Therefore,
971 * adopt a trick from the sctp code to attempt to maximize the
972 * amount of time until an id is reused, by ensuring they always
973 * "increase" (mod INT_MAX):
977 kmem_cache_free(slab, stid);
982 * Create a unique stateid_t to represent each COPY.
984 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
985 unsigned char cs_type)
989 stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
990 stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
992 idr_preload(GFP_KERNEL);
993 spin_lock(&nn->s2s_cp_lock);
994 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
995 stid->cs_stid.si_opaque.so_id = new_id;
996 stid->cs_stid.si_generation = 1;
997 spin_unlock(&nn->s2s_cp_lock);
1001 stid->cs_type = cs_type;
1005 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
1007 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID);
1010 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
1011 struct nfs4_stid *p_stid)
1013 struct nfs4_cpntf_state *cps;
1015 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
1018 cps->cpntf_time = ktime_get_boottime_seconds();
1019 refcount_set(&cps->cp_stateid.cs_count, 1);
1020 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
1022 spin_lock(&nn->s2s_cp_lock);
1023 list_add(&cps->cp_list, &p_stid->sc_cp_list);
1024 spin_unlock(&nn->s2s_cp_lock);
1031 void nfs4_free_copy_state(struct nfsd4_copy *copy)
1033 struct nfsd_net *nn;
1035 if (copy->cp_stateid.cs_type != NFS4_COPY_STID)
1037 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
1038 spin_lock(&nn->s2s_cp_lock);
1039 idr_remove(&nn->s2s_cp_stateids,
1040 copy->cp_stateid.cs_stid.si_opaque.so_id);
1041 spin_unlock(&nn->s2s_cp_lock);
1044 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1046 struct nfs4_cpntf_state *cps;
1047 struct nfsd_net *nn;
1049 nn = net_generic(net, nfsd_net_id);
1050 spin_lock(&nn->s2s_cp_lock);
1051 while (!list_empty(&stid->sc_cp_list)) {
1052 cps = list_first_entry(&stid->sc_cp_list,
1053 struct nfs4_cpntf_state, cp_list);
1054 _free_cpntf_state_locked(nn, cps);
1056 spin_unlock(&nn->s2s_cp_lock);
1059 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
1061 struct nfs4_stid *stid;
1063 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
1067 return openlockstateid(stid);
1070 static void nfs4_free_deleg(struct nfs4_stid *stid)
1072 struct nfs4_delegation *dp = delegstateid(stid);
1074 WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
1075 WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
1076 WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
1077 WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
1078 kmem_cache_free(deleg_slab, stid);
1079 atomic_long_dec(&num_delegations);
1083 * When we recall a delegation, we should be careful not to hand it
1084 * out again straight away.
1085 * To ensure this we keep a pair of bloom filters ('new' and 'old')
1086 * in which the filehandles of recalled delegations are "stored".
1087 * If a filehandle appear in either filter, a delegation is blocked.
1088 * When a delegation is recalled, the filehandle is stored in the "new"
1090 * Every 30 seconds we swap the filters and clear the "new" one,
1091 * unless both are empty of course.
1093 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
1094 * low 3 bytes as hash-table indices.
1096 * 'blocked_delegations_lock', which is always taken in block_delegations(),
1097 * is used to manage concurrent access. Testing does not need the lock
1098 * except when swapping the two filters.
1100 static DEFINE_SPINLOCK(blocked_delegations_lock);
1101 static struct bloom_pair {
1102 int entries, old_entries;
1104 int new; /* index into 'set' */
1105 DECLARE_BITMAP(set[2], 256);
1106 } blocked_delegations;
1108 static int delegation_blocked(struct knfsd_fh *fh)
1111 struct bloom_pair *bd = &blocked_delegations;
1113 if (bd->entries == 0)
1115 if (ktime_get_seconds() - bd->swap_time > 30) {
1116 spin_lock(&blocked_delegations_lock);
1117 if (ktime_get_seconds() - bd->swap_time > 30) {
1118 bd->entries -= bd->old_entries;
1119 bd->old_entries = bd->entries;
1120 memset(bd->set[bd->new], 0,
1121 sizeof(bd->set[0]));
1122 bd->new = 1-bd->new;
1123 bd->swap_time = ktime_get_seconds();
1125 spin_unlock(&blocked_delegations_lock);
1127 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1128 if (test_bit(hash&255, bd->set[0]) &&
1129 test_bit((hash>>8)&255, bd->set[0]) &&
1130 test_bit((hash>>16)&255, bd->set[0]))
1133 if (test_bit(hash&255, bd->set[1]) &&
1134 test_bit((hash>>8)&255, bd->set[1]) &&
1135 test_bit((hash>>16)&255, bd->set[1]))
1141 static void block_delegations(struct knfsd_fh *fh)
1144 struct bloom_pair *bd = &blocked_delegations;
1146 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1148 spin_lock(&blocked_delegations_lock);
1149 __set_bit(hash&255, bd->set[bd->new]);
1150 __set_bit((hash>>8)&255, bd->set[bd->new]);
1151 __set_bit((hash>>16)&255, bd->set[bd->new]);
1152 if (bd->entries == 0)
1153 bd->swap_time = ktime_get_seconds();
1155 spin_unlock(&blocked_delegations_lock);
1158 static struct nfs4_delegation *
1159 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1160 struct nfs4_clnt_odstate *odstate, u32 dl_type)
1162 struct nfs4_delegation *dp;
1163 struct nfs4_stid *stid;
1166 dprintk("NFSD alloc_init_deleg\n");
1167 n = atomic_long_inc_return(&num_delegations);
1168 if (n < 0 || n > max_delegations)
1170 if (delegation_blocked(&fp->fi_fhandle))
1172 stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
1175 dp = delegstateid(stid);
1178 * delegation seqid's are never incremented. The 4.1 special
1179 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1180 * 0 anyway just for consistency and use 1:
1182 dp->dl_stid.sc_stateid.si_generation = 1;
1183 INIT_LIST_HEAD(&dp->dl_perfile);
1184 INIT_LIST_HEAD(&dp->dl_perclnt);
1185 INIT_LIST_HEAD(&dp->dl_recall_lru);
1186 dp->dl_clnt_odstate = odstate;
1187 get_clnt_odstate(odstate);
1188 dp->dl_type = dl_type;
1190 dp->dl_recalled = false;
1191 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1192 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1193 nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client,
1194 &nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR);
1195 dp->dl_cb_fattr.ncf_file_modified = false;
1196 dp->dl_cb_fattr.ncf_cb_bmap[0] = FATTR4_WORD0_CHANGE | FATTR4_WORD0_SIZE;
1198 dp->dl_stid.sc_file = fp;
1201 atomic_long_dec(&num_delegations);
1206 nfs4_put_stid(struct nfs4_stid *s)
1208 struct nfs4_file *fp = s->sc_file;
1209 struct nfs4_client *clp = s->sc_client;
1211 might_lock(&clp->cl_lock);
1213 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1214 wake_up_all(&close_wq);
1217 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1218 nfs4_free_cpntf_statelist(clp->net, s);
1219 spin_unlock(&clp->cl_lock);
1226 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1228 stateid_t *src = &stid->sc_stateid;
1230 spin_lock(&stid->sc_lock);
1231 if (unlikely(++src->si_generation == 0))
1232 src->si_generation = 1;
1233 memcpy(dst, src, sizeof(*dst));
1234 spin_unlock(&stid->sc_lock);
1237 static void put_deleg_file(struct nfs4_file *fp)
1239 struct nfsd_file *nf = NULL;
1241 spin_lock(&fp->fi_lock);
1242 if (--fp->fi_delegees == 0)
1243 swap(nf, fp->fi_deleg_file);
1244 spin_unlock(&fp->fi_lock);
1250 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1252 struct nfs4_file *fp = dp->dl_stid.sc_file;
1253 struct nfsd_file *nf = fp->fi_deleg_file;
1255 WARN_ON_ONCE(!fp->fi_delegees);
1257 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1261 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1263 put_clnt_odstate(dp->dl_clnt_odstate);
1264 nfs4_unlock_deleg_lease(dp);
1265 nfs4_put_stid(&dp->dl_stid);
1268 void nfs4_unhash_stid(struct nfs4_stid *s)
1274 * nfs4_delegation_exists - Discover if this delegation already exists
1275 * @clp: a pointer to the nfs4_client we're granting a delegation to
1276 * @fp: a pointer to the nfs4_file we're granting a delegation on
1279 * On success: true iff an existing delegation is found
1283 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1285 struct nfs4_delegation *searchdp = NULL;
1286 struct nfs4_client *searchclp = NULL;
1288 lockdep_assert_held(&state_lock);
1289 lockdep_assert_held(&fp->fi_lock);
1291 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1292 searchclp = searchdp->dl_stid.sc_client;
1293 if (clp == searchclp) {
1301 * hash_delegation_locked - Add a delegation to the appropriate lists
1302 * @dp: a pointer to the nfs4_delegation we are adding.
1303 * @fp: a pointer to the nfs4_file we're granting a delegation on
1306 * On success: NULL if the delegation was successfully hashed.
1308 * On error: -EAGAIN if one was previously granted to this
1309 * nfs4_client for this nfs4_file. Delegation is not hashed.
1314 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1316 struct nfs4_client *clp = dp->dl_stid.sc_client;
1318 lockdep_assert_held(&state_lock);
1319 lockdep_assert_held(&fp->fi_lock);
1321 if (nfs4_delegation_exists(clp, fp))
1323 refcount_inc(&dp->dl_stid.sc_count);
1324 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1325 list_add(&dp->dl_perfile, &fp->fi_delegations);
1326 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1330 static bool delegation_hashed(struct nfs4_delegation *dp)
1332 return !(list_empty(&dp->dl_perfile));
1336 unhash_delegation_locked(struct nfs4_delegation *dp)
1338 struct nfs4_file *fp = dp->dl_stid.sc_file;
1340 lockdep_assert_held(&state_lock);
1342 if (!delegation_hashed(dp))
1345 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1346 /* Ensure that deleg break won't try to requeue it */
1348 spin_lock(&fp->fi_lock);
1349 list_del_init(&dp->dl_perclnt);
1350 list_del_init(&dp->dl_recall_lru);
1351 list_del_init(&dp->dl_perfile);
1352 spin_unlock(&fp->fi_lock);
1356 static void destroy_delegation(struct nfs4_delegation *dp)
1360 spin_lock(&state_lock);
1361 unhashed = unhash_delegation_locked(dp);
1362 spin_unlock(&state_lock);
1364 destroy_unhashed_deleg(dp);
1367 static void revoke_delegation(struct nfs4_delegation *dp)
1369 struct nfs4_client *clp = dp->dl_stid.sc_client;
1371 WARN_ON(!list_empty(&dp->dl_recall_lru));
1373 trace_nfsd_stid_revoke(&dp->dl_stid);
1375 if (clp->cl_minorversion) {
1376 spin_lock(&clp->cl_lock);
1377 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1378 refcount_inc(&dp->dl_stid.sc_count);
1379 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1380 spin_unlock(&clp->cl_lock);
1382 destroy_unhashed_deleg(dp);
1389 static unsigned int clientid_hashval(u32 id)
1391 return id & CLIENT_HASH_MASK;
1394 static unsigned int clientstr_hashval(struct xdr_netobj name)
1396 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1400 * A stateid that had a deny mode associated with it is being released
1401 * or downgraded. Recalculate the deny mode on the file.
1404 recalculate_deny_mode(struct nfs4_file *fp)
1406 struct nfs4_ol_stateid *stp;
1408 spin_lock(&fp->fi_lock);
1409 fp->fi_share_deny = 0;
1410 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1411 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1412 spin_unlock(&fp->fi_lock);
1416 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1419 bool change = false;
1421 for (i = 1; i < 4; i++) {
1422 if ((i & deny) != i) {
1428 /* Recalculate per-file deny mode if there was a change */
1430 recalculate_deny_mode(stp->st_stid.sc_file);
1433 /* release all access and file references for a given stateid */
1435 release_all_access(struct nfs4_ol_stateid *stp)
1438 struct nfs4_file *fp = stp->st_stid.sc_file;
1440 if (fp && stp->st_deny_bmap != 0)
1441 recalculate_deny_mode(fp);
1443 for (i = 1; i < 4; i++) {
1444 if (test_access(i, stp))
1445 nfs4_file_put_access(stp->st_stid.sc_file, i);
1446 clear_access(i, stp);
1450 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1452 kfree(sop->so_owner.data);
1453 sop->so_ops->so_free(sop);
1456 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1458 struct nfs4_client *clp = sop->so_client;
1460 might_lock(&clp->cl_lock);
1462 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1464 sop->so_ops->so_unhash(sop);
1465 spin_unlock(&clp->cl_lock);
1466 nfs4_free_stateowner(sop);
1470 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1472 return list_empty(&stp->st_perfile);
1475 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1477 struct nfs4_file *fp = stp->st_stid.sc_file;
1479 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1481 if (list_empty(&stp->st_perfile))
1484 spin_lock(&fp->fi_lock);
1485 list_del_init(&stp->st_perfile);
1486 spin_unlock(&fp->fi_lock);
1487 list_del(&stp->st_perstateowner);
1491 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1493 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1495 put_clnt_odstate(stp->st_clnt_odstate);
1496 release_all_access(stp);
1497 if (stp->st_stateowner)
1498 nfs4_put_stateowner(stp->st_stateowner);
1499 WARN_ON(!list_empty(&stid->sc_cp_list));
1500 kmem_cache_free(stateid_slab, stid);
1503 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1505 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1506 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1507 struct nfsd_file *nf;
1509 nf = find_any_file(stp->st_stid.sc_file);
1511 get_file(nf->nf_file);
1512 filp_close(nf->nf_file, (fl_owner_t)lo);
1515 nfs4_free_ol_stateid(stid);
1519 * Put the persistent reference to an already unhashed generic stateid, while
1520 * holding the cl_lock. If it's the last reference, then put it onto the
1521 * reaplist for later destruction.
1523 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1524 struct list_head *reaplist)
1526 struct nfs4_stid *s = &stp->st_stid;
1527 struct nfs4_client *clp = s->sc_client;
1529 lockdep_assert_held(&clp->cl_lock);
1531 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1533 if (!refcount_dec_and_test(&s->sc_count)) {
1534 wake_up_all(&close_wq);
1538 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1539 list_add(&stp->st_locks, reaplist);
1542 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1544 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1546 if (!unhash_ol_stateid(stp))
1548 list_del_init(&stp->st_locks);
1549 nfs4_unhash_stid(&stp->st_stid);
1553 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1555 struct nfs4_client *clp = stp->st_stid.sc_client;
1558 spin_lock(&clp->cl_lock);
1559 unhashed = unhash_lock_stateid(stp);
1560 spin_unlock(&clp->cl_lock);
1562 nfs4_put_stid(&stp->st_stid);
1565 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1567 struct nfs4_client *clp = lo->lo_owner.so_client;
1569 lockdep_assert_held(&clp->cl_lock);
1571 list_del_init(&lo->lo_owner.so_strhash);
1575 * Free a list of generic stateids that were collected earlier after being
1579 free_ol_stateid_reaplist(struct list_head *reaplist)
1581 struct nfs4_ol_stateid *stp;
1582 struct nfs4_file *fp;
1586 while (!list_empty(reaplist)) {
1587 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1589 list_del(&stp->st_locks);
1590 fp = stp->st_stid.sc_file;
1591 stp->st_stid.sc_free(&stp->st_stid);
1597 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1598 struct list_head *reaplist)
1600 struct nfs4_ol_stateid *stp;
1602 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1604 while (!list_empty(&open_stp->st_locks)) {
1605 stp = list_entry(open_stp->st_locks.next,
1606 struct nfs4_ol_stateid, st_locks);
1607 WARN_ON(!unhash_lock_stateid(stp));
1608 put_ol_stateid_locked(stp, reaplist);
1612 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1613 struct list_head *reaplist)
1615 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1617 if (!unhash_ol_stateid(stp))
1619 release_open_stateid_locks(stp, reaplist);
1623 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1625 LIST_HEAD(reaplist);
1627 spin_lock(&stp->st_stid.sc_client->cl_lock);
1628 if (unhash_open_stateid(stp, &reaplist))
1629 put_ol_stateid_locked(stp, &reaplist);
1630 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1631 free_ol_stateid_reaplist(&reaplist);
1634 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1636 struct nfs4_client *clp = oo->oo_owner.so_client;
1638 lockdep_assert_held(&clp->cl_lock);
1640 list_del_init(&oo->oo_owner.so_strhash);
1641 list_del_init(&oo->oo_perclient);
1644 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1646 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1648 struct nfs4_ol_stateid *s;
1650 spin_lock(&nn->client_lock);
1651 s = oo->oo_last_closed_stid;
1653 list_del_init(&oo->oo_close_lru);
1654 oo->oo_last_closed_stid = NULL;
1656 spin_unlock(&nn->client_lock);
1658 nfs4_put_stid(&s->st_stid);
1661 static void release_openowner(struct nfs4_openowner *oo)
1663 struct nfs4_ol_stateid *stp;
1664 struct nfs4_client *clp = oo->oo_owner.so_client;
1665 struct list_head reaplist;
1667 INIT_LIST_HEAD(&reaplist);
1669 spin_lock(&clp->cl_lock);
1670 unhash_openowner_locked(oo);
1671 while (!list_empty(&oo->oo_owner.so_stateids)) {
1672 stp = list_first_entry(&oo->oo_owner.so_stateids,
1673 struct nfs4_ol_stateid, st_perstateowner);
1674 if (unhash_open_stateid(stp, &reaplist))
1675 put_ol_stateid_locked(stp, &reaplist);
1677 spin_unlock(&clp->cl_lock);
1678 free_ol_stateid_reaplist(&reaplist);
1679 release_last_closed_stateid(oo);
1680 nfs4_put_stateowner(&oo->oo_owner);
1684 hash_sessionid(struct nfs4_sessionid *sessionid)
1686 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1688 return sid->sequence % SESSION_HASH_SIZE;
1691 #ifdef CONFIG_SUNRPC_DEBUG
1693 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1695 u32 *ptr = (u32 *)(&sessionid->data[0]);
1696 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1700 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1706 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1707 * won't be used for replay.
1709 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1711 struct nfs4_stateowner *so = cstate->replay_owner;
1713 if (nfserr == nfserr_replay_me)
1716 if (!seqid_mutating_err(ntohl(nfserr))) {
1717 nfsd4_cstate_clear_replay(cstate);
1722 if (so->so_is_open_owner)
1723 release_last_closed_stateid(openowner(so));
1729 gen_sessionid(struct nfsd4_session *ses)
1731 struct nfs4_client *clp = ses->se_client;
1732 struct nfsd4_sessionid *sid;
1734 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1735 sid->clientid = clp->cl_clientid;
1736 sid->sequence = current_sessionid++;
1741 * The protocol defines ca_maxresponssize_cached to include the size of
1742 * the rpc header, but all we need to cache is the data starting after
1743 * the end of the initial SEQUENCE operation--the rest we regenerate
1744 * each time. Therefore we can advertise a ca_maxresponssize_cached
1745 * value that is the number of bytes in our cache plus a few additional
1746 * bytes. In order to stay on the safe side, and not promise more than
1747 * we can cache, those additional bytes must be the minimum possible: 24
1748 * bytes of rpc header (xid through accept state, with AUTH_NULL
1749 * verifier), 12 for the compound header (with zero-length tag), and 44
1750 * for the SEQUENCE op response:
1752 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1755 free_session_slots(struct nfsd4_session *ses)
1759 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1760 free_svc_cred(&ses->se_slots[i]->sl_cred);
1761 kfree(ses->se_slots[i]);
1766 * We don't actually need to cache the rpc and session headers, so we
1767 * can allocate a little less for each slot:
1769 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1773 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1776 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1777 return size + sizeof(struct nfsd4_slot);
1781 * XXX: If we run out of reserved DRC memory we could (up to a point)
1782 * re-negotiate active sessions and reduce their slot usage to make
1783 * room for new connections. For now we just fail the create session.
1785 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1787 u32 slotsize = slot_bytes(ca);
1788 u32 num = ca->maxreqs;
1789 unsigned long avail, total_avail;
1790 unsigned int scale_factor;
1792 spin_lock(&nfsd_drc_lock);
1793 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1794 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1796 /* We have handed out more space than we chose in
1797 * set_max_drc() to allow. That isn't really a
1798 * problem as long as that doesn't make us think we
1799 * have lots more due to integer overflow.
1802 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1804 * Never use more than a fraction of the remaining memory,
1805 * unless it's the only way to give this client a slot.
1806 * The chosen fraction is either 1/8 or 1/number of threads,
1807 * whichever is smaller. This ensures there are adequate
1808 * slots to support multiple clients per thread.
1809 * Give the client one slot even if that would require
1810 * over-allocation--it is better than failure.
1812 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1814 avail = clamp_t(unsigned long, avail, slotsize,
1815 total_avail/scale_factor);
1816 num = min_t(int, num, avail / slotsize);
1817 num = max_t(int, num, 1);
1818 nfsd_drc_mem_used += num * slotsize;
1819 spin_unlock(&nfsd_drc_lock);
1824 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1826 int slotsize = slot_bytes(ca);
1828 spin_lock(&nfsd_drc_lock);
1829 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1830 spin_unlock(&nfsd_drc_lock);
1833 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1834 struct nfsd4_channel_attrs *battrs)
1836 int numslots = fattrs->maxreqs;
1837 int slotsize = slot_bytes(fattrs);
1838 struct nfsd4_session *new;
1841 BUILD_BUG_ON(struct_size(new, se_slots, NFSD_MAX_SLOTS_PER_SESSION)
1844 new = kzalloc(struct_size(new, se_slots, numslots), GFP_KERNEL);
1847 /* allocate each struct nfsd4_slot and data cache in one piece */
1848 for (i = 0; i < numslots; i++) {
1849 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1850 if (!new->se_slots[i])
1854 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1855 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1860 kfree(new->se_slots[i]);
1865 static void free_conn(struct nfsd4_conn *c)
1867 svc_xprt_put(c->cn_xprt);
1871 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1873 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1874 struct nfs4_client *clp = c->cn_session->se_client;
1876 trace_nfsd_cb_lost(clp);
1878 spin_lock(&clp->cl_lock);
1879 if (!list_empty(&c->cn_persession)) {
1880 list_del(&c->cn_persession);
1883 nfsd4_probe_callback(clp);
1884 spin_unlock(&clp->cl_lock);
1887 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1889 struct nfsd4_conn *conn;
1891 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1894 svc_xprt_get(rqstp->rq_xprt);
1895 conn->cn_xprt = rqstp->rq_xprt;
1896 conn->cn_flags = flags;
1897 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1901 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1903 conn->cn_session = ses;
1904 list_add(&conn->cn_persession, &ses->se_conns);
1907 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1909 struct nfs4_client *clp = ses->se_client;
1911 spin_lock(&clp->cl_lock);
1912 __nfsd4_hash_conn(conn, ses);
1913 spin_unlock(&clp->cl_lock);
1916 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1918 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1919 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1922 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1926 nfsd4_hash_conn(conn, ses);
1927 ret = nfsd4_register_conn(conn);
1929 /* oops; xprt is already down: */
1930 nfsd4_conn_lost(&conn->cn_xpt_user);
1931 /* We may have gained or lost a callback channel: */
1932 nfsd4_probe_callback_sync(ses->se_client);
1935 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1937 u32 dir = NFS4_CDFC4_FORE;
1939 if (cses->flags & SESSION4_BACK_CHAN)
1940 dir |= NFS4_CDFC4_BACK;
1941 return alloc_conn(rqstp, dir);
1944 /* must be called under client_lock */
1945 static void nfsd4_del_conns(struct nfsd4_session *s)
1947 struct nfs4_client *clp = s->se_client;
1948 struct nfsd4_conn *c;
1950 spin_lock(&clp->cl_lock);
1951 while (!list_empty(&s->se_conns)) {
1952 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1953 list_del_init(&c->cn_persession);
1954 spin_unlock(&clp->cl_lock);
1956 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1959 spin_lock(&clp->cl_lock);
1961 spin_unlock(&clp->cl_lock);
1964 static void __free_session(struct nfsd4_session *ses)
1966 free_session_slots(ses);
1970 static void free_session(struct nfsd4_session *ses)
1972 nfsd4_del_conns(ses);
1973 nfsd4_put_drc_mem(&ses->se_fchannel);
1974 __free_session(ses);
1977 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1980 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1982 new->se_client = clp;
1985 INIT_LIST_HEAD(&new->se_conns);
1987 new->se_cb_seq_nr = 1;
1988 new->se_flags = cses->flags;
1989 new->se_cb_prog = cses->callback_prog;
1990 new->se_cb_sec = cses->cb_sec;
1991 atomic_set(&new->se_ref, 0);
1992 idx = hash_sessionid(&new->se_sessionid);
1993 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1994 spin_lock(&clp->cl_lock);
1995 list_add(&new->se_perclnt, &clp->cl_sessions);
1996 spin_unlock(&clp->cl_lock);
1999 struct sockaddr *sa = svc_addr(rqstp);
2001 * This is a little silly; with sessions there's no real
2002 * use for the callback address. Use the peer address
2003 * as a reasonable default for now, but consider fixing
2004 * the rpc client not to require an address in the
2007 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2008 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2012 /* caller must hold client_lock */
2013 static struct nfsd4_session *
2014 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2016 struct nfsd4_session *elem;
2018 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2020 lockdep_assert_held(&nn->client_lock);
2022 dump_sessionid(__func__, sessionid);
2023 idx = hash_sessionid(sessionid);
2024 /* Search in the appropriate list */
2025 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2026 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2027 NFS4_MAX_SESSIONID_LEN)) {
2032 dprintk("%s: session not found\n", __func__);
2036 static struct nfsd4_session *
2037 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2040 struct nfsd4_session *session;
2041 __be32 status = nfserr_badsession;
2043 session = __find_in_sessionid_hashtbl(sessionid, net);
2046 status = nfsd4_get_session_locked(session);
2054 /* caller must hold client_lock */
2056 unhash_session(struct nfsd4_session *ses)
2058 struct nfs4_client *clp = ses->se_client;
2059 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2061 lockdep_assert_held(&nn->client_lock);
2063 list_del(&ses->se_hash);
2064 spin_lock(&ses->se_client->cl_lock);
2065 list_del(&ses->se_perclnt);
2066 spin_unlock(&ses->se_client->cl_lock);
2069 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2071 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2074 * We're assuming the clid was not given out from a boot
2075 * precisely 2^32 (about 136 years) before this one. That seems
2076 * a safe assumption:
2078 if (clid->cl_boot == (u32)nn->boot_time)
2080 trace_nfsd_clid_stale(clid);
2085 * XXX Should we use a slab cache ?
2086 * This type of memory management is somewhat inefficient, but we use it
2087 * anyway since SETCLIENTID is not a common operation.
2089 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2090 struct nfsd_net *nn)
2092 struct nfs4_client *clp;
2095 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
2096 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2099 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2102 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2103 if (clp->cl_name.data == NULL)
2105 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2106 sizeof(struct list_head),
2108 if (!clp->cl_ownerstr_hashtbl)
2109 goto err_no_hashtbl;
2110 for (i = 0; i < OWNER_HASH_SIZE; i++)
2111 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2112 INIT_LIST_HEAD(&clp->cl_sessions);
2113 idr_init(&clp->cl_stateids);
2114 atomic_set(&clp->cl_rpc_users, 0);
2115 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2116 clp->cl_state = NFSD4_ACTIVE;
2117 atomic_inc(&nn->nfs4_client_count);
2118 atomic_set(&clp->cl_delegs_in_recall, 0);
2119 INIT_LIST_HEAD(&clp->cl_idhash);
2120 INIT_LIST_HEAD(&clp->cl_openowners);
2121 INIT_LIST_HEAD(&clp->cl_delegations);
2122 INIT_LIST_HEAD(&clp->cl_lru);
2123 INIT_LIST_HEAD(&clp->cl_revoked);
2124 #ifdef CONFIG_NFSD_PNFS
2125 INIT_LIST_HEAD(&clp->cl_lo_states);
2127 INIT_LIST_HEAD(&clp->async_copies);
2128 spin_lock_init(&clp->async_lock);
2129 spin_lock_init(&clp->cl_lock);
2130 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2133 kfree(clp->cl_name.data);
2135 kmem_cache_free(client_slab, clp);
2139 static void __free_client(struct kref *k)
2141 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2142 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2144 free_svc_cred(&clp->cl_cred);
2145 kfree(clp->cl_ownerstr_hashtbl);
2146 kfree(clp->cl_name.data);
2147 kfree(clp->cl_nii_domain.data);
2148 kfree(clp->cl_nii_name.data);
2149 idr_destroy(&clp->cl_stateids);
2151 kmem_cache_free(client_slab, clp);
2154 static void drop_client(struct nfs4_client *clp)
2156 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2160 free_client(struct nfs4_client *clp)
2162 while (!list_empty(&clp->cl_sessions)) {
2163 struct nfsd4_session *ses;
2164 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2166 list_del(&ses->se_perclnt);
2167 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2170 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2171 if (clp->cl_nfsd_dentry) {
2172 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2173 clp->cl_nfsd_dentry = NULL;
2174 wake_up_all(&expiry_wq);
2179 /* must be called under the client_lock */
2181 unhash_client_locked(struct nfs4_client *clp)
2183 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2184 struct nfsd4_session *ses;
2186 lockdep_assert_held(&nn->client_lock);
2188 /* Mark the client as expired! */
2190 /* Make it invisible */
2191 if (!list_empty(&clp->cl_idhash)) {
2192 list_del_init(&clp->cl_idhash);
2193 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2194 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2196 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2198 list_del_init(&clp->cl_lru);
2199 spin_lock(&clp->cl_lock);
2200 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2201 list_del_init(&ses->se_hash);
2202 spin_unlock(&clp->cl_lock);
2206 unhash_client(struct nfs4_client *clp)
2208 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2210 spin_lock(&nn->client_lock);
2211 unhash_client_locked(clp);
2212 spin_unlock(&nn->client_lock);
2215 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2217 if (atomic_read(&clp->cl_rpc_users))
2218 return nfserr_jukebox;
2219 unhash_client_locked(clp);
2224 __destroy_client(struct nfs4_client *clp)
2226 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2228 struct nfs4_openowner *oo;
2229 struct nfs4_delegation *dp;
2230 struct list_head reaplist;
2232 INIT_LIST_HEAD(&reaplist);
2233 spin_lock(&state_lock);
2234 while (!list_empty(&clp->cl_delegations)) {
2235 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2236 WARN_ON(!unhash_delegation_locked(dp));
2237 list_add(&dp->dl_recall_lru, &reaplist);
2239 spin_unlock(&state_lock);
2240 while (!list_empty(&reaplist)) {
2241 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2242 list_del_init(&dp->dl_recall_lru);
2243 destroy_unhashed_deleg(dp);
2245 while (!list_empty(&clp->cl_revoked)) {
2246 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2247 list_del_init(&dp->dl_recall_lru);
2248 nfs4_put_stid(&dp->dl_stid);
2250 while (!list_empty(&clp->cl_openowners)) {
2251 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2252 nfs4_get_stateowner(&oo->oo_owner);
2253 release_openowner(oo);
2255 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2256 struct nfs4_stateowner *so, *tmp;
2258 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2260 /* Should be no openowners at this point */
2261 WARN_ON_ONCE(so->so_is_open_owner);
2262 remove_blocked_locks(lockowner(so));
2265 nfsd4_return_all_client_layouts(clp);
2266 nfsd4_shutdown_copy(clp);
2267 nfsd4_shutdown_callback(clp);
2268 if (clp->cl_cb_conn.cb_xprt)
2269 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2270 atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2271 nfsd4_dec_courtesy_client_count(nn, clp);
2273 wake_up_all(&expiry_wq);
2277 destroy_client(struct nfs4_client *clp)
2280 __destroy_client(clp);
2283 static void inc_reclaim_complete(struct nfs4_client *clp)
2285 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2287 if (!nn->track_reclaim_completes)
2289 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2291 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2292 nn->reclaim_str_hashtbl_size) {
2293 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2295 nfsd4_end_grace(nn);
2299 static void expire_client(struct nfs4_client *clp)
2302 nfsd4_client_record_remove(clp);
2303 __destroy_client(clp);
2306 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2308 memcpy(target->cl_verifier.data, source->data,
2309 sizeof(target->cl_verifier.data));
2312 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2314 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2315 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2318 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2320 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2321 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2323 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2324 if ((source->cr_principal && !target->cr_principal) ||
2325 (source->cr_raw_principal && !target->cr_raw_principal) ||
2326 (source->cr_targ_princ && !target->cr_targ_princ))
2329 target->cr_flavor = source->cr_flavor;
2330 target->cr_uid = source->cr_uid;
2331 target->cr_gid = source->cr_gid;
2332 target->cr_group_info = source->cr_group_info;
2333 get_group_info(target->cr_group_info);
2334 target->cr_gss_mech = source->cr_gss_mech;
2335 if (source->cr_gss_mech)
2336 gss_mech_get(source->cr_gss_mech);
2341 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2343 if (o1->len < o2->len)
2345 if (o1->len > o2->len)
2347 return memcmp(o1->data, o2->data, o1->len);
2351 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2353 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2357 same_clid(clientid_t *cl1, clientid_t *cl2)
2359 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2362 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2366 if (g1->ngroups != g2->ngroups)
2368 for (i=0; i<g1->ngroups; i++)
2369 if (!gid_eq(g1->gid[i], g2->gid[i]))
2375 * RFC 3530 language requires clid_inuse be returned when the
2376 * "principal" associated with a requests differs from that previously
2377 * used. We use uid, gid's, and gss principal string as our best
2378 * approximation. We also don't want to allow non-gss use of a client
2379 * established using gss: in theory cr_principal should catch that
2380 * change, but in practice cr_principal can be null even in the gss case
2381 * since gssd doesn't always pass down a principal string.
2383 static bool is_gss_cred(struct svc_cred *cr)
2385 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2386 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2391 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2393 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2394 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2395 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2396 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2398 /* XXX: check that cr_targ_princ fields match ? */
2399 if (cr1->cr_principal == cr2->cr_principal)
2401 if (!cr1->cr_principal || !cr2->cr_principal)
2403 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2406 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2408 struct svc_cred *cr = &rqstp->rq_cred;
2411 if (!cr->cr_gss_mech)
2413 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2414 return service == RPC_GSS_SVC_INTEGRITY ||
2415 service == RPC_GSS_SVC_PRIVACY;
2418 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2420 struct svc_cred *cr = &rqstp->rq_cred;
2422 if (!cl->cl_mach_cred)
2424 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2426 if (!svc_rqst_integrity_protected(rqstp))
2428 if (cl->cl_cred.cr_raw_principal)
2429 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2430 cr->cr_raw_principal);
2431 if (!cr->cr_principal)
2433 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2436 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2441 * This is opaque to client, so no need to byte-swap. Use
2442 * __force to keep sparse happy
2444 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2445 verf[1] = (__force __be32)nn->clverifier_counter++;
2446 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2449 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2451 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2452 clp->cl_clientid.cl_id = nn->clientid_counter++;
2453 gen_confirm(clp, nn);
2456 static struct nfs4_stid *
2457 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2459 struct nfs4_stid *ret;
2461 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2462 if (!ret || !ret->sc_type)
2467 static struct nfs4_stid *
2468 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2470 struct nfs4_stid *s;
2472 spin_lock(&cl->cl_lock);
2473 s = find_stateid_locked(cl, t);
2475 if (typemask & s->sc_type)
2476 refcount_inc(&s->sc_count);
2480 spin_unlock(&cl->cl_lock);
2484 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2486 struct nfsdfs_client *nc;
2487 nc = get_nfsdfs_client(inode);
2490 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2493 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2495 seq_printf(m, "\"");
2496 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2497 seq_printf(m, "\"");
2500 static const char *cb_state2str(int state)
2505 case NFSD4_CB_UNKNOWN:
2509 case NFSD4_CB_FAULT:
2515 static int client_info_show(struct seq_file *m, void *v)
2517 struct inode *inode = file_inode(m->file);
2518 struct nfs4_client *clp;
2521 clp = get_nfsdfs_clp(inode);
2524 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2525 seq_printf(m, "clientid: 0x%llx\n", clid);
2526 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2528 if (clp->cl_state == NFSD4_COURTESY)
2529 seq_puts(m, "status: courtesy\n");
2530 else if (clp->cl_state == NFSD4_EXPIRABLE)
2531 seq_puts(m, "status: expirable\n");
2532 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2533 seq_puts(m, "status: confirmed\n");
2535 seq_puts(m, "status: unconfirmed\n");
2536 seq_printf(m, "seconds from last renew: %lld\n",
2537 ktime_get_boottime_seconds() - clp->cl_time);
2538 seq_printf(m, "name: ");
2539 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2540 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2541 if (clp->cl_nii_domain.data) {
2542 seq_printf(m, "Implementation domain: ");
2543 seq_quote_mem(m, clp->cl_nii_domain.data,
2544 clp->cl_nii_domain.len);
2545 seq_printf(m, "\nImplementation name: ");
2546 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2547 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2548 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2550 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2551 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
2557 DEFINE_SHOW_ATTRIBUTE(client_info);
2559 static void *states_start(struct seq_file *s, loff_t *pos)
2560 __acquires(&clp->cl_lock)
2562 struct nfs4_client *clp = s->private;
2563 unsigned long id = *pos;
2566 spin_lock(&clp->cl_lock);
2567 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2572 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2574 struct nfs4_client *clp = s->private;
2575 unsigned long id = *pos;
2580 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2585 static void states_stop(struct seq_file *s, void *v)
2586 __releases(&clp->cl_lock)
2588 struct nfs4_client *clp = s->private;
2590 spin_unlock(&clp->cl_lock);
2593 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2595 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2598 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2600 struct inode *inode = file_inode(f->nf_file);
2602 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2603 MAJOR(inode->i_sb->s_dev),
2604 MINOR(inode->i_sb->s_dev),
2608 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2610 seq_printf(s, "owner: ");
2611 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2614 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2616 seq_printf(s, "0x%.8x", stid->si_generation);
2617 seq_printf(s, "%12phN", &stid->si_opaque);
2620 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2622 struct nfs4_ol_stateid *ols;
2623 struct nfs4_file *nf;
2624 struct nfsd_file *file;
2625 struct nfs4_stateowner *oo;
2626 unsigned int access, deny;
2628 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2629 return 0; /* XXX: or SEQ_SKIP? */
2630 ols = openlockstateid(st);
2631 oo = ols->st_stateowner;
2634 spin_lock(&nf->fi_lock);
2635 file = find_any_file_locked(nf);
2639 seq_printf(s, "- ");
2640 nfs4_show_stateid(s, &st->sc_stateid);
2641 seq_printf(s, ": { type: open, ");
2643 access = bmap_to_share_mode(ols->st_access_bmap);
2644 deny = bmap_to_share_mode(ols->st_deny_bmap);
2646 seq_printf(s, "access: %s%s, ",
2647 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2648 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2649 seq_printf(s, "deny: %s%s, ",
2650 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2651 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2653 nfs4_show_superblock(s, file);
2654 seq_printf(s, ", ");
2655 nfs4_show_fname(s, file);
2656 seq_printf(s, ", ");
2657 nfs4_show_owner(s, oo);
2658 seq_printf(s, " }\n");
2660 spin_unlock(&nf->fi_lock);
2664 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2666 struct nfs4_ol_stateid *ols;
2667 struct nfs4_file *nf;
2668 struct nfsd_file *file;
2669 struct nfs4_stateowner *oo;
2671 ols = openlockstateid(st);
2672 oo = ols->st_stateowner;
2674 spin_lock(&nf->fi_lock);
2675 file = find_any_file_locked(nf);
2679 seq_printf(s, "- ");
2680 nfs4_show_stateid(s, &st->sc_stateid);
2681 seq_printf(s, ": { type: lock, ");
2684 * Note: a lock stateid isn't really the same thing as a lock,
2685 * it's the locking state held by one owner on a file, and there
2686 * may be multiple (or no) lock ranges associated with it.
2687 * (Same for the matter is true of open stateids.)
2690 nfs4_show_superblock(s, file);
2691 /* XXX: open stateid? */
2692 seq_printf(s, ", ");
2693 nfs4_show_fname(s, file);
2694 seq_printf(s, ", ");
2695 nfs4_show_owner(s, oo);
2696 seq_printf(s, " }\n");
2698 spin_unlock(&nf->fi_lock);
2702 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2704 struct nfs4_delegation *ds;
2705 struct nfs4_file *nf;
2706 struct nfsd_file *file;
2708 ds = delegstateid(st);
2710 spin_lock(&nf->fi_lock);
2711 file = nf->fi_deleg_file;
2715 seq_printf(s, "- ");
2716 nfs4_show_stateid(s, &st->sc_stateid);
2717 seq_printf(s, ": { type: deleg, ");
2719 /* Kinda dead code as long as we only support read delegs: */
2720 seq_printf(s, "access: %s, ",
2721 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2723 /* XXX: lease time, whether it's being recalled. */
2725 nfs4_show_superblock(s, file);
2726 seq_printf(s, ", ");
2727 nfs4_show_fname(s, file);
2728 seq_printf(s, " }\n");
2730 spin_unlock(&nf->fi_lock);
2734 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2736 struct nfs4_layout_stateid *ls;
2737 struct nfsd_file *file;
2739 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2742 seq_printf(s, "- ");
2743 nfs4_show_stateid(s, &st->sc_stateid);
2744 seq_printf(s, ": { type: layout, ");
2746 /* XXX: What else would be useful? */
2748 nfs4_show_superblock(s, file);
2749 seq_printf(s, ", ");
2750 nfs4_show_fname(s, file);
2751 seq_printf(s, " }\n");
2756 static int states_show(struct seq_file *s, void *v)
2758 struct nfs4_stid *st = v;
2760 switch (st->sc_type) {
2761 case NFS4_OPEN_STID:
2762 return nfs4_show_open(s, st);
2763 case NFS4_LOCK_STID:
2764 return nfs4_show_lock(s, st);
2765 case NFS4_DELEG_STID:
2766 return nfs4_show_deleg(s, st);
2767 case NFS4_LAYOUT_STID:
2768 return nfs4_show_layout(s, st);
2770 return 0; /* XXX: or SEQ_SKIP? */
2772 /* XXX: copy stateids? */
2775 static struct seq_operations states_seq_ops = {
2776 .start = states_start,
2777 .next = states_next,
2778 .stop = states_stop,
2782 static int client_states_open(struct inode *inode, struct file *file)
2785 struct nfs4_client *clp;
2788 clp = get_nfsdfs_clp(inode);
2792 ret = seq_open(file, &states_seq_ops);
2795 s = file->private_data;
2800 static int client_opens_release(struct inode *inode, struct file *file)
2802 struct seq_file *m = file->private_data;
2803 struct nfs4_client *clp = m->private;
2805 /* XXX: alternatively, we could get/drop in seq start/stop */
2807 return seq_release(inode, file);
2810 static const struct file_operations client_states_fops = {
2811 .open = client_states_open,
2813 .llseek = seq_lseek,
2814 .release = client_opens_release,
2818 * Normally we refuse to destroy clients that are in use, but here the
2819 * administrator is telling us to just do it. We also want to wait
2820 * so the caller has a guarantee that the client's locks are gone by
2821 * the time the write returns:
2823 static void force_expire_client(struct nfs4_client *clp)
2825 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2826 bool already_expired;
2828 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2830 spin_lock(&nn->client_lock);
2832 spin_unlock(&nn->client_lock);
2834 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2835 spin_lock(&nn->client_lock);
2836 already_expired = list_empty(&clp->cl_lru);
2837 if (!already_expired)
2838 unhash_client_locked(clp);
2839 spin_unlock(&nn->client_lock);
2841 if (!already_expired)
2844 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2847 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2848 size_t size, loff_t *pos)
2851 struct nfs4_client *clp;
2853 data = simple_transaction_get(file, buf, size);
2855 return PTR_ERR(data);
2856 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2858 clp = get_nfsdfs_clp(file_inode(file));
2861 force_expire_client(clp);
2866 static const struct file_operations client_ctl_fops = {
2867 .write = client_ctl_write,
2868 .release = simple_transaction_release,
2871 static const struct tree_descr client_files[] = {
2872 [0] = {"info", &client_info_fops, S_IRUSR},
2873 [1] = {"states", &client_states_fops, S_IRUSR},
2874 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
2879 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb,
2880 struct rpc_task *task)
2882 trace_nfsd_cb_recall_any_done(cb, task);
2883 switch (task->tk_status) {
2884 case -NFS4ERR_DELAY:
2885 rpc_delay(task, 2 * HZ);
2893 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb)
2895 struct nfs4_client *clp = cb->cb_clp;
2896 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2898 spin_lock(&nn->client_lock);
2899 clear_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
2900 put_client_renew_locked(clp);
2901 spin_unlock(&nn->client_lock);
2905 nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task)
2907 struct nfs4_cb_fattr *ncf =
2908 container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
2910 ncf->ncf_cb_status = task->tk_status;
2911 switch (task->tk_status) {
2912 case -NFS4ERR_DELAY:
2913 rpc_delay(task, 2 * HZ);
2921 nfsd4_cb_getattr_release(struct nfsd4_callback *cb)
2923 struct nfs4_cb_fattr *ncf =
2924 container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
2925 struct nfs4_delegation *dp =
2926 container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
2928 nfs4_put_stid(&dp->dl_stid);
2929 clear_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags);
2930 wake_up_bit(&ncf->ncf_cb_flags, CB_GETATTR_BUSY);
2933 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
2934 .done = nfsd4_cb_recall_any_done,
2935 .release = nfsd4_cb_recall_any_release,
2938 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = {
2939 .done = nfsd4_cb_getattr_done,
2940 .release = nfsd4_cb_getattr_release,
2943 void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf)
2945 struct nfs4_delegation *dp =
2946 container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
2948 if (test_and_set_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags))
2950 refcount_inc(&dp->dl_stid.sc_count);
2951 nfsd4_run_cb(&ncf->ncf_getattr);
2954 static struct nfs4_client *create_client(struct xdr_netobj name,
2955 struct svc_rqst *rqstp, nfs4_verifier *verf)
2957 struct nfs4_client *clp;
2958 struct sockaddr *sa = svc_addr(rqstp);
2960 struct net *net = SVC_NET(rqstp);
2961 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2962 struct dentry *dentries[ARRAY_SIZE(client_files)];
2964 clp = alloc_client(name, nn);
2968 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2974 kref_init(&clp->cl_nfsdfs.cl_ref);
2975 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2976 clp->cl_time = ktime_get_boottime_seconds();
2977 clear_bit(0, &clp->cl_cb_slot_busy);
2978 copy_verf(clp, verf);
2979 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2980 clp->cl_cb_session = NULL;
2982 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2983 nn, &clp->cl_nfsdfs,
2984 clp->cl_clientid.cl_id - nn->clientid_base,
2985 client_files, dentries);
2986 clp->cl_nfsd_info_dentry = dentries[0];
2987 if (!clp->cl_nfsd_dentry) {
2991 clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL);
2996 clp->cl_ra_time = 0;
2997 nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops,
2998 NFSPROC4_CLNT_CB_RECALL_ANY);
3003 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
3005 struct rb_node **new = &(root->rb_node), *parent = NULL;
3006 struct nfs4_client *clp;
3009 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
3012 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
3013 new = &((*new)->rb_left);
3015 new = &((*new)->rb_right);
3018 rb_link_node(&new_clp->cl_namenode, parent, new);
3019 rb_insert_color(&new_clp->cl_namenode, root);
3022 static struct nfs4_client *
3023 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
3026 struct rb_node *node = root->rb_node;
3027 struct nfs4_client *clp;
3030 clp = rb_entry(node, struct nfs4_client, cl_namenode);
3031 cmp = compare_blob(&clp->cl_name, name);
3033 node = node->rb_left;
3035 node = node->rb_right;
3043 add_to_unconfirmed(struct nfs4_client *clp)
3045 unsigned int idhashval;
3046 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3048 lockdep_assert_held(&nn->client_lock);
3050 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3051 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
3052 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3053 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3054 renew_client_locked(clp);
3058 move_to_confirmed(struct nfs4_client *clp)
3060 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3061 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3063 lockdep_assert_held(&nn->client_lock);
3065 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
3066 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
3067 add_clp_to_name_tree(clp, &nn->conf_name_tree);
3068 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3069 trace_nfsd_clid_confirmed(&clp->cl_clientid);
3070 renew_client_locked(clp);
3073 static struct nfs4_client *
3074 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
3076 struct nfs4_client *clp;
3077 unsigned int idhashval = clientid_hashval(clid->cl_id);
3079 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
3080 if (same_clid(&clp->cl_clientid, clid)) {
3081 if ((bool)clp->cl_minorversion != sessions)
3083 renew_client_locked(clp);
3090 static struct nfs4_client *
3091 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3093 struct list_head *tbl = nn->conf_id_hashtbl;
3095 lockdep_assert_held(&nn->client_lock);
3096 return find_client_in_id_table(tbl, clid, sessions);
3099 static struct nfs4_client *
3100 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3102 struct list_head *tbl = nn->unconf_id_hashtbl;
3104 lockdep_assert_held(&nn->client_lock);
3105 return find_client_in_id_table(tbl, clid, sessions);
3108 static bool clp_used_exchangeid(struct nfs4_client *clp)
3110 return clp->cl_exchange_flags != 0;
3113 static struct nfs4_client *
3114 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3116 lockdep_assert_held(&nn->client_lock);
3117 return find_clp_in_name_tree(name, &nn->conf_name_tree);
3120 static struct nfs4_client *
3121 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3123 lockdep_assert_held(&nn->client_lock);
3124 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3128 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3130 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3131 struct sockaddr *sa = svc_addr(rqstp);
3132 u32 scopeid = rpc_get_scope_id(sa);
3133 unsigned short expected_family;
3135 /* Currently, we only support tcp and tcp6 for the callback channel */
3136 if (se->se_callback_netid_len == 3 &&
3137 !memcmp(se->se_callback_netid_val, "tcp", 3))
3138 expected_family = AF_INET;
3139 else if (se->se_callback_netid_len == 4 &&
3140 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3141 expected_family = AF_INET6;
3145 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3146 se->se_callback_addr_len,
3147 (struct sockaddr *)&conn->cb_addr,
3148 sizeof(conn->cb_addr));
3150 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3153 if (conn->cb_addr.ss_family == AF_INET6)
3154 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3156 conn->cb_prog = se->se_callback_prog;
3157 conn->cb_ident = se->se_callback_ident;
3158 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3159 trace_nfsd_cb_args(clp, conn);
3162 conn->cb_addr.ss_family = AF_UNSPEC;
3163 conn->cb_addrlen = 0;
3164 trace_nfsd_cb_nodelegs(clp);
3169 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3172 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3174 struct xdr_buf *buf = resp->xdr->buf;
3175 struct nfsd4_slot *slot = resp->cstate.slot;
3178 dprintk("--> %s slot %p\n", __func__, slot);
3180 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3181 slot->sl_opcnt = resp->opcnt;
3182 slot->sl_status = resp->cstate.status;
3183 free_svc_cred(&slot->sl_cred);
3184 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3186 if (!nfsd4_cache_this(resp)) {
3187 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3190 slot->sl_flags |= NFSD4_SLOT_CACHED;
3192 base = resp->cstate.data_offset;
3193 slot->sl_datalen = buf->len - base;
3194 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3195 WARN(1, "%s: sessions DRC could not cache compound\n",
3201 * Encode the replay sequence operation from the slot values.
3202 * If cachethis is FALSE encode the uncached rep error on the next
3203 * operation which sets resp->p and increments resp->opcnt for
3204 * nfs4svc_encode_compoundres.
3208 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3209 struct nfsd4_compoundres *resp)
3211 struct nfsd4_op *op;
3212 struct nfsd4_slot *slot = resp->cstate.slot;
3214 /* Encode the replayed sequence operation */
3215 op = &args->ops[resp->opcnt - 1];
3216 nfsd4_encode_operation(resp, op);
3218 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3220 if (args->opcnt == 1) {
3222 * The original operation wasn't a solo sequence--we
3223 * always cache those--so this retry must not match the
3226 op->status = nfserr_seq_false_retry;
3228 op = &args->ops[resp->opcnt++];
3229 op->status = nfserr_retry_uncached_rep;
3230 nfsd4_encode_operation(resp, op);
3236 * The sequence operation is not cached because we can use the slot and
3240 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3241 struct nfsd4_sequence *seq)
3243 struct nfsd4_slot *slot = resp->cstate.slot;
3244 struct xdr_stream *xdr = resp->xdr;
3248 dprintk("--> %s slot %p\n", __func__, slot);
3250 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3254 p = xdr_reserve_space(xdr, slot->sl_datalen);
3257 return nfserr_serverfault;
3259 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3260 xdr_commit_encode(xdr);
3262 resp->opcnt = slot->sl_opcnt;
3263 return slot->sl_status;
3267 * Set the exchange_id flags returned by the server.
3270 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3272 #ifdef CONFIG_NFSD_PNFS
3273 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3275 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3278 /* Referrals are supported, Migration is not. */
3279 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3281 /* set the wire flags to return to client. */
3282 clid->flags = new->cl_exchange_flags;
3285 static bool client_has_openowners(struct nfs4_client *clp)
3287 struct nfs4_openowner *oo;
3289 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3290 if (!list_empty(&oo->oo_owner.so_stateids))
3296 static bool client_has_state(struct nfs4_client *clp)
3298 return client_has_openowners(clp)
3299 #ifdef CONFIG_NFSD_PNFS
3300 || !list_empty(&clp->cl_lo_states)
3302 || !list_empty(&clp->cl_delegations)
3303 || !list_empty(&clp->cl_sessions)
3304 || !list_empty(&clp->async_copies);
3307 static __be32 copy_impl_id(struct nfs4_client *clp,
3308 struct nfsd4_exchange_id *exid)
3310 if (!exid->nii_domain.data)
3312 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3313 if (!clp->cl_nii_domain.data)
3314 return nfserr_jukebox;
3315 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3316 if (!clp->cl_nii_name.data)
3317 return nfserr_jukebox;
3318 clp->cl_nii_time = exid->nii_time;
3323 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3324 union nfsd4_op_u *u)
3326 struct nfsd4_exchange_id *exid = &u->exchange_id;
3327 struct nfs4_client *conf, *new;
3328 struct nfs4_client *unconf = NULL;
3330 char addr_str[INET6_ADDRSTRLEN];
3331 nfs4_verifier verf = exid->verifier;
3332 struct sockaddr *sa = svc_addr(rqstp);
3333 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3334 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3336 rpc_ntop(sa, addr_str, sizeof(addr_str));
3337 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3338 "ip_addr=%s flags %x, spa_how %u\n",
3339 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3340 addr_str, exid->flags, exid->spa_how);
3342 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3343 return nfserr_inval;
3345 new = create_client(exid->clname, rqstp, &verf);
3347 return nfserr_jukebox;
3348 status = copy_impl_id(new, exid);
3352 switch (exid->spa_how) {
3354 exid->spo_must_enforce[0] = 0;
3355 exid->spo_must_enforce[1] = (
3356 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3357 1 << (OP_EXCHANGE_ID - 32) |
3358 1 << (OP_CREATE_SESSION - 32) |
3359 1 << (OP_DESTROY_SESSION - 32) |
3360 1 << (OP_DESTROY_CLIENTID - 32));
3362 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3363 1 << (OP_OPEN_DOWNGRADE) |
3365 1 << (OP_DELEGRETURN));
3367 exid->spo_must_allow[1] &= (
3368 1 << (OP_TEST_STATEID - 32) |
3369 1 << (OP_FREE_STATEID - 32));
3370 if (!svc_rqst_integrity_protected(rqstp)) {
3371 status = nfserr_inval;
3375 * Sometimes userspace doesn't give us a principal.
3376 * Which is a bug, really. Anyway, we can't enforce
3377 * MACH_CRED in that case, better to give up now:
3379 if (!new->cl_cred.cr_principal &&
3380 !new->cl_cred.cr_raw_principal) {
3381 status = nfserr_serverfault;
3384 new->cl_mach_cred = true;
3388 default: /* checked by xdr code */
3392 status = nfserr_encr_alg_unsupp;
3396 /* Cases below refer to rfc 5661 section 18.35.4: */
3397 spin_lock(&nn->client_lock);
3398 conf = find_confirmed_client_by_name(&exid->clname, nn);
3400 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3401 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3404 if (!clp_used_exchangeid(conf)) { /* buggy client */
3405 status = nfserr_inval;
3408 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3409 status = nfserr_wrong_cred;
3412 if (!creds_match) { /* case 9 */
3413 status = nfserr_perm;
3416 if (!verfs_match) { /* case 8 */
3417 status = nfserr_not_same;
3421 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3422 trace_nfsd_clid_confirmed_r(conf);
3425 if (!creds_match) { /* case 3 */
3426 if (client_has_state(conf)) {
3427 status = nfserr_clid_inuse;
3428 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3433 if (verfs_match) { /* case 2 */
3434 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3435 trace_nfsd_clid_confirmed_r(conf);
3438 /* case 5, client reboot */
3439 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3444 if (update) { /* case 7 */
3445 status = nfserr_noent;
3449 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3450 if (unconf) /* case 4, possible retry or client restart */
3451 unhash_client_locked(unconf);
3453 /* case 1, new owner ID */
3454 trace_nfsd_clid_fresh(new);
3458 status = mark_client_expired_locked(conf);
3461 trace_nfsd_clid_replaced(&conf->cl_clientid);
3463 new->cl_minorversion = cstate->minorversion;
3464 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3465 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3467 add_to_unconfirmed(new);
3470 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3471 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3473 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3474 nfsd4_set_ex_flags(conf, exid);
3476 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3477 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3481 spin_unlock(&nn->client_lock);
3486 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3487 expire_client(unconf);
3493 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3495 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3498 /* The slot is in use, and no response has been sent. */
3500 if (seqid == slot_seqid)
3501 return nfserr_jukebox;
3503 return nfserr_seq_misordered;
3505 /* Note unsigned 32-bit arithmetic handles wraparound: */
3506 if (likely(seqid == slot_seqid + 1))
3508 if (seqid == slot_seqid)
3509 return nfserr_replay_cache;
3510 return nfserr_seq_misordered;
3514 * Cache the create session result into the create session single DRC
3515 * slot cache by saving the xdr structure. sl_seqid has been set.
3516 * Do this for solo or embedded create session operations.
3519 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3520 struct nfsd4_clid_slot *slot, __be32 nfserr)
3522 slot->sl_status = nfserr;
3523 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3527 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3528 struct nfsd4_clid_slot *slot)
3530 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3531 return slot->sl_status;
3534 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3535 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3536 1 + /* MIN tag is length with zero, only length */ \
3537 3 + /* version, opcount, opcode */ \
3538 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3539 /* seqid, slotID, slotID, cache */ \
3540 4 ) * sizeof(__be32))
3542 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3543 2 + /* verifier: AUTH_NULL, length 0 */\
3545 1 + /* MIN tag is length with zero, only length */ \
3546 3 + /* opcount, opcode, opstatus*/ \
3547 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3548 /* seqid, slotID, slotID, slotID, status */ \
3549 5 ) * sizeof(__be32))
3551 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3553 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3555 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3556 return nfserr_toosmall;
3557 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3558 return nfserr_toosmall;
3559 ca->headerpadsz = 0;
3560 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3561 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3562 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3563 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3564 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3565 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3567 * Note decreasing slot size below client's request may make it
3568 * difficult for client to function correctly, whereas
3569 * decreasing the number of slots will (just?) affect
3570 * performance. When short on memory we therefore prefer to
3571 * decrease number of slots instead of their size. Clients that
3572 * request larger slots than they need will get poor results:
3573 * Note that we always allow at least one slot, because our
3574 * accounting is soft and provides no guarantees either way.
3576 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3582 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3583 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3585 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3586 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3588 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3589 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3591 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3592 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3593 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3594 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3597 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3599 ca->headerpadsz = 0;
3601 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3602 return nfserr_toosmall;
3603 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3604 return nfserr_toosmall;
3605 ca->maxresp_cached = 0;
3607 return nfserr_toosmall;
3612 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3614 switch (cbs->flavor) {
3620 * GSS case: the spec doesn't allow us to return this
3621 * error. But it also doesn't allow us not to support
3623 * I'd rather this fail hard than return some error the
3624 * client might think it can already handle:
3626 return nfserr_encr_alg_unsupp;
3631 nfsd4_create_session(struct svc_rqst *rqstp,
3632 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3634 struct nfsd4_create_session *cr_ses = &u->create_session;
3635 struct sockaddr *sa = svc_addr(rqstp);
3636 struct nfs4_client *conf, *unconf;
3637 struct nfs4_client *old = NULL;
3638 struct nfsd4_session *new;
3639 struct nfsd4_conn *conn;
3640 struct nfsd4_clid_slot *cs_slot = NULL;
3642 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3644 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3645 return nfserr_inval;
3646 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3649 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3652 status = check_backchannel_attrs(&cr_ses->back_channel);
3654 goto out_release_drc_mem;
3655 status = nfserr_jukebox;
3656 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3658 goto out_release_drc_mem;
3659 conn = alloc_conn_from_crses(rqstp, cr_ses);
3661 goto out_free_session;
3663 spin_lock(&nn->client_lock);
3664 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3665 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3666 WARN_ON_ONCE(conf && unconf);
3669 status = nfserr_wrong_cred;
3670 if (!nfsd4_mach_creds_match(conf, rqstp))
3672 cs_slot = &conf->cl_cs_slot;
3673 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3675 if (status == nfserr_replay_cache)
3676 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3679 } else if (unconf) {
3680 status = nfserr_clid_inuse;
3681 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3682 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3683 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
3686 status = nfserr_wrong_cred;
3687 if (!nfsd4_mach_creds_match(unconf, rqstp))
3689 cs_slot = &unconf->cl_cs_slot;
3690 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3692 /* an unconfirmed replay returns misordered */
3693 status = nfserr_seq_misordered;
3696 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3698 status = mark_client_expired_locked(old);
3703 trace_nfsd_clid_replaced(&old->cl_clientid);
3705 move_to_confirmed(unconf);
3708 status = nfserr_stale_clientid;
3712 /* Persistent sessions are not supported */
3713 cr_ses->flags &= ~SESSION4_PERSIST;
3714 /* Upshifting from TCP to RDMA is not supported */
3715 cr_ses->flags &= ~SESSION4_RDMA;
3717 init_session(rqstp, new, conf, cr_ses);
3718 nfsd4_get_session_locked(new);
3720 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3721 NFS4_MAX_SESSIONID_LEN);
3722 cs_slot->sl_seqid++;
3723 cr_ses->seqid = cs_slot->sl_seqid;
3725 /* cache solo and embedded create sessions under the client_lock */
3726 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3727 spin_unlock(&nn->client_lock);
3729 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3730 /* init connection and backchannel */
3731 nfsd4_init_conn(rqstp, conn, new);
3732 nfsd4_put_session(new);
3737 spin_unlock(&nn->client_lock);
3742 __free_session(new);
3743 out_release_drc_mem:
3744 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3748 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3751 case NFS4_CDFC4_FORE:
3752 case NFS4_CDFC4_BACK:
3754 case NFS4_CDFC4_FORE_OR_BOTH:
3755 case NFS4_CDFC4_BACK_OR_BOTH:
3756 *dir = NFS4_CDFC4_BOTH;
3759 return nfserr_inval;
3762 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3763 struct nfsd4_compound_state *cstate,
3764 union nfsd4_op_u *u)
3766 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3767 struct nfsd4_session *session = cstate->session;
3768 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3771 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3774 spin_lock(&nn->client_lock);
3775 session->se_cb_prog = bc->bc_cb_program;
3776 session->se_cb_sec = bc->bc_cb_sec;
3777 spin_unlock(&nn->client_lock);
3779 nfsd4_probe_callback(session->se_client);
3784 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3786 struct nfsd4_conn *c;
3788 list_for_each_entry(c, &s->se_conns, cn_persession) {
3789 if (c->cn_xprt == xpt) {
3796 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3797 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3799 struct nfs4_client *clp = session->se_client;
3800 struct svc_xprt *xpt = rqst->rq_xprt;
3801 struct nfsd4_conn *c;
3804 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3805 spin_lock(&clp->cl_lock);
3806 c = __nfsd4_find_conn(xpt, session);
3808 status = nfserr_noent;
3809 else if (req == c->cn_flags)
3811 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3812 c->cn_flags != NFS4_CDFC4_BACK)
3814 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3815 c->cn_flags != NFS4_CDFC4_FORE)
3818 status = nfserr_inval;
3819 spin_unlock(&clp->cl_lock);
3820 if (status == nfs_ok && conn)
3825 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3826 struct nfsd4_compound_state *cstate,
3827 union nfsd4_op_u *u)
3829 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3831 struct nfsd4_conn *conn;
3832 struct nfsd4_session *session;
3833 struct net *net = SVC_NET(rqstp);
3834 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3836 if (!nfsd4_last_compound_op(rqstp))
3837 return nfserr_not_only_op;
3838 spin_lock(&nn->client_lock);
3839 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3840 spin_unlock(&nn->client_lock);
3842 goto out_no_session;
3843 status = nfserr_wrong_cred;
3844 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3846 status = nfsd4_match_existing_connection(rqstp, session,
3848 if (status == nfs_ok) {
3849 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3850 bcts->dir == NFS4_CDFC4_BACK)
3851 conn->cn_flags |= NFS4_CDFC4_BACK;
3852 nfsd4_probe_callback(session->se_client);
3855 if (status == nfserr_inval)
3857 status = nfsd4_map_bcts_dir(&bcts->dir);
3860 conn = alloc_conn(rqstp, bcts->dir);
3861 status = nfserr_jukebox;
3864 nfsd4_init_conn(rqstp, conn, session);
3867 nfsd4_put_session(session);
3872 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3874 if (!cstate->session)
3876 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3880 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3881 union nfsd4_op_u *u)
3883 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3884 struct nfsd4_session *ses;
3886 int ref_held_by_me = 0;
3887 struct net *net = SVC_NET(r);
3888 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3890 status = nfserr_not_only_op;
3891 if (nfsd4_compound_in_session(cstate, sessionid)) {
3892 if (!nfsd4_last_compound_op(r))
3896 dump_sessionid(__func__, sessionid);
3897 spin_lock(&nn->client_lock);
3898 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3900 goto out_client_lock;
3901 status = nfserr_wrong_cred;
3902 if (!nfsd4_mach_creds_match(ses->se_client, r))
3903 goto out_put_session;
3904 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3906 goto out_put_session;
3907 unhash_session(ses);
3908 spin_unlock(&nn->client_lock);
3910 nfsd4_probe_callback_sync(ses->se_client);
3912 spin_lock(&nn->client_lock);
3915 nfsd4_put_session_locked(ses);
3917 spin_unlock(&nn->client_lock);
3922 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3924 struct nfs4_client *clp = ses->se_client;
3925 struct nfsd4_conn *c;
3926 __be32 status = nfs_ok;
3929 spin_lock(&clp->cl_lock);
3930 c = __nfsd4_find_conn(new->cn_xprt, ses);
3933 status = nfserr_conn_not_bound_to_session;
3934 if (clp->cl_mach_cred)
3936 __nfsd4_hash_conn(new, ses);
3937 spin_unlock(&clp->cl_lock);
3938 ret = nfsd4_register_conn(new);
3940 /* oops; xprt is already down: */
3941 nfsd4_conn_lost(&new->cn_xpt_user);
3944 spin_unlock(&clp->cl_lock);
3949 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3951 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3953 return args->opcnt > session->se_fchannel.maxops;
3956 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3957 struct nfsd4_session *session)
3959 struct xdr_buf *xb = &rqstp->rq_arg;
3961 return xb->len > session->se_fchannel.maxreq_sz;
3964 static bool replay_matches_cache(struct svc_rqst *rqstp,
3965 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3967 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3969 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3970 (bool)seq->cachethis)
3973 * If there's an error then the reply can have fewer ops than
3976 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3979 * But if we cached a reply with *more* ops than the call you're
3980 * sending us now, then this new call is clearly not really a
3981 * replay of the old one:
3983 if (slot->sl_opcnt > argp->opcnt)
3985 /* This is the only check explicitly called by spec: */
3986 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3989 * There may be more comparisons we could actually do, but the
3990 * spec doesn't require us to catch every case where the calls
3991 * don't match (that would require caching the call as well as
3992 * the reply), so we don't bother.
3998 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3999 union nfsd4_op_u *u)
4001 struct nfsd4_sequence *seq = &u->sequence;
4002 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4003 struct xdr_stream *xdr = resp->xdr;
4004 struct nfsd4_session *session;
4005 struct nfs4_client *clp;
4006 struct nfsd4_slot *slot;
4007 struct nfsd4_conn *conn;
4010 struct net *net = SVC_NET(rqstp);
4011 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4013 if (resp->opcnt != 1)
4014 return nfserr_sequence_pos;
4017 * Will be either used or freed by nfsd4_sequence_check_conn
4020 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
4022 return nfserr_jukebox;
4024 spin_lock(&nn->client_lock);
4025 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
4027 goto out_no_session;
4028 clp = session->se_client;
4030 status = nfserr_too_many_ops;
4031 if (nfsd4_session_too_many_ops(rqstp, session))
4032 goto out_put_session;
4034 status = nfserr_req_too_big;
4035 if (nfsd4_request_too_big(rqstp, session))
4036 goto out_put_session;
4038 status = nfserr_badslot;
4039 if (seq->slotid >= session->se_fchannel.maxreqs)
4040 goto out_put_session;
4042 slot = session->se_slots[seq->slotid];
4043 dprintk("%s: slotid %d\n", __func__, seq->slotid);
4045 /* We do not negotiate the number of slots yet, so set the
4046 * maxslots to the session maxreqs which is used to encode
4047 * sr_highest_slotid and the sr_target_slot id to maxslots */
4048 seq->maxslots = session->se_fchannel.maxreqs;
4050 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
4051 slot->sl_flags & NFSD4_SLOT_INUSE);
4052 if (status == nfserr_replay_cache) {
4053 status = nfserr_seq_misordered;
4054 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
4055 goto out_put_session;
4056 status = nfserr_seq_false_retry;
4057 if (!replay_matches_cache(rqstp, seq, slot))
4058 goto out_put_session;
4059 cstate->slot = slot;
4060 cstate->session = session;
4062 /* Return the cached reply status and set cstate->status
4063 * for nfsd4_proc_compound processing */
4064 status = nfsd4_replay_cache_entry(resp, seq);
4065 cstate->status = nfserr_replay_cache;
4069 goto out_put_session;
4071 status = nfsd4_sequence_check_conn(conn, session);
4074 goto out_put_session;
4076 buflen = (seq->cachethis) ?
4077 session->se_fchannel.maxresp_cached :
4078 session->se_fchannel.maxresp_sz;
4079 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
4081 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
4082 goto out_put_session;
4083 svc_reserve(rqstp, buflen);
4086 /* Success! bump slot seqid */
4087 slot->sl_seqid = seq->seqid;
4088 slot->sl_flags |= NFSD4_SLOT_INUSE;
4090 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4092 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4094 cstate->slot = slot;
4095 cstate->session = session;
4099 switch (clp->cl_cb_state) {
4101 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
4103 case NFSD4_CB_FAULT:
4104 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4107 seq->status_flags = 0;
4109 if (!list_empty(&clp->cl_revoked))
4110 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4114 spin_unlock(&nn->client_lock);
4117 nfsd4_put_session_locked(session);
4118 goto out_no_session;
4122 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4124 struct nfsd4_compound_state *cs = &resp->cstate;
4126 if (nfsd4_has_session(cs)) {
4127 if (cs->status != nfserr_replay_cache) {
4128 nfsd4_store_cache_entry(resp);
4129 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4131 /* Drop session reference that was taken in nfsd4_sequence() */
4132 nfsd4_put_session(cs->session);
4134 put_client_renew(cs->clp);
4138 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4139 struct nfsd4_compound_state *cstate,
4140 union nfsd4_op_u *u)
4142 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4143 struct nfs4_client *conf, *unconf;
4144 struct nfs4_client *clp = NULL;
4146 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4148 spin_lock(&nn->client_lock);
4149 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4150 conf = find_confirmed_client(&dc->clientid, true, nn);
4151 WARN_ON_ONCE(conf && unconf);
4154 if (client_has_state(conf)) {
4155 status = nfserr_clientid_busy;
4158 status = mark_client_expired_locked(conf);
4165 status = nfserr_stale_clientid;
4168 if (!nfsd4_mach_creds_match(clp, rqstp)) {
4170 status = nfserr_wrong_cred;
4173 trace_nfsd_clid_destroyed(&clp->cl_clientid);
4174 unhash_client_locked(clp);
4176 spin_unlock(&nn->client_lock);
4183 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4184 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4186 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4187 struct nfs4_client *clp = cstate->clp;
4190 if (rc->rca_one_fs) {
4191 if (!cstate->current_fh.fh_dentry)
4192 return nfserr_nofilehandle;
4194 * We don't take advantage of the rca_one_fs case.
4195 * That's OK, it's optional, we can safely ignore it.
4200 status = nfserr_complete_already;
4201 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4204 status = nfserr_stale_clientid;
4205 if (is_client_expired(clp))
4207 * The following error isn't really legal.
4208 * But we only get here if the client just explicitly
4209 * destroyed the client. Surely it no longer cares what
4210 * error it gets back on an operation for the dead
4216 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4217 nfsd4_client_record_create(clp);
4218 inc_reclaim_complete(clp);
4224 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4225 union nfsd4_op_u *u)
4227 struct nfsd4_setclientid *setclid = &u->setclientid;
4228 struct xdr_netobj clname = setclid->se_name;
4229 nfs4_verifier clverifier = setclid->se_verf;
4230 struct nfs4_client *conf, *new;
4231 struct nfs4_client *unconf = NULL;
4233 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4235 new = create_client(clname, rqstp, &clverifier);
4237 return nfserr_jukebox;
4238 spin_lock(&nn->client_lock);
4239 conf = find_confirmed_client_by_name(&clname, nn);
4240 if (conf && client_has_state(conf)) {
4241 status = nfserr_clid_inuse;
4242 if (clp_used_exchangeid(conf))
4244 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4245 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4249 unconf = find_unconfirmed_client_by_name(&clname, nn);
4251 unhash_client_locked(unconf);
4253 if (same_verf(&conf->cl_verifier, &clverifier)) {
4254 copy_clid(new, conf);
4255 gen_confirm(new, nn);
4257 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4260 trace_nfsd_clid_fresh(new);
4261 new->cl_minorversion = 0;
4262 gen_callback(new, setclid, rqstp);
4263 add_to_unconfirmed(new);
4264 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4265 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4266 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4270 spin_unlock(&nn->client_lock);
4274 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4275 expire_client(unconf);
4281 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4282 struct nfsd4_compound_state *cstate,
4283 union nfsd4_op_u *u)
4285 struct nfsd4_setclientid_confirm *setclientid_confirm =
4286 &u->setclientid_confirm;
4287 struct nfs4_client *conf, *unconf;
4288 struct nfs4_client *old = NULL;
4289 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4290 clientid_t * clid = &setclientid_confirm->sc_clientid;
4292 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4294 if (STALE_CLIENTID(clid, nn))
4295 return nfserr_stale_clientid;
4297 spin_lock(&nn->client_lock);
4298 conf = find_confirmed_client(clid, false, nn);
4299 unconf = find_unconfirmed_client(clid, false, nn);
4301 * We try hard to give out unique clientid's, so if we get an
4302 * attempt to confirm the same clientid with a different cred,
4303 * the client may be buggy; this should never happen.
4305 * Nevertheless, RFC 7530 recommends INUSE for this case:
4307 status = nfserr_clid_inuse;
4308 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4309 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4312 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4313 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4316 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4317 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4320 status = nfserr_stale_clientid;
4326 unhash_client_locked(old);
4327 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4329 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4331 status = nfserr_clid_inuse;
4332 if (client_has_state(old)
4333 && !same_creds(&unconf->cl_cred,
4338 status = mark_client_expired_locked(old);
4343 trace_nfsd_clid_replaced(&old->cl_clientid);
4345 move_to_confirmed(unconf);
4348 get_client_locked(conf);
4349 spin_unlock(&nn->client_lock);
4351 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4352 nfsd4_probe_callback(conf);
4353 spin_lock(&nn->client_lock);
4354 put_client_renew_locked(conf);
4356 spin_unlock(&nn->client_lock);
4362 static struct nfs4_file *nfsd4_alloc_file(void)
4364 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4367 /* OPEN Share state helper functions */
4369 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
4371 refcount_set(&fp->fi_ref, 1);
4372 spin_lock_init(&fp->fi_lock);
4373 INIT_LIST_HEAD(&fp->fi_stateids);
4374 INIT_LIST_HEAD(&fp->fi_delegations);
4375 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4376 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4377 fp->fi_deleg_file = NULL;
4378 fp->fi_had_conflict = false;
4379 fp->fi_share_deny = 0;
4380 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4381 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4382 fp->fi_aliased = false;
4383 fp->fi_inode = d_inode(fh->fh_dentry);
4384 #ifdef CONFIG_NFSD_PNFS
4385 INIT_LIST_HEAD(&fp->fi_lo_states);
4386 atomic_set(&fp->fi_lo_recalls, 0);
4391 nfsd4_free_slabs(void)
4393 kmem_cache_destroy(client_slab);
4394 kmem_cache_destroy(openowner_slab);
4395 kmem_cache_destroy(lockowner_slab);
4396 kmem_cache_destroy(file_slab);
4397 kmem_cache_destroy(stateid_slab);
4398 kmem_cache_destroy(deleg_slab);
4399 kmem_cache_destroy(odstate_slab);
4403 nfsd4_init_slabs(void)
4405 client_slab = kmem_cache_create("nfsd4_clients",
4406 sizeof(struct nfs4_client), 0, 0, NULL);
4407 if (client_slab == NULL)
4409 openowner_slab = kmem_cache_create("nfsd4_openowners",
4410 sizeof(struct nfs4_openowner), 0, 0, NULL);
4411 if (openowner_slab == NULL)
4412 goto out_free_client_slab;
4413 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4414 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4415 if (lockowner_slab == NULL)
4416 goto out_free_openowner_slab;
4417 file_slab = kmem_cache_create("nfsd4_files",
4418 sizeof(struct nfs4_file), 0, 0, NULL);
4419 if (file_slab == NULL)
4420 goto out_free_lockowner_slab;
4421 stateid_slab = kmem_cache_create("nfsd4_stateids",
4422 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4423 if (stateid_slab == NULL)
4424 goto out_free_file_slab;
4425 deleg_slab = kmem_cache_create("nfsd4_delegations",
4426 sizeof(struct nfs4_delegation), 0, 0, NULL);
4427 if (deleg_slab == NULL)
4428 goto out_free_stateid_slab;
4429 odstate_slab = kmem_cache_create("nfsd4_odstate",
4430 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4431 if (odstate_slab == NULL)
4432 goto out_free_deleg_slab;
4435 out_free_deleg_slab:
4436 kmem_cache_destroy(deleg_slab);
4437 out_free_stateid_slab:
4438 kmem_cache_destroy(stateid_slab);
4440 kmem_cache_destroy(file_slab);
4441 out_free_lockowner_slab:
4442 kmem_cache_destroy(lockowner_slab);
4443 out_free_openowner_slab:
4444 kmem_cache_destroy(openowner_slab);
4445 out_free_client_slab:
4446 kmem_cache_destroy(client_slab);
4451 static unsigned long
4452 nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
4455 struct nfsd_net *nn = shrink->private_data;
4457 count = atomic_read(&nn->nfsd_courtesy_clients);
4459 count = atomic_long_read(&num_delegations);
4461 queue_work(laundry_wq, &nn->nfsd_shrinker_work);
4462 return (unsigned long)count;
4465 static unsigned long
4466 nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
4472 nfsd4_init_leases_net(struct nfsd_net *nn)
4477 nn->nfsd4_lease = 90; /* default lease time */
4478 nn->nfsd4_grace = 90;
4479 nn->somebody_reclaimed = false;
4480 nn->track_reclaim_completes = false;
4481 nn->clverifier_counter = get_random_u32();
4482 nn->clientid_base = get_random_u32();
4483 nn->clientid_counter = nn->clientid_base + 1;
4484 nn->s2s_cp_cl_id = nn->clientid_counter++;
4486 atomic_set(&nn->nfs4_client_count, 0);
4488 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4489 max_clients *= NFS4_CLIENTS_PER_GB;
4490 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4492 atomic_set(&nn->nfsd_courtesy_clients, 0);
4495 static void init_nfs4_replay(struct nfs4_replay *rp)
4497 rp->rp_status = nfserr_serverfault;
4499 rp->rp_buf = rp->rp_ibuf;
4500 mutex_init(&rp->rp_mutex);
4503 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4504 struct nfs4_stateowner *so)
4506 if (!nfsd4_has_session(cstate)) {
4507 mutex_lock(&so->so_replay.rp_mutex);
4508 cstate->replay_owner = nfs4_get_stateowner(so);
4512 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4514 struct nfs4_stateowner *so = cstate->replay_owner;
4517 cstate->replay_owner = NULL;
4518 mutex_unlock(&so->so_replay.rp_mutex);
4519 nfs4_put_stateowner(so);
4523 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4525 struct nfs4_stateowner *sop;
4527 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4531 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4532 if (!sop->so_owner.data) {
4533 kmem_cache_free(slab, sop);
4537 INIT_LIST_HEAD(&sop->so_stateids);
4538 sop->so_client = clp;
4539 init_nfs4_replay(&sop->so_replay);
4540 atomic_set(&sop->so_count, 1);
4544 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4546 lockdep_assert_held(&clp->cl_lock);
4548 list_add(&oo->oo_owner.so_strhash,
4549 &clp->cl_ownerstr_hashtbl[strhashval]);
4550 list_add(&oo->oo_perclient, &clp->cl_openowners);
4553 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4555 unhash_openowner_locked(openowner(so));
4558 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4560 struct nfs4_openowner *oo = openowner(so);
4562 kmem_cache_free(openowner_slab, oo);
4565 static const struct nfs4_stateowner_operations openowner_ops = {
4566 .so_unhash = nfs4_unhash_openowner,
4567 .so_free = nfs4_free_openowner,
4570 static struct nfs4_ol_stateid *
4571 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4573 struct nfs4_ol_stateid *local, *ret = NULL;
4574 struct nfs4_openowner *oo = open->op_openowner;
4576 lockdep_assert_held(&fp->fi_lock);
4578 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4579 /* ignore lock owners */
4580 if (local->st_stateowner->so_is_open_owner == 0)
4582 if (local->st_stateowner != &oo->oo_owner)
4584 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4586 refcount_inc(&ret->st_stid.sc_count);
4594 nfsd4_verify_open_stid(struct nfs4_stid *s)
4596 __be32 ret = nfs_ok;
4598 switch (s->sc_type) {
4602 case NFS4_CLOSED_STID:
4603 case NFS4_CLOSED_DELEG_STID:
4604 ret = nfserr_bad_stateid;
4606 case NFS4_REVOKED_DELEG_STID:
4607 ret = nfserr_deleg_revoked;
4612 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4614 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4618 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4619 ret = nfsd4_verify_open_stid(&stp->st_stid);
4621 mutex_unlock(&stp->st_mutex);
4625 static struct nfs4_ol_stateid *
4626 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4628 struct nfs4_ol_stateid *stp;
4630 spin_lock(&fp->fi_lock);
4631 stp = nfsd4_find_existing_open(fp, open);
4632 spin_unlock(&fp->fi_lock);
4633 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4635 nfs4_put_stid(&stp->st_stid);
4640 static struct nfs4_openowner *
4641 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4642 struct nfsd4_compound_state *cstate)
4644 struct nfs4_client *clp = cstate->clp;
4645 struct nfs4_openowner *oo, *ret;
4647 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4650 oo->oo_owner.so_ops = &openowner_ops;
4651 oo->oo_owner.so_is_open_owner = 1;
4652 oo->oo_owner.so_seqid = open->op_seqid;
4654 if (nfsd4_has_session(cstate))
4655 oo->oo_flags |= NFS4_OO_CONFIRMED;
4657 oo->oo_last_closed_stid = NULL;
4658 INIT_LIST_HEAD(&oo->oo_close_lru);
4659 spin_lock(&clp->cl_lock);
4660 ret = find_openstateowner_str_locked(strhashval, open, clp);
4662 hash_openowner(oo, clp, strhashval);
4665 nfs4_free_stateowner(&oo->oo_owner);
4667 spin_unlock(&clp->cl_lock);
4671 static struct nfs4_ol_stateid *
4672 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4675 struct nfs4_openowner *oo = open->op_openowner;
4676 struct nfs4_ol_stateid *retstp = NULL;
4677 struct nfs4_ol_stateid *stp;
4680 /* We are moving these outside of the spinlocks to avoid the warnings */
4681 mutex_init(&stp->st_mutex);
4682 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4685 spin_lock(&oo->oo_owner.so_client->cl_lock);
4686 spin_lock(&fp->fi_lock);
4688 retstp = nfsd4_find_existing_open(fp, open);
4692 open->op_stp = NULL;
4693 refcount_inc(&stp->st_stid.sc_count);
4694 stp->st_stid.sc_type = NFS4_OPEN_STID;
4695 INIT_LIST_HEAD(&stp->st_locks);
4696 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4698 stp->st_stid.sc_file = fp;
4699 stp->st_access_bmap = 0;
4700 stp->st_deny_bmap = 0;
4701 stp->st_openstp = NULL;
4702 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4703 list_add(&stp->st_perfile, &fp->fi_stateids);
4706 spin_unlock(&fp->fi_lock);
4707 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4709 /* Handle races with CLOSE */
4710 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4711 nfs4_put_stid(&retstp->st_stid);
4714 /* To keep mutex tracking happy */
4715 mutex_unlock(&stp->st_mutex);
4722 * In the 4.0 case we need to keep the owners around a little while to handle
4723 * CLOSE replay. We still do need to release any file access that is held by
4724 * them before returning however.
4727 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4729 struct nfs4_ol_stateid *last;
4730 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4731 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4734 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4737 * We know that we hold one reference via nfsd4_close, and another
4738 * "persistent" reference for the client. If the refcount is higher
4739 * than 2, then there are still calls in progress that are using this
4740 * stateid. We can't put the sc_file reference until they are finished.
4741 * Wait for the refcount to drop to 2. Since it has been unhashed,
4742 * there should be no danger of the refcount going back up again at
4745 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4747 release_all_access(s);
4748 if (s->st_stid.sc_file) {
4749 put_nfs4_file(s->st_stid.sc_file);
4750 s->st_stid.sc_file = NULL;
4753 spin_lock(&nn->client_lock);
4754 last = oo->oo_last_closed_stid;
4755 oo->oo_last_closed_stid = s;
4756 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4757 oo->oo_time = ktime_get_boottime_seconds();
4758 spin_unlock(&nn->client_lock);
4760 nfs4_put_stid(&last->st_stid);
4763 static noinline_for_stack struct nfs4_file *
4764 nfsd4_file_hash_lookup(const struct svc_fh *fhp)
4766 struct inode *inode = d_inode(fhp->fh_dentry);
4767 struct rhlist_head *tmp, *list;
4768 struct nfs4_file *fi;
4771 list = rhltable_lookup(&nfs4_file_rhltable, &inode,
4772 nfs4_file_rhash_params);
4773 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
4774 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
4775 if (refcount_inc_not_zero(&fi->fi_ref)) {
4786 * On hash insertion, identify entries with the same inode but
4787 * distinct filehandles. They will all be on the list returned
4788 * by rhltable_lookup().
4790 * inode->i_lock prevents racing insertions from adding an entry
4791 * for the same inode/fhp pair twice.
4793 static noinline_for_stack struct nfs4_file *
4794 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
4796 struct inode *inode = d_inode(fhp->fh_dentry);
4797 struct rhlist_head *tmp, *list;
4798 struct nfs4_file *ret = NULL;
4799 bool alias_found = false;
4800 struct nfs4_file *fi;
4804 spin_lock(&inode->i_lock);
4806 list = rhltable_lookup(&nfs4_file_rhltable, &inode,
4807 nfs4_file_rhash_params);
4808 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
4809 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
4810 if (refcount_inc_not_zero(&fi->fi_ref))
4813 fi->fi_aliased = alias_found = true;
4818 nfsd4_file_init(fhp, new);
4819 err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist,
4820 nfs4_file_rhash_params);
4824 new->fi_aliased = alias_found;
4828 spin_unlock(&inode->i_lock);
4833 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
4835 rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist,
4836 nfs4_file_rhash_params);
4840 * Called to check deny when READ with all zero stateid or
4841 * WRITE with all zero or all one stateid
4844 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4846 struct nfs4_file *fp;
4847 __be32 ret = nfs_ok;
4849 fp = nfsd4_file_hash_lookup(current_fh);
4853 /* Check for conflicting share reservations */
4854 spin_lock(&fp->fi_lock);
4855 if (fp->fi_share_deny & deny_type)
4856 ret = nfserr_locked;
4857 spin_unlock(&fp->fi_lock);
4862 static bool nfsd4_deleg_present(const struct inode *inode)
4864 struct file_lock_context *ctx = locks_inode_context(inode);
4866 return ctx && !list_empty_careful(&ctx->flc_lease);
4870 * nfsd_wait_for_delegreturn - wait for delegations to be returned
4871 * @rqstp: the RPC transaction being executed
4872 * @inode: in-core inode of the file being waited for
4874 * The timeout prevents deadlock if all nfsd threads happen to be
4875 * tied up waiting for returning delegations.
4878 * %true: delegation was returned
4879 * %false: timed out waiting for delegreturn
4881 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
4883 long __maybe_unused timeo;
4885 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
4886 NFSD_DELEGRETURN_TIMEOUT);
4887 trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
4891 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4893 struct nfs4_delegation *dp = cb_to_delegation(cb);
4894 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4897 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4900 * We can't do this in nfsd_break_deleg_cb because it is
4901 * already holding inode->i_lock.
4903 * If the dl_time != 0, then we know that it has already been
4904 * queued for a lease break. Don't queue it again.
4906 spin_lock(&state_lock);
4907 if (delegation_hashed(dp) && dp->dl_time == 0) {
4908 dp->dl_time = ktime_get_boottime_seconds();
4909 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4911 spin_unlock(&state_lock);
4914 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4915 struct rpc_task *task)
4917 struct nfs4_delegation *dp = cb_to_delegation(cb);
4919 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
4921 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4922 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4925 switch (task->tk_status) {
4928 case -NFS4ERR_DELAY:
4929 rpc_delay(task, 2 * HZ);
4932 case -NFS4ERR_BAD_STATEID:
4934 * Race: client probably got cb_recall before open reply
4935 * granting delegation.
4937 if (dp->dl_retries--) {
4938 rpc_delay(task, 2 * HZ);
4947 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4949 struct nfs4_delegation *dp = cb_to_delegation(cb);
4951 nfs4_put_stid(&dp->dl_stid);
4954 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4955 .prepare = nfsd4_cb_recall_prepare,
4956 .done = nfsd4_cb_recall_done,
4957 .release = nfsd4_cb_recall_release,
4960 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4963 * We're assuming the state code never drops its reference
4964 * without first removing the lease. Since we're in this lease
4965 * callback (and since the lease code is serialized by the
4966 * flc_lock) we know the server hasn't removed the lease yet, and
4967 * we know it's safe to take a reference.
4969 refcount_inc(&dp->dl_stid.sc_count);
4970 WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall));
4973 /* Called from break_lease() with flc_lock held. */
4975 nfsd_break_deleg_cb(struct file_lock *fl)
4977 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4978 struct nfs4_file *fp = dp->dl_stid.sc_file;
4979 struct nfs4_client *clp = dp->dl_stid.sc_client;
4980 struct nfsd_net *nn;
4982 trace_nfsd_cb_recall(&dp->dl_stid);
4984 dp->dl_recalled = true;
4985 atomic_inc(&clp->cl_delegs_in_recall);
4986 if (try_to_expire_client(clp)) {
4987 nn = net_generic(clp->net, nfsd_net_id);
4988 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
4992 * We don't want the locks code to timeout the lease for us;
4993 * we'll remove it ourself if a delegation isn't returned
4996 fl->fl_break_time = 0;
4998 spin_lock(&fp->fi_lock);
4999 fp->fi_had_conflict = true;
5000 nfsd_break_one_deleg(dp);
5001 spin_unlock(&fp->fi_lock);
5006 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
5007 * @fl: Lock state to check
5010 * %true: Lease conflict was resolved
5011 * %false: Lease conflict was not resolved.
5013 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
5015 struct nfs4_delegation *dl = fl->fl_owner;
5016 struct svc_rqst *rqst;
5017 struct nfs4_client *clp;
5021 rqst = kthread_data(current);
5022 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
5023 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
5025 clp = *(rqst->rq_lease_breaker);
5026 return dl->dl_stid.sc_client == clp;
5030 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
5031 struct list_head *dispose)
5033 struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
5034 struct nfs4_client *clp = dp->dl_stid.sc_client;
5036 if (arg & F_UNLCK) {
5037 if (dp->dl_recalled)
5038 atomic_dec(&clp->cl_delegs_in_recall);
5039 return lease_modify(onlist, arg, dispose);
5044 static const struct lock_manager_operations nfsd_lease_mng_ops = {
5045 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
5046 .lm_break = nfsd_break_deleg_cb,
5047 .lm_change = nfsd_change_deleg_cb,
5050 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
5052 if (nfsd4_has_session(cstate))
5054 if (seqid == so->so_seqid - 1)
5055 return nfserr_replay_me;
5056 if (seqid == so->so_seqid)
5058 return nfserr_bad_seqid;
5061 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
5062 struct nfsd_net *nn)
5064 struct nfs4_client *found;
5066 spin_lock(&nn->client_lock);
5067 found = find_confirmed_client(clid, sessions, nn);
5069 atomic_inc(&found->cl_rpc_users);
5070 spin_unlock(&nn->client_lock);
5074 static __be32 set_client(clientid_t *clid,
5075 struct nfsd4_compound_state *cstate,
5076 struct nfsd_net *nn)
5079 if (!same_clid(&cstate->clp->cl_clientid, clid))
5080 return nfserr_stale_clientid;
5083 if (STALE_CLIENTID(clid, nn))
5084 return nfserr_stale_clientid;
5086 * We're in the 4.0 case (otherwise the SEQUENCE op would have
5087 * set cstate->clp), so session = false:
5089 cstate->clp = lookup_clientid(clid, false, nn);
5091 return nfserr_expired;
5096 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
5097 struct nfsd4_open *open, struct nfsd_net *nn)
5099 clientid_t *clientid = &open->op_clientid;
5100 struct nfs4_client *clp = NULL;
5101 unsigned int strhashval;
5102 struct nfs4_openowner *oo = NULL;
5106 * In case we need it later, after we've already created the
5107 * file and don't want to risk a further failure:
5109 open->op_file = nfsd4_alloc_file();
5110 if (open->op_file == NULL)
5111 return nfserr_jukebox;
5113 status = set_client(clientid, cstate, nn);
5118 strhashval = ownerstr_hashval(&open->op_owner);
5119 oo = find_openstateowner_str(strhashval, open, clp);
5120 open->op_openowner = oo;
5124 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5125 /* Replace unconfirmed owners without checking for replay. */
5126 release_openowner(oo);
5127 open->op_openowner = NULL;
5130 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5135 oo = alloc_init_open_stateowner(strhashval, open, cstate);
5137 return nfserr_jukebox;
5138 open->op_openowner = oo;
5140 open->op_stp = nfs4_alloc_open_stateid(clp);
5142 return nfserr_jukebox;
5144 if (nfsd4_has_session(cstate) &&
5145 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5146 open->op_odstate = alloc_clnt_odstate(clp);
5147 if (!open->op_odstate)
5148 return nfserr_jukebox;
5154 static inline __be32
5155 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5157 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
5158 return nfserr_openmode;
5163 static int share_access_to_flags(u32 share_access)
5165 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5168 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
5170 struct nfs4_stid *ret;
5172 ret = find_stateid_by_type(cl, s,
5173 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
5176 return delegstateid(ret);
5179 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5181 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5182 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5186 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5187 struct nfs4_delegation **dp)
5190 __be32 status = nfserr_bad_stateid;
5191 struct nfs4_delegation *deleg;
5193 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5196 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
5197 nfs4_put_stid(&deleg->dl_stid);
5198 if (cl->cl_minorversion)
5199 status = nfserr_deleg_revoked;
5202 flags = share_access_to_flags(open->op_share_access);
5203 status = nfs4_check_delegmode(deleg, flags);
5205 nfs4_put_stid(&deleg->dl_stid);
5210 if (!nfsd4_is_deleg_cur(open))
5214 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5218 static inline int nfs4_access_to_access(u32 nfs4_access)
5222 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5223 flags |= NFSD_MAY_READ;
5224 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5225 flags |= NFSD_MAY_WRITE;
5229 static inline __be32
5230 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5231 struct nfsd4_open *open)
5233 struct iattr iattr = {
5234 .ia_valid = ATTR_SIZE,
5237 struct nfsd_attrs attrs = {
5240 if (!open->op_truncate)
5242 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5243 return nfserr_inval;
5244 return nfsd_setattr(rqstp, fh, &attrs, 0, (time64_t)0);
5247 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5248 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5249 struct nfsd4_open *open, bool new_stp)
5251 struct nfsd_file *nf = NULL;
5253 int oflag = nfs4_access_to_omode(open->op_share_access);
5254 int access = nfs4_access_to_access(open->op_share_access);
5255 unsigned char old_access_bmap, old_deny_bmap;
5257 spin_lock(&fp->fi_lock);
5260 * Are we trying to set a deny mode that would conflict with
5263 status = nfs4_file_check_deny(fp, open->op_share_deny);
5264 if (status != nfs_ok) {
5265 if (status != nfserr_share_denied) {
5266 spin_unlock(&fp->fi_lock);
5269 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5270 stp, open->op_share_deny, false))
5271 status = nfserr_jukebox;
5272 spin_unlock(&fp->fi_lock);
5276 /* set access to the file */
5277 status = nfs4_file_get_access(fp, open->op_share_access);
5278 if (status != nfs_ok) {
5279 if (status != nfserr_share_denied) {
5280 spin_unlock(&fp->fi_lock);
5283 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5284 stp, open->op_share_access, true))
5285 status = nfserr_jukebox;
5286 spin_unlock(&fp->fi_lock);
5290 /* Set access bits in stateid */
5291 old_access_bmap = stp->st_access_bmap;
5292 set_access(open->op_share_access, stp);
5294 /* Set new deny mask */
5295 old_deny_bmap = stp->st_deny_bmap;
5296 set_deny(open->op_share_deny, stp);
5297 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5299 if (!fp->fi_fds[oflag]) {
5300 spin_unlock(&fp->fi_lock);
5302 status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
5303 open->op_filp, &nf);
5304 if (status != nfs_ok)
5305 goto out_put_access;
5307 spin_lock(&fp->fi_lock);
5308 if (!fp->fi_fds[oflag]) {
5309 fp->fi_fds[oflag] = nf;
5313 spin_unlock(&fp->fi_lock);
5317 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5320 goto out_put_access;
5322 status = nfsd4_truncate(rqstp, cur_fh, open);
5324 goto out_put_access;
5328 stp->st_access_bmap = old_access_bmap;
5329 nfs4_file_put_access(fp, open->op_share_access);
5330 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5335 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5336 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5337 struct nfsd4_open *open)
5340 unsigned char old_deny_bmap = stp->st_deny_bmap;
5342 if (!test_access(open->op_share_access, stp))
5343 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5345 /* test and set deny mode */
5346 spin_lock(&fp->fi_lock);
5347 status = nfs4_file_check_deny(fp, open->op_share_deny);
5350 set_deny(open->op_share_deny, stp);
5351 fp->fi_share_deny |=
5352 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5354 case nfserr_share_denied:
5355 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5356 stp, open->op_share_deny, false))
5357 status = nfserr_jukebox;
5360 spin_unlock(&fp->fi_lock);
5362 if (status != nfs_ok)
5365 status = nfsd4_truncate(rqstp, cur_fh, open);
5366 if (status != nfs_ok)
5367 reset_union_bmap_deny(old_deny_bmap, stp);
5371 /* Should we give out recallable state?: */
5372 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5374 if (clp->cl_cb_state == NFSD4_CB_UP)
5377 * In the sessions case, since we don't have to establish a
5378 * separate connection for callbacks, we assume it's OK
5379 * until we hear otherwise:
5381 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5384 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5387 struct file_lock *fl;
5389 fl = locks_alloc_lock();
5392 fl->fl_lmops = &nfsd_lease_mng_ops;
5393 fl->fl_flags = FL_DELEG;
5394 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5395 fl->fl_end = OFFSET_MAX;
5396 fl->fl_owner = (fl_owner_t)dp;
5397 fl->fl_pid = current->tgid;
5398 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5402 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5403 struct nfs4_file *fp)
5405 struct nfs4_ol_stateid *st;
5406 struct file *f = fp->fi_deleg_file->nf_file;
5407 struct inode *ino = file_inode(f);
5410 writes = atomic_read(&ino->i_writecount);
5414 * There could be multiple filehandles (hence multiple
5415 * nfs4_files) referencing this file, but that's not too
5416 * common; let's just give up in that case rather than
5417 * trying to go look up all the clients using that other
5418 * nfs4_file as well:
5423 * If there's a close in progress, make sure that we see it
5424 * clear any fi_fds[] entries before we see it decrement
5427 smp_mb__after_atomic();
5429 if (fp->fi_fds[O_WRONLY])
5431 if (fp->fi_fds[O_RDWR])
5434 return -EAGAIN; /* There may be non-NFSv4 writers */
5436 * It's possible there are non-NFSv4 write opens in progress,
5437 * but if they haven't incremented i_writecount yet then they
5438 * also haven't called break lease yet; so, they'll break this
5439 * lease soon enough. So, all that's left to check for is NFSv4
5442 spin_lock(&fp->fi_lock);
5443 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5444 if (st->st_openstp == NULL /* it's an open */ &&
5445 access_permit_write(st) &&
5446 st->st_stid.sc_client != clp) {
5447 spin_unlock(&fp->fi_lock);
5451 spin_unlock(&fp->fi_lock);
5453 * There's a small chance that we could be racing with another
5454 * NFSv4 open. However, any open that hasn't added itself to
5455 * the fi_stateids list also hasn't called break_lease yet; so,
5456 * they'll break this lease soon enough.
5462 * It's possible that between opening the dentry and setting the delegation,
5463 * that it has been renamed or unlinked. Redo the lookup to verify that this
5467 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5468 struct svc_fh *parent)
5470 struct svc_export *exp;
5471 struct dentry *child;
5474 err = nfsd_lookup_dentry(open->op_rqstp, parent,
5475 open->op_fname, open->op_fnamelen,
5483 if (child != file_dentry(fp->fi_deleg_file->nf_file))
5490 * We avoid breaking delegations held by a client due to its own activity, but
5491 * clearing setuid/setgid bits on a write is an implicit activity and the client
5492 * may not notice and continue using the old mode. Avoid giving out a delegation
5493 * on setuid/setgid files when the client is requesting an open for write.
5496 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
5498 struct inode *inode = file_inode(nf->nf_file);
5500 if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
5501 (inode->i_mode & (S_ISUID|S_ISGID)))
5506 static struct nfs4_delegation *
5507 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5508 struct svc_fh *parent)
5511 struct nfs4_client *clp = stp->st_stid.sc_client;
5512 struct nfs4_file *fp = stp->st_stid.sc_file;
5513 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
5514 struct nfs4_delegation *dp;
5515 struct nfsd_file *nf = NULL;
5516 struct file_lock *fl;
5520 * The fi_had_conflict and nfs_get_existing_delegation checks
5521 * here are just optimizations; we'll need to recheck them at
5524 if (fp->fi_had_conflict)
5525 return ERR_PTR(-EAGAIN);
5528 * Try for a write delegation first. RFC8881 section 10.4 says:
5530 * "An OPEN_DELEGATE_WRITE delegation allows the client to handle,
5531 * on its own, all opens."
5533 * Furthermore the client can use a write delegation for most READ
5534 * operations as well, so we require a O_RDWR file here.
5536 * Offer a write delegation in the case of a BOTH open, and ensure
5537 * we get the O_RDWR descriptor.
5539 if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == NFS4_SHARE_ACCESS_BOTH) {
5540 nf = find_rw_file(fp);
5541 dl_type = NFS4_OPEN_DELEGATE_WRITE;
5545 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
5546 * file for some reason, then try for a read delegation instead.
5548 if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
5549 nf = find_readable_file(fp);
5550 dl_type = NFS4_OPEN_DELEGATE_READ;
5554 return ERR_PTR(-EAGAIN);
5556 spin_lock(&state_lock);
5557 spin_lock(&fp->fi_lock);
5558 if (nfs4_delegation_exists(clp, fp))
5560 else if (nfsd4_verify_setuid_write(open, nf))
5562 else if (!fp->fi_deleg_file) {
5563 fp->fi_deleg_file = nf;
5564 /* increment early to prevent fi_deleg_file from being
5566 fp->fi_delegees = 1;
5570 spin_unlock(&fp->fi_lock);
5571 spin_unlock(&state_lock);
5575 return ERR_PTR(status);
5578 dp = alloc_init_deleg(clp, fp, odstate, dl_type);
5582 fl = nfs4_alloc_init_lease(dp, dl_type);
5584 goto out_clnt_odstate;
5586 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5588 locks_free_lock(fl);
5590 goto out_clnt_odstate;
5593 status = nfsd4_verify_deleg_dentry(open, fp, parent);
5598 status = nfsd4_check_conflicting_opens(clp, fp);
5603 * Now that the deleg is set, check again to ensure that nothing
5604 * raced in and changed the mode while we weren't lookng.
5606 status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file);
5610 spin_lock(&state_lock);
5611 spin_lock(&fp->fi_lock);
5612 if (fp->fi_had_conflict)
5615 status = hash_delegation_locked(dp, fp);
5616 spin_unlock(&fp->fi_lock);
5617 spin_unlock(&state_lock);
5624 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5626 put_clnt_odstate(dp->dl_clnt_odstate);
5627 nfs4_put_stid(&dp->dl_stid);
5630 return ERR_PTR(status);
5633 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5635 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5636 if (status == -EAGAIN)
5637 open->op_why_no_deleg = WND4_CONTENTION;
5639 open->op_why_no_deleg = WND4_RESOURCE;
5640 switch (open->op_deleg_want) {
5641 case NFS4_SHARE_WANT_READ_DELEG:
5642 case NFS4_SHARE_WANT_WRITE_DELEG:
5643 case NFS4_SHARE_WANT_ANY_DELEG:
5645 case NFS4_SHARE_WANT_CANCEL:
5646 open->op_why_no_deleg = WND4_CANCELLED;
5648 case NFS4_SHARE_WANT_NO_DELEG:
5655 * The Linux NFS server does not offer write delegations to NFSv4.0
5656 * clients in order to avoid conflicts between write delegations and
5657 * GETATTRs requesting CHANGE or SIZE attributes.
5659 * With NFSv4.1 and later minorversions, the SEQUENCE operation that
5660 * begins each COMPOUND contains a client ID. Delegation recall can
5661 * be avoided when the server recognizes the client sending a
5662 * GETATTR also holds write delegation it conflicts with.
5664 * However, the NFSv4.0 protocol does not enable a server to
5665 * determine that a GETATTR originated from the client holding the
5666 * conflicting delegation versus coming from some other client. Per
5667 * RFC 7530 Section 16.7.5, the server must recall or send a
5668 * CB_GETATTR even when the GETATTR originates from the client that
5669 * holds the conflicting delegation.
5671 * An NFSv4.0 client can trigger a pathological situation if it
5672 * always sends a DELEGRETURN preceded by a conflicting GETATTR in
5673 * the same COMPOUND. COMPOUND execution will always stop at the
5674 * GETATTR and the DELEGRETURN will never get executed. The server
5675 * eventually revokes the delegation, which can result in loss of
5676 * open or lock state.
5679 nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5680 struct svc_fh *currentfh)
5682 struct nfs4_delegation *dp;
5683 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5684 struct nfs4_client *clp = stp->st_stid.sc_client;
5685 struct svc_fh *parent = NULL;
5691 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5692 open->op_recall = false;
5693 switch (open->op_claim_type) {
5694 case NFS4_OPEN_CLAIM_PREVIOUS:
5696 open->op_recall = true;
5698 case NFS4_OPEN_CLAIM_NULL:
5701 case NFS4_OPEN_CLAIM_FH:
5703 * Let's not give out any delegations till everyone's
5704 * had the chance to reclaim theirs, *and* until
5705 * NLM locks have all been reclaimed:
5707 if (locks_in_grace(clp->net))
5709 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5711 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
5712 !clp->cl_minorversion)
5718 dp = nfs4_set_delegation(open, stp, parent);
5722 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5724 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
5725 open->op_delegate_type = NFS4_OPEN_DELEGATE_WRITE;
5726 trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
5727 path.mnt = currentfh->fh_export->ex_path.mnt;
5728 path.dentry = currentfh->fh_dentry;
5729 if (vfs_getattr(&path, &stat,
5730 (STATX_SIZE | STATX_CTIME | STATX_CHANGE_COOKIE),
5731 AT_STATX_SYNC_AS_STAT)) {
5732 nfs4_put_stid(&dp->dl_stid);
5733 destroy_delegation(dp);
5736 dp->dl_cb_fattr.ncf_cur_fsize = stat.size;
5737 dp->dl_cb_fattr.ncf_initial_cinfo =
5738 nfsd4_change_attribute(&stat, d_inode(currentfh->fh_dentry));
5740 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5741 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5743 nfs4_put_stid(&dp->dl_stid);
5746 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5747 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5748 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5749 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5750 open->op_recall = true;
5753 /* 4.1 client asking for a delegation? */
5754 if (open->op_deleg_want)
5755 nfsd4_open_deleg_none_ext(open, status);
5759 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5760 struct nfs4_delegation *dp)
5762 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5763 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5764 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5765 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5766 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5767 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5768 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5769 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5771 /* Otherwise the client must be confused wanting a delegation
5772 * it already has, therefore we don't return
5773 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5778 * nfsd4_process_open2 - finish open processing
5779 * @rqstp: the RPC transaction being executed
5780 * @current_fh: NFSv4 COMPOUND's current filehandle
5781 * @open: OPEN arguments
5783 * If successful, (1) truncate the file if open->op_truncate was
5784 * set, (2) set open->op_stateid, (3) set open->op_delegation.
5786 * Returns %nfs_ok on success; otherwise an nfs4stat value in
5787 * network byte order is returned.
5790 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5792 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5793 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5794 struct nfs4_file *fp = NULL;
5795 struct nfs4_ol_stateid *stp = NULL;
5796 struct nfs4_delegation *dp = NULL;
5798 bool new_stp = false;
5801 * Lookup file; if found, lookup stateid and check open request,
5802 * and check for delegations in the process of being recalled.
5803 * If not found, create the nfs4_file struct
5805 fp = nfsd4_file_hash_insert(open->op_file, current_fh);
5807 return nfserr_jukebox;
5808 if (fp != open->op_file) {
5809 status = nfs4_check_deleg(cl, open, &dp);
5812 stp = nfsd4_find_and_lock_existing_open(fp, open);
5814 open->op_file = NULL;
5815 status = nfserr_bad_stateid;
5816 if (nfsd4_is_deleg_cur(open))
5821 stp = init_open_stateid(fp, open);
5827 * OPEN the file, or upgrade an existing OPEN.
5828 * If truncate fails, the OPEN fails.
5830 * stp is already locked.
5833 /* Stateid was found, this is an OPEN upgrade */
5834 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5836 mutex_unlock(&stp->st_mutex);
5840 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
5842 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5843 release_open_stateid(stp);
5844 mutex_unlock(&stp->st_mutex);
5848 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5850 if (stp->st_clnt_odstate == open->op_odstate)
5851 open->op_odstate = NULL;
5854 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5855 mutex_unlock(&stp->st_mutex);
5857 if (nfsd4_has_session(&resp->cstate)) {
5858 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5859 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5860 open->op_why_no_deleg = WND4_NOT_WANTED;
5866 * Attempt to hand out a delegation. No error return, because the
5867 * OPEN succeeds even if we fail.
5869 nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
5872 trace_nfsd_open(&stp->st_stid.sc_stateid);
5874 /* 4.1 client trying to upgrade/downgrade delegation? */
5875 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5876 open->op_deleg_want)
5877 nfsd4_deleg_xgrade_none_ext(open, dp);
5881 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5882 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5884 * To finish the open response, we just need to set the rflags.
5886 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5887 if (nfsd4_has_session(&resp->cstate))
5888 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5889 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5890 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5893 nfs4_put_stid(&dp->dl_stid);
5895 nfs4_put_stid(&stp->st_stid);
5900 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5901 struct nfsd4_open *open)
5903 if (open->op_openowner) {
5904 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5906 nfsd4_cstate_assign_replay(cstate, so);
5907 nfs4_put_stateowner(so);
5910 kmem_cache_free(file_slab, open->op_file);
5912 nfs4_put_stid(&open->op_stp->st_stid);
5913 if (open->op_odstate)
5914 kmem_cache_free(odstate_slab, open->op_odstate);
5918 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5919 union nfsd4_op_u *u)
5921 clientid_t *clid = &u->renew;
5922 struct nfs4_client *clp;
5924 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5926 trace_nfsd_clid_renew(clid);
5927 status = set_client(clid, cstate, nn);
5931 if (!list_empty(&clp->cl_delegations)
5932 && clp->cl_cb_state != NFSD4_CB_UP)
5933 return nfserr_cb_path_down;
5938 nfsd4_end_grace(struct nfsd_net *nn)
5940 /* do nothing if grace period already ended */
5941 if (nn->grace_ended)
5944 trace_nfsd_grace_complete(nn);
5945 nn->grace_ended = true;
5947 * If the server goes down again right now, an NFSv4
5948 * client will still be allowed to reclaim after it comes back up,
5949 * even if it hasn't yet had a chance to reclaim state this time.
5952 nfsd4_record_grace_done(nn);
5954 * At this point, NFSv4 clients can still reclaim. But if the
5955 * server crashes, any that have not yet reclaimed will be out
5956 * of luck on the next boot.
5958 * (NFSv4.1+ clients are considered to have reclaimed once they
5959 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5960 * have reclaimed after their first OPEN.)
5962 locks_end_grace(&nn->nfsd4_manager);
5964 * At this point, and once lockd and/or any other containers
5965 * exit their grace period, further reclaims will fail and
5966 * regular locking can resume.
5971 * If we've waited a lease period but there are still clients trying to
5972 * reclaim, wait a little longer to give them a chance to finish.
5974 static bool clients_still_reclaiming(struct nfsd_net *nn)
5976 time64_t double_grace_period_end = nn->boot_time +
5977 2 * nn->nfsd4_lease;
5979 if (nn->track_reclaim_completes &&
5980 atomic_read(&nn->nr_reclaim_complete) ==
5981 nn->reclaim_str_hashtbl_size)
5983 if (!nn->somebody_reclaimed)
5985 nn->somebody_reclaimed = false;
5987 * If we've given them *two* lease times to reclaim, and they're
5988 * still not done, give up:
5990 if (ktime_get_boottime_seconds() > double_grace_period_end)
5995 struct laundry_time {
6000 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
6002 time64_t time_remaining;
6004 if (last_refresh < lt->cutoff)
6006 time_remaining = last_refresh - lt->cutoff;
6007 lt->new_timeo = min(lt->new_timeo, time_remaining);
6011 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6012 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
6014 spin_lock_init(&nn->nfsd_ssc_lock);
6015 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
6016 init_waitqueue_head(&nn->nfsd_ssc_waitq);
6018 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
6021 * This is called when nfsd is being shutdown, after all inter_ssc
6022 * cleanup were done, to destroy the ssc delayed unmount list.
6024 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
6026 struct nfsd4_ssc_umount_item *ni = NULL;
6027 struct nfsd4_ssc_umount_item *tmp;
6029 spin_lock(&nn->nfsd_ssc_lock);
6030 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6031 list_del(&ni->nsui_list);
6032 spin_unlock(&nn->nfsd_ssc_lock);
6033 mntput(ni->nsui_vfsmount);
6035 spin_lock(&nn->nfsd_ssc_lock);
6037 spin_unlock(&nn->nfsd_ssc_lock);
6040 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
6042 bool do_wakeup = false;
6043 struct nfsd4_ssc_umount_item *ni = NULL;
6044 struct nfsd4_ssc_umount_item *tmp;
6046 spin_lock(&nn->nfsd_ssc_lock);
6047 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6048 if (time_after(jiffies, ni->nsui_expire)) {
6049 if (refcount_read(&ni->nsui_refcnt) > 1)
6052 /* mark being unmount */
6053 ni->nsui_busy = true;
6054 spin_unlock(&nn->nfsd_ssc_lock);
6055 mntput(ni->nsui_vfsmount);
6056 spin_lock(&nn->nfsd_ssc_lock);
6058 /* waiters need to start from begin of list */
6059 list_del(&ni->nsui_list);
6062 /* wakeup ssc_connect waiters */
6069 wake_up_all(&nn->nfsd_ssc_waitq);
6070 spin_unlock(&nn->nfsd_ssc_lock);
6074 /* Check if any lock belonging to this lockowner has any blockers */
6076 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
6078 struct file_lock_context *ctx;
6079 struct nfs4_ol_stateid *stp;
6080 struct nfs4_file *nf;
6082 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
6083 nf = stp->st_stid.sc_file;
6084 ctx = locks_inode_context(nf->fi_inode);
6087 if (locks_owner_has_blockers(ctx, lo))
6094 nfs4_anylock_blockers(struct nfs4_client *clp)
6097 struct nfs4_stateowner *so;
6098 struct nfs4_lockowner *lo;
6100 if (atomic_read(&clp->cl_delegs_in_recall))
6102 spin_lock(&clp->cl_lock);
6103 for (i = 0; i < OWNER_HASH_SIZE; i++) {
6104 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
6106 if (so->so_is_open_owner)
6109 if (nfs4_lockowner_has_blockers(lo)) {
6110 spin_unlock(&clp->cl_lock);
6115 spin_unlock(&clp->cl_lock);
6120 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
6121 struct laundry_time *lt)
6123 unsigned int maxreap, reapcnt = 0;
6124 struct list_head *pos, *next;
6125 struct nfs4_client *clp;
6127 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
6128 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
6129 INIT_LIST_HEAD(reaplist);
6130 spin_lock(&nn->client_lock);
6131 list_for_each_safe(pos, next, &nn->client_lru) {
6132 clp = list_entry(pos, struct nfs4_client, cl_lru);
6133 if (clp->cl_state == NFSD4_EXPIRABLE)
6135 if (!state_expired(lt, clp->cl_time))
6137 if (!atomic_read(&clp->cl_rpc_users)) {
6138 if (clp->cl_state == NFSD4_ACTIVE)
6139 atomic_inc(&nn->nfsd_courtesy_clients);
6140 clp->cl_state = NFSD4_COURTESY;
6142 if (!client_has_state(clp))
6144 if (!nfs4_anylock_blockers(clp))
6145 if (reapcnt >= maxreap)
6148 if (!mark_client_expired_locked(clp)) {
6149 list_add(&clp->cl_lru, reaplist);
6153 spin_unlock(&nn->client_lock);
6157 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
6158 struct list_head *reaplist)
6160 unsigned int maxreap = 0, reapcnt = 0;
6161 struct list_head *pos, *next;
6162 struct nfs4_client *clp;
6164 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
6165 INIT_LIST_HEAD(reaplist);
6167 spin_lock(&nn->client_lock);
6168 list_for_each_safe(pos, next, &nn->client_lru) {
6169 clp = list_entry(pos, struct nfs4_client, cl_lru);
6170 if (clp->cl_state == NFSD4_ACTIVE)
6172 if (reapcnt >= maxreap)
6174 if (!mark_client_expired_locked(clp)) {
6175 list_add(&clp->cl_lru, reaplist);
6179 spin_unlock(&nn->client_lock);
6183 nfs4_process_client_reaplist(struct list_head *reaplist)
6185 struct list_head *pos, *next;
6186 struct nfs4_client *clp;
6188 list_for_each_safe(pos, next, reaplist) {
6189 clp = list_entry(pos, struct nfs4_client, cl_lru);
6190 trace_nfsd_clid_purged(&clp->cl_clientid);
6191 list_del_init(&clp->cl_lru);
6197 nfs4_laundromat(struct nfsd_net *nn)
6199 struct nfs4_openowner *oo;
6200 struct nfs4_delegation *dp;
6201 struct nfs4_ol_stateid *stp;
6202 struct nfsd4_blocked_lock *nbl;
6203 struct list_head *pos, *next, reaplist;
6204 struct laundry_time lt = {
6205 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6206 .new_timeo = nn->nfsd4_lease
6208 struct nfs4_cpntf_state *cps;
6209 copy_stateid_t *cps_t;
6212 if (clients_still_reclaiming(nn)) {
6216 nfsd4_end_grace(nn);
6218 spin_lock(&nn->s2s_cp_lock);
6219 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6220 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6221 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
6222 state_expired(<, cps->cpntf_time))
6223 _free_cpntf_state_locked(nn, cps);
6225 spin_unlock(&nn->s2s_cp_lock);
6226 nfs4_get_client_reaplist(nn, &reaplist, <);
6227 nfs4_process_client_reaplist(&reaplist);
6229 spin_lock(&state_lock);
6230 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6231 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6232 if (!state_expired(<, dp->dl_time))
6234 WARN_ON(!unhash_delegation_locked(dp));
6235 list_add(&dp->dl_recall_lru, &reaplist);
6237 spin_unlock(&state_lock);
6238 while (!list_empty(&reaplist)) {
6239 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6241 list_del_init(&dp->dl_recall_lru);
6242 revoke_delegation(dp);
6245 spin_lock(&nn->client_lock);
6246 while (!list_empty(&nn->close_lru)) {
6247 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6249 if (!state_expired(<, oo->oo_time))
6251 list_del_init(&oo->oo_close_lru);
6252 stp = oo->oo_last_closed_stid;
6253 oo->oo_last_closed_stid = NULL;
6254 spin_unlock(&nn->client_lock);
6255 nfs4_put_stid(&stp->st_stid);
6256 spin_lock(&nn->client_lock);
6258 spin_unlock(&nn->client_lock);
6261 * It's possible for a client to try and acquire an already held lock
6262 * that is being held for a long time, and then lose interest in it.
6263 * So, we clean out any un-revisited request after a lease period
6264 * under the assumption that the client is no longer interested.
6266 * RFC5661, sec. 9.6 states that the client must not rely on getting
6267 * notifications and must continue to poll for locks, even when the
6268 * server supports them. Thus this shouldn't lead to clients blocking
6269 * indefinitely once the lock does become free.
6271 BUG_ON(!list_empty(&reaplist));
6272 spin_lock(&nn->blocked_locks_lock);
6273 while (!list_empty(&nn->blocked_locks_lru)) {
6274 nbl = list_first_entry(&nn->blocked_locks_lru,
6275 struct nfsd4_blocked_lock, nbl_lru);
6276 if (!state_expired(<, nbl->nbl_time))
6278 list_move(&nbl->nbl_lru, &reaplist);
6279 list_del_init(&nbl->nbl_list);
6281 spin_unlock(&nn->blocked_locks_lock);
6283 while (!list_empty(&reaplist)) {
6284 nbl = list_first_entry(&reaplist,
6285 struct nfsd4_blocked_lock, nbl_lru);
6286 list_del_init(&nbl->nbl_lru);
6287 free_blocked_lock(nbl);
6289 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6290 /* service the server-to-server copy delayed unmount list */
6291 nfsd4_ssc_expire_umount(nn);
6294 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
6297 static void laundromat_main(struct work_struct *);
6300 laundromat_main(struct work_struct *laundry)
6303 struct delayed_work *dwork = to_delayed_work(laundry);
6304 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6307 t = nfs4_laundromat(nn);
6308 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
6312 courtesy_client_reaper(struct nfsd_net *nn)
6314 struct list_head reaplist;
6316 nfs4_get_courtesy_client_reaplist(nn, &reaplist);
6317 nfs4_process_client_reaplist(&reaplist);
6321 deleg_reaper(struct nfsd_net *nn)
6323 struct list_head *pos, *next;
6324 struct nfs4_client *clp;
6325 struct list_head cblist;
6327 INIT_LIST_HEAD(&cblist);
6328 spin_lock(&nn->client_lock);
6329 list_for_each_safe(pos, next, &nn->client_lru) {
6330 clp = list_entry(pos, struct nfs4_client, cl_lru);
6331 if (clp->cl_state != NFSD4_ACTIVE ||
6332 list_empty(&clp->cl_delegations) ||
6333 atomic_read(&clp->cl_delegs_in_recall) ||
6334 test_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags) ||
6335 (ktime_get_boottime_seconds() -
6336 clp->cl_ra_time < 5)) {
6339 list_add(&clp->cl_ra_cblist, &cblist);
6341 /* release in nfsd4_cb_recall_any_release */
6342 atomic_inc(&clp->cl_rpc_users);
6343 set_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
6344 clp->cl_ra_time = ktime_get_boottime_seconds();
6346 spin_unlock(&nn->client_lock);
6348 while (!list_empty(&cblist)) {
6349 clp = list_first_entry(&cblist, struct nfs4_client,
6351 list_del_init(&clp->cl_ra_cblist);
6352 clp->cl_ra->ra_keep = 0;
6353 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG);
6354 trace_nfsd_cb_recall_any(clp->cl_ra);
6355 nfsd4_run_cb(&clp->cl_ra->ra_cb);
6360 nfsd4_state_shrinker_worker(struct work_struct *work)
6362 struct nfsd_net *nn = container_of(work, struct nfsd_net,
6363 nfsd_shrinker_work);
6365 courtesy_client_reaper(nn);
6369 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
6371 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
6372 return nfserr_bad_stateid;
6377 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
6379 __be32 status = nfserr_openmode;
6381 /* For lock stateid's, we test the parent open, not the lock: */
6382 if (stp->st_openstp)
6383 stp = stp->st_openstp;
6384 if ((flags & WR_STATE) && !access_permit_write(stp))
6386 if ((flags & RD_STATE) && !access_permit_read(stp))
6393 static inline __be32
6394 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
6396 if (ONE_STATEID(stateid) && (flags & RD_STATE))
6398 else if (opens_in_grace(net)) {
6399 /* Answer in remaining cases depends on existence of
6400 * conflicting state; so we must wait out the grace period. */
6401 return nfserr_grace;
6402 } else if (flags & WR_STATE)
6403 return nfs4_share_conflict(current_fh,
6404 NFS4_SHARE_DENY_WRITE);
6405 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
6406 return nfs4_share_conflict(current_fh,
6407 NFS4_SHARE_DENY_READ);
6410 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
6413 * When sessions are used the stateid generation number is ignored
6416 if (has_session && in->si_generation == 0)
6419 if (in->si_generation == ref->si_generation)
6422 /* If the client sends us a stateid from the future, it's buggy: */
6423 if (nfsd4_stateid_generation_after(in, ref))
6424 return nfserr_bad_stateid;
6426 * However, we could see a stateid from the past, even from a
6427 * non-buggy client. For example, if the client sends a lock
6428 * while some IO is outstanding, the lock may bump si_generation
6429 * while the IO is still in flight. The client could avoid that
6430 * situation by waiting for responses on all the IO requests,
6431 * but better performance may result in retrying IO that
6432 * receives an old_stateid error if requests are rarely
6433 * reordered in flight:
6435 return nfserr_old_stateid;
6438 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
6442 spin_lock(&s->sc_lock);
6443 ret = nfsd4_verify_open_stid(s);
6445 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
6446 spin_unlock(&s->sc_lock);
6450 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6452 if (ols->st_stateowner->so_is_open_owner &&
6453 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6454 return nfserr_bad_stateid;
6458 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
6460 struct nfs4_stid *s;
6461 __be32 status = nfserr_bad_stateid;
6463 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6464 CLOSE_STATEID(stateid))
6466 spin_lock(&cl->cl_lock);
6467 s = find_stateid_locked(cl, stateid);
6470 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
6473 switch (s->sc_type) {
6474 case NFS4_DELEG_STID:
6477 case NFS4_REVOKED_DELEG_STID:
6478 status = nfserr_deleg_revoked;
6480 case NFS4_OPEN_STID:
6481 case NFS4_LOCK_STID:
6482 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
6485 printk("unknown stateid type %x\n", s->sc_type);
6487 case NFS4_CLOSED_STID:
6488 case NFS4_CLOSED_DELEG_STID:
6489 status = nfserr_bad_stateid;
6492 spin_unlock(&cl->cl_lock);
6497 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6498 stateid_t *stateid, unsigned char typemask,
6499 struct nfs4_stid **s, struct nfsd_net *nn)
6502 struct nfs4_stid *stid;
6503 bool return_revoked = false;
6506 * only return revoked delegations if explicitly asked.
6507 * otherwise we report revoked or bad_stateid status.
6509 if (typemask & NFS4_REVOKED_DELEG_STID)
6510 return_revoked = true;
6511 else if (typemask & NFS4_DELEG_STID)
6512 typemask |= NFS4_REVOKED_DELEG_STID;
6514 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6515 CLOSE_STATEID(stateid))
6516 return nfserr_bad_stateid;
6517 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
6518 if (status == nfserr_stale_clientid) {
6519 if (cstate->session)
6520 return nfserr_bad_stateid;
6521 return nfserr_stale_stateid;
6525 stid = find_stateid_by_type(cstate->clp, stateid, typemask);
6527 return nfserr_bad_stateid;
6528 if ((stid->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
6529 nfs4_put_stid(stid);
6530 if (cstate->minorversion)
6531 return nfserr_deleg_revoked;
6532 return nfserr_bad_stateid;
6538 static struct nfsd_file *
6539 nfs4_find_file(struct nfs4_stid *s, int flags)
6541 struct nfsd_file *ret = NULL;
6546 switch (s->sc_type) {
6547 case NFS4_DELEG_STID:
6548 spin_lock(&s->sc_file->fi_lock);
6549 ret = nfsd_file_get(s->sc_file->fi_deleg_file);
6550 spin_unlock(&s->sc_file->fi_lock);
6552 case NFS4_OPEN_STID:
6553 case NFS4_LOCK_STID:
6554 if (flags & RD_STATE)
6555 ret = find_readable_file(s->sc_file);
6557 ret = find_writeable_file(s->sc_file);
6564 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
6568 status = nfsd4_check_openowner_confirmed(ols);
6571 return nfs4_check_openmode(ols, flags);
6575 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
6576 struct nfsd_file **nfp, int flags)
6578 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6579 struct nfsd_file *nf;
6582 nf = nfs4_find_file(s, flags);
6584 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6585 acc | NFSD_MAY_OWNER_OVERRIDE);
6591 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
6600 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6602 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
6603 if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
6605 list_del(&cps->cp_list);
6606 idr_remove(&nn->s2s_cp_stateids,
6607 cps->cp_stateid.cs_stid.si_opaque.so_id);
6611 * A READ from an inter server to server COPY will have a
6612 * copy stateid. Look up the copy notify stateid from the
6613 * idr structure and take a reference on it.
6615 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6616 struct nfs4_client *clp,
6617 struct nfs4_cpntf_state **cps)
6619 copy_stateid_t *cps_t;
6620 struct nfs4_cpntf_state *state = NULL;
6622 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6623 return nfserr_bad_stateid;
6624 spin_lock(&nn->s2s_cp_lock);
6625 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6627 state = container_of(cps_t, struct nfs4_cpntf_state,
6629 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
6634 refcount_inc(&state->cp_stateid.cs_count);
6636 _free_cpntf_state_locked(nn, state);
6639 spin_unlock(&nn->s2s_cp_lock);
6641 return nfserr_bad_stateid;
6647 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6648 struct nfs4_stid **stid)
6651 struct nfs4_cpntf_state *cps = NULL;
6652 struct nfs4_client *found;
6654 status = manage_cpntf_state(nn, st, NULL, &cps);
6658 cps->cpntf_time = ktime_get_boottime_seconds();
6660 status = nfserr_expired;
6661 found = lookup_clientid(&cps->cp_p_clid, true, nn);
6665 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6666 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6670 status = nfserr_bad_stateid;
6672 put_client_renew(found);
6674 nfs4_put_cpntf_state(nn, cps);
6678 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6680 spin_lock(&nn->s2s_cp_lock);
6681 _free_cpntf_state_locked(nn, cps);
6682 spin_unlock(&nn->s2s_cp_lock);
6686 * nfs4_preprocess_stateid_op - find and prep stateid for an operation
6687 * @rqstp: incoming request from client
6688 * @cstate: current compound state
6689 * @fhp: filehandle associated with requested stateid
6690 * @stateid: stateid (provided by client)
6691 * @flags: flags describing type of operation to be done
6692 * @nfp: optional nfsd_file return pointer (may be NULL)
6693 * @cstid: optional returned nfs4_stid pointer (may be NULL)
6695 * Given info from the client, look up a nfs4_stid for the operation. On
6696 * success, it returns a reference to the nfs4_stid and/or the nfsd_file
6697 * associated with it.
6700 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
6701 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
6702 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6703 struct nfs4_stid **cstid)
6705 struct net *net = SVC_NET(rqstp);
6706 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6707 struct nfs4_stid *s = NULL;
6713 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6715 status = nfserr_bad_stateid;
6717 status = check_special_stateids(net, fhp, stateid,
6722 status = nfsd4_lookup_stateid(cstate, stateid,
6723 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
6725 if (status == nfserr_bad_stateid)
6726 status = find_cpntf_state(nn, stateid, &s);
6729 status = nfsd4_stid_check_stateid_generation(stateid, s,
6730 nfsd4_has_session(cstate));
6734 switch (s->sc_type) {
6735 case NFS4_DELEG_STID:
6736 status = nfs4_check_delegmode(delegstateid(s), flags);
6738 case NFS4_OPEN_STID:
6739 case NFS4_LOCK_STID:
6740 status = nfs4_check_olstateid(openlockstateid(s), flags);
6743 status = nfserr_bad_stateid;
6748 status = nfs4_check_fh(fhp, s);
6751 if (status == nfs_ok && nfp)
6752 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
6755 if (!status && cstid)
6764 * Test if the stateid is valid
6767 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6768 union nfsd4_op_u *u)
6770 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
6771 struct nfsd4_test_stateid_id *stateid;
6772 struct nfs4_client *cl = cstate->clp;
6774 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
6775 stateid->ts_id_status =
6776 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
6782 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6784 struct nfs4_ol_stateid *stp = openlockstateid(s);
6787 ret = nfsd4_lock_ol_stateid(stp);
6791 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6795 ret = nfserr_locks_held;
6796 if (check_for_locks(stp->st_stid.sc_file,
6797 lockowner(stp->st_stateowner)))
6800 release_lock_stateid(stp);
6804 mutex_unlock(&stp->st_mutex);
6811 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6812 union nfsd4_op_u *u)
6814 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
6815 stateid_t *stateid = &free_stateid->fr_stateid;
6816 struct nfs4_stid *s;
6817 struct nfs4_delegation *dp;
6818 struct nfs4_client *cl = cstate->clp;
6819 __be32 ret = nfserr_bad_stateid;
6821 spin_lock(&cl->cl_lock);
6822 s = find_stateid_locked(cl, stateid);
6825 spin_lock(&s->sc_lock);
6826 switch (s->sc_type) {
6827 case NFS4_DELEG_STID:
6828 ret = nfserr_locks_held;
6830 case NFS4_OPEN_STID:
6831 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6834 ret = nfserr_locks_held;
6836 case NFS4_LOCK_STID:
6837 spin_unlock(&s->sc_lock);
6838 refcount_inc(&s->sc_count);
6839 spin_unlock(&cl->cl_lock);
6840 ret = nfsd4_free_lock_stateid(stateid, s);
6842 case NFS4_REVOKED_DELEG_STID:
6843 spin_unlock(&s->sc_lock);
6844 dp = delegstateid(s);
6845 list_del_init(&dp->dl_recall_lru);
6846 spin_unlock(&cl->cl_lock);
6850 /* Default falls through and returns nfserr_bad_stateid */
6852 spin_unlock(&s->sc_lock);
6854 spin_unlock(&cl->cl_lock);
6862 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6863 RD_STATE : WR_STATE;
6866 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6868 struct svc_fh *current_fh = &cstate->current_fh;
6869 struct nfs4_stateowner *sop = stp->st_stateowner;
6872 status = nfsd4_check_seqid(cstate, sop, seqid);
6875 status = nfsd4_lock_ol_stateid(stp);
6876 if (status != nfs_ok)
6878 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6879 if (status == nfs_ok)
6880 status = nfs4_check_fh(current_fh, &stp->st_stid);
6881 if (status != nfs_ok)
6882 mutex_unlock(&stp->st_mutex);
6887 * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op
6888 * @cstate: compund state
6889 * @seqid: seqid (provided by client)
6890 * @stateid: stateid (provided by client)
6891 * @typemask: mask of allowable types for this operation
6892 * @stpp: return pointer for the stateid found
6893 * @nn: net namespace for request
6895 * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and
6896 * return it in @stpp. On a nfs_ok return, the returned stateid will
6897 * have its st_mutex locked.
6900 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6901 stateid_t *stateid, char typemask,
6902 struct nfs4_ol_stateid **stpp,
6903 struct nfsd_net *nn)
6906 struct nfs4_stid *s;
6907 struct nfs4_ol_stateid *stp = NULL;
6909 trace_nfsd_preprocess(seqid, stateid);
6912 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6915 stp = openlockstateid(s);
6916 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6918 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6922 nfs4_put_stid(&stp->st_stid);
6926 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6927 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6930 struct nfs4_openowner *oo;
6931 struct nfs4_ol_stateid *stp;
6933 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6934 NFS4_OPEN_STID, &stp, nn);
6937 oo = openowner(stp->st_stateowner);
6938 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6939 mutex_unlock(&stp->st_mutex);
6940 nfs4_put_stid(&stp->st_stid);
6941 return nfserr_bad_stateid;
6948 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6949 union nfsd4_op_u *u)
6951 struct nfsd4_open_confirm *oc = &u->open_confirm;
6953 struct nfs4_openowner *oo;
6954 struct nfs4_ol_stateid *stp;
6955 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6957 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6958 cstate->current_fh.fh_dentry);
6960 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6964 status = nfs4_preprocess_seqid_op(cstate,
6965 oc->oc_seqid, &oc->oc_req_stateid,
6966 NFS4_OPEN_STID, &stp, nn);
6969 oo = openowner(stp->st_stateowner);
6970 status = nfserr_bad_stateid;
6971 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6972 mutex_unlock(&stp->st_mutex);
6975 oo->oo_flags |= NFS4_OO_CONFIRMED;
6976 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6977 mutex_unlock(&stp->st_mutex);
6978 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6979 nfsd4_client_record_create(oo->oo_owner.so_client);
6982 nfs4_put_stid(&stp->st_stid);
6984 nfsd4_bump_seqid(cstate, status);
6988 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6990 if (!test_access(access, stp))
6992 nfs4_file_put_access(stp->st_stid.sc_file, access);
6993 clear_access(access, stp);
6996 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6998 switch (to_access) {
6999 case NFS4_SHARE_ACCESS_READ:
7000 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
7001 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7003 case NFS4_SHARE_ACCESS_WRITE:
7004 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
7005 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7007 case NFS4_SHARE_ACCESS_BOTH:
7015 nfsd4_open_downgrade(struct svc_rqst *rqstp,
7016 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
7018 struct nfsd4_open_downgrade *od = &u->open_downgrade;
7020 struct nfs4_ol_stateid *stp;
7021 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7023 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
7024 cstate->current_fh.fh_dentry);
7026 /* We don't yet support WANT bits: */
7027 if (od->od_deleg_want)
7028 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
7031 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
7032 &od->od_stateid, &stp, nn);
7035 status = nfserr_inval;
7036 if (!test_access(od->od_share_access, stp)) {
7037 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
7038 stp->st_access_bmap, od->od_share_access);
7041 if (!test_deny(od->od_share_deny, stp)) {
7042 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
7043 stp->st_deny_bmap, od->od_share_deny);
7046 nfs4_stateid_downgrade(stp, od->od_share_access);
7047 reset_union_bmap_deny(od->od_share_deny, stp);
7048 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
7051 mutex_unlock(&stp->st_mutex);
7052 nfs4_put_stid(&stp->st_stid);
7054 nfsd4_bump_seqid(cstate, status);
7058 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
7060 struct nfs4_client *clp = s->st_stid.sc_client;
7062 LIST_HEAD(reaplist);
7063 struct nfs4_ol_stateid *stp;
7065 spin_lock(&clp->cl_lock);
7066 unhashed = unhash_open_stateid(s, &reaplist);
7068 if (clp->cl_minorversion) {
7070 put_ol_stateid_locked(s, &reaplist);
7071 spin_unlock(&clp->cl_lock);
7072 list_for_each_entry(stp, &reaplist, st_locks)
7073 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
7074 free_ol_stateid_reaplist(&reaplist);
7076 spin_unlock(&clp->cl_lock);
7077 free_ol_stateid_reaplist(&reaplist);
7079 move_to_close_lru(s, clp->net);
7084 * nfs4_unlock_state() called after encode
7087 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7088 union nfsd4_op_u *u)
7090 struct nfsd4_close *close = &u->close;
7092 struct nfs4_ol_stateid *stp;
7093 struct net *net = SVC_NET(rqstp);
7094 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7096 dprintk("NFSD: nfsd4_close on file %pd\n",
7097 cstate->current_fh.fh_dentry);
7099 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
7101 NFS4_OPEN_STID|NFS4_CLOSED_STID,
7103 nfsd4_bump_seqid(cstate, status);
7107 stp->st_stid.sc_type = NFS4_CLOSED_STID;
7110 * Technically we don't _really_ have to increment or copy it, since
7111 * it should just be gone after this operation and we clobber the
7112 * copied value below, but we continue to do so here just to ensure
7113 * that racing ops see that there was a state change.
7115 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
7117 nfsd4_close_open_stateid(stp);
7118 mutex_unlock(&stp->st_mutex);
7120 /* v4.1+ suggests that we send a special stateid in here, since the
7121 * clients should just ignore this anyway. Since this is not useful
7122 * for v4.0 clients either, we set it to the special close_stateid
7125 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
7127 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
7129 /* put reference from nfs4_preprocess_seqid_op */
7130 nfs4_put_stid(&stp->st_stid);
7136 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7137 union nfsd4_op_u *u)
7139 struct nfsd4_delegreturn *dr = &u->delegreturn;
7140 struct nfs4_delegation *dp;
7141 stateid_t *stateid = &dr->dr_stateid;
7142 struct nfs4_stid *s;
7144 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7146 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7149 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
7152 dp = delegstateid(s);
7153 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
7157 trace_nfsd_deleg_return(stateid);
7158 wake_up_var(d_inode(cstate->current_fh.fh_dentry));
7159 destroy_delegation(dp);
7161 nfs4_put_stid(&dp->dl_stid);
7166 /* last octet in a range */
7168 last_byte_offset(u64 start, u64 len)
7174 return end > start ? end - 1: NFS4_MAX_UINT64;
7178 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
7179 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
7180 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
7181 * locking, this prevents us from being completely protocol-compliant. The
7182 * real solution to this problem is to start using unsigned file offsets in
7183 * the VFS, but this is a very deep change!
7186 nfs4_transform_lock_offset(struct file_lock *lock)
7188 if (lock->fl_start < 0)
7189 lock->fl_start = OFFSET_MAX;
7190 if (lock->fl_end < 0)
7191 lock->fl_end = OFFSET_MAX;
7195 nfsd4_lm_get_owner(fl_owner_t owner)
7197 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7199 nfs4_get_stateowner(&lo->lo_owner);
7204 nfsd4_lm_put_owner(fl_owner_t owner)
7206 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7209 nfs4_put_stateowner(&lo->lo_owner);
7212 /* return pointer to struct nfs4_client if client is expirable */
7214 nfsd4_lm_lock_expirable(struct file_lock *cfl)
7216 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
7217 struct nfs4_client *clp = lo->lo_owner.so_client;
7218 struct nfsd_net *nn;
7220 if (try_to_expire_client(clp)) {
7221 nn = net_generic(clp->net, nfsd_net_id);
7222 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
7228 /* schedule laundromat to run immediately and wait for it to complete */
7230 nfsd4_lm_expire_lock(void)
7232 flush_workqueue(laundry_wq);
7236 nfsd4_lm_notify(struct file_lock *fl)
7238 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
7239 struct net *net = lo->lo_owner.so_client->net;
7240 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7241 struct nfsd4_blocked_lock *nbl = container_of(fl,
7242 struct nfsd4_blocked_lock, nbl_lock);
7245 /* An empty list means that something else is going to be using it */
7246 spin_lock(&nn->blocked_locks_lock);
7247 if (!list_empty(&nbl->nbl_list)) {
7248 list_del_init(&nbl->nbl_list);
7249 list_del_init(&nbl->nbl_lru);
7252 spin_unlock(&nn->blocked_locks_lock);
7255 trace_nfsd_cb_notify_lock(lo, nbl);
7256 nfsd4_run_cb(&nbl->nbl_cb);
7260 static const struct lock_manager_operations nfsd_posix_mng_ops = {
7261 .lm_mod_owner = THIS_MODULE,
7262 .lm_notify = nfsd4_lm_notify,
7263 .lm_get_owner = nfsd4_lm_get_owner,
7264 .lm_put_owner = nfsd4_lm_put_owner,
7265 .lm_lock_expirable = nfsd4_lm_lock_expirable,
7266 .lm_expire_lock = nfsd4_lm_expire_lock,
7270 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
7272 struct nfs4_lockowner *lo;
7274 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
7275 lo = (struct nfs4_lockowner *) fl->fl_owner;
7276 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
7278 if (!deny->ld_owner.data)
7279 /* We just don't care that much */
7281 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
7284 deny->ld_owner.len = 0;
7285 deny->ld_owner.data = NULL;
7286 deny->ld_clientid.cl_boot = 0;
7287 deny->ld_clientid.cl_id = 0;
7289 deny->ld_start = fl->fl_start;
7290 deny->ld_length = NFS4_MAX_UINT64;
7291 if (fl->fl_end != NFS4_MAX_UINT64)
7292 deny->ld_length = fl->fl_end - fl->fl_start + 1;
7293 deny->ld_type = NFS4_READ_LT;
7294 if (fl->fl_type != F_RDLCK)
7295 deny->ld_type = NFS4_WRITE_LT;
7298 static struct nfs4_lockowner *
7299 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
7301 unsigned int strhashval = ownerstr_hashval(owner);
7302 struct nfs4_stateowner *so;
7304 lockdep_assert_held(&clp->cl_lock);
7306 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7308 if (so->so_is_open_owner)
7310 if (same_owner_str(so, owner))
7311 return lockowner(nfs4_get_stateowner(so));
7316 static struct nfs4_lockowner *
7317 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
7319 struct nfs4_lockowner *lo;
7321 spin_lock(&clp->cl_lock);
7322 lo = find_lockowner_str_locked(clp, owner);
7323 spin_unlock(&clp->cl_lock);
7327 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
7329 unhash_lockowner_locked(lockowner(sop));
7332 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
7334 struct nfs4_lockowner *lo = lockowner(sop);
7336 kmem_cache_free(lockowner_slab, lo);
7339 static const struct nfs4_stateowner_operations lockowner_ops = {
7340 .so_unhash = nfs4_unhash_lockowner,
7341 .so_free = nfs4_free_lockowner,
7345 * Alloc a lock owner structure.
7346 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
7349 * strhashval = ownerstr_hashval
7351 static struct nfs4_lockowner *
7352 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
7353 struct nfs4_ol_stateid *open_stp,
7354 struct nfsd4_lock *lock)
7356 struct nfs4_lockowner *lo, *ret;
7358 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
7361 INIT_LIST_HEAD(&lo->lo_blocked);
7362 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
7363 lo->lo_owner.so_is_open_owner = 0;
7364 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
7365 lo->lo_owner.so_ops = &lockowner_ops;
7366 spin_lock(&clp->cl_lock);
7367 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
7369 list_add(&lo->lo_owner.so_strhash,
7370 &clp->cl_ownerstr_hashtbl[strhashval]);
7373 nfs4_free_stateowner(&lo->lo_owner);
7375 spin_unlock(&clp->cl_lock);
7379 static struct nfs4_ol_stateid *
7380 find_lock_stateid(const struct nfs4_lockowner *lo,
7381 const struct nfs4_ol_stateid *ost)
7383 struct nfs4_ol_stateid *lst;
7385 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
7387 /* If ost is not hashed, ost->st_locks will not be valid */
7388 if (!nfs4_ol_stateid_unhashed(ost))
7389 list_for_each_entry(lst, &ost->st_locks, st_locks) {
7390 if (lst->st_stateowner == &lo->lo_owner) {
7391 refcount_inc(&lst->st_stid.sc_count);
7398 static struct nfs4_ol_stateid *
7399 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7400 struct nfs4_file *fp, struct inode *inode,
7401 struct nfs4_ol_stateid *open_stp)
7403 struct nfs4_client *clp = lo->lo_owner.so_client;
7404 struct nfs4_ol_stateid *retstp;
7406 mutex_init(&stp->st_mutex);
7407 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
7409 spin_lock(&clp->cl_lock);
7410 if (nfs4_ol_stateid_unhashed(open_stp))
7412 retstp = find_lock_stateid(lo, open_stp);
7415 refcount_inc(&stp->st_stid.sc_count);
7416 stp->st_stid.sc_type = NFS4_LOCK_STID;
7417 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
7419 stp->st_stid.sc_file = fp;
7420 stp->st_access_bmap = 0;
7421 stp->st_deny_bmap = open_stp->st_deny_bmap;
7422 stp->st_openstp = open_stp;
7423 spin_lock(&fp->fi_lock);
7424 list_add(&stp->st_locks, &open_stp->st_locks);
7425 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
7426 list_add(&stp->st_perfile, &fp->fi_stateids);
7427 spin_unlock(&fp->fi_lock);
7428 spin_unlock(&clp->cl_lock);
7431 spin_unlock(&clp->cl_lock);
7432 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
7433 nfs4_put_stid(&retstp->st_stid);
7436 /* To keep mutex tracking happy */
7437 mutex_unlock(&stp->st_mutex);
7440 spin_unlock(&clp->cl_lock);
7441 mutex_unlock(&stp->st_mutex);
7445 static struct nfs4_ol_stateid *
7446 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7447 struct inode *inode, struct nfs4_ol_stateid *ost,
7450 struct nfs4_stid *ns = NULL;
7451 struct nfs4_ol_stateid *lst;
7452 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7453 struct nfs4_client *clp = oo->oo_owner.so_client;
7456 spin_lock(&clp->cl_lock);
7457 lst = find_lock_stateid(lo, ost);
7458 spin_unlock(&clp->cl_lock);
7460 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7462 nfs4_put_stid(&lst->st_stid);
7464 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7468 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7469 if (lst == openlockstateid(ns))
7478 check_lock_length(u64 offset, u64 length)
7480 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7481 (length > ~offset)));
7484 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
7486 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
7488 lockdep_assert_held(&fp->fi_lock);
7490 if (test_access(access, lock_stp))
7492 __nfs4_file_get_access(fp, access);
7493 set_access(access, lock_stp);
7497 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7498 struct nfs4_ol_stateid *ost,
7499 struct nfsd4_lock *lock,
7500 struct nfs4_ol_stateid **plst, bool *new)
7503 struct nfs4_file *fi = ost->st_stid.sc_file;
7504 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7505 struct nfs4_client *cl = oo->oo_owner.so_client;
7506 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
7507 struct nfs4_lockowner *lo;
7508 struct nfs4_ol_stateid *lst;
7509 unsigned int strhashval;
7511 lo = find_lockowner_str(cl, &lock->lk_new_owner);
7513 strhashval = ownerstr_hashval(&lock->lk_new_owner);
7514 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7516 return nfserr_jukebox;
7518 /* with an existing lockowner, seqids must be the same */
7519 status = nfserr_bad_seqid;
7520 if (!cstate->minorversion &&
7521 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
7525 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7527 status = nfserr_jukebox;
7534 nfs4_put_stateowner(&lo->lo_owner);
7542 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7543 union nfsd4_op_u *u)
7545 struct nfsd4_lock *lock = &u->lock;
7546 struct nfs4_openowner *open_sop = NULL;
7547 struct nfs4_lockowner *lock_sop = NULL;
7548 struct nfs4_ol_stateid *lock_stp = NULL;
7549 struct nfs4_ol_stateid *open_stp = NULL;
7550 struct nfs4_file *fp;
7551 struct nfsd_file *nf = NULL;
7552 struct nfsd4_blocked_lock *nbl = NULL;
7553 struct file_lock *file_lock = NULL;
7554 struct file_lock *conflock = NULL;
7555 struct super_block *sb;
7560 unsigned char fl_type;
7561 unsigned int fl_flags = FL_POSIX;
7562 struct net *net = SVC_NET(rqstp);
7563 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7565 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7566 (long long) lock->lk_offset,
7567 (long long) lock->lk_length);
7569 if (check_lock_length(lock->lk_offset, lock->lk_length))
7570 return nfserr_inval;
7572 if ((status = fh_verify(rqstp, &cstate->current_fh,
7573 S_IFREG, NFSD_MAY_LOCK))) {
7574 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7577 sb = cstate->current_fh.fh_dentry->d_sb;
7579 if (lock->lk_is_new) {
7580 if (nfsd4_has_session(cstate))
7581 /* See rfc 5661 18.10.3: given clientid is ignored: */
7582 memcpy(&lock->lk_new_clientid,
7583 &cstate->clp->cl_clientid,
7584 sizeof(clientid_t));
7586 /* validate and update open stateid and open seqid */
7587 status = nfs4_preprocess_confirmed_seqid_op(cstate,
7588 lock->lk_new_open_seqid,
7589 &lock->lk_new_open_stateid,
7593 mutex_unlock(&open_stp->st_mutex);
7594 open_sop = openowner(open_stp->st_stateowner);
7595 status = nfserr_bad_stateid;
7596 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
7597 &lock->lk_new_clientid))
7599 status = lookup_or_create_lock_state(cstate, open_stp, lock,
7602 status = nfs4_preprocess_seqid_op(cstate,
7603 lock->lk_old_lock_seqid,
7604 &lock->lk_old_lock_stateid,
7605 NFS4_LOCK_STID, &lock_stp, nn);
7609 lock_sop = lockowner(lock_stp->st_stateowner);
7611 lkflg = setlkflg(lock->lk_type);
7612 status = nfs4_check_openmode(lock_stp, lkflg);
7616 status = nfserr_grace;
7617 if (locks_in_grace(net) && !lock->lk_reclaim)
7619 status = nfserr_no_grace;
7620 if (!locks_in_grace(net) && lock->lk_reclaim)
7623 if (lock->lk_reclaim)
7624 fl_flags |= FL_RECLAIM;
7626 fp = lock_stp->st_stid.sc_file;
7627 switch (lock->lk_type) {
7629 if (nfsd4_has_session(cstate) ||
7630 exportfs_lock_op_is_async(sb->s_export_op))
7631 fl_flags |= FL_SLEEP;
7634 spin_lock(&fp->fi_lock);
7635 nf = find_readable_file_locked(fp);
7637 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7638 spin_unlock(&fp->fi_lock);
7641 case NFS4_WRITEW_LT:
7642 if (nfsd4_has_session(cstate) ||
7643 exportfs_lock_op_is_async(sb->s_export_op))
7644 fl_flags |= FL_SLEEP;
7647 spin_lock(&fp->fi_lock);
7648 nf = find_writeable_file_locked(fp);
7650 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7651 spin_unlock(&fp->fi_lock);
7655 status = nfserr_inval;
7660 status = nfserr_openmode;
7665 * Most filesystems with their own ->lock operations will block
7666 * the nfsd thread waiting to acquire the lock. That leads to
7667 * deadlocks (we don't want every nfsd thread tied up waiting
7668 * for file locks), so don't attempt blocking lock notifications
7669 * on those filesystems:
7671 if (!exportfs_lock_op_is_async(sb->s_export_op))
7672 fl_flags &= ~FL_SLEEP;
7674 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
7676 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
7677 status = nfserr_jukebox;
7681 file_lock = &nbl->nbl_lock;
7682 file_lock->fl_type = fl_type;
7683 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
7684 file_lock->fl_pid = current->tgid;
7685 file_lock->fl_file = nf->nf_file;
7686 file_lock->fl_flags = fl_flags;
7687 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7688 file_lock->fl_start = lock->lk_offset;
7689 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
7690 nfs4_transform_lock_offset(file_lock);
7692 conflock = locks_alloc_lock();
7694 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7695 status = nfserr_jukebox;
7699 if (fl_flags & FL_SLEEP) {
7700 nbl->nbl_time = ktime_get_boottime_seconds();
7701 spin_lock(&nn->blocked_locks_lock);
7702 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
7703 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
7704 kref_get(&nbl->nbl_kref);
7705 spin_unlock(&nn->blocked_locks_lock);
7708 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
7710 case 0: /* success! */
7711 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
7713 if (lock->lk_reclaim)
7714 nn->somebody_reclaimed = true;
7716 case FILE_LOCK_DEFERRED:
7717 kref_put(&nbl->nbl_kref, free_nbl);
7720 case -EAGAIN: /* conflock holds conflicting lock */
7721 status = nfserr_denied;
7722 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
7723 nfs4_set_lock_denied(conflock, &lock->lk_denied);
7726 status = nfserr_deadlock;
7729 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
7730 status = nfserrno(err);
7735 /* dequeue it if we queued it before */
7736 if (fl_flags & FL_SLEEP) {
7737 spin_lock(&nn->blocked_locks_lock);
7738 if (!list_empty(&nbl->nbl_list) &&
7739 !list_empty(&nbl->nbl_lru)) {
7740 list_del_init(&nbl->nbl_list);
7741 list_del_init(&nbl->nbl_lru);
7742 kref_put(&nbl->nbl_kref, free_nbl);
7744 /* nbl can use one of lists to be linked to reaplist */
7745 spin_unlock(&nn->blocked_locks_lock);
7747 free_blocked_lock(nbl);
7752 /* Bump seqid manually if the 4.0 replay owner is openowner */
7753 if (cstate->replay_owner &&
7754 cstate->replay_owner != &lock_sop->lo_owner &&
7755 seqid_mutating_err(ntohl(status)))
7756 lock_sop->lo_owner.so_seqid++;
7759 * If this is a new, never-before-used stateid, and we are
7760 * returning an error, then just go ahead and release it.
7763 release_lock_stateid(lock_stp);
7765 mutex_unlock(&lock_stp->st_mutex);
7767 nfs4_put_stid(&lock_stp->st_stid);
7770 nfs4_put_stid(&open_stp->st_stid);
7771 nfsd4_bump_seqid(cstate, status);
7773 locks_free_lock(conflock);
7777 void nfsd4_lock_release(union nfsd4_op_u *u)
7779 struct nfsd4_lock *lock = &u->lock;
7780 struct nfsd4_lock_denied *deny = &lock->lk_denied;
7782 kfree(deny->ld_owner.data);
7786 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7787 * so we do a temporary open here just to get an open file to pass to
7790 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
7792 struct nfsd_file *nf;
7793 struct inode *inode;
7796 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7799 inode = fhp->fh_dentry->d_inode;
7800 inode_lock(inode); /* to block new leases till after test_lock: */
7801 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
7804 lock->fl_file = nf->nf_file;
7805 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
7806 lock->fl_file = NULL;
7808 inode_unlock(inode);
7817 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7818 union nfsd4_op_u *u)
7820 struct nfsd4_lockt *lockt = &u->lockt;
7821 struct file_lock *file_lock = NULL;
7822 struct nfs4_lockowner *lo = NULL;
7824 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7826 if (locks_in_grace(SVC_NET(rqstp)))
7827 return nfserr_grace;
7829 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7830 return nfserr_inval;
7832 if (!nfsd4_has_session(cstate)) {
7833 status = set_client(&lockt->lt_clientid, cstate, nn);
7838 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7841 file_lock = locks_alloc_lock();
7843 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7844 status = nfserr_jukebox;
7848 switch (lockt->lt_type) {
7851 file_lock->fl_type = F_RDLCK;
7854 case NFS4_WRITEW_LT:
7855 file_lock->fl_type = F_WRLCK;
7858 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
7859 status = nfserr_inval;
7863 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
7865 file_lock->fl_owner = (fl_owner_t)lo;
7866 file_lock->fl_pid = current->tgid;
7867 file_lock->fl_flags = FL_POSIX;
7869 file_lock->fl_start = lockt->lt_offset;
7870 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
7872 nfs4_transform_lock_offset(file_lock);
7874 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
7878 if (file_lock->fl_type != F_UNLCK) {
7879 status = nfserr_denied;
7880 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
7884 nfs4_put_stateowner(&lo->lo_owner);
7886 locks_free_lock(file_lock);
7890 void nfsd4_lockt_release(union nfsd4_op_u *u)
7892 struct nfsd4_lockt *lockt = &u->lockt;
7893 struct nfsd4_lock_denied *deny = &lockt->lt_denied;
7895 kfree(deny->ld_owner.data);
7899 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7900 union nfsd4_op_u *u)
7902 struct nfsd4_locku *locku = &u->locku;
7903 struct nfs4_ol_stateid *stp;
7904 struct nfsd_file *nf = NULL;
7905 struct file_lock *file_lock = NULL;
7908 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7910 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7911 (long long) locku->lu_offset,
7912 (long long) locku->lu_length);
7914 if (check_lock_length(locku->lu_offset, locku->lu_length))
7915 return nfserr_inval;
7917 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
7918 &locku->lu_stateid, NFS4_LOCK_STID,
7922 nf = find_any_file(stp->st_stid.sc_file);
7924 status = nfserr_lock_range;
7927 file_lock = locks_alloc_lock();
7929 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7930 status = nfserr_jukebox;
7934 file_lock->fl_type = F_UNLCK;
7935 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7936 file_lock->fl_pid = current->tgid;
7937 file_lock->fl_file = nf->nf_file;
7938 file_lock->fl_flags = FL_POSIX;
7939 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7940 file_lock->fl_start = locku->lu_offset;
7942 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7944 nfs4_transform_lock_offset(file_lock);
7946 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7948 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7951 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7955 mutex_unlock(&stp->st_mutex);
7956 nfs4_put_stid(&stp->st_stid);
7958 nfsd4_bump_seqid(cstate, status);
7960 locks_free_lock(file_lock);
7964 status = nfserrno(err);
7970 * true: locks held by lockowner
7971 * false: no locks held by lockowner
7974 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7976 struct file_lock *fl;
7978 struct nfsd_file *nf = find_any_file(fp);
7979 struct inode *inode;
7980 struct file_lock_context *flctx;
7983 /* Any valid lock stateid should have some sort of access */
7988 inode = file_inode(nf->nf_file);
7989 flctx = locks_inode_context(inode);
7991 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7992 spin_lock(&flctx->flc_lock);
7993 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7994 if (fl->fl_owner == (fl_owner_t)lowner) {
7999 spin_unlock(&flctx->flc_lock);
8006 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
8007 * @rqstp: RPC transaction
8008 * @cstate: NFSv4 COMPOUND state
8009 * @u: RELEASE_LOCKOWNER arguments
8011 * The lockowner's so_count is bumped when a lock record is added
8012 * or when copying a conflicting lock. The latter case is brief,
8013 * but can lead to fleeting false positives when looking for
8017 * %nfs_ok: lockowner released or not found
8018 * %nfserr_locks_held: lockowner still in use
8019 * %nfserr_stale_clientid: clientid no longer active
8020 * %nfserr_expired: clientid not recognized
8023 nfsd4_release_lockowner(struct svc_rqst *rqstp,
8024 struct nfsd4_compound_state *cstate,
8025 union nfsd4_op_u *u)
8027 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
8028 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8029 clientid_t *clid = &rlockowner->rl_clientid;
8030 struct nfs4_ol_stateid *stp;
8031 struct nfs4_lockowner *lo;
8032 struct nfs4_client *clp;
8033 LIST_HEAD(reaplist);
8036 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
8037 clid->cl_boot, clid->cl_id);
8039 status = set_client(clid, cstate, nn);
8044 spin_lock(&clp->cl_lock);
8045 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
8047 spin_unlock(&clp->cl_lock);
8050 if (atomic_read(&lo->lo_owner.so_count) != 2) {
8051 spin_unlock(&clp->cl_lock);
8052 nfs4_put_stateowner(&lo->lo_owner);
8053 return nfserr_locks_held;
8055 unhash_lockowner_locked(lo);
8056 while (!list_empty(&lo->lo_owner.so_stateids)) {
8057 stp = list_first_entry(&lo->lo_owner.so_stateids,
8058 struct nfs4_ol_stateid,
8060 WARN_ON(!unhash_lock_stateid(stp));
8061 put_ol_stateid_locked(stp, &reaplist);
8063 spin_unlock(&clp->cl_lock);
8065 free_ol_stateid_reaplist(&reaplist);
8066 remove_blocked_locks(lo);
8067 nfs4_put_stateowner(&lo->lo_owner);
8071 static inline struct nfs4_client_reclaim *
8074 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
8078 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
8080 struct nfs4_client_reclaim *crp;
8082 crp = nfsd4_find_reclaim_client(name, nn);
8083 return (crp && crp->cr_clp);
8087 * failure => all reset bets are off, nfserr_no_grace...
8089 * The caller is responsible for freeing name.data if NULL is returned (it
8090 * will be freed in nfs4_remove_reclaim_record in the normal case).
8092 struct nfs4_client_reclaim *
8093 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
8094 struct nfsd_net *nn)
8096 unsigned int strhashval;
8097 struct nfs4_client_reclaim *crp;
8099 crp = alloc_reclaim();
8101 strhashval = clientstr_hashval(name);
8102 INIT_LIST_HEAD(&crp->cr_strhash);
8103 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
8104 crp->cr_name.data = name.data;
8105 crp->cr_name.len = name.len;
8106 crp->cr_princhash.data = princhash.data;
8107 crp->cr_princhash.len = princhash.len;
8109 nn->reclaim_str_hashtbl_size++;
8115 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
8117 list_del(&crp->cr_strhash);
8118 kfree(crp->cr_name.data);
8119 kfree(crp->cr_princhash.data);
8121 nn->reclaim_str_hashtbl_size--;
8125 nfs4_release_reclaim(struct nfsd_net *nn)
8127 struct nfs4_client_reclaim *crp = NULL;
8130 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8131 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
8132 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
8133 struct nfs4_client_reclaim, cr_strhash);
8134 nfs4_remove_reclaim_record(crp, nn);
8137 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
8141 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
8142 struct nfs4_client_reclaim *
8143 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
8145 unsigned int strhashval;
8146 struct nfs4_client_reclaim *crp = NULL;
8148 strhashval = clientstr_hashval(name);
8149 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
8150 if (compare_blob(&crp->cr_name, &name) == 0) {
8158 nfs4_check_open_reclaim(struct nfs4_client *clp)
8160 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
8161 return nfserr_no_grace;
8163 if (nfsd4_client_record_check(clp))
8164 return nfserr_reclaim_bad;
8170 * Since the lifetime of a delegation isn't limited to that of an open, a
8171 * client may quite reasonably hang on to a delegation as long as it has
8172 * the inode cached. This becomes an obvious problem the first time a
8173 * client's inode cache approaches the size of the server's total memory.
8175 * For now we avoid this problem by imposing a hard limit on the number
8176 * of delegations, which varies according to the server's memory size.
8179 set_max_delegations(void)
8182 * Allow at most 4 delegations per megabyte of RAM. Quick
8183 * estimates suggest that in the worst case (where every delegation
8184 * is for a different inode), a delegation could take about 1.5K,
8185 * giving a worst case usage of about 6% of memory.
8187 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
8190 static int nfs4_state_create_net(struct net *net)
8192 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8195 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8196 sizeof(struct list_head),
8198 if (!nn->conf_id_hashtbl)
8200 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8201 sizeof(struct list_head),
8203 if (!nn->unconf_id_hashtbl)
8205 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
8206 sizeof(struct list_head),
8208 if (!nn->sessionid_hashtbl)
8211 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8212 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
8213 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
8215 for (i = 0; i < SESSION_HASH_SIZE; i++)
8216 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
8217 nn->conf_name_tree = RB_ROOT;
8218 nn->unconf_name_tree = RB_ROOT;
8219 nn->boot_time = ktime_get_real_seconds();
8220 nn->grace_ended = false;
8221 nn->nfsd4_manager.block_opens = true;
8222 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
8223 INIT_LIST_HEAD(&nn->client_lru);
8224 INIT_LIST_HEAD(&nn->close_lru);
8225 INIT_LIST_HEAD(&nn->del_recall_lru);
8226 spin_lock_init(&nn->client_lock);
8227 spin_lock_init(&nn->s2s_cp_lock);
8228 idr_init(&nn->s2s_cp_stateids);
8230 spin_lock_init(&nn->blocked_locks_lock);
8231 INIT_LIST_HEAD(&nn->blocked_locks_lru);
8233 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
8234 INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker);
8237 nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client");
8238 if (!nn->nfsd_client_shrinker)
8241 nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan;
8242 nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count;
8243 nn->nfsd_client_shrinker->private_data = nn;
8245 shrinker_register(nn->nfsd_client_shrinker);
8251 kfree(nn->sessionid_hashtbl);
8253 kfree(nn->unconf_id_hashtbl);
8255 kfree(nn->conf_id_hashtbl);
8261 nfs4_state_destroy_net(struct net *net)
8264 struct nfs4_client *clp = NULL;
8265 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8267 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8268 while (!list_empty(&nn->conf_id_hashtbl[i])) {
8269 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8270 destroy_client(clp);
8274 WARN_ON(!list_empty(&nn->blocked_locks_lru));
8276 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8277 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
8278 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8279 destroy_client(clp);
8283 kfree(nn->sessionid_hashtbl);
8284 kfree(nn->unconf_id_hashtbl);
8285 kfree(nn->conf_id_hashtbl);
8290 nfs4_state_start_net(struct net *net)
8292 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8295 ret = nfs4_state_create_net(net);
8298 locks_start_grace(net, &nn->nfsd4_manager);
8299 nfsd4_client_tracking_init(net);
8300 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
8302 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
8303 nn->nfsd4_grace, net->ns.inum);
8304 trace_nfsd_grace_start(nn);
8305 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
8309 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
8311 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
8312 nfsd4_end_grace(nn);
8316 /* initialization to perform when the nfsd service is started: */
8319 nfs4_state_start(void)
8323 ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params);
8327 ret = nfsd4_create_callback_queue();
8329 rhltable_destroy(&nfs4_file_rhltable);
8333 set_max_delegations();
8338 nfs4_state_shutdown_net(struct net *net)
8340 struct nfs4_delegation *dp = NULL;
8341 struct list_head *pos, *next, reaplist;
8342 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8344 shrinker_free(nn->nfsd_client_shrinker);
8345 cancel_work(&nn->nfsd_shrinker_work);
8346 cancel_delayed_work_sync(&nn->laundromat_work);
8347 locks_end_grace(&nn->nfsd4_manager);
8349 INIT_LIST_HEAD(&reaplist);
8350 spin_lock(&state_lock);
8351 list_for_each_safe(pos, next, &nn->del_recall_lru) {
8352 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8353 WARN_ON(!unhash_delegation_locked(dp));
8354 list_add(&dp->dl_recall_lru, &reaplist);
8356 spin_unlock(&state_lock);
8357 list_for_each_safe(pos, next, &reaplist) {
8358 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8359 list_del_init(&dp->dl_recall_lru);
8360 destroy_unhashed_deleg(dp);
8363 nfsd4_client_tracking_exit(net);
8364 nfs4_state_destroy_net(net);
8365 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
8366 nfsd4_ssc_shutdown_umount(nn);
8371 nfs4_state_shutdown(void)
8373 nfsd4_destroy_callback_queue();
8374 rhltable_destroy(&nfs4_file_rhltable);
8378 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8380 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
8381 CURRENT_STATEID(stateid))
8382 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8386 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8388 if (cstate->minorversion) {
8389 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
8390 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8395 clear_current_stateid(struct nfsd4_compound_state *cstate)
8397 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8401 * functions to set current state id
8404 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
8405 union nfsd4_op_u *u)
8407 put_stateid(cstate, &u->open_downgrade.od_stateid);
8411 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
8412 union nfsd4_op_u *u)
8414 put_stateid(cstate, &u->open.op_stateid);
8418 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
8419 union nfsd4_op_u *u)
8421 put_stateid(cstate, &u->close.cl_stateid);
8425 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
8426 union nfsd4_op_u *u)
8428 put_stateid(cstate, &u->lock.lk_resp_stateid);
8432 * functions to consume current state id
8436 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
8437 union nfsd4_op_u *u)
8439 get_stateid(cstate, &u->open_downgrade.od_stateid);
8443 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8444 union nfsd4_op_u *u)
8446 get_stateid(cstate, &u->delegreturn.dr_stateid);
8450 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8451 union nfsd4_op_u *u)
8453 get_stateid(cstate, &u->free_stateid.fr_stateid);
8457 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8458 union nfsd4_op_u *u)
8460 get_stateid(cstate, &u->setattr.sa_stateid);
8464 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8465 union nfsd4_op_u *u)
8467 get_stateid(cstate, &u->close.cl_stateid);
8471 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8472 union nfsd4_op_u *u)
8474 get_stateid(cstate, &u->locku.lu_stateid);
8478 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8479 union nfsd4_op_u *u)
8481 get_stateid(cstate, &u->read.rd_stateid);
8485 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8486 union nfsd4_op_u *u)
8488 get_stateid(cstate, &u->write.wr_stateid);
8492 * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict
8493 * @rqstp: RPC transaction context
8494 * @inode: file to be checked for a conflict
8495 * @modified: return true if file was modified
8496 * @size: new size of file if modified is true
8498 * This function is called when there is a conflict between a write
8499 * delegation and a change/size GETATTR from another client. The server
8500 * must either use the CB_GETATTR to get the current values of the
8501 * attributes from the client that holds the delegation or recall the
8502 * delegation before replying to the GETATTR. See RFC 8881 section
8505 * Returns 0 if there is no conflict; otherwise an nfs_stat
8509 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct inode *inode,
8510 bool *modified, u64 *size)
8512 struct file_lock_context *ctx;
8513 struct nfs4_delegation *dp;
8514 struct nfs4_cb_fattr *ncf;
8515 struct file_lock *fl;
8522 ctx = locks_inode_context(inode);
8525 spin_lock(&ctx->flc_lock);
8526 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
8527 if (fl->fl_flags == FL_LAYOUT)
8529 if (fl->fl_lmops != &nfsd_lease_mng_ops) {
8531 * non-nfs lease, if it's a lease with F_RDLCK then
8532 * we are done; there isn't any write delegation
8535 if (fl->fl_type == F_RDLCK)
8539 if (fl->fl_type == F_WRLCK) {
8541 if (dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) {
8542 spin_unlock(&ctx->flc_lock);
8546 spin_unlock(&ctx->flc_lock);
8547 nfsd_stats_wdeleg_getattr_inc();
8550 ncf = &dp->dl_cb_fattr;
8551 nfs4_cb_getattr(&dp->dl_cb_fattr);
8552 wait_on_bit(&ncf->ncf_cb_flags, CB_GETATTR_BUSY, TASK_INTERRUPTIBLE);
8553 if (ncf->ncf_cb_status) {
8554 status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
8555 if (status != nfserr_jukebox ||
8556 !nfsd_wait_for_delegreturn(rqstp, inode))
8559 if (!ncf->ncf_file_modified &&
8560 (ncf->ncf_initial_cinfo != ncf->ncf_cb_change ||
8561 ncf->ncf_cur_fsize != ncf->ncf_cb_fsize))
8562 ncf->ncf_file_modified = true;
8563 if (ncf->ncf_file_modified) {
8565 * The server would not update the file's metadata
8566 * with the client's modified size.
8568 attrs.ia_mtime = attrs.ia_ctime = current_time(inode);
8569 attrs.ia_valid = ATTR_MTIME | ATTR_CTIME;
8570 setattr_copy(&nop_mnt_idmap, inode, &attrs);
8571 mark_inode_dirty(inode);
8572 ncf->ncf_cur_fsize = ncf->ncf_cb_fsize;
8573 *size = ncf->ncf_cur_fsize;
8580 spin_unlock(&ctx->flc_lock);