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