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/nfs_ssc.h>
51 #include "current_stateid.h"
55 #include "filecache.h"
58 #define NFSDDBG_FACILITY NFSDDBG_PROC
60 #define all_ones {{~0,~0},~0}
61 static const stateid_t one_stateid = {
63 .si_opaque = all_ones,
65 static const stateid_t zero_stateid = {
68 static const stateid_t currentstateid = {
71 static const stateid_t close_stateid = {
72 .si_generation = 0xffffffffU,
75 static u64 current_sessionid = 1;
77 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
78 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
79 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
80 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
82 /* forward declarations */
83 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
84 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
85 void nfsd4_end_grace(struct nfsd_net *nn);
86 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
91 * Currently used for the del_recall_lru and file hash table. In an
92 * effort to decrease the scope of the client_mutex, this spinlock may
93 * eventually cover more:
95 static DEFINE_SPINLOCK(state_lock);
97 enum nfsd4_st_mutex_lock_subclass {
98 OPEN_STATEID_MUTEX = 0,
99 LOCK_STATEID_MUTEX = 1,
103 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
104 * the refcount on the open stateid to drop.
106 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
109 * A waitqueue where a writer to clients/#/ctl destroying a client can
110 * wait for cl_rpc_users to drop to 0 and then for the client to be
113 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
115 static struct kmem_cache *client_slab;
116 static struct kmem_cache *openowner_slab;
117 static struct kmem_cache *lockowner_slab;
118 static struct kmem_cache *file_slab;
119 static struct kmem_cache *stateid_slab;
120 static struct kmem_cache *deleg_slab;
121 static struct kmem_cache *odstate_slab;
123 static void free_session(struct nfsd4_session *);
125 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
126 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
128 static struct workqueue_struct *laundry_wq;
130 int nfsd4_create_laundry_wq(void)
134 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
135 if (laundry_wq == NULL)
140 void nfsd4_destroy_laundry_wq(void)
142 destroy_workqueue(laundry_wq);
145 static bool is_session_dead(struct nfsd4_session *ses)
147 return ses->se_flags & NFS4_SESSION_DEAD;
150 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
152 if (atomic_read(&ses->se_ref) > ref_held_by_me)
153 return nfserr_jukebox;
154 ses->se_flags |= NFS4_SESSION_DEAD;
158 static bool is_client_expired(struct nfs4_client *clp)
160 return clp->cl_time == 0;
163 static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn,
164 struct nfs4_client *clp)
166 if (clp->cl_state != NFSD4_ACTIVE)
167 atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0);
170 static __be32 get_client_locked(struct nfs4_client *clp)
172 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
174 lockdep_assert_held(&nn->client_lock);
176 if (is_client_expired(clp))
177 return nfserr_expired;
178 atomic_inc(&clp->cl_rpc_users);
179 nfsd4_dec_courtesy_client_count(nn, clp);
180 clp->cl_state = NFSD4_ACTIVE;
184 /* must be called under the client_lock */
186 renew_client_locked(struct nfs4_client *clp)
188 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
190 if (is_client_expired(clp)) {
192 printk("%s: client (clientid %08x/%08x) already expired\n",
194 clp->cl_clientid.cl_boot,
195 clp->cl_clientid.cl_id);
199 list_move_tail(&clp->cl_lru, &nn->client_lru);
200 clp->cl_time = ktime_get_boottime_seconds();
201 nfsd4_dec_courtesy_client_count(nn, clp);
202 clp->cl_state = NFSD4_ACTIVE;
205 static void put_client_renew_locked(struct nfs4_client *clp)
207 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
209 lockdep_assert_held(&nn->client_lock);
211 if (!atomic_dec_and_test(&clp->cl_rpc_users))
213 if (!is_client_expired(clp))
214 renew_client_locked(clp);
216 wake_up_all(&expiry_wq);
219 static void put_client_renew(struct nfs4_client *clp)
221 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
223 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
225 if (!is_client_expired(clp))
226 renew_client_locked(clp);
228 wake_up_all(&expiry_wq);
229 spin_unlock(&nn->client_lock);
232 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
236 if (is_session_dead(ses))
237 return nfserr_badsession;
238 status = get_client_locked(ses->se_client);
241 atomic_inc(&ses->se_ref);
245 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
247 struct nfs4_client *clp = ses->se_client;
248 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
250 lockdep_assert_held(&nn->client_lock);
252 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
254 put_client_renew_locked(clp);
257 static void nfsd4_put_session(struct nfsd4_session *ses)
259 struct nfs4_client *clp = ses->se_client;
260 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
262 spin_lock(&nn->client_lock);
263 nfsd4_put_session_locked(ses);
264 spin_unlock(&nn->client_lock);
267 static struct nfsd4_blocked_lock *
268 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
271 struct nfsd4_blocked_lock *cur, *found = NULL;
273 spin_lock(&nn->blocked_locks_lock);
274 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
275 if (fh_match(fh, &cur->nbl_fh)) {
276 list_del_init(&cur->nbl_list);
277 WARN_ON(list_empty(&cur->nbl_lru));
278 list_del_init(&cur->nbl_lru);
283 spin_unlock(&nn->blocked_locks_lock);
285 locks_delete_block(&found->nbl_lock);
289 static struct nfsd4_blocked_lock *
290 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
293 struct nfsd4_blocked_lock *nbl;
295 nbl = find_blocked_lock(lo, fh, nn);
297 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
299 INIT_LIST_HEAD(&nbl->nbl_list);
300 INIT_LIST_HEAD(&nbl->nbl_lru);
301 fh_copy_shallow(&nbl->nbl_fh, fh);
302 locks_init_lock(&nbl->nbl_lock);
303 kref_init(&nbl->nbl_kref);
304 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
305 &nfsd4_cb_notify_lock_ops,
306 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
313 free_nbl(struct kref *kref)
315 struct nfsd4_blocked_lock *nbl;
317 nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
322 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
324 locks_delete_block(&nbl->nbl_lock);
325 locks_release_private(&nbl->nbl_lock);
326 kref_put(&nbl->nbl_kref, free_nbl);
330 remove_blocked_locks(struct nfs4_lockowner *lo)
332 struct nfs4_client *clp = lo->lo_owner.so_client;
333 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
334 struct nfsd4_blocked_lock *nbl;
337 /* Dequeue all blocked locks */
338 spin_lock(&nn->blocked_locks_lock);
339 while (!list_empty(&lo->lo_blocked)) {
340 nbl = list_first_entry(&lo->lo_blocked,
341 struct nfsd4_blocked_lock,
343 list_del_init(&nbl->nbl_list);
344 WARN_ON(list_empty(&nbl->nbl_lru));
345 list_move(&nbl->nbl_lru, &reaplist);
347 spin_unlock(&nn->blocked_locks_lock);
350 while (!list_empty(&reaplist)) {
351 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
353 list_del_init(&nbl->nbl_lru);
354 free_blocked_lock(nbl);
359 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
361 struct nfsd4_blocked_lock *nbl = container_of(cb,
362 struct nfsd4_blocked_lock, nbl_cb);
363 locks_delete_block(&nbl->nbl_lock);
367 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
369 trace_nfsd_cb_notify_lock_done(&zero_stateid, task);
372 * Since this is just an optimization, we don't try very hard if it
373 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
374 * just quit trying on anything else.
376 switch (task->tk_status) {
378 rpc_delay(task, 1 * HZ);
386 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
388 struct nfsd4_blocked_lock *nbl = container_of(cb,
389 struct nfsd4_blocked_lock, nbl_cb);
391 free_blocked_lock(nbl);
394 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
395 .prepare = nfsd4_cb_notify_lock_prepare,
396 .done = nfsd4_cb_notify_lock_done,
397 .release = nfsd4_cb_notify_lock_release,
401 * We store the NONE, READ, WRITE, and BOTH bits separately in the
402 * st_{access,deny}_bmap field of the stateid, in order to track not
403 * only what share bits are currently in force, but also what
404 * combinations of share bits previous opens have used. This allows us
405 * to enforce the recommendation in
406 * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
407 * the server return an error if the client attempt to downgrade to a
408 * combination of share bits not explicable by closing some of its
411 * This enforcement is arguably incomplete, since we don't keep
412 * track of access/deny bit combinations; so, e.g., we allow:
414 * OPEN allow read, deny write
415 * OPEN allow both, deny none
416 * DOWNGRADE allow read, deny none
418 * which we should reject.
420 * But you could also argue that our current code is already overkill,
421 * since it only exists to return NFS4ERR_INVAL on incorrect client
425 bmap_to_share_mode(unsigned long bmap)
428 unsigned int access = 0;
430 for (i = 1; i < 4; i++) {
431 if (test_bit(i, &bmap))
437 /* set share access for a given stateid */
439 set_access(u32 access, struct nfs4_ol_stateid *stp)
441 unsigned char mask = 1 << access;
443 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
444 stp->st_access_bmap |= mask;
447 /* clear share access for a given stateid */
449 clear_access(u32 access, struct nfs4_ol_stateid *stp)
451 unsigned char mask = 1 << access;
453 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
454 stp->st_access_bmap &= ~mask;
457 /* test whether a given stateid has access */
459 test_access(u32 access, struct nfs4_ol_stateid *stp)
461 unsigned char mask = 1 << access;
463 return (bool)(stp->st_access_bmap & mask);
466 /* set share deny for a given stateid */
468 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
470 unsigned char mask = 1 << deny;
472 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
473 stp->st_deny_bmap |= mask;
476 /* clear share deny for a given stateid */
478 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
480 unsigned char mask = 1 << deny;
482 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
483 stp->st_deny_bmap &= ~mask;
486 /* test whether a given stateid is denying specific access */
488 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
490 unsigned char mask = 1 << deny;
492 return (bool)(stp->st_deny_bmap & mask);
495 static int nfs4_access_to_omode(u32 access)
497 switch (access & NFS4_SHARE_ACCESS_BOTH) {
498 case NFS4_SHARE_ACCESS_READ:
500 case NFS4_SHARE_ACCESS_WRITE:
502 case NFS4_SHARE_ACCESS_BOTH:
510 access_permit_read(struct nfs4_ol_stateid *stp)
512 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
513 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
514 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
518 access_permit_write(struct nfs4_ol_stateid *stp)
520 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
521 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
524 static inline struct nfs4_stateowner *
525 nfs4_get_stateowner(struct nfs4_stateowner *sop)
527 atomic_inc(&sop->so_count);
532 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
534 return (sop->so_owner.len == owner->len) &&
535 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
538 static struct nfs4_openowner *
539 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
540 struct nfs4_client *clp)
542 struct nfs4_stateowner *so;
544 lockdep_assert_held(&clp->cl_lock);
546 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
548 if (!so->so_is_open_owner)
550 if (same_owner_str(so, &open->op_owner))
551 return openowner(nfs4_get_stateowner(so));
556 static struct nfs4_openowner *
557 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
558 struct nfs4_client *clp)
560 struct nfs4_openowner *oo;
562 spin_lock(&clp->cl_lock);
563 oo = find_openstateowner_str_locked(hashval, open, clp);
564 spin_unlock(&clp->cl_lock);
569 opaque_hashval(const void *ptr, int nbytes)
571 unsigned char *cptr = (unsigned char *) ptr;
581 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
583 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
585 kmem_cache_free(file_slab, fp);
589 put_nfs4_file(struct nfs4_file *fi)
591 might_lock(&state_lock);
593 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
594 hlist_del_rcu(&fi->fi_hash);
595 spin_unlock(&state_lock);
596 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
597 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
598 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
602 static struct nfsd_file *
603 __nfs4_get_fd(struct nfs4_file *f, int oflag)
605 if (f->fi_fds[oflag])
606 return nfsd_file_get(f->fi_fds[oflag]);
610 static struct nfsd_file *
611 find_writeable_file_locked(struct nfs4_file *f)
613 struct nfsd_file *ret;
615 lockdep_assert_held(&f->fi_lock);
617 ret = __nfs4_get_fd(f, O_WRONLY);
619 ret = __nfs4_get_fd(f, O_RDWR);
623 static struct nfsd_file *
624 find_writeable_file(struct nfs4_file *f)
626 struct nfsd_file *ret;
628 spin_lock(&f->fi_lock);
629 ret = find_writeable_file_locked(f);
630 spin_unlock(&f->fi_lock);
635 static struct nfsd_file *
636 find_readable_file_locked(struct nfs4_file *f)
638 struct nfsd_file *ret;
640 lockdep_assert_held(&f->fi_lock);
642 ret = __nfs4_get_fd(f, O_RDONLY);
644 ret = __nfs4_get_fd(f, O_RDWR);
648 static struct nfsd_file *
649 find_readable_file(struct nfs4_file *f)
651 struct nfsd_file *ret;
653 spin_lock(&f->fi_lock);
654 ret = find_readable_file_locked(f);
655 spin_unlock(&f->fi_lock);
661 find_any_file(struct nfs4_file *f)
663 struct nfsd_file *ret;
667 spin_lock(&f->fi_lock);
668 ret = __nfs4_get_fd(f, O_RDWR);
670 ret = __nfs4_get_fd(f, O_WRONLY);
672 ret = __nfs4_get_fd(f, O_RDONLY);
674 spin_unlock(&f->fi_lock);
678 static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
680 struct nfsd_file *ret = NULL;
682 spin_lock(&f->fi_lock);
683 if (f->fi_deleg_file)
684 ret = nfsd_file_get(f->fi_deleg_file);
685 spin_unlock(&f->fi_lock);
689 static atomic_long_t num_delegations;
690 unsigned long max_delegations;
693 * Open owner state (share locks)
696 /* hash tables for lock and open owners */
697 #define OWNER_HASH_BITS 8
698 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
699 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
701 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
705 ret = opaque_hashval(ownername->data, ownername->len);
706 return ret & OWNER_HASH_MASK;
709 /* hash table for nfs4_file */
710 #define FILE_HASH_BITS 8
711 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
713 static unsigned int file_hashval(struct svc_fh *fh)
715 struct inode *inode = d_inode(fh->fh_dentry);
717 /* XXX: why not (here & in file cache) use inode? */
718 return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS);
721 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
724 * Check if courtesy clients have conflicting access and resolve it if possible
726 * access: is op_share_access if share_access is true.
727 * Check if access mode, op_share_access, would conflict with
728 * the current deny mode of the file 'fp'.
729 * access: is op_share_deny if share_access is false.
730 * Check if the deny mode, op_share_deny, would conflict with
731 * current access of the file 'fp'.
732 * stp: skip checking this entry.
733 * new_stp: normal open, not open upgrade.
736 * false - access/deny mode conflict with normal client.
737 * true - no conflict or conflict with courtesy client(s) is resolved.
740 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
741 struct nfs4_ol_stateid *stp, u32 access, bool share_access)
743 struct nfs4_ol_stateid *st;
744 bool resolvable = true;
747 struct nfs4_client *clp;
749 lockdep_assert_held(&fp->fi_lock);
750 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
751 /* ignore lock stateid */
754 if (st == stp && new_stp)
756 /* check file access against deny mode or vice versa */
757 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
758 if (!(access & bmap_to_share_mode(bmap)))
760 clp = st->st_stid.sc_client;
761 if (try_to_expire_client(clp))
767 clp = stp->st_stid.sc_client;
768 nn = net_generic(clp->net, nfsd_net_id);
769 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
775 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
777 lockdep_assert_held(&fp->fi_lock);
779 if (access & NFS4_SHARE_ACCESS_WRITE)
780 atomic_inc(&fp->fi_access[O_WRONLY]);
781 if (access & NFS4_SHARE_ACCESS_READ)
782 atomic_inc(&fp->fi_access[O_RDONLY]);
786 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
788 lockdep_assert_held(&fp->fi_lock);
790 /* Does this access mode make sense? */
791 if (access & ~NFS4_SHARE_ACCESS_BOTH)
794 /* Does it conflict with a deny mode already set? */
795 if ((access & fp->fi_share_deny) != 0)
796 return nfserr_share_denied;
798 __nfs4_file_get_access(fp, access);
802 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
804 /* Common case is that there is no deny mode. */
806 /* Does this deny mode make sense? */
807 if (deny & ~NFS4_SHARE_DENY_BOTH)
810 if ((deny & NFS4_SHARE_DENY_READ) &&
811 atomic_read(&fp->fi_access[O_RDONLY]))
812 return nfserr_share_denied;
814 if ((deny & NFS4_SHARE_DENY_WRITE) &&
815 atomic_read(&fp->fi_access[O_WRONLY]))
816 return nfserr_share_denied;
821 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
823 might_lock(&fp->fi_lock);
825 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
826 struct nfsd_file *f1 = NULL;
827 struct nfsd_file *f2 = NULL;
829 swap(f1, fp->fi_fds[oflag]);
830 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
831 swap(f2, fp->fi_fds[O_RDWR]);
832 spin_unlock(&fp->fi_lock);
840 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
842 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
844 if (access & NFS4_SHARE_ACCESS_WRITE)
845 __nfs4_file_put_access(fp, O_WRONLY);
846 if (access & NFS4_SHARE_ACCESS_READ)
847 __nfs4_file_put_access(fp, O_RDONLY);
851 * Allocate a new open/delegation state counter. This is needed for
852 * pNFS for proper return on close semantics.
854 * Note that we only allocate it for pNFS-enabled exports, otherwise
855 * all pointers to struct nfs4_clnt_odstate are always NULL.
857 static struct nfs4_clnt_odstate *
858 alloc_clnt_odstate(struct nfs4_client *clp)
860 struct nfs4_clnt_odstate *co;
862 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
865 refcount_set(&co->co_odcount, 1);
871 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
873 struct nfs4_file *fp = co->co_file;
875 lockdep_assert_held(&fp->fi_lock);
876 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
880 get_clnt_odstate(struct nfs4_clnt_odstate *co)
883 refcount_inc(&co->co_odcount);
887 put_clnt_odstate(struct nfs4_clnt_odstate *co)
889 struct nfs4_file *fp;
895 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
896 list_del(&co->co_perfile);
897 spin_unlock(&fp->fi_lock);
899 nfsd4_return_all_file_layouts(co->co_client, fp);
900 kmem_cache_free(odstate_slab, co);
904 static struct nfs4_clnt_odstate *
905 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
907 struct nfs4_clnt_odstate *co;
908 struct nfs4_client *cl;
915 spin_lock(&fp->fi_lock);
916 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
917 if (co->co_client == cl) {
918 get_clnt_odstate(co);
924 hash_clnt_odstate_locked(new);
926 spin_unlock(&fp->fi_lock);
930 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
931 void (*sc_free)(struct nfs4_stid *))
933 struct nfs4_stid *stid;
936 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
940 idr_preload(GFP_KERNEL);
941 spin_lock(&cl->cl_lock);
942 /* Reserving 0 for start of file in nfsdfs "states" file: */
943 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
944 spin_unlock(&cl->cl_lock);
949 stid->sc_free = sc_free;
950 stid->sc_client = cl;
951 stid->sc_stateid.si_opaque.so_id = new_id;
952 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
953 /* Will be incremented before return to client: */
954 refcount_set(&stid->sc_count, 1);
955 spin_lock_init(&stid->sc_lock);
956 INIT_LIST_HEAD(&stid->sc_cp_list);
959 * It shouldn't be a problem to reuse an opaque stateid value.
960 * I don't think it is for 4.1. But with 4.0 I worry that, for
961 * example, a stray write retransmission could be accepted by
962 * the server when it should have been rejected. Therefore,
963 * adopt a trick from the sctp code to attempt to maximize the
964 * amount of time until an id is reused, by ensuring they always
965 * "increase" (mod INT_MAX):
969 kmem_cache_free(slab, stid);
974 * Create a unique stateid_t to represent each COPY.
976 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
977 unsigned char cs_type)
981 stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
982 stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
983 stid->cs_type = cs_type;
985 idr_preload(GFP_KERNEL);
986 spin_lock(&nn->s2s_cp_lock);
987 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
988 stid->cs_stid.si_opaque.so_id = new_id;
989 stid->cs_stid.si_generation = 1;
990 spin_unlock(&nn->s2s_cp_lock);
997 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
999 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID);
1002 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
1003 struct nfs4_stid *p_stid)
1005 struct nfs4_cpntf_state *cps;
1007 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
1010 cps->cpntf_time = ktime_get_boottime_seconds();
1011 refcount_set(&cps->cp_stateid.cs_count, 1);
1012 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
1014 spin_lock(&nn->s2s_cp_lock);
1015 list_add(&cps->cp_list, &p_stid->sc_cp_list);
1016 spin_unlock(&nn->s2s_cp_lock);
1023 void nfs4_free_copy_state(struct nfsd4_copy *copy)
1025 struct nfsd_net *nn;
1027 WARN_ON_ONCE(copy->cp_stateid.cs_type != NFS4_COPY_STID);
1028 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
1029 spin_lock(&nn->s2s_cp_lock);
1030 idr_remove(&nn->s2s_cp_stateids,
1031 copy->cp_stateid.cs_stid.si_opaque.so_id);
1032 spin_unlock(&nn->s2s_cp_lock);
1035 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1037 struct nfs4_cpntf_state *cps;
1038 struct nfsd_net *nn;
1040 nn = net_generic(net, nfsd_net_id);
1041 spin_lock(&nn->s2s_cp_lock);
1042 while (!list_empty(&stid->sc_cp_list)) {
1043 cps = list_first_entry(&stid->sc_cp_list,
1044 struct nfs4_cpntf_state, cp_list);
1045 _free_cpntf_state_locked(nn, cps);
1047 spin_unlock(&nn->s2s_cp_lock);
1050 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
1052 struct nfs4_stid *stid;
1054 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
1058 return openlockstateid(stid);
1061 static void nfs4_free_deleg(struct nfs4_stid *stid)
1063 struct nfs4_delegation *dp = delegstateid(stid);
1065 WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
1066 WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
1067 WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
1068 WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
1069 kmem_cache_free(deleg_slab, stid);
1070 atomic_long_dec(&num_delegations);
1074 * When we recall a delegation, we should be careful not to hand it
1075 * out again straight away.
1076 * To ensure this we keep a pair of bloom filters ('new' and 'old')
1077 * in which the filehandles of recalled delegations are "stored".
1078 * If a filehandle appear in either filter, a delegation is blocked.
1079 * When a delegation is recalled, the filehandle is stored in the "new"
1081 * Every 30 seconds we swap the filters and clear the "new" one,
1082 * unless both are empty of course.
1084 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
1085 * low 3 bytes as hash-table indices.
1087 * 'blocked_delegations_lock', which is always taken in block_delegations(),
1088 * is used to manage concurrent access. Testing does not need the lock
1089 * except when swapping the two filters.
1091 static DEFINE_SPINLOCK(blocked_delegations_lock);
1092 static struct bloom_pair {
1093 int entries, old_entries;
1095 int new; /* index into 'set' */
1096 DECLARE_BITMAP(set[2], 256);
1097 } blocked_delegations;
1099 static int delegation_blocked(struct knfsd_fh *fh)
1102 struct bloom_pair *bd = &blocked_delegations;
1104 if (bd->entries == 0)
1106 if (ktime_get_seconds() - bd->swap_time > 30) {
1107 spin_lock(&blocked_delegations_lock);
1108 if (ktime_get_seconds() - bd->swap_time > 30) {
1109 bd->entries -= bd->old_entries;
1110 bd->old_entries = bd->entries;
1111 memset(bd->set[bd->new], 0,
1112 sizeof(bd->set[0]));
1113 bd->new = 1-bd->new;
1114 bd->swap_time = ktime_get_seconds();
1116 spin_unlock(&blocked_delegations_lock);
1118 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1119 if (test_bit(hash&255, bd->set[0]) &&
1120 test_bit((hash>>8)&255, bd->set[0]) &&
1121 test_bit((hash>>16)&255, bd->set[0]))
1124 if (test_bit(hash&255, bd->set[1]) &&
1125 test_bit((hash>>8)&255, bd->set[1]) &&
1126 test_bit((hash>>16)&255, bd->set[1]))
1132 static void block_delegations(struct knfsd_fh *fh)
1135 struct bloom_pair *bd = &blocked_delegations;
1137 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1139 spin_lock(&blocked_delegations_lock);
1140 __set_bit(hash&255, bd->set[bd->new]);
1141 __set_bit((hash>>8)&255, bd->set[bd->new]);
1142 __set_bit((hash>>16)&255, bd->set[bd->new]);
1143 if (bd->entries == 0)
1144 bd->swap_time = ktime_get_seconds();
1146 spin_unlock(&blocked_delegations_lock);
1149 static struct nfs4_delegation *
1150 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1151 struct nfs4_clnt_odstate *odstate)
1153 struct nfs4_delegation *dp;
1156 dprintk("NFSD alloc_init_deleg\n");
1157 n = atomic_long_inc_return(&num_delegations);
1158 if (n < 0 || n > max_delegations)
1160 if (delegation_blocked(&fp->fi_fhandle))
1162 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
1167 * delegation seqid's are never incremented. The 4.1 special
1168 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1169 * 0 anyway just for consistency and use 1:
1171 dp->dl_stid.sc_stateid.si_generation = 1;
1172 INIT_LIST_HEAD(&dp->dl_perfile);
1173 INIT_LIST_HEAD(&dp->dl_perclnt);
1174 INIT_LIST_HEAD(&dp->dl_recall_lru);
1175 dp->dl_clnt_odstate = odstate;
1176 get_clnt_odstate(odstate);
1177 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
1179 dp->dl_recalled = false;
1180 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1181 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1183 dp->dl_stid.sc_file = fp;
1186 atomic_long_dec(&num_delegations);
1191 nfs4_put_stid(struct nfs4_stid *s)
1193 struct nfs4_file *fp = s->sc_file;
1194 struct nfs4_client *clp = s->sc_client;
1196 might_lock(&clp->cl_lock);
1198 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1199 wake_up_all(&close_wq);
1202 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1203 nfs4_free_cpntf_statelist(clp->net, s);
1204 spin_unlock(&clp->cl_lock);
1211 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1213 stateid_t *src = &stid->sc_stateid;
1215 spin_lock(&stid->sc_lock);
1216 if (unlikely(++src->si_generation == 0))
1217 src->si_generation = 1;
1218 memcpy(dst, src, sizeof(*dst));
1219 spin_unlock(&stid->sc_lock);
1222 static void put_deleg_file(struct nfs4_file *fp)
1224 struct nfsd_file *nf = NULL;
1226 spin_lock(&fp->fi_lock);
1227 if (--fp->fi_delegees == 0)
1228 swap(nf, fp->fi_deleg_file);
1229 spin_unlock(&fp->fi_lock);
1235 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1237 struct nfs4_file *fp = dp->dl_stid.sc_file;
1238 struct nfsd_file *nf = fp->fi_deleg_file;
1240 WARN_ON_ONCE(!fp->fi_delegees);
1242 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1246 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1248 put_clnt_odstate(dp->dl_clnt_odstate);
1249 nfs4_unlock_deleg_lease(dp);
1250 nfs4_put_stid(&dp->dl_stid);
1253 void nfs4_unhash_stid(struct nfs4_stid *s)
1259 * nfs4_delegation_exists - Discover if this delegation already exists
1260 * @clp: a pointer to the nfs4_client we're granting a delegation to
1261 * @fp: a pointer to the nfs4_file we're granting a delegation on
1264 * On success: true iff an existing delegation is found
1268 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1270 struct nfs4_delegation *searchdp = NULL;
1271 struct nfs4_client *searchclp = NULL;
1273 lockdep_assert_held(&state_lock);
1274 lockdep_assert_held(&fp->fi_lock);
1276 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1277 searchclp = searchdp->dl_stid.sc_client;
1278 if (clp == searchclp) {
1286 * hash_delegation_locked - Add a delegation to the appropriate lists
1287 * @dp: a pointer to the nfs4_delegation we are adding.
1288 * @fp: a pointer to the nfs4_file we're granting a delegation on
1291 * On success: NULL if the delegation was successfully hashed.
1293 * On error: -EAGAIN if one was previously granted to this
1294 * nfs4_client for this nfs4_file. Delegation is not hashed.
1299 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1301 struct nfs4_client *clp = dp->dl_stid.sc_client;
1303 lockdep_assert_held(&state_lock);
1304 lockdep_assert_held(&fp->fi_lock);
1306 if (nfs4_delegation_exists(clp, fp))
1308 refcount_inc(&dp->dl_stid.sc_count);
1309 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1310 list_add(&dp->dl_perfile, &fp->fi_delegations);
1311 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1315 static bool delegation_hashed(struct nfs4_delegation *dp)
1317 return !(list_empty(&dp->dl_perfile));
1321 unhash_delegation_locked(struct nfs4_delegation *dp)
1323 struct nfs4_file *fp = dp->dl_stid.sc_file;
1325 lockdep_assert_held(&state_lock);
1327 if (!delegation_hashed(dp))
1330 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1331 /* Ensure that deleg break won't try to requeue it */
1333 spin_lock(&fp->fi_lock);
1334 list_del_init(&dp->dl_perclnt);
1335 list_del_init(&dp->dl_recall_lru);
1336 list_del_init(&dp->dl_perfile);
1337 spin_unlock(&fp->fi_lock);
1341 static void destroy_delegation(struct nfs4_delegation *dp)
1345 spin_lock(&state_lock);
1346 unhashed = unhash_delegation_locked(dp);
1347 spin_unlock(&state_lock);
1349 destroy_unhashed_deleg(dp);
1352 static void revoke_delegation(struct nfs4_delegation *dp)
1354 struct nfs4_client *clp = dp->dl_stid.sc_client;
1356 WARN_ON(!list_empty(&dp->dl_recall_lru));
1358 if (clp->cl_minorversion) {
1359 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1360 refcount_inc(&dp->dl_stid.sc_count);
1361 spin_lock(&clp->cl_lock);
1362 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1363 spin_unlock(&clp->cl_lock);
1365 destroy_unhashed_deleg(dp);
1372 static unsigned int clientid_hashval(u32 id)
1374 return id & CLIENT_HASH_MASK;
1377 static unsigned int clientstr_hashval(struct xdr_netobj name)
1379 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1383 * A stateid that had a deny mode associated with it is being released
1384 * or downgraded. Recalculate the deny mode on the file.
1387 recalculate_deny_mode(struct nfs4_file *fp)
1389 struct nfs4_ol_stateid *stp;
1391 spin_lock(&fp->fi_lock);
1392 fp->fi_share_deny = 0;
1393 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1394 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1395 spin_unlock(&fp->fi_lock);
1399 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1402 bool change = false;
1404 for (i = 1; i < 4; i++) {
1405 if ((i & deny) != i) {
1411 /* Recalculate per-file deny mode if there was a change */
1413 recalculate_deny_mode(stp->st_stid.sc_file);
1416 /* release all access and file references for a given stateid */
1418 release_all_access(struct nfs4_ol_stateid *stp)
1421 struct nfs4_file *fp = stp->st_stid.sc_file;
1423 if (fp && stp->st_deny_bmap != 0)
1424 recalculate_deny_mode(fp);
1426 for (i = 1; i < 4; i++) {
1427 if (test_access(i, stp))
1428 nfs4_file_put_access(stp->st_stid.sc_file, i);
1429 clear_access(i, stp);
1433 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1435 kfree(sop->so_owner.data);
1436 sop->so_ops->so_free(sop);
1439 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1441 struct nfs4_client *clp = sop->so_client;
1443 might_lock(&clp->cl_lock);
1445 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1447 sop->so_ops->so_unhash(sop);
1448 spin_unlock(&clp->cl_lock);
1449 nfs4_free_stateowner(sop);
1453 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1455 return list_empty(&stp->st_perfile);
1458 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1460 struct nfs4_file *fp = stp->st_stid.sc_file;
1462 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1464 if (list_empty(&stp->st_perfile))
1467 spin_lock(&fp->fi_lock);
1468 list_del_init(&stp->st_perfile);
1469 spin_unlock(&fp->fi_lock);
1470 list_del(&stp->st_perstateowner);
1474 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1476 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1478 put_clnt_odstate(stp->st_clnt_odstate);
1479 release_all_access(stp);
1480 if (stp->st_stateowner)
1481 nfs4_put_stateowner(stp->st_stateowner);
1482 WARN_ON(!list_empty(&stid->sc_cp_list));
1483 kmem_cache_free(stateid_slab, stid);
1486 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1488 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1489 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1490 struct nfsd_file *nf;
1492 nf = find_any_file(stp->st_stid.sc_file);
1494 get_file(nf->nf_file);
1495 filp_close(nf->nf_file, (fl_owner_t)lo);
1498 nfs4_free_ol_stateid(stid);
1502 * Put the persistent reference to an already unhashed generic stateid, while
1503 * holding the cl_lock. If it's the last reference, then put it onto the
1504 * reaplist for later destruction.
1506 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1507 struct list_head *reaplist)
1509 struct nfs4_stid *s = &stp->st_stid;
1510 struct nfs4_client *clp = s->sc_client;
1512 lockdep_assert_held(&clp->cl_lock);
1514 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1516 if (!refcount_dec_and_test(&s->sc_count)) {
1517 wake_up_all(&close_wq);
1521 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1522 list_add(&stp->st_locks, reaplist);
1525 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1527 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1529 if (!unhash_ol_stateid(stp))
1531 list_del_init(&stp->st_locks);
1532 nfs4_unhash_stid(&stp->st_stid);
1536 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1538 struct nfs4_client *clp = stp->st_stid.sc_client;
1541 spin_lock(&clp->cl_lock);
1542 unhashed = unhash_lock_stateid(stp);
1543 spin_unlock(&clp->cl_lock);
1545 nfs4_put_stid(&stp->st_stid);
1548 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1550 struct nfs4_client *clp = lo->lo_owner.so_client;
1552 lockdep_assert_held(&clp->cl_lock);
1554 list_del_init(&lo->lo_owner.so_strhash);
1558 * Free a list of generic stateids that were collected earlier after being
1562 free_ol_stateid_reaplist(struct list_head *reaplist)
1564 struct nfs4_ol_stateid *stp;
1565 struct nfs4_file *fp;
1569 while (!list_empty(reaplist)) {
1570 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1572 list_del(&stp->st_locks);
1573 fp = stp->st_stid.sc_file;
1574 stp->st_stid.sc_free(&stp->st_stid);
1580 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1581 struct list_head *reaplist)
1583 struct nfs4_ol_stateid *stp;
1585 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1587 while (!list_empty(&open_stp->st_locks)) {
1588 stp = list_entry(open_stp->st_locks.next,
1589 struct nfs4_ol_stateid, st_locks);
1590 WARN_ON(!unhash_lock_stateid(stp));
1591 put_ol_stateid_locked(stp, reaplist);
1595 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1596 struct list_head *reaplist)
1598 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1600 if (!unhash_ol_stateid(stp))
1602 release_open_stateid_locks(stp, reaplist);
1606 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1608 LIST_HEAD(reaplist);
1610 spin_lock(&stp->st_stid.sc_client->cl_lock);
1611 if (unhash_open_stateid(stp, &reaplist))
1612 put_ol_stateid_locked(stp, &reaplist);
1613 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1614 free_ol_stateid_reaplist(&reaplist);
1617 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1619 struct nfs4_client *clp = oo->oo_owner.so_client;
1621 lockdep_assert_held(&clp->cl_lock);
1623 list_del_init(&oo->oo_owner.so_strhash);
1624 list_del_init(&oo->oo_perclient);
1627 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1629 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1631 struct nfs4_ol_stateid *s;
1633 spin_lock(&nn->client_lock);
1634 s = oo->oo_last_closed_stid;
1636 list_del_init(&oo->oo_close_lru);
1637 oo->oo_last_closed_stid = NULL;
1639 spin_unlock(&nn->client_lock);
1641 nfs4_put_stid(&s->st_stid);
1644 static void release_openowner(struct nfs4_openowner *oo)
1646 struct nfs4_ol_stateid *stp;
1647 struct nfs4_client *clp = oo->oo_owner.so_client;
1648 struct list_head reaplist;
1650 INIT_LIST_HEAD(&reaplist);
1652 spin_lock(&clp->cl_lock);
1653 unhash_openowner_locked(oo);
1654 while (!list_empty(&oo->oo_owner.so_stateids)) {
1655 stp = list_first_entry(&oo->oo_owner.so_stateids,
1656 struct nfs4_ol_stateid, st_perstateowner);
1657 if (unhash_open_stateid(stp, &reaplist))
1658 put_ol_stateid_locked(stp, &reaplist);
1660 spin_unlock(&clp->cl_lock);
1661 free_ol_stateid_reaplist(&reaplist);
1662 release_last_closed_stateid(oo);
1663 nfs4_put_stateowner(&oo->oo_owner);
1667 hash_sessionid(struct nfs4_sessionid *sessionid)
1669 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1671 return sid->sequence % SESSION_HASH_SIZE;
1674 #ifdef CONFIG_SUNRPC_DEBUG
1676 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1678 u32 *ptr = (u32 *)(&sessionid->data[0]);
1679 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1683 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1689 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1690 * won't be used for replay.
1692 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1694 struct nfs4_stateowner *so = cstate->replay_owner;
1696 if (nfserr == nfserr_replay_me)
1699 if (!seqid_mutating_err(ntohl(nfserr))) {
1700 nfsd4_cstate_clear_replay(cstate);
1705 if (so->so_is_open_owner)
1706 release_last_closed_stateid(openowner(so));
1712 gen_sessionid(struct nfsd4_session *ses)
1714 struct nfs4_client *clp = ses->se_client;
1715 struct nfsd4_sessionid *sid;
1717 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1718 sid->clientid = clp->cl_clientid;
1719 sid->sequence = current_sessionid++;
1724 * The protocol defines ca_maxresponssize_cached to include the size of
1725 * the rpc header, but all we need to cache is the data starting after
1726 * the end of the initial SEQUENCE operation--the rest we regenerate
1727 * each time. Therefore we can advertise a ca_maxresponssize_cached
1728 * value that is the number of bytes in our cache plus a few additional
1729 * bytes. In order to stay on the safe side, and not promise more than
1730 * we can cache, those additional bytes must be the minimum possible: 24
1731 * bytes of rpc header (xid through accept state, with AUTH_NULL
1732 * verifier), 12 for the compound header (with zero-length tag), and 44
1733 * for the SEQUENCE op response:
1735 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1738 free_session_slots(struct nfsd4_session *ses)
1742 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1743 free_svc_cred(&ses->se_slots[i]->sl_cred);
1744 kfree(ses->se_slots[i]);
1749 * We don't actually need to cache the rpc and session headers, so we
1750 * can allocate a little less for each slot:
1752 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1756 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1759 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1760 return size + sizeof(struct nfsd4_slot);
1764 * XXX: If we run out of reserved DRC memory we could (up to a point)
1765 * re-negotiate active sessions and reduce their slot usage to make
1766 * room for new connections. For now we just fail the create session.
1768 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1770 u32 slotsize = slot_bytes(ca);
1771 u32 num = ca->maxreqs;
1772 unsigned long avail, total_avail;
1773 unsigned int scale_factor;
1775 spin_lock(&nfsd_drc_lock);
1776 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1777 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1779 /* We have handed out more space than we chose in
1780 * set_max_drc() to allow. That isn't really a
1781 * problem as long as that doesn't make us think we
1782 * have lots more due to integer overflow.
1785 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1787 * Never use more than a fraction of the remaining memory,
1788 * unless it's the only way to give this client a slot.
1789 * The chosen fraction is either 1/8 or 1/number of threads,
1790 * whichever is smaller. This ensures there are adequate
1791 * slots to support multiple clients per thread.
1792 * Give the client one slot even if that would require
1793 * over-allocation--it is better than failure.
1795 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1797 avail = clamp_t(unsigned long, avail, slotsize,
1798 total_avail/scale_factor);
1799 num = min_t(int, num, avail / slotsize);
1800 num = max_t(int, num, 1);
1801 nfsd_drc_mem_used += num * slotsize;
1802 spin_unlock(&nfsd_drc_lock);
1807 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1809 int slotsize = slot_bytes(ca);
1811 spin_lock(&nfsd_drc_lock);
1812 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1813 spin_unlock(&nfsd_drc_lock);
1816 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1817 struct nfsd4_channel_attrs *battrs)
1819 int numslots = fattrs->maxreqs;
1820 int slotsize = slot_bytes(fattrs);
1821 struct nfsd4_session *new;
1824 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1825 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1826 mem = numslots * sizeof(struct nfsd4_slot *);
1828 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1831 /* allocate each struct nfsd4_slot and data cache in one piece */
1832 for (i = 0; i < numslots; i++) {
1833 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1834 if (!new->se_slots[i])
1838 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1839 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1844 kfree(new->se_slots[i]);
1849 static void free_conn(struct nfsd4_conn *c)
1851 svc_xprt_put(c->cn_xprt);
1855 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1857 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1858 struct nfs4_client *clp = c->cn_session->se_client;
1860 trace_nfsd_cb_lost(clp);
1862 spin_lock(&clp->cl_lock);
1863 if (!list_empty(&c->cn_persession)) {
1864 list_del(&c->cn_persession);
1867 nfsd4_probe_callback(clp);
1868 spin_unlock(&clp->cl_lock);
1871 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1873 struct nfsd4_conn *conn;
1875 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1878 svc_xprt_get(rqstp->rq_xprt);
1879 conn->cn_xprt = rqstp->rq_xprt;
1880 conn->cn_flags = flags;
1881 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1885 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1887 conn->cn_session = ses;
1888 list_add(&conn->cn_persession, &ses->se_conns);
1891 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1893 struct nfs4_client *clp = ses->se_client;
1895 spin_lock(&clp->cl_lock);
1896 __nfsd4_hash_conn(conn, ses);
1897 spin_unlock(&clp->cl_lock);
1900 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1902 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1903 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1906 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1910 nfsd4_hash_conn(conn, ses);
1911 ret = nfsd4_register_conn(conn);
1913 /* oops; xprt is already down: */
1914 nfsd4_conn_lost(&conn->cn_xpt_user);
1915 /* We may have gained or lost a callback channel: */
1916 nfsd4_probe_callback_sync(ses->se_client);
1919 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1921 u32 dir = NFS4_CDFC4_FORE;
1923 if (cses->flags & SESSION4_BACK_CHAN)
1924 dir |= NFS4_CDFC4_BACK;
1925 return alloc_conn(rqstp, dir);
1928 /* must be called under client_lock */
1929 static void nfsd4_del_conns(struct nfsd4_session *s)
1931 struct nfs4_client *clp = s->se_client;
1932 struct nfsd4_conn *c;
1934 spin_lock(&clp->cl_lock);
1935 while (!list_empty(&s->se_conns)) {
1936 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1937 list_del_init(&c->cn_persession);
1938 spin_unlock(&clp->cl_lock);
1940 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1943 spin_lock(&clp->cl_lock);
1945 spin_unlock(&clp->cl_lock);
1948 static void __free_session(struct nfsd4_session *ses)
1950 free_session_slots(ses);
1954 static void free_session(struct nfsd4_session *ses)
1956 nfsd4_del_conns(ses);
1957 nfsd4_put_drc_mem(&ses->se_fchannel);
1958 __free_session(ses);
1961 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1964 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1966 new->se_client = clp;
1969 INIT_LIST_HEAD(&new->se_conns);
1971 new->se_cb_seq_nr = 1;
1972 new->se_flags = cses->flags;
1973 new->se_cb_prog = cses->callback_prog;
1974 new->se_cb_sec = cses->cb_sec;
1975 atomic_set(&new->se_ref, 0);
1976 idx = hash_sessionid(&new->se_sessionid);
1977 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1978 spin_lock(&clp->cl_lock);
1979 list_add(&new->se_perclnt, &clp->cl_sessions);
1980 spin_unlock(&clp->cl_lock);
1983 struct sockaddr *sa = svc_addr(rqstp);
1985 * This is a little silly; with sessions there's no real
1986 * use for the callback address. Use the peer address
1987 * as a reasonable default for now, but consider fixing
1988 * the rpc client not to require an address in the
1991 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1992 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1996 /* caller must hold client_lock */
1997 static struct nfsd4_session *
1998 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2000 struct nfsd4_session *elem;
2002 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2004 lockdep_assert_held(&nn->client_lock);
2006 dump_sessionid(__func__, sessionid);
2007 idx = hash_sessionid(sessionid);
2008 /* Search in the appropriate list */
2009 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2010 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2011 NFS4_MAX_SESSIONID_LEN)) {
2016 dprintk("%s: session not found\n", __func__);
2020 static struct nfsd4_session *
2021 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2024 struct nfsd4_session *session;
2025 __be32 status = nfserr_badsession;
2027 session = __find_in_sessionid_hashtbl(sessionid, net);
2030 status = nfsd4_get_session_locked(session);
2038 /* caller must hold client_lock */
2040 unhash_session(struct nfsd4_session *ses)
2042 struct nfs4_client *clp = ses->se_client;
2043 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2045 lockdep_assert_held(&nn->client_lock);
2047 list_del(&ses->se_hash);
2048 spin_lock(&ses->se_client->cl_lock);
2049 list_del(&ses->se_perclnt);
2050 spin_unlock(&ses->se_client->cl_lock);
2053 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2055 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2058 * We're assuming the clid was not given out from a boot
2059 * precisely 2^32 (about 136 years) before this one. That seems
2060 * a safe assumption:
2062 if (clid->cl_boot == (u32)nn->boot_time)
2064 trace_nfsd_clid_stale(clid);
2069 * XXX Should we use a slab cache ?
2070 * This type of memory management is somewhat inefficient, but we use it
2071 * anyway since SETCLIENTID is not a common operation.
2073 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2074 struct nfsd_net *nn)
2076 struct nfs4_client *clp;
2079 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
2080 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2083 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2086 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2087 if (clp->cl_name.data == NULL)
2089 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2090 sizeof(struct list_head),
2092 if (!clp->cl_ownerstr_hashtbl)
2093 goto err_no_hashtbl;
2094 for (i = 0; i < OWNER_HASH_SIZE; i++)
2095 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2096 INIT_LIST_HEAD(&clp->cl_sessions);
2097 idr_init(&clp->cl_stateids);
2098 atomic_set(&clp->cl_rpc_users, 0);
2099 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2100 clp->cl_state = NFSD4_ACTIVE;
2101 atomic_inc(&nn->nfs4_client_count);
2102 atomic_set(&clp->cl_delegs_in_recall, 0);
2103 INIT_LIST_HEAD(&clp->cl_idhash);
2104 INIT_LIST_HEAD(&clp->cl_openowners);
2105 INIT_LIST_HEAD(&clp->cl_delegations);
2106 INIT_LIST_HEAD(&clp->cl_lru);
2107 INIT_LIST_HEAD(&clp->cl_revoked);
2108 #ifdef CONFIG_NFSD_PNFS
2109 INIT_LIST_HEAD(&clp->cl_lo_states);
2111 INIT_LIST_HEAD(&clp->async_copies);
2112 spin_lock_init(&clp->async_lock);
2113 spin_lock_init(&clp->cl_lock);
2114 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2117 kfree(clp->cl_name.data);
2119 kmem_cache_free(client_slab, clp);
2123 static void __free_client(struct kref *k)
2125 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2126 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2128 free_svc_cred(&clp->cl_cred);
2129 kfree(clp->cl_ownerstr_hashtbl);
2130 kfree(clp->cl_name.data);
2131 kfree(clp->cl_nii_domain.data);
2132 kfree(clp->cl_nii_name.data);
2133 idr_destroy(&clp->cl_stateids);
2134 kmem_cache_free(client_slab, clp);
2137 static void drop_client(struct nfs4_client *clp)
2139 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2143 free_client(struct nfs4_client *clp)
2145 while (!list_empty(&clp->cl_sessions)) {
2146 struct nfsd4_session *ses;
2147 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2149 list_del(&ses->se_perclnt);
2150 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2153 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2154 if (clp->cl_nfsd_dentry) {
2155 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2156 clp->cl_nfsd_dentry = NULL;
2157 wake_up_all(&expiry_wq);
2162 /* must be called under the client_lock */
2164 unhash_client_locked(struct nfs4_client *clp)
2166 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2167 struct nfsd4_session *ses;
2169 lockdep_assert_held(&nn->client_lock);
2171 /* Mark the client as expired! */
2173 /* Make it invisible */
2174 if (!list_empty(&clp->cl_idhash)) {
2175 list_del_init(&clp->cl_idhash);
2176 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2177 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2179 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2181 list_del_init(&clp->cl_lru);
2182 spin_lock(&clp->cl_lock);
2183 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2184 list_del_init(&ses->se_hash);
2185 spin_unlock(&clp->cl_lock);
2189 unhash_client(struct nfs4_client *clp)
2191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2193 spin_lock(&nn->client_lock);
2194 unhash_client_locked(clp);
2195 spin_unlock(&nn->client_lock);
2198 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2200 if (atomic_read(&clp->cl_rpc_users))
2201 return nfserr_jukebox;
2202 unhash_client_locked(clp);
2207 __destroy_client(struct nfs4_client *clp)
2209 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2211 struct nfs4_openowner *oo;
2212 struct nfs4_delegation *dp;
2213 struct list_head reaplist;
2215 INIT_LIST_HEAD(&reaplist);
2216 spin_lock(&state_lock);
2217 while (!list_empty(&clp->cl_delegations)) {
2218 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2219 WARN_ON(!unhash_delegation_locked(dp));
2220 list_add(&dp->dl_recall_lru, &reaplist);
2222 spin_unlock(&state_lock);
2223 while (!list_empty(&reaplist)) {
2224 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2225 list_del_init(&dp->dl_recall_lru);
2226 destroy_unhashed_deleg(dp);
2228 while (!list_empty(&clp->cl_revoked)) {
2229 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2230 list_del_init(&dp->dl_recall_lru);
2231 nfs4_put_stid(&dp->dl_stid);
2233 while (!list_empty(&clp->cl_openowners)) {
2234 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2235 nfs4_get_stateowner(&oo->oo_owner);
2236 release_openowner(oo);
2238 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2239 struct nfs4_stateowner *so, *tmp;
2241 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2243 /* Should be no openowners at this point */
2244 WARN_ON_ONCE(so->so_is_open_owner);
2245 remove_blocked_locks(lockowner(so));
2248 nfsd4_return_all_client_layouts(clp);
2249 nfsd4_shutdown_copy(clp);
2250 nfsd4_shutdown_callback(clp);
2251 if (clp->cl_cb_conn.cb_xprt)
2252 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2253 atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2254 nfsd4_dec_courtesy_client_count(nn, clp);
2256 wake_up_all(&expiry_wq);
2260 destroy_client(struct nfs4_client *clp)
2263 __destroy_client(clp);
2266 static void inc_reclaim_complete(struct nfs4_client *clp)
2268 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2270 if (!nn->track_reclaim_completes)
2272 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2274 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2275 nn->reclaim_str_hashtbl_size) {
2276 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2278 nfsd4_end_grace(nn);
2282 static void expire_client(struct nfs4_client *clp)
2285 nfsd4_client_record_remove(clp);
2286 __destroy_client(clp);
2289 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2291 memcpy(target->cl_verifier.data, source->data,
2292 sizeof(target->cl_verifier.data));
2295 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2297 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2298 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2301 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2303 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2304 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2306 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2307 if ((source->cr_principal && !target->cr_principal) ||
2308 (source->cr_raw_principal && !target->cr_raw_principal) ||
2309 (source->cr_targ_princ && !target->cr_targ_princ))
2312 target->cr_flavor = source->cr_flavor;
2313 target->cr_uid = source->cr_uid;
2314 target->cr_gid = source->cr_gid;
2315 target->cr_group_info = source->cr_group_info;
2316 get_group_info(target->cr_group_info);
2317 target->cr_gss_mech = source->cr_gss_mech;
2318 if (source->cr_gss_mech)
2319 gss_mech_get(source->cr_gss_mech);
2324 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2326 if (o1->len < o2->len)
2328 if (o1->len > o2->len)
2330 return memcmp(o1->data, o2->data, o1->len);
2334 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2336 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2340 same_clid(clientid_t *cl1, clientid_t *cl2)
2342 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2345 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2349 if (g1->ngroups != g2->ngroups)
2351 for (i=0; i<g1->ngroups; i++)
2352 if (!gid_eq(g1->gid[i], g2->gid[i]))
2358 * RFC 3530 language requires clid_inuse be returned when the
2359 * "principal" associated with a requests differs from that previously
2360 * used. We use uid, gid's, and gss principal string as our best
2361 * approximation. We also don't want to allow non-gss use of a client
2362 * established using gss: in theory cr_principal should catch that
2363 * change, but in practice cr_principal can be null even in the gss case
2364 * since gssd doesn't always pass down a principal string.
2366 static bool is_gss_cred(struct svc_cred *cr)
2368 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2369 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2374 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2376 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2377 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2378 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2379 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2381 /* XXX: check that cr_targ_princ fields match ? */
2382 if (cr1->cr_principal == cr2->cr_principal)
2384 if (!cr1->cr_principal || !cr2->cr_principal)
2386 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2389 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2391 struct svc_cred *cr = &rqstp->rq_cred;
2394 if (!cr->cr_gss_mech)
2396 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2397 return service == RPC_GSS_SVC_INTEGRITY ||
2398 service == RPC_GSS_SVC_PRIVACY;
2401 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2403 struct svc_cred *cr = &rqstp->rq_cred;
2405 if (!cl->cl_mach_cred)
2407 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2409 if (!svc_rqst_integrity_protected(rqstp))
2411 if (cl->cl_cred.cr_raw_principal)
2412 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2413 cr->cr_raw_principal);
2414 if (!cr->cr_principal)
2416 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2419 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2424 * This is opaque to client, so no need to byte-swap. Use
2425 * __force to keep sparse happy
2427 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2428 verf[1] = (__force __be32)nn->clverifier_counter++;
2429 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2432 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2434 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2435 clp->cl_clientid.cl_id = nn->clientid_counter++;
2436 gen_confirm(clp, nn);
2439 static struct nfs4_stid *
2440 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2442 struct nfs4_stid *ret;
2444 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2445 if (!ret || !ret->sc_type)
2450 static struct nfs4_stid *
2451 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2453 struct nfs4_stid *s;
2455 spin_lock(&cl->cl_lock);
2456 s = find_stateid_locked(cl, t);
2458 if (typemask & s->sc_type)
2459 refcount_inc(&s->sc_count);
2463 spin_unlock(&cl->cl_lock);
2467 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2469 struct nfsdfs_client *nc;
2470 nc = get_nfsdfs_client(inode);
2473 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2476 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2478 seq_printf(m, "\"");
2479 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2480 seq_printf(m, "\"");
2483 static const char *cb_state2str(int state)
2488 case NFSD4_CB_UNKNOWN:
2492 case NFSD4_CB_FAULT:
2498 static int client_info_show(struct seq_file *m, void *v)
2500 struct inode *inode = file_inode(m->file);
2501 struct nfs4_client *clp;
2504 clp = get_nfsdfs_clp(inode);
2507 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2508 seq_printf(m, "clientid: 0x%llx\n", clid);
2509 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2511 if (clp->cl_state == NFSD4_COURTESY)
2512 seq_puts(m, "status: courtesy\n");
2513 else if (clp->cl_state == NFSD4_EXPIRABLE)
2514 seq_puts(m, "status: expirable\n");
2515 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2516 seq_puts(m, "status: confirmed\n");
2518 seq_puts(m, "status: unconfirmed\n");
2519 seq_printf(m, "seconds from last renew: %lld\n",
2520 ktime_get_boottime_seconds() - clp->cl_time);
2521 seq_printf(m, "name: ");
2522 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2523 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2524 if (clp->cl_nii_domain.data) {
2525 seq_printf(m, "Implementation domain: ");
2526 seq_quote_mem(m, clp->cl_nii_domain.data,
2527 clp->cl_nii_domain.len);
2528 seq_printf(m, "\nImplementation name: ");
2529 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2530 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2531 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2533 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2534 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
2540 DEFINE_SHOW_ATTRIBUTE(client_info);
2542 static void *states_start(struct seq_file *s, loff_t *pos)
2543 __acquires(&clp->cl_lock)
2545 struct nfs4_client *clp = s->private;
2546 unsigned long id = *pos;
2549 spin_lock(&clp->cl_lock);
2550 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2555 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2557 struct nfs4_client *clp = s->private;
2558 unsigned long id = *pos;
2563 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2568 static void states_stop(struct seq_file *s, void *v)
2569 __releases(&clp->cl_lock)
2571 struct nfs4_client *clp = s->private;
2573 spin_unlock(&clp->cl_lock);
2576 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2578 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2581 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2583 struct inode *inode = file_inode(f->nf_file);
2585 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2586 MAJOR(inode->i_sb->s_dev),
2587 MINOR(inode->i_sb->s_dev),
2591 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2593 seq_printf(s, "owner: ");
2594 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2597 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2599 seq_printf(s, "0x%.8x", stid->si_generation);
2600 seq_printf(s, "%12phN", &stid->si_opaque);
2603 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2605 struct nfs4_ol_stateid *ols;
2606 struct nfs4_file *nf;
2607 struct nfsd_file *file;
2608 struct nfs4_stateowner *oo;
2609 unsigned int access, deny;
2611 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2612 return 0; /* XXX: or SEQ_SKIP? */
2613 ols = openlockstateid(st);
2614 oo = ols->st_stateowner;
2616 file = find_any_file(nf);
2620 seq_printf(s, "- ");
2621 nfs4_show_stateid(s, &st->sc_stateid);
2622 seq_printf(s, ": { type: open, ");
2624 access = bmap_to_share_mode(ols->st_access_bmap);
2625 deny = bmap_to_share_mode(ols->st_deny_bmap);
2627 seq_printf(s, "access: %s%s, ",
2628 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2629 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2630 seq_printf(s, "deny: %s%s, ",
2631 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2632 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2634 nfs4_show_superblock(s, file);
2635 seq_printf(s, ", ");
2636 nfs4_show_fname(s, file);
2637 seq_printf(s, ", ");
2638 nfs4_show_owner(s, oo);
2639 seq_printf(s, " }\n");
2640 nfsd_file_put(file);
2645 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2647 struct nfs4_ol_stateid *ols;
2648 struct nfs4_file *nf;
2649 struct nfsd_file *file;
2650 struct nfs4_stateowner *oo;
2652 ols = openlockstateid(st);
2653 oo = ols->st_stateowner;
2655 file = find_any_file(nf);
2659 seq_printf(s, "- ");
2660 nfs4_show_stateid(s, &st->sc_stateid);
2661 seq_printf(s, ": { type: lock, ");
2664 * Note: a lock stateid isn't really the same thing as a lock,
2665 * it's the locking state held by one owner on a file, and there
2666 * may be multiple (or no) lock ranges associated with it.
2667 * (Same for the matter is true of open stateids.)
2670 nfs4_show_superblock(s, file);
2671 /* XXX: open stateid? */
2672 seq_printf(s, ", ");
2673 nfs4_show_fname(s, file);
2674 seq_printf(s, ", ");
2675 nfs4_show_owner(s, oo);
2676 seq_printf(s, " }\n");
2677 nfsd_file_put(file);
2682 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2684 struct nfs4_delegation *ds;
2685 struct nfs4_file *nf;
2686 struct nfsd_file *file;
2688 ds = delegstateid(st);
2690 file = find_deleg_file(nf);
2694 seq_printf(s, "- ");
2695 nfs4_show_stateid(s, &st->sc_stateid);
2696 seq_printf(s, ": { type: deleg, ");
2698 /* Kinda dead code as long as we only support read delegs: */
2699 seq_printf(s, "access: %s, ",
2700 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2702 /* XXX: lease time, whether it's being recalled. */
2704 nfs4_show_superblock(s, file);
2705 seq_printf(s, ", ");
2706 nfs4_show_fname(s, file);
2707 seq_printf(s, " }\n");
2708 nfsd_file_put(file);
2713 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2715 struct nfs4_layout_stateid *ls;
2716 struct nfsd_file *file;
2718 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2721 seq_printf(s, "- ");
2722 nfs4_show_stateid(s, &st->sc_stateid);
2723 seq_printf(s, ": { type: layout, ");
2725 /* XXX: What else would be useful? */
2727 nfs4_show_superblock(s, file);
2728 seq_printf(s, ", ");
2729 nfs4_show_fname(s, file);
2730 seq_printf(s, " }\n");
2735 static int states_show(struct seq_file *s, void *v)
2737 struct nfs4_stid *st = v;
2739 switch (st->sc_type) {
2740 case NFS4_OPEN_STID:
2741 return nfs4_show_open(s, st);
2742 case NFS4_LOCK_STID:
2743 return nfs4_show_lock(s, st);
2744 case NFS4_DELEG_STID:
2745 return nfs4_show_deleg(s, st);
2746 case NFS4_LAYOUT_STID:
2747 return nfs4_show_layout(s, st);
2749 return 0; /* XXX: or SEQ_SKIP? */
2751 /* XXX: copy stateids? */
2754 static struct seq_operations states_seq_ops = {
2755 .start = states_start,
2756 .next = states_next,
2757 .stop = states_stop,
2761 static int client_states_open(struct inode *inode, struct file *file)
2764 struct nfs4_client *clp;
2767 clp = get_nfsdfs_clp(inode);
2771 ret = seq_open(file, &states_seq_ops);
2774 s = file->private_data;
2779 static int client_opens_release(struct inode *inode, struct file *file)
2781 struct seq_file *m = file->private_data;
2782 struct nfs4_client *clp = m->private;
2784 /* XXX: alternatively, we could get/drop in seq start/stop */
2789 static const struct file_operations client_states_fops = {
2790 .open = client_states_open,
2792 .llseek = seq_lseek,
2793 .release = client_opens_release,
2797 * Normally we refuse to destroy clients that are in use, but here the
2798 * administrator is telling us to just do it. We also want to wait
2799 * so the caller has a guarantee that the client's locks are gone by
2800 * the time the write returns:
2802 static void force_expire_client(struct nfs4_client *clp)
2804 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2805 bool already_expired;
2807 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2809 spin_lock(&nn->client_lock);
2811 spin_unlock(&nn->client_lock);
2813 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2814 spin_lock(&nn->client_lock);
2815 already_expired = list_empty(&clp->cl_lru);
2816 if (!already_expired)
2817 unhash_client_locked(clp);
2818 spin_unlock(&nn->client_lock);
2820 if (!already_expired)
2823 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2826 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2827 size_t size, loff_t *pos)
2830 struct nfs4_client *clp;
2832 data = simple_transaction_get(file, buf, size);
2834 return PTR_ERR(data);
2835 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2837 clp = get_nfsdfs_clp(file_inode(file));
2840 force_expire_client(clp);
2845 static const struct file_operations client_ctl_fops = {
2846 .write = client_ctl_write,
2847 .release = simple_transaction_release,
2850 static const struct tree_descr client_files[] = {
2851 [0] = {"info", &client_info_fops, S_IRUSR},
2852 [1] = {"states", &client_states_fops, S_IRUSR},
2853 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
2857 static struct nfs4_client *create_client(struct xdr_netobj name,
2858 struct svc_rqst *rqstp, nfs4_verifier *verf)
2860 struct nfs4_client *clp;
2861 struct sockaddr *sa = svc_addr(rqstp);
2863 struct net *net = SVC_NET(rqstp);
2864 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2865 struct dentry *dentries[ARRAY_SIZE(client_files)];
2867 clp = alloc_client(name, nn);
2871 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2877 kref_init(&clp->cl_nfsdfs.cl_ref);
2878 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2879 clp->cl_time = ktime_get_boottime_seconds();
2880 clear_bit(0, &clp->cl_cb_slot_busy);
2881 copy_verf(clp, verf);
2882 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2883 clp->cl_cb_session = NULL;
2885 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2886 nn, &clp->cl_nfsdfs,
2887 clp->cl_clientid.cl_id - nn->clientid_base,
2888 client_files, dentries);
2889 clp->cl_nfsd_info_dentry = dentries[0];
2890 if (!clp->cl_nfsd_dentry) {
2898 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2900 struct rb_node **new = &(root->rb_node), *parent = NULL;
2901 struct nfs4_client *clp;
2904 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2907 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2908 new = &((*new)->rb_left);
2910 new = &((*new)->rb_right);
2913 rb_link_node(&new_clp->cl_namenode, parent, new);
2914 rb_insert_color(&new_clp->cl_namenode, root);
2917 static struct nfs4_client *
2918 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2921 struct rb_node *node = root->rb_node;
2922 struct nfs4_client *clp;
2925 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2926 cmp = compare_blob(&clp->cl_name, name);
2928 node = node->rb_left;
2930 node = node->rb_right;
2938 add_to_unconfirmed(struct nfs4_client *clp)
2940 unsigned int idhashval;
2941 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2943 lockdep_assert_held(&nn->client_lock);
2945 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2946 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2947 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2948 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2949 renew_client_locked(clp);
2953 move_to_confirmed(struct nfs4_client *clp)
2955 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2956 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2958 lockdep_assert_held(&nn->client_lock);
2960 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2961 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2962 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2963 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2964 trace_nfsd_clid_confirmed(&clp->cl_clientid);
2965 renew_client_locked(clp);
2968 static struct nfs4_client *
2969 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2971 struct nfs4_client *clp;
2972 unsigned int idhashval = clientid_hashval(clid->cl_id);
2974 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2975 if (same_clid(&clp->cl_clientid, clid)) {
2976 if ((bool)clp->cl_minorversion != sessions)
2978 renew_client_locked(clp);
2985 static struct nfs4_client *
2986 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2988 struct list_head *tbl = nn->conf_id_hashtbl;
2990 lockdep_assert_held(&nn->client_lock);
2991 return find_client_in_id_table(tbl, clid, sessions);
2994 static struct nfs4_client *
2995 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2997 struct list_head *tbl = nn->unconf_id_hashtbl;
2999 lockdep_assert_held(&nn->client_lock);
3000 return find_client_in_id_table(tbl, clid, sessions);
3003 static bool clp_used_exchangeid(struct nfs4_client *clp)
3005 return clp->cl_exchange_flags != 0;
3008 static struct nfs4_client *
3009 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3011 lockdep_assert_held(&nn->client_lock);
3012 return find_clp_in_name_tree(name, &nn->conf_name_tree);
3015 static struct nfs4_client *
3016 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3018 lockdep_assert_held(&nn->client_lock);
3019 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3023 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3025 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3026 struct sockaddr *sa = svc_addr(rqstp);
3027 u32 scopeid = rpc_get_scope_id(sa);
3028 unsigned short expected_family;
3030 /* Currently, we only support tcp and tcp6 for the callback channel */
3031 if (se->se_callback_netid_len == 3 &&
3032 !memcmp(se->se_callback_netid_val, "tcp", 3))
3033 expected_family = AF_INET;
3034 else if (se->se_callback_netid_len == 4 &&
3035 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3036 expected_family = AF_INET6;
3040 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3041 se->se_callback_addr_len,
3042 (struct sockaddr *)&conn->cb_addr,
3043 sizeof(conn->cb_addr));
3045 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3048 if (conn->cb_addr.ss_family == AF_INET6)
3049 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3051 conn->cb_prog = se->se_callback_prog;
3052 conn->cb_ident = se->se_callback_ident;
3053 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3054 trace_nfsd_cb_args(clp, conn);
3057 conn->cb_addr.ss_family = AF_UNSPEC;
3058 conn->cb_addrlen = 0;
3059 trace_nfsd_cb_nodelegs(clp);
3064 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3067 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3069 struct xdr_buf *buf = resp->xdr->buf;
3070 struct nfsd4_slot *slot = resp->cstate.slot;
3073 dprintk("--> %s slot %p\n", __func__, slot);
3075 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3076 slot->sl_opcnt = resp->opcnt;
3077 slot->sl_status = resp->cstate.status;
3078 free_svc_cred(&slot->sl_cred);
3079 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3081 if (!nfsd4_cache_this(resp)) {
3082 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3085 slot->sl_flags |= NFSD4_SLOT_CACHED;
3087 base = resp->cstate.data_offset;
3088 slot->sl_datalen = buf->len - base;
3089 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3090 WARN(1, "%s: sessions DRC could not cache compound\n",
3096 * Encode the replay sequence operation from the slot values.
3097 * If cachethis is FALSE encode the uncached rep error on the next
3098 * operation which sets resp->p and increments resp->opcnt for
3099 * nfs4svc_encode_compoundres.
3103 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3104 struct nfsd4_compoundres *resp)
3106 struct nfsd4_op *op;
3107 struct nfsd4_slot *slot = resp->cstate.slot;
3109 /* Encode the replayed sequence operation */
3110 op = &args->ops[resp->opcnt - 1];
3111 nfsd4_encode_operation(resp, op);
3113 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3115 if (args->opcnt == 1) {
3117 * The original operation wasn't a solo sequence--we
3118 * always cache those--so this retry must not match the
3121 op->status = nfserr_seq_false_retry;
3123 op = &args->ops[resp->opcnt++];
3124 op->status = nfserr_retry_uncached_rep;
3125 nfsd4_encode_operation(resp, op);
3131 * The sequence operation is not cached because we can use the slot and
3135 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3136 struct nfsd4_sequence *seq)
3138 struct nfsd4_slot *slot = resp->cstate.slot;
3139 struct xdr_stream *xdr = resp->xdr;
3143 dprintk("--> %s slot %p\n", __func__, slot);
3145 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3149 p = xdr_reserve_space(xdr, slot->sl_datalen);
3152 return nfserr_serverfault;
3154 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3155 xdr_commit_encode(xdr);
3157 resp->opcnt = slot->sl_opcnt;
3158 return slot->sl_status;
3162 * Set the exchange_id flags returned by the server.
3165 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3167 #ifdef CONFIG_NFSD_PNFS
3168 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3170 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3173 /* Referrals are supported, Migration is not. */
3174 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3176 /* set the wire flags to return to client. */
3177 clid->flags = new->cl_exchange_flags;
3180 static bool client_has_openowners(struct nfs4_client *clp)
3182 struct nfs4_openowner *oo;
3184 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3185 if (!list_empty(&oo->oo_owner.so_stateids))
3191 static bool client_has_state(struct nfs4_client *clp)
3193 return client_has_openowners(clp)
3194 #ifdef CONFIG_NFSD_PNFS
3195 || !list_empty(&clp->cl_lo_states)
3197 || !list_empty(&clp->cl_delegations)
3198 || !list_empty(&clp->cl_sessions)
3199 || !list_empty(&clp->async_copies);
3202 static __be32 copy_impl_id(struct nfs4_client *clp,
3203 struct nfsd4_exchange_id *exid)
3205 if (!exid->nii_domain.data)
3207 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3208 if (!clp->cl_nii_domain.data)
3209 return nfserr_jukebox;
3210 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3211 if (!clp->cl_nii_name.data)
3212 return nfserr_jukebox;
3213 clp->cl_nii_time = exid->nii_time;
3218 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3219 union nfsd4_op_u *u)
3221 struct nfsd4_exchange_id *exid = &u->exchange_id;
3222 struct nfs4_client *conf, *new;
3223 struct nfs4_client *unconf = NULL;
3225 char addr_str[INET6_ADDRSTRLEN];
3226 nfs4_verifier verf = exid->verifier;
3227 struct sockaddr *sa = svc_addr(rqstp);
3228 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3229 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3231 rpc_ntop(sa, addr_str, sizeof(addr_str));
3232 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3233 "ip_addr=%s flags %x, spa_how %u\n",
3234 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3235 addr_str, exid->flags, exid->spa_how);
3237 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3238 return nfserr_inval;
3240 new = create_client(exid->clname, rqstp, &verf);
3242 return nfserr_jukebox;
3243 status = copy_impl_id(new, exid);
3247 switch (exid->spa_how) {
3249 exid->spo_must_enforce[0] = 0;
3250 exid->spo_must_enforce[1] = (
3251 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3252 1 << (OP_EXCHANGE_ID - 32) |
3253 1 << (OP_CREATE_SESSION - 32) |
3254 1 << (OP_DESTROY_SESSION - 32) |
3255 1 << (OP_DESTROY_CLIENTID - 32));
3257 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3258 1 << (OP_OPEN_DOWNGRADE) |
3260 1 << (OP_DELEGRETURN));
3262 exid->spo_must_allow[1] &= (
3263 1 << (OP_TEST_STATEID - 32) |
3264 1 << (OP_FREE_STATEID - 32));
3265 if (!svc_rqst_integrity_protected(rqstp)) {
3266 status = nfserr_inval;
3270 * Sometimes userspace doesn't give us a principal.
3271 * Which is a bug, really. Anyway, we can't enforce
3272 * MACH_CRED in that case, better to give up now:
3274 if (!new->cl_cred.cr_principal &&
3275 !new->cl_cred.cr_raw_principal) {
3276 status = nfserr_serverfault;
3279 new->cl_mach_cred = true;
3283 default: /* checked by xdr code */
3287 status = nfserr_encr_alg_unsupp;
3291 /* Cases below refer to rfc 5661 section 18.35.4: */
3292 spin_lock(&nn->client_lock);
3293 conf = find_confirmed_client_by_name(&exid->clname, nn);
3295 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3296 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3299 if (!clp_used_exchangeid(conf)) { /* buggy client */
3300 status = nfserr_inval;
3303 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3304 status = nfserr_wrong_cred;
3307 if (!creds_match) { /* case 9 */
3308 status = nfserr_perm;
3311 if (!verfs_match) { /* case 8 */
3312 status = nfserr_not_same;
3316 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3317 trace_nfsd_clid_confirmed_r(conf);
3320 if (!creds_match) { /* case 3 */
3321 if (client_has_state(conf)) {
3322 status = nfserr_clid_inuse;
3323 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3328 if (verfs_match) { /* case 2 */
3329 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3330 trace_nfsd_clid_confirmed_r(conf);
3333 /* case 5, client reboot */
3334 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3339 if (update) { /* case 7 */
3340 status = nfserr_noent;
3344 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3345 if (unconf) /* case 4, possible retry or client restart */
3346 unhash_client_locked(unconf);
3348 /* case 1, new owner ID */
3349 trace_nfsd_clid_fresh(new);
3353 status = mark_client_expired_locked(conf);
3356 trace_nfsd_clid_replaced(&conf->cl_clientid);
3358 new->cl_minorversion = cstate->minorversion;
3359 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3360 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3362 add_to_unconfirmed(new);
3365 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3366 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3368 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3369 nfsd4_set_ex_flags(conf, exid);
3371 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3372 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3376 spin_unlock(&nn->client_lock);
3381 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3382 expire_client(unconf);
3388 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3390 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3393 /* The slot is in use, and no response has been sent. */
3395 if (seqid == slot_seqid)
3396 return nfserr_jukebox;
3398 return nfserr_seq_misordered;
3400 /* Note unsigned 32-bit arithmetic handles wraparound: */
3401 if (likely(seqid == slot_seqid + 1))
3403 if (seqid == slot_seqid)
3404 return nfserr_replay_cache;
3405 return nfserr_seq_misordered;
3409 * Cache the create session result into the create session single DRC
3410 * slot cache by saving the xdr structure. sl_seqid has been set.
3411 * Do this for solo or embedded create session operations.
3414 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3415 struct nfsd4_clid_slot *slot, __be32 nfserr)
3417 slot->sl_status = nfserr;
3418 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3422 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3423 struct nfsd4_clid_slot *slot)
3425 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3426 return slot->sl_status;
3429 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3430 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3431 1 + /* MIN tag is length with zero, only length */ \
3432 3 + /* version, opcount, opcode */ \
3433 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3434 /* seqid, slotID, slotID, cache */ \
3435 4 ) * sizeof(__be32))
3437 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3438 2 + /* verifier: AUTH_NULL, length 0 */\
3440 1 + /* MIN tag is length with zero, only length */ \
3441 3 + /* opcount, opcode, opstatus*/ \
3442 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3443 /* seqid, slotID, slotID, slotID, status */ \
3444 5 ) * sizeof(__be32))
3446 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3448 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3450 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3451 return nfserr_toosmall;
3452 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3453 return nfserr_toosmall;
3454 ca->headerpadsz = 0;
3455 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3456 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3457 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3458 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3459 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3460 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3462 * Note decreasing slot size below client's request may make it
3463 * difficult for client to function correctly, whereas
3464 * decreasing the number of slots will (just?) affect
3465 * performance. When short on memory we therefore prefer to
3466 * decrease number of slots instead of their size. Clients that
3467 * request larger slots than they need will get poor results:
3468 * Note that we always allow at least one slot, because our
3469 * accounting is soft and provides no guarantees either way.
3471 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3477 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3478 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3480 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3481 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3483 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3484 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3486 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3487 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3488 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3489 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3492 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3494 ca->headerpadsz = 0;
3496 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3497 return nfserr_toosmall;
3498 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3499 return nfserr_toosmall;
3500 ca->maxresp_cached = 0;
3502 return nfserr_toosmall;
3507 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3509 switch (cbs->flavor) {
3515 * GSS case: the spec doesn't allow us to return this
3516 * error. But it also doesn't allow us not to support
3518 * I'd rather this fail hard than return some error the
3519 * client might think it can already handle:
3521 return nfserr_encr_alg_unsupp;
3526 nfsd4_create_session(struct svc_rqst *rqstp,
3527 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3529 struct nfsd4_create_session *cr_ses = &u->create_session;
3530 struct sockaddr *sa = svc_addr(rqstp);
3531 struct nfs4_client *conf, *unconf;
3532 struct nfs4_client *old = NULL;
3533 struct nfsd4_session *new;
3534 struct nfsd4_conn *conn;
3535 struct nfsd4_clid_slot *cs_slot = NULL;
3537 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3539 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3540 return nfserr_inval;
3541 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3544 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3547 status = check_backchannel_attrs(&cr_ses->back_channel);
3549 goto out_release_drc_mem;
3550 status = nfserr_jukebox;
3551 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3553 goto out_release_drc_mem;
3554 conn = alloc_conn_from_crses(rqstp, cr_ses);
3556 goto out_free_session;
3558 spin_lock(&nn->client_lock);
3559 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3560 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3561 WARN_ON_ONCE(conf && unconf);
3564 status = nfserr_wrong_cred;
3565 if (!nfsd4_mach_creds_match(conf, rqstp))
3567 cs_slot = &conf->cl_cs_slot;
3568 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3570 if (status == nfserr_replay_cache)
3571 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3574 } else if (unconf) {
3575 status = nfserr_clid_inuse;
3576 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3577 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3578 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
3581 status = nfserr_wrong_cred;
3582 if (!nfsd4_mach_creds_match(unconf, rqstp))
3584 cs_slot = &unconf->cl_cs_slot;
3585 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3587 /* an unconfirmed replay returns misordered */
3588 status = nfserr_seq_misordered;
3591 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3593 status = mark_client_expired_locked(old);
3598 trace_nfsd_clid_replaced(&old->cl_clientid);
3600 move_to_confirmed(unconf);
3603 status = nfserr_stale_clientid;
3607 /* Persistent sessions are not supported */
3608 cr_ses->flags &= ~SESSION4_PERSIST;
3609 /* Upshifting from TCP to RDMA is not supported */
3610 cr_ses->flags &= ~SESSION4_RDMA;
3612 init_session(rqstp, new, conf, cr_ses);
3613 nfsd4_get_session_locked(new);
3615 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3616 NFS4_MAX_SESSIONID_LEN);
3617 cs_slot->sl_seqid++;
3618 cr_ses->seqid = cs_slot->sl_seqid;
3620 /* cache solo and embedded create sessions under the client_lock */
3621 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3622 spin_unlock(&nn->client_lock);
3624 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3625 /* init connection and backchannel */
3626 nfsd4_init_conn(rqstp, conn, new);
3627 nfsd4_put_session(new);
3632 spin_unlock(&nn->client_lock);
3637 __free_session(new);
3638 out_release_drc_mem:
3639 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3643 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3646 case NFS4_CDFC4_FORE:
3647 case NFS4_CDFC4_BACK:
3649 case NFS4_CDFC4_FORE_OR_BOTH:
3650 case NFS4_CDFC4_BACK_OR_BOTH:
3651 *dir = NFS4_CDFC4_BOTH;
3654 return nfserr_inval;
3657 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3658 struct nfsd4_compound_state *cstate,
3659 union nfsd4_op_u *u)
3661 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3662 struct nfsd4_session *session = cstate->session;
3663 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3666 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3669 spin_lock(&nn->client_lock);
3670 session->se_cb_prog = bc->bc_cb_program;
3671 session->se_cb_sec = bc->bc_cb_sec;
3672 spin_unlock(&nn->client_lock);
3674 nfsd4_probe_callback(session->se_client);
3679 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3681 struct nfsd4_conn *c;
3683 list_for_each_entry(c, &s->se_conns, cn_persession) {
3684 if (c->cn_xprt == xpt) {
3691 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3692 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3694 struct nfs4_client *clp = session->se_client;
3695 struct svc_xprt *xpt = rqst->rq_xprt;
3696 struct nfsd4_conn *c;
3699 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3700 spin_lock(&clp->cl_lock);
3701 c = __nfsd4_find_conn(xpt, session);
3703 status = nfserr_noent;
3704 else if (req == c->cn_flags)
3706 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3707 c->cn_flags != NFS4_CDFC4_BACK)
3709 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3710 c->cn_flags != NFS4_CDFC4_FORE)
3713 status = nfserr_inval;
3714 spin_unlock(&clp->cl_lock);
3715 if (status == nfs_ok && conn)
3720 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3721 struct nfsd4_compound_state *cstate,
3722 union nfsd4_op_u *u)
3724 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3726 struct nfsd4_conn *conn;
3727 struct nfsd4_session *session;
3728 struct net *net = SVC_NET(rqstp);
3729 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3731 if (!nfsd4_last_compound_op(rqstp))
3732 return nfserr_not_only_op;
3733 spin_lock(&nn->client_lock);
3734 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3735 spin_unlock(&nn->client_lock);
3737 goto out_no_session;
3738 status = nfserr_wrong_cred;
3739 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3741 status = nfsd4_match_existing_connection(rqstp, session,
3743 if (status == nfs_ok) {
3744 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3745 bcts->dir == NFS4_CDFC4_BACK)
3746 conn->cn_flags |= NFS4_CDFC4_BACK;
3747 nfsd4_probe_callback(session->se_client);
3750 if (status == nfserr_inval)
3752 status = nfsd4_map_bcts_dir(&bcts->dir);
3755 conn = alloc_conn(rqstp, bcts->dir);
3756 status = nfserr_jukebox;
3759 nfsd4_init_conn(rqstp, conn, session);
3762 nfsd4_put_session(session);
3767 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3769 if (!cstate->session)
3771 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3775 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3776 union nfsd4_op_u *u)
3778 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3779 struct nfsd4_session *ses;
3781 int ref_held_by_me = 0;
3782 struct net *net = SVC_NET(r);
3783 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3785 status = nfserr_not_only_op;
3786 if (nfsd4_compound_in_session(cstate, sessionid)) {
3787 if (!nfsd4_last_compound_op(r))
3791 dump_sessionid(__func__, sessionid);
3792 spin_lock(&nn->client_lock);
3793 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3795 goto out_client_lock;
3796 status = nfserr_wrong_cred;
3797 if (!nfsd4_mach_creds_match(ses->se_client, r))
3798 goto out_put_session;
3799 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3801 goto out_put_session;
3802 unhash_session(ses);
3803 spin_unlock(&nn->client_lock);
3805 nfsd4_probe_callback_sync(ses->se_client);
3807 spin_lock(&nn->client_lock);
3810 nfsd4_put_session_locked(ses);
3812 spin_unlock(&nn->client_lock);
3817 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3819 struct nfs4_client *clp = ses->se_client;
3820 struct nfsd4_conn *c;
3821 __be32 status = nfs_ok;
3824 spin_lock(&clp->cl_lock);
3825 c = __nfsd4_find_conn(new->cn_xprt, ses);
3828 status = nfserr_conn_not_bound_to_session;
3829 if (clp->cl_mach_cred)
3831 __nfsd4_hash_conn(new, ses);
3832 spin_unlock(&clp->cl_lock);
3833 ret = nfsd4_register_conn(new);
3835 /* oops; xprt is already down: */
3836 nfsd4_conn_lost(&new->cn_xpt_user);
3839 spin_unlock(&clp->cl_lock);
3844 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3846 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3848 return args->opcnt > session->se_fchannel.maxops;
3851 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3852 struct nfsd4_session *session)
3854 struct xdr_buf *xb = &rqstp->rq_arg;
3856 return xb->len > session->se_fchannel.maxreq_sz;
3859 static bool replay_matches_cache(struct svc_rqst *rqstp,
3860 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3862 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3864 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3865 (bool)seq->cachethis)
3868 * If there's an error then the reply can have fewer ops than
3871 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3874 * But if we cached a reply with *more* ops than the call you're
3875 * sending us now, then this new call is clearly not really a
3876 * replay of the old one:
3878 if (slot->sl_opcnt > argp->opcnt)
3880 /* This is the only check explicitly called by spec: */
3881 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3884 * There may be more comparisons we could actually do, but the
3885 * spec doesn't require us to catch every case where the calls
3886 * don't match (that would require caching the call as well as
3887 * the reply), so we don't bother.
3893 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3894 union nfsd4_op_u *u)
3896 struct nfsd4_sequence *seq = &u->sequence;
3897 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3898 struct xdr_stream *xdr = resp->xdr;
3899 struct nfsd4_session *session;
3900 struct nfs4_client *clp;
3901 struct nfsd4_slot *slot;
3902 struct nfsd4_conn *conn;
3905 struct net *net = SVC_NET(rqstp);
3906 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3908 if (resp->opcnt != 1)
3909 return nfserr_sequence_pos;
3912 * Will be either used or freed by nfsd4_sequence_check_conn
3915 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3917 return nfserr_jukebox;
3919 spin_lock(&nn->client_lock);
3920 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3922 goto out_no_session;
3923 clp = session->se_client;
3925 status = nfserr_too_many_ops;
3926 if (nfsd4_session_too_many_ops(rqstp, session))
3927 goto out_put_session;
3929 status = nfserr_req_too_big;
3930 if (nfsd4_request_too_big(rqstp, session))
3931 goto out_put_session;
3933 status = nfserr_badslot;
3934 if (seq->slotid >= session->se_fchannel.maxreqs)
3935 goto out_put_session;
3937 slot = session->se_slots[seq->slotid];
3938 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3940 /* We do not negotiate the number of slots yet, so set the
3941 * maxslots to the session maxreqs which is used to encode
3942 * sr_highest_slotid and the sr_target_slot id to maxslots */
3943 seq->maxslots = session->se_fchannel.maxreqs;
3945 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3946 slot->sl_flags & NFSD4_SLOT_INUSE);
3947 if (status == nfserr_replay_cache) {
3948 status = nfserr_seq_misordered;
3949 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3950 goto out_put_session;
3951 status = nfserr_seq_false_retry;
3952 if (!replay_matches_cache(rqstp, seq, slot))
3953 goto out_put_session;
3954 cstate->slot = slot;
3955 cstate->session = session;
3957 /* Return the cached reply status and set cstate->status
3958 * for nfsd4_proc_compound processing */
3959 status = nfsd4_replay_cache_entry(resp, seq);
3960 cstate->status = nfserr_replay_cache;
3964 goto out_put_session;
3966 status = nfsd4_sequence_check_conn(conn, session);
3969 goto out_put_session;
3971 buflen = (seq->cachethis) ?
3972 session->se_fchannel.maxresp_cached :
3973 session->se_fchannel.maxresp_sz;
3974 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3976 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3977 goto out_put_session;
3978 svc_reserve(rqstp, buflen);
3981 /* Success! bump slot seqid */
3982 slot->sl_seqid = seq->seqid;
3983 slot->sl_flags |= NFSD4_SLOT_INUSE;
3985 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3987 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3989 cstate->slot = slot;
3990 cstate->session = session;
3994 switch (clp->cl_cb_state) {
3996 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3998 case NFSD4_CB_FAULT:
3999 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4002 seq->status_flags = 0;
4004 if (!list_empty(&clp->cl_revoked))
4005 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4009 spin_unlock(&nn->client_lock);
4012 nfsd4_put_session_locked(session);
4013 goto out_no_session;
4017 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4019 struct nfsd4_compound_state *cs = &resp->cstate;
4021 if (nfsd4_has_session(cs)) {
4022 if (cs->status != nfserr_replay_cache) {
4023 nfsd4_store_cache_entry(resp);
4024 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4026 /* Drop session reference that was taken in nfsd4_sequence() */
4027 nfsd4_put_session(cs->session);
4029 put_client_renew(cs->clp);
4033 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4034 struct nfsd4_compound_state *cstate,
4035 union nfsd4_op_u *u)
4037 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4038 struct nfs4_client *conf, *unconf;
4039 struct nfs4_client *clp = NULL;
4041 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4043 spin_lock(&nn->client_lock);
4044 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4045 conf = find_confirmed_client(&dc->clientid, true, nn);
4046 WARN_ON_ONCE(conf && unconf);
4049 if (client_has_state(conf)) {
4050 status = nfserr_clientid_busy;
4053 status = mark_client_expired_locked(conf);
4060 status = nfserr_stale_clientid;
4063 if (!nfsd4_mach_creds_match(clp, rqstp)) {
4065 status = nfserr_wrong_cred;
4068 trace_nfsd_clid_destroyed(&clp->cl_clientid);
4069 unhash_client_locked(clp);
4071 spin_unlock(&nn->client_lock);
4078 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4079 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4081 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4082 struct nfs4_client *clp = cstate->clp;
4085 if (rc->rca_one_fs) {
4086 if (!cstate->current_fh.fh_dentry)
4087 return nfserr_nofilehandle;
4089 * We don't take advantage of the rca_one_fs case.
4090 * That's OK, it's optional, we can safely ignore it.
4095 status = nfserr_complete_already;
4096 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4099 status = nfserr_stale_clientid;
4100 if (is_client_expired(clp))
4102 * The following error isn't really legal.
4103 * But we only get here if the client just explicitly
4104 * destroyed the client. Surely it no longer cares what
4105 * error it gets back on an operation for the dead
4111 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4112 nfsd4_client_record_create(clp);
4113 inc_reclaim_complete(clp);
4119 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4120 union nfsd4_op_u *u)
4122 struct nfsd4_setclientid *setclid = &u->setclientid;
4123 struct xdr_netobj clname = setclid->se_name;
4124 nfs4_verifier clverifier = setclid->se_verf;
4125 struct nfs4_client *conf, *new;
4126 struct nfs4_client *unconf = NULL;
4128 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4130 new = create_client(clname, rqstp, &clverifier);
4132 return nfserr_jukebox;
4133 spin_lock(&nn->client_lock);
4134 conf = find_confirmed_client_by_name(&clname, nn);
4135 if (conf && client_has_state(conf)) {
4136 status = nfserr_clid_inuse;
4137 if (clp_used_exchangeid(conf))
4139 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4140 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4144 unconf = find_unconfirmed_client_by_name(&clname, nn);
4146 unhash_client_locked(unconf);
4148 if (same_verf(&conf->cl_verifier, &clverifier)) {
4149 copy_clid(new, conf);
4150 gen_confirm(new, nn);
4152 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4155 trace_nfsd_clid_fresh(new);
4156 new->cl_minorversion = 0;
4157 gen_callback(new, setclid, rqstp);
4158 add_to_unconfirmed(new);
4159 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4160 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4161 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4165 spin_unlock(&nn->client_lock);
4169 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4170 expire_client(unconf);
4176 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4177 struct nfsd4_compound_state *cstate,
4178 union nfsd4_op_u *u)
4180 struct nfsd4_setclientid_confirm *setclientid_confirm =
4181 &u->setclientid_confirm;
4182 struct nfs4_client *conf, *unconf;
4183 struct nfs4_client *old = NULL;
4184 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4185 clientid_t * clid = &setclientid_confirm->sc_clientid;
4187 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4189 if (STALE_CLIENTID(clid, nn))
4190 return nfserr_stale_clientid;
4192 spin_lock(&nn->client_lock);
4193 conf = find_confirmed_client(clid, false, nn);
4194 unconf = find_unconfirmed_client(clid, false, nn);
4196 * We try hard to give out unique clientid's, so if we get an
4197 * attempt to confirm the same clientid with a different cred,
4198 * the client may be buggy; this should never happen.
4200 * Nevertheless, RFC 7530 recommends INUSE for this case:
4202 status = nfserr_clid_inuse;
4203 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4204 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4207 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4208 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4211 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4212 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4215 status = nfserr_stale_clientid;
4221 unhash_client_locked(old);
4222 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4224 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4226 status = nfserr_clid_inuse;
4227 if (client_has_state(old)
4228 && !same_creds(&unconf->cl_cred,
4233 status = mark_client_expired_locked(old);
4238 trace_nfsd_clid_replaced(&old->cl_clientid);
4240 move_to_confirmed(unconf);
4243 get_client_locked(conf);
4244 spin_unlock(&nn->client_lock);
4246 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4247 nfsd4_probe_callback(conf);
4248 spin_lock(&nn->client_lock);
4249 put_client_renew_locked(conf);
4251 spin_unlock(&nn->client_lock);
4257 static struct nfs4_file *nfsd4_alloc_file(void)
4259 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4262 /* OPEN Share state helper functions */
4263 static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval,
4264 struct nfs4_file *fp)
4266 lockdep_assert_held(&state_lock);
4268 refcount_set(&fp->fi_ref, 1);
4269 spin_lock_init(&fp->fi_lock);
4270 INIT_LIST_HEAD(&fp->fi_stateids);
4271 INIT_LIST_HEAD(&fp->fi_delegations);
4272 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4273 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4274 fp->fi_deleg_file = NULL;
4275 fp->fi_had_conflict = false;
4276 fp->fi_share_deny = 0;
4277 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4278 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4279 fp->fi_aliased = false;
4280 fp->fi_inode = d_inode(fh->fh_dentry);
4281 #ifdef CONFIG_NFSD_PNFS
4282 INIT_LIST_HEAD(&fp->fi_lo_states);
4283 atomic_set(&fp->fi_lo_recalls, 0);
4285 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
4289 nfsd4_free_slabs(void)
4291 kmem_cache_destroy(client_slab);
4292 kmem_cache_destroy(openowner_slab);
4293 kmem_cache_destroy(lockowner_slab);
4294 kmem_cache_destroy(file_slab);
4295 kmem_cache_destroy(stateid_slab);
4296 kmem_cache_destroy(deleg_slab);
4297 kmem_cache_destroy(odstate_slab);
4301 nfsd4_init_slabs(void)
4303 client_slab = kmem_cache_create("nfsd4_clients",
4304 sizeof(struct nfs4_client), 0, 0, NULL);
4305 if (client_slab == NULL)
4307 openowner_slab = kmem_cache_create("nfsd4_openowners",
4308 sizeof(struct nfs4_openowner), 0, 0, NULL);
4309 if (openowner_slab == NULL)
4310 goto out_free_client_slab;
4311 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4312 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4313 if (lockowner_slab == NULL)
4314 goto out_free_openowner_slab;
4315 file_slab = kmem_cache_create("nfsd4_files",
4316 sizeof(struct nfs4_file), 0, 0, NULL);
4317 if (file_slab == NULL)
4318 goto out_free_lockowner_slab;
4319 stateid_slab = kmem_cache_create("nfsd4_stateids",
4320 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4321 if (stateid_slab == NULL)
4322 goto out_free_file_slab;
4323 deleg_slab = kmem_cache_create("nfsd4_delegations",
4324 sizeof(struct nfs4_delegation), 0, 0, NULL);
4325 if (deleg_slab == NULL)
4326 goto out_free_stateid_slab;
4327 odstate_slab = kmem_cache_create("nfsd4_odstate",
4328 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4329 if (odstate_slab == NULL)
4330 goto out_free_deleg_slab;
4333 out_free_deleg_slab:
4334 kmem_cache_destroy(deleg_slab);
4335 out_free_stateid_slab:
4336 kmem_cache_destroy(stateid_slab);
4338 kmem_cache_destroy(file_slab);
4339 out_free_lockowner_slab:
4340 kmem_cache_destroy(lockowner_slab);
4341 out_free_openowner_slab:
4342 kmem_cache_destroy(openowner_slab);
4343 out_free_client_slab:
4344 kmem_cache_destroy(client_slab);
4349 static unsigned long
4350 nfsd_courtesy_client_count(struct shrinker *shrink, struct shrink_control *sc)
4353 struct nfsd_net *nn = container_of(shrink,
4354 struct nfsd_net, nfsd_client_shrinker);
4356 cnt = atomic_read(&nn->nfsd_courtesy_clients);
4358 mod_delayed_work(laundry_wq, &nn->nfsd_shrinker_work, 0);
4359 return (unsigned long)cnt;
4362 static unsigned long
4363 nfsd_courtesy_client_scan(struct shrinker *shrink, struct shrink_control *sc)
4369 nfsd4_init_leases_net(struct nfsd_net *nn)
4374 nn->nfsd4_lease = 90; /* default lease time */
4375 nn->nfsd4_grace = 90;
4376 nn->somebody_reclaimed = false;
4377 nn->track_reclaim_completes = false;
4378 nn->clverifier_counter = get_random_u32();
4379 nn->clientid_base = get_random_u32();
4380 nn->clientid_counter = nn->clientid_base + 1;
4381 nn->s2s_cp_cl_id = nn->clientid_counter++;
4383 atomic_set(&nn->nfs4_client_count, 0);
4385 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4386 max_clients *= NFS4_CLIENTS_PER_GB;
4387 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4389 atomic_set(&nn->nfsd_courtesy_clients, 0);
4390 nn->nfsd_client_shrinker.scan_objects = nfsd_courtesy_client_scan;
4391 nn->nfsd_client_shrinker.count_objects = nfsd_courtesy_client_count;
4392 nn->nfsd_client_shrinker.seeks = DEFAULT_SEEKS;
4393 return register_shrinker(&nn->nfsd_client_shrinker, "nfsd-client");
4397 nfsd4_leases_net_shutdown(struct nfsd_net *nn)
4399 unregister_shrinker(&nn->nfsd_client_shrinker);
4402 static void init_nfs4_replay(struct nfs4_replay *rp)
4404 rp->rp_status = nfserr_serverfault;
4406 rp->rp_buf = rp->rp_ibuf;
4407 mutex_init(&rp->rp_mutex);
4410 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4411 struct nfs4_stateowner *so)
4413 if (!nfsd4_has_session(cstate)) {
4414 mutex_lock(&so->so_replay.rp_mutex);
4415 cstate->replay_owner = nfs4_get_stateowner(so);
4419 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4421 struct nfs4_stateowner *so = cstate->replay_owner;
4424 cstate->replay_owner = NULL;
4425 mutex_unlock(&so->so_replay.rp_mutex);
4426 nfs4_put_stateowner(so);
4430 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4432 struct nfs4_stateowner *sop;
4434 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4438 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4439 if (!sop->so_owner.data) {
4440 kmem_cache_free(slab, sop);
4444 INIT_LIST_HEAD(&sop->so_stateids);
4445 sop->so_client = clp;
4446 init_nfs4_replay(&sop->so_replay);
4447 atomic_set(&sop->so_count, 1);
4451 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4453 lockdep_assert_held(&clp->cl_lock);
4455 list_add(&oo->oo_owner.so_strhash,
4456 &clp->cl_ownerstr_hashtbl[strhashval]);
4457 list_add(&oo->oo_perclient, &clp->cl_openowners);
4460 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4462 unhash_openowner_locked(openowner(so));
4465 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4467 struct nfs4_openowner *oo = openowner(so);
4469 kmem_cache_free(openowner_slab, oo);
4472 static const struct nfs4_stateowner_operations openowner_ops = {
4473 .so_unhash = nfs4_unhash_openowner,
4474 .so_free = nfs4_free_openowner,
4477 static struct nfs4_ol_stateid *
4478 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4480 struct nfs4_ol_stateid *local, *ret = NULL;
4481 struct nfs4_openowner *oo = open->op_openowner;
4483 lockdep_assert_held(&fp->fi_lock);
4485 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4486 /* ignore lock owners */
4487 if (local->st_stateowner->so_is_open_owner == 0)
4489 if (local->st_stateowner != &oo->oo_owner)
4491 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4493 refcount_inc(&ret->st_stid.sc_count);
4501 nfsd4_verify_open_stid(struct nfs4_stid *s)
4503 __be32 ret = nfs_ok;
4505 switch (s->sc_type) {
4509 case NFS4_CLOSED_STID:
4510 case NFS4_CLOSED_DELEG_STID:
4511 ret = nfserr_bad_stateid;
4513 case NFS4_REVOKED_DELEG_STID:
4514 ret = nfserr_deleg_revoked;
4519 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4521 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4525 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4526 ret = nfsd4_verify_open_stid(&stp->st_stid);
4528 mutex_unlock(&stp->st_mutex);
4532 static struct nfs4_ol_stateid *
4533 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4535 struct nfs4_ol_stateid *stp;
4537 spin_lock(&fp->fi_lock);
4538 stp = nfsd4_find_existing_open(fp, open);
4539 spin_unlock(&fp->fi_lock);
4540 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4542 nfs4_put_stid(&stp->st_stid);
4547 static struct nfs4_openowner *
4548 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4549 struct nfsd4_compound_state *cstate)
4551 struct nfs4_client *clp = cstate->clp;
4552 struct nfs4_openowner *oo, *ret;
4554 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4557 oo->oo_owner.so_ops = &openowner_ops;
4558 oo->oo_owner.so_is_open_owner = 1;
4559 oo->oo_owner.so_seqid = open->op_seqid;
4561 if (nfsd4_has_session(cstate))
4562 oo->oo_flags |= NFS4_OO_CONFIRMED;
4564 oo->oo_last_closed_stid = NULL;
4565 INIT_LIST_HEAD(&oo->oo_close_lru);
4566 spin_lock(&clp->cl_lock);
4567 ret = find_openstateowner_str_locked(strhashval, open, clp);
4569 hash_openowner(oo, clp, strhashval);
4572 nfs4_free_stateowner(&oo->oo_owner);
4574 spin_unlock(&clp->cl_lock);
4578 static struct nfs4_ol_stateid *
4579 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4582 struct nfs4_openowner *oo = open->op_openowner;
4583 struct nfs4_ol_stateid *retstp = NULL;
4584 struct nfs4_ol_stateid *stp;
4587 /* We are moving these outside of the spinlocks to avoid the warnings */
4588 mutex_init(&stp->st_mutex);
4589 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4592 spin_lock(&oo->oo_owner.so_client->cl_lock);
4593 spin_lock(&fp->fi_lock);
4595 retstp = nfsd4_find_existing_open(fp, open);
4599 open->op_stp = NULL;
4600 refcount_inc(&stp->st_stid.sc_count);
4601 stp->st_stid.sc_type = NFS4_OPEN_STID;
4602 INIT_LIST_HEAD(&stp->st_locks);
4603 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4605 stp->st_stid.sc_file = fp;
4606 stp->st_access_bmap = 0;
4607 stp->st_deny_bmap = 0;
4608 stp->st_openstp = NULL;
4609 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4610 list_add(&stp->st_perfile, &fp->fi_stateids);
4613 spin_unlock(&fp->fi_lock);
4614 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4616 /* Handle races with CLOSE */
4617 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4618 nfs4_put_stid(&retstp->st_stid);
4621 /* To keep mutex tracking happy */
4622 mutex_unlock(&stp->st_mutex);
4629 * In the 4.0 case we need to keep the owners around a little while to handle
4630 * CLOSE replay. We still do need to release any file access that is held by
4631 * them before returning however.
4634 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4636 struct nfs4_ol_stateid *last;
4637 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4638 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4641 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4644 * We know that we hold one reference via nfsd4_close, and another
4645 * "persistent" reference for the client. If the refcount is higher
4646 * than 2, then there are still calls in progress that are using this
4647 * stateid. We can't put the sc_file reference until they are finished.
4648 * Wait for the refcount to drop to 2. Since it has been unhashed,
4649 * there should be no danger of the refcount going back up again at
4652 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4654 release_all_access(s);
4655 if (s->st_stid.sc_file) {
4656 put_nfs4_file(s->st_stid.sc_file);
4657 s->st_stid.sc_file = NULL;
4660 spin_lock(&nn->client_lock);
4661 last = oo->oo_last_closed_stid;
4662 oo->oo_last_closed_stid = s;
4663 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4664 oo->oo_time = ktime_get_boottime_seconds();
4665 spin_unlock(&nn->client_lock);
4667 nfs4_put_stid(&last->st_stid);
4670 /* search file_hashtbl[] for file */
4671 static struct nfs4_file *
4672 find_file_locked(struct svc_fh *fh, unsigned int hashval)
4674 struct nfs4_file *fp;
4676 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4677 lockdep_is_held(&state_lock)) {
4678 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4679 if (refcount_inc_not_zero(&fp->fi_ref))
4686 static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
4687 unsigned int hashval)
4689 struct nfs4_file *fp;
4690 struct nfs4_file *ret = NULL;
4691 bool alias_found = false;
4693 spin_lock(&state_lock);
4694 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4695 lockdep_is_held(&state_lock)) {
4696 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4697 if (refcount_inc_not_zero(&fp->fi_ref))
4699 } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
4700 fp->fi_aliased = alias_found = true;
4702 if (likely(ret == NULL)) {
4703 nfsd4_init_file(fh, hashval, new);
4704 new->fi_aliased = alias_found;
4707 spin_unlock(&state_lock);
4711 static struct nfs4_file * find_file(struct svc_fh *fh)
4713 struct nfs4_file *fp;
4714 unsigned int hashval = file_hashval(fh);
4717 fp = find_file_locked(fh, hashval);
4722 static struct nfs4_file *
4723 find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
4725 struct nfs4_file *fp;
4726 unsigned int hashval = file_hashval(fh);
4729 fp = find_file_locked(fh, hashval);
4734 return insert_file(new, fh, hashval);
4738 * Called to check deny when READ with all zero stateid or
4739 * WRITE with all zero or all one stateid
4742 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4744 struct nfs4_file *fp;
4745 __be32 ret = nfs_ok;
4747 fp = find_file(current_fh);
4750 /* Check for conflicting share reservations */
4751 spin_lock(&fp->fi_lock);
4752 if (fp->fi_share_deny & deny_type)
4753 ret = nfserr_locked;
4754 spin_unlock(&fp->fi_lock);
4759 static bool nfsd4_deleg_present(const struct inode *inode)
4761 struct file_lock_context *ctx = smp_load_acquire(&inode->i_flctx);
4763 return ctx && !list_empty_careful(&ctx->flc_lease);
4767 * nfsd_wait_for_delegreturn - wait for delegations to be returned
4768 * @rqstp: the RPC transaction being executed
4769 * @inode: in-core inode of the file being waited for
4771 * The timeout prevents deadlock if all nfsd threads happen to be
4772 * tied up waiting for returning delegations.
4775 * %true: delegation was returned
4776 * %false: timed out waiting for delegreturn
4778 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
4780 long __maybe_unused timeo;
4782 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
4783 NFSD_DELEGRETURN_TIMEOUT);
4784 trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
4788 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4790 struct nfs4_delegation *dp = cb_to_delegation(cb);
4791 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4794 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4797 * We can't do this in nfsd_break_deleg_cb because it is
4798 * already holding inode->i_lock.
4800 * If the dl_time != 0, then we know that it has already been
4801 * queued for a lease break. Don't queue it again.
4803 spin_lock(&state_lock);
4804 if (delegation_hashed(dp) && dp->dl_time == 0) {
4805 dp->dl_time = ktime_get_boottime_seconds();
4806 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4808 spin_unlock(&state_lock);
4811 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4812 struct rpc_task *task)
4814 struct nfs4_delegation *dp = cb_to_delegation(cb);
4816 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
4818 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4819 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4822 switch (task->tk_status) {
4825 case -NFS4ERR_DELAY:
4826 rpc_delay(task, 2 * HZ);
4829 case -NFS4ERR_BAD_STATEID:
4831 * Race: client probably got cb_recall before open reply
4832 * granting delegation.
4834 if (dp->dl_retries--) {
4835 rpc_delay(task, 2 * HZ);
4844 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4846 struct nfs4_delegation *dp = cb_to_delegation(cb);
4848 nfs4_put_stid(&dp->dl_stid);
4851 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4852 .prepare = nfsd4_cb_recall_prepare,
4853 .done = nfsd4_cb_recall_done,
4854 .release = nfsd4_cb_recall_release,
4857 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4860 * We're assuming the state code never drops its reference
4861 * without first removing the lease. Since we're in this lease
4862 * callback (and since the lease code is serialized by the
4863 * flc_lock) we know the server hasn't removed the lease yet, and
4864 * we know it's safe to take a reference.
4866 refcount_inc(&dp->dl_stid.sc_count);
4867 WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall));
4870 /* Called from break_lease() with flc_lock held. */
4872 nfsd_break_deleg_cb(struct file_lock *fl)
4874 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4875 struct nfs4_file *fp = dp->dl_stid.sc_file;
4876 struct nfs4_client *clp = dp->dl_stid.sc_client;
4877 struct nfsd_net *nn;
4879 trace_nfsd_cb_recall(&dp->dl_stid);
4881 dp->dl_recalled = true;
4882 atomic_inc(&clp->cl_delegs_in_recall);
4883 if (try_to_expire_client(clp)) {
4884 nn = net_generic(clp->net, nfsd_net_id);
4885 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
4889 * We don't want the locks code to timeout the lease for us;
4890 * we'll remove it ourself if a delegation isn't returned
4893 fl->fl_break_time = 0;
4895 spin_lock(&fp->fi_lock);
4896 fp->fi_had_conflict = true;
4897 nfsd_break_one_deleg(dp);
4898 spin_unlock(&fp->fi_lock);
4903 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
4904 * @fl: Lock state to check
4907 * %true: Lease conflict was resolved
4908 * %false: Lease conflict was not resolved.
4910 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4912 struct nfs4_delegation *dl = fl->fl_owner;
4913 struct svc_rqst *rqst;
4914 struct nfs4_client *clp;
4918 rqst = kthread_data(current);
4919 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4920 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
4922 clp = *(rqst->rq_lease_breaker);
4923 return dl->dl_stid.sc_client == clp;
4927 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4928 struct list_head *dispose)
4930 struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
4931 struct nfs4_client *clp = dp->dl_stid.sc_client;
4933 if (arg & F_UNLCK) {
4934 if (dp->dl_recalled)
4935 atomic_dec(&clp->cl_delegs_in_recall);
4936 return lease_modify(onlist, arg, dispose);
4941 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4942 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
4943 .lm_break = nfsd_break_deleg_cb,
4944 .lm_change = nfsd_change_deleg_cb,
4947 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4949 if (nfsd4_has_session(cstate))
4951 if (seqid == so->so_seqid - 1)
4952 return nfserr_replay_me;
4953 if (seqid == so->so_seqid)
4955 return nfserr_bad_seqid;
4958 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4959 struct nfsd_net *nn)
4961 struct nfs4_client *found;
4963 spin_lock(&nn->client_lock);
4964 found = find_confirmed_client(clid, sessions, nn);
4966 atomic_inc(&found->cl_rpc_users);
4967 spin_unlock(&nn->client_lock);
4971 static __be32 set_client(clientid_t *clid,
4972 struct nfsd4_compound_state *cstate,
4973 struct nfsd_net *nn)
4976 if (!same_clid(&cstate->clp->cl_clientid, clid))
4977 return nfserr_stale_clientid;
4980 if (STALE_CLIENTID(clid, nn))
4981 return nfserr_stale_clientid;
4983 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4984 * set cstate->clp), so session = false:
4986 cstate->clp = lookup_clientid(clid, false, nn);
4988 return nfserr_expired;
4993 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4994 struct nfsd4_open *open, struct nfsd_net *nn)
4996 clientid_t *clientid = &open->op_clientid;
4997 struct nfs4_client *clp = NULL;
4998 unsigned int strhashval;
4999 struct nfs4_openowner *oo = NULL;
5003 * In case we need it later, after we've already created the
5004 * file and don't want to risk a further failure:
5006 open->op_file = nfsd4_alloc_file();
5007 if (open->op_file == NULL)
5008 return nfserr_jukebox;
5010 status = set_client(clientid, cstate, nn);
5015 strhashval = ownerstr_hashval(&open->op_owner);
5016 oo = find_openstateowner_str(strhashval, open, clp);
5017 open->op_openowner = oo;
5021 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5022 /* Replace unconfirmed owners without checking for replay. */
5023 release_openowner(oo);
5024 open->op_openowner = NULL;
5027 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5032 oo = alloc_init_open_stateowner(strhashval, open, cstate);
5034 return nfserr_jukebox;
5035 open->op_openowner = oo;
5037 open->op_stp = nfs4_alloc_open_stateid(clp);
5039 return nfserr_jukebox;
5041 if (nfsd4_has_session(cstate) &&
5042 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5043 open->op_odstate = alloc_clnt_odstate(clp);
5044 if (!open->op_odstate)
5045 return nfserr_jukebox;
5051 static inline __be32
5052 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5054 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
5055 return nfserr_openmode;
5060 static int share_access_to_flags(u32 share_access)
5062 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5065 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
5067 struct nfs4_stid *ret;
5069 ret = find_stateid_by_type(cl, s,
5070 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
5073 return delegstateid(ret);
5076 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5078 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5079 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5083 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5084 struct nfs4_delegation **dp)
5087 __be32 status = nfserr_bad_stateid;
5088 struct nfs4_delegation *deleg;
5090 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5093 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
5094 nfs4_put_stid(&deleg->dl_stid);
5095 if (cl->cl_minorversion)
5096 status = nfserr_deleg_revoked;
5099 flags = share_access_to_flags(open->op_share_access);
5100 status = nfs4_check_delegmode(deleg, flags);
5102 nfs4_put_stid(&deleg->dl_stid);
5107 if (!nfsd4_is_deleg_cur(open))
5111 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5115 static inline int nfs4_access_to_access(u32 nfs4_access)
5119 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5120 flags |= NFSD_MAY_READ;
5121 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5122 flags |= NFSD_MAY_WRITE;
5126 static inline __be32
5127 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5128 struct nfsd4_open *open)
5130 struct iattr iattr = {
5131 .ia_valid = ATTR_SIZE,
5134 struct nfsd_attrs attrs = {
5137 if (!open->op_truncate)
5139 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5140 return nfserr_inval;
5141 return nfsd_setattr(rqstp, fh, &attrs, 0, (time64_t)0);
5144 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5145 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5146 struct nfsd4_open *open, bool new_stp)
5148 struct nfsd_file *nf = NULL;
5150 int oflag = nfs4_access_to_omode(open->op_share_access);
5151 int access = nfs4_access_to_access(open->op_share_access);
5152 unsigned char old_access_bmap, old_deny_bmap;
5154 spin_lock(&fp->fi_lock);
5157 * Are we trying to set a deny mode that would conflict with
5160 status = nfs4_file_check_deny(fp, open->op_share_deny);
5161 if (status != nfs_ok) {
5162 if (status != nfserr_share_denied) {
5163 spin_unlock(&fp->fi_lock);
5166 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5167 stp, open->op_share_deny, false))
5168 status = nfserr_jukebox;
5169 spin_unlock(&fp->fi_lock);
5173 /* set access to the file */
5174 status = nfs4_file_get_access(fp, open->op_share_access);
5175 if (status != nfs_ok) {
5176 if (status != nfserr_share_denied) {
5177 spin_unlock(&fp->fi_lock);
5180 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5181 stp, open->op_share_access, true))
5182 status = nfserr_jukebox;
5183 spin_unlock(&fp->fi_lock);
5187 /* Set access bits in stateid */
5188 old_access_bmap = stp->st_access_bmap;
5189 set_access(open->op_share_access, stp);
5191 /* Set new deny mask */
5192 old_deny_bmap = stp->st_deny_bmap;
5193 set_deny(open->op_share_deny, stp);
5194 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5196 if (!fp->fi_fds[oflag]) {
5197 spin_unlock(&fp->fi_lock);
5199 if (!open->op_filp) {
5200 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
5201 if (status != nfs_ok)
5202 goto out_put_access;
5204 status = nfsd_file_create(rqstp, cur_fh, access, &nf);
5205 if (status != nfs_ok)
5206 goto out_put_access;
5207 nf->nf_file = open->op_filp;
5208 open->op_filp = NULL;
5209 trace_nfsd_file_create(rqstp, access, nf);
5212 spin_lock(&fp->fi_lock);
5213 if (!fp->fi_fds[oflag]) {
5214 fp->fi_fds[oflag] = nf;
5218 spin_unlock(&fp->fi_lock);
5222 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5225 goto out_put_access;
5227 status = nfsd4_truncate(rqstp, cur_fh, open);
5229 goto out_put_access;
5233 stp->st_access_bmap = old_access_bmap;
5234 nfs4_file_put_access(fp, open->op_share_access);
5235 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5240 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5241 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5242 struct nfsd4_open *open)
5245 unsigned char old_deny_bmap = stp->st_deny_bmap;
5247 if (!test_access(open->op_share_access, stp))
5248 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5250 /* test and set deny mode */
5251 spin_lock(&fp->fi_lock);
5252 status = nfs4_file_check_deny(fp, open->op_share_deny);
5253 if (status == nfs_ok) {
5254 if (status != nfserr_share_denied) {
5255 set_deny(open->op_share_deny, stp);
5256 fp->fi_share_deny |=
5257 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5259 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5260 stp, open->op_share_deny, false))
5261 status = nfserr_jukebox;
5264 spin_unlock(&fp->fi_lock);
5266 if (status != nfs_ok)
5269 status = nfsd4_truncate(rqstp, cur_fh, open);
5270 if (status != nfs_ok)
5271 reset_union_bmap_deny(old_deny_bmap, stp);
5275 /* Should we give out recallable state?: */
5276 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5278 if (clp->cl_cb_state == NFSD4_CB_UP)
5281 * In the sessions case, since we don't have to establish a
5282 * separate connection for callbacks, we assume it's OK
5283 * until we hear otherwise:
5285 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5288 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5291 struct file_lock *fl;
5293 fl = locks_alloc_lock();
5296 fl->fl_lmops = &nfsd_lease_mng_ops;
5297 fl->fl_flags = FL_DELEG;
5298 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5299 fl->fl_end = OFFSET_MAX;
5300 fl->fl_owner = (fl_owner_t)dp;
5301 fl->fl_pid = current->tgid;
5302 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5306 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5307 struct nfs4_file *fp)
5309 struct nfs4_ol_stateid *st;
5310 struct file *f = fp->fi_deleg_file->nf_file;
5311 struct inode *ino = locks_inode(f);
5314 writes = atomic_read(&ino->i_writecount);
5318 * There could be multiple filehandles (hence multiple
5319 * nfs4_files) referencing this file, but that's not too
5320 * common; let's just give up in that case rather than
5321 * trying to go look up all the clients using that other
5322 * nfs4_file as well:
5327 * If there's a close in progress, make sure that we see it
5328 * clear any fi_fds[] entries before we see it decrement
5331 smp_mb__after_atomic();
5333 if (fp->fi_fds[O_WRONLY])
5335 if (fp->fi_fds[O_RDWR])
5338 return -EAGAIN; /* There may be non-NFSv4 writers */
5340 * It's possible there are non-NFSv4 write opens in progress,
5341 * but if they haven't incremented i_writecount yet then they
5342 * also haven't called break lease yet; so, they'll break this
5343 * lease soon enough. So, all that's left to check for is NFSv4
5346 spin_lock(&fp->fi_lock);
5347 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5348 if (st->st_openstp == NULL /* it's an open */ &&
5349 access_permit_write(st) &&
5350 st->st_stid.sc_client != clp) {
5351 spin_unlock(&fp->fi_lock);
5355 spin_unlock(&fp->fi_lock);
5357 * There's a small chance that we could be racing with another
5358 * NFSv4 open. However, any open that hasn't added itself to
5359 * the fi_stateids list also hasn't called break_lease yet; so,
5360 * they'll break this lease soon enough.
5366 * It's possible that between opening the dentry and setting the delegation,
5367 * that it has been renamed or unlinked. Redo the lookup to verify that this
5371 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5372 struct svc_fh *parent)
5374 struct svc_export *exp;
5375 struct dentry *child;
5378 err = nfsd_lookup_dentry(open->op_rqstp, parent,
5379 open->op_fname, open->op_fnamelen,
5387 if (child != file_dentry(fp->fi_deleg_file->nf_file))
5393 static struct nfs4_delegation *
5394 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5395 struct svc_fh *parent)
5398 struct nfs4_client *clp = stp->st_stid.sc_client;
5399 struct nfs4_file *fp = stp->st_stid.sc_file;
5400 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
5401 struct nfs4_delegation *dp;
5402 struct nfsd_file *nf;
5403 struct file_lock *fl;
5406 * The fi_had_conflict and nfs_get_existing_delegation checks
5407 * here are just optimizations; we'll need to recheck them at
5410 if (fp->fi_had_conflict)
5411 return ERR_PTR(-EAGAIN);
5413 nf = find_readable_file(fp);
5416 * We probably could attempt another open and get a read
5417 * delegation, but for now, don't bother until the
5418 * client actually sends us one.
5420 return ERR_PTR(-EAGAIN);
5422 spin_lock(&state_lock);
5423 spin_lock(&fp->fi_lock);
5424 if (nfs4_delegation_exists(clp, fp))
5426 else if (!fp->fi_deleg_file) {
5427 fp->fi_deleg_file = nf;
5428 /* increment early to prevent fi_deleg_file from being
5430 fp->fi_delegees = 1;
5434 spin_unlock(&fp->fi_lock);
5435 spin_unlock(&state_lock);
5439 return ERR_PTR(status);
5442 dp = alloc_init_deleg(clp, fp, odstate);
5446 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5448 goto out_clnt_odstate;
5450 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5452 locks_free_lock(fl);
5454 goto out_clnt_odstate;
5457 status = nfsd4_verify_deleg_dentry(open, fp, parent);
5462 status = nfsd4_check_conflicting_opens(clp, fp);
5466 spin_lock(&state_lock);
5467 spin_lock(&fp->fi_lock);
5468 if (fp->fi_had_conflict)
5471 status = hash_delegation_locked(dp, fp);
5472 spin_unlock(&fp->fi_lock);
5473 spin_unlock(&state_lock);
5480 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5482 put_clnt_odstate(dp->dl_clnt_odstate);
5483 nfs4_put_stid(&dp->dl_stid);
5486 return ERR_PTR(status);
5489 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5491 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5492 if (status == -EAGAIN)
5493 open->op_why_no_deleg = WND4_CONTENTION;
5495 open->op_why_no_deleg = WND4_RESOURCE;
5496 switch (open->op_deleg_want) {
5497 case NFS4_SHARE_WANT_READ_DELEG:
5498 case NFS4_SHARE_WANT_WRITE_DELEG:
5499 case NFS4_SHARE_WANT_ANY_DELEG:
5501 case NFS4_SHARE_WANT_CANCEL:
5502 open->op_why_no_deleg = WND4_CANCELLED;
5504 case NFS4_SHARE_WANT_NO_DELEG:
5511 * Attempt to hand out a delegation.
5513 * Note we don't support write delegations, and won't until the vfs has
5514 * proper support for them.
5517 nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5518 struct svc_fh *currentfh)
5520 struct nfs4_delegation *dp;
5521 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5522 struct nfs4_client *clp = stp->st_stid.sc_client;
5523 struct svc_fh *parent = NULL;
5527 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5528 open->op_recall = 0;
5529 switch (open->op_claim_type) {
5530 case NFS4_OPEN_CLAIM_PREVIOUS:
5532 open->op_recall = 1;
5533 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5536 case NFS4_OPEN_CLAIM_NULL:
5539 case NFS4_OPEN_CLAIM_FH:
5541 * Let's not give out any delegations till everyone's
5542 * had the chance to reclaim theirs, *and* until
5543 * NLM locks have all been reclaimed:
5545 if (locks_in_grace(clp->net))
5547 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5553 dp = nfs4_set_delegation(open, stp, parent);
5557 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5559 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5560 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5561 nfs4_put_stid(&dp->dl_stid);
5564 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5565 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5566 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5567 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5568 open->op_recall = 1;
5571 /* 4.1 client asking for a delegation? */
5572 if (open->op_deleg_want)
5573 nfsd4_open_deleg_none_ext(open, status);
5577 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5578 struct nfs4_delegation *dp)
5580 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5581 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5582 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5583 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5584 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5585 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5586 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5587 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5589 /* Otherwise the client must be confused wanting a delegation
5590 * it already has, therefore we don't return
5591 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5596 * nfsd4_process_open2 - finish open processing
5597 * @rqstp: the RPC transaction being executed
5598 * @current_fh: NFSv4 COMPOUND's current filehandle
5599 * @open: OPEN arguments
5601 * If successful, (1) truncate the file if open->op_truncate was
5602 * set, (2) set open->op_stateid, (3) set open->op_delegation.
5604 * Returns %nfs_ok on success; otherwise an nfs4stat value in
5605 * network byte order is returned.
5608 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5610 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5611 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5612 struct nfs4_file *fp = NULL;
5613 struct nfs4_ol_stateid *stp = NULL;
5614 struct nfs4_delegation *dp = NULL;
5616 bool new_stp = false;
5619 * Lookup file; if found, lookup stateid and check open request,
5620 * and check for delegations in the process of being recalled.
5621 * If not found, create the nfs4_file struct
5623 fp = find_or_add_file(open->op_file, current_fh);
5624 if (fp != open->op_file) {
5625 status = nfs4_check_deleg(cl, open, &dp);
5628 stp = nfsd4_find_and_lock_existing_open(fp, open);
5630 open->op_file = NULL;
5631 status = nfserr_bad_stateid;
5632 if (nfsd4_is_deleg_cur(open))
5637 stp = init_open_stateid(fp, open);
5643 * OPEN the file, or upgrade an existing OPEN.
5644 * If truncate fails, the OPEN fails.
5646 * stp is already locked.
5649 /* Stateid was found, this is an OPEN upgrade */
5650 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5652 mutex_unlock(&stp->st_mutex);
5656 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
5658 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5659 release_open_stateid(stp);
5660 mutex_unlock(&stp->st_mutex);
5664 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5666 if (stp->st_clnt_odstate == open->op_odstate)
5667 open->op_odstate = NULL;
5670 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5671 mutex_unlock(&stp->st_mutex);
5673 if (nfsd4_has_session(&resp->cstate)) {
5674 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5675 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5676 open->op_why_no_deleg = WND4_NOT_WANTED;
5682 * Attempt to hand out a delegation. No error return, because the
5683 * OPEN succeeds even if we fail.
5685 nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
5688 trace_nfsd_open(&stp->st_stid.sc_stateid);
5690 /* 4.1 client trying to upgrade/downgrade delegation? */
5691 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5692 open->op_deleg_want)
5693 nfsd4_deleg_xgrade_none_ext(open, dp);
5697 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5698 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5700 * To finish the open response, we just need to set the rflags.
5702 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5703 if (nfsd4_has_session(&resp->cstate))
5704 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5705 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5706 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5709 nfs4_put_stid(&dp->dl_stid);
5711 nfs4_put_stid(&stp->st_stid);
5716 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5717 struct nfsd4_open *open)
5719 if (open->op_openowner) {
5720 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5722 nfsd4_cstate_assign_replay(cstate, so);
5723 nfs4_put_stateowner(so);
5726 kmem_cache_free(file_slab, open->op_file);
5728 nfs4_put_stid(&open->op_stp->st_stid);
5729 if (open->op_odstate)
5730 kmem_cache_free(odstate_slab, open->op_odstate);
5734 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5735 union nfsd4_op_u *u)
5737 clientid_t *clid = &u->renew;
5738 struct nfs4_client *clp;
5740 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5742 trace_nfsd_clid_renew(clid);
5743 status = set_client(clid, cstate, nn);
5747 if (!list_empty(&clp->cl_delegations)
5748 && clp->cl_cb_state != NFSD4_CB_UP)
5749 return nfserr_cb_path_down;
5754 nfsd4_end_grace(struct nfsd_net *nn)
5756 /* do nothing if grace period already ended */
5757 if (nn->grace_ended)
5760 trace_nfsd_grace_complete(nn);
5761 nn->grace_ended = true;
5763 * If the server goes down again right now, an NFSv4
5764 * client will still be allowed to reclaim after it comes back up,
5765 * even if it hasn't yet had a chance to reclaim state this time.
5768 nfsd4_record_grace_done(nn);
5770 * At this point, NFSv4 clients can still reclaim. But if the
5771 * server crashes, any that have not yet reclaimed will be out
5772 * of luck on the next boot.
5774 * (NFSv4.1+ clients are considered to have reclaimed once they
5775 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5776 * have reclaimed after their first OPEN.)
5778 locks_end_grace(&nn->nfsd4_manager);
5780 * At this point, and once lockd and/or any other containers
5781 * exit their grace period, further reclaims will fail and
5782 * regular locking can resume.
5787 * If we've waited a lease period but there are still clients trying to
5788 * reclaim, wait a little longer to give them a chance to finish.
5790 static bool clients_still_reclaiming(struct nfsd_net *nn)
5792 time64_t double_grace_period_end = nn->boot_time +
5793 2 * nn->nfsd4_lease;
5795 if (nn->track_reclaim_completes &&
5796 atomic_read(&nn->nr_reclaim_complete) ==
5797 nn->reclaim_str_hashtbl_size)
5799 if (!nn->somebody_reclaimed)
5801 nn->somebody_reclaimed = false;
5803 * If we've given them *two* lease times to reclaim, and they're
5804 * still not done, give up:
5806 if (ktime_get_boottime_seconds() > double_grace_period_end)
5811 struct laundry_time {
5816 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5818 time64_t time_remaining;
5820 if (last_refresh < lt->cutoff)
5822 time_remaining = last_refresh - lt->cutoff;
5823 lt->new_timeo = min(lt->new_timeo, time_remaining);
5827 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
5828 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5830 spin_lock_init(&nn->nfsd_ssc_lock);
5831 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5832 init_waitqueue_head(&nn->nfsd_ssc_waitq);
5834 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5837 * This is called when nfsd is being shutdown, after all inter_ssc
5838 * cleanup were done, to destroy the ssc delayed unmount list.
5840 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5842 struct nfsd4_ssc_umount_item *ni = NULL;
5843 struct nfsd4_ssc_umount_item *tmp;
5845 spin_lock(&nn->nfsd_ssc_lock);
5846 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5847 list_del(&ni->nsui_list);
5848 spin_unlock(&nn->nfsd_ssc_lock);
5849 mntput(ni->nsui_vfsmount);
5851 spin_lock(&nn->nfsd_ssc_lock);
5853 spin_unlock(&nn->nfsd_ssc_lock);
5856 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5858 bool do_wakeup = false;
5859 struct nfsd4_ssc_umount_item *ni = NULL;
5860 struct nfsd4_ssc_umount_item *tmp;
5862 spin_lock(&nn->nfsd_ssc_lock);
5863 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5864 if (time_after(jiffies, ni->nsui_expire)) {
5865 if (refcount_read(&ni->nsui_refcnt) > 1)
5868 /* mark being unmount */
5869 ni->nsui_busy = true;
5870 spin_unlock(&nn->nfsd_ssc_lock);
5871 mntput(ni->nsui_vfsmount);
5872 spin_lock(&nn->nfsd_ssc_lock);
5874 /* waiters need to start from begin of list */
5875 list_del(&ni->nsui_list);
5878 /* wakeup ssc_connect waiters */
5885 wake_up_all(&nn->nfsd_ssc_waitq);
5886 spin_unlock(&nn->nfsd_ssc_lock);
5890 /* Check if any lock belonging to this lockowner has any blockers */
5892 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
5894 struct file_lock_context *ctx;
5895 struct nfs4_ol_stateid *stp;
5896 struct nfs4_file *nf;
5898 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
5899 nf = stp->st_stid.sc_file;
5900 ctx = nf->fi_inode->i_flctx;
5903 if (locks_owner_has_blockers(ctx, lo))
5910 nfs4_anylock_blockers(struct nfs4_client *clp)
5913 struct nfs4_stateowner *so;
5914 struct nfs4_lockowner *lo;
5916 if (atomic_read(&clp->cl_delegs_in_recall))
5918 spin_lock(&clp->cl_lock);
5919 for (i = 0; i < OWNER_HASH_SIZE; i++) {
5920 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
5922 if (so->so_is_open_owner)
5925 if (nfs4_lockowner_has_blockers(lo)) {
5926 spin_unlock(&clp->cl_lock);
5931 spin_unlock(&clp->cl_lock);
5936 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
5937 struct laundry_time *lt)
5939 unsigned int maxreap, reapcnt = 0;
5940 struct list_head *pos, *next;
5941 struct nfs4_client *clp;
5943 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
5944 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
5945 INIT_LIST_HEAD(reaplist);
5946 spin_lock(&nn->client_lock);
5947 list_for_each_safe(pos, next, &nn->client_lru) {
5948 clp = list_entry(pos, struct nfs4_client, cl_lru);
5949 if (clp->cl_state == NFSD4_EXPIRABLE)
5951 if (!state_expired(lt, clp->cl_time))
5953 if (!atomic_read(&clp->cl_rpc_users)) {
5954 if (clp->cl_state == NFSD4_ACTIVE)
5955 atomic_inc(&nn->nfsd_courtesy_clients);
5956 clp->cl_state = NFSD4_COURTESY;
5958 if (!client_has_state(clp))
5960 if (!nfs4_anylock_blockers(clp))
5961 if (reapcnt >= maxreap)
5964 if (!mark_client_expired_locked(clp)) {
5965 list_add(&clp->cl_lru, reaplist);
5969 spin_unlock(&nn->client_lock);
5973 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
5974 struct list_head *reaplist)
5976 unsigned int maxreap = 0, reapcnt = 0;
5977 struct list_head *pos, *next;
5978 struct nfs4_client *clp;
5980 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
5981 INIT_LIST_HEAD(reaplist);
5983 spin_lock(&nn->client_lock);
5984 list_for_each_safe(pos, next, &nn->client_lru) {
5985 clp = list_entry(pos, struct nfs4_client, cl_lru);
5986 if (clp->cl_state == NFSD4_ACTIVE)
5988 if (reapcnt >= maxreap)
5990 if (!mark_client_expired_locked(clp)) {
5991 list_add(&clp->cl_lru, reaplist);
5995 spin_unlock(&nn->client_lock);
5999 nfs4_process_client_reaplist(struct list_head *reaplist)
6001 struct list_head *pos, *next;
6002 struct nfs4_client *clp;
6004 list_for_each_safe(pos, next, reaplist) {
6005 clp = list_entry(pos, struct nfs4_client, cl_lru);
6006 trace_nfsd_clid_purged(&clp->cl_clientid);
6007 list_del_init(&clp->cl_lru);
6013 nfs4_laundromat(struct nfsd_net *nn)
6015 struct nfs4_openowner *oo;
6016 struct nfs4_delegation *dp;
6017 struct nfs4_ol_stateid *stp;
6018 struct nfsd4_blocked_lock *nbl;
6019 struct list_head *pos, *next, reaplist;
6020 struct laundry_time lt = {
6021 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6022 .new_timeo = nn->nfsd4_lease
6024 struct nfs4_cpntf_state *cps;
6025 copy_stateid_t *cps_t;
6028 if (clients_still_reclaiming(nn)) {
6032 nfsd4_end_grace(nn);
6034 spin_lock(&nn->s2s_cp_lock);
6035 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6036 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6037 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
6038 state_expired(<, cps->cpntf_time))
6039 _free_cpntf_state_locked(nn, cps);
6041 spin_unlock(&nn->s2s_cp_lock);
6042 nfs4_get_client_reaplist(nn, &reaplist, <);
6043 nfs4_process_client_reaplist(&reaplist);
6045 spin_lock(&state_lock);
6046 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6047 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6048 if (!state_expired(<, dp->dl_time))
6050 WARN_ON(!unhash_delegation_locked(dp));
6051 list_add(&dp->dl_recall_lru, &reaplist);
6053 spin_unlock(&state_lock);
6054 while (!list_empty(&reaplist)) {
6055 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6057 list_del_init(&dp->dl_recall_lru);
6058 revoke_delegation(dp);
6061 spin_lock(&nn->client_lock);
6062 while (!list_empty(&nn->close_lru)) {
6063 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6065 if (!state_expired(<, oo->oo_time))
6067 list_del_init(&oo->oo_close_lru);
6068 stp = oo->oo_last_closed_stid;
6069 oo->oo_last_closed_stid = NULL;
6070 spin_unlock(&nn->client_lock);
6071 nfs4_put_stid(&stp->st_stid);
6072 spin_lock(&nn->client_lock);
6074 spin_unlock(&nn->client_lock);
6077 * It's possible for a client to try and acquire an already held lock
6078 * that is being held for a long time, and then lose interest in it.
6079 * So, we clean out any un-revisited request after a lease period
6080 * under the assumption that the client is no longer interested.
6082 * RFC5661, sec. 9.6 states that the client must not rely on getting
6083 * notifications and must continue to poll for locks, even when the
6084 * server supports them. Thus this shouldn't lead to clients blocking
6085 * indefinitely once the lock does become free.
6087 BUG_ON(!list_empty(&reaplist));
6088 spin_lock(&nn->blocked_locks_lock);
6089 while (!list_empty(&nn->blocked_locks_lru)) {
6090 nbl = list_first_entry(&nn->blocked_locks_lru,
6091 struct nfsd4_blocked_lock, nbl_lru);
6092 if (!state_expired(<, nbl->nbl_time))
6094 list_move(&nbl->nbl_lru, &reaplist);
6095 list_del_init(&nbl->nbl_list);
6097 spin_unlock(&nn->blocked_locks_lock);
6099 while (!list_empty(&reaplist)) {
6100 nbl = list_first_entry(&reaplist,
6101 struct nfsd4_blocked_lock, nbl_lru);
6102 list_del_init(&nbl->nbl_lru);
6103 free_blocked_lock(nbl);
6105 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6106 /* service the server-to-server copy delayed unmount list */
6107 nfsd4_ssc_expire_umount(nn);
6110 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
6113 static void laundromat_main(struct work_struct *);
6116 laundromat_main(struct work_struct *laundry)
6119 struct delayed_work *dwork = to_delayed_work(laundry);
6120 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6123 t = nfs4_laundromat(nn);
6124 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
6128 courtesy_client_reaper(struct work_struct *reaper)
6130 struct list_head reaplist;
6131 struct delayed_work *dwork = to_delayed_work(reaper);
6132 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6133 nfsd_shrinker_work);
6135 nfs4_get_courtesy_client_reaplist(nn, &reaplist);
6136 nfs4_process_client_reaplist(&reaplist);
6139 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
6141 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
6142 return nfserr_bad_stateid;
6147 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
6149 __be32 status = nfserr_openmode;
6151 /* For lock stateid's, we test the parent open, not the lock: */
6152 if (stp->st_openstp)
6153 stp = stp->st_openstp;
6154 if ((flags & WR_STATE) && !access_permit_write(stp))
6156 if ((flags & RD_STATE) && !access_permit_read(stp))
6163 static inline __be32
6164 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
6166 if (ONE_STATEID(stateid) && (flags & RD_STATE))
6168 else if (opens_in_grace(net)) {
6169 /* Answer in remaining cases depends on existence of
6170 * conflicting state; so we must wait out the grace period. */
6171 return nfserr_grace;
6172 } else if (flags & WR_STATE)
6173 return nfs4_share_conflict(current_fh,
6174 NFS4_SHARE_DENY_WRITE);
6175 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
6176 return nfs4_share_conflict(current_fh,
6177 NFS4_SHARE_DENY_READ);
6180 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
6183 * When sessions are used the stateid generation number is ignored
6186 if (has_session && in->si_generation == 0)
6189 if (in->si_generation == ref->si_generation)
6192 /* If the client sends us a stateid from the future, it's buggy: */
6193 if (nfsd4_stateid_generation_after(in, ref))
6194 return nfserr_bad_stateid;
6196 * However, we could see a stateid from the past, even from a
6197 * non-buggy client. For example, if the client sends a lock
6198 * while some IO is outstanding, the lock may bump si_generation
6199 * while the IO is still in flight. The client could avoid that
6200 * situation by waiting for responses on all the IO requests,
6201 * but better performance may result in retrying IO that
6202 * receives an old_stateid error if requests are rarely
6203 * reordered in flight:
6205 return nfserr_old_stateid;
6208 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
6212 spin_lock(&s->sc_lock);
6213 ret = nfsd4_verify_open_stid(s);
6215 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
6216 spin_unlock(&s->sc_lock);
6220 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6222 if (ols->st_stateowner->so_is_open_owner &&
6223 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6224 return nfserr_bad_stateid;
6228 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
6230 struct nfs4_stid *s;
6231 __be32 status = nfserr_bad_stateid;
6233 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6234 CLOSE_STATEID(stateid))
6236 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
6238 spin_lock(&cl->cl_lock);
6239 s = find_stateid_locked(cl, stateid);
6242 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
6245 switch (s->sc_type) {
6246 case NFS4_DELEG_STID:
6249 case NFS4_REVOKED_DELEG_STID:
6250 status = nfserr_deleg_revoked;
6252 case NFS4_OPEN_STID:
6253 case NFS4_LOCK_STID:
6254 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
6257 printk("unknown stateid type %x\n", s->sc_type);
6259 case NFS4_CLOSED_STID:
6260 case NFS4_CLOSED_DELEG_STID:
6261 status = nfserr_bad_stateid;
6264 spin_unlock(&cl->cl_lock);
6269 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6270 stateid_t *stateid, unsigned char typemask,
6271 struct nfs4_stid **s, struct nfsd_net *nn)
6274 struct nfs4_stid *stid;
6275 bool return_revoked = false;
6278 * only return revoked delegations if explicitly asked.
6279 * otherwise we report revoked or bad_stateid status.
6281 if (typemask & NFS4_REVOKED_DELEG_STID)
6282 return_revoked = true;
6283 else if (typemask & NFS4_DELEG_STID)
6284 typemask |= NFS4_REVOKED_DELEG_STID;
6286 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6287 CLOSE_STATEID(stateid))
6288 return nfserr_bad_stateid;
6289 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
6290 if (status == nfserr_stale_clientid) {
6291 if (cstate->session)
6292 return nfserr_bad_stateid;
6293 return nfserr_stale_stateid;
6297 stid = find_stateid_by_type(cstate->clp, stateid, typemask);
6299 return nfserr_bad_stateid;
6300 if ((stid->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
6301 nfs4_put_stid(stid);
6302 if (cstate->minorversion)
6303 return nfserr_deleg_revoked;
6304 return nfserr_bad_stateid;
6310 static struct nfsd_file *
6311 nfs4_find_file(struct nfs4_stid *s, int flags)
6316 switch (s->sc_type) {
6317 case NFS4_DELEG_STID:
6318 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
6320 return nfsd_file_get(s->sc_file->fi_deleg_file);
6321 case NFS4_OPEN_STID:
6322 case NFS4_LOCK_STID:
6323 if (flags & RD_STATE)
6324 return find_readable_file(s->sc_file);
6326 return find_writeable_file(s->sc_file);
6333 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
6337 status = nfsd4_check_openowner_confirmed(ols);
6340 return nfs4_check_openmode(ols, flags);
6344 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
6345 struct nfsd_file **nfp, int flags)
6347 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6348 struct nfsd_file *nf;
6351 nf = nfs4_find_file(s, flags);
6353 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6354 acc | NFSD_MAY_OWNER_OVERRIDE);
6360 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
6369 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6371 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
6372 if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
6374 list_del(&cps->cp_list);
6375 idr_remove(&nn->s2s_cp_stateids,
6376 cps->cp_stateid.cs_stid.si_opaque.so_id);
6380 * A READ from an inter server to server COPY will have a
6381 * copy stateid. Look up the copy notify stateid from the
6382 * idr structure and take a reference on it.
6384 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6385 struct nfs4_client *clp,
6386 struct nfs4_cpntf_state **cps)
6388 copy_stateid_t *cps_t;
6389 struct nfs4_cpntf_state *state = NULL;
6391 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6392 return nfserr_bad_stateid;
6393 spin_lock(&nn->s2s_cp_lock);
6394 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6396 state = container_of(cps_t, struct nfs4_cpntf_state,
6398 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
6403 refcount_inc(&state->cp_stateid.cs_count);
6405 _free_cpntf_state_locked(nn, state);
6408 spin_unlock(&nn->s2s_cp_lock);
6410 return nfserr_bad_stateid;
6416 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6417 struct nfs4_stid **stid)
6420 struct nfs4_cpntf_state *cps = NULL;
6421 struct nfs4_client *found;
6423 status = manage_cpntf_state(nn, st, NULL, &cps);
6427 cps->cpntf_time = ktime_get_boottime_seconds();
6429 status = nfserr_expired;
6430 found = lookup_clientid(&cps->cp_p_clid, true, nn);
6434 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6435 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6439 status = nfserr_bad_stateid;
6441 put_client_renew(found);
6443 nfs4_put_cpntf_state(nn, cps);
6447 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6449 spin_lock(&nn->s2s_cp_lock);
6450 _free_cpntf_state_locked(nn, cps);
6451 spin_unlock(&nn->s2s_cp_lock);
6455 * Checks for stateid operations
6458 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
6459 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
6460 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6461 struct nfs4_stid **cstid)
6463 struct net *net = SVC_NET(rqstp);
6464 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6465 struct nfs4_stid *s = NULL;
6471 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6473 status = nfserr_bad_stateid;
6475 status = check_special_stateids(net, fhp, stateid,
6480 status = nfsd4_lookup_stateid(cstate, stateid,
6481 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
6483 if (status == nfserr_bad_stateid)
6484 status = find_cpntf_state(nn, stateid, &s);
6487 status = nfsd4_stid_check_stateid_generation(stateid, s,
6488 nfsd4_has_session(cstate));
6492 switch (s->sc_type) {
6493 case NFS4_DELEG_STID:
6494 status = nfs4_check_delegmode(delegstateid(s), flags);
6496 case NFS4_OPEN_STID:
6497 case NFS4_LOCK_STID:
6498 status = nfs4_check_olstateid(openlockstateid(s), flags);
6501 status = nfserr_bad_stateid;
6506 status = nfs4_check_fh(fhp, s);
6509 if (status == nfs_ok && nfp)
6510 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
6513 if (!status && cstid)
6522 * Test if the stateid is valid
6525 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6526 union nfsd4_op_u *u)
6528 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
6529 struct nfsd4_test_stateid_id *stateid;
6530 struct nfs4_client *cl = cstate->clp;
6532 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
6533 stateid->ts_id_status =
6534 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
6540 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6542 struct nfs4_ol_stateid *stp = openlockstateid(s);
6545 ret = nfsd4_lock_ol_stateid(stp);
6549 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6553 ret = nfserr_locks_held;
6554 if (check_for_locks(stp->st_stid.sc_file,
6555 lockowner(stp->st_stateowner)))
6558 release_lock_stateid(stp);
6562 mutex_unlock(&stp->st_mutex);
6569 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6570 union nfsd4_op_u *u)
6572 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
6573 stateid_t *stateid = &free_stateid->fr_stateid;
6574 struct nfs4_stid *s;
6575 struct nfs4_delegation *dp;
6576 struct nfs4_client *cl = cstate->clp;
6577 __be32 ret = nfserr_bad_stateid;
6579 spin_lock(&cl->cl_lock);
6580 s = find_stateid_locked(cl, stateid);
6583 spin_lock(&s->sc_lock);
6584 switch (s->sc_type) {
6585 case NFS4_DELEG_STID:
6586 ret = nfserr_locks_held;
6588 case NFS4_OPEN_STID:
6589 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6592 ret = nfserr_locks_held;
6594 case NFS4_LOCK_STID:
6595 spin_unlock(&s->sc_lock);
6596 refcount_inc(&s->sc_count);
6597 spin_unlock(&cl->cl_lock);
6598 ret = nfsd4_free_lock_stateid(stateid, s);
6600 case NFS4_REVOKED_DELEG_STID:
6601 spin_unlock(&s->sc_lock);
6602 dp = delegstateid(s);
6603 list_del_init(&dp->dl_recall_lru);
6604 spin_unlock(&cl->cl_lock);
6608 /* Default falls through and returns nfserr_bad_stateid */
6610 spin_unlock(&s->sc_lock);
6612 spin_unlock(&cl->cl_lock);
6620 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6621 RD_STATE : WR_STATE;
6624 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6626 struct svc_fh *current_fh = &cstate->current_fh;
6627 struct nfs4_stateowner *sop = stp->st_stateowner;
6630 status = nfsd4_check_seqid(cstate, sop, seqid);
6633 status = nfsd4_lock_ol_stateid(stp);
6634 if (status != nfs_ok)
6636 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6637 if (status == nfs_ok)
6638 status = nfs4_check_fh(current_fh, &stp->st_stid);
6639 if (status != nfs_ok)
6640 mutex_unlock(&stp->st_mutex);
6645 * Checks for sequence id mutating operations.
6648 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6649 stateid_t *stateid, char typemask,
6650 struct nfs4_ol_stateid **stpp,
6651 struct nfsd_net *nn)
6654 struct nfs4_stid *s;
6655 struct nfs4_ol_stateid *stp = NULL;
6657 trace_nfsd_preprocess(seqid, stateid);
6660 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6663 stp = openlockstateid(s);
6664 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6666 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6670 nfs4_put_stid(&stp->st_stid);
6674 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6675 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6678 struct nfs4_openowner *oo;
6679 struct nfs4_ol_stateid *stp;
6681 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6682 NFS4_OPEN_STID, &stp, nn);
6685 oo = openowner(stp->st_stateowner);
6686 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6687 mutex_unlock(&stp->st_mutex);
6688 nfs4_put_stid(&stp->st_stid);
6689 return nfserr_bad_stateid;
6696 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6697 union nfsd4_op_u *u)
6699 struct nfsd4_open_confirm *oc = &u->open_confirm;
6701 struct nfs4_openowner *oo;
6702 struct nfs4_ol_stateid *stp;
6703 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6705 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6706 cstate->current_fh.fh_dentry);
6708 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6712 status = nfs4_preprocess_seqid_op(cstate,
6713 oc->oc_seqid, &oc->oc_req_stateid,
6714 NFS4_OPEN_STID, &stp, nn);
6717 oo = openowner(stp->st_stateowner);
6718 status = nfserr_bad_stateid;
6719 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6720 mutex_unlock(&stp->st_mutex);
6723 oo->oo_flags |= NFS4_OO_CONFIRMED;
6724 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6725 mutex_unlock(&stp->st_mutex);
6726 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6727 nfsd4_client_record_create(oo->oo_owner.so_client);
6730 nfs4_put_stid(&stp->st_stid);
6732 nfsd4_bump_seqid(cstate, status);
6736 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6738 if (!test_access(access, stp))
6740 nfs4_file_put_access(stp->st_stid.sc_file, access);
6741 clear_access(access, stp);
6744 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6746 switch (to_access) {
6747 case NFS4_SHARE_ACCESS_READ:
6748 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6749 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6751 case NFS4_SHARE_ACCESS_WRITE:
6752 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6753 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6755 case NFS4_SHARE_ACCESS_BOTH:
6763 nfsd4_open_downgrade(struct svc_rqst *rqstp,
6764 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
6766 struct nfsd4_open_downgrade *od = &u->open_downgrade;
6768 struct nfs4_ol_stateid *stp;
6769 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6771 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6772 cstate->current_fh.fh_dentry);
6774 /* We don't yet support WANT bits: */
6775 if (od->od_deleg_want)
6776 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6779 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
6780 &od->od_stateid, &stp, nn);
6783 status = nfserr_inval;
6784 if (!test_access(od->od_share_access, stp)) {
6785 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6786 stp->st_access_bmap, od->od_share_access);
6789 if (!test_deny(od->od_share_deny, stp)) {
6790 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6791 stp->st_deny_bmap, od->od_share_deny);
6794 nfs4_stateid_downgrade(stp, od->od_share_access);
6795 reset_union_bmap_deny(od->od_share_deny, stp);
6796 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
6799 mutex_unlock(&stp->st_mutex);
6800 nfs4_put_stid(&stp->st_stid);
6802 nfsd4_bump_seqid(cstate, status);
6806 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6808 struct nfs4_client *clp = s->st_stid.sc_client;
6810 LIST_HEAD(reaplist);
6811 struct nfs4_ol_stateid *stp;
6813 spin_lock(&clp->cl_lock);
6814 unhashed = unhash_open_stateid(s, &reaplist);
6816 if (clp->cl_minorversion) {
6818 put_ol_stateid_locked(s, &reaplist);
6819 spin_unlock(&clp->cl_lock);
6820 list_for_each_entry(stp, &reaplist, st_locks)
6821 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
6822 free_ol_stateid_reaplist(&reaplist);
6824 spin_unlock(&clp->cl_lock);
6825 free_ol_stateid_reaplist(&reaplist);
6827 move_to_close_lru(s, clp->net);
6832 * nfs4_unlock_state() called after encode
6835 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6836 union nfsd4_op_u *u)
6838 struct nfsd4_close *close = &u->close;
6840 struct nfs4_ol_stateid *stp;
6841 struct net *net = SVC_NET(rqstp);
6842 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6844 dprintk("NFSD: nfsd4_close on file %pd\n",
6845 cstate->current_fh.fh_dentry);
6847 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6849 NFS4_OPEN_STID|NFS4_CLOSED_STID,
6851 nfsd4_bump_seqid(cstate, status);
6855 stp->st_stid.sc_type = NFS4_CLOSED_STID;
6858 * Technically we don't _really_ have to increment or copy it, since
6859 * it should just be gone after this operation and we clobber the
6860 * copied value below, but we continue to do so here just to ensure
6861 * that racing ops see that there was a state change.
6863 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
6865 nfsd4_close_open_stateid(stp);
6866 mutex_unlock(&stp->st_mutex);
6868 /* v4.1+ suggests that we send a special stateid in here, since the
6869 * clients should just ignore this anyway. Since this is not useful
6870 * for v4.0 clients either, we set it to the special close_stateid
6873 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6875 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6877 /* put reference from nfs4_preprocess_seqid_op */
6878 nfs4_put_stid(&stp->st_stid);
6884 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6885 union nfsd4_op_u *u)
6887 struct nfsd4_delegreturn *dr = &u->delegreturn;
6888 struct nfs4_delegation *dp;
6889 stateid_t *stateid = &dr->dr_stateid;
6890 struct nfs4_stid *s;
6892 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6894 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6897 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6900 dp = delegstateid(s);
6901 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6905 wake_up_var(d_inode(cstate->current_fh.fh_dentry));
6906 destroy_delegation(dp);
6908 nfs4_put_stid(&dp->dl_stid);
6913 /* last octet in a range */
6915 last_byte_offset(u64 start, u64 len)
6921 return end > start ? end - 1: NFS4_MAX_UINT64;
6925 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6926 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6927 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6928 * locking, this prevents us from being completely protocol-compliant. The
6929 * real solution to this problem is to start using unsigned file offsets in
6930 * the VFS, but this is a very deep change!
6933 nfs4_transform_lock_offset(struct file_lock *lock)
6935 if (lock->fl_start < 0)
6936 lock->fl_start = OFFSET_MAX;
6937 if (lock->fl_end < 0)
6938 lock->fl_end = OFFSET_MAX;
6942 nfsd4_lm_get_owner(fl_owner_t owner)
6944 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6946 nfs4_get_stateowner(&lo->lo_owner);
6951 nfsd4_lm_put_owner(fl_owner_t owner)
6953 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6956 nfs4_put_stateowner(&lo->lo_owner);
6959 /* return pointer to struct nfs4_client if client is expirable */
6961 nfsd4_lm_lock_expirable(struct file_lock *cfl)
6963 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
6964 struct nfs4_client *clp = lo->lo_owner.so_client;
6965 struct nfsd_net *nn;
6967 if (try_to_expire_client(clp)) {
6968 nn = net_generic(clp->net, nfsd_net_id);
6969 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
6975 /* schedule laundromat to run immediately and wait for it to complete */
6977 nfsd4_lm_expire_lock(void)
6979 flush_workqueue(laundry_wq);
6983 nfsd4_lm_notify(struct file_lock *fl)
6985 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6986 struct net *net = lo->lo_owner.so_client->net;
6987 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6988 struct nfsd4_blocked_lock *nbl = container_of(fl,
6989 struct nfsd4_blocked_lock, nbl_lock);
6992 /* An empty list means that something else is going to be using it */
6993 spin_lock(&nn->blocked_locks_lock);
6994 if (!list_empty(&nbl->nbl_list)) {
6995 list_del_init(&nbl->nbl_list);
6996 list_del_init(&nbl->nbl_lru);
6999 spin_unlock(&nn->blocked_locks_lock);
7002 trace_nfsd_cb_notify_lock(lo, nbl);
7003 nfsd4_run_cb(&nbl->nbl_cb);
7007 static const struct lock_manager_operations nfsd_posix_mng_ops = {
7008 .lm_mod_owner = THIS_MODULE,
7009 .lm_notify = nfsd4_lm_notify,
7010 .lm_get_owner = nfsd4_lm_get_owner,
7011 .lm_put_owner = nfsd4_lm_put_owner,
7012 .lm_lock_expirable = nfsd4_lm_lock_expirable,
7013 .lm_expire_lock = nfsd4_lm_expire_lock,
7017 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
7019 struct nfs4_lockowner *lo;
7021 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
7022 lo = (struct nfs4_lockowner *) fl->fl_owner;
7023 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
7025 if (!deny->ld_owner.data)
7026 /* We just don't care that much */
7028 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
7031 deny->ld_owner.len = 0;
7032 deny->ld_owner.data = NULL;
7033 deny->ld_clientid.cl_boot = 0;
7034 deny->ld_clientid.cl_id = 0;
7036 deny->ld_start = fl->fl_start;
7037 deny->ld_length = NFS4_MAX_UINT64;
7038 if (fl->fl_end != NFS4_MAX_UINT64)
7039 deny->ld_length = fl->fl_end - fl->fl_start + 1;
7040 deny->ld_type = NFS4_READ_LT;
7041 if (fl->fl_type != F_RDLCK)
7042 deny->ld_type = NFS4_WRITE_LT;
7045 static struct nfs4_lockowner *
7046 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
7048 unsigned int strhashval = ownerstr_hashval(owner);
7049 struct nfs4_stateowner *so;
7051 lockdep_assert_held(&clp->cl_lock);
7053 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7055 if (so->so_is_open_owner)
7057 if (same_owner_str(so, owner))
7058 return lockowner(nfs4_get_stateowner(so));
7063 static struct nfs4_lockowner *
7064 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
7066 struct nfs4_lockowner *lo;
7068 spin_lock(&clp->cl_lock);
7069 lo = find_lockowner_str_locked(clp, owner);
7070 spin_unlock(&clp->cl_lock);
7074 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
7076 unhash_lockowner_locked(lockowner(sop));
7079 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
7081 struct nfs4_lockowner *lo = lockowner(sop);
7083 kmem_cache_free(lockowner_slab, lo);
7086 static const struct nfs4_stateowner_operations lockowner_ops = {
7087 .so_unhash = nfs4_unhash_lockowner,
7088 .so_free = nfs4_free_lockowner,
7092 * Alloc a lock owner structure.
7093 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
7096 * strhashval = ownerstr_hashval
7098 static struct nfs4_lockowner *
7099 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
7100 struct nfs4_ol_stateid *open_stp,
7101 struct nfsd4_lock *lock)
7103 struct nfs4_lockowner *lo, *ret;
7105 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
7108 INIT_LIST_HEAD(&lo->lo_blocked);
7109 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
7110 lo->lo_owner.so_is_open_owner = 0;
7111 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
7112 lo->lo_owner.so_ops = &lockowner_ops;
7113 spin_lock(&clp->cl_lock);
7114 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
7116 list_add(&lo->lo_owner.so_strhash,
7117 &clp->cl_ownerstr_hashtbl[strhashval]);
7120 nfs4_free_stateowner(&lo->lo_owner);
7122 spin_unlock(&clp->cl_lock);
7126 static struct nfs4_ol_stateid *
7127 find_lock_stateid(const struct nfs4_lockowner *lo,
7128 const struct nfs4_ol_stateid *ost)
7130 struct nfs4_ol_stateid *lst;
7132 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
7134 /* If ost is not hashed, ost->st_locks will not be valid */
7135 if (!nfs4_ol_stateid_unhashed(ost))
7136 list_for_each_entry(lst, &ost->st_locks, st_locks) {
7137 if (lst->st_stateowner == &lo->lo_owner) {
7138 refcount_inc(&lst->st_stid.sc_count);
7145 static struct nfs4_ol_stateid *
7146 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7147 struct nfs4_file *fp, struct inode *inode,
7148 struct nfs4_ol_stateid *open_stp)
7150 struct nfs4_client *clp = lo->lo_owner.so_client;
7151 struct nfs4_ol_stateid *retstp;
7153 mutex_init(&stp->st_mutex);
7154 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
7156 spin_lock(&clp->cl_lock);
7157 if (nfs4_ol_stateid_unhashed(open_stp))
7159 retstp = find_lock_stateid(lo, open_stp);
7162 refcount_inc(&stp->st_stid.sc_count);
7163 stp->st_stid.sc_type = NFS4_LOCK_STID;
7164 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
7166 stp->st_stid.sc_file = fp;
7167 stp->st_access_bmap = 0;
7168 stp->st_deny_bmap = open_stp->st_deny_bmap;
7169 stp->st_openstp = open_stp;
7170 spin_lock(&fp->fi_lock);
7171 list_add(&stp->st_locks, &open_stp->st_locks);
7172 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
7173 list_add(&stp->st_perfile, &fp->fi_stateids);
7174 spin_unlock(&fp->fi_lock);
7175 spin_unlock(&clp->cl_lock);
7178 spin_unlock(&clp->cl_lock);
7179 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
7180 nfs4_put_stid(&retstp->st_stid);
7183 /* To keep mutex tracking happy */
7184 mutex_unlock(&stp->st_mutex);
7187 spin_unlock(&clp->cl_lock);
7188 mutex_unlock(&stp->st_mutex);
7192 static struct nfs4_ol_stateid *
7193 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7194 struct inode *inode, struct nfs4_ol_stateid *ost,
7197 struct nfs4_stid *ns = NULL;
7198 struct nfs4_ol_stateid *lst;
7199 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7200 struct nfs4_client *clp = oo->oo_owner.so_client;
7203 spin_lock(&clp->cl_lock);
7204 lst = find_lock_stateid(lo, ost);
7205 spin_unlock(&clp->cl_lock);
7207 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7209 nfs4_put_stid(&lst->st_stid);
7211 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7215 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7216 if (lst == openlockstateid(ns))
7225 check_lock_length(u64 offset, u64 length)
7227 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7228 (length > ~offset)));
7231 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
7233 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
7235 lockdep_assert_held(&fp->fi_lock);
7237 if (test_access(access, lock_stp))
7239 __nfs4_file_get_access(fp, access);
7240 set_access(access, lock_stp);
7244 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7245 struct nfs4_ol_stateid *ost,
7246 struct nfsd4_lock *lock,
7247 struct nfs4_ol_stateid **plst, bool *new)
7250 struct nfs4_file *fi = ost->st_stid.sc_file;
7251 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7252 struct nfs4_client *cl = oo->oo_owner.so_client;
7253 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
7254 struct nfs4_lockowner *lo;
7255 struct nfs4_ol_stateid *lst;
7256 unsigned int strhashval;
7258 lo = find_lockowner_str(cl, &lock->lk_new_owner);
7260 strhashval = ownerstr_hashval(&lock->lk_new_owner);
7261 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7263 return nfserr_jukebox;
7265 /* with an existing lockowner, seqids must be the same */
7266 status = nfserr_bad_seqid;
7267 if (!cstate->minorversion &&
7268 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
7272 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7274 status = nfserr_jukebox;
7281 nfs4_put_stateowner(&lo->lo_owner);
7289 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7290 union nfsd4_op_u *u)
7292 struct nfsd4_lock *lock = &u->lock;
7293 struct nfs4_openowner *open_sop = NULL;
7294 struct nfs4_lockowner *lock_sop = NULL;
7295 struct nfs4_ol_stateid *lock_stp = NULL;
7296 struct nfs4_ol_stateid *open_stp = NULL;
7297 struct nfs4_file *fp;
7298 struct nfsd_file *nf = NULL;
7299 struct nfsd4_blocked_lock *nbl = NULL;
7300 struct file_lock *file_lock = NULL;
7301 struct file_lock *conflock = NULL;
7306 unsigned char fl_type;
7307 unsigned int fl_flags = FL_POSIX;
7308 struct net *net = SVC_NET(rqstp);
7309 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7311 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7312 (long long) lock->lk_offset,
7313 (long long) lock->lk_length);
7315 if (check_lock_length(lock->lk_offset, lock->lk_length))
7316 return nfserr_inval;
7318 if ((status = fh_verify(rqstp, &cstate->current_fh,
7319 S_IFREG, NFSD_MAY_LOCK))) {
7320 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7324 if (lock->lk_is_new) {
7325 if (nfsd4_has_session(cstate))
7326 /* See rfc 5661 18.10.3: given clientid is ignored: */
7327 memcpy(&lock->lk_new_clientid,
7328 &cstate->clp->cl_clientid,
7329 sizeof(clientid_t));
7331 /* validate and update open stateid and open seqid */
7332 status = nfs4_preprocess_confirmed_seqid_op(cstate,
7333 lock->lk_new_open_seqid,
7334 &lock->lk_new_open_stateid,
7338 mutex_unlock(&open_stp->st_mutex);
7339 open_sop = openowner(open_stp->st_stateowner);
7340 status = nfserr_bad_stateid;
7341 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
7342 &lock->lk_new_clientid))
7344 status = lookup_or_create_lock_state(cstate, open_stp, lock,
7347 status = nfs4_preprocess_seqid_op(cstate,
7348 lock->lk_old_lock_seqid,
7349 &lock->lk_old_lock_stateid,
7350 NFS4_LOCK_STID, &lock_stp, nn);
7354 lock_sop = lockowner(lock_stp->st_stateowner);
7356 lkflg = setlkflg(lock->lk_type);
7357 status = nfs4_check_openmode(lock_stp, lkflg);
7361 status = nfserr_grace;
7362 if (locks_in_grace(net) && !lock->lk_reclaim)
7364 status = nfserr_no_grace;
7365 if (!locks_in_grace(net) && lock->lk_reclaim)
7368 if (lock->lk_reclaim)
7369 fl_flags |= FL_RECLAIM;
7371 fp = lock_stp->st_stid.sc_file;
7372 switch (lock->lk_type) {
7374 if (nfsd4_has_session(cstate))
7375 fl_flags |= FL_SLEEP;
7378 spin_lock(&fp->fi_lock);
7379 nf = find_readable_file_locked(fp);
7381 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7382 spin_unlock(&fp->fi_lock);
7385 case NFS4_WRITEW_LT:
7386 if (nfsd4_has_session(cstate))
7387 fl_flags |= FL_SLEEP;
7390 spin_lock(&fp->fi_lock);
7391 nf = find_writeable_file_locked(fp);
7393 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7394 spin_unlock(&fp->fi_lock);
7398 status = nfserr_inval;
7403 status = nfserr_openmode;
7408 * Most filesystems with their own ->lock operations will block
7409 * the nfsd thread waiting to acquire the lock. That leads to
7410 * deadlocks (we don't want every nfsd thread tied up waiting
7411 * for file locks), so don't attempt blocking lock notifications
7412 * on those filesystems:
7414 if (nf->nf_file->f_op->lock)
7415 fl_flags &= ~FL_SLEEP;
7417 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
7419 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
7420 status = nfserr_jukebox;
7424 file_lock = &nbl->nbl_lock;
7425 file_lock->fl_type = fl_type;
7426 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
7427 file_lock->fl_pid = current->tgid;
7428 file_lock->fl_file = nf->nf_file;
7429 file_lock->fl_flags = fl_flags;
7430 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7431 file_lock->fl_start = lock->lk_offset;
7432 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
7433 nfs4_transform_lock_offset(file_lock);
7435 conflock = locks_alloc_lock();
7437 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7438 status = nfserr_jukebox;
7442 if (fl_flags & FL_SLEEP) {
7443 nbl->nbl_time = ktime_get_boottime_seconds();
7444 spin_lock(&nn->blocked_locks_lock);
7445 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
7446 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
7447 kref_get(&nbl->nbl_kref);
7448 spin_unlock(&nn->blocked_locks_lock);
7451 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
7453 case 0: /* success! */
7454 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
7456 if (lock->lk_reclaim)
7457 nn->somebody_reclaimed = true;
7459 case FILE_LOCK_DEFERRED:
7460 kref_put(&nbl->nbl_kref, free_nbl);
7463 case -EAGAIN: /* conflock holds conflicting lock */
7464 status = nfserr_denied;
7465 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
7466 nfs4_set_lock_denied(conflock, &lock->lk_denied);
7469 status = nfserr_deadlock;
7472 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
7473 status = nfserrno(err);
7478 /* dequeue it if we queued it before */
7479 if (fl_flags & FL_SLEEP) {
7480 spin_lock(&nn->blocked_locks_lock);
7481 if (!list_empty(&nbl->nbl_list) &&
7482 !list_empty(&nbl->nbl_lru)) {
7483 list_del_init(&nbl->nbl_list);
7484 list_del_init(&nbl->nbl_lru);
7485 kref_put(&nbl->nbl_kref, free_nbl);
7487 /* nbl can use one of lists to be linked to reaplist */
7488 spin_unlock(&nn->blocked_locks_lock);
7490 free_blocked_lock(nbl);
7495 /* Bump seqid manually if the 4.0 replay owner is openowner */
7496 if (cstate->replay_owner &&
7497 cstate->replay_owner != &lock_sop->lo_owner &&
7498 seqid_mutating_err(ntohl(status)))
7499 lock_sop->lo_owner.so_seqid++;
7502 * If this is a new, never-before-used stateid, and we are
7503 * returning an error, then just go ahead and release it.
7506 release_lock_stateid(lock_stp);
7508 mutex_unlock(&lock_stp->st_mutex);
7510 nfs4_put_stid(&lock_stp->st_stid);
7513 nfs4_put_stid(&open_stp->st_stid);
7514 nfsd4_bump_seqid(cstate, status);
7516 locks_free_lock(conflock);
7521 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7522 * so we do a temporary open here just to get an open file to pass to
7525 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
7527 struct nfsd_file *nf;
7528 struct inode *inode;
7531 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7534 inode = fhp->fh_dentry->d_inode;
7535 inode_lock(inode); /* to block new leases till after test_lock: */
7536 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
7539 lock->fl_file = nf->nf_file;
7540 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
7541 lock->fl_file = NULL;
7543 inode_unlock(inode);
7552 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7553 union nfsd4_op_u *u)
7555 struct nfsd4_lockt *lockt = &u->lockt;
7556 struct file_lock *file_lock = NULL;
7557 struct nfs4_lockowner *lo = NULL;
7559 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7561 if (locks_in_grace(SVC_NET(rqstp)))
7562 return nfserr_grace;
7564 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7565 return nfserr_inval;
7567 if (!nfsd4_has_session(cstate)) {
7568 status = set_client(&lockt->lt_clientid, cstate, nn);
7573 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7576 file_lock = locks_alloc_lock();
7578 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7579 status = nfserr_jukebox;
7583 switch (lockt->lt_type) {
7586 file_lock->fl_type = F_RDLCK;
7589 case NFS4_WRITEW_LT:
7590 file_lock->fl_type = F_WRLCK;
7593 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
7594 status = nfserr_inval;
7598 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
7600 file_lock->fl_owner = (fl_owner_t)lo;
7601 file_lock->fl_pid = current->tgid;
7602 file_lock->fl_flags = FL_POSIX;
7604 file_lock->fl_start = lockt->lt_offset;
7605 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
7607 nfs4_transform_lock_offset(file_lock);
7609 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
7613 if (file_lock->fl_type != F_UNLCK) {
7614 status = nfserr_denied;
7615 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
7619 nfs4_put_stateowner(&lo->lo_owner);
7621 locks_free_lock(file_lock);
7626 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7627 union nfsd4_op_u *u)
7629 struct nfsd4_locku *locku = &u->locku;
7630 struct nfs4_ol_stateid *stp;
7631 struct nfsd_file *nf = NULL;
7632 struct file_lock *file_lock = NULL;
7635 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7637 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7638 (long long) locku->lu_offset,
7639 (long long) locku->lu_length);
7641 if (check_lock_length(locku->lu_offset, locku->lu_length))
7642 return nfserr_inval;
7644 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
7645 &locku->lu_stateid, NFS4_LOCK_STID,
7649 nf = find_any_file(stp->st_stid.sc_file);
7651 status = nfserr_lock_range;
7654 file_lock = locks_alloc_lock();
7656 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7657 status = nfserr_jukebox;
7661 file_lock->fl_type = F_UNLCK;
7662 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7663 file_lock->fl_pid = current->tgid;
7664 file_lock->fl_file = nf->nf_file;
7665 file_lock->fl_flags = FL_POSIX;
7666 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7667 file_lock->fl_start = locku->lu_offset;
7669 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7671 nfs4_transform_lock_offset(file_lock);
7673 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7675 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7678 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7682 mutex_unlock(&stp->st_mutex);
7683 nfs4_put_stid(&stp->st_stid);
7685 nfsd4_bump_seqid(cstate, status);
7687 locks_free_lock(file_lock);
7691 status = nfserrno(err);
7697 * true: locks held by lockowner
7698 * false: no locks held by lockowner
7701 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7703 struct file_lock *fl;
7705 struct nfsd_file *nf = find_any_file(fp);
7706 struct inode *inode;
7707 struct file_lock_context *flctx;
7710 /* Any valid lock stateid should have some sort of access */
7715 inode = locks_inode(nf->nf_file);
7716 flctx = inode->i_flctx;
7718 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7719 spin_lock(&flctx->flc_lock);
7720 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7721 if (fl->fl_owner == (fl_owner_t)lowner) {
7726 spin_unlock(&flctx->flc_lock);
7733 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
7734 * @rqstp: RPC transaction
7735 * @cstate: NFSv4 COMPOUND state
7736 * @u: RELEASE_LOCKOWNER arguments
7738 * The lockowner's so_count is bumped when a lock record is added
7739 * or when copying a conflicting lock. The latter case is brief,
7740 * but can lead to fleeting false positives when looking for
7744 * %nfs_ok: lockowner released or not found
7745 * %nfserr_locks_held: lockowner still in use
7746 * %nfserr_stale_clientid: clientid no longer active
7747 * %nfserr_expired: clientid not recognized
7750 nfsd4_release_lockowner(struct svc_rqst *rqstp,
7751 struct nfsd4_compound_state *cstate,
7752 union nfsd4_op_u *u)
7754 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
7755 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7756 clientid_t *clid = &rlockowner->rl_clientid;
7757 struct nfs4_ol_stateid *stp;
7758 struct nfs4_lockowner *lo;
7759 struct nfs4_client *clp;
7760 LIST_HEAD(reaplist);
7763 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7764 clid->cl_boot, clid->cl_id);
7766 status = set_client(clid, cstate, nn);
7771 spin_lock(&clp->cl_lock);
7772 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
7774 spin_unlock(&clp->cl_lock);
7777 if (atomic_read(&lo->lo_owner.so_count) != 2) {
7778 spin_unlock(&clp->cl_lock);
7779 nfs4_put_stateowner(&lo->lo_owner);
7780 return nfserr_locks_held;
7782 unhash_lockowner_locked(lo);
7783 while (!list_empty(&lo->lo_owner.so_stateids)) {
7784 stp = list_first_entry(&lo->lo_owner.so_stateids,
7785 struct nfs4_ol_stateid,
7787 WARN_ON(!unhash_lock_stateid(stp));
7788 put_ol_stateid_locked(stp, &reaplist);
7790 spin_unlock(&clp->cl_lock);
7792 free_ol_stateid_reaplist(&reaplist);
7793 remove_blocked_locks(lo);
7794 nfs4_put_stateowner(&lo->lo_owner);
7798 static inline struct nfs4_client_reclaim *
7801 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
7805 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7807 struct nfs4_client_reclaim *crp;
7809 crp = nfsd4_find_reclaim_client(name, nn);
7810 return (crp && crp->cr_clp);
7814 * failure => all reset bets are off, nfserr_no_grace...
7816 * The caller is responsible for freeing name.data if NULL is returned (it
7817 * will be freed in nfs4_remove_reclaim_record in the normal case).
7819 struct nfs4_client_reclaim *
7820 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7821 struct nfsd_net *nn)
7823 unsigned int strhashval;
7824 struct nfs4_client_reclaim *crp;
7826 crp = alloc_reclaim();
7828 strhashval = clientstr_hashval(name);
7829 INIT_LIST_HEAD(&crp->cr_strhash);
7830 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
7831 crp->cr_name.data = name.data;
7832 crp->cr_name.len = name.len;
7833 crp->cr_princhash.data = princhash.data;
7834 crp->cr_princhash.len = princhash.len;
7836 nn->reclaim_str_hashtbl_size++;
7842 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
7844 list_del(&crp->cr_strhash);
7845 kfree(crp->cr_name.data);
7846 kfree(crp->cr_princhash.data);
7848 nn->reclaim_str_hashtbl_size--;
7852 nfs4_release_reclaim(struct nfsd_net *nn)
7854 struct nfs4_client_reclaim *crp = NULL;
7857 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7858 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7859 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
7860 struct nfs4_client_reclaim, cr_strhash);
7861 nfs4_remove_reclaim_record(crp, nn);
7864 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
7868 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
7869 struct nfs4_client_reclaim *
7870 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
7872 unsigned int strhashval;
7873 struct nfs4_client_reclaim *crp = NULL;
7875 strhashval = clientstr_hashval(name);
7876 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
7877 if (compare_blob(&crp->cr_name, &name) == 0) {
7885 nfs4_check_open_reclaim(struct nfs4_client *clp)
7887 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
7888 return nfserr_no_grace;
7890 if (nfsd4_client_record_check(clp))
7891 return nfserr_reclaim_bad;
7897 * Since the lifetime of a delegation isn't limited to that of an open, a
7898 * client may quite reasonably hang on to a delegation as long as it has
7899 * the inode cached. This becomes an obvious problem the first time a
7900 * client's inode cache approaches the size of the server's total memory.
7902 * For now we avoid this problem by imposing a hard limit on the number
7903 * of delegations, which varies according to the server's memory size.
7906 set_max_delegations(void)
7909 * Allow at most 4 delegations per megabyte of RAM. Quick
7910 * estimates suggest that in the worst case (where every delegation
7911 * is for a different inode), a delegation could take about 1.5K,
7912 * giving a worst case usage of about 6% of memory.
7914 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7917 static int nfs4_state_create_net(struct net *net)
7919 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7922 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7923 sizeof(struct list_head),
7925 if (!nn->conf_id_hashtbl)
7927 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7928 sizeof(struct list_head),
7930 if (!nn->unconf_id_hashtbl)
7932 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7933 sizeof(struct list_head),
7935 if (!nn->sessionid_hashtbl)
7938 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7939 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7940 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7942 for (i = 0; i < SESSION_HASH_SIZE; i++)
7943 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7944 nn->conf_name_tree = RB_ROOT;
7945 nn->unconf_name_tree = RB_ROOT;
7946 nn->boot_time = ktime_get_real_seconds();
7947 nn->grace_ended = false;
7948 nn->nfsd4_manager.block_opens = true;
7949 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7950 INIT_LIST_HEAD(&nn->client_lru);
7951 INIT_LIST_HEAD(&nn->close_lru);
7952 INIT_LIST_HEAD(&nn->del_recall_lru);
7953 spin_lock_init(&nn->client_lock);
7954 spin_lock_init(&nn->s2s_cp_lock);
7955 idr_init(&nn->s2s_cp_stateids);
7957 spin_lock_init(&nn->blocked_locks_lock);
7958 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7960 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7961 INIT_DELAYED_WORK(&nn->nfsd_shrinker_work, courtesy_client_reaper);
7967 kfree(nn->unconf_id_hashtbl);
7969 kfree(nn->conf_id_hashtbl);
7975 nfs4_state_destroy_net(struct net *net)
7978 struct nfs4_client *clp = NULL;
7979 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7981 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7982 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7983 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7984 destroy_client(clp);
7988 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7990 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7991 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7992 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7993 destroy_client(clp);
7997 kfree(nn->sessionid_hashtbl);
7998 kfree(nn->unconf_id_hashtbl);
7999 kfree(nn->conf_id_hashtbl);
8004 nfs4_state_start_net(struct net *net)
8006 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8009 ret = nfs4_state_create_net(net);
8012 locks_start_grace(net, &nn->nfsd4_manager);
8013 nfsd4_client_tracking_init(net);
8014 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
8016 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
8017 nn->nfsd4_grace, net->ns.inum);
8018 trace_nfsd_grace_start(nn);
8019 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
8023 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
8025 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
8026 nfsd4_end_grace(nn);
8030 /* initialization to perform when the nfsd service is started: */
8033 nfs4_state_start(void)
8037 ret = nfsd4_create_callback_queue();
8041 set_max_delegations();
8046 nfs4_state_shutdown_net(struct net *net)
8048 struct nfs4_delegation *dp = NULL;
8049 struct list_head *pos, *next, reaplist;
8050 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8052 cancel_delayed_work_sync(&nn->laundromat_work);
8053 locks_end_grace(&nn->nfsd4_manager);
8055 INIT_LIST_HEAD(&reaplist);
8056 spin_lock(&state_lock);
8057 list_for_each_safe(pos, next, &nn->del_recall_lru) {
8058 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8059 WARN_ON(!unhash_delegation_locked(dp));
8060 list_add(&dp->dl_recall_lru, &reaplist);
8062 spin_unlock(&state_lock);
8063 list_for_each_safe(pos, next, &reaplist) {
8064 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8065 list_del_init(&dp->dl_recall_lru);
8066 destroy_unhashed_deleg(dp);
8069 nfsd4_client_tracking_exit(net);
8070 nfs4_state_destroy_net(net);
8071 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
8072 nfsd4_ssc_shutdown_umount(nn);
8077 nfs4_state_shutdown(void)
8079 nfsd4_destroy_callback_queue();
8083 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8085 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
8086 CURRENT_STATEID(stateid))
8087 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8091 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8093 if (cstate->minorversion) {
8094 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
8095 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8100 clear_current_stateid(struct nfsd4_compound_state *cstate)
8102 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8106 * functions to set current state id
8109 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
8110 union nfsd4_op_u *u)
8112 put_stateid(cstate, &u->open_downgrade.od_stateid);
8116 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
8117 union nfsd4_op_u *u)
8119 put_stateid(cstate, &u->open.op_stateid);
8123 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
8124 union nfsd4_op_u *u)
8126 put_stateid(cstate, &u->close.cl_stateid);
8130 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
8131 union nfsd4_op_u *u)
8133 put_stateid(cstate, &u->lock.lk_resp_stateid);
8137 * functions to consume current state id
8141 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
8142 union nfsd4_op_u *u)
8144 get_stateid(cstate, &u->open_downgrade.od_stateid);
8148 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8149 union nfsd4_op_u *u)
8151 get_stateid(cstate, &u->delegreturn.dr_stateid);
8155 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8156 union nfsd4_op_u *u)
8158 get_stateid(cstate, &u->free_stateid.fr_stateid);
8162 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8163 union nfsd4_op_u *u)
8165 get_stateid(cstate, &u->setattr.sa_stateid);
8169 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8170 union nfsd4_op_u *u)
8172 get_stateid(cstate, &u->close.cl_stateid);
8176 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8177 union nfsd4_op_u *u)
8179 get_stateid(cstate, &u->locku.lu_stateid);
8183 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8184 union nfsd4_op_u *u)
8186 get_stateid(cstate, &u->read.rd_stateid);
8190 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8191 union nfsd4_op_u *u)
8193 get_stateid(cstate, &u->write.wr_stateid);