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_any_file_locked(struct nfs4_file *f)
680 lockdep_assert_held(&f->fi_lock);
682 if (f->fi_fds[O_RDWR])
683 return f->fi_fds[O_RDWR];
684 if (f->fi_fds[O_WRONLY])
685 return f->fi_fds[O_WRONLY];
686 if (f->fi_fds[O_RDONLY])
687 return f->fi_fds[O_RDONLY];
691 static struct nfsd_file *find_deleg_file_locked(struct nfs4_file *f)
693 lockdep_assert_held(&f->fi_lock);
695 if (f->fi_deleg_file)
696 return f->fi_deleg_file;
700 static atomic_long_t num_delegations;
701 unsigned long max_delegations;
704 * Open owner state (share locks)
707 /* hash tables for lock and open owners */
708 #define OWNER_HASH_BITS 8
709 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
710 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
712 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
716 ret = opaque_hashval(ownername->data, ownername->len);
717 return ret & OWNER_HASH_MASK;
720 /* hash table for nfs4_file */
721 #define FILE_HASH_BITS 8
722 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
724 static unsigned int file_hashval(struct svc_fh *fh)
726 struct inode *inode = d_inode(fh->fh_dentry);
728 /* XXX: why not (here & in file cache) use inode? */
729 return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS);
732 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
735 * Check if courtesy clients have conflicting access and resolve it if possible
737 * access: is op_share_access if share_access is true.
738 * Check if access mode, op_share_access, would conflict with
739 * the current deny mode of the file 'fp'.
740 * access: is op_share_deny if share_access is false.
741 * Check if the deny mode, op_share_deny, would conflict with
742 * current access of the file 'fp'.
743 * stp: skip checking this entry.
744 * new_stp: normal open, not open upgrade.
747 * false - access/deny mode conflict with normal client.
748 * true - no conflict or conflict with courtesy client(s) is resolved.
751 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
752 struct nfs4_ol_stateid *stp, u32 access, bool share_access)
754 struct nfs4_ol_stateid *st;
755 bool resolvable = true;
758 struct nfs4_client *clp;
760 lockdep_assert_held(&fp->fi_lock);
761 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
762 /* ignore lock stateid */
765 if (st == stp && new_stp)
767 /* check file access against deny mode or vice versa */
768 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
769 if (!(access & bmap_to_share_mode(bmap)))
771 clp = st->st_stid.sc_client;
772 if (try_to_expire_client(clp))
778 clp = stp->st_stid.sc_client;
779 nn = net_generic(clp->net, nfsd_net_id);
780 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
786 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
788 lockdep_assert_held(&fp->fi_lock);
790 if (access & NFS4_SHARE_ACCESS_WRITE)
791 atomic_inc(&fp->fi_access[O_WRONLY]);
792 if (access & NFS4_SHARE_ACCESS_READ)
793 atomic_inc(&fp->fi_access[O_RDONLY]);
797 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
799 lockdep_assert_held(&fp->fi_lock);
801 /* Does this access mode make sense? */
802 if (access & ~NFS4_SHARE_ACCESS_BOTH)
805 /* Does it conflict with a deny mode already set? */
806 if ((access & fp->fi_share_deny) != 0)
807 return nfserr_share_denied;
809 __nfs4_file_get_access(fp, access);
813 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
815 /* Common case is that there is no deny mode. */
817 /* Does this deny mode make sense? */
818 if (deny & ~NFS4_SHARE_DENY_BOTH)
821 if ((deny & NFS4_SHARE_DENY_READ) &&
822 atomic_read(&fp->fi_access[O_RDONLY]))
823 return nfserr_share_denied;
825 if ((deny & NFS4_SHARE_DENY_WRITE) &&
826 atomic_read(&fp->fi_access[O_WRONLY]))
827 return nfserr_share_denied;
832 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
834 might_lock(&fp->fi_lock);
836 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
837 struct nfsd_file *f1 = NULL;
838 struct nfsd_file *f2 = NULL;
840 swap(f1, fp->fi_fds[oflag]);
841 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
842 swap(f2, fp->fi_fds[O_RDWR]);
843 spin_unlock(&fp->fi_lock);
851 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
853 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
855 if (access & NFS4_SHARE_ACCESS_WRITE)
856 __nfs4_file_put_access(fp, O_WRONLY);
857 if (access & NFS4_SHARE_ACCESS_READ)
858 __nfs4_file_put_access(fp, O_RDONLY);
862 * Allocate a new open/delegation state counter. This is needed for
863 * pNFS for proper return on close semantics.
865 * Note that we only allocate it for pNFS-enabled exports, otherwise
866 * all pointers to struct nfs4_clnt_odstate are always NULL.
868 static struct nfs4_clnt_odstate *
869 alloc_clnt_odstate(struct nfs4_client *clp)
871 struct nfs4_clnt_odstate *co;
873 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
876 refcount_set(&co->co_odcount, 1);
882 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
884 struct nfs4_file *fp = co->co_file;
886 lockdep_assert_held(&fp->fi_lock);
887 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
891 get_clnt_odstate(struct nfs4_clnt_odstate *co)
894 refcount_inc(&co->co_odcount);
898 put_clnt_odstate(struct nfs4_clnt_odstate *co)
900 struct nfs4_file *fp;
906 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
907 list_del(&co->co_perfile);
908 spin_unlock(&fp->fi_lock);
910 nfsd4_return_all_file_layouts(co->co_client, fp);
911 kmem_cache_free(odstate_slab, co);
915 static struct nfs4_clnt_odstate *
916 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
918 struct nfs4_clnt_odstate *co;
919 struct nfs4_client *cl;
926 spin_lock(&fp->fi_lock);
927 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
928 if (co->co_client == cl) {
929 get_clnt_odstate(co);
935 hash_clnt_odstate_locked(new);
937 spin_unlock(&fp->fi_lock);
941 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
942 void (*sc_free)(struct nfs4_stid *))
944 struct nfs4_stid *stid;
947 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
951 idr_preload(GFP_KERNEL);
952 spin_lock(&cl->cl_lock);
953 /* Reserving 0 for start of file in nfsdfs "states" file: */
954 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
955 spin_unlock(&cl->cl_lock);
960 stid->sc_free = sc_free;
961 stid->sc_client = cl;
962 stid->sc_stateid.si_opaque.so_id = new_id;
963 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
964 /* Will be incremented before return to client: */
965 refcount_set(&stid->sc_count, 1);
966 spin_lock_init(&stid->sc_lock);
967 INIT_LIST_HEAD(&stid->sc_cp_list);
970 * It shouldn't be a problem to reuse an opaque stateid value.
971 * I don't think it is for 4.1. But with 4.0 I worry that, for
972 * example, a stray write retransmission could be accepted by
973 * the server when it should have been rejected. Therefore,
974 * adopt a trick from the sctp code to attempt to maximize the
975 * amount of time until an id is reused, by ensuring they always
976 * "increase" (mod INT_MAX):
980 kmem_cache_free(slab, stid);
985 * Create a unique stateid_t to represent each COPY.
987 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
988 unsigned char cs_type)
992 stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
993 stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
994 stid->cs_type = cs_type;
996 idr_preload(GFP_KERNEL);
997 spin_lock(&nn->s2s_cp_lock);
998 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
999 stid->cs_stid.si_opaque.so_id = new_id;
1000 stid->cs_stid.si_generation = 1;
1001 spin_unlock(&nn->s2s_cp_lock);
1008 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
1010 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID);
1013 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
1014 struct nfs4_stid *p_stid)
1016 struct nfs4_cpntf_state *cps;
1018 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
1021 cps->cpntf_time = ktime_get_boottime_seconds();
1022 refcount_set(&cps->cp_stateid.cs_count, 1);
1023 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
1025 spin_lock(&nn->s2s_cp_lock);
1026 list_add(&cps->cp_list, &p_stid->sc_cp_list);
1027 spin_unlock(&nn->s2s_cp_lock);
1034 void nfs4_free_copy_state(struct nfsd4_copy *copy)
1036 struct nfsd_net *nn;
1038 WARN_ON_ONCE(copy->cp_stateid.cs_type != NFS4_COPY_STID);
1039 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
1040 spin_lock(&nn->s2s_cp_lock);
1041 idr_remove(&nn->s2s_cp_stateids,
1042 copy->cp_stateid.cs_stid.si_opaque.so_id);
1043 spin_unlock(&nn->s2s_cp_lock);
1046 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1048 struct nfs4_cpntf_state *cps;
1049 struct nfsd_net *nn;
1051 nn = net_generic(net, nfsd_net_id);
1052 spin_lock(&nn->s2s_cp_lock);
1053 while (!list_empty(&stid->sc_cp_list)) {
1054 cps = list_first_entry(&stid->sc_cp_list,
1055 struct nfs4_cpntf_state, cp_list);
1056 _free_cpntf_state_locked(nn, cps);
1058 spin_unlock(&nn->s2s_cp_lock);
1061 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
1063 struct nfs4_stid *stid;
1065 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
1069 return openlockstateid(stid);
1072 static void nfs4_free_deleg(struct nfs4_stid *stid)
1074 struct nfs4_delegation *dp = delegstateid(stid);
1076 WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
1077 WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
1078 WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
1079 WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
1080 kmem_cache_free(deleg_slab, stid);
1081 atomic_long_dec(&num_delegations);
1085 * When we recall a delegation, we should be careful not to hand it
1086 * out again straight away.
1087 * To ensure this we keep a pair of bloom filters ('new' and 'old')
1088 * in which the filehandles of recalled delegations are "stored".
1089 * If a filehandle appear in either filter, a delegation is blocked.
1090 * When a delegation is recalled, the filehandle is stored in the "new"
1092 * Every 30 seconds we swap the filters and clear the "new" one,
1093 * unless both are empty of course.
1095 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
1096 * low 3 bytes as hash-table indices.
1098 * 'blocked_delegations_lock', which is always taken in block_delegations(),
1099 * is used to manage concurrent access. Testing does not need the lock
1100 * except when swapping the two filters.
1102 static DEFINE_SPINLOCK(blocked_delegations_lock);
1103 static struct bloom_pair {
1104 int entries, old_entries;
1106 int new; /* index into 'set' */
1107 DECLARE_BITMAP(set[2], 256);
1108 } blocked_delegations;
1110 static int delegation_blocked(struct knfsd_fh *fh)
1113 struct bloom_pair *bd = &blocked_delegations;
1115 if (bd->entries == 0)
1117 if (ktime_get_seconds() - bd->swap_time > 30) {
1118 spin_lock(&blocked_delegations_lock);
1119 if (ktime_get_seconds() - bd->swap_time > 30) {
1120 bd->entries -= bd->old_entries;
1121 bd->old_entries = bd->entries;
1122 memset(bd->set[bd->new], 0,
1123 sizeof(bd->set[0]));
1124 bd->new = 1-bd->new;
1125 bd->swap_time = ktime_get_seconds();
1127 spin_unlock(&blocked_delegations_lock);
1129 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1130 if (test_bit(hash&255, bd->set[0]) &&
1131 test_bit((hash>>8)&255, bd->set[0]) &&
1132 test_bit((hash>>16)&255, bd->set[0]))
1135 if (test_bit(hash&255, bd->set[1]) &&
1136 test_bit((hash>>8)&255, bd->set[1]) &&
1137 test_bit((hash>>16)&255, bd->set[1]))
1143 static void block_delegations(struct knfsd_fh *fh)
1146 struct bloom_pair *bd = &blocked_delegations;
1148 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1150 spin_lock(&blocked_delegations_lock);
1151 __set_bit(hash&255, bd->set[bd->new]);
1152 __set_bit((hash>>8)&255, bd->set[bd->new]);
1153 __set_bit((hash>>16)&255, bd->set[bd->new]);
1154 if (bd->entries == 0)
1155 bd->swap_time = ktime_get_seconds();
1157 spin_unlock(&blocked_delegations_lock);
1160 static struct nfs4_delegation *
1161 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1162 struct nfs4_clnt_odstate *odstate)
1164 struct nfs4_delegation *dp;
1167 dprintk("NFSD alloc_init_deleg\n");
1168 n = atomic_long_inc_return(&num_delegations);
1169 if (n < 0 || n > max_delegations)
1171 if (delegation_blocked(&fp->fi_fhandle))
1173 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
1178 * delegation seqid's are never incremented. The 4.1 special
1179 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1180 * 0 anyway just for consistency and use 1:
1182 dp->dl_stid.sc_stateid.si_generation = 1;
1183 INIT_LIST_HEAD(&dp->dl_perfile);
1184 INIT_LIST_HEAD(&dp->dl_perclnt);
1185 INIT_LIST_HEAD(&dp->dl_recall_lru);
1186 dp->dl_clnt_odstate = odstate;
1187 get_clnt_odstate(odstate);
1188 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
1190 dp->dl_recalled = false;
1191 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1192 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1194 dp->dl_stid.sc_file = fp;
1197 atomic_long_dec(&num_delegations);
1202 nfs4_put_stid(struct nfs4_stid *s)
1204 struct nfs4_file *fp = s->sc_file;
1205 struct nfs4_client *clp = s->sc_client;
1207 might_lock(&clp->cl_lock);
1209 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1210 wake_up_all(&close_wq);
1213 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1214 nfs4_free_cpntf_statelist(clp->net, s);
1215 spin_unlock(&clp->cl_lock);
1222 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1224 stateid_t *src = &stid->sc_stateid;
1226 spin_lock(&stid->sc_lock);
1227 if (unlikely(++src->si_generation == 0))
1228 src->si_generation = 1;
1229 memcpy(dst, src, sizeof(*dst));
1230 spin_unlock(&stid->sc_lock);
1233 static void put_deleg_file(struct nfs4_file *fp)
1235 struct nfsd_file *nf = NULL;
1237 spin_lock(&fp->fi_lock);
1238 if (--fp->fi_delegees == 0)
1239 swap(nf, fp->fi_deleg_file);
1240 spin_unlock(&fp->fi_lock);
1246 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1248 struct nfs4_file *fp = dp->dl_stid.sc_file;
1249 struct nfsd_file *nf = fp->fi_deleg_file;
1251 WARN_ON_ONCE(!fp->fi_delegees);
1253 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1257 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1259 put_clnt_odstate(dp->dl_clnt_odstate);
1260 nfs4_unlock_deleg_lease(dp);
1261 nfs4_put_stid(&dp->dl_stid);
1264 void nfs4_unhash_stid(struct nfs4_stid *s)
1270 * nfs4_delegation_exists - Discover if this delegation already exists
1271 * @clp: a pointer to the nfs4_client we're granting a delegation to
1272 * @fp: a pointer to the nfs4_file we're granting a delegation on
1275 * On success: true iff an existing delegation is found
1279 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1281 struct nfs4_delegation *searchdp = NULL;
1282 struct nfs4_client *searchclp = NULL;
1284 lockdep_assert_held(&state_lock);
1285 lockdep_assert_held(&fp->fi_lock);
1287 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1288 searchclp = searchdp->dl_stid.sc_client;
1289 if (clp == searchclp) {
1297 * hash_delegation_locked - Add a delegation to the appropriate lists
1298 * @dp: a pointer to the nfs4_delegation we are adding.
1299 * @fp: a pointer to the nfs4_file we're granting a delegation on
1302 * On success: NULL if the delegation was successfully hashed.
1304 * On error: -EAGAIN if one was previously granted to this
1305 * nfs4_client for this nfs4_file. Delegation is not hashed.
1310 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1312 struct nfs4_client *clp = dp->dl_stid.sc_client;
1314 lockdep_assert_held(&state_lock);
1315 lockdep_assert_held(&fp->fi_lock);
1317 if (nfs4_delegation_exists(clp, fp))
1319 refcount_inc(&dp->dl_stid.sc_count);
1320 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1321 list_add(&dp->dl_perfile, &fp->fi_delegations);
1322 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1326 static bool delegation_hashed(struct nfs4_delegation *dp)
1328 return !(list_empty(&dp->dl_perfile));
1332 unhash_delegation_locked(struct nfs4_delegation *dp)
1334 struct nfs4_file *fp = dp->dl_stid.sc_file;
1336 lockdep_assert_held(&state_lock);
1338 if (!delegation_hashed(dp))
1341 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1342 /* Ensure that deleg break won't try to requeue it */
1344 spin_lock(&fp->fi_lock);
1345 list_del_init(&dp->dl_perclnt);
1346 list_del_init(&dp->dl_recall_lru);
1347 list_del_init(&dp->dl_perfile);
1348 spin_unlock(&fp->fi_lock);
1352 static void destroy_delegation(struct nfs4_delegation *dp)
1356 spin_lock(&state_lock);
1357 unhashed = unhash_delegation_locked(dp);
1358 spin_unlock(&state_lock);
1360 destroy_unhashed_deleg(dp);
1363 static void revoke_delegation(struct nfs4_delegation *dp)
1365 struct nfs4_client *clp = dp->dl_stid.sc_client;
1367 WARN_ON(!list_empty(&dp->dl_recall_lru));
1369 if (clp->cl_minorversion) {
1370 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1371 refcount_inc(&dp->dl_stid.sc_count);
1372 spin_lock(&clp->cl_lock);
1373 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1374 spin_unlock(&clp->cl_lock);
1376 destroy_unhashed_deleg(dp);
1383 static unsigned int clientid_hashval(u32 id)
1385 return id & CLIENT_HASH_MASK;
1388 static unsigned int clientstr_hashval(struct xdr_netobj name)
1390 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1394 * A stateid that had a deny mode associated with it is being released
1395 * or downgraded. Recalculate the deny mode on the file.
1398 recalculate_deny_mode(struct nfs4_file *fp)
1400 struct nfs4_ol_stateid *stp;
1402 spin_lock(&fp->fi_lock);
1403 fp->fi_share_deny = 0;
1404 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1405 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1406 spin_unlock(&fp->fi_lock);
1410 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1413 bool change = false;
1415 for (i = 1; i < 4; i++) {
1416 if ((i & deny) != i) {
1422 /* Recalculate per-file deny mode if there was a change */
1424 recalculate_deny_mode(stp->st_stid.sc_file);
1427 /* release all access and file references for a given stateid */
1429 release_all_access(struct nfs4_ol_stateid *stp)
1432 struct nfs4_file *fp = stp->st_stid.sc_file;
1434 if (fp && stp->st_deny_bmap != 0)
1435 recalculate_deny_mode(fp);
1437 for (i = 1; i < 4; i++) {
1438 if (test_access(i, stp))
1439 nfs4_file_put_access(stp->st_stid.sc_file, i);
1440 clear_access(i, stp);
1444 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1446 kfree(sop->so_owner.data);
1447 sop->so_ops->so_free(sop);
1450 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1452 struct nfs4_client *clp = sop->so_client;
1454 might_lock(&clp->cl_lock);
1456 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1458 sop->so_ops->so_unhash(sop);
1459 spin_unlock(&clp->cl_lock);
1460 nfs4_free_stateowner(sop);
1464 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1466 return list_empty(&stp->st_perfile);
1469 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1471 struct nfs4_file *fp = stp->st_stid.sc_file;
1473 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1475 if (list_empty(&stp->st_perfile))
1478 spin_lock(&fp->fi_lock);
1479 list_del_init(&stp->st_perfile);
1480 spin_unlock(&fp->fi_lock);
1481 list_del(&stp->st_perstateowner);
1485 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1487 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1489 put_clnt_odstate(stp->st_clnt_odstate);
1490 release_all_access(stp);
1491 if (stp->st_stateowner)
1492 nfs4_put_stateowner(stp->st_stateowner);
1493 WARN_ON(!list_empty(&stid->sc_cp_list));
1494 kmem_cache_free(stateid_slab, stid);
1497 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1499 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1500 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1501 struct nfsd_file *nf;
1503 nf = find_any_file(stp->st_stid.sc_file);
1505 get_file(nf->nf_file);
1506 filp_close(nf->nf_file, (fl_owner_t)lo);
1509 nfs4_free_ol_stateid(stid);
1513 * Put the persistent reference to an already unhashed generic stateid, while
1514 * holding the cl_lock. If it's the last reference, then put it onto the
1515 * reaplist for later destruction.
1517 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1518 struct list_head *reaplist)
1520 struct nfs4_stid *s = &stp->st_stid;
1521 struct nfs4_client *clp = s->sc_client;
1523 lockdep_assert_held(&clp->cl_lock);
1525 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1527 if (!refcount_dec_and_test(&s->sc_count)) {
1528 wake_up_all(&close_wq);
1532 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1533 list_add(&stp->st_locks, reaplist);
1536 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1538 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1540 if (!unhash_ol_stateid(stp))
1542 list_del_init(&stp->st_locks);
1543 nfs4_unhash_stid(&stp->st_stid);
1547 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1549 struct nfs4_client *clp = stp->st_stid.sc_client;
1552 spin_lock(&clp->cl_lock);
1553 unhashed = unhash_lock_stateid(stp);
1554 spin_unlock(&clp->cl_lock);
1556 nfs4_put_stid(&stp->st_stid);
1559 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1561 struct nfs4_client *clp = lo->lo_owner.so_client;
1563 lockdep_assert_held(&clp->cl_lock);
1565 list_del_init(&lo->lo_owner.so_strhash);
1569 * Free a list of generic stateids that were collected earlier after being
1573 free_ol_stateid_reaplist(struct list_head *reaplist)
1575 struct nfs4_ol_stateid *stp;
1576 struct nfs4_file *fp;
1580 while (!list_empty(reaplist)) {
1581 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1583 list_del(&stp->st_locks);
1584 fp = stp->st_stid.sc_file;
1585 stp->st_stid.sc_free(&stp->st_stid);
1591 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1592 struct list_head *reaplist)
1594 struct nfs4_ol_stateid *stp;
1596 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1598 while (!list_empty(&open_stp->st_locks)) {
1599 stp = list_entry(open_stp->st_locks.next,
1600 struct nfs4_ol_stateid, st_locks);
1601 WARN_ON(!unhash_lock_stateid(stp));
1602 put_ol_stateid_locked(stp, reaplist);
1606 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1607 struct list_head *reaplist)
1609 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1611 if (!unhash_ol_stateid(stp))
1613 release_open_stateid_locks(stp, reaplist);
1617 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1619 LIST_HEAD(reaplist);
1621 spin_lock(&stp->st_stid.sc_client->cl_lock);
1622 if (unhash_open_stateid(stp, &reaplist))
1623 put_ol_stateid_locked(stp, &reaplist);
1624 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1625 free_ol_stateid_reaplist(&reaplist);
1628 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1630 struct nfs4_client *clp = oo->oo_owner.so_client;
1632 lockdep_assert_held(&clp->cl_lock);
1634 list_del_init(&oo->oo_owner.so_strhash);
1635 list_del_init(&oo->oo_perclient);
1638 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1640 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1642 struct nfs4_ol_stateid *s;
1644 spin_lock(&nn->client_lock);
1645 s = oo->oo_last_closed_stid;
1647 list_del_init(&oo->oo_close_lru);
1648 oo->oo_last_closed_stid = NULL;
1650 spin_unlock(&nn->client_lock);
1652 nfs4_put_stid(&s->st_stid);
1655 static void release_openowner(struct nfs4_openowner *oo)
1657 struct nfs4_ol_stateid *stp;
1658 struct nfs4_client *clp = oo->oo_owner.so_client;
1659 struct list_head reaplist;
1661 INIT_LIST_HEAD(&reaplist);
1663 spin_lock(&clp->cl_lock);
1664 unhash_openowner_locked(oo);
1665 while (!list_empty(&oo->oo_owner.so_stateids)) {
1666 stp = list_first_entry(&oo->oo_owner.so_stateids,
1667 struct nfs4_ol_stateid, st_perstateowner);
1668 if (unhash_open_stateid(stp, &reaplist))
1669 put_ol_stateid_locked(stp, &reaplist);
1671 spin_unlock(&clp->cl_lock);
1672 free_ol_stateid_reaplist(&reaplist);
1673 release_last_closed_stateid(oo);
1674 nfs4_put_stateowner(&oo->oo_owner);
1678 hash_sessionid(struct nfs4_sessionid *sessionid)
1680 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1682 return sid->sequence % SESSION_HASH_SIZE;
1685 #ifdef CONFIG_SUNRPC_DEBUG
1687 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1689 u32 *ptr = (u32 *)(&sessionid->data[0]);
1690 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1694 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1700 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1701 * won't be used for replay.
1703 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1705 struct nfs4_stateowner *so = cstate->replay_owner;
1707 if (nfserr == nfserr_replay_me)
1710 if (!seqid_mutating_err(ntohl(nfserr))) {
1711 nfsd4_cstate_clear_replay(cstate);
1716 if (so->so_is_open_owner)
1717 release_last_closed_stateid(openowner(so));
1723 gen_sessionid(struct nfsd4_session *ses)
1725 struct nfs4_client *clp = ses->se_client;
1726 struct nfsd4_sessionid *sid;
1728 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1729 sid->clientid = clp->cl_clientid;
1730 sid->sequence = current_sessionid++;
1735 * The protocol defines ca_maxresponssize_cached to include the size of
1736 * the rpc header, but all we need to cache is the data starting after
1737 * the end of the initial SEQUENCE operation--the rest we regenerate
1738 * each time. Therefore we can advertise a ca_maxresponssize_cached
1739 * value that is the number of bytes in our cache plus a few additional
1740 * bytes. In order to stay on the safe side, and not promise more than
1741 * we can cache, those additional bytes must be the minimum possible: 24
1742 * bytes of rpc header (xid through accept state, with AUTH_NULL
1743 * verifier), 12 for the compound header (with zero-length tag), and 44
1744 * for the SEQUENCE op response:
1746 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1749 free_session_slots(struct nfsd4_session *ses)
1753 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1754 free_svc_cred(&ses->se_slots[i]->sl_cred);
1755 kfree(ses->se_slots[i]);
1760 * We don't actually need to cache the rpc and session headers, so we
1761 * can allocate a little less for each slot:
1763 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1767 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1770 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1771 return size + sizeof(struct nfsd4_slot);
1775 * XXX: If we run out of reserved DRC memory we could (up to a point)
1776 * re-negotiate active sessions and reduce their slot usage to make
1777 * room for new connections. For now we just fail the create session.
1779 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1781 u32 slotsize = slot_bytes(ca);
1782 u32 num = ca->maxreqs;
1783 unsigned long avail, total_avail;
1784 unsigned int scale_factor;
1786 spin_lock(&nfsd_drc_lock);
1787 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1788 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1790 /* We have handed out more space than we chose in
1791 * set_max_drc() to allow. That isn't really a
1792 * problem as long as that doesn't make us think we
1793 * have lots more due to integer overflow.
1796 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1798 * Never use more than a fraction of the remaining memory,
1799 * unless it's the only way to give this client a slot.
1800 * The chosen fraction is either 1/8 or 1/number of threads,
1801 * whichever is smaller. This ensures there are adequate
1802 * slots to support multiple clients per thread.
1803 * Give the client one slot even if that would require
1804 * over-allocation--it is better than failure.
1806 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1808 avail = clamp_t(unsigned long, avail, slotsize,
1809 total_avail/scale_factor);
1810 num = min_t(int, num, avail / slotsize);
1811 num = max_t(int, num, 1);
1812 nfsd_drc_mem_used += num * slotsize;
1813 spin_unlock(&nfsd_drc_lock);
1818 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1820 int slotsize = slot_bytes(ca);
1822 spin_lock(&nfsd_drc_lock);
1823 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1824 spin_unlock(&nfsd_drc_lock);
1827 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1828 struct nfsd4_channel_attrs *battrs)
1830 int numslots = fattrs->maxreqs;
1831 int slotsize = slot_bytes(fattrs);
1832 struct nfsd4_session *new;
1835 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1836 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1837 mem = numslots * sizeof(struct nfsd4_slot *);
1839 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1842 /* allocate each struct nfsd4_slot and data cache in one piece */
1843 for (i = 0; i < numslots; i++) {
1844 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1845 if (!new->se_slots[i])
1849 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1850 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1855 kfree(new->se_slots[i]);
1860 static void free_conn(struct nfsd4_conn *c)
1862 svc_xprt_put(c->cn_xprt);
1866 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1868 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1869 struct nfs4_client *clp = c->cn_session->se_client;
1871 trace_nfsd_cb_lost(clp);
1873 spin_lock(&clp->cl_lock);
1874 if (!list_empty(&c->cn_persession)) {
1875 list_del(&c->cn_persession);
1878 nfsd4_probe_callback(clp);
1879 spin_unlock(&clp->cl_lock);
1882 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1884 struct nfsd4_conn *conn;
1886 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1889 svc_xprt_get(rqstp->rq_xprt);
1890 conn->cn_xprt = rqstp->rq_xprt;
1891 conn->cn_flags = flags;
1892 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1896 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1898 conn->cn_session = ses;
1899 list_add(&conn->cn_persession, &ses->se_conns);
1902 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1904 struct nfs4_client *clp = ses->se_client;
1906 spin_lock(&clp->cl_lock);
1907 __nfsd4_hash_conn(conn, ses);
1908 spin_unlock(&clp->cl_lock);
1911 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1913 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1914 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1917 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1921 nfsd4_hash_conn(conn, ses);
1922 ret = nfsd4_register_conn(conn);
1924 /* oops; xprt is already down: */
1925 nfsd4_conn_lost(&conn->cn_xpt_user);
1926 /* We may have gained or lost a callback channel: */
1927 nfsd4_probe_callback_sync(ses->se_client);
1930 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1932 u32 dir = NFS4_CDFC4_FORE;
1934 if (cses->flags & SESSION4_BACK_CHAN)
1935 dir |= NFS4_CDFC4_BACK;
1936 return alloc_conn(rqstp, dir);
1939 /* must be called under client_lock */
1940 static void nfsd4_del_conns(struct nfsd4_session *s)
1942 struct nfs4_client *clp = s->se_client;
1943 struct nfsd4_conn *c;
1945 spin_lock(&clp->cl_lock);
1946 while (!list_empty(&s->se_conns)) {
1947 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1948 list_del_init(&c->cn_persession);
1949 spin_unlock(&clp->cl_lock);
1951 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1954 spin_lock(&clp->cl_lock);
1956 spin_unlock(&clp->cl_lock);
1959 static void __free_session(struct nfsd4_session *ses)
1961 free_session_slots(ses);
1965 static void free_session(struct nfsd4_session *ses)
1967 nfsd4_del_conns(ses);
1968 nfsd4_put_drc_mem(&ses->se_fchannel);
1969 __free_session(ses);
1972 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1975 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1977 new->se_client = clp;
1980 INIT_LIST_HEAD(&new->se_conns);
1982 new->se_cb_seq_nr = 1;
1983 new->se_flags = cses->flags;
1984 new->se_cb_prog = cses->callback_prog;
1985 new->se_cb_sec = cses->cb_sec;
1986 atomic_set(&new->se_ref, 0);
1987 idx = hash_sessionid(&new->se_sessionid);
1988 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1989 spin_lock(&clp->cl_lock);
1990 list_add(&new->se_perclnt, &clp->cl_sessions);
1991 spin_unlock(&clp->cl_lock);
1994 struct sockaddr *sa = svc_addr(rqstp);
1996 * This is a little silly; with sessions there's no real
1997 * use for the callback address. Use the peer address
1998 * as a reasonable default for now, but consider fixing
1999 * the rpc client not to require an address in the
2002 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2003 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2007 /* caller must hold client_lock */
2008 static struct nfsd4_session *
2009 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2011 struct nfsd4_session *elem;
2013 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2015 lockdep_assert_held(&nn->client_lock);
2017 dump_sessionid(__func__, sessionid);
2018 idx = hash_sessionid(sessionid);
2019 /* Search in the appropriate list */
2020 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2021 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2022 NFS4_MAX_SESSIONID_LEN)) {
2027 dprintk("%s: session not found\n", __func__);
2031 static struct nfsd4_session *
2032 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2035 struct nfsd4_session *session;
2036 __be32 status = nfserr_badsession;
2038 session = __find_in_sessionid_hashtbl(sessionid, net);
2041 status = nfsd4_get_session_locked(session);
2049 /* caller must hold client_lock */
2051 unhash_session(struct nfsd4_session *ses)
2053 struct nfs4_client *clp = ses->se_client;
2054 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2056 lockdep_assert_held(&nn->client_lock);
2058 list_del(&ses->se_hash);
2059 spin_lock(&ses->se_client->cl_lock);
2060 list_del(&ses->se_perclnt);
2061 spin_unlock(&ses->se_client->cl_lock);
2064 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2066 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2069 * We're assuming the clid was not given out from a boot
2070 * precisely 2^32 (about 136 years) before this one. That seems
2071 * a safe assumption:
2073 if (clid->cl_boot == (u32)nn->boot_time)
2075 trace_nfsd_clid_stale(clid);
2080 * XXX Should we use a slab cache ?
2081 * This type of memory management is somewhat inefficient, but we use it
2082 * anyway since SETCLIENTID is not a common operation.
2084 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2085 struct nfsd_net *nn)
2087 struct nfs4_client *clp;
2090 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
2091 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2094 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2097 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2098 if (clp->cl_name.data == NULL)
2100 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2101 sizeof(struct list_head),
2103 if (!clp->cl_ownerstr_hashtbl)
2104 goto err_no_hashtbl;
2105 for (i = 0; i < OWNER_HASH_SIZE; i++)
2106 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2107 INIT_LIST_HEAD(&clp->cl_sessions);
2108 idr_init(&clp->cl_stateids);
2109 atomic_set(&clp->cl_rpc_users, 0);
2110 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2111 clp->cl_state = NFSD4_ACTIVE;
2112 atomic_inc(&nn->nfs4_client_count);
2113 atomic_set(&clp->cl_delegs_in_recall, 0);
2114 INIT_LIST_HEAD(&clp->cl_idhash);
2115 INIT_LIST_HEAD(&clp->cl_openowners);
2116 INIT_LIST_HEAD(&clp->cl_delegations);
2117 INIT_LIST_HEAD(&clp->cl_lru);
2118 INIT_LIST_HEAD(&clp->cl_revoked);
2119 #ifdef CONFIG_NFSD_PNFS
2120 INIT_LIST_HEAD(&clp->cl_lo_states);
2122 INIT_LIST_HEAD(&clp->async_copies);
2123 spin_lock_init(&clp->async_lock);
2124 spin_lock_init(&clp->cl_lock);
2125 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2128 kfree(clp->cl_name.data);
2130 kmem_cache_free(client_slab, clp);
2134 static void __free_client(struct kref *k)
2136 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2137 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2139 free_svc_cred(&clp->cl_cred);
2140 kfree(clp->cl_ownerstr_hashtbl);
2141 kfree(clp->cl_name.data);
2142 kfree(clp->cl_nii_domain.data);
2143 kfree(clp->cl_nii_name.data);
2144 idr_destroy(&clp->cl_stateids);
2145 kmem_cache_free(client_slab, clp);
2148 static void drop_client(struct nfs4_client *clp)
2150 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2154 free_client(struct nfs4_client *clp)
2156 while (!list_empty(&clp->cl_sessions)) {
2157 struct nfsd4_session *ses;
2158 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2160 list_del(&ses->se_perclnt);
2161 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2164 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2165 if (clp->cl_nfsd_dentry) {
2166 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2167 clp->cl_nfsd_dentry = NULL;
2168 wake_up_all(&expiry_wq);
2173 /* must be called under the client_lock */
2175 unhash_client_locked(struct nfs4_client *clp)
2177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2178 struct nfsd4_session *ses;
2180 lockdep_assert_held(&nn->client_lock);
2182 /* Mark the client as expired! */
2184 /* Make it invisible */
2185 if (!list_empty(&clp->cl_idhash)) {
2186 list_del_init(&clp->cl_idhash);
2187 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2188 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2190 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2192 list_del_init(&clp->cl_lru);
2193 spin_lock(&clp->cl_lock);
2194 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2195 list_del_init(&ses->se_hash);
2196 spin_unlock(&clp->cl_lock);
2200 unhash_client(struct nfs4_client *clp)
2202 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2204 spin_lock(&nn->client_lock);
2205 unhash_client_locked(clp);
2206 spin_unlock(&nn->client_lock);
2209 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2211 if (atomic_read(&clp->cl_rpc_users))
2212 return nfserr_jukebox;
2213 unhash_client_locked(clp);
2218 __destroy_client(struct nfs4_client *clp)
2220 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2222 struct nfs4_openowner *oo;
2223 struct nfs4_delegation *dp;
2224 struct list_head reaplist;
2226 INIT_LIST_HEAD(&reaplist);
2227 spin_lock(&state_lock);
2228 while (!list_empty(&clp->cl_delegations)) {
2229 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2230 WARN_ON(!unhash_delegation_locked(dp));
2231 list_add(&dp->dl_recall_lru, &reaplist);
2233 spin_unlock(&state_lock);
2234 while (!list_empty(&reaplist)) {
2235 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2236 list_del_init(&dp->dl_recall_lru);
2237 destroy_unhashed_deleg(dp);
2239 while (!list_empty(&clp->cl_revoked)) {
2240 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2241 list_del_init(&dp->dl_recall_lru);
2242 nfs4_put_stid(&dp->dl_stid);
2244 while (!list_empty(&clp->cl_openowners)) {
2245 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2246 nfs4_get_stateowner(&oo->oo_owner);
2247 release_openowner(oo);
2249 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2250 struct nfs4_stateowner *so, *tmp;
2252 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2254 /* Should be no openowners at this point */
2255 WARN_ON_ONCE(so->so_is_open_owner);
2256 remove_blocked_locks(lockowner(so));
2259 nfsd4_return_all_client_layouts(clp);
2260 nfsd4_shutdown_copy(clp);
2261 nfsd4_shutdown_callback(clp);
2262 if (clp->cl_cb_conn.cb_xprt)
2263 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2264 atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2265 nfsd4_dec_courtesy_client_count(nn, clp);
2267 wake_up_all(&expiry_wq);
2271 destroy_client(struct nfs4_client *clp)
2274 __destroy_client(clp);
2277 static void inc_reclaim_complete(struct nfs4_client *clp)
2279 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2281 if (!nn->track_reclaim_completes)
2283 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2285 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2286 nn->reclaim_str_hashtbl_size) {
2287 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2289 nfsd4_end_grace(nn);
2293 static void expire_client(struct nfs4_client *clp)
2296 nfsd4_client_record_remove(clp);
2297 __destroy_client(clp);
2300 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2302 memcpy(target->cl_verifier.data, source->data,
2303 sizeof(target->cl_verifier.data));
2306 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2308 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2309 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2312 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2314 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2315 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2317 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2318 if ((source->cr_principal && !target->cr_principal) ||
2319 (source->cr_raw_principal && !target->cr_raw_principal) ||
2320 (source->cr_targ_princ && !target->cr_targ_princ))
2323 target->cr_flavor = source->cr_flavor;
2324 target->cr_uid = source->cr_uid;
2325 target->cr_gid = source->cr_gid;
2326 target->cr_group_info = source->cr_group_info;
2327 get_group_info(target->cr_group_info);
2328 target->cr_gss_mech = source->cr_gss_mech;
2329 if (source->cr_gss_mech)
2330 gss_mech_get(source->cr_gss_mech);
2335 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2337 if (o1->len < o2->len)
2339 if (o1->len > o2->len)
2341 return memcmp(o1->data, o2->data, o1->len);
2345 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2347 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2351 same_clid(clientid_t *cl1, clientid_t *cl2)
2353 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2356 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2360 if (g1->ngroups != g2->ngroups)
2362 for (i=0; i<g1->ngroups; i++)
2363 if (!gid_eq(g1->gid[i], g2->gid[i]))
2369 * RFC 3530 language requires clid_inuse be returned when the
2370 * "principal" associated with a requests differs from that previously
2371 * used. We use uid, gid's, and gss principal string as our best
2372 * approximation. We also don't want to allow non-gss use of a client
2373 * established using gss: in theory cr_principal should catch that
2374 * change, but in practice cr_principal can be null even in the gss case
2375 * since gssd doesn't always pass down a principal string.
2377 static bool is_gss_cred(struct svc_cred *cr)
2379 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2380 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2385 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2387 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2388 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2389 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2390 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2392 /* XXX: check that cr_targ_princ fields match ? */
2393 if (cr1->cr_principal == cr2->cr_principal)
2395 if (!cr1->cr_principal || !cr2->cr_principal)
2397 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2400 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2402 struct svc_cred *cr = &rqstp->rq_cred;
2405 if (!cr->cr_gss_mech)
2407 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2408 return service == RPC_GSS_SVC_INTEGRITY ||
2409 service == RPC_GSS_SVC_PRIVACY;
2412 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2414 struct svc_cred *cr = &rqstp->rq_cred;
2416 if (!cl->cl_mach_cred)
2418 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2420 if (!svc_rqst_integrity_protected(rqstp))
2422 if (cl->cl_cred.cr_raw_principal)
2423 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2424 cr->cr_raw_principal);
2425 if (!cr->cr_principal)
2427 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2430 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2435 * This is opaque to client, so no need to byte-swap. Use
2436 * __force to keep sparse happy
2438 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2439 verf[1] = (__force __be32)nn->clverifier_counter++;
2440 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2443 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2445 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2446 clp->cl_clientid.cl_id = nn->clientid_counter++;
2447 gen_confirm(clp, nn);
2450 static struct nfs4_stid *
2451 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2453 struct nfs4_stid *ret;
2455 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2456 if (!ret || !ret->sc_type)
2461 static struct nfs4_stid *
2462 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2464 struct nfs4_stid *s;
2466 spin_lock(&cl->cl_lock);
2467 s = find_stateid_locked(cl, t);
2469 if (typemask & s->sc_type)
2470 refcount_inc(&s->sc_count);
2474 spin_unlock(&cl->cl_lock);
2478 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2480 struct nfsdfs_client *nc;
2481 nc = get_nfsdfs_client(inode);
2484 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2487 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2489 seq_printf(m, "\"");
2490 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2491 seq_printf(m, "\"");
2494 static const char *cb_state2str(int state)
2499 case NFSD4_CB_UNKNOWN:
2503 case NFSD4_CB_FAULT:
2509 static int client_info_show(struct seq_file *m, void *v)
2511 struct inode *inode = file_inode(m->file);
2512 struct nfs4_client *clp;
2515 clp = get_nfsdfs_clp(inode);
2518 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2519 seq_printf(m, "clientid: 0x%llx\n", clid);
2520 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2522 if (clp->cl_state == NFSD4_COURTESY)
2523 seq_puts(m, "status: courtesy\n");
2524 else if (clp->cl_state == NFSD4_EXPIRABLE)
2525 seq_puts(m, "status: expirable\n");
2526 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2527 seq_puts(m, "status: confirmed\n");
2529 seq_puts(m, "status: unconfirmed\n");
2530 seq_printf(m, "seconds from last renew: %lld\n",
2531 ktime_get_boottime_seconds() - clp->cl_time);
2532 seq_printf(m, "name: ");
2533 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2534 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2535 if (clp->cl_nii_domain.data) {
2536 seq_printf(m, "Implementation domain: ");
2537 seq_quote_mem(m, clp->cl_nii_domain.data,
2538 clp->cl_nii_domain.len);
2539 seq_printf(m, "\nImplementation name: ");
2540 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2541 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2542 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2544 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2545 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
2551 DEFINE_SHOW_ATTRIBUTE(client_info);
2553 static void *states_start(struct seq_file *s, loff_t *pos)
2554 __acquires(&clp->cl_lock)
2556 struct nfs4_client *clp = s->private;
2557 unsigned long id = *pos;
2560 spin_lock(&clp->cl_lock);
2561 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2566 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2568 struct nfs4_client *clp = s->private;
2569 unsigned long id = *pos;
2574 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2579 static void states_stop(struct seq_file *s, void *v)
2580 __releases(&clp->cl_lock)
2582 struct nfs4_client *clp = s->private;
2584 spin_unlock(&clp->cl_lock);
2587 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2589 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2592 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2594 struct inode *inode = file_inode(f->nf_file);
2596 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2597 MAJOR(inode->i_sb->s_dev),
2598 MINOR(inode->i_sb->s_dev),
2602 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2604 seq_printf(s, "owner: ");
2605 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2608 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2610 seq_printf(s, "0x%.8x", stid->si_generation);
2611 seq_printf(s, "%12phN", &stid->si_opaque);
2614 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2616 struct nfs4_ol_stateid *ols;
2617 struct nfs4_file *nf;
2618 struct nfsd_file *file;
2619 struct nfs4_stateowner *oo;
2620 unsigned int access, deny;
2622 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2623 return 0; /* XXX: or SEQ_SKIP? */
2624 ols = openlockstateid(st);
2625 oo = ols->st_stateowner;
2628 spin_lock(&nf->fi_lock);
2629 file = find_any_file_locked(nf);
2633 seq_printf(s, "- ");
2634 nfs4_show_stateid(s, &st->sc_stateid);
2635 seq_printf(s, ": { type: open, ");
2637 access = bmap_to_share_mode(ols->st_access_bmap);
2638 deny = bmap_to_share_mode(ols->st_deny_bmap);
2640 seq_printf(s, "access: %s%s, ",
2641 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2642 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2643 seq_printf(s, "deny: %s%s, ",
2644 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2645 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2647 nfs4_show_superblock(s, file);
2648 seq_printf(s, ", ");
2649 nfs4_show_fname(s, file);
2650 seq_printf(s, ", ");
2651 nfs4_show_owner(s, oo);
2652 seq_printf(s, " }\n");
2654 spin_unlock(&nf->fi_lock);
2658 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2660 struct nfs4_ol_stateid *ols;
2661 struct nfs4_file *nf;
2662 struct nfsd_file *file;
2663 struct nfs4_stateowner *oo;
2665 ols = openlockstateid(st);
2666 oo = ols->st_stateowner;
2668 spin_lock(&nf->fi_lock);
2669 file = find_any_file_locked(nf);
2673 seq_printf(s, "- ");
2674 nfs4_show_stateid(s, &st->sc_stateid);
2675 seq_printf(s, ": { type: lock, ");
2678 * Note: a lock stateid isn't really the same thing as a lock,
2679 * it's the locking state held by one owner on a file, and there
2680 * may be multiple (or no) lock ranges associated with it.
2681 * (Same for the matter is true of open stateids.)
2684 nfs4_show_superblock(s, file);
2685 /* XXX: open stateid? */
2686 seq_printf(s, ", ");
2687 nfs4_show_fname(s, file);
2688 seq_printf(s, ", ");
2689 nfs4_show_owner(s, oo);
2690 seq_printf(s, " }\n");
2692 spin_unlock(&nf->fi_lock);
2696 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2698 struct nfs4_delegation *ds;
2699 struct nfs4_file *nf;
2700 struct nfsd_file *file;
2702 ds = delegstateid(st);
2704 spin_lock(&nf->fi_lock);
2705 file = find_deleg_file_locked(nf);
2709 seq_printf(s, "- ");
2710 nfs4_show_stateid(s, &st->sc_stateid);
2711 seq_printf(s, ": { type: deleg, ");
2713 /* Kinda dead code as long as we only support read delegs: */
2714 seq_printf(s, "access: %s, ",
2715 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2717 /* XXX: lease time, whether it's being recalled. */
2719 nfs4_show_superblock(s, file);
2720 seq_printf(s, ", ");
2721 nfs4_show_fname(s, file);
2722 seq_printf(s, " }\n");
2724 spin_unlock(&nf->fi_lock);
2728 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2730 struct nfs4_layout_stateid *ls;
2731 struct nfsd_file *file;
2733 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2736 seq_printf(s, "- ");
2737 nfs4_show_stateid(s, &st->sc_stateid);
2738 seq_printf(s, ": { type: layout, ");
2740 /* XXX: What else would be useful? */
2742 nfs4_show_superblock(s, file);
2743 seq_printf(s, ", ");
2744 nfs4_show_fname(s, file);
2745 seq_printf(s, " }\n");
2750 static int states_show(struct seq_file *s, void *v)
2752 struct nfs4_stid *st = v;
2754 switch (st->sc_type) {
2755 case NFS4_OPEN_STID:
2756 return nfs4_show_open(s, st);
2757 case NFS4_LOCK_STID:
2758 return nfs4_show_lock(s, st);
2759 case NFS4_DELEG_STID:
2760 return nfs4_show_deleg(s, st);
2761 case NFS4_LAYOUT_STID:
2762 return nfs4_show_layout(s, st);
2764 return 0; /* XXX: or SEQ_SKIP? */
2766 /* XXX: copy stateids? */
2769 static struct seq_operations states_seq_ops = {
2770 .start = states_start,
2771 .next = states_next,
2772 .stop = states_stop,
2776 static int client_states_open(struct inode *inode, struct file *file)
2779 struct nfs4_client *clp;
2782 clp = get_nfsdfs_clp(inode);
2786 ret = seq_open(file, &states_seq_ops);
2789 s = file->private_data;
2794 static int client_opens_release(struct inode *inode, struct file *file)
2796 struct seq_file *m = file->private_data;
2797 struct nfs4_client *clp = m->private;
2799 /* XXX: alternatively, we could get/drop in seq start/stop */
2804 static const struct file_operations client_states_fops = {
2805 .open = client_states_open,
2807 .llseek = seq_lseek,
2808 .release = client_opens_release,
2812 * Normally we refuse to destroy clients that are in use, but here the
2813 * administrator is telling us to just do it. We also want to wait
2814 * so the caller has a guarantee that the client's locks are gone by
2815 * the time the write returns:
2817 static void force_expire_client(struct nfs4_client *clp)
2819 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2820 bool already_expired;
2822 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2824 spin_lock(&nn->client_lock);
2826 spin_unlock(&nn->client_lock);
2828 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2829 spin_lock(&nn->client_lock);
2830 already_expired = list_empty(&clp->cl_lru);
2831 if (!already_expired)
2832 unhash_client_locked(clp);
2833 spin_unlock(&nn->client_lock);
2835 if (!already_expired)
2838 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2841 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2842 size_t size, loff_t *pos)
2845 struct nfs4_client *clp;
2847 data = simple_transaction_get(file, buf, size);
2849 return PTR_ERR(data);
2850 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2852 clp = get_nfsdfs_clp(file_inode(file));
2855 force_expire_client(clp);
2860 static const struct file_operations client_ctl_fops = {
2861 .write = client_ctl_write,
2862 .release = simple_transaction_release,
2865 static const struct tree_descr client_files[] = {
2866 [0] = {"info", &client_info_fops, S_IRUSR},
2867 [1] = {"states", &client_states_fops, S_IRUSR},
2868 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
2872 static struct nfs4_client *create_client(struct xdr_netobj name,
2873 struct svc_rqst *rqstp, nfs4_verifier *verf)
2875 struct nfs4_client *clp;
2876 struct sockaddr *sa = svc_addr(rqstp);
2878 struct net *net = SVC_NET(rqstp);
2879 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2880 struct dentry *dentries[ARRAY_SIZE(client_files)];
2882 clp = alloc_client(name, nn);
2886 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2892 kref_init(&clp->cl_nfsdfs.cl_ref);
2893 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2894 clp->cl_time = ktime_get_boottime_seconds();
2895 clear_bit(0, &clp->cl_cb_slot_busy);
2896 copy_verf(clp, verf);
2897 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2898 clp->cl_cb_session = NULL;
2900 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2901 nn, &clp->cl_nfsdfs,
2902 clp->cl_clientid.cl_id - nn->clientid_base,
2903 client_files, dentries);
2904 clp->cl_nfsd_info_dentry = dentries[0];
2905 if (!clp->cl_nfsd_dentry) {
2913 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2915 struct rb_node **new = &(root->rb_node), *parent = NULL;
2916 struct nfs4_client *clp;
2919 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2922 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2923 new = &((*new)->rb_left);
2925 new = &((*new)->rb_right);
2928 rb_link_node(&new_clp->cl_namenode, parent, new);
2929 rb_insert_color(&new_clp->cl_namenode, root);
2932 static struct nfs4_client *
2933 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2936 struct rb_node *node = root->rb_node;
2937 struct nfs4_client *clp;
2940 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2941 cmp = compare_blob(&clp->cl_name, name);
2943 node = node->rb_left;
2945 node = node->rb_right;
2953 add_to_unconfirmed(struct nfs4_client *clp)
2955 unsigned int idhashval;
2956 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2958 lockdep_assert_held(&nn->client_lock);
2960 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2961 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2962 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2963 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2964 renew_client_locked(clp);
2968 move_to_confirmed(struct nfs4_client *clp)
2970 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2971 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2973 lockdep_assert_held(&nn->client_lock);
2975 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2976 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2977 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2978 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2979 trace_nfsd_clid_confirmed(&clp->cl_clientid);
2980 renew_client_locked(clp);
2983 static struct nfs4_client *
2984 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2986 struct nfs4_client *clp;
2987 unsigned int idhashval = clientid_hashval(clid->cl_id);
2989 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2990 if (same_clid(&clp->cl_clientid, clid)) {
2991 if ((bool)clp->cl_minorversion != sessions)
2993 renew_client_locked(clp);
3000 static struct nfs4_client *
3001 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3003 struct list_head *tbl = nn->conf_id_hashtbl;
3005 lockdep_assert_held(&nn->client_lock);
3006 return find_client_in_id_table(tbl, clid, sessions);
3009 static struct nfs4_client *
3010 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3012 struct list_head *tbl = nn->unconf_id_hashtbl;
3014 lockdep_assert_held(&nn->client_lock);
3015 return find_client_in_id_table(tbl, clid, sessions);
3018 static bool clp_used_exchangeid(struct nfs4_client *clp)
3020 return clp->cl_exchange_flags != 0;
3023 static struct nfs4_client *
3024 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3026 lockdep_assert_held(&nn->client_lock);
3027 return find_clp_in_name_tree(name, &nn->conf_name_tree);
3030 static struct nfs4_client *
3031 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3033 lockdep_assert_held(&nn->client_lock);
3034 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3038 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3040 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3041 struct sockaddr *sa = svc_addr(rqstp);
3042 u32 scopeid = rpc_get_scope_id(sa);
3043 unsigned short expected_family;
3045 /* Currently, we only support tcp and tcp6 for the callback channel */
3046 if (se->se_callback_netid_len == 3 &&
3047 !memcmp(se->se_callback_netid_val, "tcp", 3))
3048 expected_family = AF_INET;
3049 else if (se->se_callback_netid_len == 4 &&
3050 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3051 expected_family = AF_INET6;
3055 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3056 se->se_callback_addr_len,
3057 (struct sockaddr *)&conn->cb_addr,
3058 sizeof(conn->cb_addr));
3060 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3063 if (conn->cb_addr.ss_family == AF_INET6)
3064 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3066 conn->cb_prog = se->se_callback_prog;
3067 conn->cb_ident = se->se_callback_ident;
3068 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3069 trace_nfsd_cb_args(clp, conn);
3072 conn->cb_addr.ss_family = AF_UNSPEC;
3073 conn->cb_addrlen = 0;
3074 trace_nfsd_cb_nodelegs(clp);
3079 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3082 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3084 struct xdr_buf *buf = resp->xdr->buf;
3085 struct nfsd4_slot *slot = resp->cstate.slot;
3088 dprintk("--> %s slot %p\n", __func__, slot);
3090 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3091 slot->sl_opcnt = resp->opcnt;
3092 slot->sl_status = resp->cstate.status;
3093 free_svc_cred(&slot->sl_cred);
3094 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3096 if (!nfsd4_cache_this(resp)) {
3097 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3100 slot->sl_flags |= NFSD4_SLOT_CACHED;
3102 base = resp->cstate.data_offset;
3103 slot->sl_datalen = buf->len - base;
3104 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3105 WARN(1, "%s: sessions DRC could not cache compound\n",
3111 * Encode the replay sequence operation from the slot values.
3112 * If cachethis is FALSE encode the uncached rep error on the next
3113 * operation which sets resp->p and increments resp->opcnt for
3114 * nfs4svc_encode_compoundres.
3118 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3119 struct nfsd4_compoundres *resp)
3121 struct nfsd4_op *op;
3122 struct nfsd4_slot *slot = resp->cstate.slot;
3124 /* Encode the replayed sequence operation */
3125 op = &args->ops[resp->opcnt - 1];
3126 nfsd4_encode_operation(resp, op);
3128 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3130 if (args->opcnt == 1) {
3132 * The original operation wasn't a solo sequence--we
3133 * always cache those--so this retry must not match the
3136 op->status = nfserr_seq_false_retry;
3138 op = &args->ops[resp->opcnt++];
3139 op->status = nfserr_retry_uncached_rep;
3140 nfsd4_encode_operation(resp, op);
3146 * The sequence operation is not cached because we can use the slot and
3150 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3151 struct nfsd4_sequence *seq)
3153 struct nfsd4_slot *slot = resp->cstate.slot;
3154 struct xdr_stream *xdr = resp->xdr;
3158 dprintk("--> %s slot %p\n", __func__, slot);
3160 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3164 p = xdr_reserve_space(xdr, slot->sl_datalen);
3167 return nfserr_serverfault;
3169 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3170 xdr_commit_encode(xdr);
3172 resp->opcnt = slot->sl_opcnt;
3173 return slot->sl_status;
3177 * Set the exchange_id flags returned by the server.
3180 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3182 #ifdef CONFIG_NFSD_PNFS
3183 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3185 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3188 /* Referrals are supported, Migration is not. */
3189 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3191 /* set the wire flags to return to client. */
3192 clid->flags = new->cl_exchange_flags;
3195 static bool client_has_openowners(struct nfs4_client *clp)
3197 struct nfs4_openowner *oo;
3199 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3200 if (!list_empty(&oo->oo_owner.so_stateids))
3206 static bool client_has_state(struct nfs4_client *clp)
3208 return client_has_openowners(clp)
3209 #ifdef CONFIG_NFSD_PNFS
3210 || !list_empty(&clp->cl_lo_states)
3212 || !list_empty(&clp->cl_delegations)
3213 || !list_empty(&clp->cl_sessions)
3214 || !list_empty(&clp->async_copies);
3217 static __be32 copy_impl_id(struct nfs4_client *clp,
3218 struct nfsd4_exchange_id *exid)
3220 if (!exid->nii_domain.data)
3222 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3223 if (!clp->cl_nii_domain.data)
3224 return nfserr_jukebox;
3225 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3226 if (!clp->cl_nii_name.data)
3227 return nfserr_jukebox;
3228 clp->cl_nii_time = exid->nii_time;
3233 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3234 union nfsd4_op_u *u)
3236 struct nfsd4_exchange_id *exid = &u->exchange_id;
3237 struct nfs4_client *conf, *new;
3238 struct nfs4_client *unconf = NULL;
3240 char addr_str[INET6_ADDRSTRLEN];
3241 nfs4_verifier verf = exid->verifier;
3242 struct sockaddr *sa = svc_addr(rqstp);
3243 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3244 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3246 rpc_ntop(sa, addr_str, sizeof(addr_str));
3247 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3248 "ip_addr=%s flags %x, spa_how %u\n",
3249 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3250 addr_str, exid->flags, exid->spa_how);
3252 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3253 return nfserr_inval;
3255 new = create_client(exid->clname, rqstp, &verf);
3257 return nfserr_jukebox;
3258 status = copy_impl_id(new, exid);
3262 switch (exid->spa_how) {
3264 exid->spo_must_enforce[0] = 0;
3265 exid->spo_must_enforce[1] = (
3266 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3267 1 << (OP_EXCHANGE_ID - 32) |
3268 1 << (OP_CREATE_SESSION - 32) |
3269 1 << (OP_DESTROY_SESSION - 32) |
3270 1 << (OP_DESTROY_CLIENTID - 32));
3272 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3273 1 << (OP_OPEN_DOWNGRADE) |
3275 1 << (OP_DELEGRETURN));
3277 exid->spo_must_allow[1] &= (
3278 1 << (OP_TEST_STATEID - 32) |
3279 1 << (OP_FREE_STATEID - 32));
3280 if (!svc_rqst_integrity_protected(rqstp)) {
3281 status = nfserr_inval;
3285 * Sometimes userspace doesn't give us a principal.
3286 * Which is a bug, really. Anyway, we can't enforce
3287 * MACH_CRED in that case, better to give up now:
3289 if (!new->cl_cred.cr_principal &&
3290 !new->cl_cred.cr_raw_principal) {
3291 status = nfserr_serverfault;
3294 new->cl_mach_cred = true;
3298 default: /* checked by xdr code */
3302 status = nfserr_encr_alg_unsupp;
3306 /* Cases below refer to rfc 5661 section 18.35.4: */
3307 spin_lock(&nn->client_lock);
3308 conf = find_confirmed_client_by_name(&exid->clname, nn);
3310 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3311 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3314 if (!clp_used_exchangeid(conf)) { /* buggy client */
3315 status = nfserr_inval;
3318 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3319 status = nfserr_wrong_cred;
3322 if (!creds_match) { /* case 9 */
3323 status = nfserr_perm;
3326 if (!verfs_match) { /* case 8 */
3327 status = nfserr_not_same;
3331 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3332 trace_nfsd_clid_confirmed_r(conf);
3335 if (!creds_match) { /* case 3 */
3336 if (client_has_state(conf)) {
3337 status = nfserr_clid_inuse;
3338 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3343 if (verfs_match) { /* case 2 */
3344 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3345 trace_nfsd_clid_confirmed_r(conf);
3348 /* case 5, client reboot */
3349 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3354 if (update) { /* case 7 */
3355 status = nfserr_noent;
3359 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3360 if (unconf) /* case 4, possible retry or client restart */
3361 unhash_client_locked(unconf);
3363 /* case 1, new owner ID */
3364 trace_nfsd_clid_fresh(new);
3368 status = mark_client_expired_locked(conf);
3371 trace_nfsd_clid_replaced(&conf->cl_clientid);
3373 new->cl_minorversion = cstate->minorversion;
3374 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3375 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3377 add_to_unconfirmed(new);
3380 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3381 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3383 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3384 nfsd4_set_ex_flags(conf, exid);
3386 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3387 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3391 spin_unlock(&nn->client_lock);
3396 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3397 expire_client(unconf);
3403 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3405 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3408 /* The slot is in use, and no response has been sent. */
3410 if (seqid == slot_seqid)
3411 return nfserr_jukebox;
3413 return nfserr_seq_misordered;
3415 /* Note unsigned 32-bit arithmetic handles wraparound: */
3416 if (likely(seqid == slot_seqid + 1))
3418 if (seqid == slot_seqid)
3419 return nfserr_replay_cache;
3420 return nfserr_seq_misordered;
3424 * Cache the create session result into the create session single DRC
3425 * slot cache by saving the xdr structure. sl_seqid has been set.
3426 * Do this for solo or embedded create session operations.
3429 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3430 struct nfsd4_clid_slot *slot, __be32 nfserr)
3432 slot->sl_status = nfserr;
3433 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3437 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3438 struct nfsd4_clid_slot *slot)
3440 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3441 return slot->sl_status;
3444 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3445 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3446 1 + /* MIN tag is length with zero, only length */ \
3447 3 + /* version, opcount, opcode */ \
3448 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3449 /* seqid, slotID, slotID, cache */ \
3450 4 ) * sizeof(__be32))
3452 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3453 2 + /* verifier: AUTH_NULL, length 0 */\
3455 1 + /* MIN tag is length with zero, only length */ \
3456 3 + /* opcount, opcode, opstatus*/ \
3457 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3458 /* seqid, slotID, slotID, slotID, status */ \
3459 5 ) * sizeof(__be32))
3461 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3463 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3465 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3466 return nfserr_toosmall;
3467 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3468 return nfserr_toosmall;
3469 ca->headerpadsz = 0;
3470 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3471 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3472 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3473 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3474 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3475 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3477 * Note decreasing slot size below client's request may make it
3478 * difficult for client to function correctly, whereas
3479 * decreasing the number of slots will (just?) affect
3480 * performance. When short on memory we therefore prefer to
3481 * decrease number of slots instead of their size. Clients that
3482 * request larger slots than they need will get poor results:
3483 * Note that we always allow at least one slot, because our
3484 * accounting is soft and provides no guarantees either way.
3486 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3492 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3493 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3495 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3496 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3498 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3499 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3501 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3502 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3503 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3504 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3507 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3509 ca->headerpadsz = 0;
3511 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3512 return nfserr_toosmall;
3513 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3514 return nfserr_toosmall;
3515 ca->maxresp_cached = 0;
3517 return nfserr_toosmall;
3522 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3524 switch (cbs->flavor) {
3530 * GSS case: the spec doesn't allow us to return this
3531 * error. But it also doesn't allow us not to support
3533 * I'd rather this fail hard than return some error the
3534 * client might think it can already handle:
3536 return nfserr_encr_alg_unsupp;
3541 nfsd4_create_session(struct svc_rqst *rqstp,
3542 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3544 struct nfsd4_create_session *cr_ses = &u->create_session;
3545 struct sockaddr *sa = svc_addr(rqstp);
3546 struct nfs4_client *conf, *unconf;
3547 struct nfs4_client *old = NULL;
3548 struct nfsd4_session *new;
3549 struct nfsd4_conn *conn;
3550 struct nfsd4_clid_slot *cs_slot = NULL;
3552 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3554 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3555 return nfserr_inval;
3556 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3559 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3562 status = check_backchannel_attrs(&cr_ses->back_channel);
3564 goto out_release_drc_mem;
3565 status = nfserr_jukebox;
3566 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3568 goto out_release_drc_mem;
3569 conn = alloc_conn_from_crses(rqstp, cr_ses);
3571 goto out_free_session;
3573 spin_lock(&nn->client_lock);
3574 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3575 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3576 WARN_ON_ONCE(conf && unconf);
3579 status = nfserr_wrong_cred;
3580 if (!nfsd4_mach_creds_match(conf, rqstp))
3582 cs_slot = &conf->cl_cs_slot;
3583 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3585 if (status == nfserr_replay_cache)
3586 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3589 } else if (unconf) {
3590 status = nfserr_clid_inuse;
3591 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3592 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3593 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
3596 status = nfserr_wrong_cred;
3597 if (!nfsd4_mach_creds_match(unconf, rqstp))
3599 cs_slot = &unconf->cl_cs_slot;
3600 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3602 /* an unconfirmed replay returns misordered */
3603 status = nfserr_seq_misordered;
3606 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3608 status = mark_client_expired_locked(old);
3613 trace_nfsd_clid_replaced(&old->cl_clientid);
3615 move_to_confirmed(unconf);
3618 status = nfserr_stale_clientid;
3622 /* Persistent sessions are not supported */
3623 cr_ses->flags &= ~SESSION4_PERSIST;
3624 /* Upshifting from TCP to RDMA is not supported */
3625 cr_ses->flags &= ~SESSION4_RDMA;
3627 init_session(rqstp, new, conf, cr_ses);
3628 nfsd4_get_session_locked(new);
3630 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3631 NFS4_MAX_SESSIONID_LEN);
3632 cs_slot->sl_seqid++;
3633 cr_ses->seqid = cs_slot->sl_seqid;
3635 /* cache solo and embedded create sessions under the client_lock */
3636 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3637 spin_unlock(&nn->client_lock);
3639 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3640 /* init connection and backchannel */
3641 nfsd4_init_conn(rqstp, conn, new);
3642 nfsd4_put_session(new);
3647 spin_unlock(&nn->client_lock);
3652 __free_session(new);
3653 out_release_drc_mem:
3654 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3658 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3661 case NFS4_CDFC4_FORE:
3662 case NFS4_CDFC4_BACK:
3664 case NFS4_CDFC4_FORE_OR_BOTH:
3665 case NFS4_CDFC4_BACK_OR_BOTH:
3666 *dir = NFS4_CDFC4_BOTH;
3669 return nfserr_inval;
3672 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3673 struct nfsd4_compound_state *cstate,
3674 union nfsd4_op_u *u)
3676 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3677 struct nfsd4_session *session = cstate->session;
3678 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3681 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3684 spin_lock(&nn->client_lock);
3685 session->se_cb_prog = bc->bc_cb_program;
3686 session->se_cb_sec = bc->bc_cb_sec;
3687 spin_unlock(&nn->client_lock);
3689 nfsd4_probe_callback(session->se_client);
3694 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3696 struct nfsd4_conn *c;
3698 list_for_each_entry(c, &s->se_conns, cn_persession) {
3699 if (c->cn_xprt == xpt) {
3706 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3707 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3709 struct nfs4_client *clp = session->se_client;
3710 struct svc_xprt *xpt = rqst->rq_xprt;
3711 struct nfsd4_conn *c;
3714 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3715 spin_lock(&clp->cl_lock);
3716 c = __nfsd4_find_conn(xpt, session);
3718 status = nfserr_noent;
3719 else if (req == c->cn_flags)
3721 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3722 c->cn_flags != NFS4_CDFC4_BACK)
3724 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3725 c->cn_flags != NFS4_CDFC4_FORE)
3728 status = nfserr_inval;
3729 spin_unlock(&clp->cl_lock);
3730 if (status == nfs_ok && conn)
3735 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3736 struct nfsd4_compound_state *cstate,
3737 union nfsd4_op_u *u)
3739 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3741 struct nfsd4_conn *conn;
3742 struct nfsd4_session *session;
3743 struct net *net = SVC_NET(rqstp);
3744 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3746 if (!nfsd4_last_compound_op(rqstp))
3747 return nfserr_not_only_op;
3748 spin_lock(&nn->client_lock);
3749 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3750 spin_unlock(&nn->client_lock);
3752 goto out_no_session;
3753 status = nfserr_wrong_cred;
3754 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3756 status = nfsd4_match_existing_connection(rqstp, session,
3758 if (status == nfs_ok) {
3759 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3760 bcts->dir == NFS4_CDFC4_BACK)
3761 conn->cn_flags |= NFS4_CDFC4_BACK;
3762 nfsd4_probe_callback(session->se_client);
3765 if (status == nfserr_inval)
3767 status = nfsd4_map_bcts_dir(&bcts->dir);
3770 conn = alloc_conn(rqstp, bcts->dir);
3771 status = nfserr_jukebox;
3774 nfsd4_init_conn(rqstp, conn, session);
3777 nfsd4_put_session(session);
3782 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3784 if (!cstate->session)
3786 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3790 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3791 union nfsd4_op_u *u)
3793 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3794 struct nfsd4_session *ses;
3796 int ref_held_by_me = 0;
3797 struct net *net = SVC_NET(r);
3798 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3800 status = nfserr_not_only_op;
3801 if (nfsd4_compound_in_session(cstate, sessionid)) {
3802 if (!nfsd4_last_compound_op(r))
3806 dump_sessionid(__func__, sessionid);
3807 spin_lock(&nn->client_lock);
3808 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3810 goto out_client_lock;
3811 status = nfserr_wrong_cred;
3812 if (!nfsd4_mach_creds_match(ses->se_client, r))
3813 goto out_put_session;
3814 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3816 goto out_put_session;
3817 unhash_session(ses);
3818 spin_unlock(&nn->client_lock);
3820 nfsd4_probe_callback_sync(ses->se_client);
3822 spin_lock(&nn->client_lock);
3825 nfsd4_put_session_locked(ses);
3827 spin_unlock(&nn->client_lock);
3832 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3834 struct nfs4_client *clp = ses->se_client;
3835 struct nfsd4_conn *c;
3836 __be32 status = nfs_ok;
3839 spin_lock(&clp->cl_lock);
3840 c = __nfsd4_find_conn(new->cn_xprt, ses);
3843 status = nfserr_conn_not_bound_to_session;
3844 if (clp->cl_mach_cred)
3846 __nfsd4_hash_conn(new, ses);
3847 spin_unlock(&clp->cl_lock);
3848 ret = nfsd4_register_conn(new);
3850 /* oops; xprt is already down: */
3851 nfsd4_conn_lost(&new->cn_xpt_user);
3854 spin_unlock(&clp->cl_lock);
3859 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3861 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3863 return args->opcnt > session->se_fchannel.maxops;
3866 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3867 struct nfsd4_session *session)
3869 struct xdr_buf *xb = &rqstp->rq_arg;
3871 return xb->len > session->se_fchannel.maxreq_sz;
3874 static bool replay_matches_cache(struct svc_rqst *rqstp,
3875 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3877 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3879 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3880 (bool)seq->cachethis)
3883 * If there's an error then the reply can have fewer ops than
3886 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3889 * But if we cached a reply with *more* ops than the call you're
3890 * sending us now, then this new call is clearly not really a
3891 * replay of the old one:
3893 if (slot->sl_opcnt > argp->opcnt)
3895 /* This is the only check explicitly called by spec: */
3896 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3899 * There may be more comparisons we could actually do, but the
3900 * spec doesn't require us to catch every case where the calls
3901 * don't match (that would require caching the call as well as
3902 * the reply), so we don't bother.
3908 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3909 union nfsd4_op_u *u)
3911 struct nfsd4_sequence *seq = &u->sequence;
3912 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3913 struct xdr_stream *xdr = resp->xdr;
3914 struct nfsd4_session *session;
3915 struct nfs4_client *clp;
3916 struct nfsd4_slot *slot;
3917 struct nfsd4_conn *conn;
3920 struct net *net = SVC_NET(rqstp);
3921 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3923 if (resp->opcnt != 1)
3924 return nfserr_sequence_pos;
3927 * Will be either used or freed by nfsd4_sequence_check_conn
3930 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3932 return nfserr_jukebox;
3934 spin_lock(&nn->client_lock);
3935 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3937 goto out_no_session;
3938 clp = session->se_client;
3940 status = nfserr_too_many_ops;
3941 if (nfsd4_session_too_many_ops(rqstp, session))
3942 goto out_put_session;
3944 status = nfserr_req_too_big;
3945 if (nfsd4_request_too_big(rqstp, session))
3946 goto out_put_session;
3948 status = nfserr_badslot;
3949 if (seq->slotid >= session->se_fchannel.maxreqs)
3950 goto out_put_session;
3952 slot = session->se_slots[seq->slotid];
3953 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3955 /* We do not negotiate the number of slots yet, so set the
3956 * maxslots to the session maxreqs which is used to encode
3957 * sr_highest_slotid and the sr_target_slot id to maxslots */
3958 seq->maxslots = session->se_fchannel.maxreqs;
3960 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3961 slot->sl_flags & NFSD4_SLOT_INUSE);
3962 if (status == nfserr_replay_cache) {
3963 status = nfserr_seq_misordered;
3964 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3965 goto out_put_session;
3966 status = nfserr_seq_false_retry;
3967 if (!replay_matches_cache(rqstp, seq, slot))
3968 goto out_put_session;
3969 cstate->slot = slot;
3970 cstate->session = session;
3972 /* Return the cached reply status and set cstate->status
3973 * for nfsd4_proc_compound processing */
3974 status = nfsd4_replay_cache_entry(resp, seq);
3975 cstate->status = nfserr_replay_cache;
3979 goto out_put_session;
3981 status = nfsd4_sequence_check_conn(conn, session);
3984 goto out_put_session;
3986 buflen = (seq->cachethis) ?
3987 session->se_fchannel.maxresp_cached :
3988 session->se_fchannel.maxresp_sz;
3989 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3991 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3992 goto out_put_session;
3993 svc_reserve(rqstp, buflen);
3996 /* Success! bump slot seqid */
3997 slot->sl_seqid = seq->seqid;
3998 slot->sl_flags |= NFSD4_SLOT_INUSE;
4000 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4002 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4004 cstate->slot = slot;
4005 cstate->session = session;
4009 switch (clp->cl_cb_state) {
4011 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
4013 case NFSD4_CB_FAULT:
4014 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4017 seq->status_flags = 0;
4019 if (!list_empty(&clp->cl_revoked))
4020 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4024 spin_unlock(&nn->client_lock);
4027 nfsd4_put_session_locked(session);
4028 goto out_no_session;
4032 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4034 struct nfsd4_compound_state *cs = &resp->cstate;
4036 if (nfsd4_has_session(cs)) {
4037 if (cs->status != nfserr_replay_cache) {
4038 nfsd4_store_cache_entry(resp);
4039 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4041 /* Drop session reference that was taken in nfsd4_sequence() */
4042 nfsd4_put_session(cs->session);
4044 put_client_renew(cs->clp);
4048 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4049 struct nfsd4_compound_state *cstate,
4050 union nfsd4_op_u *u)
4052 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4053 struct nfs4_client *conf, *unconf;
4054 struct nfs4_client *clp = NULL;
4056 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4058 spin_lock(&nn->client_lock);
4059 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4060 conf = find_confirmed_client(&dc->clientid, true, nn);
4061 WARN_ON_ONCE(conf && unconf);
4064 if (client_has_state(conf)) {
4065 status = nfserr_clientid_busy;
4068 status = mark_client_expired_locked(conf);
4075 status = nfserr_stale_clientid;
4078 if (!nfsd4_mach_creds_match(clp, rqstp)) {
4080 status = nfserr_wrong_cred;
4083 trace_nfsd_clid_destroyed(&clp->cl_clientid);
4084 unhash_client_locked(clp);
4086 spin_unlock(&nn->client_lock);
4093 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4094 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4096 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4097 struct nfs4_client *clp = cstate->clp;
4100 if (rc->rca_one_fs) {
4101 if (!cstate->current_fh.fh_dentry)
4102 return nfserr_nofilehandle;
4104 * We don't take advantage of the rca_one_fs case.
4105 * That's OK, it's optional, we can safely ignore it.
4110 status = nfserr_complete_already;
4111 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4114 status = nfserr_stale_clientid;
4115 if (is_client_expired(clp))
4117 * The following error isn't really legal.
4118 * But we only get here if the client just explicitly
4119 * destroyed the client. Surely it no longer cares what
4120 * error it gets back on an operation for the dead
4126 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4127 nfsd4_client_record_create(clp);
4128 inc_reclaim_complete(clp);
4134 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4135 union nfsd4_op_u *u)
4137 struct nfsd4_setclientid *setclid = &u->setclientid;
4138 struct xdr_netobj clname = setclid->se_name;
4139 nfs4_verifier clverifier = setclid->se_verf;
4140 struct nfs4_client *conf, *new;
4141 struct nfs4_client *unconf = NULL;
4143 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4145 new = create_client(clname, rqstp, &clverifier);
4147 return nfserr_jukebox;
4148 spin_lock(&nn->client_lock);
4149 conf = find_confirmed_client_by_name(&clname, nn);
4150 if (conf && client_has_state(conf)) {
4151 status = nfserr_clid_inuse;
4152 if (clp_used_exchangeid(conf))
4154 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4155 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4159 unconf = find_unconfirmed_client_by_name(&clname, nn);
4161 unhash_client_locked(unconf);
4163 if (same_verf(&conf->cl_verifier, &clverifier)) {
4164 copy_clid(new, conf);
4165 gen_confirm(new, nn);
4167 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4170 trace_nfsd_clid_fresh(new);
4171 new->cl_minorversion = 0;
4172 gen_callback(new, setclid, rqstp);
4173 add_to_unconfirmed(new);
4174 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4175 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4176 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4180 spin_unlock(&nn->client_lock);
4184 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4185 expire_client(unconf);
4191 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4192 struct nfsd4_compound_state *cstate,
4193 union nfsd4_op_u *u)
4195 struct nfsd4_setclientid_confirm *setclientid_confirm =
4196 &u->setclientid_confirm;
4197 struct nfs4_client *conf, *unconf;
4198 struct nfs4_client *old = NULL;
4199 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4200 clientid_t * clid = &setclientid_confirm->sc_clientid;
4202 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4204 if (STALE_CLIENTID(clid, nn))
4205 return nfserr_stale_clientid;
4207 spin_lock(&nn->client_lock);
4208 conf = find_confirmed_client(clid, false, nn);
4209 unconf = find_unconfirmed_client(clid, false, nn);
4211 * We try hard to give out unique clientid's, so if we get an
4212 * attempt to confirm the same clientid with a different cred,
4213 * the client may be buggy; this should never happen.
4215 * Nevertheless, RFC 7530 recommends INUSE for this case:
4217 status = nfserr_clid_inuse;
4218 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4219 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4222 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4223 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4226 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4227 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4230 status = nfserr_stale_clientid;
4236 unhash_client_locked(old);
4237 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4239 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4241 status = nfserr_clid_inuse;
4242 if (client_has_state(old)
4243 && !same_creds(&unconf->cl_cred,
4248 status = mark_client_expired_locked(old);
4253 trace_nfsd_clid_replaced(&old->cl_clientid);
4255 move_to_confirmed(unconf);
4258 get_client_locked(conf);
4259 spin_unlock(&nn->client_lock);
4261 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4262 nfsd4_probe_callback(conf);
4263 spin_lock(&nn->client_lock);
4264 put_client_renew_locked(conf);
4266 spin_unlock(&nn->client_lock);
4272 static struct nfs4_file *nfsd4_alloc_file(void)
4274 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4277 /* OPEN Share state helper functions */
4278 static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval,
4279 struct nfs4_file *fp)
4281 lockdep_assert_held(&state_lock);
4283 refcount_set(&fp->fi_ref, 1);
4284 spin_lock_init(&fp->fi_lock);
4285 INIT_LIST_HEAD(&fp->fi_stateids);
4286 INIT_LIST_HEAD(&fp->fi_delegations);
4287 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4288 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4289 fp->fi_deleg_file = NULL;
4290 fp->fi_had_conflict = false;
4291 fp->fi_share_deny = 0;
4292 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4293 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4294 fp->fi_aliased = false;
4295 fp->fi_inode = d_inode(fh->fh_dentry);
4296 #ifdef CONFIG_NFSD_PNFS
4297 INIT_LIST_HEAD(&fp->fi_lo_states);
4298 atomic_set(&fp->fi_lo_recalls, 0);
4300 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
4304 nfsd4_free_slabs(void)
4306 kmem_cache_destroy(client_slab);
4307 kmem_cache_destroy(openowner_slab);
4308 kmem_cache_destroy(lockowner_slab);
4309 kmem_cache_destroy(file_slab);
4310 kmem_cache_destroy(stateid_slab);
4311 kmem_cache_destroy(deleg_slab);
4312 kmem_cache_destroy(odstate_slab);
4316 nfsd4_init_slabs(void)
4318 client_slab = kmem_cache_create("nfsd4_clients",
4319 sizeof(struct nfs4_client), 0, 0, NULL);
4320 if (client_slab == NULL)
4322 openowner_slab = kmem_cache_create("nfsd4_openowners",
4323 sizeof(struct nfs4_openowner), 0, 0, NULL);
4324 if (openowner_slab == NULL)
4325 goto out_free_client_slab;
4326 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4327 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4328 if (lockowner_slab == NULL)
4329 goto out_free_openowner_slab;
4330 file_slab = kmem_cache_create("nfsd4_files",
4331 sizeof(struct nfs4_file), 0, 0, NULL);
4332 if (file_slab == NULL)
4333 goto out_free_lockowner_slab;
4334 stateid_slab = kmem_cache_create("nfsd4_stateids",
4335 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4336 if (stateid_slab == NULL)
4337 goto out_free_file_slab;
4338 deleg_slab = kmem_cache_create("nfsd4_delegations",
4339 sizeof(struct nfs4_delegation), 0, 0, NULL);
4340 if (deleg_slab == NULL)
4341 goto out_free_stateid_slab;
4342 odstate_slab = kmem_cache_create("nfsd4_odstate",
4343 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4344 if (odstate_slab == NULL)
4345 goto out_free_deleg_slab;
4348 out_free_deleg_slab:
4349 kmem_cache_destroy(deleg_slab);
4350 out_free_stateid_slab:
4351 kmem_cache_destroy(stateid_slab);
4353 kmem_cache_destroy(file_slab);
4354 out_free_lockowner_slab:
4355 kmem_cache_destroy(lockowner_slab);
4356 out_free_openowner_slab:
4357 kmem_cache_destroy(openowner_slab);
4358 out_free_client_slab:
4359 kmem_cache_destroy(client_slab);
4364 static unsigned long
4365 nfsd_courtesy_client_count(struct shrinker *shrink, struct shrink_control *sc)
4368 struct nfsd_net *nn = container_of(shrink,
4369 struct nfsd_net, nfsd_client_shrinker);
4371 cnt = atomic_read(&nn->nfsd_courtesy_clients);
4373 mod_delayed_work(laundry_wq, &nn->nfsd_shrinker_work, 0);
4374 return (unsigned long)cnt;
4377 static unsigned long
4378 nfsd_courtesy_client_scan(struct shrinker *shrink, struct shrink_control *sc)
4384 nfsd4_init_leases_net(struct nfsd_net *nn)
4389 nn->nfsd4_lease = 90; /* default lease time */
4390 nn->nfsd4_grace = 90;
4391 nn->somebody_reclaimed = false;
4392 nn->track_reclaim_completes = false;
4393 nn->clverifier_counter = get_random_u32();
4394 nn->clientid_base = get_random_u32();
4395 nn->clientid_counter = nn->clientid_base + 1;
4396 nn->s2s_cp_cl_id = nn->clientid_counter++;
4398 atomic_set(&nn->nfs4_client_count, 0);
4400 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4401 max_clients *= NFS4_CLIENTS_PER_GB;
4402 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4404 atomic_set(&nn->nfsd_courtesy_clients, 0);
4405 nn->nfsd_client_shrinker.scan_objects = nfsd_courtesy_client_scan;
4406 nn->nfsd_client_shrinker.count_objects = nfsd_courtesy_client_count;
4407 nn->nfsd_client_shrinker.seeks = DEFAULT_SEEKS;
4408 return register_shrinker(&nn->nfsd_client_shrinker, "nfsd-client");
4412 nfsd4_leases_net_shutdown(struct nfsd_net *nn)
4414 unregister_shrinker(&nn->nfsd_client_shrinker);
4417 static void init_nfs4_replay(struct nfs4_replay *rp)
4419 rp->rp_status = nfserr_serverfault;
4421 rp->rp_buf = rp->rp_ibuf;
4422 mutex_init(&rp->rp_mutex);
4425 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4426 struct nfs4_stateowner *so)
4428 if (!nfsd4_has_session(cstate)) {
4429 mutex_lock(&so->so_replay.rp_mutex);
4430 cstate->replay_owner = nfs4_get_stateowner(so);
4434 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4436 struct nfs4_stateowner *so = cstate->replay_owner;
4439 cstate->replay_owner = NULL;
4440 mutex_unlock(&so->so_replay.rp_mutex);
4441 nfs4_put_stateowner(so);
4445 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4447 struct nfs4_stateowner *sop;
4449 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4453 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4454 if (!sop->so_owner.data) {
4455 kmem_cache_free(slab, sop);
4459 INIT_LIST_HEAD(&sop->so_stateids);
4460 sop->so_client = clp;
4461 init_nfs4_replay(&sop->so_replay);
4462 atomic_set(&sop->so_count, 1);
4466 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4468 lockdep_assert_held(&clp->cl_lock);
4470 list_add(&oo->oo_owner.so_strhash,
4471 &clp->cl_ownerstr_hashtbl[strhashval]);
4472 list_add(&oo->oo_perclient, &clp->cl_openowners);
4475 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4477 unhash_openowner_locked(openowner(so));
4480 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4482 struct nfs4_openowner *oo = openowner(so);
4484 kmem_cache_free(openowner_slab, oo);
4487 static const struct nfs4_stateowner_operations openowner_ops = {
4488 .so_unhash = nfs4_unhash_openowner,
4489 .so_free = nfs4_free_openowner,
4492 static struct nfs4_ol_stateid *
4493 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4495 struct nfs4_ol_stateid *local, *ret = NULL;
4496 struct nfs4_openowner *oo = open->op_openowner;
4498 lockdep_assert_held(&fp->fi_lock);
4500 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4501 /* ignore lock owners */
4502 if (local->st_stateowner->so_is_open_owner == 0)
4504 if (local->st_stateowner != &oo->oo_owner)
4506 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4508 refcount_inc(&ret->st_stid.sc_count);
4516 nfsd4_verify_open_stid(struct nfs4_stid *s)
4518 __be32 ret = nfs_ok;
4520 switch (s->sc_type) {
4524 case NFS4_CLOSED_STID:
4525 case NFS4_CLOSED_DELEG_STID:
4526 ret = nfserr_bad_stateid;
4528 case NFS4_REVOKED_DELEG_STID:
4529 ret = nfserr_deleg_revoked;
4534 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4536 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4540 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4541 ret = nfsd4_verify_open_stid(&stp->st_stid);
4543 mutex_unlock(&stp->st_mutex);
4547 static struct nfs4_ol_stateid *
4548 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4550 struct nfs4_ol_stateid *stp;
4552 spin_lock(&fp->fi_lock);
4553 stp = nfsd4_find_existing_open(fp, open);
4554 spin_unlock(&fp->fi_lock);
4555 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4557 nfs4_put_stid(&stp->st_stid);
4562 static struct nfs4_openowner *
4563 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4564 struct nfsd4_compound_state *cstate)
4566 struct nfs4_client *clp = cstate->clp;
4567 struct nfs4_openowner *oo, *ret;
4569 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4572 oo->oo_owner.so_ops = &openowner_ops;
4573 oo->oo_owner.so_is_open_owner = 1;
4574 oo->oo_owner.so_seqid = open->op_seqid;
4576 if (nfsd4_has_session(cstate))
4577 oo->oo_flags |= NFS4_OO_CONFIRMED;
4579 oo->oo_last_closed_stid = NULL;
4580 INIT_LIST_HEAD(&oo->oo_close_lru);
4581 spin_lock(&clp->cl_lock);
4582 ret = find_openstateowner_str_locked(strhashval, open, clp);
4584 hash_openowner(oo, clp, strhashval);
4587 nfs4_free_stateowner(&oo->oo_owner);
4589 spin_unlock(&clp->cl_lock);
4593 static struct nfs4_ol_stateid *
4594 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4597 struct nfs4_openowner *oo = open->op_openowner;
4598 struct nfs4_ol_stateid *retstp = NULL;
4599 struct nfs4_ol_stateid *stp;
4602 /* We are moving these outside of the spinlocks to avoid the warnings */
4603 mutex_init(&stp->st_mutex);
4604 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4607 spin_lock(&oo->oo_owner.so_client->cl_lock);
4608 spin_lock(&fp->fi_lock);
4610 retstp = nfsd4_find_existing_open(fp, open);
4614 open->op_stp = NULL;
4615 refcount_inc(&stp->st_stid.sc_count);
4616 stp->st_stid.sc_type = NFS4_OPEN_STID;
4617 INIT_LIST_HEAD(&stp->st_locks);
4618 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4620 stp->st_stid.sc_file = fp;
4621 stp->st_access_bmap = 0;
4622 stp->st_deny_bmap = 0;
4623 stp->st_openstp = NULL;
4624 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4625 list_add(&stp->st_perfile, &fp->fi_stateids);
4628 spin_unlock(&fp->fi_lock);
4629 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4631 /* Handle races with CLOSE */
4632 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4633 nfs4_put_stid(&retstp->st_stid);
4636 /* To keep mutex tracking happy */
4637 mutex_unlock(&stp->st_mutex);
4644 * In the 4.0 case we need to keep the owners around a little while to handle
4645 * CLOSE replay. We still do need to release any file access that is held by
4646 * them before returning however.
4649 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4651 struct nfs4_ol_stateid *last;
4652 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4653 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4656 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4659 * We know that we hold one reference via nfsd4_close, and another
4660 * "persistent" reference for the client. If the refcount is higher
4661 * than 2, then there are still calls in progress that are using this
4662 * stateid. We can't put the sc_file reference until they are finished.
4663 * Wait for the refcount to drop to 2. Since it has been unhashed,
4664 * there should be no danger of the refcount going back up again at
4667 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4669 release_all_access(s);
4670 if (s->st_stid.sc_file) {
4671 put_nfs4_file(s->st_stid.sc_file);
4672 s->st_stid.sc_file = NULL;
4675 spin_lock(&nn->client_lock);
4676 last = oo->oo_last_closed_stid;
4677 oo->oo_last_closed_stid = s;
4678 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4679 oo->oo_time = ktime_get_boottime_seconds();
4680 spin_unlock(&nn->client_lock);
4682 nfs4_put_stid(&last->st_stid);
4685 /* search file_hashtbl[] for file */
4686 static struct nfs4_file *
4687 find_file_locked(struct svc_fh *fh, unsigned int hashval)
4689 struct nfs4_file *fp;
4691 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4692 lockdep_is_held(&state_lock)) {
4693 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4694 if (refcount_inc_not_zero(&fp->fi_ref))
4701 static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
4702 unsigned int hashval)
4704 struct nfs4_file *fp;
4705 struct nfs4_file *ret = NULL;
4706 bool alias_found = false;
4708 spin_lock(&state_lock);
4709 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4710 lockdep_is_held(&state_lock)) {
4711 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4712 if (refcount_inc_not_zero(&fp->fi_ref))
4714 } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
4715 fp->fi_aliased = alias_found = true;
4717 if (likely(ret == NULL)) {
4718 nfsd4_init_file(fh, hashval, new);
4719 new->fi_aliased = alias_found;
4722 spin_unlock(&state_lock);
4726 static struct nfs4_file * find_file(struct svc_fh *fh)
4728 struct nfs4_file *fp;
4729 unsigned int hashval = file_hashval(fh);
4732 fp = find_file_locked(fh, hashval);
4737 static struct nfs4_file *
4738 find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
4740 struct nfs4_file *fp;
4741 unsigned int hashval = file_hashval(fh);
4744 fp = find_file_locked(fh, hashval);
4749 return insert_file(new, fh, hashval);
4753 * Called to check deny when READ with all zero stateid or
4754 * WRITE with all zero or all one stateid
4757 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4759 struct nfs4_file *fp;
4760 __be32 ret = nfs_ok;
4762 fp = find_file(current_fh);
4765 /* Check for conflicting share reservations */
4766 spin_lock(&fp->fi_lock);
4767 if (fp->fi_share_deny & deny_type)
4768 ret = nfserr_locked;
4769 spin_unlock(&fp->fi_lock);
4774 static bool nfsd4_deleg_present(const struct inode *inode)
4776 struct file_lock_context *ctx = smp_load_acquire(&inode->i_flctx);
4778 return ctx && !list_empty_careful(&ctx->flc_lease);
4782 * nfsd_wait_for_delegreturn - wait for delegations to be returned
4783 * @rqstp: the RPC transaction being executed
4784 * @inode: in-core inode of the file being waited for
4786 * The timeout prevents deadlock if all nfsd threads happen to be
4787 * tied up waiting for returning delegations.
4790 * %true: delegation was returned
4791 * %false: timed out waiting for delegreturn
4793 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
4795 long __maybe_unused timeo;
4797 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
4798 NFSD_DELEGRETURN_TIMEOUT);
4799 trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
4803 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4805 struct nfs4_delegation *dp = cb_to_delegation(cb);
4806 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4809 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4812 * We can't do this in nfsd_break_deleg_cb because it is
4813 * already holding inode->i_lock.
4815 * If the dl_time != 0, then we know that it has already been
4816 * queued for a lease break. Don't queue it again.
4818 spin_lock(&state_lock);
4819 if (delegation_hashed(dp) && dp->dl_time == 0) {
4820 dp->dl_time = ktime_get_boottime_seconds();
4821 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4823 spin_unlock(&state_lock);
4826 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4827 struct rpc_task *task)
4829 struct nfs4_delegation *dp = cb_to_delegation(cb);
4831 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
4833 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4834 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4837 switch (task->tk_status) {
4840 case -NFS4ERR_DELAY:
4841 rpc_delay(task, 2 * HZ);
4844 case -NFS4ERR_BAD_STATEID:
4846 * Race: client probably got cb_recall before open reply
4847 * granting delegation.
4849 if (dp->dl_retries--) {
4850 rpc_delay(task, 2 * HZ);
4859 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4861 struct nfs4_delegation *dp = cb_to_delegation(cb);
4863 nfs4_put_stid(&dp->dl_stid);
4866 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4867 .prepare = nfsd4_cb_recall_prepare,
4868 .done = nfsd4_cb_recall_done,
4869 .release = nfsd4_cb_recall_release,
4872 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4875 * We're assuming the state code never drops its reference
4876 * without first removing the lease. Since we're in this lease
4877 * callback (and since the lease code is serialized by the
4878 * flc_lock) we know the server hasn't removed the lease yet, and
4879 * we know it's safe to take a reference.
4881 refcount_inc(&dp->dl_stid.sc_count);
4882 WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall));
4885 /* Called from break_lease() with flc_lock held. */
4887 nfsd_break_deleg_cb(struct file_lock *fl)
4889 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4890 struct nfs4_file *fp = dp->dl_stid.sc_file;
4891 struct nfs4_client *clp = dp->dl_stid.sc_client;
4892 struct nfsd_net *nn;
4894 trace_nfsd_cb_recall(&dp->dl_stid);
4896 dp->dl_recalled = true;
4897 atomic_inc(&clp->cl_delegs_in_recall);
4898 if (try_to_expire_client(clp)) {
4899 nn = net_generic(clp->net, nfsd_net_id);
4900 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
4904 * We don't want the locks code to timeout the lease for us;
4905 * we'll remove it ourself if a delegation isn't returned
4908 fl->fl_break_time = 0;
4910 spin_lock(&fp->fi_lock);
4911 fp->fi_had_conflict = true;
4912 nfsd_break_one_deleg(dp);
4913 spin_unlock(&fp->fi_lock);
4918 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
4919 * @fl: Lock state to check
4922 * %true: Lease conflict was resolved
4923 * %false: Lease conflict was not resolved.
4925 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4927 struct nfs4_delegation *dl = fl->fl_owner;
4928 struct svc_rqst *rqst;
4929 struct nfs4_client *clp;
4933 rqst = kthread_data(current);
4934 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4935 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
4937 clp = *(rqst->rq_lease_breaker);
4938 return dl->dl_stid.sc_client == clp;
4942 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4943 struct list_head *dispose)
4945 struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
4946 struct nfs4_client *clp = dp->dl_stid.sc_client;
4948 if (arg & F_UNLCK) {
4949 if (dp->dl_recalled)
4950 atomic_dec(&clp->cl_delegs_in_recall);
4951 return lease_modify(onlist, arg, dispose);
4956 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4957 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
4958 .lm_break = nfsd_break_deleg_cb,
4959 .lm_change = nfsd_change_deleg_cb,
4962 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4964 if (nfsd4_has_session(cstate))
4966 if (seqid == so->so_seqid - 1)
4967 return nfserr_replay_me;
4968 if (seqid == so->so_seqid)
4970 return nfserr_bad_seqid;
4973 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4974 struct nfsd_net *nn)
4976 struct nfs4_client *found;
4978 spin_lock(&nn->client_lock);
4979 found = find_confirmed_client(clid, sessions, nn);
4981 atomic_inc(&found->cl_rpc_users);
4982 spin_unlock(&nn->client_lock);
4986 static __be32 set_client(clientid_t *clid,
4987 struct nfsd4_compound_state *cstate,
4988 struct nfsd_net *nn)
4991 if (!same_clid(&cstate->clp->cl_clientid, clid))
4992 return nfserr_stale_clientid;
4995 if (STALE_CLIENTID(clid, nn))
4996 return nfserr_stale_clientid;
4998 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4999 * set cstate->clp), so session = false:
5001 cstate->clp = lookup_clientid(clid, false, nn);
5003 return nfserr_expired;
5008 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
5009 struct nfsd4_open *open, struct nfsd_net *nn)
5011 clientid_t *clientid = &open->op_clientid;
5012 struct nfs4_client *clp = NULL;
5013 unsigned int strhashval;
5014 struct nfs4_openowner *oo = NULL;
5018 * In case we need it later, after we've already created the
5019 * file and don't want to risk a further failure:
5021 open->op_file = nfsd4_alloc_file();
5022 if (open->op_file == NULL)
5023 return nfserr_jukebox;
5025 status = set_client(clientid, cstate, nn);
5030 strhashval = ownerstr_hashval(&open->op_owner);
5031 oo = find_openstateowner_str(strhashval, open, clp);
5032 open->op_openowner = oo;
5036 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5037 /* Replace unconfirmed owners without checking for replay. */
5038 release_openowner(oo);
5039 open->op_openowner = NULL;
5042 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5047 oo = alloc_init_open_stateowner(strhashval, open, cstate);
5049 return nfserr_jukebox;
5050 open->op_openowner = oo;
5052 open->op_stp = nfs4_alloc_open_stateid(clp);
5054 return nfserr_jukebox;
5056 if (nfsd4_has_session(cstate) &&
5057 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5058 open->op_odstate = alloc_clnt_odstate(clp);
5059 if (!open->op_odstate)
5060 return nfserr_jukebox;
5066 static inline __be32
5067 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5069 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
5070 return nfserr_openmode;
5075 static int share_access_to_flags(u32 share_access)
5077 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5080 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
5082 struct nfs4_stid *ret;
5084 ret = find_stateid_by_type(cl, s,
5085 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
5088 return delegstateid(ret);
5091 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5093 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5094 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5098 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5099 struct nfs4_delegation **dp)
5102 __be32 status = nfserr_bad_stateid;
5103 struct nfs4_delegation *deleg;
5105 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5108 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
5109 nfs4_put_stid(&deleg->dl_stid);
5110 if (cl->cl_minorversion)
5111 status = nfserr_deleg_revoked;
5114 flags = share_access_to_flags(open->op_share_access);
5115 status = nfs4_check_delegmode(deleg, flags);
5117 nfs4_put_stid(&deleg->dl_stid);
5122 if (!nfsd4_is_deleg_cur(open))
5126 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5130 static inline int nfs4_access_to_access(u32 nfs4_access)
5134 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5135 flags |= NFSD_MAY_READ;
5136 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5137 flags |= NFSD_MAY_WRITE;
5141 static inline __be32
5142 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5143 struct nfsd4_open *open)
5145 struct iattr iattr = {
5146 .ia_valid = ATTR_SIZE,
5149 struct nfsd_attrs attrs = {
5152 if (!open->op_truncate)
5154 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5155 return nfserr_inval;
5156 return nfsd_setattr(rqstp, fh, &attrs, 0, (time64_t)0);
5159 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5160 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5161 struct nfsd4_open *open, bool new_stp)
5163 struct nfsd_file *nf = NULL;
5165 int oflag = nfs4_access_to_omode(open->op_share_access);
5166 int access = nfs4_access_to_access(open->op_share_access);
5167 unsigned char old_access_bmap, old_deny_bmap;
5169 spin_lock(&fp->fi_lock);
5172 * Are we trying to set a deny mode that would conflict with
5175 status = nfs4_file_check_deny(fp, open->op_share_deny);
5176 if (status != nfs_ok) {
5177 if (status != nfserr_share_denied) {
5178 spin_unlock(&fp->fi_lock);
5181 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5182 stp, open->op_share_deny, false))
5183 status = nfserr_jukebox;
5184 spin_unlock(&fp->fi_lock);
5188 /* set access to the file */
5189 status = nfs4_file_get_access(fp, open->op_share_access);
5190 if (status != nfs_ok) {
5191 if (status != nfserr_share_denied) {
5192 spin_unlock(&fp->fi_lock);
5195 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5196 stp, open->op_share_access, true))
5197 status = nfserr_jukebox;
5198 spin_unlock(&fp->fi_lock);
5202 /* Set access bits in stateid */
5203 old_access_bmap = stp->st_access_bmap;
5204 set_access(open->op_share_access, stp);
5206 /* Set new deny mask */
5207 old_deny_bmap = stp->st_deny_bmap;
5208 set_deny(open->op_share_deny, stp);
5209 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5211 if (!fp->fi_fds[oflag]) {
5212 spin_unlock(&fp->fi_lock);
5214 status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
5215 open->op_filp, &nf);
5216 if (status != nfs_ok)
5217 goto out_put_access;
5219 spin_lock(&fp->fi_lock);
5220 if (!fp->fi_fds[oflag]) {
5221 fp->fi_fds[oflag] = nf;
5225 spin_unlock(&fp->fi_lock);
5229 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5232 goto out_put_access;
5234 status = nfsd4_truncate(rqstp, cur_fh, open);
5236 goto out_put_access;
5240 stp->st_access_bmap = old_access_bmap;
5241 nfs4_file_put_access(fp, open->op_share_access);
5242 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5247 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5248 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5249 struct nfsd4_open *open)
5252 unsigned char old_deny_bmap = stp->st_deny_bmap;
5254 if (!test_access(open->op_share_access, stp))
5255 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5257 /* test and set deny mode */
5258 spin_lock(&fp->fi_lock);
5259 status = nfs4_file_check_deny(fp, open->op_share_deny);
5260 if (status == nfs_ok) {
5261 if (status != nfserr_share_denied) {
5262 set_deny(open->op_share_deny, stp);
5263 fp->fi_share_deny |=
5264 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5266 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5267 stp, open->op_share_deny, false))
5268 status = nfserr_jukebox;
5271 spin_unlock(&fp->fi_lock);
5273 if (status != nfs_ok)
5276 status = nfsd4_truncate(rqstp, cur_fh, open);
5277 if (status != nfs_ok)
5278 reset_union_bmap_deny(old_deny_bmap, stp);
5282 /* Should we give out recallable state?: */
5283 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5285 if (clp->cl_cb_state == NFSD4_CB_UP)
5288 * In the sessions case, since we don't have to establish a
5289 * separate connection for callbacks, we assume it's OK
5290 * until we hear otherwise:
5292 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5295 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5298 struct file_lock *fl;
5300 fl = locks_alloc_lock();
5303 fl->fl_lmops = &nfsd_lease_mng_ops;
5304 fl->fl_flags = FL_DELEG;
5305 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5306 fl->fl_end = OFFSET_MAX;
5307 fl->fl_owner = (fl_owner_t)dp;
5308 fl->fl_pid = current->tgid;
5309 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5313 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5314 struct nfs4_file *fp)
5316 struct nfs4_ol_stateid *st;
5317 struct file *f = fp->fi_deleg_file->nf_file;
5318 struct inode *ino = locks_inode(f);
5321 writes = atomic_read(&ino->i_writecount);
5325 * There could be multiple filehandles (hence multiple
5326 * nfs4_files) referencing this file, but that's not too
5327 * common; let's just give up in that case rather than
5328 * trying to go look up all the clients using that other
5329 * nfs4_file as well:
5334 * If there's a close in progress, make sure that we see it
5335 * clear any fi_fds[] entries before we see it decrement
5338 smp_mb__after_atomic();
5340 if (fp->fi_fds[O_WRONLY])
5342 if (fp->fi_fds[O_RDWR])
5345 return -EAGAIN; /* There may be non-NFSv4 writers */
5347 * It's possible there are non-NFSv4 write opens in progress,
5348 * but if they haven't incremented i_writecount yet then they
5349 * also haven't called break lease yet; so, they'll break this
5350 * lease soon enough. So, all that's left to check for is NFSv4
5353 spin_lock(&fp->fi_lock);
5354 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5355 if (st->st_openstp == NULL /* it's an open */ &&
5356 access_permit_write(st) &&
5357 st->st_stid.sc_client != clp) {
5358 spin_unlock(&fp->fi_lock);
5362 spin_unlock(&fp->fi_lock);
5364 * There's a small chance that we could be racing with another
5365 * NFSv4 open. However, any open that hasn't added itself to
5366 * the fi_stateids list also hasn't called break_lease yet; so,
5367 * they'll break this lease soon enough.
5373 * It's possible that between opening the dentry and setting the delegation,
5374 * that it has been renamed or unlinked. Redo the lookup to verify that this
5378 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5379 struct svc_fh *parent)
5381 struct svc_export *exp;
5382 struct dentry *child;
5385 err = nfsd_lookup_dentry(open->op_rqstp, parent,
5386 open->op_fname, open->op_fnamelen,
5394 if (child != file_dentry(fp->fi_deleg_file->nf_file))
5400 static struct nfs4_delegation *
5401 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5402 struct svc_fh *parent)
5405 struct nfs4_client *clp = stp->st_stid.sc_client;
5406 struct nfs4_file *fp = stp->st_stid.sc_file;
5407 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
5408 struct nfs4_delegation *dp;
5409 struct nfsd_file *nf;
5410 struct file_lock *fl;
5413 * The fi_had_conflict and nfs_get_existing_delegation checks
5414 * here are just optimizations; we'll need to recheck them at
5417 if (fp->fi_had_conflict)
5418 return ERR_PTR(-EAGAIN);
5420 nf = find_readable_file(fp);
5423 * We probably could attempt another open and get a read
5424 * delegation, but for now, don't bother until the
5425 * client actually sends us one.
5427 return ERR_PTR(-EAGAIN);
5429 spin_lock(&state_lock);
5430 spin_lock(&fp->fi_lock);
5431 if (nfs4_delegation_exists(clp, fp))
5433 else if (!fp->fi_deleg_file) {
5434 fp->fi_deleg_file = nf;
5435 /* increment early to prevent fi_deleg_file from being
5437 fp->fi_delegees = 1;
5441 spin_unlock(&fp->fi_lock);
5442 spin_unlock(&state_lock);
5446 return ERR_PTR(status);
5449 dp = alloc_init_deleg(clp, fp, odstate);
5453 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5455 goto out_clnt_odstate;
5457 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5459 locks_free_lock(fl);
5461 goto out_clnt_odstate;
5464 status = nfsd4_verify_deleg_dentry(open, fp, parent);
5469 status = nfsd4_check_conflicting_opens(clp, fp);
5473 spin_lock(&state_lock);
5474 spin_lock(&fp->fi_lock);
5475 if (fp->fi_had_conflict)
5478 status = hash_delegation_locked(dp, fp);
5479 spin_unlock(&fp->fi_lock);
5480 spin_unlock(&state_lock);
5487 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5489 put_clnt_odstate(dp->dl_clnt_odstate);
5490 nfs4_put_stid(&dp->dl_stid);
5493 return ERR_PTR(status);
5496 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5498 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5499 if (status == -EAGAIN)
5500 open->op_why_no_deleg = WND4_CONTENTION;
5502 open->op_why_no_deleg = WND4_RESOURCE;
5503 switch (open->op_deleg_want) {
5504 case NFS4_SHARE_WANT_READ_DELEG:
5505 case NFS4_SHARE_WANT_WRITE_DELEG:
5506 case NFS4_SHARE_WANT_ANY_DELEG:
5508 case NFS4_SHARE_WANT_CANCEL:
5509 open->op_why_no_deleg = WND4_CANCELLED;
5511 case NFS4_SHARE_WANT_NO_DELEG:
5518 * Attempt to hand out a delegation.
5520 * Note we don't support write delegations, and won't until the vfs has
5521 * proper support for them.
5524 nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5525 struct svc_fh *currentfh)
5527 struct nfs4_delegation *dp;
5528 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5529 struct nfs4_client *clp = stp->st_stid.sc_client;
5530 struct svc_fh *parent = NULL;
5534 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5535 open->op_recall = 0;
5536 switch (open->op_claim_type) {
5537 case NFS4_OPEN_CLAIM_PREVIOUS:
5539 open->op_recall = 1;
5540 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5543 case NFS4_OPEN_CLAIM_NULL:
5546 case NFS4_OPEN_CLAIM_FH:
5548 * Let's not give out any delegations till everyone's
5549 * had the chance to reclaim theirs, *and* until
5550 * NLM locks have all been reclaimed:
5552 if (locks_in_grace(clp->net))
5554 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5560 dp = nfs4_set_delegation(open, stp, parent);
5564 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5566 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5567 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5568 nfs4_put_stid(&dp->dl_stid);
5571 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5572 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5573 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5574 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5575 open->op_recall = 1;
5578 /* 4.1 client asking for a delegation? */
5579 if (open->op_deleg_want)
5580 nfsd4_open_deleg_none_ext(open, status);
5584 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5585 struct nfs4_delegation *dp)
5587 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5588 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5589 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5590 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5591 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5592 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5593 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5594 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5596 /* Otherwise the client must be confused wanting a delegation
5597 * it already has, therefore we don't return
5598 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5603 * nfsd4_process_open2 - finish open processing
5604 * @rqstp: the RPC transaction being executed
5605 * @current_fh: NFSv4 COMPOUND's current filehandle
5606 * @open: OPEN arguments
5608 * If successful, (1) truncate the file if open->op_truncate was
5609 * set, (2) set open->op_stateid, (3) set open->op_delegation.
5611 * Returns %nfs_ok on success; otherwise an nfs4stat value in
5612 * network byte order is returned.
5615 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5617 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5618 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5619 struct nfs4_file *fp = NULL;
5620 struct nfs4_ol_stateid *stp = NULL;
5621 struct nfs4_delegation *dp = NULL;
5623 bool new_stp = false;
5626 * Lookup file; if found, lookup stateid and check open request,
5627 * and check for delegations in the process of being recalled.
5628 * If not found, create the nfs4_file struct
5630 fp = find_or_add_file(open->op_file, current_fh);
5631 if (fp != open->op_file) {
5632 status = nfs4_check_deleg(cl, open, &dp);
5635 stp = nfsd4_find_and_lock_existing_open(fp, open);
5637 open->op_file = NULL;
5638 status = nfserr_bad_stateid;
5639 if (nfsd4_is_deleg_cur(open))
5644 stp = init_open_stateid(fp, open);
5650 * OPEN the file, or upgrade an existing OPEN.
5651 * If truncate fails, the OPEN fails.
5653 * stp is already locked.
5656 /* Stateid was found, this is an OPEN upgrade */
5657 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5659 mutex_unlock(&stp->st_mutex);
5663 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
5665 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5666 release_open_stateid(stp);
5667 mutex_unlock(&stp->st_mutex);
5671 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5673 if (stp->st_clnt_odstate == open->op_odstate)
5674 open->op_odstate = NULL;
5677 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5678 mutex_unlock(&stp->st_mutex);
5680 if (nfsd4_has_session(&resp->cstate)) {
5681 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5682 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5683 open->op_why_no_deleg = WND4_NOT_WANTED;
5689 * Attempt to hand out a delegation. No error return, because the
5690 * OPEN succeeds even if we fail.
5692 nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
5695 trace_nfsd_open(&stp->st_stid.sc_stateid);
5697 /* 4.1 client trying to upgrade/downgrade delegation? */
5698 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5699 open->op_deleg_want)
5700 nfsd4_deleg_xgrade_none_ext(open, dp);
5704 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5705 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5707 * To finish the open response, we just need to set the rflags.
5709 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5710 if (nfsd4_has_session(&resp->cstate))
5711 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5712 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5713 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5716 nfs4_put_stid(&dp->dl_stid);
5718 nfs4_put_stid(&stp->st_stid);
5723 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5724 struct nfsd4_open *open)
5726 if (open->op_openowner) {
5727 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5729 nfsd4_cstate_assign_replay(cstate, so);
5730 nfs4_put_stateowner(so);
5733 kmem_cache_free(file_slab, open->op_file);
5735 nfs4_put_stid(&open->op_stp->st_stid);
5736 if (open->op_odstate)
5737 kmem_cache_free(odstate_slab, open->op_odstate);
5741 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5742 union nfsd4_op_u *u)
5744 clientid_t *clid = &u->renew;
5745 struct nfs4_client *clp;
5747 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5749 trace_nfsd_clid_renew(clid);
5750 status = set_client(clid, cstate, nn);
5754 if (!list_empty(&clp->cl_delegations)
5755 && clp->cl_cb_state != NFSD4_CB_UP)
5756 return nfserr_cb_path_down;
5761 nfsd4_end_grace(struct nfsd_net *nn)
5763 /* do nothing if grace period already ended */
5764 if (nn->grace_ended)
5767 trace_nfsd_grace_complete(nn);
5768 nn->grace_ended = true;
5770 * If the server goes down again right now, an NFSv4
5771 * client will still be allowed to reclaim after it comes back up,
5772 * even if it hasn't yet had a chance to reclaim state this time.
5775 nfsd4_record_grace_done(nn);
5777 * At this point, NFSv4 clients can still reclaim. But if the
5778 * server crashes, any that have not yet reclaimed will be out
5779 * of luck on the next boot.
5781 * (NFSv4.1+ clients are considered to have reclaimed once they
5782 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5783 * have reclaimed after their first OPEN.)
5785 locks_end_grace(&nn->nfsd4_manager);
5787 * At this point, and once lockd and/or any other containers
5788 * exit their grace period, further reclaims will fail and
5789 * regular locking can resume.
5794 * If we've waited a lease period but there are still clients trying to
5795 * reclaim, wait a little longer to give them a chance to finish.
5797 static bool clients_still_reclaiming(struct nfsd_net *nn)
5799 time64_t double_grace_period_end = nn->boot_time +
5800 2 * nn->nfsd4_lease;
5802 if (nn->track_reclaim_completes &&
5803 atomic_read(&nn->nr_reclaim_complete) ==
5804 nn->reclaim_str_hashtbl_size)
5806 if (!nn->somebody_reclaimed)
5808 nn->somebody_reclaimed = false;
5810 * If we've given them *two* lease times to reclaim, and they're
5811 * still not done, give up:
5813 if (ktime_get_boottime_seconds() > double_grace_period_end)
5818 struct laundry_time {
5823 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5825 time64_t time_remaining;
5827 if (last_refresh < lt->cutoff)
5829 time_remaining = last_refresh - lt->cutoff;
5830 lt->new_timeo = min(lt->new_timeo, time_remaining);
5834 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
5835 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5837 spin_lock_init(&nn->nfsd_ssc_lock);
5838 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5839 init_waitqueue_head(&nn->nfsd_ssc_waitq);
5841 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5844 * This is called when nfsd is being shutdown, after all inter_ssc
5845 * cleanup were done, to destroy the ssc delayed unmount list.
5847 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5849 struct nfsd4_ssc_umount_item *ni = NULL;
5850 struct nfsd4_ssc_umount_item *tmp;
5852 spin_lock(&nn->nfsd_ssc_lock);
5853 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5854 list_del(&ni->nsui_list);
5855 spin_unlock(&nn->nfsd_ssc_lock);
5856 mntput(ni->nsui_vfsmount);
5858 spin_lock(&nn->nfsd_ssc_lock);
5860 spin_unlock(&nn->nfsd_ssc_lock);
5863 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5865 bool do_wakeup = false;
5866 struct nfsd4_ssc_umount_item *ni = NULL;
5867 struct nfsd4_ssc_umount_item *tmp;
5869 spin_lock(&nn->nfsd_ssc_lock);
5870 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5871 if (time_after(jiffies, ni->nsui_expire)) {
5872 if (refcount_read(&ni->nsui_refcnt) > 1)
5875 /* mark being unmount */
5876 ni->nsui_busy = true;
5877 spin_unlock(&nn->nfsd_ssc_lock);
5878 mntput(ni->nsui_vfsmount);
5879 spin_lock(&nn->nfsd_ssc_lock);
5881 /* waiters need to start from begin of list */
5882 list_del(&ni->nsui_list);
5885 /* wakeup ssc_connect waiters */
5892 wake_up_all(&nn->nfsd_ssc_waitq);
5893 spin_unlock(&nn->nfsd_ssc_lock);
5897 /* Check if any lock belonging to this lockowner has any blockers */
5899 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
5901 struct file_lock_context *ctx;
5902 struct nfs4_ol_stateid *stp;
5903 struct nfs4_file *nf;
5905 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
5906 nf = stp->st_stid.sc_file;
5907 ctx = nf->fi_inode->i_flctx;
5910 if (locks_owner_has_blockers(ctx, lo))
5917 nfs4_anylock_blockers(struct nfs4_client *clp)
5920 struct nfs4_stateowner *so;
5921 struct nfs4_lockowner *lo;
5923 if (atomic_read(&clp->cl_delegs_in_recall))
5925 spin_lock(&clp->cl_lock);
5926 for (i = 0; i < OWNER_HASH_SIZE; i++) {
5927 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
5929 if (so->so_is_open_owner)
5932 if (nfs4_lockowner_has_blockers(lo)) {
5933 spin_unlock(&clp->cl_lock);
5938 spin_unlock(&clp->cl_lock);
5943 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
5944 struct laundry_time *lt)
5946 unsigned int maxreap, reapcnt = 0;
5947 struct list_head *pos, *next;
5948 struct nfs4_client *clp;
5950 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
5951 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
5952 INIT_LIST_HEAD(reaplist);
5953 spin_lock(&nn->client_lock);
5954 list_for_each_safe(pos, next, &nn->client_lru) {
5955 clp = list_entry(pos, struct nfs4_client, cl_lru);
5956 if (clp->cl_state == NFSD4_EXPIRABLE)
5958 if (!state_expired(lt, clp->cl_time))
5960 if (!atomic_read(&clp->cl_rpc_users)) {
5961 if (clp->cl_state == NFSD4_ACTIVE)
5962 atomic_inc(&nn->nfsd_courtesy_clients);
5963 clp->cl_state = NFSD4_COURTESY;
5965 if (!client_has_state(clp))
5967 if (!nfs4_anylock_blockers(clp))
5968 if (reapcnt >= maxreap)
5971 if (!mark_client_expired_locked(clp)) {
5972 list_add(&clp->cl_lru, reaplist);
5976 spin_unlock(&nn->client_lock);
5980 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
5981 struct list_head *reaplist)
5983 unsigned int maxreap = 0, reapcnt = 0;
5984 struct list_head *pos, *next;
5985 struct nfs4_client *clp;
5987 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
5988 INIT_LIST_HEAD(reaplist);
5990 spin_lock(&nn->client_lock);
5991 list_for_each_safe(pos, next, &nn->client_lru) {
5992 clp = list_entry(pos, struct nfs4_client, cl_lru);
5993 if (clp->cl_state == NFSD4_ACTIVE)
5995 if (reapcnt >= maxreap)
5997 if (!mark_client_expired_locked(clp)) {
5998 list_add(&clp->cl_lru, reaplist);
6002 spin_unlock(&nn->client_lock);
6006 nfs4_process_client_reaplist(struct list_head *reaplist)
6008 struct list_head *pos, *next;
6009 struct nfs4_client *clp;
6011 list_for_each_safe(pos, next, reaplist) {
6012 clp = list_entry(pos, struct nfs4_client, cl_lru);
6013 trace_nfsd_clid_purged(&clp->cl_clientid);
6014 list_del_init(&clp->cl_lru);
6020 nfs4_laundromat(struct nfsd_net *nn)
6022 struct nfs4_openowner *oo;
6023 struct nfs4_delegation *dp;
6024 struct nfs4_ol_stateid *stp;
6025 struct nfsd4_blocked_lock *nbl;
6026 struct list_head *pos, *next, reaplist;
6027 struct laundry_time lt = {
6028 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6029 .new_timeo = nn->nfsd4_lease
6031 struct nfs4_cpntf_state *cps;
6032 copy_stateid_t *cps_t;
6035 if (clients_still_reclaiming(nn)) {
6039 nfsd4_end_grace(nn);
6041 spin_lock(&nn->s2s_cp_lock);
6042 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6043 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6044 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
6045 state_expired(<, cps->cpntf_time))
6046 _free_cpntf_state_locked(nn, cps);
6048 spin_unlock(&nn->s2s_cp_lock);
6049 nfs4_get_client_reaplist(nn, &reaplist, <);
6050 nfs4_process_client_reaplist(&reaplist);
6052 spin_lock(&state_lock);
6053 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6054 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6055 if (!state_expired(<, dp->dl_time))
6057 WARN_ON(!unhash_delegation_locked(dp));
6058 list_add(&dp->dl_recall_lru, &reaplist);
6060 spin_unlock(&state_lock);
6061 while (!list_empty(&reaplist)) {
6062 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6064 list_del_init(&dp->dl_recall_lru);
6065 revoke_delegation(dp);
6068 spin_lock(&nn->client_lock);
6069 while (!list_empty(&nn->close_lru)) {
6070 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6072 if (!state_expired(<, oo->oo_time))
6074 list_del_init(&oo->oo_close_lru);
6075 stp = oo->oo_last_closed_stid;
6076 oo->oo_last_closed_stid = NULL;
6077 spin_unlock(&nn->client_lock);
6078 nfs4_put_stid(&stp->st_stid);
6079 spin_lock(&nn->client_lock);
6081 spin_unlock(&nn->client_lock);
6084 * It's possible for a client to try and acquire an already held lock
6085 * that is being held for a long time, and then lose interest in it.
6086 * So, we clean out any un-revisited request after a lease period
6087 * under the assumption that the client is no longer interested.
6089 * RFC5661, sec. 9.6 states that the client must not rely on getting
6090 * notifications and must continue to poll for locks, even when the
6091 * server supports them. Thus this shouldn't lead to clients blocking
6092 * indefinitely once the lock does become free.
6094 BUG_ON(!list_empty(&reaplist));
6095 spin_lock(&nn->blocked_locks_lock);
6096 while (!list_empty(&nn->blocked_locks_lru)) {
6097 nbl = list_first_entry(&nn->blocked_locks_lru,
6098 struct nfsd4_blocked_lock, nbl_lru);
6099 if (!state_expired(<, nbl->nbl_time))
6101 list_move(&nbl->nbl_lru, &reaplist);
6102 list_del_init(&nbl->nbl_list);
6104 spin_unlock(&nn->blocked_locks_lock);
6106 while (!list_empty(&reaplist)) {
6107 nbl = list_first_entry(&reaplist,
6108 struct nfsd4_blocked_lock, nbl_lru);
6109 list_del_init(&nbl->nbl_lru);
6110 free_blocked_lock(nbl);
6112 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6113 /* service the server-to-server copy delayed unmount list */
6114 nfsd4_ssc_expire_umount(nn);
6117 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
6120 static void laundromat_main(struct work_struct *);
6123 laundromat_main(struct work_struct *laundry)
6126 struct delayed_work *dwork = to_delayed_work(laundry);
6127 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6130 t = nfs4_laundromat(nn);
6131 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
6135 courtesy_client_reaper(struct work_struct *reaper)
6137 struct list_head reaplist;
6138 struct delayed_work *dwork = to_delayed_work(reaper);
6139 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6140 nfsd_shrinker_work);
6142 nfs4_get_courtesy_client_reaplist(nn, &reaplist);
6143 nfs4_process_client_reaplist(&reaplist);
6146 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
6148 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
6149 return nfserr_bad_stateid;
6154 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
6156 __be32 status = nfserr_openmode;
6158 /* For lock stateid's, we test the parent open, not the lock: */
6159 if (stp->st_openstp)
6160 stp = stp->st_openstp;
6161 if ((flags & WR_STATE) && !access_permit_write(stp))
6163 if ((flags & RD_STATE) && !access_permit_read(stp))
6170 static inline __be32
6171 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
6173 if (ONE_STATEID(stateid) && (flags & RD_STATE))
6175 else if (opens_in_grace(net)) {
6176 /* Answer in remaining cases depends on existence of
6177 * conflicting state; so we must wait out the grace period. */
6178 return nfserr_grace;
6179 } else if (flags & WR_STATE)
6180 return nfs4_share_conflict(current_fh,
6181 NFS4_SHARE_DENY_WRITE);
6182 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
6183 return nfs4_share_conflict(current_fh,
6184 NFS4_SHARE_DENY_READ);
6187 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
6190 * When sessions are used the stateid generation number is ignored
6193 if (has_session && in->si_generation == 0)
6196 if (in->si_generation == ref->si_generation)
6199 /* If the client sends us a stateid from the future, it's buggy: */
6200 if (nfsd4_stateid_generation_after(in, ref))
6201 return nfserr_bad_stateid;
6203 * However, we could see a stateid from the past, even from a
6204 * non-buggy client. For example, if the client sends a lock
6205 * while some IO is outstanding, the lock may bump si_generation
6206 * while the IO is still in flight. The client could avoid that
6207 * situation by waiting for responses on all the IO requests,
6208 * but better performance may result in retrying IO that
6209 * receives an old_stateid error if requests are rarely
6210 * reordered in flight:
6212 return nfserr_old_stateid;
6215 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
6219 spin_lock(&s->sc_lock);
6220 ret = nfsd4_verify_open_stid(s);
6222 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
6223 spin_unlock(&s->sc_lock);
6227 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6229 if (ols->st_stateowner->so_is_open_owner &&
6230 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6231 return nfserr_bad_stateid;
6235 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
6237 struct nfs4_stid *s;
6238 __be32 status = nfserr_bad_stateid;
6240 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6241 CLOSE_STATEID(stateid))
6243 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
6245 spin_lock(&cl->cl_lock);
6246 s = find_stateid_locked(cl, stateid);
6249 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
6252 switch (s->sc_type) {
6253 case NFS4_DELEG_STID:
6256 case NFS4_REVOKED_DELEG_STID:
6257 status = nfserr_deleg_revoked;
6259 case NFS4_OPEN_STID:
6260 case NFS4_LOCK_STID:
6261 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
6264 printk("unknown stateid type %x\n", s->sc_type);
6266 case NFS4_CLOSED_STID:
6267 case NFS4_CLOSED_DELEG_STID:
6268 status = nfserr_bad_stateid;
6271 spin_unlock(&cl->cl_lock);
6276 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6277 stateid_t *stateid, unsigned char typemask,
6278 struct nfs4_stid **s, struct nfsd_net *nn)
6281 struct nfs4_stid *stid;
6282 bool return_revoked = false;
6285 * only return revoked delegations if explicitly asked.
6286 * otherwise we report revoked or bad_stateid status.
6288 if (typemask & NFS4_REVOKED_DELEG_STID)
6289 return_revoked = true;
6290 else if (typemask & NFS4_DELEG_STID)
6291 typemask |= NFS4_REVOKED_DELEG_STID;
6293 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6294 CLOSE_STATEID(stateid))
6295 return nfserr_bad_stateid;
6296 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
6297 if (status == nfserr_stale_clientid) {
6298 if (cstate->session)
6299 return nfserr_bad_stateid;
6300 return nfserr_stale_stateid;
6304 stid = find_stateid_by_type(cstate->clp, stateid, typemask);
6306 return nfserr_bad_stateid;
6307 if ((stid->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
6308 nfs4_put_stid(stid);
6309 if (cstate->minorversion)
6310 return nfserr_deleg_revoked;
6311 return nfserr_bad_stateid;
6317 static struct nfsd_file *
6318 nfs4_find_file(struct nfs4_stid *s, int flags)
6323 switch (s->sc_type) {
6324 case NFS4_DELEG_STID:
6325 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
6327 return nfsd_file_get(s->sc_file->fi_deleg_file);
6328 case NFS4_OPEN_STID:
6329 case NFS4_LOCK_STID:
6330 if (flags & RD_STATE)
6331 return find_readable_file(s->sc_file);
6333 return find_writeable_file(s->sc_file);
6340 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
6344 status = nfsd4_check_openowner_confirmed(ols);
6347 return nfs4_check_openmode(ols, flags);
6351 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
6352 struct nfsd_file **nfp, int flags)
6354 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6355 struct nfsd_file *nf;
6358 nf = nfs4_find_file(s, flags);
6360 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6361 acc | NFSD_MAY_OWNER_OVERRIDE);
6367 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
6376 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6378 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
6379 if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
6381 list_del(&cps->cp_list);
6382 idr_remove(&nn->s2s_cp_stateids,
6383 cps->cp_stateid.cs_stid.si_opaque.so_id);
6387 * A READ from an inter server to server COPY will have a
6388 * copy stateid. Look up the copy notify stateid from the
6389 * idr structure and take a reference on it.
6391 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6392 struct nfs4_client *clp,
6393 struct nfs4_cpntf_state **cps)
6395 copy_stateid_t *cps_t;
6396 struct nfs4_cpntf_state *state = NULL;
6398 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6399 return nfserr_bad_stateid;
6400 spin_lock(&nn->s2s_cp_lock);
6401 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6403 state = container_of(cps_t, struct nfs4_cpntf_state,
6405 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
6410 refcount_inc(&state->cp_stateid.cs_count);
6412 _free_cpntf_state_locked(nn, state);
6415 spin_unlock(&nn->s2s_cp_lock);
6417 return nfserr_bad_stateid;
6423 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6424 struct nfs4_stid **stid)
6427 struct nfs4_cpntf_state *cps = NULL;
6428 struct nfs4_client *found;
6430 status = manage_cpntf_state(nn, st, NULL, &cps);
6434 cps->cpntf_time = ktime_get_boottime_seconds();
6436 status = nfserr_expired;
6437 found = lookup_clientid(&cps->cp_p_clid, true, nn);
6441 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6442 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6446 status = nfserr_bad_stateid;
6448 put_client_renew(found);
6450 nfs4_put_cpntf_state(nn, cps);
6454 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6456 spin_lock(&nn->s2s_cp_lock);
6457 _free_cpntf_state_locked(nn, cps);
6458 spin_unlock(&nn->s2s_cp_lock);
6462 * Checks for stateid operations
6465 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
6466 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
6467 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6468 struct nfs4_stid **cstid)
6470 struct net *net = SVC_NET(rqstp);
6471 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6472 struct nfs4_stid *s = NULL;
6478 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6480 status = nfserr_bad_stateid;
6482 status = check_special_stateids(net, fhp, stateid,
6487 status = nfsd4_lookup_stateid(cstate, stateid,
6488 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
6490 if (status == nfserr_bad_stateid)
6491 status = find_cpntf_state(nn, stateid, &s);
6494 status = nfsd4_stid_check_stateid_generation(stateid, s,
6495 nfsd4_has_session(cstate));
6499 switch (s->sc_type) {
6500 case NFS4_DELEG_STID:
6501 status = nfs4_check_delegmode(delegstateid(s), flags);
6503 case NFS4_OPEN_STID:
6504 case NFS4_LOCK_STID:
6505 status = nfs4_check_olstateid(openlockstateid(s), flags);
6508 status = nfserr_bad_stateid;
6513 status = nfs4_check_fh(fhp, s);
6516 if (status == nfs_ok && nfp)
6517 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
6520 if (!status && cstid)
6529 * Test if the stateid is valid
6532 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6533 union nfsd4_op_u *u)
6535 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
6536 struct nfsd4_test_stateid_id *stateid;
6537 struct nfs4_client *cl = cstate->clp;
6539 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
6540 stateid->ts_id_status =
6541 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
6547 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6549 struct nfs4_ol_stateid *stp = openlockstateid(s);
6552 ret = nfsd4_lock_ol_stateid(stp);
6556 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6560 ret = nfserr_locks_held;
6561 if (check_for_locks(stp->st_stid.sc_file,
6562 lockowner(stp->st_stateowner)))
6565 release_lock_stateid(stp);
6569 mutex_unlock(&stp->st_mutex);
6576 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6577 union nfsd4_op_u *u)
6579 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
6580 stateid_t *stateid = &free_stateid->fr_stateid;
6581 struct nfs4_stid *s;
6582 struct nfs4_delegation *dp;
6583 struct nfs4_client *cl = cstate->clp;
6584 __be32 ret = nfserr_bad_stateid;
6586 spin_lock(&cl->cl_lock);
6587 s = find_stateid_locked(cl, stateid);
6590 spin_lock(&s->sc_lock);
6591 switch (s->sc_type) {
6592 case NFS4_DELEG_STID:
6593 ret = nfserr_locks_held;
6595 case NFS4_OPEN_STID:
6596 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6599 ret = nfserr_locks_held;
6601 case NFS4_LOCK_STID:
6602 spin_unlock(&s->sc_lock);
6603 refcount_inc(&s->sc_count);
6604 spin_unlock(&cl->cl_lock);
6605 ret = nfsd4_free_lock_stateid(stateid, s);
6607 case NFS4_REVOKED_DELEG_STID:
6608 spin_unlock(&s->sc_lock);
6609 dp = delegstateid(s);
6610 list_del_init(&dp->dl_recall_lru);
6611 spin_unlock(&cl->cl_lock);
6615 /* Default falls through and returns nfserr_bad_stateid */
6617 spin_unlock(&s->sc_lock);
6619 spin_unlock(&cl->cl_lock);
6627 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6628 RD_STATE : WR_STATE;
6631 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6633 struct svc_fh *current_fh = &cstate->current_fh;
6634 struct nfs4_stateowner *sop = stp->st_stateowner;
6637 status = nfsd4_check_seqid(cstate, sop, seqid);
6640 status = nfsd4_lock_ol_stateid(stp);
6641 if (status != nfs_ok)
6643 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6644 if (status == nfs_ok)
6645 status = nfs4_check_fh(current_fh, &stp->st_stid);
6646 if (status != nfs_ok)
6647 mutex_unlock(&stp->st_mutex);
6652 * Checks for sequence id mutating operations.
6655 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6656 stateid_t *stateid, char typemask,
6657 struct nfs4_ol_stateid **stpp,
6658 struct nfsd_net *nn)
6661 struct nfs4_stid *s;
6662 struct nfs4_ol_stateid *stp = NULL;
6664 trace_nfsd_preprocess(seqid, stateid);
6667 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6670 stp = openlockstateid(s);
6671 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6673 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6677 nfs4_put_stid(&stp->st_stid);
6681 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6682 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6685 struct nfs4_openowner *oo;
6686 struct nfs4_ol_stateid *stp;
6688 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6689 NFS4_OPEN_STID, &stp, nn);
6692 oo = openowner(stp->st_stateowner);
6693 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6694 mutex_unlock(&stp->st_mutex);
6695 nfs4_put_stid(&stp->st_stid);
6696 return nfserr_bad_stateid;
6703 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6704 union nfsd4_op_u *u)
6706 struct nfsd4_open_confirm *oc = &u->open_confirm;
6708 struct nfs4_openowner *oo;
6709 struct nfs4_ol_stateid *stp;
6710 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6712 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6713 cstate->current_fh.fh_dentry);
6715 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6719 status = nfs4_preprocess_seqid_op(cstate,
6720 oc->oc_seqid, &oc->oc_req_stateid,
6721 NFS4_OPEN_STID, &stp, nn);
6724 oo = openowner(stp->st_stateowner);
6725 status = nfserr_bad_stateid;
6726 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6727 mutex_unlock(&stp->st_mutex);
6730 oo->oo_flags |= NFS4_OO_CONFIRMED;
6731 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6732 mutex_unlock(&stp->st_mutex);
6733 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6734 nfsd4_client_record_create(oo->oo_owner.so_client);
6737 nfs4_put_stid(&stp->st_stid);
6739 nfsd4_bump_seqid(cstate, status);
6743 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6745 if (!test_access(access, stp))
6747 nfs4_file_put_access(stp->st_stid.sc_file, access);
6748 clear_access(access, stp);
6751 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6753 switch (to_access) {
6754 case NFS4_SHARE_ACCESS_READ:
6755 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6756 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6758 case NFS4_SHARE_ACCESS_WRITE:
6759 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6760 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6762 case NFS4_SHARE_ACCESS_BOTH:
6770 nfsd4_open_downgrade(struct svc_rqst *rqstp,
6771 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
6773 struct nfsd4_open_downgrade *od = &u->open_downgrade;
6775 struct nfs4_ol_stateid *stp;
6776 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6778 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6779 cstate->current_fh.fh_dentry);
6781 /* We don't yet support WANT bits: */
6782 if (od->od_deleg_want)
6783 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6786 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
6787 &od->od_stateid, &stp, nn);
6790 status = nfserr_inval;
6791 if (!test_access(od->od_share_access, stp)) {
6792 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6793 stp->st_access_bmap, od->od_share_access);
6796 if (!test_deny(od->od_share_deny, stp)) {
6797 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6798 stp->st_deny_bmap, od->od_share_deny);
6801 nfs4_stateid_downgrade(stp, od->od_share_access);
6802 reset_union_bmap_deny(od->od_share_deny, stp);
6803 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
6806 mutex_unlock(&stp->st_mutex);
6807 nfs4_put_stid(&stp->st_stid);
6809 nfsd4_bump_seqid(cstate, status);
6813 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6815 struct nfs4_client *clp = s->st_stid.sc_client;
6817 LIST_HEAD(reaplist);
6818 struct nfs4_ol_stateid *stp;
6820 spin_lock(&clp->cl_lock);
6821 unhashed = unhash_open_stateid(s, &reaplist);
6823 if (clp->cl_minorversion) {
6825 put_ol_stateid_locked(s, &reaplist);
6826 spin_unlock(&clp->cl_lock);
6827 list_for_each_entry(stp, &reaplist, st_locks)
6828 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
6829 free_ol_stateid_reaplist(&reaplist);
6831 spin_unlock(&clp->cl_lock);
6832 free_ol_stateid_reaplist(&reaplist);
6834 move_to_close_lru(s, clp->net);
6839 * nfs4_unlock_state() called after encode
6842 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6843 union nfsd4_op_u *u)
6845 struct nfsd4_close *close = &u->close;
6847 struct nfs4_ol_stateid *stp;
6848 struct net *net = SVC_NET(rqstp);
6849 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6851 dprintk("NFSD: nfsd4_close on file %pd\n",
6852 cstate->current_fh.fh_dentry);
6854 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6856 NFS4_OPEN_STID|NFS4_CLOSED_STID,
6858 nfsd4_bump_seqid(cstate, status);
6862 stp->st_stid.sc_type = NFS4_CLOSED_STID;
6865 * Technically we don't _really_ have to increment or copy it, since
6866 * it should just be gone after this operation and we clobber the
6867 * copied value below, but we continue to do so here just to ensure
6868 * that racing ops see that there was a state change.
6870 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
6872 nfsd4_close_open_stateid(stp);
6873 mutex_unlock(&stp->st_mutex);
6875 /* v4.1+ suggests that we send a special stateid in here, since the
6876 * clients should just ignore this anyway. Since this is not useful
6877 * for v4.0 clients either, we set it to the special close_stateid
6880 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6882 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6884 /* put reference from nfs4_preprocess_seqid_op */
6885 nfs4_put_stid(&stp->st_stid);
6891 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6892 union nfsd4_op_u *u)
6894 struct nfsd4_delegreturn *dr = &u->delegreturn;
6895 struct nfs4_delegation *dp;
6896 stateid_t *stateid = &dr->dr_stateid;
6897 struct nfs4_stid *s;
6899 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6901 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6904 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6907 dp = delegstateid(s);
6908 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6912 wake_up_var(d_inode(cstate->current_fh.fh_dentry));
6913 destroy_delegation(dp);
6915 nfs4_put_stid(&dp->dl_stid);
6920 /* last octet in a range */
6922 last_byte_offset(u64 start, u64 len)
6928 return end > start ? end - 1: NFS4_MAX_UINT64;
6932 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6933 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6934 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6935 * locking, this prevents us from being completely protocol-compliant. The
6936 * real solution to this problem is to start using unsigned file offsets in
6937 * the VFS, but this is a very deep change!
6940 nfs4_transform_lock_offset(struct file_lock *lock)
6942 if (lock->fl_start < 0)
6943 lock->fl_start = OFFSET_MAX;
6944 if (lock->fl_end < 0)
6945 lock->fl_end = OFFSET_MAX;
6949 nfsd4_lm_get_owner(fl_owner_t owner)
6951 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6953 nfs4_get_stateowner(&lo->lo_owner);
6958 nfsd4_lm_put_owner(fl_owner_t owner)
6960 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6963 nfs4_put_stateowner(&lo->lo_owner);
6966 /* return pointer to struct nfs4_client if client is expirable */
6968 nfsd4_lm_lock_expirable(struct file_lock *cfl)
6970 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
6971 struct nfs4_client *clp = lo->lo_owner.so_client;
6972 struct nfsd_net *nn;
6974 if (try_to_expire_client(clp)) {
6975 nn = net_generic(clp->net, nfsd_net_id);
6976 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
6982 /* schedule laundromat to run immediately and wait for it to complete */
6984 nfsd4_lm_expire_lock(void)
6986 flush_workqueue(laundry_wq);
6990 nfsd4_lm_notify(struct file_lock *fl)
6992 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6993 struct net *net = lo->lo_owner.so_client->net;
6994 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6995 struct nfsd4_blocked_lock *nbl = container_of(fl,
6996 struct nfsd4_blocked_lock, nbl_lock);
6999 /* An empty list means that something else is going to be using it */
7000 spin_lock(&nn->blocked_locks_lock);
7001 if (!list_empty(&nbl->nbl_list)) {
7002 list_del_init(&nbl->nbl_list);
7003 list_del_init(&nbl->nbl_lru);
7006 spin_unlock(&nn->blocked_locks_lock);
7009 trace_nfsd_cb_notify_lock(lo, nbl);
7010 nfsd4_run_cb(&nbl->nbl_cb);
7014 static const struct lock_manager_operations nfsd_posix_mng_ops = {
7015 .lm_mod_owner = THIS_MODULE,
7016 .lm_notify = nfsd4_lm_notify,
7017 .lm_get_owner = nfsd4_lm_get_owner,
7018 .lm_put_owner = nfsd4_lm_put_owner,
7019 .lm_lock_expirable = nfsd4_lm_lock_expirable,
7020 .lm_expire_lock = nfsd4_lm_expire_lock,
7024 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
7026 struct nfs4_lockowner *lo;
7028 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
7029 lo = (struct nfs4_lockowner *) fl->fl_owner;
7030 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
7032 if (!deny->ld_owner.data)
7033 /* We just don't care that much */
7035 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
7038 deny->ld_owner.len = 0;
7039 deny->ld_owner.data = NULL;
7040 deny->ld_clientid.cl_boot = 0;
7041 deny->ld_clientid.cl_id = 0;
7043 deny->ld_start = fl->fl_start;
7044 deny->ld_length = NFS4_MAX_UINT64;
7045 if (fl->fl_end != NFS4_MAX_UINT64)
7046 deny->ld_length = fl->fl_end - fl->fl_start + 1;
7047 deny->ld_type = NFS4_READ_LT;
7048 if (fl->fl_type != F_RDLCK)
7049 deny->ld_type = NFS4_WRITE_LT;
7052 static struct nfs4_lockowner *
7053 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
7055 unsigned int strhashval = ownerstr_hashval(owner);
7056 struct nfs4_stateowner *so;
7058 lockdep_assert_held(&clp->cl_lock);
7060 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7062 if (so->so_is_open_owner)
7064 if (same_owner_str(so, owner))
7065 return lockowner(nfs4_get_stateowner(so));
7070 static struct nfs4_lockowner *
7071 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
7073 struct nfs4_lockowner *lo;
7075 spin_lock(&clp->cl_lock);
7076 lo = find_lockowner_str_locked(clp, owner);
7077 spin_unlock(&clp->cl_lock);
7081 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
7083 unhash_lockowner_locked(lockowner(sop));
7086 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
7088 struct nfs4_lockowner *lo = lockowner(sop);
7090 kmem_cache_free(lockowner_slab, lo);
7093 static const struct nfs4_stateowner_operations lockowner_ops = {
7094 .so_unhash = nfs4_unhash_lockowner,
7095 .so_free = nfs4_free_lockowner,
7099 * Alloc a lock owner structure.
7100 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
7103 * strhashval = ownerstr_hashval
7105 static struct nfs4_lockowner *
7106 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
7107 struct nfs4_ol_stateid *open_stp,
7108 struct nfsd4_lock *lock)
7110 struct nfs4_lockowner *lo, *ret;
7112 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
7115 INIT_LIST_HEAD(&lo->lo_blocked);
7116 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
7117 lo->lo_owner.so_is_open_owner = 0;
7118 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
7119 lo->lo_owner.so_ops = &lockowner_ops;
7120 spin_lock(&clp->cl_lock);
7121 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
7123 list_add(&lo->lo_owner.so_strhash,
7124 &clp->cl_ownerstr_hashtbl[strhashval]);
7127 nfs4_free_stateowner(&lo->lo_owner);
7129 spin_unlock(&clp->cl_lock);
7133 static struct nfs4_ol_stateid *
7134 find_lock_stateid(const struct nfs4_lockowner *lo,
7135 const struct nfs4_ol_stateid *ost)
7137 struct nfs4_ol_stateid *lst;
7139 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
7141 /* If ost is not hashed, ost->st_locks will not be valid */
7142 if (!nfs4_ol_stateid_unhashed(ost))
7143 list_for_each_entry(lst, &ost->st_locks, st_locks) {
7144 if (lst->st_stateowner == &lo->lo_owner) {
7145 refcount_inc(&lst->st_stid.sc_count);
7152 static struct nfs4_ol_stateid *
7153 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7154 struct nfs4_file *fp, struct inode *inode,
7155 struct nfs4_ol_stateid *open_stp)
7157 struct nfs4_client *clp = lo->lo_owner.so_client;
7158 struct nfs4_ol_stateid *retstp;
7160 mutex_init(&stp->st_mutex);
7161 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
7163 spin_lock(&clp->cl_lock);
7164 if (nfs4_ol_stateid_unhashed(open_stp))
7166 retstp = find_lock_stateid(lo, open_stp);
7169 refcount_inc(&stp->st_stid.sc_count);
7170 stp->st_stid.sc_type = NFS4_LOCK_STID;
7171 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
7173 stp->st_stid.sc_file = fp;
7174 stp->st_access_bmap = 0;
7175 stp->st_deny_bmap = open_stp->st_deny_bmap;
7176 stp->st_openstp = open_stp;
7177 spin_lock(&fp->fi_lock);
7178 list_add(&stp->st_locks, &open_stp->st_locks);
7179 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
7180 list_add(&stp->st_perfile, &fp->fi_stateids);
7181 spin_unlock(&fp->fi_lock);
7182 spin_unlock(&clp->cl_lock);
7185 spin_unlock(&clp->cl_lock);
7186 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
7187 nfs4_put_stid(&retstp->st_stid);
7190 /* To keep mutex tracking happy */
7191 mutex_unlock(&stp->st_mutex);
7194 spin_unlock(&clp->cl_lock);
7195 mutex_unlock(&stp->st_mutex);
7199 static struct nfs4_ol_stateid *
7200 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7201 struct inode *inode, struct nfs4_ol_stateid *ost,
7204 struct nfs4_stid *ns = NULL;
7205 struct nfs4_ol_stateid *lst;
7206 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7207 struct nfs4_client *clp = oo->oo_owner.so_client;
7210 spin_lock(&clp->cl_lock);
7211 lst = find_lock_stateid(lo, ost);
7212 spin_unlock(&clp->cl_lock);
7214 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7216 nfs4_put_stid(&lst->st_stid);
7218 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7222 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7223 if (lst == openlockstateid(ns))
7232 check_lock_length(u64 offset, u64 length)
7234 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7235 (length > ~offset)));
7238 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
7240 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
7242 lockdep_assert_held(&fp->fi_lock);
7244 if (test_access(access, lock_stp))
7246 __nfs4_file_get_access(fp, access);
7247 set_access(access, lock_stp);
7251 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7252 struct nfs4_ol_stateid *ost,
7253 struct nfsd4_lock *lock,
7254 struct nfs4_ol_stateid **plst, bool *new)
7257 struct nfs4_file *fi = ost->st_stid.sc_file;
7258 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7259 struct nfs4_client *cl = oo->oo_owner.so_client;
7260 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
7261 struct nfs4_lockowner *lo;
7262 struct nfs4_ol_stateid *lst;
7263 unsigned int strhashval;
7265 lo = find_lockowner_str(cl, &lock->lk_new_owner);
7267 strhashval = ownerstr_hashval(&lock->lk_new_owner);
7268 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7270 return nfserr_jukebox;
7272 /* with an existing lockowner, seqids must be the same */
7273 status = nfserr_bad_seqid;
7274 if (!cstate->minorversion &&
7275 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
7279 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7281 status = nfserr_jukebox;
7288 nfs4_put_stateowner(&lo->lo_owner);
7296 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7297 union nfsd4_op_u *u)
7299 struct nfsd4_lock *lock = &u->lock;
7300 struct nfs4_openowner *open_sop = NULL;
7301 struct nfs4_lockowner *lock_sop = NULL;
7302 struct nfs4_ol_stateid *lock_stp = NULL;
7303 struct nfs4_ol_stateid *open_stp = NULL;
7304 struct nfs4_file *fp;
7305 struct nfsd_file *nf = NULL;
7306 struct nfsd4_blocked_lock *nbl = NULL;
7307 struct file_lock *file_lock = NULL;
7308 struct file_lock *conflock = NULL;
7313 unsigned char fl_type;
7314 unsigned int fl_flags = FL_POSIX;
7315 struct net *net = SVC_NET(rqstp);
7316 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7318 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7319 (long long) lock->lk_offset,
7320 (long long) lock->lk_length);
7322 if (check_lock_length(lock->lk_offset, lock->lk_length))
7323 return nfserr_inval;
7325 if ((status = fh_verify(rqstp, &cstate->current_fh,
7326 S_IFREG, NFSD_MAY_LOCK))) {
7327 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7331 if (lock->lk_is_new) {
7332 if (nfsd4_has_session(cstate))
7333 /* See rfc 5661 18.10.3: given clientid is ignored: */
7334 memcpy(&lock->lk_new_clientid,
7335 &cstate->clp->cl_clientid,
7336 sizeof(clientid_t));
7338 /* validate and update open stateid and open seqid */
7339 status = nfs4_preprocess_confirmed_seqid_op(cstate,
7340 lock->lk_new_open_seqid,
7341 &lock->lk_new_open_stateid,
7345 mutex_unlock(&open_stp->st_mutex);
7346 open_sop = openowner(open_stp->st_stateowner);
7347 status = nfserr_bad_stateid;
7348 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
7349 &lock->lk_new_clientid))
7351 status = lookup_or_create_lock_state(cstate, open_stp, lock,
7354 status = nfs4_preprocess_seqid_op(cstate,
7355 lock->lk_old_lock_seqid,
7356 &lock->lk_old_lock_stateid,
7357 NFS4_LOCK_STID, &lock_stp, nn);
7361 lock_sop = lockowner(lock_stp->st_stateowner);
7363 lkflg = setlkflg(lock->lk_type);
7364 status = nfs4_check_openmode(lock_stp, lkflg);
7368 status = nfserr_grace;
7369 if (locks_in_grace(net) && !lock->lk_reclaim)
7371 status = nfserr_no_grace;
7372 if (!locks_in_grace(net) && lock->lk_reclaim)
7375 if (lock->lk_reclaim)
7376 fl_flags |= FL_RECLAIM;
7378 fp = lock_stp->st_stid.sc_file;
7379 switch (lock->lk_type) {
7381 if (nfsd4_has_session(cstate))
7382 fl_flags |= FL_SLEEP;
7385 spin_lock(&fp->fi_lock);
7386 nf = find_readable_file_locked(fp);
7388 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7389 spin_unlock(&fp->fi_lock);
7392 case NFS4_WRITEW_LT:
7393 if (nfsd4_has_session(cstate))
7394 fl_flags |= FL_SLEEP;
7397 spin_lock(&fp->fi_lock);
7398 nf = find_writeable_file_locked(fp);
7400 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7401 spin_unlock(&fp->fi_lock);
7405 status = nfserr_inval;
7410 status = nfserr_openmode;
7415 * Most filesystems with their own ->lock operations will block
7416 * the nfsd thread waiting to acquire the lock. That leads to
7417 * deadlocks (we don't want every nfsd thread tied up waiting
7418 * for file locks), so don't attempt blocking lock notifications
7419 * on those filesystems:
7421 if (nf->nf_file->f_op->lock)
7422 fl_flags &= ~FL_SLEEP;
7424 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
7426 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
7427 status = nfserr_jukebox;
7431 file_lock = &nbl->nbl_lock;
7432 file_lock->fl_type = fl_type;
7433 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
7434 file_lock->fl_pid = current->tgid;
7435 file_lock->fl_file = nf->nf_file;
7436 file_lock->fl_flags = fl_flags;
7437 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7438 file_lock->fl_start = lock->lk_offset;
7439 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
7440 nfs4_transform_lock_offset(file_lock);
7442 conflock = locks_alloc_lock();
7444 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7445 status = nfserr_jukebox;
7449 if (fl_flags & FL_SLEEP) {
7450 nbl->nbl_time = ktime_get_boottime_seconds();
7451 spin_lock(&nn->blocked_locks_lock);
7452 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
7453 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
7454 kref_get(&nbl->nbl_kref);
7455 spin_unlock(&nn->blocked_locks_lock);
7458 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
7460 case 0: /* success! */
7461 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
7463 if (lock->lk_reclaim)
7464 nn->somebody_reclaimed = true;
7466 case FILE_LOCK_DEFERRED:
7467 kref_put(&nbl->nbl_kref, free_nbl);
7470 case -EAGAIN: /* conflock holds conflicting lock */
7471 status = nfserr_denied;
7472 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
7473 nfs4_set_lock_denied(conflock, &lock->lk_denied);
7476 status = nfserr_deadlock;
7479 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
7480 status = nfserrno(err);
7485 /* dequeue it if we queued it before */
7486 if (fl_flags & FL_SLEEP) {
7487 spin_lock(&nn->blocked_locks_lock);
7488 if (!list_empty(&nbl->nbl_list) &&
7489 !list_empty(&nbl->nbl_lru)) {
7490 list_del_init(&nbl->nbl_list);
7491 list_del_init(&nbl->nbl_lru);
7492 kref_put(&nbl->nbl_kref, free_nbl);
7494 /* nbl can use one of lists to be linked to reaplist */
7495 spin_unlock(&nn->blocked_locks_lock);
7497 free_blocked_lock(nbl);
7502 /* Bump seqid manually if the 4.0 replay owner is openowner */
7503 if (cstate->replay_owner &&
7504 cstate->replay_owner != &lock_sop->lo_owner &&
7505 seqid_mutating_err(ntohl(status)))
7506 lock_sop->lo_owner.so_seqid++;
7509 * If this is a new, never-before-used stateid, and we are
7510 * returning an error, then just go ahead and release it.
7513 release_lock_stateid(lock_stp);
7515 mutex_unlock(&lock_stp->st_mutex);
7517 nfs4_put_stid(&lock_stp->st_stid);
7520 nfs4_put_stid(&open_stp->st_stid);
7521 nfsd4_bump_seqid(cstate, status);
7523 locks_free_lock(conflock);
7528 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7529 * so we do a temporary open here just to get an open file to pass to
7532 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
7534 struct nfsd_file *nf;
7535 struct inode *inode;
7538 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7541 inode = fhp->fh_dentry->d_inode;
7542 inode_lock(inode); /* to block new leases till after test_lock: */
7543 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
7546 lock->fl_file = nf->nf_file;
7547 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
7548 lock->fl_file = NULL;
7550 inode_unlock(inode);
7559 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7560 union nfsd4_op_u *u)
7562 struct nfsd4_lockt *lockt = &u->lockt;
7563 struct file_lock *file_lock = NULL;
7564 struct nfs4_lockowner *lo = NULL;
7566 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7568 if (locks_in_grace(SVC_NET(rqstp)))
7569 return nfserr_grace;
7571 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7572 return nfserr_inval;
7574 if (!nfsd4_has_session(cstate)) {
7575 status = set_client(&lockt->lt_clientid, cstate, nn);
7580 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7583 file_lock = locks_alloc_lock();
7585 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7586 status = nfserr_jukebox;
7590 switch (lockt->lt_type) {
7593 file_lock->fl_type = F_RDLCK;
7596 case NFS4_WRITEW_LT:
7597 file_lock->fl_type = F_WRLCK;
7600 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
7601 status = nfserr_inval;
7605 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
7607 file_lock->fl_owner = (fl_owner_t)lo;
7608 file_lock->fl_pid = current->tgid;
7609 file_lock->fl_flags = FL_POSIX;
7611 file_lock->fl_start = lockt->lt_offset;
7612 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
7614 nfs4_transform_lock_offset(file_lock);
7616 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
7620 if (file_lock->fl_type != F_UNLCK) {
7621 status = nfserr_denied;
7622 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
7626 nfs4_put_stateowner(&lo->lo_owner);
7628 locks_free_lock(file_lock);
7633 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7634 union nfsd4_op_u *u)
7636 struct nfsd4_locku *locku = &u->locku;
7637 struct nfs4_ol_stateid *stp;
7638 struct nfsd_file *nf = NULL;
7639 struct file_lock *file_lock = NULL;
7642 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7644 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7645 (long long) locku->lu_offset,
7646 (long long) locku->lu_length);
7648 if (check_lock_length(locku->lu_offset, locku->lu_length))
7649 return nfserr_inval;
7651 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
7652 &locku->lu_stateid, NFS4_LOCK_STID,
7656 nf = find_any_file(stp->st_stid.sc_file);
7658 status = nfserr_lock_range;
7661 file_lock = locks_alloc_lock();
7663 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7664 status = nfserr_jukebox;
7668 file_lock->fl_type = F_UNLCK;
7669 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7670 file_lock->fl_pid = current->tgid;
7671 file_lock->fl_file = nf->nf_file;
7672 file_lock->fl_flags = FL_POSIX;
7673 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7674 file_lock->fl_start = locku->lu_offset;
7676 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7678 nfs4_transform_lock_offset(file_lock);
7680 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7682 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7685 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7689 mutex_unlock(&stp->st_mutex);
7690 nfs4_put_stid(&stp->st_stid);
7692 nfsd4_bump_seqid(cstate, status);
7694 locks_free_lock(file_lock);
7698 status = nfserrno(err);
7704 * true: locks held by lockowner
7705 * false: no locks held by lockowner
7708 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7710 struct file_lock *fl;
7712 struct nfsd_file *nf = find_any_file(fp);
7713 struct inode *inode;
7714 struct file_lock_context *flctx;
7717 /* Any valid lock stateid should have some sort of access */
7722 inode = locks_inode(nf->nf_file);
7723 flctx = inode->i_flctx;
7725 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7726 spin_lock(&flctx->flc_lock);
7727 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7728 if (fl->fl_owner == (fl_owner_t)lowner) {
7733 spin_unlock(&flctx->flc_lock);
7740 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
7741 * @rqstp: RPC transaction
7742 * @cstate: NFSv4 COMPOUND state
7743 * @u: RELEASE_LOCKOWNER arguments
7745 * The lockowner's so_count is bumped when a lock record is added
7746 * or when copying a conflicting lock. The latter case is brief,
7747 * but can lead to fleeting false positives when looking for
7751 * %nfs_ok: lockowner released or not found
7752 * %nfserr_locks_held: lockowner still in use
7753 * %nfserr_stale_clientid: clientid no longer active
7754 * %nfserr_expired: clientid not recognized
7757 nfsd4_release_lockowner(struct svc_rqst *rqstp,
7758 struct nfsd4_compound_state *cstate,
7759 union nfsd4_op_u *u)
7761 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
7762 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7763 clientid_t *clid = &rlockowner->rl_clientid;
7764 struct nfs4_ol_stateid *stp;
7765 struct nfs4_lockowner *lo;
7766 struct nfs4_client *clp;
7767 LIST_HEAD(reaplist);
7770 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7771 clid->cl_boot, clid->cl_id);
7773 status = set_client(clid, cstate, nn);
7778 spin_lock(&clp->cl_lock);
7779 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
7781 spin_unlock(&clp->cl_lock);
7784 if (atomic_read(&lo->lo_owner.so_count) != 2) {
7785 spin_unlock(&clp->cl_lock);
7786 nfs4_put_stateowner(&lo->lo_owner);
7787 return nfserr_locks_held;
7789 unhash_lockowner_locked(lo);
7790 while (!list_empty(&lo->lo_owner.so_stateids)) {
7791 stp = list_first_entry(&lo->lo_owner.so_stateids,
7792 struct nfs4_ol_stateid,
7794 WARN_ON(!unhash_lock_stateid(stp));
7795 put_ol_stateid_locked(stp, &reaplist);
7797 spin_unlock(&clp->cl_lock);
7799 free_ol_stateid_reaplist(&reaplist);
7800 remove_blocked_locks(lo);
7801 nfs4_put_stateowner(&lo->lo_owner);
7805 static inline struct nfs4_client_reclaim *
7808 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
7812 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7814 struct nfs4_client_reclaim *crp;
7816 crp = nfsd4_find_reclaim_client(name, nn);
7817 return (crp && crp->cr_clp);
7821 * failure => all reset bets are off, nfserr_no_grace...
7823 * The caller is responsible for freeing name.data if NULL is returned (it
7824 * will be freed in nfs4_remove_reclaim_record in the normal case).
7826 struct nfs4_client_reclaim *
7827 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7828 struct nfsd_net *nn)
7830 unsigned int strhashval;
7831 struct nfs4_client_reclaim *crp;
7833 crp = alloc_reclaim();
7835 strhashval = clientstr_hashval(name);
7836 INIT_LIST_HEAD(&crp->cr_strhash);
7837 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
7838 crp->cr_name.data = name.data;
7839 crp->cr_name.len = name.len;
7840 crp->cr_princhash.data = princhash.data;
7841 crp->cr_princhash.len = princhash.len;
7843 nn->reclaim_str_hashtbl_size++;
7849 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
7851 list_del(&crp->cr_strhash);
7852 kfree(crp->cr_name.data);
7853 kfree(crp->cr_princhash.data);
7855 nn->reclaim_str_hashtbl_size--;
7859 nfs4_release_reclaim(struct nfsd_net *nn)
7861 struct nfs4_client_reclaim *crp = NULL;
7864 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7865 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7866 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
7867 struct nfs4_client_reclaim, cr_strhash);
7868 nfs4_remove_reclaim_record(crp, nn);
7871 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
7875 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
7876 struct nfs4_client_reclaim *
7877 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
7879 unsigned int strhashval;
7880 struct nfs4_client_reclaim *crp = NULL;
7882 strhashval = clientstr_hashval(name);
7883 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
7884 if (compare_blob(&crp->cr_name, &name) == 0) {
7892 nfs4_check_open_reclaim(struct nfs4_client *clp)
7894 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
7895 return nfserr_no_grace;
7897 if (nfsd4_client_record_check(clp))
7898 return nfserr_reclaim_bad;
7904 * Since the lifetime of a delegation isn't limited to that of an open, a
7905 * client may quite reasonably hang on to a delegation as long as it has
7906 * the inode cached. This becomes an obvious problem the first time a
7907 * client's inode cache approaches the size of the server's total memory.
7909 * For now we avoid this problem by imposing a hard limit on the number
7910 * of delegations, which varies according to the server's memory size.
7913 set_max_delegations(void)
7916 * Allow at most 4 delegations per megabyte of RAM. Quick
7917 * estimates suggest that in the worst case (where every delegation
7918 * is for a different inode), a delegation could take about 1.5K,
7919 * giving a worst case usage of about 6% of memory.
7921 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7924 static int nfs4_state_create_net(struct net *net)
7926 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7929 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7930 sizeof(struct list_head),
7932 if (!nn->conf_id_hashtbl)
7934 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7935 sizeof(struct list_head),
7937 if (!nn->unconf_id_hashtbl)
7939 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7940 sizeof(struct list_head),
7942 if (!nn->sessionid_hashtbl)
7945 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7946 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7947 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7949 for (i = 0; i < SESSION_HASH_SIZE; i++)
7950 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7951 nn->conf_name_tree = RB_ROOT;
7952 nn->unconf_name_tree = RB_ROOT;
7953 nn->boot_time = ktime_get_real_seconds();
7954 nn->grace_ended = false;
7955 nn->nfsd4_manager.block_opens = true;
7956 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7957 INIT_LIST_HEAD(&nn->client_lru);
7958 INIT_LIST_HEAD(&nn->close_lru);
7959 INIT_LIST_HEAD(&nn->del_recall_lru);
7960 spin_lock_init(&nn->client_lock);
7961 spin_lock_init(&nn->s2s_cp_lock);
7962 idr_init(&nn->s2s_cp_stateids);
7964 spin_lock_init(&nn->blocked_locks_lock);
7965 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7967 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7968 INIT_DELAYED_WORK(&nn->nfsd_shrinker_work, courtesy_client_reaper);
7974 kfree(nn->unconf_id_hashtbl);
7976 kfree(nn->conf_id_hashtbl);
7982 nfs4_state_destroy_net(struct net *net)
7985 struct nfs4_client *clp = NULL;
7986 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7988 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7989 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7990 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7991 destroy_client(clp);
7995 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7997 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7998 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7999 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8000 destroy_client(clp);
8004 kfree(nn->sessionid_hashtbl);
8005 kfree(nn->unconf_id_hashtbl);
8006 kfree(nn->conf_id_hashtbl);
8011 nfs4_state_start_net(struct net *net)
8013 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8016 ret = nfs4_state_create_net(net);
8019 locks_start_grace(net, &nn->nfsd4_manager);
8020 nfsd4_client_tracking_init(net);
8021 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
8023 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
8024 nn->nfsd4_grace, net->ns.inum);
8025 trace_nfsd_grace_start(nn);
8026 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
8030 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
8032 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
8033 nfsd4_end_grace(nn);
8037 /* initialization to perform when the nfsd service is started: */
8040 nfs4_state_start(void)
8044 ret = nfsd4_create_callback_queue();
8048 set_max_delegations();
8053 nfs4_state_shutdown_net(struct net *net)
8055 struct nfs4_delegation *dp = NULL;
8056 struct list_head *pos, *next, reaplist;
8057 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8059 cancel_delayed_work_sync(&nn->laundromat_work);
8060 locks_end_grace(&nn->nfsd4_manager);
8062 INIT_LIST_HEAD(&reaplist);
8063 spin_lock(&state_lock);
8064 list_for_each_safe(pos, next, &nn->del_recall_lru) {
8065 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8066 WARN_ON(!unhash_delegation_locked(dp));
8067 list_add(&dp->dl_recall_lru, &reaplist);
8069 spin_unlock(&state_lock);
8070 list_for_each_safe(pos, next, &reaplist) {
8071 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8072 list_del_init(&dp->dl_recall_lru);
8073 destroy_unhashed_deleg(dp);
8076 nfsd4_client_tracking_exit(net);
8077 nfs4_state_destroy_net(net);
8078 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
8079 nfsd4_ssc_shutdown_umount(nn);
8084 nfs4_state_shutdown(void)
8086 nfsd4_destroy_callback_queue();
8090 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8092 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
8093 CURRENT_STATEID(stateid))
8094 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8098 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8100 if (cstate->minorversion) {
8101 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
8102 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8107 clear_current_stateid(struct nfsd4_compound_state *cstate)
8109 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8113 * functions to set current state id
8116 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
8117 union nfsd4_op_u *u)
8119 put_stateid(cstate, &u->open_downgrade.od_stateid);
8123 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
8124 union nfsd4_op_u *u)
8126 put_stateid(cstate, &u->open.op_stateid);
8130 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
8131 union nfsd4_op_u *u)
8133 put_stateid(cstate, &u->close.cl_stateid);
8137 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
8138 union nfsd4_op_u *u)
8140 put_stateid(cstate, &u->lock.lk_resp_stateid);
8144 * functions to consume current state id
8148 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
8149 union nfsd4_op_u *u)
8151 get_stateid(cstate, &u->open_downgrade.od_stateid);
8155 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8156 union nfsd4_op_u *u)
8158 get_stateid(cstate, &u->delegreturn.dr_stateid);
8162 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8163 union nfsd4_op_u *u)
8165 get_stateid(cstate, &u->free_stateid.fr_stateid);
8169 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8170 union nfsd4_op_u *u)
8172 get_stateid(cstate, &u->setattr.sa_stateid);
8176 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8177 union nfsd4_op_u *u)
8179 get_stateid(cstate, &u->close.cl_stateid);
8183 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8184 union nfsd4_op_u *u)
8186 get_stateid(cstate, &u->locku.lu_stateid);
8190 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8191 union nfsd4_op_u *u)
8193 get_stateid(cstate, &u->read.rd_stateid);
8197 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8198 union nfsd4_op_u *u)
8200 get_stateid(cstate, &u->write.wr_stateid);