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 nfs4_clnt_odstate *odstate)
1136 struct nfs4_delegation *dp;
1139 dprintk("NFSD alloc_init_deleg\n");
1140 n = atomic_long_inc_return(&num_delegations);
1141 if (n < 0 || n > max_delegations)
1143 if (delegation_blocked(&fp->fi_fhandle))
1145 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
1150 * delegation seqid's are never incremented. The 4.1 special
1151 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1152 * 0 anyway just for consistency and use 1:
1154 dp->dl_stid.sc_stateid.si_generation = 1;
1155 INIT_LIST_HEAD(&dp->dl_perfile);
1156 INIT_LIST_HEAD(&dp->dl_perclnt);
1157 INIT_LIST_HEAD(&dp->dl_recall_lru);
1158 dp->dl_clnt_odstate = odstate;
1159 get_clnt_odstate(odstate);
1160 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
1162 dp->dl_recalled = false;
1163 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1164 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1166 dp->dl_stid.sc_file = fp;
1169 atomic_long_dec(&num_delegations);
1174 nfs4_put_stid(struct nfs4_stid *s)
1176 struct nfs4_file *fp = s->sc_file;
1177 struct nfs4_client *clp = s->sc_client;
1179 might_lock(&clp->cl_lock);
1181 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1182 wake_up_all(&close_wq);
1185 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1186 nfs4_free_cpntf_statelist(clp->net, s);
1187 spin_unlock(&clp->cl_lock);
1194 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1196 stateid_t *src = &stid->sc_stateid;
1198 spin_lock(&stid->sc_lock);
1199 if (unlikely(++src->si_generation == 0))
1200 src->si_generation = 1;
1201 memcpy(dst, src, sizeof(*dst));
1202 spin_unlock(&stid->sc_lock);
1205 static void put_deleg_file(struct nfs4_file *fp)
1207 struct nfsd_file *nf = NULL;
1209 spin_lock(&fp->fi_lock);
1210 if (--fp->fi_delegees == 0)
1211 swap(nf, fp->fi_deleg_file);
1212 spin_unlock(&fp->fi_lock);
1218 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1220 struct nfs4_file *fp = dp->dl_stid.sc_file;
1221 struct nfsd_file *nf = fp->fi_deleg_file;
1223 WARN_ON_ONCE(!fp->fi_delegees);
1225 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1229 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1231 put_clnt_odstate(dp->dl_clnt_odstate);
1232 nfs4_unlock_deleg_lease(dp);
1233 nfs4_put_stid(&dp->dl_stid);
1236 void nfs4_unhash_stid(struct nfs4_stid *s)
1242 * nfs4_delegation_exists - Discover if this delegation already exists
1243 * @clp: a pointer to the nfs4_client we're granting a delegation to
1244 * @fp: a pointer to the nfs4_file we're granting a delegation on
1247 * On success: true iff an existing delegation is found
1251 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1253 struct nfs4_delegation *searchdp = NULL;
1254 struct nfs4_client *searchclp = NULL;
1256 lockdep_assert_held(&state_lock);
1257 lockdep_assert_held(&fp->fi_lock);
1259 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1260 searchclp = searchdp->dl_stid.sc_client;
1261 if (clp == searchclp) {
1269 * hash_delegation_locked - Add a delegation to the appropriate lists
1270 * @dp: a pointer to the nfs4_delegation we are adding.
1271 * @fp: a pointer to the nfs4_file we're granting a delegation on
1274 * On success: NULL if the delegation was successfully hashed.
1276 * On error: -EAGAIN if one was previously granted to this
1277 * nfs4_client for this nfs4_file. Delegation is not hashed.
1282 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1284 struct nfs4_client *clp = dp->dl_stid.sc_client;
1286 lockdep_assert_held(&state_lock);
1287 lockdep_assert_held(&fp->fi_lock);
1289 if (nfs4_delegation_exists(clp, fp))
1291 refcount_inc(&dp->dl_stid.sc_count);
1292 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1293 list_add(&dp->dl_perfile, &fp->fi_delegations);
1294 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1298 static bool delegation_hashed(struct nfs4_delegation *dp)
1300 return !(list_empty(&dp->dl_perfile));
1304 unhash_delegation_locked(struct nfs4_delegation *dp)
1306 struct nfs4_file *fp = dp->dl_stid.sc_file;
1308 lockdep_assert_held(&state_lock);
1310 if (!delegation_hashed(dp))
1313 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1314 /* Ensure that deleg break won't try to requeue it */
1316 spin_lock(&fp->fi_lock);
1317 list_del_init(&dp->dl_perclnt);
1318 list_del_init(&dp->dl_recall_lru);
1319 list_del_init(&dp->dl_perfile);
1320 spin_unlock(&fp->fi_lock);
1324 static void destroy_delegation(struct nfs4_delegation *dp)
1328 spin_lock(&state_lock);
1329 unhashed = unhash_delegation_locked(dp);
1330 spin_unlock(&state_lock);
1332 destroy_unhashed_deleg(dp);
1335 static void revoke_delegation(struct nfs4_delegation *dp)
1337 struct nfs4_client *clp = dp->dl_stid.sc_client;
1339 WARN_ON(!list_empty(&dp->dl_recall_lru));
1341 if (clp->cl_minorversion) {
1342 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1343 refcount_inc(&dp->dl_stid.sc_count);
1344 spin_lock(&clp->cl_lock);
1345 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1346 spin_unlock(&clp->cl_lock);
1348 destroy_unhashed_deleg(dp);
1355 static unsigned int clientid_hashval(u32 id)
1357 return id & CLIENT_HASH_MASK;
1360 static unsigned int clientstr_hashval(struct xdr_netobj name)
1362 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1366 * A stateid that had a deny mode associated with it is being released
1367 * or downgraded. Recalculate the deny mode on the file.
1370 recalculate_deny_mode(struct nfs4_file *fp)
1372 struct nfs4_ol_stateid *stp;
1374 spin_lock(&fp->fi_lock);
1375 fp->fi_share_deny = 0;
1376 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1377 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1378 spin_unlock(&fp->fi_lock);
1382 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1385 bool change = false;
1387 for (i = 1; i < 4; i++) {
1388 if ((i & deny) != i) {
1394 /* Recalculate per-file deny mode if there was a change */
1396 recalculate_deny_mode(stp->st_stid.sc_file);
1399 /* release all access and file references for a given stateid */
1401 release_all_access(struct nfs4_ol_stateid *stp)
1404 struct nfs4_file *fp = stp->st_stid.sc_file;
1406 if (fp && stp->st_deny_bmap != 0)
1407 recalculate_deny_mode(fp);
1409 for (i = 1; i < 4; i++) {
1410 if (test_access(i, stp))
1411 nfs4_file_put_access(stp->st_stid.sc_file, i);
1412 clear_access(i, stp);
1416 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1418 kfree(sop->so_owner.data);
1419 sop->so_ops->so_free(sop);
1422 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1424 struct nfs4_client *clp = sop->so_client;
1426 might_lock(&clp->cl_lock);
1428 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1430 sop->so_ops->so_unhash(sop);
1431 spin_unlock(&clp->cl_lock);
1432 nfs4_free_stateowner(sop);
1436 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1438 return list_empty(&stp->st_perfile);
1441 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1443 struct nfs4_file *fp = stp->st_stid.sc_file;
1445 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1447 if (list_empty(&stp->st_perfile))
1450 spin_lock(&fp->fi_lock);
1451 list_del_init(&stp->st_perfile);
1452 spin_unlock(&fp->fi_lock);
1453 list_del(&stp->st_perstateowner);
1457 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1459 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1461 put_clnt_odstate(stp->st_clnt_odstate);
1462 release_all_access(stp);
1463 if (stp->st_stateowner)
1464 nfs4_put_stateowner(stp->st_stateowner);
1465 kmem_cache_free(stateid_slab, stid);
1468 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1470 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1471 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1472 struct nfsd_file *nf;
1474 nf = find_any_file(stp->st_stid.sc_file);
1476 get_file(nf->nf_file);
1477 filp_close(nf->nf_file, (fl_owner_t)lo);
1480 nfs4_free_ol_stateid(stid);
1484 * Put the persistent reference to an already unhashed generic stateid, while
1485 * holding the cl_lock. If it's the last reference, then put it onto the
1486 * reaplist for later destruction.
1488 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1489 struct list_head *reaplist)
1491 struct nfs4_stid *s = &stp->st_stid;
1492 struct nfs4_client *clp = s->sc_client;
1494 lockdep_assert_held(&clp->cl_lock);
1496 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1498 if (!refcount_dec_and_test(&s->sc_count)) {
1499 wake_up_all(&close_wq);
1503 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1504 list_add(&stp->st_locks, reaplist);
1507 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1509 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1511 if (!unhash_ol_stateid(stp))
1513 list_del_init(&stp->st_locks);
1514 nfs4_unhash_stid(&stp->st_stid);
1518 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1520 struct nfs4_client *clp = stp->st_stid.sc_client;
1523 spin_lock(&clp->cl_lock);
1524 unhashed = unhash_lock_stateid(stp);
1525 spin_unlock(&clp->cl_lock);
1527 nfs4_put_stid(&stp->st_stid);
1530 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1532 struct nfs4_client *clp = lo->lo_owner.so_client;
1534 lockdep_assert_held(&clp->cl_lock);
1536 list_del_init(&lo->lo_owner.so_strhash);
1540 * Free a list of generic stateids that were collected earlier after being
1544 free_ol_stateid_reaplist(struct list_head *reaplist)
1546 struct nfs4_ol_stateid *stp;
1547 struct nfs4_file *fp;
1551 while (!list_empty(reaplist)) {
1552 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1554 list_del(&stp->st_locks);
1555 fp = stp->st_stid.sc_file;
1556 stp->st_stid.sc_free(&stp->st_stid);
1562 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1563 struct list_head *reaplist)
1565 struct nfs4_ol_stateid *stp;
1567 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1569 while (!list_empty(&open_stp->st_locks)) {
1570 stp = list_entry(open_stp->st_locks.next,
1571 struct nfs4_ol_stateid, st_locks);
1572 WARN_ON(!unhash_lock_stateid(stp));
1573 put_ol_stateid_locked(stp, reaplist);
1577 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1578 struct list_head *reaplist)
1580 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1582 if (!unhash_ol_stateid(stp))
1584 release_open_stateid_locks(stp, reaplist);
1588 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1590 LIST_HEAD(reaplist);
1592 spin_lock(&stp->st_stid.sc_client->cl_lock);
1593 if (unhash_open_stateid(stp, &reaplist))
1594 put_ol_stateid_locked(stp, &reaplist);
1595 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1596 free_ol_stateid_reaplist(&reaplist);
1599 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1601 struct nfs4_client *clp = oo->oo_owner.so_client;
1603 lockdep_assert_held(&clp->cl_lock);
1605 list_del_init(&oo->oo_owner.so_strhash);
1606 list_del_init(&oo->oo_perclient);
1609 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1611 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1613 struct nfs4_ol_stateid *s;
1615 spin_lock(&nn->client_lock);
1616 s = oo->oo_last_closed_stid;
1618 list_del_init(&oo->oo_close_lru);
1619 oo->oo_last_closed_stid = NULL;
1621 spin_unlock(&nn->client_lock);
1623 nfs4_put_stid(&s->st_stid);
1626 static void release_openowner(struct nfs4_openowner *oo)
1628 struct nfs4_ol_stateid *stp;
1629 struct nfs4_client *clp = oo->oo_owner.so_client;
1630 struct list_head reaplist;
1632 INIT_LIST_HEAD(&reaplist);
1634 spin_lock(&clp->cl_lock);
1635 unhash_openowner_locked(oo);
1636 while (!list_empty(&oo->oo_owner.so_stateids)) {
1637 stp = list_first_entry(&oo->oo_owner.so_stateids,
1638 struct nfs4_ol_stateid, st_perstateowner);
1639 if (unhash_open_stateid(stp, &reaplist))
1640 put_ol_stateid_locked(stp, &reaplist);
1642 spin_unlock(&clp->cl_lock);
1643 free_ol_stateid_reaplist(&reaplist);
1644 release_last_closed_stateid(oo);
1645 nfs4_put_stateowner(&oo->oo_owner);
1649 hash_sessionid(struct nfs4_sessionid *sessionid)
1651 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1653 return sid->sequence % SESSION_HASH_SIZE;
1656 #ifdef CONFIG_SUNRPC_DEBUG
1658 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1660 u32 *ptr = (u32 *)(&sessionid->data[0]);
1661 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1665 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1671 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1672 * won't be used for replay.
1674 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1676 struct nfs4_stateowner *so = cstate->replay_owner;
1678 if (nfserr == nfserr_replay_me)
1681 if (!seqid_mutating_err(ntohl(nfserr))) {
1682 nfsd4_cstate_clear_replay(cstate);
1687 if (so->so_is_open_owner)
1688 release_last_closed_stateid(openowner(so));
1694 gen_sessionid(struct nfsd4_session *ses)
1696 struct nfs4_client *clp = ses->se_client;
1697 struct nfsd4_sessionid *sid;
1699 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1700 sid->clientid = clp->cl_clientid;
1701 sid->sequence = current_sessionid++;
1706 * The protocol defines ca_maxresponssize_cached to include the size of
1707 * the rpc header, but all we need to cache is the data starting after
1708 * the end of the initial SEQUENCE operation--the rest we regenerate
1709 * each time. Therefore we can advertise a ca_maxresponssize_cached
1710 * value that is the number of bytes in our cache plus a few additional
1711 * bytes. In order to stay on the safe side, and not promise more than
1712 * we can cache, those additional bytes must be the minimum possible: 24
1713 * bytes of rpc header (xid through accept state, with AUTH_NULL
1714 * verifier), 12 for the compound header (with zero-length tag), and 44
1715 * for the SEQUENCE op response:
1717 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1720 free_session_slots(struct nfsd4_session *ses)
1724 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1725 free_svc_cred(&ses->se_slots[i]->sl_cred);
1726 kfree(ses->se_slots[i]);
1731 * We don't actually need to cache the rpc and session headers, so we
1732 * can allocate a little less for each slot:
1734 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1738 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1741 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1742 return size + sizeof(struct nfsd4_slot);
1746 * XXX: If we run out of reserved DRC memory we could (up to a point)
1747 * re-negotiate active sessions and reduce their slot usage to make
1748 * room for new connections. For now we just fail the create session.
1750 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1752 u32 slotsize = slot_bytes(ca);
1753 u32 num = ca->maxreqs;
1754 unsigned long avail, total_avail;
1755 unsigned int scale_factor;
1757 spin_lock(&nfsd_drc_lock);
1758 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1759 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1761 /* We have handed out more space than we chose in
1762 * set_max_drc() to allow. That isn't really a
1763 * problem as long as that doesn't make us think we
1764 * have lots more due to integer overflow.
1767 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1769 * Never use more than a fraction of the remaining memory,
1770 * unless it's the only way to give this client a slot.
1771 * The chosen fraction is either 1/8 or 1/number of threads,
1772 * whichever is smaller. This ensures there are adequate
1773 * slots to support multiple clients per thread.
1774 * Give the client one slot even if that would require
1775 * over-allocation--it is better than failure.
1777 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1779 avail = clamp_t(unsigned long, avail, slotsize,
1780 total_avail/scale_factor);
1781 num = min_t(int, num, avail / slotsize);
1782 num = max_t(int, num, 1);
1783 nfsd_drc_mem_used += num * slotsize;
1784 spin_unlock(&nfsd_drc_lock);
1789 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1791 int slotsize = slot_bytes(ca);
1793 spin_lock(&nfsd_drc_lock);
1794 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1795 spin_unlock(&nfsd_drc_lock);
1798 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1799 struct nfsd4_channel_attrs *battrs)
1801 int numslots = fattrs->maxreqs;
1802 int slotsize = slot_bytes(fattrs);
1803 struct nfsd4_session *new;
1806 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1807 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1808 mem = numslots * sizeof(struct nfsd4_slot *);
1810 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1813 /* allocate each struct nfsd4_slot and data cache in one piece */
1814 for (i = 0; i < numslots; i++) {
1815 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1816 if (!new->se_slots[i])
1820 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1821 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1826 kfree(new->se_slots[i]);
1831 static void free_conn(struct nfsd4_conn *c)
1833 svc_xprt_put(c->cn_xprt);
1837 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1839 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1840 struct nfs4_client *clp = c->cn_session->se_client;
1842 trace_nfsd_cb_lost(clp);
1844 spin_lock(&clp->cl_lock);
1845 if (!list_empty(&c->cn_persession)) {
1846 list_del(&c->cn_persession);
1849 nfsd4_probe_callback(clp);
1850 spin_unlock(&clp->cl_lock);
1853 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1855 struct nfsd4_conn *conn;
1857 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1860 svc_xprt_get(rqstp->rq_xprt);
1861 conn->cn_xprt = rqstp->rq_xprt;
1862 conn->cn_flags = flags;
1863 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1867 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1869 conn->cn_session = ses;
1870 list_add(&conn->cn_persession, &ses->se_conns);
1873 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1875 struct nfs4_client *clp = ses->se_client;
1877 spin_lock(&clp->cl_lock);
1878 __nfsd4_hash_conn(conn, ses);
1879 spin_unlock(&clp->cl_lock);
1882 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1884 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1885 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1888 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1892 nfsd4_hash_conn(conn, ses);
1893 ret = nfsd4_register_conn(conn);
1895 /* oops; xprt is already down: */
1896 nfsd4_conn_lost(&conn->cn_xpt_user);
1897 /* We may have gained or lost a callback channel: */
1898 nfsd4_probe_callback_sync(ses->se_client);
1901 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1903 u32 dir = NFS4_CDFC4_FORE;
1905 if (cses->flags & SESSION4_BACK_CHAN)
1906 dir |= NFS4_CDFC4_BACK;
1907 return alloc_conn(rqstp, dir);
1910 /* must be called under client_lock */
1911 static void nfsd4_del_conns(struct nfsd4_session *s)
1913 struct nfs4_client *clp = s->se_client;
1914 struct nfsd4_conn *c;
1916 spin_lock(&clp->cl_lock);
1917 while (!list_empty(&s->se_conns)) {
1918 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1919 list_del_init(&c->cn_persession);
1920 spin_unlock(&clp->cl_lock);
1922 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1925 spin_lock(&clp->cl_lock);
1927 spin_unlock(&clp->cl_lock);
1930 static void __free_session(struct nfsd4_session *ses)
1932 free_session_slots(ses);
1936 static void free_session(struct nfsd4_session *ses)
1938 nfsd4_del_conns(ses);
1939 nfsd4_put_drc_mem(&ses->se_fchannel);
1940 __free_session(ses);
1943 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1946 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1948 new->se_client = clp;
1951 INIT_LIST_HEAD(&new->se_conns);
1953 new->se_cb_seq_nr = 1;
1954 new->se_flags = cses->flags;
1955 new->se_cb_prog = cses->callback_prog;
1956 new->se_cb_sec = cses->cb_sec;
1957 atomic_set(&new->se_ref, 0);
1958 idx = hash_sessionid(&new->se_sessionid);
1959 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1960 spin_lock(&clp->cl_lock);
1961 list_add(&new->se_perclnt, &clp->cl_sessions);
1962 spin_unlock(&clp->cl_lock);
1965 struct sockaddr *sa = svc_addr(rqstp);
1967 * This is a little silly; with sessions there's no real
1968 * use for the callback address. Use the peer address
1969 * as a reasonable default for now, but consider fixing
1970 * the rpc client not to require an address in the
1973 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1974 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1978 /* caller must hold client_lock */
1979 static struct nfsd4_session *
1980 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1982 struct nfsd4_session *elem;
1984 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1986 lockdep_assert_held(&nn->client_lock);
1988 dump_sessionid(__func__, sessionid);
1989 idx = hash_sessionid(sessionid);
1990 /* Search in the appropriate list */
1991 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1992 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1993 NFS4_MAX_SESSIONID_LEN)) {
1998 dprintk("%s: session not found\n", __func__);
2002 static struct nfsd4_session *
2003 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2006 struct nfsd4_session *session;
2007 __be32 status = nfserr_badsession;
2009 session = __find_in_sessionid_hashtbl(sessionid, net);
2012 status = nfsd4_get_session_locked(session);
2020 /* caller must hold client_lock */
2022 unhash_session(struct nfsd4_session *ses)
2024 struct nfs4_client *clp = ses->se_client;
2025 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2027 lockdep_assert_held(&nn->client_lock);
2029 list_del(&ses->se_hash);
2030 spin_lock(&ses->se_client->cl_lock);
2031 list_del(&ses->se_perclnt);
2032 spin_unlock(&ses->se_client->cl_lock);
2035 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2037 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2040 * We're assuming the clid was not given out from a boot
2041 * precisely 2^32 (about 136 years) before this one. That seems
2042 * a safe assumption:
2044 if (clid->cl_boot == (u32)nn->boot_time)
2046 trace_nfsd_clid_stale(clid);
2051 * XXX Should we use a slab cache ?
2052 * This type of memory management is somewhat inefficient, but we use it
2053 * anyway since SETCLIENTID is not a common operation.
2055 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2056 struct nfsd_net *nn)
2058 struct nfs4_client *clp;
2061 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
2062 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2065 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2068 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2069 if (clp->cl_name.data == NULL)
2071 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2072 sizeof(struct list_head),
2074 if (!clp->cl_ownerstr_hashtbl)
2075 goto err_no_hashtbl;
2076 for (i = 0; i < OWNER_HASH_SIZE; i++)
2077 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2078 INIT_LIST_HEAD(&clp->cl_sessions);
2079 idr_init(&clp->cl_stateids);
2080 atomic_set(&clp->cl_rpc_users, 0);
2081 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2082 clp->cl_state = NFSD4_ACTIVE;
2083 atomic_inc(&nn->nfs4_client_count);
2084 atomic_set(&clp->cl_delegs_in_recall, 0);
2085 INIT_LIST_HEAD(&clp->cl_idhash);
2086 INIT_LIST_HEAD(&clp->cl_openowners);
2087 INIT_LIST_HEAD(&clp->cl_delegations);
2088 INIT_LIST_HEAD(&clp->cl_lru);
2089 INIT_LIST_HEAD(&clp->cl_revoked);
2090 #ifdef CONFIG_NFSD_PNFS
2091 INIT_LIST_HEAD(&clp->cl_lo_states);
2093 INIT_LIST_HEAD(&clp->async_copies);
2094 spin_lock_init(&clp->async_lock);
2095 spin_lock_init(&clp->cl_lock);
2096 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2099 kfree(clp->cl_name.data);
2101 kmem_cache_free(client_slab, clp);
2105 static void __free_client(struct kref *k)
2107 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2108 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2110 free_svc_cred(&clp->cl_cred);
2111 kfree(clp->cl_ownerstr_hashtbl);
2112 kfree(clp->cl_name.data);
2113 kfree(clp->cl_nii_domain.data);
2114 kfree(clp->cl_nii_name.data);
2115 idr_destroy(&clp->cl_stateids);
2116 kmem_cache_free(client_slab, clp);
2119 static void drop_client(struct nfs4_client *clp)
2121 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2125 free_client(struct nfs4_client *clp)
2127 while (!list_empty(&clp->cl_sessions)) {
2128 struct nfsd4_session *ses;
2129 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2131 list_del(&ses->se_perclnt);
2132 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2135 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2136 if (clp->cl_nfsd_dentry) {
2137 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2138 clp->cl_nfsd_dentry = NULL;
2139 wake_up_all(&expiry_wq);
2144 /* must be called under the client_lock */
2146 unhash_client_locked(struct nfs4_client *clp)
2148 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2149 struct nfsd4_session *ses;
2151 lockdep_assert_held(&nn->client_lock);
2153 /* Mark the client as expired! */
2155 /* Make it invisible */
2156 if (!list_empty(&clp->cl_idhash)) {
2157 list_del_init(&clp->cl_idhash);
2158 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2159 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2161 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2163 list_del_init(&clp->cl_lru);
2164 spin_lock(&clp->cl_lock);
2165 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2166 list_del_init(&ses->se_hash);
2167 spin_unlock(&clp->cl_lock);
2171 unhash_client(struct nfs4_client *clp)
2173 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2175 spin_lock(&nn->client_lock);
2176 unhash_client_locked(clp);
2177 spin_unlock(&nn->client_lock);
2180 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2182 if (atomic_read(&clp->cl_rpc_users))
2183 return nfserr_jukebox;
2184 unhash_client_locked(clp);
2189 __destroy_client(struct nfs4_client *clp)
2191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2193 struct nfs4_openowner *oo;
2194 struct nfs4_delegation *dp;
2195 struct list_head reaplist;
2197 INIT_LIST_HEAD(&reaplist);
2198 spin_lock(&state_lock);
2199 while (!list_empty(&clp->cl_delegations)) {
2200 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2201 WARN_ON(!unhash_delegation_locked(dp));
2202 list_add(&dp->dl_recall_lru, &reaplist);
2204 spin_unlock(&state_lock);
2205 while (!list_empty(&reaplist)) {
2206 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2207 list_del_init(&dp->dl_recall_lru);
2208 destroy_unhashed_deleg(dp);
2210 while (!list_empty(&clp->cl_revoked)) {
2211 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2212 list_del_init(&dp->dl_recall_lru);
2213 nfs4_put_stid(&dp->dl_stid);
2215 while (!list_empty(&clp->cl_openowners)) {
2216 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2217 nfs4_get_stateowner(&oo->oo_owner);
2218 release_openowner(oo);
2220 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2221 struct nfs4_stateowner *so, *tmp;
2223 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2225 /* Should be no openowners at this point */
2226 WARN_ON_ONCE(so->so_is_open_owner);
2227 remove_blocked_locks(lockowner(so));
2230 nfsd4_return_all_client_layouts(clp);
2231 nfsd4_shutdown_copy(clp);
2232 nfsd4_shutdown_callback(clp);
2233 if (clp->cl_cb_conn.cb_xprt)
2234 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2235 atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2237 wake_up_all(&expiry_wq);
2241 destroy_client(struct nfs4_client *clp)
2244 __destroy_client(clp);
2247 static void inc_reclaim_complete(struct nfs4_client *clp)
2249 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2251 if (!nn->track_reclaim_completes)
2253 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2255 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2256 nn->reclaim_str_hashtbl_size) {
2257 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2259 nfsd4_end_grace(nn);
2263 static void expire_client(struct nfs4_client *clp)
2266 nfsd4_client_record_remove(clp);
2267 __destroy_client(clp);
2270 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2272 memcpy(target->cl_verifier.data, source->data,
2273 sizeof(target->cl_verifier.data));
2276 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2278 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2279 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2282 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2284 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2285 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2287 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2288 if ((source->cr_principal && !target->cr_principal) ||
2289 (source->cr_raw_principal && !target->cr_raw_principal) ||
2290 (source->cr_targ_princ && !target->cr_targ_princ))
2293 target->cr_flavor = source->cr_flavor;
2294 target->cr_uid = source->cr_uid;
2295 target->cr_gid = source->cr_gid;
2296 target->cr_group_info = source->cr_group_info;
2297 get_group_info(target->cr_group_info);
2298 target->cr_gss_mech = source->cr_gss_mech;
2299 if (source->cr_gss_mech)
2300 gss_mech_get(source->cr_gss_mech);
2305 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2307 if (o1->len < o2->len)
2309 if (o1->len > o2->len)
2311 return memcmp(o1->data, o2->data, o1->len);
2315 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2317 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2321 same_clid(clientid_t *cl1, clientid_t *cl2)
2323 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2326 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2330 if (g1->ngroups != g2->ngroups)
2332 for (i=0; i<g1->ngroups; i++)
2333 if (!gid_eq(g1->gid[i], g2->gid[i]))
2339 * RFC 3530 language requires clid_inuse be returned when the
2340 * "principal" associated with a requests differs from that previously
2341 * used. We use uid, gid's, and gss principal string as our best
2342 * approximation. We also don't want to allow non-gss use of a client
2343 * established using gss: in theory cr_principal should catch that
2344 * change, but in practice cr_principal can be null even in the gss case
2345 * since gssd doesn't always pass down a principal string.
2347 static bool is_gss_cred(struct svc_cred *cr)
2349 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2350 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2355 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2357 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2358 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2359 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2360 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2362 /* XXX: check that cr_targ_princ fields match ? */
2363 if (cr1->cr_principal == cr2->cr_principal)
2365 if (!cr1->cr_principal || !cr2->cr_principal)
2367 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2370 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2372 struct svc_cred *cr = &rqstp->rq_cred;
2375 if (!cr->cr_gss_mech)
2377 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2378 return service == RPC_GSS_SVC_INTEGRITY ||
2379 service == RPC_GSS_SVC_PRIVACY;
2382 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2384 struct svc_cred *cr = &rqstp->rq_cred;
2386 if (!cl->cl_mach_cred)
2388 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2390 if (!svc_rqst_integrity_protected(rqstp))
2392 if (cl->cl_cred.cr_raw_principal)
2393 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2394 cr->cr_raw_principal);
2395 if (!cr->cr_principal)
2397 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2400 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2405 * This is opaque to client, so no need to byte-swap. Use
2406 * __force to keep sparse happy
2408 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2409 verf[1] = (__force __be32)nn->clverifier_counter++;
2410 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2413 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2415 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2416 clp->cl_clientid.cl_id = nn->clientid_counter++;
2417 gen_confirm(clp, nn);
2420 static struct nfs4_stid *
2421 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2423 struct nfs4_stid *ret;
2425 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2426 if (!ret || !ret->sc_type)
2431 static struct nfs4_stid *
2432 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2434 struct nfs4_stid *s;
2436 spin_lock(&cl->cl_lock);
2437 s = find_stateid_locked(cl, t);
2439 if (typemask & s->sc_type)
2440 refcount_inc(&s->sc_count);
2444 spin_unlock(&cl->cl_lock);
2448 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2450 struct nfsdfs_client *nc;
2451 nc = get_nfsdfs_client(inode);
2454 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2457 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2459 seq_printf(m, "\"");
2460 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2461 seq_printf(m, "\"");
2464 static const char *cb_state2str(int state)
2469 case NFSD4_CB_UNKNOWN:
2473 case NFSD4_CB_FAULT:
2479 static int client_info_show(struct seq_file *m, void *v)
2481 struct inode *inode = m->private;
2482 struct nfs4_client *clp;
2485 clp = get_nfsdfs_clp(inode);
2488 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2489 seq_printf(m, "clientid: 0x%llx\n", clid);
2490 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2492 if (clp->cl_state == NFSD4_COURTESY)
2493 seq_puts(m, "status: courtesy\n");
2494 else if (clp->cl_state == NFSD4_EXPIRABLE)
2495 seq_puts(m, "status: expirable\n");
2496 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2497 seq_puts(m, "status: confirmed\n");
2499 seq_puts(m, "status: unconfirmed\n");
2500 seq_printf(m, "seconds from last renew: %lld\n",
2501 ktime_get_boottime_seconds() - clp->cl_time);
2502 seq_printf(m, "name: ");
2503 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2504 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2505 if (clp->cl_nii_domain.data) {
2506 seq_printf(m, "Implementation domain: ");
2507 seq_quote_mem(m, clp->cl_nii_domain.data,
2508 clp->cl_nii_domain.len);
2509 seq_printf(m, "\nImplementation name: ");
2510 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2511 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2512 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2514 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2515 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
2521 static int client_info_open(struct inode *inode, struct file *file)
2523 return single_open(file, client_info_show, inode);
2526 static const struct file_operations client_info_fops = {
2527 .open = client_info_open,
2529 .llseek = seq_lseek,
2530 .release = single_release,
2533 static void *states_start(struct seq_file *s, loff_t *pos)
2534 __acquires(&clp->cl_lock)
2536 struct nfs4_client *clp = s->private;
2537 unsigned long id = *pos;
2540 spin_lock(&clp->cl_lock);
2541 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2546 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2548 struct nfs4_client *clp = s->private;
2549 unsigned long id = *pos;
2554 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2559 static void states_stop(struct seq_file *s, void *v)
2560 __releases(&clp->cl_lock)
2562 struct nfs4_client *clp = s->private;
2564 spin_unlock(&clp->cl_lock);
2567 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2569 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2572 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2574 struct inode *inode = file_inode(f->nf_file);
2576 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2577 MAJOR(inode->i_sb->s_dev),
2578 MINOR(inode->i_sb->s_dev),
2582 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2584 seq_printf(s, "owner: ");
2585 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2588 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2590 seq_printf(s, "0x%.8x", stid->si_generation);
2591 seq_printf(s, "%12phN", &stid->si_opaque);
2594 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2596 struct nfs4_ol_stateid *ols;
2597 struct nfs4_file *nf;
2598 struct nfsd_file *file;
2599 struct nfs4_stateowner *oo;
2600 unsigned int access, deny;
2602 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2603 return 0; /* XXX: or SEQ_SKIP? */
2604 ols = openlockstateid(st);
2605 oo = ols->st_stateowner;
2607 file = find_any_file(nf);
2611 seq_printf(s, "- ");
2612 nfs4_show_stateid(s, &st->sc_stateid);
2613 seq_printf(s, ": { type: open, ");
2615 access = bmap_to_share_mode(ols->st_access_bmap);
2616 deny = bmap_to_share_mode(ols->st_deny_bmap);
2618 seq_printf(s, "access: %s%s, ",
2619 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2620 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2621 seq_printf(s, "deny: %s%s, ",
2622 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2623 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2625 nfs4_show_superblock(s, file);
2626 seq_printf(s, ", ");
2627 nfs4_show_fname(s, file);
2628 seq_printf(s, ", ");
2629 nfs4_show_owner(s, oo);
2630 seq_printf(s, " }\n");
2631 nfsd_file_put(file);
2636 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2638 struct nfs4_ol_stateid *ols;
2639 struct nfs4_file *nf;
2640 struct nfsd_file *file;
2641 struct nfs4_stateowner *oo;
2643 ols = openlockstateid(st);
2644 oo = ols->st_stateowner;
2646 file = find_any_file(nf);
2650 seq_printf(s, "- ");
2651 nfs4_show_stateid(s, &st->sc_stateid);
2652 seq_printf(s, ": { type: lock, ");
2655 * Note: a lock stateid isn't really the same thing as a lock,
2656 * it's the locking state held by one owner on a file, and there
2657 * may be multiple (or no) lock ranges associated with it.
2658 * (Same for the matter is true of open stateids.)
2661 nfs4_show_superblock(s, file);
2662 /* XXX: open stateid? */
2663 seq_printf(s, ", ");
2664 nfs4_show_fname(s, file);
2665 seq_printf(s, ", ");
2666 nfs4_show_owner(s, oo);
2667 seq_printf(s, " }\n");
2668 nfsd_file_put(file);
2673 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2675 struct nfs4_delegation *ds;
2676 struct nfs4_file *nf;
2677 struct nfsd_file *file;
2679 ds = delegstateid(st);
2681 file = find_deleg_file(nf);
2685 seq_printf(s, "- ");
2686 nfs4_show_stateid(s, &st->sc_stateid);
2687 seq_printf(s, ": { type: deleg, ");
2689 /* Kinda dead code as long as we only support read delegs: */
2690 seq_printf(s, "access: %s, ",
2691 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2693 /* XXX: lease time, whether it's being recalled. */
2695 nfs4_show_superblock(s, file);
2696 seq_printf(s, ", ");
2697 nfs4_show_fname(s, file);
2698 seq_printf(s, " }\n");
2699 nfsd_file_put(file);
2704 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2706 struct nfs4_layout_stateid *ls;
2707 struct nfsd_file *file;
2709 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2712 seq_printf(s, "- ");
2713 nfs4_show_stateid(s, &st->sc_stateid);
2714 seq_printf(s, ": { type: layout, ");
2716 /* XXX: What else would be useful? */
2718 nfs4_show_superblock(s, file);
2719 seq_printf(s, ", ");
2720 nfs4_show_fname(s, file);
2721 seq_printf(s, " }\n");
2726 static int states_show(struct seq_file *s, void *v)
2728 struct nfs4_stid *st = v;
2730 switch (st->sc_type) {
2731 case NFS4_OPEN_STID:
2732 return nfs4_show_open(s, st);
2733 case NFS4_LOCK_STID:
2734 return nfs4_show_lock(s, st);
2735 case NFS4_DELEG_STID:
2736 return nfs4_show_deleg(s, st);
2737 case NFS4_LAYOUT_STID:
2738 return nfs4_show_layout(s, st);
2740 return 0; /* XXX: or SEQ_SKIP? */
2742 /* XXX: copy stateids? */
2745 static struct seq_operations states_seq_ops = {
2746 .start = states_start,
2747 .next = states_next,
2748 .stop = states_stop,
2752 static int client_states_open(struct inode *inode, struct file *file)
2755 struct nfs4_client *clp;
2758 clp = get_nfsdfs_clp(inode);
2762 ret = seq_open(file, &states_seq_ops);
2765 s = file->private_data;
2770 static int client_opens_release(struct inode *inode, struct file *file)
2772 struct seq_file *m = file->private_data;
2773 struct nfs4_client *clp = m->private;
2775 /* XXX: alternatively, we could get/drop in seq start/stop */
2780 static const struct file_operations client_states_fops = {
2781 .open = client_states_open,
2783 .llseek = seq_lseek,
2784 .release = client_opens_release,
2788 * Normally we refuse to destroy clients that are in use, but here the
2789 * administrator is telling us to just do it. We also want to wait
2790 * so the caller has a guarantee that the client's locks are gone by
2791 * the time the write returns:
2793 static void force_expire_client(struct nfs4_client *clp)
2795 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2796 bool already_expired;
2798 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2800 spin_lock(&nn->client_lock);
2802 spin_unlock(&nn->client_lock);
2804 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2805 spin_lock(&nn->client_lock);
2806 already_expired = list_empty(&clp->cl_lru);
2807 if (!already_expired)
2808 unhash_client_locked(clp);
2809 spin_unlock(&nn->client_lock);
2811 if (!already_expired)
2814 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2817 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2818 size_t size, loff_t *pos)
2821 struct nfs4_client *clp;
2823 data = simple_transaction_get(file, buf, size);
2825 return PTR_ERR(data);
2826 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2828 clp = get_nfsdfs_clp(file_inode(file));
2831 force_expire_client(clp);
2836 static const struct file_operations client_ctl_fops = {
2837 .write = client_ctl_write,
2838 .release = simple_transaction_release,
2841 static const struct tree_descr client_files[] = {
2842 [0] = {"info", &client_info_fops, S_IRUSR},
2843 [1] = {"states", &client_states_fops, S_IRUSR},
2844 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
2848 static struct nfs4_client *create_client(struct xdr_netobj name,
2849 struct svc_rqst *rqstp, nfs4_verifier *verf)
2851 struct nfs4_client *clp;
2852 struct sockaddr *sa = svc_addr(rqstp);
2854 struct net *net = SVC_NET(rqstp);
2855 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2856 struct dentry *dentries[ARRAY_SIZE(client_files)];
2858 clp = alloc_client(name, nn);
2862 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2868 kref_init(&clp->cl_nfsdfs.cl_ref);
2869 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2870 clp->cl_time = ktime_get_boottime_seconds();
2871 clear_bit(0, &clp->cl_cb_slot_busy);
2872 copy_verf(clp, verf);
2873 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2874 clp->cl_cb_session = NULL;
2876 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2877 nn, &clp->cl_nfsdfs,
2878 clp->cl_clientid.cl_id - nn->clientid_base,
2879 client_files, dentries);
2880 clp->cl_nfsd_info_dentry = dentries[0];
2881 if (!clp->cl_nfsd_dentry) {
2889 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2891 struct rb_node **new = &(root->rb_node), *parent = NULL;
2892 struct nfs4_client *clp;
2895 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2898 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2899 new = &((*new)->rb_left);
2901 new = &((*new)->rb_right);
2904 rb_link_node(&new_clp->cl_namenode, parent, new);
2905 rb_insert_color(&new_clp->cl_namenode, root);
2908 static struct nfs4_client *
2909 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2912 struct rb_node *node = root->rb_node;
2913 struct nfs4_client *clp;
2916 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2917 cmp = compare_blob(&clp->cl_name, name);
2919 node = node->rb_left;
2921 node = node->rb_right;
2929 add_to_unconfirmed(struct nfs4_client *clp)
2931 unsigned int idhashval;
2932 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2934 lockdep_assert_held(&nn->client_lock);
2936 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2937 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2938 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2939 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2940 renew_client_locked(clp);
2944 move_to_confirmed(struct nfs4_client *clp)
2946 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2947 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2949 lockdep_assert_held(&nn->client_lock);
2951 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2952 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2953 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2954 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2955 trace_nfsd_clid_confirmed(&clp->cl_clientid);
2956 renew_client_locked(clp);
2959 static struct nfs4_client *
2960 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2962 struct nfs4_client *clp;
2963 unsigned int idhashval = clientid_hashval(clid->cl_id);
2965 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2966 if (same_clid(&clp->cl_clientid, clid)) {
2967 if ((bool)clp->cl_minorversion != sessions)
2969 renew_client_locked(clp);
2976 static struct nfs4_client *
2977 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2979 struct list_head *tbl = nn->conf_id_hashtbl;
2981 lockdep_assert_held(&nn->client_lock);
2982 return find_client_in_id_table(tbl, clid, sessions);
2985 static struct nfs4_client *
2986 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2988 struct list_head *tbl = nn->unconf_id_hashtbl;
2990 lockdep_assert_held(&nn->client_lock);
2991 return find_client_in_id_table(tbl, clid, sessions);
2994 static bool clp_used_exchangeid(struct nfs4_client *clp)
2996 return clp->cl_exchange_flags != 0;
2999 static struct nfs4_client *
3000 find_confirmed_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->conf_name_tree);
3006 static struct nfs4_client *
3007 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3009 lockdep_assert_held(&nn->client_lock);
3010 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3014 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3016 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3017 struct sockaddr *sa = svc_addr(rqstp);
3018 u32 scopeid = rpc_get_scope_id(sa);
3019 unsigned short expected_family;
3021 /* Currently, we only support tcp and tcp6 for the callback channel */
3022 if (se->se_callback_netid_len == 3 &&
3023 !memcmp(se->se_callback_netid_val, "tcp", 3))
3024 expected_family = AF_INET;
3025 else if (se->se_callback_netid_len == 4 &&
3026 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3027 expected_family = AF_INET6;
3031 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3032 se->se_callback_addr_len,
3033 (struct sockaddr *)&conn->cb_addr,
3034 sizeof(conn->cb_addr));
3036 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3039 if (conn->cb_addr.ss_family == AF_INET6)
3040 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3042 conn->cb_prog = se->se_callback_prog;
3043 conn->cb_ident = se->se_callback_ident;
3044 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3045 trace_nfsd_cb_args(clp, conn);
3048 conn->cb_addr.ss_family = AF_UNSPEC;
3049 conn->cb_addrlen = 0;
3050 trace_nfsd_cb_nodelegs(clp);
3055 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3058 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3060 struct xdr_buf *buf = resp->xdr->buf;
3061 struct nfsd4_slot *slot = resp->cstate.slot;
3064 dprintk("--> %s slot %p\n", __func__, slot);
3066 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3067 slot->sl_opcnt = resp->opcnt;
3068 slot->sl_status = resp->cstate.status;
3069 free_svc_cred(&slot->sl_cred);
3070 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3072 if (!nfsd4_cache_this(resp)) {
3073 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3076 slot->sl_flags |= NFSD4_SLOT_CACHED;
3078 base = resp->cstate.data_offset;
3079 slot->sl_datalen = buf->len - base;
3080 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3081 WARN(1, "%s: sessions DRC could not cache compound\n",
3087 * Encode the replay sequence operation from the slot values.
3088 * If cachethis is FALSE encode the uncached rep error on the next
3089 * operation which sets resp->p and increments resp->opcnt for
3090 * nfs4svc_encode_compoundres.
3094 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3095 struct nfsd4_compoundres *resp)
3097 struct nfsd4_op *op;
3098 struct nfsd4_slot *slot = resp->cstate.slot;
3100 /* Encode the replayed sequence operation */
3101 op = &args->ops[resp->opcnt - 1];
3102 nfsd4_encode_operation(resp, op);
3104 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3106 if (args->opcnt == 1) {
3108 * The original operation wasn't a solo sequence--we
3109 * always cache those--so this retry must not match the
3112 op->status = nfserr_seq_false_retry;
3114 op = &args->ops[resp->opcnt++];
3115 op->status = nfserr_retry_uncached_rep;
3116 nfsd4_encode_operation(resp, op);
3122 * The sequence operation is not cached because we can use the slot and
3126 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3127 struct nfsd4_sequence *seq)
3129 struct nfsd4_slot *slot = resp->cstate.slot;
3130 struct xdr_stream *xdr = resp->xdr;
3134 dprintk("--> %s slot %p\n", __func__, slot);
3136 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3140 p = xdr_reserve_space(xdr, slot->sl_datalen);
3143 return nfserr_serverfault;
3145 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3146 xdr_commit_encode(xdr);
3148 resp->opcnt = slot->sl_opcnt;
3149 return slot->sl_status;
3153 * Set the exchange_id flags returned by the server.
3156 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3158 #ifdef CONFIG_NFSD_PNFS
3159 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3161 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3164 /* Referrals are supported, Migration is not. */
3165 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3167 /* set the wire flags to return to client. */
3168 clid->flags = new->cl_exchange_flags;
3171 static bool client_has_openowners(struct nfs4_client *clp)
3173 struct nfs4_openowner *oo;
3175 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3176 if (!list_empty(&oo->oo_owner.so_stateids))
3182 static bool client_has_state(struct nfs4_client *clp)
3184 return client_has_openowners(clp)
3185 #ifdef CONFIG_NFSD_PNFS
3186 || !list_empty(&clp->cl_lo_states)
3188 || !list_empty(&clp->cl_delegations)
3189 || !list_empty(&clp->cl_sessions)
3190 || !list_empty(&clp->async_copies);
3193 static __be32 copy_impl_id(struct nfs4_client *clp,
3194 struct nfsd4_exchange_id *exid)
3196 if (!exid->nii_domain.data)
3198 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3199 if (!clp->cl_nii_domain.data)
3200 return nfserr_jukebox;
3201 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3202 if (!clp->cl_nii_name.data)
3203 return nfserr_jukebox;
3204 clp->cl_nii_time = exid->nii_time;
3209 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3210 union nfsd4_op_u *u)
3212 struct nfsd4_exchange_id *exid = &u->exchange_id;
3213 struct nfs4_client *conf, *new;
3214 struct nfs4_client *unconf = NULL;
3216 char addr_str[INET6_ADDRSTRLEN];
3217 nfs4_verifier verf = exid->verifier;
3218 struct sockaddr *sa = svc_addr(rqstp);
3219 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3220 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3222 rpc_ntop(sa, addr_str, sizeof(addr_str));
3223 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3224 "ip_addr=%s flags %x, spa_how %u\n",
3225 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3226 addr_str, exid->flags, exid->spa_how);
3228 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3229 return nfserr_inval;
3231 new = create_client(exid->clname, rqstp, &verf);
3233 return nfserr_jukebox;
3234 status = copy_impl_id(new, exid);
3238 switch (exid->spa_how) {
3240 exid->spo_must_enforce[0] = 0;
3241 exid->spo_must_enforce[1] = (
3242 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3243 1 << (OP_EXCHANGE_ID - 32) |
3244 1 << (OP_CREATE_SESSION - 32) |
3245 1 << (OP_DESTROY_SESSION - 32) |
3246 1 << (OP_DESTROY_CLIENTID - 32));
3248 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3249 1 << (OP_OPEN_DOWNGRADE) |
3251 1 << (OP_DELEGRETURN));
3253 exid->spo_must_allow[1] &= (
3254 1 << (OP_TEST_STATEID - 32) |
3255 1 << (OP_FREE_STATEID - 32));
3256 if (!svc_rqst_integrity_protected(rqstp)) {
3257 status = nfserr_inval;
3261 * Sometimes userspace doesn't give us a principal.
3262 * Which is a bug, really. Anyway, we can't enforce
3263 * MACH_CRED in that case, better to give up now:
3265 if (!new->cl_cred.cr_principal &&
3266 !new->cl_cred.cr_raw_principal) {
3267 status = nfserr_serverfault;
3270 new->cl_mach_cred = true;
3274 default: /* checked by xdr code */
3278 status = nfserr_encr_alg_unsupp;
3282 /* Cases below refer to rfc 5661 section 18.35.4: */
3283 spin_lock(&nn->client_lock);
3284 conf = find_confirmed_client_by_name(&exid->clname, nn);
3286 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3287 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3290 if (!clp_used_exchangeid(conf)) { /* buggy client */
3291 status = nfserr_inval;
3294 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3295 status = nfserr_wrong_cred;
3298 if (!creds_match) { /* case 9 */
3299 status = nfserr_perm;
3302 if (!verfs_match) { /* case 8 */
3303 status = nfserr_not_same;
3307 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3308 trace_nfsd_clid_confirmed_r(conf);
3311 if (!creds_match) { /* case 3 */
3312 if (client_has_state(conf)) {
3313 status = nfserr_clid_inuse;
3314 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3319 if (verfs_match) { /* case 2 */
3320 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3321 trace_nfsd_clid_confirmed_r(conf);
3324 /* case 5, client reboot */
3325 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3330 if (update) { /* case 7 */
3331 status = nfserr_noent;
3335 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3336 if (unconf) /* case 4, possible retry or client restart */
3337 unhash_client_locked(unconf);
3339 /* case 1, new owner ID */
3340 trace_nfsd_clid_fresh(new);
3344 status = mark_client_expired_locked(conf);
3347 trace_nfsd_clid_replaced(&conf->cl_clientid);
3349 new->cl_minorversion = cstate->minorversion;
3350 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3351 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3353 add_to_unconfirmed(new);
3356 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3357 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3359 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3360 nfsd4_set_ex_flags(conf, exid);
3362 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3363 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3367 spin_unlock(&nn->client_lock);
3372 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3373 expire_client(unconf);
3379 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3381 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3384 /* The slot is in use, and no response has been sent. */
3386 if (seqid == slot_seqid)
3387 return nfserr_jukebox;
3389 return nfserr_seq_misordered;
3391 /* Note unsigned 32-bit arithmetic handles wraparound: */
3392 if (likely(seqid == slot_seqid + 1))
3394 if (seqid == slot_seqid)
3395 return nfserr_replay_cache;
3396 return nfserr_seq_misordered;
3400 * Cache the create session result into the create session single DRC
3401 * slot cache by saving the xdr structure. sl_seqid has been set.
3402 * Do this for solo or embedded create session operations.
3405 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3406 struct nfsd4_clid_slot *slot, __be32 nfserr)
3408 slot->sl_status = nfserr;
3409 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3413 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3414 struct nfsd4_clid_slot *slot)
3416 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3417 return slot->sl_status;
3420 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3421 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3422 1 + /* MIN tag is length with zero, only length */ \
3423 3 + /* version, opcount, opcode */ \
3424 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3425 /* seqid, slotID, slotID, cache */ \
3426 4 ) * sizeof(__be32))
3428 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3429 2 + /* verifier: AUTH_NULL, length 0 */\
3431 1 + /* MIN tag is length with zero, only length */ \
3432 3 + /* opcount, opcode, opstatus*/ \
3433 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3434 /* seqid, slotID, slotID, slotID, status */ \
3435 5 ) * sizeof(__be32))
3437 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3439 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3441 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3442 return nfserr_toosmall;
3443 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3444 return nfserr_toosmall;
3445 ca->headerpadsz = 0;
3446 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3447 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3448 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3449 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3450 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3451 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3453 * Note decreasing slot size below client's request may make it
3454 * difficult for client to function correctly, whereas
3455 * decreasing the number of slots will (just?) affect
3456 * performance. When short on memory we therefore prefer to
3457 * decrease number of slots instead of their size. Clients that
3458 * request larger slots than they need will get poor results:
3459 * Note that we always allow at least one slot, because our
3460 * accounting is soft and provides no guarantees either way.
3462 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3468 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3469 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3471 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3472 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3474 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3475 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3477 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3478 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3479 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3480 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3483 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3485 ca->headerpadsz = 0;
3487 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3488 return nfserr_toosmall;
3489 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3490 return nfserr_toosmall;
3491 ca->maxresp_cached = 0;
3493 return nfserr_toosmall;
3498 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3500 switch (cbs->flavor) {
3506 * GSS case: the spec doesn't allow us to return this
3507 * error. But it also doesn't allow us not to support
3509 * I'd rather this fail hard than return some error the
3510 * client might think it can already handle:
3512 return nfserr_encr_alg_unsupp;
3517 nfsd4_create_session(struct svc_rqst *rqstp,
3518 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3520 struct nfsd4_create_session *cr_ses = &u->create_session;
3521 struct sockaddr *sa = svc_addr(rqstp);
3522 struct nfs4_client *conf, *unconf;
3523 struct nfs4_client *old = NULL;
3524 struct nfsd4_session *new;
3525 struct nfsd4_conn *conn;
3526 struct nfsd4_clid_slot *cs_slot = NULL;
3528 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3530 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3531 return nfserr_inval;
3532 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3535 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3538 status = check_backchannel_attrs(&cr_ses->back_channel);
3540 goto out_release_drc_mem;
3541 status = nfserr_jukebox;
3542 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3544 goto out_release_drc_mem;
3545 conn = alloc_conn_from_crses(rqstp, cr_ses);
3547 goto out_free_session;
3549 spin_lock(&nn->client_lock);
3550 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3551 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3552 WARN_ON_ONCE(conf && unconf);
3555 status = nfserr_wrong_cred;
3556 if (!nfsd4_mach_creds_match(conf, rqstp))
3558 cs_slot = &conf->cl_cs_slot;
3559 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3561 if (status == nfserr_replay_cache)
3562 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3565 } else if (unconf) {
3566 status = nfserr_clid_inuse;
3567 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3568 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3569 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
3572 status = nfserr_wrong_cred;
3573 if (!nfsd4_mach_creds_match(unconf, rqstp))
3575 cs_slot = &unconf->cl_cs_slot;
3576 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3578 /* an unconfirmed replay returns misordered */
3579 status = nfserr_seq_misordered;
3582 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3584 status = mark_client_expired_locked(old);
3589 trace_nfsd_clid_replaced(&old->cl_clientid);
3591 move_to_confirmed(unconf);
3594 status = nfserr_stale_clientid;
3598 /* Persistent sessions are not supported */
3599 cr_ses->flags &= ~SESSION4_PERSIST;
3600 /* Upshifting from TCP to RDMA is not supported */
3601 cr_ses->flags &= ~SESSION4_RDMA;
3603 init_session(rqstp, new, conf, cr_ses);
3604 nfsd4_get_session_locked(new);
3606 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3607 NFS4_MAX_SESSIONID_LEN);
3608 cs_slot->sl_seqid++;
3609 cr_ses->seqid = cs_slot->sl_seqid;
3611 /* cache solo and embedded create sessions under the client_lock */
3612 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3613 spin_unlock(&nn->client_lock);
3615 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3616 /* init connection and backchannel */
3617 nfsd4_init_conn(rqstp, conn, new);
3618 nfsd4_put_session(new);
3623 spin_unlock(&nn->client_lock);
3628 __free_session(new);
3629 out_release_drc_mem:
3630 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3634 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3637 case NFS4_CDFC4_FORE:
3638 case NFS4_CDFC4_BACK:
3640 case NFS4_CDFC4_FORE_OR_BOTH:
3641 case NFS4_CDFC4_BACK_OR_BOTH:
3642 *dir = NFS4_CDFC4_BOTH;
3645 return nfserr_inval;
3648 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3649 struct nfsd4_compound_state *cstate,
3650 union nfsd4_op_u *u)
3652 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3653 struct nfsd4_session *session = cstate->session;
3654 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3657 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3660 spin_lock(&nn->client_lock);
3661 session->se_cb_prog = bc->bc_cb_program;
3662 session->se_cb_sec = bc->bc_cb_sec;
3663 spin_unlock(&nn->client_lock);
3665 nfsd4_probe_callback(session->se_client);
3670 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3672 struct nfsd4_conn *c;
3674 list_for_each_entry(c, &s->se_conns, cn_persession) {
3675 if (c->cn_xprt == xpt) {
3682 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3683 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3685 struct nfs4_client *clp = session->se_client;
3686 struct svc_xprt *xpt = rqst->rq_xprt;
3687 struct nfsd4_conn *c;
3690 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3691 spin_lock(&clp->cl_lock);
3692 c = __nfsd4_find_conn(xpt, session);
3694 status = nfserr_noent;
3695 else if (req == c->cn_flags)
3697 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3698 c->cn_flags != NFS4_CDFC4_BACK)
3700 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3701 c->cn_flags != NFS4_CDFC4_FORE)
3704 status = nfserr_inval;
3705 spin_unlock(&clp->cl_lock);
3706 if (status == nfs_ok && conn)
3711 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3712 struct nfsd4_compound_state *cstate,
3713 union nfsd4_op_u *u)
3715 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3717 struct nfsd4_conn *conn;
3718 struct nfsd4_session *session;
3719 struct net *net = SVC_NET(rqstp);
3720 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3722 if (!nfsd4_last_compound_op(rqstp))
3723 return nfserr_not_only_op;
3724 spin_lock(&nn->client_lock);
3725 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3726 spin_unlock(&nn->client_lock);
3728 goto out_no_session;
3729 status = nfserr_wrong_cred;
3730 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3732 status = nfsd4_match_existing_connection(rqstp, session,
3734 if (status == nfs_ok) {
3735 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3736 bcts->dir == NFS4_CDFC4_BACK)
3737 conn->cn_flags |= NFS4_CDFC4_BACK;
3738 nfsd4_probe_callback(session->se_client);
3741 if (status == nfserr_inval)
3743 status = nfsd4_map_bcts_dir(&bcts->dir);
3746 conn = alloc_conn(rqstp, bcts->dir);
3747 status = nfserr_jukebox;
3750 nfsd4_init_conn(rqstp, conn, session);
3753 nfsd4_put_session(session);
3758 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3760 if (!cstate->session)
3762 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3766 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3767 union nfsd4_op_u *u)
3769 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3770 struct nfsd4_session *ses;
3772 int ref_held_by_me = 0;
3773 struct net *net = SVC_NET(r);
3774 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3776 status = nfserr_not_only_op;
3777 if (nfsd4_compound_in_session(cstate, sessionid)) {
3778 if (!nfsd4_last_compound_op(r))
3782 dump_sessionid(__func__, sessionid);
3783 spin_lock(&nn->client_lock);
3784 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3786 goto out_client_lock;
3787 status = nfserr_wrong_cred;
3788 if (!nfsd4_mach_creds_match(ses->se_client, r))
3789 goto out_put_session;
3790 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3792 goto out_put_session;
3793 unhash_session(ses);
3794 spin_unlock(&nn->client_lock);
3796 nfsd4_probe_callback_sync(ses->se_client);
3798 spin_lock(&nn->client_lock);
3801 nfsd4_put_session_locked(ses);
3803 spin_unlock(&nn->client_lock);
3808 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3810 struct nfs4_client *clp = ses->se_client;
3811 struct nfsd4_conn *c;
3812 __be32 status = nfs_ok;
3815 spin_lock(&clp->cl_lock);
3816 c = __nfsd4_find_conn(new->cn_xprt, ses);
3819 status = nfserr_conn_not_bound_to_session;
3820 if (clp->cl_mach_cred)
3822 __nfsd4_hash_conn(new, ses);
3823 spin_unlock(&clp->cl_lock);
3824 ret = nfsd4_register_conn(new);
3826 /* oops; xprt is already down: */
3827 nfsd4_conn_lost(&new->cn_xpt_user);
3830 spin_unlock(&clp->cl_lock);
3835 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3837 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3839 return args->opcnt > session->se_fchannel.maxops;
3842 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3843 struct nfsd4_session *session)
3845 struct xdr_buf *xb = &rqstp->rq_arg;
3847 return xb->len > session->se_fchannel.maxreq_sz;
3850 static bool replay_matches_cache(struct svc_rqst *rqstp,
3851 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3853 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3855 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3856 (bool)seq->cachethis)
3859 * If there's an error then the reply can have fewer ops than
3862 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3865 * But if we cached a reply with *more* ops than the call you're
3866 * sending us now, then this new call is clearly not really a
3867 * replay of the old one:
3869 if (slot->sl_opcnt > argp->opcnt)
3871 /* This is the only check explicitly called by spec: */
3872 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3875 * There may be more comparisons we could actually do, but the
3876 * spec doesn't require us to catch every case where the calls
3877 * don't match (that would require caching the call as well as
3878 * the reply), so we don't bother.
3884 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3885 union nfsd4_op_u *u)
3887 struct nfsd4_sequence *seq = &u->sequence;
3888 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3889 struct xdr_stream *xdr = resp->xdr;
3890 struct nfsd4_session *session;
3891 struct nfs4_client *clp;
3892 struct nfsd4_slot *slot;
3893 struct nfsd4_conn *conn;
3896 struct net *net = SVC_NET(rqstp);
3897 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3899 if (resp->opcnt != 1)
3900 return nfserr_sequence_pos;
3903 * Will be either used or freed by nfsd4_sequence_check_conn
3906 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3908 return nfserr_jukebox;
3910 spin_lock(&nn->client_lock);
3911 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3913 goto out_no_session;
3914 clp = session->se_client;
3916 status = nfserr_too_many_ops;
3917 if (nfsd4_session_too_many_ops(rqstp, session))
3918 goto out_put_session;
3920 status = nfserr_req_too_big;
3921 if (nfsd4_request_too_big(rqstp, session))
3922 goto out_put_session;
3924 status = nfserr_badslot;
3925 if (seq->slotid >= session->se_fchannel.maxreqs)
3926 goto out_put_session;
3928 slot = session->se_slots[seq->slotid];
3929 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3931 /* We do not negotiate the number of slots yet, so set the
3932 * maxslots to the session maxreqs which is used to encode
3933 * sr_highest_slotid and the sr_target_slot id to maxslots */
3934 seq->maxslots = session->se_fchannel.maxreqs;
3936 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3937 slot->sl_flags & NFSD4_SLOT_INUSE);
3938 if (status == nfserr_replay_cache) {
3939 status = nfserr_seq_misordered;
3940 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3941 goto out_put_session;
3942 status = nfserr_seq_false_retry;
3943 if (!replay_matches_cache(rqstp, seq, slot))
3944 goto out_put_session;
3945 cstate->slot = slot;
3946 cstate->session = session;
3948 /* Return the cached reply status and set cstate->status
3949 * for nfsd4_proc_compound processing */
3950 status = nfsd4_replay_cache_entry(resp, seq);
3951 cstate->status = nfserr_replay_cache;
3955 goto out_put_session;
3957 status = nfsd4_sequence_check_conn(conn, session);
3960 goto out_put_session;
3962 buflen = (seq->cachethis) ?
3963 session->se_fchannel.maxresp_cached :
3964 session->se_fchannel.maxresp_sz;
3965 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3967 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3968 goto out_put_session;
3969 svc_reserve(rqstp, buflen);
3972 /* Success! bump slot seqid */
3973 slot->sl_seqid = seq->seqid;
3974 slot->sl_flags |= NFSD4_SLOT_INUSE;
3976 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3978 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3980 cstate->slot = slot;
3981 cstate->session = session;
3985 switch (clp->cl_cb_state) {
3987 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3989 case NFSD4_CB_FAULT:
3990 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3993 seq->status_flags = 0;
3995 if (!list_empty(&clp->cl_revoked))
3996 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4000 spin_unlock(&nn->client_lock);
4003 nfsd4_put_session_locked(session);
4004 goto out_no_session;
4008 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4010 struct nfsd4_compound_state *cs = &resp->cstate;
4012 if (nfsd4_has_session(cs)) {
4013 if (cs->status != nfserr_replay_cache) {
4014 nfsd4_store_cache_entry(resp);
4015 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4017 /* Drop session reference that was taken in nfsd4_sequence() */
4018 nfsd4_put_session(cs->session);
4020 put_client_renew(cs->clp);
4024 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4025 struct nfsd4_compound_state *cstate,
4026 union nfsd4_op_u *u)
4028 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4029 struct nfs4_client *conf, *unconf;
4030 struct nfs4_client *clp = NULL;
4032 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4034 spin_lock(&nn->client_lock);
4035 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4036 conf = find_confirmed_client(&dc->clientid, true, nn);
4037 WARN_ON_ONCE(conf && unconf);
4040 if (client_has_state(conf)) {
4041 status = nfserr_clientid_busy;
4044 status = mark_client_expired_locked(conf);
4051 status = nfserr_stale_clientid;
4054 if (!nfsd4_mach_creds_match(clp, rqstp)) {
4056 status = nfserr_wrong_cred;
4059 trace_nfsd_clid_destroyed(&clp->cl_clientid);
4060 unhash_client_locked(clp);
4062 spin_unlock(&nn->client_lock);
4069 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4070 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4072 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4073 struct nfs4_client *clp = cstate->clp;
4076 if (rc->rca_one_fs) {
4077 if (!cstate->current_fh.fh_dentry)
4078 return nfserr_nofilehandle;
4080 * We don't take advantage of the rca_one_fs case.
4081 * That's OK, it's optional, we can safely ignore it.
4086 status = nfserr_complete_already;
4087 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4090 status = nfserr_stale_clientid;
4091 if (is_client_expired(clp))
4093 * The following error isn't really legal.
4094 * But we only get here if the client just explicitly
4095 * destroyed the client. Surely it no longer cares what
4096 * error it gets back on an operation for the dead
4102 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4103 nfsd4_client_record_create(clp);
4104 inc_reclaim_complete(clp);
4110 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4111 union nfsd4_op_u *u)
4113 struct nfsd4_setclientid *setclid = &u->setclientid;
4114 struct xdr_netobj clname = setclid->se_name;
4115 nfs4_verifier clverifier = setclid->se_verf;
4116 struct nfs4_client *conf, *new;
4117 struct nfs4_client *unconf = NULL;
4119 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4121 new = create_client(clname, rqstp, &clverifier);
4123 return nfserr_jukebox;
4124 spin_lock(&nn->client_lock);
4125 conf = find_confirmed_client_by_name(&clname, nn);
4126 if (conf && client_has_state(conf)) {
4127 status = nfserr_clid_inuse;
4128 if (clp_used_exchangeid(conf))
4130 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4131 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4135 unconf = find_unconfirmed_client_by_name(&clname, nn);
4137 unhash_client_locked(unconf);
4139 if (same_verf(&conf->cl_verifier, &clverifier)) {
4140 copy_clid(new, conf);
4141 gen_confirm(new, nn);
4143 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4146 trace_nfsd_clid_fresh(new);
4147 new->cl_minorversion = 0;
4148 gen_callback(new, setclid, rqstp);
4149 add_to_unconfirmed(new);
4150 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4151 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4152 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4156 spin_unlock(&nn->client_lock);
4160 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4161 expire_client(unconf);
4167 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4168 struct nfsd4_compound_state *cstate,
4169 union nfsd4_op_u *u)
4171 struct nfsd4_setclientid_confirm *setclientid_confirm =
4172 &u->setclientid_confirm;
4173 struct nfs4_client *conf, *unconf;
4174 struct nfs4_client *old = NULL;
4175 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4176 clientid_t * clid = &setclientid_confirm->sc_clientid;
4178 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4180 if (STALE_CLIENTID(clid, nn))
4181 return nfserr_stale_clientid;
4183 spin_lock(&nn->client_lock);
4184 conf = find_confirmed_client(clid, false, nn);
4185 unconf = find_unconfirmed_client(clid, false, nn);
4187 * We try hard to give out unique clientid's, so if we get an
4188 * attempt to confirm the same clientid with a different cred,
4189 * the client may be buggy; this should never happen.
4191 * Nevertheless, RFC 7530 recommends INUSE for this case:
4193 status = nfserr_clid_inuse;
4194 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4195 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4198 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4199 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4202 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4203 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4206 status = nfserr_stale_clientid;
4212 unhash_client_locked(old);
4213 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4215 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4217 status = nfserr_clid_inuse;
4218 if (client_has_state(old)
4219 && !same_creds(&unconf->cl_cred,
4224 status = mark_client_expired_locked(old);
4229 trace_nfsd_clid_replaced(&old->cl_clientid);
4231 move_to_confirmed(unconf);
4234 get_client_locked(conf);
4235 spin_unlock(&nn->client_lock);
4237 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4238 nfsd4_probe_callback(conf);
4239 spin_lock(&nn->client_lock);
4240 put_client_renew_locked(conf);
4242 spin_unlock(&nn->client_lock);
4248 static struct nfs4_file *nfsd4_alloc_file(void)
4250 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4253 /* OPEN Share state helper functions */
4254 static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval,
4255 struct nfs4_file *fp)
4257 lockdep_assert_held(&state_lock);
4259 refcount_set(&fp->fi_ref, 1);
4260 spin_lock_init(&fp->fi_lock);
4261 INIT_LIST_HEAD(&fp->fi_stateids);
4262 INIT_LIST_HEAD(&fp->fi_delegations);
4263 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4264 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4265 fp->fi_deleg_file = NULL;
4266 fp->fi_had_conflict = false;
4267 fp->fi_share_deny = 0;
4268 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4269 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4270 fp->fi_aliased = false;
4271 fp->fi_inode = d_inode(fh->fh_dentry);
4272 #ifdef CONFIG_NFSD_PNFS
4273 INIT_LIST_HEAD(&fp->fi_lo_states);
4274 atomic_set(&fp->fi_lo_recalls, 0);
4276 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
4280 nfsd4_free_slabs(void)
4282 kmem_cache_destroy(client_slab);
4283 kmem_cache_destroy(openowner_slab);
4284 kmem_cache_destroy(lockowner_slab);
4285 kmem_cache_destroy(file_slab);
4286 kmem_cache_destroy(stateid_slab);
4287 kmem_cache_destroy(deleg_slab);
4288 kmem_cache_destroy(odstate_slab);
4292 nfsd4_init_slabs(void)
4294 client_slab = kmem_cache_create("nfsd4_clients",
4295 sizeof(struct nfs4_client), 0, 0, NULL);
4296 if (client_slab == NULL)
4298 openowner_slab = kmem_cache_create("nfsd4_openowners",
4299 sizeof(struct nfs4_openowner), 0, 0, NULL);
4300 if (openowner_slab == NULL)
4301 goto out_free_client_slab;
4302 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4303 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4304 if (lockowner_slab == NULL)
4305 goto out_free_openowner_slab;
4306 file_slab = kmem_cache_create("nfsd4_files",
4307 sizeof(struct nfs4_file), 0, 0, NULL);
4308 if (file_slab == NULL)
4309 goto out_free_lockowner_slab;
4310 stateid_slab = kmem_cache_create("nfsd4_stateids",
4311 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4312 if (stateid_slab == NULL)
4313 goto out_free_file_slab;
4314 deleg_slab = kmem_cache_create("nfsd4_delegations",
4315 sizeof(struct nfs4_delegation), 0, 0, NULL);
4316 if (deleg_slab == NULL)
4317 goto out_free_stateid_slab;
4318 odstate_slab = kmem_cache_create("nfsd4_odstate",
4319 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4320 if (odstate_slab == NULL)
4321 goto out_free_deleg_slab;
4324 out_free_deleg_slab:
4325 kmem_cache_destroy(deleg_slab);
4326 out_free_stateid_slab:
4327 kmem_cache_destroy(stateid_slab);
4329 kmem_cache_destroy(file_slab);
4330 out_free_lockowner_slab:
4331 kmem_cache_destroy(lockowner_slab);
4332 out_free_openowner_slab:
4333 kmem_cache_destroy(openowner_slab);
4334 out_free_client_slab:
4335 kmem_cache_destroy(client_slab);
4340 void nfsd4_init_leases_net(struct nfsd_net *nn)
4345 nn->nfsd4_lease = 90; /* default lease time */
4346 nn->nfsd4_grace = 90;
4347 nn->somebody_reclaimed = false;
4348 nn->track_reclaim_completes = false;
4349 nn->clverifier_counter = prandom_u32();
4350 nn->clientid_base = prandom_u32();
4351 nn->clientid_counter = nn->clientid_base + 1;
4352 nn->s2s_cp_cl_id = nn->clientid_counter++;
4354 atomic_set(&nn->nfs4_client_count, 0);
4356 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4357 max_clients *= NFS4_CLIENTS_PER_GB;
4358 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4361 static void init_nfs4_replay(struct nfs4_replay *rp)
4363 rp->rp_status = nfserr_serverfault;
4365 rp->rp_buf = rp->rp_ibuf;
4366 mutex_init(&rp->rp_mutex);
4369 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4370 struct nfs4_stateowner *so)
4372 if (!nfsd4_has_session(cstate)) {
4373 mutex_lock(&so->so_replay.rp_mutex);
4374 cstate->replay_owner = nfs4_get_stateowner(so);
4378 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4380 struct nfs4_stateowner *so = cstate->replay_owner;
4383 cstate->replay_owner = NULL;
4384 mutex_unlock(&so->so_replay.rp_mutex);
4385 nfs4_put_stateowner(so);
4389 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4391 struct nfs4_stateowner *sop;
4393 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4397 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4398 if (!sop->so_owner.data) {
4399 kmem_cache_free(slab, sop);
4403 INIT_LIST_HEAD(&sop->so_stateids);
4404 sop->so_client = clp;
4405 init_nfs4_replay(&sop->so_replay);
4406 atomic_set(&sop->so_count, 1);
4410 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4412 lockdep_assert_held(&clp->cl_lock);
4414 list_add(&oo->oo_owner.so_strhash,
4415 &clp->cl_ownerstr_hashtbl[strhashval]);
4416 list_add(&oo->oo_perclient, &clp->cl_openowners);
4419 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4421 unhash_openowner_locked(openowner(so));
4424 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4426 struct nfs4_openowner *oo = openowner(so);
4428 kmem_cache_free(openowner_slab, oo);
4431 static const struct nfs4_stateowner_operations openowner_ops = {
4432 .so_unhash = nfs4_unhash_openowner,
4433 .so_free = nfs4_free_openowner,
4436 static struct nfs4_ol_stateid *
4437 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4439 struct nfs4_ol_stateid *local, *ret = NULL;
4440 struct nfs4_openowner *oo = open->op_openowner;
4442 lockdep_assert_held(&fp->fi_lock);
4444 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4445 /* ignore lock owners */
4446 if (local->st_stateowner->so_is_open_owner == 0)
4448 if (local->st_stateowner != &oo->oo_owner)
4450 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4452 refcount_inc(&ret->st_stid.sc_count);
4460 nfsd4_verify_open_stid(struct nfs4_stid *s)
4462 __be32 ret = nfs_ok;
4464 switch (s->sc_type) {
4468 case NFS4_CLOSED_STID:
4469 case NFS4_CLOSED_DELEG_STID:
4470 ret = nfserr_bad_stateid;
4472 case NFS4_REVOKED_DELEG_STID:
4473 ret = nfserr_deleg_revoked;
4478 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4480 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4484 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4485 ret = nfsd4_verify_open_stid(&stp->st_stid);
4487 mutex_unlock(&stp->st_mutex);
4491 static struct nfs4_ol_stateid *
4492 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4494 struct nfs4_ol_stateid *stp;
4496 spin_lock(&fp->fi_lock);
4497 stp = nfsd4_find_existing_open(fp, open);
4498 spin_unlock(&fp->fi_lock);
4499 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4501 nfs4_put_stid(&stp->st_stid);
4506 static struct nfs4_openowner *
4507 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4508 struct nfsd4_compound_state *cstate)
4510 struct nfs4_client *clp = cstate->clp;
4511 struct nfs4_openowner *oo, *ret;
4513 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4516 oo->oo_owner.so_ops = &openowner_ops;
4517 oo->oo_owner.so_is_open_owner = 1;
4518 oo->oo_owner.so_seqid = open->op_seqid;
4520 if (nfsd4_has_session(cstate))
4521 oo->oo_flags |= NFS4_OO_CONFIRMED;
4523 oo->oo_last_closed_stid = NULL;
4524 INIT_LIST_HEAD(&oo->oo_close_lru);
4525 spin_lock(&clp->cl_lock);
4526 ret = find_openstateowner_str_locked(strhashval, open, clp);
4528 hash_openowner(oo, clp, strhashval);
4531 nfs4_free_stateowner(&oo->oo_owner);
4533 spin_unlock(&clp->cl_lock);
4537 static struct nfs4_ol_stateid *
4538 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4541 struct nfs4_openowner *oo = open->op_openowner;
4542 struct nfs4_ol_stateid *retstp = NULL;
4543 struct nfs4_ol_stateid *stp;
4546 /* We are moving these outside of the spinlocks to avoid the warnings */
4547 mutex_init(&stp->st_mutex);
4548 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4551 spin_lock(&oo->oo_owner.so_client->cl_lock);
4552 spin_lock(&fp->fi_lock);
4554 retstp = nfsd4_find_existing_open(fp, open);
4558 open->op_stp = NULL;
4559 refcount_inc(&stp->st_stid.sc_count);
4560 stp->st_stid.sc_type = NFS4_OPEN_STID;
4561 INIT_LIST_HEAD(&stp->st_locks);
4562 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4564 stp->st_stid.sc_file = fp;
4565 stp->st_access_bmap = 0;
4566 stp->st_deny_bmap = 0;
4567 stp->st_openstp = NULL;
4568 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4569 list_add(&stp->st_perfile, &fp->fi_stateids);
4572 spin_unlock(&fp->fi_lock);
4573 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4575 /* Handle races with CLOSE */
4576 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4577 nfs4_put_stid(&retstp->st_stid);
4580 /* To keep mutex tracking happy */
4581 mutex_unlock(&stp->st_mutex);
4588 * In the 4.0 case we need to keep the owners around a little while to handle
4589 * CLOSE replay. We still do need to release any file access that is held by
4590 * them before returning however.
4593 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4595 struct nfs4_ol_stateid *last;
4596 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4597 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4600 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4603 * We know that we hold one reference via nfsd4_close, and another
4604 * "persistent" reference for the client. If the refcount is higher
4605 * than 2, then there are still calls in progress that are using this
4606 * stateid. We can't put the sc_file reference until they are finished.
4607 * Wait for the refcount to drop to 2. Since it has been unhashed,
4608 * there should be no danger of the refcount going back up again at
4611 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4613 release_all_access(s);
4614 if (s->st_stid.sc_file) {
4615 put_nfs4_file(s->st_stid.sc_file);
4616 s->st_stid.sc_file = NULL;
4619 spin_lock(&nn->client_lock);
4620 last = oo->oo_last_closed_stid;
4621 oo->oo_last_closed_stid = s;
4622 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4623 oo->oo_time = ktime_get_boottime_seconds();
4624 spin_unlock(&nn->client_lock);
4626 nfs4_put_stid(&last->st_stid);
4629 /* search file_hashtbl[] for file */
4630 static struct nfs4_file *
4631 find_file_locked(struct svc_fh *fh, unsigned int hashval)
4633 struct nfs4_file *fp;
4635 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4636 lockdep_is_held(&state_lock)) {
4637 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4638 if (refcount_inc_not_zero(&fp->fi_ref))
4645 static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
4646 unsigned int hashval)
4648 struct nfs4_file *fp;
4649 struct nfs4_file *ret = NULL;
4650 bool alias_found = false;
4652 spin_lock(&state_lock);
4653 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4654 lockdep_is_held(&state_lock)) {
4655 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4656 if (refcount_inc_not_zero(&fp->fi_ref))
4658 } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
4659 fp->fi_aliased = alias_found = true;
4661 if (likely(ret == NULL)) {
4662 nfsd4_init_file(fh, hashval, new);
4663 new->fi_aliased = alias_found;
4666 spin_unlock(&state_lock);
4670 static struct nfs4_file * find_file(struct svc_fh *fh)
4672 struct nfs4_file *fp;
4673 unsigned int hashval = file_hashval(fh);
4676 fp = find_file_locked(fh, hashval);
4681 static struct nfs4_file *
4682 find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
4684 struct nfs4_file *fp;
4685 unsigned int hashval = file_hashval(fh);
4688 fp = find_file_locked(fh, hashval);
4693 return insert_file(new, fh, hashval);
4697 * Called to check deny when READ with all zero stateid or
4698 * WRITE with all zero or all one stateid
4701 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4703 struct nfs4_file *fp;
4704 __be32 ret = nfs_ok;
4706 fp = find_file(current_fh);
4709 /* Check for conflicting share reservations */
4710 spin_lock(&fp->fi_lock);
4711 if (fp->fi_share_deny & deny_type)
4712 ret = nfserr_locked;
4713 spin_unlock(&fp->fi_lock);
4718 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4720 struct nfs4_delegation *dp = cb_to_delegation(cb);
4721 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4724 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4727 * We can't do this in nfsd_break_deleg_cb because it is
4728 * already holding inode->i_lock.
4730 * If the dl_time != 0, then we know that it has already been
4731 * queued for a lease break. Don't queue it again.
4733 spin_lock(&state_lock);
4734 if (delegation_hashed(dp) && dp->dl_time == 0) {
4735 dp->dl_time = ktime_get_boottime_seconds();
4736 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4738 spin_unlock(&state_lock);
4741 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4742 struct rpc_task *task)
4744 struct nfs4_delegation *dp = cb_to_delegation(cb);
4746 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4747 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4750 switch (task->tk_status) {
4753 case -NFS4ERR_DELAY:
4754 rpc_delay(task, 2 * HZ);
4757 case -NFS4ERR_BAD_STATEID:
4759 * Race: client probably got cb_recall before open reply
4760 * granting delegation.
4762 if (dp->dl_retries--) {
4763 rpc_delay(task, 2 * HZ);
4772 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4774 struct nfs4_delegation *dp = cb_to_delegation(cb);
4776 nfs4_put_stid(&dp->dl_stid);
4779 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4780 .prepare = nfsd4_cb_recall_prepare,
4781 .done = nfsd4_cb_recall_done,
4782 .release = nfsd4_cb_recall_release,
4785 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4788 * We're assuming the state code never drops its reference
4789 * without first removing the lease. Since we're in this lease
4790 * callback (and since the lease code is serialized by the
4791 * i_lock) we know the server hasn't removed the lease yet, and
4792 * we know it's safe to take a reference.
4794 refcount_inc(&dp->dl_stid.sc_count);
4795 nfsd4_run_cb(&dp->dl_recall);
4798 /* Called from break_lease() with i_lock held. */
4800 nfsd_break_deleg_cb(struct file_lock *fl)
4803 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4804 struct nfs4_file *fp = dp->dl_stid.sc_file;
4805 struct nfs4_client *clp = dp->dl_stid.sc_client;
4806 struct nfsd_net *nn;
4808 trace_nfsd_cb_recall(&dp->dl_stid);
4810 dp->dl_recalled = true;
4811 atomic_inc(&clp->cl_delegs_in_recall);
4812 if (try_to_expire_client(clp)) {
4813 nn = net_generic(clp->net, nfsd_net_id);
4814 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
4818 * We don't want the locks code to timeout the lease for us;
4819 * we'll remove it ourself if a delegation isn't returned
4822 fl->fl_break_time = 0;
4824 spin_lock(&fp->fi_lock);
4825 fp->fi_had_conflict = true;
4826 nfsd_break_one_deleg(dp);
4827 spin_unlock(&fp->fi_lock);
4832 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
4833 * @fl: Lock state to check
4836 * %true: Lease conflict was resolved
4837 * %false: Lease conflict was not resolved.
4839 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4841 struct nfs4_delegation *dl = fl->fl_owner;
4842 struct svc_rqst *rqst;
4843 struct nfs4_client *clp;
4847 rqst = kthread_data(current);
4848 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4849 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
4851 clp = *(rqst->rq_lease_breaker);
4852 return dl->dl_stid.sc_client == clp;
4856 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4857 struct list_head *dispose)
4859 struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
4860 struct nfs4_client *clp = dp->dl_stid.sc_client;
4862 if (arg & F_UNLCK) {
4863 if (dp->dl_recalled)
4864 atomic_dec(&clp->cl_delegs_in_recall);
4865 return lease_modify(onlist, arg, dispose);
4870 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4871 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
4872 .lm_break = nfsd_break_deleg_cb,
4873 .lm_change = nfsd_change_deleg_cb,
4876 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4878 if (nfsd4_has_session(cstate))
4880 if (seqid == so->so_seqid - 1)
4881 return nfserr_replay_me;
4882 if (seqid == so->so_seqid)
4884 return nfserr_bad_seqid;
4887 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4888 struct nfsd_net *nn)
4890 struct nfs4_client *found;
4892 spin_lock(&nn->client_lock);
4893 found = find_confirmed_client(clid, sessions, nn);
4895 atomic_inc(&found->cl_rpc_users);
4896 spin_unlock(&nn->client_lock);
4900 static __be32 set_client(clientid_t *clid,
4901 struct nfsd4_compound_state *cstate,
4902 struct nfsd_net *nn)
4905 if (!same_clid(&cstate->clp->cl_clientid, clid))
4906 return nfserr_stale_clientid;
4909 if (STALE_CLIENTID(clid, nn))
4910 return nfserr_stale_clientid;
4912 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4913 * set cstate->clp), so session = false:
4915 cstate->clp = lookup_clientid(clid, false, nn);
4917 return nfserr_expired;
4922 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4923 struct nfsd4_open *open, struct nfsd_net *nn)
4925 clientid_t *clientid = &open->op_clientid;
4926 struct nfs4_client *clp = NULL;
4927 unsigned int strhashval;
4928 struct nfs4_openowner *oo = NULL;
4932 * In case we need it later, after we've already created the
4933 * file and don't want to risk a further failure:
4935 open->op_file = nfsd4_alloc_file();
4936 if (open->op_file == NULL)
4937 return nfserr_jukebox;
4939 status = set_client(clientid, cstate, nn);
4944 strhashval = ownerstr_hashval(&open->op_owner);
4945 oo = find_openstateowner_str(strhashval, open, clp);
4946 open->op_openowner = oo;
4950 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4951 /* Replace unconfirmed owners without checking for replay. */
4952 release_openowner(oo);
4953 open->op_openowner = NULL;
4956 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4961 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4963 return nfserr_jukebox;
4964 open->op_openowner = oo;
4966 open->op_stp = nfs4_alloc_open_stateid(clp);
4968 return nfserr_jukebox;
4970 if (nfsd4_has_session(cstate) &&
4971 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4972 open->op_odstate = alloc_clnt_odstate(clp);
4973 if (!open->op_odstate)
4974 return nfserr_jukebox;
4980 static inline __be32
4981 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4983 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4984 return nfserr_openmode;
4989 static int share_access_to_flags(u32 share_access)
4991 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4994 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4996 struct nfs4_stid *ret;
4998 ret = find_stateid_by_type(cl, s,
4999 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
5002 return delegstateid(ret);
5005 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5007 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5008 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5012 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5013 struct nfs4_delegation **dp)
5016 __be32 status = nfserr_bad_stateid;
5017 struct nfs4_delegation *deleg;
5019 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5022 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
5023 nfs4_put_stid(&deleg->dl_stid);
5024 if (cl->cl_minorversion)
5025 status = nfserr_deleg_revoked;
5028 flags = share_access_to_flags(open->op_share_access);
5029 status = nfs4_check_delegmode(deleg, flags);
5031 nfs4_put_stid(&deleg->dl_stid);
5036 if (!nfsd4_is_deleg_cur(open))
5040 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5044 static inline int nfs4_access_to_access(u32 nfs4_access)
5048 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5049 flags |= NFSD_MAY_READ;
5050 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5051 flags |= NFSD_MAY_WRITE;
5055 static inline __be32
5056 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5057 struct nfsd4_open *open)
5059 struct iattr iattr = {
5060 .ia_valid = ATTR_SIZE,
5063 struct nfsd_attrs attrs = {
5066 if (!open->op_truncate)
5068 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5069 return nfserr_inval;
5070 return nfsd_setattr(rqstp, fh, &attrs, 0, (time64_t)0);
5073 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5074 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5075 struct nfsd4_open *open, bool new_stp)
5077 struct nfsd_file *nf = NULL;
5079 int oflag = nfs4_access_to_omode(open->op_share_access);
5080 int access = nfs4_access_to_access(open->op_share_access);
5081 unsigned char old_access_bmap, old_deny_bmap;
5083 spin_lock(&fp->fi_lock);
5086 * Are we trying to set a deny mode that would conflict with
5089 status = nfs4_file_check_deny(fp, open->op_share_deny);
5090 if (status != nfs_ok) {
5091 if (status != nfserr_share_denied) {
5092 spin_unlock(&fp->fi_lock);
5095 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5096 stp, open->op_share_deny, false))
5097 status = nfserr_jukebox;
5098 spin_unlock(&fp->fi_lock);
5102 /* set access to the file */
5103 status = nfs4_file_get_access(fp, open->op_share_access);
5104 if (status != nfs_ok) {
5105 if (status != nfserr_share_denied) {
5106 spin_unlock(&fp->fi_lock);
5109 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5110 stp, open->op_share_access, true))
5111 status = nfserr_jukebox;
5112 spin_unlock(&fp->fi_lock);
5116 /* Set access bits in stateid */
5117 old_access_bmap = stp->st_access_bmap;
5118 set_access(open->op_share_access, stp);
5120 /* Set new deny mask */
5121 old_deny_bmap = stp->st_deny_bmap;
5122 set_deny(open->op_share_deny, stp);
5123 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5125 if (!fp->fi_fds[oflag]) {
5126 spin_unlock(&fp->fi_lock);
5128 if (!open->op_filp) {
5129 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
5130 if (status != nfs_ok)
5131 goto out_put_access;
5133 status = nfsd_file_create(rqstp, cur_fh, access, &nf);
5134 if (status != nfs_ok)
5135 goto out_put_access;
5136 nf->nf_file = open->op_filp;
5137 open->op_filp = NULL;
5138 trace_nfsd_file_create(rqstp, access, nf);
5141 spin_lock(&fp->fi_lock);
5142 if (!fp->fi_fds[oflag]) {
5143 fp->fi_fds[oflag] = nf;
5147 spin_unlock(&fp->fi_lock);
5151 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5154 goto out_put_access;
5156 status = nfsd4_truncate(rqstp, cur_fh, open);
5158 goto out_put_access;
5162 stp->st_access_bmap = old_access_bmap;
5163 nfs4_file_put_access(fp, open->op_share_access);
5164 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5169 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5170 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5171 struct nfsd4_open *open)
5174 unsigned char old_deny_bmap = stp->st_deny_bmap;
5176 if (!test_access(open->op_share_access, stp))
5177 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5179 /* test and set deny mode */
5180 spin_lock(&fp->fi_lock);
5181 status = nfs4_file_check_deny(fp, open->op_share_deny);
5182 if (status == nfs_ok) {
5183 if (status != nfserr_share_denied) {
5184 set_deny(open->op_share_deny, stp);
5185 fp->fi_share_deny |=
5186 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5188 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5189 stp, open->op_share_deny, false))
5190 status = nfserr_jukebox;
5193 spin_unlock(&fp->fi_lock);
5195 if (status != nfs_ok)
5198 status = nfsd4_truncate(rqstp, cur_fh, open);
5199 if (status != nfs_ok)
5200 reset_union_bmap_deny(old_deny_bmap, stp);
5204 /* Should we give out recallable state?: */
5205 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5207 if (clp->cl_cb_state == NFSD4_CB_UP)
5210 * In the sessions case, since we don't have to establish a
5211 * separate connection for callbacks, we assume it's OK
5212 * until we hear otherwise:
5214 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5217 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5220 struct file_lock *fl;
5222 fl = locks_alloc_lock();
5225 fl->fl_lmops = &nfsd_lease_mng_ops;
5226 fl->fl_flags = FL_DELEG;
5227 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5228 fl->fl_end = OFFSET_MAX;
5229 fl->fl_owner = (fl_owner_t)dp;
5230 fl->fl_pid = current->tgid;
5231 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5235 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5236 struct nfs4_file *fp)
5238 struct nfs4_ol_stateid *st;
5239 struct file *f = fp->fi_deleg_file->nf_file;
5240 struct inode *ino = locks_inode(f);
5243 writes = atomic_read(&ino->i_writecount);
5247 * There could be multiple filehandles (hence multiple
5248 * nfs4_files) referencing this file, but that's not too
5249 * common; let's just give up in that case rather than
5250 * trying to go look up all the clients using that other
5251 * nfs4_file as well:
5256 * If there's a close in progress, make sure that we see it
5257 * clear any fi_fds[] entries before we see it decrement
5260 smp_mb__after_atomic();
5262 if (fp->fi_fds[O_WRONLY])
5264 if (fp->fi_fds[O_RDWR])
5267 return -EAGAIN; /* There may be non-NFSv4 writers */
5269 * It's possible there are non-NFSv4 write opens in progress,
5270 * but if they haven't incremented i_writecount yet then they
5271 * also haven't called break lease yet; so, they'll break this
5272 * lease soon enough. So, all that's left to check for is NFSv4
5275 spin_lock(&fp->fi_lock);
5276 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5277 if (st->st_openstp == NULL /* it's an open */ &&
5278 access_permit_write(st) &&
5279 st->st_stid.sc_client != clp) {
5280 spin_unlock(&fp->fi_lock);
5284 spin_unlock(&fp->fi_lock);
5286 * There's a small chance that we could be racing with another
5287 * NFSv4 open. However, any open that hasn't added itself to
5288 * the fi_stateids list also hasn't called break_lease yet; so,
5289 * they'll break this lease soon enough.
5295 * It's possible that between opening the dentry and setting the delegation,
5296 * that it has been renamed or unlinked. Redo the lookup to verify that this
5300 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5301 struct svc_fh *parent)
5303 struct svc_export *exp;
5304 struct dentry *child;
5307 err = nfsd_lookup_dentry(open->op_rqstp, parent,
5308 open->op_fname, open->op_fnamelen,
5315 if (child != file_dentry(fp->fi_deleg_file->nf_file))
5321 static struct nfs4_delegation *
5322 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5323 struct svc_fh *parent)
5326 struct nfs4_client *clp = stp->st_stid.sc_client;
5327 struct nfs4_file *fp = stp->st_stid.sc_file;
5328 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
5329 struct nfs4_delegation *dp;
5330 struct nfsd_file *nf;
5331 struct file_lock *fl;
5334 * The fi_had_conflict and nfs_get_existing_delegation checks
5335 * here are just optimizations; we'll need to recheck them at
5338 if (fp->fi_had_conflict)
5339 return ERR_PTR(-EAGAIN);
5341 nf = find_readable_file(fp);
5344 * We probably could attempt another open and get a read
5345 * delegation, but for now, don't bother until the
5346 * client actually sends us one.
5348 return ERR_PTR(-EAGAIN);
5350 spin_lock(&state_lock);
5351 spin_lock(&fp->fi_lock);
5352 if (nfs4_delegation_exists(clp, fp))
5354 else if (!fp->fi_deleg_file) {
5355 fp->fi_deleg_file = nf;
5356 /* increment early to prevent fi_deleg_file from being
5358 fp->fi_delegees = 1;
5362 spin_unlock(&fp->fi_lock);
5363 spin_unlock(&state_lock);
5367 return ERR_PTR(status);
5370 dp = alloc_init_deleg(clp, fp, odstate);
5374 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5376 goto out_clnt_odstate;
5378 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5380 locks_free_lock(fl);
5382 goto out_clnt_odstate;
5385 status = nfsd4_verify_deleg_dentry(open, fp, parent);
5390 status = nfsd4_check_conflicting_opens(clp, fp);
5394 spin_lock(&state_lock);
5395 spin_lock(&fp->fi_lock);
5396 if (fp->fi_had_conflict)
5399 status = hash_delegation_locked(dp, fp);
5400 spin_unlock(&fp->fi_lock);
5401 spin_unlock(&state_lock);
5408 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5410 put_clnt_odstate(dp->dl_clnt_odstate);
5411 nfs4_put_stid(&dp->dl_stid);
5414 return ERR_PTR(status);
5417 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5419 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5420 if (status == -EAGAIN)
5421 open->op_why_no_deleg = WND4_CONTENTION;
5423 open->op_why_no_deleg = WND4_RESOURCE;
5424 switch (open->op_deleg_want) {
5425 case NFS4_SHARE_WANT_READ_DELEG:
5426 case NFS4_SHARE_WANT_WRITE_DELEG:
5427 case NFS4_SHARE_WANT_ANY_DELEG:
5429 case NFS4_SHARE_WANT_CANCEL:
5430 open->op_why_no_deleg = WND4_CANCELLED;
5432 case NFS4_SHARE_WANT_NO_DELEG:
5439 * Attempt to hand out a delegation.
5441 * Note we don't support write delegations, and won't until the vfs has
5442 * proper support for them.
5445 nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5446 struct svc_fh *currentfh)
5448 struct nfs4_delegation *dp;
5449 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5450 struct nfs4_client *clp = stp->st_stid.sc_client;
5451 struct svc_fh *parent = NULL;
5455 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5456 open->op_recall = 0;
5457 switch (open->op_claim_type) {
5458 case NFS4_OPEN_CLAIM_PREVIOUS:
5460 open->op_recall = 1;
5461 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5464 case NFS4_OPEN_CLAIM_NULL:
5467 case NFS4_OPEN_CLAIM_FH:
5469 * Let's not give out any delegations till everyone's
5470 * had the chance to reclaim theirs, *and* until
5471 * NLM locks have all been reclaimed:
5473 if (locks_in_grace(clp->net))
5475 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5481 dp = nfs4_set_delegation(open, stp, parent);
5485 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5487 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5488 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5489 nfs4_put_stid(&dp->dl_stid);
5492 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5493 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5494 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5495 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5496 open->op_recall = 1;
5499 /* 4.1 client asking for a delegation? */
5500 if (open->op_deleg_want)
5501 nfsd4_open_deleg_none_ext(open, status);
5505 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5506 struct nfs4_delegation *dp)
5508 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5509 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5510 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5511 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5512 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5513 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5514 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5515 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5517 /* Otherwise the client must be confused wanting a delegation
5518 * it already has, therefore we don't return
5519 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5524 * nfsd4_process_open2 - finish open processing
5525 * @rqstp: the RPC transaction being executed
5526 * @current_fh: NFSv4 COMPOUND's current filehandle
5527 * @open: OPEN arguments
5529 * If successful, (1) truncate the file if open->op_truncate was
5530 * set, (2) set open->op_stateid, (3) set open->op_delegation.
5532 * Returns %nfs_ok on success; otherwise an nfs4stat value in
5533 * network byte order is returned.
5536 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5538 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5539 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5540 struct nfs4_file *fp = NULL;
5541 struct nfs4_ol_stateid *stp = NULL;
5542 struct nfs4_delegation *dp = NULL;
5544 bool new_stp = false;
5547 * Lookup file; if found, lookup stateid and check open request,
5548 * and check for delegations in the process of being recalled.
5549 * If not found, create the nfs4_file struct
5551 fp = find_or_add_file(open->op_file, current_fh);
5552 if (fp != open->op_file) {
5553 status = nfs4_check_deleg(cl, open, &dp);
5556 stp = nfsd4_find_and_lock_existing_open(fp, open);
5558 open->op_file = NULL;
5559 status = nfserr_bad_stateid;
5560 if (nfsd4_is_deleg_cur(open))
5565 stp = init_open_stateid(fp, open);
5571 * OPEN the file, or upgrade an existing OPEN.
5572 * If truncate fails, the OPEN fails.
5574 * stp is already locked.
5577 /* Stateid was found, this is an OPEN upgrade */
5578 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5580 mutex_unlock(&stp->st_mutex);
5584 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
5586 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5587 release_open_stateid(stp);
5588 mutex_unlock(&stp->st_mutex);
5592 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5594 if (stp->st_clnt_odstate == open->op_odstate)
5595 open->op_odstate = NULL;
5598 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5599 mutex_unlock(&stp->st_mutex);
5601 if (nfsd4_has_session(&resp->cstate)) {
5602 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5603 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5604 open->op_why_no_deleg = WND4_NOT_WANTED;
5610 * Attempt to hand out a delegation. No error return, because the
5611 * OPEN succeeds even if we fail.
5613 nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
5616 trace_nfsd_open(&stp->st_stid.sc_stateid);
5618 /* 4.1 client trying to upgrade/downgrade delegation? */
5619 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5620 open->op_deleg_want)
5621 nfsd4_deleg_xgrade_none_ext(open, dp);
5625 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5626 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5628 * To finish the open response, we just need to set the rflags.
5630 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5631 if (nfsd4_has_session(&resp->cstate))
5632 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5633 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5634 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5637 nfs4_put_stid(&dp->dl_stid);
5639 nfs4_put_stid(&stp->st_stid);
5644 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5645 struct nfsd4_open *open)
5647 if (open->op_openowner) {
5648 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5650 nfsd4_cstate_assign_replay(cstate, so);
5651 nfs4_put_stateowner(so);
5654 kmem_cache_free(file_slab, open->op_file);
5656 nfs4_put_stid(&open->op_stp->st_stid);
5657 if (open->op_odstate)
5658 kmem_cache_free(odstate_slab, open->op_odstate);
5662 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5663 union nfsd4_op_u *u)
5665 clientid_t *clid = &u->renew;
5666 struct nfs4_client *clp;
5668 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5670 trace_nfsd_clid_renew(clid);
5671 status = set_client(clid, cstate, nn);
5675 if (!list_empty(&clp->cl_delegations)
5676 && clp->cl_cb_state != NFSD4_CB_UP)
5677 return nfserr_cb_path_down;
5682 nfsd4_end_grace(struct nfsd_net *nn)
5684 /* do nothing if grace period already ended */
5685 if (nn->grace_ended)
5688 trace_nfsd_grace_complete(nn);
5689 nn->grace_ended = true;
5691 * If the server goes down again right now, an NFSv4
5692 * client will still be allowed to reclaim after it comes back up,
5693 * even if it hasn't yet had a chance to reclaim state this time.
5696 nfsd4_record_grace_done(nn);
5698 * At this point, NFSv4 clients can still reclaim. But if the
5699 * server crashes, any that have not yet reclaimed will be out
5700 * of luck on the next boot.
5702 * (NFSv4.1+ clients are considered to have reclaimed once they
5703 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5704 * have reclaimed after their first OPEN.)
5706 locks_end_grace(&nn->nfsd4_manager);
5708 * At this point, and once lockd and/or any other containers
5709 * exit their grace period, further reclaims will fail and
5710 * regular locking can resume.
5715 * If we've waited a lease period but there are still clients trying to
5716 * reclaim, wait a little longer to give them a chance to finish.
5718 static bool clients_still_reclaiming(struct nfsd_net *nn)
5720 time64_t double_grace_period_end = nn->boot_time +
5721 2 * nn->nfsd4_lease;
5723 if (nn->track_reclaim_completes &&
5724 atomic_read(&nn->nr_reclaim_complete) ==
5725 nn->reclaim_str_hashtbl_size)
5727 if (!nn->somebody_reclaimed)
5729 nn->somebody_reclaimed = false;
5731 * If we've given them *two* lease times to reclaim, and they're
5732 * still not done, give up:
5734 if (ktime_get_boottime_seconds() > double_grace_period_end)
5739 struct laundry_time {
5744 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5746 time64_t time_remaining;
5748 if (last_refresh < lt->cutoff)
5750 time_remaining = last_refresh - lt->cutoff;
5751 lt->new_timeo = min(lt->new_timeo, time_remaining);
5755 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
5756 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5758 spin_lock_init(&nn->nfsd_ssc_lock);
5759 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5760 init_waitqueue_head(&nn->nfsd_ssc_waitq);
5762 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5765 * This is called when nfsd is being shutdown, after all inter_ssc
5766 * cleanup were done, to destroy the ssc delayed unmount list.
5768 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5770 struct nfsd4_ssc_umount_item *ni = NULL;
5771 struct nfsd4_ssc_umount_item *tmp;
5773 spin_lock(&nn->nfsd_ssc_lock);
5774 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5775 list_del(&ni->nsui_list);
5776 spin_unlock(&nn->nfsd_ssc_lock);
5777 mntput(ni->nsui_vfsmount);
5779 spin_lock(&nn->nfsd_ssc_lock);
5781 spin_unlock(&nn->nfsd_ssc_lock);
5784 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5786 bool do_wakeup = false;
5787 struct nfsd4_ssc_umount_item *ni = NULL;
5788 struct nfsd4_ssc_umount_item *tmp;
5790 spin_lock(&nn->nfsd_ssc_lock);
5791 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5792 if (time_after(jiffies, ni->nsui_expire)) {
5793 if (refcount_read(&ni->nsui_refcnt) > 1)
5796 /* mark being unmount */
5797 ni->nsui_busy = true;
5798 spin_unlock(&nn->nfsd_ssc_lock);
5799 mntput(ni->nsui_vfsmount);
5800 spin_lock(&nn->nfsd_ssc_lock);
5802 /* waiters need to start from begin of list */
5803 list_del(&ni->nsui_list);
5806 /* wakeup ssc_connect waiters */
5813 wake_up_all(&nn->nfsd_ssc_waitq);
5814 spin_unlock(&nn->nfsd_ssc_lock);
5818 /* Check if any lock belonging to this lockowner has any blockers */
5820 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
5822 struct file_lock_context *ctx;
5823 struct nfs4_ol_stateid *stp;
5824 struct nfs4_file *nf;
5826 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
5827 nf = stp->st_stid.sc_file;
5828 ctx = nf->fi_inode->i_flctx;
5831 if (locks_owner_has_blockers(ctx, lo))
5838 nfs4_anylock_blockers(struct nfs4_client *clp)
5841 struct nfs4_stateowner *so;
5842 struct nfs4_lockowner *lo;
5844 if (atomic_read(&clp->cl_delegs_in_recall))
5846 spin_lock(&clp->cl_lock);
5847 for (i = 0; i < OWNER_HASH_SIZE; i++) {
5848 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
5850 if (so->so_is_open_owner)
5853 if (nfs4_lockowner_has_blockers(lo)) {
5854 spin_unlock(&clp->cl_lock);
5859 spin_unlock(&clp->cl_lock);
5864 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
5865 struct laundry_time *lt)
5867 unsigned int maxreap, reapcnt = 0;
5868 struct list_head *pos, *next;
5869 struct nfs4_client *clp;
5871 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
5872 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
5873 INIT_LIST_HEAD(reaplist);
5874 spin_lock(&nn->client_lock);
5875 list_for_each_safe(pos, next, &nn->client_lru) {
5876 clp = list_entry(pos, struct nfs4_client, cl_lru);
5877 if (clp->cl_state == NFSD4_EXPIRABLE)
5879 if (!state_expired(lt, clp->cl_time))
5881 if (!atomic_read(&clp->cl_rpc_users))
5882 clp->cl_state = NFSD4_COURTESY;
5883 if (!client_has_state(clp))
5885 if (!nfs4_anylock_blockers(clp))
5886 if (reapcnt >= maxreap)
5889 if (!mark_client_expired_locked(clp)) {
5890 list_add(&clp->cl_lru, reaplist);
5894 spin_unlock(&nn->client_lock);
5898 nfs4_laundromat(struct nfsd_net *nn)
5900 struct nfs4_client *clp;
5901 struct nfs4_openowner *oo;
5902 struct nfs4_delegation *dp;
5903 struct nfs4_ol_stateid *stp;
5904 struct nfsd4_blocked_lock *nbl;
5905 struct list_head *pos, *next, reaplist;
5906 struct laundry_time lt = {
5907 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
5908 .new_timeo = nn->nfsd4_lease
5910 struct nfs4_cpntf_state *cps;
5911 copy_stateid_t *cps_t;
5914 if (clients_still_reclaiming(nn)) {
5918 nfsd4_end_grace(nn);
5920 spin_lock(&nn->s2s_cp_lock);
5921 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5922 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5923 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
5924 state_expired(<, cps->cpntf_time))
5925 _free_cpntf_state_locked(nn, cps);
5927 spin_unlock(&nn->s2s_cp_lock);
5928 nfs4_get_client_reaplist(nn, &reaplist, <);
5929 list_for_each_safe(pos, next, &reaplist) {
5930 clp = list_entry(pos, struct nfs4_client, cl_lru);
5931 trace_nfsd_clid_purged(&clp->cl_clientid);
5932 list_del_init(&clp->cl_lru);
5935 spin_lock(&state_lock);
5936 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5937 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5938 if (!state_expired(<, dp->dl_time))
5940 WARN_ON(!unhash_delegation_locked(dp));
5941 list_add(&dp->dl_recall_lru, &reaplist);
5943 spin_unlock(&state_lock);
5944 while (!list_empty(&reaplist)) {
5945 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5947 list_del_init(&dp->dl_recall_lru);
5948 revoke_delegation(dp);
5951 spin_lock(&nn->client_lock);
5952 while (!list_empty(&nn->close_lru)) {
5953 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5955 if (!state_expired(<, oo->oo_time))
5957 list_del_init(&oo->oo_close_lru);
5958 stp = oo->oo_last_closed_stid;
5959 oo->oo_last_closed_stid = NULL;
5960 spin_unlock(&nn->client_lock);
5961 nfs4_put_stid(&stp->st_stid);
5962 spin_lock(&nn->client_lock);
5964 spin_unlock(&nn->client_lock);
5967 * It's possible for a client to try and acquire an already held lock
5968 * that is being held for a long time, and then lose interest in it.
5969 * So, we clean out any un-revisited request after a lease period
5970 * under the assumption that the client is no longer interested.
5972 * RFC5661, sec. 9.6 states that the client must not rely on getting
5973 * notifications and must continue to poll for locks, even when the
5974 * server supports them. Thus this shouldn't lead to clients blocking
5975 * indefinitely once the lock does become free.
5977 BUG_ON(!list_empty(&reaplist));
5978 spin_lock(&nn->blocked_locks_lock);
5979 while (!list_empty(&nn->blocked_locks_lru)) {
5980 nbl = list_first_entry(&nn->blocked_locks_lru,
5981 struct nfsd4_blocked_lock, nbl_lru);
5982 if (!state_expired(<, nbl->nbl_time))
5984 list_move(&nbl->nbl_lru, &reaplist);
5985 list_del_init(&nbl->nbl_list);
5987 spin_unlock(&nn->blocked_locks_lock);
5989 while (!list_empty(&reaplist)) {
5990 nbl = list_first_entry(&reaplist,
5991 struct nfsd4_blocked_lock, nbl_lru);
5992 list_del_init(&nbl->nbl_lru);
5993 free_blocked_lock(nbl);
5995 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
5996 /* service the server-to-server copy delayed unmount list */
5997 nfsd4_ssc_expire_umount(nn);
6000 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
6003 static void laundromat_main(struct work_struct *);
6006 laundromat_main(struct work_struct *laundry)
6009 struct delayed_work *dwork = to_delayed_work(laundry);
6010 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6013 t = nfs4_laundromat(nn);
6014 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
6017 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
6019 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
6020 return nfserr_bad_stateid;
6025 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
6027 __be32 status = nfserr_openmode;
6029 /* For lock stateid's, we test the parent open, not the lock: */
6030 if (stp->st_openstp)
6031 stp = stp->st_openstp;
6032 if ((flags & WR_STATE) && !access_permit_write(stp))
6034 if ((flags & RD_STATE) && !access_permit_read(stp))
6041 static inline __be32
6042 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
6044 if (ONE_STATEID(stateid) && (flags & RD_STATE))
6046 else if (opens_in_grace(net)) {
6047 /* Answer in remaining cases depends on existence of
6048 * conflicting state; so we must wait out the grace period. */
6049 return nfserr_grace;
6050 } else if (flags & WR_STATE)
6051 return nfs4_share_conflict(current_fh,
6052 NFS4_SHARE_DENY_WRITE);
6053 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
6054 return nfs4_share_conflict(current_fh,
6055 NFS4_SHARE_DENY_READ);
6058 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
6061 * When sessions are used the stateid generation number is ignored
6064 if (has_session && in->si_generation == 0)
6067 if (in->si_generation == ref->si_generation)
6070 /* If the client sends us a stateid from the future, it's buggy: */
6071 if (nfsd4_stateid_generation_after(in, ref))
6072 return nfserr_bad_stateid;
6074 * However, we could see a stateid from the past, even from a
6075 * non-buggy client. For example, if the client sends a lock
6076 * while some IO is outstanding, the lock may bump si_generation
6077 * while the IO is still in flight. The client could avoid that
6078 * situation by waiting for responses on all the IO requests,
6079 * but better performance may result in retrying IO that
6080 * receives an old_stateid error if requests are rarely
6081 * reordered in flight:
6083 return nfserr_old_stateid;
6086 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
6090 spin_lock(&s->sc_lock);
6091 ret = nfsd4_verify_open_stid(s);
6093 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
6094 spin_unlock(&s->sc_lock);
6098 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6100 if (ols->st_stateowner->so_is_open_owner &&
6101 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6102 return nfserr_bad_stateid;
6106 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
6108 struct nfs4_stid *s;
6109 __be32 status = nfserr_bad_stateid;
6111 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6112 CLOSE_STATEID(stateid))
6114 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
6116 spin_lock(&cl->cl_lock);
6117 s = find_stateid_locked(cl, stateid);
6120 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
6123 switch (s->sc_type) {
6124 case NFS4_DELEG_STID:
6127 case NFS4_REVOKED_DELEG_STID:
6128 status = nfserr_deleg_revoked;
6130 case NFS4_OPEN_STID:
6131 case NFS4_LOCK_STID:
6132 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
6135 printk("unknown stateid type %x\n", s->sc_type);
6137 case NFS4_CLOSED_STID:
6138 case NFS4_CLOSED_DELEG_STID:
6139 status = nfserr_bad_stateid;
6142 spin_unlock(&cl->cl_lock);
6147 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6148 stateid_t *stateid, unsigned char typemask,
6149 struct nfs4_stid **s, struct nfsd_net *nn)
6152 bool return_revoked = false;
6155 * only return revoked delegations if explicitly asked.
6156 * otherwise we report revoked or bad_stateid status.
6158 if (typemask & NFS4_REVOKED_DELEG_STID)
6159 return_revoked = true;
6160 else if (typemask & NFS4_DELEG_STID)
6161 typemask |= NFS4_REVOKED_DELEG_STID;
6163 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6164 CLOSE_STATEID(stateid))
6165 return nfserr_bad_stateid;
6166 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
6167 if (status == nfserr_stale_clientid) {
6168 if (cstate->session)
6169 return nfserr_bad_stateid;
6170 return nfserr_stale_stateid;
6174 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
6176 return nfserr_bad_stateid;
6177 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
6179 if (cstate->minorversion)
6180 return nfserr_deleg_revoked;
6181 return nfserr_bad_stateid;
6186 static struct nfsd_file *
6187 nfs4_find_file(struct nfs4_stid *s, int flags)
6192 switch (s->sc_type) {
6193 case NFS4_DELEG_STID:
6194 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
6196 return nfsd_file_get(s->sc_file->fi_deleg_file);
6197 case NFS4_OPEN_STID:
6198 case NFS4_LOCK_STID:
6199 if (flags & RD_STATE)
6200 return find_readable_file(s->sc_file);
6202 return find_writeable_file(s->sc_file);
6209 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
6213 status = nfsd4_check_openowner_confirmed(ols);
6216 return nfs4_check_openmode(ols, flags);
6220 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
6221 struct nfsd_file **nfp, int flags)
6223 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6224 struct nfsd_file *nf;
6227 nf = nfs4_find_file(s, flags);
6229 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6230 acc | NFSD_MAY_OWNER_OVERRIDE);
6236 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
6245 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6247 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
6248 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
6250 list_del(&cps->cp_list);
6251 idr_remove(&nn->s2s_cp_stateids,
6252 cps->cp_stateid.stid.si_opaque.so_id);
6256 * A READ from an inter server to server COPY will have a
6257 * copy stateid. Look up the copy notify stateid from the
6258 * idr structure and take a reference on it.
6260 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6261 struct nfs4_client *clp,
6262 struct nfs4_cpntf_state **cps)
6264 copy_stateid_t *cps_t;
6265 struct nfs4_cpntf_state *state = NULL;
6267 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6268 return nfserr_bad_stateid;
6269 spin_lock(&nn->s2s_cp_lock);
6270 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6272 state = container_of(cps_t, struct nfs4_cpntf_state,
6274 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
6279 refcount_inc(&state->cp_stateid.sc_count);
6281 _free_cpntf_state_locked(nn, state);
6284 spin_unlock(&nn->s2s_cp_lock);
6286 return nfserr_bad_stateid;
6292 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6293 struct nfs4_stid **stid)
6296 struct nfs4_cpntf_state *cps = NULL;
6297 struct nfs4_client *found;
6299 status = manage_cpntf_state(nn, st, NULL, &cps);
6303 cps->cpntf_time = ktime_get_boottime_seconds();
6305 status = nfserr_expired;
6306 found = lookup_clientid(&cps->cp_p_clid, true, nn);
6310 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6311 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6315 status = nfserr_bad_stateid;
6317 put_client_renew(found);
6319 nfs4_put_cpntf_state(nn, cps);
6323 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6325 spin_lock(&nn->s2s_cp_lock);
6326 _free_cpntf_state_locked(nn, cps);
6327 spin_unlock(&nn->s2s_cp_lock);
6331 * Checks for stateid operations
6334 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
6335 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
6336 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6337 struct nfs4_stid **cstid)
6339 struct net *net = SVC_NET(rqstp);
6340 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6341 struct nfs4_stid *s = NULL;
6347 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6349 status = nfserr_bad_stateid;
6351 status = check_special_stateids(net, fhp, stateid,
6356 status = nfsd4_lookup_stateid(cstate, stateid,
6357 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
6359 if (status == nfserr_bad_stateid)
6360 status = find_cpntf_state(nn, stateid, &s);
6363 status = nfsd4_stid_check_stateid_generation(stateid, s,
6364 nfsd4_has_session(cstate));
6368 switch (s->sc_type) {
6369 case NFS4_DELEG_STID:
6370 status = nfs4_check_delegmode(delegstateid(s), flags);
6372 case NFS4_OPEN_STID:
6373 case NFS4_LOCK_STID:
6374 status = nfs4_check_olstateid(openlockstateid(s), flags);
6377 status = nfserr_bad_stateid;
6382 status = nfs4_check_fh(fhp, s);
6385 if (status == nfs_ok && nfp)
6386 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
6389 if (!status && cstid)
6398 * Test if the stateid is valid
6401 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6402 union nfsd4_op_u *u)
6404 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
6405 struct nfsd4_test_stateid_id *stateid;
6406 struct nfs4_client *cl = cstate->clp;
6408 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
6409 stateid->ts_id_status =
6410 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
6416 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6418 struct nfs4_ol_stateid *stp = openlockstateid(s);
6421 ret = nfsd4_lock_ol_stateid(stp);
6425 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6429 ret = nfserr_locks_held;
6430 if (check_for_locks(stp->st_stid.sc_file,
6431 lockowner(stp->st_stateowner)))
6434 release_lock_stateid(stp);
6438 mutex_unlock(&stp->st_mutex);
6445 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6446 union nfsd4_op_u *u)
6448 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
6449 stateid_t *stateid = &free_stateid->fr_stateid;
6450 struct nfs4_stid *s;
6451 struct nfs4_delegation *dp;
6452 struct nfs4_client *cl = cstate->clp;
6453 __be32 ret = nfserr_bad_stateid;
6455 spin_lock(&cl->cl_lock);
6456 s = find_stateid_locked(cl, stateid);
6459 spin_lock(&s->sc_lock);
6460 switch (s->sc_type) {
6461 case NFS4_DELEG_STID:
6462 ret = nfserr_locks_held;
6464 case NFS4_OPEN_STID:
6465 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6468 ret = nfserr_locks_held;
6470 case NFS4_LOCK_STID:
6471 spin_unlock(&s->sc_lock);
6472 refcount_inc(&s->sc_count);
6473 spin_unlock(&cl->cl_lock);
6474 ret = nfsd4_free_lock_stateid(stateid, s);
6476 case NFS4_REVOKED_DELEG_STID:
6477 spin_unlock(&s->sc_lock);
6478 dp = delegstateid(s);
6479 list_del_init(&dp->dl_recall_lru);
6480 spin_unlock(&cl->cl_lock);
6484 /* Default falls through and returns nfserr_bad_stateid */
6486 spin_unlock(&s->sc_lock);
6488 spin_unlock(&cl->cl_lock);
6496 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6497 RD_STATE : WR_STATE;
6500 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6502 struct svc_fh *current_fh = &cstate->current_fh;
6503 struct nfs4_stateowner *sop = stp->st_stateowner;
6506 status = nfsd4_check_seqid(cstate, sop, seqid);
6509 status = nfsd4_lock_ol_stateid(stp);
6510 if (status != nfs_ok)
6512 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6513 if (status == nfs_ok)
6514 status = nfs4_check_fh(current_fh, &stp->st_stid);
6515 if (status != nfs_ok)
6516 mutex_unlock(&stp->st_mutex);
6521 * Checks for sequence id mutating operations.
6524 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6525 stateid_t *stateid, char typemask,
6526 struct nfs4_ol_stateid **stpp,
6527 struct nfsd_net *nn)
6530 struct nfs4_stid *s;
6531 struct nfs4_ol_stateid *stp = NULL;
6533 trace_nfsd_preprocess(seqid, stateid);
6536 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6539 stp = openlockstateid(s);
6540 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6542 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6546 nfs4_put_stid(&stp->st_stid);
6550 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6551 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6554 struct nfs4_openowner *oo;
6555 struct nfs4_ol_stateid *stp;
6557 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6558 NFS4_OPEN_STID, &stp, nn);
6561 oo = openowner(stp->st_stateowner);
6562 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6563 mutex_unlock(&stp->st_mutex);
6564 nfs4_put_stid(&stp->st_stid);
6565 return nfserr_bad_stateid;
6572 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6573 union nfsd4_op_u *u)
6575 struct nfsd4_open_confirm *oc = &u->open_confirm;
6577 struct nfs4_openowner *oo;
6578 struct nfs4_ol_stateid *stp;
6579 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6581 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6582 cstate->current_fh.fh_dentry);
6584 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6588 status = nfs4_preprocess_seqid_op(cstate,
6589 oc->oc_seqid, &oc->oc_req_stateid,
6590 NFS4_OPEN_STID, &stp, nn);
6593 oo = openowner(stp->st_stateowner);
6594 status = nfserr_bad_stateid;
6595 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6596 mutex_unlock(&stp->st_mutex);
6599 oo->oo_flags |= NFS4_OO_CONFIRMED;
6600 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6601 mutex_unlock(&stp->st_mutex);
6602 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6603 nfsd4_client_record_create(oo->oo_owner.so_client);
6606 nfs4_put_stid(&stp->st_stid);
6608 nfsd4_bump_seqid(cstate, status);
6612 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6614 if (!test_access(access, stp))
6616 nfs4_file_put_access(stp->st_stid.sc_file, access);
6617 clear_access(access, stp);
6620 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6622 switch (to_access) {
6623 case NFS4_SHARE_ACCESS_READ:
6624 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6625 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6627 case NFS4_SHARE_ACCESS_WRITE:
6628 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6629 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6631 case NFS4_SHARE_ACCESS_BOTH:
6639 nfsd4_open_downgrade(struct svc_rqst *rqstp,
6640 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
6642 struct nfsd4_open_downgrade *od = &u->open_downgrade;
6644 struct nfs4_ol_stateid *stp;
6645 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6647 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6648 cstate->current_fh.fh_dentry);
6650 /* We don't yet support WANT bits: */
6651 if (od->od_deleg_want)
6652 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6655 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
6656 &od->od_stateid, &stp, nn);
6659 status = nfserr_inval;
6660 if (!test_access(od->od_share_access, stp)) {
6661 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6662 stp->st_access_bmap, od->od_share_access);
6665 if (!test_deny(od->od_share_deny, stp)) {
6666 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6667 stp->st_deny_bmap, od->od_share_deny);
6670 nfs4_stateid_downgrade(stp, od->od_share_access);
6671 reset_union_bmap_deny(od->od_share_deny, stp);
6672 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
6675 mutex_unlock(&stp->st_mutex);
6676 nfs4_put_stid(&stp->st_stid);
6678 nfsd4_bump_seqid(cstate, status);
6682 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6684 struct nfs4_client *clp = s->st_stid.sc_client;
6686 LIST_HEAD(reaplist);
6688 spin_lock(&clp->cl_lock);
6689 unhashed = unhash_open_stateid(s, &reaplist);
6691 if (clp->cl_minorversion) {
6693 put_ol_stateid_locked(s, &reaplist);
6694 spin_unlock(&clp->cl_lock);
6695 free_ol_stateid_reaplist(&reaplist);
6697 spin_unlock(&clp->cl_lock);
6698 free_ol_stateid_reaplist(&reaplist);
6700 move_to_close_lru(s, clp->net);
6705 * nfs4_unlock_state() called after encode
6708 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6709 union nfsd4_op_u *u)
6711 struct nfsd4_close *close = &u->close;
6713 struct nfs4_ol_stateid *stp;
6714 struct net *net = SVC_NET(rqstp);
6715 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6717 dprintk("NFSD: nfsd4_close on file %pd\n",
6718 cstate->current_fh.fh_dentry);
6720 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6722 NFS4_OPEN_STID|NFS4_CLOSED_STID,
6724 nfsd4_bump_seqid(cstate, status);
6728 stp->st_stid.sc_type = NFS4_CLOSED_STID;
6731 * Technically we don't _really_ have to increment or copy it, since
6732 * it should just be gone after this operation and we clobber the
6733 * copied value below, but we continue to do so here just to ensure
6734 * that racing ops see that there was a state change.
6736 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
6738 nfsd4_close_open_stateid(stp);
6739 mutex_unlock(&stp->st_mutex);
6741 /* v4.1+ suggests that we send a special stateid in here, since the
6742 * clients should just ignore this anyway. Since this is not useful
6743 * for v4.0 clients either, we set it to the special close_stateid
6746 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6748 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6750 /* put reference from nfs4_preprocess_seqid_op */
6751 nfs4_put_stid(&stp->st_stid);
6757 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6758 union nfsd4_op_u *u)
6760 struct nfsd4_delegreturn *dr = &u->delegreturn;
6761 struct nfs4_delegation *dp;
6762 stateid_t *stateid = &dr->dr_stateid;
6763 struct nfs4_stid *s;
6765 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6767 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6770 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6773 dp = delegstateid(s);
6774 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6778 destroy_delegation(dp);
6780 nfs4_put_stid(&dp->dl_stid);
6785 /* last octet in a range */
6787 last_byte_offset(u64 start, u64 len)
6793 return end > start ? end - 1: NFS4_MAX_UINT64;
6797 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6798 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6799 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6800 * locking, this prevents us from being completely protocol-compliant. The
6801 * real solution to this problem is to start using unsigned file offsets in
6802 * the VFS, but this is a very deep change!
6805 nfs4_transform_lock_offset(struct file_lock *lock)
6807 if (lock->fl_start < 0)
6808 lock->fl_start = OFFSET_MAX;
6809 if (lock->fl_end < 0)
6810 lock->fl_end = OFFSET_MAX;
6814 nfsd4_lm_get_owner(fl_owner_t owner)
6816 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6818 nfs4_get_stateowner(&lo->lo_owner);
6823 nfsd4_lm_put_owner(fl_owner_t owner)
6825 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6828 nfs4_put_stateowner(&lo->lo_owner);
6831 /* return pointer to struct nfs4_client if client is expirable */
6833 nfsd4_lm_lock_expirable(struct file_lock *cfl)
6835 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
6836 struct nfs4_client *clp = lo->lo_owner.so_client;
6837 struct nfsd_net *nn;
6839 if (try_to_expire_client(clp)) {
6840 nn = net_generic(clp->net, nfsd_net_id);
6841 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
6847 /* schedule laundromat to run immediately and wait for it to complete */
6849 nfsd4_lm_expire_lock(void)
6851 flush_workqueue(laundry_wq);
6855 nfsd4_lm_notify(struct file_lock *fl)
6857 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6858 struct net *net = lo->lo_owner.so_client->net;
6859 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6860 struct nfsd4_blocked_lock *nbl = container_of(fl,
6861 struct nfsd4_blocked_lock, nbl_lock);
6864 /* An empty list means that something else is going to be using it */
6865 spin_lock(&nn->blocked_locks_lock);
6866 if (!list_empty(&nbl->nbl_list)) {
6867 list_del_init(&nbl->nbl_list);
6868 list_del_init(&nbl->nbl_lru);
6871 spin_unlock(&nn->blocked_locks_lock);
6874 trace_nfsd_cb_notify_lock(lo, nbl);
6875 nfsd4_run_cb(&nbl->nbl_cb);
6879 static const struct lock_manager_operations nfsd_posix_mng_ops = {
6880 .lm_mod_owner = THIS_MODULE,
6881 .lm_notify = nfsd4_lm_notify,
6882 .lm_get_owner = nfsd4_lm_get_owner,
6883 .lm_put_owner = nfsd4_lm_put_owner,
6884 .lm_lock_expirable = nfsd4_lm_lock_expirable,
6885 .lm_expire_lock = nfsd4_lm_expire_lock,
6889 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6891 struct nfs4_lockowner *lo;
6893 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
6894 lo = (struct nfs4_lockowner *) fl->fl_owner;
6895 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6897 if (!deny->ld_owner.data)
6898 /* We just don't care that much */
6900 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
6903 deny->ld_owner.len = 0;
6904 deny->ld_owner.data = NULL;
6905 deny->ld_clientid.cl_boot = 0;
6906 deny->ld_clientid.cl_id = 0;
6908 deny->ld_start = fl->fl_start;
6909 deny->ld_length = NFS4_MAX_UINT64;
6910 if (fl->fl_end != NFS4_MAX_UINT64)
6911 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6912 deny->ld_type = NFS4_READ_LT;
6913 if (fl->fl_type != F_RDLCK)
6914 deny->ld_type = NFS4_WRITE_LT;
6917 static struct nfs4_lockowner *
6918 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
6920 unsigned int strhashval = ownerstr_hashval(owner);
6921 struct nfs4_stateowner *so;
6923 lockdep_assert_held(&clp->cl_lock);
6925 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6927 if (so->so_is_open_owner)
6929 if (same_owner_str(so, owner))
6930 return lockowner(nfs4_get_stateowner(so));
6935 static struct nfs4_lockowner *
6936 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
6938 struct nfs4_lockowner *lo;
6940 spin_lock(&clp->cl_lock);
6941 lo = find_lockowner_str_locked(clp, owner);
6942 spin_unlock(&clp->cl_lock);
6946 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6948 unhash_lockowner_locked(lockowner(sop));
6951 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6953 struct nfs4_lockowner *lo = lockowner(sop);
6955 kmem_cache_free(lockowner_slab, lo);
6958 static const struct nfs4_stateowner_operations lockowner_ops = {
6959 .so_unhash = nfs4_unhash_lockowner,
6960 .so_free = nfs4_free_lockowner,
6964 * Alloc a lock owner structure.
6965 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
6968 * strhashval = ownerstr_hashval
6970 static struct nfs4_lockowner *
6971 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6972 struct nfs4_ol_stateid *open_stp,
6973 struct nfsd4_lock *lock)
6975 struct nfs4_lockowner *lo, *ret;
6977 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6980 INIT_LIST_HEAD(&lo->lo_blocked);
6981 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6982 lo->lo_owner.so_is_open_owner = 0;
6983 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6984 lo->lo_owner.so_ops = &lockowner_ops;
6985 spin_lock(&clp->cl_lock);
6986 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
6988 list_add(&lo->lo_owner.so_strhash,
6989 &clp->cl_ownerstr_hashtbl[strhashval]);
6992 nfs4_free_stateowner(&lo->lo_owner);
6994 spin_unlock(&clp->cl_lock);
6998 static struct nfs4_ol_stateid *
6999 find_lock_stateid(const struct nfs4_lockowner *lo,
7000 const struct nfs4_ol_stateid *ost)
7002 struct nfs4_ol_stateid *lst;
7004 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
7006 /* If ost is not hashed, ost->st_locks will not be valid */
7007 if (!nfs4_ol_stateid_unhashed(ost))
7008 list_for_each_entry(lst, &ost->st_locks, st_locks) {
7009 if (lst->st_stateowner == &lo->lo_owner) {
7010 refcount_inc(&lst->st_stid.sc_count);
7017 static struct nfs4_ol_stateid *
7018 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7019 struct nfs4_file *fp, struct inode *inode,
7020 struct nfs4_ol_stateid *open_stp)
7022 struct nfs4_client *clp = lo->lo_owner.so_client;
7023 struct nfs4_ol_stateid *retstp;
7025 mutex_init(&stp->st_mutex);
7026 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
7028 spin_lock(&clp->cl_lock);
7029 if (nfs4_ol_stateid_unhashed(open_stp))
7031 retstp = find_lock_stateid(lo, open_stp);
7034 refcount_inc(&stp->st_stid.sc_count);
7035 stp->st_stid.sc_type = NFS4_LOCK_STID;
7036 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
7038 stp->st_stid.sc_file = fp;
7039 stp->st_access_bmap = 0;
7040 stp->st_deny_bmap = open_stp->st_deny_bmap;
7041 stp->st_openstp = open_stp;
7042 spin_lock(&fp->fi_lock);
7043 list_add(&stp->st_locks, &open_stp->st_locks);
7044 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
7045 list_add(&stp->st_perfile, &fp->fi_stateids);
7046 spin_unlock(&fp->fi_lock);
7047 spin_unlock(&clp->cl_lock);
7050 spin_unlock(&clp->cl_lock);
7051 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
7052 nfs4_put_stid(&retstp->st_stid);
7055 /* To keep mutex tracking happy */
7056 mutex_unlock(&stp->st_mutex);
7059 spin_unlock(&clp->cl_lock);
7060 mutex_unlock(&stp->st_mutex);
7064 static struct nfs4_ol_stateid *
7065 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7066 struct inode *inode, struct nfs4_ol_stateid *ost,
7069 struct nfs4_stid *ns = NULL;
7070 struct nfs4_ol_stateid *lst;
7071 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7072 struct nfs4_client *clp = oo->oo_owner.so_client;
7075 spin_lock(&clp->cl_lock);
7076 lst = find_lock_stateid(lo, ost);
7077 spin_unlock(&clp->cl_lock);
7079 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7081 nfs4_put_stid(&lst->st_stid);
7083 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7087 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7088 if (lst == openlockstateid(ns))
7097 check_lock_length(u64 offset, u64 length)
7099 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7100 (length > ~offset)));
7103 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
7105 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
7107 lockdep_assert_held(&fp->fi_lock);
7109 if (test_access(access, lock_stp))
7111 __nfs4_file_get_access(fp, access);
7112 set_access(access, lock_stp);
7116 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7117 struct nfs4_ol_stateid *ost,
7118 struct nfsd4_lock *lock,
7119 struct nfs4_ol_stateid **plst, bool *new)
7122 struct nfs4_file *fi = ost->st_stid.sc_file;
7123 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7124 struct nfs4_client *cl = oo->oo_owner.so_client;
7125 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
7126 struct nfs4_lockowner *lo;
7127 struct nfs4_ol_stateid *lst;
7128 unsigned int strhashval;
7130 lo = find_lockowner_str(cl, &lock->lk_new_owner);
7132 strhashval = ownerstr_hashval(&lock->lk_new_owner);
7133 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7135 return nfserr_jukebox;
7137 /* with an existing lockowner, seqids must be the same */
7138 status = nfserr_bad_seqid;
7139 if (!cstate->minorversion &&
7140 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
7144 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7146 status = nfserr_jukebox;
7153 nfs4_put_stateowner(&lo->lo_owner);
7161 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7162 union nfsd4_op_u *u)
7164 struct nfsd4_lock *lock = &u->lock;
7165 struct nfs4_openowner *open_sop = NULL;
7166 struct nfs4_lockowner *lock_sop = NULL;
7167 struct nfs4_ol_stateid *lock_stp = NULL;
7168 struct nfs4_ol_stateid *open_stp = NULL;
7169 struct nfs4_file *fp;
7170 struct nfsd_file *nf = NULL;
7171 struct nfsd4_blocked_lock *nbl = NULL;
7172 struct file_lock *file_lock = NULL;
7173 struct file_lock *conflock = NULL;
7178 unsigned char fl_type;
7179 unsigned int fl_flags = FL_POSIX;
7180 struct net *net = SVC_NET(rqstp);
7181 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7183 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7184 (long long) lock->lk_offset,
7185 (long long) lock->lk_length);
7187 if (check_lock_length(lock->lk_offset, lock->lk_length))
7188 return nfserr_inval;
7190 if ((status = fh_verify(rqstp, &cstate->current_fh,
7191 S_IFREG, NFSD_MAY_LOCK))) {
7192 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7196 if (lock->lk_is_new) {
7197 if (nfsd4_has_session(cstate))
7198 /* See rfc 5661 18.10.3: given clientid is ignored: */
7199 memcpy(&lock->lk_new_clientid,
7200 &cstate->clp->cl_clientid,
7201 sizeof(clientid_t));
7203 /* validate and update open stateid and open seqid */
7204 status = nfs4_preprocess_confirmed_seqid_op(cstate,
7205 lock->lk_new_open_seqid,
7206 &lock->lk_new_open_stateid,
7210 mutex_unlock(&open_stp->st_mutex);
7211 open_sop = openowner(open_stp->st_stateowner);
7212 status = nfserr_bad_stateid;
7213 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
7214 &lock->lk_new_clientid))
7216 status = lookup_or_create_lock_state(cstate, open_stp, lock,
7219 status = nfs4_preprocess_seqid_op(cstate,
7220 lock->lk_old_lock_seqid,
7221 &lock->lk_old_lock_stateid,
7222 NFS4_LOCK_STID, &lock_stp, nn);
7226 lock_sop = lockowner(lock_stp->st_stateowner);
7228 lkflg = setlkflg(lock->lk_type);
7229 status = nfs4_check_openmode(lock_stp, lkflg);
7233 status = nfserr_grace;
7234 if (locks_in_grace(net) && !lock->lk_reclaim)
7236 status = nfserr_no_grace;
7237 if (!locks_in_grace(net) && lock->lk_reclaim)
7240 if (lock->lk_reclaim)
7241 fl_flags |= FL_RECLAIM;
7243 fp = lock_stp->st_stid.sc_file;
7244 switch (lock->lk_type) {
7246 if (nfsd4_has_session(cstate))
7247 fl_flags |= FL_SLEEP;
7250 spin_lock(&fp->fi_lock);
7251 nf = find_readable_file_locked(fp);
7253 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7254 spin_unlock(&fp->fi_lock);
7257 case NFS4_WRITEW_LT:
7258 if (nfsd4_has_session(cstate))
7259 fl_flags |= FL_SLEEP;
7262 spin_lock(&fp->fi_lock);
7263 nf = find_writeable_file_locked(fp);
7265 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7266 spin_unlock(&fp->fi_lock);
7270 status = nfserr_inval;
7275 status = nfserr_openmode;
7280 * Most filesystems with their own ->lock operations will block
7281 * the nfsd thread waiting to acquire the lock. That leads to
7282 * deadlocks (we don't want every nfsd thread tied up waiting
7283 * for file locks), so don't attempt blocking lock notifications
7284 * on those filesystems:
7286 if (nf->nf_file->f_op->lock)
7287 fl_flags &= ~FL_SLEEP;
7289 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
7291 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
7292 status = nfserr_jukebox;
7296 file_lock = &nbl->nbl_lock;
7297 file_lock->fl_type = fl_type;
7298 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
7299 file_lock->fl_pid = current->tgid;
7300 file_lock->fl_file = nf->nf_file;
7301 file_lock->fl_flags = fl_flags;
7302 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7303 file_lock->fl_start = lock->lk_offset;
7304 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
7305 nfs4_transform_lock_offset(file_lock);
7307 conflock = locks_alloc_lock();
7309 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7310 status = nfserr_jukebox;
7314 if (fl_flags & FL_SLEEP) {
7315 nbl->nbl_time = ktime_get_boottime_seconds();
7316 spin_lock(&nn->blocked_locks_lock);
7317 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
7318 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
7319 kref_get(&nbl->nbl_kref);
7320 spin_unlock(&nn->blocked_locks_lock);
7323 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
7325 case 0: /* success! */
7326 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
7328 if (lock->lk_reclaim)
7329 nn->somebody_reclaimed = true;
7331 case FILE_LOCK_DEFERRED:
7332 kref_put(&nbl->nbl_kref, free_nbl);
7335 case -EAGAIN: /* conflock holds conflicting lock */
7336 status = nfserr_denied;
7337 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
7338 nfs4_set_lock_denied(conflock, &lock->lk_denied);
7341 status = nfserr_deadlock;
7344 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
7345 status = nfserrno(err);
7350 /* dequeue it if we queued it before */
7351 if (fl_flags & FL_SLEEP) {
7352 spin_lock(&nn->blocked_locks_lock);
7353 if (!list_empty(&nbl->nbl_list) &&
7354 !list_empty(&nbl->nbl_lru)) {
7355 list_del_init(&nbl->nbl_list);
7356 list_del_init(&nbl->nbl_lru);
7357 kref_put(&nbl->nbl_kref, free_nbl);
7359 /* nbl can use one of lists to be linked to reaplist */
7360 spin_unlock(&nn->blocked_locks_lock);
7362 free_blocked_lock(nbl);
7367 /* Bump seqid manually if the 4.0 replay owner is openowner */
7368 if (cstate->replay_owner &&
7369 cstate->replay_owner != &lock_sop->lo_owner &&
7370 seqid_mutating_err(ntohl(status)))
7371 lock_sop->lo_owner.so_seqid++;
7374 * If this is a new, never-before-used stateid, and we are
7375 * returning an error, then just go ahead and release it.
7378 release_lock_stateid(lock_stp);
7380 mutex_unlock(&lock_stp->st_mutex);
7382 nfs4_put_stid(&lock_stp->st_stid);
7385 nfs4_put_stid(&open_stp->st_stid);
7386 nfsd4_bump_seqid(cstate, status);
7388 locks_free_lock(conflock);
7393 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7394 * so we do a temporary open here just to get an open file to pass to
7397 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
7399 struct nfsd_file *nf;
7400 struct inode *inode;
7403 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7406 inode = fhp->fh_dentry->d_inode;
7407 inode_lock(inode); /* to block new leases till after test_lock: */
7408 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
7411 lock->fl_file = nf->nf_file;
7412 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
7413 lock->fl_file = NULL;
7415 inode_unlock(inode);
7424 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7425 union nfsd4_op_u *u)
7427 struct nfsd4_lockt *lockt = &u->lockt;
7428 struct file_lock *file_lock = NULL;
7429 struct nfs4_lockowner *lo = NULL;
7431 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7433 if (locks_in_grace(SVC_NET(rqstp)))
7434 return nfserr_grace;
7436 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7437 return nfserr_inval;
7439 if (!nfsd4_has_session(cstate)) {
7440 status = set_client(&lockt->lt_clientid, cstate, nn);
7445 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7448 file_lock = locks_alloc_lock();
7450 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7451 status = nfserr_jukebox;
7455 switch (lockt->lt_type) {
7458 file_lock->fl_type = F_RDLCK;
7461 case NFS4_WRITEW_LT:
7462 file_lock->fl_type = F_WRLCK;
7465 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
7466 status = nfserr_inval;
7470 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
7472 file_lock->fl_owner = (fl_owner_t)lo;
7473 file_lock->fl_pid = current->tgid;
7474 file_lock->fl_flags = FL_POSIX;
7476 file_lock->fl_start = lockt->lt_offset;
7477 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
7479 nfs4_transform_lock_offset(file_lock);
7481 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
7485 if (file_lock->fl_type != F_UNLCK) {
7486 status = nfserr_denied;
7487 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
7491 nfs4_put_stateowner(&lo->lo_owner);
7493 locks_free_lock(file_lock);
7498 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7499 union nfsd4_op_u *u)
7501 struct nfsd4_locku *locku = &u->locku;
7502 struct nfs4_ol_stateid *stp;
7503 struct nfsd_file *nf = NULL;
7504 struct file_lock *file_lock = NULL;
7507 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7509 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7510 (long long) locku->lu_offset,
7511 (long long) locku->lu_length);
7513 if (check_lock_length(locku->lu_offset, locku->lu_length))
7514 return nfserr_inval;
7516 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
7517 &locku->lu_stateid, NFS4_LOCK_STID,
7521 nf = find_any_file(stp->st_stid.sc_file);
7523 status = nfserr_lock_range;
7526 file_lock = locks_alloc_lock();
7528 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7529 status = nfserr_jukebox;
7533 file_lock->fl_type = F_UNLCK;
7534 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7535 file_lock->fl_pid = current->tgid;
7536 file_lock->fl_file = nf->nf_file;
7537 file_lock->fl_flags = FL_POSIX;
7538 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7539 file_lock->fl_start = locku->lu_offset;
7541 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7543 nfs4_transform_lock_offset(file_lock);
7545 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7547 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7550 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7554 mutex_unlock(&stp->st_mutex);
7555 nfs4_put_stid(&stp->st_stid);
7557 nfsd4_bump_seqid(cstate, status);
7559 locks_free_lock(file_lock);
7563 status = nfserrno(err);
7569 * true: locks held by lockowner
7570 * false: no locks held by lockowner
7573 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7575 struct file_lock *fl;
7577 struct nfsd_file *nf = find_any_file(fp);
7578 struct inode *inode;
7579 struct file_lock_context *flctx;
7582 /* Any valid lock stateid should have some sort of access */
7587 inode = locks_inode(nf->nf_file);
7588 flctx = inode->i_flctx;
7590 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7591 spin_lock(&flctx->flc_lock);
7592 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7593 if (fl->fl_owner == (fl_owner_t)lowner) {
7598 spin_unlock(&flctx->flc_lock);
7605 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
7606 * @rqstp: RPC transaction
7607 * @cstate: NFSv4 COMPOUND state
7608 * @u: RELEASE_LOCKOWNER arguments
7610 * The lockowner's so_count is bumped when a lock record is added
7611 * or when copying a conflicting lock. The latter case is brief,
7612 * but can lead to fleeting false positives when looking for
7616 * %nfs_ok: lockowner released or not found
7617 * %nfserr_locks_held: lockowner still in use
7618 * %nfserr_stale_clientid: clientid no longer active
7619 * %nfserr_expired: clientid not recognized
7622 nfsd4_release_lockowner(struct svc_rqst *rqstp,
7623 struct nfsd4_compound_state *cstate,
7624 union nfsd4_op_u *u)
7626 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
7627 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7628 clientid_t *clid = &rlockowner->rl_clientid;
7629 struct nfs4_ol_stateid *stp;
7630 struct nfs4_lockowner *lo;
7631 struct nfs4_client *clp;
7632 LIST_HEAD(reaplist);
7635 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7636 clid->cl_boot, clid->cl_id);
7638 status = set_client(clid, cstate, nn);
7643 spin_lock(&clp->cl_lock);
7644 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
7646 spin_unlock(&clp->cl_lock);
7649 if (atomic_read(&lo->lo_owner.so_count) != 2) {
7650 spin_unlock(&clp->cl_lock);
7651 nfs4_put_stateowner(&lo->lo_owner);
7652 return nfserr_locks_held;
7654 unhash_lockowner_locked(lo);
7655 while (!list_empty(&lo->lo_owner.so_stateids)) {
7656 stp = list_first_entry(&lo->lo_owner.so_stateids,
7657 struct nfs4_ol_stateid,
7659 WARN_ON(!unhash_lock_stateid(stp));
7660 put_ol_stateid_locked(stp, &reaplist);
7662 spin_unlock(&clp->cl_lock);
7664 free_ol_stateid_reaplist(&reaplist);
7665 remove_blocked_locks(lo);
7666 nfs4_put_stateowner(&lo->lo_owner);
7670 static inline struct nfs4_client_reclaim *
7673 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
7677 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7679 struct nfs4_client_reclaim *crp;
7681 crp = nfsd4_find_reclaim_client(name, nn);
7682 return (crp && crp->cr_clp);
7686 * failure => all reset bets are off, nfserr_no_grace...
7688 * The caller is responsible for freeing name.data if NULL is returned (it
7689 * will be freed in nfs4_remove_reclaim_record in the normal case).
7691 struct nfs4_client_reclaim *
7692 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7693 struct nfsd_net *nn)
7695 unsigned int strhashval;
7696 struct nfs4_client_reclaim *crp;
7698 crp = alloc_reclaim();
7700 strhashval = clientstr_hashval(name);
7701 INIT_LIST_HEAD(&crp->cr_strhash);
7702 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
7703 crp->cr_name.data = name.data;
7704 crp->cr_name.len = name.len;
7705 crp->cr_princhash.data = princhash.data;
7706 crp->cr_princhash.len = princhash.len;
7708 nn->reclaim_str_hashtbl_size++;
7714 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
7716 list_del(&crp->cr_strhash);
7717 kfree(crp->cr_name.data);
7718 kfree(crp->cr_princhash.data);
7720 nn->reclaim_str_hashtbl_size--;
7724 nfs4_release_reclaim(struct nfsd_net *nn)
7726 struct nfs4_client_reclaim *crp = NULL;
7729 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7730 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7731 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
7732 struct nfs4_client_reclaim, cr_strhash);
7733 nfs4_remove_reclaim_record(crp, nn);
7736 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
7740 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
7741 struct nfs4_client_reclaim *
7742 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
7744 unsigned int strhashval;
7745 struct nfs4_client_reclaim *crp = NULL;
7747 strhashval = clientstr_hashval(name);
7748 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
7749 if (compare_blob(&crp->cr_name, &name) == 0) {
7757 nfs4_check_open_reclaim(struct nfs4_client *clp)
7759 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
7760 return nfserr_no_grace;
7762 if (nfsd4_client_record_check(clp))
7763 return nfserr_reclaim_bad;
7769 * Since the lifetime of a delegation isn't limited to that of an open, a
7770 * client may quite reasonably hang on to a delegation as long as it has
7771 * the inode cached. This becomes an obvious problem the first time a
7772 * client's inode cache approaches the size of the server's total memory.
7774 * For now we avoid this problem by imposing a hard limit on the number
7775 * of delegations, which varies according to the server's memory size.
7778 set_max_delegations(void)
7781 * Allow at most 4 delegations per megabyte of RAM. Quick
7782 * estimates suggest that in the worst case (where every delegation
7783 * is for a different inode), a delegation could take about 1.5K,
7784 * giving a worst case usage of about 6% of memory.
7786 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7789 static int nfs4_state_create_net(struct net *net)
7791 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7794 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7795 sizeof(struct list_head),
7797 if (!nn->conf_id_hashtbl)
7799 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7800 sizeof(struct list_head),
7802 if (!nn->unconf_id_hashtbl)
7804 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7805 sizeof(struct list_head),
7807 if (!nn->sessionid_hashtbl)
7810 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7811 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7812 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7814 for (i = 0; i < SESSION_HASH_SIZE; i++)
7815 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7816 nn->conf_name_tree = RB_ROOT;
7817 nn->unconf_name_tree = RB_ROOT;
7818 nn->boot_time = ktime_get_real_seconds();
7819 nn->grace_ended = false;
7820 nn->nfsd4_manager.block_opens = true;
7821 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7822 INIT_LIST_HEAD(&nn->client_lru);
7823 INIT_LIST_HEAD(&nn->close_lru);
7824 INIT_LIST_HEAD(&nn->del_recall_lru);
7825 spin_lock_init(&nn->client_lock);
7826 spin_lock_init(&nn->s2s_cp_lock);
7827 idr_init(&nn->s2s_cp_stateids);
7829 spin_lock_init(&nn->blocked_locks_lock);
7830 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7832 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7838 kfree(nn->unconf_id_hashtbl);
7840 kfree(nn->conf_id_hashtbl);
7846 nfs4_state_destroy_net(struct net *net)
7849 struct nfs4_client *clp = NULL;
7850 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7852 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7853 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7854 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7855 destroy_client(clp);
7859 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7861 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7862 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7863 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7864 destroy_client(clp);
7868 kfree(nn->sessionid_hashtbl);
7869 kfree(nn->unconf_id_hashtbl);
7870 kfree(nn->conf_id_hashtbl);
7875 nfs4_state_start_net(struct net *net)
7877 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7880 ret = nfs4_state_create_net(net);
7883 locks_start_grace(net, &nn->nfsd4_manager);
7884 nfsd4_client_tracking_init(net);
7885 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7887 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
7888 nn->nfsd4_grace, net->ns.inum);
7889 trace_nfsd_grace_start(nn);
7890 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7894 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7896 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7897 nfsd4_end_grace(nn);
7901 /* initialization to perform when the nfsd service is started: */
7904 nfs4_state_start(void)
7908 ret = nfsd4_create_callback_queue();
7912 set_max_delegations();
7917 nfs4_state_shutdown_net(struct net *net)
7919 struct nfs4_delegation *dp = NULL;
7920 struct list_head *pos, *next, reaplist;
7921 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7923 cancel_delayed_work_sync(&nn->laundromat_work);
7924 locks_end_grace(&nn->nfsd4_manager);
7926 INIT_LIST_HEAD(&reaplist);
7927 spin_lock(&state_lock);
7928 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7929 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7930 WARN_ON(!unhash_delegation_locked(dp));
7931 list_add(&dp->dl_recall_lru, &reaplist);
7933 spin_unlock(&state_lock);
7934 list_for_each_safe(pos, next, &reaplist) {
7935 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7936 list_del_init(&dp->dl_recall_lru);
7937 destroy_unhashed_deleg(dp);
7940 nfsd4_client_tracking_exit(net);
7941 nfs4_state_destroy_net(net);
7942 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
7943 nfsd4_ssc_shutdown_umount(nn);
7948 nfs4_state_shutdown(void)
7950 nfsd4_destroy_callback_queue();
7954 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7956 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7957 CURRENT_STATEID(stateid))
7958 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7962 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7964 if (cstate->minorversion) {
7965 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7966 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7971 clear_current_stateid(struct nfsd4_compound_state *cstate)
7973 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7977 * functions to set current state id
7980 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7981 union nfsd4_op_u *u)
7983 put_stateid(cstate, &u->open_downgrade.od_stateid);
7987 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7988 union nfsd4_op_u *u)
7990 put_stateid(cstate, &u->open.op_stateid);
7994 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7995 union nfsd4_op_u *u)
7997 put_stateid(cstate, &u->close.cl_stateid);
8001 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
8002 union nfsd4_op_u *u)
8004 put_stateid(cstate, &u->lock.lk_resp_stateid);
8008 * functions to consume current state id
8012 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
8013 union nfsd4_op_u *u)
8015 get_stateid(cstate, &u->open_downgrade.od_stateid);
8019 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8020 union nfsd4_op_u *u)
8022 get_stateid(cstate, &u->delegreturn.dr_stateid);
8026 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8027 union nfsd4_op_u *u)
8029 get_stateid(cstate, &u->free_stateid.fr_stateid);
8033 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8034 union nfsd4_op_u *u)
8036 get_stateid(cstate, &u->setattr.sa_stateid);
8040 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8041 union nfsd4_op_u *u)
8043 get_stateid(cstate, &u->close.cl_stateid);
8047 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8048 union nfsd4_op_u *u)
8050 get_stateid(cstate, &u->locku.lu_stateid);
8054 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8055 union nfsd4_op_u *u)
8057 get_stateid(cstate, &u->read.rd_stateid);
8061 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8062 union nfsd4_op_u *u)
8064 get_stateid(cstate, &u->write.wr_stateid);