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 __be32 get_client_locked(struct nfs4_client *clp)
165 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
167 lockdep_assert_held(&nn->client_lock);
169 if (is_client_expired(clp))
170 return nfserr_expired;
171 atomic_inc(&clp->cl_rpc_users);
172 clp->cl_state = NFSD4_ACTIVE;
176 /* must be called under the client_lock */
178 renew_client_locked(struct nfs4_client *clp)
180 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
182 if (is_client_expired(clp)) {
184 printk("%s: client (clientid %08x/%08x) already expired\n",
186 clp->cl_clientid.cl_boot,
187 clp->cl_clientid.cl_id);
191 list_move_tail(&clp->cl_lru, &nn->client_lru);
192 clp->cl_time = ktime_get_boottime_seconds();
193 clp->cl_state = NFSD4_ACTIVE;
196 static void put_client_renew_locked(struct nfs4_client *clp)
198 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
200 lockdep_assert_held(&nn->client_lock);
202 if (!atomic_dec_and_test(&clp->cl_rpc_users))
204 if (!is_client_expired(clp))
205 renew_client_locked(clp);
207 wake_up_all(&expiry_wq);
210 static void put_client_renew(struct nfs4_client *clp)
212 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
214 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
216 if (!is_client_expired(clp))
217 renew_client_locked(clp);
219 wake_up_all(&expiry_wq);
220 spin_unlock(&nn->client_lock);
223 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
227 if (is_session_dead(ses))
228 return nfserr_badsession;
229 status = get_client_locked(ses->se_client);
232 atomic_inc(&ses->se_ref);
236 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
238 struct nfs4_client *clp = ses->se_client;
239 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
241 lockdep_assert_held(&nn->client_lock);
243 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
245 put_client_renew_locked(clp);
248 static void nfsd4_put_session(struct nfsd4_session *ses)
250 struct nfs4_client *clp = ses->se_client;
251 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
253 spin_lock(&nn->client_lock);
254 nfsd4_put_session_locked(ses);
255 spin_unlock(&nn->client_lock);
258 static struct nfsd4_blocked_lock *
259 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
262 struct nfsd4_blocked_lock *cur, *found = NULL;
264 spin_lock(&nn->blocked_locks_lock);
265 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
266 if (fh_match(fh, &cur->nbl_fh)) {
267 list_del_init(&cur->nbl_list);
268 WARN_ON(list_empty(&cur->nbl_lru));
269 list_del_init(&cur->nbl_lru);
274 spin_unlock(&nn->blocked_locks_lock);
276 locks_delete_block(&found->nbl_lock);
280 static struct nfsd4_blocked_lock *
281 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
284 struct nfsd4_blocked_lock *nbl;
286 nbl = find_blocked_lock(lo, fh, nn);
288 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
290 INIT_LIST_HEAD(&nbl->nbl_list);
291 INIT_LIST_HEAD(&nbl->nbl_lru);
292 fh_copy_shallow(&nbl->nbl_fh, fh);
293 locks_init_lock(&nbl->nbl_lock);
294 kref_init(&nbl->nbl_kref);
295 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
296 &nfsd4_cb_notify_lock_ops,
297 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
304 free_nbl(struct kref *kref)
306 struct nfsd4_blocked_lock *nbl;
308 nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
313 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
315 locks_delete_block(&nbl->nbl_lock);
316 locks_release_private(&nbl->nbl_lock);
317 kref_put(&nbl->nbl_kref, free_nbl);
321 remove_blocked_locks(struct nfs4_lockowner *lo)
323 struct nfs4_client *clp = lo->lo_owner.so_client;
324 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
325 struct nfsd4_blocked_lock *nbl;
328 /* Dequeue all blocked locks */
329 spin_lock(&nn->blocked_locks_lock);
330 while (!list_empty(&lo->lo_blocked)) {
331 nbl = list_first_entry(&lo->lo_blocked,
332 struct nfsd4_blocked_lock,
334 list_del_init(&nbl->nbl_list);
335 WARN_ON(list_empty(&nbl->nbl_lru));
336 list_move(&nbl->nbl_lru, &reaplist);
338 spin_unlock(&nn->blocked_locks_lock);
341 while (!list_empty(&reaplist)) {
342 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
344 list_del_init(&nbl->nbl_lru);
345 free_blocked_lock(nbl);
350 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
352 struct nfsd4_blocked_lock *nbl = container_of(cb,
353 struct nfsd4_blocked_lock, nbl_cb);
354 locks_delete_block(&nbl->nbl_lock);
358 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
361 * Since this is just an optimization, we don't try very hard if it
362 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
363 * just quit trying on anything else.
365 switch (task->tk_status) {
367 rpc_delay(task, 1 * HZ);
375 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
377 struct nfsd4_blocked_lock *nbl = container_of(cb,
378 struct nfsd4_blocked_lock, nbl_cb);
380 free_blocked_lock(nbl);
383 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
384 .prepare = nfsd4_cb_notify_lock_prepare,
385 .done = nfsd4_cb_notify_lock_done,
386 .release = nfsd4_cb_notify_lock_release,
390 * We store the NONE, READ, WRITE, and BOTH bits separately in the
391 * st_{access,deny}_bmap field of the stateid, in order to track not
392 * only what share bits are currently in force, but also what
393 * combinations of share bits previous opens have used. This allows us
394 * to enforce the recommendation in
395 * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
396 * the server return an error if the client attempt to downgrade to a
397 * combination of share bits not explicable by closing some of its
400 * This enforcement is arguably incomplete, since we don't keep
401 * track of access/deny bit combinations; so, e.g., we allow:
403 * OPEN allow read, deny write
404 * OPEN allow both, deny none
405 * DOWNGRADE allow read, deny none
407 * which we should reject.
409 * But you could also argue that our current code is already overkill,
410 * since it only exists to return NFS4ERR_INVAL on incorrect client
414 bmap_to_share_mode(unsigned long bmap)
417 unsigned int access = 0;
419 for (i = 1; i < 4; i++) {
420 if (test_bit(i, &bmap))
426 /* set share access for a given stateid */
428 set_access(u32 access, struct nfs4_ol_stateid *stp)
430 unsigned char mask = 1 << access;
432 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
433 stp->st_access_bmap |= mask;
436 /* clear share access for a given stateid */
438 clear_access(u32 access, struct nfs4_ol_stateid *stp)
440 unsigned char mask = 1 << access;
442 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
443 stp->st_access_bmap &= ~mask;
446 /* test whether a given stateid has access */
448 test_access(u32 access, struct nfs4_ol_stateid *stp)
450 unsigned char mask = 1 << access;
452 return (bool)(stp->st_access_bmap & mask);
455 /* set share deny for a given stateid */
457 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
459 unsigned char mask = 1 << deny;
461 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
462 stp->st_deny_bmap |= mask;
465 /* clear share deny for a given stateid */
467 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
469 unsigned char mask = 1 << deny;
471 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
472 stp->st_deny_bmap &= ~mask;
475 /* test whether a given stateid is denying specific access */
477 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
479 unsigned char mask = 1 << deny;
481 return (bool)(stp->st_deny_bmap & mask);
484 static int nfs4_access_to_omode(u32 access)
486 switch (access & NFS4_SHARE_ACCESS_BOTH) {
487 case NFS4_SHARE_ACCESS_READ:
489 case NFS4_SHARE_ACCESS_WRITE:
491 case NFS4_SHARE_ACCESS_BOTH:
499 access_permit_read(struct nfs4_ol_stateid *stp)
501 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
502 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
503 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
507 access_permit_write(struct nfs4_ol_stateid *stp)
509 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
510 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
513 static inline struct nfs4_stateowner *
514 nfs4_get_stateowner(struct nfs4_stateowner *sop)
516 atomic_inc(&sop->so_count);
521 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
523 return (sop->so_owner.len == owner->len) &&
524 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
527 static struct nfs4_openowner *
528 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
529 struct nfs4_client *clp)
531 struct nfs4_stateowner *so;
533 lockdep_assert_held(&clp->cl_lock);
535 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
537 if (!so->so_is_open_owner)
539 if (same_owner_str(so, &open->op_owner))
540 return openowner(nfs4_get_stateowner(so));
545 static struct nfs4_openowner *
546 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
547 struct nfs4_client *clp)
549 struct nfs4_openowner *oo;
551 spin_lock(&clp->cl_lock);
552 oo = find_openstateowner_str_locked(hashval, open, clp);
553 spin_unlock(&clp->cl_lock);
558 opaque_hashval(const void *ptr, int nbytes)
560 unsigned char *cptr = (unsigned char *) ptr;
570 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
572 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
574 kmem_cache_free(file_slab, fp);
578 put_nfs4_file(struct nfs4_file *fi)
580 might_lock(&state_lock);
582 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
583 hlist_del_rcu(&fi->fi_hash);
584 spin_unlock(&state_lock);
585 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
586 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
587 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
591 static struct nfsd_file *
592 __nfs4_get_fd(struct nfs4_file *f, int oflag)
594 if (f->fi_fds[oflag])
595 return nfsd_file_get(f->fi_fds[oflag]);
599 static struct nfsd_file *
600 find_writeable_file_locked(struct nfs4_file *f)
602 struct nfsd_file *ret;
604 lockdep_assert_held(&f->fi_lock);
606 ret = __nfs4_get_fd(f, O_WRONLY);
608 ret = __nfs4_get_fd(f, O_RDWR);
612 static struct nfsd_file *
613 find_writeable_file(struct nfs4_file *f)
615 struct nfsd_file *ret;
617 spin_lock(&f->fi_lock);
618 ret = find_writeable_file_locked(f);
619 spin_unlock(&f->fi_lock);
624 static struct nfsd_file *
625 find_readable_file_locked(struct nfs4_file *f)
627 struct nfsd_file *ret;
629 lockdep_assert_held(&f->fi_lock);
631 ret = __nfs4_get_fd(f, O_RDONLY);
633 ret = __nfs4_get_fd(f, O_RDWR);
637 static struct nfsd_file *
638 find_readable_file(struct nfs4_file *f)
640 struct nfsd_file *ret;
642 spin_lock(&f->fi_lock);
643 ret = find_readable_file_locked(f);
644 spin_unlock(&f->fi_lock);
650 find_any_file(struct nfs4_file *f)
652 struct nfsd_file *ret;
656 spin_lock(&f->fi_lock);
657 ret = __nfs4_get_fd(f, O_RDWR);
659 ret = __nfs4_get_fd(f, O_WRONLY);
661 ret = __nfs4_get_fd(f, O_RDONLY);
663 spin_unlock(&f->fi_lock);
667 static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
669 struct nfsd_file *ret = NULL;
671 spin_lock(&f->fi_lock);
672 if (f->fi_deleg_file)
673 ret = nfsd_file_get(f->fi_deleg_file);
674 spin_unlock(&f->fi_lock);
678 static atomic_long_t num_delegations;
679 unsigned long max_delegations;
682 * Open owner state (share locks)
685 /* hash tables for lock and open owners */
686 #define OWNER_HASH_BITS 8
687 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
688 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
690 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
694 ret = opaque_hashval(ownername->data, ownername->len);
695 return ret & OWNER_HASH_MASK;
698 /* hash table for nfs4_file */
699 #define FILE_HASH_BITS 8
700 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
702 static unsigned int file_hashval(struct svc_fh *fh)
704 struct inode *inode = d_inode(fh->fh_dentry);
706 /* XXX: why not (here & in file cache) use inode? */
707 return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS);
710 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
713 * Check if courtesy clients have conflicting access and resolve it if possible
715 * access: is op_share_access if share_access is true.
716 * Check if access mode, op_share_access, would conflict with
717 * the current deny mode of the file 'fp'.
718 * access: is op_share_deny if share_access is false.
719 * Check if the deny mode, op_share_deny, would conflict with
720 * current access of the file 'fp'.
721 * stp: skip checking this entry.
722 * new_stp: normal open, not open upgrade.
725 * false - access/deny mode conflict with normal client.
726 * true - no conflict or conflict with courtesy client(s) is resolved.
729 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
730 struct nfs4_ol_stateid *stp, u32 access, bool share_access)
732 struct nfs4_ol_stateid *st;
733 bool resolvable = true;
736 struct nfs4_client *clp;
738 lockdep_assert_held(&fp->fi_lock);
739 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
740 /* ignore lock stateid */
743 if (st == stp && new_stp)
745 /* check file access against deny mode or vice versa */
746 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
747 if (!(access & bmap_to_share_mode(bmap)))
749 clp = st->st_stid.sc_client;
750 if (try_to_expire_client(clp))
756 clp = stp->st_stid.sc_client;
757 nn = net_generic(clp->net, nfsd_net_id);
758 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
764 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
766 lockdep_assert_held(&fp->fi_lock);
768 if (access & NFS4_SHARE_ACCESS_WRITE)
769 atomic_inc(&fp->fi_access[O_WRONLY]);
770 if (access & NFS4_SHARE_ACCESS_READ)
771 atomic_inc(&fp->fi_access[O_RDONLY]);
775 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
777 lockdep_assert_held(&fp->fi_lock);
779 /* Does this access mode make sense? */
780 if (access & ~NFS4_SHARE_ACCESS_BOTH)
783 /* Does it conflict with a deny mode already set? */
784 if ((access & fp->fi_share_deny) != 0)
785 return nfserr_share_denied;
787 __nfs4_file_get_access(fp, access);
791 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
793 /* Common case is that there is no deny mode. */
795 /* Does this deny mode make sense? */
796 if (deny & ~NFS4_SHARE_DENY_BOTH)
799 if ((deny & NFS4_SHARE_DENY_READ) &&
800 atomic_read(&fp->fi_access[O_RDONLY]))
801 return nfserr_share_denied;
803 if ((deny & NFS4_SHARE_DENY_WRITE) &&
804 atomic_read(&fp->fi_access[O_WRONLY]))
805 return nfserr_share_denied;
810 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
812 might_lock(&fp->fi_lock);
814 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
815 struct nfsd_file *f1 = NULL;
816 struct nfsd_file *f2 = NULL;
818 swap(f1, fp->fi_fds[oflag]);
819 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
820 swap(f2, fp->fi_fds[O_RDWR]);
821 spin_unlock(&fp->fi_lock);
829 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
831 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
833 if (access & NFS4_SHARE_ACCESS_WRITE)
834 __nfs4_file_put_access(fp, O_WRONLY);
835 if (access & NFS4_SHARE_ACCESS_READ)
836 __nfs4_file_put_access(fp, O_RDONLY);
840 * Allocate a new open/delegation state counter. This is needed for
841 * pNFS for proper return on close semantics.
843 * Note that we only allocate it for pNFS-enabled exports, otherwise
844 * all pointers to struct nfs4_clnt_odstate are always NULL.
846 static struct nfs4_clnt_odstate *
847 alloc_clnt_odstate(struct nfs4_client *clp)
849 struct nfs4_clnt_odstate *co;
851 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
854 refcount_set(&co->co_odcount, 1);
860 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
862 struct nfs4_file *fp = co->co_file;
864 lockdep_assert_held(&fp->fi_lock);
865 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
869 get_clnt_odstate(struct nfs4_clnt_odstate *co)
872 refcount_inc(&co->co_odcount);
876 put_clnt_odstate(struct nfs4_clnt_odstate *co)
878 struct nfs4_file *fp;
884 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
885 list_del(&co->co_perfile);
886 spin_unlock(&fp->fi_lock);
888 nfsd4_return_all_file_layouts(co->co_client, fp);
889 kmem_cache_free(odstate_slab, co);
893 static struct nfs4_clnt_odstate *
894 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
896 struct nfs4_clnt_odstate *co;
897 struct nfs4_client *cl;
904 spin_lock(&fp->fi_lock);
905 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
906 if (co->co_client == cl) {
907 get_clnt_odstate(co);
913 hash_clnt_odstate_locked(new);
915 spin_unlock(&fp->fi_lock);
919 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
920 void (*sc_free)(struct nfs4_stid *))
922 struct nfs4_stid *stid;
925 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
929 idr_preload(GFP_KERNEL);
930 spin_lock(&cl->cl_lock);
931 /* Reserving 0 for start of file in nfsdfs "states" file: */
932 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
933 spin_unlock(&cl->cl_lock);
938 stid->sc_free = sc_free;
939 stid->sc_client = cl;
940 stid->sc_stateid.si_opaque.so_id = new_id;
941 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
942 /* Will be incremented before return to client: */
943 refcount_set(&stid->sc_count, 1);
944 spin_lock_init(&stid->sc_lock);
945 INIT_LIST_HEAD(&stid->sc_cp_list);
948 * It shouldn't be a problem to reuse an opaque stateid value.
949 * I don't think it is for 4.1. But with 4.0 I worry that, for
950 * example, a stray write retransmission could be accepted by
951 * the server when it should have been rejected. Therefore,
952 * adopt a trick from the sctp code to attempt to maximize the
953 * amount of time until an id is reused, by ensuring they always
954 * "increase" (mod INT_MAX):
958 kmem_cache_free(slab, stid);
963 * Create a unique stateid_t to represent each COPY.
965 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
966 unsigned char sc_type)
970 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
971 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
972 stid->sc_type = sc_type;
974 idr_preload(GFP_KERNEL);
975 spin_lock(&nn->s2s_cp_lock);
976 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
977 stid->stid.si_opaque.so_id = new_id;
978 stid->stid.si_generation = 1;
979 spin_unlock(&nn->s2s_cp_lock);
986 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
988 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID);
991 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
992 struct nfs4_stid *p_stid)
994 struct nfs4_cpntf_state *cps;
996 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
999 cps->cpntf_time = ktime_get_boottime_seconds();
1000 refcount_set(&cps->cp_stateid.sc_count, 1);
1001 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
1003 spin_lock(&nn->s2s_cp_lock);
1004 list_add(&cps->cp_list, &p_stid->sc_cp_list);
1005 spin_unlock(&nn->s2s_cp_lock);
1012 void nfs4_free_copy_state(struct nfsd4_copy *copy)
1014 struct nfsd_net *nn;
1016 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
1017 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
1018 spin_lock(&nn->s2s_cp_lock);
1019 idr_remove(&nn->s2s_cp_stateids,
1020 copy->cp_stateid.stid.si_opaque.so_id);
1021 spin_unlock(&nn->s2s_cp_lock);
1024 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1026 struct nfs4_cpntf_state *cps;
1027 struct nfsd_net *nn;
1029 nn = net_generic(net, nfsd_net_id);
1030 spin_lock(&nn->s2s_cp_lock);
1031 while (!list_empty(&stid->sc_cp_list)) {
1032 cps = list_first_entry(&stid->sc_cp_list,
1033 struct nfs4_cpntf_state, cp_list);
1034 _free_cpntf_state_locked(nn, cps);
1036 spin_unlock(&nn->s2s_cp_lock);
1039 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
1041 struct nfs4_stid *stid;
1043 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
1047 return openlockstateid(stid);
1050 static void nfs4_free_deleg(struct nfs4_stid *stid)
1052 kmem_cache_free(deleg_slab, stid);
1053 atomic_long_dec(&num_delegations);
1057 * When we recall a delegation, we should be careful not to hand it
1058 * out again straight away.
1059 * To ensure this we keep a pair of bloom filters ('new' and 'old')
1060 * in which the filehandles of recalled delegations are "stored".
1061 * If a filehandle appear in either filter, a delegation is blocked.
1062 * When a delegation is recalled, the filehandle is stored in the "new"
1064 * Every 30 seconds we swap the filters and clear the "new" one,
1065 * unless both are empty of course.
1067 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
1068 * low 3 bytes as hash-table indices.
1070 * 'blocked_delegations_lock', which is always taken in block_delegations(),
1071 * is used to manage concurrent access. Testing does not need the lock
1072 * except when swapping the two filters.
1074 static DEFINE_SPINLOCK(blocked_delegations_lock);
1075 static struct bloom_pair {
1076 int entries, old_entries;
1078 int new; /* index into 'set' */
1079 DECLARE_BITMAP(set[2], 256);
1080 } blocked_delegations;
1082 static int delegation_blocked(struct knfsd_fh *fh)
1085 struct bloom_pair *bd = &blocked_delegations;
1087 if (bd->entries == 0)
1089 if (ktime_get_seconds() - bd->swap_time > 30) {
1090 spin_lock(&blocked_delegations_lock);
1091 if (ktime_get_seconds() - bd->swap_time > 30) {
1092 bd->entries -= bd->old_entries;
1093 bd->old_entries = bd->entries;
1094 memset(bd->set[bd->new], 0,
1095 sizeof(bd->set[0]));
1096 bd->new = 1-bd->new;
1097 bd->swap_time = ktime_get_seconds();
1099 spin_unlock(&blocked_delegations_lock);
1101 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1102 if (test_bit(hash&255, bd->set[0]) &&
1103 test_bit((hash>>8)&255, bd->set[0]) &&
1104 test_bit((hash>>16)&255, bd->set[0]))
1107 if (test_bit(hash&255, bd->set[1]) &&
1108 test_bit((hash>>8)&255, bd->set[1]) &&
1109 test_bit((hash>>16)&255, bd->set[1]))
1115 static void block_delegations(struct knfsd_fh *fh)
1118 struct bloom_pair *bd = &blocked_delegations;
1120 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1122 spin_lock(&blocked_delegations_lock);
1123 __set_bit(hash&255, bd->set[bd->new]);
1124 __set_bit((hash>>8)&255, bd->set[bd->new]);
1125 __set_bit((hash>>16)&255, bd->set[bd->new]);
1126 if (bd->entries == 0)
1127 bd->swap_time = ktime_get_seconds();
1129 spin_unlock(&blocked_delegations_lock);
1132 static struct nfs4_delegation *
1133 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1134 struct svc_fh *current_fh,
1135 struct nfs4_clnt_odstate *odstate)
1137 struct nfs4_delegation *dp;
1140 dprintk("NFSD alloc_init_deleg\n");
1141 n = atomic_long_inc_return(&num_delegations);
1142 if (n < 0 || n > max_delegations)
1144 if (delegation_blocked(¤t_fh->fh_handle))
1146 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
1151 * delegation seqid's are never incremented. The 4.1 special
1152 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1153 * 0 anyway just for consistency and use 1:
1155 dp->dl_stid.sc_stateid.si_generation = 1;
1156 INIT_LIST_HEAD(&dp->dl_perfile);
1157 INIT_LIST_HEAD(&dp->dl_perclnt);
1158 INIT_LIST_HEAD(&dp->dl_recall_lru);
1159 dp->dl_clnt_odstate = odstate;
1160 get_clnt_odstate(odstate);
1161 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
1163 dp->dl_recalled = false;
1164 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1165 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1167 dp->dl_stid.sc_file = fp;
1170 atomic_long_dec(&num_delegations);
1175 nfs4_put_stid(struct nfs4_stid *s)
1177 struct nfs4_file *fp = s->sc_file;
1178 struct nfs4_client *clp = s->sc_client;
1180 might_lock(&clp->cl_lock);
1182 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1183 wake_up_all(&close_wq);
1186 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1187 nfs4_free_cpntf_statelist(clp->net, s);
1188 spin_unlock(&clp->cl_lock);
1195 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1197 stateid_t *src = &stid->sc_stateid;
1199 spin_lock(&stid->sc_lock);
1200 if (unlikely(++src->si_generation == 0))
1201 src->si_generation = 1;
1202 memcpy(dst, src, sizeof(*dst));
1203 spin_unlock(&stid->sc_lock);
1206 static void put_deleg_file(struct nfs4_file *fp)
1208 struct nfsd_file *nf = NULL;
1210 spin_lock(&fp->fi_lock);
1211 if (--fp->fi_delegees == 0)
1212 swap(nf, fp->fi_deleg_file);
1213 spin_unlock(&fp->fi_lock);
1219 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1221 struct nfs4_file *fp = dp->dl_stid.sc_file;
1222 struct nfsd_file *nf = fp->fi_deleg_file;
1224 WARN_ON_ONCE(!fp->fi_delegees);
1226 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1230 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1232 put_clnt_odstate(dp->dl_clnt_odstate);
1233 nfs4_unlock_deleg_lease(dp);
1234 nfs4_put_stid(&dp->dl_stid);
1237 void nfs4_unhash_stid(struct nfs4_stid *s)
1243 * nfs4_delegation_exists - Discover if this delegation already exists
1244 * @clp: a pointer to the nfs4_client we're granting a delegation to
1245 * @fp: a pointer to the nfs4_file we're granting a delegation on
1248 * On success: true iff an existing delegation is found
1252 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1254 struct nfs4_delegation *searchdp = NULL;
1255 struct nfs4_client *searchclp = NULL;
1257 lockdep_assert_held(&state_lock);
1258 lockdep_assert_held(&fp->fi_lock);
1260 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1261 searchclp = searchdp->dl_stid.sc_client;
1262 if (clp == searchclp) {
1270 * hash_delegation_locked - Add a delegation to the appropriate lists
1271 * @dp: a pointer to the nfs4_delegation we are adding.
1272 * @fp: a pointer to the nfs4_file we're granting a delegation on
1275 * On success: NULL if the delegation was successfully hashed.
1277 * On error: -EAGAIN if one was previously granted to this
1278 * nfs4_client for this nfs4_file. Delegation is not hashed.
1283 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1285 struct nfs4_client *clp = dp->dl_stid.sc_client;
1287 lockdep_assert_held(&state_lock);
1288 lockdep_assert_held(&fp->fi_lock);
1290 if (nfs4_delegation_exists(clp, fp))
1292 refcount_inc(&dp->dl_stid.sc_count);
1293 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1294 list_add(&dp->dl_perfile, &fp->fi_delegations);
1295 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1299 static bool delegation_hashed(struct nfs4_delegation *dp)
1301 return !(list_empty(&dp->dl_perfile));
1305 unhash_delegation_locked(struct nfs4_delegation *dp)
1307 struct nfs4_file *fp = dp->dl_stid.sc_file;
1309 lockdep_assert_held(&state_lock);
1311 if (!delegation_hashed(dp))
1314 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1315 /* Ensure that deleg break won't try to requeue it */
1317 spin_lock(&fp->fi_lock);
1318 list_del_init(&dp->dl_perclnt);
1319 list_del_init(&dp->dl_recall_lru);
1320 list_del_init(&dp->dl_perfile);
1321 spin_unlock(&fp->fi_lock);
1325 static void destroy_delegation(struct nfs4_delegation *dp)
1329 spin_lock(&state_lock);
1330 unhashed = unhash_delegation_locked(dp);
1331 spin_unlock(&state_lock);
1333 destroy_unhashed_deleg(dp);
1336 static void revoke_delegation(struct nfs4_delegation *dp)
1338 struct nfs4_client *clp = dp->dl_stid.sc_client;
1340 WARN_ON(!list_empty(&dp->dl_recall_lru));
1342 if (clp->cl_minorversion) {
1343 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1344 refcount_inc(&dp->dl_stid.sc_count);
1345 spin_lock(&clp->cl_lock);
1346 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1347 spin_unlock(&clp->cl_lock);
1349 destroy_unhashed_deleg(dp);
1356 static unsigned int clientid_hashval(u32 id)
1358 return id & CLIENT_HASH_MASK;
1361 static unsigned int clientstr_hashval(struct xdr_netobj name)
1363 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1367 * A stateid that had a deny mode associated with it is being released
1368 * or downgraded. Recalculate the deny mode on the file.
1371 recalculate_deny_mode(struct nfs4_file *fp)
1373 struct nfs4_ol_stateid *stp;
1375 spin_lock(&fp->fi_lock);
1376 fp->fi_share_deny = 0;
1377 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1378 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1379 spin_unlock(&fp->fi_lock);
1383 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1386 bool change = false;
1388 for (i = 1; i < 4; i++) {
1389 if ((i & deny) != i) {
1395 /* Recalculate per-file deny mode if there was a change */
1397 recalculate_deny_mode(stp->st_stid.sc_file);
1400 /* release all access and file references for a given stateid */
1402 release_all_access(struct nfs4_ol_stateid *stp)
1405 struct nfs4_file *fp = stp->st_stid.sc_file;
1407 if (fp && stp->st_deny_bmap != 0)
1408 recalculate_deny_mode(fp);
1410 for (i = 1; i < 4; i++) {
1411 if (test_access(i, stp))
1412 nfs4_file_put_access(stp->st_stid.sc_file, i);
1413 clear_access(i, stp);
1417 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1419 kfree(sop->so_owner.data);
1420 sop->so_ops->so_free(sop);
1423 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1425 struct nfs4_client *clp = sop->so_client;
1427 might_lock(&clp->cl_lock);
1429 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1431 sop->so_ops->so_unhash(sop);
1432 spin_unlock(&clp->cl_lock);
1433 nfs4_free_stateowner(sop);
1437 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1439 return list_empty(&stp->st_perfile);
1442 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1444 struct nfs4_file *fp = stp->st_stid.sc_file;
1446 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1448 if (list_empty(&stp->st_perfile))
1451 spin_lock(&fp->fi_lock);
1452 list_del_init(&stp->st_perfile);
1453 spin_unlock(&fp->fi_lock);
1454 list_del(&stp->st_perstateowner);
1458 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1460 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1462 put_clnt_odstate(stp->st_clnt_odstate);
1463 release_all_access(stp);
1464 if (stp->st_stateowner)
1465 nfs4_put_stateowner(stp->st_stateowner);
1466 kmem_cache_free(stateid_slab, stid);
1469 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1471 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1472 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1473 struct nfsd_file *nf;
1475 nf = find_any_file(stp->st_stid.sc_file);
1477 get_file(nf->nf_file);
1478 filp_close(nf->nf_file, (fl_owner_t)lo);
1481 nfs4_free_ol_stateid(stid);
1485 * Put the persistent reference to an already unhashed generic stateid, while
1486 * holding the cl_lock. If it's the last reference, then put it onto the
1487 * reaplist for later destruction.
1489 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1490 struct list_head *reaplist)
1492 struct nfs4_stid *s = &stp->st_stid;
1493 struct nfs4_client *clp = s->sc_client;
1495 lockdep_assert_held(&clp->cl_lock);
1497 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1499 if (!refcount_dec_and_test(&s->sc_count)) {
1500 wake_up_all(&close_wq);
1504 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1505 list_add(&stp->st_locks, reaplist);
1508 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1510 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1512 if (!unhash_ol_stateid(stp))
1514 list_del_init(&stp->st_locks);
1515 nfs4_unhash_stid(&stp->st_stid);
1519 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1521 struct nfs4_client *clp = stp->st_stid.sc_client;
1524 spin_lock(&clp->cl_lock);
1525 unhashed = unhash_lock_stateid(stp);
1526 spin_unlock(&clp->cl_lock);
1528 nfs4_put_stid(&stp->st_stid);
1531 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1533 struct nfs4_client *clp = lo->lo_owner.so_client;
1535 lockdep_assert_held(&clp->cl_lock);
1537 list_del_init(&lo->lo_owner.so_strhash);
1541 * Free a list of generic stateids that were collected earlier after being
1545 free_ol_stateid_reaplist(struct list_head *reaplist)
1547 struct nfs4_ol_stateid *stp;
1548 struct nfs4_file *fp;
1552 while (!list_empty(reaplist)) {
1553 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1555 list_del(&stp->st_locks);
1556 fp = stp->st_stid.sc_file;
1557 stp->st_stid.sc_free(&stp->st_stid);
1563 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1564 struct list_head *reaplist)
1566 struct nfs4_ol_stateid *stp;
1568 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1570 while (!list_empty(&open_stp->st_locks)) {
1571 stp = list_entry(open_stp->st_locks.next,
1572 struct nfs4_ol_stateid, st_locks);
1573 WARN_ON(!unhash_lock_stateid(stp));
1574 put_ol_stateid_locked(stp, reaplist);
1578 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1579 struct list_head *reaplist)
1581 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1583 if (!unhash_ol_stateid(stp))
1585 release_open_stateid_locks(stp, reaplist);
1589 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1591 LIST_HEAD(reaplist);
1593 spin_lock(&stp->st_stid.sc_client->cl_lock);
1594 if (unhash_open_stateid(stp, &reaplist))
1595 put_ol_stateid_locked(stp, &reaplist);
1596 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1597 free_ol_stateid_reaplist(&reaplist);
1600 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1602 struct nfs4_client *clp = oo->oo_owner.so_client;
1604 lockdep_assert_held(&clp->cl_lock);
1606 list_del_init(&oo->oo_owner.so_strhash);
1607 list_del_init(&oo->oo_perclient);
1610 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1612 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1614 struct nfs4_ol_stateid *s;
1616 spin_lock(&nn->client_lock);
1617 s = oo->oo_last_closed_stid;
1619 list_del_init(&oo->oo_close_lru);
1620 oo->oo_last_closed_stid = NULL;
1622 spin_unlock(&nn->client_lock);
1624 nfs4_put_stid(&s->st_stid);
1627 static void release_openowner(struct nfs4_openowner *oo)
1629 struct nfs4_ol_stateid *stp;
1630 struct nfs4_client *clp = oo->oo_owner.so_client;
1631 struct list_head reaplist;
1633 INIT_LIST_HEAD(&reaplist);
1635 spin_lock(&clp->cl_lock);
1636 unhash_openowner_locked(oo);
1637 while (!list_empty(&oo->oo_owner.so_stateids)) {
1638 stp = list_first_entry(&oo->oo_owner.so_stateids,
1639 struct nfs4_ol_stateid, st_perstateowner);
1640 if (unhash_open_stateid(stp, &reaplist))
1641 put_ol_stateid_locked(stp, &reaplist);
1643 spin_unlock(&clp->cl_lock);
1644 free_ol_stateid_reaplist(&reaplist);
1645 release_last_closed_stateid(oo);
1646 nfs4_put_stateowner(&oo->oo_owner);
1650 hash_sessionid(struct nfs4_sessionid *sessionid)
1652 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1654 return sid->sequence % SESSION_HASH_SIZE;
1657 #ifdef CONFIG_SUNRPC_DEBUG
1659 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1661 u32 *ptr = (u32 *)(&sessionid->data[0]);
1662 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1666 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1672 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1673 * won't be used for replay.
1675 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1677 struct nfs4_stateowner *so = cstate->replay_owner;
1679 if (nfserr == nfserr_replay_me)
1682 if (!seqid_mutating_err(ntohl(nfserr))) {
1683 nfsd4_cstate_clear_replay(cstate);
1688 if (so->so_is_open_owner)
1689 release_last_closed_stateid(openowner(so));
1695 gen_sessionid(struct nfsd4_session *ses)
1697 struct nfs4_client *clp = ses->se_client;
1698 struct nfsd4_sessionid *sid;
1700 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1701 sid->clientid = clp->cl_clientid;
1702 sid->sequence = current_sessionid++;
1707 * The protocol defines ca_maxresponssize_cached to include the size of
1708 * the rpc header, but all we need to cache is the data starting after
1709 * the end of the initial SEQUENCE operation--the rest we regenerate
1710 * each time. Therefore we can advertise a ca_maxresponssize_cached
1711 * value that is the number of bytes in our cache plus a few additional
1712 * bytes. In order to stay on the safe side, and not promise more than
1713 * we can cache, those additional bytes must be the minimum possible: 24
1714 * bytes of rpc header (xid through accept state, with AUTH_NULL
1715 * verifier), 12 for the compound header (with zero-length tag), and 44
1716 * for the SEQUENCE op response:
1718 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1721 free_session_slots(struct nfsd4_session *ses)
1725 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1726 free_svc_cred(&ses->se_slots[i]->sl_cred);
1727 kfree(ses->se_slots[i]);
1732 * We don't actually need to cache the rpc and session headers, so we
1733 * can allocate a little less for each slot:
1735 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1739 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1742 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1743 return size + sizeof(struct nfsd4_slot);
1747 * XXX: If we run out of reserved DRC memory we could (up to a point)
1748 * re-negotiate active sessions and reduce their slot usage to make
1749 * room for new connections. For now we just fail the create session.
1751 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1753 u32 slotsize = slot_bytes(ca);
1754 u32 num = ca->maxreqs;
1755 unsigned long avail, total_avail;
1756 unsigned int scale_factor;
1758 spin_lock(&nfsd_drc_lock);
1759 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1760 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1762 /* We have handed out more space than we chose in
1763 * set_max_drc() to allow. That isn't really a
1764 * problem as long as that doesn't make us think we
1765 * have lots more due to integer overflow.
1768 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1770 * Never use more than a fraction of the remaining memory,
1771 * unless it's the only way to give this client a slot.
1772 * The chosen fraction is either 1/8 or 1/number of threads,
1773 * whichever is smaller. This ensures there are adequate
1774 * slots to support multiple clients per thread.
1775 * Give the client one slot even if that would require
1776 * over-allocation--it is better than failure.
1778 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1780 avail = clamp_t(unsigned long, avail, slotsize,
1781 total_avail/scale_factor);
1782 num = min_t(int, num, avail / slotsize);
1783 num = max_t(int, num, 1);
1784 nfsd_drc_mem_used += num * slotsize;
1785 spin_unlock(&nfsd_drc_lock);
1790 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1792 int slotsize = slot_bytes(ca);
1794 spin_lock(&nfsd_drc_lock);
1795 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1796 spin_unlock(&nfsd_drc_lock);
1799 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1800 struct nfsd4_channel_attrs *battrs)
1802 int numslots = fattrs->maxreqs;
1803 int slotsize = slot_bytes(fattrs);
1804 struct nfsd4_session *new;
1807 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1808 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1809 mem = numslots * sizeof(struct nfsd4_slot *);
1811 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1814 /* allocate each struct nfsd4_slot and data cache in one piece */
1815 for (i = 0; i < numslots; i++) {
1816 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1817 if (!new->se_slots[i])
1821 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1822 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1827 kfree(new->se_slots[i]);
1832 static void free_conn(struct nfsd4_conn *c)
1834 svc_xprt_put(c->cn_xprt);
1838 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1840 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1841 struct nfs4_client *clp = c->cn_session->se_client;
1843 trace_nfsd_cb_lost(clp);
1845 spin_lock(&clp->cl_lock);
1846 if (!list_empty(&c->cn_persession)) {
1847 list_del(&c->cn_persession);
1850 nfsd4_probe_callback(clp);
1851 spin_unlock(&clp->cl_lock);
1854 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1856 struct nfsd4_conn *conn;
1858 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1861 svc_xprt_get(rqstp->rq_xprt);
1862 conn->cn_xprt = rqstp->rq_xprt;
1863 conn->cn_flags = flags;
1864 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1868 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1870 conn->cn_session = ses;
1871 list_add(&conn->cn_persession, &ses->se_conns);
1874 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1876 struct nfs4_client *clp = ses->se_client;
1878 spin_lock(&clp->cl_lock);
1879 __nfsd4_hash_conn(conn, ses);
1880 spin_unlock(&clp->cl_lock);
1883 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1885 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1886 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1889 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1893 nfsd4_hash_conn(conn, ses);
1894 ret = nfsd4_register_conn(conn);
1896 /* oops; xprt is already down: */
1897 nfsd4_conn_lost(&conn->cn_xpt_user);
1898 /* We may have gained or lost a callback channel: */
1899 nfsd4_probe_callback_sync(ses->se_client);
1902 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1904 u32 dir = NFS4_CDFC4_FORE;
1906 if (cses->flags & SESSION4_BACK_CHAN)
1907 dir |= NFS4_CDFC4_BACK;
1908 return alloc_conn(rqstp, dir);
1911 /* must be called under client_lock */
1912 static void nfsd4_del_conns(struct nfsd4_session *s)
1914 struct nfs4_client *clp = s->se_client;
1915 struct nfsd4_conn *c;
1917 spin_lock(&clp->cl_lock);
1918 while (!list_empty(&s->se_conns)) {
1919 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1920 list_del_init(&c->cn_persession);
1921 spin_unlock(&clp->cl_lock);
1923 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1926 spin_lock(&clp->cl_lock);
1928 spin_unlock(&clp->cl_lock);
1931 static void __free_session(struct nfsd4_session *ses)
1933 free_session_slots(ses);
1937 static void free_session(struct nfsd4_session *ses)
1939 nfsd4_del_conns(ses);
1940 nfsd4_put_drc_mem(&ses->se_fchannel);
1941 __free_session(ses);
1944 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1947 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1949 new->se_client = clp;
1952 INIT_LIST_HEAD(&new->se_conns);
1954 new->se_cb_seq_nr = 1;
1955 new->se_flags = cses->flags;
1956 new->se_cb_prog = cses->callback_prog;
1957 new->se_cb_sec = cses->cb_sec;
1958 atomic_set(&new->se_ref, 0);
1959 idx = hash_sessionid(&new->se_sessionid);
1960 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1961 spin_lock(&clp->cl_lock);
1962 list_add(&new->se_perclnt, &clp->cl_sessions);
1963 spin_unlock(&clp->cl_lock);
1966 struct sockaddr *sa = svc_addr(rqstp);
1968 * This is a little silly; with sessions there's no real
1969 * use for the callback address. Use the peer address
1970 * as a reasonable default for now, but consider fixing
1971 * the rpc client not to require an address in the
1974 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1975 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1979 /* caller must hold client_lock */
1980 static struct nfsd4_session *
1981 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1983 struct nfsd4_session *elem;
1985 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1987 lockdep_assert_held(&nn->client_lock);
1989 dump_sessionid(__func__, sessionid);
1990 idx = hash_sessionid(sessionid);
1991 /* Search in the appropriate list */
1992 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1993 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1994 NFS4_MAX_SESSIONID_LEN)) {
1999 dprintk("%s: session not found\n", __func__);
2003 static struct nfsd4_session *
2004 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2007 struct nfsd4_session *session;
2008 __be32 status = nfserr_badsession;
2010 session = __find_in_sessionid_hashtbl(sessionid, net);
2013 status = nfsd4_get_session_locked(session);
2021 /* caller must hold client_lock */
2023 unhash_session(struct nfsd4_session *ses)
2025 struct nfs4_client *clp = ses->se_client;
2026 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2028 lockdep_assert_held(&nn->client_lock);
2030 list_del(&ses->se_hash);
2031 spin_lock(&ses->se_client->cl_lock);
2032 list_del(&ses->se_perclnt);
2033 spin_unlock(&ses->se_client->cl_lock);
2036 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2038 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2041 * We're assuming the clid was not given out from a boot
2042 * precisely 2^32 (about 136 years) before this one. That seems
2043 * a safe assumption:
2045 if (clid->cl_boot == (u32)nn->boot_time)
2047 trace_nfsd_clid_stale(clid);
2052 * XXX Should we use a slab cache ?
2053 * This type of memory management is somewhat inefficient, but we use it
2054 * anyway since SETCLIENTID is not a common operation.
2056 static struct nfs4_client *alloc_client(struct xdr_netobj name)
2058 struct nfs4_client *clp;
2061 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2064 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2065 if (clp->cl_name.data == NULL)
2067 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2068 sizeof(struct list_head),
2070 if (!clp->cl_ownerstr_hashtbl)
2071 goto err_no_hashtbl;
2072 for (i = 0; i < OWNER_HASH_SIZE; i++)
2073 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2074 INIT_LIST_HEAD(&clp->cl_sessions);
2075 idr_init(&clp->cl_stateids);
2076 atomic_set(&clp->cl_rpc_users, 0);
2077 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2078 clp->cl_state = NFSD4_ACTIVE;
2079 atomic_set(&clp->cl_delegs_in_recall, 0);
2080 INIT_LIST_HEAD(&clp->cl_idhash);
2081 INIT_LIST_HEAD(&clp->cl_openowners);
2082 INIT_LIST_HEAD(&clp->cl_delegations);
2083 INIT_LIST_HEAD(&clp->cl_lru);
2084 INIT_LIST_HEAD(&clp->cl_revoked);
2085 #ifdef CONFIG_NFSD_PNFS
2086 INIT_LIST_HEAD(&clp->cl_lo_states);
2088 INIT_LIST_HEAD(&clp->async_copies);
2089 spin_lock_init(&clp->async_lock);
2090 spin_lock_init(&clp->cl_lock);
2091 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2094 kfree(clp->cl_name.data);
2096 kmem_cache_free(client_slab, clp);
2100 static void __free_client(struct kref *k)
2102 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2103 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2105 free_svc_cred(&clp->cl_cred);
2106 kfree(clp->cl_ownerstr_hashtbl);
2107 kfree(clp->cl_name.data);
2108 kfree(clp->cl_nii_domain.data);
2109 kfree(clp->cl_nii_name.data);
2110 idr_destroy(&clp->cl_stateids);
2111 kmem_cache_free(client_slab, clp);
2114 static void drop_client(struct nfs4_client *clp)
2116 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2120 free_client(struct nfs4_client *clp)
2122 while (!list_empty(&clp->cl_sessions)) {
2123 struct nfsd4_session *ses;
2124 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2126 list_del(&ses->se_perclnt);
2127 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2130 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2131 if (clp->cl_nfsd_dentry) {
2132 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2133 clp->cl_nfsd_dentry = NULL;
2134 wake_up_all(&expiry_wq);
2139 /* must be called under the client_lock */
2141 unhash_client_locked(struct nfs4_client *clp)
2143 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2144 struct nfsd4_session *ses;
2146 lockdep_assert_held(&nn->client_lock);
2148 /* Mark the client as expired! */
2150 /* Make it invisible */
2151 if (!list_empty(&clp->cl_idhash)) {
2152 list_del_init(&clp->cl_idhash);
2153 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2154 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2156 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2158 list_del_init(&clp->cl_lru);
2159 spin_lock(&clp->cl_lock);
2160 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2161 list_del_init(&ses->se_hash);
2162 spin_unlock(&clp->cl_lock);
2166 unhash_client(struct nfs4_client *clp)
2168 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2170 spin_lock(&nn->client_lock);
2171 unhash_client_locked(clp);
2172 spin_unlock(&nn->client_lock);
2175 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2177 if (atomic_read(&clp->cl_rpc_users))
2178 return nfserr_jukebox;
2179 unhash_client_locked(clp);
2184 __destroy_client(struct nfs4_client *clp)
2187 struct nfs4_openowner *oo;
2188 struct nfs4_delegation *dp;
2189 struct list_head reaplist;
2191 INIT_LIST_HEAD(&reaplist);
2192 spin_lock(&state_lock);
2193 while (!list_empty(&clp->cl_delegations)) {
2194 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2195 WARN_ON(!unhash_delegation_locked(dp));
2196 list_add(&dp->dl_recall_lru, &reaplist);
2198 spin_unlock(&state_lock);
2199 while (!list_empty(&reaplist)) {
2200 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2201 list_del_init(&dp->dl_recall_lru);
2202 destroy_unhashed_deleg(dp);
2204 while (!list_empty(&clp->cl_revoked)) {
2205 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2206 list_del_init(&dp->dl_recall_lru);
2207 nfs4_put_stid(&dp->dl_stid);
2209 while (!list_empty(&clp->cl_openowners)) {
2210 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2211 nfs4_get_stateowner(&oo->oo_owner);
2212 release_openowner(oo);
2214 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2215 struct nfs4_stateowner *so, *tmp;
2217 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2219 /* Should be no openowners at this point */
2220 WARN_ON_ONCE(so->so_is_open_owner);
2221 remove_blocked_locks(lockowner(so));
2224 nfsd4_return_all_client_layouts(clp);
2225 nfsd4_shutdown_copy(clp);
2226 nfsd4_shutdown_callback(clp);
2227 if (clp->cl_cb_conn.cb_xprt)
2228 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2230 wake_up_all(&expiry_wq);
2234 destroy_client(struct nfs4_client *clp)
2237 __destroy_client(clp);
2240 static void inc_reclaim_complete(struct nfs4_client *clp)
2242 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2244 if (!nn->track_reclaim_completes)
2246 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2248 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2249 nn->reclaim_str_hashtbl_size) {
2250 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2252 nfsd4_end_grace(nn);
2256 static void expire_client(struct nfs4_client *clp)
2259 nfsd4_client_record_remove(clp);
2260 __destroy_client(clp);
2263 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2265 memcpy(target->cl_verifier.data, source->data,
2266 sizeof(target->cl_verifier.data));
2269 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2271 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2272 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2275 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2277 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2278 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2280 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2281 if ((source->cr_principal && !target->cr_principal) ||
2282 (source->cr_raw_principal && !target->cr_raw_principal) ||
2283 (source->cr_targ_princ && !target->cr_targ_princ))
2286 target->cr_flavor = source->cr_flavor;
2287 target->cr_uid = source->cr_uid;
2288 target->cr_gid = source->cr_gid;
2289 target->cr_group_info = source->cr_group_info;
2290 get_group_info(target->cr_group_info);
2291 target->cr_gss_mech = source->cr_gss_mech;
2292 if (source->cr_gss_mech)
2293 gss_mech_get(source->cr_gss_mech);
2298 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2300 if (o1->len < o2->len)
2302 if (o1->len > o2->len)
2304 return memcmp(o1->data, o2->data, o1->len);
2308 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2310 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2314 same_clid(clientid_t *cl1, clientid_t *cl2)
2316 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2319 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2323 if (g1->ngroups != g2->ngroups)
2325 for (i=0; i<g1->ngroups; i++)
2326 if (!gid_eq(g1->gid[i], g2->gid[i]))
2332 * RFC 3530 language requires clid_inuse be returned when the
2333 * "principal" associated with a requests differs from that previously
2334 * used. We use uid, gid's, and gss principal string as our best
2335 * approximation. We also don't want to allow non-gss use of a client
2336 * established using gss: in theory cr_principal should catch that
2337 * change, but in practice cr_principal can be null even in the gss case
2338 * since gssd doesn't always pass down a principal string.
2340 static bool is_gss_cred(struct svc_cred *cr)
2342 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2343 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2348 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2350 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2351 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2352 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2353 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2355 /* XXX: check that cr_targ_princ fields match ? */
2356 if (cr1->cr_principal == cr2->cr_principal)
2358 if (!cr1->cr_principal || !cr2->cr_principal)
2360 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2363 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2365 struct svc_cred *cr = &rqstp->rq_cred;
2368 if (!cr->cr_gss_mech)
2370 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2371 return service == RPC_GSS_SVC_INTEGRITY ||
2372 service == RPC_GSS_SVC_PRIVACY;
2375 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2377 struct svc_cred *cr = &rqstp->rq_cred;
2379 if (!cl->cl_mach_cred)
2381 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2383 if (!svc_rqst_integrity_protected(rqstp))
2385 if (cl->cl_cred.cr_raw_principal)
2386 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2387 cr->cr_raw_principal);
2388 if (!cr->cr_principal)
2390 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2393 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2398 * This is opaque to client, so no need to byte-swap. Use
2399 * __force to keep sparse happy
2401 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2402 verf[1] = (__force __be32)nn->clverifier_counter++;
2403 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2406 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2408 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2409 clp->cl_clientid.cl_id = nn->clientid_counter++;
2410 gen_confirm(clp, nn);
2413 static struct nfs4_stid *
2414 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2416 struct nfs4_stid *ret;
2418 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2419 if (!ret || !ret->sc_type)
2424 static struct nfs4_stid *
2425 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2427 struct nfs4_stid *s;
2429 spin_lock(&cl->cl_lock);
2430 s = find_stateid_locked(cl, t);
2432 if (typemask & s->sc_type)
2433 refcount_inc(&s->sc_count);
2437 spin_unlock(&cl->cl_lock);
2441 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2443 struct nfsdfs_client *nc;
2444 nc = get_nfsdfs_client(inode);
2447 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2450 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2452 seq_printf(m, "\"");
2453 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2454 seq_printf(m, "\"");
2457 static const char *cb_state2str(int state)
2462 case NFSD4_CB_UNKNOWN:
2466 case NFSD4_CB_FAULT:
2472 static int client_info_show(struct seq_file *m, void *v)
2474 struct inode *inode = m->private;
2475 struct nfs4_client *clp;
2478 clp = get_nfsdfs_clp(inode);
2481 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2482 seq_printf(m, "clientid: 0x%llx\n", clid);
2483 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2485 if (clp->cl_state == NFSD4_COURTESY)
2486 seq_puts(m, "status: courtesy\n");
2487 else if (clp->cl_state == NFSD4_EXPIRABLE)
2488 seq_puts(m, "status: expirable\n");
2489 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2490 seq_puts(m, "status: confirmed\n");
2492 seq_puts(m, "status: unconfirmed\n");
2493 seq_printf(m, "seconds from last renew: %lld\n",
2494 ktime_get_boottime_seconds() - clp->cl_time);
2495 seq_printf(m, "name: ");
2496 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2497 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2498 if (clp->cl_nii_domain.data) {
2499 seq_printf(m, "Implementation domain: ");
2500 seq_quote_mem(m, clp->cl_nii_domain.data,
2501 clp->cl_nii_domain.len);
2502 seq_printf(m, "\nImplementation name: ");
2503 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2504 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2505 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2507 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2508 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
2514 static int client_info_open(struct inode *inode, struct file *file)
2516 return single_open(file, client_info_show, inode);
2519 static const struct file_operations client_info_fops = {
2520 .open = client_info_open,
2522 .llseek = seq_lseek,
2523 .release = single_release,
2526 static void *states_start(struct seq_file *s, loff_t *pos)
2527 __acquires(&clp->cl_lock)
2529 struct nfs4_client *clp = s->private;
2530 unsigned long id = *pos;
2533 spin_lock(&clp->cl_lock);
2534 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2539 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2541 struct nfs4_client *clp = s->private;
2542 unsigned long id = *pos;
2547 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2552 static void states_stop(struct seq_file *s, void *v)
2553 __releases(&clp->cl_lock)
2555 struct nfs4_client *clp = s->private;
2557 spin_unlock(&clp->cl_lock);
2560 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2562 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2565 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2567 struct inode *inode = f->nf_inode;
2569 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2570 MAJOR(inode->i_sb->s_dev),
2571 MINOR(inode->i_sb->s_dev),
2575 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2577 seq_printf(s, "owner: ");
2578 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2581 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2583 seq_printf(s, "0x%.8x", stid->si_generation);
2584 seq_printf(s, "%12phN", &stid->si_opaque);
2587 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2589 struct nfs4_ol_stateid *ols;
2590 struct nfs4_file *nf;
2591 struct nfsd_file *file;
2592 struct nfs4_stateowner *oo;
2593 unsigned int access, deny;
2595 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2596 return 0; /* XXX: or SEQ_SKIP? */
2597 ols = openlockstateid(st);
2598 oo = ols->st_stateowner;
2600 file = find_any_file(nf);
2604 seq_printf(s, "- ");
2605 nfs4_show_stateid(s, &st->sc_stateid);
2606 seq_printf(s, ": { type: open, ");
2608 access = bmap_to_share_mode(ols->st_access_bmap);
2609 deny = bmap_to_share_mode(ols->st_deny_bmap);
2611 seq_printf(s, "access: %s%s, ",
2612 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2613 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2614 seq_printf(s, "deny: %s%s, ",
2615 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2616 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2618 nfs4_show_superblock(s, file);
2619 seq_printf(s, ", ");
2620 nfs4_show_fname(s, file);
2621 seq_printf(s, ", ");
2622 nfs4_show_owner(s, oo);
2623 seq_printf(s, " }\n");
2624 nfsd_file_put(file);
2629 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2631 struct nfs4_ol_stateid *ols;
2632 struct nfs4_file *nf;
2633 struct nfsd_file *file;
2634 struct nfs4_stateowner *oo;
2636 ols = openlockstateid(st);
2637 oo = ols->st_stateowner;
2639 file = find_any_file(nf);
2643 seq_printf(s, "- ");
2644 nfs4_show_stateid(s, &st->sc_stateid);
2645 seq_printf(s, ": { type: lock, ");
2648 * Note: a lock stateid isn't really the same thing as a lock,
2649 * it's the locking state held by one owner on a file, and there
2650 * may be multiple (or no) lock ranges associated with it.
2651 * (Same for the matter is true of open stateids.)
2654 nfs4_show_superblock(s, file);
2655 /* XXX: open stateid? */
2656 seq_printf(s, ", ");
2657 nfs4_show_fname(s, file);
2658 seq_printf(s, ", ");
2659 nfs4_show_owner(s, oo);
2660 seq_printf(s, " }\n");
2661 nfsd_file_put(file);
2666 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2668 struct nfs4_delegation *ds;
2669 struct nfs4_file *nf;
2670 struct nfsd_file *file;
2672 ds = delegstateid(st);
2674 file = find_deleg_file(nf);
2678 seq_printf(s, "- ");
2679 nfs4_show_stateid(s, &st->sc_stateid);
2680 seq_printf(s, ": { type: deleg, ");
2682 /* Kinda dead code as long as we only support read delegs: */
2683 seq_printf(s, "access: %s, ",
2684 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2686 /* XXX: lease time, whether it's being recalled. */
2688 nfs4_show_superblock(s, file);
2689 seq_printf(s, ", ");
2690 nfs4_show_fname(s, file);
2691 seq_printf(s, " }\n");
2692 nfsd_file_put(file);
2697 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2699 struct nfs4_layout_stateid *ls;
2700 struct nfsd_file *file;
2702 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2705 seq_printf(s, "- ");
2706 nfs4_show_stateid(s, &st->sc_stateid);
2707 seq_printf(s, ": { type: layout, ");
2709 /* XXX: What else would be useful? */
2711 nfs4_show_superblock(s, file);
2712 seq_printf(s, ", ");
2713 nfs4_show_fname(s, file);
2714 seq_printf(s, " }\n");
2719 static int states_show(struct seq_file *s, void *v)
2721 struct nfs4_stid *st = v;
2723 switch (st->sc_type) {
2724 case NFS4_OPEN_STID:
2725 return nfs4_show_open(s, st);
2726 case NFS4_LOCK_STID:
2727 return nfs4_show_lock(s, st);
2728 case NFS4_DELEG_STID:
2729 return nfs4_show_deleg(s, st);
2730 case NFS4_LAYOUT_STID:
2731 return nfs4_show_layout(s, st);
2733 return 0; /* XXX: or SEQ_SKIP? */
2735 /* XXX: copy stateids? */
2738 static struct seq_operations states_seq_ops = {
2739 .start = states_start,
2740 .next = states_next,
2741 .stop = states_stop,
2745 static int client_states_open(struct inode *inode, struct file *file)
2748 struct nfs4_client *clp;
2751 clp = get_nfsdfs_clp(inode);
2755 ret = seq_open(file, &states_seq_ops);
2758 s = file->private_data;
2763 static int client_opens_release(struct inode *inode, struct file *file)
2765 struct seq_file *m = file->private_data;
2766 struct nfs4_client *clp = m->private;
2768 /* XXX: alternatively, we could get/drop in seq start/stop */
2773 static const struct file_operations client_states_fops = {
2774 .open = client_states_open,
2776 .llseek = seq_lseek,
2777 .release = client_opens_release,
2781 * Normally we refuse to destroy clients that are in use, but here the
2782 * administrator is telling us to just do it. We also want to wait
2783 * so the caller has a guarantee that the client's locks are gone by
2784 * the time the write returns:
2786 static void force_expire_client(struct nfs4_client *clp)
2788 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2789 bool already_expired;
2791 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2793 spin_lock(&nn->client_lock);
2795 spin_unlock(&nn->client_lock);
2797 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2798 spin_lock(&nn->client_lock);
2799 already_expired = list_empty(&clp->cl_lru);
2800 if (!already_expired)
2801 unhash_client_locked(clp);
2802 spin_unlock(&nn->client_lock);
2804 if (!already_expired)
2807 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2810 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2811 size_t size, loff_t *pos)
2814 struct nfs4_client *clp;
2816 data = simple_transaction_get(file, buf, size);
2818 return PTR_ERR(data);
2819 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2821 clp = get_nfsdfs_clp(file_inode(file));
2824 force_expire_client(clp);
2829 static const struct file_operations client_ctl_fops = {
2830 .write = client_ctl_write,
2831 .release = simple_transaction_release,
2834 static const struct tree_descr client_files[] = {
2835 [0] = {"info", &client_info_fops, S_IRUSR},
2836 [1] = {"states", &client_states_fops, S_IRUSR},
2837 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
2841 static struct nfs4_client *create_client(struct xdr_netobj name,
2842 struct svc_rqst *rqstp, nfs4_verifier *verf)
2844 struct nfs4_client *clp;
2845 struct sockaddr *sa = svc_addr(rqstp);
2847 struct net *net = SVC_NET(rqstp);
2848 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2849 struct dentry *dentries[ARRAY_SIZE(client_files)];
2851 clp = alloc_client(name);
2855 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2861 kref_init(&clp->cl_nfsdfs.cl_ref);
2862 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2863 clp->cl_time = ktime_get_boottime_seconds();
2864 clear_bit(0, &clp->cl_cb_slot_busy);
2865 copy_verf(clp, verf);
2866 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2867 clp->cl_cb_session = NULL;
2869 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2870 nn, &clp->cl_nfsdfs,
2871 clp->cl_clientid.cl_id - nn->clientid_base,
2872 client_files, dentries);
2873 clp->cl_nfsd_info_dentry = dentries[0];
2874 if (!clp->cl_nfsd_dentry) {
2882 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2884 struct rb_node **new = &(root->rb_node), *parent = NULL;
2885 struct nfs4_client *clp;
2888 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2891 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2892 new = &((*new)->rb_left);
2894 new = &((*new)->rb_right);
2897 rb_link_node(&new_clp->cl_namenode, parent, new);
2898 rb_insert_color(&new_clp->cl_namenode, root);
2901 static struct nfs4_client *
2902 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2905 struct rb_node *node = root->rb_node;
2906 struct nfs4_client *clp;
2909 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2910 cmp = compare_blob(&clp->cl_name, name);
2912 node = node->rb_left;
2914 node = node->rb_right;
2922 add_to_unconfirmed(struct nfs4_client *clp)
2924 unsigned int idhashval;
2925 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2927 lockdep_assert_held(&nn->client_lock);
2929 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2930 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2931 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2932 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2933 renew_client_locked(clp);
2937 move_to_confirmed(struct nfs4_client *clp)
2939 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2940 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2942 lockdep_assert_held(&nn->client_lock);
2944 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2945 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2946 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2947 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2948 trace_nfsd_clid_confirmed(&clp->cl_clientid);
2949 renew_client_locked(clp);
2952 static struct nfs4_client *
2953 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2955 struct nfs4_client *clp;
2956 unsigned int idhashval = clientid_hashval(clid->cl_id);
2958 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2959 if (same_clid(&clp->cl_clientid, clid)) {
2960 if ((bool)clp->cl_minorversion != sessions)
2962 renew_client_locked(clp);
2969 static struct nfs4_client *
2970 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2972 struct list_head *tbl = nn->conf_id_hashtbl;
2974 lockdep_assert_held(&nn->client_lock);
2975 return find_client_in_id_table(tbl, clid, sessions);
2978 static struct nfs4_client *
2979 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2981 struct list_head *tbl = nn->unconf_id_hashtbl;
2983 lockdep_assert_held(&nn->client_lock);
2984 return find_client_in_id_table(tbl, clid, sessions);
2987 static bool clp_used_exchangeid(struct nfs4_client *clp)
2989 return clp->cl_exchange_flags != 0;
2992 static struct nfs4_client *
2993 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2995 lockdep_assert_held(&nn->client_lock);
2996 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2999 static struct nfs4_client *
3000 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3002 lockdep_assert_held(&nn->client_lock);
3003 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3007 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3009 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3010 struct sockaddr *sa = svc_addr(rqstp);
3011 u32 scopeid = rpc_get_scope_id(sa);
3012 unsigned short expected_family;
3014 /* Currently, we only support tcp and tcp6 for the callback channel */
3015 if (se->se_callback_netid_len == 3 &&
3016 !memcmp(se->se_callback_netid_val, "tcp", 3))
3017 expected_family = AF_INET;
3018 else if (se->se_callback_netid_len == 4 &&
3019 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3020 expected_family = AF_INET6;
3024 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3025 se->se_callback_addr_len,
3026 (struct sockaddr *)&conn->cb_addr,
3027 sizeof(conn->cb_addr));
3029 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3032 if (conn->cb_addr.ss_family == AF_INET6)
3033 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3035 conn->cb_prog = se->se_callback_prog;
3036 conn->cb_ident = se->se_callback_ident;
3037 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3038 trace_nfsd_cb_args(clp, conn);
3041 conn->cb_addr.ss_family = AF_UNSPEC;
3042 conn->cb_addrlen = 0;
3043 trace_nfsd_cb_nodelegs(clp);
3048 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3051 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3053 struct xdr_buf *buf = resp->xdr->buf;
3054 struct nfsd4_slot *slot = resp->cstate.slot;
3057 dprintk("--> %s slot %p\n", __func__, slot);
3059 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3060 slot->sl_opcnt = resp->opcnt;
3061 slot->sl_status = resp->cstate.status;
3062 free_svc_cred(&slot->sl_cred);
3063 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3065 if (!nfsd4_cache_this(resp)) {
3066 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3069 slot->sl_flags |= NFSD4_SLOT_CACHED;
3071 base = resp->cstate.data_offset;
3072 slot->sl_datalen = buf->len - base;
3073 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3074 WARN(1, "%s: sessions DRC could not cache compound\n",
3080 * Encode the replay sequence operation from the slot values.
3081 * If cachethis is FALSE encode the uncached rep error on the next
3082 * operation which sets resp->p and increments resp->opcnt for
3083 * nfs4svc_encode_compoundres.
3087 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3088 struct nfsd4_compoundres *resp)
3090 struct nfsd4_op *op;
3091 struct nfsd4_slot *slot = resp->cstate.slot;
3093 /* Encode the replayed sequence operation */
3094 op = &args->ops[resp->opcnt - 1];
3095 nfsd4_encode_operation(resp, op);
3097 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3099 if (args->opcnt == 1) {
3101 * The original operation wasn't a solo sequence--we
3102 * always cache those--so this retry must not match the
3105 op->status = nfserr_seq_false_retry;
3107 op = &args->ops[resp->opcnt++];
3108 op->status = nfserr_retry_uncached_rep;
3109 nfsd4_encode_operation(resp, op);
3115 * The sequence operation is not cached because we can use the slot and
3119 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3120 struct nfsd4_sequence *seq)
3122 struct nfsd4_slot *slot = resp->cstate.slot;
3123 struct xdr_stream *xdr = resp->xdr;
3127 dprintk("--> %s slot %p\n", __func__, slot);
3129 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3133 p = xdr_reserve_space(xdr, slot->sl_datalen);
3136 return nfserr_serverfault;
3138 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3139 xdr_commit_encode(xdr);
3141 resp->opcnt = slot->sl_opcnt;
3142 return slot->sl_status;
3146 * Set the exchange_id flags returned by the server.
3149 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3151 #ifdef CONFIG_NFSD_PNFS
3152 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3154 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3157 /* Referrals are supported, Migration is not. */
3158 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3160 /* set the wire flags to return to client. */
3161 clid->flags = new->cl_exchange_flags;
3164 static bool client_has_openowners(struct nfs4_client *clp)
3166 struct nfs4_openowner *oo;
3168 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3169 if (!list_empty(&oo->oo_owner.so_stateids))
3175 static bool client_has_state(struct nfs4_client *clp)
3177 return client_has_openowners(clp)
3178 #ifdef CONFIG_NFSD_PNFS
3179 || !list_empty(&clp->cl_lo_states)
3181 || !list_empty(&clp->cl_delegations)
3182 || !list_empty(&clp->cl_sessions)
3183 || !list_empty(&clp->async_copies);
3186 static __be32 copy_impl_id(struct nfs4_client *clp,
3187 struct nfsd4_exchange_id *exid)
3189 if (!exid->nii_domain.data)
3191 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3192 if (!clp->cl_nii_domain.data)
3193 return nfserr_jukebox;
3194 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3195 if (!clp->cl_nii_name.data)
3196 return nfserr_jukebox;
3197 clp->cl_nii_time = exid->nii_time;
3202 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3203 union nfsd4_op_u *u)
3205 struct nfsd4_exchange_id *exid = &u->exchange_id;
3206 struct nfs4_client *conf, *new;
3207 struct nfs4_client *unconf = NULL;
3209 char addr_str[INET6_ADDRSTRLEN];
3210 nfs4_verifier verf = exid->verifier;
3211 struct sockaddr *sa = svc_addr(rqstp);
3212 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3213 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3215 rpc_ntop(sa, addr_str, sizeof(addr_str));
3216 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3217 "ip_addr=%s flags %x, spa_how %u\n",
3218 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3219 addr_str, exid->flags, exid->spa_how);
3221 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3222 return nfserr_inval;
3224 new = create_client(exid->clname, rqstp, &verf);
3226 return nfserr_jukebox;
3227 status = copy_impl_id(new, exid);
3231 switch (exid->spa_how) {
3233 exid->spo_must_enforce[0] = 0;
3234 exid->spo_must_enforce[1] = (
3235 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3236 1 << (OP_EXCHANGE_ID - 32) |
3237 1 << (OP_CREATE_SESSION - 32) |
3238 1 << (OP_DESTROY_SESSION - 32) |
3239 1 << (OP_DESTROY_CLIENTID - 32));
3241 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3242 1 << (OP_OPEN_DOWNGRADE) |
3244 1 << (OP_DELEGRETURN));
3246 exid->spo_must_allow[1] &= (
3247 1 << (OP_TEST_STATEID - 32) |
3248 1 << (OP_FREE_STATEID - 32));
3249 if (!svc_rqst_integrity_protected(rqstp)) {
3250 status = nfserr_inval;
3254 * Sometimes userspace doesn't give us a principal.
3255 * Which is a bug, really. Anyway, we can't enforce
3256 * MACH_CRED in that case, better to give up now:
3258 if (!new->cl_cred.cr_principal &&
3259 !new->cl_cred.cr_raw_principal) {
3260 status = nfserr_serverfault;
3263 new->cl_mach_cred = true;
3267 default: /* checked by xdr code */
3271 status = nfserr_encr_alg_unsupp;
3275 /* Cases below refer to rfc 5661 section 18.35.4: */
3276 spin_lock(&nn->client_lock);
3277 conf = find_confirmed_client_by_name(&exid->clname, nn);
3279 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3280 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3283 if (!clp_used_exchangeid(conf)) { /* buggy client */
3284 status = nfserr_inval;
3287 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3288 status = nfserr_wrong_cred;
3291 if (!creds_match) { /* case 9 */
3292 status = nfserr_perm;
3295 if (!verfs_match) { /* case 8 */
3296 status = nfserr_not_same;
3300 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3301 trace_nfsd_clid_confirmed_r(conf);
3304 if (!creds_match) { /* case 3 */
3305 if (client_has_state(conf)) {
3306 status = nfserr_clid_inuse;
3307 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3312 if (verfs_match) { /* case 2 */
3313 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3314 trace_nfsd_clid_confirmed_r(conf);
3317 /* case 5, client reboot */
3318 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3323 if (update) { /* case 7 */
3324 status = nfserr_noent;
3328 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3329 if (unconf) /* case 4, possible retry or client restart */
3330 unhash_client_locked(unconf);
3332 /* case 1, new owner ID */
3333 trace_nfsd_clid_fresh(new);
3337 status = mark_client_expired_locked(conf);
3340 trace_nfsd_clid_replaced(&conf->cl_clientid);
3342 new->cl_minorversion = cstate->minorversion;
3343 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3344 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3346 add_to_unconfirmed(new);
3349 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3350 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3352 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3353 nfsd4_set_ex_flags(conf, exid);
3355 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3356 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3360 spin_unlock(&nn->client_lock);
3365 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3366 expire_client(unconf);
3372 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3374 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3377 /* The slot is in use, and no response has been sent. */
3379 if (seqid == slot_seqid)
3380 return nfserr_jukebox;
3382 return nfserr_seq_misordered;
3384 /* Note unsigned 32-bit arithmetic handles wraparound: */
3385 if (likely(seqid == slot_seqid + 1))
3387 if (seqid == slot_seqid)
3388 return nfserr_replay_cache;
3389 return nfserr_seq_misordered;
3393 * Cache the create session result into the create session single DRC
3394 * slot cache by saving the xdr structure. sl_seqid has been set.
3395 * Do this for solo or embedded create session operations.
3398 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3399 struct nfsd4_clid_slot *slot, __be32 nfserr)
3401 slot->sl_status = nfserr;
3402 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3406 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3407 struct nfsd4_clid_slot *slot)
3409 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3410 return slot->sl_status;
3413 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3414 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3415 1 + /* MIN tag is length with zero, only length */ \
3416 3 + /* version, opcount, opcode */ \
3417 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3418 /* seqid, slotID, slotID, cache */ \
3419 4 ) * sizeof(__be32))
3421 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3422 2 + /* verifier: AUTH_NULL, length 0 */\
3424 1 + /* MIN tag is length with zero, only length */ \
3425 3 + /* opcount, opcode, opstatus*/ \
3426 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3427 /* seqid, slotID, slotID, slotID, status */ \
3428 5 ) * sizeof(__be32))
3430 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3432 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3434 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3435 return nfserr_toosmall;
3436 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3437 return nfserr_toosmall;
3438 ca->headerpadsz = 0;
3439 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3440 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3441 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3442 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3443 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3444 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3446 * Note decreasing slot size below client's request may make it
3447 * difficult for client to function correctly, whereas
3448 * decreasing the number of slots will (just?) affect
3449 * performance. When short on memory we therefore prefer to
3450 * decrease number of slots instead of their size. Clients that
3451 * request larger slots than they need will get poor results:
3452 * Note that we always allow at least one slot, because our
3453 * accounting is soft and provides no guarantees either way.
3455 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3461 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3462 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3464 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3465 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3467 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3468 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3470 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3471 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3472 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3473 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3476 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3478 ca->headerpadsz = 0;
3480 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3481 return nfserr_toosmall;
3482 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3483 return nfserr_toosmall;
3484 ca->maxresp_cached = 0;
3486 return nfserr_toosmall;
3491 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3493 switch (cbs->flavor) {
3499 * GSS case: the spec doesn't allow us to return this
3500 * error. But it also doesn't allow us not to support
3502 * I'd rather this fail hard than return some error the
3503 * client might think it can already handle:
3505 return nfserr_encr_alg_unsupp;
3510 nfsd4_create_session(struct svc_rqst *rqstp,
3511 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3513 struct nfsd4_create_session *cr_ses = &u->create_session;
3514 struct sockaddr *sa = svc_addr(rqstp);
3515 struct nfs4_client *conf, *unconf;
3516 struct nfs4_client *old = NULL;
3517 struct nfsd4_session *new;
3518 struct nfsd4_conn *conn;
3519 struct nfsd4_clid_slot *cs_slot = NULL;
3521 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3523 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3524 return nfserr_inval;
3525 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3528 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3531 status = check_backchannel_attrs(&cr_ses->back_channel);
3533 goto out_release_drc_mem;
3534 status = nfserr_jukebox;
3535 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3537 goto out_release_drc_mem;
3538 conn = alloc_conn_from_crses(rqstp, cr_ses);
3540 goto out_free_session;
3542 spin_lock(&nn->client_lock);
3543 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3544 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3545 WARN_ON_ONCE(conf && unconf);
3548 status = nfserr_wrong_cred;
3549 if (!nfsd4_mach_creds_match(conf, rqstp))
3551 cs_slot = &conf->cl_cs_slot;
3552 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3554 if (status == nfserr_replay_cache)
3555 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3558 } else if (unconf) {
3559 status = nfserr_clid_inuse;
3560 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3561 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3562 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
3565 status = nfserr_wrong_cred;
3566 if (!nfsd4_mach_creds_match(unconf, rqstp))
3568 cs_slot = &unconf->cl_cs_slot;
3569 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3571 /* an unconfirmed replay returns misordered */
3572 status = nfserr_seq_misordered;
3575 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3577 status = mark_client_expired_locked(old);
3582 trace_nfsd_clid_replaced(&old->cl_clientid);
3584 move_to_confirmed(unconf);
3587 status = nfserr_stale_clientid;
3591 /* Persistent sessions are not supported */
3592 cr_ses->flags &= ~SESSION4_PERSIST;
3593 /* Upshifting from TCP to RDMA is not supported */
3594 cr_ses->flags &= ~SESSION4_RDMA;
3596 init_session(rqstp, new, conf, cr_ses);
3597 nfsd4_get_session_locked(new);
3599 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3600 NFS4_MAX_SESSIONID_LEN);
3601 cs_slot->sl_seqid++;
3602 cr_ses->seqid = cs_slot->sl_seqid;
3604 /* cache solo and embedded create sessions under the client_lock */
3605 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3606 spin_unlock(&nn->client_lock);
3608 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3609 /* init connection and backchannel */
3610 nfsd4_init_conn(rqstp, conn, new);
3611 nfsd4_put_session(new);
3616 spin_unlock(&nn->client_lock);
3621 __free_session(new);
3622 out_release_drc_mem:
3623 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3627 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3630 case NFS4_CDFC4_FORE:
3631 case NFS4_CDFC4_BACK:
3633 case NFS4_CDFC4_FORE_OR_BOTH:
3634 case NFS4_CDFC4_BACK_OR_BOTH:
3635 *dir = NFS4_CDFC4_BOTH;
3638 return nfserr_inval;
3641 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3642 struct nfsd4_compound_state *cstate,
3643 union nfsd4_op_u *u)
3645 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3646 struct nfsd4_session *session = cstate->session;
3647 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3650 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3653 spin_lock(&nn->client_lock);
3654 session->se_cb_prog = bc->bc_cb_program;
3655 session->se_cb_sec = bc->bc_cb_sec;
3656 spin_unlock(&nn->client_lock);
3658 nfsd4_probe_callback(session->se_client);
3663 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3665 struct nfsd4_conn *c;
3667 list_for_each_entry(c, &s->se_conns, cn_persession) {
3668 if (c->cn_xprt == xpt) {
3675 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3676 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3678 struct nfs4_client *clp = session->se_client;
3679 struct svc_xprt *xpt = rqst->rq_xprt;
3680 struct nfsd4_conn *c;
3683 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3684 spin_lock(&clp->cl_lock);
3685 c = __nfsd4_find_conn(xpt, session);
3687 status = nfserr_noent;
3688 else if (req == c->cn_flags)
3690 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3691 c->cn_flags != NFS4_CDFC4_BACK)
3693 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3694 c->cn_flags != NFS4_CDFC4_FORE)
3697 status = nfserr_inval;
3698 spin_unlock(&clp->cl_lock);
3699 if (status == nfs_ok && conn)
3704 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3705 struct nfsd4_compound_state *cstate,
3706 union nfsd4_op_u *u)
3708 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3710 struct nfsd4_conn *conn;
3711 struct nfsd4_session *session;
3712 struct net *net = SVC_NET(rqstp);
3713 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3715 if (!nfsd4_last_compound_op(rqstp))
3716 return nfserr_not_only_op;
3717 spin_lock(&nn->client_lock);
3718 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3719 spin_unlock(&nn->client_lock);
3721 goto out_no_session;
3722 status = nfserr_wrong_cred;
3723 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3725 status = nfsd4_match_existing_connection(rqstp, session,
3727 if (status == nfs_ok) {
3728 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3729 bcts->dir == NFS4_CDFC4_BACK)
3730 conn->cn_flags |= NFS4_CDFC4_BACK;
3731 nfsd4_probe_callback(session->se_client);
3734 if (status == nfserr_inval)
3736 status = nfsd4_map_bcts_dir(&bcts->dir);
3739 conn = alloc_conn(rqstp, bcts->dir);
3740 status = nfserr_jukebox;
3743 nfsd4_init_conn(rqstp, conn, session);
3746 nfsd4_put_session(session);
3751 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3753 if (!cstate->session)
3755 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3759 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3760 union nfsd4_op_u *u)
3762 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3763 struct nfsd4_session *ses;
3765 int ref_held_by_me = 0;
3766 struct net *net = SVC_NET(r);
3767 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3769 status = nfserr_not_only_op;
3770 if (nfsd4_compound_in_session(cstate, sessionid)) {
3771 if (!nfsd4_last_compound_op(r))
3775 dump_sessionid(__func__, sessionid);
3776 spin_lock(&nn->client_lock);
3777 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3779 goto out_client_lock;
3780 status = nfserr_wrong_cred;
3781 if (!nfsd4_mach_creds_match(ses->se_client, r))
3782 goto out_put_session;
3783 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3785 goto out_put_session;
3786 unhash_session(ses);
3787 spin_unlock(&nn->client_lock);
3789 nfsd4_probe_callback_sync(ses->se_client);
3791 spin_lock(&nn->client_lock);
3794 nfsd4_put_session_locked(ses);
3796 spin_unlock(&nn->client_lock);
3801 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3803 struct nfs4_client *clp = ses->se_client;
3804 struct nfsd4_conn *c;
3805 __be32 status = nfs_ok;
3808 spin_lock(&clp->cl_lock);
3809 c = __nfsd4_find_conn(new->cn_xprt, ses);
3812 status = nfserr_conn_not_bound_to_session;
3813 if (clp->cl_mach_cred)
3815 __nfsd4_hash_conn(new, ses);
3816 spin_unlock(&clp->cl_lock);
3817 ret = nfsd4_register_conn(new);
3819 /* oops; xprt is already down: */
3820 nfsd4_conn_lost(&new->cn_xpt_user);
3823 spin_unlock(&clp->cl_lock);
3828 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3830 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3832 return args->opcnt > session->se_fchannel.maxops;
3835 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3836 struct nfsd4_session *session)
3838 struct xdr_buf *xb = &rqstp->rq_arg;
3840 return xb->len > session->se_fchannel.maxreq_sz;
3843 static bool replay_matches_cache(struct svc_rqst *rqstp,
3844 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3846 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3848 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3849 (bool)seq->cachethis)
3852 * If there's an error then the reply can have fewer ops than
3855 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3858 * But if we cached a reply with *more* ops than the call you're
3859 * sending us now, then this new call is clearly not really a
3860 * replay of the old one:
3862 if (slot->sl_opcnt > argp->opcnt)
3864 /* This is the only check explicitly called by spec: */
3865 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3868 * There may be more comparisons we could actually do, but the
3869 * spec doesn't require us to catch every case where the calls
3870 * don't match (that would require caching the call as well as
3871 * the reply), so we don't bother.
3877 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3878 union nfsd4_op_u *u)
3880 struct nfsd4_sequence *seq = &u->sequence;
3881 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3882 struct xdr_stream *xdr = resp->xdr;
3883 struct nfsd4_session *session;
3884 struct nfs4_client *clp;
3885 struct nfsd4_slot *slot;
3886 struct nfsd4_conn *conn;
3889 struct net *net = SVC_NET(rqstp);
3890 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3892 if (resp->opcnt != 1)
3893 return nfserr_sequence_pos;
3896 * Will be either used or freed by nfsd4_sequence_check_conn
3899 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3901 return nfserr_jukebox;
3903 spin_lock(&nn->client_lock);
3904 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3906 goto out_no_session;
3907 clp = session->se_client;
3909 status = nfserr_too_many_ops;
3910 if (nfsd4_session_too_many_ops(rqstp, session))
3911 goto out_put_session;
3913 status = nfserr_req_too_big;
3914 if (nfsd4_request_too_big(rqstp, session))
3915 goto out_put_session;
3917 status = nfserr_badslot;
3918 if (seq->slotid >= session->se_fchannel.maxreqs)
3919 goto out_put_session;
3921 slot = session->se_slots[seq->slotid];
3922 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3924 /* We do not negotiate the number of slots yet, so set the
3925 * maxslots to the session maxreqs which is used to encode
3926 * sr_highest_slotid and the sr_target_slot id to maxslots */
3927 seq->maxslots = session->se_fchannel.maxreqs;
3929 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3930 slot->sl_flags & NFSD4_SLOT_INUSE);
3931 if (status == nfserr_replay_cache) {
3932 status = nfserr_seq_misordered;
3933 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3934 goto out_put_session;
3935 status = nfserr_seq_false_retry;
3936 if (!replay_matches_cache(rqstp, seq, slot))
3937 goto out_put_session;
3938 cstate->slot = slot;
3939 cstate->session = session;
3941 /* Return the cached reply status and set cstate->status
3942 * for nfsd4_proc_compound processing */
3943 status = nfsd4_replay_cache_entry(resp, seq);
3944 cstate->status = nfserr_replay_cache;
3948 goto out_put_session;
3950 status = nfsd4_sequence_check_conn(conn, session);
3953 goto out_put_session;
3955 buflen = (seq->cachethis) ?
3956 session->se_fchannel.maxresp_cached :
3957 session->se_fchannel.maxresp_sz;
3958 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3960 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3961 goto out_put_session;
3962 svc_reserve(rqstp, buflen);
3965 /* Success! bump slot seqid */
3966 slot->sl_seqid = seq->seqid;
3967 slot->sl_flags |= NFSD4_SLOT_INUSE;
3969 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3971 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3973 cstate->slot = slot;
3974 cstate->session = session;
3978 switch (clp->cl_cb_state) {
3980 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3982 case NFSD4_CB_FAULT:
3983 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3986 seq->status_flags = 0;
3988 if (!list_empty(&clp->cl_revoked))
3989 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3993 spin_unlock(&nn->client_lock);
3996 nfsd4_put_session_locked(session);
3997 goto out_no_session;
4001 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4003 struct nfsd4_compound_state *cs = &resp->cstate;
4005 if (nfsd4_has_session(cs)) {
4006 if (cs->status != nfserr_replay_cache) {
4007 nfsd4_store_cache_entry(resp);
4008 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4010 /* Drop session reference that was taken in nfsd4_sequence() */
4011 nfsd4_put_session(cs->session);
4013 put_client_renew(cs->clp);
4017 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4018 struct nfsd4_compound_state *cstate,
4019 union nfsd4_op_u *u)
4021 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4022 struct nfs4_client *conf, *unconf;
4023 struct nfs4_client *clp = NULL;
4025 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4027 spin_lock(&nn->client_lock);
4028 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4029 conf = find_confirmed_client(&dc->clientid, true, nn);
4030 WARN_ON_ONCE(conf && unconf);
4033 if (client_has_state(conf)) {
4034 status = nfserr_clientid_busy;
4037 status = mark_client_expired_locked(conf);
4044 status = nfserr_stale_clientid;
4047 if (!nfsd4_mach_creds_match(clp, rqstp)) {
4049 status = nfserr_wrong_cred;
4052 trace_nfsd_clid_destroyed(&clp->cl_clientid);
4053 unhash_client_locked(clp);
4055 spin_unlock(&nn->client_lock);
4062 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4063 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4065 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4066 struct nfs4_client *clp = cstate->clp;
4069 if (rc->rca_one_fs) {
4070 if (!cstate->current_fh.fh_dentry)
4071 return nfserr_nofilehandle;
4073 * We don't take advantage of the rca_one_fs case.
4074 * That's OK, it's optional, we can safely ignore it.
4079 status = nfserr_complete_already;
4080 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4083 status = nfserr_stale_clientid;
4084 if (is_client_expired(clp))
4086 * The following error isn't really legal.
4087 * But we only get here if the client just explicitly
4088 * destroyed the client. Surely it no longer cares what
4089 * error it gets back on an operation for the dead
4095 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4096 nfsd4_client_record_create(clp);
4097 inc_reclaim_complete(clp);
4103 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4104 union nfsd4_op_u *u)
4106 struct nfsd4_setclientid *setclid = &u->setclientid;
4107 struct xdr_netobj clname = setclid->se_name;
4108 nfs4_verifier clverifier = setclid->se_verf;
4109 struct nfs4_client *conf, *new;
4110 struct nfs4_client *unconf = NULL;
4112 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4114 new = create_client(clname, rqstp, &clverifier);
4116 return nfserr_jukebox;
4117 spin_lock(&nn->client_lock);
4118 conf = find_confirmed_client_by_name(&clname, nn);
4119 if (conf && client_has_state(conf)) {
4120 status = nfserr_clid_inuse;
4121 if (clp_used_exchangeid(conf))
4123 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4124 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4128 unconf = find_unconfirmed_client_by_name(&clname, nn);
4130 unhash_client_locked(unconf);
4132 if (same_verf(&conf->cl_verifier, &clverifier)) {
4133 copy_clid(new, conf);
4134 gen_confirm(new, nn);
4136 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4139 trace_nfsd_clid_fresh(new);
4140 new->cl_minorversion = 0;
4141 gen_callback(new, setclid, rqstp);
4142 add_to_unconfirmed(new);
4143 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4144 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4145 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4149 spin_unlock(&nn->client_lock);
4153 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4154 expire_client(unconf);
4160 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4161 struct nfsd4_compound_state *cstate,
4162 union nfsd4_op_u *u)
4164 struct nfsd4_setclientid_confirm *setclientid_confirm =
4165 &u->setclientid_confirm;
4166 struct nfs4_client *conf, *unconf;
4167 struct nfs4_client *old = NULL;
4168 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4169 clientid_t * clid = &setclientid_confirm->sc_clientid;
4171 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4173 if (STALE_CLIENTID(clid, nn))
4174 return nfserr_stale_clientid;
4176 spin_lock(&nn->client_lock);
4177 conf = find_confirmed_client(clid, false, nn);
4178 unconf = find_unconfirmed_client(clid, false, nn);
4180 * We try hard to give out unique clientid's, so if we get an
4181 * attempt to confirm the same clientid with a different cred,
4182 * the client may be buggy; this should never happen.
4184 * Nevertheless, RFC 7530 recommends INUSE for this case:
4186 status = nfserr_clid_inuse;
4187 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4188 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4191 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4192 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4195 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4196 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4199 status = nfserr_stale_clientid;
4205 unhash_client_locked(old);
4206 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4208 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4210 status = nfserr_clid_inuse;
4211 if (client_has_state(old)
4212 && !same_creds(&unconf->cl_cred,
4217 status = mark_client_expired_locked(old);
4222 trace_nfsd_clid_replaced(&old->cl_clientid);
4224 move_to_confirmed(unconf);
4227 get_client_locked(conf);
4228 spin_unlock(&nn->client_lock);
4230 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4231 nfsd4_probe_callback(conf);
4232 spin_lock(&nn->client_lock);
4233 put_client_renew_locked(conf);
4235 spin_unlock(&nn->client_lock);
4241 static struct nfs4_file *nfsd4_alloc_file(void)
4243 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4246 /* OPEN Share state helper functions */
4247 static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval,
4248 struct nfs4_file *fp)
4250 lockdep_assert_held(&state_lock);
4252 refcount_set(&fp->fi_ref, 1);
4253 spin_lock_init(&fp->fi_lock);
4254 INIT_LIST_HEAD(&fp->fi_stateids);
4255 INIT_LIST_HEAD(&fp->fi_delegations);
4256 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4257 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4258 fp->fi_deleg_file = NULL;
4259 fp->fi_had_conflict = false;
4260 fp->fi_share_deny = 0;
4261 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4262 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4263 fp->fi_aliased = false;
4264 fp->fi_inode = d_inode(fh->fh_dentry);
4265 #ifdef CONFIG_NFSD_PNFS
4266 INIT_LIST_HEAD(&fp->fi_lo_states);
4267 atomic_set(&fp->fi_lo_recalls, 0);
4269 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
4273 nfsd4_free_slabs(void)
4275 kmem_cache_destroy(client_slab);
4276 kmem_cache_destroy(openowner_slab);
4277 kmem_cache_destroy(lockowner_slab);
4278 kmem_cache_destroy(file_slab);
4279 kmem_cache_destroy(stateid_slab);
4280 kmem_cache_destroy(deleg_slab);
4281 kmem_cache_destroy(odstate_slab);
4285 nfsd4_init_slabs(void)
4287 client_slab = kmem_cache_create("nfsd4_clients",
4288 sizeof(struct nfs4_client), 0, 0, NULL);
4289 if (client_slab == NULL)
4291 openowner_slab = kmem_cache_create("nfsd4_openowners",
4292 sizeof(struct nfs4_openowner), 0, 0, NULL);
4293 if (openowner_slab == NULL)
4294 goto out_free_client_slab;
4295 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4296 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4297 if (lockowner_slab == NULL)
4298 goto out_free_openowner_slab;
4299 file_slab = kmem_cache_create("nfsd4_files",
4300 sizeof(struct nfs4_file), 0, 0, NULL);
4301 if (file_slab == NULL)
4302 goto out_free_lockowner_slab;
4303 stateid_slab = kmem_cache_create("nfsd4_stateids",
4304 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4305 if (stateid_slab == NULL)
4306 goto out_free_file_slab;
4307 deleg_slab = kmem_cache_create("nfsd4_delegations",
4308 sizeof(struct nfs4_delegation), 0, 0, NULL);
4309 if (deleg_slab == NULL)
4310 goto out_free_stateid_slab;
4311 odstate_slab = kmem_cache_create("nfsd4_odstate",
4312 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4313 if (odstate_slab == NULL)
4314 goto out_free_deleg_slab;
4317 out_free_deleg_slab:
4318 kmem_cache_destroy(deleg_slab);
4319 out_free_stateid_slab:
4320 kmem_cache_destroy(stateid_slab);
4322 kmem_cache_destroy(file_slab);
4323 out_free_lockowner_slab:
4324 kmem_cache_destroy(lockowner_slab);
4325 out_free_openowner_slab:
4326 kmem_cache_destroy(openowner_slab);
4327 out_free_client_slab:
4328 kmem_cache_destroy(client_slab);
4333 static void init_nfs4_replay(struct nfs4_replay *rp)
4335 rp->rp_status = nfserr_serverfault;
4337 rp->rp_buf = rp->rp_ibuf;
4338 mutex_init(&rp->rp_mutex);
4341 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4342 struct nfs4_stateowner *so)
4344 if (!nfsd4_has_session(cstate)) {
4345 mutex_lock(&so->so_replay.rp_mutex);
4346 cstate->replay_owner = nfs4_get_stateowner(so);
4350 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4352 struct nfs4_stateowner *so = cstate->replay_owner;
4355 cstate->replay_owner = NULL;
4356 mutex_unlock(&so->so_replay.rp_mutex);
4357 nfs4_put_stateowner(so);
4361 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4363 struct nfs4_stateowner *sop;
4365 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4369 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4370 if (!sop->so_owner.data) {
4371 kmem_cache_free(slab, sop);
4375 INIT_LIST_HEAD(&sop->so_stateids);
4376 sop->so_client = clp;
4377 init_nfs4_replay(&sop->so_replay);
4378 atomic_set(&sop->so_count, 1);
4382 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4384 lockdep_assert_held(&clp->cl_lock);
4386 list_add(&oo->oo_owner.so_strhash,
4387 &clp->cl_ownerstr_hashtbl[strhashval]);
4388 list_add(&oo->oo_perclient, &clp->cl_openowners);
4391 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4393 unhash_openowner_locked(openowner(so));
4396 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4398 struct nfs4_openowner *oo = openowner(so);
4400 kmem_cache_free(openowner_slab, oo);
4403 static const struct nfs4_stateowner_operations openowner_ops = {
4404 .so_unhash = nfs4_unhash_openowner,
4405 .so_free = nfs4_free_openowner,
4408 static struct nfs4_ol_stateid *
4409 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4411 struct nfs4_ol_stateid *local, *ret = NULL;
4412 struct nfs4_openowner *oo = open->op_openowner;
4414 lockdep_assert_held(&fp->fi_lock);
4416 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4417 /* ignore lock owners */
4418 if (local->st_stateowner->so_is_open_owner == 0)
4420 if (local->st_stateowner != &oo->oo_owner)
4422 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4424 refcount_inc(&ret->st_stid.sc_count);
4432 nfsd4_verify_open_stid(struct nfs4_stid *s)
4434 __be32 ret = nfs_ok;
4436 switch (s->sc_type) {
4440 case NFS4_CLOSED_STID:
4441 case NFS4_CLOSED_DELEG_STID:
4442 ret = nfserr_bad_stateid;
4444 case NFS4_REVOKED_DELEG_STID:
4445 ret = nfserr_deleg_revoked;
4450 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4452 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4456 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4457 ret = nfsd4_verify_open_stid(&stp->st_stid);
4459 mutex_unlock(&stp->st_mutex);
4463 static struct nfs4_ol_stateid *
4464 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4466 struct nfs4_ol_stateid *stp;
4468 spin_lock(&fp->fi_lock);
4469 stp = nfsd4_find_existing_open(fp, open);
4470 spin_unlock(&fp->fi_lock);
4471 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4473 nfs4_put_stid(&stp->st_stid);
4478 static struct nfs4_openowner *
4479 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4480 struct nfsd4_compound_state *cstate)
4482 struct nfs4_client *clp = cstate->clp;
4483 struct nfs4_openowner *oo, *ret;
4485 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4488 oo->oo_owner.so_ops = &openowner_ops;
4489 oo->oo_owner.so_is_open_owner = 1;
4490 oo->oo_owner.so_seqid = open->op_seqid;
4492 if (nfsd4_has_session(cstate))
4493 oo->oo_flags |= NFS4_OO_CONFIRMED;
4495 oo->oo_last_closed_stid = NULL;
4496 INIT_LIST_HEAD(&oo->oo_close_lru);
4497 spin_lock(&clp->cl_lock);
4498 ret = find_openstateowner_str_locked(strhashval, open, clp);
4500 hash_openowner(oo, clp, strhashval);
4503 nfs4_free_stateowner(&oo->oo_owner);
4505 spin_unlock(&clp->cl_lock);
4509 static struct nfs4_ol_stateid *
4510 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4513 struct nfs4_openowner *oo = open->op_openowner;
4514 struct nfs4_ol_stateid *retstp = NULL;
4515 struct nfs4_ol_stateid *stp;
4518 /* We are moving these outside of the spinlocks to avoid the warnings */
4519 mutex_init(&stp->st_mutex);
4520 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4523 spin_lock(&oo->oo_owner.so_client->cl_lock);
4524 spin_lock(&fp->fi_lock);
4526 retstp = nfsd4_find_existing_open(fp, open);
4530 open->op_stp = NULL;
4531 refcount_inc(&stp->st_stid.sc_count);
4532 stp->st_stid.sc_type = NFS4_OPEN_STID;
4533 INIT_LIST_HEAD(&stp->st_locks);
4534 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4536 stp->st_stid.sc_file = fp;
4537 stp->st_access_bmap = 0;
4538 stp->st_deny_bmap = 0;
4539 stp->st_openstp = NULL;
4540 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4541 list_add(&stp->st_perfile, &fp->fi_stateids);
4544 spin_unlock(&fp->fi_lock);
4545 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4547 /* Handle races with CLOSE */
4548 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4549 nfs4_put_stid(&retstp->st_stid);
4552 /* To keep mutex tracking happy */
4553 mutex_unlock(&stp->st_mutex);
4560 * In the 4.0 case we need to keep the owners around a little while to handle
4561 * CLOSE replay. We still do need to release any file access that is held by
4562 * them before returning however.
4565 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4567 struct nfs4_ol_stateid *last;
4568 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4569 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4572 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4575 * We know that we hold one reference via nfsd4_close, and another
4576 * "persistent" reference for the client. If the refcount is higher
4577 * than 2, then there are still calls in progress that are using this
4578 * stateid. We can't put the sc_file reference until they are finished.
4579 * Wait for the refcount to drop to 2. Since it has been unhashed,
4580 * there should be no danger of the refcount going back up again at
4583 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4585 release_all_access(s);
4586 if (s->st_stid.sc_file) {
4587 put_nfs4_file(s->st_stid.sc_file);
4588 s->st_stid.sc_file = NULL;
4591 spin_lock(&nn->client_lock);
4592 last = oo->oo_last_closed_stid;
4593 oo->oo_last_closed_stid = s;
4594 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4595 oo->oo_time = ktime_get_boottime_seconds();
4596 spin_unlock(&nn->client_lock);
4598 nfs4_put_stid(&last->st_stid);
4601 /* search file_hashtbl[] for file */
4602 static struct nfs4_file *
4603 find_file_locked(struct svc_fh *fh, unsigned int hashval)
4605 struct nfs4_file *fp;
4607 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4608 lockdep_is_held(&state_lock)) {
4609 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4610 if (refcount_inc_not_zero(&fp->fi_ref))
4617 static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
4618 unsigned int hashval)
4620 struct nfs4_file *fp;
4621 struct nfs4_file *ret = NULL;
4622 bool alias_found = false;
4624 spin_lock(&state_lock);
4625 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4626 lockdep_is_held(&state_lock)) {
4627 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4628 if (refcount_inc_not_zero(&fp->fi_ref))
4630 } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
4631 fp->fi_aliased = alias_found = true;
4633 if (likely(ret == NULL)) {
4634 nfsd4_init_file(fh, hashval, new);
4635 new->fi_aliased = alias_found;
4638 spin_unlock(&state_lock);
4642 static struct nfs4_file * find_file(struct svc_fh *fh)
4644 struct nfs4_file *fp;
4645 unsigned int hashval = file_hashval(fh);
4648 fp = find_file_locked(fh, hashval);
4653 static struct nfs4_file *
4654 find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
4656 struct nfs4_file *fp;
4657 unsigned int hashval = file_hashval(fh);
4660 fp = find_file_locked(fh, hashval);
4665 return insert_file(new, fh, hashval);
4669 * Called to check deny when READ with all zero stateid or
4670 * WRITE with all zero or all one stateid
4673 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4675 struct nfs4_file *fp;
4676 __be32 ret = nfs_ok;
4678 fp = find_file(current_fh);
4681 /* Check for conflicting share reservations */
4682 spin_lock(&fp->fi_lock);
4683 if (fp->fi_share_deny & deny_type)
4684 ret = nfserr_locked;
4685 spin_unlock(&fp->fi_lock);
4690 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4692 struct nfs4_delegation *dp = cb_to_delegation(cb);
4693 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4696 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4699 * We can't do this in nfsd_break_deleg_cb because it is
4700 * already holding inode->i_lock.
4702 * If the dl_time != 0, then we know that it has already been
4703 * queued for a lease break. Don't queue it again.
4705 spin_lock(&state_lock);
4706 if (delegation_hashed(dp) && dp->dl_time == 0) {
4707 dp->dl_time = ktime_get_boottime_seconds();
4708 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4710 spin_unlock(&state_lock);
4713 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4714 struct rpc_task *task)
4716 struct nfs4_delegation *dp = cb_to_delegation(cb);
4718 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4719 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4722 switch (task->tk_status) {
4725 case -NFS4ERR_DELAY:
4726 rpc_delay(task, 2 * HZ);
4729 case -NFS4ERR_BAD_STATEID:
4731 * Race: client probably got cb_recall before open reply
4732 * granting delegation.
4734 if (dp->dl_retries--) {
4735 rpc_delay(task, 2 * HZ);
4744 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4746 struct nfs4_delegation *dp = cb_to_delegation(cb);
4748 nfs4_put_stid(&dp->dl_stid);
4751 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4752 .prepare = nfsd4_cb_recall_prepare,
4753 .done = nfsd4_cb_recall_done,
4754 .release = nfsd4_cb_recall_release,
4757 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4760 * We're assuming the state code never drops its reference
4761 * without first removing the lease. Since we're in this lease
4762 * callback (and since the lease code is serialized by the
4763 * i_lock) we know the server hasn't removed the lease yet, and
4764 * we know it's safe to take a reference.
4766 refcount_inc(&dp->dl_stid.sc_count);
4767 nfsd4_run_cb(&dp->dl_recall);
4770 /* Called from break_lease() with i_lock held. */
4772 nfsd_break_deleg_cb(struct file_lock *fl)
4775 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4776 struct nfs4_file *fp = dp->dl_stid.sc_file;
4777 struct nfs4_client *clp = dp->dl_stid.sc_client;
4778 struct nfsd_net *nn;
4780 trace_nfsd_cb_recall(&dp->dl_stid);
4782 dp->dl_recalled = true;
4783 atomic_inc(&clp->cl_delegs_in_recall);
4784 if (try_to_expire_client(clp)) {
4785 nn = net_generic(clp->net, nfsd_net_id);
4786 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
4790 * We don't want the locks code to timeout the lease for us;
4791 * we'll remove it ourself if a delegation isn't returned
4794 fl->fl_break_time = 0;
4796 spin_lock(&fp->fi_lock);
4797 fp->fi_had_conflict = true;
4798 nfsd_break_one_deleg(dp);
4799 spin_unlock(&fp->fi_lock);
4804 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
4805 * @fl: Lock state to check
4808 * %true: Lease conflict was resolved
4809 * %false: Lease conflict was not resolved.
4811 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4813 struct nfs4_delegation *dl = fl->fl_owner;
4814 struct svc_rqst *rqst;
4815 struct nfs4_client *clp;
4819 rqst = kthread_data(current);
4820 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4821 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
4823 clp = *(rqst->rq_lease_breaker);
4824 return dl->dl_stid.sc_client == clp;
4828 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4829 struct list_head *dispose)
4831 struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
4832 struct nfs4_client *clp = dp->dl_stid.sc_client;
4834 if (arg & F_UNLCK) {
4835 if (dp->dl_recalled)
4836 atomic_dec(&clp->cl_delegs_in_recall);
4837 return lease_modify(onlist, arg, dispose);
4842 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4843 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
4844 .lm_break = nfsd_break_deleg_cb,
4845 .lm_change = nfsd_change_deleg_cb,
4848 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4850 if (nfsd4_has_session(cstate))
4852 if (seqid == so->so_seqid - 1)
4853 return nfserr_replay_me;
4854 if (seqid == so->so_seqid)
4856 return nfserr_bad_seqid;
4859 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4860 struct nfsd_net *nn)
4862 struct nfs4_client *found;
4864 spin_lock(&nn->client_lock);
4865 found = find_confirmed_client(clid, sessions, nn);
4867 atomic_inc(&found->cl_rpc_users);
4868 spin_unlock(&nn->client_lock);
4872 static __be32 set_client(clientid_t *clid,
4873 struct nfsd4_compound_state *cstate,
4874 struct nfsd_net *nn)
4877 if (!same_clid(&cstate->clp->cl_clientid, clid))
4878 return nfserr_stale_clientid;
4881 if (STALE_CLIENTID(clid, nn))
4882 return nfserr_stale_clientid;
4884 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4885 * set cstate->clp), so session = false:
4887 cstate->clp = lookup_clientid(clid, false, nn);
4889 return nfserr_expired;
4894 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4895 struct nfsd4_open *open, struct nfsd_net *nn)
4897 clientid_t *clientid = &open->op_clientid;
4898 struct nfs4_client *clp = NULL;
4899 unsigned int strhashval;
4900 struct nfs4_openowner *oo = NULL;
4904 * In case we need it later, after we've already created the
4905 * file and don't want to risk a further failure:
4907 open->op_file = nfsd4_alloc_file();
4908 if (open->op_file == NULL)
4909 return nfserr_jukebox;
4911 status = set_client(clientid, cstate, nn);
4916 strhashval = ownerstr_hashval(&open->op_owner);
4917 oo = find_openstateowner_str(strhashval, open, clp);
4918 open->op_openowner = oo;
4922 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4923 /* Replace unconfirmed owners without checking for replay. */
4924 release_openowner(oo);
4925 open->op_openowner = NULL;
4928 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4933 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4935 return nfserr_jukebox;
4936 open->op_openowner = oo;
4938 open->op_stp = nfs4_alloc_open_stateid(clp);
4940 return nfserr_jukebox;
4942 if (nfsd4_has_session(cstate) &&
4943 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4944 open->op_odstate = alloc_clnt_odstate(clp);
4945 if (!open->op_odstate)
4946 return nfserr_jukebox;
4952 static inline __be32
4953 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4955 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4956 return nfserr_openmode;
4961 static int share_access_to_flags(u32 share_access)
4963 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4966 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4968 struct nfs4_stid *ret;
4970 ret = find_stateid_by_type(cl, s,
4971 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4974 return delegstateid(ret);
4977 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4979 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4980 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4984 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4985 struct nfs4_delegation **dp)
4988 __be32 status = nfserr_bad_stateid;
4989 struct nfs4_delegation *deleg;
4991 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4994 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4995 nfs4_put_stid(&deleg->dl_stid);
4996 if (cl->cl_minorversion)
4997 status = nfserr_deleg_revoked;
5000 flags = share_access_to_flags(open->op_share_access);
5001 status = nfs4_check_delegmode(deleg, flags);
5003 nfs4_put_stid(&deleg->dl_stid);
5008 if (!nfsd4_is_deleg_cur(open))
5012 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5016 static inline int nfs4_access_to_access(u32 nfs4_access)
5020 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5021 flags |= NFSD_MAY_READ;
5022 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5023 flags |= NFSD_MAY_WRITE;
5027 static inline __be32
5028 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5029 struct nfsd4_open *open)
5031 struct iattr iattr = {
5032 .ia_valid = ATTR_SIZE,
5035 if (!open->op_truncate)
5037 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5038 return nfserr_inval;
5039 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
5042 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5043 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5044 struct nfsd4_open *open, bool new_stp)
5046 struct nfsd_file *nf = NULL;
5048 int oflag = nfs4_access_to_omode(open->op_share_access);
5049 int access = nfs4_access_to_access(open->op_share_access);
5050 unsigned char old_access_bmap, old_deny_bmap;
5052 spin_lock(&fp->fi_lock);
5055 * Are we trying to set a deny mode that would conflict with
5058 status = nfs4_file_check_deny(fp, open->op_share_deny);
5059 if (status != nfs_ok) {
5060 if (status != nfserr_share_denied) {
5061 spin_unlock(&fp->fi_lock);
5064 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5065 stp, open->op_share_deny, false))
5066 status = nfserr_jukebox;
5067 spin_unlock(&fp->fi_lock);
5071 /* set access to the file */
5072 status = nfs4_file_get_access(fp, open->op_share_access);
5073 if (status != nfs_ok) {
5074 if (status != nfserr_share_denied) {
5075 spin_unlock(&fp->fi_lock);
5078 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5079 stp, open->op_share_access, true))
5080 status = nfserr_jukebox;
5081 spin_unlock(&fp->fi_lock);
5085 /* Set access bits in stateid */
5086 old_access_bmap = stp->st_access_bmap;
5087 set_access(open->op_share_access, stp);
5089 /* Set new deny mask */
5090 old_deny_bmap = stp->st_deny_bmap;
5091 set_deny(open->op_share_deny, stp);
5092 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5094 if (!fp->fi_fds[oflag]) {
5095 spin_unlock(&fp->fi_lock);
5097 if (!open->op_filp) {
5098 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
5099 if (status != nfs_ok)
5100 goto out_put_access;
5102 status = nfsd_file_create(rqstp, cur_fh, access, &nf);
5103 if (status != nfs_ok)
5104 goto out_put_access;
5105 nf->nf_file = open->op_filp;
5106 open->op_filp = NULL;
5109 spin_lock(&fp->fi_lock);
5110 if (!fp->fi_fds[oflag]) {
5111 fp->fi_fds[oflag] = nf;
5115 spin_unlock(&fp->fi_lock);
5119 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5122 goto out_put_access;
5124 status = nfsd4_truncate(rqstp, cur_fh, open);
5126 goto out_put_access;
5130 stp->st_access_bmap = old_access_bmap;
5131 nfs4_file_put_access(fp, open->op_share_access);
5132 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5137 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5138 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5139 struct nfsd4_open *open)
5142 unsigned char old_deny_bmap = stp->st_deny_bmap;
5144 if (!test_access(open->op_share_access, stp))
5145 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5147 /* test and set deny mode */
5148 spin_lock(&fp->fi_lock);
5149 status = nfs4_file_check_deny(fp, open->op_share_deny);
5150 if (status == nfs_ok) {
5151 if (status != nfserr_share_denied) {
5152 set_deny(open->op_share_deny, stp);
5153 fp->fi_share_deny |=
5154 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5156 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5157 stp, open->op_share_deny, false))
5158 status = nfserr_jukebox;
5161 spin_unlock(&fp->fi_lock);
5163 if (status != nfs_ok)
5166 status = nfsd4_truncate(rqstp, cur_fh, open);
5167 if (status != nfs_ok)
5168 reset_union_bmap_deny(old_deny_bmap, stp);
5172 /* Should we give out recallable state?: */
5173 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5175 if (clp->cl_cb_state == NFSD4_CB_UP)
5178 * In the sessions case, since we don't have to establish a
5179 * separate connection for callbacks, we assume it's OK
5180 * until we hear otherwise:
5182 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5185 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5188 struct file_lock *fl;
5190 fl = locks_alloc_lock();
5193 fl->fl_lmops = &nfsd_lease_mng_ops;
5194 fl->fl_flags = FL_DELEG;
5195 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5196 fl->fl_end = OFFSET_MAX;
5197 fl->fl_owner = (fl_owner_t)dp;
5198 fl->fl_pid = current->tgid;
5199 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5203 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5204 struct nfs4_file *fp)
5206 struct nfs4_ol_stateid *st;
5207 struct file *f = fp->fi_deleg_file->nf_file;
5208 struct inode *ino = locks_inode(f);
5211 writes = atomic_read(&ino->i_writecount);
5215 * There could be multiple filehandles (hence multiple
5216 * nfs4_files) referencing this file, but that's not too
5217 * common; let's just give up in that case rather than
5218 * trying to go look up all the clients using that other
5219 * nfs4_file as well:
5224 * If there's a close in progress, make sure that we see it
5225 * clear any fi_fds[] entries before we see it decrement
5228 smp_mb__after_atomic();
5230 if (fp->fi_fds[O_WRONLY])
5232 if (fp->fi_fds[O_RDWR])
5235 return -EAGAIN; /* There may be non-NFSv4 writers */
5237 * It's possible there are non-NFSv4 write opens in progress,
5238 * but if they haven't incremented i_writecount yet then they
5239 * also haven't called break lease yet; so, they'll break this
5240 * lease soon enough. So, all that's left to check for is NFSv4
5243 spin_lock(&fp->fi_lock);
5244 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5245 if (st->st_openstp == NULL /* it's an open */ &&
5246 access_permit_write(st) &&
5247 st->st_stid.sc_client != clp) {
5248 spin_unlock(&fp->fi_lock);
5252 spin_unlock(&fp->fi_lock);
5254 * There's a small chance that we could be racing with another
5255 * NFSv4 open. However, any open that hasn't added itself to
5256 * the fi_stateids list also hasn't called break_lease yet; so,
5257 * they'll break this lease soon enough.
5262 static struct nfs4_delegation *
5263 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
5264 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
5267 struct nfs4_delegation *dp;
5268 struct nfsd_file *nf;
5269 struct file_lock *fl;
5272 * The fi_had_conflict and nfs_get_existing_delegation checks
5273 * here are just optimizations; we'll need to recheck them at
5276 if (fp->fi_had_conflict)
5277 return ERR_PTR(-EAGAIN);
5279 nf = find_readable_file(fp);
5282 * We probably could attempt another open and get a read
5283 * delegation, but for now, don't bother until the
5284 * client actually sends us one.
5286 return ERR_PTR(-EAGAIN);
5288 spin_lock(&state_lock);
5289 spin_lock(&fp->fi_lock);
5290 if (nfs4_delegation_exists(clp, fp))
5292 else if (!fp->fi_deleg_file) {
5293 fp->fi_deleg_file = nf;
5294 /* increment early to prevent fi_deleg_file from being
5296 fp->fi_delegees = 1;
5300 spin_unlock(&fp->fi_lock);
5301 spin_unlock(&state_lock);
5305 return ERR_PTR(status);
5308 dp = alloc_init_deleg(clp, fp, fh, odstate);
5312 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5314 goto out_clnt_odstate;
5316 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5318 locks_free_lock(fl);
5320 goto out_clnt_odstate;
5321 status = nfsd4_check_conflicting_opens(clp, fp);
5325 spin_lock(&state_lock);
5326 spin_lock(&fp->fi_lock);
5327 if (fp->fi_had_conflict)
5330 status = hash_delegation_locked(dp, fp);
5331 spin_unlock(&fp->fi_lock);
5332 spin_unlock(&state_lock);
5339 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5341 put_clnt_odstate(dp->dl_clnt_odstate);
5342 nfs4_put_stid(&dp->dl_stid);
5345 return ERR_PTR(status);
5348 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5350 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5351 if (status == -EAGAIN)
5352 open->op_why_no_deleg = WND4_CONTENTION;
5354 open->op_why_no_deleg = WND4_RESOURCE;
5355 switch (open->op_deleg_want) {
5356 case NFS4_SHARE_WANT_READ_DELEG:
5357 case NFS4_SHARE_WANT_WRITE_DELEG:
5358 case NFS4_SHARE_WANT_ANY_DELEG:
5360 case NFS4_SHARE_WANT_CANCEL:
5361 open->op_why_no_deleg = WND4_CANCELLED;
5363 case NFS4_SHARE_WANT_NO_DELEG:
5370 * Attempt to hand out a delegation.
5372 * Note we don't support write delegations, and won't until the vfs has
5373 * proper support for them.
5376 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5377 struct nfs4_ol_stateid *stp)
5379 struct nfs4_delegation *dp;
5380 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5381 struct nfs4_client *clp = stp->st_stid.sc_client;
5385 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5386 open->op_recall = 0;
5387 switch (open->op_claim_type) {
5388 case NFS4_OPEN_CLAIM_PREVIOUS:
5390 open->op_recall = 1;
5391 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5394 case NFS4_OPEN_CLAIM_NULL:
5395 case NFS4_OPEN_CLAIM_FH:
5397 * Let's not give out any delegations till everyone's
5398 * had the chance to reclaim theirs, *and* until
5399 * NLM locks have all been reclaimed:
5401 if (locks_in_grace(clp->net))
5403 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5409 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
5413 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5415 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5416 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5417 nfs4_put_stid(&dp->dl_stid);
5420 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5421 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5422 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5423 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5424 open->op_recall = 1;
5427 /* 4.1 client asking for a delegation? */
5428 if (open->op_deleg_want)
5429 nfsd4_open_deleg_none_ext(open, status);
5433 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5434 struct nfs4_delegation *dp)
5436 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5437 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5438 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5439 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5440 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5441 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5442 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5443 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5445 /* Otherwise the client must be confused wanting a delegation
5446 * it already has, therefore we don't return
5447 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5452 * nfsd4_process_open2 - finish open processing
5453 * @rqstp: the RPC transaction being executed
5454 * @current_fh: NFSv4 COMPOUND's current filehandle
5455 * @open: OPEN arguments
5457 * If successful, (1) truncate the file if open->op_truncate was
5458 * set, (2) set open->op_stateid, (3) set open->op_delegation.
5460 * Returns %nfs_ok on success; otherwise an nfs4stat value in
5461 * network byte order is returned.
5464 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5466 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5467 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5468 struct nfs4_file *fp = NULL;
5469 struct nfs4_ol_stateid *stp = NULL;
5470 struct nfs4_delegation *dp = NULL;
5472 bool new_stp = false;
5475 * Lookup file; if found, lookup stateid and check open request,
5476 * and check for delegations in the process of being recalled.
5477 * If not found, create the nfs4_file struct
5479 fp = find_or_add_file(open->op_file, current_fh);
5480 if (fp != open->op_file) {
5481 status = nfs4_check_deleg(cl, open, &dp);
5484 stp = nfsd4_find_and_lock_existing_open(fp, open);
5486 open->op_file = NULL;
5487 status = nfserr_bad_stateid;
5488 if (nfsd4_is_deleg_cur(open))
5493 stp = init_open_stateid(fp, open);
5499 * OPEN the file, or upgrade an existing OPEN.
5500 * If truncate fails, the OPEN fails.
5502 * stp is already locked.
5505 /* Stateid was found, this is an OPEN upgrade */
5506 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5508 mutex_unlock(&stp->st_mutex);
5512 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
5514 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5515 release_open_stateid(stp);
5516 mutex_unlock(&stp->st_mutex);
5520 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5522 if (stp->st_clnt_odstate == open->op_odstate)
5523 open->op_odstate = NULL;
5526 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5527 mutex_unlock(&stp->st_mutex);
5529 if (nfsd4_has_session(&resp->cstate)) {
5530 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5531 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5532 open->op_why_no_deleg = WND4_NOT_WANTED;
5538 * Attempt to hand out a delegation. No error return, because the
5539 * OPEN succeeds even if we fail.
5541 nfs4_open_delegation(current_fh, open, stp);
5544 trace_nfsd_open(&stp->st_stid.sc_stateid);
5546 /* 4.1 client trying to upgrade/downgrade delegation? */
5547 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5548 open->op_deleg_want)
5549 nfsd4_deleg_xgrade_none_ext(open, dp);
5553 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5554 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5556 * To finish the open response, we just need to set the rflags.
5558 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5559 if (nfsd4_has_session(&resp->cstate))
5560 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5561 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5562 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5565 nfs4_put_stid(&dp->dl_stid);
5567 nfs4_put_stid(&stp->st_stid);
5572 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5573 struct nfsd4_open *open)
5575 if (open->op_openowner) {
5576 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5578 nfsd4_cstate_assign_replay(cstate, so);
5579 nfs4_put_stateowner(so);
5582 kmem_cache_free(file_slab, open->op_file);
5584 nfs4_put_stid(&open->op_stp->st_stid);
5585 if (open->op_odstate)
5586 kmem_cache_free(odstate_slab, open->op_odstate);
5590 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5591 union nfsd4_op_u *u)
5593 clientid_t *clid = &u->renew;
5594 struct nfs4_client *clp;
5596 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5598 trace_nfsd_clid_renew(clid);
5599 status = set_client(clid, cstate, nn);
5603 if (!list_empty(&clp->cl_delegations)
5604 && clp->cl_cb_state != NFSD4_CB_UP)
5605 return nfserr_cb_path_down;
5610 nfsd4_end_grace(struct nfsd_net *nn)
5612 /* do nothing if grace period already ended */
5613 if (nn->grace_ended)
5616 trace_nfsd_grace_complete(nn);
5617 nn->grace_ended = true;
5619 * If the server goes down again right now, an NFSv4
5620 * client will still be allowed to reclaim after it comes back up,
5621 * even if it hasn't yet had a chance to reclaim state this time.
5624 nfsd4_record_grace_done(nn);
5626 * At this point, NFSv4 clients can still reclaim. But if the
5627 * server crashes, any that have not yet reclaimed will be out
5628 * of luck on the next boot.
5630 * (NFSv4.1+ clients are considered to have reclaimed once they
5631 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5632 * have reclaimed after their first OPEN.)
5634 locks_end_grace(&nn->nfsd4_manager);
5636 * At this point, and once lockd and/or any other containers
5637 * exit their grace period, further reclaims will fail and
5638 * regular locking can resume.
5643 * If we've waited a lease period but there are still clients trying to
5644 * reclaim, wait a little longer to give them a chance to finish.
5646 static bool clients_still_reclaiming(struct nfsd_net *nn)
5648 time64_t double_grace_period_end = nn->boot_time +
5649 2 * nn->nfsd4_lease;
5651 if (nn->track_reclaim_completes &&
5652 atomic_read(&nn->nr_reclaim_complete) ==
5653 nn->reclaim_str_hashtbl_size)
5655 if (!nn->somebody_reclaimed)
5657 nn->somebody_reclaimed = false;
5659 * If we've given them *two* lease times to reclaim, and they're
5660 * still not done, give up:
5662 if (ktime_get_boottime_seconds() > double_grace_period_end)
5667 struct laundry_time {
5672 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5674 time64_t time_remaining;
5676 if (last_refresh < lt->cutoff)
5678 time_remaining = last_refresh - lt->cutoff;
5679 lt->new_timeo = min(lt->new_timeo, time_remaining);
5683 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
5684 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5686 spin_lock_init(&nn->nfsd_ssc_lock);
5687 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5688 init_waitqueue_head(&nn->nfsd_ssc_waitq);
5690 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5693 * This is called when nfsd is being shutdown, after all inter_ssc
5694 * cleanup were done, to destroy the ssc delayed unmount list.
5696 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5698 struct nfsd4_ssc_umount_item *ni = NULL;
5699 struct nfsd4_ssc_umount_item *tmp;
5701 spin_lock(&nn->nfsd_ssc_lock);
5702 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5703 list_del(&ni->nsui_list);
5704 spin_unlock(&nn->nfsd_ssc_lock);
5705 mntput(ni->nsui_vfsmount);
5707 spin_lock(&nn->nfsd_ssc_lock);
5709 spin_unlock(&nn->nfsd_ssc_lock);
5712 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5714 bool do_wakeup = false;
5715 struct nfsd4_ssc_umount_item *ni = NULL;
5716 struct nfsd4_ssc_umount_item *tmp;
5718 spin_lock(&nn->nfsd_ssc_lock);
5719 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5720 if (time_after(jiffies, ni->nsui_expire)) {
5721 if (refcount_read(&ni->nsui_refcnt) > 1)
5724 /* mark being unmount */
5725 ni->nsui_busy = true;
5726 spin_unlock(&nn->nfsd_ssc_lock);
5727 mntput(ni->nsui_vfsmount);
5728 spin_lock(&nn->nfsd_ssc_lock);
5730 /* waiters need to start from begin of list */
5731 list_del(&ni->nsui_list);
5734 /* wakeup ssc_connect waiters */
5741 wake_up_all(&nn->nfsd_ssc_waitq);
5742 spin_unlock(&nn->nfsd_ssc_lock);
5746 /* Check if any lock belonging to this lockowner has any blockers */
5748 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
5750 struct file_lock_context *ctx;
5751 struct nfs4_ol_stateid *stp;
5752 struct nfs4_file *nf;
5754 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
5755 nf = stp->st_stid.sc_file;
5756 ctx = nf->fi_inode->i_flctx;
5759 if (locks_owner_has_blockers(ctx, lo))
5766 nfs4_anylock_blockers(struct nfs4_client *clp)
5769 struct nfs4_stateowner *so;
5770 struct nfs4_lockowner *lo;
5772 if (atomic_read(&clp->cl_delegs_in_recall))
5774 spin_lock(&clp->cl_lock);
5775 for (i = 0; i < OWNER_HASH_SIZE; i++) {
5776 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
5778 if (so->so_is_open_owner)
5781 if (nfs4_lockowner_has_blockers(lo)) {
5782 spin_unlock(&clp->cl_lock);
5787 spin_unlock(&clp->cl_lock);
5792 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
5793 struct laundry_time *lt)
5795 struct list_head *pos, *next;
5796 struct nfs4_client *clp;
5798 INIT_LIST_HEAD(reaplist);
5799 spin_lock(&nn->client_lock);
5800 list_for_each_safe(pos, next, &nn->client_lru) {
5801 clp = list_entry(pos, struct nfs4_client, cl_lru);
5802 if (clp->cl_state == NFSD4_EXPIRABLE)
5804 if (!state_expired(lt, clp->cl_time))
5806 if (!atomic_read(&clp->cl_rpc_users))
5807 clp->cl_state = NFSD4_COURTESY;
5808 if (!client_has_state(clp) ||
5809 ktime_get_boottime_seconds() >=
5810 (clp->cl_time + NFSD_COURTESY_CLIENT_TIMEOUT))
5812 if (nfs4_anylock_blockers(clp)) {
5814 if (!mark_client_expired_locked(clp))
5815 list_add(&clp->cl_lru, reaplist);
5818 spin_unlock(&nn->client_lock);
5822 nfs4_laundromat(struct nfsd_net *nn)
5824 struct nfs4_client *clp;
5825 struct nfs4_openowner *oo;
5826 struct nfs4_delegation *dp;
5827 struct nfs4_ol_stateid *stp;
5828 struct nfsd4_blocked_lock *nbl;
5829 struct list_head *pos, *next, reaplist;
5830 struct laundry_time lt = {
5831 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
5832 .new_timeo = nn->nfsd4_lease
5834 struct nfs4_cpntf_state *cps;
5835 copy_stateid_t *cps_t;
5838 if (clients_still_reclaiming(nn)) {
5842 nfsd4_end_grace(nn);
5844 spin_lock(&nn->s2s_cp_lock);
5845 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5846 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5847 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
5848 state_expired(<, cps->cpntf_time))
5849 _free_cpntf_state_locked(nn, cps);
5851 spin_unlock(&nn->s2s_cp_lock);
5852 nfs4_get_client_reaplist(nn, &reaplist, <);
5853 list_for_each_safe(pos, next, &reaplist) {
5854 clp = list_entry(pos, struct nfs4_client, cl_lru);
5855 trace_nfsd_clid_purged(&clp->cl_clientid);
5856 list_del_init(&clp->cl_lru);
5859 spin_lock(&state_lock);
5860 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5861 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5862 if (!state_expired(<, dp->dl_time))
5864 WARN_ON(!unhash_delegation_locked(dp));
5865 list_add(&dp->dl_recall_lru, &reaplist);
5867 spin_unlock(&state_lock);
5868 while (!list_empty(&reaplist)) {
5869 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5871 list_del_init(&dp->dl_recall_lru);
5872 revoke_delegation(dp);
5875 spin_lock(&nn->client_lock);
5876 while (!list_empty(&nn->close_lru)) {
5877 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5879 if (!state_expired(<, oo->oo_time))
5881 list_del_init(&oo->oo_close_lru);
5882 stp = oo->oo_last_closed_stid;
5883 oo->oo_last_closed_stid = NULL;
5884 spin_unlock(&nn->client_lock);
5885 nfs4_put_stid(&stp->st_stid);
5886 spin_lock(&nn->client_lock);
5888 spin_unlock(&nn->client_lock);
5891 * It's possible for a client to try and acquire an already held lock
5892 * that is being held for a long time, and then lose interest in it.
5893 * So, we clean out any un-revisited request after a lease period
5894 * under the assumption that the client is no longer interested.
5896 * RFC5661, sec. 9.6 states that the client must not rely on getting
5897 * notifications and must continue to poll for locks, even when the
5898 * server supports them. Thus this shouldn't lead to clients blocking
5899 * indefinitely once the lock does become free.
5901 BUG_ON(!list_empty(&reaplist));
5902 spin_lock(&nn->blocked_locks_lock);
5903 while (!list_empty(&nn->blocked_locks_lru)) {
5904 nbl = list_first_entry(&nn->blocked_locks_lru,
5905 struct nfsd4_blocked_lock, nbl_lru);
5906 if (!state_expired(<, nbl->nbl_time))
5908 list_move(&nbl->nbl_lru, &reaplist);
5909 list_del_init(&nbl->nbl_list);
5911 spin_unlock(&nn->blocked_locks_lock);
5913 while (!list_empty(&reaplist)) {
5914 nbl = list_first_entry(&reaplist,
5915 struct nfsd4_blocked_lock, nbl_lru);
5916 list_del_init(&nbl->nbl_lru);
5917 free_blocked_lock(nbl);
5919 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
5920 /* service the server-to-server copy delayed unmount list */
5921 nfsd4_ssc_expire_umount(nn);
5924 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
5927 static void laundromat_main(struct work_struct *);
5930 laundromat_main(struct work_struct *laundry)
5933 struct delayed_work *dwork = to_delayed_work(laundry);
5934 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5937 t = nfs4_laundromat(nn);
5938 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
5941 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
5943 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
5944 return nfserr_bad_stateid;
5949 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
5951 __be32 status = nfserr_openmode;
5953 /* For lock stateid's, we test the parent open, not the lock: */
5954 if (stp->st_openstp)
5955 stp = stp->st_openstp;
5956 if ((flags & WR_STATE) && !access_permit_write(stp))
5958 if ((flags & RD_STATE) && !access_permit_read(stp))
5965 static inline __be32
5966 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
5968 if (ONE_STATEID(stateid) && (flags & RD_STATE))
5970 else if (opens_in_grace(net)) {
5971 /* Answer in remaining cases depends on existence of
5972 * conflicting state; so we must wait out the grace period. */
5973 return nfserr_grace;
5974 } else if (flags & WR_STATE)
5975 return nfs4_share_conflict(current_fh,
5976 NFS4_SHARE_DENY_WRITE);
5977 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5978 return nfs4_share_conflict(current_fh,
5979 NFS4_SHARE_DENY_READ);
5982 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
5985 * When sessions are used the stateid generation number is ignored
5988 if (has_session && in->si_generation == 0)
5991 if (in->si_generation == ref->si_generation)
5994 /* If the client sends us a stateid from the future, it's buggy: */
5995 if (nfsd4_stateid_generation_after(in, ref))
5996 return nfserr_bad_stateid;
5998 * However, we could see a stateid from the past, even from a
5999 * non-buggy client. For example, if the client sends a lock
6000 * while some IO is outstanding, the lock may bump si_generation
6001 * while the IO is still in flight. The client could avoid that
6002 * situation by waiting for responses on all the IO requests,
6003 * but better performance may result in retrying IO that
6004 * receives an old_stateid error if requests are rarely
6005 * reordered in flight:
6007 return nfserr_old_stateid;
6010 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
6014 spin_lock(&s->sc_lock);
6015 ret = nfsd4_verify_open_stid(s);
6017 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
6018 spin_unlock(&s->sc_lock);
6022 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6024 if (ols->st_stateowner->so_is_open_owner &&
6025 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6026 return nfserr_bad_stateid;
6030 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
6032 struct nfs4_stid *s;
6033 __be32 status = nfserr_bad_stateid;
6035 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6036 CLOSE_STATEID(stateid))
6038 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
6040 spin_lock(&cl->cl_lock);
6041 s = find_stateid_locked(cl, stateid);
6044 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
6047 switch (s->sc_type) {
6048 case NFS4_DELEG_STID:
6051 case NFS4_REVOKED_DELEG_STID:
6052 status = nfserr_deleg_revoked;
6054 case NFS4_OPEN_STID:
6055 case NFS4_LOCK_STID:
6056 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
6059 printk("unknown stateid type %x\n", s->sc_type);
6061 case NFS4_CLOSED_STID:
6062 case NFS4_CLOSED_DELEG_STID:
6063 status = nfserr_bad_stateid;
6066 spin_unlock(&cl->cl_lock);
6071 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6072 stateid_t *stateid, unsigned char typemask,
6073 struct nfs4_stid **s, struct nfsd_net *nn)
6076 bool return_revoked = false;
6079 * only return revoked delegations if explicitly asked.
6080 * otherwise we report revoked or bad_stateid status.
6082 if (typemask & NFS4_REVOKED_DELEG_STID)
6083 return_revoked = true;
6084 else if (typemask & NFS4_DELEG_STID)
6085 typemask |= NFS4_REVOKED_DELEG_STID;
6087 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6088 CLOSE_STATEID(stateid))
6089 return nfserr_bad_stateid;
6090 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
6091 if (status == nfserr_stale_clientid) {
6092 if (cstate->session)
6093 return nfserr_bad_stateid;
6094 return nfserr_stale_stateid;
6098 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
6100 return nfserr_bad_stateid;
6101 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
6103 if (cstate->minorversion)
6104 return nfserr_deleg_revoked;
6105 return nfserr_bad_stateid;
6110 static struct nfsd_file *
6111 nfs4_find_file(struct nfs4_stid *s, int flags)
6116 switch (s->sc_type) {
6117 case NFS4_DELEG_STID:
6118 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
6120 return nfsd_file_get(s->sc_file->fi_deleg_file);
6121 case NFS4_OPEN_STID:
6122 case NFS4_LOCK_STID:
6123 if (flags & RD_STATE)
6124 return find_readable_file(s->sc_file);
6126 return find_writeable_file(s->sc_file);
6133 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
6137 status = nfsd4_check_openowner_confirmed(ols);
6140 return nfs4_check_openmode(ols, flags);
6144 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
6145 struct nfsd_file **nfp, int flags)
6147 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6148 struct nfsd_file *nf;
6151 nf = nfs4_find_file(s, flags);
6153 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6154 acc | NFSD_MAY_OWNER_OVERRIDE);
6160 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
6169 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6171 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
6172 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
6174 list_del(&cps->cp_list);
6175 idr_remove(&nn->s2s_cp_stateids,
6176 cps->cp_stateid.stid.si_opaque.so_id);
6180 * A READ from an inter server to server COPY will have a
6181 * copy stateid. Look up the copy notify stateid from the
6182 * idr structure and take a reference on it.
6184 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6185 struct nfs4_client *clp,
6186 struct nfs4_cpntf_state **cps)
6188 copy_stateid_t *cps_t;
6189 struct nfs4_cpntf_state *state = NULL;
6191 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6192 return nfserr_bad_stateid;
6193 spin_lock(&nn->s2s_cp_lock);
6194 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6196 state = container_of(cps_t, struct nfs4_cpntf_state,
6198 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
6203 refcount_inc(&state->cp_stateid.sc_count);
6205 _free_cpntf_state_locked(nn, state);
6208 spin_unlock(&nn->s2s_cp_lock);
6210 return nfserr_bad_stateid;
6216 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6217 struct nfs4_stid **stid)
6220 struct nfs4_cpntf_state *cps = NULL;
6221 struct nfs4_client *found;
6223 status = manage_cpntf_state(nn, st, NULL, &cps);
6227 cps->cpntf_time = ktime_get_boottime_seconds();
6229 status = nfserr_expired;
6230 found = lookup_clientid(&cps->cp_p_clid, true, nn);
6234 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6235 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6239 status = nfserr_bad_stateid;
6241 put_client_renew(found);
6243 nfs4_put_cpntf_state(nn, cps);
6247 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6249 spin_lock(&nn->s2s_cp_lock);
6250 _free_cpntf_state_locked(nn, cps);
6251 spin_unlock(&nn->s2s_cp_lock);
6255 * Checks for stateid operations
6258 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
6259 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
6260 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6261 struct nfs4_stid **cstid)
6263 struct net *net = SVC_NET(rqstp);
6264 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6265 struct nfs4_stid *s = NULL;
6271 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6273 status = nfserr_bad_stateid;
6275 status = check_special_stateids(net, fhp, stateid,
6280 status = nfsd4_lookup_stateid(cstate, stateid,
6281 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
6283 if (status == nfserr_bad_stateid)
6284 status = find_cpntf_state(nn, stateid, &s);
6287 status = nfsd4_stid_check_stateid_generation(stateid, s,
6288 nfsd4_has_session(cstate));
6292 switch (s->sc_type) {
6293 case NFS4_DELEG_STID:
6294 status = nfs4_check_delegmode(delegstateid(s), flags);
6296 case NFS4_OPEN_STID:
6297 case NFS4_LOCK_STID:
6298 status = nfs4_check_olstateid(openlockstateid(s), flags);
6301 status = nfserr_bad_stateid;
6306 status = nfs4_check_fh(fhp, s);
6309 if (status == nfs_ok && nfp)
6310 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
6313 if (!status && cstid)
6322 * Test if the stateid is valid
6325 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6326 union nfsd4_op_u *u)
6328 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
6329 struct nfsd4_test_stateid_id *stateid;
6330 struct nfs4_client *cl = cstate->clp;
6332 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
6333 stateid->ts_id_status =
6334 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
6340 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6342 struct nfs4_ol_stateid *stp = openlockstateid(s);
6345 ret = nfsd4_lock_ol_stateid(stp);
6349 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6353 ret = nfserr_locks_held;
6354 if (check_for_locks(stp->st_stid.sc_file,
6355 lockowner(stp->st_stateowner)))
6358 release_lock_stateid(stp);
6362 mutex_unlock(&stp->st_mutex);
6369 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6370 union nfsd4_op_u *u)
6372 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
6373 stateid_t *stateid = &free_stateid->fr_stateid;
6374 struct nfs4_stid *s;
6375 struct nfs4_delegation *dp;
6376 struct nfs4_client *cl = cstate->clp;
6377 __be32 ret = nfserr_bad_stateid;
6379 spin_lock(&cl->cl_lock);
6380 s = find_stateid_locked(cl, stateid);
6383 spin_lock(&s->sc_lock);
6384 switch (s->sc_type) {
6385 case NFS4_DELEG_STID:
6386 ret = nfserr_locks_held;
6388 case NFS4_OPEN_STID:
6389 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6392 ret = nfserr_locks_held;
6394 case NFS4_LOCK_STID:
6395 spin_unlock(&s->sc_lock);
6396 refcount_inc(&s->sc_count);
6397 spin_unlock(&cl->cl_lock);
6398 ret = nfsd4_free_lock_stateid(stateid, s);
6400 case NFS4_REVOKED_DELEG_STID:
6401 spin_unlock(&s->sc_lock);
6402 dp = delegstateid(s);
6403 list_del_init(&dp->dl_recall_lru);
6404 spin_unlock(&cl->cl_lock);
6408 /* Default falls through and returns nfserr_bad_stateid */
6410 spin_unlock(&s->sc_lock);
6412 spin_unlock(&cl->cl_lock);
6420 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6421 RD_STATE : WR_STATE;
6424 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6426 struct svc_fh *current_fh = &cstate->current_fh;
6427 struct nfs4_stateowner *sop = stp->st_stateowner;
6430 status = nfsd4_check_seqid(cstate, sop, seqid);
6433 status = nfsd4_lock_ol_stateid(stp);
6434 if (status != nfs_ok)
6436 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6437 if (status == nfs_ok)
6438 status = nfs4_check_fh(current_fh, &stp->st_stid);
6439 if (status != nfs_ok)
6440 mutex_unlock(&stp->st_mutex);
6445 * Checks for sequence id mutating operations.
6448 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6449 stateid_t *stateid, char typemask,
6450 struct nfs4_ol_stateid **stpp,
6451 struct nfsd_net *nn)
6454 struct nfs4_stid *s;
6455 struct nfs4_ol_stateid *stp = NULL;
6457 trace_nfsd_preprocess(seqid, stateid);
6460 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6463 stp = openlockstateid(s);
6464 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6466 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6470 nfs4_put_stid(&stp->st_stid);
6474 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6475 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6478 struct nfs4_openowner *oo;
6479 struct nfs4_ol_stateid *stp;
6481 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6482 NFS4_OPEN_STID, &stp, nn);
6485 oo = openowner(stp->st_stateowner);
6486 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6487 mutex_unlock(&stp->st_mutex);
6488 nfs4_put_stid(&stp->st_stid);
6489 return nfserr_bad_stateid;
6496 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6497 union nfsd4_op_u *u)
6499 struct nfsd4_open_confirm *oc = &u->open_confirm;
6501 struct nfs4_openowner *oo;
6502 struct nfs4_ol_stateid *stp;
6503 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6505 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6506 cstate->current_fh.fh_dentry);
6508 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6512 status = nfs4_preprocess_seqid_op(cstate,
6513 oc->oc_seqid, &oc->oc_req_stateid,
6514 NFS4_OPEN_STID, &stp, nn);
6517 oo = openowner(stp->st_stateowner);
6518 status = nfserr_bad_stateid;
6519 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6520 mutex_unlock(&stp->st_mutex);
6523 oo->oo_flags |= NFS4_OO_CONFIRMED;
6524 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6525 mutex_unlock(&stp->st_mutex);
6526 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6527 nfsd4_client_record_create(oo->oo_owner.so_client);
6530 nfs4_put_stid(&stp->st_stid);
6532 nfsd4_bump_seqid(cstate, status);
6536 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6538 if (!test_access(access, stp))
6540 nfs4_file_put_access(stp->st_stid.sc_file, access);
6541 clear_access(access, stp);
6544 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6546 switch (to_access) {
6547 case NFS4_SHARE_ACCESS_READ:
6548 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6549 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6551 case NFS4_SHARE_ACCESS_WRITE:
6552 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6553 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6555 case NFS4_SHARE_ACCESS_BOTH:
6563 nfsd4_open_downgrade(struct svc_rqst *rqstp,
6564 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
6566 struct nfsd4_open_downgrade *od = &u->open_downgrade;
6568 struct nfs4_ol_stateid *stp;
6569 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6571 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6572 cstate->current_fh.fh_dentry);
6574 /* We don't yet support WANT bits: */
6575 if (od->od_deleg_want)
6576 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6579 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
6580 &od->od_stateid, &stp, nn);
6583 status = nfserr_inval;
6584 if (!test_access(od->od_share_access, stp)) {
6585 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6586 stp->st_access_bmap, od->od_share_access);
6589 if (!test_deny(od->od_share_deny, stp)) {
6590 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6591 stp->st_deny_bmap, od->od_share_deny);
6594 nfs4_stateid_downgrade(stp, od->od_share_access);
6595 reset_union_bmap_deny(od->od_share_deny, stp);
6596 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
6599 mutex_unlock(&stp->st_mutex);
6600 nfs4_put_stid(&stp->st_stid);
6602 nfsd4_bump_seqid(cstate, status);
6606 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6608 struct nfs4_client *clp = s->st_stid.sc_client;
6610 LIST_HEAD(reaplist);
6612 spin_lock(&clp->cl_lock);
6613 unhashed = unhash_open_stateid(s, &reaplist);
6615 if (clp->cl_minorversion) {
6617 put_ol_stateid_locked(s, &reaplist);
6618 spin_unlock(&clp->cl_lock);
6619 free_ol_stateid_reaplist(&reaplist);
6621 spin_unlock(&clp->cl_lock);
6622 free_ol_stateid_reaplist(&reaplist);
6624 move_to_close_lru(s, clp->net);
6629 * nfs4_unlock_state() called after encode
6632 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6633 union nfsd4_op_u *u)
6635 struct nfsd4_close *close = &u->close;
6637 struct nfs4_ol_stateid *stp;
6638 struct net *net = SVC_NET(rqstp);
6639 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6641 dprintk("NFSD: nfsd4_close on file %pd\n",
6642 cstate->current_fh.fh_dentry);
6644 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6646 NFS4_OPEN_STID|NFS4_CLOSED_STID,
6648 nfsd4_bump_seqid(cstate, status);
6652 stp->st_stid.sc_type = NFS4_CLOSED_STID;
6655 * Technically we don't _really_ have to increment or copy it, since
6656 * it should just be gone after this operation and we clobber the
6657 * copied value below, but we continue to do so here just to ensure
6658 * that racing ops see that there was a state change.
6660 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
6662 nfsd4_close_open_stateid(stp);
6663 mutex_unlock(&stp->st_mutex);
6665 /* v4.1+ suggests that we send a special stateid in here, since the
6666 * clients should just ignore this anyway. Since this is not useful
6667 * for v4.0 clients either, we set it to the special close_stateid
6670 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6672 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6674 /* put reference from nfs4_preprocess_seqid_op */
6675 nfs4_put_stid(&stp->st_stid);
6681 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6682 union nfsd4_op_u *u)
6684 struct nfsd4_delegreturn *dr = &u->delegreturn;
6685 struct nfs4_delegation *dp;
6686 stateid_t *stateid = &dr->dr_stateid;
6687 struct nfs4_stid *s;
6689 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6691 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6694 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6697 dp = delegstateid(s);
6698 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6702 destroy_delegation(dp);
6704 nfs4_put_stid(&dp->dl_stid);
6709 /* last octet in a range */
6711 last_byte_offset(u64 start, u64 len)
6717 return end > start ? end - 1: NFS4_MAX_UINT64;
6721 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6722 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6723 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6724 * locking, this prevents us from being completely protocol-compliant. The
6725 * real solution to this problem is to start using unsigned file offsets in
6726 * the VFS, but this is a very deep change!
6729 nfs4_transform_lock_offset(struct file_lock *lock)
6731 if (lock->fl_start < 0)
6732 lock->fl_start = OFFSET_MAX;
6733 if (lock->fl_end < 0)
6734 lock->fl_end = OFFSET_MAX;
6738 nfsd4_lm_get_owner(fl_owner_t owner)
6740 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6742 nfs4_get_stateowner(&lo->lo_owner);
6747 nfsd4_lm_put_owner(fl_owner_t owner)
6749 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6752 nfs4_put_stateowner(&lo->lo_owner);
6755 /* return pointer to struct nfs4_client if client is expirable */
6757 nfsd4_lm_lock_expirable(struct file_lock *cfl)
6759 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
6760 struct nfs4_client *clp = lo->lo_owner.so_client;
6761 struct nfsd_net *nn;
6763 if (try_to_expire_client(clp)) {
6764 nn = net_generic(clp->net, nfsd_net_id);
6765 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
6771 /* schedule laundromat to run immediately and wait for it to complete */
6773 nfsd4_lm_expire_lock(void)
6775 flush_workqueue(laundry_wq);
6779 nfsd4_lm_notify(struct file_lock *fl)
6781 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6782 struct net *net = lo->lo_owner.so_client->net;
6783 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6784 struct nfsd4_blocked_lock *nbl = container_of(fl,
6785 struct nfsd4_blocked_lock, nbl_lock);
6788 /* An empty list means that something else is going to be using it */
6789 spin_lock(&nn->blocked_locks_lock);
6790 if (!list_empty(&nbl->nbl_list)) {
6791 list_del_init(&nbl->nbl_list);
6792 list_del_init(&nbl->nbl_lru);
6795 spin_unlock(&nn->blocked_locks_lock);
6798 trace_nfsd_cb_notify_lock(lo, nbl);
6799 nfsd4_run_cb(&nbl->nbl_cb);
6803 static const struct lock_manager_operations nfsd_posix_mng_ops = {
6804 .lm_mod_owner = THIS_MODULE,
6805 .lm_notify = nfsd4_lm_notify,
6806 .lm_get_owner = nfsd4_lm_get_owner,
6807 .lm_put_owner = nfsd4_lm_put_owner,
6808 .lm_lock_expirable = nfsd4_lm_lock_expirable,
6809 .lm_expire_lock = nfsd4_lm_expire_lock,
6813 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6815 struct nfs4_lockowner *lo;
6817 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
6818 lo = (struct nfs4_lockowner *) fl->fl_owner;
6819 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6821 if (!deny->ld_owner.data)
6822 /* We just don't care that much */
6824 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
6827 deny->ld_owner.len = 0;
6828 deny->ld_owner.data = NULL;
6829 deny->ld_clientid.cl_boot = 0;
6830 deny->ld_clientid.cl_id = 0;
6832 deny->ld_start = fl->fl_start;
6833 deny->ld_length = NFS4_MAX_UINT64;
6834 if (fl->fl_end != NFS4_MAX_UINT64)
6835 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6836 deny->ld_type = NFS4_READ_LT;
6837 if (fl->fl_type != F_RDLCK)
6838 deny->ld_type = NFS4_WRITE_LT;
6841 static struct nfs4_lockowner *
6842 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
6844 unsigned int strhashval = ownerstr_hashval(owner);
6845 struct nfs4_stateowner *so;
6847 lockdep_assert_held(&clp->cl_lock);
6849 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6851 if (so->so_is_open_owner)
6853 if (same_owner_str(so, owner))
6854 return lockowner(nfs4_get_stateowner(so));
6859 static struct nfs4_lockowner *
6860 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
6862 struct nfs4_lockowner *lo;
6864 spin_lock(&clp->cl_lock);
6865 lo = find_lockowner_str_locked(clp, owner);
6866 spin_unlock(&clp->cl_lock);
6870 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6872 unhash_lockowner_locked(lockowner(sop));
6875 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6877 struct nfs4_lockowner *lo = lockowner(sop);
6879 kmem_cache_free(lockowner_slab, lo);
6882 static const struct nfs4_stateowner_operations lockowner_ops = {
6883 .so_unhash = nfs4_unhash_lockowner,
6884 .so_free = nfs4_free_lockowner,
6888 * Alloc a lock owner structure.
6889 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
6892 * strhashval = ownerstr_hashval
6894 static struct nfs4_lockowner *
6895 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6896 struct nfs4_ol_stateid *open_stp,
6897 struct nfsd4_lock *lock)
6899 struct nfs4_lockowner *lo, *ret;
6901 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6904 INIT_LIST_HEAD(&lo->lo_blocked);
6905 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6906 lo->lo_owner.so_is_open_owner = 0;
6907 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6908 lo->lo_owner.so_ops = &lockowner_ops;
6909 spin_lock(&clp->cl_lock);
6910 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
6912 list_add(&lo->lo_owner.so_strhash,
6913 &clp->cl_ownerstr_hashtbl[strhashval]);
6916 nfs4_free_stateowner(&lo->lo_owner);
6918 spin_unlock(&clp->cl_lock);
6922 static struct nfs4_ol_stateid *
6923 find_lock_stateid(const struct nfs4_lockowner *lo,
6924 const struct nfs4_ol_stateid *ost)
6926 struct nfs4_ol_stateid *lst;
6928 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
6930 /* If ost is not hashed, ost->st_locks will not be valid */
6931 if (!nfs4_ol_stateid_unhashed(ost))
6932 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6933 if (lst->st_stateowner == &lo->lo_owner) {
6934 refcount_inc(&lst->st_stid.sc_count);
6941 static struct nfs4_ol_stateid *
6942 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6943 struct nfs4_file *fp, struct inode *inode,
6944 struct nfs4_ol_stateid *open_stp)
6946 struct nfs4_client *clp = lo->lo_owner.so_client;
6947 struct nfs4_ol_stateid *retstp;
6949 mutex_init(&stp->st_mutex);
6950 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
6952 spin_lock(&clp->cl_lock);
6953 if (nfs4_ol_stateid_unhashed(open_stp))
6955 retstp = find_lock_stateid(lo, open_stp);
6958 refcount_inc(&stp->st_stid.sc_count);
6959 stp->st_stid.sc_type = NFS4_LOCK_STID;
6960 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
6962 stp->st_stid.sc_file = fp;
6963 stp->st_access_bmap = 0;
6964 stp->st_deny_bmap = open_stp->st_deny_bmap;
6965 stp->st_openstp = open_stp;
6966 spin_lock(&fp->fi_lock);
6967 list_add(&stp->st_locks, &open_stp->st_locks);
6968 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
6969 list_add(&stp->st_perfile, &fp->fi_stateids);
6970 spin_unlock(&fp->fi_lock);
6971 spin_unlock(&clp->cl_lock);
6974 spin_unlock(&clp->cl_lock);
6975 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6976 nfs4_put_stid(&retstp->st_stid);
6979 /* To keep mutex tracking happy */
6980 mutex_unlock(&stp->st_mutex);
6983 spin_unlock(&clp->cl_lock);
6984 mutex_unlock(&stp->st_mutex);
6988 static struct nfs4_ol_stateid *
6989 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6990 struct inode *inode, struct nfs4_ol_stateid *ost,
6993 struct nfs4_stid *ns = NULL;
6994 struct nfs4_ol_stateid *lst;
6995 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6996 struct nfs4_client *clp = oo->oo_owner.so_client;
6999 spin_lock(&clp->cl_lock);
7000 lst = find_lock_stateid(lo, ost);
7001 spin_unlock(&clp->cl_lock);
7003 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7005 nfs4_put_stid(&lst->st_stid);
7007 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7011 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7012 if (lst == openlockstateid(ns))
7021 check_lock_length(u64 offset, u64 length)
7023 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7024 (length > ~offset)));
7027 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
7029 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
7031 lockdep_assert_held(&fp->fi_lock);
7033 if (test_access(access, lock_stp))
7035 __nfs4_file_get_access(fp, access);
7036 set_access(access, lock_stp);
7040 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7041 struct nfs4_ol_stateid *ost,
7042 struct nfsd4_lock *lock,
7043 struct nfs4_ol_stateid **plst, bool *new)
7046 struct nfs4_file *fi = ost->st_stid.sc_file;
7047 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7048 struct nfs4_client *cl = oo->oo_owner.so_client;
7049 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
7050 struct nfs4_lockowner *lo;
7051 struct nfs4_ol_stateid *lst;
7052 unsigned int strhashval;
7054 lo = find_lockowner_str(cl, &lock->lk_new_owner);
7056 strhashval = ownerstr_hashval(&lock->lk_new_owner);
7057 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7059 return nfserr_jukebox;
7061 /* with an existing lockowner, seqids must be the same */
7062 status = nfserr_bad_seqid;
7063 if (!cstate->minorversion &&
7064 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
7068 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7070 status = nfserr_jukebox;
7077 nfs4_put_stateowner(&lo->lo_owner);
7085 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7086 union nfsd4_op_u *u)
7088 struct nfsd4_lock *lock = &u->lock;
7089 struct nfs4_openowner *open_sop = NULL;
7090 struct nfs4_lockowner *lock_sop = NULL;
7091 struct nfs4_ol_stateid *lock_stp = NULL;
7092 struct nfs4_ol_stateid *open_stp = NULL;
7093 struct nfs4_file *fp;
7094 struct nfsd_file *nf = NULL;
7095 struct nfsd4_blocked_lock *nbl = NULL;
7096 struct file_lock *file_lock = NULL;
7097 struct file_lock *conflock = NULL;
7102 unsigned char fl_type;
7103 unsigned int fl_flags = FL_POSIX;
7104 struct net *net = SVC_NET(rqstp);
7105 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7107 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7108 (long long) lock->lk_offset,
7109 (long long) lock->lk_length);
7111 if (check_lock_length(lock->lk_offset, lock->lk_length))
7112 return nfserr_inval;
7114 if ((status = fh_verify(rqstp, &cstate->current_fh,
7115 S_IFREG, NFSD_MAY_LOCK))) {
7116 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7120 if (lock->lk_is_new) {
7121 if (nfsd4_has_session(cstate))
7122 /* See rfc 5661 18.10.3: given clientid is ignored: */
7123 memcpy(&lock->lk_new_clientid,
7124 &cstate->clp->cl_clientid,
7125 sizeof(clientid_t));
7127 /* validate and update open stateid and open seqid */
7128 status = nfs4_preprocess_confirmed_seqid_op(cstate,
7129 lock->lk_new_open_seqid,
7130 &lock->lk_new_open_stateid,
7134 mutex_unlock(&open_stp->st_mutex);
7135 open_sop = openowner(open_stp->st_stateowner);
7136 status = nfserr_bad_stateid;
7137 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
7138 &lock->lk_new_clientid))
7140 status = lookup_or_create_lock_state(cstate, open_stp, lock,
7143 status = nfs4_preprocess_seqid_op(cstate,
7144 lock->lk_old_lock_seqid,
7145 &lock->lk_old_lock_stateid,
7146 NFS4_LOCK_STID, &lock_stp, nn);
7150 lock_sop = lockowner(lock_stp->st_stateowner);
7152 lkflg = setlkflg(lock->lk_type);
7153 status = nfs4_check_openmode(lock_stp, lkflg);
7157 status = nfserr_grace;
7158 if (locks_in_grace(net) && !lock->lk_reclaim)
7160 status = nfserr_no_grace;
7161 if (!locks_in_grace(net) && lock->lk_reclaim)
7164 if (lock->lk_reclaim)
7165 fl_flags |= FL_RECLAIM;
7167 fp = lock_stp->st_stid.sc_file;
7168 switch (lock->lk_type) {
7170 if (nfsd4_has_session(cstate))
7171 fl_flags |= FL_SLEEP;
7174 spin_lock(&fp->fi_lock);
7175 nf = find_readable_file_locked(fp);
7177 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7178 spin_unlock(&fp->fi_lock);
7181 case NFS4_WRITEW_LT:
7182 if (nfsd4_has_session(cstate))
7183 fl_flags |= FL_SLEEP;
7186 spin_lock(&fp->fi_lock);
7187 nf = find_writeable_file_locked(fp);
7189 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7190 spin_unlock(&fp->fi_lock);
7194 status = nfserr_inval;
7199 status = nfserr_openmode;
7204 * Most filesystems with their own ->lock operations will block
7205 * the nfsd thread waiting to acquire the lock. That leads to
7206 * deadlocks (we don't want every nfsd thread tied up waiting
7207 * for file locks), so don't attempt blocking lock notifications
7208 * on those filesystems:
7210 if (nf->nf_file->f_op->lock)
7211 fl_flags &= ~FL_SLEEP;
7213 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
7215 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
7216 status = nfserr_jukebox;
7220 file_lock = &nbl->nbl_lock;
7221 file_lock->fl_type = fl_type;
7222 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
7223 file_lock->fl_pid = current->tgid;
7224 file_lock->fl_file = nf->nf_file;
7225 file_lock->fl_flags = fl_flags;
7226 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7227 file_lock->fl_start = lock->lk_offset;
7228 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
7229 nfs4_transform_lock_offset(file_lock);
7231 conflock = locks_alloc_lock();
7233 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7234 status = nfserr_jukebox;
7238 if (fl_flags & FL_SLEEP) {
7239 nbl->nbl_time = ktime_get_boottime_seconds();
7240 spin_lock(&nn->blocked_locks_lock);
7241 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
7242 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
7243 kref_get(&nbl->nbl_kref);
7244 spin_unlock(&nn->blocked_locks_lock);
7247 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
7249 case 0: /* success! */
7250 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
7252 if (lock->lk_reclaim)
7253 nn->somebody_reclaimed = true;
7255 case FILE_LOCK_DEFERRED:
7256 kref_put(&nbl->nbl_kref, free_nbl);
7259 case -EAGAIN: /* conflock holds conflicting lock */
7260 status = nfserr_denied;
7261 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
7262 nfs4_set_lock_denied(conflock, &lock->lk_denied);
7265 status = nfserr_deadlock;
7268 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
7269 status = nfserrno(err);
7274 /* dequeue it if we queued it before */
7275 if (fl_flags & FL_SLEEP) {
7276 spin_lock(&nn->blocked_locks_lock);
7277 if (!list_empty(&nbl->nbl_list) &&
7278 !list_empty(&nbl->nbl_lru)) {
7279 list_del_init(&nbl->nbl_list);
7280 list_del_init(&nbl->nbl_lru);
7281 kref_put(&nbl->nbl_kref, free_nbl);
7283 /* nbl can use one of lists to be linked to reaplist */
7284 spin_unlock(&nn->blocked_locks_lock);
7286 free_blocked_lock(nbl);
7291 /* Bump seqid manually if the 4.0 replay owner is openowner */
7292 if (cstate->replay_owner &&
7293 cstate->replay_owner != &lock_sop->lo_owner &&
7294 seqid_mutating_err(ntohl(status)))
7295 lock_sop->lo_owner.so_seqid++;
7298 * If this is a new, never-before-used stateid, and we are
7299 * returning an error, then just go ahead and release it.
7302 release_lock_stateid(lock_stp);
7304 mutex_unlock(&lock_stp->st_mutex);
7306 nfs4_put_stid(&lock_stp->st_stid);
7309 nfs4_put_stid(&open_stp->st_stid);
7310 nfsd4_bump_seqid(cstate, status);
7312 locks_free_lock(conflock);
7317 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7318 * so we do a temporary open here just to get an open file to pass to
7321 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
7323 struct nfsd_file *nf;
7326 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7329 fh_lock(fhp); /* to block new leases till after test_lock: */
7330 err = nfserrno(nfsd_open_break_lease(fhp->fh_dentry->d_inode,
7334 lock->fl_file = nf->nf_file;
7335 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
7336 lock->fl_file = NULL;
7347 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7348 union nfsd4_op_u *u)
7350 struct nfsd4_lockt *lockt = &u->lockt;
7351 struct file_lock *file_lock = NULL;
7352 struct nfs4_lockowner *lo = NULL;
7354 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7356 if (locks_in_grace(SVC_NET(rqstp)))
7357 return nfserr_grace;
7359 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7360 return nfserr_inval;
7362 if (!nfsd4_has_session(cstate)) {
7363 status = set_client(&lockt->lt_clientid, cstate, nn);
7368 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7371 file_lock = locks_alloc_lock();
7373 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7374 status = nfserr_jukebox;
7378 switch (lockt->lt_type) {
7381 file_lock->fl_type = F_RDLCK;
7384 case NFS4_WRITEW_LT:
7385 file_lock->fl_type = F_WRLCK;
7388 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
7389 status = nfserr_inval;
7393 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
7395 file_lock->fl_owner = (fl_owner_t)lo;
7396 file_lock->fl_pid = current->tgid;
7397 file_lock->fl_flags = FL_POSIX;
7399 file_lock->fl_start = lockt->lt_offset;
7400 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
7402 nfs4_transform_lock_offset(file_lock);
7404 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
7408 if (file_lock->fl_type != F_UNLCK) {
7409 status = nfserr_denied;
7410 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
7414 nfs4_put_stateowner(&lo->lo_owner);
7416 locks_free_lock(file_lock);
7421 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7422 union nfsd4_op_u *u)
7424 struct nfsd4_locku *locku = &u->locku;
7425 struct nfs4_ol_stateid *stp;
7426 struct nfsd_file *nf = NULL;
7427 struct file_lock *file_lock = NULL;
7430 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7432 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7433 (long long) locku->lu_offset,
7434 (long long) locku->lu_length);
7436 if (check_lock_length(locku->lu_offset, locku->lu_length))
7437 return nfserr_inval;
7439 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
7440 &locku->lu_stateid, NFS4_LOCK_STID,
7444 nf = find_any_file(stp->st_stid.sc_file);
7446 status = nfserr_lock_range;
7449 file_lock = locks_alloc_lock();
7451 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7452 status = nfserr_jukebox;
7456 file_lock->fl_type = F_UNLCK;
7457 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7458 file_lock->fl_pid = current->tgid;
7459 file_lock->fl_file = nf->nf_file;
7460 file_lock->fl_flags = FL_POSIX;
7461 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7462 file_lock->fl_start = locku->lu_offset;
7464 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7466 nfs4_transform_lock_offset(file_lock);
7468 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7470 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7473 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7477 mutex_unlock(&stp->st_mutex);
7478 nfs4_put_stid(&stp->st_stid);
7480 nfsd4_bump_seqid(cstate, status);
7482 locks_free_lock(file_lock);
7486 status = nfserrno(err);
7492 * true: locks held by lockowner
7493 * false: no locks held by lockowner
7496 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7498 struct file_lock *fl;
7500 struct nfsd_file *nf = find_any_file(fp);
7501 struct inode *inode;
7502 struct file_lock_context *flctx;
7505 /* Any valid lock stateid should have some sort of access */
7510 inode = locks_inode(nf->nf_file);
7511 flctx = inode->i_flctx;
7513 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7514 spin_lock(&flctx->flc_lock);
7515 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7516 if (fl->fl_owner == (fl_owner_t)lowner) {
7521 spin_unlock(&flctx->flc_lock);
7528 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
7529 * @rqstp: RPC transaction
7530 * @cstate: NFSv4 COMPOUND state
7531 * @u: RELEASE_LOCKOWNER arguments
7533 * The lockowner's so_count is bumped when a lock record is added
7534 * or when copying a conflicting lock. The latter case is brief,
7535 * but can lead to fleeting false positives when looking for
7539 * %nfs_ok: lockowner released or not found
7540 * %nfserr_locks_held: lockowner still in use
7541 * %nfserr_stale_clientid: clientid no longer active
7542 * %nfserr_expired: clientid not recognized
7545 nfsd4_release_lockowner(struct svc_rqst *rqstp,
7546 struct nfsd4_compound_state *cstate,
7547 union nfsd4_op_u *u)
7549 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
7550 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7551 clientid_t *clid = &rlockowner->rl_clientid;
7552 struct nfs4_ol_stateid *stp;
7553 struct nfs4_lockowner *lo;
7554 struct nfs4_client *clp;
7555 LIST_HEAD(reaplist);
7558 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7559 clid->cl_boot, clid->cl_id);
7561 status = set_client(clid, cstate, nn);
7566 spin_lock(&clp->cl_lock);
7567 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
7569 spin_unlock(&clp->cl_lock);
7572 if (atomic_read(&lo->lo_owner.so_count) != 2) {
7573 spin_unlock(&clp->cl_lock);
7574 nfs4_put_stateowner(&lo->lo_owner);
7575 return nfserr_locks_held;
7577 unhash_lockowner_locked(lo);
7578 while (!list_empty(&lo->lo_owner.so_stateids)) {
7579 stp = list_first_entry(&lo->lo_owner.so_stateids,
7580 struct nfs4_ol_stateid,
7582 WARN_ON(!unhash_lock_stateid(stp));
7583 put_ol_stateid_locked(stp, &reaplist);
7585 spin_unlock(&clp->cl_lock);
7587 free_ol_stateid_reaplist(&reaplist);
7588 remove_blocked_locks(lo);
7589 nfs4_put_stateowner(&lo->lo_owner);
7593 static inline struct nfs4_client_reclaim *
7596 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
7600 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7602 struct nfs4_client_reclaim *crp;
7604 crp = nfsd4_find_reclaim_client(name, nn);
7605 return (crp && crp->cr_clp);
7609 * failure => all reset bets are off, nfserr_no_grace...
7611 * The caller is responsible for freeing name.data if NULL is returned (it
7612 * will be freed in nfs4_remove_reclaim_record in the normal case).
7614 struct nfs4_client_reclaim *
7615 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7616 struct nfsd_net *nn)
7618 unsigned int strhashval;
7619 struct nfs4_client_reclaim *crp;
7621 crp = alloc_reclaim();
7623 strhashval = clientstr_hashval(name);
7624 INIT_LIST_HEAD(&crp->cr_strhash);
7625 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
7626 crp->cr_name.data = name.data;
7627 crp->cr_name.len = name.len;
7628 crp->cr_princhash.data = princhash.data;
7629 crp->cr_princhash.len = princhash.len;
7631 nn->reclaim_str_hashtbl_size++;
7637 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
7639 list_del(&crp->cr_strhash);
7640 kfree(crp->cr_name.data);
7641 kfree(crp->cr_princhash.data);
7643 nn->reclaim_str_hashtbl_size--;
7647 nfs4_release_reclaim(struct nfsd_net *nn)
7649 struct nfs4_client_reclaim *crp = NULL;
7652 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7653 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7654 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
7655 struct nfs4_client_reclaim, cr_strhash);
7656 nfs4_remove_reclaim_record(crp, nn);
7659 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
7663 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
7664 struct nfs4_client_reclaim *
7665 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
7667 unsigned int strhashval;
7668 struct nfs4_client_reclaim *crp = NULL;
7670 strhashval = clientstr_hashval(name);
7671 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
7672 if (compare_blob(&crp->cr_name, &name) == 0) {
7680 nfs4_check_open_reclaim(struct nfs4_client *clp)
7682 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
7683 return nfserr_no_grace;
7685 if (nfsd4_client_record_check(clp))
7686 return nfserr_reclaim_bad;
7692 * Since the lifetime of a delegation isn't limited to that of an open, a
7693 * client may quite reasonably hang on to a delegation as long as it has
7694 * the inode cached. This becomes an obvious problem the first time a
7695 * client's inode cache approaches the size of the server's total memory.
7697 * For now we avoid this problem by imposing a hard limit on the number
7698 * of delegations, which varies according to the server's memory size.
7701 set_max_delegations(void)
7704 * Allow at most 4 delegations per megabyte of RAM. Quick
7705 * estimates suggest that in the worst case (where every delegation
7706 * is for a different inode), a delegation could take about 1.5K,
7707 * giving a worst case usage of about 6% of memory.
7709 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7712 static int nfs4_state_create_net(struct net *net)
7714 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7717 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7718 sizeof(struct list_head),
7720 if (!nn->conf_id_hashtbl)
7722 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7723 sizeof(struct list_head),
7725 if (!nn->unconf_id_hashtbl)
7727 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7728 sizeof(struct list_head),
7730 if (!nn->sessionid_hashtbl)
7733 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7734 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7735 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7737 for (i = 0; i < SESSION_HASH_SIZE; i++)
7738 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7739 nn->conf_name_tree = RB_ROOT;
7740 nn->unconf_name_tree = RB_ROOT;
7741 nn->boot_time = ktime_get_real_seconds();
7742 nn->grace_ended = false;
7743 nn->nfsd4_manager.block_opens = true;
7744 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7745 INIT_LIST_HEAD(&nn->client_lru);
7746 INIT_LIST_HEAD(&nn->close_lru);
7747 INIT_LIST_HEAD(&nn->del_recall_lru);
7748 spin_lock_init(&nn->client_lock);
7749 spin_lock_init(&nn->s2s_cp_lock);
7750 idr_init(&nn->s2s_cp_stateids);
7752 spin_lock_init(&nn->blocked_locks_lock);
7753 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7755 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7761 kfree(nn->unconf_id_hashtbl);
7763 kfree(nn->conf_id_hashtbl);
7769 nfs4_state_destroy_net(struct net *net)
7772 struct nfs4_client *clp = NULL;
7773 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7775 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7776 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7777 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7778 destroy_client(clp);
7782 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7784 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7785 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7786 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7787 destroy_client(clp);
7791 kfree(nn->sessionid_hashtbl);
7792 kfree(nn->unconf_id_hashtbl);
7793 kfree(nn->conf_id_hashtbl);
7798 nfs4_state_start_net(struct net *net)
7800 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7803 ret = nfs4_state_create_net(net);
7806 locks_start_grace(net, &nn->nfsd4_manager);
7807 nfsd4_client_tracking_init(net);
7808 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7810 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
7811 nn->nfsd4_grace, net->ns.inum);
7812 trace_nfsd_grace_start(nn);
7813 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7817 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7819 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7820 nfsd4_end_grace(nn);
7824 /* initialization to perform when the nfsd service is started: */
7827 nfs4_state_start(void)
7831 ret = nfsd4_create_callback_queue();
7835 set_max_delegations();
7840 nfs4_state_shutdown_net(struct net *net)
7842 struct nfs4_delegation *dp = NULL;
7843 struct list_head *pos, *next, reaplist;
7844 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7846 cancel_delayed_work_sync(&nn->laundromat_work);
7847 locks_end_grace(&nn->nfsd4_manager);
7849 INIT_LIST_HEAD(&reaplist);
7850 spin_lock(&state_lock);
7851 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7852 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7853 WARN_ON(!unhash_delegation_locked(dp));
7854 list_add(&dp->dl_recall_lru, &reaplist);
7856 spin_unlock(&state_lock);
7857 list_for_each_safe(pos, next, &reaplist) {
7858 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7859 list_del_init(&dp->dl_recall_lru);
7860 destroy_unhashed_deleg(dp);
7863 nfsd4_client_tracking_exit(net);
7864 nfs4_state_destroy_net(net);
7865 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
7866 nfsd4_ssc_shutdown_umount(nn);
7871 nfs4_state_shutdown(void)
7873 nfsd4_destroy_callback_queue();
7877 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7879 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7880 CURRENT_STATEID(stateid))
7881 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7885 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7887 if (cstate->minorversion) {
7888 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7889 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7894 clear_current_stateid(struct nfsd4_compound_state *cstate)
7896 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7900 * functions to set current state id
7903 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7904 union nfsd4_op_u *u)
7906 put_stateid(cstate, &u->open_downgrade.od_stateid);
7910 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7911 union nfsd4_op_u *u)
7913 put_stateid(cstate, &u->open.op_stateid);
7917 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7918 union nfsd4_op_u *u)
7920 put_stateid(cstate, &u->close.cl_stateid);
7924 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7925 union nfsd4_op_u *u)
7927 put_stateid(cstate, &u->lock.lk_resp_stateid);
7931 * functions to consume current state id
7935 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7936 union nfsd4_op_u *u)
7938 get_stateid(cstate, &u->open_downgrade.od_stateid);
7942 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7943 union nfsd4_op_u *u)
7945 get_stateid(cstate, &u->delegreturn.dr_stateid);
7949 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7950 union nfsd4_op_u *u)
7952 get_stateid(cstate, &u->free_stateid.fr_stateid);
7956 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7957 union nfsd4_op_u *u)
7959 get_stateid(cstate, &u->setattr.sa_stateid);
7963 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7964 union nfsd4_op_u *u)
7966 get_stateid(cstate, &u->close.cl_stateid);
7970 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7971 union nfsd4_op_u *u)
7973 get_stateid(cstate, &u->locku.lu_stateid);
7977 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7978 union nfsd4_op_u *u)
7980 get_stateid(cstate, &u->read.rd_stateid);
7984 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7985 union nfsd4_op_u *u)
7987 get_stateid(cstate, &u->write.wr_stateid);