xref: /linux/fs/nfsd/nfs4state.c (revision 56ec2b646de6349c8c8c05c8df17b4d8998c467a)
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 <linux/fsnotify.h>
47 #include <linux/rhashtable.h>
48 #include <linux/nfs_ssc.h>
49 
50 #include "xdr4.h"
51 #include "xdr4cb.h"
52 #include "vfs.h"
53 #include "current_stateid.h"
54 
55 #include "netns.h"
56 #include "pnfs.h"
57 #include "filecache.h"
58 #include "trace.h"
59 
60 #define NFSDDBG_FACILITY                NFSDDBG_PROC
61 
62 #define all_ones {{ ~0, ~0}, ~0}
63 static const stateid_t one_stateid = {
64 	.si_generation = ~0,
65 	.si_opaque = all_ones,
66 };
67 static const stateid_t zero_stateid = {
68 	/* all fields zero */
69 };
70 static const stateid_t currentstateid = {
71 	.si_generation = 1,
72 };
73 static const stateid_t close_stateid = {
74 	.si_generation = 0xffffffffU,
75 };
76 
77 static u64 current_sessionid = 1;
78 
79 bool nfsd_delegts_enabled __read_mostly = true;
80 
81 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
82 #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
83 #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
84 #define CLOSE_STATEID(stateid)  (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
85 
86 /* forward declarations */
87 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
88 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
89 static void nfsd4_end_grace(struct nfsd_net *nn);
90 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
91 static void nfsd4_file_hash_remove(struct nfs4_file *fi);
92 static void deleg_reaper(struct nfsd_net *nn);
93 
94 /* Locking: */
95 
96 enum nfsd4_st_mutex_lock_subclass {
97 	OPEN_STATEID_MUTEX = 0,
98 	LOCK_STATEID_MUTEX = 1,
99 };
100 
101 /*
102  * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
103  * the refcount on the open stateid to drop.
104  */
105 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
106 
107 /*
108  * A waitqueue where a writer to clients/#/ctl destroying a client can
109  * wait for cl_rpc_users to drop to 0 and then for the client to be
110  * unhashed.
111  */
112 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
113 
114 static struct kmem_cache *client_slab;
115 static struct kmem_cache *openowner_slab;
116 static struct kmem_cache *lockowner_slab;
117 static struct kmem_cache *file_slab;
118 static struct kmem_cache *stateid_slab;
119 static struct kmem_cache *deleg_slab;
120 static struct kmem_cache *odstate_slab;
121 
122 static void free_session(struct nfsd4_session *);
123 
124 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
125 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
126 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops;
127 
128 static struct workqueue_struct *laundry_wq;
129 
nfsd4_create_laundry_wq(void)130 int nfsd4_create_laundry_wq(void)
131 {
132 	int rc = 0;
133 
134 	laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
135 	if (laundry_wq == NULL)
136 		rc = -ENOMEM;
137 	return rc;
138 }
139 
nfsd4_destroy_laundry_wq(void)140 void nfsd4_destroy_laundry_wq(void)
141 {
142 	destroy_workqueue(laundry_wq);
143 }
144 
is_session_dead(struct nfsd4_session * ses)145 static bool is_session_dead(struct nfsd4_session *ses)
146 {
147 	return ses->se_dead;
148 }
149 
mark_session_dead_locked(struct nfsd4_session * ses,int ref_held_by_me)150 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
151 {
152 	if (atomic_read(&ses->se_ref) > ref_held_by_me)
153 		return nfserr_jukebox;
154 	ses->se_dead = true;
155 	return nfs_ok;
156 }
157 
is_client_expired(struct nfs4_client * clp)158 static bool is_client_expired(struct nfs4_client *clp)
159 {
160 	return clp->cl_time == 0;
161 }
162 
nfsd4_dec_courtesy_client_count(struct nfsd_net * nn,struct nfs4_client * clp)163 static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn,
164 					struct nfs4_client *clp)
165 {
166 	if (clp->cl_state != NFSD4_ACTIVE)
167 		atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0);
168 }
169 
get_client_locked(struct nfs4_client * clp)170 static __be32 get_client_locked(struct nfs4_client *clp)
171 {
172 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
173 
174 	lockdep_assert_held(&nn->client_lock);
175 
176 	if (is_client_expired(clp))
177 		return nfserr_expired;
178 	atomic_inc(&clp->cl_rpc_users);
179 	nfsd4_dec_courtesy_client_count(nn, clp);
180 	clp->cl_state = NFSD4_ACTIVE;
181 	return nfs_ok;
182 }
183 
184 /* must be called under the client_lock */
185 static inline void
renew_client_locked(struct nfs4_client * clp)186 renew_client_locked(struct nfs4_client *clp)
187 {
188 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
189 
190 	if (is_client_expired(clp)) {
191 		WARN_ON(1);
192 		printk("%s: client (clientid %08x/%08x) already expired\n",
193 			__func__,
194 			clp->cl_clientid.cl_boot,
195 			clp->cl_clientid.cl_id);
196 		return;
197 	}
198 
199 	list_move_tail(&clp->cl_lru, &nn->client_lru);
200 	clp->cl_time = ktime_get_boottime_seconds();
201 	nfsd4_dec_courtesy_client_count(nn, clp);
202 	clp->cl_state = NFSD4_ACTIVE;
203 }
204 
put_client_renew_locked(struct nfs4_client * clp)205 static void put_client_renew_locked(struct nfs4_client *clp)
206 {
207 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
208 
209 	lockdep_assert_held(&nn->client_lock);
210 
211 	if (!atomic_dec_and_test(&clp->cl_rpc_users))
212 		return;
213 	if (!is_client_expired(clp))
214 		renew_client_locked(clp);
215 	else
216 		wake_up_all(&expiry_wq);
217 }
218 
put_client_renew(struct nfs4_client * clp)219 static void put_client_renew(struct nfs4_client *clp)
220 {
221 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
222 
223 	if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
224 		return;
225 	if (!is_client_expired(clp))
226 		renew_client_locked(clp);
227 	else
228 		wake_up_all(&expiry_wq);
229 	spin_unlock(&nn->client_lock);
230 }
231 
nfsd4_get_session_locked(struct nfsd4_session * ses)232 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
233 {
234 	__be32 status;
235 
236 	if (is_session_dead(ses))
237 		return nfserr_badsession;
238 	status = get_client_locked(ses->se_client);
239 	if (status)
240 		return status;
241 	atomic_inc(&ses->se_ref);
242 	return nfs_ok;
243 }
244 
nfsd4_put_session_locked(struct nfsd4_session * ses)245 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
246 {
247 	struct nfs4_client *clp = ses->se_client;
248 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
249 
250 	lockdep_assert_held(&nn->client_lock);
251 
252 	if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
253 		free_session(ses);
254 	put_client_renew_locked(clp);
255 }
256 
nfsd4_put_session(struct nfsd4_session * ses)257 static void nfsd4_put_session(struct nfsd4_session *ses)
258 {
259 	struct nfs4_client *clp = ses->se_client;
260 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
261 
262 	spin_lock(&nn->client_lock);
263 	nfsd4_put_session_locked(ses);
264 	spin_unlock(&nn->client_lock);
265 }
266 
267 static struct nfsd4_blocked_lock *
find_blocked_lock(struct nfs4_lockowner * lo,struct knfsd_fh * fh,struct nfsd_net * nn)268 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
269 			struct nfsd_net *nn)
270 {
271 	struct nfsd4_blocked_lock *cur, *found = NULL;
272 
273 	spin_lock(&nn->blocked_locks_lock);
274 	list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
275 		if (fh_match(fh, &cur->nbl_fh)) {
276 			list_del_init(&cur->nbl_list);
277 			WARN_ON(list_empty(&cur->nbl_lru));
278 			list_del_init(&cur->nbl_lru);
279 			found = cur;
280 			break;
281 		}
282 	}
283 	spin_unlock(&nn->blocked_locks_lock);
284 	if (found)
285 		locks_delete_block(&found->nbl_lock);
286 	return found;
287 }
288 
289 static struct nfsd4_blocked_lock *
find_or_allocate_block(struct nfs4_lockowner * lo,struct knfsd_fh * fh,struct nfsd_net * nn)290 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
291 			struct nfsd_net *nn)
292 {
293 	struct nfsd4_blocked_lock *nbl;
294 
295 	nbl = find_blocked_lock(lo, fh, nn);
296 	if (!nbl) {
297 		nbl = kmalloc_obj(*nbl);
298 		if (nbl) {
299 			INIT_LIST_HEAD(&nbl->nbl_list);
300 			INIT_LIST_HEAD(&nbl->nbl_lru);
301 			fh_copy_shallow(&nbl->nbl_fh, fh);
302 			locks_init_lock(&nbl->nbl_lock);
303 			kref_init(&nbl->nbl_kref);
304 			nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
305 					&nfsd4_cb_notify_lock_ops,
306 					NFSPROC4_CLNT_CB_NOTIFY_LOCK);
307 		}
308 	}
309 	return nbl;
310 }
311 
312 static void
free_nbl(struct kref * kref)313 free_nbl(struct kref *kref)
314 {
315 	struct nfsd4_blocked_lock *nbl;
316 
317 	nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
318 	locks_release_private(&nbl->nbl_lock);
319 	kfree(nbl);
320 }
321 
322 static void
free_blocked_lock(struct nfsd4_blocked_lock * nbl)323 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
324 {
325 	locks_delete_block(&nbl->nbl_lock);
326 	kref_put(&nbl->nbl_kref, free_nbl);
327 }
328 
329 static void
remove_blocked_locks(struct nfs4_lockowner * lo)330 remove_blocked_locks(struct nfs4_lockowner *lo)
331 {
332 	struct nfs4_client *clp = lo->lo_owner.so_client;
333 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
334 	struct nfsd4_blocked_lock *nbl;
335 	LIST_HEAD(reaplist);
336 
337 	/* Dequeue all blocked locks */
338 	spin_lock(&nn->blocked_locks_lock);
339 	while (!list_empty(&lo->lo_blocked)) {
340 		nbl = list_first_entry(&lo->lo_blocked,
341 					struct nfsd4_blocked_lock,
342 					nbl_list);
343 		list_del_init(&nbl->nbl_list);
344 		WARN_ON(list_empty(&nbl->nbl_lru));
345 		list_move(&nbl->nbl_lru, &reaplist);
346 	}
347 	spin_unlock(&nn->blocked_locks_lock);
348 
349 	/* Now free them */
350 	while (!list_empty(&reaplist)) {
351 		nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
352 					nbl_lru);
353 		list_del_init(&nbl->nbl_lru);
354 		free_blocked_lock(nbl);
355 	}
356 }
357 
358 static void
nfsd4_cb_notify_lock_prepare(struct nfsd4_callback * cb)359 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
360 {
361 	struct nfsd4_blocked_lock	*nbl = container_of(cb,
362 						struct nfsd4_blocked_lock, nbl_cb);
363 	locks_delete_block(&nbl->nbl_lock);
364 }
365 
366 static int
nfsd4_cb_notify_lock_done(struct nfsd4_callback * cb,struct rpc_task * task)367 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
368 {
369 	trace_nfsd_cb_notify_lock_done(&zero_stateid, task);
370 
371 	/*
372 	 * Since this is just an optimization, we don't try very hard if it
373 	 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
374 	 * just quit trying on anything else.
375 	 */
376 	switch (task->tk_status) {
377 	case -NFS4ERR_DELAY:
378 		rpc_delay(task, 1 * HZ);
379 		return 0;
380 	default:
381 		return 1;
382 	}
383 }
384 
385 static void
nfsd4_cb_notify_lock_release(struct nfsd4_callback * cb)386 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
387 {
388 	struct nfsd4_blocked_lock	*nbl = container_of(cb,
389 						struct nfsd4_blocked_lock, nbl_cb);
390 
391 	free_blocked_lock(nbl);
392 }
393 
394 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
395 	.prepare	= nfsd4_cb_notify_lock_prepare,
396 	.done		= nfsd4_cb_notify_lock_done,
397 	.release	= nfsd4_cb_notify_lock_release,
398 	.opcode		= OP_CB_NOTIFY_LOCK,
399 };
400 
401 /*
402  * We store the NONE, READ, WRITE, and BOTH bits separately in the
403  * st_{access,deny}_bmap field of the stateid, in order to track not
404  * only what share bits are currently in force, but also what
405  * combinations of share bits previous opens have used.  This allows us
406  * to enforce the recommendation in
407  * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
408  * the server return an error if the client attempt to downgrade to a
409  * combination of share bits not explicable by closing some of its
410  * previous opens.
411  *
412  * This enforcement is arguably incomplete, since we don't keep
413  * track of access/deny bit combinations; so, e.g., we allow:
414  *
415  *	OPEN allow read, deny write
416  *	OPEN allow both, deny none
417  *	DOWNGRADE allow read, deny none
418  *
419  * which we should reject.
420  *
421  * But you could also argue that our current code is already overkill,
422  * since it only exists to return NFS4ERR_INVAL on incorrect client
423  * behavior.
424  */
425 static unsigned int
bmap_to_share_mode(unsigned long bmap)426 bmap_to_share_mode(unsigned long bmap)
427 {
428 	int i;
429 	unsigned int access = 0;
430 
431 	for (i = 1; i < 4; i++) {
432 		if (test_bit(i, &bmap))
433 			access |= i;
434 	}
435 	return access;
436 }
437 
438 /* set share access for a given stateid */
439 static inline void
set_access(u32 access,struct nfs4_ol_stateid * stp)440 set_access(u32 access, struct nfs4_ol_stateid *stp)
441 {
442 	unsigned char mask = 1 << access;
443 
444 	WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
445 	stp->st_access_bmap |= mask;
446 }
447 
448 /* clear share access for a given stateid */
449 static inline void
clear_access(u32 access,struct nfs4_ol_stateid * stp)450 clear_access(u32 access, struct nfs4_ol_stateid *stp)
451 {
452 	unsigned char mask = 1 << access;
453 
454 	WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
455 	stp->st_access_bmap &= ~mask;
456 }
457 
458 /* test whether a given stateid has access */
459 static inline bool
test_access(u32 access,struct nfs4_ol_stateid * stp)460 test_access(u32 access, struct nfs4_ol_stateid *stp)
461 {
462 	unsigned char mask = 1 << access;
463 
464 	return (bool)(stp->st_access_bmap & mask);
465 }
466 
467 /* set share deny for a given stateid */
468 static inline void
set_deny(u32 deny,struct nfs4_ol_stateid * stp)469 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
470 {
471 	unsigned char mask = 1 << deny;
472 
473 	WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
474 	stp->st_deny_bmap |= mask;
475 }
476 
477 /* clear share deny for a given stateid */
478 static inline void
clear_deny(u32 deny,struct nfs4_ol_stateid * stp)479 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
480 {
481 	unsigned char mask = 1 << deny;
482 
483 	WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
484 	stp->st_deny_bmap &= ~mask;
485 }
486 
487 /* test whether a given stateid is denying specific access */
488 static inline bool
test_deny(u32 deny,struct nfs4_ol_stateid * stp)489 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
490 {
491 	unsigned char mask = 1 << deny;
492 
493 	return (bool)(stp->st_deny_bmap & mask);
494 }
495 
nfs4_access_to_omode(u32 access)496 static int nfs4_access_to_omode(u32 access)
497 {
498 	switch (access & NFS4_SHARE_ACCESS_BOTH) {
499 	case NFS4_SHARE_ACCESS_READ:
500 		return O_RDONLY;
501 	case NFS4_SHARE_ACCESS_WRITE:
502 		return O_WRONLY;
503 	case NFS4_SHARE_ACCESS_BOTH:
504 		return O_RDWR;
505 	}
506 	WARN_ON_ONCE(1);
507 	return O_RDONLY;
508 }
509 
510 static inline int
access_permit_read(struct nfs4_ol_stateid * stp)511 access_permit_read(struct nfs4_ol_stateid *stp)
512 {
513 	return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
514 		test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
515 		test_access(NFS4_SHARE_ACCESS_WRITE, stp);
516 }
517 
518 static inline int
access_permit_write(struct nfs4_ol_stateid * stp)519 access_permit_write(struct nfs4_ol_stateid *stp)
520 {
521 	return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
522 		test_access(NFS4_SHARE_ACCESS_BOTH, stp);
523 }
524 
525 static inline struct nfs4_stateowner *
nfs4_get_stateowner(struct nfs4_stateowner * sop)526 nfs4_get_stateowner(struct nfs4_stateowner *sop)
527 {
528 	atomic_inc(&sop->so_count);
529 	return sop;
530 }
531 
532 static int
same_owner_str(struct nfs4_stateowner * sop,struct xdr_netobj * owner)533 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
534 {
535 	return (sop->so_owner.len == owner->len) &&
536 		0 == memcmp(sop->so_owner.data, owner->data, owner->len);
537 }
538 
539 static struct nfs4_openowner *
find_openstateowner_str(unsigned int hashval,struct nfsd4_open * open,struct nfs4_client * clp)540 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
541 			struct nfs4_client *clp)
542 {
543 	struct nfs4_stateowner *so;
544 
545 	lockdep_assert_held(&clp->cl_lock);
546 
547 	list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
548 			    so_strhash) {
549 		if (!so->so_is_open_owner)
550 			continue;
551 		if (same_owner_str(so, &open->op_owner))
552 			return openowner(nfs4_get_stateowner(so));
553 	}
554 	return NULL;
555 }
556 
557 static inline u32
opaque_hashval(const void * ptr,int nbytes)558 opaque_hashval(const void *ptr, int nbytes)
559 {
560 	unsigned char *cptr = (unsigned char *) ptr;
561 
562 	u32 x = 0;
563 	while (nbytes--) {
564 		x *= 37;
565 		x += *cptr++;
566 	}
567 	return x;
568 }
569 
570 void
put_nfs4_file(struct nfs4_file * fi)571 put_nfs4_file(struct nfs4_file *fi)
572 {
573 	if (refcount_dec_and_test(&fi->fi_ref)) {
574 		nfsd4_file_hash_remove(fi);
575 		WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
576 		WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
577 		kfree_rcu(fi, fi_rcu);
578 	}
579 }
580 
581 static struct nfsd_file *
find_writeable_file_locked(struct nfs4_file * f)582 find_writeable_file_locked(struct nfs4_file *f)
583 {
584 	struct nfsd_file *ret;
585 
586 	lockdep_assert_held(&f->fi_lock);
587 
588 	ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
589 	if (!ret)
590 		ret = nfsd_file_get(f->fi_fds[O_RDWR]);
591 	return ret;
592 }
593 
594 static struct nfsd_file *
find_writeable_file(struct nfs4_file * f)595 find_writeable_file(struct nfs4_file *f)
596 {
597 	struct nfsd_file *ret;
598 
599 	spin_lock(&f->fi_lock);
600 	ret = find_writeable_file_locked(f);
601 	spin_unlock(&f->fi_lock);
602 
603 	return ret;
604 }
605 
606 static struct nfsd_file *
find_readable_file_locked(struct nfs4_file * f)607 find_readable_file_locked(struct nfs4_file *f)
608 {
609 	struct nfsd_file *ret;
610 
611 	lockdep_assert_held(&f->fi_lock);
612 
613 	ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
614 	if (!ret)
615 		ret = nfsd_file_get(f->fi_fds[O_RDWR]);
616 	return ret;
617 }
618 
619 static struct nfsd_file *
find_readable_file(struct nfs4_file * f)620 find_readable_file(struct nfs4_file *f)
621 {
622 	struct nfsd_file *ret;
623 
624 	spin_lock(&f->fi_lock);
625 	ret = find_readable_file_locked(f);
626 	spin_unlock(&f->fi_lock);
627 
628 	return ret;
629 }
630 
631 struct nfsd_file *
find_any_file(struct nfs4_file * f)632 find_any_file(struct nfs4_file *f)
633 {
634 	struct nfsd_file *ret;
635 
636 	if (!f)
637 		return NULL;
638 	spin_lock(&f->fi_lock);
639 	ret = nfsd_file_get(f->fi_fds[O_RDWR]);
640 	if (!ret) {
641 		ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
642 		if (!ret)
643 			ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
644 	}
645 	spin_unlock(&f->fi_lock);
646 	return ret;
647 }
648 
find_any_file_locked(struct nfs4_file * f)649 static struct nfsd_file *find_any_file_locked(struct nfs4_file *f)
650 {
651 	lockdep_assert_held(&f->fi_lock);
652 
653 	if (f->fi_fds[O_RDWR])
654 		return f->fi_fds[O_RDWR];
655 	if (f->fi_fds[O_WRONLY])
656 		return f->fi_fds[O_WRONLY];
657 	if (f->fi_fds[O_RDONLY])
658 		return f->fi_fds[O_RDONLY];
659 	return NULL;
660 }
661 
662 static atomic_long_t num_delegations;
663 unsigned long max_delegations;
664 
665 /*
666  * Open owner state (share locks)
667  */
668 
669 /* hash tables for lock and open owners */
670 #define OWNER_HASH_BITS              8
671 #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
672 #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
673 
ownerstr_hashval(struct xdr_netobj * ownername)674 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
675 {
676 	unsigned int ret;
677 
678 	ret = opaque_hashval(ownername->data, ownername->len);
679 	return ret & OWNER_HASH_MASK;
680 }
681 
682 static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp;
683 
684 static const struct rhashtable_params nfs4_file_rhash_params = {
685 	.key_len		= sizeof_field(struct nfs4_file, fi_inode),
686 	.key_offset		= offsetof(struct nfs4_file, fi_inode),
687 	.head_offset		= offsetof(struct nfs4_file, fi_rlist),
688 
689 	/*
690 	 * Start with a single page hash table to reduce resizing churn
691 	 * on light workloads.
692 	 */
693 	.min_size		= 256,
694 	.automatic_shrinking	= true,
695 };
696 
697 /*
698  * Check if courtesy clients have conflicting access and resolve it if possible
699  *
700  * access:  is op_share_access if share_access is true.
701  *	    Check if access mode, op_share_access, would conflict with
702  *	    the current deny mode of the file 'fp'.
703  * access:  is op_share_deny if share_access is false.
704  *	    Check if the deny mode, op_share_deny, would conflict with
705  *	    current access of the file 'fp'.
706  * stp:     skip checking this entry.
707  * new_stp: normal open, not open upgrade.
708  *
709  * Function returns:
710  *	false - access/deny mode conflict with normal client.
711  *	true  - no conflict or conflict with courtesy client(s) is resolved.
712  */
713 static bool
nfs4_resolve_deny_conflicts_locked(struct nfs4_file * fp,bool new_stp,struct nfs4_ol_stateid * stp,u32 access,bool share_access)714 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
715 		struct nfs4_ol_stateid *stp, u32 access, bool share_access)
716 {
717 	struct nfs4_ol_stateid *st;
718 	bool resolvable = true;
719 	unsigned char bmap;
720 	struct nfsd_net *nn;
721 	struct nfs4_client *clp;
722 
723 	lockdep_assert_held(&fp->fi_lock);
724 	list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
725 		/* ignore lock stateid */
726 		if (st->st_openstp)
727 			continue;
728 		if (st == stp && new_stp)
729 			continue;
730 		/* check file access against deny mode or vice versa */
731 		bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
732 		if (!(access & bmap_to_share_mode(bmap)))
733 			continue;
734 		clp = st->st_stid.sc_client;
735 		if (try_to_expire_client(clp))
736 			continue;
737 		resolvable = false;
738 		break;
739 	}
740 	if (resolvable) {
741 		clp = stp->st_stid.sc_client;
742 		nn = net_generic(clp->net, nfsd_net_id);
743 		mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
744 	}
745 	return resolvable;
746 }
747 
748 static void
__nfs4_file_get_access(struct nfs4_file * fp,u32 access)749 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
750 {
751 	lockdep_assert_held(&fp->fi_lock);
752 
753 	if (access & NFS4_SHARE_ACCESS_WRITE)
754 		atomic_inc(&fp->fi_access[O_WRONLY]);
755 	if (access & NFS4_SHARE_ACCESS_READ)
756 		atomic_inc(&fp->fi_access[O_RDONLY]);
757 }
758 
759 static __be32
nfs4_file_get_access(struct nfs4_file * fp,u32 access)760 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
761 {
762 	lockdep_assert_held(&fp->fi_lock);
763 
764 	/* Does this access mode make sense? */
765 	if (access & ~NFS4_SHARE_ACCESS_BOTH)
766 		return nfserr_inval;
767 
768 	/* Does it conflict with a deny mode already set? */
769 	if ((access & fp->fi_share_deny) != 0)
770 		return nfserr_share_denied;
771 
772 	__nfs4_file_get_access(fp, access);
773 	return nfs_ok;
774 }
775 
nfs4_file_check_deny(struct nfs4_file * fp,u32 deny)776 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
777 {
778 	/* Common case is that there is no deny mode. */
779 	if (deny) {
780 		/* Does this deny mode make sense? */
781 		if (deny & ~NFS4_SHARE_DENY_BOTH)
782 			return nfserr_inval;
783 
784 		if ((deny & NFS4_SHARE_DENY_READ) &&
785 		    atomic_read(&fp->fi_access[O_RDONLY]))
786 			return nfserr_share_denied;
787 
788 		if ((deny & NFS4_SHARE_DENY_WRITE) &&
789 		    atomic_read(&fp->fi_access[O_WRONLY]))
790 			return nfserr_share_denied;
791 	}
792 	return nfs_ok;
793 }
794 
__nfs4_file_put_access(struct nfs4_file * fp,int oflag)795 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
796 {
797 	might_lock(&fp->fi_lock);
798 
799 	if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
800 		struct nfsd_file *f1 = NULL;
801 		struct nfsd_file *f2 = NULL;
802 
803 		swap(f1, fp->fi_fds[oflag]);
804 		if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
805 			swap(f2, fp->fi_fds[O_RDWR]);
806 		spin_unlock(&fp->fi_lock);
807 		if (f1)
808 			nfsd_file_put(f1);
809 		if (f2)
810 			nfsd_file_put(f2);
811 	}
812 }
813 
nfs4_file_put_access(struct nfs4_file * fp,u32 access)814 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
815 {
816 	WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
817 
818 	if (access & NFS4_SHARE_ACCESS_WRITE)
819 		__nfs4_file_put_access(fp, O_WRONLY);
820 	if (access & NFS4_SHARE_ACCESS_READ)
821 		__nfs4_file_put_access(fp, O_RDONLY);
822 }
823 
824 /*
825  * Allocate a new open/delegation state counter. This is needed for
826  * pNFS for proper return on close semantics.
827  *
828  * Note that we only allocate it for pNFS-enabled exports, otherwise
829  * all pointers to struct nfs4_clnt_odstate are always NULL.
830  */
831 static struct nfs4_clnt_odstate *
alloc_clnt_odstate(struct nfs4_client * clp)832 alloc_clnt_odstate(struct nfs4_client *clp)
833 {
834 	struct nfs4_clnt_odstate *co;
835 
836 	co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
837 	if (co) {
838 		co->co_client = clp;
839 		refcount_set(&co->co_odcount, 1);
840 	}
841 	return co;
842 }
843 
844 static void
hash_clnt_odstate_locked(struct nfs4_clnt_odstate * co)845 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
846 {
847 	struct nfs4_file *fp = co->co_file;
848 
849 	lockdep_assert_held(&fp->fi_lock);
850 	list_add(&co->co_perfile, &fp->fi_clnt_odstate);
851 }
852 
853 static inline void
get_clnt_odstate(struct nfs4_clnt_odstate * co)854 get_clnt_odstate(struct nfs4_clnt_odstate *co)
855 {
856 	if (co)
857 		refcount_inc(&co->co_odcount);
858 }
859 
860 static void
put_clnt_odstate(struct nfs4_clnt_odstate * co)861 put_clnt_odstate(struct nfs4_clnt_odstate *co)
862 {
863 	struct nfs4_file *fp;
864 
865 	if (!co)
866 		return;
867 
868 	fp = co->co_file;
869 	if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
870 		list_del(&co->co_perfile);
871 		spin_unlock(&fp->fi_lock);
872 
873 		nfsd4_return_all_file_layouts(co->co_client, fp);
874 		kmem_cache_free(odstate_slab, co);
875 	}
876 }
877 
878 static struct nfs4_clnt_odstate *
find_or_hash_clnt_odstate(struct nfs4_file * fp,struct nfs4_clnt_odstate * new)879 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
880 {
881 	struct nfs4_clnt_odstate *co;
882 	struct nfs4_client *cl;
883 
884 	if (!new)
885 		return NULL;
886 
887 	cl = new->co_client;
888 
889 	spin_lock(&fp->fi_lock);
890 	list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
891 		if (co->co_client == cl) {
892 			get_clnt_odstate(co);
893 			goto out;
894 		}
895 	}
896 	co = new;
897 	co->co_file = fp;
898 	hash_clnt_odstate_locked(new);
899 out:
900 	spin_unlock(&fp->fi_lock);
901 	return co;
902 }
903 
nfs4_alloc_stid(struct nfs4_client * cl,struct kmem_cache * slab,void (* sc_free)(struct nfs4_stid *))904 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
905 				  void (*sc_free)(struct nfs4_stid *))
906 {
907 	struct nfs4_stid *stid;
908 	int new_id;
909 
910 	stid = kmem_cache_zalloc(slab, GFP_KERNEL);
911 	if (!stid)
912 		return NULL;
913 
914 	idr_preload(GFP_KERNEL);
915 	spin_lock(&cl->cl_lock);
916 	/* Reserving 0 for start of file in nfsdfs "states" file: */
917 	new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
918 	spin_unlock(&cl->cl_lock);
919 	idr_preload_end();
920 	if (new_id < 0)
921 		goto out_free;
922 
923 	stid->sc_free = sc_free;
924 	stid->sc_client = cl;
925 	stid->sc_stateid.si_opaque.so_id = new_id;
926 	stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
927 	/* Will be incremented before return to client: */
928 	refcount_set(&stid->sc_count, 1);
929 	spin_lock_init(&stid->sc_lock);
930 	INIT_LIST_HEAD(&stid->sc_cp_list);
931 
932 	return stid;
933 out_free:
934 	kmem_cache_free(slab, stid);
935 	return NULL;
936 }
937 
938 /*
939  * Create a unique stateid_t to represent each COPY.
940  */
nfs4_init_cp_state(struct nfsd_net * nn,copy_stateid_t * stid,unsigned char cs_type)941 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
942 			      unsigned char cs_type)
943 {
944 	int new_id;
945 
946 	stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
947 	stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
948 
949 	idr_preload(GFP_KERNEL);
950 	spin_lock(&nn->s2s_cp_lock);
951 	new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
952 	stid->cs_stid.si_opaque.so_id = new_id;
953 	stid->cs_stid.si_generation = 1;
954 	spin_unlock(&nn->s2s_cp_lock);
955 	idr_preload_end();
956 	if (new_id < 0)
957 		return 0;
958 	stid->cs_type = cs_type;
959 	return 1;
960 }
961 
nfs4_init_copy_state(struct nfsd_net * nn,struct nfsd4_copy * copy)962 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
963 {
964 	return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
965 }
966 
nfs4_alloc_init_cpntf_state(struct nfsd_net * nn,struct nfs4_stid * p_stid)967 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
968 						     struct nfs4_stid *p_stid)
969 {
970 	struct nfs4_cpntf_state *cps;
971 
972 	cps = kzalloc_obj(struct nfs4_cpntf_state);
973 	if (!cps)
974 		return NULL;
975 	cps->cpntf_time = ktime_get_boottime_seconds();
976 	refcount_set(&cps->cp_stateid.cs_count, 1);
977 	if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
978 		goto out_free;
979 	spin_lock(&nn->s2s_cp_lock);
980 	list_add(&cps->cp_list, &p_stid->sc_cp_list);
981 	spin_unlock(&nn->s2s_cp_lock);
982 	return cps;
983 out_free:
984 	kfree(cps);
985 	return NULL;
986 }
987 
nfs4_free_copy_state(struct nfsd4_copy * copy)988 void nfs4_free_copy_state(struct nfsd4_copy *copy)
989 {
990 	struct nfsd_net *nn;
991 
992 	if (copy->cp_stateid.cs_type != NFS4_COPY_STID)
993 		return;
994 	nn = net_generic(copy->cp_clp->net, nfsd_net_id);
995 	spin_lock(&nn->s2s_cp_lock);
996 	idr_remove(&nn->s2s_cp_stateids,
997 		   copy->cp_stateid.cs_stid.si_opaque.so_id);
998 	spin_unlock(&nn->s2s_cp_lock);
999 }
1000 
nfs4_free_cpntf_statelist(struct net * net,struct nfs4_stid * stid)1001 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1002 {
1003 	struct nfs4_cpntf_state *cps;
1004 	struct nfsd_net *nn;
1005 
1006 	nn = net_generic(net, nfsd_net_id);
1007 	spin_lock(&nn->s2s_cp_lock);
1008 	while (!list_empty(&stid->sc_cp_list)) {
1009 		cps = list_first_entry(&stid->sc_cp_list,
1010 				       struct nfs4_cpntf_state, cp_list);
1011 		_free_cpntf_state_locked(nn, cps);
1012 	}
1013 	spin_unlock(&nn->s2s_cp_lock);
1014 }
1015 
nfs4_alloc_open_stateid(struct nfs4_client * clp)1016 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
1017 {
1018 	struct nfs4_stid *stid;
1019 
1020 	stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
1021 	if (!stid)
1022 		return NULL;
1023 
1024 	return openlockstateid(stid);
1025 }
1026 
1027 /*
1028  * As the sc_free callback of deleg, this may be called by nfs4_put_stid
1029  * in nfsd_break_one_deleg.
1030  * Considering nfsd_break_one_deleg is called with the flc->flc_lock held,
1031  * this function mustn't ever sleep.
1032  */
nfs4_free_deleg(struct nfs4_stid * stid)1033 static void nfs4_free_deleg(struct nfs4_stid *stid)
1034 {
1035 	struct nfs4_delegation *dp = delegstateid(stid);
1036 
1037 	WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
1038 	WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
1039 	WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
1040 	WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
1041 	kmem_cache_free(deleg_slab, stid);
1042 	atomic_long_dec(&num_delegations);
1043 }
1044 
1045 /*
1046  * When we recall a delegation, we should be careful not to hand it
1047  * out again straight away.
1048  * To ensure this we keep a pair of bloom filters ('new' and 'old')
1049  * in which the filehandles of recalled delegations are "stored".
1050  * If a filehandle appear in either filter, a delegation is blocked.
1051  * When a delegation is recalled, the filehandle is stored in the "new"
1052  * filter.
1053  * Every 30 seconds we swap the filters and clear the "new" one,
1054  * unless both are empty of course.  This results in delegations for a
1055  * given filehandle being blocked for between 30 and 60 seconds.
1056  *
1057  * Each filter is 256 bits.  We hash the filehandle to 32bit and use the
1058  * low 3 bytes as hash-table indices.
1059  *
1060  * 'blocked_delegations_lock', which is always taken in block_delegations(),
1061  * is used to manage concurrent access.  Testing does not need the lock
1062  * except when swapping the two filters.
1063  */
1064 static DEFINE_SPINLOCK(blocked_delegations_lock);
1065 static struct bloom_pair {
1066 	int	entries, old_entries;
1067 	time64_t swap_time;
1068 	int	new; /* index into 'set' */
1069 	DECLARE_BITMAP(set[2], 256);
1070 } blocked_delegations;
1071 
delegation_blocked(struct knfsd_fh * fh)1072 static int delegation_blocked(struct knfsd_fh *fh)
1073 {
1074 	u32 hash;
1075 	struct bloom_pair *bd = &blocked_delegations;
1076 
1077 	if (bd->entries == 0)
1078 		return 0;
1079 	if (ktime_get_seconds() - bd->swap_time > 30) {
1080 		spin_lock(&blocked_delegations_lock);
1081 		if (ktime_get_seconds() - bd->swap_time > 30) {
1082 			bd->entries -= bd->old_entries;
1083 			bd->old_entries = bd->entries;
1084 			bd->new = 1-bd->new;
1085 			memset(bd->set[bd->new], 0,
1086 			       sizeof(bd->set[0]));
1087 			bd->swap_time = ktime_get_seconds();
1088 		}
1089 		spin_unlock(&blocked_delegations_lock);
1090 	}
1091 	hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1092 	if (test_bit(hash&255, bd->set[0]) &&
1093 	    test_bit((hash>>8)&255, bd->set[0]) &&
1094 	    test_bit((hash>>16)&255, bd->set[0]))
1095 		return 1;
1096 
1097 	if (test_bit(hash&255, bd->set[1]) &&
1098 	    test_bit((hash>>8)&255, bd->set[1]) &&
1099 	    test_bit((hash>>16)&255, bd->set[1]))
1100 		return 1;
1101 
1102 	return 0;
1103 }
1104 
block_delegations(struct knfsd_fh * fh)1105 static void block_delegations(struct knfsd_fh *fh)
1106 {
1107 	u32 hash;
1108 	struct bloom_pair *bd = &blocked_delegations;
1109 
1110 	hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1111 
1112 	spin_lock(&blocked_delegations_lock);
1113 	__set_bit(hash&255, bd->set[bd->new]);
1114 	__set_bit((hash>>8)&255, bd->set[bd->new]);
1115 	__set_bit((hash>>16)&255, bd->set[bd->new]);
1116 	if (bd->entries == 0)
1117 		bd->swap_time = ktime_get_seconds();
1118 	bd->entries += 1;
1119 	spin_unlock(&blocked_delegations_lock);
1120 }
1121 
1122 static struct nfs4_delegation *
alloc_init_deleg(struct nfs4_client * clp,struct nfs4_file * fp,struct nfs4_clnt_odstate * odstate,u32 dl_type)1123 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1124 		 struct nfs4_clnt_odstate *odstate, u32 dl_type)
1125 {
1126 	struct nfs4_delegation *dp;
1127 	struct nfs4_stid *stid;
1128 	long n;
1129 
1130 	dprintk("NFSD alloc_init_deleg\n");
1131 	n = atomic_long_inc_return(&num_delegations);
1132 	if (n < 0 || n > max_delegations)
1133 		goto out_dec;
1134 	if (delegation_blocked(&fp->fi_fhandle))
1135 		goto out_dec;
1136 	stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
1137 	if (stid == NULL)
1138 		goto out_dec;
1139 	dp = delegstateid(stid);
1140 
1141 	/*
1142 	 * delegation seqid's are never incremented.  The 4.1 special
1143 	 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1144 	 * 0 anyway just for consistency and use 1:
1145 	 */
1146 	dp->dl_stid.sc_stateid.si_generation = 1;
1147 	INIT_LIST_HEAD(&dp->dl_perfile);
1148 	INIT_LIST_HEAD(&dp->dl_perclnt);
1149 	INIT_LIST_HEAD(&dp->dl_recall_lru);
1150 	dp->dl_clnt_odstate = odstate;
1151 	get_clnt_odstate(odstate);
1152 	dp->dl_type = dl_type;
1153 	dp->dl_retries = 1;
1154 	dp->dl_recalled = false;
1155 	nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1156 		      &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1157 	nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client,
1158 			&nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR);
1159 	dp->dl_cb_fattr.ncf_file_modified = false;
1160 	get_nfs4_file(fp);
1161 	dp->dl_stid.sc_file = fp;
1162 	return dp;
1163 out_dec:
1164 	atomic_long_dec(&num_delegations);
1165 	return NULL;
1166 }
1167 
1168 void
nfs4_put_stid(struct nfs4_stid * s)1169 nfs4_put_stid(struct nfs4_stid *s)
1170 {
1171 	struct nfs4_file *fp = s->sc_file;
1172 	struct nfs4_client *clp = s->sc_client;
1173 
1174 	might_lock(&clp->cl_lock);
1175 
1176 	if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1177 		wake_up_all(&close_wq);
1178 		return;
1179 	}
1180 	idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1181 	if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
1182 		atomic_dec(&s->sc_client->cl_admin_revoked);
1183 	nfs4_free_cpntf_statelist(clp->net, s);
1184 	spin_unlock(&clp->cl_lock);
1185 	s->sc_free(s);
1186 	if (fp)
1187 		put_nfs4_file(fp);
1188 }
1189 
1190 void
nfs4_inc_and_copy_stateid(stateid_t * dst,struct nfs4_stid * stid)1191 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1192 {
1193 	stateid_t *src = &stid->sc_stateid;
1194 
1195 	spin_lock(&stid->sc_lock);
1196 	if (unlikely(++src->si_generation == 0))
1197 		src->si_generation = 1;
1198 	memcpy(dst, src, sizeof(*dst));
1199 	spin_unlock(&stid->sc_lock);
1200 }
1201 
put_deleg_file(struct nfs4_file * fp)1202 static void put_deleg_file(struct nfs4_file *fp)
1203 {
1204 	struct nfsd_file *rnf = NULL;
1205 	struct nfsd_file *nf = NULL;
1206 
1207 	spin_lock(&fp->fi_lock);
1208 	if (--fp->fi_delegees == 0) {
1209 		swap(nf, fp->fi_deleg_file);
1210 		swap(rnf, fp->fi_rdeleg_file);
1211 	}
1212 	spin_unlock(&fp->fi_lock);
1213 
1214 	if (nf)
1215 		nfsd_file_put(nf);
1216 	if (rnf) {
1217 		nfsd_file_put(rnf);
1218 		nfs4_file_put_access(fp, NFS4_SHARE_ACCESS_READ);
1219 	}
1220 }
1221 
nfsd4_finalize_deleg_timestamps(struct nfs4_delegation * dp,struct file * f)1222 static void nfsd4_finalize_deleg_timestamps(struct nfs4_delegation *dp, struct file *f)
1223 {
1224 	/* don't do anything if FMODE_NOCMTIME isn't set */
1225 	if ((READ_ONCE(f->f_mode) & FMODE_NOCMTIME) == 0)
1226 		return;
1227 
1228 	spin_lock(&f->f_lock);
1229 	f->f_mode &= ~FMODE_NOCMTIME;
1230 	spin_unlock(&f->f_lock);
1231 
1232 	/* was it never written? */
1233 	if (!dp->dl_written)
1234 		return;
1235 
1236 	/* did it get a setattr for the timestamps at some point? */
1237 	if (dp->dl_setattr)
1238 		return;
1239 
1240 	/* Stamp everything to "now" */
1241 	nfsd_update_cmtime_attr(f, ATTR_ATIME);
1242 }
1243 
nfs4_unlock_deleg_lease(struct nfs4_delegation * dp)1244 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1245 {
1246 	struct nfs4_file *fp = dp->dl_stid.sc_file;
1247 	struct nfsd_file *nf = fp->fi_deleg_file;
1248 
1249 	WARN_ON_ONCE(!fp->fi_delegees);
1250 
1251 	nfsd4_finalize_deleg_timestamps(dp, nf->nf_file);
1252 	kernel_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1253 	put_deleg_file(fp);
1254 }
1255 
destroy_unhashed_deleg(struct nfs4_delegation * dp)1256 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1257 {
1258 	put_clnt_odstate(dp->dl_clnt_odstate);
1259 	nfs4_unlock_deleg_lease(dp);
1260 	nfs4_put_stid(&dp->dl_stid);
1261 }
1262 
1263 /**
1264  * nfs4_delegation_exists - Discover if this delegation already exists
1265  * @clp:     a pointer to the nfs4_client we're granting a delegation to
1266  * @fp:      a pointer to the nfs4_file we're granting a delegation on
1267  *
1268  * Return:
1269  *      On success: true iff an existing delegation is found
1270  */
1271 
1272 static bool
nfs4_delegation_exists(struct nfs4_client * clp,struct nfs4_file * fp)1273 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1274 {
1275 	struct nfs4_delegation *searchdp = NULL;
1276 	struct nfs4_client *searchclp = NULL;
1277 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1278 
1279 	lockdep_assert_held(&nn->deleg_lock);
1280 	lockdep_assert_held(&fp->fi_lock);
1281 
1282 	list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1283 		searchclp = searchdp->dl_stid.sc_client;
1284 		if (clp == searchclp) {
1285 			return true;
1286 		}
1287 	}
1288 	return false;
1289 }
1290 
1291 /**
1292  * hash_delegation_locked - Add a delegation to the appropriate lists
1293  * @dp:     a pointer to the nfs4_delegation we are adding.
1294  * @fp:     a pointer to the nfs4_file we're granting a delegation on
1295  *
1296  * Return:
1297  *      On success: NULL if the delegation was successfully hashed.
1298  *
1299  *      On error: -EAGAIN if one was previously granted to this
1300  *                 nfs4_client for this nfs4_file. Delegation is not hashed.
1301  *
1302  */
1303 
1304 static int
hash_delegation_locked(struct nfs4_delegation * dp,struct nfs4_file * fp)1305 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1306 {
1307 	struct nfs4_client *clp = dp->dl_stid.sc_client;
1308 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1309 
1310 	lockdep_assert_held(&nn->deleg_lock);
1311 	lockdep_assert_held(&fp->fi_lock);
1312 	lockdep_assert_held(&clp->cl_lock);
1313 
1314 	if (nfs4_delegation_exists(clp, fp))
1315 		return -EAGAIN;
1316 	refcount_inc(&dp->dl_stid.sc_count);
1317 	dp->dl_stid.sc_type = SC_TYPE_DELEG;
1318 	list_add(&dp->dl_perfile, &fp->fi_delegations);
1319 	list_add(&dp->dl_perclnt, &clp->cl_delegations);
1320 	return 0;
1321 }
1322 
delegation_hashed(struct nfs4_delegation * dp)1323 static bool delegation_hashed(struct nfs4_delegation *dp)
1324 {
1325 	return !(list_empty(&dp->dl_perfile));
1326 }
1327 
1328 static bool
unhash_delegation_locked(struct nfs4_delegation * dp,unsigned short statusmask)1329 unhash_delegation_locked(struct nfs4_delegation *dp, unsigned short statusmask)
1330 {
1331 	struct nfs4_file *fp = dp->dl_stid.sc_file;
1332 	struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
1333 					  nfsd_net_id);
1334 
1335 	lockdep_assert_held(&nn->deleg_lock);
1336 
1337 	if (!delegation_hashed(dp))
1338 		return false;
1339 
1340 	if (statusmask == SC_STATUS_REVOKED &&
1341 	    dp->dl_stid.sc_client->cl_minorversion == 0)
1342 		statusmask = SC_STATUS_CLOSED;
1343 	dp->dl_stid.sc_status |= statusmask;
1344 	if (statusmask & SC_STATUS_ADMIN_REVOKED)
1345 		atomic_inc(&dp->dl_stid.sc_client->cl_admin_revoked);
1346 
1347 	/* Ensure that deleg break won't try to requeue it */
1348 	++dp->dl_time;
1349 	spin_lock(&fp->fi_lock);
1350 	list_del_init(&dp->dl_perclnt);
1351 	list_del_init(&dp->dl_recall_lru);
1352 	list_del_init(&dp->dl_perfile);
1353 	spin_unlock(&fp->fi_lock);
1354 	return true;
1355 }
1356 
destroy_delegation(struct nfs4_delegation * dp)1357 static void destroy_delegation(struct nfs4_delegation *dp)
1358 {
1359 	bool unhashed;
1360 	struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
1361 					  nfsd_net_id);
1362 
1363 	spin_lock(&nn->deleg_lock);
1364 	unhashed = unhash_delegation_locked(dp, SC_STATUS_CLOSED);
1365 	spin_unlock(&nn->deleg_lock);
1366 	if (unhashed)
1367 		destroy_unhashed_deleg(dp);
1368 }
1369 
1370 /**
1371  * revoke_delegation - perform nfs4 delegation structure cleanup
1372  * @dp: pointer to the delegation
1373  *
1374  * This function assumes that it's called either from the administrative
1375  * interface (nfsd4_revoke_states()) that's revoking a specific delegation
1376  * stateid or it's called from a laundromat thread (nfsd4_landromat()) that
1377  * determined that this specific state has expired and needs to be revoked
1378  * (both mark state with the appropriate stid sc_status mode). It is also
1379  * assumed that a reference was taken on the @dp state.
1380  *
1381  * If this function finds that the @dp state is SC_STATUS_FREED it means
1382  * that a FREE_STATEID operation for this stateid has been processed and
1383  * we can proceed to removing it from recalled list. However, if @dp state
1384  * isn't marked SC_STATUS_FREED, it means we need place it on the cl_revoked
1385  * list and wait for the FREE_STATEID to arrive from the client. At the same
1386  * time, we need to mark it as SC_STATUS_FREEABLE to indicate to the
1387  * nfsd4_free_stateid() function that this stateid has already been added
1388  * to the cl_revoked list and that nfsd4_free_stateid() is now responsible
1389  * for removing it from the list. Inspection of where the delegation state
1390  * in the revocation process is protected by the clp->cl_lock.
1391  */
revoke_delegation(struct nfs4_delegation * dp)1392 static void revoke_delegation(struct nfs4_delegation *dp)
1393 {
1394 	struct nfs4_client *clp = dp->dl_stid.sc_client;
1395 
1396 	WARN_ON(!list_empty(&dp->dl_recall_lru));
1397 	WARN_ON_ONCE(dp->dl_stid.sc_client->cl_minorversion > 0 &&
1398 		     !(dp->dl_stid.sc_status &
1399 		     (SC_STATUS_REVOKED | SC_STATUS_ADMIN_REVOKED)));
1400 
1401 	trace_nfsd_stid_revoke(&dp->dl_stid);
1402 
1403 	spin_lock(&clp->cl_lock);
1404 	if (dp->dl_stid.sc_status & SC_STATUS_FREED) {
1405 		list_del_init(&dp->dl_recall_lru);
1406 		goto out;
1407 	}
1408 	list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1409 	dp->dl_stid.sc_status |= SC_STATUS_FREEABLE;
1410 out:
1411 	spin_unlock(&clp->cl_lock);
1412 	destroy_unhashed_deleg(dp);
1413 }
1414 
1415 /*
1416  * SETCLIENTID state
1417  */
1418 
clientid_hashval(u32 id)1419 static unsigned int clientid_hashval(u32 id)
1420 {
1421 	return id & CLIENT_HASH_MASK;
1422 }
1423 
clientstr_hashval(struct xdr_netobj name)1424 static unsigned int clientstr_hashval(struct xdr_netobj name)
1425 {
1426 	return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1427 }
1428 
1429 /*
1430  * A stateid that had a deny mode associated with it is being released
1431  * or downgraded. Recalculate the deny mode on the file.
1432  */
1433 static void
recalculate_deny_mode(struct nfs4_file * fp)1434 recalculate_deny_mode(struct nfs4_file *fp)
1435 {
1436 	struct nfs4_ol_stateid *stp;
1437 	u32 old_deny;
1438 
1439 	spin_lock(&fp->fi_lock);
1440 	old_deny = fp->fi_share_deny;
1441 	fp->fi_share_deny = 0;
1442 	list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1443 		fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1444 		if (fp->fi_share_deny == old_deny)
1445 			break;
1446 	}
1447 	spin_unlock(&fp->fi_lock);
1448 }
1449 
1450 static void
reset_union_bmap_deny(u32 deny,struct nfs4_ol_stateid * stp)1451 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1452 {
1453 	int i;
1454 	bool change = false;
1455 
1456 	for (i = 1; i < 4; i++) {
1457 		if ((i & deny) != i) {
1458 			change = true;
1459 			clear_deny(i, stp);
1460 		}
1461 	}
1462 
1463 	/* Recalculate per-file deny mode if there was a change */
1464 	if (change)
1465 		recalculate_deny_mode(stp->st_stid.sc_file);
1466 }
1467 
1468 /* release all access and file references for a given stateid */
1469 static void
release_all_access(struct nfs4_ol_stateid * stp)1470 release_all_access(struct nfs4_ol_stateid *stp)
1471 {
1472 	int i;
1473 	struct nfs4_file *fp = stp->st_stid.sc_file;
1474 
1475 	if (fp && stp->st_deny_bmap != 0)
1476 		recalculate_deny_mode(fp);
1477 
1478 	for (i = 1; i < 4; i++) {
1479 		if (test_access(i, stp))
1480 			nfs4_file_put_access(stp->st_stid.sc_file, i);
1481 		clear_access(i, stp);
1482 	}
1483 }
1484 
1485 /**
1486  * nfs4_replay_free_cache - release dynamically allocated replay buffer
1487  * @rp: replay cache to reset
1488  *
1489  * If @rp->rp_buf points to a kmalloc'd buffer, free it and reset
1490  * rp_buf to the inline rp_ibuf. Always zeroes rp_buflen.
1491  */
nfs4_replay_free_cache(struct nfs4_replay * rp)1492 void nfs4_replay_free_cache(struct nfs4_replay *rp)
1493 {
1494 	if (rp->rp_buf != rp->rp_ibuf)
1495 		kfree(rp->rp_buf);
1496 	rp->rp_buf = rp->rp_ibuf;
1497 	rp->rp_buflen = 0;
1498 }
1499 
nfs4_free_stateowner(struct nfs4_stateowner * sop)1500 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1501 {
1502 	nfs4_replay_free_cache(&sop->so_replay);
1503 	kfree(sop->so_owner.data);
1504 	sop->so_ops->so_free(sop);
1505 }
1506 
nfs4_put_stateowner(struct nfs4_stateowner * sop)1507 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1508 {
1509 	struct nfs4_client *clp = sop->so_client;
1510 
1511 	might_lock(&clp->cl_lock);
1512 
1513 	if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1514 		return;
1515 	sop->so_ops->so_unhash(sop);
1516 	spin_unlock(&clp->cl_lock);
1517 	nfs4_free_stateowner(sop);
1518 }
1519 
1520 static bool
nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid * stp)1521 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1522 {
1523 	return list_empty(&stp->st_perfile);
1524 }
1525 
unhash_ol_stateid(struct nfs4_ol_stateid * stp)1526 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1527 {
1528 	struct nfs4_file *fp = stp->st_stid.sc_file;
1529 
1530 	lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1531 
1532 	if (list_empty(&stp->st_perfile))
1533 		return false;
1534 
1535 	spin_lock(&fp->fi_lock);
1536 	list_del_init(&stp->st_perfile);
1537 	spin_unlock(&fp->fi_lock);
1538 	list_del(&stp->st_perstateowner);
1539 	return true;
1540 }
1541 
nfs4_free_ol_stateid(struct nfs4_stid * stid)1542 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1543 {
1544 	struct nfs4_ol_stateid *stp = openlockstateid(stid);
1545 
1546 	put_clnt_odstate(stp->st_clnt_odstate);
1547 	release_all_access(stp);
1548 	if (stp->st_stateowner)
1549 		nfs4_put_stateowner(stp->st_stateowner);
1550 	if (!list_empty(&stid->sc_cp_list))
1551 		nfs4_free_cpntf_statelist(stid->sc_client->net, stid);
1552 	kmem_cache_free(stateid_slab, stid);
1553 }
1554 
nfs4_free_lock_stateid(struct nfs4_stid * stid)1555 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1556 {
1557 	struct nfs4_ol_stateid *stp = openlockstateid(stid);
1558 	struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1559 	struct nfsd_file *nf;
1560 
1561 	nf = find_any_file(stp->st_stid.sc_file);
1562 	if (nf) {
1563 		get_file(nf->nf_file);
1564 		filp_close(nf->nf_file, (fl_owner_t)lo);
1565 		nfsd_file_put(nf);
1566 	}
1567 	nfs4_free_ol_stateid(stid);
1568 }
1569 
1570 /*
1571  * Put the persistent reference to an already unhashed generic stateid, while
1572  * holding the cl_lock. If it's the last reference, then put it onto the
1573  * reaplist for later destruction.
1574  */
put_ol_stateid_locked(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1575 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1576 				       struct list_head *reaplist)
1577 {
1578 	struct nfs4_stid *s = &stp->st_stid;
1579 	struct nfs4_client *clp = s->sc_client;
1580 
1581 	lockdep_assert_held(&clp->cl_lock);
1582 
1583 	WARN_ON_ONCE(!list_empty(&stp->st_locks));
1584 
1585 	if (!refcount_dec_and_test(&s->sc_count)) {
1586 		wake_up_all(&close_wq);
1587 		return;
1588 	}
1589 
1590 	idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1591 	if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
1592 		atomic_dec(&s->sc_client->cl_admin_revoked);
1593 	list_add(&stp->st_locks, reaplist);
1594 }
1595 
unhash_lock_stateid(struct nfs4_ol_stateid * stp)1596 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1597 {
1598 	lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1599 
1600 	if (!unhash_ol_stateid(stp))
1601 		return false;
1602 	list_del_init(&stp->st_locks);
1603 	stp->st_stid.sc_status |= SC_STATUS_CLOSED;
1604 	return true;
1605 }
1606 
release_lock_stateid(struct nfs4_ol_stateid * stp)1607 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1608 {
1609 	struct nfs4_client *clp = stp->st_stid.sc_client;
1610 	bool unhashed;
1611 
1612 	spin_lock(&clp->cl_lock);
1613 	unhashed = unhash_lock_stateid(stp);
1614 	spin_unlock(&clp->cl_lock);
1615 	if (unhashed)
1616 		nfs4_put_stid(&stp->st_stid);
1617 }
1618 
unhash_lockowner_locked(struct nfs4_lockowner * lo)1619 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1620 {
1621 	struct nfs4_client *clp = lo->lo_owner.so_client;
1622 
1623 	lockdep_assert_held(&clp->cl_lock);
1624 
1625 	list_del_init(&lo->lo_owner.so_strhash);
1626 }
1627 
1628 /*
1629  * Free a list of generic stateids that were collected earlier after being
1630  * fully unhashed.
1631  */
1632 static void
free_ol_stateid_reaplist(struct list_head * reaplist)1633 free_ol_stateid_reaplist(struct list_head *reaplist)
1634 {
1635 	struct nfs4_ol_stateid *stp;
1636 	struct nfs4_file *fp;
1637 
1638 	might_sleep();
1639 
1640 	while (!list_empty(reaplist)) {
1641 		stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1642 				       st_locks);
1643 		list_del(&stp->st_locks);
1644 		fp = stp->st_stid.sc_file;
1645 		stp->st_stid.sc_free(&stp->st_stid);
1646 		if (fp)
1647 			put_nfs4_file(fp);
1648 	}
1649 }
1650 
release_open_stateid_locks(struct nfs4_ol_stateid * open_stp,struct list_head * reaplist)1651 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1652 				       struct list_head *reaplist)
1653 {
1654 	struct nfs4_ol_stateid *stp;
1655 
1656 	lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1657 
1658 	while (!list_empty(&open_stp->st_locks)) {
1659 		stp = list_entry(open_stp->st_locks.next,
1660 				struct nfs4_ol_stateid, st_locks);
1661 		unhash_lock_stateid(stp);
1662 		put_ol_stateid_locked(stp, reaplist);
1663 	}
1664 }
1665 
unhash_open_stateid(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1666 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1667 				struct list_head *reaplist)
1668 {
1669 	lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1670 
1671 	if (!unhash_ol_stateid(stp))
1672 		return false;
1673 	release_open_stateid_locks(stp, reaplist);
1674 	return true;
1675 }
1676 
release_open_stateid(struct nfs4_ol_stateid * stp)1677 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1678 {
1679 	LIST_HEAD(reaplist);
1680 
1681 	spin_lock(&stp->st_stid.sc_client->cl_lock);
1682 	stp->st_stid.sc_status |= SC_STATUS_CLOSED;
1683 	if (unhash_open_stateid(stp, &reaplist))
1684 		put_ol_stateid_locked(stp, &reaplist);
1685 	spin_unlock(&stp->st_stid.sc_client->cl_lock);
1686 	free_ol_stateid_reaplist(&reaplist);
1687 }
1688 
nfs4_openowner_unhashed(struct nfs4_openowner * oo)1689 static bool nfs4_openowner_unhashed(struct nfs4_openowner *oo)
1690 {
1691 	lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1692 
1693 	return list_empty(&oo->oo_owner.so_strhash) &&
1694 		list_empty(&oo->oo_perclient);
1695 }
1696 
unhash_openowner_locked(struct nfs4_openowner * oo)1697 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1698 {
1699 	struct nfs4_client *clp = oo->oo_owner.so_client;
1700 
1701 	lockdep_assert_held(&clp->cl_lock);
1702 
1703 	list_del_init(&oo->oo_owner.so_strhash);
1704 	list_del_init(&oo->oo_perclient);
1705 }
1706 
release_last_closed_stateid(struct nfs4_openowner * oo)1707 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1708 {
1709 	struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1710 					  nfsd_net_id);
1711 	struct nfs4_ol_stateid *s;
1712 
1713 	spin_lock(&nn->client_lock);
1714 	s = oo->oo_last_closed_stid;
1715 	if (s) {
1716 		list_del_init(&oo->oo_close_lru);
1717 		oo->oo_last_closed_stid = NULL;
1718 	}
1719 	spin_unlock(&nn->client_lock);
1720 	if (s)
1721 		nfs4_put_stid(&s->st_stid);
1722 }
1723 
release_openowner(struct nfs4_openowner * oo)1724 static void release_openowner(struct nfs4_openowner *oo)
1725 {
1726 	struct nfs4_ol_stateid *stp;
1727 	struct nfs4_client *clp = oo->oo_owner.so_client;
1728 	LIST_HEAD(reaplist);
1729 
1730 	spin_lock(&clp->cl_lock);
1731 	unhash_openowner_locked(oo);
1732 	while (!list_empty(&oo->oo_owner.so_stateids)) {
1733 		stp = list_first_entry(&oo->oo_owner.so_stateids,
1734 				struct nfs4_ol_stateid, st_perstateowner);
1735 		if (unhash_open_stateid(stp, &reaplist))
1736 			put_ol_stateid_locked(stp, &reaplist);
1737 	}
1738 	spin_unlock(&clp->cl_lock);
1739 	free_ol_stateid_reaplist(&reaplist);
1740 	release_last_closed_stateid(oo);
1741 	nfs4_put_stateowner(&oo->oo_owner);
1742 }
1743 
find_one_sb_stid(struct nfs4_client * clp,struct super_block * sb,unsigned int sc_types)1744 static struct nfs4_stid *find_one_sb_stid(struct nfs4_client *clp,
1745 					  struct super_block *sb,
1746 					  unsigned int sc_types)
1747 {
1748 	unsigned long id, tmp;
1749 	struct nfs4_stid *stid;
1750 
1751 	spin_lock(&clp->cl_lock);
1752 	idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id)
1753 		if ((stid->sc_type & sc_types) &&
1754 		    stid->sc_status == 0 &&
1755 		    stid->sc_file->fi_inode->i_sb == sb) {
1756 			refcount_inc(&stid->sc_count);
1757 			break;
1758 		}
1759 	spin_unlock(&clp->cl_lock);
1760 	return stid;
1761 }
1762 
1763 /**
1764  * nfsd4_revoke_states - revoke all nfsv4 states associated with given filesystem
1765  * @nn:   used to identify instance of nfsd (there is one per net namespace)
1766  * @sb:   super_block used to identify target filesystem
1767  *
1768  * All nfs4 states (open, lock, delegation, layout) held by the server instance
1769  * and associated with a file on the given filesystem will be revoked resulting
1770  * in any files being closed and so all references from nfsd to the filesystem
1771  * being released.  Thus nfsd will no longer prevent the filesystem from being
1772  * unmounted.
1773  *
1774  * The clients which own the states will subsequently being notified that the
1775  * states have been "admin-revoked".
1776  */
nfsd4_revoke_states(struct nfsd_net * nn,struct super_block * sb)1777 void nfsd4_revoke_states(struct nfsd_net *nn, struct super_block *sb)
1778 {
1779 	unsigned int idhashval;
1780 	unsigned int sc_types;
1781 
1782 	sc_types = SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG | SC_TYPE_LAYOUT;
1783 
1784 	spin_lock(&nn->client_lock);
1785 	for (idhashval = 0; idhashval < CLIENT_HASH_SIZE; idhashval++) {
1786 		struct list_head *head = &nn->conf_id_hashtbl[idhashval];
1787 		struct nfs4_client *clp;
1788 	retry:
1789 		list_for_each_entry(clp, head, cl_idhash) {
1790 			struct nfs4_stid *stid = find_one_sb_stid(clp, sb,
1791 								  sc_types);
1792 			if (stid) {
1793 				struct nfs4_ol_stateid *stp;
1794 				struct nfs4_delegation *dp;
1795 				struct nfs4_layout_stateid *ls;
1796 
1797 				spin_unlock(&nn->client_lock);
1798 				switch (stid->sc_type) {
1799 				case SC_TYPE_OPEN:
1800 					stp = openlockstateid(stid);
1801 					mutex_lock_nested(&stp->st_mutex,
1802 							  OPEN_STATEID_MUTEX);
1803 
1804 					spin_lock(&clp->cl_lock);
1805 					if (stid->sc_status == 0) {
1806 						stid->sc_status |=
1807 							SC_STATUS_ADMIN_REVOKED;
1808 						atomic_inc(&clp->cl_admin_revoked);
1809 						spin_unlock(&clp->cl_lock);
1810 						release_all_access(stp);
1811 					} else
1812 						spin_unlock(&clp->cl_lock);
1813 					mutex_unlock(&stp->st_mutex);
1814 					break;
1815 				case SC_TYPE_LOCK:
1816 					stp = openlockstateid(stid);
1817 					mutex_lock_nested(&stp->st_mutex,
1818 							  LOCK_STATEID_MUTEX);
1819 					spin_lock(&clp->cl_lock);
1820 					if (stid->sc_status == 0) {
1821 						struct nfs4_lockowner *lo =
1822 							lockowner(stp->st_stateowner);
1823 						struct nfsd_file *nf;
1824 
1825 						stid->sc_status |=
1826 							SC_STATUS_ADMIN_REVOKED;
1827 						atomic_inc(&clp->cl_admin_revoked);
1828 						spin_unlock(&clp->cl_lock);
1829 						nf = find_any_file(stp->st_stid.sc_file);
1830 						if (nf) {
1831 							get_file(nf->nf_file);
1832 							filp_close(nf->nf_file,
1833 								   (fl_owner_t)lo);
1834 							nfsd_file_put(nf);
1835 						}
1836 						release_all_access(stp);
1837 					} else
1838 						spin_unlock(&clp->cl_lock);
1839 					mutex_unlock(&stp->st_mutex);
1840 					break;
1841 				case SC_TYPE_DELEG:
1842 					refcount_inc(&stid->sc_count);
1843 					dp = delegstateid(stid);
1844 					spin_lock(&nn->deleg_lock);
1845 					if (!unhash_delegation_locked(
1846 						    dp, SC_STATUS_ADMIN_REVOKED))
1847 						dp = NULL;
1848 					spin_unlock(&nn->deleg_lock);
1849 					if (dp)
1850 						revoke_delegation(dp);
1851 					break;
1852 				case SC_TYPE_LAYOUT:
1853 					ls = layoutstateid(stid);
1854 					spin_lock(&clp->cl_lock);
1855 					if (stid->sc_status == 0) {
1856 						stid->sc_status |=
1857 							SC_STATUS_ADMIN_REVOKED;
1858 						atomic_inc(&clp->cl_admin_revoked);
1859 					}
1860 					spin_unlock(&clp->cl_lock);
1861 					nfsd4_close_layout(ls);
1862 					break;
1863 				}
1864 				nfs4_put_stid(stid);
1865 				spin_lock(&nn->client_lock);
1866 				if (clp->cl_minorversion == 0)
1867 					/* Allow cleanup after a lease period.
1868 					 * store_release ensures cleanup will
1869 					 * see any newly revoked states if it
1870 					 * sees the time updated.
1871 					 */
1872 					nn->nfs40_last_revoke =
1873 						ktime_get_boottime_seconds();
1874 				goto retry;
1875 			}
1876 		}
1877 	}
1878 	spin_unlock(&nn->client_lock);
1879 }
1880 
1881 static inline int
hash_sessionid(struct nfs4_sessionid * sessionid)1882 hash_sessionid(struct nfs4_sessionid *sessionid)
1883 {
1884 	struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1885 
1886 	return sid->sequence % SESSION_HASH_SIZE;
1887 }
1888 
1889 #ifdef CONFIG_SUNRPC_DEBUG
1890 static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)1891 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1892 {
1893 	u32 *ptr = (u32 *)(&sessionid->data[0]);
1894 	dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1895 }
1896 #else
1897 static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)1898 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1899 {
1900 }
1901 #endif
1902 
1903 /*
1904  * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1905  * won't be used for replay.
1906  */
nfsd4_bump_seqid(struct nfsd4_compound_state * cstate,__be32 nfserr)1907 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1908 {
1909 	struct nfs4_stateowner *so = cstate->replay_owner;
1910 
1911 	if (nfserr == nfserr_replay_me)
1912 		return;
1913 
1914 	if (!seqid_mutating_err(ntohl(nfserr))) {
1915 		nfsd4_cstate_clear_replay(cstate);
1916 		return;
1917 	}
1918 	if (!so)
1919 		return;
1920 	if (so->so_is_open_owner)
1921 		release_last_closed_stateid(openowner(so));
1922 	so->so_seqid++;
1923 	return;
1924 }
1925 
1926 static void
gen_sessionid(struct nfsd4_session * ses)1927 gen_sessionid(struct nfsd4_session *ses)
1928 {
1929 	struct nfs4_client *clp = ses->se_client;
1930 	struct nfsd4_sessionid *sid;
1931 
1932 	sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1933 	sid->clientid = clp->cl_clientid;
1934 	sid->sequence = current_sessionid++;
1935 	sid->reserved = 0;
1936 }
1937 
1938 /*
1939  * The protocol defines ca_maxresponssize_cached to include the size of
1940  * the rpc header, but all we need to cache is the data starting after
1941  * the end of the initial SEQUENCE operation--the rest we regenerate
1942  * each time.  Therefore we can advertise a ca_maxresponssize_cached
1943  * value that is the number of bytes in our cache plus a few additional
1944  * bytes.  In order to stay on the safe side, and not promise more than
1945  * we can cache, those additional bytes must be the minimum possible: 24
1946  * bytes of rpc header (xid through accept state, with AUTH_NULL
1947  * verifier), 12 for the compound header (with zero-length tag), and 44
1948  * for the SEQUENCE op response:
1949  */
1950 #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
1951 
1952 static struct shrinker *nfsd_slot_shrinker;
1953 static DEFINE_SPINLOCK(nfsd_session_list_lock);
1954 static LIST_HEAD(nfsd_session_list);
1955 /* The sum of "target_slots-1" on every session.  The shrinker can push this
1956  * down, though it can take a little while for the memory to actually
1957  * be freed.  The "-1" is because we can never free slot 0 while the
1958  * session is active.
1959  */
1960 static atomic_t nfsd_total_target_slots = ATOMIC_INIT(0);
1961 
1962 static void
free_session_slots(struct nfsd4_session * ses,int from)1963 free_session_slots(struct nfsd4_session *ses, int from)
1964 {
1965 	int i;
1966 
1967 	if (from >= ses->se_fchannel.maxreqs)
1968 		return;
1969 
1970 	for (i = from; i < ses->se_fchannel.maxreqs; i++) {
1971 		struct nfsd4_slot *slot = xa_load(&ses->se_slots, i);
1972 
1973 		/*
1974 		 * Save the seqid in case we reactivate this slot.
1975 		 * This will never require a memory allocation so GFP
1976 		 * flag is irrelevant
1977 		 */
1978 		xa_store(&ses->se_slots, i, xa_mk_value(slot->sl_seqid), 0);
1979 		free_svc_cred(&slot->sl_cred);
1980 		kfree(slot);
1981 	}
1982 	ses->se_fchannel.maxreqs = from;
1983 	if (ses->se_target_maxslots > from) {
1984 		int new_target = from ?: 1;
1985 		atomic_sub(ses->se_target_maxslots - new_target, &nfsd_total_target_slots);
1986 		ses->se_target_maxslots = new_target;
1987 	}
1988 }
1989 
1990 /**
1991  * reduce_session_slots - reduce the target max-slots of a session if possible
1992  * @ses:  The session to affect
1993  * @dec:  how much to decrease the target by
1994  *
1995  * This interface can be used by a shrinker to reduce the target max-slots
1996  * for a session so that some slots can eventually be freed.
1997  * It uses spin_trylock() as it may be called in a context where another
1998  * spinlock is held that has a dependency on client_lock.  As shrinkers are
1999  * best-effort, skiping a session is client_lock is already held has no
2000  * great coast
2001  *
2002  * Return value:
2003  *   The number of slots that the target was reduced by.
2004  */
2005 static int
reduce_session_slots(struct nfsd4_session * ses,int dec)2006 reduce_session_slots(struct nfsd4_session *ses, int dec)
2007 {
2008 	struct nfsd_net *nn = net_generic(ses->se_client->net,
2009 					  nfsd_net_id);
2010 	int ret = 0;
2011 
2012 	if (ses->se_target_maxslots <= 1)
2013 		return ret;
2014 	if (!spin_trylock(&nn->client_lock))
2015 		return ret;
2016 	ret = min(dec, ses->se_target_maxslots-1);
2017 	ses->se_target_maxslots -= ret;
2018 	atomic_sub(ret, &nfsd_total_target_slots);
2019 	ses->se_slot_gen += 1;
2020 	if (ses->se_slot_gen == 0) {
2021 		int i;
2022 		ses->se_slot_gen = 1;
2023 		for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
2024 			struct nfsd4_slot *slot = xa_load(&ses->se_slots, i);
2025 			slot->sl_generation = 0;
2026 		}
2027 	}
2028 	spin_unlock(&nn->client_lock);
2029 	return ret;
2030 }
2031 
nfsd4_alloc_slot(struct nfsd4_channel_attrs * fattrs,int index,gfp_t gfp)2032 static struct nfsd4_slot *nfsd4_alloc_slot(struct nfsd4_channel_attrs *fattrs,
2033 					   int index, gfp_t gfp)
2034 {
2035 	struct nfsd4_slot *slot;
2036 	size_t size;
2037 
2038 	/*
2039 	 * The RPC and NFS session headers are never saved in
2040 	 * the slot reply cache buffer.
2041 	 */
2042 	size = fattrs->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ ?
2043 		0 : fattrs->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
2044 
2045 	slot = kzalloc_flex(*slot, sl_data, size, gfp);
2046 	if (!slot)
2047 		return NULL;
2048 	slot->sl_index = index;
2049 	return slot;
2050 }
2051 
alloc_session(struct nfsd4_channel_attrs * fattrs,struct nfsd4_channel_attrs * battrs)2052 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
2053 					   struct nfsd4_channel_attrs *battrs)
2054 {
2055 	int numslots = fattrs->maxreqs;
2056 	struct nfsd4_session *new;
2057 	struct nfsd4_slot *slot;
2058 	int i;
2059 
2060 	new = kzalloc_obj(*new);
2061 	if (!new)
2062 		return NULL;
2063 	xa_init(&new->se_slots);
2064 
2065 	slot = nfsd4_alloc_slot(fattrs, 0, GFP_KERNEL);
2066 	if (!slot || xa_is_err(xa_store(&new->se_slots, 0, slot, GFP_KERNEL)))
2067 		goto out_free;
2068 
2069 	for (i = 1; i < numslots; i++) {
2070 		const gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
2071 		slot = nfsd4_alloc_slot(fattrs, i, gfp);
2072 		if (!slot)
2073 			break;
2074 		if (xa_is_err(xa_store(&new->se_slots, i, slot, gfp))) {
2075 			kfree(slot);
2076 			break;
2077 		}
2078 	}
2079 	fattrs->maxreqs = i;
2080 	memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
2081 	new->se_target_maxslots = i;
2082 	atomic_add(i - 1, &nfsd_total_target_slots);
2083 	new->se_cb_slot_avail = ~0U;
2084 	new->se_cb_highest_slot = min(battrs->maxreqs - 1,
2085 				      NFSD_BC_SLOT_TABLE_SIZE - 1);
2086 	spin_lock_init(&new->se_lock);
2087 	return new;
2088 out_free:
2089 	kfree(slot);
2090 	xa_destroy(&new->se_slots);
2091 	kfree(new);
2092 	return NULL;
2093 }
2094 
free_conn(struct nfsd4_conn * c)2095 static void free_conn(struct nfsd4_conn *c)
2096 {
2097 	svc_xprt_put(c->cn_xprt);
2098 	kfree(c);
2099 }
2100 
nfsd4_conn_lost(struct svc_xpt_user * u)2101 static void nfsd4_conn_lost(struct svc_xpt_user *u)
2102 {
2103 	struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
2104 	struct nfs4_client *clp = c->cn_session->se_client;
2105 
2106 	trace_nfsd_cb_lost(clp);
2107 
2108 	spin_lock(&clp->cl_lock);
2109 	if (!list_empty(&c->cn_persession)) {
2110 		list_del(&c->cn_persession);
2111 		free_conn(c);
2112 	}
2113 	nfsd4_probe_callback(clp);
2114 	spin_unlock(&clp->cl_lock);
2115 }
2116 
alloc_conn(struct svc_rqst * rqstp,u32 flags)2117 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
2118 {
2119 	struct nfsd4_conn *conn;
2120 
2121 	conn = kmalloc_obj(struct nfsd4_conn);
2122 	if (!conn)
2123 		return NULL;
2124 	svc_xprt_get(rqstp->rq_xprt);
2125 	conn->cn_xprt = rqstp->rq_xprt;
2126 	conn->cn_flags = flags;
2127 	INIT_LIST_HEAD(&conn->cn_xpt_user.list);
2128 	return conn;
2129 }
2130 
__nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)2131 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
2132 {
2133 	conn->cn_session = ses;
2134 	list_add(&conn->cn_persession, &ses->se_conns);
2135 }
2136 
nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)2137 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
2138 {
2139 	struct nfs4_client *clp = ses->se_client;
2140 
2141 	spin_lock(&clp->cl_lock);
2142 	__nfsd4_hash_conn(conn, ses);
2143 	spin_unlock(&clp->cl_lock);
2144 }
2145 
nfsd4_register_conn(struct nfsd4_conn * conn)2146 static int nfsd4_register_conn(struct nfsd4_conn *conn)
2147 {
2148 	conn->cn_xpt_user.callback = nfsd4_conn_lost;
2149 	return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
2150 }
2151 
nfsd4_init_conn(struct svc_rqst * rqstp,struct nfsd4_conn * conn,struct nfsd4_session * ses)2152 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
2153 {
2154 	int ret;
2155 
2156 	nfsd4_hash_conn(conn, ses);
2157 	ret = nfsd4_register_conn(conn);
2158 	if (ret)
2159 		/* oops; xprt is already down: */
2160 		nfsd4_conn_lost(&conn->cn_xpt_user);
2161 	/* We may have gained or lost a callback channel: */
2162 	nfsd4_probe_callback_sync(ses->se_client);
2163 }
2164 
alloc_conn_from_crses(struct svc_rqst * rqstp,struct nfsd4_create_session * cses)2165 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
2166 {
2167 	u32 dir = NFS4_CDFC4_FORE;
2168 
2169 	if (cses->flags & SESSION4_BACK_CHAN)
2170 		dir |= NFS4_CDFC4_BACK;
2171 	return alloc_conn(rqstp, dir);
2172 }
2173 
2174 /* must be called under client_lock */
nfsd4_del_conns(struct nfsd4_session * s)2175 static void nfsd4_del_conns(struct nfsd4_session *s)
2176 {
2177 	struct nfs4_client *clp = s->se_client;
2178 	struct nfsd4_conn *c;
2179 
2180 	spin_lock(&clp->cl_lock);
2181 	while (!list_empty(&s->se_conns)) {
2182 		c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
2183 		list_del_init(&c->cn_persession);
2184 		spin_unlock(&clp->cl_lock);
2185 
2186 		unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
2187 		free_conn(c);
2188 
2189 		spin_lock(&clp->cl_lock);
2190 	}
2191 	spin_unlock(&clp->cl_lock);
2192 }
2193 
__free_session(struct nfsd4_session * ses)2194 static void __free_session(struct nfsd4_session *ses)
2195 {
2196 	free_session_slots(ses, 0);
2197 	xa_destroy(&ses->se_slots);
2198 	kfree(ses);
2199 }
2200 
free_session(struct nfsd4_session * ses)2201 static void free_session(struct nfsd4_session *ses)
2202 {
2203 	nfsd4_del_conns(ses);
2204 	__free_session(ses);
2205 }
2206 
2207 static unsigned long
nfsd_slot_count(struct shrinker * s,struct shrink_control * sc)2208 nfsd_slot_count(struct shrinker *s, struct shrink_control *sc)
2209 {
2210 	unsigned long cnt = atomic_read(&nfsd_total_target_slots);
2211 
2212 	return cnt ? cnt : SHRINK_EMPTY;
2213 }
2214 
2215 static unsigned long
nfsd_slot_scan(struct shrinker * s,struct shrink_control * sc)2216 nfsd_slot_scan(struct shrinker *s, struct shrink_control *sc)
2217 {
2218 	struct nfsd4_session *ses;
2219 	unsigned long scanned = 0;
2220 	unsigned long freed = 0;
2221 
2222 	spin_lock(&nfsd_session_list_lock);
2223 	list_for_each_entry(ses, &nfsd_session_list, se_all_sessions) {
2224 		freed += reduce_session_slots(ses, 1);
2225 		scanned += 1;
2226 		if (scanned >= sc->nr_to_scan) {
2227 			/* Move starting point for next scan */
2228 			list_move(&nfsd_session_list, &ses->se_all_sessions);
2229 			break;
2230 		}
2231 	}
2232 	spin_unlock(&nfsd_session_list_lock);
2233 	sc->nr_scanned = scanned;
2234 	return freed;
2235 }
2236 
init_session(struct svc_rqst * rqstp,struct nfsd4_session * new,struct nfs4_client * clp,struct nfsd4_create_session * cses)2237 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
2238 {
2239 	int idx;
2240 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2241 
2242 	new->se_client = clp;
2243 	gen_sessionid(new);
2244 
2245 	INIT_LIST_HEAD(&new->se_conns);
2246 
2247 	atomic_set(&new->se_ref, 0);
2248 	new->se_dead = false;
2249 	new->se_cb_prog = cses->callback_prog;
2250 	new->se_cb_sec = cses->cb_sec;
2251 
2252 	for (idx = 0; idx < NFSD_BC_SLOT_TABLE_SIZE; ++idx)
2253 		new->se_cb_seq_nr[idx] = 1;
2254 
2255 	idx = hash_sessionid(&new->se_sessionid);
2256 	list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
2257 	spin_lock(&clp->cl_lock);
2258 	list_add(&new->se_perclnt, &clp->cl_sessions);
2259 	spin_unlock(&clp->cl_lock);
2260 
2261 	spin_lock(&nfsd_session_list_lock);
2262 	list_add_tail(&new->se_all_sessions, &nfsd_session_list);
2263 	spin_unlock(&nfsd_session_list_lock);
2264 
2265 	{
2266 		struct sockaddr *sa = svc_addr(rqstp);
2267 		/*
2268 		 * This is a little silly; with sessions there's no real
2269 		 * use for the callback address.  Use the peer address
2270 		 * as a reasonable default for now, but consider fixing
2271 		 * the rpc client not to require an address in the
2272 		 * future:
2273 		 */
2274 		rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2275 		clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2276 	}
2277 }
2278 
2279 /* caller must hold client_lock */
2280 static struct nfsd4_session *
__find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net)2281 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2282 {
2283 	struct nfsd4_session *elem;
2284 	int idx;
2285 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2286 
2287 	lockdep_assert_held(&nn->client_lock);
2288 
2289 	dump_sessionid(__func__, sessionid);
2290 	idx = hash_sessionid(sessionid);
2291 	/* Search in the appropriate list */
2292 	list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2293 		if (!memcmp(elem->se_sessionid.data, sessionid->data,
2294 			    NFS4_MAX_SESSIONID_LEN)) {
2295 			return elem;
2296 		}
2297 	}
2298 
2299 	dprintk("%s: session not found\n", __func__);
2300 	return NULL;
2301 }
2302 
2303 static struct nfsd4_session *
find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net,__be32 * ret)2304 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2305 		__be32 *ret)
2306 {
2307 	struct nfsd4_session *session;
2308 	__be32 status = nfserr_badsession;
2309 
2310 	session = __find_in_sessionid_hashtbl(sessionid, net);
2311 	if (!session)
2312 		goto out;
2313 	status = nfsd4_get_session_locked(session);
2314 	if (status)
2315 		session = NULL;
2316 out:
2317 	*ret = status;
2318 	return session;
2319 }
2320 
2321 /* caller must hold client_lock */
2322 static void
unhash_session(struct nfsd4_session * ses)2323 unhash_session(struct nfsd4_session *ses)
2324 {
2325 	struct nfs4_client *clp = ses->se_client;
2326 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2327 
2328 	lockdep_assert_held(&nn->client_lock);
2329 
2330 	list_del(&ses->se_hash);
2331 	spin_lock(&ses->se_client->cl_lock);
2332 	list_del(&ses->se_perclnt);
2333 	spin_unlock(&ses->se_client->cl_lock);
2334 	spin_lock(&nfsd_session_list_lock);
2335 	list_del(&ses->se_all_sessions);
2336 	spin_unlock(&nfsd_session_list_lock);
2337 }
2338 
2339 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2340 static int
STALE_CLIENTID(clientid_t * clid,struct nfsd_net * nn)2341 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2342 {
2343 	/*
2344 	 * We're assuming the clid was not given out from a boot
2345 	 * precisely 2^32 (about 136 years) before this one.  That seems
2346 	 * a safe assumption:
2347 	 */
2348 	if (clid->cl_boot == (u32)nn->boot_time)
2349 		return 0;
2350 	trace_nfsd_clid_stale(clid);
2351 	return 1;
2352 }
2353 
alloc_client(struct xdr_netobj name,struct nfsd_net * nn)2354 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2355 				struct nfsd_net *nn)
2356 {
2357 	struct nfs4_client *clp;
2358 	int i;
2359 
2360 	if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients &&
2361 	    atomic_read(&nn->nfsd_courtesy_clients) > 0)
2362 		mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2363 
2364 	clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2365 	if (clp == NULL)
2366 		return NULL;
2367 	xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2368 	if (clp->cl_name.data == NULL)
2369 		goto err_no_name;
2370 	clp->cl_ownerstr_hashtbl = kmalloc_objs(struct list_head,
2371 						OWNER_HASH_SIZE);
2372 	if (!clp->cl_ownerstr_hashtbl)
2373 		goto err_no_hashtbl;
2374 	clp->cl_callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0);
2375 	if (!clp->cl_callback_wq)
2376 		goto err_no_callback_wq;
2377 
2378 	for (i = 0; i < OWNER_HASH_SIZE; i++)
2379 		INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2380 	INIT_LIST_HEAD(&clp->cl_sessions);
2381 	idr_init(&clp->cl_stateids);
2382 	atomic_set(&clp->cl_rpc_users, 0);
2383 	clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2384 	clp->cl_state = NFSD4_ACTIVE;
2385 	atomic_inc(&nn->nfs4_client_count);
2386 	atomic_set(&clp->cl_delegs_in_recall, 0);
2387 	INIT_LIST_HEAD(&clp->cl_idhash);
2388 	INIT_LIST_HEAD(&clp->cl_openowners);
2389 	INIT_LIST_HEAD(&clp->cl_delegations);
2390 	INIT_LIST_HEAD(&clp->cl_lru);
2391 	INIT_LIST_HEAD(&clp->cl_revoked);
2392 #ifdef CONFIG_NFSD_PNFS
2393 	INIT_LIST_HEAD(&clp->cl_lo_states);
2394 #endif
2395 #ifdef CONFIG_NFSD_SCSILAYOUT
2396 	xa_init(&clp->cl_dev_fences);
2397 	mutex_init(&clp->cl_fence_mutex);
2398 #endif
2399 	INIT_LIST_HEAD(&clp->async_copies);
2400 	spin_lock_init(&clp->async_lock);
2401 	spin_lock_init(&clp->cl_lock);
2402 	rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2403 	return clp;
2404 err_no_callback_wq:
2405 	kfree(clp->cl_ownerstr_hashtbl);
2406 err_no_hashtbl:
2407 	kfree(clp->cl_name.data);
2408 err_no_name:
2409 	kmem_cache_free(client_slab, clp);
2410 	return NULL;
2411 }
2412 
__free_client(struct kref * k)2413 static void __free_client(struct kref *k)
2414 {
2415 	struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2416 	struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2417 
2418 	free_svc_cred(&clp->cl_cred);
2419 	destroy_workqueue(clp->cl_callback_wq);
2420 	kfree(clp->cl_ownerstr_hashtbl);
2421 	kfree(clp->cl_name.data);
2422 	kfree(clp->cl_nii_domain.data);
2423 	kfree(clp->cl_nii_name.data);
2424 	idr_destroy(&clp->cl_stateids);
2425 	kfree(clp->cl_ra);
2426 	kmem_cache_free(client_slab, clp);
2427 }
2428 
2429 /**
2430  * nfsd4_put_client - release a reference on an nfs4_client
2431  * @clp: the client to be released
2432  *
2433  * When the last reference is released, the client is freed.
2434  */
nfsd4_put_client(struct nfs4_client * clp)2435 void nfsd4_put_client(struct nfs4_client *clp)
2436 {
2437 	kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2438 }
2439 
2440 static void
free_client(struct nfs4_client * clp)2441 free_client(struct nfs4_client *clp)
2442 {
2443 	while (!list_empty(&clp->cl_sessions)) {
2444 		struct nfsd4_session *ses;
2445 		ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2446 				se_perclnt);
2447 		list_del(&ses->se_perclnt);
2448 		WARN_ON_ONCE(atomic_read(&ses->se_ref));
2449 		free_session(ses);
2450 	}
2451 	rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2452 	if (clp->cl_nfsd_dentry) {
2453 		nfsd_client_rmdir(clp->cl_nfsd_dentry);
2454 		clp->cl_nfsd_dentry = NULL;
2455 		wake_up_all(&expiry_wq);
2456 	}
2457 	nfsd4_put_client(clp);
2458 }
2459 
2460 /* must be called under the client_lock */
2461 static void
unhash_client_locked(struct nfs4_client * clp)2462 unhash_client_locked(struct nfs4_client *clp)
2463 {
2464 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2465 	struct nfsd4_session *ses;
2466 
2467 	lockdep_assert_held(&nn->client_lock);
2468 
2469 	/* Mark the client as expired! */
2470 	clp->cl_time = 0;
2471 	/* Make it invisible */
2472 	if (!list_empty(&clp->cl_idhash)) {
2473 		list_del_init(&clp->cl_idhash);
2474 		if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2475 			rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2476 		else
2477 			rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2478 	}
2479 	list_del_init(&clp->cl_lru);
2480 	spin_lock(&clp->cl_lock);
2481 	spin_lock(&nfsd_session_list_lock);
2482 	list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) {
2483 		list_del_init(&ses->se_hash);
2484 		list_del_init(&ses->se_all_sessions);
2485 	}
2486 	spin_unlock(&nfsd_session_list_lock);
2487 	spin_unlock(&clp->cl_lock);
2488 }
2489 
2490 static void
unhash_client(struct nfs4_client * clp)2491 unhash_client(struct nfs4_client *clp)
2492 {
2493 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2494 
2495 	spin_lock(&nn->client_lock);
2496 	unhash_client_locked(clp);
2497 	spin_unlock(&nn->client_lock);
2498 }
2499 
mark_client_expired_locked(struct nfs4_client * clp)2500 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2501 {
2502 	int users = atomic_read(&clp->cl_rpc_users);
2503 
2504 	trace_nfsd_mark_client_expired(clp, users);
2505 
2506 	if (users)
2507 		return nfserr_jukebox;
2508 	unhash_client_locked(clp);
2509 	return nfs_ok;
2510 }
2511 
2512 static void
__destroy_client(struct nfs4_client * clp)2513 __destroy_client(struct nfs4_client *clp)
2514 {
2515 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2516 	int i;
2517 	struct nfs4_openowner *oo;
2518 	struct nfs4_delegation *dp;
2519 	LIST_HEAD(reaplist);
2520 
2521 	spin_lock(&nn->deleg_lock);
2522 	while (!list_empty(&clp->cl_delegations)) {
2523 		dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2524 		unhash_delegation_locked(dp, SC_STATUS_CLOSED);
2525 		list_add(&dp->dl_recall_lru, &reaplist);
2526 	}
2527 	spin_unlock(&nn->deleg_lock);
2528 	while (!list_empty(&reaplist)) {
2529 		dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2530 		list_del_init(&dp->dl_recall_lru);
2531 		destroy_unhashed_deleg(dp);
2532 	}
2533 	while (!list_empty(&clp->cl_revoked)) {
2534 		dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2535 		list_del_init(&dp->dl_recall_lru);
2536 		nfs4_put_stid(&dp->dl_stid);
2537 	}
2538 	while (!list_empty(&clp->cl_openowners)) {
2539 		oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2540 		nfs4_get_stateowner(&oo->oo_owner);
2541 		release_openowner(oo);
2542 	}
2543 	for (i = 0; i < OWNER_HASH_SIZE; i++) {
2544 		struct nfs4_stateowner *so, *tmp;
2545 
2546 		list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2547 					 so_strhash) {
2548 			/* Should be no openowners at this point */
2549 			WARN_ON_ONCE(so->so_is_open_owner);
2550 			remove_blocked_locks(lockowner(so));
2551 		}
2552 	}
2553 	nfsd4_return_all_client_layouts(clp);
2554 	nfsd4_shutdown_copy(clp);
2555 	nfsd4_shutdown_callback(clp);
2556 	if (clp->cl_cb_conn.cb_xprt)
2557 		svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2558 	atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2559 	nfsd4_dec_courtesy_client_count(nn, clp);
2560 #ifdef CONFIG_NFSD_SCSILAYOUT
2561 	xa_destroy(&clp->cl_dev_fences);
2562 #endif
2563 	free_client(clp);
2564 	wake_up_all(&expiry_wq);
2565 }
2566 
2567 static void
destroy_client(struct nfs4_client * clp)2568 destroy_client(struct nfs4_client *clp)
2569 {
2570 	unhash_client(clp);
2571 	__destroy_client(clp);
2572 }
2573 
inc_reclaim_complete(struct nfs4_client * clp)2574 static void inc_reclaim_complete(struct nfs4_client *clp)
2575 {
2576 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2577 
2578 	if (!nn->track_reclaim_completes)
2579 		return;
2580 	if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2581 		return;
2582 	if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2583 			nn->reclaim_str_hashtbl_size) {
2584 		printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2585 				clp->net->ns.inum);
2586 		nfsd4_end_grace(nn);
2587 	}
2588 }
2589 
expire_client(struct nfs4_client * clp)2590 static void expire_client(struct nfs4_client *clp)
2591 {
2592 	unhash_client(clp);
2593 	nfsd4_client_record_remove(clp);
2594 	__destroy_client(clp);
2595 }
2596 
copy_verf(struct nfs4_client * target,nfs4_verifier * source)2597 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2598 {
2599 	memcpy(target->cl_verifier.data, source->data,
2600 			sizeof(target->cl_verifier.data));
2601 }
2602 
copy_clid(struct nfs4_client * target,struct nfs4_client * source)2603 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2604 {
2605 	target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2606 	target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2607 }
2608 
copy_cred(struct svc_cred * target,struct svc_cred * source)2609 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2610 {
2611 	target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2612 	target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2613 								GFP_KERNEL);
2614 	target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2615 	if ((source->cr_principal && !target->cr_principal) ||
2616 	    (source->cr_raw_principal && !target->cr_raw_principal) ||
2617 	    (source->cr_targ_princ && !target->cr_targ_princ))
2618 		return -ENOMEM;
2619 
2620 	target->cr_flavor = source->cr_flavor;
2621 	target->cr_uid = source->cr_uid;
2622 	target->cr_gid = source->cr_gid;
2623 	target->cr_group_info = source->cr_group_info;
2624 	get_group_info(target->cr_group_info);
2625 	target->cr_gss_mech = source->cr_gss_mech;
2626 	if (source->cr_gss_mech)
2627 		gss_mech_get(source->cr_gss_mech);
2628 	return 0;
2629 }
2630 
2631 static int
compare_blob(const struct xdr_netobj * o1,const struct xdr_netobj * o2)2632 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2633 {
2634 	if (o1->len < o2->len)
2635 		return -1;
2636 	if (o1->len > o2->len)
2637 		return 1;
2638 	return memcmp(o1->data, o2->data, o1->len);
2639 }
2640 
2641 static int
same_verf(nfs4_verifier * v1,nfs4_verifier * v2)2642 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2643 {
2644 	return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2645 }
2646 
2647 static int
same_clid(clientid_t * cl1,clientid_t * cl2)2648 same_clid(clientid_t *cl1, clientid_t *cl2)
2649 {
2650 	return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2651 }
2652 
groups_equal(struct group_info * g1,struct group_info * g2)2653 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2654 {
2655 	int i;
2656 
2657 	if (g1->ngroups != g2->ngroups)
2658 		return false;
2659 	for (i=0; i<g1->ngroups; i++)
2660 		if (!gid_eq(g1->gid[i], g2->gid[i]))
2661 			return false;
2662 	return true;
2663 }
2664 
2665 /*
2666  * RFC 3530 language requires clid_inuse be returned when the
2667  * "principal" associated with a requests differs from that previously
2668  * used.  We use uid, gid's, and gss principal string as our best
2669  * approximation.  We also don't want to allow non-gss use of a client
2670  * established using gss: in theory cr_principal should catch that
2671  * change, but in practice cr_principal can be null even in the gss case
2672  * since gssd doesn't always pass down a principal string.
2673  */
is_gss_cred(struct svc_cred * cr)2674 static bool is_gss_cred(struct svc_cred *cr)
2675 {
2676 	/* Is cr_flavor one of the gss "pseudoflavors"?: */
2677 	return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2678 }
2679 
2680 
2681 static bool
same_creds(struct svc_cred * cr1,struct svc_cred * cr2)2682 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2683 {
2684 	if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2685 		|| (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2686 		|| (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2687 		|| !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2688 		return false;
2689 	/* XXX: check that cr_targ_princ fields match ? */
2690 	if (cr1->cr_principal == cr2->cr_principal)
2691 		return true;
2692 	if (!cr1->cr_principal || !cr2->cr_principal)
2693 		return false;
2694 	return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2695 }
2696 
svc_rqst_integrity_protected(struct svc_rqst * rqstp)2697 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2698 {
2699 	struct svc_cred *cr = &rqstp->rq_cred;
2700 	u32 service;
2701 
2702 	if (!cr->cr_gss_mech)
2703 		return false;
2704 	service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2705 	return service == RPC_GSS_SVC_INTEGRITY ||
2706 	       service == RPC_GSS_SVC_PRIVACY;
2707 }
2708 
nfsd4_mach_creds_match(struct nfs4_client * cl,struct svc_rqst * rqstp)2709 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2710 {
2711 	struct svc_cred *cr = &rqstp->rq_cred;
2712 
2713 	if (!cl->cl_mach_cred)
2714 		return true;
2715 	if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2716 		return false;
2717 	if (!svc_rqst_integrity_protected(rqstp))
2718 		return false;
2719 	if (cl->cl_cred.cr_raw_principal)
2720 		return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2721 						cr->cr_raw_principal);
2722 	if (!cr->cr_principal)
2723 		return false;
2724 	return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2725 }
2726 
gen_confirm(struct nfs4_client * clp,struct nfsd_net * nn)2727 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2728 {
2729 	__be32 verf[2];
2730 
2731 	/*
2732 	 * This is opaque to client, so no need to byte-swap. Use
2733 	 * __force to keep sparse happy
2734 	 */
2735 	verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2736 	verf[1] = (__force __be32)nn->clverifier_counter++;
2737 	memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2738 }
2739 
gen_clid(struct nfs4_client * clp,struct nfsd_net * nn)2740 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2741 {
2742 	clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2743 	clp->cl_clientid.cl_id = nn->clientid_counter++;
2744 	gen_confirm(clp, nn);
2745 }
2746 
2747 static struct nfs4_stid *
find_stateid_locked(struct nfs4_client * cl,stateid_t * t)2748 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2749 {
2750 	struct nfs4_stid *ret;
2751 
2752 	ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2753 	if (!ret || !ret->sc_type)
2754 		return NULL;
2755 	return ret;
2756 }
2757 
2758 static struct nfs4_stid *
find_stateid_by_type(struct nfs4_client * cl,stateid_t * t,unsigned short typemask,unsigned short ok_states)2759 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t,
2760 		     unsigned short typemask, unsigned short ok_states)
2761 {
2762 	struct nfs4_stid *s;
2763 
2764 	spin_lock(&cl->cl_lock);
2765 	s = find_stateid_locked(cl, t);
2766 	if (s != NULL) {
2767 		if ((s->sc_status & ~ok_states) == 0 &&
2768 		    (typemask & s->sc_type))
2769 			refcount_inc(&s->sc_count);
2770 		else
2771 			s = NULL;
2772 	}
2773 	spin_unlock(&cl->cl_lock);
2774 	return s;
2775 }
2776 
get_nfsdfs_clp(struct inode * inode)2777 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2778 {
2779 	struct nfsdfs_client *nc;
2780 	nc = get_nfsdfs_client(inode);
2781 	if (!nc)
2782 		return NULL;
2783 	return container_of(nc, struct nfs4_client, cl_nfsdfs);
2784 }
2785 
seq_quote_mem(struct seq_file * m,char * data,int len)2786 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2787 {
2788 	seq_puts(m, "\"");
2789 	seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2790 	seq_puts(m, "\"");
2791 }
2792 
cb_state2str(int state)2793 static const char *cb_state2str(int state)
2794 {
2795 	switch (state) {
2796 	case NFSD4_CB_UP:
2797 		return "UP";
2798 	case NFSD4_CB_UNKNOWN:
2799 		return "UNKNOWN";
2800 	case NFSD4_CB_DOWN:
2801 		return "DOWN";
2802 	case NFSD4_CB_FAULT:
2803 		return "FAULT";
2804 	}
2805 	return "UNDEFINED";
2806 }
2807 
client_info_show(struct seq_file * m,void * v)2808 static int client_info_show(struct seq_file *m, void *v)
2809 {
2810 	struct inode *inode = file_inode(m->file);
2811 	struct nfsd4_session *ses;
2812 	struct nfs4_client *clp;
2813 	u64 clid;
2814 
2815 	clp = get_nfsdfs_clp(inode);
2816 	if (!clp)
2817 		return -ENXIO;
2818 	memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2819 	seq_printf(m, "clientid: 0x%llx\n", clid);
2820 	seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2821 
2822 	if (clp->cl_state == NFSD4_COURTESY)
2823 		seq_puts(m, "status: courtesy\n");
2824 	else if (clp->cl_state == NFSD4_EXPIRABLE)
2825 		seq_puts(m, "status: expirable\n");
2826 	else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2827 		seq_puts(m, "status: confirmed\n");
2828 	else
2829 		seq_puts(m, "status: unconfirmed\n");
2830 	seq_printf(m, "seconds from last renew: %lld\n",
2831 		ktime_get_boottime_seconds() - clp->cl_time);
2832 	seq_puts(m, "name: ");
2833 	seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2834 	seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2835 	if (clp->cl_nii_domain.data) {
2836 		seq_puts(m, "Implementation domain: ");
2837 		seq_quote_mem(m, clp->cl_nii_domain.data,
2838 					clp->cl_nii_domain.len);
2839 		seq_puts(m, "\nImplementation name: ");
2840 		seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2841 		seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2842 			clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2843 	}
2844 	seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2845 	seq_printf(m, "callback address: \"%pISpc\"\n", &clp->cl_cb_conn.cb_addr);
2846 	seq_printf(m, "admin-revoked states: %d\n",
2847 		   atomic_read(&clp->cl_admin_revoked));
2848 	spin_lock(&clp->cl_lock);
2849 	seq_printf(m, "session slots:");
2850 	list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2851 		seq_printf(m, " %u", ses->se_fchannel.maxreqs);
2852 	seq_printf(m, "\nsession target slots:");
2853 	list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2854 		seq_printf(m, " %u", ses->se_target_maxslots);
2855 	spin_unlock(&clp->cl_lock);
2856 	seq_puts(m, "\n");
2857 
2858 	nfsd4_put_client(clp);
2859 
2860 	return 0;
2861 }
2862 
2863 DEFINE_SHOW_ATTRIBUTE(client_info);
2864 
states_start(struct seq_file * s,loff_t * pos)2865 static void *states_start(struct seq_file *s, loff_t *pos)
2866 	__acquires(&clp->cl_lock)
2867 {
2868 	struct nfs4_client *clp = s->private;
2869 	unsigned long id = *pos;
2870 	void *ret;
2871 
2872 	spin_lock(&clp->cl_lock);
2873 	ret = idr_get_next_ul(&clp->cl_stateids, &id);
2874 	*pos = id;
2875 	return ret;
2876 }
2877 
states_next(struct seq_file * s,void * v,loff_t * pos)2878 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2879 {
2880 	struct nfs4_client *clp = s->private;
2881 	unsigned long id = *pos;
2882 	void *ret;
2883 
2884 	id = *pos;
2885 	id++;
2886 	ret = idr_get_next_ul(&clp->cl_stateids, &id);
2887 	*pos = id;
2888 	return ret;
2889 }
2890 
states_stop(struct seq_file * s,void * v)2891 static void states_stop(struct seq_file *s, void *v)
2892 	__releases(&clp->cl_lock)
2893 {
2894 	struct nfs4_client *clp = s->private;
2895 
2896 	spin_unlock(&clp->cl_lock);
2897 }
2898 
nfs4_show_fname(struct seq_file * s,struct nfsd_file * f)2899 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2900 {
2901          seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2902 }
2903 
nfs4_show_superblock(struct seq_file * s,struct nfsd_file * f)2904 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2905 {
2906 	struct inode *inode = file_inode(f->nf_file);
2907 
2908 	seq_printf(s, "superblock: \"%02x:%02x:%llu\"",
2909 					MAJOR(inode->i_sb->s_dev),
2910 					 MINOR(inode->i_sb->s_dev),
2911 					 inode->i_ino);
2912 }
2913 
nfs4_show_owner(struct seq_file * s,struct nfs4_stateowner * oo)2914 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2915 {
2916 	seq_puts(s, "owner: ");
2917 	seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2918 }
2919 
nfs4_show_stateid(struct seq_file * s,stateid_t * stid)2920 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2921 {
2922 	seq_printf(s, "0x%.8x", stid->si_generation);
2923 	seq_printf(s, "%12phN", &stid->si_opaque);
2924 }
2925 
nfs4_show_open(struct seq_file * s,struct nfs4_stid * st)2926 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2927 {
2928 	struct nfs4_ol_stateid *ols;
2929 	struct nfs4_file *nf;
2930 	struct nfsd_file *file;
2931 	struct nfs4_stateowner *oo;
2932 	unsigned int access, deny;
2933 
2934 	ols = openlockstateid(st);
2935 	oo = ols->st_stateowner;
2936 	nf = st->sc_file;
2937 
2938 	seq_puts(s, "- ");
2939 	nfs4_show_stateid(s, &st->sc_stateid);
2940 	seq_puts(s, ": { type: open, ");
2941 
2942 	access = bmap_to_share_mode(ols->st_access_bmap);
2943 	deny   = bmap_to_share_mode(ols->st_deny_bmap);
2944 
2945 	seq_printf(s, "access: %s%s, ",
2946 		access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2947 		access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2948 	seq_printf(s, "deny: %s%s, ",
2949 		deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2950 		deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2951 
2952 	if (nf) {
2953 		spin_lock(&nf->fi_lock);
2954 		file = find_any_file_locked(nf);
2955 		if (file) {
2956 			nfs4_show_superblock(s, file);
2957 			seq_puts(s, ", ");
2958 			nfs4_show_fname(s, file);
2959 			seq_puts(s, ", ");
2960 		}
2961 		spin_unlock(&nf->fi_lock);
2962 	} else
2963 		seq_puts(s, "closed, ");
2964 	nfs4_show_owner(s, oo);
2965 	if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
2966 		seq_puts(s, ", admin-revoked");
2967 	seq_puts(s, " }\n");
2968 	return 0;
2969 }
2970 
nfs4_show_lock(struct seq_file * s,struct nfs4_stid * st)2971 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2972 {
2973 	struct nfs4_ol_stateid *ols;
2974 	struct nfs4_file *nf;
2975 	struct nfsd_file *file;
2976 	struct nfs4_stateowner *oo;
2977 
2978 	ols = openlockstateid(st);
2979 	oo = ols->st_stateowner;
2980 	nf = st->sc_file;
2981 
2982 	seq_puts(s, "- ");
2983 	nfs4_show_stateid(s, &st->sc_stateid);
2984 	seq_puts(s, ": { type: lock, ");
2985 
2986 	spin_lock(&nf->fi_lock);
2987 	file = find_any_file_locked(nf);
2988 	if (file) {
2989 		/*
2990 		 * Note: a lock stateid isn't really the same thing as a lock,
2991 		 * it's the locking state held by one owner on a file, and there
2992 		 * may be multiple (or no) lock ranges associated with it.
2993 		 * (Same for the matter is true of open stateids.)
2994 		 */
2995 
2996 		nfs4_show_superblock(s, file);
2997 		/* XXX: open stateid? */
2998 		seq_puts(s, ", ");
2999 		nfs4_show_fname(s, file);
3000 		seq_puts(s, ", ");
3001 	}
3002 	nfs4_show_owner(s, oo);
3003 	if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
3004 		seq_puts(s, ", admin-revoked");
3005 	seq_puts(s, " }\n");
3006 	spin_unlock(&nf->fi_lock);
3007 	return 0;
3008 }
3009 
nfs4_show_deleg_type(u32 dl_type)3010 static char *nfs4_show_deleg_type(u32 dl_type)
3011 {
3012 	switch (dl_type) {
3013 	case OPEN_DELEGATE_READ:
3014 		return "r";
3015 	case OPEN_DELEGATE_WRITE:
3016 		return "w";
3017 	case OPEN_DELEGATE_READ_ATTRS_DELEG:
3018 		return "ra";
3019 	case OPEN_DELEGATE_WRITE_ATTRS_DELEG:
3020 		return "wa";
3021 	}
3022 	return "?";
3023 }
3024 
nfs4_show_deleg(struct seq_file * s,struct nfs4_stid * st)3025 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
3026 {
3027 	struct nfs4_delegation *ds;
3028 	struct nfs4_file *nf;
3029 	struct nfsd_file *file;
3030 
3031 	ds = delegstateid(st);
3032 	nf = st->sc_file;
3033 
3034 	seq_puts(s, "- ");
3035 	nfs4_show_stateid(s, &st->sc_stateid);
3036 	seq_puts(s, ": { type: deleg, ");
3037 
3038 	seq_printf(s, "access: %s", nfs4_show_deleg_type(ds->dl_type));
3039 
3040 	/* XXX: lease time, whether it's being recalled. */
3041 
3042 	spin_lock(&nf->fi_lock);
3043 	file = nf->fi_deleg_file;
3044 	if (file) {
3045 		seq_puts(s, ", ");
3046 		nfs4_show_superblock(s, file);
3047 		seq_puts(s, ", ");
3048 		nfs4_show_fname(s, file);
3049 	}
3050 	spin_unlock(&nf->fi_lock);
3051 	if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
3052 		seq_puts(s, ", admin-revoked");
3053 	seq_puts(s, " }\n");
3054 	return 0;
3055 }
3056 
nfs4_show_layout(struct seq_file * s,struct nfs4_stid * st)3057 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
3058 {
3059 	struct nfs4_layout_stateid *ls;
3060 	struct nfsd_file *file;
3061 
3062 	ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
3063 
3064 	seq_puts(s, "- ");
3065 	nfs4_show_stateid(s, &st->sc_stateid);
3066 	seq_puts(s, ": { type: layout");
3067 
3068 	/* XXX: What else would be useful? */
3069 
3070 	spin_lock(&ls->ls_stid.sc_file->fi_lock);
3071 	file = ls->ls_file;
3072 	if (file) {
3073 		seq_puts(s, ", ");
3074 		nfs4_show_superblock(s, file);
3075 		seq_puts(s, ", ");
3076 		nfs4_show_fname(s, file);
3077 	}
3078 	spin_unlock(&ls->ls_stid.sc_file->fi_lock);
3079 	if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
3080 		seq_puts(s, ", admin-revoked");
3081 	seq_puts(s, " }\n");
3082 
3083 	return 0;
3084 }
3085 
states_show(struct seq_file * s,void * v)3086 static int states_show(struct seq_file *s, void *v)
3087 {
3088 	struct nfs4_stid *st = v;
3089 
3090 	switch (st->sc_type) {
3091 	case SC_TYPE_OPEN:
3092 		return nfs4_show_open(s, st);
3093 	case SC_TYPE_LOCK:
3094 		return nfs4_show_lock(s, st);
3095 	case SC_TYPE_DELEG:
3096 		return nfs4_show_deleg(s, st);
3097 	case SC_TYPE_LAYOUT:
3098 		return nfs4_show_layout(s, st);
3099 	default:
3100 		return 0; /* XXX: or SEQ_SKIP? */
3101 	}
3102 	/* XXX: copy stateids? */
3103 }
3104 
3105 static struct seq_operations states_seq_ops = {
3106 	.start = states_start,
3107 	.next = states_next,
3108 	.stop = states_stop,
3109 	.show = states_show
3110 };
3111 
client_states_open(struct inode * inode,struct file * file)3112 static int client_states_open(struct inode *inode, struct file *file)
3113 {
3114 	struct seq_file *s;
3115 	struct nfs4_client *clp;
3116 	int ret;
3117 
3118 	clp = get_nfsdfs_clp(inode);
3119 	if (!clp)
3120 		return -ENXIO;
3121 
3122 	ret = seq_open(file, &states_seq_ops);
3123 	if (ret) {
3124 		nfsd4_put_client(clp);
3125 		return ret;
3126 	}
3127 	s = file->private_data;
3128 	s->private = clp;
3129 	return 0;
3130 }
3131 
client_opens_release(struct inode * inode,struct file * file)3132 static int client_opens_release(struct inode *inode, struct file *file)
3133 {
3134 	struct seq_file *m = file->private_data;
3135 	struct nfs4_client *clp = m->private;
3136 
3137 	/* XXX: alternatively, we could get/drop in seq start/stop */
3138 	nfsd4_put_client(clp);
3139 	return seq_release(inode, file);
3140 }
3141 
3142 static const struct file_operations client_states_fops = {
3143 	.open		= client_states_open,
3144 	.read		= seq_read,
3145 	.llseek		= seq_lseek,
3146 	.release	= client_opens_release,
3147 };
3148 
3149 /*
3150  * Normally we refuse to destroy clients that are in use, but here the
3151  * administrator is telling us to just do it.  We also want to wait
3152  * so the caller has a guarantee that the client's locks are gone by
3153  * the time the write returns:
3154  */
force_expire_client(struct nfs4_client * clp)3155 static void force_expire_client(struct nfs4_client *clp)
3156 {
3157 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3158 	bool already_expired;
3159 
3160 	trace_nfsd_clid_admin_expired(&clp->cl_clientid);
3161 
3162 	spin_lock(&nn->client_lock);
3163 	clp->cl_time = 0;
3164 	spin_unlock(&nn->client_lock);
3165 
3166 	wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
3167 	spin_lock(&nn->client_lock);
3168 	already_expired = list_empty(&clp->cl_lru);
3169 	if (!already_expired)
3170 		unhash_client_locked(clp);
3171 	spin_unlock(&nn->client_lock);
3172 
3173 	if (!already_expired)
3174 		expire_client(clp);
3175 	else
3176 		wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
3177 }
3178 
client_ctl_write(struct file * file,const char __user * buf,size_t size,loff_t * pos)3179 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
3180 				   size_t size, loff_t *pos)
3181 {
3182 	char *data;
3183 	struct nfs4_client *clp;
3184 
3185 	data = simple_transaction_get(file, buf, size);
3186 	if (IS_ERR(data))
3187 		return PTR_ERR(data);
3188 	if (size != 7 || 0 != memcmp(data, "expire\n", 7))
3189 		return -EINVAL;
3190 	clp = get_nfsdfs_clp(file_inode(file));
3191 	if (!clp)
3192 		return -ENXIO;
3193 	force_expire_client(clp);
3194 	nfsd4_put_client(clp);
3195 	return 7;
3196 }
3197 
3198 static const struct file_operations client_ctl_fops = {
3199 	.write		= client_ctl_write,
3200 	.release	= simple_transaction_release,
3201 };
3202 
3203 static const struct tree_descr client_files[] = {
3204 	[0] = {"info", &client_info_fops, S_IRUSR},
3205 	[1] = {"states", &client_states_fops, S_IRUSR},
3206 	[2] = {"ctl", &client_ctl_fops, S_IWUSR},
3207 	[3] = {""},
3208 };
3209 
3210 static int
nfsd4_cb_recall_any_done(struct nfsd4_callback * cb,struct rpc_task * task)3211 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb,
3212 				struct rpc_task *task)
3213 {
3214 	trace_nfsd_cb_recall_any_done(cb, task);
3215 	switch (task->tk_status) {
3216 	case -NFS4ERR_DELAY:
3217 		rpc_delay(task, 2 * HZ);
3218 		return 0;
3219 	default:
3220 		return 1;
3221 	}
3222 }
3223 
3224 static void
nfsd4_cb_recall_any_release(struct nfsd4_callback * cb)3225 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb)
3226 {
3227 	struct nfs4_client *clp = cb->cb_clp;
3228 
3229 	nfsd4_put_client(clp);
3230 }
3231 
3232 static int
nfsd4_cb_getattr_done(struct nfsd4_callback * cb,struct rpc_task * task)3233 nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task)
3234 {
3235 	struct nfs4_cb_fattr *ncf =
3236 			container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
3237 	struct nfs4_delegation *dp =
3238 			container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
3239 
3240 	trace_nfsd_cb_getattr_done(&dp->dl_stid.sc_stateid, task);
3241 	ncf->ncf_cb_status = task->tk_status;
3242 	switch (task->tk_status) {
3243 	case -NFS4ERR_DELAY:
3244 		rpc_delay(task, 2 * HZ);
3245 		return 0;
3246 	default:
3247 		return 1;
3248 	}
3249 }
3250 
3251 static void
nfsd4_cb_getattr_release(struct nfsd4_callback * cb)3252 nfsd4_cb_getattr_release(struct nfsd4_callback *cb)
3253 {
3254 	struct nfs4_cb_fattr *ncf =
3255 			container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
3256 	struct nfs4_delegation *dp =
3257 			container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
3258 
3259 	nfs4_put_stid(&dp->dl_stid);
3260 }
3261 
3262 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
3263 	.done		= nfsd4_cb_recall_any_done,
3264 	.release	= nfsd4_cb_recall_any_release,
3265 	.opcode		= OP_CB_RECALL_ANY,
3266 };
3267 
3268 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = {
3269 	.done		= nfsd4_cb_getattr_done,
3270 	.release	= nfsd4_cb_getattr_release,
3271 	.opcode		= OP_CB_GETATTR,
3272 };
3273 
nfs4_cb_getattr(struct nfs4_cb_fattr * ncf)3274 static void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf)
3275 {
3276 	struct nfs4_delegation *dp =
3277 			container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
3278 
3279 	if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &ncf->ncf_getattr.cb_flags))
3280 		return;
3281 
3282 	/* set to proper status when nfsd4_cb_getattr_done runs */
3283 	ncf->ncf_cb_status = NFS4ERR_IO;
3284 
3285 	/* ensure that wake_bit is done when RUNNING is cleared */
3286 	set_bit(NFSD4_CALLBACK_WAKE, &ncf->ncf_getattr.cb_flags);
3287 
3288 	refcount_inc(&dp->dl_stid.sc_count);
3289 	nfsd4_run_cb(&ncf->ncf_getattr);
3290 }
3291 
create_client(struct xdr_netobj name,struct svc_rqst * rqstp,nfs4_verifier * verf)3292 static struct nfs4_client *create_client(struct xdr_netobj name,
3293 		struct svc_rqst *rqstp, nfs4_verifier *verf)
3294 {
3295 	struct nfs4_client *clp;
3296 	struct sockaddr *sa = svc_addr(rqstp);
3297 	int ret;
3298 	struct net *net = SVC_NET(rqstp);
3299 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3300 	struct dentry *dentries[ARRAY_SIZE(client_files)];
3301 
3302 	clp = alloc_client(name, nn);
3303 	if (clp == NULL)
3304 		return NULL;
3305 
3306 	ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
3307 	if (ret) {
3308 		free_client(clp);
3309 		return NULL;
3310 	}
3311 	gen_clid(clp, nn);
3312 	kref_init(&clp->cl_nfsdfs.cl_ref);
3313 	nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
3314 	clp->cl_time = ktime_get_boottime_seconds();
3315 	copy_verf(clp, verf);
3316 	memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
3317 	clp->cl_cb_session = NULL;
3318 	clp->net = net;
3319 	clp->cl_nfsd_dentry = nfsd_client_mkdir(
3320 		nn, &clp->cl_nfsdfs,
3321 		clp->cl_clientid.cl_id - nn->clientid_base,
3322 		client_files, dentries);
3323 	clp->cl_nfsd_info_dentry = dentries[0];
3324 	if (!clp->cl_nfsd_dentry) {
3325 		free_client(clp);
3326 		return NULL;
3327 	}
3328 	clp->cl_ra = kzalloc_obj(*clp->cl_ra);
3329 	if (!clp->cl_ra) {
3330 		free_client(clp);
3331 		return NULL;
3332 	}
3333 	clp->cl_ra_time = 0;
3334 	nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops,
3335 			NFSPROC4_CLNT_CB_RECALL_ANY);
3336 	return clp;
3337 }
3338 
3339 static void
add_clp_to_name_tree(struct nfs4_client * new_clp,struct rb_root * root)3340 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
3341 {
3342 	struct rb_node **new = &(root->rb_node), *parent = NULL;
3343 	struct nfs4_client *clp;
3344 
3345 	while (*new) {
3346 		clp = rb_entry(*new, struct nfs4_client, cl_namenode);
3347 		parent = *new;
3348 
3349 		if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
3350 			new = &((*new)->rb_left);
3351 		else
3352 			new = &((*new)->rb_right);
3353 	}
3354 
3355 	rb_link_node(&new_clp->cl_namenode, parent, new);
3356 	rb_insert_color(&new_clp->cl_namenode, root);
3357 }
3358 
3359 static struct nfs4_client *
find_clp_in_name_tree(struct xdr_netobj * name,struct rb_root * root)3360 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
3361 {
3362 	int cmp;
3363 	struct rb_node *node = root->rb_node;
3364 	struct nfs4_client *clp;
3365 
3366 	while (node) {
3367 		clp = rb_entry(node, struct nfs4_client, cl_namenode);
3368 		cmp = compare_blob(&clp->cl_name, name);
3369 		if (cmp > 0)
3370 			node = node->rb_left;
3371 		else if (cmp < 0)
3372 			node = node->rb_right;
3373 		else
3374 			return clp;
3375 	}
3376 	return NULL;
3377 }
3378 
3379 static void
add_to_unconfirmed(struct nfs4_client * clp)3380 add_to_unconfirmed(struct nfs4_client *clp)
3381 {
3382 	unsigned int idhashval;
3383 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3384 
3385 	lockdep_assert_held(&nn->client_lock);
3386 
3387 	clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3388 	add_clp_to_name_tree(clp, &nn->unconf_name_tree);
3389 	idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3390 	list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3391 	renew_client_locked(clp);
3392 }
3393 
3394 static void
move_to_confirmed(struct nfs4_client * clp)3395 move_to_confirmed(struct nfs4_client *clp)
3396 {
3397 	unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3398 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3399 
3400 	lockdep_assert_held(&nn->client_lock);
3401 
3402 	list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
3403 	rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
3404 	add_clp_to_name_tree(clp, &nn->conf_name_tree);
3405 	set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3406 	trace_nfsd_clid_confirmed(&clp->cl_clientid);
3407 	renew_client_locked(clp);
3408 }
3409 
3410 static struct nfs4_client *
find_client_in_id_table(struct list_head * tbl,clientid_t * clid,bool sessions)3411 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
3412 {
3413 	struct nfs4_client *clp;
3414 	unsigned int idhashval = clientid_hashval(clid->cl_id);
3415 
3416 	list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
3417 		if (same_clid(&clp->cl_clientid, clid)) {
3418 			if ((bool)clp->cl_minorversion != sessions)
3419 				return NULL;
3420 			renew_client_locked(clp);
3421 			return clp;
3422 		}
3423 	}
3424 	return NULL;
3425 }
3426 
3427 static struct nfs4_client *
find_confirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)3428 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3429 {
3430 	struct list_head *tbl = nn->conf_id_hashtbl;
3431 
3432 	lockdep_assert_held(&nn->client_lock);
3433 	return find_client_in_id_table(tbl, clid, sessions);
3434 }
3435 
3436 static struct nfs4_client *
find_unconfirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)3437 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3438 {
3439 	struct list_head *tbl = nn->unconf_id_hashtbl;
3440 
3441 	lockdep_assert_held(&nn->client_lock);
3442 	return find_client_in_id_table(tbl, clid, sessions);
3443 }
3444 
clp_used_exchangeid(struct nfs4_client * clp)3445 static bool clp_used_exchangeid(struct nfs4_client *clp)
3446 {
3447 	return clp->cl_exchange_flags != 0;
3448 }
3449 
3450 static struct nfs4_client *
find_confirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)3451 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3452 {
3453 	lockdep_assert_held(&nn->client_lock);
3454 	return find_clp_in_name_tree(name, &nn->conf_name_tree);
3455 }
3456 
3457 static struct nfs4_client *
find_unconfirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)3458 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3459 {
3460 	lockdep_assert_held(&nn->client_lock);
3461 	return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3462 }
3463 
3464 static void
gen_callback(struct nfs4_client * clp,struct nfsd4_setclientid * se,struct svc_rqst * rqstp)3465 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3466 {
3467 	struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3468 	struct sockaddr	*sa = svc_addr(rqstp);
3469 	u32 scopeid = rpc_get_scope_id(sa);
3470 	unsigned short expected_family;
3471 
3472 	/* Currently, we only support tcp and tcp6 for the callback channel */
3473 	if (se->se_callback_netid_len == 3 &&
3474 	    !memcmp(se->se_callback_netid_val, "tcp", 3))
3475 		expected_family = AF_INET;
3476 	else if (se->se_callback_netid_len == 4 &&
3477 		 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3478 		expected_family = AF_INET6;
3479 	else
3480 		goto out_err;
3481 
3482 	conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3483 					    se->se_callback_addr_len,
3484 					    (struct sockaddr *)&conn->cb_addr,
3485 					    sizeof(conn->cb_addr));
3486 
3487 	if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3488 		goto out_err;
3489 
3490 	if (conn->cb_addr.ss_family == AF_INET6)
3491 		((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3492 
3493 	conn->cb_prog = se->se_callback_prog;
3494 	conn->cb_ident = se->se_callback_ident;
3495 	memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3496 	trace_nfsd_cb_args(clp, conn);
3497 	return;
3498 out_err:
3499 	conn->cb_addr.ss_family = AF_UNSPEC;
3500 	conn->cb_addrlen = 0;
3501 	trace_nfsd_cb_nodelegs(clp);
3502 	return;
3503 }
3504 
3505 /*
3506  * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3507  */
3508 static void
nfsd4_store_cache_entry(struct nfsd4_compoundres * resp)3509 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3510 {
3511 	struct xdr_buf *buf = resp->xdr->buf;
3512 	struct nfsd4_slot *slot = resp->cstate.slot;
3513 	unsigned int base;
3514 
3515 	/*
3516 	 * RFC 5661 Section 2.10.6.1.2:
3517 	 *
3518 	 * Any time SEQUENCE ... returns an error ... [t]he replier MUST NOT
3519 	 * modify the reply cache entry for the slot whenever an error is
3520 	 * returned from SEQUENCE ...
3521 	 *
3522 	 * Because nfsd4_store_cache_entry is called only by
3523 	 * nfsd4_sequence_done(), nfsd4_store_cache_entry() is called only
3524 	 * when a SEQUENCE operation was part of the COMPOUND.
3525 	 * nfs41_check_op_ordering() ensures SEQUENCE is the first op.
3526 	 */
3527 	if (resp->opcnt == 1 && resp->cstate.status != nfs_ok)
3528 		return;
3529 
3530 	slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3531 	slot->sl_opcnt = resp->opcnt;
3532 	slot->sl_status = resp->cstate.status;
3533 	free_svc_cred(&slot->sl_cred);
3534 	copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3535 
3536 	if (!(resp->cstate.slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
3537 		slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3538 		return;
3539 	}
3540 	slot->sl_flags |= NFSD4_SLOT_CACHED;
3541 
3542 	base = resp->cstate.data_offset;
3543 	slot->sl_datalen = buf->len - base;
3544 	if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3545 		WARN(1, "%s: sessions DRC could not cache compound\n",
3546 		     __func__);
3547 	return;
3548 }
3549 
3550 /*
3551  * The sequence operation is not cached because we can use the slot and
3552  * session values.
3553  */
3554 static __be32
nfsd4_replay_cache_entry(struct nfsd4_compoundres * resp,struct nfsd4_sequence * seq)3555 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3556 			 struct nfsd4_sequence *seq)
3557 {
3558 	struct nfsd4_compoundargs *args = resp->rqstp->rq_argp;
3559 	struct nfsd4_slot *slot = resp->cstate.slot;
3560 	struct xdr_stream *xdr = resp->xdr;
3561 	__be32 *p;
3562 
3563 	dprintk("--> %s slot %p\n", __func__, slot);
3564 
3565 	/* Always encode the SEQUENCE response. */
3566 	nfsd4_encode_operation(resp, &args->ops[0]);
3567 	if (args->opcnt == 1)
3568 		/* A solo SEQUENCE - nothing was cached */
3569 		return args->ops[0].status;
3570 
3571 	if (!(slot->sl_flags & NFSD4_SLOT_CACHED)) {
3572 		/* We weren't asked to cache this. */
3573 		struct nfsd4_op *op;
3574 
3575 		op = &args->ops[resp->opcnt++];
3576 		op->status = nfserr_retry_uncached_rep;
3577 		nfsd4_encode_operation(resp, op);
3578 		return op->status;
3579 	}
3580 
3581 	/* return reply from cache */
3582 	p = xdr_reserve_space(xdr, slot->sl_datalen);
3583 	if (!p) {
3584 		WARN_ON_ONCE(1);
3585 		return nfserr_serverfault;
3586 	}
3587 	xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3588 	xdr_commit_encode(xdr);
3589 
3590 	resp->opcnt = slot->sl_opcnt;
3591 	return slot->sl_status;
3592 }
3593 
3594 /*
3595  * Set the exchange_id flags returned by the server.
3596  */
3597 static void
nfsd4_set_ex_flags(struct nfs4_client * new,struct nfsd4_exchange_id * clid)3598 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3599 {
3600 #ifdef CONFIG_NFSD_PNFS
3601 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3602 #else
3603 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3604 #endif
3605 
3606 	/* Referrals are supported, Migration is not. */
3607 	new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3608 
3609 	/* set the wire flags to return to client. */
3610 	clid->flags = new->cl_exchange_flags;
3611 }
3612 
client_has_openowners(struct nfs4_client * clp)3613 static bool client_has_openowners(struct nfs4_client *clp)
3614 {
3615 	struct nfs4_openowner *oo;
3616 
3617 	list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3618 		if (!list_empty(&oo->oo_owner.so_stateids))
3619 			return true;
3620 	}
3621 	return false;
3622 }
3623 
client_has_state(struct nfs4_client * clp)3624 static bool client_has_state(struct nfs4_client *clp)
3625 {
3626 	return client_has_openowners(clp)
3627 #ifdef CONFIG_NFSD_PNFS
3628 		|| !list_empty(&clp->cl_lo_states)
3629 #endif
3630 		|| !list_empty(&clp->cl_delegations)
3631 		|| !list_empty(&clp->cl_sessions)
3632 		|| nfsd4_has_active_async_copies(clp);
3633 }
3634 
copy_impl_id(struct nfs4_client * clp,struct nfsd4_exchange_id * exid)3635 static __be32 copy_impl_id(struct nfs4_client *clp,
3636 				struct nfsd4_exchange_id *exid)
3637 {
3638 	if (!exid->nii_domain.data)
3639 		return 0;
3640 	xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3641 	if (!clp->cl_nii_domain.data)
3642 		return nfserr_jukebox;
3643 	xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3644 	if (!clp->cl_nii_name.data)
3645 		return nfserr_jukebox;
3646 	clp->cl_nii_time = exid->nii_time;
3647 	return 0;
3648 }
3649 
3650 __be32
nfsd4_exchange_id(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3651 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3652 		union nfsd4_op_u *u)
3653 {
3654 	struct nfsd4_exchange_id *exid = &u->exchange_id;
3655 	struct nfs4_client *conf, *new;
3656 	struct nfs4_client *unconf = NULL;
3657 	__be32 status;
3658 	char			addr_str[INET6_ADDRSTRLEN];
3659 	nfs4_verifier		verf = exid->verifier;
3660 	struct sockaddr		*sa = svc_addr(rqstp);
3661 	bool	update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3662 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3663 
3664 	rpc_ntop(sa, addr_str, sizeof(addr_str));
3665 	dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3666 		"ip_addr=%s flags %x, spa_how %u\n",
3667 		__func__, rqstp, exid, exid->clname.len, exid->clname.data,
3668 		addr_str, exid->flags, exid->spa_how);
3669 
3670 	exid->server_impl_name = kasprintf(GFP_KERNEL, "%s %s %s %s",
3671 					   utsname()->sysname, utsname()->release,
3672 					   utsname()->version, utsname()->machine);
3673 	if (!exid->server_impl_name)
3674 		return nfserr_jukebox;
3675 
3676 	if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3677 		return nfserr_inval;
3678 
3679 	new = create_client(exid->clname, rqstp, &verf);
3680 	if (new == NULL)
3681 		return nfserr_jukebox;
3682 	status = copy_impl_id(new, exid);
3683 	if (status)
3684 		goto out_nolock;
3685 
3686 	switch (exid->spa_how) {
3687 	case SP4_MACH_CRED:
3688 		exid->spo_must_enforce[0] = 0;
3689 		exid->spo_must_enforce[1] = (
3690 			1 << (OP_BIND_CONN_TO_SESSION - 32) |
3691 			1 << (OP_EXCHANGE_ID - 32) |
3692 			1 << (OP_CREATE_SESSION - 32) |
3693 			1 << (OP_DESTROY_SESSION - 32) |
3694 			1 << (OP_DESTROY_CLIENTID - 32));
3695 
3696 		exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3697 					1 << (OP_OPEN_DOWNGRADE) |
3698 					1 << (OP_LOCKU) |
3699 					1 << (OP_DELEGRETURN));
3700 
3701 		exid->spo_must_allow[1] &= (
3702 					1 << (OP_TEST_STATEID - 32) |
3703 					1 << (OP_FREE_STATEID - 32));
3704 		if (!svc_rqst_integrity_protected(rqstp)) {
3705 			status = nfserr_inval;
3706 			goto out_nolock;
3707 		}
3708 		/*
3709 		 * Sometimes userspace doesn't give us a principal.
3710 		 * Which is a bug, really.  Anyway, we can't enforce
3711 		 * MACH_CRED in that case, better to give up now:
3712 		 */
3713 		if (!new->cl_cred.cr_principal &&
3714 					!new->cl_cred.cr_raw_principal) {
3715 			status = nfserr_serverfault;
3716 			goto out_nolock;
3717 		}
3718 		new->cl_mach_cred = true;
3719 		break;
3720 	case SP4_NONE:
3721 		break;
3722 	default:				/* checked by xdr code */
3723 		WARN_ON_ONCE(1);
3724 		fallthrough;
3725 	case SP4_SSV:
3726 		status = nfserr_encr_alg_unsupp;
3727 		goto out_nolock;
3728 	}
3729 
3730 	/* Cases below refer to rfc 5661 section 18.35.4: */
3731 	spin_lock(&nn->client_lock);
3732 	conf = find_confirmed_client_by_name(&exid->clname, nn);
3733 	if (conf) {
3734 		bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3735 		bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3736 
3737 		if (update) {
3738 			if (!clp_used_exchangeid(conf)) { /* buggy client */
3739 				status = nfserr_inval;
3740 				goto out;
3741 			}
3742 			if (!nfsd4_mach_creds_match(conf, rqstp)) {
3743 				status = nfserr_wrong_cred;
3744 				goto out;
3745 			}
3746 			if (!creds_match) { /* case 9 */
3747 				status = nfserr_perm;
3748 				goto out;
3749 			}
3750 			if (!verfs_match) { /* case 8 */
3751 				status = nfserr_not_same;
3752 				goto out;
3753 			}
3754 			/* case 6 */
3755 			exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3756 			trace_nfsd_clid_confirmed_r(conf);
3757 			goto out_copy;
3758 		}
3759 		if (!creds_match) { /* case 3 */
3760 			if (client_has_state(conf)) {
3761 				status = nfserr_clid_inuse;
3762 				trace_nfsd_clid_cred_mismatch(conf, rqstp);
3763 				goto out;
3764 			}
3765 			goto out_new;
3766 		}
3767 		if (verfs_match) { /* case 2 */
3768 			conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3769 			trace_nfsd_clid_confirmed_r(conf);
3770 			goto out_copy;
3771 		}
3772 		/* case 5, client reboot */
3773 		trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3774 		conf = NULL;
3775 		goto out_new;
3776 	}
3777 
3778 	if (update) { /* case 7 */
3779 		status = nfserr_noent;
3780 		goto out;
3781 	}
3782 
3783 	unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3784 	if (unconf) /* case 4, possible retry or client restart */
3785 		unhash_client_locked(unconf);
3786 
3787 	/* case 1, new owner ID */
3788 	trace_nfsd_clid_fresh(new);
3789 
3790 out_new:
3791 	if (conf) {
3792 		status = mark_client_expired_locked(conf);
3793 		if (status)
3794 			goto out;
3795 		trace_nfsd_clid_replaced(&conf->cl_clientid);
3796 	}
3797 	new->cl_minorversion = cstate->minorversion;
3798 	new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3799 	new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3800 
3801 	/* Contrived initial CREATE_SESSION response */
3802 	new->cl_cs_slot.sl_status = nfserr_seq_misordered;
3803 
3804 	add_to_unconfirmed(new);
3805 	swap(new, conf);
3806 out_copy:
3807 	exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3808 	exid->clientid.cl_id = conf->cl_clientid.cl_id;
3809 
3810 	exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3811 	nfsd4_set_ex_flags(conf, exid);
3812 
3813 	exid->nii_domain.len = sizeof("kernel.org") - 1;
3814 	exid->nii_domain.data = "kernel.org";
3815 
3816 	/*
3817 	 * Note that RFC 8881 places no length limit on
3818 	 * nii_name, but this implementation permits no
3819 	 * more than NFS4_OPAQUE_LIMIT bytes.
3820 	 */
3821 	exid->nii_name.len = strlen(exid->server_impl_name);
3822 	if (exid->nii_name.len > NFS4_OPAQUE_LIMIT)
3823 		exid->nii_name.len = NFS4_OPAQUE_LIMIT;
3824 	exid->nii_name.data = exid->server_impl_name;
3825 
3826 	/* just send zeros - the date is in nii_name */
3827 	exid->nii_time.tv_sec = 0;
3828 	exid->nii_time.tv_nsec = 0;
3829 
3830 	dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3831 		conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3832 	status = nfs_ok;
3833 
3834 out:
3835 	spin_unlock(&nn->client_lock);
3836 out_nolock:
3837 	if (new)
3838 		expire_client(new);
3839 	if (unconf) {
3840 		trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3841 		expire_client(unconf);
3842 	}
3843 	return status;
3844 }
3845 
3846 void
nfsd4_exchange_id_release(union nfsd4_op_u * u)3847 nfsd4_exchange_id_release(union nfsd4_op_u *u)
3848 {
3849 	struct nfsd4_exchange_id *exid = &u->exchange_id;
3850 
3851 	kfree(exid->server_impl_name);
3852 }
3853 
check_slot_seqid(u32 seqid,u32 slot_seqid,u8 flags)3854 static __be32 check_slot_seqid(u32 seqid, u32 slot_seqid, u8 flags)
3855 {
3856 	/* The slot is in use, and no response has been sent. */
3857 	if (flags & NFSD4_SLOT_INUSE) {
3858 		if (seqid == slot_seqid)
3859 			return nfserr_jukebox;
3860 		else
3861 			return nfserr_seq_misordered;
3862 	}
3863 	/* Note unsigned 32-bit arithmetic handles wraparound: */
3864 	if (likely(seqid == slot_seqid + 1))
3865 		return nfs_ok;
3866 	if ((flags & NFSD4_SLOT_REUSED) && seqid == 1)
3867 		return nfs_ok;
3868 	if (seqid == slot_seqid)
3869 		return nfserr_replay_cache;
3870 	return nfserr_seq_misordered;
3871 }
3872 
3873 /*
3874  * Cache the create session result into the create session single DRC
3875  * slot cache by saving the xdr structure. sl_seqid has been set.
3876  * Do this for solo or embedded create session operations.
3877  */
3878 static void
nfsd4_cache_create_session(struct nfsd4_create_session * cr_ses,struct nfsd4_clid_slot * slot,__be32 nfserr)3879 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3880 			   struct nfsd4_clid_slot *slot, __be32 nfserr)
3881 {
3882 	slot->sl_status = nfserr;
3883 	memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3884 }
3885 
3886 static __be32
nfsd4_replay_create_session(struct nfsd4_create_session * cr_ses,struct nfsd4_clid_slot * slot)3887 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3888 			    struct nfsd4_clid_slot *slot)
3889 {
3890 	memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3891 	return slot->sl_status;
3892 }
3893 
3894 #define NFSD_MIN_REQ_HDR_SEQ_SZ	((\
3895 			2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3896 			1 +	/* MIN tag is length with zero, only length */ \
3897 			3 +	/* version, opcount, opcode */ \
3898 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3899 				/* seqid, slotID, slotID, cache */ \
3900 			4 ) * sizeof(__be32))
3901 
3902 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3903 			2 +	/* verifier: AUTH_NULL, length 0 */\
3904 			1 +	/* status */ \
3905 			1 +	/* MIN tag is length with zero, only length */ \
3906 			3 +	/* opcount, opcode, opstatus*/ \
3907 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3908 				/* seqid, slotID, slotID, slotID, status */ \
3909 			5 ) * sizeof(__be32))
3910 
check_forechannel_attrs(struct nfsd4_channel_attrs * ca,struct nfsd_net * nn)3911 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3912 {
3913 	u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3914 
3915 	if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3916 		return nfserr_toosmall;
3917 	if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3918 		return nfserr_toosmall;
3919 	ca->headerpadsz = 0;
3920 	ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3921 	ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3922 	ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3923 	ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3924 			NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3925 	ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3926 
3927 	return nfs_ok;
3928 }
3929 
3930 /*
3931  * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3932  * These are based on similar macros in linux/sunrpc/msg_prot.h .
3933  */
3934 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3935 	(RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3936 
3937 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3938 	(RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3939 
3940 #define NFSD_CB_MAX_REQ_SZ	((NFS4_enc_cb_recall_sz + \
3941 				 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3942 #define NFSD_CB_MAX_RESP_SZ	((NFS4_dec_cb_recall_sz + \
3943 				 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3944 				 sizeof(__be32))
3945 
check_backchannel_attrs(struct nfsd4_channel_attrs * ca)3946 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3947 {
3948 	ca->headerpadsz = 0;
3949 
3950 	if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3951 		return nfserr_toosmall;
3952 	if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3953 		return nfserr_toosmall;
3954 	ca->maxresp_cached = 0;
3955 	if (ca->maxops < 2)
3956 		return nfserr_toosmall;
3957 
3958 	return nfs_ok;
3959 }
3960 
nfsd4_check_cb_sec(struct nfsd4_cb_sec * cbs)3961 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3962 {
3963 	switch (cbs->flavor) {
3964 	case RPC_AUTH_NULL:
3965 	case RPC_AUTH_UNIX:
3966 		return nfs_ok;
3967 	default:
3968 		/*
3969 		 * GSS case: the spec doesn't allow us to return this
3970 		 * error.  But it also doesn't allow us not to support
3971 		 * GSS.
3972 		 * I'd rather this fail hard than return some error the
3973 		 * client might think it can already handle:
3974 		 */
3975 		return nfserr_encr_alg_unsupp;
3976 	}
3977 }
3978 
3979 __be32
nfsd4_create_session(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3980 nfsd4_create_session(struct svc_rqst *rqstp,
3981 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3982 {
3983 	struct nfsd4_create_session *cr_ses = &u->create_session;
3984 	struct sockaddr *sa = svc_addr(rqstp);
3985 	struct nfs4_client *conf, *unconf;
3986 	struct nfsd4_clid_slot *cs_slot;
3987 	struct nfs4_client *old = NULL;
3988 	struct nfsd4_session *new;
3989 	struct nfsd4_conn *conn;
3990 	__be32 status = 0;
3991 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3992 
3993 	if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3994 		return nfserr_inval;
3995 	status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3996 	if (status)
3997 		return status;
3998 	status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3999 	if (status)
4000 		return status;
4001 	status = check_backchannel_attrs(&cr_ses->back_channel);
4002 	if (status)
4003 		goto out_err;
4004 	status = nfserr_jukebox;
4005 	new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
4006 	if (!new)
4007 		goto out_err;
4008 	conn = alloc_conn_from_crses(rqstp, cr_ses);
4009 	if (!conn)
4010 		goto out_free_session;
4011 
4012 	spin_lock(&nn->client_lock);
4013 
4014 	/* RFC 8881 Section 18.36.4 Phase 1: Client record look-up. */
4015 	unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
4016 	conf = find_confirmed_client(&cr_ses->clientid, true, nn);
4017 	if (!conf && !unconf) {
4018 		status = nfserr_stale_clientid;
4019 		goto out_free_conn;
4020 	}
4021 
4022 	/* RFC 8881 Section 18.36.4 Phase 2: Sequence ID processing. */
4023 	if (conf) {
4024 		cs_slot = &conf->cl_cs_slot;
4025 		trace_nfsd_slot_seqid_conf(conf, cr_ses);
4026 	} else {
4027 		cs_slot = &unconf->cl_cs_slot;
4028 		trace_nfsd_slot_seqid_unconf(unconf, cr_ses);
4029 	}
4030 	status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
4031 	switch (status) {
4032 	case nfs_ok:
4033 		cs_slot->sl_seqid++;
4034 		cr_ses->seqid = cs_slot->sl_seqid;
4035 		break;
4036 	case nfserr_replay_cache:
4037 		status = nfsd4_replay_create_session(cr_ses, cs_slot);
4038 		fallthrough;
4039 	case nfserr_jukebox:
4040 		/* The server MUST NOT cache NFS4ERR_DELAY */
4041 		goto out_free_conn;
4042 	default:
4043 		goto out_cache_error;
4044 	}
4045 
4046 	/* RFC 8881 Section 18.36.4 Phase 3: Client ID confirmation. */
4047 	if (conf) {
4048 		status = nfserr_wrong_cred;
4049 		if (!nfsd4_mach_creds_match(conf, rqstp))
4050 			goto out_cache_error;
4051 	} else {
4052 		status = nfserr_clid_inuse;
4053 		if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
4054 		    !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
4055 			trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4056 			goto out_cache_error;
4057 		}
4058 		status = nfserr_wrong_cred;
4059 		if (!nfsd4_mach_creds_match(unconf, rqstp))
4060 			goto out_cache_error;
4061 		old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4062 		if (old) {
4063 			status = mark_client_expired_locked(old);
4064 			if (status)
4065 				goto out_expired_error;
4066 			trace_nfsd_clid_replaced(&old->cl_clientid);
4067 		}
4068 		move_to_confirmed(unconf);
4069 		conf = unconf;
4070 	}
4071 
4072 	/* RFC 8881 Section 18.36.4 Phase 4: Session creation. */
4073 	status = nfs_ok;
4074 	/* Persistent sessions are not supported */
4075 	cr_ses->flags &= ~SESSION4_PERSIST;
4076 	/* Upshifting from TCP to RDMA is not supported */
4077 	cr_ses->flags &= ~SESSION4_RDMA;
4078 	/* Report the correct number of backchannel slots */
4079 	cr_ses->back_channel.maxreqs = new->se_cb_highest_slot + 1;
4080 
4081 	init_session(rqstp, new, conf, cr_ses);
4082 	nfsd4_get_session_locked(new);
4083 
4084 	memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
4085 	       NFS4_MAX_SESSIONID_LEN);
4086 
4087 	/* cache solo and embedded create sessions under the client_lock */
4088 	nfsd4_cache_create_session(cr_ses, cs_slot, status);
4089 	spin_unlock(&nn->client_lock);
4090 	if (conf == unconf)
4091 		fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4092 	/* init connection and backchannel */
4093 	nfsd4_init_conn(rqstp, conn, new);
4094 	nfsd4_put_session(new);
4095 	if (old)
4096 		expire_client(old);
4097 	return status;
4098 
4099 out_expired_error:
4100 	/*
4101 	 * Revert the slot seq_nr change so the server will process
4102 	 * the client's resend instead of returning a cached response.
4103 	 */
4104 	if (status == nfserr_jukebox) {
4105 		cs_slot->sl_seqid--;
4106 		cr_ses->seqid = cs_slot->sl_seqid;
4107 		goto out_free_conn;
4108 	}
4109 out_cache_error:
4110 	nfsd4_cache_create_session(cr_ses, cs_slot, status);
4111 out_free_conn:
4112 	spin_unlock(&nn->client_lock);
4113 	free_conn(conn);
4114 out_free_session:
4115 	__free_session(new);
4116 out_err:
4117 	return status;
4118 }
4119 
nfsd4_map_bcts_dir(u32 * dir)4120 static __be32 nfsd4_map_bcts_dir(u32 *dir)
4121 {
4122 	switch (*dir) {
4123 	case NFS4_CDFC4_FORE:
4124 	case NFS4_CDFC4_BACK:
4125 		return nfs_ok;
4126 	case NFS4_CDFC4_FORE_OR_BOTH:
4127 	case NFS4_CDFC4_BACK_OR_BOTH:
4128 		*dir = NFS4_CDFC4_BOTH;
4129 		return nfs_ok;
4130 	}
4131 	return nfserr_inval;
4132 }
4133 
nfsd4_backchannel_ctl(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4134 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
4135 		struct nfsd4_compound_state *cstate,
4136 		union nfsd4_op_u *u)
4137 {
4138 	struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
4139 	struct nfsd4_session *session = cstate->session;
4140 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4141 	__be32 status;
4142 
4143 	status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
4144 	if (status)
4145 		return status;
4146 	spin_lock(&nn->client_lock);
4147 	session->se_cb_prog = bc->bc_cb_program;
4148 	session->se_cb_sec = bc->bc_cb_sec;
4149 	spin_unlock(&nn->client_lock);
4150 
4151 	nfsd4_probe_callback(session->se_client);
4152 
4153 	return nfs_ok;
4154 }
4155 
__nfsd4_find_conn(struct svc_xprt * xpt,struct nfsd4_session * s)4156 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
4157 {
4158 	struct nfsd4_conn *c;
4159 
4160 	list_for_each_entry(c, &s->se_conns, cn_persession) {
4161 		if (c->cn_xprt == xpt) {
4162 			return c;
4163 		}
4164 	}
4165 	return NULL;
4166 }
4167 
nfsd4_match_existing_connection(struct svc_rqst * rqst,struct nfsd4_session * session,u32 req,struct nfsd4_conn ** conn)4168 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
4169 		struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
4170 {
4171 	struct nfs4_client *clp = session->se_client;
4172 	struct svc_xprt *xpt = rqst->rq_xprt;
4173 	struct nfsd4_conn *c;
4174 	__be32 status;
4175 
4176 	/* Following the last paragraph of RFC 5661 Section 18.34.3: */
4177 	spin_lock(&clp->cl_lock);
4178 	c = __nfsd4_find_conn(xpt, session);
4179 	if (!c)
4180 		status = nfserr_noent;
4181 	else if (req == c->cn_flags)
4182 		status = nfs_ok;
4183 	else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
4184 				c->cn_flags != NFS4_CDFC4_BACK)
4185 		status = nfs_ok;
4186 	else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
4187 				c->cn_flags != NFS4_CDFC4_FORE)
4188 		status = nfs_ok;
4189 	else
4190 		status = nfserr_inval;
4191 	spin_unlock(&clp->cl_lock);
4192 	if (status == nfs_ok && conn)
4193 		*conn = c;
4194 	return status;
4195 }
4196 
nfsd4_bind_conn_to_session(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4197 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
4198 		     struct nfsd4_compound_state *cstate,
4199 		     union nfsd4_op_u *u)
4200 {
4201 	struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
4202 	__be32 status;
4203 	struct nfsd4_conn *conn;
4204 	struct nfsd4_session *session;
4205 	struct net *net = SVC_NET(rqstp);
4206 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4207 
4208 	if (!nfsd4_last_compound_op(rqstp))
4209 		return nfserr_not_only_op;
4210 	spin_lock(&nn->client_lock);
4211 	session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
4212 	spin_unlock(&nn->client_lock);
4213 	if (!session)
4214 		goto out_no_session;
4215 	status = nfserr_wrong_cred;
4216 	if (!nfsd4_mach_creds_match(session->se_client, rqstp))
4217 		goto out;
4218 	status = nfsd4_match_existing_connection(rqstp, session,
4219 			bcts->dir, &conn);
4220 	if (status == nfs_ok) {
4221 		if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
4222 				bcts->dir == NFS4_CDFC4_BACK)
4223 			conn->cn_flags |= NFS4_CDFC4_BACK;
4224 		nfsd4_probe_callback(session->se_client);
4225 		goto out;
4226 	}
4227 	if (status == nfserr_inval)
4228 		goto out;
4229 	status = nfsd4_map_bcts_dir(&bcts->dir);
4230 	if (status)
4231 		goto out;
4232 	conn = alloc_conn(rqstp, bcts->dir);
4233 	status = nfserr_jukebox;
4234 	if (!conn)
4235 		goto out;
4236 	nfsd4_init_conn(rqstp, conn, session);
4237 	status = nfs_ok;
4238 out:
4239 	nfsd4_put_session(session);
4240 out_no_session:
4241 	return status;
4242 }
4243 
nfsd4_compound_in_session(struct nfsd4_compound_state * cstate,struct nfs4_sessionid * sid)4244 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
4245 {
4246 	if (!cstate->session)
4247 		return false;
4248 	return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
4249 }
4250 
4251 __be32
nfsd4_destroy_session(struct svc_rqst * r,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4252 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
4253 		union nfsd4_op_u *u)
4254 {
4255 	struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
4256 	struct nfsd4_session *ses;
4257 	__be32 status;
4258 	int ref_held_by_me = 0;
4259 	struct net *net = SVC_NET(r);
4260 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4261 
4262 	status = nfserr_not_only_op;
4263 	if (nfsd4_compound_in_session(cstate, sessionid)) {
4264 		if (!nfsd4_last_compound_op(r))
4265 			goto out;
4266 		ref_held_by_me++;
4267 	}
4268 	dump_sessionid(__func__, sessionid);
4269 	spin_lock(&nn->client_lock);
4270 	ses = find_in_sessionid_hashtbl(sessionid, net, &status);
4271 	if (!ses)
4272 		goto out_client_lock;
4273 	status = nfserr_wrong_cred;
4274 	if (!nfsd4_mach_creds_match(ses->se_client, r))
4275 		goto out_put_session;
4276 	status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
4277 	if (status)
4278 		goto out_put_session;
4279 	unhash_session(ses);
4280 	spin_unlock(&nn->client_lock);
4281 
4282 	nfsd4_probe_callback_sync(ses->se_client);
4283 
4284 	spin_lock(&nn->client_lock);
4285 	status = nfs_ok;
4286 out_put_session:
4287 	nfsd4_put_session_locked(ses);
4288 out_client_lock:
4289 	spin_unlock(&nn->client_lock);
4290 out:
4291 	return status;
4292 }
4293 
nfsd4_sequence_check_conn(struct nfsd4_conn * new,struct nfsd4_session * ses)4294 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
4295 {
4296 	struct nfs4_client *clp = ses->se_client;
4297 	struct nfsd4_conn *c;
4298 	__be32 status = nfs_ok;
4299 	int ret;
4300 
4301 	spin_lock(&clp->cl_lock);
4302 	c = __nfsd4_find_conn(new->cn_xprt, ses);
4303 	if (c)
4304 		goto out_free;
4305 	status = nfserr_conn_not_bound_to_session;
4306 	if (clp->cl_mach_cred)
4307 		goto out_free;
4308 	__nfsd4_hash_conn(new, ses);
4309 	spin_unlock(&clp->cl_lock);
4310 	ret = nfsd4_register_conn(new);
4311 	if (ret)
4312 		/* oops; xprt is already down: */
4313 		nfsd4_conn_lost(&new->cn_xpt_user);
4314 	return nfs_ok;
4315 out_free:
4316 	spin_unlock(&clp->cl_lock);
4317 	free_conn(new);
4318 	return status;
4319 }
4320 
nfsd4_session_too_many_ops(struct svc_rqst * rqstp,struct nfsd4_session * session)4321 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
4322 {
4323 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
4324 
4325 	return args->opcnt > session->se_fchannel.maxops;
4326 }
4327 
nfsd4_request_too_big(struct svc_rqst * rqstp,struct nfsd4_session * session)4328 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
4329 				  struct nfsd4_session *session)
4330 {
4331 	struct xdr_buf *xb = &rqstp->rq_arg;
4332 
4333 	return xb->len > session->se_fchannel.maxreq_sz;
4334 }
4335 
replay_matches_cache(struct svc_rqst * rqstp,struct nfsd4_sequence * seq,struct nfsd4_slot * slot)4336 static bool replay_matches_cache(struct svc_rqst *rqstp,
4337 		 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
4338 {
4339 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
4340 
4341 	if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
4342 	    (bool)seq->cachethis)
4343 		return false;
4344 	/*
4345 	 * If there's an error then the reply can have fewer ops than
4346 	 * the call.
4347 	 */
4348 	if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
4349 		return false;
4350 	/*
4351 	 * But if we cached a reply with *more* ops than the call you're
4352 	 * sending us now, then this new call is clearly not really a
4353 	 * replay of the old one:
4354 	 */
4355 	if (slot->sl_opcnt > argp->opcnt)
4356 		return false;
4357 	/* This is the only check explicitly called by spec: */
4358 	if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
4359 		return false;
4360 	/*
4361 	 * There may be more comparisons we could actually do, but the
4362 	 * spec doesn't require us to catch every case where the calls
4363 	 * don't match (that would require caching the call as well as
4364 	 * the reply), so we don't bother.
4365 	 */
4366 	return true;
4367 }
4368 
4369 /*
4370  * Note that the response is constructed here both for the case
4371  * of a new SEQUENCE request and for a replayed SEQUENCE request.
4372  * We do not cache SEQUENCE responses as SEQUENCE is idempotent.
4373  */
nfsd4_construct_sequence_response(struct nfsd4_session * session,struct nfsd4_sequence * seq)4374 static void nfsd4_construct_sequence_response(struct nfsd4_session *session,
4375 					      struct nfsd4_sequence *seq)
4376 {
4377 	struct nfs4_client *clp = session->se_client;
4378 
4379 	seq->maxslots_response = max(session->se_target_maxslots,
4380 				     seq->maxslots);
4381 	seq->target_maxslots = session->se_target_maxslots;
4382 
4383 	switch (clp->cl_cb_state) {
4384 	case NFSD4_CB_DOWN:
4385 		seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
4386 		break;
4387 	case NFSD4_CB_FAULT:
4388 		seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4389 		break;
4390 	default:
4391 		seq->status_flags = 0;
4392 	}
4393 	if (!list_empty(&clp->cl_revoked))
4394 		seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4395 	if (atomic_read(&clp->cl_admin_revoked))
4396 		seq->status_flags |= SEQ4_STATUS_ADMIN_STATE_REVOKED;
4397 }
4398 
4399 __be32
nfsd4_sequence(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4400 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4401 		union nfsd4_op_u *u)
4402 {
4403 	struct nfsd4_sequence *seq = &u->sequence;
4404 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
4405 	struct xdr_stream *xdr = resp->xdr;
4406 	struct nfsd4_session *session;
4407 	struct nfs4_client *clp;
4408 	struct nfsd4_slot *slot;
4409 	struct nfsd4_conn *conn;
4410 	__be32 status;
4411 	int buflen;
4412 	struct net *net = SVC_NET(rqstp);
4413 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4414 
4415 	if (resp->opcnt != 1)
4416 		return nfserr_sequence_pos;
4417 
4418 	/*
4419 	 * Will be either used or freed by nfsd4_sequence_check_conn
4420 	 * below.
4421 	 */
4422 	conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
4423 	if (!conn)
4424 		return nfserr_jukebox;
4425 
4426 	spin_lock(&nn->client_lock);
4427 	session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
4428 	if (!session)
4429 		goto out_no_session;
4430 	clp = session->se_client;
4431 
4432 	status = nfserr_too_many_ops;
4433 	if (nfsd4_session_too_many_ops(rqstp, session))
4434 		goto out_put_session;
4435 
4436 	status = nfserr_req_too_big;
4437 	if (nfsd4_request_too_big(rqstp, session))
4438 		goto out_put_session;
4439 
4440 	status = nfserr_badslot;
4441 	if (seq->slotid >= session->se_fchannel.maxreqs)
4442 		goto out_put_session;
4443 
4444 	slot = xa_load(&session->se_slots, seq->slotid);
4445 	dprintk("%s: slotid %d\n", __func__, seq->slotid);
4446 
4447 	trace_nfsd_slot_seqid_sequence(clp, seq, slot);
4448 
4449 	nfsd4_construct_sequence_response(session, seq);
4450 
4451 	status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_flags);
4452 	if (status == nfserr_replay_cache) {
4453 		status = nfserr_seq_misordered;
4454 		if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
4455 			goto out_put_session;
4456 		status = nfserr_seq_false_retry;
4457 		if (!replay_matches_cache(rqstp, seq, slot))
4458 			goto out_put_session;
4459 		cstate->slot = slot;
4460 		cstate->session = session;
4461 		cstate->clp = clp;
4462 		/* Return the cached reply status and set cstate->status
4463 		 * for nfsd4_proc_compound processing */
4464 		status = nfsd4_replay_cache_entry(resp, seq);
4465 		cstate->status = nfserr_replay_cache;
4466 		goto out;
4467 	}
4468 	if (status)
4469 		goto out_put_session;
4470 
4471 	status = nfsd4_sequence_check_conn(conn, session);
4472 	conn = NULL;
4473 	if (status)
4474 		goto out_put_session;
4475 
4476 	if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
4477 	    slot->sl_generation == session->se_slot_gen &&
4478 	    seq->maxslots <= session->se_target_maxslots)
4479 		/* Client acknowledged our reduce maxreqs */
4480 		free_session_slots(session, session->se_target_maxslots);
4481 
4482 	buflen = (seq->cachethis) ?
4483 			session->se_fchannel.maxresp_cached :
4484 			session->se_fchannel.maxresp_sz;
4485 	status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
4486 				    nfserr_rep_too_big;
4487 	if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
4488 		goto out_put_session;
4489 	svc_reserve_auth(rqstp, buflen);
4490 
4491 	status = nfs_ok;
4492 	/* Success! accept new slot seqid */
4493 	slot->sl_seqid = seq->seqid;
4494 	slot->sl_flags &= ~NFSD4_SLOT_REUSED;
4495 	slot->sl_flags |= NFSD4_SLOT_INUSE;
4496 	slot->sl_generation = session->se_slot_gen;
4497 	if (seq->cachethis)
4498 		slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4499 	else
4500 		slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4501 
4502 	cstate->slot = slot;
4503 	cstate->session = session;
4504 	cstate->clp = clp;
4505 
4506 	/*
4507 	 * If the client ever uses the highest available slot,
4508 	 * gently try to allocate another 20%.  This allows
4509 	 * fairly quick growth without grossly over-shooting what
4510 	 * the client might use.
4511 	 */
4512 	if (seq->slotid == session->se_fchannel.maxreqs - 1 &&
4513 	    session->se_target_maxslots >= session->se_fchannel.maxreqs &&
4514 	    session->se_fchannel.maxreqs < NFSD_MAX_SLOTS_PER_SESSION) {
4515 		int s = session->se_fchannel.maxreqs;
4516 		int cnt = DIV_ROUND_UP(s, 5);
4517 		void *prev_slot;
4518 
4519 		do {
4520 			/*
4521 			 * GFP_NOWAIT both allows allocation under a
4522 			 * spinlock, and only succeeds if there is
4523 			 * plenty of memory.
4524 			 */
4525 			slot = nfsd4_alloc_slot(&session->se_fchannel, s,
4526 						GFP_NOWAIT);
4527 			prev_slot = xa_load(&session->se_slots, s);
4528 			if (xa_is_value(prev_slot) && slot) {
4529 				slot->sl_seqid = xa_to_value(prev_slot);
4530 				slot->sl_flags |= NFSD4_SLOT_REUSED;
4531 			}
4532 			if (slot &&
4533 			    !xa_is_err(xa_store(&session->se_slots, s, slot,
4534 						GFP_NOWAIT))) {
4535 				s += 1;
4536 				session->se_fchannel.maxreqs = s;
4537 				atomic_add(s - session->se_target_maxslots,
4538 					   &nfsd_total_target_slots);
4539 				session->se_target_maxslots = s;
4540 			} else {
4541 				kfree(slot);
4542 				slot = NULL;
4543 			}
4544 		} while (slot && --cnt > 0);
4545 	}
4546 
4547 out:
4548 	trace_nfsd_seq4_status(rqstp, seq);
4549 out_no_session:
4550 	if (conn)
4551 		free_conn(conn);
4552 	spin_unlock(&nn->client_lock);
4553 	return status;
4554 out_put_session:
4555 	nfsd4_put_session_locked(session);
4556 	goto out_no_session;
4557 }
4558 
4559 void
nfsd4_sequence_done(struct nfsd4_compoundres * resp)4560 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4561 {
4562 	struct nfsd4_compound_state *cs = &resp->cstate;
4563 
4564 	if (nfsd4_has_session(cs)) {
4565 		if (cs->status != nfserr_replay_cache) {
4566 			nfsd4_store_cache_entry(resp);
4567 			cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4568 		}
4569 		/* Drop session reference that was taken in nfsd4_sequence() */
4570 		nfsd4_put_session(cs->session);
4571 	} else if (cs->clp)
4572 		put_client_renew(cs->clp);
4573 }
4574 
4575 __be32
nfsd4_destroy_clientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4576 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4577 		struct nfsd4_compound_state *cstate,
4578 		union nfsd4_op_u *u)
4579 {
4580 	struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4581 	struct nfs4_client *conf, *unconf;
4582 	struct nfs4_client *clp = NULL;
4583 	__be32 status = 0;
4584 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4585 
4586 	spin_lock(&nn->client_lock);
4587 	unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4588 	conf = find_confirmed_client(&dc->clientid, true, nn);
4589 	WARN_ON_ONCE(conf && unconf);
4590 
4591 	if (conf) {
4592 		if (client_has_state(conf)) {
4593 			status = nfserr_clientid_busy;
4594 			goto out;
4595 		}
4596 		status = mark_client_expired_locked(conf);
4597 		if (status)
4598 			goto out;
4599 		clp = conf;
4600 	} else if (unconf)
4601 		clp = unconf;
4602 	else {
4603 		status = nfserr_stale_clientid;
4604 		goto out;
4605 	}
4606 	if (!nfsd4_mach_creds_match(clp, rqstp)) {
4607 		clp = NULL;
4608 		status = nfserr_wrong_cred;
4609 		goto out;
4610 	}
4611 	trace_nfsd_clid_destroyed(&clp->cl_clientid);
4612 	unhash_client_locked(clp);
4613 out:
4614 	spin_unlock(&nn->client_lock);
4615 	if (clp)
4616 		expire_client(clp);
4617 	return status;
4618 }
4619 
4620 __be32
nfsd4_reclaim_complete(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4621 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4622 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4623 {
4624 	struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4625 	struct nfs4_client *clp = cstate->clp;
4626 	__be32 status = 0;
4627 
4628 	if (rc->rca_one_fs) {
4629 		if (!cstate->current_fh.fh_dentry)
4630 			return nfserr_nofilehandle;
4631 		/*
4632 		 * We don't take advantage of the rca_one_fs case.
4633 		 * That's OK, it's optional, we can safely ignore it.
4634 		 */
4635 		return nfs_ok;
4636 	}
4637 
4638 	status = nfserr_complete_already;
4639 	if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4640 		goto out;
4641 
4642 	status = nfserr_stale_clientid;
4643 	if (is_client_expired(clp))
4644 		/*
4645 		 * The following error isn't really legal.
4646 		 * But we only get here if the client just explicitly
4647 		 * destroyed the client.  Surely it no longer cares what
4648 		 * error it gets back on an operation for the dead
4649 		 * client.
4650 		 */
4651 		goto out;
4652 
4653 	status = nfs_ok;
4654 	trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4655 	nfsd4_client_record_create(clp);
4656 	inc_reclaim_complete(clp);
4657 out:
4658 	return status;
4659 }
4660 
4661 __be32
nfsd4_setclientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4662 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4663 		  union nfsd4_op_u *u)
4664 {
4665 	struct nfsd4_setclientid *setclid = &u->setclientid;
4666 	struct xdr_netobj 	clname = setclid->se_name;
4667 	nfs4_verifier		clverifier = setclid->se_verf;
4668 	struct nfs4_client	*conf, *new;
4669 	struct nfs4_client	*unconf = NULL;
4670 	__be32 			status;
4671 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4672 
4673 	new = create_client(clname, rqstp, &clverifier);
4674 	if (new == NULL)
4675 		return nfserr_jukebox;
4676 	spin_lock(&nn->client_lock);
4677 	conf = find_confirmed_client_by_name(&clname, nn);
4678 	if (conf && client_has_state(conf)) {
4679 		status = nfserr_clid_inuse;
4680 		if (clp_used_exchangeid(conf))
4681 			goto out;
4682 		if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4683 			trace_nfsd_clid_cred_mismatch(conf, rqstp);
4684 			goto out;
4685 		}
4686 	}
4687 	unconf = find_unconfirmed_client_by_name(&clname, nn);
4688 	if (unconf)
4689 		unhash_client_locked(unconf);
4690 	if (conf) {
4691 		if (same_verf(&conf->cl_verifier, &clverifier)) {
4692 			copy_clid(new, conf);
4693 			gen_confirm(new, nn);
4694 		} else
4695 			trace_nfsd_clid_verf_mismatch(conf, rqstp,
4696 						      &clverifier);
4697 	} else
4698 		trace_nfsd_clid_fresh(new);
4699 	new->cl_minorversion = 0;
4700 	gen_callback(new, setclid, rqstp);
4701 	add_to_unconfirmed(new);
4702 	setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4703 	setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4704 	memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4705 	new = NULL;
4706 	status = nfs_ok;
4707 out:
4708 	spin_unlock(&nn->client_lock);
4709 	if (new)
4710 		free_client(new);
4711 	if (unconf) {
4712 		trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4713 		expire_client(unconf);
4714 	}
4715 	return status;
4716 }
4717 
4718 __be32
nfsd4_setclientid_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4719 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4720 			struct nfsd4_compound_state *cstate,
4721 			union nfsd4_op_u *u)
4722 {
4723 	struct nfsd4_setclientid_confirm *setclientid_confirm =
4724 			&u->setclientid_confirm;
4725 	struct nfs4_client *conf, *unconf;
4726 	struct nfs4_client *old = NULL;
4727 	nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4728 	clientid_t * clid = &setclientid_confirm->sc_clientid;
4729 	__be32 status;
4730 	struct nfsd_net	*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4731 
4732 	if (STALE_CLIENTID(clid, nn))
4733 		return nfserr_stale_clientid;
4734 
4735 	spin_lock(&nn->client_lock);
4736 	conf = find_confirmed_client(clid, false, nn);
4737 	unconf = find_unconfirmed_client(clid, false, nn);
4738 	/*
4739 	 * We try hard to give out unique clientid's, so if we get an
4740 	 * attempt to confirm the same clientid with a different cred,
4741 	 * the client may be buggy; this should never happen.
4742 	 *
4743 	 * Nevertheless, RFC 7530 recommends INUSE for this case:
4744 	 */
4745 	status = nfserr_clid_inuse;
4746 	if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4747 		trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4748 		goto out;
4749 	}
4750 	if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4751 		trace_nfsd_clid_cred_mismatch(conf, rqstp);
4752 		goto out;
4753 	}
4754 	if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4755 		if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4756 			status = nfs_ok;
4757 		} else
4758 			status = nfserr_stale_clientid;
4759 		goto out;
4760 	}
4761 	status = nfs_ok;
4762 	if (conf) {
4763 		if (get_client_locked(conf) == nfs_ok) {
4764 			old = unconf;
4765 			unhash_client_locked(old);
4766 			nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4767 		} else {
4768 			conf = NULL;
4769 		}
4770 	}
4771 
4772 	if (!conf) {
4773 		old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4774 		if (old) {
4775 			status = nfserr_clid_inuse;
4776 			if (client_has_state(old)
4777 					&& !same_creds(&unconf->cl_cred,
4778 							&old->cl_cred)) {
4779 				old = NULL;
4780 				goto out;
4781 			}
4782 			status = mark_client_expired_locked(old);
4783 			if (status) {
4784 				old = NULL;
4785 				goto out;
4786 			}
4787 			trace_nfsd_clid_replaced(&old->cl_clientid);
4788 		}
4789 		status = get_client_locked(unconf);
4790 		if (status != nfs_ok) {
4791 			old = NULL;
4792 			goto out;
4793 		}
4794 		move_to_confirmed(unconf);
4795 		conf = unconf;
4796 	}
4797 	spin_unlock(&nn->client_lock);
4798 	if (conf == unconf)
4799 		fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4800 	nfsd4_probe_callback(conf);
4801 	spin_lock(&nn->client_lock);
4802 	put_client_renew_locked(conf);
4803 out:
4804 	spin_unlock(&nn->client_lock);
4805 	if (old)
4806 		expire_client(old);
4807 	return status;
4808 }
4809 
nfsd4_alloc_file(void)4810 static struct nfs4_file *nfsd4_alloc_file(void)
4811 {
4812 	return kmem_cache_alloc(file_slab, GFP_KERNEL);
4813 }
4814 
4815 /* OPEN Share state helper functions */
4816 
nfsd4_file_init(const struct svc_fh * fh,struct nfs4_file * fp)4817 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
4818 {
4819 	refcount_set(&fp->fi_ref, 1);
4820 	spin_lock_init(&fp->fi_lock);
4821 	INIT_LIST_HEAD(&fp->fi_stateids);
4822 	INIT_LIST_HEAD(&fp->fi_delegations);
4823 	INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4824 	fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4825 	fp->fi_deleg_file = NULL;
4826 	fp->fi_rdeleg_file = NULL;
4827 	fp->fi_had_conflict = false;
4828 	fp->fi_share_deny = 0;
4829 	memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4830 	memset(fp->fi_access, 0, sizeof(fp->fi_access));
4831 	fp->fi_aliased = false;
4832 	fp->fi_inode = d_inode(fh->fh_dentry);
4833 #ifdef CONFIG_NFSD_PNFS
4834 	INIT_LIST_HEAD(&fp->fi_lo_states);
4835 	atomic_set(&fp->fi_lo_recalls, 0);
4836 #endif
4837 }
4838 
4839 void
nfsd4_free_slabs(void)4840 nfsd4_free_slabs(void)
4841 {
4842 	kmem_cache_destroy(client_slab);
4843 	kmem_cache_destroy(openowner_slab);
4844 	kmem_cache_destroy(lockowner_slab);
4845 	kmem_cache_destroy(file_slab);
4846 	kmem_cache_destroy(stateid_slab);
4847 	kmem_cache_destroy(deleg_slab);
4848 	kmem_cache_destroy(odstate_slab);
4849 }
4850 
4851 int
nfsd4_init_slabs(void)4852 nfsd4_init_slabs(void)
4853 {
4854 	client_slab = KMEM_CACHE(nfs4_client, 0);
4855 	if (client_slab == NULL)
4856 		goto out;
4857 	openowner_slab = KMEM_CACHE(nfs4_openowner, 0);
4858 	if (openowner_slab == NULL)
4859 		goto out_free_client_slab;
4860 	lockowner_slab = KMEM_CACHE(nfs4_lockowner, 0);
4861 	if (lockowner_slab == NULL)
4862 		goto out_free_openowner_slab;
4863 	file_slab = KMEM_CACHE(nfs4_file, 0);
4864 	if (file_slab == NULL)
4865 		goto out_free_lockowner_slab;
4866 	stateid_slab = KMEM_CACHE(nfs4_ol_stateid, 0);
4867 	if (stateid_slab == NULL)
4868 		goto out_free_file_slab;
4869 	deleg_slab = KMEM_CACHE(nfs4_delegation, 0);
4870 	if (deleg_slab == NULL)
4871 		goto out_free_stateid_slab;
4872 	odstate_slab = KMEM_CACHE(nfs4_clnt_odstate, 0);
4873 	if (odstate_slab == NULL)
4874 		goto out_free_deleg_slab;
4875 	return 0;
4876 
4877 out_free_deleg_slab:
4878 	kmem_cache_destroy(deleg_slab);
4879 out_free_stateid_slab:
4880 	kmem_cache_destroy(stateid_slab);
4881 out_free_file_slab:
4882 	kmem_cache_destroy(file_slab);
4883 out_free_lockowner_slab:
4884 	kmem_cache_destroy(lockowner_slab);
4885 out_free_openowner_slab:
4886 	kmem_cache_destroy(openowner_slab);
4887 out_free_client_slab:
4888 	kmem_cache_destroy(client_slab);
4889 out:
4890 	return -ENOMEM;
4891 }
4892 
4893 static unsigned long
nfsd4_state_shrinker_count(struct shrinker * shrink,struct shrink_control * sc)4894 nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
4895 {
4896 	struct nfsd_net *nn = shrink->private_data;
4897 	long count;
4898 
4899 	count = atomic_read(&nn->nfsd_courtesy_clients);
4900 	if (!count)
4901 		count = atomic_long_read(&num_delegations);
4902 	if (count)
4903 		queue_work(laundry_wq, &nn->nfsd_shrinker_work);
4904 	return (unsigned long)count;
4905 }
4906 
4907 static unsigned long
nfsd4_state_shrinker_scan(struct shrinker * shrink,struct shrink_control * sc)4908 nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
4909 {
4910 	return SHRINK_STOP;
4911 }
4912 
4913 void
nfsd4_init_leases_net(struct nfsd_net * nn)4914 nfsd4_init_leases_net(struct nfsd_net *nn)
4915 {
4916 	struct sysinfo si;
4917 	u64 max_clients;
4918 
4919 	nn->nfsd4_lease = 90;	/* default lease time */
4920 	nn->nfsd4_grace = 90;
4921 	nn->somebody_reclaimed = false;
4922 	nn->track_reclaim_completes = false;
4923 	nn->clverifier_counter = get_random_u32();
4924 	nn->clientid_base = get_random_u32();
4925 	nn->clientid_counter = nn->clientid_base + 1;
4926 	nn->s2s_cp_cl_id = nn->clientid_counter++;
4927 
4928 	atomic_set(&nn->nfs4_client_count, 0);
4929 	si_meminfo(&si);
4930 	max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4931 	max_clients *= NFS4_CLIENTS_PER_GB;
4932 	nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4933 
4934 	atomic_set(&nn->nfsd_courtesy_clients, 0);
4935 }
4936 
4937 enum rp_lock {
4938 	RP_UNLOCKED,
4939 	RP_LOCKED,
4940 	RP_UNHASHED,
4941 };
4942 
init_nfs4_replay(struct nfs4_replay * rp)4943 static void init_nfs4_replay(struct nfs4_replay *rp)
4944 {
4945 	rp->rp_status = nfserr_serverfault;
4946 	rp->rp_buflen = 0;
4947 	rp->rp_buf = rp->rp_ibuf;
4948 	rp->rp_locked = RP_UNLOCKED;
4949 }
4950 
nfsd4_cstate_assign_replay(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so)4951 static int nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4952 				      struct nfs4_stateowner *so)
4953 {
4954 	if (!nfsd4_has_session(cstate)) {
4955 		wait_var_event(&so->so_replay.rp_locked,
4956 			       cmpxchg(&so->so_replay.rp_locked,
4957 				       RP_UNLOCKED, RP_LOCKED) != RP_LOCKED);
4958 		if (so->so_replay.rp_locked == RP_UNHASHED)
4959 			return -EAGAIN;
4960 		cstate->replay_owner = nfs4_get_stateowner(so);
4961 	}
4962 	return 0;
4963 }
4964 
nfsd4_cstate_clear_replay(struct nfsd4_compound_state * cstate)4965 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4966 {
4967 	struct nfs4_stateowner *so = cstate->replay_owner;
4968 
4969 	if (so != NULL) {
4970 		cstate->replay_owner = NULL;
4971 		store_release_wake_up(&so->so_replay.rp_locked, RP_UNLOCKED);
4972 		nfs4_put_stateowner(so);
4973 	}
4974 }
4975 
alloc_stateowner(struct kmem_cache * slab,struct xdr_netobj * owner,struct nfs4_client * clp)4976 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4977 {
4978 	struct nfs4_stateowner *sop;
4979 
4980 	sop = kmem_cache_alloc(slab, GFP_KERNEL);
4981 	if (!sop)
4982 		return NULL;
4983 
4984 	xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4985 	if (!sop->so_owner.data) {
4986 		kmem_cache_free(slab, sop);
4987 		return NULL;
4988 	}
4989 
4990 	INIT_LIST_HEAD(&sop->so_stateids);
4991 	sop->so_client = clp;
4992 	init_nfs4_replay(&sop->so_replay);
4993 	atomic_set(&sop->so_count, 1);
4994 	return sop;
4995 }
4996 
hash_openowner(struct nfs4_openowner * oo,struct nfs4_client * clp,unsigned int strhashval)4997 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4998 {
4999 	lockdep_assert_held(&clp->cl_lock);
5000 
5001 	list_add(&oo->oo_owner.so_strhash,
5002 		 &clp->cl_ownerstr_hashtbl[strhashval]);
5003 	list_add(&oo->oo_perclient, &clp->cl_openowners);
5004 }
5005 
nfs4_unhash_openowner(struct nfs4_stateowner * so)5006 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
5007 {
5008 	unhash_openowner_locked(openowner(so));
5009 }
5010 
nfs4_free_openowner(struct nfs4_stateowner * so)5011 static void nfs4_free_openowner(struct nfs4_stateowner *so)
5012 {
5013 	struct nfs4_openowner *oo = openowner(so);
5014 
5015 	kmem_cache_free(openowner_slab, oo);
5016 }
5017 
5018 static const struct nfs4_stateowner_operations openowner_ops = {
5019 	.so_unhash =	nfs4_unhash_openowner,
5020 	.so_free =	nfs4_free_openowner,
5021 };
5022 
5023 static struct nfs4_ol_stateid *
nfsd4_find_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)5024 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
5025 {
5026 	struct nfs4_ol_stateid *local, *ret = NULL;
5027 	struct nfs4_openowner *oo = open->op_openowner;
5028 
5029 	lockdep_assert_held(&fp->fi_lock);
5030 
5031 	list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
5032 		/* ignore lock owners */
5033 		if (local->st_stateowner->so_is_open_owner == 0)
5034 			continue;
5035 		if (local->st_stateowner != &oo->oo_owner)
5036 			continue;
5037 		if (local->st_stid.sc_type == SC_TYPE_OPEN &&
5038 		    !local->st_stid.sc_status) {
5039 			ret = local;
5040 			refcount_inc(&ret->st_stid.sc_count);
5041 			break;
5042 		}
5043 	}
5044 	return ret;
5045 }
5046 
nfsd4_drop_revoked_stid(struct nfs4_stid * s)5047 static void nfsd4_drop_revoked_stid(struct nfs4_stid *s)
5048 	__releases(&s->sc_client->cl_lock)
5049 {
5050 	struct nfs4_client *cl = s->sc_client;
5051 	LIST_HEAD(reaplist);
5052 	struct nfs4_ol_stateid *stp;
5053 	struct nfs4_delegation *dp;
5054 	bool unhashed;
5055 
5056 	switch (s->sc_type) {
5057 	case SC_TYPE_OPEN:
5058 		stp = openlockstateid(s);
5059 		if (unhash_open_stateid(stp, &reaplist))
5060 			put_ol_stateid_locked(stp, &reaplist);
5061 		spin_unlock(&cl->cl_lock);
5062 		free_ol_stateid_reaplist(&reaplist);
5063 		break;
5064 	case SC_TYPE_LOCK:
5065 		stp = openlockstateid(s);
5066 		unhashed = unhash_lock_stateid(stp);
5067 		spin_unlock(&cl->cl_lock);
5068 		if (unhashed)
5069 			nfs4_put_stid(s);
5070 		break;
5071 	case SC_TYPE_DELEG:
5072 		dp = delegstateid(s);
5073 		list_del_init(&dp->dl_recall_lru);
5074 		spin_unlock(&cl->cl_lock);
5075 		nfs4_put_stid(s);
5076 		break;
5077 	default:
5078 		spin_unlock(&cl->cl_lock);
5079 	}
5080 }
5081 
nfsd40_drop_revoked_stid(struct nfs4_client * cl,stateid_t * stid)5082 static void nfsd40_drop_revoked_stid(struct nfs4_client *cl,
5083 				    stateid_t *stid)
5084 {
5085 	/* NFSv4.0 has no way for the client to tell the server
5086 	 * that it can forget an admin-revoked stateid.
5087 	 * So we keep it around until the first time that the
5088 	 * client uses it, and drop it the first time
5089 	 * nfserr_admin_revoked is returned.
5090 	 * For v4.1 and later we wait until explicitly told
5091 	 * to free the stateid.
5092 	 */
5093 	if (cl->cl_minorversion == 0) {
5094 		struct nfs4_stid *st;
5095 
5096 		spin_lock(&cl->cl_lock);
5097 		st = find_stateid_locked(cl, stid);
5098 		if (st)
5099 			nfsd4_drop_revoked_stid(st);
5100 		else
5101 			spin_unlock(&cl->cl_lock);
5102 	}
5103 }
5104 
5105 static __be32
nfsd4_verify_open_stid(struct nfs4_stid * s)5106 nfsd4_verify_open_stid(struct nfs4_stid *s)
5107 {
5108 	__be32 ret = nfs_ok;
5109 
5110 	if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
5111 		ret = nfserr_admin_revoked;
5112 	else if (s->sc_status & SC_STATUS_REVOKED)
5113 		ret = nfserr_deleg_revoked;
5114 	else if (s->sc_status & SC_STATUS_CLOSED)
5115 		ret = nfserr_bad_stateid;
5116 	return ret;
5117 }
5118 
5119 /* Lock the stateid st_mutex, and deal with races with CLOSE */
5120 static __be32
nfsd4_lock_ol_stateid(struct nfs4_ol_stateid * stp)5121 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
5122 {
5123 	__be32 ret;
5124 
5125 	mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
5126 	ret = nfsd4_verify_open_stid(&stp->st_stid);
5127 	if (ret == nfserr_admin_revoked)
5128 		nfsd40_drop_revoked_stid(stp->st_stid.sc_client,
5129 					&stp->st_stid.sc_stateid);
5130 
5131 	if (ret != nfs_ok)
5132 		mutex_unlock(&stp->st_mutex);
5133 	return ret;
5134 }
5135 
5136 static struct nfs4_ol_stateid *
nfsd4_find_and_lock_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)5137 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
5138 {
5139 	struct nfs4_ol_stateid *stp;
5140 	for (;;) {
5141 		spin_lock(&fp->fi_lock);
5142 		stp = nfsd4_find_existing_open(fp, open);
5143 		spin_unlock(&fp->fi_lock);
5144 		if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
5145 			break;
5146 		nfs4_put_stid(&stp->st_stid);
5147 	}
5148 	return stp;
5149 }
5150 
5151 static struct nfs4_openowner *
find_or_alloc_open_stateowner(unsigned int strhashval,struct nfsd4_open * open,struct nfsd4_compound_state * cstate)5152 find_or_alloc_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
5153 			      struct nfsd4_compound_state *cstate)
5154 {
5155 	struct nfs4_client *clp = cstate->clp;
5156 	struct nfs4_openowner *oo, *new = NULL;
5157 
5158 retry:
5159 	spin_lock(&clp->cl_lock);
5160 	oo = find_openstateowner_str(strhashval, open, clp);
5161 	if (!oo && new) {
5162 		hash_openowner(new, clp, strhashval);
5163 		spin_unlock(&clp->cl_lock);
5164 		return new;
5165 	}
5166 	spin_unlock(&clp->cl_lock);
5167 
5168 	if (oo && !(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5169 		/* Replace unconfirmed owners without checking for replay. */
5170 		release_openowner(oo);
5171 		oo = NULL;
5172 	}
5173 	if (oo) {
5174 		if (new)
5175 			nfs4_free_stateowner(&new->oo_owner);
5176 		return oo;
5177 	}
5178 
5179 	new = alloc_stateowner(openowner_slab, &open->op_owner, clp);
5180 	if (!new)
5181 		return NULL;
5182 	new->oo_owner.so_ops = &openowner_ops;
5183 	new->oo_owner.so_is_open_owner = 1;
5184 	new->oo_owner.so_seqid = open->op_seqid;
5185 	new->oo_flags = 0;
5186 	if (nfsd4_has_session(cstate))
5187 		new->oo_flags |= NFS4_OO_CONFIRMED;
5188 	new->oo_time = 0;
5189 	new->oo_last_closed_stid = NULL;
5190 	INIT_LIST_HEAD(&new->oo_close_lru);
5191 	goto retry;
5192 }
5193 
5194 static struct nfs4_ol_stateid *
init_open_stateid(struct nfs4_file * fp,struct nfsd4_open * open)5195 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
5196 {
5197 
5198 	struct nfs4_openowner *oo = open->op_openowner;
5199 	struct nfs4_ol_stateid *retstp = NULL;
5200 	struct nfs4_ol_stateid *stp;
5201 
5202 	stp = open->op_stp;
5203 	/* We are moving these outside of the spinlocks to avoid the warnings */
5204 	mutex_init(&stp->st_mutex);
5205 	mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
5206 
5207 retry:
5208 	spin_lock(&oo->oo_owner.so_client->cl_lock);
5209 	spin_lock(&fp->fi_lock);
5210 
5211 	if (nfs4_openowner_unhashed(oo)) {
5212 		mutex_unlock(&stp->st_mutex);
5213 		stp = NULL;
5214 		goto out_unlock;
5215 	}
5216 
5217 	retstp = nfsd4_find_existing_open(fp, open);
5218 	if (retstp)
5219 		goto out_unlock;
5220 
5221 	open->op_stp = NULL;
5222 	refcount_inc(&stp->st_stid.sc_count);
5223 	stp->st_stid.sc_type = SC_TYPE_OPEN;
5224 	INIT_LIST_HEAD(&stp->st_locks);
5225 	stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
5226 	get_nfs4_file(fp);
5227 	stp->st_stid.sc_file = fp;
5228 	stp->st_access_bmap = 0;
5229 	stp->st_deny_bmap = 0;
5230 	stp->st_openstp = NULL;
5231 	list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
5232 	list_add(&stp->st_perfile, &fp->fi_stateids);
5233 
5234 out_unlock:
5235 	spin_unlock(&fp->fi_lock);
5236 	spin_unlock(&oo->oo_owner.so_client->cl_lock);
5237 	if (retstp) {
5238 		/* Handle races with CLOSE */
5239 		if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5240 			nfs4_put_stid(&retstp->st_stid);
5241 			goto retry;
5242 		}
5243 		/* To keep mutex tracking happy */
5244 		mutex_unlock(&stp->st_mutex);
5245 		stp = retstp;
5246 	}
5247 	return stp;
5248 }
5249 
5250 /*
5251  * In the 4.0 case we need to keep the owners around a little while to handle
5252  * CLOSE replay. We still do need to release any file access that is held by
5253  * them before returning however.
5254  */
5255 static void
move_to_close_lru(struct nfs4_ol_stateid * s,struct net * net)5256 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
5257 {
5258 	struct nfs4_ol_stateid *last;
5259 	struct nfs4_openowner *oo = openowner(s->st_stateowner);
5260 	struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
5261 						nfsd_net_id);
5262 
5263 	dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
5264 
5265 	/*
5266 	 * We know that we hold one reference via nfsd4_close, and another
5267 	 * "persistent" reference for the client. If the refcount is higher
5268 	 * than 2, then there are still calls in progress that are using this
5269 	 * stateid. We can't put the sc_file reference until they are finished.
5270 	 * Wait for the refcount to drop to 2. Since it has been unhashed,
5271 	 * there should be no danger of the refcount going back up again at
5272 	 * this point.
5273 	 * Some threads with a reference might be waiting for rp_locked,
5274 	 * so tell them to stop waiting.
5275 	 */
5276 	store_release_wake_up(&oo->oo_owner.so_replay.rp_locked, RP_UNHASHED);
5277 	wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
5278 
5279 	release_all_access(s);
5280 	if (s->st_stid.sc_file) {
5281 		put_nfs4_file(s->st_stid.sc_file);
5282 		s->st_stid.sc_file = NULL;
5283 	}
5284 
5285 	spin_lock(&nn->client_lock);
5286 	last = oo->oo_last_closed_stid;
5287 	oo->oo_last_closed_stid = s;
5288 	list_move_tail(&oo->oo_close_lru, &nn->close_lru);
5289 	oo->oo_time = ktime_get_boottime_seconds();
5290 	spin_unlock(&nn->client_lock);
5291 	if (last)
5292 		nfs4_put_stid(&last->st_stid);
5293 }
5294 
5295 static noinline_for_stack struct nfs4_file *
nfsd4_file_hash_lookup(const struct svc_fh * fhp)5296 nfsd4_file_hash_lookup(const struct svc_fh *fhp)
5297 {
5298 	struct inode *inode = d_inode(fhp->fh_dentry);
5299 	struct rhlist_head *tmp, *list;
5300 	struct nfs4_file *fi;
5301 
5302 	rcu_read_lock();
5303 	list = rhltable_lookup(&nfs4_file_rhltable, &inode,
5304 			       nfs4_file_rhash_params);
5305 	rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
5306 		if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
5307 			if (refcount_inc_not_zero(&fi->fi_ref)) {
5308 				rcu_read_unlock();
5309 				return fi;
5310 			}
5311 		}
5312 	}
5313 	rcu_read_unlock();
5314 	return NULL;
5315 }
5316 
5317 /*
5318  * On hash insertion, identify entries with the same inode but
5319  * distinct filehandles. They will all be on the list returned
5320  * by rhltable_lookup().
5321  *
5322  * inode->i_lock prevents racing insertions from adding an entry
5323  * for the same inode/fhp pair twice.
5324  */
5325 static noinline_for_stack struct nfs4_file *
nfsd4_file_hash_insert(struct nfs4_file * new,const struct svc_fh * fhp)5326 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
5327 {
5328 	struct inode *inode = d_inode(fhp->fh_dentry);
5329 	struct rhlist_head *tmp, *list;
5330 	struct nfs4_file *ret = NULL;
5331 	bool alias_found = false;
5332 	struct nfs4_file *fi;
5333 	int err;
5334 
5335 	rcu_read_lock();
5336 	spin_lock(&inode->i_lock);
5337 
5338 	list = rhltable_lookup(&nfs4_file_rhltable, &inode,
5339 			       nfs4_file_rhash_params);
5340 	rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
5341 		if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
5342 			if (refcount_inc_not_zero(&fi->fi_ref))
5343 				ret = fi;
5344 		} else
5345 			fi->fi_aliased = alias_found = true;
5346 	}
5347 	if (ret)
5348 		goto out_unlock;
5349 
5350 	nfsd4_file_init(fhp, new);
5351 	err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist,
5352 			      nfs4_file_rhash_params);
5353 	if (err)
5354 		goto out_unlock;
5355 
5356 	new->fi_aliased = alias_found;
5357 	ret = new;
5358 
5359 out_unlock:
5360 	spin_unlock(&inode->i_lock);
5361 	rcu_read_unlock();
5362 	return ret;
5363 }
5364 
nfsd4_file_hash_remove(struct nfs4_file * fi)5365 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
5366 {
5367 	rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist,
5368 			nfs4_file_rhash_params);
5369 }
5370 
5371 /*
5372  * Called to check deny when READ with all zero stateid or
5373  * WRITE with all zero or all one stateid
5374  */
5375 static __be32
nfs4_share_conflict(struct svc_fh * current_fh,unsigned int deny_type)5376 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
5377 {
5378 	struct nfs4_file *fp;
5379 	__be32 ret = nfs_ok;
5380 
5381 	fp = nfsd4_file_hash_lookup(current_fh);
5382 	if (!fp)
5383 		return ret;
5384 
5385 	/* Check for conflicting share reservations */
5386 	spin_lock(&fp->fi_lock);
5387 	if (fp->fi_share_deny & deny_type)
5388 		ret = nfserr_locked;
5389 	spin_unlock(&fp->fi_lock);
5390 	put_nfs4_file(fp);
5391 	return ret;
5392 }
5393 
nfsd4_deleg_present(const struct inode * inode)5394 static bool nfsd4_deleg_present(const struct inode *inode)
5395 {
5396 	struct file_lock_context *ctx = locks_inode_context(inode);
5397 
5398 	return ctx && !list_empty_careful(&ctx->flc_lease);
5399 }
5400 
5401 /**
5402  * nfsd_wait_for_delegreturn - wait for delegations to be returned
5403  * @rqstp: the RPC transaction being executed
5404  * @inode: in-core inode of the file being waited for
5405  *
5406  * The timeout prevents deadlock if all nfsd threads happen to be
5407  * tied up waiting for returning delegations.
5408  *
5409  * Return values:
5410  *   %true: delegation was returned
5411  *   %false: timed out waiting for delegreturn
5412  */
nfsd_wait_for_delegreturn(struct svc_rqst * rqstp,struct inode * inode)5413 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
5414 {
5415 	long __maybe_unused timeo;
5416 
5417 	timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
5418 				       NFSD_DELEGRETURN_TIMEOUT);
5419 	trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
5420 	return timeo > 0;
5421 }
5422 
nfsd4_cb_recall_prepare(struct nfsd4_callback * cb)5423 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
5424 {
5425 	struct nfs4_delegation *dp = cb_to_delegation(cb);
5426 	struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
5427 					  nfsd_net_id);
5428 
5429 	block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
5430 
5431 	/*
5432 	 * We can't do this in nfsd_break_deleg_cb because it is
5433 	 * already holding inode->i_lock.
5434 	 *
5435 	 * If the dl_time != 0, then we know that it has already been
5436 	 * queued for a lease break. Don't queue it again.
5437 	 */
5438 	spin_lock(&nn->deleg_lock);
5439 	if (delegation_hashed(dp) && dp->dl_time == 0) {
5440 		dp->dl_time = ktime_get_boottime_seconds();
5441 		list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
5442 	}
5443 	spin_unlock(&nn->deleg_lock);
5444 }
5445 
nfsd4_cb_recall_done(struct nfsd4_callback * cb,struct rpc_task * task)5446 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
5447 		struct rpc_task *task)
5448 {
5449 	struct nfs4_delegation *dp = cb_to_delegation(cb);
5450 
5451 	trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
5452 
5453 	if (dp->dl_stid.sc_status)
5454 		/* CLOSED or REVOKED */
5455 		return 1;
5456 
5457 	switch (task->tk_status) {
5458 	case 0:
5459 		return 1;
5460 	case -NFS4ERR_DELAY:
5461 		rpc_delay(task, 2 * HZ);
5462 		return 0;
5463 	case -EBADHANDLE:
5464 	case -NFS4ERR_BAD_STATEID:
5465 		/*
5466 		 * Race: client probably got cb_recall before open reply
5467 		 * granting delegation.
5468 		 */
5469 		if (dp->dl_retries--) {
5470 			rpc_delay(task, 2 * HZ);
5471 			return 0;
5472 		}
5473 		fallthrough;
5474 	default:
5475 		return 1;
5476 	}
5477 }
5478 
nfsd4_cb_recall_release(struct nfsd4_callback * cb)5479 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
5480 {
5481 	struct nfs4_delegation *dp = cb_to_delegation(cb);
5482 
5483 	nfs4_put_stid(&dp->dl_stid);
5484 }
5485 
5486 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
5487 	.prepare	= nfsd4_cb_recall_prepare,
5488 	.done		= nfsd4_cb_recall_done,
5489 	.release	= nfsd4_cb_recall_release,
5490 	.opcode		= OP_CB_RECALL,
5491 };
5492 
nfsd_break_one_deleg(struct nfs4_delegation * dp)5493 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
5494 {
5495 	bool queued;
5496 
5497 	if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &dp->dl_recall.cb_flags))
5498 		return;
5499 
5500 	/*
5501 	 * We're assuming the state code never drops its reference
5502 	 * without first removing the lease.  Since we're in this lease
5503 	 * callback (and since the lease code is serialized by the
5504 	 * flc_lock) we know the server hasn't removed the lease yet, and
5505 	 * we know it's safe to take a reference.
5506 	 */
5507 	refcount_inc(&dp->dl_stid.sc_count);
5508 	queued = nfsd4_run_cb(&dp->dl_recall);
5509 	WARN_ON_ONCE(!queued);
5510 	if (!queued)
5511 		refcount_dec(&dp->dl_stid.sc_count);
5512 }
5513 
5514 /* Called from break_lease() with flc_lock held. */
5515 static bool
nfsd_break_deleg_cb(struct file_lease * fl)5516 nfsd_break_deleg_cb(struct file_lease *fl)
5517 {
5518 	struct nfs4_delegation *dp = (struct nfs4_delegation *) fl->c.flc_owner;
5519 	struct nfs4_file *fp = dp->dl_stid.sc_file;
5520 	struct nfs4_client *clp = dp->dl_stid.sc_client;
5521 	struct nfsd_net *nn;
5522 
5523 	trace_nfsd_cb_recall(&dp->dl_stid);
5524 
5525 	dp->dl_recalled = true;
5526 	atomic_inc(&clp->cl_delegs_in_recall);
5527 	if (try_to_expire_client(clp)) {
5528 		nn = net_generic(clp->net, nfsd_net_id);
5529 		mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
5530 	}
5531 
5532 	/*
5533 	 * We don't want the locks code to timeout the lease for us;
5534 	 * we'll remove it ourself if a delegation isn't returned
5535 	 * in time:
5536 	 */
5537 	fl->fl_break_time = 0;
5538 
5539 	fp->fi_had_conflict = true;
5540 	nfsd_break_one_deleg(dp);
5541 	return false;
5542 }
5543 
5544 /**
5545  * nfsd_breaker_owns_lease - Check if lease conflict was resolved
5546  * @fl: Lock state to check
5547  *
5548  * Return values:
5549  *   %true: Lease conflict was resolved
5550  *   %false: Lease conflict was not resolved.
5551  */
nfsd_breaker_owns_lease(struct file_lease * fl)5552 static bool nfsd_breaker_owns_lease(struct file_lease *fl)
5553 {
5554 	struct nfs4_delegation *dl = fl->c.flc_owner;
5555 	struct nfsd_thread_local_info *ntli;
5556 	struct svc_rqst *rqst;
5557 	struct nfs4_client *clp;
5558 
5559 	rqst = nfsd_current_rqst();
5560 	if (!nfsd_v4client(rqst))
5561 		return false;
5562 	ntli = rqst->rq_private;
5563 	clp = *ntli->ntli_lease_breaker;
5564 	return dl->dl_stid.sc_client == clp;
5565 }
5566 
5567 static int
nfsd_change_deleg_cb(struct file_lease * onlist,int arg,struct list_head * dispose)5568 nfsd_change_deleg_cb(struct file_lease *onlist, int arg,
5569 		     struct list_head *dispose)
5570 {
5571 	struct nfs4_delegation *dp = (struct nfs4_delegation *) onlist->c.flc_owner;
5572 	struct nfs4_client *clp = dp->dl_stid.sc_client;
5573 
5574 	if (arg & F_UNLCK) {
5575 		if (dp->dl_recalled)
5576 			atomic_dec(&clp->cl_delegs_in_recall);
5577 		return lease_modify(onlist, arg, dispose);
5578 	} else
5579 		return -EAGAIN;
5580 }
5581 
5582 /**
5583  *  nfsd4_deleg_lm_open_conflict - see if the given file points to an inode that has
5584  *				   an existing open that would conflict with the
5585  *				   desired lease.
5586  * @filp:	file to check
5587  * @arg:	type of lease that we're trying to acquire
5588  *
5589  * The kernel will call into this operation to determine whether there
5590  * are conflicting opens that may prevent the deleg from being granted.
5591  * For nfsd, that check is done at a higher level, so this trivially
5592  * returns 0.
5593  */
5594 static int
nfsd4_deleg_lm_open_conflict(struct file * filp,int arg)5595 nfsd4_deleg_lm_open_conflict(struct file *filp, int arg)
5596 {
5597 	return 0;
5598 }
5599 
5600 static const struct lease_manager_operations nfsd_lease_mng_ops = {
5601 	.lm_breaker_owns_lease = nfsd_breaker_owns_lease,
5602 	.lm_break = nfsd_break_deleg_cb,
5603 	.lm_change = nfsd_change_deleg_cb,
5604 	.lm_open_conflict = nfsd4_deleg_lm_open_conflict,
5605 };
5606 
nfsd4_check_seqid(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so,u32 seqid)5607 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
5608 {
5609 	if (nfsd4_has_session(cstate))
5610 		return nfs_ok;
5611 	if (seqid == so->so_seqid - 1)
5612 		return nfserr_replay_me;
5613 	if (seqid == so->so_seqid)
5614 		return nfs_ok;
5615 	return nfserr_bad_seqid;
5616 }
5617 
lookup_clientid(clientid_t * clid,bool sessions,struct nfsd_net * nn)5618 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
5619 						struct nfsd_net *nn)
5620 {
5621 	struct nfs4_client *found;
5622 
5623 	spin_lock(&nn->client_lock);
5624 	found = find_confirmed_client(clid, sessions, nn);
5625 	if (found)
5626 		atomic_inc(&found->cl_rpc_users);
5627 	spin_unlock(&nn->client_lock);
5628 	return found;
5629 }
5630 
set_client(clientid_t * clid,struct nfsd4_compound_state * cstate,struct nfsd_net * nn)5631 static __be32 set_client(clientid_t *clid,
5632 		struct nfsd4_compound_state *cstate,
5633 		struct nfsd_net *nn)
5634 {
5635 	if (cstate->clp) {
5636 		if (!same_clid(&cstate->clp->cl_clientid, clid))
5637 			return nfserr_stale_clientid;
5638 		return nfs_ok;
5639 	}
5640 	if (STALE_CLIENTID(clid, nn))
5641 		return nfserr_stale_clientid;
5642 	/*
5643 	 * We're in the 4.0 case (otherwise the SEQUENCE op would have
5644 	 * set cstate->clp), so session = false:
5645 	 */
5646 	cstate->clp = lookup_clientid(clid, false, nn);
5647 	if (!cstate->clp)
5648 		return nfserr_expired;
5649 	return nfs_ok;
5650 }
5651 
5652 __be32
nfsd4_process_open1(struct nfsd4_compound_state * cstate,struct nfsd4_open * open,struct nfsd_net * nn)5653 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
5654 		    struct nfsd4_open *open, struct nfsd_net *nn)
5655 {
5656 	clientid_t *clientid = &open->op_clientid;
5657 	struct nfs4_client *clp = NULL;
5658 	unsigned int strhashval;
5659 	struct nfs4_openowner *oo = NULL;
5660 	__be32 status;
5661 
5662 	/*
5663 	 * In case we need it later, after we've already created the
5664 	 * file and don't want to risk a further failure:
5665 	 */
5666 	open->op_file = nfsd4_alloc_file();
5667 	if (open->op_file == NULL)
5668 		return nfserr_jukebox;
5669 
5670 	status = set_client(clientid, cstate, nn);
5671 	if (status)
5672 		return status;
5673 	clp = cstate->clp;
5674 
5675 	strhashval = ownerstr_hashval(&open->op_owner);
5676 retry:
5677 	oo = find_or_alloc_open_stateowner(strhashval, open, cstate);
5678 	open->op_openowner = oo;
5679 	if (!oo)
5680 		return nfserr_jukebox;
5681 	if (nfsd4_cstate_assign_replay(cstate, &oo->oo_owner) == -EAGAIN) {
5682 		nfs4_put_stateowner(&oo->oo_owner);
5683 		goto retry;
5684 	}
5685 	status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5686 	if (status)
5687 		return status;
5688 
5689 	open->op_stp = nfs4_alloc_open_stateid(clp);
5690 	if (!open->op_stp)
5691 		return nfserr_jukebox;
5692 
5693 	if (nfsd4_has_session(cstate) &&
5694 	    (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5695 		open->op_odstate = alloc_clnt_odstate(clp);
5696 		if (!open->op_odstate)
5697 			return nfserr_jukebox;
5698 	}
5699 
5700 	return nfs_ok;
5701 }
5702 
5703 static inline __be32
nfs4_check_delegmode(struct nfs4_delegation * dp,int flags)5704 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5705 {
5706 	if (!(flags & RD_STATE) && deleg_is_read(dp->dl_type))
5707 		return nfserr_openmode;
5708 	else
5709 		return nfs_ok;
5710 }
5711 
share_access_to_flags(u32 share_access)5712 static int share_access_to_flags(u32 share_access)
5713 {
5714 	return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5715 }
5716 
find_deleg_stateid(struct nfs4_client * cl,stateid_t * s)5717 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl,
5718 						  stateid_t *s)
5719 {
5720 	struct nfs4_stid *ret;
5721 
5722 	ret = find_stateid_by_type(cl, s, SC_TYPE_DELEG, SC_STATUS_REVOKED);
5723 	if (!ret)
5724 		return NULL;
5725 	return delegstateid(ret);
5726 }
5727 
nfsd4_is_deleg_cur(struct nfsd4_open * open)5728 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5729 {
5730 	return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5731 	       open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5732 }
5733 
5734 static __be32
nfs4_check_deleg(struct nfs4_client * cl,struct nfsd4_open * open,struct nfs4_delegation ** dp)5735 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5736 		struct nfs4_delegation **dp)
5737 {
5738 	int flags;
5739 	__be32 status = nfserr_bad_stateid;
5740 	struct nfs4_delegation *deleg;
5741 
5742 	deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5743 	if (deleg == NULL)
5744 		goto out;
5745 	if (deleg->dl_stid.sc_status & SC_STATUS_ADMIN_REVOKED) {
5746 		nfs4_put_stid(&deleg->dl_stid);
5747 		status = nfserr_admin_revoked;
5748 		goto out;
5749 	}
5750 	if (deleg->dl_stid.sc_status & SC_STATUS_REVOKED) {
5751 		nfs4_put_stid(&deleg->dl_stid);
5752 		nfsd40_drop_revoked_stid(cl, &open->op_delegate_stateid);
5753 		status = nfserr_deleg_revoked;
5754 		goto out;
5755 	}
5756 	flags = share_access_to_flags(open->op_share_access);
5757 	status = nfs4_check_delegmode(deleg, flags);
5758 	if (status) {
5759 		nfs4_put_stid(&deleg->dl_stid);
5760 		goto out;
5761 	}
5762 	*dp = deleg;
5763 out:
5764 	if (!nfsd4_is_deleg_cur(open))
5765 		return nfs_ok;
5766 	if (status)
5767 		return status;
5768 	open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5769 	return nfs_ok;
5770 }
5771 
nfs4_access_to_access(u32 nfs4_access)5772 static inline int nfs4_access_to_access(u32 nfs4_access)
5773 {
5774 	int flags = 0;
5775 
5776 	if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5777 		flags |= NFSD_MAY_READ;
5778 	if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5779 		flags |= NFSD_MAY_WRITE;
5780 	return flags;
5781 }
5782 
5783 static inline __be32
nfsd4_truncate(struct svc_rqst * rqstp,struct svc_fh * fh,struct nfsd4_open * open)5784 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5785 		struct nfsd4_open *open)
5786 {
5787 	struct iattr iattr = {
5788 		.ia_valid = ATTR_SIZE,
5789 		.ia_size = 0,
5790 	};
5791 	struct nfsd_attrs attrs = {
5792 		.na_iattr	= &iattr,
5793 	};
5794 	if (!open->op_truncate)
5795 		return 0;
5796 	if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5797 		return nfserr_inval;
5798 	return nfsd_setattr(rqstp, fh, &attrs, NULL);
5799 }
5800 
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,bool new_stp)5801 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5802 		struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5803 		struct nfsd4_open *open, bool new_stp)
5804 {
5805 	struct nfsd_file *nf = NULL;
5806 	__be32 status;
5807 	int oflag = nfs4_access_to_omode(open->op_share_access);
5808 	int access = nfs4_access_to_access(open->op_share_access);
5809 	unsigned char old_access_bmap, old_deny_bmap;
5810 
5811 	spin_lock(&fp->fi_lock);
5812 
5813 	/*
5814 	 * Are we trying to set a deny mode that would conflict with
5815 	 * current access?
5816 	 */
5817 	status = nfs4_file_check_deny(fp, open->op_share_deny);
5818 	if (status != nfs_ok) {
5819 		if (status != nfserr_share_denied) {
5820 			spin_unlock(&fp->fi_lock);
5821 			goto out;
5822 		}
5823 		if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5824 				stp, open->op_share_deny, false))
5825 			status = nfserr_jukebox;
5826 		spin_unlock(&fp->fi_lock);
5827 		goto out;
5828 	}
5829 
5830 	/* set access to the file */
5831 	status = nfs4_file_get_access(fp, open->op_share_access);
5832 	if (status != nfs_ok) {
5833 		if (status != nfserr_share_denied) {
5834 			spin_unlock(&fp->fi_lock);
5835 			goto out;
5836 		}
5837 		if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5838 				stp, open->op_share_access, true))
5839 			status = nfserr_jukebox;
5840 		spin_unlock(&fp->fi_lock);
5841 		goto out;
5842 	}
5843 
5844 	/* Set access bits in stateid */
5845 	old_access_bmap = stp->st_access_bmap;
5846 	set_access(open->op_share_access, stp);
5847 
5848 	/* Set new deny mask */
5849 	old_deny_bmap = stp->st_deny_bmap;
5850 	set_deny(open->op_share_deny, stp);
5851 	fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5852 
5853 	if (!fp->fi_fds[oflag]) {
5854 		spin_unlock(&fp->fi_lock);
5855 
5856 		status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
5857 						  open->op_filp, &nf);
5858 		if (status != nfs_ok)
5859 			goto out_put_access;
5860 
5861 		spin_lock(&fp->fi_lock);
5862 		if (!fp->fi_fds[oflag]) {
5863 			fp->fi_fds[oflag] = nf;
5864 			nf = NULL;
5865 		}
5866 	}
5867 	spin_unlock(&fp->fi_lock);
5868 	if (nf)
5869 		nfsd_file_put(nf);
5870 
5871 	status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5872 								access));
5873 	if (status)
5874 		goto out_put_access;
5875 
5876 	status = nfsd4_truncate(rqstp, cur_fh, open);
5877 	if (status)
5878 		goto out_put_access;
5879 out:
5880 	return status;
5881 out_put_access:
5882 	stp->st_access_bmap = old_access_bmap;
5883 	nfs4_file_put_access(fp, open->op_share_access);
5884 	reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5885 	goto out;
5886 }
5887 
5888 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)5889 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5890 		struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5891 		struct nfsd4_open *open)
5892 {
5893 	__be32 status;
5894 	unsigned char old_deny_bmap = stp->st_deny_bmap;
5895 
5896 	if (!test_access(open->op_share_access, stp))
5897 		return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5898 
5899 	/* test and set deny mode */
5900 	spin_lock(&fp->fi_lock);
5901 	status = nfs4_file_check_deny(fp, open->op_share_deny);
5902 	switch (status) {
5903 	case nfs_ok:
5904 		set_deny(open->op_share_deny, stp);
5905 		fp->fi_share_deny |=
5906 			(open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5907 		break;
5908 	case nfserr_share_denied:
5909 		if (nfs4_resolve_deny_conflicts_locked(fp, false,
5910 				stp, open->op_share_deny, false))
5911 			status = nfserr_jukebox;
5912 		break;
5913 	}
5914 	spin_unlock(&fp->fi_lock);
5915 
5916 	if (status != nfs_ok)
5917 		return status;
5918 
5919 	status = nfsd4_truncate(rqstp, cur_fh, open);
5920 	if (status != nfs_ok)
5921 		reset_union_bmap_deny(old_deny_bmap, stp);
5922 	return status;
5923 }
5924 
5925 /* Should we give out recallable state?: */
nfsd4_cb_channel_good(struct nfs4_client * clp)5926 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5927 {
5928 	if (clp->cl_cb_state == NFSD4_CB_UP)
5929 		return true;
5930 	/*
5931 	 * In the sessions case, since we don't have to establish a
5932 	 * separate connection for callbacks, we assume it's OK
5933 	 * until we hear otherwise:
5934 	 */
5935 	return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5936 }
5937 
nfs4_alloc_init_lease(struct nfs4_delegation * dp)5938 static struct file_lease *nfs4_alloc_init_lease(struct nfs4_delegation *dp)
5939 {
5940 	struct file_lease *fl;
5941 
5942 	fl = locks_alloc_lease();
5943 	if (!fl)
5944 		return NULL;
5945 	fl->fl_lmops = &nfsd_lease_mng_ops;
5946 	fl->c.flc_flags = FL_DELEG;
5947 	fl->c.flc_type = deleg_is_read(dp->dl_type) ? F_RDLCK : F_WRLCK;
5948 	fl->c.flc_owner = (fl_owner_t)dp;
5949 	fl->c.flc_pid = current->tgid;
5950 	fl->c.flc_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5951 	return fl;
5952 }
5953 
nfsd4_check_conflicting_opens(struct nfs4_client * clp,struct nfs4_file * fp)5954 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5955 					 struct nfs4_file *fp)
5956 {
5957 	struct nfs4_ol_stateid *st;
5958 	struct file *f = fp->fi_deleg_file->nf_file;
5959 	struct inode *ino = file_inode(f);
5960 	int writes;
5961 
5962 	writes = atomic_read(&ino->i_writecount);
5963 	if (!writes)
5964 		return 0;
5965 	/*
5966 	 * There could be multiple filehandles (hence multiple
5967 	 * nfs4_files) referencing this file, but that's not too
5968 	 * common; let's just give up in that case rather than
5969 	 * trying to go look up all the clients using that other
5970 	 * nfs4_file as well:
5971 	 */
5972 	if (fp->fi_aliased)
5973 		return -EAGAIN;
5974 	/*
5975 	 * If there's a close in progress, make sure that we see it
5976 	 * clear any fi_fds[] entries before we see it decrement
5977 	 * i_writecount:
5978 	 */
5979 	smp_mb__after_atomic();
5980 
5981 	if (fp->fi_fds[O_WRONLY])
5982 		writes--;
5983 	if (fp->fi_fds[O_RDWR])
5984 		writes--;
5985 	if (writes > 0)
5986 		return -EAGAIN; /* There may be non-NFSv4 writers */
5987 	/*
5988 	 * It's possible there are non-NFSv4 write opens in progress,
5989 	 * but if they haven't incremented i_writecount yet then they
5990 	 * also haven't called break lease yet; so, they'll break this
5991 	 * lease soon enough.  So, all that's left to check for is NFSv4
5992 	 * opens:
5993 	 */
5994 	spin_lock(&fp->fi_lock);
5995 	list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5996 		if (st->st_openstp == NULL /* it's an open */ &&
5997 		    access_permit_write(st) &&
5998 		    st->st_stid.sc_client != clp) {
5999 			spin_unlock(&fp->fi_lock);
6000 			return -EAGAIN;
6001 		}
6002 	}
6003 	spin_unlock(&fp->fi_lock);
6004 	/*
6005 	 * There's a small chance that we could be racing with another
6006 	 * NFSv4 open.  However, any open that hasn't added itself to
6007 	 * the fi_stateids list also hasn't called break_lease yet; so,
6008 	 * they'll break this lease soon enough.
6009 	 */
6010 	return 0;
6011 }
6012 
6013 /*
6014  * It's possible that between opening the dentry and setting the delegation,
6015  * that it has been renamed or unlinked. Redo the lookup to verify that this
6016  * hasn't happened.
6017  */
6018 static int
nfsd4_verify_deleg_dentry(struct nfsd4_open * open,struct nfs4_file * fp,struct svc_fh * parent)6019 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
6020 			  struct svc_fh *parent)
6021 {
6022 	struct svc_export *exp;
6023 	struct dentry *child;
6024 	__be32 err;
6025 
6026 	err = nfsd_lookup_dentry(open->op_rqstp, parent,
6027 				 open->op_fname, open->op_fnamelen,
6028 				 &exp, &child);
6029 
6030 	if (err)
6031 		return -EAGAIN;
6032 
6033 	exp_put(exp);
6034 	dput(child);
6035 	if (child != file_dentry(fp->fi_deleg_file->nf_file))
6036 		return -EAGAIN;
6037 
6038 	return 0;
6039 }
6040 
6041 /*
6042  * We avoid breaking delegations held by a client due to its own activity, but
6043  * clearing setuid/setgid bits on a write is an implicit activity and the client
6044  * may not notice and continue using the old mode. Avoid giving out a delegation
6045  * on setuid/setgid files when the client is requesting an open for write.
6046  */
6047 static int
nfsd4_verify_setuid_write(struct nfsd4_open * open,struct nfsd_file * nf)6048 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
6049 {
6050 	struct inode *inode = file_inode(nf->nf_file);
6051 
6052 	if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
6053 	    (inode->i_mode & (S_ISUID|S_ISGID)))
6054 		return -EAGAIN;
6055 	return 0;
6056 }
6057 
6058 /*
6059  * Timestamp delegation was introduced in RFC7862. Runtime switch for disabling
6060  * this feature is /sys/kernel/debug/nfsd/delegated_timestamps.
6061  */
nfsd4_want_deleg_timestamps(const struct nfsd4_open * open)6062 static bool nfsd4_want_deleg_timestamps(const struct nfsd4_open *open)
6063 {
6064 	if (!nfsd_delegts_enabled)
6065 		return false;
6066 	return open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_DELEG_TIMESTAMPS;
6067 }
6068 
6069 static struct nfs4_delegation *
nfs4_set_delegation(struct nfsd4_open * open,struct nfs4_ol_stateid * stp,struct svc_fh * parent)6070 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
6071 		    struct svc_fh *parent)
6072 {
6073 	bool deleg_ts = nfsd4_want_deleg_timestamps(open);
6074 	struct nfs4_client *clp = stp->st_stid.sc_client;
6075 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
6076 	struct nfs4_file *fp = stp->st_stid.sc_file;
6077 	struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
6078 	struct nfs4_delegation *dp;
6079 	struct nfsd_file *nf = NULL;
6080 	struct file_lease *fl;
6081 	int status = 0;
6082 	u32 dl_type;
6083 
6084 	/*
6085 	 * The fi_had_conflict and nfs_get_existing_delegation checks
6086 	 * here are just optimizations; we'll need to recheck them at
6087 	 * the end:
6088 	 */
6089 	if (fp->fi_had_conflict)
6090 		return ERR_PTR(-EAGAIN);
6091 
6092 	/*
6093 	 * Try for a write delegation first. RFC8881 section 10.4 says:
6094 	 *
6095 	 *  "An OPEN_DELEGATE_WRITE delegation allows the client to handle,
6096 	 *   on its own, all opens."
6097 	 *
6098 	 * Furthermore, section 9.1.2 says:
6099 	 *
6100 	 *  "In the case of READ, the server may perform the corresponding
6101 	 *  check on the access mode, or it may choose to allow READ for
6102 	 *  OPEN4_SHARE_ACCESS_WRITE, to accommodate clients whose WRITE
6103 	 *  implementation may unavoidably do reads (e.g., due to buffer
6104 	 *  cache constraints)."
6105 	 *
6106 	 *  We choose to offer a write delegation for OPEN with the
6107 	 *  OPEN4_SHARE_ACCESS_WRITE access mode to accommodate such clients.
6108 	 */
6109 	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
6110 		nf = find_writeable_file(fp);
6111 		dl_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG : OPEN_DELEGATE_WRITE;
6112 	}
6113 
6114 	/*
6115 	 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
6116 	 * file for some reason, then try for a read delegation instead.
6117 	 */
6118 	if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
6119 		nf = find_readable_file(fp);
6120 		dl_type = deleg_ts ? OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ;
6121 	}
6122 
6123 	if (!nf)
6124 		return ERR_PTR(-EAGAIN);
6125 
6126 	/*
6127 	 * File delegations and associated locks cannot be recovered if the
6128 	 * export is from an NFS proxy server.
6129 	 */
6130 	if (exportfs_cannot_lock(nf->nf_file->f_path.mnt->mnt_sb->s_export_op)) {
6131 		nfsd_file_put(nf);
6132 		return ERR_PTR(-EOPNOTSUPP);
6133 	}
6134 
6135 	spin_lock(&nn->deleg_lock);
6136 	spin_lock(&fp->fi_lock);
6137 	if (nfs4_delegation_exists(clp, fp))
6138 		status = -EAGAIN;
6139 	else if (nfsd4_verify_setuid_write(open, nf))
6140 		status = -EAGAIN;
6141 	else if (!fp->fi_deleg_file) {
6142 		fp->fi_deleg_file = nf;
6143 		/* increment early to prevent fi_deleg_file from being
6144 		 * cleared */
6145 		fp->fi_delegees = 1;
6146 		nf = NULL;
6147 	} else
6148 		fp->fi_delegees++;
6149 	spin_unlock(&fp->fi_lock);
6150 	spin_unlock(&nn->deleg_lock);
6151 	if (nf)
6152 		nfsd_file_put(nf);
6153 	if (status)
6154 		return ERR_PTR(status);
6155 
6156 	status = -ENOMEM;
6157 	dp = alloc_init_deleg(clp, fp, odstate, dl_type);
6158 	if (!dp)
6159 		goto out_delegees;
6160 
6161 	fl = nfs4_alloc_init_lease(dp);
6162 	if (!fl)
6163 		goto out_clnt_odstate;
6164 
6165 	status = kernel_setlease(fp->fi_deleg_file->nf_file,
6166 				      fl->c.flc_type, &fl, NULL);
6167 	if (fl)
6168 		locks_free_lease(fl);
6169 	if (status)
6170 		goto out_clnt_odstate;
6171 
6172 	if (parent) {
6173 		status = nfsd4_verify_deleg_dentry(open, fp, parent);
6174 		if (status)
6175 			goto out_unlock;
6176 	}
6177 
6178 	status = nfsd4_check_conflicting_opens(clp, fp);
6179 	if (status)
6180 		goto out_unlock;
6181 
6182 	/*
6183 	 * Now that the deleg is set, check again to ensure that nothing
6184 	 * raced in and changed the mode while we weren't looking.
6185 	 */
6186 	status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file);
6187 	if (status)
6188 		goto out_unlock;
6189 
6190 	status = -EAGAIN;
6191 	if (fp->fi_had_conflict)
6192 		goto out_unlock;
6193 
6194 	spin_lock(&nn->deleg_lock);
6195 	spin_lock(&clp->cl_lock);
6196 	spin_lock(&fp->fi_lock);
6197 	status = hash_delegation_locked(dp, fp);
6198 	spin_unlock(&fp->fi_lock);
6199 	spin_unlock(&clp->cl_lock);
6200 	spin_unlock(&nn->deleg_lock);
6201 
6202 	if (status)
6203 		goto out_unlock;
6204 
6205 	return dp;
6206 out_unlock:
6207 	kernel_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
6208 out_clnt_odstate:
6209 	put_clnt_odstate(dp->dl_clnt_odstate);
6210 	nfs4_put_stid(&dp->dl_stid);
6211 out_delegees:
6212 	put_deleg_file(fp);
6213 	return ERR_PTR(status);
6214 }
6215 
nfsd4_open_deleg_none_ext(struct nfsd4_open * open,int status)6216 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
6217 {
6218 	open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6219 	if (status == -EAGAIN)
6220 		open->op_why_no_deleg = WND4_CONTENTION;
6221 	else {
6222 		open->op_why_no_deleg = WND4_RESOURCE;
6223 		switch (open->op_deleg_want) {
6224 		case OPEN4_SHARE_ACCESS_WANT_READ_DELEG:
6225 		case OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG:
6226 		case OPEN4_SHARE_ACCESS_WANT_ANY_DELEG:
6227 			break;
6228 		case OPEN4_SHARE_ACCESS_WANT_CANCEL:
6229 			open->op_why_no_deleg = WND4_CANCELLED;
6230 			break;
6231 		case OPEN4_SHARE_ACCESS_WANT_NO_DELEG:
6232 			WARN_ON_ONCE(1);
6233 		}
6234 	}
6235 }
6236 
6237 static bool
nfs4_delegation_stat(struct nfs4_delegation * dp,struct svc_fh * currentfh,struct kstat * stat)6238 nfs4_delegation_stat(struct nfs4_delegation *dp, struct svc_fh *currentfh,
6239 		     struct kstat *stat)
6240 {
6241 	struct nfsd_file *nf = find_writeable_file(dp->dl_stid.sc_file);
6242 	struct path path;
6243 	int rc;
6244 
6245 	if (!nf)
6246 		return false;
6247 
6248 	path.mnt = currentfh->fh_export->ex_path.mnt;
6249 	path.dentry = file_dentry(nf->nf_file);
6250 
6251 	rc = vfs_getattr(&path, stat,
6252 			 STATX_MODE | STATX_SIZE | STATX_ATIME |
6253 			 STATX_MTIME | STATX_CTIME | STATX_CHANGE_COOKIE,
6254 			 AT_STATX_SYNC_AS_STAT);
6255 
6256 	nfsd_file_put(nf);
6257 	return rc == 0;
6258 }
6259 
6260 /*
6261  * Add NFS4_SHARE_ACCESS_READ to the write delegation granted on OPEN
6262  * with NFS4_SHARE_ACCESS_WRITE by allocating separate nfsd_file and
6263  * struct file to be used for read with delegation stateid.
6264  *
6265  */
6266 static bool
nfsd4_add_rdaccess_to_wrdeleg(struct svc_rqst * rqstp,struct nfsd4_open * open,struct svc_fh * fh,struct nfs4_ol_stateid * stp)6267 nfsd4_add_rdaccess_to_wrdeleg(struct svc_rqst *rqstp, struct nfsd4_open *open,
6268 			      struct svc_fh *fh, struct nfs4_ol_stateid *stp)
6269 {
6270 	struct nfs4_file *fp;
6271 	struct nfsd_file *nf = NULL;
6272 
6273 	if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) ==
6274 			NFS4_SHARE_ACCESS_WRITE) {
6275 		if (nfsd_file_acquire_opened(rqstp, fh, NFSD_MAY_READ, NULL, &nf))
6276 			return (false);
6277 		fp = stp->st_stid.sc_file;
6278 		spin_lock(&fp->fi_lock);
6279 		if (!fp->fi_fds[O_RDONLY]) {
6280 			__nfs4_file_get_access(fp, NFS4_SHARE_ACCESS_READ);
6281 			fp->fi_fds[O_RDONLY] = nf;
6282 			fp->fi_rdeleg_file = nfsd_file_get(fp->fi_fds[O_RDONLY]);
6283 			nf = NULL;
6284 		}
6285 		spin_unlock(&fp->fi_lock);
6286 		if (nf)
6287 			nfsd_file_put(nf);
6288 	}
6289 	return true;
6290 }
6291 
6292 /*
6293  * The Linux NFS server does not offer write delegations to NFSv4.0
6294  * clients in order to avoid conflicts between write delegations and
6295  * GETATTRs requesting CHANGE or SIZE attributes.
6296  *
6297  * With NFSv4.1 and later minorversions, the SEQUENCE operation that
6298  * begins each COMPOUND contains a client ID. Delegation recall can
6299  * be avoided when the server recognizes the client sending a
6300  * GETATTR also holds write delegation it conflicts with.
6301  *
6302  * However, the NFSv4.0 protocol does not enable a server to
6303  * determine that a GETATTR originated from the client holding the
6304  * conflicting delegation versus coming from some other client. Per
6305  * RFC 7530 Section 16.7.5, the server must recall or send a
6306  * CB_GETATTR even when the GETATTR originates from the client that
6307  * holds the conflicting delegation.
6308  *
6309  * An NFSv4.0 client can trigger a pathological situation if it
6310  * always sends a DELEGRETURN preceded by a conflicting GETATTR in
6311  * the same COMPOUND. COMPOUND execution will always stop at the
6312  * GETATTR and the DELEGRETURN will never get executed. The server
6313  * eventually revokes the delegation, which can result in loss of
6314  * open or lock state.
6315  */
6316 static void
nfs4_open_delegation(struct svc_rqst * rqstp,struct nfsd4_open * open,struct nfs4_ol_stateid * stp,struct svc_fh * currentfh,struct svc_fh * fh)6317 nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
6318 		     struct nfs4_ol_stateid *stp, struct svc_fh *currentfh,
6319 		     struct svc_fh *fh)
6320 {
6321 	struct nfs4_openowner *oo = openowner(stp->st_stateowner);
6322 	bool deleg_ts = nfsd4_want_deleg_timestamps(open);
6323 	struct nfs4_client *clp = stp->st_stid.sc_client;
6324 	struct svc_fh *parent = NULL;
6325 	struct nfs4_delegation *dp;
6326 	struct kstat stat;
6327 	int status = 0;
6328 	int cb_up;
6329 
6330 	cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
6331 	open->op_recall = false;
6332 	switch (open->op_claim_type) {
6333 		case NFS4_OPEN_CLAIM_PREVIOUS:
6334 			if (!cb_up)
6335 				open->op_recall = true;
6336 			break;
6337 		case NFS4_OPEN_CLAIM_NULL:
6338 			parent = currentfh;
6339 			fallthrough;
6340 		case NFS4_OPEN_CLAIM_FH:
6341 			/*
6342 			 * Let's not give out any delegations till everyone's
6343 			 * had the chance to reclaim theirs, *and* until
6344 			 * NLM locks have all been reclaimed:
6345 			 */
6346 			if (locks_in_grace(clp->net))
6347 				goto out_no_deleg;
6348 			if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
6349 				goto out_no_deleg;
6350 			if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
6351 					!clp->cl_minorversion)
6352 				goto out_no_deleg;
6353 			break;
6354 		default:
6355 			goto out_no_deleg;
6356 	}
6357 	dp = nfs4_set_delegation(open, stp, parent);
6358 	if (IS_ERR(dp))
6359 		goto out_no_deleg;
6360 
6361 	memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
6362 
6363 	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
6364 		struct file *f = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
6365 
6366 		if (!nfsd4_add_rdaccess_to_wrdeleg(rqstp, open, fh, stp) ||
6367 				!nfs4_delegation_stat(dp, currentfh, &stat)) {
6368 			nfs4_put_stid(&dp->dl_stid);
6369 			destroy_delegation(dp);
6370 			goto out_no_deleg;
6371 		}
6372 		open->op_delegate_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG :
6373 						    OPEN_DELEGATE_WRITE;
6374 		dp->dl_cb_fattr.ncf_initial_cinfo = nfsd4_change_attribute(&stat);
6375 		dp->dl_atime = stat.atime;
6376 		dp->dl_ctime = stat.ctime;
6377 		dp->dl_mtime = stat.mtime;
6378 		spin_lock(&f->f_lock);
6379 		if (deleg_ts)
6380 			f->f_mode |= FMODE_NOCMTIME;
6381 		spin_unlock(&f->f_lock);
6382 		trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
6383 	} else {
6384 		open->op_delegate_type = deleg_ts && nfs4_delegation_stat(dp, currentfh, &stat) ?
6385 					 OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ;
6386 		dp->dl_atime = stat.atime;
6387 		trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
6388 	}
6389 	nfs4_put_stid(&dp->dl_stid);
6390 	return;
6391 out_no_deleg:
6392 	open->op_delegate_type = OPEN_DELEGATE_NONE;
6393 
6394 	/* 4.1 client asking for a delegation? */
6395 	if (open->op_deleg_want)
6396 		nfsd4_open_deleg_none_ext(open, status);
6397 	return;
6398 }
6399 
nfsd4_deleg_xgrade_none_ext(struct nfsd4_open * open,struct nfs4_delegation * dp)6400 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
6401 					struct nfs4_delegation *dp)
6402 {
6403 	if (deleg_is_write(dp->dl_type)) {
6404 		if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_READ_DELEG) {
6405 			open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6406 			open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
6407 		} else if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG) {
6408 			open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6409 			open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
6410 		}
6411 	}
6412 	/* Otherwise the client must be confused wanting a delegation
6413 	 * it already has, therefore we don't return
6414 	 * OPEN_DELEGATE_NONE_EXT and reason.
6415 	 */
6416 }
6417 
6418 /* Are we returning only a delegation stateid? */
open_xor_delegation(struct nfsd4_open * open)6419 static bool open_xor_delegation(struct nfsd4_open *open)
6420 {
6421 	if (!(open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_OPEN_XOR_DELEGATION))
6422 		return false;
6423 	/* Did we actually get a delegation? */
6424 	if (!deleg_is_read(open->op_delegate_type) && !deleg_is_write(open->op_delegate_type))
6425 		return false;
6426 	return true;
6427 }
6428 
6429 /**
6430  * nfsd4_process_open2 - finish open processing
6431  * @rqstp: the RPC transaction being executed
6432  * @current_fh: NFSv4 COMPOUND's current filehandle
6433  * @open: OPEN arguments
6434  *
6435  * If successful, (1) truncate the file if open->op_truncate was
6436  * set, (2) set open->op_stateid, (3) set open->op_delegation.
6437  *
6438  * Returns %nfs_ok on success; otherwise an nfs4stat value in
6439  * network byte order is returned.
6440  */
6441 __be32
nfsd4_process_open2(struct svc_rqst * rqstp,struct svc_fh * current_fh,struct nfsd4_open * open)6442 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
6443 {
6444 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
6445 	struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
6446 	struct nfs4_file *fp = NULL;
6447 	struct nfs4_ol_stateid *stp = NULL;
6448 	struct nfs4_delegation *dp = NULL;
6449 	__be32 status;
6450 	bool new_stp = false;
6451 
6452 	/*
6453 	 * Lookup file; if found, lookup stateid and check open request,
6454 	 * and check for delegations in the process of being recalled.
6455 	 * If not found, create the nfs4_file struct
6456 	 */
6457 	fp = nfsd4_file_hash_insert(open->op_file, current_fh);
6458 	if (unlikely(!fp))
6459 		return nfserr_jukebox;
6460 	if (fp != open->op_file) {
6461 		status = nfs4_check_deleg(cl, open, &dp);
6462 		if (status)
6463 			goto out;
6464 		if (dp && nfsd4_is_deleg_cur(open) &&
6465 				(dp->dl_stid.sc_file != fp)) {
6466 			/*
6467 			 * RFC8881 section 8.2.4 mandates the server to return
6468 			 * NFS4ERR_BAD_STATEID if the selected table entry does
6469 			 * not match the current filehandle. However returning
6470 			 * NFS4ERR_BAD_STATEID in the OPEN can cause the client
6471 			 * to repeatedly retry the operation with the same
6472 			 * stateid, since the stateid itself is valid. To avoid
6473 			 * this situation NFSD returns NFS4ERR_INVAL instead.
6474 			 */
6475 			status = nfserr_inval;
6476 			goto out;
6477 		}
6478 		stp = nfsd4_find_and_lock_existing_open(fp, open);
6479 	} else {
6480 		open->op_file = NULL;
6481 		status = nfserr_bad_stateid;
6482 		if (nfsd4_is_deleg_cur(open))
6483 			goto out;
6484 	}
6485 
6486 	if (!stp) {
6487 		stp = init_open_stateid(fp, open);
6488 		if (!stp) {
6489 			status = nfserr_jukebox;
6490 			goto out;
6491 		}
6492 
6493 		if (!open->op_stp)
6494 			new_stp = true;
6495 	}
6496 
6497 	/*
6498 	 * OPEN the file, or upgrade an existing OPEN.
6499 	 * If truncate fails, the OPEN fails.
6500 	 *
6501 	 * stp is already locked.
6502 	 */
6503 	if (!new_stp) {
6504 		/* Stateid was found, this is an OPEN upgrade */
6505 		status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
6506 		if (status) {
6507 			mutex_unlock(&stp->st_mutex);
6508 			goto out;
6509 		}
6510 	} else {
6511 		status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
6512 		if (status) {
6513 			release_open_stateid(stp);
6514 			mutex_unlock(&stp->st_mutex);
6515 			goto out;
6516 		}
6517 
6518 		stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
6519 							open->op_odstate);
6520 		if (stp->st_clnt_odstate == open->op_odstate)
6521 			open->op_odstate = NULL;
6522 	}
6523 
6524 	nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
6525 	mutex_unlock(&stp->st_mutex);
6526 
6527 	if (nfsd4_has_session(&resp->cstate)) {
6528 		if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_NO_DELEG) {
6529 			open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6530 			open->op_why_no_deleg = WND4_NOT_WANTED;
6531 			goto nodeleg;
6532 		}
6533 	}
6534 
6535 	/*
6536 	* Attempt to hand out a delegation. No error return, because the
6537 	* OPEN succeeds even if we fail.
6538 	*/
6539 	nfs4_open_delegation(rqstp, open, stp,
6540 		&resp->cstate.current_fh, current_fh);
6541 
6542 	/*
6543 	 * If there is an existing open stateid, it must be updated and
6544 	 * returned. Only respect WANT_OPEN_XOR_DELEGATION when a new
6545 	 * open stateid would have to be created.
6546 	 */
6547 	if (new_stp && open_xor_delegation(open)) {
6548 		memcpy(&open->op_stateid, &zero_stateid, sizeof(open->op_stateid));
6549 		open->op_rflags |= OPEN4_RESULT_NO_OPEN_STATEID;
6550 		release_open_stateid(stp);
6551 	}
6552 nodeleg:
6553 	status = nfs_ok;
6554 	trace_nfsd_open(&stp->st_stid.sc_stateid);
6555 out:
6556 	/* 4.1 client trying to upgrade/downgrade delegation? */
6557 	if (open->op_delegate_type == OPEN_DELEGATE_NONE && dp &&
6558 	    open->op_deleg_want)
6559 		nfsd4_deleg_xgrade_none_ext(open, dp);
6560 
6561 	if (fp)
6562 		put_nfs4_file(fp);
6563 	if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
6564 		open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
6565 	/*
6566 	* To finish the open response, we just need to set the rflags.
6567 	*/
6568 	open->op_rflags |= NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
6569 	if (nfsd4_has_session(&resp->cstate))
6570 		open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
6571 	else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
6572 		open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
6573 
6574 	if (dp)
6575 		nfs4_put_stid(&dp->dl_stid);
6576 	if (stp)
6577 		nfs4_put_stid(&stp->st_stid);
6578 
6579 	return status;
6580 }
6581 
nfsd4_cleanup_open_state(struct nfsd4_compound_state * cstate,struct nfsd4_open * open)6582 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
6583 			      struct nfsd4_open *open)
6584 {
6585 	if (open->op_openowner)
6586 		nfs4_put_stateowner(&open->op_openowner->oo_owner);
6587 	if (open->op_file)
6588 		kmem_cache_free(file_slab, open->op_file);
6589 	if (open->op_stp)
6590 		nfs4_put_stid(&open->op_stp->st_stid);
6591 	if (open->op_odstate)
6592 		kmem_cache_free(odstate_slab, open->op_odstate);
6593 }
6594 
6595 __be32
nfsd4_renew(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6596 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6597 	    union nfsd4_op_u *u)
6598 {
6599 	clientid_t *clid = &u->renew;
6600 	struct nfs4_client *clp;
6601 	__be32 status;
6602 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6603 
6604 	trace_nfsd_clid_renew(clid);
6605 	status = set_client(clid, cstate, nn);
6606 	if (status)
6607 		return status;
6608 	clp = cstate->clp;
6609 	if (!list_empty(&clp->cl_delegations)
6610 			&& clp->cl_cb_state != NFSD4_CB_UP)
6611 		return nfserr_cb_path_down;
6612 	return nfs_ok;
6613 }
6614 
6615 static void
nfsd4_end_grace(struct nfsd_net * nn)6616 nfsd4_end_grace(struct nfsd_net *nn)
6617 {
6618 	/* do nothing if grace period already ended */
6619 	if (nn->grace_ended)
6620 		return;
6621 
6622 	trace_nfsd_grace_complete(nn);
6623 	nn->grace_ended = true;
6624 	/*
6625 	 * If the server goes down again right now, an NFSv4
6626 	 * client will still be allowed to reclaim after it comes back up,
6627 	 * even if it hasn't yet had a chance to reclaim state this time.
6628 	 *
6629 	 */
6630 	nfsd4_record_grace_done(nn);
6631 	/*
6632 	 * At this point, NFSv4 clients can still reclaim.  But if the
6633 	 * server crashes, any that have not yet reclaimed will be out
6634 	 * of luck on the next boot.
6635 	 *
6636 	 * (NFSv4.1+ clients are considered to have reclaimed once they
6637 	 * call RECLAIM_COMPLETE.  NFSv4.0 clients are considered to
6638 	 * have reclaimed after their first OPEN.)
6639 	 */
6640 	locks_end_grace(&nn->nfsd4_manager);
6641 	/*
6642 	 * At this point, and once lockd and/or any other containers
6643 	 * exit their grace period, further reclaims will fail and
6644 	 * regular locking can resume.
6645 	 */
6646 }
6647 
6648 /**
6649  * nfsd4_force_end_grace - forcibly end the NFSv4 grace period
6650  * @nn: network namespace for the server instance to be updated
6651  *
6652  * Forces bypass of normal grace period completion, then schedules
6653  * the laundromat to end the grace period immediately. Does not wait
6654  * for the grace period to fully terminate before returning.
6655  *
6656  * Return values:
6657  *   %true: Grace termination schedule
6658  *   %false: No action was taken
6659  */
nfsd4_force_end_grace(struct nfsd_net * nn)6660 bool nfsd4_force_end_grace(struct nfsd_net *nn)
6661 {
6662 	if (!nn->client_tracking_ops)
6663 		return false;
6664 	if (READ_ONCE(nn->grace_ended))
6665 		return false;
6666 	/* laundromat_work must be initialised now, though it might be disabled */
6667 	WRITE_ONCE(nn->grace_end_forced, true);
6668 	/* mod_delayed_work() doesn't queue work after
6669 	 * nfs4_state_shutdown_net() has called disable_delayed_work_sync()
6670 	 */
6671 	mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
6672 	return true;
6673 }
6674 
6675 /*
6676  * If we've waited a lease period but there are still clients trying to
6677  * reclaim, wait a little longer to give them a chance to finish.
6678  */
clients_still_reclaiming(struct nfsd_net * nn)6679 static bool clients_still_reclaiming(struct nfsd_net *nn)
6680 {
6681 	time64_t double_grace_period_end = nn->boot_time +
6682 					   2 * nn->nfsd4_lease;
6683 
6684 	if (READ_ONCE(nn->grace_end_forced))
6685 		return false;
6686 	if (nn->track_reclaim_completes &&
6687 			atomic_read(&nn->nr_reclaim_complete) ==
6688 			nn->reclaim_str_hashtbl_size)
6689 		return false;
6690 	if (!nn->somebody_reclaimed)
6691 		return false;
6692 	nn->somebody_reclaimed = false;
6693 	/*
6694 	 * If we've given them *two* lease times to reclaim, and they're
6695 	 * still not done, give up:
6696 	 */
6697 	if (ktime_get_boottime_seconds() > double_grace_period_end)
6698 		return false;
6699 	return true;
6700 }
6701 
6702 struct laundry_time {
6703 	time64_t cutoff;
6704 	time64_t new_timeo;
6705 };
6706 
state_expired(struct laundry_time * lt,time64_t last_refresh)6707 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
6708 {
6709 	time64_t time_remaining;
6710 
6711 	if (last_refresh < lt->cutoff)
6712 		return true;
6713 	time_remaining = last_refresh - lt->cutoff;
6714 	lt->new_timeo = min(lt->new_timeo, time_remaining);
6715 	return false;
6716 }
6717 
6718 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
nfsd4_ssc_init_umount_work(struct nfsd_net * nn)6719 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
6720 {
6721 	spin_lock_init(&nn->nfsd_ssc_lock);
6722 	INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
6723 	init_waitqueue_head(&nn->nfsd_ssc_waitq);
6724 }
6725 
6726 /*
6727  * This is called when nfsd is being shutdown, after all inter_ssc
6728  * cleanup were done, to destroy the ssc delayed unmount list.
6729  */
nfsd4_ssc_shutdown_umount(struct nfsd_net * nn)6730 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
6731 {
6732 	struct nfsd4_ssc_umount_item *ni = NULL;
6733 	struct nfsd4_ssc_umount_item *tmp;
6734 
6735 	spin_lock(&nn->nfsd_ssc_lock);
6736 	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6737 		list_del(&ni->nsui_list);
6738 		spin_unlock(&nn->nfsd_ssc_lock);
6739 		mntput(ni->nsui_vfsmount);
6740 		kfree(ni);
6741 		spin_lock(&nn->nfsd_ssc_lock);
6742 	}
6743 	spin_unlock(&nn->nfsd_ssc_lock);
6744 }
6745 
nfsd4_ssc_expire_umount(struct nfsd_net * nn)6746 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
6747 {
6748 	bool do_wakeup = false;
6749 	struct nfsd4_ssc_umount_item *ni = NULL;
6750 	struct nfsd4_ssc_umount_item *tmp;
6751 
6752 	spin_lock(&nn->nfsd_ssc_lock);
6753 	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6754 		if (time_after(jiffies, ni->nsui_expire)) {
6755 			if (refcount_read(&ni->nsui_refcnt) > 1)
6756 				continue;
6757 
6758 			/* mark being unmount */
6759 			ni->nsui_busy = true;
6760 			spin_unlock(&nn->nfsd_ssc_lock);
6761 			mntput(ni->nsui_vfsmount);
6762 			spin_lock(&nn->nfsd_ssc_lock);
6763 
6764 			/* waiters need to start from begin of list */
6765 			list_del(&ni->nsui_list);
6766 			kfree(ni);
6767 
6768 			/* wakeup ssc_connect waiters */
6769 			do_wakeup = true;
6770 			continue;
6771 		}
6772 		break;
6773 	}
6774 	if (do_wakeup)
6775 		wake_up_all(&nn->nfsd_ssc_waitq);
6776 	spin_unlock(&nn->nfsd_ssc_lock);
6777 }
6778 #endif
6779 
6780 /* Check if any lock belonging to this lockowner has any blockers */
6781 static bool
nfs4_lockowner_has_blockers(struct nfs4_lockowner * lo)6782 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
6783 {
6784 	struct file_lock_context *ctx;
6785 	struct nfs4_ol_stateid *stp;
6786 	struct nfs4_file *nf;
6787 
6788 	list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
6789 		nf = stp->st_stid.sc_file;
6790 		ctx = locks_inode_context(nf->fi_inode);
6791 		if (!ctx)
6792 			continue;
6793 		if (locks_owner_has_blockers(ctx, lo))
6794 			return true;
6795 	}
6796 	return false;
6797 }
6798 
6799 static bool
nfs4_anylock_blockers(struct nfs4_client * clp)6800 nfs4_anylock_blockers(struct nfs4_client *clp)
6801 {
6802 	int i;
6803 	struct nfs4_stateowner *so;
6804 	struct nfs4_lockowner *lo;
6805 
6806 	if (atomic_read(&clp->cl_delegs_in_recall))
6807 		return true;
6808 	spin_lock(&clp->cl_lock);
6809 	for (i = 0; i < OWNER_HASH_SIZE; i++) {
6810 		list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
6811 				so_strhash) {
6812 			if (so->so_is_open_owner)
6813 				continue;
6814 			lo = lockowner(so);
6815 			if (nfs4_lockowner_has_blockers(lo)) {
6816 				spin_unlock(&clp->cl_lock);
6817 				return true;
6818 			}
6819 		}
6820 	}
6821 	spin_unlock(&clp->cl_lock);
6822 	return false;
6823 }
6824 
6825 static void
nfs4_get_client_reaplist(struct nfsd_net * nn,struct list_head * reaplist,struct laundry_time * lt)6826 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
6827 				struct laundry_time *lt)
6828 {
6829 	unsigned int maxreap, reapcnt = 0;
6830 	struct list_head *pos, *next;
6831 	struct nfs4_client *clp;
6832 
6833 	maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
6834 			NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
6835 	INIT_LIST_HEAD(reaplist);
6836 	spin_lock(&nn->client_lock);
6837 	list_for_each_safe(pos, next, &nn->client_lru) {
6838 		clp = list_entry(pos, struct nfs4_client, cl_lru);
6839 		if (clp->cl_state == NFSD4_EXPIRABLE)
6840 			goto exp_client;
6841 		if (!state_expired(lt, clp->cl_time))
6842 			break;
6843 		if (!atomic_read(&clp->cl_rpc_users)) {
6844 			if (clp->cl_state == NFSD4_ACTIVE)
6845 				atomic_inc(&nn->nfsd_courtesy_clients);
6846 			clp->cl_state = NFSD4_COURTESY;
6847 		}
6848 		if (!client_has_state(clp))
6849 			goto exp_client;
6850 		if (!nfs4_anylock_blockers(clp))
6851 			if (reapcnt >= maxreap)
6852 				continue;
6853 exp_client:
6854 		if (!mark_client_expired_locked(clp)) {
6855 			list_add(&clp->cl_lru, reaplist);
6856 			reapcnt++;
6857 		}
6858 	}
6859 	spin_unlock(&nn->client_lock);
6860 }
6861 
6862 static void
nfs4_get_courtesy_client_reaplist(struct nfsd_net * nn,struct list_head * reaplist)6863 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
6864 				struct list_head *reaplist)
6865 {
6866 	unsigned int maxreap = 0, reapcnt = 0;
6867 	struct list_head *pos, *next;
6868 	struct nfs4_client *clp;
6869 
6870 	maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
6871 	INIT_LIST_HEAD(reaplist);
6872 
6873 	spin_lock(&nn->client_lock);
6874 	list_for_each_safe(pos, next, &nn->client_lru) {
6875 		clp = list_entry(pos, struct nfs4_client, cl_lru);
6876 		if (clp->cl_state == NFSD4_ACTIVE)
6877 			break;
6878 		if (reapcnt >= maxreap)
6879 			break;
6880 		if (!mark_client_expired_locked(clp)) {
6881 			list_add(&clp->cl_lru, reaplist);
6882 			reapcnt++;
6883 		}
6884 	}
6885 	spin_unlock(&nn->client_lock);
6886 }
6887 
6888 static void
nfs4_process_client_reaplist(struct list_head * reaplist)6889 nfs4_process_client_reaplist(struct list_head *reaplist)
6890 {
6891 	struct list_head *pos, *next;
6892 	struct nfs4_client *clp;
6893 
6894 	list_for_each_safe(pos, next, reaplist) {
6895 		clp = list_entry(pos, struct nfs4_client, cl_lru);
6896 		trace_nfsd_clid_purged(&clp->cl_clientid);
6897 		list_del_init(&clp->cl_lru);
6898 		expire_client(clp);
6899 	}
6900 }
6901 
nfs40_clean_admin_revoked(struct nfsd_net * nn,struct laundry_time * lt)6902 static void nfs40_clean_admin_revoked(struct nfsd_net *nn,
6903 				      struct laundry_time *lt)
6904 {
6905 	struct nfs4_client *clp;
6906 
6907 	spin_lock(&nn->client_lock);
6908 	if (nn->nfs40_last_revoke == 0 ||
6909 	    nn->nfs40_last_revoke > lt->cutoff) {
6910 		spin_unlock(&nn->client_lock);
6911 		return;
6912 	}
6913 	nn->nfs40_last_revoke = 0;
6914 
6915 retry:
6916 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6917 		unsigned long id, tmp;
6918 		struct nfs4_stid *stid;
6919 
6920 		if (atomic_read(&clp->cl_admin_revoked) == 0)
6921 			continue;
6922 
6923 		spin_lock(&clp->cl_lock);
6924 		idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id)
6925 			if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) {
6926 				refcount_inc(&stid->sc_count);
6927 				spin_unlock(&nn->client_lock);
6928 				/* this function drops ->cl_lock */
6929 				nfsd4_drop_revoked_stid(stid);
6930 				nfs4_put_stid(stid);
6931 				spin_lock(&nn->client_lock);
6932 				goto retry;
6933 			}
6934 		spin_unlock(&clp->cl_lock);
6935 	}
6936 	spin_unlock(&nn->client_lock);
6937 }
6938 
6939 static time64_t
nfs4_laundromat(struct nfsd_net * nn)6940 nfs4_laundromat(struct nfsd_net *nn)
6941 {
6942 	struct nfs4_openowner *oo;
6943 	struct nfs4_delegation *dp;
6944 	struct nfs4_ol_stateid *stp;
6945 	struct nfsd4_blocked_lock *nbl;
6946 	struct list_head *pos, *next, reaplist;
6947 	struct laundry_time lt = {
6948 		.cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6949 		.new_timeo = nn->nfsd4_lease
6950 	};
6951 	struct nfs4_cpntf_state *cps;
6952 	copy_stateid_t *cps_t;
6953 	int i;
6954 
6955 	if (clients_still_reclaiming(nn)) {
6956 		lt.new_timeo = 0;
6957 		goto out;
6958 	}
6959 	nfsd4_end_grace(nn);
6960 
6961 	spin_lock(&nn->s2s_cp_lock);
6962 	idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6963 		cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6964 		if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
6965 				state_expired(&lt, cps->cpntf_time))
6966 			_free_cpntf_state_locked(nn, cps);
6967 	}
6968 	spin_unlock(&nn->s2s_cp_lock);
6969 	nfsd4_async_copy_reaper(nn);
6970 	nfs4_get_client_reaplist(nn, &reaplist, &lt);
6971 	nfs4_process_client_reaplist(&reaplist);
6972 
6973 	nfs40_clean_admin_revoked(nn, &lt);
6974 
6975 	spin_lock(&nn->deleg_lock);
6976 	list_for_each_safe(pos, next, &nn->del_recall_lru) {
6977 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6978 		if (!state_expired(&lt, dp->dl_time))
6979 			break;
6980 		refcount_inc(&dp->dl_stid.sc_count);
6981 		unhash_delegation_locked(dp, SC_STATUS_REVOKED);
6982 		list_add(&dp->dl_recall_lru, &reaplist);
6983 	}
6984 	spin_unlock(&nn->deleg_lock);
6985 	while (!list_empty(&reaplist)) {
6986 		dp = list_first_entry(&reaplist, struct nfs4_delegation,
6987 					dl_recall_lru);
6988 		list_del_init(&dp->dl_recall_lru);
6989 		revoke_delegation(dp);
6990 	}
6991 
6992 	spin_lock(&nn->client_lock);
6993 	while (!list_empty(&nn->close_lru)) {
6994 		oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6995 					oo_close_lru);
6996 		if (!state_expired(&lt, oo->oo_time))
6997 			break;
6998 		list_del_init(&oo->oo_close_lru);
6999 		stp = oo->oo_last_closed_stid;
7000 		oo->oo_last_closed_stid = NULL;
7001 		spin_unlock(&nn->client_lock);
7002 		nfs4_put_stid(&stp->st_stid);
7003 		spin_lock(&nn->client_lock);
7004 	}
7005 	spin_unlock(&nn->client_lock);
7006 
7007 	/*
7008 	 * It's possible for a client to try and acquire an already held lock
7009 	 * that is being held for a long time, and then lose interest in it.
7010 	 * So, we clean out any un-revisited request after a lease period
7011 	 * under the assumption that the client is no longer interested.
7012 	 *
7013 	 * RFC5661, sec. 9.6 states that the client must not rely on getting
7014 	 * notifications and must continue to poll for locks, even when the
7015 	 * server supports them. Thus this shouldn't lead to clients blocking
7016 	 * indefinitely once the lock does become free.
7017 	 */
7018 	BUG_ON(!list_empty(&reaplist));
7019 	spin_lock(&nn->blocked_locks_lock);
7020 	while (!list_empty(&nn->blocked_locks_lru)) {
7021 		nbl = list_first_entry(&nn->blocked_locks_lru,
7022 					struct nfsd4_blocked_lock, nbl_lru);
7023 		if (!state_expired(&lt, nbl->nbl_time))
7024 			break;
7025 		list_move(&nbl->nbl_lru, &reaplist);
7026 		list_del_init(&nbl->nbl_list);
7027 	}
7028 	spin_unlock(&nn->blocked_locks_lock);
7029 
7030 	while (!list_empty(&reaplist)) {
7031 		nbl = list_first_entry(&reaplist,
7032 					struct nfsd4_blocked_lock, nbl_lru);
7033 		list_del_init(&nbl->nbl_lru);
7034 		free_blocked_lock(nbl);
7035 	}
7036 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
7037 	/* service the server-to-server copy delayed unmount list */
7038 	nfsd4_ssc_expire_umount(nn);
7039 #endif
7040 	if (atomic_long_read(&num_delegations) >= max_delegations)
7041 		deleg_reaper(nn);
7042 out:
7043 	return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
7044 }
7045 
7046 static void laundromat_main(struct work_struct *);
7047 
7048 static void
laundromat_main(struct work_struct * laundry)7049 laundromat_main(struct work_struct *laundry)
7050 {
7051 	time64_t t;
7052 	struct delayed_work *dwork = to_delayed_work(laundry);
7053 	struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
7054 					   laundromat_work);
7055 
7056 	t = nfs4_laundromat(nn);
7057 	queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
7058 }
7059 
7060 static void
courtesy_client_reaper(struct nfsd_net * nn)7061 courtesy_client_reaper(struct nfsd_net *nn)
7062 {
7063 	struct list_head reaplist;
7064 
7065 	nfs4_get_courtesy_client_reaplist(nn, &reaplist);
7066 	nfs4_process_client_reaplist(&reaplist);
7067 }
7068 
7069 static void
deleg_reaper(struct nfsd_net * nn)7070 deleg_reaper(struct nfsd_net *nn)
7071 {
7072 	struct list_head *pos, *next;
7073 	struct nfs4_client *clp;
7074 
7075 	spin_lock(&nn->client_lock);
7076 	list_for_each_safe(pos, next, &nn->client_lru) {
7077 		clp = list_entry(pos, struct nfs4_client, cl_lru);
7078 
7079 		if (clp->cl_state != NFSD4_ACTIVE)
7080 			continue;
7081 		if (list_empty(&clp->cl_delegations))
7082 			continue;
7083 		if (atomic_read(&clp->cl_delegs_in_recall))
7084 			continue;
7085 		if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &clp->cl_ra->ra_cb.cb_flags))
7086 			continue;
7087 		if (ktime_get_boottime_seconds() - clp->cl_ra_time < 5)
7088 			continue;
7089 		if (clp->cl_cb_state != NFSD4_CB_UP)
7090 			continue;
7091 
7092 		/* release in nfsd4_cb_recall_any_release */
7093 		kref_get(&clp->cl_nfsdfs.cl_ref);
7094 		clp->cl_ra_time = ktime_get_boottime_seconds();
7095 		clp->cl_ra->ra_keep = 0;
7096 		clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG) |
7097 						BIT(RCA4_TYPE_MASK_WDATA_DLG);
7098 		trace_nfsd_cb_recall_any(clp->cl_ra);
7099 		nfsd4_run_cb(&clp->cl_ra->ra_cb);
7100 	}
7101 	spin_unlock(&nn->client_lock);
7102 }
7103 
7104 static void
nfsd4_state_shrinker_worker(struct work_struct * work)7105 nfsd4_state_shrinker_worker(struct work_struct *work)
7106 {
7107 	struct nfsd_net *nn = container_of(work, struct nfsd_net,
7108 				nfsd_shrinker_work);
7109 
7110 	courtesy_client_reaper(nn);
7111 	deleg_reaper(nn);
7112 }
7113 
nfs4_check_fh(struct svc_fh * fhp,struct nfs4_stid * stp)7114 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
7115 {
7116 	if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
7117 		return nfserr_bad_stateid;
7118 	return nfs_ok;
7119 }
7120 
7121 static
nfs4_check_openmode(struct nfs4_ol_stateid * stp,int flags)7122 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
7123 {
7124         __be32 status = nfserr_openmode;
7125 
7126 	/* For lock stateid's, we test the parent open, not the lock: */
7127 	if (stp->st_openstp)
7128 		stp = stp->st_openstp;
7129 	if ((flags & WR_STATE) && !access_permit_write(stp))
7130                 goto out;
7131 	if ((flags & RD_STATE) && !access_permit_read(stp))
7132                 goto out;
7133 	status = nfs_ok;
7134 out:
7135 	return status;
7136 }
7137 
7138 static inline __be32
check_special_stateids(struct net * net,svc_fh * current_fh,stateid_t * stateid,int flags)7139 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
7140 {
7141 	if (ONE_STATEID(stateid) && (flags & RD_STATE))
7142 		return nfs_ok;
7143 	else if (opens_in_grace(net)) {
7144 		/* Answer in remaining cases depends on existence of
7145 		 * conflicting state; so we must wait out the grace period. */
7146 		return nfserr_grace;
7147 	} else if (flags & WR_STATE)
7148 		return nfs4_share_conflict(current_fh,
7149 				NFS4_SHARE_DENY_WRITE);
7150 	else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
7151 		return nfs4_share_conflict(current_fh,
7152 				NFS4_SHARE_DENY_READ);
7153 }
7154 
check_stateid_generation(stateid_t * in,stateid_t * ref,bool has_session)7155 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
7156 {
7157 	/*
7158 	 * When sessions are used the stateid generation number is ignored
7159 	 * when it is zero.
7160 	 */
7161 	if (has_session && in->si_generation == 0)
7162 		return nfs_ok;
7163 
7164 	if (in->si_generation == ref->si_generation)
7165 		return nfs_ok;
7166 
7167 	/* If the client sends us a stateid from the future, it's buggy: */
7168 	if (nfsd4_stateid_generation_after(in, ref))
7169 		return nfserr_bad_stateid;
7170 	/*
7171 	 * However, we could see a stateid from the past, even from a
7172 	 * non-buggy client.  For example, if the client sends a lock
7173 	 * while some IO is outstanding, the lock may bump si_generation
7174 	 * while the IO is still in flight.  The client could avoid that
7175 	 * situation by waiting for responses on all the IO requests,
7176 	 * but better performance may result in retrying IO that
7177 	 * receives an old_stateid error if requests are rarely
7178 	 * reordered in flight:
7179 	 */
7180 	return nfserr_old_stateid;
7181 }
7182 
nfsd4_stid_check_stateid_generation(stateid_t * in,struct nfs4_stid * s,bool has_session)7183 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
7184 {
7185 	__be32 ret;
7186 
7187 	spin_lock(&s->sc_lock);
7188 	ret = nfsd4_verify_open_stid(s);
7189 	if (ret == nfs_ok)
7190 		ret = check_stateid_generation(in, &s->sc_stateid, has_session);
7191 	spin_unlock(&s->sc_lock);
7192 	if (ret == nfserr_admin_revoked)
7193 		nfsd40_drop_revoked_stid(s->sc_client,
7194 					&s->sc_stateid);
7195 	return ret;
7196 }
7197 
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid * ols)7198 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
7199 {
7200 	if (ols->st_stateowner->so_is_open_owner &&
7201 	    !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
7202 		return nfserr_bad_stateid;
7203 	return nfs_ok;
7204 }
7205 
nfsd4_validate_stateid(struct nfs4_client * cl,stateid_t * stateid)7206 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
7207 {
7208 	struct nfs4_stid *s;
7209 	__be32 status = nfserr_bad_stateid;
7210 
7211 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
7212 		CLOSE_STATEID(stateid))
7213 		return status;
7214 	spin_lock(&cl->cl_lock);
7215 	s = find_stateid_locked(cl, stateid);
7216 	if (!s)
7217 		goto out_unlock;
7218 	status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
7219 	if (status)
7220 		goto out_unlock;
7221 	status = nfsd4_verify_open_stid(s);
7222 	if (status)
7223 		goto out_unlock;
7224 
7225 	switch (s->sc_type) {
7226 	case SC_TYPE_DELEG:
7227 		status = nfs_ok;
7228 		break;
7229 	case SC_TYPE_OPEN:
7230 	case SC_TYPE_LOCK:
7231 		status = nfsd4_check_openowner_confirmed(openlockstateid(s));
7232 		break;
7233 	default:
7234 		printk("unknown stateid type %x\n", s->sc_type);
7235 		status = nfserr_bad_stateid;
7236 	}
7237 out_unlock:
7238 	spin_unlock(&cl->cl_lock);
7239 	if (status == nfserr_admin_revoked)
7240 		nfsd40_drop_revoked_stid(cl, stateid);
7241 	return status;
7242 }
7243 
7244 __be32
nfsd4_lookup_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid,unsigned short typemask,unsigned short statusmask,struct nfs4_stid ** s,struct nfsd_net * nn)7245 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
7246 		     stateid_t *stateid,
7247 		     unsigned short typemask, unsigned short statusmask,
7248 		     struct nfs4_stid **s, struct nfsd_net *nn)
7249 {
7250 	__be32 status;
7251 	struct nfs4_stid *stid;
7252 	bool return_revoked = false;
7253 
7254 	/*
7255 	 *  only return revoked delegations if explicitly asked.
7256 	 *  otherwise we report revoked or bad_stateid status.
7257 	 */
7258 	if (statusmask & SC_STATUS_REVOKED)
7259 		return_revoked = true;
7260 	if (typemask & SC_TYPE_DELEG)
7261 		/* Always allow REVOKED for DELEG so we can
7262 		 * return the appropriate error.
7263 		 */
7264 		statusmask |= SC_STATUS_REVOKED;
7265 
7266 	statusmask |= SC_STATUS_ADMIN_REVOKED | SC_STATUS_FREEABLE;
7267 
7268 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
7269 		CLOSE_STATEID(stateid))
7270 		return nfserr_bad_stateid;
7271 	status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
7272 	if (status == nfserr_stale_clientid) {
7273 		if (cstate->session)
7274 			return nfserr_bad_stateid;
7275 		return nfserr_stale_stateid;
7276 	}
7277 	if (status)
7278 		return status;
7279 	stid = find_stateid_by_type(cstate->clp, stateid, typemask, statusmask);
7280 	if (!stid)
7281 		return nfserr_bad_stateid;
7282 	if ((stid->sc_status & SC_STATUS_REVOKED) && !return_revoked) {
7283 		nfs4_put_stid(stid);
7284 		return nfserr_deleg_revoked;
7285 	}
7286 	if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) {
7287 		nfsd40_drop_revoked_stid(cstate->clp, stateid);
7288 		nfs4_put_stid(stid);
7289 		return nfserr_admin_revoked;
7290 	}
7291 	*s = stid;
7292 	return nfs_ok;
7293 }
7294 
7295 static struct nfsd_file *
nfs4_find_file(struct nfs4_stid * s,int flags)7296 nfs4_find_file(struct nfs4_stid *s, int flags)
7297 {
7298 	struct nfsd_file *ret = NULL;
7299 
7300 	if (!s || s->sc_status)
7301 		return NULL;
7302 
7303 	switch (s->sc_type) {
7304 	case SC_TYPE_DELEG:
7305 	case SC_TYPE_OPEN:
7306 	case SC_TYPE_LOCK:
7307 		if (flags & RD_STATE)
7308 			ret = find_readable_file(s->sc_file);
7309 		else
7310 			ret = find_writeable_file(s->sc_file);
7311 	}
7312 
7313 	return ret;
7314 }
7315 
7316 static __be32
nfs4_check_olstateid(struct nfs4_ol_stateid * ols,int flags)7317 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
7318 {
7319 	__be32 status;
7320 
7321 	status = nfsd4_check_openowner_confirmed(ols);
7322 	if (status)
7323 		return status;
7324 	return nfs4_check_openmode(ols, flags);
7325 }
7326 
7327 static __be32
nfs4_check_file(struct svc_rqst * rqstp,struct svc_fh * fhp,struct nfs4_stid * s,struct nfsd_file ** nfp,int flags)7328 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
7329 		struct nfsd_file **nfp, int flags)
7330 {
7331 	int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
7332 	struct nfsd_file *nf;
7333 	__be32 status;
7334 
7335 	nf = nfs4_find_file(s, flags);
7336 	if (nf) {
7337 		status = nfsd_permission(&rqstp->rq_cred,
7338 					 fhp->fh_export, fhp->fh_dentry,
7339 				acc | NFSD_MAY_OWNER_OVERRIDE);
7340 		if (status) {
7341 			nfsd_file_put(nf);
7342 			goto out;
7343 		}
7344 	} else {
7345 		status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
7346 		if (status)
7347 			return status;
7348 	}
7349 	*nfp = nf;
7350 out:
7351 	return status;
7352 }
7353 static void
_free_cpntf_state_locked(struct nfsd_net * nn,struct nfs4_cpntf_state * cps)7354 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
7355 {
7356 	WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
7357 	if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
7358 		return;
7359 	list_del(&cps->cp_list);
7360 	idr_remove(&nn->s2s_cp_stateids,
7361 		   cps->cp_stateid.cs_stid.si_opaque.so_id);
7362 	kfree(cps);
7363 }
7364 /*
7365  * A READ from an inter server to server COPY will have a
7366  * copy stateid. Look up the copy notify stateid from the
7367  * idr structure and take a reference on it.
7368  */
manage_cpntf_state(struct nfsd_net * nn,stateid_t * st,struct nfs4_client * clp,struct nfs4_cpntf_state ** cps)7369 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
7370 			  struct nfs4_client *clp,
7371 			  struct nfs4_cpntf_state **cps)
7372 {
7373 	copy_stateid_t *cps_t;
7374 	struct nfs4_cpntf_state *state = NULL;
7375 
7376 	if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
7377 		return nfserr_bad_stateid;
7378 	spin_lock(&nn->s2s_cp_lock);
7379 	cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
7380 	if (cps_t) {
7381 		state = container_of(cps_t, struct nfs4_cpntf_state,
7382 				     cp_stateid);
7383 		if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
7384 			state = NULL;
7385 			goto unlock;
7386 		}
7387 		if (!clp)
7388 			refcount_inc(&state->cp_stateid.cs_count);
7389 		else
7390 			_free_cpntf_state_locked(nn, state);
7391 	}
7392 unlock:
7393 	spin_unlock(&nn->s2s_cp_lock);
7394 	if (!state)
7395 		return nfserr_bad_stateid;
7396 	if (!clp)
7397 		*cps = state;
7398 	return 0;
7399 }
7400 
find_cpntf_state(struct nfsd_net * nn,stateid_t * st,struct nfs4_stid ** stid)7401 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
7402 			       struct nfs4_stid **stid)
7403 {
7404 	__be32 status;
7405 	struct nfs4_cpntf_state *cps = NULL;
7406 	struct nfs4_client *found;
7407 
7408 	status = manage_cpntf_state(nn, st, NULL, &cps);
7409 	if (status)
7410 		return status;
7411 
7412 	cps->cpntf_time = ktime_get_boottime_seconds();
7413 
7414 	status = nfserr_expired;
7415 	found = lookup_clientid(&cps->cp_p_clid, true, nn);
7416 	if (!found)
7417 		goto out;
7418 
7419 	*stid = find_stateid_by_type(found, &cps->cp_p_stateid,
7420 				     SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK,
7421 				     0);
7422 	if (*stid)
7423 		status = nfs_ok;
7424 	else
7425 		status = nfserr_bad_stateid;
7426 
7427 	put_client_renew(found);
7428 out:
7429 	nfs4_put_cpntf_state(nn, cps);
7430 	return status;
7431 }
7432 
nfs4_put_cpntf_state(struct nfsd_net * nn,struct nfs4_cpntf_state * cps)7433 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
7434 {
7435 	spin_lock(&nn->s2s_cp_lock);
7436 	_free_cpntf_state_locked(nn, cps);
7437 	spin_unlock(&nn->s2s_cp_lock);
7438 }
7439 
7440 /**
7441  * nfs4_preprocess_stateid_op - find and prep stateid for an operation
7442  * @rqstp: incoming request from client
7443  * @cstate: current compound state
7444  * @fhp: filehandle associated with requested stateid
7445  * @stateid: stateid (provided by client)
7446  * @flags: flags describing type of operation to be done
7447  * @nfp: optional nfsd_file return pointer (may be NULL)
7448  * @cstid: optional returned nfs4_stid pointer (may be NULL)
7449  *
7450  * Given info from the client, look up a nfs4_stid for the operation. On
7451  * success, it returns a reference to the nfs4_stid and/or the nfsd_file
7452  * associated with it.
7453  */
7454 __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)7455 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
7456 		struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
7457 		stateid_t *stateid, int flags, struct nfsd_file **nfp,
7458 		struct nfs4_stid **cstid)
7459 {
7460 	struct net *net = SVC_NET(rqstp);
7461 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7462 	struct nfs4_stid *s = NULL;
7463 	__be32 status;
7464 
7465 	if (nfp)
7466 		*nfp = NULL;
7467 
7468 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
7469 		status = check_special_stateids(net, fhp, stateid, flags);
7470 		goto done;
7471 	}
7472 
7473 	status = nfsd4_lookup_stateid(cstate, stateid,
7474 				SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK,
7475 				0, &s, nn);
7476 	if (status == nfserr_bad_stateid)
7477 		status = find_cpntf_state(nn, stateid, &s);
7478 	if (status)
7479 		return status;
7480 	status = nfsd4_stid_check_stateid_generation(stateid, s,
7481 			nfsd4_has_session(cstate));
7482 	if (status)
7483 		goto out;
7484 
7485 	switch (s->sc_type) {
7486 	case SC_TYPE_DELEG:
7487 		status = nfs4_check_delegmode(delegstateid(s), flags);
7488 		break;
7489 	case SC_TYPE_OPEN:
7490 	case SC_TYPE_LOCK:
7491 		status = nfs4_check_olstateid(openlockstateid(s), flags);
7492 		break;
7493 	}
7494 	if (status)
7495 		goto out;
7496 	status = nfs4_check_fh(fhp, s);
7497 
7498 done:
7499 	if (status == nfs_ok && nfp)
7500 		status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
7501 out:
7502 	if (s) {
7503 		if (!status && cstid)
7504 			*cstid = s;
7505 		else
7506 			nfs4_put_stid(s);
7507 	}
7508 	return status;
7509 }
7510 
7511 /*
7512  * Test if the stateid is valid
7513  */
7514 __be32
nfsd4_test_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7515 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7516 		   union nfsd4_op_u *u)
7517 {
7518 	struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
7519 	struct nfsd4_test_stateid_id *stateid;
7520 	struct nfs4_client *cl = cstate->clp;
7521 
7522 	list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
7523 		stateid->ts_id_status =
7524 			nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
7525 
7526 	return nfs_ok;
7527 }
7528 
7529 static __be32
nfsd4_free_lock_stateid(stateid_t * stateid,struct nfs4_stid * s)7530 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
7531 {
7532 	struct nfs4_ol_stateid *stp = openlockstateid(s);
7533 	__be32 ret;
7534 
7535 	ret = nfsd4_lock_ol_stateid(stp);
7536 	if (ret)
7537 		goto out_put_stid;
7538 
7539 	ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
7540 	if (ret)
7541 		goto out;
7542 
7543 	ret = nfserr_locks_held;
7544 	if (check_for_locks(stp->st_stid.sc_file,
7545 			    lockowner(stp->st_stateowner)))
7546 		goto out;
7547 
7548 	release_lock_stateid(stp);
7549 	ret = nfs_ok;
7550 
7551 out:
7552 	mutex_unlock(&stp->st_mutex);
7553 out_put_stid:
7554 	nfs4_put_stid(s);
7555 	return ret;
7556 }
7557 
7558 __be32
nfsd4_free_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7559 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7560 		   union nfsd4_op_u *u)
7561 {
7562 	struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
7563 	stateid_t *stateid = &free_stateid->fr_stateid;
7564 	struct nfs4_stid *s;
7565 	struct nfs4_delegation *dp;
7566 	struct nfs4_client *cl = cstate->clp;
7567 	__be32 ret = nfserr_bad_stateid;
7568 
7569 	spin_lock(&cl->cl_lock);
7570 	s = find_stateid_locked(cl, stateid);
7571 	if (!s || s->sc_status & SC_STATUS_CLOSED)
7572 		goto out_unlock;
7573 	if (s->sc_status & SC_STATUS_ADMIN_REVOKED) {
7574 		nfsd4_drop_revoked_stid(s);
7575 		ret = nfs_ok;
7576 		goto out;
7577 	}
7578 	spin_lock(&s->sc_lock);
7579 	switch (s->sc_type) {
7580 	case SC_TYPE_DELEG:
7581 		if (s->sc_status & SC_STATUS_REVOKED) {
7582 			s->sc_status |= SC_STATUS_CLOSED;
7583 			spin_unlock(&s->sc_lock);
7584 			dp = delegstateid(s);
7585 			if (s->sc_status & SC_STATUS_FREEABLE)
7586 				list_del_init(&dp->dl_recall_lru);
7587 			s->sc_status |= SC_STATUS_FREED;
7588 			spin_unlock(&cl->cl_lock);
7589 			nfs4_put_stid(s);
7590 			ret = nfs_ok;
7591 			goto out;
7592 		}
7593 		ret = nfserr_locks_held;
7594 		break;
7595 	case SC_TYPE_OPEN:
7596 		ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
7597 		if (ret)
7598 			break;
7599 		ret = nfserr_locks_held;
7600 		break;
7601 	case SC_TYPE_LOCK:
7602 		spin_unlock(&s->sc_lock);
7603 		refcount_inc(&s->sc_count);
7604 		spin_unlock(&cl->cl_lock);
7605 		ret = nfsd4_free_lock_stateid(stateid, s);
7606 		goto out;
7607 	}
7608 	spin_unlock(&s->sc_lock);
7609 out_unlock:
7610 	spin_unlock(&cl->cl_lock);
7611 out:
7612 	return ret;
7613 }
7614 
7615 static inline int
setlkflg(int type)7616 setlkflg (int type)
7617 {
7618 	return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
7619 		RD_STATE : WR_STATE;
7620 }
7621 
nfs4_seqid_op_checks(struct nfsd4_compound_state * cstate,stateid_t * stateid,u32 seqid,struct nfs4_ol_stateid * stp)7622 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
7623 {
7624 	struct svc_fh *current_fh = &cstate->current_fh;
7625 	struct nfs4_stateowner *sop = stp->st_stateowner;
7626 	__be32 status;
7627 
7628 	status = nfsd4_check_seqid(cstate, sop, seqid);
7629 	if (status)
7630 		return status;
7631 	status = nfsd4_lock_ol_stateid(stp);
7632 	if (status != nfs_ok)
7633 		return status;
7634 	status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
7635 	if (status == nfs_ok)
7636 		status = nfs4_check_fh(current_fh, &stp->st_stid);
7637 	if (status != nfs_ok)
7638 		mutex_unlock(&stp->st_mutex);
7639 	return status;
7640 }
7641 
7642 /**
7643  * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op
7644  * @cstate: compund state
7645  * @seqid: seqid (provided by client)
7646  * @stateid: stateid (provided by client)
7647  * @typemask: mask of allowable types for this operation
7648  * @statusmask: mask of allowed states: 0 or STID_CLOSED
7649  * @stpp: return pointer for the stateid found
7650  * @nn: net namespace for request
7651  *
7652  * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and
7653  * return it in @stpp. On a nfs_ok return, the returned stateid will
7654  * have its st_mutex locked.
7655  */
7656 static __be32
nfs4_preprocess_seqid_op(struct nfsd4_compound_state * cstate,u32 seqid,stateid_t * stateid,unsigned short typemask,unsigned short statusmask,struct nfs4_ol_stateid ** stpp,struct nfsd_net * nn)7657 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
7658 			 stateid_t *stateid,
7659 			 unsigned short typemask, unsigned short statusmask,
7660 			 struct nfs4_ol_stateid **stpp,
7661 			 struct nfsd_net *nn)
7662 {
7663 	__be32 status;
7664 	struct nfs4_stid *s;
7665 	struct nfs4_ol_stateid *stp = NULL;
7666 
7667 	trace_nfsd_preprocess(seqid, stateid);
7668 
7669 	*stpp = NULL;
7670 retry:
7671 	status = nfsd4_lookup_stateid(cstate, stateid,
7672 				      typemask, statusmask, &s, nn);
7673 	if (status)
7674 		return status;
7675 	stp = openlockstateid(s);
7676 	if (nfsd4_cstate_assign_replay(cstate, stp->st_stateowner) == -EAGAIN) {
7677 		nfs4_put_stateowner(stp->st_stateowner);
7678 		goto retry;
7679 	}
7680 
7681 	status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
7682 	if (!status)
7683 		*stpp = stp;
7684 	else
7685 		nfs4_put_stid(&stp->st_stid);
7686 	return status;
7687 }
7688 
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state * cstate,u32 seqid,stateid_t * stateid,struct nfs4_ol_stateid ** stpp,struct nfsd_net * nn)7689 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
7690 						 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
7691 {
7692 	__be32 status;
7693 	struct nfs4_openowner *oo;
7694 	struct nfs4_ol_stateid *stp;
7695 
7696 	status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
7697 					  SC_TYPE_OPEN, 0, &stp, nn);
7698 	if (status)
7699 		return status;
7700 	oo = openowner(stp->st_stateowner);
7701 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
7702 		mutex_unlock(&stp->st_mutex);
7703 		nfs4_put_stid(&stp->st_stid);
7704 		return nfserr_bad_stateid;
7705 	}
7706 	*stpp = stp;
7707 	return nfs_ok;
7708 }
7709 
7710 __be32
nfsd4_open_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7711 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7712 		   union nfsd4_op_u *u)
7713 {
7714 	struct nfsd4_open_confirm *oc = &u->open_confirm;
7715 	__be32 status;
7716 	struct nfs4_openowner *oo;
7717 	struct nfs4_ol_stateid *stp;
7718 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7719 
7720 	dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
7721 			cstate->current_fh.fh_dentry);
7722 
7723 	status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
7724 	if (status)
7725 		return status;
7726 
7727 	status = nfs4_preprocess_seqid_op(cstate,
7728 					  oc->oc_seqid, &oc->oc_req_stateid,
7729 					  SC_TYPE_OPEN, 0, &stp, nn);
7730 	if (status)
7731 		goto out;
7732 	oo = openowner(stp->st_stateowner);
7733 	status = nfserr_bad_stateid;
7734 	if (oo->oo_flags & NFS4_OO_CONFIRMED) {
7735 		mutex_unlock(&stp->st_mutex);
7736 		goto put_stateid;
7737 	}
7738 	oo->oo_flags |= NFS4_OO_CONFIRMED;
7739 	nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
7740 	mutex_unlock(&stp->st_mutex);
7741 	trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
7742 	nfsd4_client_record_create(oo->oo_owner.so_client);
7743 	status = nfs_ok;
7744 put_stateid:
7745 	nfs4_put_stid(&stp->st_stid);
7746 out:
7747 	nfsd4_bump_seqid(cstate, status);
7748 	return status;
7749 }
7750 
nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid * stp,u32 access)7751 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
7752 {
7753 	if (!test_access(access, stp))
7754 		return;
7755 	nfs4_file_put_access(stp->st_stid.sc_file, access);
7756 	clear_access(access, stp);
7757 }
7758 
nfs4_stateid_downgrade(struct nfs4_ol_stateid * stp,u32 to_access)7759 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
7760 {
7761 	switch (to_access) {
7762 	case NFS4_SHARE_ACCESS_READ:
7763 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
7764 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7765 		break;
7766 	case NFS4_SHARE_ACCESS_WRITE:
7767 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
7768 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7769 		break;
7770 	case NFS4_SHARE_ACCESS_BOTH:
7771 		break;
7772 	default:
7773 		WARN_ON_ONCE(1);
7774 	}
7775 }
7776 
7777 __be32
nfsd4_open_downgrade(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7778 nfsd4_open_downgrade(struct svc_rqst *rqstp,
7779 		     struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
7780 {
7781 	struct nfsd4_open_downgrade *od = &u->open_downgrade;
7782 	__be32 status;
7783 	struct nfs4_ol_stateid *stp;
7784 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7785 
7786 	dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
7787 			cstate->current_fh.fh_dentry);
7788 
7789 	/* We don't yet support WANT bits: */
7790 	if (od->od_deleg_want)
7791 		dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
7792 			od->od_deleg_want);
7793 
7794 	status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
7795 					&od->od_stateid, &stp, nn);
7796 	if (status)
7797 		goto out;
7798 	status = nfserr_inval;
7799 	if (!test_access(od->od_share_access, stp)) {
7800 		dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
7801 			stp->st_access_bmap, od->od_share_access);
7802 		goto put_stateid;
7803 	}
7804 	if (!test_deny(od->od_share_deny, stp)) {
7805 		dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
7806 			stp->st_deny_bmap, od->od_share_deny);
7807 		goto put_stateid;
7808 	}
7809 	nfs4_stateid_downgrade(stp, od->od_share_access);
7810 	reset_union_bmap_deny(od->od_share_deny, stp);
7811 	nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
7812 	status = nfs_ok;
7813 put_stateid:
7814 	mutex_unlock(&stp->st_mutex);
7815 	nfs4_put_stid(&stp->st_stid);
7816 out:
7817 	nfsd4_bump_seqid(cstate, status);
7818 	return status;
7819 }
7820 
nfsd4_close_open_stateid(struct nfs4_ol_stateid * s)7821 static bool nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
7822 {
7823 	struct nfs4_client *clp = s->st_stid.sc_client;
7824 	bool unhashed;
7825 	LIST_HEAD(reaplist);
7826 	struct nfs4_ol_stateid *stp;
7827 
7828 	spin_lock(&clp->cl_lock);
7829 	unhashed = unhash_open_stateid(s, &reaplist);
7830 
7831 	if (clp->cl_minorversion) {
7832 		if (unhashed)
7833 			put_ol_stateid_locked(s, &reaplist);
7834 		spin_unlock(&clp->cl_lock);
7835 		list_for_each_entry(stp, &reaplist, st_locks)
7836 			nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
7837 		free_ol_stateid_reaplist(&reaplist);
7838 		return false;
7839 	} else {
7840 		spin_unlock(&clp->cl_lock);
7841 		free_ol_stateid_reaplist(&reaplist);
7842 		return unhashed;
7843 	}
7844 }
7845 
7846 /*
7847  * nfs4_unlock_state() called after encode
7848  */
7849 __be32
nfsd4_close(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7850 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7851 		union nfsd4_op_u *u)
7852 {
7853 	struct nfsd4_close *close = &u->close;
7854 	__be32 status;
7855 	struct nfs4_ol_stateid *stp;
7856 	struct net *net = SVC_NET(rqstp);
7857 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7858 	bool need_move_to_close_list;
7859 
7860 	dprintk("NFSD: nfsd4_close on file %pd\n",
7861 			cstate->current_fh.fh_dentry);
7862 
7863 	status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
7864 					  &close->cl_stateid,
7865 					  SC_TYPE_OPEN, SC_STATUS_CLOSED,
7866 					  &stp, nn);
7867 	nfsd4_bump_seqid(cstate, status);
7868 	if (status)
7869 		goto out;
7870 
7871 	spin_lock(&stp->st_stid.sc_client->cl_lock);
7872 	stp->st_stid.sc_status |= SC_STATUS_CLOSED;
7873 	spin_unlock(&stp->st_stid.sc_client->cl_lock);
7874 
7875 	/*
7876 	 * Technically we don't _really_ have to increment or copy it, since
7877 	 * it should just be gone after this operation and we clobber the
7878 	 * copied value below, but we continue to do so here just to ensure
7879 	 * that racing ops see that there was a state change.
7880 	 */
7881 	nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
7882 
7883 	need_move_to_close_list = nfsd4_close_open_stateid(stp);
7884 	mutex_unlock(&stp->st_mutex);
7885 	if (need_move_to_close_list)
7886 		move_to_close_lru(stp, net);
7887 
7888 	/* v4.1+ suggests that we send a special stateid in here, since the
7889 	 * clients should just ignore this anyway. Since this is not useful
7890 	 * for v4.0 clients either, we set it to the special close_stateid
7891 	 * universally.
7892 	 *
7893 	 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
7894 	 */
7895 	memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
7896 
7897 	/* put reference from nfs4_preprocess_seqid_op */
7898 	nfs4_put_stid(&stp->st_stid);
7899 out:
7900 	return status;
7901 }
7902 
7903 __be32
nfsd4_delegreturn(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7904 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7905 		  union nfsd4_op_u *u)
7906 {
7907 	struct nfsd4_delegreturn *dr = &u->delegreturn;
7908 	struct nfs4_delegation *dp;
7909 	stateid_t *stateid = &dr->dr_stateid;
7910 	struct nfs4_stid *s;
7911 	__be32 status;
7912 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7913 
7914 	status = fh_verify(rqstp, &cstate->current_fh, 0, 0);
7915 	if (status)
7916 		return status;
7917 
7918 	status = nfsd4_lookup_stateid(cstate, stateid, SC_TYPE_DELEG, SC_STATUS_REVOKED, &s, nn);
7919 	if (status)
7920 		goto out;
7921 	dp = delegstateid(s);
7922 	status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
7923 	if (status)
7924 		goto put_stateid;
7925 
7926 	trace_nfsd_deleg_return(stateid);
7927 	destroy_delegation(dp);
7928 	smp_mb__after_atomic();
7929 	wake_up_var(d_inode(cstate->current_fh.fh_dentry));
7930 put_stateid:
7931 	nfs4_put_stid(&dp->dl_stid);
7932 out:
7933 	return status;
7934 }
7935 
7936 /* last octet in a range */
7937 static inline u64
last_byte_offset(u64 start,u64 len)7938 last_byte_offset(u64 start, u64 len)
7939 {
7940 	u64 end;
7941 
7942 	WARN_ON_ONCE(!len);
7943 	end = start + len;
7944 	return end > start ? end - 1: NFS4_MAX_UINT64;
7945 }
7946 
7947 /*
7948  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
7949  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
7950  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
7951  * locking, this prevents us from being completely protocol-compliant.  The
7952  * real solution to this problem is to start using unsigned file offsets in
7953  * the VFS, but this is a very deep change!
7954  */
7955 static inline void
nfs4_transform_lock_offset(struct file_lock * lock)7956 nfs4_transform_lock_offset(struct file_lock *lock)
7957 {
7958 	if (lock->fl_start < 0)
7959 		lock->fl_start = OFFSET_MAX;
7960 	if (lock->fl_end < 0)
7961 		lock->fl_end = OFFSET_MAX;
7962 }
7963 
7964 static fl_owner_t
nfsd4_lm_get_owner(fl_owner_t owner)7965 nfsd4_lm_get_owner(fl_owner_t owner)
7966 {
7967 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7968 
7969 	nfs4_get_stateowner(&lo->lo_owner);
7970 	return owner;
7971 }
7972 
7973 static void
nfsd4_lm_put_owner(fl_owner_t owner)7974 nfsd4_lm_put_owner(fl_owner_t owner)
7975 {
7976 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7977 
7978 	if (lo)
7979 		nfs4_put_stateowner(&lo->lo_owner);
7980 }
7981 
7982 /* return pointer to struct nfs4_client if client is expirable */
7983 static bool
nfsd4_lm_lock_expirable(struct file_lock * cfl)7984 nfsd4_lm_lock_expirable(struct file_lock *cfl)
7985 {
7986 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *) cfl->c.flc_owner;
7987 	struct nfs4_client *clp = lo->lo_owner.so_client;
7988 	struct nfsd_net *nn;
7989 
7990 	if (try_to_expire_client(clp)) {
7991 		nn = net_generic(clp->net, nfsd_net_id);
7992 		mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
7993 		return true;
7994 	}
7995 	return false;
7996 }
7997 
7998 /* schedule laundromat to run immediately and wait for it to complete */
7999 static void
nfsd4_lm_expire_lock(void)8000 nfsd4_lm_expire_lock(void)
8001 {
8002 	flush_workqueue(laundry_wq);
8003 }
8004 
8005 static void
nfsd4_lm_notify(struct file_lock * fl)8006 nfsd4_lm_notify(struct file_lock *fl)
8007 {
8008 	struct nfs4_lockowner		*lo = (struct nfs4_lockowner *) fl->c.flc_owner;
8009 	struct net			*net = lo->lo_owner.so_client->net;
8010 	struct nfsd_net			*nn = net_generic(net, nfsd_net_id);
8011 	struct nfsd4_blocked_lock	*nbl = container_of(fl,
8012 						struct nfsd4_blocked_lock, nbl_lock);
8013 	bool queue = false;
8014 
8015 	/* An empty list means that something else is going to be using it */
8016 	spin_lock(&nn->blocked_locks_lock);
8017 	if (!list_empty(&nbl->nbl_list)) {
8018 		list_del_init(&nbl->nbl_list);
8019 		list_del_init(&nbl->nbl_lru);
8020 		queue = true;
8021 	}
8022 	spin_unlock(&nn->blocked_locks_lock);
8023 
8024 	if (queue) {
8025 		trace_nfsd_cb_notify_lock(lo, nbl);
8026 		nfsd4_try_run_cb(&nbl->nbl_cb);
8027 	}
8028 }
8029 
8030 static const struct lock_manager_operations nfsd_posix_mng_ops  = {
8031 	.lm_mod_owner = THIS_MODULE,
8032 	.lm_notify = nfsd4_lm_notify,
8033 	.lm_get_owner = nfsd4_lm_get_owner,
8034 	.lm_put_owner = nfsd4_lm_put_owner,
8035 	.lm_lock_expirable = nfsd4_lm_lock_expirable,
8036 	.lm_expire_lock = nfsd4_lm_expire_lock,
8037 };
8038 
8039 static inline void
nfs4_set_lock_denied(struct file_lock * fl,struct nfsd4_lock_denied * deny)8040 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
8041 {
8042 	struct nfs4_lockowner *lo;
8043 
8044 	if (fl->fl_lmops == &nfsd_posix_mng_ops) {
8045 		lo = (struct nfs4_lockowner *) fl->c.flc_owner;
8046 		xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
8047 						GFP_KERNEL);
8048 		if (!deny->ld_owner.data)
8049 			/* We just don't care that much */
8050 			goto nevermind;
8051 		deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
8052 	} else {
8053 nevermind:
8054 		deny->ld_owner.len = 0;
8055 		deny->ld_owner.data = NULL;
8056 		deny->ld_clientid.cl_boot = 0;
8057 		deny->ld_clientid.cl_id = 0;
8058 	}
8059 	deny->ld_start = fl->fl_start;
8060 	deny->ld_length = NFS4_MAX_UINT64;
8061 	if (fl->fl_end != NFS4_MAX_UINT64)
8062 		deny->ld_length = fl->fl_end - fl->fl_start + 1;
8063 	deny->ld_type = NFS4_READ_LT;
8064 	if (fl->c.flc_type != F_RDLCK)
8065 		deny->ld_type = NFS4_WRITE_LT;
8066 }
8067 
8068 static struct nfs4_lockowner *
find_lockowner_str_locked(struct nfs4_client * clp,struct xdr_netobj * owner)8069 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
8070 {
8071 	unsigned int strhashval = ownerstr_hashval(owner);
8072 	struct nfs4_stateowner *so;
8073 
8074 	lockdep_assert_held(&clp->cl_lock);
8075 
8076 	list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
8077 			    so_strhash) {
8078 		if (so->so_is_open_owner)
8079 			continue;
8080 		if (same_owner_str(so, owner))
8081 			return lockowner(nfs4_get_stateowner(so));
8082 	}
8083 	return NULL;
8084 }
8085 
8086 static struct nfs4_lockowner *
find_lockowner_str(struct nfs4_client * clp,struct xdr_netobj * owner)8087 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
8088 {
8089 	struct nfs4_lockowner *lo;
8090 
8091 	spin_lock(&clp->cl_lock);
8092 	lo = find_lockowner_str_locked(clp, owner);
8093 	spin_unlock(&clp->cl_lock);
8094 	return lo;
8095 }
8096 
nfs4_unhash_lockowner(struct nfs4_stateowner * sop)8097 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
8098 {
8099 	unhash_lockowner_locked(lockowner(sop));
8100 }
8101 
nfs4_free_lockowner(struct nfs4_stateowner * sop)8102 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
8103 {
8104 	struct nfs4_lockowner *lo = lockowner(sop);
8105 
8106 	kmem_cache_free(lockowner_slab, lo);
8107 }
8108 
8109 static const struct nfs4_stateowner_operations lockowner_ops = {
8110 	.so_unhash =	nfs4_unhash_lockowner,
8111 	.so_free =	nfs4_free_lockowner,
8112 };
8113 
8114 /*
8115  * Alloc a lock owner structure.
8116  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
8117  * occurred.
8118  *
8119  * strhashval = ownerstr_hashval
8120  */
8121 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)8122 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
8123 			   struct nfs4_ol_stateid *open_stp,
8124 			   struct nfsd4_lock *lock)
8125 {
8126 	struct nfs4_lockowner *lo, *ret;
8127 
8128 	lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
8129 	if (!lo)
8130 		return NULL;
8131 	INIT_LIST_HEAD(&lo->lo_blocked);
8132 	INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
8133 	lo->lo_owner.so_is_open_owner = 0;
8134 	lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
8135 	lo->lo_owner.so_ops = &lockowner_ops;
8136 	spin_lock(&clp->cl_lock);
8137 	ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
8138 	if (ret == NULL) {
8139 		list_add(&lo->lo_owner.so_strhash,
8140 			 &clp->cl_ownerstr_hashtbl[strhashval]);
8141 		ret = lo;
8142 	} else
8143 		nfs4_free_stateowner(&lo->lo_owner);
8144 
8145 	spin_unlock(&clp->cl_lock);
8146 	return ret;
8147 }
8148 
8149 static struct nfs4_ol_stateid *
find_lock_stateid(const struct nfs4_lockowner * lo,const struct nfs4_ol_stateid * ost)8150 find_lock_stateid(const struct nfs4_lockowner *lo,
8151 		  const struct nfs4_ol_stateid *ost)
8152 {
8153 	struct nfs4_ol_stateid *lst;
8154 
8155 	lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
8156 
8157 	/* If ost is not hashed, ost->st_locks will not be valid */
8158 	if (!nfs4_ol_stateid_unhashed(ost))
8159 		list_for_each_entry(lst, &ost->st_locks, st_locks) {
8160 			if (lst->st_stateowner == &lo->lo_owner) {
8161 				refcount_inc(&lst->st_stid.sc_count);
8162 				return lst;
8163 			}
8164 		}
8165 	return NULL;
8166 }
8167 
8168 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)8169 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
8170 		  struct nfs4_file *fp, struct inode *inode,
8171 		  struct nfs4_ol_stateid *open_stp)
8172 {
8173 	struct nfs4_client *clp = lo->lo_owner.so_client;
8174 	struct nfs4_ol_stateid *retstp;
8175 
8176 	mutex_init(&stp->st_mutex);
8177 	mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
8178 retry:
8179 	spin_lock(&clp->cl_lock);
8180 	if (nfs4_ol_stateid_unhashed(open_stp))
8181 		goto out_close;
8182 	retstp = find_lock_stateid(lo, open_stp);
8183 	if (retstp)
8184 		goto out_found;
8185 	refcount_inc(&stp->st_stid.sc_count);
8186 	stp->st_stid.sc_type = SC_TYPE_LOCK;
8187 	stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
8188 	get_nfs4_file(fp);
8189 	stp->st_stid.sc_file = fp;
8190 	stp->st_access_bmap = 0;
8191 	stp->st_deny_bmap = open_stp->st_deny_bmap;
8192 	stp->st_openstp = open_stp;
8193 	spin_lock(&fp->fi_lock);
8194 	list_add(&stp->st_locks, &open_stp->st_locks);
8195 	list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
8196 	list_add(&stp->st_perfile, &fp->fi_stateids);
8197 	spin_unlock(&fp->fi_lock);
8198 	spin_unlock(&clp->cl_lock);
8199 	return stp;
8200 out_found:
8201 	spin_unlock(&clp->cl_lock);
8202 	if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
8203 		nfs4_put_stid(&retstp->st_stid);
8204 		goto retry;
8205 	}
8206 	/* To keep mutex tracking happy */
8207 	mutex_unlock(&stp->st_mutex);
8208 	return retstp;
8209 out_close:
8210 	spin_unlock(&clp->cl_lock);
8211 	mutex_unlock(&stp->st_mutex);
8212 	return NULL;
8213 }
8214 
8215 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)8216 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
8217 			    struct inode *inode, struct nfs4_ol_stateid *ost,
8218 			    bool *new)
8219 {
8220 	struct nfs4_stid *ns = NULL;
8221 	struct nfs4_ol_stateid *lst;
8222 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
8223 	struct nfs4_client *clp = oo->oo_owner.so_client;
8224 
8225 	*new = false;
8226 	spin_lock(&clp->cl_lock);
8227 	lst = find_lock_stateid(lo, ost);
8228 	spin_unlock(&clp->cl_lock);
8229 	if (lst != NULL) {
8230 		if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
8231 			goto out;
8232 		nfs4_put_stid(&lst->st_stid);
8233 	}
8234 	ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
8235 	if (ns == NULL)
8236 		return NULL;
8237 
8238 	lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
8239 	if (lst == openlockstateid(ns))
8240 		*new = true;
8241 	else
8242 		nfs4_put_stid(ns);
8243 out:
8244 	return lst;
8245 }
8246 
8247 static int
check_lock_length(u64 offset,u64 length)8248 check_lock_length(u64 offset, u64 length)
8249 {
8250 	return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
8251 		(length > ~offset)));
8252 }
8253 
get_lock_access(struct nfs4_ol_stateid * lock_stp,u32 access)8254 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
8255 {
8256 	struct nfs4_file *fp = lock_stp->st_stid.sc_file;
8257 
8258 	lockdep_assert_held(&fp->fi_lock);
8259 
8260 	if (test_access(access, lock_stp))
8261 		return;
8262 	__nfs4_file_get_access(fp, access);
8263 	set_access(access, lock_stp);
8264 }
8265 
8266 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)8267 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
8268 			    struct nfs4_ol_stateid *ost,
8269 			    struct nfsd4_lock *lock,
8270 			    struct nfs4_ol_stateid **plst, bool *new)
8271 {
8272 	__be32 status;
8273 	struct nfs4_file *fi = ost->st_stid.sc_file;
8274 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
8275 	struct nfs4_client *cl = oo->oo_owner.so_client;
8276 	struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
8277 	struct nfs4_lockowner *lo;
8278 	struct nfs4_ol_stateid *lst;
8279 	unsigned int strhashval;
8280 
8281 	lo = find_lockowner_str(cl, &lock->lk_new_owner);
8282 	if (!lo) {
8283 		strhashval = ownerstr_hashval(&lock->lk_new_owner);
8284 		lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
8285 		if (lo == NULL)
8286 			return nfserr_jukebox;
8287 	} else {
8288 		/* with an existing lockowner, seqids must be the same */
8289 		status = nfserr_bad_seqid;
8290 		if (!cstate->minorversion &&
8291 		    lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
8292 			goto out;
8293 	}
8294 
8295 	lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
8296 	if (lst == NULL) {
8297 		status = nfserr_jukebox;
8298 		goto out;
8299 	}
8300 
8301 	status = nfs_ok;
8302 	*plst = lst;
8303 out:
8304 	nfs4_put_stateowner(&lo->lo_owner);
8305 	return status;
8306 }
8307 
8308 /*
8309  *  LOCK operation
8310  */
8311 __be32
nfsd4_lock(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8312 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8313 	   union nfsd4_op_u *u)
8314 {
8315 	struct nfsd4_lock *lock = &u->lock;
8316 	struct nfs4_openowner *open_sop = NULL;
8317 	struct nfs4_lockowner *lock_sop = NULL;
8318 	struct nfs4_ol_stateid *lock_stp = NULL;
8319 	struct nfs4_ol_stateid *open_stp = NULL;
8320 	struct nfs4_file *fp;
8321 	struct nfsd_file *nf = NULL;
8322 	struct nfsd4_blocked_lock *nbl = NULL;
8323 	struct file_lock *file_lock = NULL;
8324 	struct file_lock *conflock = NULL;
8325 	__be32 status = 0;
8326 	int lkflg;
8327 	int err;
8328 	bool new = false;
8329 	unsigned char type;
8330 	unsigned int flags = FL_POSIX;
8331 	struct net *net = SVC_NET(rqstp);
8332 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8333 
8334 	dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
8335 		(long long) lock->lk_offset,
8336 		(long long) lock->lk_length);
8337 
8338 	if (check_lock_length(lock->lk_offset, lock->lk_length))
8339 		 return nfserr_inval;
8340 
8341 	status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
8342 	if (status != nfs_ok)
8343 		return status;
8344 	if (exportfs_cannot_lock(cstate->current_fh.fh_dentry->d_sb->s_export_op)) {
8345 		status = nfserr_notsupp;
8346 		goto out;
8347 	}
8348 
8349 	if (lock->lk_is_new) {
8350 		if (nfsd4_has_session(cstate))
8351 			/* See rfc 5661 18.10.3: given clientid is ignored: */
8352 			memcpy(&lock->lk_new_clientid,
8353 				&cstate->clp->cl_clientid,
8354 				sizeof(clientid_t));
8355 
8356 		/* validate and update open stateid and open seqid */
8357 		status = nfs4_preprocess_confirmed_seqid_op(cstate,
8358 				        lock->lk_new_open_seqid,
8359 		                        &lock->lk_new_open_stateid,
8360 					&open_stp, nn);
8361 		if (status)
8362 			goto out;
8363 		mutex_unlock(&open_stp->st_mutex);
8364 		open_sop = openowner(open_stp->st_stateowner);
8365 		status = nfserr_bad_stateid;
8366 		if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
8367 						&lock->lk_new_clientid))
8368 			goto out;
8369 		status = lookup_or_create_lock_state(cstate, open_stp, lock,
8370 							&lock_stp, &new);
8371 	} else {
8372 		status = nfs4_preprocess_seqid_op(cstate,
8373 						  lock->lk_old_lock_seqid,
8374 						  &lock->lk_old_lock_stateid,
8375 						  SC_TYPE_LOCK, 0, &lock_stp,
8376 						  nn);
8377 	}
8378 	if (status)
8379 		goto out;
8380 	lock_sop = lockowner(lock_stp->st_stateowner);
8381 
8382 	lkflg = setlkflg(lock->lk_type);
8383 	status = nfs4_check_openmode(lock_stp, lkflg);
8384 	if (status)
8385 		goto out;
8386 
8387 	status = nfserr_grace;
8388 	if (locks_in_grace(net) && !lock->lk_reclaim)
8389 		goto out;
8390 	status = nfserr_no_grace;
8391 	if (!locks_in_grace(net) && lock->lk_reclaim)
8392 		goto out;
8393 
8394 	if (lock->lk_reclaim)
8395 		flags |= FL_RECLAIM;
8396 
8397 	fp = lock_stp->st_stid.sc_file;
8398 	switch (lock->lk_type) {
8399 		case NFS4_READW_LT:
8400 			fallthrough;
8401 		case NFS4_READ_LT:
8402 			spin_lock(&fp->fi_lock);
8403 			nf = find_readable_file_locked(fp);
8404 			if (nf)
8405 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
8406 			spin_unlock(&fp->fi_lock);
8407 			type = F_RDLCK;
8408 			break;
8409 		case NFS4_WRITEW_LT:
8410 			fallthrough;
8411 		case NFS4_WRITE_LT:
8412 			spin_lock(&fp->fi_lock);
8413 			nf = find_writeable_file_locked(fp);
8414 			if (nf)
8415 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
8416 			spin_unlock(&fp->fi_lock);
8417 			type = F_WRLCK;
8418 			break;
8419 		default:
8420 			status = nfserr_inval;
8421 		goto out;
8422 	}
8423 
8424 	if (!nf) {
8425 		status = nfserr_openmode;
8426 		goto out;
8427 	}
8428 
8429 	if (lock->lk_type & (NFS4_READW_LT | NFS4_WRITEW_LT) &&
8430 		nfsd4_has_session(cstate) &&
8431 		locks_can_async_lock(nf->nf_file->f_op))
8432 			flags |= FL_SLEEP;
8433 
8434 	nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
8435 	if (!nbl) {
8436 		dprintk("NFSD: %s: unable to allocate block!\n", __func__);
8437 		status = nfserr_jukebox;
8438 		goto out;
8439 	}
8440 
8441 	file_lock = &nbl->nbl_lock;
8442 	file_lock->c.flc_type = type;
8443 	file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
8444 	file_lock->c.flc_pid = current->tgid;
8445 	file_lock->c.flc_file = nf->nf_file;
8446 	file_lock->c.flc_flags = flags;
8447 	file_lock->fl_lmops = &nfsd_posix_mng_ops;
8448 	file_lock->fl_start = lock->lk_offset;
8449 	file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
8450 	nfs4_transform_lock_offset(file_lock);
8451 
8452 	conflock = locks_alloc_lock();
8453 	if (!conflock) {
8454 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8455 		status = nfserr_jukebox;
8456 		goto out;
8457 	}
8458 
8459 	if (flags & FL_SLEEP) {
8460 		nbl->nbl_time = ktime_get_boottime_seconds();
8461 		spin_lock(&nn->blocked_locks_lock);
8462 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
8463 		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
8464 		kref_get(&nbl->nbl_kref);
8465 		spin_unlock(&nn->blocked_locks_lock);
8466 	}
8467 
8468 	err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
8469 	switch (err) {
8470 	case 0: /* success! */
8471 		nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
8472 		status = 0;
8473 		if (lock->lk_reclaim)
8474 			nn->somebody_reclaimed = true;
8475 		break;
8476 	case FILE_LOCK_DEFERRED:
8477 		kref_put(&nbl->nbl_kref, free_nbl);
8478 		nbl = NULL;
8479 		fallthrough;
8480 	case -EAGAIN:		/* conflock holds conflicting lock */
8481 		status = nfserr_denied;
8482 		dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
8483 		nfs4_set_lock_denied(conflock, &lock->lk_denied);
8484 		break;
8485 	case -EDEADLK:
8486 		status = nfserr_deadlock;
8487 		break;
8488 	default:
8489 		dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
8490 		status = nfserrno(err);
8491 		break;
8492 	}
8493 out:
8494 	if (nbl) {
8495 		/* dequeue it if we queued it before */
8496 		if (flags & FL_SLEEP) {
8497 			spin_lock(&nn->blocked_locks_lock);
8498 			if (!list_empty(&nbl->nbl_list) &&
8499 			    !list_empty(&nbl->nbl_lru)) {
8500 				list_del_init(&nbl->nbl_list);
8501 				list_del_init(&nbl->nbl_lru);
8502 				kref_put(&nbl->nbl_kref, free_nbl);
8503 			}
8504 			/* nbl can use one of lists to be linked to reaplist */
8505 			spin_unlock(&nn->blocked_locks_lock);
8506 		}
8507 		free_blocked_lock(nbl);
8508 	}
8509 	if (nf)
8510 		nfsd_file_put(nf);
8511 	if (lock_stp) {
8512 		/* Bump seqid manually if the 4.0 replay owner is openowner */
8513 		if (cstate->replay_owner &&
8514 		    cstate->replay_owner != &lock_sop->lo_owner &&
8515 		    seqid_mutating_err(ntohl(status)))
8516 			lock_sop->lo_owner.so_seqid++;
8517 
8518 		/*
8519 		 * If this is a new, never-before-used stateid, and we are
8520 		 * returning an error, then just go ahead and release it.
8521 		 */
8522 		if (status && new)
8523 			release_lock_stateid(lock_stp);
8524 
8525 		mutex_unlock(&lock_stp->st_mutex);
8526 
8527 		nfs4_put_stid(&lock_stp->st_stid);
8528 	}
8529 	if (open_stp)
8530 		nfs4_put_stid(&open_stp->st_stid);
8531 	nfsd4_bump_seqid(cstate, status);
8532 	if (conflock)
8533 		locks_free_lock(conflock);
8534 	return status;
8535 }
8536 
nfsd4_lock_release(union nfsd4_op_u * u)8537 void nfsd4_lock_release(union nfsd4_op_u *u)
8538 {
8539 	struct nfsd4_lock *lock = &u->lock;
8540 	struct nfsd4_lock_denied *deny = &lock->lk_denied;
8541 
8542 	kfree(deny->ld_owner.data);
8543 }
8544 
8545 /*
8546  * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
8547  * so we do a temporary open here just to get an open file to pass to
8548  * vfs_test_lock.
8549  */
nfsd_test_lock(struct svc_rqst * rqstp,struct svc_fh * fhp,struct file_lock * lock)8550 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
8551 {
8552 	struct nfsd_file *nf;
8553 	struct inode *inode;
8554 	__be32 err;
8555 
8556 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
8557 	if (err)
8558 		return err;
8559 	inode = fhp->fh_dentry->d_inode;
8560 	inode_lock(inode); /* to block new leases till after test_lock: */
8561 	err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
8562 	if (err)
8563 		goto out;
8564 	lock->c.flc_file = nf->nf_file;
8565 	err = nfserrno(vfs_test_lock(nf->nf_file, lock));
8566 	lock->c.flc_file = NULL;
8567 out:
8568 	inode_unlock(inode);
8569 	nfsd_file_put(nf);
8570 	return err;
8571 }
8572 
8573 /*
8574  * LOCKT operation
8575  */
8576 __be32
nfsd4_lockt(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8577 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8578 	    union nfsd4_op_u *u)
8579 {
8580 	struct nfsd4_lockt *lockt = &u->lockt;
8581 	struct file_lock *file_lock = NULL;
8582 	struct nfs4_lockowner *lo = NULL;
8583 	__be32 status;
8584 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8585 
8586 	if (locks_in_grace(SVC_NET(rqstp)))
8587 		return nfserr_grace;
8588 
8589 	if (check_lock_length(lockt->lt_offset, lockt->lt_length))
8590 		 return nfserr_inval;
8591 
8592 	if (!nfsd4_has_session(cstate)) {
8593 		status = set_client(&lockt->lt_clientid, cstate, nn);
8594 		if (status)
8595 			goto out;
8596 	}
8597 
8598 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
8599 		goto out;
8600 
8601 	file_lock = locks_alloc_lock();
8602 	if (!file_lock) {
8603 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8604 		status = nfserr_jukebox;
8605 		goto out;
8606 	}
8607 
8608 	switch (lockt->lt_type) {
8609 		case NFS4_READ_LT:
8610 		case NFS4_READW_LT:
8611 			file_lock->c.flc_type = F_RDLCK;
8612 			break;
8613 		case NFS4_WRITE_LT:
8614 		case NFS4_WRITEW_LT:
8615 			file_lock->c.flc_type = F_WRLCK;
8616 			break;
8617 		default:
8618 			dprintk("NFSD: nfs4_lockt: bad lock type!\n");
8619 			status = nfserr_inval;
8620 			goto out;
8621 	}
8622 
8623 	lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
8624 	if (lo)
8625 		file_lock->c.flc_owner = (fl_owner_t)lo;
8626 	file_lock->c.flc_pid = current->tgid;
8627 	file_lock->c.flc_flags = FL_POSIX;
8628 
8629 	file_lock->fl_start = lockt->lt_offset;
8630 	file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
8631 
8632 	nfs4_transform_lock_offset(file_lock);
8633 
8634 	status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
8635 	if (status)
8636 		goto out;
8637 
8638 	if (file_lock->c.flc_type != F_UNLCK) {
8639 		status = nfserr_denied;
8640 		nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
8641 	}
8642 out:
8643 	if (lo)
8644 		nfs4_put_stateowner(&lo->lo_owner);
8645 	if (file_lock)
8646 		locks_free_lock(file_lock);
8647 	return status;
8648 }
8649 
nfsd4_lockt_release(union nfsd4_op_u * u)8650 void nfsd4_lockt_release(union nfsd4_op_u *u)
8651 {
8652 	struct nfsd4_lockt *lockt = &u->lockt;
8653 	struct nfsd4_lock_denied *deny = &lockt->lt_denied;
8654 
8655 	kfree(deny->ld_owner.data);
8656 }
8657 
8658 __be32
nfsd4_locku(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8659 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8660 	    union nfsd4_op_u *u)
8661 {
8662 	struct nfsd4_locku *locku = &u->locku;
8663 	struct nfs4_ol_stateid *stp;
8664 	struct nfsd_file *nf = NULL;
8665 	struct file_lock *file_lock = NULL;
8666 	__be32 status;
8667 	int err;
8668 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8669 
8670 	dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
8671 		(long long) locku->lu_offset,
8672 		(long long) locku->lu_length);
8673 
8674 	if (check_lock_length(locku->lu_offset, locku->lu_length))
8675 		 return nfserr_inval;
8676 
8677 	status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
8678 					  &locku->lu_stateid, SC_TYPE_LOCK, 0,
8679 					  &stp, nn);
8680 	if (status)
8681 		goto out;
8682 	nf = find_any_file(stp->st_stid.sc_file);
8683 	if (!nf) {
8684 		status = nfserr_lock_range;
8685 		goto put_stateid;
8686 	}
8687 	if (exportfs_cannot_lock(nf->nf_file->f_path.mnt->mnt_sb->s_export_op)) {
8688 		status = nfserr_notsupp;
8689 		goto put_file;
8690 	}
8691 
8692 	file_lock = locks_alloc_lock();
8693 	if (!file_lock) {
8694 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8695 		status = nfserr_jukebox;
8696 		goto put_file;
8697 	}
8698 
8699 	file_lock->c.flc_type = F_UNLCK;
8700 	file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
8701 	file_lock->c.flc_pid = current->tgid;
8702 	file_lock->c.flc_file = nf->nf_file;
8703 	file_lock->c.flc_flags = FL_POSIX;
8704 	file_lock->fl_lmops = &nfsd_posix_mng_ops;
8705 	file_lock->fl_start = locku->lu_offset;
8706 
8707 	file_lock->fl_end = last_byte_offset(locku->lu_offset,
8708 						locku->lu_length);
8709 	nfs4_transform_lock_offset(file_lock);
8710 
8711 	err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
8712 	if (err) {
8713 		dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
8714 		goto out_nfserr;
8715 	}
8716 	nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
8717 put_file:
8718 	nfsd_file_put(nf);
8719 put_stateid:
8720 	mutex_unlock(&stp->st_mutex);
8721 	nfs4_put_stid(&stp->st_stid);
8722 out:
8723 	nfsd4_bump_seqid(cstate, status);
8724 	if (file_lock)
8725 		locks_free_lock(file_lock);
8726 	return status;
8727 
8728 out_nfserr:
8729 	status = nfserrno(err);
8730 	goto put_file;
8731 }
8732 
8733 /*
8734  * returns
8735  * 	true:  locks held by lockowner
8736  * 	false: no locks held by lockowner
8737  */
8738 static bool
check_for_locks(struct nfs4_file * fp,struct nfs4_lockowner * lowner)8739 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
8740 {
8741 	struct file_lock *fl;
8742 	int status = false;
8743 	struct nfsd_file *nf;
8744 	struct inode *inode;
8745 	struct file_lock_context *flctx;
8746 
8747 	spin_lock(&fp->fi_lock);
8748 	nf = find_any_file_locked(fp);
8749 	if (!nf) {
8750 		/* Any valid lock stateid should have some sort of access */
8751 		WARN_ON_ONCE(1);
8752 		goto out;
8753 	}
8754 
8755 	inode = file_inode(nf->nf_file);
8756 	flctx = locks_inode_context(inode);
8757 
8758 	if (flctx && !list_empty_careful(&flctx->flc_posix)) {
8759 		spin_lock(&flctx->flc_lock);
8760 		for_each_file_lock(fl, &flctx->flc_posix) {
8761 			if (fl->c.flc_owner == (fl_owner_t)lowner) {
8762 				status = true;
8763 				break;
8764 			}
8765 		}
8766 		spin_unlock(&flctx->flc_lock);
8767 	}
8768 out:
8769 	spin_unlock(&fp->fi_lock);
8770 	return status;
8771 }
8772 
8773 /**
8774  * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
8775  * @rqstp: RPC transaction
8776  * @cstate: NFSv4 COMPOUND state
8777  * @u: RELEASE_LOCKOWNER arguments
8778  *
8779  * Check if there are any locks still held and if not, free the lockowner
8780  * and any lock state that is owned.
8781  *
8782  * Return values:
8783  *   %nfs_ok: lockowner released or not found
8784  *   %nfserr_locks_held: lockowner still in use
8785  *   %nfserr_stale_clientid: clientid no longer active
8786  *   %nfserr_expired: clientid not recognized
8787  */
8788 __be32
nfsd4_release_lockowner(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8789 nfsd4_release_lockowner(struct svc_rqst *rqstp,
8790 			struct nfsd4_compound_state *cstate,
8791 			union nfsd4_op_u *u)
8792 {
8793 	struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
8794 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8795 	clientid_t *clid = &rlockowner->rl_clientid;
8796 	struct nfs4_ol_stateid *stp;
8797 	struct nfs4_lockowner *lo;
8798 	struct nfs4_client *clp;
8799 	LIST_HEAD(reaplist);
8800 	__be32 status;
8801 
8802 	dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
8803 		clid->cl_boot, clid->cl_id);
8804 
8805 	status = set_client(clid, cstate, nn);
8806 	if (status)
8807 		return status;
8808 	clp = cstate->clp;
8809 
8810 	spin_lock(&clp->cl_lock);
8811 	lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
8812 	if (!lo) {
8813 		spin_unlock(&clp->cl_lock);
8814 		return nfs_ok;
8815 	}
8816 
8817 	list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
8818 		if (check_for_locks(stp->st_stid.sc_file, lo)) {
8819 			spin_unlock(&clp->cl_lock);
8820 			nfs4_put_stateowner(&lo->lo_owner);
8821 			return nfserr_locks_held;
8822 		}
8823 	}
8824 	unhash_lockowner_locked(lo);
8825 	while (!list_empty(&lo->lo_owner.so_stateids)) {
8826 		stp = list_first_entry(&lo->lo_owner.so_stateids,
8827 				       struct nfs4_ol_stateid,
8828 				       st_perstateowner);
8829 		unhash_lock_stateid(stp);
8830 		put_ol_stateid_locked(stp, &reaplist);
8831 	}
8832 	spin_unlock(&clp->cl_lock);
8833 
8834 	free_ol_stateid_reaplist(&reaplist);
8835 	remove_blocked_locks(lo);
8836 	nfs4_put_stateowner(&lo->lo_owner);
8837 	return nfs_ok;
8838 }
8839 
8840 static inline struct nfs4_client_reclaim *
alloc_reclaim(void)8841 alloc_reclaim(void)
8842 {
8843 	return kmalloc_obj(struct nfs4_client_reclaim);
8844 }
8845 
8846 bool
nfs4_has_reclaimed_state(struct xdr_netobj name,struct nfsd_net * nn)8847 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
8848 {
8849 	struct nfs4_client_reclaim *crp;
8850 
8851 	crp = nfsd4_find_reclaim_client(name, nn);
8852 	return (crp && crp->cr_clp);
8853 }
8854 
8855 /*
8856  * failure => all reset bets are off, nfserr_no_grace...
8857  */
8858 struct nfs4_client_reclaim *
nfs4_client_to_reclaim(struct xdr_netobj name,struct xdr_netobj princhash,struct nfsd_net * nn)8859 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
8860 		struct nfsd_net *nn)
8861 {
8862 	unsigned int strhashval;
8863 	struct nfs4_client_reclaim *crp;
8864 
8865 	name.data = kmemdup(name.data, name.len, GFP_KERNEL);
8866 	if (!name.data) {
8867 		dprintk("%s: failed to allocate memory for name.data!\n",
8868 			__func__);
8869 		return NULL;
8870 	}
8871 	if (princhash.len) {
8872 		princhash.data = kmemdup(princhash.data, princhash.len, GFP_KERNEL);
8873 		if (!princhash.data) {
8874 			dprintk("%s: failed to allocate memory for princhash.data!\n",
8875 				__func__);
8876 			kfree(name.data);
8877 			return NULL;
8878 		}
8879 	} else
8880 		princhash.data = NULL;
8881 	crp = alloc_reclaim();
8882 	if (crp) {
8883 		strhashval = clientstr_hashval(name);
8884 		INIT_LIST_HEAD(&crp->cr_strhash);
8885 		list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
8886 		crp->cr_name.data = name.data;
8887 		crp->cr_name.len = name.len;
8888 		crp->cr_princhash.data = princhash.data;
8889 		crp->cr_princhash.len = princhash.len;
8890 		crp->cr_clp = NULL;
8891 		nn->reclaim_str_hashtbl_size++;
8892 	} else {
8893 		kfree(name.data);
8894 		kfree(princhash.data);
8895 	}
8896 	return crp;
8897 }
8898 
8899 void
nfs4_remove_reclaim_record(struct nfs4_client_reclaim * crp,struct nfsd_net * nn)8900 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
8901 {
8902 	list_del(&crp->cr_strhash);
8903 	kfree(crp->cr_name.data);
8904 	kfree(crp->cr_princhash.data);
8905 	kfree(crp);
8906 	nn->reclaim_str_hashtbl_size--;
8907 }
8908 
8909 void
nfs4_release_reclaim(struct nfsd_net * nn)8910 nfs4_release_reclaim(struct nfsd_net *nn)
8911 {
8912 	struct nfs4_client_reclaim *crp = NULL;
8913 	int i;
8914 
8915 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8916 		while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
8917 			crp = list_entry(nn->reclaim_str_hashtbl[i].next,
8918 			                struct nfs4_client_reclaim, cr_strhash);
8919 			nfs4_remove_reclaim_record(crp, nn);
8920 		}
8921 	}
8922 	WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
8923 }
8924 
8925 /*
8926  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
8927 struct nfs4_client_reclaim *
nfsd4_find_reclaim_client(struct xdr_netobj name,struct nfsd_net * nn)8928 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
8929 {
8930 	unsigned int strhashval;
8931 	struct nfs4_client_reclaim *crp = NULL;
8932 
8933 	strhashval = clientstr_hashval(name);
8934 	list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
8935 		if (compare_blob(&crp->cr_name, &name) == 0) {
8936 			return crp;
8937 		}
8938 	}
8939 	return NULL;
8940 }
8941 
8942 __be32
nfs4_check_open_reclaim(struct nfs4_client * clp)8943 nfs4_check_open_reclaim(struct nfs4_client *clp)
8944 {
8945 	if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
8946 		return nfserr_no_grace;
8947 
8948 	if (nfsd4_client_record_check(clp))
8949 		return nfserr_reclaim_bad;
8950 
8951 	return nfs_ok;
8952 }
8953 
8954 /*
8955  * Since the lifetime of a delegation isn't limited to that of an open, a
8956  * client may quite reasonably hang on to a delegation as long as it has
8957  * the inode cached.  This becomes an obvious problem the first time a
8958  * client's inode cache approaches the size of the server's total memory.
8959  *
8960  * For now we avoid this problem by imposing a hard limit on the number
8961  * of delegations, which varies according to the server's memory size.
8962  */
8963 static void
set_max_delegations(void)8964 set_max_delegations(void)
8965 {
8966 	/*
8967 	 * Allow at most 4 delegations per megabyte of RAM.  Quick
8968 	 * estimates suggest that in the worst case (where every delegation
8969 	 * is for a different inode), a delegation could take about 1.5K,
8970 	 * giving a worst case usage of about 6% of memory.
8971 	 */
8972 	max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
8973 }
8974 
nfs4_state_create_net(struct net * net)8975 static int nfs4_state_create_net(struct net *net)
8976 {
8977 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8978 	int i;
8979 
8980 	nn->conf_id_hashtbl = kmalloc_objs(struct list_head, CLIENT_HASH_SIZE);
8981 	if (!nn->conf_id_hashtbl)
8982 		goto err;
8983 	nn->unconf_id_hashtbl = kmalloc_objs(struct list_head, CLIENT_HASH_SIZE);
8984 	if (!nn->unconf_id_hashtbl)
8985 		goto err_unconf_id;
8986 	nn->sessionid_hashtbl = kmalloc_objs(struct list_head,
8987 					     SESSION_HASH_SIZE);
8988 	if (!nn->sessionid_hashtbl)
8989 		goto err_sessionid;
8990 
8991 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8992 		INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
8993 		INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
8994 	}
8995 	for (i = 0; i < SESSION_HASH_SIZE; i++)
8996 		INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
8997 	nn->conf_name_tree = RB_ROOT;
8998 	nn->unconf_name_tree = RB_ROOT;
8999 	nn->boot_time = ktime_get_real_seconds();
9000 	nn->grace_ended = false;
9001 	nn->grace_end_forced = false;
9002 	nn->nfsd4_manager.block_opens = true;
9003 	INIT_LIST_HEAD(&nn->nfsd4_manager.list);
9004 	INIT_LIST_HEAD(&nn->client_lru);
9005 	INIT_LIST_HEAD(&nn->close_lru);
9006 	INIT_LIST_HEAD(&nn->del_recall_lru);
9007 	spin_lock_init(&nn->deleg_lock);
9008 	spin_lock_init(&nn->client_lock);
9009 	spin_lock_init(&nn->s2s_cp_lock);
9010 	idr_init(&nn->s2s_cp_stateids);
9011 	atomic_set(&nn->pending_async_copies, 0);
9012 
9013 	spin_lock_init(&nn->blocked_locks_lock);
9014 	INIT_LIST_HEAD(&nn->blocked_locks_lru);
9015 
9016 	INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
9017 	/* Make sure this cannot run until client tracking is initialised */
9018 	disable_delayed_work(&nn->laundromat_work);
9019 	INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker);
9020 	get_net(net);
9021 
9022 	nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client");
9023 	if (!nn->nfsd_client_shrinker)
9024 		goto err_shrinker;
9025 
9026 	nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan;
9027 	nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count;
9028 	nn->nfsd_client_shrinker->private_data = nn;
9029 
9030 	shrinker_register(nn->nfsd_client_shrinker);
9031 
9032 	return 0;
9033 
9034 err_shrinker:
9035 	put_net(net);
9036 	kfree(nn->sessionid_hashtbl);
9037 err_sessionid:
9038 	kfree(nn->unconf_id_hashtbl);
9039 err_unconf_id:
9040 	kfree(nn->conf_id_hashtbl);
9041 err:
9042 	return -ENOMEM;
9043 }
9044 
9045 static void
nfs4_state_destroy_net(struct net * net)9046 nfs4_state_destroy_net(struct net *net)
9047 {
9048 	int i;
9049 	struct nfs4_client *clp = NULL;
9050 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9051 
9052 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
9053 		while (!list_empty(&nn->conf_id_hashtbl[i])) {
9054 			clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
9055 			destroy_client(clp);
9056 		}
9057 	}
9058 
9059 	WARN_ON(!list_empty(&nn->blocked_locks_lru));
9060 
9061 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
9062 		while (!list_empty(&nn->unconf_id_hashtbl[i])) {
9063 			clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
9064 			destroy_client(clp);
9065 		}
9066 	}
9067 
9068 	kfree(nn->sessionid_hashtbl);
9069 	kfree(nn->unconf_id_hashtbl);
9070 	kfree(nn->conf_id_hashtbl);
9071 	put_net(net);
9072 }
9073 
9074 int
nfs4_state_start_net(struct net * net)9075 nfs4_state_start_net(struct net *net)
9076 {
9077 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9078 	int ret;
9079 
9080 	ret = nfs4_state_create_net(net);
9081 	if (ret)
9082 		return ret;
9083 	locks_start_grace(net, &nn->nfsd4_manager);
9084 	nfsd4_client_tracking_init(net);
9085 	/* safe for laundromat to run now */
9086 	enable_delayed_work(&nn->laundromat_work);
9087 	if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
9088 		goto skip_grace;
9089 	printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
9090 	       nn->nfsd4_grace, net->ns.inum);
9091 	trace_nfsd_grace_start(nn);
9092 	queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
9093 	return 0;
9094 
9095 skip_grace:
9096 	printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
9097 			net->ns.inum);
9098 	queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
9099 	nfsd4_end_grace(nn);
9100 	return 0;
9101 }
9102 
9103 /* initialization to perform when the nfsd service is started: */
9104 int
nfs4_state_start(void)9105 nfs4_state_start(void)
9106 {
9107 	int ret;
9108 
9109 	ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params);
9110 	if (ret)
9111 		return ret;
9112 
9113 	nfsd_slot_shrinker = shrinker_alloc(0, "nfsd-DRC-slot");
9114 	if (!nfsd_slot_shrinker) {
9115 		rhltable_destroy(&nfs4_file_rhltable);
9116 		return -ENOMEM;
9117 	}
9118 	nfsd_slot_shrinker->count_objects = nfsd_slot_count;
9119 	nfsd_slot_shrinker->scan_objects = nfsd_slot_scan;
9120 	shrinker_register(nfsd_slot_shrinker);
9121 
9122 	set_max_delegations();
9123 	return 0;
9124 }
9125 
9126 void
nfs4_state_shutdown_net(struct net * net)9127 nfs4_state_shutdown_net(struct net *net)
9128 {
9129 	struct nfs4_delegation *dp = NULL;
9130 	struct list_head *pos, *next, reaplist;
9131 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9132 
9133 	shrinker_free(nn->nfsd_client_shrinker);
9134 	cancel_work_sync(&nn->nfsd_shrinker_work);
9135 	disable_delayed_work_sync(&nn->laundromat_work);
9136 	locks_end_grace(&nn->nfsd4_manager);
9137 
9138 	INIT_LIST_HEAD(&reaplist);
9139 	spin_lock(&nn->deleg_lock);
9140 	list_for_each_safe(pos, next, &nn->del_recall_lru) {
9141 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
9142 		unhash_delegation_locked(dp, SC_STATUS_CLOSED);
9143 		list_add(&dp->dl_recall_lru, &reaplist);
9144 	}
9145 	spin_unlock(&nn->deleg_lock);
9146 	list_for_each_safe(pos, next, &reaplist) {
9147 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
9148 		list_del_init(&dp->dl_recall_lru);
9149 		destroy_unhashed_deleg(dp);
9150 	}
9151 
9152 	nfsd4_client_tracking_exit(net);
9153 	nfs4_state_destroy_net(net);
9154 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
9155 	nfsd4_ssc_shutdown_umount(nn);
9156 #endif
9157 }
9158 
9159 void
nfs4_state_shutdown(void)9160 nfs4_state_shutdown(void)
9161 {
9162 	rhltable_destroy(&nfs4_file_rhltable);
9163 	shrinker_free(nfsd_slot_shrinker);
9164 }
9165 
9166 static void
get_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)9167 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
9168 {
9169 	if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
9170 	    CURRENT_STATEID(stateid))
9171 		memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
9172 }
9173 
9174 static void
put_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)9175 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
9176 {
9177 	if (cstate->minorversion) {
9178 		memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
9179 		SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
9180 	}
9181 }
9182 
9183 void
clear_current_stateid(struct nfsd4_compound_state * cstate)9184 clear_current_stateid(struct nfsd4_compound_state *cstate)
9185 {
9186 	CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
9187 }
9188 
9189 /*
9190  * functions to set current state id
9191  */
9192 void
nfsd4_set_opendowngradestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9193 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
9194 		union nfsd4_op_u *u)
9195 {
9196 	put_stateid(cstate, &u->open_downgrade.od_stateid);
9197 }
9198 
9199 void
nfsd4_set_openstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9200 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
9201 		union nfsd4_op_u *u)
9202 {
9203 	put_stateid(cstate, &u->open.op_stateid);
9204 }
9205 
9206 void
nfsd4_set_closestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9207 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
9208 		union nfsd4_op_u *u)
9209 {
9210 	put_stateid(cstate, &u->close.cl_stateid);
9211 }
9212 
9213 void
nfsd4_set_lockstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9214 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
9215 		union nfsd4_op_u *u)
9216 {
9217 	put_stateid(cstate, &u->lock.lk_resp_stateid);
9218 }
9219 
9220 /*
9221  * functions to consume current state id
9222  */
9223 
9224 void
nfsd4_get_opendowngradestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9225 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
9226 		union nfsd4_op_u *u)
9227 {
9228 	get_stateid(cstate, &u->open_downgrade.od_stateid);
9229 }
9230 
9231 void
nfsd4_get_delegreturnstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9232 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
9233 		union nfsd4_op_u *u)
9234 {
9235 	get_stateid(cstate, &u->delegreturn.dr_stateid);
9236 }
9237 
9238 void
nfsd4_get_freestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9239 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
9240 		union nfsd4_op_u *u)
9241 {
9242 	get_stateid(cstate, &u->free_stateid.fr_stateid);
9243 }
9244 
9245 void
nfsd4_get_setattrstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9246 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
9247 		union nfsd4_op_u *u)
9248 {
9249 	get_stateid(cstate, &u->setattr.sa_stateid);
9250 }
9251 
9252 void
nfsd4_get_closestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9253 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
9254 		union nfsd4_op_u *u)
9255 {
9256 	get_stateid(cstate, &u->close.cl_stateid);
9257 }
9258 
9259 void
nfsd4_get_lockustateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9260 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
9261 		union nfsd4_op_u *u)
9262 {
9263 	get_stateid(cstate, &u->locku.lu_stateid);
9264 }
9265 
9266 void
nfsd4_get_readstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9267 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
9268 		union nfsd4_op_u *u)
9269 {
9270 	get_stateid(cstate, &u->read.rd_stateid);
9271 }
9272 
9273 void
nfsd4_get_writestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9274 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
9275 		union nfsd4_op_u *u)
9276 {
9277 	get_stateid(cstate, &u->write.wr_stateid);
9278 }
9279 
9280 /**
9281  * nfsd4_vet_deleg_time - vet and set the timespec for a delegated timestamp update
9282  * @req: timestamp from the client
9283  * @orig: original timestamp in the inode
9284  * @now: current time
9285  *
9286  * Given a timestamp from the client response, check it against the
9287  * current timestamp in the inode and the current time. Returns true
9288  * if the inode's timestamp needs to be updated, and false otherwise.
9289  * @req may also be changed if the timestamp needs to be clamped.
9290  */
nfsd4_vet_deleg_time(struct timespec64 * req,const struct timespec64 * orig,const struct timespec64 * now)9291 bool nfsd4_vet_deleg_time(struct timespec64 *req, const struct timespec64 *orig,
9292 			  const struct timespec64 *now)
9293 {
9294 
9295 	/*
9296 	 * "When the time presented is before the original time, then the
9297 	 *  update is ignored." Also no need to update if there is no change.
9298 	 */
9299 	if (timespec64_compare(req, orig) <= 0)
9300 		return false;
9301 
9302 	/*
9303 	 * "When the time presented is in the future, the server can either
9304 	 *  clamp the new time to the current time, or it may
9305 	 *  return NFS4ERR_DELAY to the client, allowing it to retry."
9306 	 */
9307 	if (timespec64_compare(req, now) > 0)
9308 		*req = *now;
9309 
9310 	return true;
9311 }
9312 
cb_getattr_update_times(struct dentry * dentry,struct nfs4_delegation * dp)9313 static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation *dp)
9314 {
9315 	struct inode *inode = d_inode(dentry);
9316 	struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr;
9317 	struct iattr attrs = { };
9318 	int ret;
9319 
9320 	if (deleg_attrs_deleg(dp->dl_type)) {
9321 		struct timespec64 now = current_time(inode);
9322 
9323 		attrs.ia_atime = ncf->ncf_cb_atime;
9324 		attrs.ia_mtime = ncf->ncf_cb_mtime;
9325 
9326 		if (nfsd4_vet_deleg_time(&attrs.ia_atime, &dp->dl_atime, &now))
9327 			attrs.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
9328 
9329 		if (nfsd4_vet_deleg_time(&attrs.ia_mtime, &dp->dl_mtime, &now)) {
9330 			attrs.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
9331 			attrs.ia_ctime = attrs.ia_mtime;
9332 			if (nfsd4_vet_deleg_time(&attrs.ia_ctime, &dp->dl_ctime, &now))
9333 				attrs.ia_valid |= ATTR_CTIME | ATTR_CTIME_SET;
9334 		}
9335 	} else {
9336 		attrs.ia_valid |= ATTR_MTIME | ATTR_CTIME;
9337 	}
9338 
9339 	if (!attrs.ia_valid)
9340 		return 0;
9341 
9342 	attrs.ia_valid |= ATTR_DELEG;
9343 	inode_lock(inode);
9344 	ret = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL);
9345 	inode_unlock(inode);
9346 	return ret;
9347 }
9348 
9349 /**
9350  * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict
9351  * @rqstp: RPC transaction context
9352  * @dentry: dentry of inode to be checked for a conflict
9353  * @pdp: returned WRITE delegation, if one was found
9354  *
9355  * This function is called when there is a conflict between a write
9356  * delegation and a change/size GETATTR from another client. The server
9357  * must either use the CB_GETATTR to get the current values of the
9358  * attributes from the client that holds the delegation or recall the
9359  * delegation before replying to the GETATTR. See RFC 8881 section
9360  * 18.7.4.
9361  *
9362  * Returns 0 if there is no conflict; otherwise an nfs_stat
9363  * code is returned. If @pdp is set to a non-NULL value, then the
9364  * caller must put the reference.
9365  */
9366 __be32
nfsd4_deleg_getattr_conflict(struct svc_rqst * rqstp,struct dentry * dentry,struct nfs4_delegation ** pdp)9367 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
9368 			     struct nfs4_delegation **pdp)
9369 {
9370 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
9371 	struct nfsd_thread_local_info *ntli = rqstp->rq_private;
9372 	struct file_lock_context *ctx;
9373 	struct nfs4_delegation *dp = NULL;
9374 	struct file_lease *fl;
9375 	struct nfs4_cb_fattr *ncf;
9376 	struct inode *inode = d_inode(dentry);
9377 	__be32 status;
9378 
9379 	ctx = locks_inode_context(inode);
9380 	if (!ctx)
9381 		return nfs_ok;
9382 
9383 #define NON_NFSD_LEASE ((void *)1)
9384 
9385 	spin_lock(&ctx->flc_lock);
9386 	for_each_file_lock(fl, &ctx->flc_lease) {
9387 		if (fl->c.flc_flags == FL_LAYOUT)
9388 			continue;
9389 		if (fl->c.flc_type == F_WRLCK) {
9390 			if (fl->fl_lmops == &nfsd_lease_mng_ops)
9391 				dp = fl->c.flc_owner;
9392 			else
9393 				dp = NON_NFSD_LEASE;
9394 		}
9395 		break;
9396 	}
9397 	if (dp == NULL || dp == NON_NFSD_LEASE ||
9398 	    dp->dl_recall.cb_clp == *(ntli->ntli_lease_breaker)) {
9399 		spin_unlock(&ctx->flc_lock);
9400 		if (dp == NON_NFSD_LEASE) {
9401 			status = nfserrno(nfsd_open_break_lease(inode,
9402 								NFSD_MAY_READ));
9403 			if (status != nfserr_jukebox ||
9404 			    !nfsd_wait_for_delegreturn(rqstp, inode))
9405 				return status;
9406 		}
9407 		return 0;
9408 	}
9409 
9410 	nfsd_stats_wdeleg_getattr_inc(nn);
9411 	refcount_inc(&dp->dl_stid.sc_count);
9412 	ncf = &dp->dl_cb_fattr;
9413 	nfs4_cb_getattr(&dp->dl_cb_fattr);
9414 	spin_unlock(&ctx->flc_lock);
9415 
9416 	wait_on_bit_timeout(&ncf->ncf_getattr.cb_flags, NFSD4_CALLBACK_RUNNING,
9417 			    TASK_UNINTERRUPTIBLE, NFSD_CB_GETATTR_TIMEOUT);
9418 	if (ncf->ncf_cb_status) {
9419 		/* Recall delegation only if client didn't respond */
9420 		status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
9421 		if (status != nfserr_jukebox ||
9422 		    !nfsd_wait_for_delegreturn(rqstp, inode))
9423 			goto out_status;
9424 		status = nfs_ok;
9425 		goto out_status;
9426 	}
9427 	if (!ncf->ncf_file_modified) {
9428 		if (ncf->ncf_initial_cinfo != ncf->ncf_cb_change)
9429 			ncf->ncf_file_modified = true;
9430 		else if (i_size_read(inode) != ncf->ncf_cb_fsize)
9431 			ncf->ncf_file_modified = true;
9432 	}
9433 	if (ncf->ncf_file_modified) {
9434 		int err;
9435 
9436 		/*
9437 		 * Per section 10.4.3 of RFC 8881, the server would
9438 		 * not update the file's metadata with the client's
9439 		 * modified size
9440 		 */
9441 		err = cb_getattr_update_times(dentry, dp);
9442 		if (err) {
9443 			status = nfserrno(err);
9444 			goto out_status;
9445 		}
9446 		ncf->ncf_cur_fsize = ncf->ncf_cb_fsize;
9447 		*pdp = dp;
9448 		return nfs_ok;
9449 	}
9450 	status = nfs_ok;
9451 out_status:
9452 	nfs4_put_stid(&dp->dl_stid);
9453 	return status;
9454 }
9455 
9456 /**
9457  * nfsd_get_dir_deleg - attempt to get a directory delegation
9458  * @cstate: compound state
9459  * @gdd: GET_DIR_DELEGATION arg/resp structure
9460  * @nf: nfsd_file opened on the directory
9461  *
9462  * Given a GET_DIR_DELEGATION request @gdd, attempt to acquire a delegation
9463  * on the directory to which @nf refers. Note that this does not set up any
9464  * sort of async notifications for the delegation.
9465  */
9466 struct nfs4_delegation *
nfsd_get_dir_deleg(struct nfsd4_compound_state * cstate,struct nfsd4_get_dir_delegation * gdd,struct nfsd_file * nf)9467 nfsd_get_dir_deleg(struct nfsd4_compound_state *cstate,
9468 		   struct nfsd4_get_dir_delegation *gdd,
9469 		   struct nfsd_file *nf)
9470 {
9471 	struct nfs4_client *clp = cstate->clp;
9472 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
9473 	struct nfs4_delegation *dp;
9474 	struct file_lease *fl;
9475 	struct nfs4_file *fp, *rfp;
9476 	int status = 0;
9477 
9478 	fp = nfsd4_alloc_file();
9479 	if (!fp)
9480 		return ERR_PTR(-ENOMEM);
9481 
9482 	nfsd4_file_init(&cstate->current_fh, fp);
9483 
9484 	rfp = nfsd4_file_hash_insert(fp, &cstate->current_fh);
9485 	if (unlikely(!rfp)) {
9486 		put_nfs4_file(fp);
9487 		return ERR_PTR(-ENOMEM);
9488 	}
9489 
9490 	if (rfp != fp) {
9491 		put_nfs4_file(fp);
9492 		fp = rfp;
9493 	}
9494 
9495 	/* if this client already has one, return that it's unavailable */
9496 	spin_lock(&nn->deleg_lock);
9497 	spin_lock(&fp->fi_lock);
9498 	/* existing delegation? */
9499 	if (nfs4_delegation_exists(clp, fp)) {
9500 		status = -EAGAIN;
9501 	} else if (!fp->fi_deleg_file) {
9502 		fp->fi_deleg_file = nfsd_file_get(nf);
9503 		fp->fi_delegees = 1;
9504 	} else {
9505 		++fp->fi_delegees;
9506 	}
9507 	spin_unlock(&fp->fi_lock);
9508 	spin_unlock(&nn->deleg_lock);
9509 
9510 	if (status) {
9511 		put_nfs4_file(fp);
9512 		return ERR_PTR(status);
9513 	}
9514 
9515 	/* Try to set up the lease */
9516 	status = -ENOMEM;
9517 	dp = alloc_init_deleg(clp, fp, NULL, NFS4_OPEN_DELEGATE_READ);
9518 	if (!dp)
9519 		goto out_delegees;
9520 
9521 	fl = nfs4_alloc_init_lease(dp);
9522 	if (!fl)
9523 		goto out_put_stid;
9524 
9525 	status = kernel_setlease(nf->nf_file,
9526 				 fl->c.flc_type, &fl, NULL);
9527 	if (fl)
9528 		locks_free_lease(fl);
9529 	if (status)
9530 		goto out_put_stid;
9531 
9532 	/*
9533 	 * Now, try to hash it. This can fail if we race another nfsd task
9534 	 * trying to set a delegation on the same file. If that happens,
9535 	 * then just say UNAVAIL.
9536 	 */
9537 	spin_lock(&nn->deleg_lock);
9538 	spin_lock(&clp->cl_lock);
9539 	spin_lock(&fp->fi_lock);
9540 	status = hash_delegation_locked(dp, fp);
9541 	spin_unlock(&fp->fi_lock);
9542 	spin_unlock(&clp->cl_lock);
9543 	spin_unlock(&nn->deleg_lock);
9544 
9545 	if (!status) {
9546 		put_nfs4_file(fp);
9547 		return dp;
9548 	}
9549 
9550 	/* Something failed. Drop the lease and clean up the stid */
9551 	kernel_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
9552 out_put_stid:
9553 	nfs4_put_stid(&dp->dl_stid);
9554 out_delegees:
9555 	put_deleg_file(fp);
9556 	put_nfs4_file(fp);
9557 	return ERR_PTR(status);
9558 }
9559 
9560 /**
9561  * nfsd_update_cmtime_attr - update file's delegated ctime/mtime,
9562  *                           and optionally other attributes (ie ATTR_ATIME).
9563  * @f: pointer to an opened file
9564  * @flags: any additional flags that should be updated
9565  *
9566  * Given upon opening a file delegated attributes were issues, update
9567  * @f attributes to current times.
9568  */
nfsd_update_cmtime_attr(struct file * f,unsigned int flags)9569 void nfsd_update_cmtime_attr(struct file *f, unsigned int flags)
9570 {
9571 	int ret;
9572 	struct inode *inode = file_inode(f);
9573 	struct iattr attr = {
9574 		.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_DELEG | flags,
9575 	};
9576 
9577 	inode_lock(inode);
9578 	ret = notify_change(&nop_mnt_idmap, f->f_path.dentry, &attr, NULL);
9579 	inode_unlock(inode);
9580 	if (ret)
9581 		pr_notice_ratelimited("nfsd: Unable to update timestamps on "
9582 				      "inode %02x:%02x:%llu: %d\n",
9583 				      MAJOR(inode->i_sb->s_dev),
9584 				      MINOR(inode->i_sb->s_dev),
9585 				      inode->i_ino, ret);
9586 }
9587