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