xref: /openbmc/linux/fs/nfsd/nfs4state.c (revision a2cce7a9)
1 /*
2 *  Copyright (c) 2001 The Regents of the University of Michigan.
3 *  All rights reserved.
4 *
5 *  Kendrick Smith <kmsmith@umich.edu>
6 *  Andy Adamson <kandros@umich.edu>
7 *
8 *  Redistribution and use in source and binary forms, with or without
9 *  modification, are permitted provided that the following conditions
10 *  are met:
11 *
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.
20 *
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.
32 *
33 */
34 
35 #include <linux/file.h>
36 #include <linux/fs.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 "xdr4.h"
46 #include "xdr4cb.h"
47 #include "vfs.h"
48 #include "current_stateid.h"
49 
50 #include "netns.h"
51 #include "pnfs.h"
52 
53 #define NFSDDBG_FACILITY                NFSDDBG_PROC
54 
55 #define all_ones {{~0,~0},~0}
56 static const stateid_t one_stateid = {
57 	.si_generation = ~0,
58 	.si_opaque = all_ones,
59 };
60 static const stateid_t zero_stateid = {
61 	/* all fields zero */
62 };
63 static const stateid_t currentstateid = {
64 	.si_generation = 1,
65 };
66 
67 static u64 current_sessionid = 1;
68 
69 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
70 #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
71 #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
72 
73 /* forward declarations */
74 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
75 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
76 
77 /* Locking: */
78 
79 /*
80  * Currently used for the del_recall_lru and file hash table.  In an
81  * effort to decrease the scope of the client_mutex, this spinlock may
82  * eventually cover more:
83  */
84 static DEFINE_SPINLOCK(state_lock);
85 
86 /*
87  * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
88  * the refcount on the open stateid to drop.
89  */
90 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
91 
92 static struct kmem_cache *openowner_slab;
93 static struct kmem_cache *lockowner_slab;
94 static struct kmem_cache *file_slab;
95 static struct kmem_cache *stateid_slab;
96 static struct kmem_cache *deleg_slab;
97 static struct kmem_cache *odstate_slab;
98 
99 static void free_session(struct nfsd4_session *);
100 
101 static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
102 
103 static bool is_session_dead(struct nfsd4_session *ses)
104 {
105 	return ses->se_flags & NFS4_SESSION_DEAD;
106 }
107 
108 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
109 {
110 	if (atomic_read(&ses->se_ref) > ref_held_by_me)
111 		return nfserr_jukebox;
112 	ses->se_flags |= NFS4_SESSION_DEAD;
113 	return nfs_ok;
114 }
115 
116 static bool is_client_expired(struct nfs4_client *clp)
117 {
118 	return clp->cl_time == 0;
119 }
120 
121 static __be32 get_client_locked(struct nfs4_client *clp)
122 {
123 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
124 
125 	lockdep_assert_held(&nn->client_lock);
126 
127 	if (is_client_expired(clp))
128 		return nfserr_expired;
129 	atomic_inc(&clp->cl_refcount);
130 	return nfs_ok;
131 }
132 
133 /* must be called under the client_lock */
134 static inline void
135 renew_client_locked(struct nfs4_client *clp)
136 {
137 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
138 
139 	if (is_client_expired(clp)) {
140 		WARN_ON(1);
141 		printk("%s: client (clientid %08x/%08x) already expired\n",
142 			__func__,
143 			clp->cl_clientid.cl_boot,
144 			clp->cl_clientid.cl_id);
145 		return;
146 	}
147 
148 	dprintk("renewing client (clientid %08x/%08x)\n",
149 			clp->cl_clientid.cl_boot,
150 			clp->cl_clientid.cl_id);
151 	list_move_tail(&clp->cl_lru, &nn->client_lru);
152 	clp->cl_time = get_seconds();
153 }
154 
155 static void put_client_renew_locked(struct nfs4_client *clp)
156 {
157 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
158 
159 	lockdep_assert_held(&nn->client_lock);
160 
161 	if (!atomic_dec_and_test(&clp->cl_refcount))
162 		return;
163 	if (!is_client_expired(clp))
164 		renew_client_locked(clp);
165 }
166 
167 static void put_client_renew(struct nfs4_client *clp)
168 {
169 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
170 
171 	if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
172 		return;
173 	if (!is_client_expired(clp))
174 		renew_client_locked(clp);
175 	spin_unlock(&nn->client_lock);
176 }
177 
178 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
179 {
180 	__be32 status;
181 
182 	if (is_session_dead(ses))
183 		return nfserr_badsession;
184 	status = get_client_locked(ses->se_client);
185 	if (status)
186 		return status;
187 	atomic_inc(&ses->se_ref);
188 	return nfs_ok;
189 }
190 
191 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
192 {
193 	struct nfs4_client *clp = ses->se_client;
194 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
195 
196 	lockdep_assert_held(&nn->client_lock);
197 
198 	if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
199 		free_session(ses);
200 	put_client_renew_locked(clp);
201 }
202 
203 static void nfsd4_put_session(struct nfsd4_session *ses)
204 {
205 	struct nfs4_client *clp = ses->se_client;
206 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
207 
208 	spin_lock(&nn->client_lock);
209 	nfsd4_put_session_locked(ses);
210 	spin_unlock(&nn->client_lock);
211 }
212 
213 static inline struct nfs4_stateowner *
214 nfs4_get_stateowner(struct nfs4_stateowner *sop)
215 {
216 	atomic_inc(&sop->so_count);
217 	return sop;
218 }
219 
220 static int
221 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
222 {
223 	return (sop->so_owner.len == owner->len) &&
224 		0 == memcmp(sop->so_owner.data, owner->data, owner->len);
225 }
226 
227 static struct nfs4_openowner *
228 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
229 			struct nfs4_client *clp)
230 {
231 	struct nfs4_stateowner *so;
232 
233 	lockdep_assert_held(&clp->cl_lock);
234 
235 	list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
236 			    so_strhash) {
237 		if (!so->so_is_open_owner)
238 			continue;
239 		if (same_owner_str(so, &open->op_owner))
240 			return openowner(nfs4_get_stateowner(so));
241 	}
242 	return NULL;
243 }
244 
245 static struct nfs4_openowner *
246 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
247 			struct nfs4_client *clp)
248 {
249 	struct nfs4_openowner *oo;
250 
251 	spin_lock(&clp->cl_lock);
252 	oo = find_openstateowner_str_locked(hashval, open, clp);
253 	spin_unlock(&clp->cl_lock);
254 	return oo;
255 }
256 
257 static inline u32
258 opaque_hashval(const void *ptr, int nbytes)
259 {
260 	unsigned char *cptr = (unsigned char *) ptr;
261 
262 	u32 x = 0;
263 	while (nbytes--) {
264 		x *= 37;
265 		x += *cptr++;
266 	}
267 	return x;
268 }
269 
270 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
271 {
272 	struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
273 
274 	kmem_cache_free(file_slab, fp);
275 }
276 
277 void
278 put_nfs4_file(struct nfs4_file *fi)
279 {
280 	might_lock(&state_lock);
281 
282 	if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
283 		hlist_del_rcu(&fi->fi_hash);
284 		spin_unlock(&state_lock);
285 		WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
286 		WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
287 		call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
288 	}
289 }
290 
291 static struct file *
292 __nfs4_get_fd(struct nfs4_file *f, int oflag)
293 {
294 	if (f->fi_fds[oflag])
295 		return get_file(f->fi_fds[oflag]);
296 	return NULL;
297 }
298 
299 static struct file *
300 find_writeable_file_locked(struct nfs4_file *f)
301 {
302 	struct file *ret;
303 
304 	lockdep_assert_held(&f->fi_lock);
305 
306 	ret = __nfs4_get_fd(f, O_WRONLY);
307 	if (!ret)
308 		ret = __nfs4_get_fd(f, O_RDWR);
309 	return ret;
310 }
311 
312 static struct file *
313 find_writeable_file(struct nfs4_file *f)
314 {
315 	struct file *ret;
316 
317 	spin_lock(&f->fi_lock);
318 	ret = find_writeable_file_locked(f);
319 	spin_unlock(&f->fi_lock);
320 
321 	return ret;
322 }
323 
324 static struct file *find_readable_file_locked(struct nfs4_file *f)
325 {
326 	struct file *ret;
327 
328 	lockdep_assert_held(&f->fi_lock);
329 
330 	ret = __nfs4_get_fd(f, O_RDONLY);
331 	if (!ret)
332 		ret = __nfs4_get_fd(f, O_RDWR);
333 	return ret;
334 }
335 
336 static struct file *
337 find_readable_file(struct nfs4_file *f)
338 {
339 	struct file *ret;
340 
341 	spin_lock(&f->fi_lock);
342 	ret = find_readable_file_locked(f);
343 	spin_unlock(&f->fi_lock);
344 
345 	return ret;
346 }
347 
348 struct file *
349 find_any_file(struct nfs4_file *f)
350 {
351 	struct file *ret;
352 
353 	spin_lock(&f->fi_lock);
354 	ret = __nfs4_get_fd(f, O_RDWR);
355 	if (!ret) {
356 		ret = __nfs4_get_fd(f, O_WRONLY);
357 		if (!ret)
358 			ret = __nfs4_get_fd(f, O_RDONLY);
359 	}
360 	spin_unlock(&f->fi_lock);
361 	return ret;
362 }
363 
364 static atomic_long_t num_delegations;
365 unsigned long max_delegations;
366 
367 /*
368  * Open owner state (share locks)
369  */
370 
371 /* hash tables for lock and open owners */
372 #define OWNER_HASH_BITS              8
373 #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
374 #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
375 
376 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
377 {
378 	unsigned int ret;
379 
380 	ret = opaque_hashval(ownername->data, ownername->len);
381 	return ret & OWNER_HASH_MASK;
382 }
383 
384 /* hash table for nfs4_file */
385 #define FILE_HASH_BITS                   8
386 #define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
387 
388 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
389 {
390 	return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
391 }
392 
393 static unsigned int file_hashval(struct knfsd_fh *fh)
394 {
395 	return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
396 }
397 
398 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
399 
400 static void
401 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
402 {
403 	lockdep_assert_held(&fp->fi_lock);
404 
405 	if (access & NFS4_SHARE_ACCESS_WRITE)
406 		atomic_inc(&fp->fi_access[O_WRONLY]);
407 	if (access & NFS4_SHARE_ACCESS_READ)
408 		atomic_inc(&fp->fi_access[O_RDONLY]);
409 }
410 
411 static __be32
412 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
413 {
414 	lockdep_assert_held(&fp->fi_lock);
415 
416 	/* Does this access mode make sense? */
417 	if (access & ~NFS4_SHARE_ACCESS_BOTH)
418 		return nfserr_inval;
419 
420 	/* Does it conflict with a deny mode already set? */
421 	if ((access & fp->fi_share_deny) != 0)
422 		return nfserr_share_denied;
423 
424 	__nfs4_file_get_access(fp, access);
425 	return nfs_ok;
426 }
427 
428 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
429 {
430 	/* Common case is that there is no deny mode. */
431 	if (deny) {
432 		/* Does this deny mode make sense? */
433 		if (deny & ~NFS4_SHARE_DENY_BOTH)
434 			return nfserr_inval;
435 
436 		if ((deny & NFS4_SHARE_DENY_READ) &&
437 		    atomic_read(&fp->fi_access[O_RDONLY]))
438 			return nfserr_share_denied;
439 
440 		if ((deny & NFS4_SHARE_DENY_WRITE) &&
441 		    atomic_read(&fp->fi_access[O_WRONLY]))
442 			return nfserr_share_denied;
443 	}
444 	return nfs_ok;
445 }
446 
447 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
448 {
449 	might_lock(&fp->fi_lock);
450 
451 	if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
452 		struct file *f1 = NULL;
453 		struct file *f2 = NULL;
454 
455 		swap(f1, fp->fi_fds[oflag]);
456 		if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
457 			swap(f2, fp->fi_fds[O_RDWR]);
458 		spin_unlock(&fp->fi_lock);
459 		if (f1)
460 			fput(f1);
461 		if (f2)
462 			fput(f2);
463 	}
464 }
465 
466 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
467 {
468 	WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
469 
470 	if (access & NFS4_SHARE_ACCESS_WRITE)
471 		__nfs4_file_put_access(fp, O_WRONLY);
472 	if (access & NFS4_SHARE_ACCESS_READ)
473 		__nfs4_file_put_access(fp, O_RDONLY);
474 }
475 
476 /*
477  * Allocate a new open/delegation state counter. This is needed for
478  * pNFS for proper return on close semantics.
479  *
480  * Note that we only allocate it for pNFS-enabled exports, otherwise
481  * all pointers to struct nfs4_clnt_odstate are always NULL.
482  */
483 static struct nfs4_clnt_odstate *
484 alloc_clnt_odstate(struct nfs4_client *clp)
485 {
486 	struct nfs4_clnt_odstate *co;
487 
488 	co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
489 	if (co) {
490 		co->co_client = clp;
491 		atomic_set(&co->co_odcount, 1);
492 	}
493 	return co;
494 }
495 
496 static void
497 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
498 {
499 	struct nfs4_file *fp = co->co_file;
500 
501 	lockdep_assert_held(&fp->fi_lock);
502 	list_add(&co->co_perfile, &fp->fi_clnt_odstate);
503 }
504 
505 static inline void
506 get_clnt_odstate(struct nfs4_clnt_odstate *co)
507 {
508 	if (co)
509 		atomic_inc(&co->co_odcount);
510 }
511 
512 static void
513 put_clnt_odstate(struct nfs4_clnt_odstate *co)
514 {
515 	struct nfs4_file *fp;
516 
517 	if (!co)
518 		return;
519 
520 	fp = co->co_file;
521 	if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
522 		list_del(&co->co_perfile);
523 		spin_unlock(&fp->fi_lock);
524 
525 		nfsd4_return_all_file_layouts(co->co_client, fp);
526 		kmem_cache_free(odstate_slab, co);
527 	}
528 }
529 
530 static struct nfs4_clnt_odstate *
531 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
532 {
533 	struct nfs4_clnt_odstate *co;
534 	struct nfs4_client *cl;
535 
536 	if (!new)
537 		return NULL;
538 
539 	cl = new->co_client;
540 
541 	spin_lock(&fp->fi_lock);
542 	list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
543 		if (co->co_client == cl) {
544 			get_clnt_odstate(co);
545 			goto out;
546 		}
547 	}
548 	co = new;
549 	co->co_file = fp;
550 	hash_clnt_odstate_locked(new);
551 out:
552 	spin_unlock(&fp->fi_lock);
553 	return co;
554 }
555 
556 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
557 					 struct kmem_cache *slab)
558 {
559 	struct nfs4_stid *stid;
560 	int new_id;
561 
562 	stid = kmem_cache_zalloc(slab, GFP_KERNEL);
563 	if (!stid)
564 		return NULL;
565 
566 	idr_preload(GFP_KERNEL);
567 	spin_lock(&cl->cl_lock);
568 	new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
569 	spin_unlock(&cl->cl_lock);
570 	idr_preload_end();
571 	if (new_id < 0)
572 		goto out_free;
573 	stid->sc_client = cl;
574 	stid->sc_stateid.si_opaque.so_id = new_id;
575 	stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
576 	/* Will be incremented before return to client: */
577 	atomic_set(&stid->sc_count, 1);
578 
579 	/*
580 	 * It shouldn't be a problem to reuse an opaque stateid value.
581 	 * I don't think it is for 4.1.  But with 4.0 I worry that, for
582 	 * example, a stray write retransmission could be accepted by
583 	 * the server when it should have been rejected.  Therefore,
584 	 * adopt a trick from the sctp code to attempt to maximize the
585 	 * amount of time until an id is reused, by ensuring they always
586 	 * "increase" (mod INT_MAX):
587 	 */
588 	return stid;
589 out_free:
590 	kmem_cache_free(slab, stid);
591 	return NULL;
592 }
593 
594 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
595 {
596 	struct nfs4_stid *stid;
597 	struct nfs4_ol_stateid *stp;
598 
599 	stid = nfs4_alloc_stid(clp, stateid_slab);
600 	if (!stid)
601 		return NULL;
602 
603 	stp = openlockstateid(stid);
604 	stp->st_stid.sc_free = nfs4_free_ol_stateid;
605 	return stp;
606 }
607 
608 static void nfs4_free_deleg(struct nfs4_stid *stid)
609 {
610 	kmem_cache_free(deleg_slab, stid);
611 	atomic_long_dec(&num_delegations);
612 }
613 
614 /*
615  * When we recall a delegation, we should be careful not to hand it
616  * out again straight away.
617  * To ensure this we keep a pair of bloom filters ('new' and 'old')
618  * in which the filehandles of recalled delegations are "stored".
619  * If a filehandle appear in either filter, a delegation is blocked.
620  * When a delegation is recalled, the filehandle is stored in the "new"
621  * filter.
622  * Every 30 seconds we swap the filters and clear the "new" one,
623  * unless both are empty of course.
624  *
625  * Each filter is 256 bits.  We hash the filehandle to 32bit and use the
626  * low 3 bytes as hash-table indices.
627  *
628  * 'blocked_delegations_lock', which is always taken in block_delegations(),
629  * is used to manage concurrent access.  Testing does not need the lock
630  * except when swapping the two filters.
631  */
632 static DEFINE_SPINLOCK(blocked_delegations_lock);
633 static struct bloom_pair {
634 	int	entries, old_entries;
635 	time_t	swap_time;
636 	int	new; /* index into 'set' */
637 	DECLARE_BITMAP(set[2], 256);
638 } blocked_delegations;
639 
640 static int delegation_blocked(struct knfsd_fh *fh)
641 {
642 	u32 hash;
643 	struct bloom_pair *bd = &blocked_delegations;
644 
645 	if (bd->entries == 0)
646 		return 0;
647 	if (seconds_since_boot() - bd->swap_time > 30) {
648 		spin_lock(&blocked_delegations_lock);
649 		if (seconds_since_boot() - bd->swap_time > 30) {
650 			bd->entries -= bd->old_entries;
651 			bd->old_entries = bd->entries;
652 			memset(bd->set[bd->new], 0,
653 			       sizeof(bd->set[0]));
654 			bd->new = 1-bd->new;
655 			bd->swap_time = seconds_since_boot();
656 		}
657 		spin_unlock(&blocked_delegations_lock);
658 	}
659 	hash = jhash(&fh->fh_base, fh->fh_size, 0);
660 	if (test_bit(hash&255, bd->set[0]) &&
661 	    test_bit((hash>>8)&255, bd->set[0]) &&
662 	    test_bit((hash>>16)&255, bd->set[0]))
663 		return 1;
664 
665 	if (test_bit(hash&255, bd->set[1]) &&
666 	    test_bit((hash>>8)&255, bd->set[1]) &&
667 	    test_bit((hash>>16)&255, bd->set[1]))
668 		return 1;
669 
670 	return 0;
671 }
672 
673 static void block_delegations(struct knfsd_fh *fh)
674 {
675 	u32 hash;
676 	struct bloom_pair *bd = &blocked_delegations;
677 
678 	hash = jhash(&fh->fh_base, fh->fh_size, 0);
679 
680 	spin_lock(&blocked_delegations_lock);
681 	__set_bit(hash&255, bd->set[bd->new]);
682 	__set_bit((hash>>8)&255, bd->set[bd->new]);
683 	__set_bit((hash>>16)&255, bd->set[bd->new]);
684 	if (bd->entries == 0)
685 		bd->swap_time = seconds_since_boot();
686 	bd->entries += 1;
687 	spin_unlock(&blocked_delegations_lock);
688 }
689 
690 static struct nfs4_delegation *
691 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
692 		 struct nfs4_clnt_odstate *odstate)
693 {
694 	struct nfs4_delegation *dp;
695 	long n;
696 
697 	dprintk("NFSD alloc_init_deleg\n");
698 	n = atomic_long_inc_return(&num_delegations);
699 	if (n < 0 || n > max_delegations)
700 		goto out_dec;
701 	if (delegation_blocked(&current_fh->fh_handle))
702 		goto out_dec;
703 	dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
704 	if (dp == NULL)
705 		goto out_dec;
706 
707 	dp->dl_stid.sc_free = nfs4_free_deleg;
708 	/*
709 	 * delegation seqid's are never incremented.  The 4.1 special
710 	 * meaning of seqid 0 isn't meaningful, really, but let's avoid
711 	 * 0 anyway just for consistency and use 1:
712 	 */
713 	dp->dl_stid.sc_stateid.si_generation = 1;
714 	INIT_LIST_HEAD(&dp->dl_perfile);
715 	INIT_LIST_HEAD(&dp->dl_perclnt);
716 	INIT_LIST_HEAD(&dp->dl_recall_lru);
717 	dp->dl_clnt_odstate = odstate;
718 	get_clnt_odstate(odstate);
719 	dp->dl_type = NFS4_OPEN_DELEGATE_READ;
720 	dp->dl_retries = 1;
721 	nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
722 		      &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
723 	return dp;
724 out_dec:
725 	atomic_long_dec(&num_delegations);
726 	return NULL;
727 }
728 
729 void
730 nfs4_put_stid(struct nfs4_stid *s)
731 {
732 	struct nfs4_file *fp = s->sc_file;
733 	struct nfs4_client *clp = s->sc_client;
734 
735 	might_lock(&clp->cl_lock);
736 
737 	if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
738 		wake_up_all(&close_wq);
739 		return;
740 	}
741 	idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
742 	spin_unlock(&clp->cl_lock);
743 	s->sc_free(s);
744 	if (fp)
745 		put_nfs4_file(fp);
746 }
747 
748 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
749 {
750 	struct file *filp = NULL;
751 
752 	spin_lock(&fp->fi_lock);
753 	if (fp->fi_deleg_file && --fp->fi_delegees == 0)
754 		swap(filp, fp->fi_deleg_file);
755 	spin_unlock(&fp->fi_lock);
756 
757 	if (filp) {
758 		vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
759 		fput(filp);
760 	}
761 }
762 
763 void nfs4_unhash_stid(struct nfs4_stid *s)
764 {
765 	s->sc_type = 0;
766 }
767 
768 static void
769 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
770 {
771 	lockdep_assert_held(&state_lock);
772 	lockdep_assert_held(&fp->fi_lock);
773 
774 	atomic_inc(&dp->dl_stid.sc_count);
775 	dp->dl_stid.sc_type = NFS4_DELEG_STID;
776 	list_add(&dp->dl_perfile, &fp->fi_delegations);
777 	list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
778 }
779 
780 static bool
781 unhash_delegation_locked(struct nfs4_delegation *dp)
782 {
783 	struct nfs4_file *fp = dp->dl_stid.sc_file;
784 
785 	lockdep_assert_held(&state_lock);
786 
787 	if (list_empty(&dp->dl_perfile))
788 		return false;
789 
790 	dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
791 	/* Ensure that deleg break won't try to requeue it */
792 	++dp->dl_time;
793 	spin_lock(&fp->fi_lock);
794 	list_del_init(&dp->dl_perclnt);
795 	list_del_init(&dp->dl_recall_lru);
796 	list_del_init(&dp->dl_perfile);
797 	spin_unlock(&fp->fi_lock);
798 	return true;
799 }
800 
801 static void destroy_delegation(struct nfs4_delegation *dp)
802 {
803 	bool unhashed;
804 
805 	spin_lock(&state_lock);
806 	unhashed = unhash_delegation_locked(dp);
807 	spin_unlock(&state_lock);
808 	if (unhashed) {
809 		put_clnt_odstate(dp->dl_clnt_odstate);
810 		nfs4_put_deleg_lease(dp->dl_stid.sc_file);
811 		nfs4_put_stid(&dp->dl_stid);
812 	}
813 }
814 
815 static void revoke_delegation(struct nfs4_delegation *dp)
816 {
817 	struct nfs4_client *clp = dp->dl_stid.sc_client;
818 
819 	WARN_ON(!list_empty(&dp->dl_recall_lru));
820 
821 	put_clnt_odstate(dp->dl_clnt_odstate);
822 	nfs4_put_deleg_lease(dp->dl_stid.sc_file);
823 
824 	if (clp->cl_minorversion == 0)
825 		nfs4_put_stid(&dp->dl_stid);
826 	else {
827 		dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
828 		spin_lock(&clp->cl_lock);
829 		list_add(&dp->dl_recall_lru, &clp->cl_revoked);
830 		spin_unlock(&clp->cl_lock);
831 	}
832 }
833 
834 /*
835  * SETCLIENTID state
836  */
837 
838 static unsigned int clientid_hashval(u32 id)
839 {
840 	return id & CLIENT_HASH_MASK;
841 }
842 
843 static unsigned int clientstr_hashval(const char *name)
844 {
845 	return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
846 }
847 
848 /*
849  * We store the NONE, READ, WRITE, and BOTH bits separately in the
850  * st_{access,deny}_bmap field of the stateid, in order to track not
851  * only what share bits are currently in force, but also what
852  * combinations of share bits previous opens have used.  This allows us
853  * to enforce the recommendation of rfc 3530 14.2.19 that the server
854  * return an error if the client attempt to downgrade to a combination
855  * of share bits not explicable by closing some of its previous opens.
856  *
857  * XXX: This enforcement is actually incomplete, since we don't keep
858  * track of access/deny bit combinations; so, e.g., we allow:
859  *
860  *	OPEN allow read, deny write
861  *	OPEN allow both, deny none
862  *	DOWNGRADE allow read, deny none
863  *
864  * which we should reject.
865  */
866 static unsigned int
867 bmap_to_share_mode(unsigned long bmap) {
868 	int i;
869 	unsigned int access = 0;
870 
871 	for (i = 1; i < 4; i++) {
872 		if (test_bit(i, &bmap))
873 			access |= i;
874 	}
875 	return access;
876 }
877 
878 /* set share access for a given stateid */
879 static inline void
880 set_access(u32 access, struct nfs4_ol_stateid *stp)
881 {
882 	unsigned char mask = 1 << access;
883 
884 	WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
885 	stp->st_access_bmap |= mask;
886 }
887 
888 /* clear share access for a given stateid */
889 static inline void
890 clear_access(u32 access, struct nfs4_ol_stateid *stp)
891 {
892 	unsigned char mask = 1 << access;
893 
894 	WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
895 	stp->st_access_bmap &= ~mask;
896 }
897 
898 /* test whether a given stateid has access */
899 static inline bool
900 test_access(u32 access, struct nfs4_ol_stateid *stp)
901 {
902 	unsigned char mask = 1 << access;
903 
904 	return (bool)(stp->st_access_bmap & mask);
905 }
906 
907 /* set share deny for a given stateid */
908 static inline void
909 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
910 {
911 	unsigned char mask = 1 << deny;
912 
913 	WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
914 	stp->st_deny_bmap |= mask;
915 }
916 
917 /* clear share deny for a given stateid */
918 static inline void
919 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
920 {
921 	unsigned char mask = 1 << deny;
922 
923 	WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
924 	stp->st_deny_bmap &= ~mask;
925 }
926 
927 /* test whether a given stateid is denying specific access */
928 static inline bool
929 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
930 {
931 	unsigned char mask = 1 << deny;
932 
933 	return (bool)(stp->st_deny_bmap & mask);
934 }
935 
936 static int nfs4_access_to_omode(u32 access)
937 {
938 	switch (access & NFS4_SHARE_ACCESS_BOTH) {
939 	case NFS4_SHARE_ACCESS_READ:
940 		return O_RDONLY;
941 	case NFS4_SHARE_ACCESS_WRITE:
942 		return O_WRONLY;
943 	case NFS4_SHARE_ACCESS_BOTH:
944 		return O_RDWR;
945 	}
946 	WARN_ON_ONCE(1);
947 	return O_RDONLY;
948 }
949 
950 /*
951  * A stateid that had a deny mode associated with it is being released
952  * or downgraded. Recalculate the deny mode on the file.
953  */
954 static void
955 recalculate_deny_mode(struct nfs4_file *fp)
956 {
957 	struct nfs4_ol_stateid *stp;
958 
959 	spin_lock(&fp->fi_lock);
960 	fp->fi_share_deny = 0;
961 	list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
962 		fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
963 	spin_unlock(&fp->fi_lock);
964 }
965 
966 static void
967 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
968 {
969 	int i;
970 	bool change = false;
971 
972 	for (i = 1; i < 4; i++) {
973 		if ((i & deny) != i) {
974 			change = true;
975 			clear_deny(i, stp);
976 		}
977 	}
978 
979 	/* Recalculate per-file deny mode if there was a change */
980 	if (change)
981 		recalculate_deny_mode(stp->st_stid.sc_file);
982 }
983 
984 /* release all access and file references for a given stateid */
985 static void
986 release_all_access(struct nfs4_ol_stateid *stp)
987 {
988 	int i;
989 	struct nfs4_file *fp = stp->st_stid.sc_file;
990 
991 	if (fp && stp->st_deny_bmap != 0)
992 		recalculate_deny_mode(fp);
993 
994 	for (i = 1; i < 4; i++) {
995 		if (test_access(i, stp))
996 			nfs4_file_put_access(stp->st_stid.sc_file, i);
997 		clear_access(i, stp);
998 	}
999 }
1000 
1001 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1002 {
1003 	kfree(sop->so_owner.data);
1004 	sop->so_ops->so_free(sop);
1005 }
1006 
1007 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1008 {
1009 	struct nfs4_client *clp = sop->so_client;
1010 
1011 	might_lock(&clp->cl_lock);
1012 
1013 	if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1014 		return;
1015 	sop->so_ops->so_unhash(sop);
1016 	spin_unlock(&clp->cl_lock);
1017 	nfs4_free_stateowner(sop);
1018 }
1019 
1020 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1021 {
1022 	struct nfs4_file *fp = stp->st_stid.sc_file;
1023 
1024 	lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1025 
1026 	if (list_empty(&stp->st_perfile))
1027 		return false;
1028 
1029 	spin_lock(&fp->fi_lock);
1030 	list_del_init(&stp->st_perfile);
1031 	spin_unlock(&fp->fi_lock);
1032 	list_del(&stp->st_perstateowner);
1033 	return true;
1034 }
1035 
1036 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1037 {
1038 	struct nfs4_ol_stateid *stp = openlockstateid(stid);
1039 
1040 	put_clnt_odstate(stp->st_clnt_odstate);
1041 	release_all_access(stp);
1042 	if (stp->st_stateowner)
1043 		nfs4_put_stateowner(stp->st_stateowner);
1044 	kmem_cache_free(stateid_slab, stid);
1045 }
1046 
1047 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1048 {
1049 	struct nfs4_ol_stateid *stp = openlockstateid(stid);
1050 	struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1051 	struct file *file;
1052 
1053 	file = find_any_file(stp->st_stid.sc_file);
1054 	if (file)
1055 		filp_close(file, (fl_owner_t)lo);
1056 	nfs4_free_ol_stateid(stid);
1057 }
1058 
1059 /*
1060  * Put the persistent reference to an already unhashed generic stateid, while
1061  * holding the cl_lock. If it's the last reference, then put it onto the
1062  * reaplist for later destruction.
1063  */
1064 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1065 				       struct list_head *reaplist)
1066 {
1067 	struct nfs4_stid *s = &stp->st_stid;
1068 	struct nfs4_client *clp = s->sc_client;
1069 
1070 	lockdep_assert_held(&clp->cl_lock);
1071 
1072 	WARN_ON_ONCE(!list_empty(&stp->st_locks));
1073 
1074 	if (!atomic_dec_and_test(&s->sc_count)) {
1075 		wake_up_all(&close_wq);
1076 		return;
1077 	}
1078 
1079 	idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1080 	list_add(&stp->st_locks, reaplist);
1081 }
1082 
1083 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1084 {
1085 	struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1086 
1087 	lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1088 
1089 	list_del_init(&stp->st_locks);
1090 	nfs4_unhash_stid(&stp->st_stid);
1091 	return unhash_ol_stateid(stp);
1092 }
1093 
1094 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1095 {
1096 	struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1097 	bool unhashed;
1098 
1099 	spin_lock(&oo->oo_owner.so_client->cl_lock);
1100 	unhashed = unhash_lock_stateid(stp);
1101 	spin_unlock(&oo->oo_owner.so_client->cl_lock);
1102 	if (unhashed)
1103 		nfs4_put_stid(&stp->st_stid);
1104 }
1105 
1106 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1107 {
1108 	struct nfs4_client *clp = lo->lo_owner.so_client;
1109 
1110 	lockdep_assert_held(&clp->cl_lock);
1111 
1112 	list_del_init(&lo->lo_owner.so_strhash);
1113 }
1114 
1115 /*
1116  * Free a list of generic stateids that were collected earlier after being
1117  * fully unhashed.
1118  */
1119 static void
1120 free_ol_stateid_reaplist(struct list_head *reaplist)
1121 {
1122 	struct nfs4_ol_stateid *stp;
1123 	struct nfs4_file *fp;
1124 
1125 	might_sleep();
1126 
1127 	while (!list_empty(reaplist)) {
1128 		stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1129 				       st_locks);
1130 		list_del(&stp->st_locks);
1131 		fp = stp->st_stid.sc_file;
1132 		stp->st_stid.sc_free(&stp->st_stid);
1133 		if (fp)
1134 			put_nfs4_file(fp);
1135 	}
1136 }
1137 
1138 static void release_lockowner(struct nfs4_lockowner *lo)
1139 {
1140 	struct nfs4_client *clp = lo->lo_owner.so_client;
1141 	struct nfs4_ol_stateid *stp;
1142 	struct list_head reaplist;
1143 
1144 	INIT_LIST_HEAD(&reaplist);
1145 
1146 	spin_lock(&clp->cl_lock);
1147 	unhash_lockowner_locked(lo);
1148 	while (!list_empty(&lo->lo_owner.so_stateids)) {
1149 		stp = list_first_entry(&lo->lo_owner.so_stateids,
1150 				struct nfs4_ol_stateid, st_perstateowner);
1151 		WARN_ON(!unhash_lock_stateid(stp));
1152 		put_ol_stateid_locked(stp, &reaplist);
1153 	}
1154 	spin_unlock(&clp->cl_lock);
1155 	free_ol_stateid_reaplist(&reaplist);
1156 	nfs4_put_stateowner(&lo->lo_owner);
1157 }
1158 
1159 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1160 				       struct list_head *reaplist)
1161 {
1162 	struct nfs4_ol_stateid *stp;
1163 
1164 	lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1165 
1166 	while (!list_empty(&open_stp->st_locks)) {
1167 		stp = list_entry(open_stp->st_locks.next,
1168 				struct nfs4_ol_stateid, st_locks);
1169 		WARN_ON(!unhash_lock_stateid(stp));
1170 		put_ol_stateid_locked(stp, reaplist);
1171 	}
1172 }
1173 
1174 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1175 				struct list_head *reaplist)
1176 {
1177 	bool unhashed;
1178 
1179 	lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1180 
1181 	unhashed = unhash_ol_stateid(stp);
1182 	release_open_stateid_locks(stp, reaplist);
1183 	return unhashed;
1184 }
1185 
1186 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1187 {
1188 	LIST_HEAD(reaplist);
1189 
1190 	spin_lock(&stp->st_stid.sc_client->cl_lock);
1191 	if (unhash_open_stateid(stp, &reaplist))
1192 		put_ol_stateid_locked(stp, &reaplist);
1193 	spin_unlock(&stp->st_stid.sc_client->cl_lock);
1194 	free_ol_stateid_reaplist(&reaplist);
1195 }
1196 
1197 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1198 {
1199 	struct nfs4_client *clp = oo->oo_owner.so_client;
1200 
1201 	lockdep_assert_held(&clp->cl_lock);
1202 
1203 	list_del_init(&oo->oo_owner.so_strhash);
1204 	list_del_init(&oo->oo_perclient);
1205 }
1206 
1207 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1208 {
1209 	struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1210 					  nfsd_net_id);
1211 	struct nfs4_ol_stateid *s;
1212 
1213 	spin_lock(&nn->client_lock);
1214 	s = oo->oo_last_closed_stid;
1215 	if (s) {
1216 		list_del_init(&oo->oo_close_lru);
1217 		oo->oo_last_closed_stid = NULL;
1218 	}
1219 	spin_unlock(&nn->client_lock);
1220 	if (s)
1221 		nfs4_put_stid(&s->st_stid);
1222 }
1223 
1224 static void release_openowner(struct nfs4_openowner *oo)
1225 {
1226 	struct nfs4_ol_stateid *stp;
1227 	struct nfs4_client *clp = oo->oo_owner.so_client;
1228 	struct list_head reaplist;
1229 
1230 	INIT_LIST_HEAD(&reaplist);
1231 
1232 	spin_lock(&clp->cl_lock);
1233 	unhash_openowner_locked(oo);
1234 	while (!list_empty(&oo->oo_owner.so_stateids)) {
1235 		stp = list_first_entry(&oo->oo_owner.so_stateids,
1236 				struct nfs4_ol_stateid, st_perstateowner);
1237 		if (unhash_open_stateid(stp, &reaplist))
1238 			put_ol_stateid_locked(stp, &reaplist);
1239 	}
1240 	spin_unlock(&clp->cl_lock);
1241 	free_ol_stateid_reaplist(&reaplist);
1242 	release_last_closed_stateid(oo);
1243 	nfs4_put_stateowner(&oo->oo_owner);
1244 }
1245 
1246 static inline int
1247 hash_sessionid(struct nfs4_sessionid *sessionid)
1248 {
1249 	struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1250 
1251 	return sid->sequence % SESSION_HASH_SIZE;
1252 }
1253 
1254 #ifdef CONFIG_SUNRPC_DEBUG
1255 static inline void
1256 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1257 {
1258 	u32 *ptr = (u32 *)(&sessionid->data[0]);
1259 	dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1260 }
1261 #else
1262 static inline void
1263 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1264 {
1265 }
1266 #endif
1267 
1268 /*
1269  * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1270  * won't be used for replay.
1271  */
1272 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1273 {
1274 	struct nfs4_stateowner *so = cstate->replay_owner;
1275 
1276 	if (nfserr == nfserr_replay_me)
1277 		return;
1278 
1279 	if (!seqid_mutating_err(ntohl(nfserr))) {
1280 		nfsd4_cstate_clear_replay(cstate);
1281 		return;
1282 	}
1283 	if (!so)
1284 		return;
1285 	if (so->so_is_open_owner)
1286 		release_last_closed_stateid(openowner(so));
1287 	so->so_seqid++;
1288 	return;
1289 }
1290 
1291 static void
1292 gen_sessionid(struct nfsd4_session *ses)
1293 {
1294 	struct nfs4_client *clp = ses->se_client;
1295 	struct nfsd4_sessionid *sid;
1296 
1297 	sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1298 	sid->clientid = clp->cl_clientid;
1299 	sid->sequence = current_sessionid++;
1300 	sid->reserved = 0;
1301 }
1302 
1303 /*
1304  * The protocol defines ca_maxresponssize_cached to include the size of
1305  * the rpc header, but all we need to cache is the data starting after
1306  * the end of the initial SEQUENCE operation--the rest we regenerate
1307  * each time.  Therefore we can advertise a ca_maxresponssize_cached
1308  * value that is the number of bytes in our cache plus a few additional
1309  * bytes.  In order to stay on the safe side, and not promise more than
1310  * we can cache, those additional bytes must be the minimum possible: 24
1311  * bytes of rpc header (xid through accept state, with AUTH_NULL
1312  * verifier), 12 for the compound header (with zero-length tag), and 44
1313  * for the SEQUENCE op response:
1314  */
1315 #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
1316 
1317 static void
1318 free_session_slots(struct nfsd4_session *ses)
1319 {
1320 	int i;
1321 
1322 	for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1323 		kfree(ses->se_slots[i]);
1324 }
1325 
1326 /*
1327  * We don't actually need to cache the rpc and session headers, so we
1328  * can allocate a little less for each slot:
1329  */
1330 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1331 {
1332 	u32 size;
1333 
1334 	if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1335 		size = 0;
1336 	else
1337 		size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1338 	return size + sizeof(struct nfsd4_slot);
1339 }
1340 
1341 /*
1342  * XXX: If we run out of reserved DRC memory we could (up to a point)
1343  * re-negotiate active sessions and reduce their slot usage to make
1344  * room for new connections. For now we just fail the create session.
1345  */
1346 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1347 {
1348 	u32 slotsize = slot_bytes(ca);
1349 	u32 num = ca->maxreqs;
1350 	int avail;
1351 
1352 	spin_lock(&nfsd_drc_lock);
1353 	avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1354 		    nfsd_drc_max_mem - nfsd_drc_mem_used);
1355 	num = min_t(int, num, avail / slotsize);
1356 	nfsd_drc_mem_used += num * slotsize;
1357 	spin_unlock(&nfsd_drc_lock);
1358 
1359 	return num;
1360 }
1361 
1362 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1363 {
1364 	int slotsize = slot_bytes(ca);
1365 
1366 	spin_lock(&nfsd_drc_lock);
1367 	nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1368 	spin_unlock(&nfsd_drc_lock);
1369 }
1370 
1371 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1372 					   struct nfsd4_channel_attrs *battrs)
1373 {
1374 	int numslots = fattrs->maxreqs;
1375 	int slotsize = slot_bytes(fattrs);
1376 	struct nfsd4_session *new;
1377 	int mem, i;
1378 
1379 	BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1380 			+ sizeof(struct nfsd4_session) > PAGE_SIZE);
1381 	mem = numslots * sizeof(struct nfsd4_slot *);
1382 
1383 	new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1384 	if (!new)
1385 		return NULL;
1386 	/* allocate each struct nfsd4_slot and data cache in one piece */
1387 	for (i = 0; i < numslots; i++) {
1388 		new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1389 		if (!new->se_slots[i])
1390 			goto out_free;
1391 	}
1392 
1393 	memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1394 	memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1395 
1396 	return new;
1397 out_free:
1398 	while (i--)
1399 		kfree(new->se_slots[i]);
1400 	kfree(new);
1401 	return NULL;
1402 }
1403 
1404 static void free_conn(struct nfsd4_conn *c)
1405 {
1406 	svc_xprt_put(c->cn_xprt);
1407 	kfree(c);
1408 }
1409 
1410 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1411 {
1412 	struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1413 	struct nfs4_client *clp = c->cn_session->se_client;
1414 
1415 	spin_lock(&clp->cl_lock);
1416 	if (!list_empty(&c->cn_persession)) {
1417 		list_del(&c->cn_persession);
1418 		free_conn(c);
1419 	}
1420 	nfsd4_probe_callback(clp);
1421 	spin_unlock(&clp->cl_lock);
1422 }
1423 
1424 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1425 {
1426 	struct nfsd4_conn *conn;
1427 
1428 	conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1429 	if (!conn)
1430 		return NULL;
1431 	svc_xprt_get(rqstp->rq_xprt);
1432 	conn->cn_xprt = rqstp->rq_xprt;
1433 	conn->cn_flags = flags;
1434 	INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1435 	return conn;
1436 }
1437 
1438 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1439 {
1440 	conn->cn_session = ses;
1441 	list_add(&conn->cn_persession, &ses->se_conns);
1442 }
1443 
1444 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1445 {
1446 	struct nfs4_client *clp = ses->se_client;
1447 
1448 	spin_lock(&clp->cl_lock);
1449 	__nfsd4_hash_conn(conn, ses);
1450 	spin_unlock(&clp->cl_lock);
1451 }
1452 
1453 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1454 {
1455 	conn->cn_xpt_user.callback = nfsd4_conn_lost;
1456 	return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1457 }
1458 
1459 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1460 {
1461 	int ret;
1462 
1463 	nfsd4_hash_conn(conn, ses);
1464 	ret = nfsd4_register_conn(conn);
1465 	if (ret)
1466 		/* oops; xprt is already down: */
1467 		nfsd4_conn_lost(&conn->cn_xpt_user);
1468 	/* We may have gained or lost a callback channel: */
1469 	nfsd4_probe_callback_sync(ses->se_client);
1470 }
1471 
1472 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1473 {
1474 	u32 dir = NFS4_CDFC4_FORE;
1475 
1476 	if (cses->flags & SESSION4_BACK_CHAN)
1477 		dir |= NFS4_CDFC4_BACK;
1478 	return alloc_conn(rqstp, dir);
1479 }
1480 
1481 /* must be called under client_lock */
1482 static void nfsd4_del_conns(struct nfsd4_session *s)
1483 {
1484 	struct nfs4_client *clp = s->se_client;
1485 	struct nfsd4_conn *c;
1486 
1487 	spin_lock(&clp->cl_lock);
1488 	while (!list_empty(&s->se_conns)) {
1489 		c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1490 		list_del_init(&c->cn_persession);
1491 		spin_unlock(&clp->cl_lock);
1492 
1493 		unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1494 		free_conn(c);
1495 
1496 		spin_lock(&clp->cl_lock);
1497 	}
1498 	spin_unlock(&clp->cl_lock);
1499 }
1500 
1501 static void __free_session(struct nfsd4_session *ses)
1502 {
1503 	free_session_slots(ses);
1504 	kfree(ses);
1505 }
1506 
1507 static void free_session(struct nfsd4_session *ses)
1508 {
1509 	nfsd4_del_conns(ses);
1510 	nfsd4_put_drc_mem(&ses->se_fchannel);
1511 	__free_session(ses);
1512 }
1513 
1514 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1515 {
1516 	int idx;
1517 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1518 
1519 	new->se_client = clp;
1520 	gen_sessionid(new);
1521 
1522 	INIT_LIST_HEAD(&new->se_conns);
1523 
1524 	new->se_cb_seq_nr = 1;
1525 	new->se_flags = cses->flags;
1526 	new->se_cb_prog = cses->callback_prog;
1527 	new->se_cb_sec = cses->cb_sec;
1528 	atomic_set(&new->se_ref, 0);
1529 	idx = hash_sessionid(&new->se_sessionid);
1530 	list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1531 	spin_lock(&clp->cl_lock);
1532 	list_add(&new->se_perclnt, &clp->cl_sessions);
1533 	spin_unlock(&clp->cl_lock);
1534 
1535 	{
1536 		struct sockaddr *sa = svc_addr(rqstp);
1537 		/*
1538 		 * This is a little silly; with sessions there's no real
1539 		 * use for the callback address.  Use the peer address
1540 		 * as a reasonable default for now, but consider fixing
1541 		 * the rpc client not to require an address in the
1542 		 * future:
1543 		 */
1544 		rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1545 		clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1546 	}
1547 }
1548 
1549 /* caller must hold client_lock */
1550 static struct nfsd4_session *
1551 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1552 {
1553 	struct nfsd4_session *elem;
1554 	int idx;
1555 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1556 
1557 	lockdep_assert_held(&nn->client_lock);
1558 
1559 	dump_sessionid(__func__, sessionid);
1560 	idx = hash_sessionid(sessionid);
1561 	/* Search in the appropriate list */
1562 	list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1563 		if (!memcmp(elem->se_sessionid.data, sessionid->data,
1564 			    NFS4_MAX_SESSIONID_LEN)) {
1565 			return elem;
1566 		}
1567 	}
1568 
1569 	dprintk("%s: session not found\n", __func__);
1570 	return NULL;
1571 }
1572 
1573 static struct nfsd4_session *
1574 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1575 		__be32 *ret)
1576 {
1577 	struct nfsd4_session *session;
1578 	__be32 status = nfserr_badsession;
1579 
1580 	session = __find_in_sessionid_hashtbl(sessionid, net);
1581 	if (!session)
1582 		goto out;
1583 	status = nfsd4_get_session_locked(session);
1584 	if (status)
1585 		session = NULL;
1586 out:
1587 	*ret = status;
1588 	return session;
1589 }
1590 
1591 /* caller must hold client_lock */
1592 static void
1593 unhash_session(struct nfsd4_session *ses)
1594 {
1595 	struct nfs4_client *clp = ses->se_client;
1596 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1597 
1598 	lockdep_assert_held(&nn->client_lock);
1599 
1600 	list_del(&ses->se_hash);
1601 	spin_lock(&ses->se_client->cl_lock);
1602 	list_del(&ses->se_perclnt);
1603 	spin_unlock(&ses->se_client->cl_lock);
1604 }
1605 
1606 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1607 static int
1608 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1609 {
1610 	/*
1611 	 * We're assuming the clid was not given out from a boot
1612 	 * precisely 2^32 (about 136 years) before this one.  That seems
1613 	 * a safe assumption:
1614 	 */
1615 	if (clid->cl_boot == (u32)nn->boot_time)
1616 		return 0;
1617 	dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1618 		clid->cl_boot, clid->cl_id, nn->boot_time);
1619 	return 1;
1620 }
1621 
1622 /*
1623  * XXX Should we use a slab cache ?
1624  * This type of memory management is somewhat inefficient, but we use it
1625  * anyway since SETCLIENTID is not a common operation.
1626  */
1627 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1628 {
1629 	struct nfs4_client *clp;
1630 	int i;
1631 
1632 	clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1633 	if (clp == NULL)
1634 		return NULL;
1635 	clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1636 	if (clp->cl_name.data == NULL)
1637 		goto err_no_name;
1638 	clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1639 			OWNER_HASH_SIZE, GFP_KERNEL);
1640 	if (!clp->cl_ownerstr_hashtbl)
1641 		goto err_no_hashtbl;
1642 	for (i = 0; i < OWNER_HASH_SIZE; i++)
1643 		INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1644 	clp->cl_name.len = name.len;
1645 	INIT_LIST_HEAD(&clp->cl_sessions);
1646 	idr_init(&clp->cl_stateids);
1647 	atomic_set(&clp->cl_refcount, 0);
1648 	clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1649 	INIT_LIST_HEAD(&clp->cl_idhash);
1650 	INIT_LIST_HEAD(&clp->cl_openowners);
1651 	INIT_LIST_HEAD(&clp->cl_delegations);
1652 	INIT_LIST_HEAD(&clp->cl_lru);
1653 	INIT_LIST_HEAD(&clp->cl_revoked);
1654 #ifdef CONFIG_NFSD_PNFS
1655 	INIT_LIST_HEAD(&clp->cl_lo_states);
1656 #endif
1657 	spin_lock_init(&clp->cl_lock);
1658 	rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1659 	return clp;
1660 err_no_hashtbl:
1661 	kfree(clp->cl_name.data);
1662 err_no_name:
1663 	kfree(clp);
1664 	return NULL;
1665 }
1666 
1667 static void
1668 free_client(struct nfs4_client *clp)
1669 {
1670 	while (!list_empty(&clp->cl_sessions)) {
1671 		struct nfsd4_session *ses;
1672 		ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1673 				se_perclnt);
1674 		list_del(&ses->se_perclnt);
1675 		WARN_ON_ONCE(atomic_read(&ses->se_ref));
1676 		free_session(ses);
1677 	}
1678 	rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1679 	free_svc_cred(&clp->cl_cred);
1680 	kfree(clp->cl_ownerstr_hashtbl);
1681 	kfree(clp->cl_name.data);
1682 	idr_destroy(&clp->cl_stateids);
1683 	kfree(clp);
1684 }
1685 
1686 /* must be called under the client_lock */
1687 static void
1688 unhash_client_locked(struct nfs4_client *clp)
1689 {
1690 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1691 	struct nfsd4_session *ses;
1692 
1693 	lockdep_assert_held(&nn->client_lock);
1694 
1695 	/* Mark the client as expired! */
1696 	clp->cl_time = 0;
1697 	/* Make it invisible */
1698 	if (!list_empty(&clp->cl_idhash)) {
1699 		list_del_init(&clp->cl_idhash);
1700 		if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1701 			rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1702 		else
1703 			rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1704 	}
1705 	list_del_init(&clp->cl_lru);
1706 	spin_lock(&clp->cl_lock);
1707 	list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1708 		list_del_init(&ses->se_hash);
1709 	spin_unlock(&clp->cl_lock);
1710 }
1711 
1712 static void
1713 unhash_client(struct nfs4_client *clp)
1714 {
1715 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1716 
1717 	spin_lock(&nn->client_lock);
1718 	unhash_client_locked(clp);
1719 	spin_unlock(&nn->client_lock);
1720 }
1721 
1722 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1723 {
1724 	if (atomic_read(&clp->cl_refcount))
1725 		return nfserr_jukebox;
1726 	unhash_client_locked(clp);
1727 	return nfs_ok;
1728 }
1729 
1730 static void
1731 __destroy_client(struct nfs4_client *clp)
1732 {
1733 	struct nfs4_openowner *oo;
1734 	struct nfs4_delegation *dp;
1735 	struct list_head reaplist;
1736 
1737 	INIT_LIST_HEAD(&reaplist);
1738 	spin_lock(&state_lock);
1739 	while (!list_empty(&clp->cl_delegations)) {
1740 		dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1741 		WARN_ON(!unhash_delegation_locked(dp));
1742 		list_add(&dp->dl_recall_lru, &reaplist);
1743 	}
1744 	spin_unlock(&state_lock);
1745 	while (!list_empty(&reaplist)) {
1746 		dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1747 		list_del_init(&dp->dl_recall_lru);
1748 		put_clnt_odstate(dp->dl_clnt_odstate);
1749 		nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1750 		nfs4_put_stid(&dp->dl_stid);
1751 	}
1752 	while (!list_empty(&clp->cl_revoked)) {
1753 		dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1754 		list_del_init(&dp->dl_recall_lru);
1755 		nfs4_put_stid(&dp->dl_stid);
1756 	}
1757 	while (!list_empty(&clp->cl_openowners)) {
1758 		oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1759 		nfs4_get_stateowner(&oo->oo_owner);
1760 		release_openowner(oo);
1761 	}
1762 	nfsd4_return_all_client_layouts(clp);
1763 	nfsd4_shutdown_callback(clp);
1764 	if (clp->cl_cb_conn.cb_xprt)
1765 		svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1766 	free_client(clp);
1767 }
1768 
1769 static void
1770 destroy_client(struct nfs4_client *clp)
1771 {
1772 	unhash_client(clp);
1773 	__destroy_client(clp);
1774 }
1775 
1776 static void expire_client(struct nfs4_client *clp)
1777 {
1778 	unhash_client(clp);
1779 	nfsd4_client_record_remove(clp);
1780 	__destroy_client(clp);
1781 }
1782 
1783 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1784 {
1785 	memcpy(target->cl_verifier.data, source->data,
1786 			sizeof(target->cl_verifier.data));
1787 }
1788 
1789 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1790 {
1791 	target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1792 	target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1793 }
1794 
1795 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1796 {
1797 	if (source->cr_principal) {
1798 		target->cr_principal =
1799 				kstrdup(source->cr_principal, GFP_KERNEL);
1800 		if (target->cr_principal == NULL)
1801 			return -ENOMEM;
1802 	} else
1803 		target->cr_principal = NULL;
1804 	target->cr_flavor = source->cr_flavor;
1805 	target->cr_uid = source->cr_uid;
1806 	target->cr_gid = source->cr_gid;
1807 	target->cr_group_info = source->cr_group_info;
1808 	get_group_info(target->cr_group_info);
1809 	target->cr_gss_mech = source->cr_gss_mech;
1810 	if (source->cr_gss_mech)
1811 		gss_mech_get(source->cr_gss_mech);
1812 	return 0;
1813 }
1814 
1815 static int
1816 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1817 {
1818 	if (o1->len < o2->len)
1819 		return -1;
1820 	if (o1->len > o2->len)
1821 		return 1;
1822 	return memcmp(o1->data, o2->data, o1->len);
1823 }
1824 
1825 static int same_name(const char *n1, const char *n2)
1826 {
1827 	return 0 == memcmp(n1, n2, HEXDIR_LEN);
1828 }
1829 
1830 static int
1831 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1832 {
1833 	return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1834 }
1835 
1836 static int
1837 same_clid(clientid_t *cl1, clientid_t *cl2)
1838 {
1839 	return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1840 }
1841 
1842 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1843 {
1844 	int i;
1845 
1846 	if (g1->ngroups != g2->ngroups)
1847 		return false;
1848 	for (i=0; i<g1->ngroups; i++)
1849 		if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1850 			return false;
1851 	return true;
1852 }
1853 
1854 /*
1855  * RFC 3530 language requires clid_inuse be returned when the
1856  * "principal" associated with a requests differs from that previously
1857  * used.  We use uid, gid's, and gss principal string as our best
1858  * approximation.  We also don't want to allow non-gss use of a client
1859  * established using gss: in theory cr_principal should catch that
1860  * change, but in practice cr_principal can be null even in the gss case
1861  * since gssd doesn't always pass down a principal string.
1862  */
1863 static bool is_gss_cred(struct svc_cred *cr)
1864 {
1865 	/* Is cr_flavor one of the gss "pseudoflavors"?: */
1866 	return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1867 }
1868 
1869 
1870 static bool
1871 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1872 {
1873 	if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1874 		|| (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1875 		|| (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1876 		|| !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1877 		return false;
1878 	if (cr1->cr_principal == cr2->cr_principal)
1879 		return true;
1880 	if (!cr1->cr_principal || !cr2->cr_principal)
1881 		return false;
1882 	return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1883 }
1884 
1885 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1886 {
1887 	struct svc_cred *cr = &rqstp->rq_cred;
1888 	u32 service;
1889 
1890 	if (!cr->cr_gss_mech)
1891 		return false;
1892 	service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1893 	return service == RPC_GSS_SVC_INTEGRITY ||
1894 	       service == RPC_GSS_SVC_PRIVACY;
1895 }
1896 
1897 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1898 {
1899 	struct svc_cred *cr = &rqstp->rq_cred;
1900 
1901 	if (!cl->cl_mach_cred)
1902 		return true;
1903 	if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1904 		return false;
1905 	if (!svc_rqst_integrity_protected(rqstp))
1906 		return false;
1907 	if (!cr->cr_principal)
1908 		return false;
1909 	return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1910 }
1911 
1912 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1913 {
1914 	__be32 verf[2];
1915 
1916 	/*
1917 	 * This is opaque to client, so no need to byte-swap. Use
1918 	 * __force to keep sparse happy
1919 	 */
1920 	verf[0] = (__force __be32)get_seconds();
1921 	verf[1] = (__force __be32)nn->clverifier_counter++;
1922 	memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1923 }
1924 
1925 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1926 {
1927 	clp->cl_clientid.cl_boot = nn->boot_time;
1928 	clp->cl_clientid.cl_id = nn->clientid_counter++;
1929 	gen_confirm(clp, nn);
1930 }
1931 
1932 static struct nfs4_stid *
1933 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1934 {
1935 	struct nfs4_stid *ret;
1936 
1937 	ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1938 	if (!ret || !ret->sc_type)
1939 		return NULL;
1940 	return ret;
1941 }
1942 
1943 static struct nfs4_stid *
1944 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1945 {
1946 	struct nfs4_stid *s;
1947 
1948 	spin_lock(&cl->cl_lock);
1949 	s = find_stateid_locked(cl, t);
1950 	if (s != NULL) {
1951 		if (typemask & s->sc_type)
1952 			atomic_inc(&s->sc_count);
1953 		else
1954 			s = NULL;
1955 	}
1956 	spin_unlock(&cl->cl_lock);
1957 	return s;
1958 }
1959 
1960 static struct nfs4_client *create_client(struct xdr_netobj name,
1961 		struct svc_rqst *rqstp, nfs4_verifier *verf)
1962 {
1963 	struct nfs4_client *clp;
1964 	struct sockaddr *sa = svc_addr(rqstp);
1965 	int ret;
1966 	struct net *net = SVC_NET(rqstp);
1967 
1968 	clp = alloc_client(name);
1969 	if (clp == NULL)
1970 		return NULL;
1971 
1972 	ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1973 	if (ret) {
1974 		free_client(clp);
1975 		return NULL;
1976 	}
1977 	nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
1978 	clp->cl_time = get_seconds();
1979 	clear_bit(0, &clp->cl_cb_slot_busy);
1980 	copy_verf(clp, verf);
1981 	rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1982 	clp->cl_cb_session = NULL;
1983 	clp->net = net;
1984 	return clp;
1985 }
1986 
1987 static void
1988 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1989 {
1990 	struct rb_node **new = &(root->rb_node), *parent = NULL;
1991 	struct nfs4_client *clp;
1992 
1993 	while (*new) {
1994 		clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1995 		parent = *new;
1996 
1997 		if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1998 			new = &((*new)->rb_left);
1999 		else
2000 			new = &((*new)->rb_right);
2001 	}
2002 
2003 	rb_link_node(&new_clp->cl_namenode, parent, new);
2004 	rb_insert_color(&new_clp->cl_namenode, root);
2005 }
2006 
2007 static struct nfs4_client *
2008 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2009 {
2010 	int cmp;
2011 	struct rb_node *node = root->rb_node;
2012 	struct nfs4_client *clp;
2013 
2014 	while (node) {
2015 		clp = rb_entry(node, struct nfs4_client, cl_namenode);
2016 		cmp = compare_blob(&clp->cl_name, name);
2017 		if (cmp > 0)
2018 			node = node->rb_left;
2019 		else if (cmp < 0)
2020 			node = node->rb_right;
2021 		else
2022 			return clp;
2023 	}
2024 	return NULL;
2025 }
2026 
2027 static void
2028 add_to_unconfirmed(struct nfs4_client *clp)
2029 {
2030 	unsigned int idhashval;
2031 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2032 
2033 	lockdep_assert_held(&nn->client_lock);
2034 
2035 	clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2036 	add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2037 	idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2038 	list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2039 	renew_client_locked(clp);
2040 }
2041 
2042 static void
2043 move_to_confirmed(struct nfs4_client *clp)
2044 {
2045 	unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2046 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2047 
2048 	lockdep_assert_held(&nn->client_lock);
2049 
2050 	dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2051 	list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2052 	rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2053 	add_clp_to_name_tree(clp, &nn->conf_name_tree);
2054 	set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2055 	renew_client_locked(clp);
2056 }
2057 
2058 static struct nfs4_client *
2059 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2060 {
2061 	struct nfs4_client *clp;
2062 	unsigned int idhashval = clientid_hashval(clid->cl_id);
2063 
2064 	list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2065 		if (same_clid(&clp->cl_clientid, clid)) {
2066 			if ((bool)clp->cl_minorversion != sessions)
2067 				return NULL;
2068 			renew_client_locked(clp);
2069 			return clp;
2070 		}
2071 	}
2072 	return NULL;
2073 }
2074 
2075 static struct nfs4_client *
2076 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2077 {
2078 	struct list_head *tbl = nn->conf_id_hashtbl;
2079 
2080 	lockdep_assert_held(&nn->client_lock);
2081 	return find_client_in_id_table(tbl, clid, sessions);
2082 }
2083 
2084 static struct nfs4_client *
2085 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2086 {
2087 	struct list_head *tbl = nn->unconf_id_hashtbl;
2088 
2089 	lockdep_assert_held(&nn->client_lock);
2090 	return find_client_in_id_table(tbl, clid, sessions);
2091 }
2092 
2093 static bool clp_used_exchangeid(struct nfs4_client *clp)
2094 {
2095 	return clp->cl_exchange_flags != 0;
2096 }
2097 
2098 static struct nfs4_client *
2099 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2100 {
2101 	lockdep_assert_held(&nn->client_lock);
2102 	return find_clp_in_name_tree(name, &nn->conf_name_tree);
2103 }
2104 
2105 static struct nfs4_client *
2106 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2107 {
2108 	lockdep_assert_held(&nn->client_lock);
2109 	return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2110 }
2111 
2112 static void
2113 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2114 {
2115 	struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2116 	struct sockaddr	*sa = svc_addr(rqstp);
2117 	u32 scopeid = rpc_get_scope_id(sa);
2118 	unsigned short expected_family;
2119 
2120 	/* Currently, we only support tcp and tcp6 for the callback channel */
2121 	if (se->se_callback_netid_len == 3 &&
2122 	    !memcmp(se->se_callback_netid_val, "tcp", 3))
2123 		expected_family = AF_INET;
2124 	else if (se->se_callback_netid_len == 4 &&
2125 		 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2126 		expected_family = AF_INET6;
2127 	else
2128 		goto out_err;
2129 
2130 	conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2131 					    se->se_callback_addr_len,
2132 					    (struct sockaddr *)&conn->cb_addr,
2133 					    sizeof(conn->cb_addr));
2134 
2135 	if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2136 		goto out_err;
2137 
2138 	if (conn->cb_addr.ss_family == AF_INET6)
2139 		((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2140 
2141 	conn->cb_prog = se->se_callback_prog;
2142 	conn->cb_ident = se->se_callback_ident;
2143 	memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2144 	return;
2145 out_err:
2146 	conn->cb_addr.ss_family = AF_UNSPEC;
2147 	conn->cb_addrlen = 0;
2148 	dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2149 		"will not receive delegations\n",
2150 		clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2151 
2152 	return;
2153 }
2154 
2155 /*
2156  * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2157  */
2158 static void
2159 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2160 {
2161 	struct xdr_buf *buf = resp->xdr.buf;
2162 	struct nfsd4_slot *slot = resp->cstate.slot;
2163 	unsigned int base;
2164 
2165 	dprintk("--> %s slot %p\n", __func__, slot);
2166 
2167 	slot->sl_opcnt = resp->opcnt;
2168 	slot->sl_status = resp->cstate.status;
2169 
2170 	slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2171 	if (nfsd4_not_cached(resp)) {
2172 		slot->sl_datalen = 0;
2173 		return;
2174 	}
2175 	base = resp->cstate.data_offset;
2176 	slot->sl_datalen = buf->len - base;
2177 	if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2178 		WARN("%s: sessions DRC could not cache compound\n", __func__);
2179 	return;
2180 }
2181 
2182 /*
2183  * Encode the replay sequence operation from the slot values.
2184  * If cachethis is FALSE encode the uncached rep error on the next
2185  * operation which sets resp->p and increments resp->opcnt for
2186  * nfs4svc_encode_compoundres.
2187  *
2188  */
2189 static __be32
2190 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2191 			  struct nfsd4_compoundres *resp)
2192 {
2193 	struct nfsd4_op *op;
2194 	struct nfsd4_slot *slot = resp->cstate.slot;
2195 
2196 	/* Encode the replayed sequence operation */
2197 	op = &args->ops[resp->opcnt - 1];
2198 	nfsd4_encode_operation(resp, op);
2199 
2200 	/* Return nfserr_retry_uncached_rep in next operation. */
2201 	if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2202 		op = &args->ops[resp->opcnt++];
2203 		op->status = nfserr_retry_uncached_rep;
2204 		nfsd4_encode_operation(resp, op);
2205 	}
2206 	return op->status;
2207 }
2208 
2209 /*
2210  * The sequence operation is not cached because we can use the slot and
2211  * session values.
2212  */
2213 static __be32
2214 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2215 			 struct nfsd4_sequence *seq)
2216 {
2217 	struct nfsd4_slot *slot = resp->cstate.slot;
2218 	struct xdr_stream *xdr = &resp->xdr;
2219 	__be32 *p;
2220 	__be32 status;
2221 
2222 	dprintk("--> %s slot %p\n", __func__, slot);
2223 
2224 	status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2225 	if (status)
2226 		return status;
2227 
2228 	p = xdr_reserve_space(xdr, slot->sl_datalen);
2229 	if (!p) {
2230 		WARN_ON_ONCE(1);
2231 		return nfserr_serverfault;
2232 	}
2233 	xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2234 	xdr_commit_encode(xdr);
2235 
2236 	resp->opcnt = slot->sl_opcnt;
2237 	return slot->sl_status;
2238 }
2239 
2240 /*
2241  * Set the exchange_id flags returned by the server.
2242  */
2243 static void
2244 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2245 {
2246 #ifdef CONFIG_NFSD_PNFS
2247 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2248 #else
2249 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2250 #endif
2251 
2252 	/* Referrals are supported, Migration is not. */
2253 	new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2254 
2255 	/* set the wire flags to return to client. */
2256 	clid->flags = new->cl_exchange_flags;
2257 }
2258 
2259 static bool client_has_state(struct nfs4_client *clp)
2260 {
2261 	/*
2262 	 * Note clp->cl_openowners check isn't quite right: there's no
2263 	 * need to count owners without stateid's.
2264 	 *
2265 	 * Also note we should probably be using this in 4.0 case too.
2266 	 */
2267 	return !list_empty(&clp->cl_openowners)
2268 #ifdef CONFIG_NFSD_PNFS
2269 		|| !list_empty(&clp->cl_lo_states)
2270 #endif
2271 		|| !list_empty(&clp->cl_delegations)
2272 		|| !list_empty(&clp->cl_sessions);
2273 }
2274 
2275 __be32
2276 nfsd4_exchange_id(struct svc_rqst *rqstp,
2277 		  struct nfsd4_compound_state *cstate,
2278 		  struct nfsd4_exchange_id *exid)
2279 {
2280 	struct nfs4_client *conf, *new;
2281 	struct nfs4_client *unconf = NULL;
2282 	__be32 status;
2283 	char			addr_str[INET6_ADDRSTRLEN];
2284 	nfs4_verifier		verf = exid->verifier;
2285 	struct sockaddr		*sa = svc_addr(rqstp);
2286 	bool	update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2287 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2288 
2289 	rpc_ntop(sa, addr_str, sizeof(addr_str));
2290 	dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2291 		"ip_addr=%s flags %x, spa_how %d\n",
2292 		__func__, rqstp, exid, exid->clname.len, exid->clname.data,
2293 		addr_str, exid->flags, exid->spa_how);
2294 
2295 	if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2296 		return nfserr_inval;
2297 
2298 	switch (exid->spa_how) {
2299 	case SP4_MACH_CRED:
2300 		if (!svc_rqst_integrity_protected(rqstp))
2301 			return nfserr_inval;
2302 	case SP4_NONE:
2303 		break;
2304 	default:				/* checked by xdr code */
2305 		WARN_ON_ONCE(1);
2306 	case SP4_SSV:
2307 		return nfserr_encr_alg_unsupp;
2308 	}
2309 
2310 	new = create_client(exid->clname, rqstp, &verf);
2311 	if (new == NULL)
2312 		return nfserr_jukebox;
2313 
2314 	/* Cases below refer to rfc 5661 section 18.35.4: */
2315 	spin_lock(&nn->client_lock);
2316 	conf = find_confirmed_client_by_name(&exid->clname, nn);
2317 	if (conf) {
2318 		bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2319 		bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2320 
2321 		if (update) {
2322 			if (!clp_used_exchangeid(conf)) { /* buggy client */
2323 				status = nfserr_inval;
2324 				goto out;
2325 			}
2326 			if (!mach_creds_match(conf, rqstp)) {
2327 				status = nfserr_wrong_cred;
2328 				goto out;
2329 			}
2330 			if (!creds_match) { /* case 9 */
2331 				status = nfserr_perm;
2332 				goto out;
2333 			}
2334 			if (!verfs_match) { /* case 8 */
2335 				status = nfserr_not_same;
2336 				goto out;
2337 			}
2338 			/* case 6 */
2339 			exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2340 			goto out_copy;
2341 		}
2342 		if (!creds_match) { /* case 3 */
2343 			if (client_has_state(conf)) {
2344 				status = nfserr_clid_inuse;
2345 				goto out;
2346 			}
2347 			goto out_new;
2348 		}
2349 		if (verfs_match) { /* case 2 */
2350 			conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2351 			goto out_copy;
2352 		}
2353 		/* case 5, client reboot */
2354 		conf = NULL;
2355 		goto out_new;
2356 	}
2357 
2358 	if (update) { /* case 7 */
2359 		status = nfserr_noent;
2360 		goto out;
2361 	}
2362 
2363 	unconf  = find_unconfirmed_client_by_name(&exid->clname, nn);
2364 	if (unconf) /* case 4, possible retry or client restart */
2365 		unhash_client_locked(unconf);
2366 
2367 	/* case 1 (normal case) */
2368 out_new:
2369 	if (conf) {
2370 		status = mark_client_expired_locked(conf);
2371 		if (status)
2372 			goto out;
2373 	}
2374 	new->cl_minorversion = cstate->minorversion;
2375 	new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2376 
2377 	gen_clid(new, nn);
2378 	add_to_unconfirmed(new);
2379 	swap(new, conf);
2380 out_copy:
2381 	exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2382 	exid->clientid.cl_id = conf->cl_clientid.cl_id;
2383 
2384 	exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2385 	nfsd4_set_ex_flags(conf, exid);
2386 
2387 	dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2388 		conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2389 	status = nfs_ok;
2390 
2391 out:
2392 	spin_unlock(&nn->client_lock);
2393 	if (new)
2394 		expire_client(new);
2395 	if (unconf)
2396 		expire_client(unconf);
2397 	return status;
2398 }
2399 
2400 static __be32
2401 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2402 {
2403 	dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2404 		slot_seqid);
2405 
2406 	/* The slot is in use, and no response has been sent. */
2407 	if (slot_inuse) {
2408 		if (seqid == slot_seqid)
2409 			return nfserr_jukebox;
2410 		else
2411 			return nfserr_seq_misordered;
2412 	}
2413 	/* Note unsigned 32-bit arithmetic handles wraparound: */
2414 	if (likely(seqid == slot_seqid + 1))
2415 		return nfs_ok;
2416 	if (seqid == slot_seqid)
2417 		return nfserr_replay_cache;
2418 	return nfserr_seq_misordered;
2419 }
2420 
2421 /*
2422  * Cache the create session result into the create session single DRC
2423  * slot cache by saving the xdr structure. sl_seqid has been set.
2424  * Do this for solo or embedded create session operations.
2425  */
2426 static void
2427 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2428 			   struct nfsd4_clid_slot *slot, __be32 nfserr)
2429 {
2430 	slot->sl_status = nfserr;
2431 	memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2432 }
2433 
2434 static __be32
2435 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2436 			    struct nfsd4_clid_slot *slot)
2437 {
2438 	memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2439 	return slot->sl_status;
2440 }
2441 
2442 #define NFSD_MIN_REQ_HDR_SEQ_SZ	((\
2443 			2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2444 			1 +	/* MIN tag is length with zero, only length */ \
2445 			3 +	/* version, opcount, opcode */ \
2446 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2447 				/* seqid, slotID, slotID, cache */ \
2448 			4 ) * sizeof(__be32))
2449 
2450 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2451 			2 +	/* verifier: AUTH_NULL, length 0 */\
2452 			1 +	/* status */ \
2453 			1 +	/* MIN tag is length with zero, only length */ \
2454 			3 +	/* opcount, opcode, opstatus*/ \
2455 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2456 				/* seqid, slotID, slotID, slotID, status */ \
2457 			5 ) * sizeof(__be32))
2458 
2459 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2460 {
2461 	u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2462 
2463 	if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2464 		return nfserr_toosmall;
2465 	if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2466 		return nfserr_toosmall;
2467 	ca->headerpadsz = 0;
2468 	ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2469 	ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2470 	ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2471 	ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2472 			NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2473 	ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2474 	/*
2475 	 * Note decreasing slot size below client's request may make it
2476 	 * difficult for client to function correctly, whereas
2477 	 * decreasing the number of slots will (just?) affect
2478 	 * performance.  When short on memory we therefore prefer to
2479 	 * decrease number of slots instead of their size.  Clients that
2480 	 * request larger slots than they need will get poor results:
2481 	 */
2482 	ca->maxreqs = nfsd4_get_drc_mem(ca);
2483 	if (!ca->maxreqs)
2484 		return nfserr_jukebox;
2485 
2486 	return nfs_ok;
2487 }
2488 
2489 #define NFSD_CB_MAX_REQ_SZ	((NFS4_enc_cb_recall_sz + \
2490 				 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2491 #define NFSD_CB_MAX_RESP_SZ	((NFS4_dec_cb_recall_sz + \
2492 				 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2493 
2494 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2495 {
2496 	ca->headerpadsz = 0;
2497 
2498 	/*
2499 	 * These RPC_MAX_HEADER macros are overkill, especially since we
2500 	 * don't even do gss on the backchannel yet.  But this is still
2501 	 * less than 1k.  Tighten up this estimate in the unlikely event
2502 	 * it turns out to be a problem for some client:
2503 	 */
2504 	if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2505 		return nfserr_toosmall;
2506 	if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2507 		return nfserr_toosmall;
2508 	ca->maxresp_cached = 0;
2509 	if (ca->maxops < 2)
2510 		return nfserr_toosmall;
2511 
2512 	return nfs_ok;
2513 }
2514 
2515 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2516 {
2517 	switch (cbs->flavor) {
2518 	case RPC_AUTH_NULL:
2519 	case RPC_AUTH_UNIX:
2520 		return nfs_ok;
2521 	default:
2522 		/*
2523 		 * GSS case: the spec doesn't allow us to return this
2524 		 * error.  But it also doesn't allow us not to support
2525 		 * GSS.
2526 		 * I'd rather this fail hard than return some error the
2527 		 * client might think it can already handle:
2528 		 */
2529 		return nfserr_encr_alg_unsupp;
2530 	}
2531 }
2532 
2533 __be32
2534 nfsd4_create_session(struct svc_rqst *rqstp,
2535 		     struct nfsd4_compound_state *cstate,
2536 		     struct nfsd4_create_session *cr_ses)
2537 {
2538 	struct sockaddr *sa = svc_addr(rqstp);
2539 	struct nfs4_client *conf, *unconf;
2540 	struct nfs4_client *old = NULL;
2541 	struct nfsd4_session *new;
2542 	struct nfsd4_conn *conn;
2543 	struct nfsd4_clid_slot *cs_slot = NULL;
2544 	__be32 status = 0;
2545 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2546 
2547 	if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2548 		return nfserr_inval;
2549 	status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2550 	if (status)
2551 		return status;
2552 	status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2553 	if (status)
2554 		return status;
2555 	status = check_backchannel_attrs(&cr_ses->back_channel);
2556 	if (status)
2557 		goto out_release_drc_mem;
2558 	status = nfserr_jukebox;
2559 	new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2560 	if (!new)
2561 		goto out_release_drc_mem;
2562 	conn = alloc_conn_from_crses(rqstp, cr_ses);
2563 	if (!conn)
2564 		goto out_free_session;
2565 
2566 	spin_lock(&nn->client_lock);
2567 	unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2568 	conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2569 	WARN_ON_ONCE(conf && unconf);
2570 
2571 	if (conf) {
2572 		status = nfserr_wrong_cred;
2573 		if (!mach_creds_match(conf, rqstp))
2574 			goto out_free_conn;
2575 		cs_slot = &conf->cl_cs_slot;
2576 		status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2577 		if (status) {
2578 			if (status == nfserr_replay_cache)
2579 				status = nfsd4_replay_create_session(cr_ses, cs_slot);
2580 			goto out_free_conn;
2581 		}
2582 	} else if (unconf) {
2583 		if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2584 		    !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2585 			status = nfserr_clid_inuse;
2586 			goto out_free_conn;
2587 		}
2588 		status = nfserr_wrong_cred;
2589 		if (!mach_creds_match(unconf, rqstp))
2590 			goto out_free_conn;
2591 		cs_slot = &unconf->cl_cs_slot;
2592 		status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2593 		if (status) {
2594 			/* an unconfirmed replay returns misordered */
2595 			status = nfserr_seq_misordered;
2596 			goto out_free_conn;
2597 		}
2598 		old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2599 		if (old) {
2600 			status = mark_client_expired_locked(old);
2601 			if (status) {
2602 				old = NULL;
2603 				goto out_free_conn;
2604 			}
2605 		}
2606 		move_to_confirmed(unconf);
2607 		conf = unconf;
2608 	} else {
2609 		status = nfserr_stale_clientid;
2610 		goto out_free_conn;
2611 	}
2612 	status = nfs_ok;
2613 	/*
2614 	 * We do not support RDMA or persistent sessions
2615 	 */
2616 	cr_ses->flags &= ~SESSION4_PERSIST;
2617 	cr_ses->flags &= ~SESSION4_RDMA;
2618 
2619 	init_session(rqstp, new, conf, cr_ses);
2620 	nfsd4_get_session_locked(new);
2621 
2622 	memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2623 	       NFS4_MAX_SESSIONID_LEN);
2624 	cs_slot->sl_seqid++;
2625 	cr_ses->seqid = cs_slot->sl_seqid;
2626 
2627 	/* cache solo and embedded create sessions under the client_lock */
2628 	nfsd4_cache_create_session(cr_ses, cs_slot, status);
2629 	spin_unlock(&nn->client_lock);
2630 	/* init connection and backchannel */
2631 	nfsd4_init_conn(rqstp, conn, new);
2632 	nfsd4_put_session(new);
2633 	if (old)
2634 		expire_client(old);
2635 	return status;
2636 out_free_conn:
2637 	spin_unlock(&nn->client_lock);
2638 	free_conn(conn);
2639 	if (old)
2640 		expire_client(old);
2641 out_free_session:
2642 	__free_session(new);
2643 out_release_drc_mem:
2644 	nfsd4_put_drc_mem(&cr_ses->fore_channel);
2645 	return status;
2646 }
2647 
2648 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2649 {
2650 	switch (*dir) {
2651 	case NFS4_CDFC4_FORE:
2652 	case NFS4_CDFC4_BACK:
2653 		return nfs_ok;
2654 	case NFS4_CDFC4_FORE_OR_BOTH:
2655 	case NFS4_CDFC4_BACK_OR_BOTH:
2656 		*dir = NFS4_CDFC4_BOTH;
2657 		return nfs_ok;
2658 	};
2659 	return nfserr_inval;
2660 }
2661 
2662 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2663 {
2664 	struct nfsd4_session *session = cstate->session;
2665 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2666 	__be32 status;
2667 
2668 	status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2669 	if (status)
2670 		return status;
2671 	spin_lock(&nn->client_lock);
2672 	session->se_cb_prog = bc->bc_cb_program;
2673 	session->se_cb_sec = bc->bc_cb_sec;
2674 	spin_unlock(&nn->client_lock);
2675 
2676 	nfsd4_probe_callback(session->se_client);
2677 
2678 	return nfs_ok;
2679 }
2680 
2681 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2682 		     struct nfsd4_compound_state *cstate,
2683 		     struct nfsd4_bind_conn_to_session *bcts)
2684 {
2685 	__be32 status;
2686 	struct nfsd4_conn *conn;
2687 	struct nfsd4_session *session;
2688 	struct net *net = SVC_NET(rqstp);
2689 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2690 
2691 	if (!nfsd4_last_compound_op(rqstp))
2692 		return nfserr_not_only_op;
2693 	spin_lock(&nn->client_lock);
2694 	session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2695 	spin_unlock(&nn->client_lock);
2696 	if (!session)
2697 		goto out_no_session;
2698 	status = nfserr_wrong_cred;
2699 	if (!mach_creds_match(session->se_client, rqstp))
2700 		goto out;
2701 	status = nfsd4_map_bcts_dir(&bcts->dir);
2702 	if (status)
2703 		goto out;
2704 	conn = alloc_conn(rqstp, bcts->dir);
2705 	status = nfserr_jukebox;
2706 	if (!conn)
2707 		goto out;
2708 	nfsd4_init_conn(rqstp, conn, session);
2709 	status = nfs_ok;
2710 out:
2711 	nfsd4_put_session(session);
2712 out_no_session:
2713 	return status;
2714 }
2715 
2716 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2717 {
2718 	if (!session)
2719 		return 0;
2720 	return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2721 }
2722 
2723 __be32
2724 nfsd4_destroy_session(struct svc_rqst *r,
2725 		      struct nfsd4_compound_state *cstate,
2726 		      struct nfsd4_destroy_session *sessionid)
2727 {
2728 	struct nfsd4_session *ses;
2729 	__be32 status;
2730 	int ref_held_by_me = 0;
2731 	struct net *net = SVC_NET(r);
2732 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2733 
2734 	status = nfserr_not_only_op;
2735 	if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2736 		if (!nfsd4_last_compound_op(r))
2737 			goto out;
2738 		ref_held_by_me++;
2739 	}
2740 	dump_sessionid(__func__, &sessionid->sessionid);
2741 	spin_lock(&nn->client_lock);
2742 	ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2743 	if (!ses)
2744 		goto out_client_lock;
2745 	status = nfserr_wrong_cred;
2746 	if (!mach_creds_match(ses->se_client, r))
2747 		goto out_put_session;
2748 	status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2749 	if (status)
2750 		goto out_put_session;
2751 	unhash_session(ses);
2752 	spin_unlock(&nn->client_lock);
2753 
2754 	nfsd4_probe_callback_sync(ses->se_client);
2755 
2756 	spin_lock(&nn->client_lock);
2757 	status = nfs_ok;
2758 out_put_session:
2759 	nfsd4_put_session_locked(ses);
2760 out_client_lock:
2761 	spin_unlock(&nn->client_lock);
2762 out:
2763 	return status;
2764 }
2765 
2766 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2767 {
2768 	struct nfsd4_conn *c;
2769 
2770 	list_for_each_entry(c, &s->se_conns, cn_persession) {
2771 		if (c->cn_xprt == xpt) {
2772 			return c;
2773 		}
2774 	}
2775 	return NULL;
2776 }
2777 
2778 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2779 {
2780 	struct nfs4_client *clp = ses->se_client;
2781 	struct nfsd4_conn *c;
2782 	__be32 status = nfs_ok;
2783 	int ret;
2784 
2785 	spin_lock(&clp->cl_lock);
2786 	c = __nfsd4_find_conn(new->cn_xprt, ses);
2787 	if (c)
2788 		goto out_free;
2789 	status = nfserr_conn_not_bound_to_session;
2790 	if (clp->cl_mach_cred)
2791 		goto out_free;
2792 	__nfsd4_hash_conn(new, ses);
2793 	spin_unlock(&clp->cl_lock);
2794 	ret = nfsd4_register_conn(new);
2795 	if (ret)
2796 		/* oops; xprt is already down: */
2797 		nfsd4_conn_lost(&new->cn_xpt_user);
2798 	return nfs_ok;
2799 out_free:
2800 	spin_unlock(&clp->cl_lock);
2801 	free_conn(new);
2802 	return status;
2803 }
2804 
2805 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2806 {
2807 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
2808 
2809 	return args->opcnt > session->se_fchannel.maxops;
2810 }
2811 
2812 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2813 				  struct nfsd4_session *session)
2814 {
2815 	struct xdr_buf *xb = &rqstp->rq_arg;
2816 
2817 	return xb->len > session->se_fchannel.maxreq_sz;
2818 }
2819 
2820 __be32
2821 nfsd4_sequence(struct svc_rqst *rqstp,
2822 	       struct nfsd4_compound_state *cstate,
2823 	       struct nfsd4_sequence *seq)
2824 {
2825 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
2826 	struct xdr_stream *xdr = &resp->xdr;
2827 	struct nfsd4_session *session;
2828 	struct nfs4_client *clp;
2829 	struct nfsd4_slot *slot;
2830 	struct nfsd4_conn *conn;
2831 	__be32 status;
2832 	int buflen;
2833 	struct net *net = SVC_NET(rqstp);
2834 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2835 
2836 	if (resp->opcnt != 1)
2837 		return nfserr_sequence_pos;
2838 
2839 	/*
2840 	 * Will be either used or freed by nfsd4_sequence_check_conn
2841 	 * below.
2842 	 */
2843 	conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2844 	if (!conn)
2845 		return nfserr_jukebox;
2846 
2847 	spin_lock(&nn->client_lock);
2848 	session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2849 	if (!session)
2850 		goto out_no_session;
2851 	clp = session->se_client;
2852 
2853 	status = nfserr_too_many_ops;
2854 	if (nfsd4_session_too_many_ops(rqstp, session))
2855 		goto out_put_session;
2856 
2857 	status = nfserr_req_too_big;
2858 	if (nfsd4_request_too_big(rqstp, session))
2859 		goto out_put_session;
2860 
2861 	status = nfserr_badslot;
2862 	if (seq->slotid >= session->se_fchannel.maxreqs)
2863 		goto out_put_session;
2864 
2865 	slot = session->se_slots[seq->slotid];
2866 	dprintk("%s: slotid %d\n", __func__, seq->slotid);
2867 
2868 	/* We do not negotiate the number of slots yet, so set the
2869 	 * maxslots to the session maxreqs which is used to encode
2870 	 * sr_highest_slotid and the sr_target_slot id to maxslots */
2871 	seq->maxslots = session->se_fchannel.maxreqs;
2872 
2873 	status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2874 					slot->sl_flags & NFSD4_SLOT_INUSE);
2875 	if (status == nfserr_replay_cache) {
2876 		status = nfserr_seq_misordered;
2877 		if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2878 			goto out_put_session;
2879 		cstate->slot = slot;
2880 		cstate->session = session;
2881 		cstate->clp = clp;
2882 		/* Return the cached reply status and set cstate->status
2883 		 * for nfsd4_proc_compound processing */
2884 		status = nfsd4_replay_cache_entry(resp, seq);
2885 		cstate->status = nfserr_replay_cache;
2886 		goto out;
2887 	}
2888 	if (status)
2889 		goto out_put_session;
2890 
2891 	status = nfsd4_sequence_check_conn(conn, session);
2892 	conn = NULL;
2893 	if (status)
2894 		goto out_put_session;
2895 
2896 	buflen = (seq->cachethis) ?
2897 			session->se_fchannel.maxresp_cached :
2898 			session->se_fchannel.maxresp_sz;
2899 	status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2900 				    nfserr_rep_too_big;
2901 	if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2902 		goto out_put_session;
2903 	svc_reserve(rqstp, buflen);
2904 
2905 	status = nfs_ok;
2906 	/* Success! bump slot seqid */
2907 	slot->sl_seqid = seq->seqid;
2908 	slot->sl_flags |= NFSD4_SLOT_INUSE;
2909 	if (seq->cachethis)
2910 		slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2911 	else
2912 		slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2913 
2914 	cstate->slot = slot;
2915 	cstate->session = session;
2916 	cstate->clp = clp;
2917 
2918 out:
2919 	switch (clp->cl_cb_state) {
2920 	case NFSD4_CB_DOWN:
2921 		seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2922 		break;
2923 	case NFSD4_CB_FAULT:
2924 		seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2925 		break;
2926 	default:
2927 		seq->status_flags = 0;
2928 	}
2929 	if (!list_empty(&clp->cl_revoked))
2930 		seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2931 out_no_session:
2932 	if (conn)
2933 		free_conn(conn);
2934 	spin_unlock(&nn->client_lock);
2935 	return status;
2936 out_put_session:
2937 	nfsd4_put_session_locked(session);
2938 	goto out_no_session;
2939 }
2940 
2941 void
2942 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2943 {
2944 	struct nfsd4_compound_state *cs = &resp->cstate;
2945 
2946 	if (nfsd4_has_session(cs)) {
2947 		if (cs->status != nfserr_replay_cache) {
2948 			nfsd4_store_cache_entry(resp);
2949 			cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2950 		}
2951 		/* Drop session reference that was taken in nfsd4_sequence() */
2952 		nfsd4_put_session(cs->session);
2953 	} else if (cs->clp)
2954 		put_client_renew(cs->clp);
2955 }
2956 
2957 __be32
2958 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2959 {
2960 	struct nfs4_client *conf, *unconf;
2961 	struct nfs4_client *clp = NULL;
2962 	__be32 status = 0;
2963 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2964 
2965 	spin_lock(&nn->client_lock);
2966 	unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2967 	conf = find_confirmed_client(&dc->clientid, true, nn);
2968 	WARN_ON_ONCE(conf && unconf);
2969 
2970 	if (conf) {
2971 		if (client_has_state(conf)) {
2972 			status = nfserr_clientid_busy;
2973 			goto out;
2974 		}
2975 		status = mark_client_expired_locked(conf);
2976 		if (status)
2977 			goto out;
2978 		clp = conf;
2979 	} else if (unconf)
2980 		clp = unconf;
2981 	else {
2982 		status = nfserr_stale_clientid;
2983 		goto out;
2984 	}
2985 	if (!mach_creds_match(clp, rqstp)) {
2986 		clp = NULL;
2987 		status = nfserr_wrong_cred;
2988 		goto out;
2989 	}
2990 	unhash_client_locked(clp);
2991 out:
2992 	spin_unlock(&nn->client_lock);
2993 	if (clp)
2994 		expire_client(clp);
2995 	return status;
2996 }
2997 
2998 __be32
2999 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3000 {
3001 	__be32 status = 0;
3002 
3003 	if (rc->rca_one_fs) {
3004 		if (!cstate->current_fh.fh_dentry)
3005 			return nfserr_nofilehandle;
3006 		/*
3007 		 * We don't take advantage of the rca_one_fs case.
3008 		 * That's OK, it's optional, we can safely ignore it.
3009 		 */
3010 		 return nfs_ok;
3011 	}
3012 
3013 	status = nfserr_complete_already;
3014 	if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3015 			     &cstate->session->se_client->cl_flags))
3016 		goto out;
3017 
3018 	status = nfserr_stale_clientid;
3019 	if (is_client_expired(cstate->session->se_client))
3020 		/*
3021 		 * The following error isn't really legal.
3022 		 * But we only get here if the client just explicitly
3023 		 * destroyed the client.  Surely it no longer cares what
3024 		 * error it gets back on an operation for the dead
3025 		 * client.
3026 		 */
3027 		goto out;
3028 
3029 	status = nfs_ok;
3030 	nfsd4_client_record_create(cstate->session->se_client);
3031 out:
3032 	return status;
3033 }
3034 
3035 __be32
3036 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3037 		  struct nfsd4_setclientid *setclid)
3038 {
3039 	struct xdr_netobj 	clname = setclid->se_name;
3040 	nfs4_verifier		clverifier = setclid->se_verf;
3041 	struct nfs4_client	*conf, *new;
3042 	struct nfs4_client	*unconf = NULL;
3043 	__be32 			status;
3044 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3045 
3046 	new = create_client(clname, rqstp, &clverifier);
3047 	if (new == NULL)
3048 		return nfserr_jukebox;
3049 	/* Cases below refer to rfc 3530 section 14.2.33: */
3050 	spin_lock(&nn->client_lock);
3051 	conf = find_confirmed_client_by_name(&clname, nn);
3052 	if (conf) {
3053 		/* case 0: */
3054 		status = nfserr_clid_inuse;
3055 		if (clp_used_exchangeid(conf))
3056 			goto out;
3057 		if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3058 			char addr_str[INET6_ADDRSTRLEN];
3059 			rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3060 				 sizeof(addr_str));
3061 			dprintk("NFSD: setclientid: string in use by client "
3062 				"at %s\n", addr_str);
3063 			goto out;
3064 		}
3065 	}
3066 	unconf = find_unconfirmed_client_by_name(&clname, nn);
3067 	if (unconf)
3068 		unhash_client_locked(unconf);
3069 	if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3070 		/* case 1: probable callback update */
3071 		copy_clid(new, conf);
3072 		gen_confirm(new, nn);
3073 	} else /* case 4 (new client) or cases 2, 3 (client reboot): */
3074 		gen_clid(new, nn);
3075 	new->cl_minorversion = 0;
3076 	gen_callback(new, setclid, rqstp);
3077 	add_to_unconfirmed(new);
3078 	setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3079 	setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3080 	memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3081 	new = NULL;
3082 	status = nfs_ok;
3083 out:
3084 	spin_unlock(&nn->client_lock);
3085 	if (new)
3086 		free_client(new);
3087 	if (unconf)
3088 		expire_client(unconf);
3089 	return status;
3090 }
3091 
3092 
3093 __be32
3094 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3095 			 struct nfsd4_compound_state *cstate,
3096 			 struct nfsd4_setclientid_confirm *setclientid_confirm)
3097 {
3098 	struct nfs4_client *conf, *unconf;
3099 	struct nfs4_client *old = NULL;
3100 	nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3101 	clientid_t * clid = &setclientid_confirm->sc_clientid;
3102 	__be32 status;
3103 	struct nfsd_net	*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3104 
3105 	if (STALE_CLIENTID(clid, nn))
3106 		return nfserr_stale_clientid;
3107 
3108 	spin_lock(&nn->client_lock);
3109 	conf = find_confirmed_client(clid, false, nn);
3110 	unconf = find_unconfirmed_client(clid, false, nn);
3111 	/*
3112 	 * We try hard to give out unique clientid's, so if we get an
3113 	 * attempt to confirm the same clientid with a different cred,
3114 	 * the client may be buggy; this should never happen.
3115 	 *
3116 	 * Nevertheless, RFC 7530 recommends INUSE for this case:
3117 	 */
3118 	status = nfserr_clid_inuse;
3119 	if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3120 		goto out;
3121 	if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3122 		goto out;
3123 	/* cases below refer to rfc 3530 section 14.2.34: */
3124 	if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3125 		if (conf && !unconf) /* case 2: probable retransmit */
3126 			status = nfs_ok;
3127 		else /* case 4: client hasn't noticed we rebooted yet? */
3128 			status = nfserr_stale_clientid;
3129 		goto out;
3130 	}
3131 	status = nfs_ok;
3132 	if (conf) { /* case 1: callback update */
3133 		old = unconf;
3134 		unhash_client_locked(old);
3135 		nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3136 	} else { /* case 3: normal case; new or rebooted client */
3137 		old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3138 		if (old) {
3139 			status = mark_client_expired_locked(old);
3140 			if (status) {
3141 				old = NULL;
3142 				goto out;
3143 			}
3144 		}
3145 		move_to_confirmed(unconf);
3146 		conf = unconf;
3147 	}
3148 	get_client_locked(conf);
3149 	spin_unlock(&nn->client_lock);
3150 	nfsd4_probe_callback(conf);
3151 	spin_lock(&nn->client_lock);
3152 	put_client_renew_locked(conf);
3153 out:
3154 	spin_unlock(&nn->client_lock);
3155 	if (old)
3156 		expire_client(old);
3157 	return status;
3158 }
3159 
3160 static struct nfs4_file *nfsd4_alloc_file(void)
3161 {
3162 	return kmem_cache_alloc(file_slab, GFP_KERNEL);
3163 }
3164 
3165 /* OPEN Share state helper functions */
3166 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3167 				struct nfs4_file *fp)
3168 {
3169 	lockdep_assert_held(&state_lock);
3170 
3171 	atomic_set(&fp->fi_ref, 1);
3172 	spin_lock_init(&fp->fi_lock);
3173 	INIT_LIST_HEAD(&fp->fi_stateids);
3174 	INIT_LIST_HEAD(&fp->fi_delegations);
3175 	INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3176 	fh_copy_shallow(&fp->fi_fhandle, fh);
3177 	fp->fi_deleg_file = NULL;
3178 	fp->fi_had_conflict = false;
3179 	fp->fi_share_deny = 0;
3180 	memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3181 	memset(fp->fi_access, 0, sizeof(fp->fi_access));
3182 #ifdef CONFIG_NFSD_PNFS
3183 	INIT_LIST_HEAD(&fp->fi_lo_states);
3184 	atomic_set(&fp->fi_lo_recalls, 0);
3185 #endif
3186 	hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3187 }
3188 
3189 void
3190 nfsd4_free_slabs(void)
3191 {
3192 	kmem_cache_destroy(odstate_slab);
3193 	kmem_cache_destroy(openowner_slab);
3194 	kmem_cache_destroy(lockowner_slab);
3195 	kmem_cache_destroy(file_slab);
3196 	kmem_cache_destroy(stateid_slab);
3197 	kmem_cache_destroy(deleg_slab);
3198 }
3199 
3200 int
3201 nfsd4_init_slabs(void)
3202 {
3203 	openowner_slab = kmem_cache_create("nfsd4_openowners",
3204 			sizeof(struct nfs4_openowner), 0, 0, NULL);
3205 	if (openowner_slab == NULL)
3206 		goto out;
3207 	lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3208 			sizeof(struct nfs4_lockowner), 0, 0, NULL);
3209 	if (lockowner_slab == NULL)
3210 		goto out_free_openowner_slab;
3211 	file_slab = kmem_cache_create("nfsd4_files",
3212 			sizeof(struct nfs4_file), 0, 0, NULL);
3213 	if (file_slab == NULL)
3214 		goto out_free_lockowner_slab;
3215 	stateid_slab = kmem_cache_create("nfsd4_stateids",
3216 			sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3217 	if (stateid_slab == NULL)
3218 		goto out_free_file_slab;
3219 	deleg_slab = kmem_cache_create("nfsd4_delegations",
3220 			sizeof(struct nfs4_delegation), 0, 0, NULL);
3221 	if (deleg_slab == NULL)
3222 		goto out_free_stateid_slab;
3223 	odstate_slab = kmem_cache_create("nfsd4_odstate",
3224 			sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3225 	if (odstate_slab == NULL)
3226 		goto out_free_deleg_slab;
3227 	return 0;
3228 
3229 out_free_deleg_slab:
3230 	kmem_cache_destroy(deleg_slab);
3231 out_free_stateid_slab:
3232 	kmem_cache_destroy(stateid_slab);
3233 out_free_file_slab:
3234 	kmem_cache_destroy(file_slab);
3235 out_free_lockowner_slab:
3236 	kmem_cache_destroy(lockowner_slab);
3237 out_free_openowner_slab:
3238 	kmem_cache_destroy(openowner_slab);
3239 out:
3240 	dprintk("nfsd4: out of memory while initializing nfsv4\n");
3241 	return -ENOMEM;
3242 }
3243 
3244 static void init_nfs4_replay(struct nfs4_replay *rp)
3245 {
3246 	rp->rp_status = nfserr_serverfault;
3247 	rp->rp_buflen = 0;
3248 	rp->rp_buf = rp->rp_ibuf;
3249 	mutex_init(&rp->rp_mutex);
3250 }
3251 
3252 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3253 		struct nfs4_stateowner *so)
3254 {
3255 	if (!nfsd4_has_session(cstate)) {
3256 		mutex_lock(&so->so_replay.rp_mutex);
3257 		cstate->replay_owner = nfs4_get_stateowner(so);
3258 	}
3259 }
3260 
3261 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3262 {
3263 	struct nfs4_stateowner *so = cstate->replay_owner;
3264 
3265 	if (so != NULL) {
3266 		cstate->replay_owner = NULL;
3267 		mutex_unlock(&so->so_replay.rp_mutex);
3268 		nfs4_put_stateowner(so);
3269 	}
3270 }
3271 
3272 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3273 {
3274 	struct nfs4_stateowner *sop;
3275 
3276 	sop = kmem_cache_alloc(slab, GFP_KERNEL);
3277 	if (!sop)
3278 		return NULL;
3279 
3280 	sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3281 	if (!sop->so_owner.data) {
3282 		kmem_cache_free(slab, sop);
3283 		return NULL;
3284 	}
3285 	sop->so_owner.len = owner->len;
3286 
3287 	INIT_LIST_HEAD(&sop->so_stateids);
3288 	sop->so_client = clp;
3289 	init_nfs4_replay(&sop->so_replay);
3290 	atomic_set(&sop->so_count, 1);
3291 	return sop;
3292 }
3293 
3294 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3295 {
3296 	lockdep_assert_held(&clp->cl_lock);
3297 
3298 	list_add(&oo->oo_owner.so_strhash,
3299 		 &clp->cl_ownerstr_hashtbl[strhashval]);
3300 	list_add(&oo->oo_perclient, &clp->cl_openowners);
3301 }
3302 
3303 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3304 {
3305 	unhash_openowner_locked(openowner(so));
3306 }
3307 
3308 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3309 {
3310 	struct nfs4_openowner *oo = openowner(so);
3311 
3312 	kmem_cache_free(openowner_slab, oo);
3313 }
3314 
3315 static const struct nfs4_stateowner_operations openowner_ops = {
3316 	.so_unhash =	nfs4_unhash_openowner,
3317 	.so_free =	nfs4_free_openowner,
3318 };
3319 
3320 static struct nfs4_openowner *
3321 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3322 			   struct nfsd4_compound_state *cstate)
3323 {
3324 	struct nfs4_client *clp = cstate->clp;
3325 	struct nfs4_openowner *oo, *ret;
3326 
3327 	oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3328 	if (!oo)
3329 		return NULL;
3330 	oo->oo_owner.so_ops = &openowner_ops;
3331 	oo->oo_owner.so_is_open_owner = 1;
3332 	oo->oo_owner.so_seqid = open->op_seqid;
3333 	oo->oo_flags = 0;
3334 	if (nfsd4_has_session(cstate))
3335 		oo->oo_flags |= NFS4_OO_CONFIRMED;
3336 	oo->oo_time = 0;
3337 	oo->oo_last_closed_stid = NULL;
3338 	INIT_LIST_HEAD(&oo->oo_close_lru);
3339 	spin_lock(&clp->cl_lock);
3340 	ret = find_openstateowner_str_locked(strhashval, open, clp);
3341 	if (ret == NULL) {
3342 		hash_openowner(oo, clp, strhashval);
3343 		ret = oo;
3344 	} else
3345 		nfs4_free_stateowner(&oo->oo_owner);
3346 
3347 	spin_unlock(&clp->cl_lock);
3348 	return ret;
3349 }
3350 
3351 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3352 	struct nfs4_openowner *oo = open->op_openowner;
3353 
3354 	atomic_inc(&stp->st_stid.sc_count);
3355 	stp->st_stid.sc_type = NFS4_OPEN_STID;
3356 	INIT_LIST_HEAD(&stp->st_locks);
3357 	stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3358 	get_nfs4_file(fp);
3359 	stp->st_stid.sc_file = fp;
3360 	stp->st_access_bmap = 0;
3361 	stp->st_deny_bmap = 0;
3362 	stp->st_openstp = NULL;
3363 	spin_lock(&oo->oo_owner.so_client->cl_lock);
3364 	list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3365 	spin_lock(&fp->fi_lock);
3366 	list_add(&stp->st_perfile, &fp->fi_stateids);
3367 	spin_unlock(&fp->fi_lock);
3368 	spin_unlock(&oo->oo_owner.so_client->cl_lock);
3369 }
3370 
3371 /*
3372  * In the 4.0 case we need to keep the owners around a little while to handle
3373  * CLOSE replay. We still do need to release any file access that is held by
3374  * them before returning however.
3375  */
3376 static void
3377 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3378 {
3379 	struct nfs4_ol_stateid *last;
3380 	struct nfs4_openowner *oo = openowner(s->st_stateowner);
3381 	struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3382 						nfsd_net_id);
3383 
3384 	dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3385 
3386 	/*
3387 	 * We know that we hold one reference via nfsd4_close, and another
3388 	 * "persistent" reference for the client. If the refcount is higher
3389 	 * than 2, then there are still calls in progress that are using this
3390 	 * stateid. We can't put the sc_file reference until they are finished.
3391 	 * Wait for the refcount to drop to 2. Since it has been unhashed,
3392 	 * there should be no danger of the refcount going back up again at
3393 	 * this point.
3394 	 */
3395 	wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3396 
3397 	release_all_access(s);
3398 	if (s->st_stid.sc_file) {
3399 		put_nfs4_file(s->st_stid.sc_file);
3400 		s->st_stid.sc_file = NULL;
3401 	}
3402 
3403 	spin_lock(&nn->client_lock);
3404 	last = oo->oo_last_closed_stid;
3405 	oo->oo_last_closed_stid = s;
3406 	list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3407 	oo->oo_time = get_seconds();
3408 	spin_unlock(&nn->client_lock);
3409 	if (last)
3410 		nfs4_put_stid(&last->st_stid);
3411 }
3412 
3413 /* search file_hashtbl[] for file */
3414 static struct nfs4_file *
3415 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3416 {
3417 	struct nfs4_file *fp;
3418 
3419 	hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3420 		if (fh_match(&fp->fi_fhandle, fh)) {
3421 			if (atomic_inc_not_zero(&fp->fi_ref))
3422 				return fp;
3423 		}
3424 	}
3425 	return NULL;
3426 }
3427 
3428 struct nfs4_file *
3429 find_file(struct knfsd_fh *fh)
3430 {
3431 	struct nfs4_file *fp;
3432 	unsigned int hashval = file_hashval(fh);
3433 
3434 	rcu_read_lock();
3435 	fp = find_file_locked(fh, hashval);
3436 	rcu_read_unlock();
3437 	return fp;
3438 }
3439 
3440 static struct nfs4_file *
3441 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3442 {
3443 	struct nfs4_file *fp;
3444 	unsigned int hashval = file_hashval(fh);
3445 
3446 	rcu_read_lock();
3447 	fp = find_file_locked(fh, hashval);
3448 	rcu_read_unlock();
3449 	if (fp)
3450 		return fp;
3451 
3452 	spin_lock(&state_lock);
3453 	fp = find_file_locked(fh, hashval);
3454 	if (likely(fp == NULL)) {
3455 		nfsd4_init_file(fh, hashval, new);
3456 		fp = new;
3457 	}
3458 	spin_unlock(&state_lock);
3459 
3460 	return fp;
3461 }
3462 
3463 /*
3464  * Called to check deny when READ with all zero stateid or
3465  * WRITE with all zero or all one stateid
3466  */
3467 static __be32
3468 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3469 {
3470 	struct nfs4_file *fp;
3471 	__be32 ret = nfs_ok;
3472 
3473 	fp = find_file(&current_fh->fh_handle);
3474 	if (!fp)
3475 		return ret;
3476 	/* Check for conflicting share reservations */
3477 	spin_lock(&fp->fi_lock);
3478 	if (fp->fi_share_deny & deny_type)
3479 		ret = nfserr_locked;
3480 	spin_unlock(&fp->fi_lock);
3481 	put_nfs4_file(fp);
3482 	return ret;
3483 }
3484 
3485 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3486 {
3487 	struct nfs4_delegation *dp = cb_to_delegation(cb);
3488 	struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3489 					  nfsd_net_id);
3490 
3491 	block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3492 
3493 	/*
3494 	 * We can't do this in nfsd_break_deleg_cb because it is
3495 	 * already holding inode->i_lock.
3496 	 *
3497 	 * If the dl_time != 0, then we know that it has already been
3498 	 * queued for a lease break. Don't queue it again.
3499 	 */
3500 	spin_lock(&state_lock);
3501 	if (dp->dl_time == 0) {
3502 		dp->dl_time = get_seconds();
3503 		list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3504 	}
3505 	spin_unlock(&state_lock);
3506 }
3507 
3508 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3509 		struct rpc_task *task)
3510 {
3511 	struct nfs4_delegation *dp = cb_to_delegation(cb);
3512 
3513 	if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3514 	        return 1;
3515 
3516 	switch (task->tk_status) {
3517 	case 0:
3518 		return 1;
3519 	case -EBADHANDLE:
3520 	case -NFS4ERR_BAD_STATEID:
3521 		/*
3522 		 * Race: client probably got cb_recall before open reply
3523 		 * granting delegation.
3524 		 */
3525 		if (dp->dl_retries--) {
3526 			rpc_delay(task, 2 * HZ);
3527 			return 0;
3528 		}
3529 		/*FALLTHRU*/
3530 	default:
3531 		return -1;
3532 	}
3533 }
3534 
3535 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3536 {
3537 	struct nfs4_delegation *dp = cb_to_delegation(cb);
3538 
3539 	nfs4_put_stid(&dp->dl_stid);
3540 }
3541 
3542 static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3543 	.prepare	= nfsd4_cb_recall_prepare,
3544 	.done		= nfsd4_cb_recall_done,
3545 	.release	= nfsd4_cb_recall_release,
3546 };
3547 
3548 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3549 {
3550 	/*
3551 	 * We're assuming the state code never drops its reference
3552 	 * without first removing the lease.  Since we're in this lease
3553 	 * callback (and since the lease code is serialized by the kernel
3554 	 * lock) we know the server hasn't removed the lease yet, we know
3555 	 * it's safe to take a reference.
3556 	 */
3557 	atomic_inc(&dp->dl_stid.sc_count);
3558 	nfsd4_run_cb(&dp->dl_recall);
3559 }
3560 
3561 /* Called from break_lease() with i_lock held. */
3562 static bool
3563 nfsd_break_deleg_cb(struct file_lock *fl)
3564 {
3565 	bool ret = false;
3566 	struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3567 	struct nfs4_delegation *dp;
3568 
3569 	if (!fp) {
3570 		WARN(1, "(%p)->fl_owner NULL\n", fl);
3571 		return ret;
3572 	}
3573 	if (fp->fi_had_conflict) {
3574 		WARN(1, "duplicate break on %p\n", fp);
3575 		return ret;
3576 	}
3577 	/*
3578 	 * We don't want the locks code to timeout the lease for us;
3579 	 * we'll remove it ourself if a delegation isn't returned
3580 	 * in time:
3581 	 */
3582 	fl->fl_break_time = 0;
3583 
3584 	spin_lock(&fp->fi_lock);
3585 	fp->fi_had_conflict = true;
3586 	/*
3587 	 * If there are no delegations on the list, then return true
3588 	 * so that the lease code will go ahead and delete it.
3589 	 */
3590 	if (list_empty(&fp->fi_delegations))
3591 		ret = true;
3592 	else
3593 		list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3594 			nfsd_break_one_deleg(dp);
3595 	spin_unlock(&fp->fi_lock);
3596 	return ret;
3597 }
3598 
3599 static int
3600 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3601 		     struct list_head *dispose)
3602 {
3603 	if (arg & F_UNLCK)
3604 		return lease_modify(onlist, arg, dispose);
3605 	else
3606 		return -EAGAIN;
3607 }
3608 
3609 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3610 	.lm_break = nfsd_break_deleg_cb,
3611 	.lm_change = nfsd_change_deleg_cb,
3612 };
3613 
3614 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3615 {
3616 	if (nfsd4_has_session(cstate))
3617 		return nfs_ok;
3618 	if (seqid == so->so_seqid - 1)
3619 		return nfserr_replay_me;
3620 	if (seqid == so->so_seqid)
3621 		return nfs_ok;
3622 	return nfserr_bad_seqid;
3623 }
3624 
3625 static __be32 lookup_clientid(clientid_t *clid,
3626 		struct nfsd4_compound_state *cstate,
3627 		struct nfsd_net *nn)
3628 {
3629 	struct nfs4_client *found;
3630 
3631 	if (cstate->clp) {
3632 		found = cstate->clp;
3633 		if (!same_clid(&found->cl_clientid, clid))
3634 			return nfserr_stale_clientid;
3635 		return nfs_ok;
3636 	}
3637 
3638 	if (STALE_CLIENTID(clid, nn))
3639 		return nfserr_stale_clientid;
3640 
3641 	/*
3642 	 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3643 	 * cached already then we know this is for is for v4.0 and "sessions"
3644 	 * will be false.
3645 	 */
3646 	WARN_ON_ONCE(cstate->session);
3647 	spin_lock(&nn->client_lock);
3648 	found = find_confirmed_client(clid, false, nn);
3649 	if (!found) {
3650 		spin_unlock(&nn->client_lock);
3651 		return nfserr_expired;
3652 	}
3653 	atomic_inc(&found->cl_refcount);
3654 	spin_unlock(&nn->client_lock);
3655 
3656 	/* Cache the nfs4_client in cstate! */
3657 	cstate->clp = found;
3658 	return nfs_ok;
3659 }
3660 
3661 __be32
3662 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3663 		    struct nfsd4_open *open, struct nfsd_net *nn)
3664 {
3665 	clientid_t *clientid = &open->op_clientid;
3666 	struct nfs4_client *clp = NULL;
3667 	unsigned int strhashval;
3668 	struct nfs4_openowner *oo = NULL;
3669 	__be32 status;
3670 
3671 	if (STALE_CLIENTID(&open->op_clientid, nn))
3672 		return nfserr_stale_clientid;
3673 	/*
3674 	 * In case we need it later, after we've already created the
3675 	 * file and don't want to risk a further failure:
3676 	 */
3677 	open->op_file = nfsd4_alloc_file();
3678 	if (open->op_file == NULL)
3679 		return nfserr_jukebox;
3680 
3681 	status = lookup_clientid(clientid, cstate, nn);
3682 	if (status)
3683 		return status;
3684 	clp = cstate->clp;
3685 
3686 	strhashval = ownerstr_hashval(&open->op_owner);
3687 	oo = find_openstateowner_str(strhashval, open, clp);
3688 	open->op_openowner = oo;
3689 	if (!oo) {
3690 		goto new_owner;
3691 	}
3692 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3693 		/* Replace unconfirmed owners without checking for replay. */
3694 		release_openowner(oo);
3695 		open->op_openowner = NULL;
3696 		goto new_owner;
3697 	}
3698 	status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3699 	if (status)
3700 		return status;
3701 	goto alloc_stateid;
3702 new_owner:
3703 	oo = alloc_init_open_stateowner(strhashval, open, cstate);
3704 	if (oo == NULL)
3705 		return nfserr_jukebox;
3706 	open->op_openowner = oo;
3707 alloc_stateid:
3708 	open->op_stp = nfs4_alloc_open_stateid(clp);
3709 	if (!open->op_stp)
3710 		return nfserr_jukebox;
3711 
3712 	if (nfsd4_has_session(cstate) &&
3713 	    (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3714 		open->op_odstate = alloc_clnt_odstate(clp);
3715 		if (!open->op_odstate)
3716 			return nfserr_jukebox;
3717 	}
3718 
3719 	return nfs_ok;
3720 }
3721 
3722 static inline __be32
3723 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3724 {
3725 	if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3726 		return nfserr_openmode;
3727 	else
3728 		return nfs_ok;
3729 }
3730 
3731 static int share_access_to_flags(u32 share_access)
3732 {
3733 	return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3734 }
3735 
3736 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3737 {
3738 	struct nfs4_stid *ret;
3739 
3740 	ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3741 	if (!ret)
3742 		return NULL;
3743 	return delegstateid(ret);
3744 }
3745 
3746 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3747 {
3748 	return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3749 	       open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3750 }
3751 
3752 static __be32
3753 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3754 		struct nfs4_delegation **dp)
3755 {
3756 	int flags;
3757 	__be32 status = nfserr_bad_stateid;
3758 	struct nfs4_delegation *deleg;
3759 
3760 	deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3761 	if (deleg == NULL)
3762 		goto out;
3763 	flags = share_access_to_flags(open->op_share_access);
3764 	status = nfs4_check_delegmode(deleg, flags);
3765 	if (status) {
3766 		nfs4_put_stid(&deleg->dl_stid);
3767 		goto out;
3768 	}
3769 	*dp = deleg;
3770 out:
3771 	if (!nfsd4_is_deleg_cur(open))
3772 		return nfs_ok;
3773 	if (status)
3774 		return status;
3775 	open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3776 	return nfs_ok;
3777 }
3778 
3779 static struct nfs4_ol_stateid *
3780 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3781 {
3782 	struct nfs4_ol_stateid *local, *ret = NULL;
3783 	struct nfs4_openowner *oo = open->op_openowner;
3784 
3785 	spin_lock(&fp->fi_lock);
3786 	list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3787 		/* ignore lock owners */
3788 		if (local->st_stateowner->so_is_open_owner == 0)
3789 			continue;
3790 		if (local->st_stateowner == &oo->oo_owner) {
3791 			ret = local;
3792 			atomic_inc(&ret->st_stid.sc_count);
3793 			break;
3794 		}
3795 	}
3796 	spin_unlock(&fp->fi_lock);
3797 	return ret;
3798 }
3799 
3800 static inline int nfs4_access_to_access(u32 nfs4_access)
3801 {
3802 	int flags = 0;
3803 
3804 	if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3805 		flags |= NFSD_MAY_READ;
3806 	if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3807 		flags |= NFSD_MAY_WRITE;
3808 	return flags;
3809 }
3810 
3811 static inline __be32
3812 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3813 		struct nfsd4_open *open)
3814 {
3815 	struct iattr iattr = {
3816 		.ia_valid = ATTR_SIZE,
3817 		.ia_size = 0,
3818 	};
3819 	if (!open->op_truncate)
3820 		return 0;
3821 	if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3822 		return nfserr_inval;
3823 	return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3824 }
3825 
3826 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3827 		struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3828 		struct nfsd4_open *open)
3829 {
3830 	struct file *filp = NULL;
3831 	__be32 status;
3832 	int oflag = nfs4_access_to_omode(open->op_share_access);
3833 	int access = nfs4_access_to_access(open->op_share_access);
3834 	unsigned char old_access_bmap, old_deny_bmap;
3835 
3836 	spin_lock(&fp->fi_lock);
3837 
3838 	/*
3839 	 * Are we trying to set a deny mode that would conflict with
3840 	 * current access?
3841 	 */
3842 	status = nfs4_file_check_deny(fp, open->op_share_deny);
3843 	if (status != nfs_ok) {
3844 		spin_unlock(&fp->fi_lock);
3845 		goto out;
3846 	}
3847 
3848 	/* set access to the file */
3849 	status = nfs4_file_get_access(fp, open->op_share_access);
3850 	if (status != nfs_ok) {
3851 		spin_unlock(&fp->fi_lock);
3852 		goto out;
3853 	}
3854 
3855 	/* Set access bits in stateid */
3856 	old_access_bmap = stp->st_access_bmap;
3857 	set_access(open->op_share_access, stp);
3858 
3859 	/* Set new deny mask */
3860 	old_deny_bmap = stp->st_deny_bmap;
3861 	set_deny(open->op_share_deny, stp);
3862 	fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3863 
3864 	if (!fp->fi_fds[oflag]) {
3865 		spin_unlock(&fp->fi_lock);
3866 		status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3867 		if (status)
3868 			goto out_put_access;
3869 		spin_lock(&fp->fi_lock);
3870 		if (!fp->fi_fds[oflag]) {
3871 			fp->fi_fds[oflag] = filp;
3872 			filp = NULL;
3873 		}
3874 	}
3875 	spin_unlock(&fp->fi_lock);
3876 	if (filp)
3877 		fput(filp);
3878 
3879 	status = nfsd4_truncate(rqstp, cur_fh, open);
3880 	if (status)
3881 		goto out_put_access;
3882 out:
3883 	return status;
3884 out_put_access:
3885 	stp->st_access_bmap = old_access_bmap;
3886 	nfs4_file_put_access(fp, open->op_share_access);
3887 	reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3888 	goto out;
3889 }
3890 
3891 static __be32
3892 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
3893 {
3894 	__be32 status;
3895 	unsigned char old_deny_bmap = stp->st_deny_bmap;
3896 
3897 	if (!test_access(open->op_share_access, stp))
3898 		return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3899 
3900 	/* test and set deny mode */
3901 	spin_lock(&fp->fi_lock);
3902 	status = nfs4_file_check_deny(fp, open->op_share_deny);
3903 	if (status == nfs_ok) {
3904 		set_deny(open->op_share_deny, stp);
3905 		fp->fi_share_deny |=
3906 				(open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3907 	}
3908 	spin_unlock(&fp->fi_lock);
3909 
3910 	if (status != nfs_ok)
3911 		return status;
3912 
3913 	status = nfsd4_truncate(rqstp, cur_fh, open);
3914 	if (status != nfs_ok)
3915 		reset_union_bmap_deny(old_deny_bmap, stp);
3916 	return status;
3917 }
3918 
3919 /* Should we give out recallable state?: */
3920 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3921 {
3922 	if (clp->cl_cb_state == NFSD4_CB_UP)
3923 		return true;
3924 	/*
3925 	 * In the sessions case, since we don't have to establish a
3926 	 * separate connection for callbacks, we assume it's OK
3927 	 * until we hear otherwise:
3928 	 */
3929 	return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3930 }
3931 
3932 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3933 {
3934 	struct file_lock *fl;
3935 
3936 	fl = locks_alloc_lock();
3937 	if (!fl)
3938 		return NULL;
3939 	fl->fl_lmops = &nfsd_lease_mng_ops;
3940 	fl->fl_flags = FL_DELEG;
3941 	fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3942 	fl->fl_end = OFFSET_MAX;
3943 	fl->fl_owner = (fl_owner_t)fp;
3944 	fl->fl_pid = current->tgid;
3945 	return fl;
3946 }
3947 
3948 static int nfs4_setlease(struct nfs4_delegation *dp)
3949 {
3950 	struct nfs4_file *fp = dp->dl_stid.sc_file;
3951 	struct file_lock *fl;
3952 	struct file *filp;
3953 	int status = 0;
3954 
3955 	fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3956 	if (!fl)
3957 		return -ENOMEM;
3958 	filp = find_readable_file(fp);
3959 	if (!filp) {
3960 		/* We should always have a readable file here */
3961 		WARN_ON_ONCE(1);
3962 		locks_free_lock(fl);
3963 		return -EBADF;
3964 	}
3965 	fl->fl_file = filp;
3966 	status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
3967 	if (fl)
3968 		locks_free_lock(fl);
3969 	if (status)
3970 		goto out_fput;
3971 	spin_lock(&state_lock);
3972 	spin_lock(&fp->fi_lock);
3973 	/* Did the lease get broken before we took the lock? */
3974 	status = -EAGAIN;
3975 	if (fp->fi_had_conflict)
3976 		goto out_unlock;
3977 	/* Race breaker */
3978 	if (fp->fi_deleg_file) {
3979 		status = 0;
3980 		++fp->fi_delegees;
3981 		hash_delegation_locked(dp, fp);
3982 		goto out_unlock;
3983 	}
3984 	fp->fi_deleg_file = filp;
3985 	fp->fi_delegees = 1;
3986 	hash_delegation_locked(dp, fp);
3987 	spin_unlock(&fp->fi_lock);
3988 	spin_unlock(&state_lock);
3989 	return 0;
3990 out_unlock:
3991 	spin_unlock(&fp->fi_lock);
3992 	spin_unlock(&state_lock);
3993 out_fput:
3994 	fput(filp);
3995 	return status;
3996 }
3997 
3998 static struct nfs4_delegation *
3999 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4000 		    struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4001 {
4002 	int status;
4003 	struct nfs4_delegation *dp;
4004 
4005 	if (fp->fi_had_conflict)
4006 		return ERR_PTR(-EAGAIN);
4007 
4008 	dp = alloc_init_deleg(clp, fh, odstate);
4009 	if (!dp)
4010 		return ERR_PTR(-ENOMEM);
4011 
4012 	get_nfs4_file(fp);
4013 	spin_lock(&state_lock);
4014 	spin_lock(&fp->fi_lock);
4015 	dp->dl_stid.sc_file = fp;
4016 	if (!fp->fi_deleg_file) {
4017 		spin_unlock(&fp->fi_lock);
4018 		spin_unlock(&state_lock);
4019 		status = nfs4_setlease(dp);
4020 		goto out;
4021 	}
4022 	if (fp->fi_had_conflict) {
4023 		status = -EAGAIN;
4024 		goto out_unlock;
4025 	}
4026 	++fp->fi_delegees;
4027 	hash_delegation_locked(dp, fp);
4028 	status = 0;
4029 out_unlock:
4030 	spin_unlock(&fp->fi_lock);
4031 	spin_unlock(&state_lock);
4032 out:
4033 	if (status) {
4034 		put_clnt_odstate(dp->dl_clnt_odstate);
4035 		nfs4_put_stid(&dp->dl_stid);
4036 		return ERR_PTR(status);
4037 	}
4038 	return dp;
4039 }
4040 
4041 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4042 {
4043 	open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4044 	if (status == -EAGAIN)
4045 		open->op_why_no_deleg = WND4_CONTENTION;
4046 	else {
4047 		open->op_why_no_deleg = WND4_RESOURCE;
4048 		switch (open->op_deleg_want) {
4049 		case NFS4_SHARE_WANT_READ_DELEG:
4050 		case NFS4_SHARE_WANT_WRITE_DELEG:
4051 		case NFS4_SHARE_WANT_ANY_DELEG:
4052 			break;
4053 		case NFS4_SHARE_WANT_CANCEL:
4054 			open->op_why_no_deleg = WND4_CANCELLED;
4055 			break;
4056 		case NFS4_SHARE_WANT_NO_DELEG:
4057 			WARN_ON_ONCE(1);
4058 		}
4059 	}
4060 }
4061 
4062 /*
4063  * Attempt to hand out a delegation.
4064  *
4065  * Note we don't support write delegations, and won't until the vfs has
4066  * proper support for them.
4067  */
4068 static void
4069 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4070 			struct nfs4_ol_stateid *stp)
4071 {
4072 	struct nfs4_delegation *dp;
4073 	struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4074 	struct nfs4_client *clp = stp->st_stid.sc_client;
4075 	int cb_up;
4076 	int status = 0;
4077 
4078 	cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4079 	open->op_recall = 0;
4080 	switch (open->op_claim_type) {
4081 		case NFS4_OPEN_CLAIM_PREVIOUS:
4082 			if (!cb_up)
4083 				open->op_recall = 1;
4084 			if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4085 				goto out_no_deleg;
4086 			break;
4087 		case NFS4_OPEN_CLAIM_NULL:
4088 		case NFS4_OPEN_CLAIM_FH:
4089 			/*
4090 			 * Let's not give out any delegations till everyone's
4091 			 * had the chance to reclaim theirs, *and* until
4092 			 * NLM locks have all been reclaimed:
4093 			 */
4094 			if (locks_in_grace(clp->net))
4095 				goto out_no_deleg;
4096 			if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4097 				goto out_no_deleg;
4098 			/*
4099 			 * Also, if the file was opened for write or
4100 			 * create, there's a good chance the client's
4101 			 * about to write to it, resulting in an
4102 			 * immediate recall (since we don't support
4103 			 * write delegations):
4104 			 */
4105 			if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4106 				goto out_no_deleg;
4107 			if (open->op_create == NFS4_OPEN_CREATE)
4108 				goto out_no_deleg;
4109 			break;
4110 		default:
4111 			goto out_no_deleg;
4112 	}
4113 	dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4114 	if (IS_ERR(dp))
4115 		goto out_no_deleg;
4116 
4117 	memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4118 
4119 	dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4120 		STATEID_VAL(&dp->dl_stid.sc_stateid));
4121 	open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4122 	nfs4_put_stid(&dp->dl_stid);
4123 	return;
4124 out_no_deleg:
4125 	open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4126 	if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4127 	    open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4128 		dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4129 		open->op_recall = 1;
4130 	}
4131 
4132 	/* 4.1 client asking for a delegation? */
4133 	if (open->op_deleg_want)
4134 		nfsd4_open_deleg_none_ext(open, status);
4135 	return;
4136 }
4137 
4138 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4139 					struct nfs4_delegation *dp)
4140 {
4141 	if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4142 	    dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4143 		open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4144 		open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4145 	} else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4146 		   dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4147 		open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4148 		open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4149 	}
4150 	/* Otherwise the client must be confused wanting a delegation
4151 	 * it already has, therefore we don't return
4152 	 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4153 	 */
4154 }
4155 
4156 __be32
4157 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4158 {
4159 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
4160 	struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4161 	struct nfs4_file *fp = NULL;
4162 	struct nfs4_ol_stateid *stp = NULL;
4163 	struct nfs4_delegation *dp = NULL;
4164 	__be32 status;
4165 
4166 	/*
4167 	 * Lookup file; if found, lookup stateid and check open request,
4168 	 * and check for delegations in the process of being recalled.
4169 	 * If not found, create the nfs4_file struct
4170 	 */
4171 	fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
4172 	if (fp != open->op_file) {
4173 		status = nfs4_check_deleg(cl, open, &dp);
4174 		if (status)
4175 			goto out;
4176 		stp = nfsd4_find_existing_open(fp, open);
4177 	} else {
4178 		open->op_file = NULL;
4179 		status = nfserr_bad_stateid;
4180 		if (nfsd4_is_deleg_cur(open))
4181 			goto out;
4182 	}
4183 
4184 	/*
4185 	 * OPEN the file, or upgrade an existing OPEN.
4186 	 * If truncate fails, the OPEN fails.
4187 	 */
4188 	if (stp) {
4189 		/* Stateid was found, this is an OPEN upgrade */
4190 		status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4191 		if (status)
4192 			goto out;
4193 	} else {
4194 		stp = open->op_stp;
4195 		open->op_stp = NULL;
4196 		init_open_stateid(stp, fp, open);
4197 		status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4198 		if (status) {
4199 			release_open_stateid(stp);
4200 			goto out;
4201 		}
4202 
4203 		stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4204 							open->op_odstate);
4205 		if (stp->st_clnt_odstate == open->op_odstate)
4206 			open->op_odstate = NULL;
4207 	}
4208 	update_stateid(&stp->st_stid.sc_stateid);
4209 	memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4210 
4211 	if (nfsd4_has_session(&resp->cstate)) {
4212 		if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4213 			open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4214 			open->op_why_no_deleg = WND4_NOT_WANTED;
4215 			goto nodeleg;
4216 		}
4217 	}
4218 
4219 	/*
4220 	* Attempt to hand out a delegation. No error return, because the
4221 	* OPEN succeeds even if we fail.
4222 	*/
4223 	nfs4_open_delegation(current_fh, open, stp);
4224 nodeleg:
4225 	status = nfs_ok;
4226 
4227 	dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4228 		STATEID_VAL(&stp->st_stid.sc_stateid));
4229 out:
4230 	/* 4.1 client trying to upgrade/downgrade delegation? */
4231 	if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4232 	    open->op_deleg_want)
4233 		nfsd4_deleg_xgrade_none_ext(open, dp);
4234 
4235 	if (fp)
4236 		put_nfs4_file(fp);
4237 	if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4238 		open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4239 	/*
4240 	* To finish the open response, we just need to set the rflags.
4241 	*/
4242 	open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4243 	if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4244 	    !nfsd4_has_session(&resp->cstate))
4245 		open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4246 	if (dp)
4247 		nfs4_put_stid(&dp->dl_stid);
4248 	if (stp)
4249 		nfs4_put_stid(&stp->st_stid);
4250 
4251 	return status;
4252 }
4253 
4254 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4255 			      struct nfsd4_open *open)
4256 {
4257 	if (open->op_openowner) {
4258 		struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4259 
4260 		nfsd4_cstate_assign_replay(cstate, so);
4261 		nfs4_put_stateowner(so);
4262 	}
4263 	if (open->op_file)
4264 		kmem_cache_free(file_slab, open->op_file);
4265 	if (open->op_stp)
4266 		nfs4_put_stid(&open->op_stp->st_stid);
4267 	if (open->op_odstate)
4268 		kmem_cache_free(odstate_slab, open->op_odstate);
4269 }
4270 
4271 __be32
4272 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4273 	    clientid_t *clid)
4274 {
4275 	struct nfs4_client *clp;
4276 	__be32 status;
4277 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4278 
4279 	dprintk("process_renew(%08x/%08x): starting\n",
4280 			clid->cl_boot, clid->cl_id);
4281 	status = lookup_clientid(clid, cstate, nn);
4282 	if (status)
4283 		goto out;
4284 	clp = cstate->clp;
4285 	status = nfserr_cb_path_down;
4286 	if (!list_empty(&clp->cl_delegations)
4287 			&& clp->cl_cb_state != NFSD4_CB_UP)
4288 		goto out;
4289 	status = nfs_ok;
4290 out:
4291 	return status;
4292 }
4293 
4294 void
4295 nfsd4_end_grace(struct nfsd_net *nn)
4296 {
4297 	/* do nothing if grace period already ended */
4298 	if (nn->grace_ended)
4299 		return;
4300 
4301 	dprintk("NFSD: end of grace period\n");
4302 	nn->grace_ended = true;
4303 	/*
4304 	 * If the server goes down again right now, an NFSv4
4305 	 * client will still be allowed to reclaim after it comes back up,
4306 	 * even if it hasn't yet had a chance to reclaim state this time.
4307 	 *
4308 	 */
4309 	nfsd4_record_grace_done(nn);
4310 	/*
4311 	 * At this point, NFSv4 clients can still reclaim.  But if the
4312 	 * server crashes, any that have not yet reclaimed will be out
4313 	 * of luck on the next boot.
4314 	 *
4315 	 * (NFSv4.1+ clients are considered to have reclaimed once they
4316 	 * call RECLAIM_COMPLETE.  NFSv4.0 clients are considered to
4317 	 * have reclaimed after their first OPEN.)
4318 	 */
4319 	locks_end_grace(&nn->nfsd4_manager);
4320 	/*
4321 	 * At this point, and once lockd and/or any other containers
4322 	 * exit their grace period, further reclaims will fail and
4323 	 * regular locking can resume.
4324 	 */
4325 }
4326 
4327 static time_t
4328 nfs4_laundromat(struct nfsd_net *nn)
4329 {
4330 	struct nfs4_client *clp;
4331 	struct nfs4_openowner *oo;
4332 	struct nfs4_delegation *dp;
4333 	struct nfs4_ol_stateid *stp;
4334 	struct list_head *pos, *next, reaplist;
4335 	time_t cutoff = get_seconds() - nn->nfsd4_lease;
4336 	time_t t, new_timeo = nn->nfsd4_lease;
4337 
4338 	dprintk("NFSD: laundromat service - starting\n");
4339 	nfsd4_end_grace(nn);
4340 	INIT_LIST_HEAD(&reaplist);
4341 	spin_lock(&nn->client_lock);
4342 	list_for_each_safe(pos, next, &nn->client_lru) {
4343 		clp = list_entry(pos, struct nfs4_client, cl_lru);
4344 		if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4345 			t = clp->cl_time - cutoff;
4346 			new_timeo = min(new_timeo, t);
4347 			break;
4348 		}
4349 		if (mark_client_expired_locked(clp)) {
4350 			dprintk("NFSD: client in use (clientid %08x)\n",
4351 				clp->cl_clientid.cl_id);
4352 			continue;
4353 		}
4354 		list_add(&clp->cl_lru, &reaplist);
4355 	}
4356 	spin_unlock(&nn->client_lock);
4357 	list_for_each_safe(pos, next, &reaplist) {
4358 		clp = list_entry(pos, struct nfs4_client, cl_lru);
4359 		dprintk("NFSD: purging unused client (clientid %08x)\n",
4360 			clp->cl_clientid.cl_id);
4361 		list_del_init(&clp->cl_lru);
4362 		expire_client(clp);
4363 	}
4364 	spin_lock(&state_lock);
4365 	list_for_each_safe(pos, next, &nn->del_recall_lru) {
4366 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4367 		if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4368 			t = dp->dl_time - cutoff;
4369 			new_timeo = min(new_timeo, t);
4370 			break;
4371 		}
4372 		WARN_ON(!unhash_delegation_locked(dp));
4373 		list_add(&dp->dl_recall_lru, &reaplist);
4374 	}
4375 	spin_unlock(&state_lock);
4376 	while (!list_empty(&reaplist)) {
4377 		dp = list_first_entry(&reaplist, struct nfs4_delegation,
4378 					dl_recall_lru);
4379 		list_del_init(&dp->dl_recall_lru);
4380 		revoke_delegation(dp);
4381 	}
4382 
4383 	spin_lock(&nn->client_lock);
4384 	while (!list_empty(&nn->close_lru)) {
4385 		oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4386 					oo_close_lru);
4387 		if (time_after((unsigned long)oo->oo_time,
4388 			       (unsigned long)cutoff)) {
4389 			t = oo->oo_time - cutoff;
4390 			new_timeo = min(new_timeo, t);
4391 			break;
4392 		}
4393 		list_del_init(&oo->oo_close_lru);
4394 		stp = oo->oo_last_closed_stid;
4395 		oo->oo_last_closed_stid = NULL;
4396 		spin_unlock(&nn->client_lock);
4397 		nfs4_put_stid(&stp->st_stid);
4398 		spin_lock(&nn->client_lock);
4399 	}
4400 	spin_unlock(&nn->client_lock);
4401 
4402 	new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4403 	return new_timeo;
4404 }
4405 
4406 static struct workqueue_struct *laundry_wq;
4407 static void laundromat_main(struct work_struct *);
4408 
4409 static void
4410 laundromat_main(struct work_struct *laundry)
4411 {
4412 	time_t t;
4413 	struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4414 						  work);
4415 	struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4416 					   laundromat_work);
4417 
4418 	t = nfs4_laundromat(nn);
4419 	dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4420 	queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4421 }
4422 
4423 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4424 {
4425 	if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4426 		return nfserr_bad_stateid;
4427 	return nfs_ok;
4428 }
4429 
4430 static inline int
4431 access_permit_read(struct nfs4_ol_stateid *stp)
4432 {
4433 	return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4434 		test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4435 		test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4436 }
4437 
4438 static inline int
4439 access_permit_write(struct nfs4_ol_stateid *stp)
4440 {
4441 	return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4442 		test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4443 }
4444 
4445 static
4446 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4447 {
4448         __be32 status = nfserr_openmode;
4449 
4450 	/* For lock stateid's, we test the parent open, not the lock: */
4451 	if (stp->st_openstp)
4452 		stp = stp->st_openstp;
4453 	if ((flags & WR_STATE) && !access_permit_write(stp))
4454                 goto out;
4455 	if ((flags & RD_STATE) && !access_permit_read(stp))
4456                 goto out;
4457 	status = nfs_ok;
4458 out:
4459 	return status;
4460 }
4461 
4462 static inline __be32
4463 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4464 {
4465 	if (ONE_STATEID(stateid) && (flags & RD_STATE))
4466 		return nfs_ok;
4467 	else if (opens_in_grace(net)) {
4468 		/* Answer in remaining cases depends on existence of
4469 		 * conflicting state; so we must wait out the grace period. */
4470 		return nfserr_grace;
4471 	} else if (flags & WR_STATE)
4472 		return nfs4_share_conflict(current_fh,
4473 				NFS4_SHARE_DENY_WRITE);
4474 	else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4475 		return nfs4_share_conflict(current_fh,
4476 				NFS4_SHARE_DENY_READ);
4477 }
4478 
4479 /*
4480  * Allow READ/WRITE during grace period on recovered state only for files
4481  * that are not able to provide mandatory locking.
4482  */
4483 static inline int
4484 grace_disallows_io(struct net *net, struct inode *inode)
4485 {
4486 	return opens_in_grace(net) && mandatory_lock(inode);
4487 }
4488 
4489 /* Returns true iff a is later than b: */
4490 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4491 {
4492 	return (s32)(a->si_generation - b->si_generation) > 0;
4493 }
4494 
4495 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4496 {
4497 	/*
4498 	 * When sessions are used the stateid generation number is ignored
4499 	 * when it is zero.
4500 	 */
4501 	if (has_session && in->si_generation == 0)
4502 		return nfs_ok;
4503 
4504 	if (in->si_generation == ref->si_generation)
4505 		return nfs_ok;
4506 
4507 	/* If the client sends us a stateid from the future, it's buggy: */
4508 	if (stateid_generation_after(in, ref))
4509 		return nfserr_bad_stateid;
4510 	/*
4511 	 * However, we could see a stateid from the past, even from a
4512 	 * non-buggy client.  For example, if the client sends a lock
4513 	 * while some IO is outstanding, the lock may bump si_generation
4514 	 * while the IO is still in flight.  The client could avoid that
4515 	 * situation by waiting for responses on all the IO requests,
4516 	 * but better performance may result in retrying IO that
4517 	 * receives an old_stateid error if requests are rarely
4518 	 * reordered in flight:
4519 	 */
4520 	return nfserr_old_stateid;
4521 }
4522 
4523 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4524 {
4525 	if (ols->st_stateowner->so_is_open_owner &&
4526 	    !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4527 		return nfserr_bad_stateid;
4528 	return nfs_ok;
4529 }
4530 
4531 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4532 {
4533 	struct nfs4_stid *s;
4534 	__be32 status = nfserr_bad_stateid;
4535 
4536 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4537 		return status;
4538 	/* Client debugging aid. */
4539 	if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4540 		char addr_str[INET6_ADDRSTRLEN];
4541 		rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4542 				 sizeof(addr_str));
4543 		pr_warn_ratelimited("NFSD: client %s testing state ID "
4544 					"with incorrect client ID\n", addr_str);
4545 		return status;
4546 	}
4547 	spin_lock(&cl->cl_lock);
4548 	s = find_stateid_locked(cl, stateid);
4549 	if (!s)
4550 		goto out_unlock;
4551 	status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4552 	if (status)
4553 		goto out_unlock;
4554 	switch (s->sc_type) {
4555 	case NFS4_DELEG_STID:
4556 		status = nfs_ok;
4557 		break;
4558 	case NFS4_REVOKED_DELEG_STID:
4559 		status = nfserr_deleg_revoked;
4560 		break;
4561 	case NFS4_OPEN_STID:
4562 	case NFS4_LOCK_STID:
4563 		status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4564 		break;
4565 	default:
4566 		printk("unknown stateid type %x\n", s->sc_type);
4567 		/* Fallthrough */
4568 	case NFS4_CLOSED_STID:
4569 	case NFS4_CLOSED_DELEG_STID:
4570 		status = nfserr_bad_stateid;
4571 	}
4572 out_unlock:
4573 	spin_unlock(&cl->cl_lock);
4574 	return status;
4575 }
4576 
4577 __be32
4578 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4579 		     stateid_t *stateid, unsigned char typemask,
4580 		     struct nfs4_stid **s, struct nfsd_net *nn)
4581 {
4582 	__be32 status;
4583 
4584 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4585 		return nfserr_bad_stateid;
4586 	status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4587 	if (status == nfserr_stale_clientid) {
4588 		if (cstate->session)
4589 			return nfserr_bad_stateid;
4590 		return nfserr_stale_stateid;
4591 	}
4592 	if (status)
4593 		return status;
4594 	*s = find_stateid_by_type(cstate->clp, stateid, typemask);
4595 	if (!*s)
4596 		return nfserr_bad_stateid;
4597 	return nfs_ok;
4598 }
4599 
4600 static struct file *
4601 nfs4_find_file(struct nfs4_stid *s, int flags)
4602 {
4603 	if (!s)
4604 		return NULL;
4605 
4606 	switch (s->sc_type) {
4607 	case NFS4_DELEG_STID:
4608 		if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4609 			return NULL;
4610 		return get_file(s->sc_file->fi_deleg_file);
4611 	case NFS4_OPEN_STID:
4612 	case NFS4_LOCK_STID:
4613 		if (flags & RD_STATE)
4614 			return find_readable_file(s->sc_file);
4615 		else
4616 			return find_writeable_file(s->sc_file);
4617 		break;
4618 	}
4619 
4620 	return NULL;
4621 }
4622 
4623 static __be32
4624 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4625 {
4626 	__be32 status;
4627 
4628 	status = nfsd4_check_openowner_confirmed(ols);
4629 	if (status)
4630 		return status;
4631 	return nfs4_check_openmode(ols, flags);
4632 }
4633 
4634 static __be32
4635 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4636 		struct file **filpp, bool *tmp_file, int flags)
4637 {
4638 	int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4639 	struct file *file;
4640 	__be32 status;
4641 
4642 	file = nfs4_find_file(s, flags);
4643 	if (file) {
4644 		status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4645 				acc | NFSD_MAY_OWNER_OVERRIDE);
4646 		if (status) {
4647 			fput(file);
4648 			return status;
4649 		}
4650 
4651 		*filpp = file;
4652 	} else {
4653 		status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4654 		if (status)
4655 			return status;
4656 
4657 		if (tmp_file)
4658 			*tmp_file = true;
4659 	}
4660 
4661 	return 0;
4662 }
4663 
4664 /*
4665  * Checks for stateid operations
4666  */
4667 __be32
4668 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4669 		struct nfsd4_compound_state *cstate, stateid_t *stateid,
4670 		int flags, struct file **filpp, bool *tmp_file)
4671 {
4672 	struct svc_fh *fhp = &cstate->current_fh;
4673 	struct inode *ino = d_inode(fhp->fh_dentry);
4674 	struct net *net = SVC_NET(rqstp);
4675 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4676 	struct nfs4_stid *s = NULL;
4677 	__be32 status;
4678 
4679 	if (filpp)
4680 		*filpp = NULL;
4681 	if (tmp_file)
4682 		*tmp_file = false;
4683 
4684 	if (grace_disallows_io(net, ino))
4685 		return nfserr_grace;
4686 
4687 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4688 		status = check_special_stateids(net, fhp, stateid, flags);
4689 		goto done;
4690 	}
4691 
4692 	status = nfsd4_lookup_stateid(cstate, stateid,
4693 				NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4694 				&s, nn);
4695 	if (status)
4696 		return status;
4697 	status = check_stateid_generation(stateid, &s->sc_stateid,
4698 			nfsd4_has_session(cstate));
4699 	if (status)
4700 		goto out;
4701 
4702 	switch (s->sc_type) {
4703 	case NFS4_DELEG_STID:
4704 		status = nfs4_check_delegmode(delegstateid(s), flags);
4705 		break;
4706 	case NFS4_OPEN_STID:
4707 	case NFS4_LOCK_STID:
4708 		status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
4709 		break;
4710 	default:
4711 		status = nfserr_bad_stateid;
4712 		break;
4713 	}
4714 	if (status)
4715 		goto out;
4716 	status = nfs4_check_fh(fhp, s);
4717 
4718 done:
4719 	if (!status && filpp)
4720 		status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
4721 out:
4722 	if (s)
4723 		nfs4_put_stid(s);
4724 	return status;
4725 }
4726 
4727 /*
4728  * Test if the stateid is valid
4729  */
4730 __be32
4731 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4732 		   struct nfsd4_test_stateid *test_stateid)
4733 {
4734 	struct nfsd4_test_stateid_id *stateid;
4735 	struct nfs4_client *cl = cstate->session->se_client;
4736 
4737 	list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4738 		stateid->ts_id_status =
4739 			nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4740 
4741 	return nfs_ok;
4742 }
4743 
4744 __be32
4745 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4746 		   struct nfsd4_free_stateid *free_stateid)
4747 {
4748 	stateid_t *stateid = &free_stateid->fr_stateid;
4749 	struct nfs4_stid *s;
4750 	struct nfs4_delegation *dp;
4751 	struct nfs4_ol_stateid *stp;
4752 	struct nfs4_client *cl = cstate->session->se_client;
4753 	__be32 ret = nfserr_bad_stateid;
4754 
4755 	spin_lock(&cl->cl_lock);
4756 	s = find_stateid_locked(cl, stateid);
4757 	if (!s)
4758 		goto out_unlock;
4759 	switch (s->sc_type) {
4760 	case NFS4_DELEG_STID:
4761 		ret = nfserr_locks_held;
4762 		break;
4763 	case NFS4_OPEN_STID:
4764 		ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4765 		if (ret)
4766 			break;
4767 		ret = nfserr_locks_held;
4768 		break;
4769 	case NFS4_LOCK_STID:
4770 		ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4771 		if (ret)
4772 			break;
4773 		stp = openlockstateid(s);
4774 		ret = nfserr_locks_held;
4775 		if (check_for_locks(stp->st_stid.sc_file,
4776 				    lockowner(stp->st_stateowner)))
4777 			break;
4778 		WARN_ON(!unhash_lock_stateid(stp));
4779 		spin_unlock(&cl->cl_lock);
4780 		nfs4_put_stid(s);
4781 		ret = nfs_ok;
4782 		goto out;
4783 	case NFS4_REVOKED_DELEG_STID:
4784 		dp = delegstateid(s);
4785 		list_del_init(&dp->dl_recall_lru);
4786 		spin_unlock(&cl->cl_lock);
4787 		nfs4_put_stid(s);
4788 		ret = nfs_ok;
4789 		goto out;
4790 	/* Default falls through and returns nfserr_bad_stateid */
4791 	}
4792 out_unlock:
4793 	spin_unlock(&cl->cl_lock);
4794 out:
4795 	return ret;
4796 }
4797 
4798 static inline int
4799 setlkflg (int type)
4800 {
4801 	return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4802 		RD_STATE : WR_STATE;
4803 }
4804 
4805 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4806 {
4807 	struct svc_fh *current_fh = &cstate->current_fh;
4808 	struct nfs4_stateowner *sop = stp->st_stateowner;
4809 	__be32 status;
4810 
4811 	status = nfsd4_check_seqid(cstate, sop, seqid);
4812 	if (status)
4813 		return status;
4814 	if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4815 		|| stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4816 		/*
4817 		 * "Closed" stateid's exist *only* to return
4818 		 * nfserr_replay_me from the previous step, and
4819 		 * revoked delegations are kept only for free_stateid.
4820 		 */
4821 		return nfserr_bad_stateid;
4822 	status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4823 	if (status)
4824 		return status;
4825 	return nfs4_check_fh(current_fh, &stp->st_stid);
4826 }
4827 
4828 /*
4829  * Checks for sequence id mutating operations.
4830  */
4831 static __be32
4832 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4833 			 stateid_t *stateid, char typemask,
4834 			 struct nfs4_ol_stateid **stpp,
4835 			 struct nfsd_net *nn)
4836 {
4837 	__be32 status;
4838 	struct nfs4_stid *s;
4839 	struct nfs4_ol_stateid *stp = NULL;
4840 
4841 	dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4842 		seqid, STATEID_VAL(stateid));
4843 
4844 	*stpp = NULL;
4845 	status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4846 	if (status)
4847 		return status;
4848 	stp = openlockstateid(s);
4849 	nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4850 
4851 	status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4852 	if (!status)
4853 		*stpp = stp;
4854 	else
4855 		nfs4_put_stid(&stp->st_stid);
4856 	return status;
4857 }
4858 
4859 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4860 						 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4861 {
4862 	__be32 status;
4863 	struct nfs4_openowner *oo;
4864 	struct nfs4_ol_stateid *stp;
4865 
4866 	status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4867 						NFS4_OPEN_STID, &stp, nn);
4868 	if (status)
4869 		return status;
4870 	oo = openowner(stp->st_stateowner);
4871 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4872 		nfs4_put_stid(&stp->st_stid);
4873 		return nfserr_bad_stateid;
4874 	}
4875 	*stpp = stp;
4876 	return nfs_ok;
4877 }
4878 
4879 __be32
4880 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4881 		   struct nfsd4_open_confirm *oc)
4882 {
4883 	__be32 status;
4884 	struct nfs4_openowner *oo;
4885 	struct nfs4_ol_stateid *stp;
4886 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4887 
4888 	dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4889 			cstate->current_fh.fh_dentry);
4890 
4891 	status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4892 	if (status)
4893 		return status;
4894 
4895 	status = nfs4_preprocess_seqid_op(cstate,
4896 					oc->oc_seqid, &oc->oc_req_stateid,
4897 					NFS4_OPEN_STID, &stp, nn);
4898 	if (status)
4899 		goto out;
4900 	oo = openowner(stp->st_stateowner);
4901 	status = nfserr_bad_stateid;
4902 	if (oo->oo_flags & NFS4_OO_CONFIRMED)
4903 		goto put_stateid;
4904 	oo->oo_flags |= NFS4_OO_CONFIRMED;
4905 	update_stateid(&stp->st_stid.sc_stateid);
4906 	memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4907 	dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4908 		__func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4909 
4910 	nfsd4_client_record_create(oo->oo_owner.so_client);
4911 	status = nfs_ok;
4912 put_stateid:
4913 	nfs4_put_stid(&stp->st_stid);
4914 out:
4915 	nfsd4_bump_seqid(cstate, status);
4916 	return status;
4917 }
4918 
4919 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4920 {
4921 	if (!test_access(access, stp))
4922 		return;
4923 	nfs4_file_put_access(stp->st_stid.sc_file, access);
4924 	clear_access(access, stp);
4925 }
4926 
4927 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4928 {
4929 	switch (to_access) {
4930 	case NFS4_SHARE_ACCESS_READ:
4931 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4932 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4933 		break;
4934 	case NFS4_SHARE_ACCESS_WRITE:
4935 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4936 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4937 		break;
4938 	case NFS4_SHARE_ACCESS_BOTH:
4939 		break;
4940 	default:
4941 		WARN_ON_ONCE(1);
4942 	}
4943 }
4944 
4945 __be32
4946 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4947 		     struct nfsd4_compound_state *cstate,
4948 		     struct nfsd4_open_downgrade *od)
4949 {
4950 	__be32 status;
4951 	struct nfs4_ol_stateid *stp;
4952 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4953 
4954 	dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4955 			cstate->current_fh.fh_dentry);
4956 
4957 	/* We don't yet support WANT bits: */
4958 	if (od->od_deleg_want)
4959 		dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4960 			od->od_deleg_want);
4961 
4962 	status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4963 					&od->od_stateid, &stp, nn);
4964 	if (status)
4965 		goto out;
4966 	status = nfserr_inval;
4967 	if (!test_access(od->od_share_access, stp)) {
4968 		dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4969 			stp->st_access_bmap, od->od_share_access);
4970 		goto put_stateid;
4971 	}
4972 	if (!test_deny(od->od_share_deny, stp)) {
4973 		dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4974 			stp->st_deny_bmap, od->od_share_deny);
4975 		goto put_stateid;
4976 	}
4977 	nfs4_stateid_downgrade(stp, od->od_share_access);
4978 
4979 	reset_union_bmap_deny(od->od_share_deny, stp);
4980 
4981 	update_stateid(&stp->st_stid.sc_stateid);
4982 	memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4983 	status = nfs_ok;
4984 put_stateid:
4985 	nfs4_put_stid(&stp->st_stid);
4986 out:
4987 	nfsd4_bump_seqid(cstate, status);
4988 	return status;
4989 }
4990 
4991 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4992 {
4993 	struct nfs4_client *clp = s->st_stid.sc_client;
4994 	bool unhashed;
4995 	LIST_HEAD(reaplist);
4996 
4997 	s->st_stid.sc_type = NFS4_CLOSED_STID;
4998 	spin_lock(&clp->cl_lock);
4999 	unhashed = unhash_open_stateid(s, &reaplist);
5000 
5001 	if (clp->cl_minorversion) {
5002 		if (unhashed)
5003 			put_ol_stateid_locked(s, &reaplist);
5004 		spin_unlock(&clp->cl_lock);
5005 		free_ol_stateid_reaplist(&reaplist);
5006 	} else {
5007 		spin_unlock(&clp->cl_lock);
5008 		free_ol_stateid_reaplist(&reaplist);
5009 		if (unhashed)
5010 			move_to_close_lru(s, clp->net);
5011 	}
5012 }
5013 
5014 /*
5015  * nfs4_unlock_state() called after encode
5016  */
5017 __be32
5018 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5019 	    struct nfsd4_close *close)
5020 {
5021 	__be32 status;
5022 	struct nfs4_ol_stateid *stp;
5023 	struct net *net = SVC_NET(rqstp);
5024 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5025 
5026 	dprintk("NFSD: nfsd4_close on file %pd\n",
5027 			cstate->current_fh.fh_dentry);
5028 
5029 	status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5030 					&close->cl_stateid,
5031 					NFS4_OPEN_STID|NFS4_CLOSED_STID,
5032 					&stp, nn);
5033 	nfsd4_bump_seqid(cstate, status);
5034 	if (status)
5035 		goto out;
5036 	update_stateid(&stp->st_stid.sc_stateid);
5037 	memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5038 
5039 	nfsd4_close_open_stateid(stp);
5040 
5041 	/* put reference from nfs4_preprocess_seqid_op */
5042 	nfs4_put_stid(&stp->st_stid);
5043 out:
5044 	return status;
5045 }
5046 
5047 __be32
5048 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5049 		  struct nfsd4_delegreturn *dr)
5050 {
5051 	struct nfs4_delegation *dp;
5052 	stateid_t *stateid = &dr->dr_stateid;
5053 	struct nfs4_stid *s;
5054 	__be32 status;
5055 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5056 
5057 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5058 		return status;
5059 
5060 	status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5061 	if (status)
5062 		goto out;
5063 	dp = delegstateid(s);
5064 	status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5065 	if (status)
5066 		goto put_stateid;
5067 
5068 	destroy_delegation(dp);
5069 put_stateid:
5070 	nfs4_put_stid(&dp->dl_stid);
5071 out:
5072 	return status;
5073 }
5074 
5075 static inline u64
5076 end_offset(u64 start, u64 len)
5077 {
5078 	u64 end;
5079 
5080 	end = start + len;
5081 	return end >= start ? end: NFS4_MAX_UINT64;
5082 }
5083 
5084 /* last octet in a range */
5085 static inline u64
5086 last_byte_offset(u64 start, u64 len)
5087 {
5088 	u64 end;
5089 
5090 	WARN_ON_ONCE(!len);
5091 	end = start + len;
5092 	return end > start ? end - 1: NFS4_MAX_UINT64;
5093 }
5094 
5095 /*
5096  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5097  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5098  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
5099  * locking, this prevents us from being completely protocol-compliant.  The
5100  * real solution to this problem is to start using unsigned file offsets in
5101  * the VFS, but this is a very deep change!
5102  */
5103 static inline void
5104 nfs4_transform_lock_offset(struct file_lock *lock)
5105 {
5106 	if (lock->fl_start < 0)
5107 		lock->fl_start = OFFSET_MAX;
5108 	if (lock->fl_end < 0)
5109 		lock->fl_end = OFFSET_MAX;
5110 }
5111 
5112 static fl_owner_t
5113 nfsd4_fl_get_owner(fl_owner_t owner)
5114 {
5115 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5116 
5117 	nfs4_get_stateowner(&lo->lo_owner);
5118 	return owner;
5119 }
5120 
5121 static void
5122 nfsd4_fl_put_owner(fl_owner_t owner)
5123 {
5124 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5125 
5126 	if (lo)
5127 		nfs4_put_stateowner(&lo->lo_owner);
5128 }
5129 
5130 static const struct lock_manager_operations nfsd_posix_mng_ops  = {
5131 	.lm_get_owner = nfsd4_fl_get_owner,
5132 	.lm_put_owner = nfsd4_fl_put_owner,
5133 };
5134 
5135 static inline void
5136 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5137 {
5138 	struct nfs4_lockowner *lo;
5139 
5140 	if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5141 		lo = (struct nfs4_lockowner *) fl->fl_owner;
5142 		deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5143 					lo->lo_owner.so_owner.len, GFP_KERNEL);
5144 		if (!deny->ld_owner.data)
5145 			/* We just don't care that much */
5146 			goto nevermind;
5147 		deny->ld_owner.len = lo->lo_owner.so_owner.len;
5148 		deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5149 	} else {
5150 nevermind:
5151 		deny->ld_owner.len = 0;
5152 		deny->ld_owner.data = NULL;
5153 		deny->ld_clientid.cl_boot = 0;
5154 		deny->ld_clientid.cl_id = 0;
5155 	}
5156 	deny->ld_start = fl->fl_start;
5157 	deny->ld_length = NFS4_MAX_UINT64;
5158 	if (fl->fl_end != NFS4_MAX_UINT64)
5159 		deny->ld_length = fl->fl_end - fl->fl_start + 1;
5160 	deny->ld_type = NFS4_READ_LT;
5161 	if (fl->fl_type != F_RDLCK)
5162 		deny->ld_type = NFS4_WRITE_LT;
5163 }
5164 
5165 static struct nfs4_lockowner *
5166 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5167 {
5168 	unsigned int strhashval = ownerstr_hashval(owner);
5169 	struct nfs4_stateowner *so;
5170 
5171 	lockdep_assert_held(&clp->cl_lock);
5172 
5173 	list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5174 			    so_strhash) {
5175 		if (so->so_is_open_owner)
5176 			continue;
5177 		if (same_owner_str(so, owner))
5178 			return lockowner(nfs4_get_stateowner(so));
5179 	}
5180 	return NULL;
5181 }
5182 
5183 static struct nfs4_lockowner *
5184 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5185 {
5186 	struct nfs4_lockowner *lo;
5187 
5188 	spin_lock(&clp->cl_lock);
5189 	lo = find_lockowner_str_locked(clp, owner);
5190 	spin_unlock(&clp->cl_lock);
5191 	return lo;
5192 }
5193 
5194 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5195 {
5196 	unhash_lockowner_locked(lockowner(sop));
5197 }
5198 
5199 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5200 {
5201 	struct nfs4_lockowner *lo = lockowner(sop);
5202 
5203 	kmem_cache_free(lockowner_slab, lo);
5204 }
5205 
5206 static const struct nfs4_stateowner_operations lockowner_ops = {
5207 	.so_unhash =	nfs4_unhash_lockowner,
5208 	.so_free =	nfs4_free_lockowner,
5209 };
5210 
5211 /*
5212  * Alloc a lock owner structure.
5213  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5214  * occurred.
5215  *
5216  * strhashval = ownerstr_hashval
5217  */
5218 static struct nfs4_lockowner *
5219 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5220 			   struct nfs4_ol_stateid *open_stp,
5221 			   struct nfsd4_lock *lock)
5222 {
5223 	struct nfs4_lockowner *lo, *ret;
5224 
5225 	lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5226 	if (!lo)
5227 		return NULL;
5228 	INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5229 	lo->lo_owner.so_is_open_owner = 0;
5230 	lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5231 	lo->lo_owner.so_ops = &lockowner_ops;
5232 	spin_lock(&clp->cl_lock);
5233 	ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5234 	if (ret == NULL) {
5235 		list_add(&lo->lo_owner.so_strhash,
5236 			 &clp->cl_ownerstr_hashtbl[strhashval]);
5237 		ret = lo;
5238 	} else
5239 		nfs4_free_stateowner(&lo->lo_owner);
5240 
5241 	spin_unlock(&clp->cl_lock);
5242 	return ret;
5243 }
5244 
5245 static void
5246 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5247 		  struct nfs4_file *fp, struct inode *inode,
5248 		  struct nfs4_ol_stateid *open_stp)
5249 {
5250 	struct nfs4_client *clp = lo->lo_owner.so_client;
5251 
5252 	lockdep_assert_held(&clp->cl_lock);
5253 
5254 	atomic_inc(&stp->st_stid.sc_count);
5255 	stp->st_stid.sc_type = NFS4_LOCK_STID;
5256 	stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5257 	get_nfs4_file(fp);
5258 	stp->st_stid.sc_file = fp;
5259 	stp->st_stid.sc_free = nfs4_free_lock_stateid;
5260 	stp->st_access_bmap = 0;
5261 	stp->st_deny_bmap = open_stp->st_deny_bmap;
5262 	stp->st_openstp = open_stp;
5263 	list_add(&stp->st_locks, &open_stp->st_locks);
5264 	list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5265 	spin_lock(&fp->fi_lock);
5266 	list_add(&stp->st_perfile, &fp->fi_stateids);
5267 	spin_unlock(&fp->fi_lock);
5268 }
5269 
5270 static struct nfs4_ol_stateid *
5271 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5272 {
5273 	struct nfs4_ol_stateid *lst;
5274 	struct nfs4_client *clp = lo->lo_owner.so_client;
5275 
5276 	lockdep_assert_held(&clp->cl_lock);
5277 
5278 	list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5279 		if (lst->st_stid.sc_file == fp) {
5280 			atomic_inc(&lst->st_stid.sc_count);
5281 			return lst;
5282 		}
5283 	}
5284 	return NULL;
5285 }
5286 
5287 static struct nfs4_ol_stateid *
5288 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5289 			    struct inode *inode, struct nfs4_ol_stateid *ost,
5290 			    bool *new)
5291 {
5292 	struct nfs4_stid *ns = NULL;
5293 	struct nfs4_ol_stateid *lst;
5294 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5295 	struct nfs4_client *clp = oo->oo_owner.so_client;
5296 
5297 	spin_lock(&clp->cl_lock);
5298 	lst = find_lock_stateid(lo, fi);
5299 	if (lst == NULL) {
5300 		spin_unlock(&clp->cl_lock);
5301 		ns = nfs4_alloc_stid(clp, stateid_slab);
5302 		if (ns == NULL)
5303 			return NULL;
5304 
5305 		spin_lock(&clp->cl_lock);
5306 		lst = find_lock_stateid(lo, fi);
5307 		if (likely(!lst)) {
5308 			lst = openlockstateid(ns);
5309 			init_lock_stateid(lst, lo, fi, inode, ost);
5310 			ns = NULL;
5311 			*new = true;
5312 		}
5313 	}
5314 	spin_unlock(&clp->cl_lock);
5315 	if (ns)
5316 		nfs4_put_stid(ns);
5317 	return lst;
5318 }
5319 
5320 static int
5321 check_lock_length(u64 offset, u64 length)
5322 {
5323 	return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5324 		(length > ~offset)));
5325 }
5326 
5327 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5328 {
5329 	struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5330 
5331 	lockdep_assert_held(&fp->fi_lock);
5332 
5333 	if (test_access(access, lock_stp))
5334 		return;
5335 	__nfs4_file_get_access(fp, access);
5336 	set_access(access, lock_stp);
5337 }
5338 
5339 static __be32
5340 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5341 			    struct nfs4_ol_stateid *ost,
5342 			    struct nfsd4_lock *lock,
5343 			    struct nfs4_ol_stateid **lst, bool *new)
5344 {
5345 	__be32 status;
5346 	struct nfs4_file *fi = ost->st_stid.sc_file;
5347 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5348 	struct nfs4_client *cl = oo->oo_owner.so_client;
5349 	struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5350 	struct nfs4_lockowner *lo;
5351 	unsigned int strhashval;
5352 
5353 	lo = find_lockowner_str(cl, &lock->lk_new_owner);
5354 	if (!lo) {
5355 		strhashval = ownerstr_hashval(&lock->lk_new_owner);
5356 		lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5357 		if (lo == NULL)
5358 			return nfserr_jukebox;
5359 	} else {
5360 		/* with an existing lockowner, seqids must be the same */
5361 		status = nfserr_bad_seqid;
5362 		if (!cstate->minorversion &&
5363 		    lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5364 			goto out;
5365 	}
5366 
5367 	*lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5368 	if (*lst == NULL) {
5369 		status = nfserr_jukebox;
5370 		goto out;
5371 	}
5372 	status = nfs_ok;
5373 out:
5374 	nfs4_put_stateowner(&lo->lo_owner);
5375 	return status;
5376 }
5377 
5378 /*
5379  *  LOCK operation
5380  */
5381 __be32
5382 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5383 	   struct nfsd4_lock *lock)
5384 {
5385 	struct nfs4_openowner *open_sop = NULL;
5386 	struct nfs4_lockowner *lock_sop = NULL;
5387 	struct nfs4_ol_stateid *lock_stp = NULL;
5388 	struct nfs4_ol_stateid *open_stp = NULL;
5389 	struct nfs4_file *fp;
5390 	struct file *filp = NULL;
5391 	struct file_lock *file_lock = NULL;
5392 	struct file_lock *conflock = NULL;
5393 	__be32 status = 0;
5394 	int lkflg;
5395 	int err;
5396 	bool new = false;
5397 	struct net *net = SVC_NET(rqstp);
5398 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5399 
5400 	dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5401 		(long long) lock->lk_offset,
5402 		(long long) lock->lk_length);
5403 
5404 	if (check_lock_length(lock->lk_offset, lock->lk_length))
5405 		 return nfserr_inval;
5406 
5407 	if ((status = fh_verify(rqstp, &cstate->current_fh,
5408 				S_IFREG, NFSD_MAY_LOCK))) {
5409 		dprintk("NFSD: nfsd4_lock: permission denied!\n");
5410 		return status;
5411 	}
5412 
5413 	if (lock->lk_is_new) {
5414 		if (nfsd4_has_session(cstate))
5415 			/* See rfc 5661 18.10.3: given clientid is ignored: */
5416 			memcpy(&lock->lk_new_clientid,
5417 				&cstate->session->se_client->cl_clientid,
5418 				sizeof(clientid_t));
5419 
5420 		status = nfserr_stale_clientid;
5421 		if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5422 			goto out;
5423 
5424 		/* validate and update open stateid and open seqid */
5425 		status = nfs4_preprocess_confirmed_seqid_op(cstate,
5426 				        lock->lk_new_open_seqid,
5427 		                        &lock->lk_new_open_stateid,
5428 					&open_stp, nn);
5429 		if (status)
5430 			goto out;
5431 		open_sop = openowner(open_stp->st_stateowner);
5432 		status = nfserr_bad_stateid;
5433 		if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5434 						&lock->lk_new_clientid))
5435 			goto out;
5436 		status = lookup_or_create_lock_state(cstate, open_stp, lock,
5437 							&lock_stp, &new);
5438 	} else {
5439 		status = nfs4_preprocess_seqid_op(cstate,
5440 				       lock->lk_old_lock_seqid,
5441 				       &lock->lk_old_lock_stateid,
5442 				       NFS4_LOCK_STID, &lock_stp, nn);
5443 	}
5444 	if (status)
5445 		goto out;
5446 	lock_sop = lockowner(lock_stp->st_stateowner);
5447 
5448 	lkflg = setlkflg(lock->lk_type);
5449 	status = nfs4_check_openmode(lock_stp, lkflg);
5450 	if (status)
5451 		goto out;
5452 
5453 	status = nfserr_grace;
5454 	if (locks_in_grace(net) && !lock->lk_reclaim)
5455 		goto out;
5456 	status = nfserr_no_grace;
5457 	if (!locks_in_grace(net) && lock->lk_reclaim)
5458 		goto out;
5459 
5460 	file_lock = locks_alloc_lock();
5461 	if (!file_lock) {
5462 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5463 		status = nfserr_jukebox;
5464 		goto out;
5465 	}
5466 
5467 	fp = lock_stp->st_stid.sc_file;
5468 	switch (lock->lk_type) {
5469 		case NFS4_READ_LT:
5470 		case NFS4_READW_LT:
5471 			spin_lock(&fp->fi_lock);
5472 			filp = find_readable_file_locked(fp);
5473 			if (filp)
5474 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5475 			spin_unlock(&fp->fi_lock);
5476 			file_lock->fl_type = F_RDLCK;
5477 			break;
5478 		case NFS4_WRITE_LT:
5479 		case NFS4_WRITEW_LT:
5480 			spin_lock(&fp->fi_lock);
5481 			filp = find_writeable_file_locked(fp);
5482 			if (filp)
5483 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5484 			spin_unlock(&fp->fi_lock);
5485 			file_lock->fl_type = F_WRLCK;
5486 			break;
5487 		default:
5488 			status = nfserr_inval;
5489 		goto out;
5490 	}
5491 	if (!filp) {
5492 		status = nfserr_openmode;
5493 		goto out;
5494 	}
5495 
5496 	file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5497 	file_lock->fl_pid = current->tgid;
5498 	file_lock->fl_file = filp;
5499 	file_lock->fl_flags = FL_POSIX;
5500 	file_lock->fl_lmops = &nfsd_posix_mng_ops;
5501 	file_lock->fl_start = lock->lk_offset;
5502 	file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5503 	nfs4_transform_lock_offset(file_lock);
5504 
5505 	conflock = locks_alloc_lock();
5506 	if (!conflock) {
5507 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5508 		status = nfserr_jukebox;
5509 		goto out;
5510 	}
5511 
5512 	err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5513 	switch (-err) {
5514 	case 0: /* success! */
5515 		update_stateid(&lock_stp->st_stid.sc_stateid);
5516 		memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5517 				sizeof(stateid_t));
5518 		status = 0;
5519 		break;
5520 	case (EAGAIN):		/* conflock holds conflicting lock */
5521 		status = nfserr_denied;
5522 		dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5523 		nfs4_set_lock_denied(conflock, &lock->lk_denied);
5524 		break;
5525 	case (EDEADLK):
5526 		status = nfserr_deadlock;
5527 		break;
5528 	default:
5529 		dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5530 		status = nfserrno(err);
5531 		break;
5532 	}
5533 out:
5534 	if (filp)
5535 		fput(filp);
5536 	if (lock_stp) {
5537 		/* Bump seqid manually if the 4.0 replay owner is openowner */
5538 		if (cstate->replay_owner &&
5539 		    cstate->replay_owner != &lock_sop->lo_owner &&
5540 		    seqid_mutating_err(ntohl(status)))
5541 			lock_sop->lo_owner.so_seqid++;
5542 
5543 		/*
5544 		 * If this is a new, never-before-used stateid, and we are
5545 		 * returning an error, then just go ahead and release it.
5546 		 */
5547 		if (status && new)
5548 			release_lock_stateid(lock_stp);
5549 
5550 		nfs4_put_stid(&lock_stp->st_stid);
5551 	}
5552 	if (open_stp)
5553 		nfs4_put_stid(&open_stp->st_stid);
5554 	nfsd4_bump_seqid(cstate, status);
5555 	if (file_lock)
5556 		locks_free_lock(file_lock);
5557 	if (conflock)
5558 		locks_free_lock(conflock);
5559 	return status;
5560 }
5561 
5562 /*
5563  * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5564  * so we do a temporary open here just to get an open file to pass to
5565  * vfs_test_lock.  (Arguably perhaps test_lock should be done with an
5566  * inode operation.)
5567  */
5568 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5569 {
5570 	struct file *file;
5571 	__be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5572 	if (!err) {
5573 		err = nfserrno(vfs_test_lock(file, lock));
5574 		fput(file);
5575 	}
5576 	return err;
5577 }
5578 
5579 /*
5580  * LOCKT operation
5581  */
5582 __be32
5583 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5584 	    struct nfsd4_lockt *lockt)
5585 {
5586 	struct file_lock *file_lock = NULL;
5587 	struct nfs4_lockowner *lo = NULL;
5588 	__be32 status;
5589 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5590 
5591 	if (locks_in_grace(SVC_NET(rqstp)))
5592 		return nfserr_grace;
5593 
5594 	if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5595 		 return nfserr_inval;
5596 
5597 	if (!nfsd4_has_session(cstate)) {
5598 		status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5599 		if (status)
5600 			goto out;
5601 	}
5602 
5603 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5604 		goto out;
5605 
5606 	file_lock = locks_alloc_lock();
5607 	if (!file_lock) {
5608 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5609 		status = nfserr_jukebox;
5610 		goto out;
5611 	}
5612 
5613 	switch (lockt->lt_type) {
5614 		case NFS4_READ_LT:
5615 		case NFS4_READW_LT:
5616 			file_lock->fl_type = F_RDLCK;
5617 		break;
5618 		case NFS4_WRITE_LT:
5619 		case NFS4_WRITEW_LT:
5620 			file_lock->fl_type = F_WRLCK;
5621 		break;
5622 		default:
5623 			dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5624 			status = nfserr_inval;
5625 		goto out;
5626 	}
5627 
5628 	lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
5629 	if (lo)
5630 		file_lock->fl_owner = (fl_owner_t)lo;
5631 	file_lock->fl_pid = current->tgid;
5632 	file_lock->fl_flags = FL_POSIX;
5633 
5634 	file_lock->fl_start = lockt->lt_offset;
5635 	file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5636 
5637 	nfs4_transform_lock_offset(file_lock);
5638 
5639 	status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5640 	if (status)
5641 		goto out;
5642 
5643 	if (file_lock->fl_type != F_UNLCK) {
5644 		status = nfserr_denied;
5645 		nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5646 	}
5647 out:
5648 	if (lo)
5649 		nfs4_put_stateowner(&lo->lo_owner);
5650 	if (file_lock)
5651 		locks_free_lock(file_lock);
5652 	return status;
5653 }
5654 
5655 __be32
5656 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5657 	    struct nfsd4_locku *locku)
5658 {
5659 	struct nfs4_ol_stateid *stp;
5660 	struct file *filp = NULL;
5661 	struct file_lock *file_lock = NULL;
5662 	__be32 status;
5663 	int err;
5664 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5665 
5666 	dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5667 		(long long) locku->lu_offset,
5668 		(long long) locku->lu_length);
5669 
5670 	if (check_lock_length(locku->lu_offset, locku->lu_length))
5671 		 return nfserr_inval;
5672 
5673 	status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5674 					&locku->lu_stateid, NFS4_LOCK_STID,
5675 					&stp, nn);
5676 	if (status)
5677 		goto out;
5678 	filp = find_any_file(stp->st_stid.sc_file);
5679 	if (!filp) {
5680 		status = nfserr_lock_range;
5681 		goto put_stateid;
5682 	}
5683 	file_lock = locks_alloc_lock();
5684 	if (!file_lock) {
5685 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5686 		status = nfserr_jukebox;
5687 		goto fput;
5688 	}
5689 
5690 	file_lock->fl_type = F_UNLCK;
5691 	file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5692 	file_lock->fl_pid = current->tgid;
5693 	file_lock->fl_file = filp;
5694 	file_lock->fl_flags = FL_POSIX;
5695 	file_lock->fl_lmops = &nfsd_posix_mng_ops;
5696 	file_lock->fl_start = locku->lu_offset;
5697 
5698 	file_lock->fl_end = last_byte_offset(locku->lu_offset,
5699 						locku->lu_length);
5700 	nfs4_transform_lock_offset(file_lock);
5701 
5702 	err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5703 	if (err) {
5704 		dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5705 		goto out_nfserr;
5706 	}
5707 	update_stateid(&stp->st_stid.sc_stateid);
5708 	memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5709 fput:
5710 	fput(filp);
5711 put_stateid:
5712 	nfs4_put_stid(&stp->st_stid);
5713 out:
5714 	nfsd4_bump_seqid(cstate, status);
5715 	if (file_lock)
5716 		locks_free_lock(file_lock);
5717 	return status;
5718 
5719 out_nfserr:
5720 	status = nfserrno(err);
5721 	goto fput;
5722 }
5723 
5724 /*
5725  * returns
5726  * 	true:  locks held by lockowner
5727  * 	false: no locks held by lockowner
5728  */
5729 static bool
5730 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5731 {
5732 	struct file_lock *fl;
5733 	int status = false;
5734 	struct file *filp = find_any_file(fp);
5735 	struct inode *inode;
5736 	struct file_lock_context *flctx;
5737 
5738 	if (!filp) {
5739 		/* Any valid lock stateid should have some sort of access */
5740 		WARN_ON_ONCE(1);
5741 		return status;
5742 	}
5743 
5744 	inode = file_inode(filp);
5745 	flctx = inode->i_flctx;
5746 
5747 	if (flctx && !list_empty_careful(&flctx->flc_posix)) {
5748 		spin_lock(&flctx->flc_lock);
5749 		list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5750 			if (fl->fl_owner == (fl_owner_t)lowner) {
5751 				status = true;
5752 				break;
5753 			}
5754 		}
5755 		spin_unlock(&flctx->flc_lock);
5756 	}
5757 	fput(filp);
5758 	return status;
5759 }
5760 
5761 __be32
5762 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5763 			struct nfsd4_compound_state *cstate,
5764 			struct nfsd4_release_lockowner *rlockowner)
5765 {
5766 	clientid_t *clid = &rlockowner->rl_clientid;
5767 	struct nfs4_stateowner *sop;
5768 	struct nfs4_lockowner *lo = NULL;
5769 	struct nfs4_ol_stateid *stp;
5770 	struct xdr_netobj *owner = &rlockowner->rl_owner;
5771 	unsigned int hashval = ownerstr_hashval(owner);
5772 	__be32 status;
5773 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5774 	struct nfs4_client *clp;
5775 
5776 	dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5777 		clid->cl_boot, clid->cl_id);
5778 
5779 	status = lookup_clientid(clid, cstate, nn);
5780 	if (status)
5781 		return status;
5782 
5783 	clp = cstate->clp;
5784 	/* Find the matching lock stateowner */
5785 	spin_lock(&clp->cl_lock);
5786 	list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5787 			    so_strhash) {
5788 
5789 		if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5790 			continue;
5791 
5792 		/* see if there are still any locks associated with it */
5793 		lo = lockowner(sop);
5794 		list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5795 			if (check_for_locks(stp->st_stid.sc_file, lo)) {
5796 				status = nfserr_locks_held;
5797 				spin_unlock(&clp->cl_lock);
5798 				return status;
5799 			}
5800 		}
5801 
5802 		nfs4_get_stateowner(sop);
5803 		break;
5804 	}
5805 	spin_unlock(&clp->cl_lock);
5806 	if (lo)
5807 		release_lockowner(lo);
5808 	return status;
5809 }
5810 
5811 static inline struct nfs4_client_reclaim *
5812 alloc_reclaim(void)
5813 {
5814 	return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5815 }
5816 
5817 bool
5818 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5819 {
5820 	struct nfs4_client_reclaim *crp;
5821 
5822 	crp = nfsd4_find_reclaim_client(name, nn);
5823 	return (crp && crp->cr_clp);
5824 }
5825 
5826 /*
5827  * failure => all reset bets are off, nfserr_no_grace...
5828  */
5829 struct nfs4_client_reclaim *
5830 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5831 {
5832 	unsigned int strhashval;
5833 	struct nfs4_client_reclaim *crp;
5834 
5835 	dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5836 	crp = alloc_reclaim();
5837 	if (crp) {
5838 		strhashval = clientstr_hashval(name);
5839 		INIT_LIST_HEAD(&crp->cr_strhash);
5840 		list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5841 		memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5842 		crp->cr_clp = NULL;
5843 		nn->reclaim_str_hashtbl_size++;
5844 	}
5845 	return crp;
5846 }
5847 
5848 void
5849 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5850 {
5851 	list_del(&crp->cr_strhash);
5852 	kfree(crp);
5853 	nn->reclaim_str_hashtbl_size--;
5854 }
5855 
5856 void
5857 nfs4_release_reclaim(struct nfsd_net *nn)
5858 {
5859 	struct nfs4_client_reclaim *crp = NULL;
5860 	int i;
5861 
5862 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5863 		while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5864 			crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5865 			                struct nfs4_client_reclaim, cr_strhash);
5866 			nfs4_remove_reclaim_record(crp, nn);
5867 		}
5868 	}
5869 	WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5870 }
5871 
5872 /*
5873  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5874 struct nfs4_client_reclaim *
5875 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5876 {
5877 	unsigned int strhashval;
5878 	struct nfs4_client_reclaim *crp = NULL;
5879 
5880 	dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5881 
5882 	strhashval = clientstr_hashval(recdir);
5883 	list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5884 		if (same_name(crp->cr_recdir, recdir)) {
5885 			return crp;
5886 		}
5887 	}
5888 	return NULL;
5889 }
5890 
5891 /*
5892 * Called from OPEN. Look for clientid in reclaim list.
5893 */
5894 __be32
5895 nfs4_check_open_reclaim(clientid_t *clid,
5896 		struct nfsd4_compound_state *cstate,
5897 		struct nfsd_net *nn)
5898 {
5899 	__be32 status;
5900 
5901 	/* find clientid in conf_id_hashtbl */
5902 	status = lookup_clientid(clid, cstate, nn);
5903 	if (status)
5904 		return nfserr_reclaim_bad;
5905 
5906 	if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5907 		return nfserr_no_grace;
5908 
5909 	if (nfsd4_client_record_check(cstate->clp))
5910 		return nfserr_reclaim_bad;
5911 
5912 	return nfs_ok;
5913 }
5914 
5915 #ifdef CONFIG_NFSD_FAULT_INJECTION
5916 static inline void
5917 put_client(struct nfs4_client *clp)
5918 {
5919 	atomic_dec(&clp->cl_refcount);
5920 }
5921 
5922 static struct nfs4_client *
5923 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5924 {
5925 	struct nfs4_client *clp;
5926 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5927 					  nfsd_net_id);
5928 
5929 	if (!nfsd_netns_ready(nn))
5930 		return NULL;
5931 
5932 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5933 		if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5934 			return clp;
5935 	}
5936 	return NULL;
5937 }
5938 
5939 u64
5940 nfsd_inject_print_clients(void)
5941 {
5942 	struct nfs4_client *clp;
5943 	u64 count = 0;
5944 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5945 					  nfsd_net_id);
5946 	char buf[INET6_ADDRSTRLEN];
5947 
5948 	if (!nfsd_netns_ready(nn))
5949 		return 0;
5950 
5951 	spin_lock(&nn->client_lock);
5952 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5953 		rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5954 		pr_info("NFS Client: %s\n", buf);
5955 		++count;
5956 	}
5957 	spin_unlock(&nn->client_lock);
5958 
5959 	return count;
5960 }
5961 
5962 u64
5963 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
5964 {
5965 	u64 count = 0;
5966 	struct nfs4_client *clp;
5967 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5968 					  nfsd_net_id);
5969 
5970 	if (!nfsd_netns_ready(nn))
5971 		return count;
5972 
5973 	spin_lock(&nn->client_lock);
5974 	clp = nfsd_find_client(addr, addr_size);
5975 	if (clp) {
5976 		if (mark_client_expired_locked(clp) == nfs_ok)
5977 			++count;
5978 		else
5979 			clp = NULL;
5980 	}
5981 	spin_unlock(&nn->client_lock);
5982 
5983 	if (clp)
5984 		expire_client(clp);
5985 
5986 	return count;
5987 }
5988 
5989 u64
5990 nfsd_inject_forget_clients(u64 max)
5991 {
5992 	u64 count = 0;
5993 	struct nfs4_client *clp, *next;
5994 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5995 						nfsd_net_id);
5996 	LIST_HEAD(reaplist);
5997 
5998 	if (!nfsd_netns_ready(nn))
5999 		return count;
6000 
6001 	spin_lock(&nn->client_lock);
6002 	list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6003 		if (mark_client_expired_locked(clp) == nfs_ok) {
6004 			list_add(&clp->cl_lru, &reaplist);
6005 			if (max != 0 && ++count >= max)
6006 				break;
6007 		}
6008 	}
6009 	spin_unlock(&nn->client_lock);
6010 
6011 	list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6012 		expire_client(clp);
6013 
6014 	return count;
6015 }
6016 
6017 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6018 			     const char *type)
6019 {
6020 	char buf[INET6_ADDRSTRLEN];
6021 	rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6022 	printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6023 }
6024 
6025 static void
6026 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6027 			     struct list_head *collect)
6028 {
6029 	struct nfs4_client *clp = lst->st_stid.sc_client;
6030 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6031 					  nfsd_net_id);
6032 
6033 	if (!collect)
6034 		return;
6035 
6036 	lockdep_assert_held(&nn->client_lock);
6037 	atomic_inc(&clp->cl_refcount);
6038 	list_add(&lst->st_locks, collect);
6039 }
6040 
6041 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6042 				    struct list_head *collect,
6043 				    bool (*func)(struct nfs4_ol_stateid *))
6044 {
6045 	struct nfs4_openowner *oop;
6046 	struct nfs4_ol_stateid *stp, *st_next;
6047 	struct nfs4_ol_stateid *lst, *lst_next;
6048 	u64 count = 0;
6049 
6050 	spin_lock(&clp->cl_lock);
6051 	list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6052 		list_for_each_entry_safe(stp, st_next,
6053 				&oop->oo_owner.so_stateids, st_perstateowner) {
6054 			list_for_each_entry_safe(lst, lst_next,
6055 					&stp->st_locks, st_locks) {
6056 				if (func) {
6057 					if (func(lst))
6058 						nfsd_inject_add_lock_to_list(lst,
6059 									collect);
6060 				}
6061 				++count;
6062 				/*
6063 				 * Despite the fact that these functions deal
6064 				 * with 64-bit integers for "count", we must
6065 				 * ensure that it doesn't blow up the
6066 				 * clp->cl_refcount. Throw a warning if we
6067 				 * start to approach INT_MAX here.
6068 				 */
6069 				WARN_ON_ONCE(count == (INT_MAX / 2));
6070 				if (count == max)
6071 					goto out;
6072 			}
6073 		}
6074 	}
6075 out:
6076 	spin_unlock(&clp->cl_lock);
6077 
6078 	return count;
6079 }
6080 
6081 static u64
6082 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6083 			  u64 max)
6084 {
6085 	return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6086 }
6087 
6088 static u64
6089 nfsd_print_client_locks(struct nfs4_client *clp)
6090 {
6091 	u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6092 	nfsd_print_count(clp, count, "locked files");
6093 	return count;
6094 }
6095 
6096 u64
6097 nfsd_inject_print_locks(void)
6098 {
6099 	struct nfs4_client *clp;
6100 	u64 count = 0;
6101 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6102 						nfsd_net_id);
6103 
6104 	if (!nfsd_netns_ready(nn))
6105 		return 0;
6106 
6107 	spin_lock(&nn->client_lock);
6108 	list_for_each_entry(clp, &nn->client_lru, cl_lru)
6109 		count += nfsd_print_client_locks(clp);
6110 	spin_unlock(&nn->client_lock);
6111 
6112 	return count;
6113 }
6114 
6115 static void
6116 nfsd_reap_locks(struct list_head *reaplist)
6117 {
6118 	struct nfs4_client *clp;
6119 	struct nfs4_ol_stateid *stp, *next;
6120 
6121 	list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6122 		list_del_init(&stp->st_locks);
6123 		clp = stp->st_stid.sc_client;
6124 		nfs4_put_stid(&stp->st_stid);
6125 		put_client(clp);
6126 	}
6127 }
6128 
6129 u64
6130 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6131 {
6132 	unsigned int count = 0;
6133 	struct nfs4_client *clp;
6134 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6135 						nfsd_net_id);
6136 	LIST_HEAD(reaplist);
6137 
6138 	if (!nfsd_netns_ready(nn))
6139 		return count;
6140 
6141 	spin_lock(&nn->client_lock);
6142 	clp = nfsd_find_client(addr, addr_size);
6143 	if (clp)
6144 		count = nfsd_collect_client_locks(clp, &reaplist, 0);
6145 	spin_unlock(&nn->client_lock);
6146 	nfsd_reap_locks(&reaplist);
6147 	return count;
6148 }
6149 
6150 u64
6151 nfsd_inject_forget_locks(u64 max)
6152 {
6153 	u64 count = 0;
6154 	struct nfs4_client *clp;
6155 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6156 						nfsd_net_id);
6157 	LIST_HEAD(reaplist);
6158 
6159 	if (!nfsd_netns_ready(nn))
6160 		return count;
6161 
6162 	spin_lock(&nn->client_lock);
6163 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6164 		count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6165 		if (max != 0 && count >= max)
6166 			break;
6167 	}
6168 	spin_unlock(&nn->client_lock);
6169 	nfsd_reap_locks(&reaplist);
6170 	return count;
6171 }
6172 
6173 static u64
6174 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6175 			      struct list_head *collect,
6176 			      void (*func)(struct nfs4_openowner *))
6177 {
6178 	struct nfs4_openowner *oop, *next;
6179 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6180 						nfsd_net_id);
6181 	u64 count = 0;
6182 
6183 	lockdep_assert_held(&nn->client_lock);
6184 
6185 	spin_lock(&clp->cl_lock);
6186 	list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6187 		if (func) {
6188 			func(oop);
6189 			if (collect) {
6190 				atomic_inc(&clp->cl_refcount);
6191 				list_add(&oop->oo_perclient, collect);
6192 			}
6193 		}
6194 		++count;
6195 		/*
6196 		 * Despite the fact that these functions deal with
6197 		 * 64-bit integers for "count", we must ensure that
6198 		 * it doesn't blow up the clp->cl_refcount. Throw a
6199 		 * warning if we start to approach INT_MAX here.
6200 		 */
6201 		WARN_ON_ONCE(count == (INT_MAX / 2));
6202 		if (count == max)
6203 			break;
6204 	}
6205 	spin_unlock(&clp->cl_lock);
6206 
6207 	return count;
6208 }
6209 
6210 static u64
6211 nfsd_print_client_openowners(struct nfs4_client *clp)
6212 {
6213 	u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6214 
6215 	nfsd_print_count(clp, count, "openowners");
6216 	return count;
6217 }
6218 
6219 static u64
6220 nfsd_collect_client_openowners(struct nfs4_client *clp,
6221 			       struct list_head *collect, u64 max)
6222 {
6223 	return nfsd_foreach_client_openowner(clp, max, collect,
6224 						unhash_openowner_locked);
6225 }
6226 
6227 u64
6228 nfsd_inject_print_openowners(void)
6229 {
6230 	struct nfs4_client *clp;
6231 	u64 count = 0;
6232 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6233 						nfsd_net_id);
6234 
6235 	if (!nfsd_netns_ready(nn))
6236 		return 0;
6237 
6238 	spin_lock(&nn->client_lock);
6239 	list_for_each_entry(clp, &nn->client_lru, cl_lru)
6240 		count += nfsd_print_client_openowners(clp);
6241 	spin_unlock(&nn->client_lock);
6242 
6243 	return count;
6244 }
6245 
6246 static void
6247 nfsd_reap_openowners(struct list_head *reaplist)
6248 {
6249 	struct nfs4_client *clp;
6250 	struct nfs4_openowner *oop, *next;
6251 
6252 	list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6253 		list_del_init(&oop->oo_perclient);
6254 		clp = oop->oo_owner.so_client;
6255 		release_openowner(oop);
6256 		put_client(clp);
6257 	}
6258 }
6259 
6260 u64
6261 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6262 				     size_t addr_size)
6263 {
6264 	unsigned int count = 0;
6265 	struct nfs4_client *clp;
6266 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6267 						nfsd_net_id);
6268 	LIST_HEAD(reaplist);
6269 
6270 	if (!nfsd_netns_ready(nn))
6271 		return count;
6272 
6273 	spin_lock(&nn->client_lock);
6274 	clp = nfsd_find_client(addr, addr_size);
6275 	if (clp)
6276 		count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6277 	spin_unlock(&nn->client_lock);
6278 	nfsd_reap_openowners(&reaplist);
6279 	return count;
6280 }
6281 
6282 u64
6283 nfsd_inject_forget_openowners(u64 max)
6284 {
6285 	u64 count = 0;
6286 	struct nfs4_client *clp;
6287 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6288 						nfsd_net_id);
6289 	LIST_HEAD(reaplist);
6290 
6291 	if (!nfsd_netns_ready(nn))
6292 		return count;
6293 
6294 	spin_lock(&nn->client_lock);
6295 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6296 		count += nfsd_collect_client_openowners(clp, &reaplist,
6297 							max - count);
6298 		if (max != 0 && count >= max)
6299 			break;
6300 	}
6301 	spin_unlock(&nn->client_lock);
6302 	nfsd_reap_openowners(&reaplist);
6303 	return count;
6304 }
6305 
6306 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6307 				     struct list_head *victims)
6308 {
6309 	struct nfs4_delegation *dp, *next;
6310 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6311 						nfsd_net_id);
6312 	u64 count = 0;
6313 
6314 	lockdep_assert_held(&nn->client_lock);
6315 
6316 	spin_lock(&state_lock);
6317 	list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6318 		if (victims) {
6319 			/*
6320 			 * It's not safe to mess with delegations that have a
6321 			 * non-zero dl_time. They might have already been broken
6322 			 * and could be processed by the laundromat outside of
6323 			 * the state_lock. Just leave them be.
6324 			 */
6325 			if (dp->dl_time != 0)
6326 				continue;
6327 
6328 			atomic_inc(&clp->cl_refcount);
6329 			WARN_ON(!unhash_delegation_locked(dp));
6330 			list_add(&dp->dl_recall_lru, victims);
6331 		}
6332 		++count;
6333 		/*
6334 		 * Despite the fact that these functions deal with
6335 		 * 64-bit integers for "count", we must ensure that
6336 		 * it doesn't blow up the clp->cl_refcount. Throw a
6337 		 * warning if we start to approach INT_MAX here.
6338 		 */
6339 		WARN_ON_ONCE(count == (INT_MAX / 2));
6340 		if (count == max)
6341 			break;
6342 	}
6343 	spin_unlock(&state_lock);
6344 	return count;
6345 }
6346 
6347 static u64
6348 nfsd_print_client_delegations(struct nfs4_client *clp)
6349 {
6350 	u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6351 
6352 	nfsd_print_count(clp, count, "delegations");
6353 	return count;
6354 }
6355 
6356 u64
6357 nfsd_inject_print_delegations(void)
6358 {
6359 	struct nfs4_client *clp;
6360 	u64 count = 0;
6361 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6362 						nfsd_net_id);
6363 
6364 	if (!nfsd_netns_ready(nn))
6365 		return 0;
6366 
6367 	spin_lock(&nn->client_lock);
6368 	list_for_each_entry(clp, &nn->client_lru, cl_lru)
6369 		count += nfsd_print_client_delegations(clp);
6370 	spin_unlock(&nn->client_lock);
6371 
6372 	return count;
6373 }
6374 
6375 static void
6376 nfsd_forget_delegations(struct list_head *reaplist)
6377 {
6378 	struct nfs4_client *clp;
6379 	struct nfs4_delegation *dp, *next;
6380 
6381 	list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6382 		list_del_init(&dp->dl_recall_lru);
6383 		clp = dp->dl_stid.sc_client;
6384 		revoke_delegation(dp);
6385 		put_client(clp);
6386 	}
6387 }
6388 
6389 u64
6390 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6391 				      size_t addr_size)
6392 {
6393 	u64 count = 0;
6394 	struct nfs4_client *clp;
6395 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6396 						nfsd_net_id);
6397 	LIST_HEAD(reaplist);
6398 
6399 	if (!nfsd_netns_ready(nn))
6400 		return count;
6401 
6402 	spin_lock(&nn->client_lock);
6403 	clp = nfsd_find_client(addr, addr_size);
6404 	if (clp)
6405 		count = nfsd_find_all_delegations(clp, 0, &reaplist);
6406 	spin_unlock(&nn->client_lock);
6407 
6408 	nfsd_forget_delegations(&reaplist);
6409 	return count;
6410 }
6411 
6412 u64
6413 nfsd_inject_forget_delegations(u64 max)
6414 {
6415 	u64 count = 0;
6416 	struct nfs4_client *clp;
6417 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6418 						nfsd_net_id);
6419 	LIST_HEAD(reaplist);
6420 
6421 	if (!nfsd_netns_ready(nn))
6422 		return count;
6423 
6424 	spin_lock(&nn->client_lock);
6425 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6426 		count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6427 		if (max != 0 && count >= max)
6428 			break;
6429 	}
6430 	spin_unlock(&nn->client_lock);
6431 	nfsd_forget_delegations(&reaplist);
6432 	return count;
6433 }
6434 
6435 static void
6436 nfsd_recall_delegations(struct list_head *reaplist)
6437 {
6438 	struct nfs4_client *clp;
6439 	struct nfs4_delegation *dp, *next;
6440 
6441 	list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6442 		list_del_init(&dp->dl_recall_lru);
6443 		clp = dp->dl_stid.sc_client;
6444 		/*
6445 		 * We skipped all entries that had a zero dl_time before,
6446 		 * so we can now reset the dl_time back to 0. If a delegation
6447 		 * break comes in now, then it won't make any difference since
6448 		 * we're recalling it either way.
6449 		 */
6450 		spin_lock(&state_lock);
6451 		dp->dl_time = 0;
6452 		spin_unlock(&state_lock);
6453 		nfsd_break_one_deleg(dp);
6454 		put_client(clp);
6455 	}
6456 }
6457 
6458 u64
6459 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6460 				      size_t addr_size)
6461 {
6462 	u64 count = 0;
6463 	struct nfs4_client *clp;
6464 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6465 						nfsd_net_id);
6466 	LIST_HEAD(reaplist);
6467 
6468 	if (!nfsd_netns_ready(nn))
6469 		return count;
6470 
6471 	spin_lock(&nn->client_lock);
6472 	clp = nfsd_find_client(addr, addr_size);
6473 	if (clp)
6474 		count = nfsd_find_all_delegations(clp, 0, &reaplist);
6475 	spin_unlock(&nn->client_lock);
6476 
6477 	nfsd_recall_delegations(&reaplist);
6478 	return count;
6479 }
6480 
6481 u64
6482 nfsd_inject_recall_delegations(u64 max)
6483 {
6484 	u64 count = 0;
6485 	struct nfs4_client *clp, *next;
6486 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6487 						nfsd_net_id);
6488 	LIST_HEAD(reaplist);
6489 
6490 	if (!nfsd_netns_ready(nn))
6491 		return count;
6492 
6493 	spin_lock(&nn->client_lock);
6494 	list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6495 		count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6496 		if (max != 0 && ++count >= max)
6497 			break;
6498 	}
6499 	spin_unlock(&nn->client_lock);
6500 	nfsd_recall_delegations(&reaplist);
6501 	return count;
6502 }
6503 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6504 
6505 /*
6506  * Since the lifetime of a delegation isn't limited to that of an open, a
6507  * client may quite reasonably hang on to a delegation as long as it has
6508  * the inode cached.  This becomes an obvious problem the first time a
6509  * client's inode cache approaches the size of the server's total memory.
6510  *
6511  * For now we avoid this problem by imposing a hard limit on the number
6512  * of delegations, which varies according to the server's memory size.
6513  */
6514 static void
6515 set_max_delegations(void)
6516 {
6517 	/*
6518 	 * Allow at most 4 delegations per megabyte of RAM.  Quick
6519 	 * estimates suggest that in the worst case (where every delegation
6520 	 * is for a different inode), a delegation could take about 1.5K,
6521 	 * giving a worst case usage of about 6% of memory.
6522 	 */
6523 	max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6524 }
6525 
6526 static int nfs4_state_create_net(struct net *net)
6527 {
6528 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6529 	int i;
6530 
6531 	nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6532 			CLIENT_HASH_SIZE, GFP_KERNEL);
6533 	if (!nn->conf_id_hashtbl)
6534 		goto err;
6535 	nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6536 			CLIENT_HASH_SIZE, GFP_KERNEL);
6537 	if (!nn->unconf_id_hashtbl)
6538 		goto err_unconf_id;
6539 	nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6540 			SESSION_HASH_SIZE, GFP_KERNEL);
6541 	if (!nn->sessionid_hashtbl)
6542 		goto err_sessionid;
6543 
6544 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6545 		INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6546 		INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6547 	}
6548 	for (i = 0; i < SESSION_HASH_SIZE; i++)
6549 		INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6550 	nn->conf_name_tree = RB_ROOT;
6551 	nn->unconf_name_tree = RB_ROOT;
6552 	INIT_LIST_HEAD(&nn->client_lru);
6553 	INIT_LIST_HEAD(&nn->close_lru);
6554 	INIT_LIST_HEAD(&nn->del_recall_lru);
6555 	spin_lock_init(&nn->client_lock);
6556 
6557 	INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6558 	get_net(net);
6559 
6560 	return 0;
6561 
6562 err_sessionid:
6563 	kfree(nn->unconf_id_hashtbl);
6564 err_unconf_id:
6565 	kfree(nn->conf_id_hashtbl);
6566 err:
6567 	return -ENOMEM;
6568 }
6569 
6570 static void
6571 nfs4_state_destroy_net(struct net *net)
6572 {
6573 	int i;
6574 	struct nfs4_client *clp = NULL;
6575 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6576 
6577 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6578 		while (!list_empty(&nn->conf_id_hashtbl[i])) {
6579 			clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6580 			destroy_client(clp);
6581 		}
6582 	}
6583 
6584 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6585 		while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6586 			clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6587 			destroy_client(clp);
6588 		}
6589 	}
6590 
6591 	kfree(nn->sessionid_hashtbl);
6592 	kfree(nn->unconf_id_hashtbl);
6593 	kfree(nn->conf_id_hashtbl);
6594 	put_net(net);
6595 }
6596 
6597 int
6598 nfs4_state_start_net(struct net *net)
6599 {
6600 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6601 	int ret;
6602 
6603 	ret = nfs4_state_create_net(net);
6604 	if (ret)
6605 		return ret;
6606 	nn->boot_time = get_seconds();
6607 	nn->grace_ended = false;
6608 	nn->nfsd4_manager.block_opens = true;
6609 	locks_start_grace(net, &nn->nfsd4_manager);
6610 	nfsd4_client_tracking_init(net);
6611 	printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6612 	       nn->nfsd4_grace, net);
6613 	queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6614 	return 0;
6615 }
6616 
6617 /* initialization to perform when the nfsd service is started: */
6618 
6619 int
6620 nfs4_state_start(void)
6621 {
6622 	int ret;
6623 
6624 	ret = set_callback_cred();
6625 	if (ret)
6626 		return -ENOMEM;
6627 	laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
6628 	if (laundry_wq == NULL) {
6629 		ret = -ENOMEM;
6630 		goto out_recovery;
6631 	}
6632 	ret = nfsd4_create_callback_queue();
6633 	if (ret)
6634 		goto out_free_laundry;
6635 
6636 	set_max_delegations();
6637 
6638 	return 0;
6639 
6640 out_free_laundry:
6641 	destroy_workqueue(laundry_wq);
6642 out_recovery:
6643 	return ret;
6644 }
6645 
6646 void
6647 nfs4_state_shutdown_net(struct net *net)
6648 {
6649 	struct nfs4_delegation *dp = NULL;
6650 	struct list_head *pos, *next, reaplist;
6651 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6652 
6653 	cancel_delayed_work_sync(&nn->laundromat_work);
6654 	locks_end_grace(&nn->nfsd4_manager);
6655 
6656 	INIT_LIST_HEAD(&reaplist);
6657 	spin_lock(&state_lock);
6658 	list_for_each_safe(pos, next, &nn->del_recall_lru) {
6659 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6660 		WARN_ON(!unhash_delegation_locked(dp));
6661 		list_add(&dp->dl_recall_lru, &reaplist);
6662 	}
6663 	spin_unlock(&state_lock);
6664 	list_for_each_safe(pos, next, &reaplist) {
6665 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6666 		list_del_init(&dp->dl_recall_lru);
6667 		put_clnt_odstate(dp->dl_clnt_odstate);
6668 		nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6669 		nfs4_put_stid(&dp->dl_stid);
6670 	}
6671 
6672 	nfsd4_client_tracking_exit(net);
6673 	nfs4_state_destroy_net(net);
6674 }
6675 
6676 void
6677 nfs4_state_shutdown(void)
6678 {
6679 	destroy_workqueue(laundry_wq);
6680 	nfsd4_destroy_callback_queue();
6681 }
6682 
6683 static void
6684 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6685 {
6686 	if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6687 		memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6688 }
6689 
6690 static void
6691 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6692 {
6693 	if (cstate->minorversion) {
6694 		memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6695 		SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6696 	}
6697 }
6698 
6699 void
6700 clear_current_stateid(struct nfsd4_compound_state *cstate)
6701 {
6702 	CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6703 }
6704 
6705 /*
6706  * functions to set current state id
6707  */
6708 void
6709 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6710 {
6711 	put_stateid(cstate, &odp->od_stateid);
6712 }
6713 
6714 void
6715 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6716 {
6717 	put_stateid(cstate, &open->op_stateid);
6718 }
6719 
6720 void
6721 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6722 {
6723 	put_stateid(cstate, &close->cl_stateid);
6724 }
6725 
6726 void
6727 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6728 {
6729 	put_stateid(cstate, &lock->lk_resp_stateid);
6730 }
6731 
6732 /*
6733  * functions to consume current state id
6734  */
6735 
6736 void
6737 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6738 {
6739 	get_stateid(cstate, &odp->od_stateid);
6740 }
6741 
6742 void
6743 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6744 {
6745 	get_stateid(cstate, &drp->dr_stateid);
6746 }
6747 
6748 void
6749 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6750 {
6751 	get_stateid(cstate, &fsp->fr_stateid);
6752 }
6753 
6754 void
6755 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6756 {
6757 	get_stateid(cstate, &setattr->sa_stateid);
6758 }
6759 
6760 void
6761 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6762 {
6763 	get_stateid(cstate, &close->cl_stateid);
6764 }
6765 
6766 void
6767 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6768 {
6769 	get_stateid(cstate, &locku->lu_stateid);
6770 }
6771 
6772 void
6773 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6774 {
6775 	get_stateid(cstate, &read->rd_stateid);
6776 }
6777 
6778 void
6779 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6780 {
6781 	get_stateid(cstate, &write->wr_stateid);
6782 }
6783