xref: /linux/fs/smb/server/vfs_cache.c (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
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 
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 
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 
50 static void fd_limit_close(void)
51 {
52 	atomic_long_inc(&fd_limit);
53 }
54 
55 /*
56  * INODE hash
57  */
58 
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 
69 static struct ksmbd_inode *__ksmbd_inode_lookup(struct dentry *de)
70 {
71 	struct hlist_head *head = inode_hashtable +
72 		inode_hash(d_inode(de)->i_sb, (unsigned long)de);
73 	struct ksmbd_inode *ci = NULL, *ret_ci = NULL;
74 
75 	hlist_for_each_entry(ci, head, m_hash) {
76 		if (ci->m_de == de) {
77 			if (atomic_inc_not_zero(&ci->m_count))
78 				ret_ci = ci;
79 			break;
80 		}
81 	}
82 	return ret_ci;
83 }
84 
85 static struct ksmbd_inode *ksmbd_inode_lookup(struct ksmbd_file *fp)
86 {
87 	return __ksmbd_inode_lookup(fp->filp->f_path.dentry);
88 }
89 
90 struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d)
91 {
92 	struct ksmbd_inode *ci;
93 
94 	read_lock(&inode_hash_lock);
95 	ci = __ksmbd_inode_lookup(d);
96 	read_unlock(&inode_hash_lock);
97 
98 	return ci;
99 }
100 
101 int ksmbd_query_inode_status(struct dentry *dentry)
102 {
103 	struct ksmbd_inode *ci;
104 	int ret = KSMBD_INODE_STATUS_UNKNOWN;
105 
106 	read_lock(&inode_hash_lock);
107 	ci = __ksmbd_inode_lookup(dentry);
108 	if (ci) {
109 		ret = KSMBD_INODE_STATUS_OK;
110 		if (ci->m_flags & (S_DEL_PENDING | S_DEL_ON_CLS))
111 			ret = KSMBD_INODE_STATUS_PENDING_DELETE;
112 		atomic_dec(&ci->m_count);
113 	}
114 	read_unlock(&inode_hash_lock);
115 	return ret;
116 }
117 
118 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp)
119 {
120 	return (fp->f_ci->m_flags & (S_DEL_PENDING | S_DEL_ON_CLS));
121 }
122 
123 void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp)
124 {
125 	fp->f_ci->m_flags |= S_DEL_PENDING;
126 }
127 
128 void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp)
129 {
130 	fp->f_ci->m_flags &= ~S_DEL_PENDING;
131 }
132 
133 void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp,
134 				  int file_info)
135 {
136 	if (ksmbd_stream_fd(fp)) {
137 		fp->f_ci->m_flags |= S_DEL_ON_CLS_STREAM;
138 		return;
139 	}
140 
141 	fp->f_ci->m_flags |= S_DEL_ON_CLS;
142 }
143 
144 static void ksmbd_inode_hash(struct ksmbd_inode *ci)
145 {
146 	struct hlist_head *b = inode_hashtable +
147 		inode_hash(d_inode(ci->m_de)->i_sb, (unsigned long)ci->m_de);
148 
149 	hlist_add_head(&ci->m_hash, b);
150 }
151 
152 static void ksmbd_inode_unhash(struct ksmbd_inode *ci)
153 {
154 	write_lock(&inode_hash_lock);
155 	hlist_del_init(&ci->m_hash);
156 	write_unlock(&inode_hash_lock);
157 }
158 
159 static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp)
160 {
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 	init_rwsem(&ci->m_lock);
169 	ci->m_de = fp->filp->f_path.dentry;
170 	return 0;
171 }
172 
173 static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp)
174 {
175 	struct ksmbd_inode *ci, *tmpci;
176 	int rc;
177 
178 	read_lock(&inode_hash_lock);
179 	ci = ksmbd_inode_lookup(fp);
180 	read_unlock(&inode_hash_lock);
181 	if (ci)
182 		return ci;
183 
184 	ci = kmalloc(sizeof(struct ksmbd_inode), GFP_KERNEL);
185 	if (!ci)
186 		return NULL;
187 
188 	rc = ksmbd_inode_init(ci, fp);
189 	if (rc) {
190 		pr_err("inode initialized failed\n");
191 		kfree(ci);
192 		return NULL;
193 	}
194 
195 	write_lock(&inode_hash_lock);
196 	tmpci = ksmbd_inode_lookup(fp);
197 	if (!tmpci) {
198 		ksmbd_inode_hash(ci);
199 	} else {
200 		kfree(ci);
201 		ci = tmpci;
202 	}
203 	write_unlock(&inode_hash_lock);
204 	return ci;
205 }
206 
207 static void ksmbd_inode_free(struct ksmbd_inode *ci)
208 {
209 	ksmbd_inode_unhash(ci);
210 	kfree(ci);
211 }
212 
213 void ksmbd_inode_put(struct ksmbd_inode *ci)
214 {
215 	if (atomic_dec_and_test(&ci->m_count))
216 		ksmbd_inode_free(ci);
217 }
218 
219 int __init ksmbd_inode_hash_init(void)
220 {
221 	unsigned int loop;
222 	unsigned long numentries = 16384;
223 	unsigned long bucketsize = sizeof(struct hlist_head);
224 	unsigned long size;
225 
226 	inode_hash_shift = ilog2(numentries);
227 	inode_hash_mask = (1 << inode_hash_shift) - 1;
228 
229 	size = bucketsize << inode_hash_shift;
230 
231 	/* init master fp hash table */
232 	inode_hashtable = vmalloc(size);
233 	if (!inode_hashtable)
234 		return -ENOMEM;
235 
236 	for (loop = 0; loop < (1U << inode_hash_shift); loop++)
237 		INIT_HLIST_HEAD(&inode_hashtable[loop]);
238 	return 0;
239 }
240 
241 void ksmbd_release_inode_hash(void)
242 {
243 	vfree(inode_hashtable);
244 }
245 
246 static void __ksmbd_inode_close(struct ksmbd_file *fp)
247 {
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,
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 		down_write(&ci->m_lock);
265 		if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_PENDING)) {
266 			ci->m_flags &= ~(S_DEL_ON_CLS | S_DEL_PENDING);
267 			up_write(&ci->m_lock);
268 			ksmbd_vfs_unlink(filp);
269 			down_write(&ci->m_lock);
270 		}
271 		up_write(&ci->m_lock);
272 
273 		ksmbd_inode_free(ci);
274 	}
275 }
276 
277 static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp)
278 {
279 	if (!has_file_id(fp->persistent_id))
280 		return;
281 
282 	write_lock(&global_ft.lock);
283 	idr_remove(global_ft.idr, fp->persistent_id);
284 	write_unlock(&global_ft.lock);
285 }
286 
287 static void __ksmbd_remove_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
288 {
289 	if (!has_file_id(fp->volatile_id))
290 		return;
291 
292 	down_write(&fp->f_ci->m_lock);
293 	list_del_init(&fp->node);
294 	up_write(&fp->f_ci->m_lock);
295 
296 	write_lock(&ft->lock);
297 	idr_remove(ft->idr, fp->volatile_id);
298 	write_unlock(&ft->lock);
299 }
300 
301 static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
302 {
303 	struct file *filp;
304 	struct ksmbd_lock *smb_lock, *tmp_lock;
305 
306 	fd_limit_close();
307 	__ksmbd_remove_durable_fd(fp);
308 	if (ft)
309 		__ksmbd_remove_fd(ft, fp);
310 
311 	close_id_del_oplock(fp);
312 	filp = fp->filp;
313 
314 	__ksmbd_inode_close(fp);
315 	if (!IS_ERR_OR_NULL(filp))
316 		fput(filp);
317 
318 	/* because the reference count of fp is 0, it is guaranteed that
319 	 * there are not accesses to fp->lock_list.
320 	 */
321 	list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
322 		spin_lock(&fp->conn->llist_lock);
323 		list_del(&smb_lock->clist);
324 		spin_unlock(&fp->conn->llist_lock);
325 
326 		list_del(&smb_lock->flist);
327 		locks_free_lock(smb_lock->fl);
328 		kfree(smb_lock);
329 	}
330 
331 	if (ksmbd_stream_fd(fp))
332 		kfree(fp->stream.name);
333 	kmem_cache_free(filp_cache, fp);
334 }
335 
336 static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp)
337 {
338 	if (fp->f_state != FP_INITED)
339 		return NULL;
340 
341 	if (!atomic_inc_not_zero(&fp->refcount))
342 		return NULL;
343 	return fp;
344 }
345 
346 static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft,
347 					    u64 id)
348 {
349 	struct ksmbd_file *fp;
350 
351 	if (!has_file_id(id))
352 		return NULL;
353 
354 	read_lock(&ft->lock);
355 	fp = idr_find(ft->idr, id);
356 	if (fp)
357 		fp = ksmbd_fp_get(fp);
358 	read_unlock(&ft->lock);
359 	return fp;
360 }
361 
362 static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp)
363 {
364 	__ksmbd_close_fd(&work->sess->file_table, fp);
365 	atomic_dec(&work->conn->stats.open_files_count);
366 }
367 
368 static void set_close_state_blocked_works(struct ksmbd_file *fp)
369 {
370 	struct ksmbd_work *cancel_work;
371 
372 	spin_lock(&fp->f_lock);
373 	list_for_each_entry(cancel_work, &fp->blocked_works,
374 				 fp_entry) {
375 		cancel_work->state = KSMBD_WORK_CLOSED;
376 		cancel_work->cancel_fn(cancel_work->cancel_argv);
377 	}
378 	spin_unlock(&fp->f_lock);
379 }
380 
381 int ksmbd_close_fd(struct ksmbd_work *work, u64 id)
382 {
383 	struct ksmbd_file	*fp;
384 	struct ksmbd_file_table	*ft;
385 
386 	if (!has_file_id(id))
387 		return 0;
388 
389 	ft = &work->sess->file_table;
390 	write_lock(&ft->lock);
391 	fp = idr_find(ft->idr, id);
392 	if (fp) {
393 		set_close_state_blocked_works(fp);
394 
395 		if (fp->f_state != FP_INITED)
396 			fp = NULL;
397 		else {
398 			fp->f_state = FP_CLOSED;
399 			if (!atomic_dec_and_test(&fp->refcount))
400 				fp = NULL;
401 		}
402 	}
403 	write_unlock(&ft->lock);
404 
405 	if (!fp)
406 		return -EINVAL;
407 
408 	__put_fd_final(work, fp);
409 	return 0;
410 }
411 
412 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp)
413 {
414 	if (!fp)
415 		return;
416 
417 	if (!atomic_dec_and_test(&fp->refcount))
418 		return;
419 	__put_fd_final(work, fp);
420 }
421 
422 static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp)
423 {
424 	if (!fp)
425 		return false;
426 	if (fp->tcon != tcon)
427 		return false;
428 	return true;
429 }
430 
431 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id)
432 {
433 	return __ksmbd_lookup_fd(&work->sess->file_table, id);
434 }
435 
436 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id)
437 {
438 	struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
439 
440 	if (__sanity_check(work->tcon, fp))
441 		return fp;
442 
443 	ksmbd_fd_put(work, fp);
444 	return NULL;
445 }
446 
447 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id,
448 					u64 pid)
449 {
450 	struct ksmbd_file *fp;
451 
452 	if (!has_file_id(id)) {
453 		id = work->compound_fid;
454 		pid = work->compound_pfid;
455 	}
456 
457 	fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
458 	if (!__sanity_check(work->tcon, fp)) {
459 		ksmbd_fd_put(work, fp);
460 		return NULL;
461 	}
462 	if (fp->persistent_id != pid) {
463 		ksmbd_fd_put(work, fp);
464 		return NULL;
465 	}
466 	return fp;
467 }
468 
469 struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id)
470 {
471 	return __ksmbd_lookup_fd(&global_ft, id);
472 }
473 
474 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id)
475 {
476 	struct ksmbd_file *fp;
477 
478 	fp = __ksmbd_lookup_fd(&global_ft, id);
479 	if (fp && fp->conn) {
480 		ksmbd_put_durable_fd(fp);
481 		fp = NULL;
482 	}
483 
484 	return fp;
485 }
486 
487 void ksmbd_put_durable_fd(struct ksmbd_file *fp)
488 {
489 	if (!atomic_dec_and_test(&fp->refcount))
490 		return;
491 
492 	__ksmbd_close_fd(NULL, fp);
493 }
494 
495 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid)
496 {
497 	struct ksmbd_file	*fp = NULL;
498 	unsigned int		id;
499 
500 	read_lock(&global_ft.lock);
501 	idr_for_each_entry(global_ft.idr, fp, id) {
502 		if (!memcmp(fp->create_guid,
503 			    cguid,
504 			    SMB2_CREATE_GUID_SIZE)) {
505 			fp = ksmbd_fp_get(fp);
506 			break;
507 		}
508 	}
509 	read_unlock(&global_ft.lock);
510 
511 	return fp;
512 }
513 
514 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry)
515 {
516 	struct ksmbd_file	*lfp;
517 	struct ksmbd_inode	*ci;
518 	struct inode		*inode = d_inode(dentry);
519 
520 	read_lock(&inode_hash_lock);
521 	ci = __ksmbd_inode_lookup(dentry);
522 	read_unlock(&inode_hash_lock);
523 	if (!ci)
524 		return NULL;
525 
526 	down_read(&ci->m_lock);
527 	list_for_each_entry(lfp, &ci->m_fp_list, node) {
528 		if (inode == file_inode(lfp->filp)) {
529 			atomic_dec(&ci->m_count);
530 			lfp = ksmbd_fp_get(lfp);
531 			up_read(&ci->m_lock);
532 			return lfp;
533 		}
534 	}
535 	atomic_dec(&ci->m_count);
536 	up_read(&ci->m_lock);
537 	return NULL;
538 }
539 
540 #define OPEN_ID_TYPE_VOLATILE_ID	(0)
541 #define OPEN_ID_TYPE_PERSISTENT_ID	(1)
542 
543 static void __open_id_set(struct ksmbd_file *fp, u64 id, int type)
544 {
545 	if (type == OPEN_ID_TYPE_VOLATILE_ID)
546 		fp->volatile_id = id;
547 	if (type == OPEN_ID_TYPE_PERSISTENT_ID)
548 		fp->persistent_id = id;
549 }
550 
551 static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
552 		     int type)
553 {
554 	u64			id = 0;
555 	int			ret;
556 
557 	if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) {
558 		__open_id_set(fp, KSMBD_NO_FID, type);
559 		return -EMFILE;
560 	}
561 
562 	idr_preload(GFP_KERNEL);
563 	write_lock(&ft->lock);
564 	ret = idr_alloc_cyclic(ft->idr, fp, 0, INT_MAX - 1, GFP_NOWAIT);
565 	if (ret >= 0) {
566 		id = ret;
567 		ret = 0;
568 	} else {
569 		id = KSMBD_NO_FID;
570 		fd_limit_close();
571 	}
572 
573 	__open_id_set(fp, id, type);
574 	write_unlock(&ft->lock);
575 	idr_preload_end();
576 	return ret;
577 }
578 
579 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp)
580 {
581 	__open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID);
582 	return fp->persistent_id;
583 }
584 
585 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp)
586 {
587 	struct ksmbd_file *fp;
588 	int ret;
589 
590 	fp = kmem_cache_zalloc(filp_cache, GFP_KERNEL);
591 	if (!fp) {
592 		pr_err("Failed to allocate memory\n");
593 		return ERR_PTR(-ENOMEM);
594 	}
595 
596 	INIT_LIST_HEAD(&fp->blocked_works);
597 	INIT_LIST_HEAD(&fp->node);
598 	INIT_LIST_HEAD(&fp->lock_list);
599 	spin_lock_init(&fp->f_lock);
600 	atomic_set(&fp->refcount, 1);
601 
602 	fp->filp		= filp;
603 	fp->conn		= work->conn;
604 	fp->tcon		= work->tcon;
605 	fp->volatile_id		= KSMBD_NO_FID;
606 	fp->persistent_id	= KSMBD_NO_FID;
607 	fp->f_state		= FP_NEW;
608 	fp->f_ci		= ksmbd_inode_get(fp);
609 
610 	if (!fp->f_ci) {
611 		ret = -ENOMEM;
612 		goto err_out;
613 	}
614 
615 	ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
616 	if (ret) {
617 		ksmbd_inode_put(fp->f_ci);
618 		goto err_out;
619 	}
620 
621 	atomic_inc(&work->conn->stats.open_files_count);
622 	return fp;
623 
624 err_out:
625 	kmem_cache_free(filp_cache, fp);
626 	return ERR_PTR(ret);
627 }
628 
629 void ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
630 			 unsigned int state)
631 {
632 	if (!fp)
633 		return;
634 
635 	write_lock(&ft->lock);
636 	fp->f_state = state;
637 	write_unlock(&ft->lock);
638 }
639 
640 static int
641 __close_file_table_ids(struct ksmbd_file_table *ft,
642 		       struct ksmbd_tree_connect *tcon,
643 		       bool (*skip)(struct ksmbd_tree_connect *tcon,
644 				    struct ksmbd_file *fp))
645 {
646 	unsigned int			id;
647 	struct ksmbd_file		*fp;
648 	int				num = 0;
649 
650 	idr_for_each_entry(ft->idr, fp, id) {
651 		if (skip(tcon, fp))
652 			continue;
653 
654 		set_close_state_blocked_works(fp);
655 
656 		if (!atomic_dec_and_test(&fp->refcount))
657 			continue;
658 		__ksmbd_close_fd(ft, fp);
659 		num++;
660 	}
661 	return num;
662 }
663 
664 static inline bool is_reconnectable(struct ksmbd_file *fp)
665 {
666 	struct oplock_info *opinfo = opinfo_get(fp);
667 	bool reconn = false;
668 
669 	if (!opinfo)
670 		return false;
671 
672 	if (opinfo->op_state != OPLOCK_STATE_NONE) {
673 		opinfo_put(opinfo);
674 		return false;
675 	}
676 
677 	if (fp->is_resilient || fp->is_persistent)
678 		reconn = true;
679 	else if (fp->is_durable && opinfo->is_lease &&
680 		 opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
681 		reconn = true;
682 
683 	else if (fp->is_durable && opinfo->level == SMB2_OPLOCK_LEVEL_BATCH)
684 		reconn = true;
685 
686 	opinfo_put(opinfo);
687 	return reconn;
688 }
689 
690 static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon,
691 			       struct ksmbd_file *fp)
692 {
693 	return fp->tcon != tcon;
694 }
695 
696 static bool session_fd_check(struct ksmbd_tree_connect *tcon,
697 			     struct ksmbd_file *fp)
698 {
699 	struct ksmbd_inode *ci;
700 	struct oplock_info *op;
701 	struct ksmbd_conn *conn;
702 
703 	if (!is_reconnectable(fp))
704 		return false;
705 
706 	conn = fp->conn;
707 	ci = fp->f_ci;
708 	down_write(&ci->m_lock);
709 	list_for_each_entry_rcu(op, &ci->m_op_list, op_entry) {
710 		if (op->conn != conn)
711 			continue;
712 		op->conn = NULL;
713 	}
714 	up_write(&ci->m_lock);
715 
716 	fp->conn = NULL;
717 	fp->tcon = NULL;
718 	fp->volatile_id = KSMBD_NO_FID;
719 
720 	return true;
721 }
722 
723 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work)
724 {
725 	int num = __close_file_table_ids(&work->sess->file_table,
726 					 work->tcon,
727 					 tree_conn_fd_check);
728 
729 	atomic_sub(num, &work->conn->stats.open_files_count);
730 }
731 
732 void ksmbd_close_session_fds(struct ksmbd_work *work)
733 {
734 	int num = __close_file_table_ids(&work->sess->file_table,
735 					 work->tcon,
736 					 session_fd_check);
737 
738 	atomic_sub(num, &work->conn->stats.open_files_count);
739 }
740 
741 int ksmbd_init_global_file_table(void)
742 {
743 	return ksmbd_init_file_table(&global_ft);
744 }
745 
746 void ksmbd_free_global_file_table(void)
747 {
748 	struct ksmbd_file	*fp = NULL;
749 	unsigned int		id;
750 
751 	idr_for_each_entry(global_ft.idr, fp, id) {
752 		__ksmbd_remove_durable_fd(fp);
753 		kmem_cache_free(filp_cache, fp);
754 	}
755 
756 	ksmbd_destroy_file_table(&global_ft);
757 }
758 
759 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
760 				  struct ksmbd_file *fp, char *name)
761 {
762 	char *pathname, *ab_pathname;
763 	int ret = 0;
764 
765 	pathname = kmalloc(PATH_MAX, GFP_KERNEL);
766 	if (!pathname)
767 		return -EACCES;
768 
769 	ab_pathname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
770 	if (IS_ERR(ab_pathname)) {
771 		kfree(pathname);
772 		return -EACCES;
773 	}
774 
775 	if (name && strcmp(&ab_pathname[share->path_sz + 1], name)) {
776 		ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
777 		ret = -EINVAL;
778 	}
779 
780 	kfree(pathname);
781 
782 	return ret;
783 }
784 
785 int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp)
786 {
787 	struct ksmbd_inode *ci;
788 	struct oplock_info *op;
789 
790 	if (!fp->is_durable || fp->conn || fp->tcon) {
791 		pr_err("Invalid durable fd [%p:%p]\n", fp->conn, fp->tcon);
792 		return -EBADF;
793 	}
794 
795 	if (has_file_id(fp->volatile_id)) {
796 		pr_err("Still in use durable fd: %llu\n", fp->volatile_id);
797 		return -EBADF;
798 	}
799 
800 	fp->conn = work->conn;
801 	fp->tcon = work->tcon;
802 
803 	ci = fp->f_ci;
804 	down_write(&ci->m_lock);
805 	list_for_each_entry_rcu(op, &ci->m_op_list, op_entry) {
806 		if (op->conn)
807 			continue;
808 		op->conn = fp->conn;
809 	}
810 	up_write(&ci->m_lock);
811 
812 	__open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
813 	if (!has_file_id(fp->volatile_id)) {
814 		fp->conn = NULL;
815 		fp->tcon = NULL;
816 		return -EBADF;
817 	}
818 	return 0;
819 }
820 
821 int ksmbd_init_file_table(struct ksmbd_file_table *ft)
822 {
823 	ft->idr = kzalloc(sizeof(struct idr), GFP_KERNEL);
824 	if (!ft->idr)
825 		return -ENOMEM;
826 
827 	idr_init(ft->idr);
828 	rwlock_init(&ft->lock);
829 	return 0;
830 }
831 
832 void ksmbd_destroy_file_table(struct ksmbd_file_table *ft)
833 {
834 	if (!ft->idr)
835 		return;
836 
837 	__close_file_table_ids(ft, NULL, session_fd_check);
838 	idr_destroy(ft->idr);
839 	kfree(ft->idr);
840 	ft->idr = NULL;
841 }
842 
843 int ksmbd_init_file_cache(void)
844 {
845 	filp_cache = kmem_cache_create("ksmbd_file_cache",
846 				       sizeof(struct ksmbd_file), 0,
847 				       SLAB_HWCACHE_ALIGN, NULL);
848 	if (!filp_cache)
849 		goto out;
850 
851 	return 0;
852 
853 out:
854 	pr_err("failed to allocate file cache\n");
855 	return -ENOMEM;
856 }
857 
858 void ksmbd_exit_file_cache(void)
859 {
860 	kmem_cache_destroy(filp_cache);
861 }
862