1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4  * Copyright (C) 2019 Samsung Electronics Co., Ltd.
5  */
6 
7 #include <linux/fs.h>
8 #include <linux/filelock.h>
9 #include <linux/slab.h>
10 #include <linux/vmalloc.h>
11 
12 #include "glob.h"
13 #include "vfs_cache.h"
14 #include "oplock.h"
15 #include "vfs.h"
16 #include "connection.h"
17 #include "mgmt/tree_connect.h"
18 #include "mgmt/user_session.h"
19 #include "smb_common.h"
20 
21 #define S_DEL_PENDING			1
22 #define S_DEL_ON_CLS			2
23 #define S_DEL_ON_CLS_STREAM		8
24 
25 static unsigned int inode_hash_mask __read_mostly;
26 static unsigned int inode_hash_shift __read_mostly;
27 static struct hlist_head *inode_hashtable __read_mostly;
28 static DEFINE_RWLOCK(inode_hash_lock);
29 
30 static struct ksmbd_file_table global_ft;
31 static atomic_long_t fd_limit;
32 static struct kmem_cache *filp_cache;
33 
ksmbd_set_fd_limit(unsigned long limit)34 void ksmbd_set_fd_limit(unsigned long limit)
35 {
36 	limit = min(limit, get_max_files());
37 	atomic_long_set(&fd_limit, limit);
38 }
39 
fd_limit_depleted(void)40 static bool fd_limit_depleted(void)
41 {
42 	long v = atomic_long_dec_return(&fd_limit);
43 
44 	if (v >= 0)
45 		return false;
46 	atomic_long_inc(&fd_limit);
47 	return true;
48 }
49 
fd_limit_close(void)50 static void fd_limit_close(void)
51 {
52 	atomic_long_inc(&fd_limit);
53 }
54 
55 /*
56  * INODE hash
57  */
58 
inode_hash(struct super_block * sb,unsigned long hashval)59 static unsigned long inode_hash(struct super_block *sb, unsigned long hashval)
60 {
61 	unsigned long tmp;
62 
63 	tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
64 		L1_CACHE_BYTES;
65 	tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> inode_hash_shift);
66 	return tmp & inode_hash_mask;
67 }
68 
__ksmbd_inode_lookup(struct inode * inode)69 static struct ksmbd_inode *__ksmbd_inode_lookup(struct inode *inode)
70 {
71 	struct hlist_head *head = inode_hashtable +
72 		inode_hash(inode->i_sb, inode->i_ino);
73 	struct ksmbd_inode *ci = NULL, *ret_ci = NULL;
74 
75 	hlist_for_each_entry(ci, head, m_hash) {
76 		if (ci->m_inode == inode) {
77 			if (atomic_inc_not_zero(&ci->m_count))
78 				ret_ci = ci;
79 			break;
80 		}
81 	}
82 	return ret_ci;
83 }
84 
ksmbd_inode_lookup(struct ksmbd_file * fp)85 static struct ksmbd_inode *ksmbd_inode_lookup(struct ksmbd_file *fp)
86 {
87 	return __ksmbd_inode_lookup(file_inode(fp->filp));
88 }
89 
ksmbd_inode_lookup_by_vfsinode(struct inode * inode)90 static struct ksmbd_inode *ksmbd_inode_lookup_by_vfsinode(struct inode *inode)
91 {
92 	struct ksmbd_inode *ci;
93 
94 	read_lock(&inode_hash_lock);
95 	ci = __ksmbd_inode_lookup(inode);
96 	read_unlock(&inode_hash_lock);
97 	return ci;
98 }
99 
ksmbd_query_inode_status(struct inode * inode)100 int ksmbd_query_inode_status(struct inode *inode)
101 {
102 	struct ksmbd_inode *ci;
103 	int ret = KSMBD_INODE_STATUS_UNKNOWN;
104 
105 	read_lock(&inode_hash_lock);
106 	ci = __ksmbd_inode_lookup(inode);
107 	if (ci) {
108 		ret = KSMBD_INODE_STATUS_OK;
109 		if (ci->m_flags & S_DEL_PENDING)
110 			ret = KSMBD_INODE_STATUS_PENDING_DELETE;
111 		atomic_dec(&ci->m_count);
112 	}
113 	read_unlock(&inode_hash_lock);
114 	return ret;
115 }
116 
ksmbd_inode_pending_delete(struct ksmbd_file * fp)117 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp)
118 {
119 	return (fp->f_ci->m_flags & S_DEL_PENDING);
120 }
121 
ksmbd_set_inode_pending_delete(struct ksmbd_file * fp)122 void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp)
123 {
124 	fp->f_ci->m_flags |= S_DEL_PENDING;
125 }
126 
ksmbd_clear_inode_pending_delete(struct ksmbd_file * fp)127 void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp)
128 {
129 	fp->f_ci->m_flags &= ~S_DEL_PENDING;
130 }
131 
ksmbd_fd_set_delete_on_close(struct ksmbd_file * fp,int file_info)132 void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp,
133 				  int file_info)
134 {
135 	if (ksmbd_stream_fd(fp)) {
136 		fp->f_ci->m_flags |= S_DEL_ON_CLS_STREAM;
137 		return;
138 	}
139 
140 	fp->f_ci->m_flags |= S_DEL_ON_CLS;
141 }
142 
ksmbd_inode_hash(struct ksmbd_inode * ci)143 static void ksmbd_inode_hash(struct ksmbd_inode *ci)
144 {
145 	struct hlist_head *b = inode_hashtable +
146 		inode_hash(ci->m_inode->i_sb, ci->m_inode->i_ino);
147 
148 	hlist_add_head(&ci->m_hash, b);
149 }
150 
ksmbd_inode_unhash(struct ksmbd_inode * ci)151 static void ksmbd_inode_unhash(struct ksmbd_inode *ci)
152 {
153 	write_lock(&inode_hash_lock);
154 	hlist_del_init(&ci->m_hash);
155 	write_unlock(&inode_hash_lock);
156 }
157 
ksmbd_inode_init(struct ksmbd_inode * ci,struct ksmbd_file * fp)158 static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp)
159 {
160 	ci->m_inode = file_inode(fp->filp);
161 	atomic_set(&ci->m_count, 1);
162 	atomic_set(&ci->op_count, 0);
163 	atomic_set(&ci->sop_count, 0);
164 	ci->m_flags = 0;
165 	ci->m_fattr = 0;
166 	INIT_LIST_HEAD(&ci->m_fp_list);
167 	INIT_LIST_HEAD(&ci->m_op_list);
168 	rwlock_init(&ci->m_lock);
169 	return 0;
170 }
171 
ksmbd_inode_get(struct ksmbd_file * fp)172 static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp)
173 {
174 	struct ksmbd_inode *ci, *tmpci;
175 	int rc;
176 
177 	read_lock(&inode_hash_lock);
178 	ci = ksmbd_inode_lookup(fp);
179 	read_unlock(&inode_hash_lock);
180 	if (ci)
181 		return ci;
182 
183 	ci = kmalloc(sizeof(struct ksmbd_inode), GFP_KERNEL);
184 	if (!ci)
185 		return NULL;
186 
187 	rc = ksmbd_inode_init(ci, fp);
188 	if (rc) {
189 		pr_err("inode initialized failed\n");
190 		kfree(ci);
191 		return NULL;
192 	}
193 
194 	write_lock(&inode_hash_lock);
195 	tmpci = ksmbd_inode_lookup(fp);
196 	if (!tmpci) {
197 		ksmbd_inode_hash(ci);
198 	} else {
199 		kfree(ci);
200 		ci = tmpci;
201 	}
202 	write_unlock(&inode_hash_lock);
203 	return ci;
204 }
205 
ksmbd_inode_free(struct ksmbd_inode * ci)206 static void ksmbd_inode_free(struct ksmbd_inode *ci)
207 {
208 	ksmbd_inode_unhash(ci);
209 	kfree(ci);
210 }
211 
ksmbd_inode_put(struct ksmbd_inode * ci)212 static void ksmbd_inode_put(struct ksmbd_inode *ci)
213 {
214 	if (atomic_dec_and_test(&ci->m_count))
215 		ksmbd_inode_free(ci);
216 }
217 
ksmbd_inode_hash_init(void)218 int __init ksmbd_inode_hash_init(void)
219 {
220 	unsigned int loop;
221 	unsigned long numentries = 16384;
222 	unsigned long bucketsize = sizeof(struct hlist_head);
223 	unsigned long size;
224 
225 	inode_hash_shift = ilog2(numentries);
226 	inode_hash_mask = (1 << inode_hash_shift) - 1;
227 
228 	size = bucketsize << inode_hash_shift;
229 
230 	/* init master fp hash table */
231 	inode_hashtable = vmalloc(size);
232 	if (!inode_hashtable)
233 		return -ENOMEM;
234 
235 	for (loop = 0; loop < (1U << inode_hash_shift); loop++)
236 		INIT_HLIST_HEAD(&inode_hashtable[loop]);
237 	return 0;
238 }
239 
ksmbd_release_inode_hash(void)240 void ksmbd_release_inode_hash(void)
241 {
242 	vfree(inode_hashtable);
243 }
244 
__ksmbd_inode_close(struct ksmbd_file * fp)245 static void __ksmbd_inode_close(struct ksmbd_file *fp)
246 {
247 	struct dentry *dir, *dentry;
248 	struct ksmbd_inode *ci = fp->f_ci;
249 	int err;
250 	struct file *filp;
251 
252 	filp = fp->filp;
253 	if (ksmbd_stream_fd(fp) && (ci->m_flags & S_DEL_ON_CLS_STREAM)) {
254 		ci->m_flags &= ~S_DEL_ON_CLS_STREAM;
255 		err = ksmbd_vfs_remove_xattr(file_mnt_idmap(filp),
256 					     filp->f_path.dentry,
257 					     fp->stream.name);
258 		if (err)
259 			pr_err("remove xattr failed : %s\n",
260 			       fp->stream.name);
261 	}
262 
263 	if (atomic_dec_and_test(&ci->m_count)) {
264 		write_lock(&ci->m_lock);
265 		if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_PENDING)) {
266 			dentry = filp->f_path.dentry;
267 			dir = dentry->d_parent;
268 			ci->m_flags &= ~(S_DEL_ON_CLS | S_DEL_PENDING);
269 			write_unlock(&ci->m_lock);
270 			ksmbd_vfs_unlink(file_mnt_idmap(filp), dir, dentry);
271 			write_lock(&ci->m_lock);
272 		}
273 		write_unlock(&ci->m_lock);
274 
275 		ksmbd_inode_free(ci);
276 	}
277 }
278 
__ksmbd_remove_durable_fd(struct ksmbd_file * fp)279 static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp)
280 {
281 	if (!has_file_id(fp->persistent_id))
282 		return;
283 
284 	write_lock(&global_ft.lock);
285 	idr_remove(global_ft.idr, fp->persistent_id);
286 	write_unlock(&global_ft.lock);
287 }
288 
__ksmbd_remove_fd(struct ksmbd_file_table * ft,struct ksmbd_file * fp)289 static void __ksmbd_remove_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
290 {
291 	if (!has_file_id(fp->volatile_id))
292 		return;
293 
294 	write_lock(&fp->f_ci->m_lock);
295 	list_del_init(&fp->node);
296 	write_unlock(&fp->f_ci->m_lock);
297 
298 	write_lock(&ft->lock);
299 	idr_remove(ft->idr, fp->volatile_id);
300 	write_unlock(&ft->lock);
301 }
302 
__ksmbd_close_fd(struct ksmbd_file_table * ft,struct ksmbd_file * fp)303 static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
304 {
305 	struct file *filp;
306 	struct ksmbd_lock *smb_lock, *tmp_lock;
307 
308 	fd_limit_close();
309 	__ksmbd_remove_durable_fd(fp);
310 	__ksmbd_remove_fd(ft, fp);
311 
312 	close_id_del_oplock(fp);
313 	filp = fp->filp;
314 
315 	__ksmbd_inode_close(fp);
316 	if (!IS_ERR_OR_NULL(filp))
317 		fput(filp);
318 
319 	/* because the reference count of fp is 0, it is guaranteed that
320 	 * there are not accesses to fp->lock_list.
321 	 */
322 	list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
323 		spin_lock(&fp->conn->llist_lock);
324 		list_del(&smb_lock->clist);
325 		spin_unlock(&fp->conn->llist_lock);
326 
327 		list_del(&smb_lock->flist);
328 		locks_free_lock(smb_lock->fl);
329 		kfree(smb_lock);
330 	}
331 
332 	if (ksmbd_stream_fd(fp))
333 		kfree(fp->stream.name);
334 	kmem_cache_free(filp_cache, fp);
335 }
336 
ksmbd_fp_get(struct ksmbd_file * fp)337 static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp)
338 {
339 	if (!atomic_inc_not_zero(&fp->refcount))
340 		return NULL;
341 	return fp;
342 }
343 
__ksmbd_lookup_fd(struct ksmbd_file_table * ft,u64 id)344 static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft,
345 					    u64 id)
346 {
347 	struct ksmbd_file *fp;
348 
349 	if (!has_file_id(id))
350 		return NULL;
351 
352 	read_lock(&ft->lock);
353 	fp = idr_find(ft->idr, id);
354 	if (fp)
355 		fp = ksmbd_fp_get(fp);
356 	read_unlock(&ft->lock);
357 	return fp;
358 }
359 
__put_fd_final(struct ksmbd_work * work,struct ksmbd_file * fp)360 static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp)
361 {
362 	__ksmbd_close_fd(&work->sess->file_table, fp);
363 	atomic_dec(&work->conn->stats.open_files_count);
364 }
365 
set_close_state_blocked_works(struct ksmbd_file * fp)366 static void set_close_state_blocked_works(struct ksmbd_file *fp)
367 {
368 	struct ksmbd_work *cancel_work;
369 
370 	spin_lock(&fp->f_lock);
371 	list_for_each_entry(cancel_work, &fp->blocked_works,
372 				 fp_entry) {
373 		cancel_work->state = KSMBD_WORK_CLOSED;
374 		cancel_work->cancel_fn(cancel_work->cancel_argv);
375 	}
376 	spin_unlock(&fp->f_lock);
377 }
378 
ksmbd_close_fd(struct ksmbd_work * work,u64 id)379 int ksmbd_close_fd(struct ksmbd_work *work, u64 id)
380 {
381 	struct ksmbd_file	*fp;
382 	struct ksmbd_file_table	*ft;
383 
384 	if (!has_file_id(id))
385 		return 0;
386 
387 	ft = &work->sess->file_table;
388 	read_lock(&ft->lock);
389 	fp = idr_find(ft->idr, id);
390 	if (fp) {
391 		set_close_state_blocked_works(fp);
392 
393 		if (!atomic_dec_and_test(&fp->refcount))
394 			fp = NULL;
395 	}
396 	read_unlock(&ft->lock);
397 
398 	if (!fp)
399 		return -EINVAL;
400 
401 	__put_fd_final(work, fp);
402 	return 0;
403 }
404 
ksmbd_fd_put(struct ksmbd_work * work,struct ksmbd_file * fp)405 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp)
406 {
407 	if (!fp)
408 		return;
409 
410 	if (!atomic_dec_and_test(&fp->refcount))
411 		return;
412 	__put_fd_final(work, fp);
413 }
414 
__sanity_check(struct ksmbd_tree_connect * tcon,struct ksmbd_file * fp)415 static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp)
416 {
417 	if (!fp)
418 		return false;
419 	if (fp->tcon != tcon)
420 		return false;
421 	return true;
422 }
423 
ksmbd_lookup_foreign_fd(struct ksmbd_work * work,u64 id)424 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id)
425 {
426 	return __ksmbd_lookup_fd(&work->sess->file_table, id);
427 }
428 
ksmbd_lookup_fd_fast(struct ksmbd_work * work,u64 id)429 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id)
430 {
431 	struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
432 
433 	if (__sanity_check(work->tcon, fp))
434 		return fp;
435 
436 	ksmbd_fd_put(work, fp);
437 	return NULL;
438 }
439 
ksmbd_lookup_fd_slow(struct ksmbd_work * work,u64 id,u64 pid)440 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id,
441 					u64 pid)
442 {
443 	struct ksmbd_file *fp;
444 
445 	if (!has_file_id(id)) {
446 		id = work->compound_fid;
447 		pid = work->compound_pfid;
448 	}
449 
450 	fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
451 	if (!__sanity_check(work->tcon, fp)) {
452 		ksmbd_fd_put(work, fp);
453 		return NULL;
454 	}
455 	if (fp->persistent_id != pid) {
456 		ksmbd_fd_put(work, fp);
457 		return NULL;
458 	}
459 	return fp;
460 }
461 
ksmbd_lookup_durable_fd(unsigned long long id)462 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id)
463 {
464 	return __ksmbd_lookup_fd(&global_ft, id);
465 }
466 
ksmbd_lookup_fd_cguid(char * cguid)467 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid)
468 {
469 	struct ksmbd_file	*fp = NULL;
470 	unsigned int		id;
471 
472 	read_lock(&global_ft.lock);
473 	idr_for_each_entry(global_ft.idr, fp, id) {
474 		if (!memcmp(fp->create_guid,
475 			    cguid,
476 			    SMB2_CREATE_GUID_SIZE)) {
477 			fp = ksmbd_fp_get(fp);
478 			break;
479 		}
480 	}
481 	read_unlock(&global_ft.lock);
482 
483 	return fp;
484 }
485 
ksmbd_lookup_fd_inode(struct inode * inode)486 struct ksmbd_file *ksmbd_lookup_fd_inode(struct inode *inode)
487 {
488 	struct ksmbd_file	*lfp;
489 	struct ksmbd_inode	*ci;
490 
491 	ci = ksmbd_inode_lookup_by_vfsinode(inode);
492 	if (!ci)
493 		return NULL;
494 
495 	read_lock(&ci->m_lock);
496 	list_for_each_entry(lfp, &ci->m_fp_list, node) {
497 		if (inode == file_inode(lfp->filp)) {
498 			atomic_dec(&ci->m_count);
499 			lfp = ksmbd_fp_get(lfp);
500 			read_unlock(&ci->m_lock);
501 			return lfp;
502 		}
503 	}
504 	atomic_dec(&ci->m_count);
505 	read_unlock(&ci->m_lock);
506 	return NULL;
507 }
508 
509 #define OPEN_ID_TYPE_VOLATILE_ID	(0)
510 #define OPEN_ID_TYPE_PERSISTENT_ID	(1)
511 
__open_id_set(struct ksmbd_file * fp,u64 id,int type)512 static void __open_id_set(struct ksmbd_file *fp, u64 id, int type)
513 {
514 	if (type == OPEN_ID_TYPE_VOLATILE_ID)
515 		fp->volatile_id = id;
516 	if (type == OPEN_ID_TYPE_PERSISTENT_ID)
517 		fp->persistent_id = id;
518 }
519 
__open_id(struct ksmbd_file_table * ft,struct ksmbd_file * fp,int type)520 static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
521 		     int type)
522 {
523 	u64			id = 0;
524 	int			ret;
525 
526 	if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) {
527 		__open_id_set(fp, KSMBD_NO_FID, type);
528 		return -EMFILE;
529 	}
530 
531 	idr_preload(GFP_KERNEL);
532 	write_lock(&ft->lock);
533 	ret = idr_alloc_cyclic(ft->idr, fp, 0, INT_MAX - 1, GFP_NOWAIT);
534 	if (ret >= 0) {
535 		id = ret;
536 		ret = 0;
537 	} else {
538 		id = KSMBD_NO_FID;
539 		fd_limit_close();
540 	}
541 
542 	__open_id_set(fp, id, type);
543 	write_unlock(&ft->lock);
544 	idr_preload_end();
545 	return ret;
546 }
547 
ksmbd_open_durable_fd(struct ksmbd_file * fp)548 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp)
549 {
550 	__open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID);
551 	return fp->persistent_id;
552 }
553 
ksmbd_open_fd(struct ksmbd_work * work,struct file * filp)554 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp)
555 {
556 	struct ksmbd_file *fp;
557 	int ret;
558 
559 	fp = kmem_cache_zalloc(filp_cache, GFP_KERNEL);
560 	if (!fp) {
561 		pr_err("Failed to allocate memory\n");
562 		return ERR_PTR(-ENOMEM);
563 	}
564 
565 	INIT_LIST_HEAD(&fp->blocked_works);
566 	INIT_LIST_HEAD(&fp->node);
567 	INIT_LIST_HEAD(&fp->lock_list);
568 	spin_lock_init(&fp->f_lock);
569 	atomic_set(&fp->refcount, 1);
570 
571 	fp->filp		= filp;
572 	fp->conn		= work->conn;
573 	fp->tcon		= work->tcon;
574 	fp->volatile_id		= KSMBD_NO_FID;
575 	fp->persistent_id	= KSMBD_NO_FID;
576 	fp->f_ci		= ksmbd_inode_get(fp);
577 
578 	if (!fp->f_ci) {
579 		ret = -ENOMEM;
580 		goto err_out;
581 	}
582 
583 	ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
584 	if (ret) {
585 		ksmbd_inode_put(fp->f_ci);
586 		goto err_out;
587 	}
588 
589 	atomic_inc(&work->conn->stats.open_files_count);
590 	return fp;
591 
592 err_out:
593 	kmem_cache_free(filp_cache, fp);
594 	return ERR_PTR(ret);
595 }
596 
597 static int
__close_file_table_ids(struct ksmbd_file_table * ft,struct ksmbd_tree_connect * tcon,bool (* skip)(struct ksmbd_tree_connect * tcon,struct ksmbd_file * fp))598 __close_file_table_ids(struct ksmbd_file_table *ft,
599 		       struct ksmbd_tree_connect *tcon,
600 		       bool (*skip)(struct ksmbd_tree_connect *tcon,
601 				    struct ksmbd_file *fp))
602 {
603 	unsigned int			id;
604 	struct ksmbd_file		*fp;
605 	int				num = 0;
606 
607 	idr_for_each_entry(ft->idr, fp, id) {
608 		if (skip(tcon, fp))
609 			continue;
610 
611 		set_close_state_blocked_works(fp);
612 
613 		if (!atomic_dec_and_test(&fp->refcount))
614 			continue;
615 		__ksmbd_close_fd(ft, fp);
616 		num++;
617 	}
618 	return num;
619 }
620 
tree_conn_fd_check(struct ksmbd_tree_connect * tcon,struct ksmbd_file * fp)621 static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon,
622 			       struct ksmbd_file *fp)
623 {
624 	return fp->tcon != tcon;
625 }
626 
session_fd_check(struct ksmbd_tree_connect * tcon,struct ksmbd_file * fp)627 static bool session_fd_check(struct ksmbd_tree_connect *tcon,
628 			     struct ksmbd_file *fp)
629 {
630 	return false;
631 }
632 
ksmbd_close_tree_conn_fds(struct ksmbd_work * work)633 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work)
634 {
635 	int num = __close_file_table_ids(&work->sess->file_table,
636 					 work->tcon,
637 					 tree_conn_fd_check);
638 
639 	atomic_sub(num, &work->conn->stats.open_files_count);
640 }
641 
ksmbd_close_session_fds(struct ksmbd_work * work)642 void ksmbd_close_session_fds(struct ksmbd_work *work)
643 {
644 	int num = __close_file_table_ids(&work->sess->file_table,
645 					 work->tcon,
646 					 session_fd_check);
647 
648 	atomic_sub(num, &work->conn->stats.open_files_count);
649 }
650 
ksmbd_init_global_file_table(void)651 int ksmbd_init_global_file_table(void)
652 {
653 	return ksmbd_init_file_table(&global_ft);
654 }
655 
ksmbd_free_global_file_table(void)656 void ksmbd_free_global_file_table(void)
657 {
658 	struct ksmbd_file	*fp = NULL;
659 	unsigned int		id;
660 
661 	idr_for_each_entry(global_ft.idr, fp, id) {
662 		__ksmbd_remove_durable_fd(fp);
663 		kmem_cache_free(filp_cache, fp);
664 	}
665 
666 	ksmbd_destroy_file_table(&global_ft);
667 }
668 
ksmbd_init_file_table(struct ksmbd_file_table * ft)669 int ksmbd_init_file_table(struct ksmbd_file_table *ft)
670 {
671 	ft->idr = kzalloc(sizeof(struct idr), GFP_KERNEL);
672 	if (!ft->idr)
673 		return -ENOMEM;
674 
675 	idr_init(ft->idr);
676 	rwlock_init(&ft->lock);
677 	return 0;
678 }
679 
ksmbd_destroy_file_table(struct ksmbd_file_table * ft)680 void ksmbd_destroy_file_table(struct ksmbd_file_table *ft)
681 {
682 	if (!ft->idr)
683 		return;
684 
685 	__close_file_table_ids(ft, NULL, session_fd_check);
686 	idr_destroy(ft->idr);
687 	kfree(ft->idr);
688 	ft->idr = NULL;
689 }
690 
ksmbd_init_file_cache(void)691 int ksmbd_init_file_cache(void)
692 {
693 	filp_cache = kmem_cache_create("ksmbd_file_cache",
694 				       sizeof(struct ksmbd_file), 0,
695 				       SLAB_HWCACHE_ALIGN, NULL);
696 	if (!filp_cache)
697 		goto out;
698 
699 	return 0;
700 
701 out:
702 	pr_err("failed to allocate file cache\n");
703 	return -ENOMEM;
704 }
705 
ksmbd_exit_file_cache(void)706 void ksmbd_exit_file_cache(void)
707 {
708 	kmem_cache_destroy(filp_cache);
709 }
710