xref: /linux/fs/smb/server/vfs_cache.c (revision 5ce5227cd60b7213359e91837727ed1b6d457d49)
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 #include <linux/kthread.h>
12 #include <linux/freezer.h>
13 #include <linux/dcache.h>
14 
15 #include "glob.h"
16 #include "vfs_cache.h"
17 #include "oplock.h"
18 #include "vfs.h"
19 #include "connection.h"
20 #include "misc.h"
21 #include "mgmt/tree_connect.h"
22 #include "mgmt/user_session.h"
23 #include "mgmt/user_config.h"
24 #include "smb_common.h"
25 #include "server.h"
26 #include "smb2pdu.h"
27 
28 #define S_DEL_PENDING			1
29 #define S_DEL_ON_CLS			2
30 #define S_DEL_ON_CLS_STREAM		8
31 
32 static unsigned int inode_hash_mask __read_mostly;
33 static unsigned int inode_hash_shift __read_mostly;
34 static struct hlist_head *inode_hashtable __read_mostly;
35 static DEFINE_RWLOCK(inode_hash_lock);
36 
37 static struct ksmbd_file_table global_ft;
38 static atomic_long_t fd_limit;
39 static struct kmem_cache *filp_cache;
40 
41 static int ksmbd_mark_fp_closed(struct ksmbd_file *fp);
42 
43 #define OPLOCK_NONE      0
44 #define OPLOCK_EXCLUSIVE 1
45 #define OPLOCK_BATCH     2
46 #define OPLOCK_READ      3  /* level 2 oplock */
47 
48 #ifdef CONFIG_PROC_FS
49 
50 static const struct ksmbd_const_name ksmbd_lease_const_names[] = {
51 	{le32_to_cpu(SMB2_LEASE_NONE_LE), "LEASE_NONE"},
52 	{le32_to_cpu(SMB2_LEASE_READ_CACHING_LE), "LEASE_R"},
53 	{le32_to_cpu(SMB2_LEASE_HANDLE_CACHING_LE), "LEASE_H"},
54 	{le32_to_cpu(SMB2_LEASE_WRITE_CACHING_LE), "LEASE_W"},
55 	{le32_to_cpu(SMB2_LEASE_READ_CACHING_LE |
56 		     SMB2_LEASE_HANDLE_CACHING_LE), "LEASE_RH"},
57 	{le32_to_cpu(SMB2_LEASE_READ_CACHING_LE |
58 		     SMB2_LEASE_WRITE_CACHING_LE), "LEASE_RW"},
59 	{le32_to_cpu(SMB2_LEASE_HANDLE_CACHING_LE |
60 		     SMB2_LEASE_WRITE_CACHING_LE), "LEASE_WH"},
61 	{le32_to_cpu(SMB2_LEASE_READ_CACHING_LE |
62 		     SMB2_LEASE_HANDLE_CACHING_LE |
63 		     SMB2_LEASE_WRITE_CACHING_LE), "LEASE_RWH"},
64 };
65 
66 static const struct ksmbd_const_name ksmbd_oplock_const_names[] = {
67 	{SMB2_OPLOCK_LEVEL_NONE, "OPLOCK_NONE"},
68 	{SMB2_OPLOCK_LEVEL_II, "OPLOCK_II"},
69 	{SMB2_OPLOCK_LEVEL_EXCLUSIVE, "OPLOCK_EXCLUSIVE"},
70 	{SMB2_OPLOCK_LEVEL_BATCH, "OPLOCK_BATCH"},
71 };
72 
73 static const struct ksmbd_const_name ksmbd_file_state_names[] = {
74 	{FP_NEW, "new"},
75 	{FP_INITED, "open"},
76 	{FP_CLOSED, "closed"},
77 };
78 
79 #define KSMBD_PROC_FILE_DURABLE		BIT(0)
80 #define KSMBD_PROC_FILE_PERSISTENT	BIT(1)
81 #define KSMBD_PROC_FILE_RESILIENT	BIT(2)
82 #define KSMBD_PROC_FILE_DELETE_ON_CLOSE	BIT(3)
83 #define KSMBD_PROC_FILE_STREAM		BIT(4)
84 #define KSMBD_PROC_FILE_POSIX		BIT(5)
85 #define KSMBD_PROC_FILE_ATTRIB_ONLY	BIT(6)
86 
87 static const struct ksmbd_const_name ksmbd_file_flag_names[] = {
88 	{KSMBD_PROC_FILE_DURABLE, "durable"},
89 	{KSMBD_PROC_FILE_PERSISTENT, "persistent"},
90 	{KSMBD_PROC_FILE_RESILIENT, "resilient"},
91 	{KSMBD_PROC_FILE_DELETE_ON_CLOSE, "delete-on-close"},
92 	{KSMBD_PROC_FILE_STREAM, "stream"},
93 	{KSMBD_PROC_FILE_POSIX, "posix"},
94 	{KSMBD_PROC_FILE_ATTRIB_ONLY, "attrib-only"},
95 };
96 
97 static unsigned int ksmbd_proc_file_flags(struct ksmbd_file *fp)
98 {
99 	unsigned int flags = 0;
100 
101 	if (fp->is_durable)
102 		flags |= KSMBD_PROC_FILE_DURABLE;
103 	if (fp->is_persistent)
104 		flags |= KSMBD_PROC_FILE_PERSISTENT;
105 	if (fp->is_resilient)
106 		flags |= KSMBD_PROC_FILE_RESILIENT;
107 	if (fp->coption & FILE_DELETE_ON_CLOSE_LE)
108 		flags |= KSMBD_PROC_FILE_DELETE_ON_CLOSE;
109 	if (fp->stream.name)
110 		flags |= KSMBD_PROC_FILE_STREAM;
111 	if (fp->is_posix_ctxt)
112 		flags |= KSMBD_PROC_FILE_POSIX;
113 	if (fp->attrib_only)
114 		flags |= KSMBD_PROC_FILE_ATTRIB_ONLY;
115 	return flags;
116 }
117 
118 static int proc_show_files(struct seq_file *m, void *v)
119 {
120 	struct ksmbd_file *fp = NULL;
121 	unsigned int id;
122 	struct oplock_info *opinfo;
123 
124 	read_lock(&global_ft.lock);
125 	idr_for_each_entry(global_ft.idr, fp, id) {
126 		seq_printf(m, "tree_id:\t0x%x\n", fp->tcon ? fp->tcon->id : 0);
127 		seq_printf(m, "persistent_id:\t0x%llx\n", fp->persistent_id);
128 		seq_printf(m, "volatile_id:\t0x%llx\n", fp->volatile_id);
129 		seq_printf(m, "refcount:\t%d\n", atomic_read(&fp->refcount));
130 
131 		rcu_read_lock();
132 		opinfo = rcu_dereference(fp->f_opinfo);
133 		if (opinfo) {
134 			const struct ksmbd_const_name *const_names;
135 			const char *name;
136 			int count;
137 			unsigned int level;
138 
139 			if (opinfo->is_lease) {
140 				const_names = ksmbd_lease_const_names;
141 				count = ARRAY_SIZE(ksmbd_lease_const_names);
142 				level = le32_to_cpu(opinfo->o_lease->state);
143 			} else {
144 				const_names = ksmbd_oplock_const_names;
145 				count = ARRAY_SIZE(ksmbd_oplock_const_names);
146 				level = opinfo->level;
147 			}
148 			rcu_read_unlock();
149 			name = ksmbd_proc_const_name(const_names, count, level);
150 			if (name)
151 				seq_printf(m, "oplock:\t%s\n", name);
152 			else
153 				seq_printf(m, "oplock:\t0x%x\n", level);
154 		} else {
155 			rcu_read_unlock();
156 			seq_puts(m, "oplock:\tnone\n");
157 		}
158 
159 		seq_printf(m, "state:\t%s\n",
160 			   ksmbd_proc_const_name(ksmbd_file_state_names,
161 						 ARRAY_SIZE(ksmbd_file_state_names),
162 						 fp->f_state));
163 		seq_printf(m, "durable_timeout:\t%u\n", fp->durable_timeout);
164 		seq_printf(m, "create_options:\t0x%08x\n",
165 			   le32_to_cpu(fp->coption));
166 		seq_printf(m, "desired_access:\t0x%08x\n",
167 			   le32_to_cpu(fp->daccess));
168 		seq_printf(m, "share_access:\t0x%08x\n",
169 			   le32_to_cpu(fp->saccess));
170 		seq_puts(m, "flags:\t");
171 		ksmbd_proc_show_flag_names(m, ksmbd_file_flag_names,
172 					   ARRAY_SIZE(ksmbd_file_flag_names),
173 					   ksmbd_proc_file_flags(fp));
174 		seq_printf(m, "\nname:\t%s\n\n",
175 			   fp->filp->f_path.dentry->d_name.name);
176 	}
177 	read_unlock(&global_ft.lock);
178 	return 0;
179 }
180 
181 static int create_proc_files(void)
182 {
183 	ksmbd_proc_create("files", proc_show_files, NULL);
184 	return 0;
185 }
186 #else
187 static int create_proc_files(void) { return 0; }
188 #endif
189 
190 static bool durable_scavenger_running;
191 static DEFINE_MUTEX(durable_scavenger_lock);
192 static wait_queue_head_t dh_wq;
193 
194 bool ksmbd_durable_scavenger_active(void)
195 {
196 	bool active;
197 
198 	mutex_lock(&durable_scavenger_lock);
199 	active = durable_scavenger_running;
200 	mutex_unlock(&durable_scavenger_lock);
201 	return active;
202 }
203 
204 void ksmbd_set_fd_limit(unsigned long limit)
205 {
206 	limit = min(limit, get_max_files());
207 	atomic_long_set(&fd_limit, limit);
208 }
209 
210 static bool fd_limit_depleted(void)
211 {
212 	long v = atomic_long_dec_return(&fd_limit);
213 
214 	if (v >= 0)
215 		return false;
216 	atomic_long_inc(&fd_limit);
217 	return true;
218 }
219 
220 static void fd_limit_close(void)
221 {
222 	atomic_long_inc(&fd_limit);
223 }
224 
225 /*
226  * INODE hash
227  */
228 
229 static unsigned long inode_hash(struct super_block *sb, unsigned long hashval)
230 {
231 	unsigned long tmp;
232 
233 	tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
234 		L1_CACHE_BYTES;
235 	tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> inode_hash_shift);
236 	return tmp & inode_hash_mask;
237 }
238 
239 static struct ksmbd_inode *__ksmbd_inode_lookup(struct dentry *de)
240 {
241 	struct hlist_head *head = inode_hashtable +
242 		inode_hash(d_inode(de)->i_sb, (unsigned long)de);
243 	struct ksmbd_inode *ci = NULL, *ret_ci = NULL;
244 
245 	hlist_for_each_entry(ci, head, m_hash) {
246 		if (ci->m_de == de) {
247 			if (atomic_inc_not_zero(&ci->m_count))
248 				ret_ci = ci;
249 			break;
250 		}
251 	}
252 	return ret_ci;
253 }
254 
255 static struct ksmbd_inode *ksmbd_inode_lookup(struct ksmbd_file *fp)
256 {
257 	return __ksmbd_inode_lookup(fp->filp->f_path.dentry);
258 }
259 
260 struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d)
261 {
262 	struct ksmbd_inode *ci;
263 
264 	read_lock(&inode_hash_lock);
265 	ci = __ksmbd_inode_lookup(d);
266 	read_unlock(&inode_hash_lock);
267 
268 	return ci;
269 }
270 
271 int ksmbd_query_inode_status(struct dentry *dentry)
272 {
273 	struct ksmbd_inode *ci;
274 	int ret = KSMBD_INODE_STATUS_UNKNOWN;
275 
276 	read_lock(&inode_hash_lock);
277 	ci = __ksmbd_inode_lookup(dentry);
278 	read_unlock(&inode_hash_lock);
279 	if (!ci)
280 		return ret;
281 
282 	down_read(&ci->m_lock);
283 	if (ci->m_flags & S_DEL_PENDING)
284 		ret = KSMBD_INODE_STATUS_PENDING_DELETE;
285 	else
286 		ret = KSMBD_INODE_STATUS_OK;
287 	up_read(&ci->m_lock);
288 
289 	ksmbd_inode_put(ci);
290 	return ret;
291 }
292 
293 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp)
294 {
295 	struct ksmbd_inode *ci = fp->f_ci;
296 	int ret;
297 
298 	down_read(&ci->m_lock);
299 	ret = (ci->m_flags & S_DEL_PENDING);
300 	up_read(&ci->m_lock);
301 	if (ret || !ksmbd_stream_fd(fp))
302 		return ret;
303 
304 	spin_lock(&fp->f_lock);
305 	ret = fp->stream_del_pending;
306 	spin_unlock(&fp->f_lock);
307 
308 	return ret;
309 }
310 
311 void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp)
312 {
313 	struct ksmbd_inode *ci = fp->f_ci;
314 
315 	down_write(&ci->m_lock);
316 	ci->m_flags |= S_DEL_PENDING;
317 	up_write(&ci->m_lock);
318 }
319 
320 void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp)
321 {
322 	struct ksmbd_inode *ci = fp->f_ci;
323 
324 	down_write(&ci->m_lock);
325 	ci->m_flags &= ~S_DEL_PENDING;
326 	up_write(&ci->m_lock);
327 }
328 
329 bool ksmbd_has_stream_without_delete_share(struct ksmbd_file *fp)
330 {
331 	struct ksmbd_file *prev_fp;
332 	struct ksmbd_inode *ci = fp->f_ci;
333 	bool ret = false;
334 
335 	if (ksmbd_stream_fd(fp))
336 		return false;
337 
338 	down_read(&ci->m_lock);
339 	list_for_each_entry(prev_fp, &ci->m_fp_list, node) {
340 		if (prev_fp == fp || !ksmbd_stream_fd(prev_fp))
341 			continue;
342 
343 		if (file_inode(fp->filp) != file_inode(prev_fp->filp))
344 			continue;
345 
346 		if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE)) {
347 			ret = true;
348 			break;
349 		}
350 	}
351 	up_read(&ci->m_lock);
352 
353 	return ret;
354 }
355 
356 void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp,
357 				  int file_info)
358 {
359 	struct ksmbd_inode *ci = fp->f_ci;
360 
361 	down_write(&ci->m_lock);
362 	if (ksmbd_stream_fd(fp))
363 		ci->m_flags |= S_DEL_ON_CLS_STREAM;
364 	else
365 		ci->m_flags |= S_DEL_ON_CLS;
366 	up_write(&ci->m_lock);
367 }
368 
369 /*
370  * FileDispositionInformation (SET_INFO) on a stream handle must only
371  * mark the stream for deletion, not the whole file -- otherwise
372  * deleting a single alternate data stream (e.g. AFP_AfpInfo) deletes
373  * the entire file's data along with it.
374  *
375  * This is tracked on fp itself (stream_del_pending), not the shared
376  * ksmbd_inode: the inode-wide S_DEL_ON_CLS_STREAM flag used by
377  * ksmbd_fd_set_delete_on_close() can't record *which* stream should be
378  * deleted, so if a different stream handle on the same file closed
379  * first, it would delete the wrong stream.
380  */
381 void ksmbd_fd_set_delete_pending(struct ksmbd_file *fp)
382 {
383 	if (ksmbd_stream_fd(fp)) {
384 		spin_lock(&fp->f_lock);
385 		fp->stream_del_pending = true;
386 		spin_unlock(&fp->f_lock);
387 	} else {
388 		ksmbd_set_inode_pending_delete(fp);
389 	}
390 }
391 
392 void ksmbd_fd_clear_delete_pending(struct ksmbd_file *fp)
393 {
394 	if (ksmbd_stream_fd(fp)) {
395 		spin_lock(&fp->f_lock);
396 		fp->stream_del_pending = false;
397 		spin_unlock(&fp->f_lock);
398 	} else {
399 		ksmbd_clear_inode_pending_delete(fp);
400 	}
401 }
402 
403 static void ksmbd_inode_hash(struct ksmbd_inode *ci)
404 {
405 	struct hlist_head *b = inode_hashtable +
406 		inode_hash(d_inode(ci->m_de)->i_sb, (unsigned long)ci->m_de);
407 
408 	hlist_add_head(&ci->m_hash, b);
409 }
410 
411 static void ksmbd_inode_unhash(struct ksmbd_inode *ci)
412 {
413 	write_lock(&inode_hash_lock);
414 	hlist_del_init(&ci->m_hash);
415 	write_unlock(&inode_hash_lock);
416 }
417 
418 static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp)
419 {
420 	atomic_set(&ci->m_count, 1);
421 	atomic_set(&ci->op_count, 0);
422 	atomic_set(&ci->sop_count, 0);
423 	ci->m_flags = 0;
424 	ci->m_fattr = 0;
425 	INIT_LIST_HEAD(&ci->m_fp_list);
426 	INIT_LIST_HEAD(&ci->m_op_list);
427 	init_rwsem(&ci->m_lock);
428 	ci->m_de = fp->filp->f_path.dentry;
429 	return 0;
430 }
431 
432 static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp)
433 {
434 	struct ksmbd_inode *ci, *tmpci;
435 	int rc;
436 
437 	read_lock(&inode_hash_lock);
438 	ci = ksmbd_inode_lookup(fp);
439 	read_unlock(&inode_hash_lock);
440 	if (ci)
441 		return ci;
442 
443 	ci = kmalloc_obj(struct ksmbd_inode, KSMBD_DEFAULT_GFP);
444 	if (!ci)
445 		return NULL;
446 
447 	rc = ksmbd_inode_init(ci, fp);
448 	if (rc) {
449 		pr_err("inode initialized failed\n");
450 		kfree(ci);
451 		return NULL;
452 	}
453 
454 	write_lock(&inode_hash_lock);
455 	tmpci = ksmbd_inode_lookup(fp);
456 	if (!tmpci) {
457 		ksmbd_inode_hash(ci);
458 	} else {
459 		kfree(ci);
460 		ci = tmpci;
461 	}
462 	write_unlock(&inode_hash_lock);
463 	return ci;
464 }
465 
466 static void ksmbd_inode_free(struct ksmbd_inode *ci)
467 {
468 	ksmbd_inode_unhash(ci);
469 	kfree(ci);
470 }
471 
472 void ksmbd_inode_put(struct ksmbd_inode *ci)
473 {
474 	if (atomic_dec_and_test(&ci->m_count))
475 		ksmbd_inode_free(ci);
476 }
477 
478 int __init ksmbd_inode_hash_init(void)
479 {
480 	unsigned int loop;
481 	unsigned long numentries = 16384;
482 	unsigned long bucketsize = sizeof(struct hlist_head);
483 	unsigned long size;
484 
485 	inode_hash_shift = ilog2(numentries);
486 	inode_hash_mask = (1 << inode_hash_shift) - 1;
487 
488 	size = bucketsize << inode_hash_shift;
489 
490 	/* init master fp hash table */
491 	inode_hashtable = vmalloc(size);
492 	if (!inode_hashtable)
493 		return -ENOMEM;
494 
495 	for (loop = 0; loop < (1U << inode_hash_shift); loop++)
496 		INIT_HLIST_HEAD(&inode_hashtable[loop]);
497 	return 0;
498 }
499 
500 void ksmbd_release_inode_hash(void)
501 {
502 	vfree(inode_hashtable);
503 }
504 
505 static void __ksmbd_inode_close(struct ksmbd_file *fp)
506 {
507 	struct ksmbd_inode *ci = fp->f_ci;
508 	int err;
509 	struct file *filp;
510 
511 	filp = fp->filp;
512 
513 	if (ksmbd_stream_fd(fp)) {
514 		bool remove_stream_xattr = false;
515 
516 		down_write(&ci->m_lock);
517 		if (ci->m_flags & S_DEL_ON_CLS_STREAM) {
518 			ci->m_flags &= ~S_DEL_ON_CLS_STREAM;
519 			remove_stream_xattr = true;
520 		}
521 		up_write(&ci->m_lock);
522 
523 		/*
524 		 * Per-handle delete-pending from ksmbd_fd_set_delete_pending()
525 		 * (FileDispositionInformation on this stream) -- separate from
526 		 * the inode-wide flag above, which only ever meant "some
527 		 * stream on this file" with no way to say which one.
528 		 */
529 		spin_lock(&fp->f_lock);
530 		if (fp->stream_del_pending) {
531 			fp->stream_del_pending = false;
532 			remove_stream_xattr = true;
533 		}
534 		spin_unlock(&fp->f_lock);
535 
536 		if (remove_stream_xattr) {
537 			const struct cred *saved_cred;
538 
539 			saved_cred = override_creds(filp->f_cred);
540 			err = ksmbd_vfs_remove_xattr(file_mnt_idmap(filp),
541 						     &filp->f_path,
542 						     fp->stream.name,
543 						     true);
544 			revert_creds(saved_cred);
545 			if (err)
546 				pr_err("remove xattr failed : %s\n",
547 				       fp->stream.name);
548 		}
549 	}
550 
551 	down_write(&ci->m_lock);
552 	/* Promote S_DEL_ON_CLS to S_DEL_PENDING when close */
553 	if (ci->m_flags & S_DEL_ON_CLS) {
554 		ci->m_flags &= ~S_DEL_ON_CLS;
555 		ci->m_flags |= S_DEL_PENDING;
556 	}
557 	up_write(&ci->m_lock);
558 
559 	if (atomic_dec_and_test(&ci->m_count)) {
560 		bool do_unlink = false;
561 
562 		down_write(&ci->m_lock);
563 		if (ci->m_flags & S_DEL_PENDING) {
564 			ci->m_flags &= ~S_DEL_PENDING;
565 			do_unlink = true;
566 		}
567 		up_write(&ci->m_lock);
568 
569 		if (do_unlink)
570 			ksmbd_vfs_unlink(filp);
571 
572 		ksmbd_inode_free(ci);
573 	}
574 }
575 
576 static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp)
577 {
578 	if (!has_file_id(fp->persistent_id))
579 		return;
580 
581 	idr_remove(global_ft.idr, fp->persistent_id);
582 	/*
583 	 * Clear persistent_id so a later __ksmbd_close_fd() that runs from a
584 	 * delayed putter (e.g. when a concurrent ksmbd_lookup_fd_inode()
585 	 * walker held the final reference) does not re-issue idr_remove() on
586 	 * an id that idr_alloc_cyclic() may have already handed out to a new
587 	 * durable handle.
588 	 */
589 	fp->persistent_id = KSMBD_NO_FID;
590 }
591 
592 static void ksmbd_remove_durable_fd(struct ksmbd_file *fp)
593 {
594 	write_lock(&global_ft.lock);
595 	__ksmbd_remove_durable_fd(fp);
596 	write_unlock(&global_ft.lock);
597 	if (waitqueue_active(&dh_wq))
598 		wake_up(&dh_wq);
599 }
600 
601 static void __ksmbd_remove_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
602 {
603 	down_write(&fp->f_ci->m_lock);
604 	list_del_init(&fp->node);
605 	up_write(&fp->f_ci->m_lock);
606 
607 	if (!has_file_id(fp->volatile_id))
608 		return;
609 
610 	write_lock(&ft->lock);
611 	idr_remove(ft->idr, fp->volatile_id);
612 	write_unlock(&ft->lock);
613 }
614 
615 static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
616 {
617 	struct file *filp;
618 	struct ksmbd_lock *smb_lock, *tmp_lock;
619 	struct ksmbd_work *cn_work;
620 
621 	fd_limit_close();
622 	ksmbd_remove_durable_fd(fp);
623 	if (ft)
624 		__ksmbd_remove_fd(ft, fp);
625 
626 	close_id_del_oplock(fp);
627 	filp = fp->filp;
628 
629 	__ksmbd_inode_close(fp);
630 	if (!IS_ERR_OR_NULL(filp))
631 		fput(filp);
632 
633 	/* because the reference count of fp is 0, it is guaranteed that
634 	 * there are not accesses to fp->lock_list.
635 	 */
636 	list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
637 		struct ksmbd_conn *conn = smb_lock->conn;
638 
639 		if (conn) {
640 			spin_lock(&conn->llist_lock);
641 			list_del_init(&smb_lock->clist);
642 			smb_lock->conn = NULL;
643 			spin_unlock(&conn->llist_lock);
644 			ksmbd_conn_put(conn);
645 		}
646 
647 		list_del(&smb_lock->flist);
648 		locks_free_lock(smb_lock->fl);
649 		kfree(smb_lock);
650 	}
651 
652 	/*
653 	 * Complete any CHANGE_NOTIFY left pending on this handle now that
654 	 * it is closed. KSMBD never completes CHANGE_NOTIFY spontaneously
655 	 * (no real change-notification backend), only on close -- matching
656 	 * genuine SMB2/macOS smbfs semantics and avoiding the Finder
657 	 * "directory changed, re-enumerate everything" loop.
658 	 *
659 	 * smb2_notify() on another connection can be adding to
660 	 * notify_pendings under fp->f_lock at the same time this handle is
661 	 * closed, and a client-sent CANCEL can concurrently be racing to
662 	 * claim the same entry via smb2_notify_cancel_fn() (smb2pdu.c).
663 	 * Pop one entry at a time under the lock via list_del_init() rather
664 	 * than a bulk list_splice_init(): list_del_init() leaves the node
665 	 * self-linked ("empty"), which is what the cancel path checks under
666 	 * the same lock to tell whether it lost the race -- a bulk splice
667 	 * would instead relink every entry into a shared local list, so an
668 	 * entry claimed here would still read as "not empty" to a racing
669 	 * cancel_fn, and both sides could end up freeing the same work.
670 	 * ksmbd_conn_write() can sleep (it takes conn's write mutex), so it
671 	 * must not be called while fp->f_lock is held -- release the lock
672 	 * before processing each popped entry, then reacquire it for the
673 	 * next.
674 	 */
675 	for (;;) {
676 		spin_lock(&fp->f_lock);
677 		if (list_empty(&fp->notify_pendings)) {
678 			spin_unlock(&fp->f_lock);
679 			break;
680 		}
681 		cn_work = list_first_entry(&fp->notify_pendings,
682 					   struct ksmbd_work, notify_entry);
683 		list_del_init(&cn_work->notify_entry);
684 		spin_unlock(&fp->f_lock);
685 
686 		ksmbd_conn_write(cn_work);
687 		/*
688 		 * release_async_work() removes cn_work from
689 		 * conn->async_requests, frees cancel_argv, and releases+zeroes
690 		 * async_id -- all needed before ksmbd_free_work_struct(), which
691 		 * only releases async_id itself if still nonzero (i.e. if this
692 		 * hadn't already been done).
693 		 */
694 		release_async_work(cn_work);
695 		ksmbd_free_work_struct(cn_work);
696 	}
697 
698 	/*
699 	 * Drop fp's strong reference on conn (taken in ksmbd_open_fd() /
700 	 * ksmbd_reopen_durable_fd()).  Durable fps that reached the
701 	 * scavenger have already had fp->conn cleared by session_fd_check(),
702 	 * in which case there is nothing to drop here.
703 	 */
704 	if (fp->conn) {
705 		ksmbd_conn_put(fp->conn);
706 		fp->conn = NULL;
707 	}
708 
709 	if (ksmbd_stream_fd(fp))
710 		kfree(fp->stream.name);
711 	kfree(fp->owner.name);
712 
713 	kmem_cache_free(filp_cache, fp);
714 }
715 
716 /**
717  * ksmbd_close_disconnected_durable_delete_on_close() - drop a delete-on-close
718  *	file kept present only by disconnected durable handles
719  * @dentry:	dentry of the file being opened
720  *
721  * A durable handle opened with delete-on-close is preserved across a
722  * disconnect so it can be reclaimed by a durable reconnect.  When a new
723  * (non-reconnect) open arrives for the same name instead, the disconnected
724  * handle has to give way.  Close such handles so their delete-on-close is
725  * applied and the file is removed once the last handle is gone, letting the
726  * new open create a fresh file.
727  *
728  * The caller's inode reference is dropped before closing so that the final
729  * close can promote S_DEL_ON_CLS to S_DEL_PENDING and unlink the file.
730  *
731  * Return:	true if a disconnected durable handle was closed.
732  */
733 bool ksmbd_close_disconnected_durable_delete_on_close(struct dentry *dentry)
734 {
735 	struct ksmbd_inode *ci;
736 	struct ksmbd_file *fp, *tmp;
737 	LIST_HEAD(dispose);
738 	bool closed = false;
739 
740 	ci = ksmbd_inode_lookup_lock(dentry);
741 	if (!ci)
742 		return false;
743 
744 	down_write(&ci->m_lock);
745 	if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_ON_CLS_STREAM | S_DEL_PENDING)) {
746 		list_for_each_entry_safe(fp, tmp, &ci->m_fp_list, node) {
747 			if (fp->conn || !fp->is_durable ||
748 			    fp->f_state != FP_INITED)
749 				continue;
750 
751 			/*
752 			 * Claim the close before unlinking fp from m_fp_list.
753 			 * refcount == 1 means only the durable lifetime ref is
754 			 * left. Add a transient ref so final close can drop both.
755 			 */
756 			write_lock(&global_ft.lock);
757 			if (atomic_read(&fp->refcount) == 1) {
758 				atomic_inc(&fp->refcount);
759 				__ksmbd_remove_durable_fd(fp);
760 				ksmbd_mark_fp_closed(fp);
761 				list_move_tail(&fp->node, &dispose);
762 			}
763 			write_unlock(&global_ft.lock);
764 		}
765 	}
766 	up_write(&ci->m_lock);
767 
768 	/*
769 	 * Drop our lookup reference before closing so the last __ksmbd_close_fd()
770 	 * can drop m_count to zero and unlink the delete-on-close file.  The
771 	 * collected handles still hold the transient reference taken above, so
772 	 * ci stays valid until they are closed below.
773 	 */
774 	ksmbd_inode_put(ci);
775 
776 	while (!list_empty(&dispose)) {
777 		fp = list_first_entry(&dispose, struct ksmbd_file, node);
778 		list_del_init(&fp->node);
779 		if (atomic_sub_and_test(2, &fp->refcount)) {
780 			__ksmbd_close_fd(NULL, fp);
781 			closed = true;
782 		}
783 	}
784 
785 	return closed;
786 }
787 
788 static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp)
789 {
790 	if (fp->f_state != FP_INITED)
791 		return NULL;
792 
793 	if (!atomic_inc_not_zero(&fp->refcount))
794 		return NULL;
795 	return fp;
796 }
797 
798 struct ksmbd_file *ksmbd_file_get(struct ksmbd_file *fp)
799 {
800 	return ksmbd_fp_get(fp);
801 }
802 
803 static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft,
804 					    u64 id)
805 {
806 	struct ksmbd_file *fp;
807 
808 	if (!has_file_id(id))
809 		return NULL;
810 
811 	read_lock(&ft->lock);
812 	fp = idr_find(ft->idr, id);
813 	if (fp)
814 		fp = ksmbd_fp_get(fp);
815 	read_unlock(&ft->lock);
816 	return fp;
817 }
818 
819 static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp)
820 {
821 	/*
822 	 * Detached durable fp -- session_fd_check() cleared fp->conn at
823 	 * preserve, so this fp is no longer tracked by any conn's
824 	 * stats.open_files_count.  This happens when
825 	 * ksmbd_scavenger_dispose_dh() hands the final close off to an
826 	 * m_fp_list walker (e.g. ksmbd_lookup_fd_inode()) whose work->conn
827 	 * is unrelated to the conn that originally opened the handle; close
828 	 * via the NULL-ft path so we do not underflow that unrelated
829 	 * counter.
830 	 */
831 	if (!fp->conn) {
832 		__ksmbd_close_fd(NULL, fp);
833 		return;
834 	}
835 	__ksmbd_close_fd(&work->sess->file_table, fp);
836 	atomic_dec(&work->conn->stats.open_files_count);
837 }
838 
839 static void set_close_state_blocked_works(struct ksmbd_file *fp)
840 {
841 	struct ksmbd_work *cancel_work;
842 
843 	spin_lock(&fp->f_lock);
844 	list_for_each_entry(cancel_work, &fp->blocked_works,
845 				 fp_entry) {
846 		cancel_work->state = KSMBD_WORK_CLOSED;
847 		cancel_work->cancel_fn(cancel_work->cancel_argv);
848 	}
849 	spin_unlock(&fp->f_lock);
850 }
851 
852 int ksmbd_close_fd(struct ksmbd_work *work, u64 id)
853 {
854 	struct ksmbd_file	*fp;
855 	struct ksmbd_file_table	*ft;
856 	bool closed = false;
857 
858 	if (!has_file_id(id))
859 		return 0;
860 
861 	ft = &work->sess->file_table;
862 	write_lock(&ft->lock);
863 	fp = idr_find(ft->idr, id);
864 	if (fp) {
865 		set_close_state_blocked_works(fp);
866 
867 		if (fp->f_state != FP_INITED)
868 			fp = NULL;
869 		else {
870 			fp->f_state = FP_CLOSED;
871 			idr_remove(ft->idr, id);
872 			fp->volatile_id = KSMBD_NO_FID;
873 			closed = true;
874 			if (!atomic_dec_and_test(&fp->refcount))
875 				fp = NULL;
876 		}
877 	}
878 	write_unlock(&ft->lock);
879 
880 	if (!fp)
881 		return closed ? 0 : -EINVAL;
882 
883 	__put_fd_final(work, fp);
884 	return 0;
885 }
886 
887 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp)
888 {
889 	if (!fp)
890 		return;
891 
892 	if (!atomic_dec_and_test(&fp->refcount))
893 		return;
894 	__put_fd_final(work, fp);
895 }
896 
897 static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp)
898 {
899 	if (!fp)
900 		return false;
901 	if (fp->tcon != tcon)
902 		return false;
903 	return true;
904 }
905 
906 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id)
907 {
908 	return __ksmbd_lookup_fd(&work->sess->file_table, id);
909 }
910 
911 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id)
912 {
913 	struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
914 
915 	if (__sanity_check(work->tcon, fp))
916 		return fp;
917 
918 	ksmbd_fd_put(work, fp);
919 	return NULL;
920 }
921 
922 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id,
923 					u64 pid)
924 {
925 	struct ksmbd_file *fp;
926 
927 	if (!has_file_id(id)) {
928 		id = work->compound_fid;
929 		pid = work->compound_pfid;
930 	}
931 
932 	fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
933 	if (!__sanity_check(work->tcon, fp)) {
934 		ksmbd_fd_put(work, fp);
935 		return NULL;
936 	}
937 	if (fp->persistent_id != pid) {
938 		ksmbd_fd_put(work, fp);
939 		return NULL;
940 	}
941 	return fp;
942 }
943 
944 struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id)
945 {
946 	return __ksmbd_lookup_fd(&global_ft, id);
947 }
948 
949 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id)
950 {
951 	struct ksmbd_file *fp;
952 
953 	fp = __ksmbd_lookup_fd(&global_ft, id);
954 	if (fp && (fp->durable_reconnect_disabled ||
955 		   fp->conn ||
956 		   (fp->durable_scavenger_timeout &&
957 		    (fp->durable_scavenger_timeout <
958 		     jiffies_to_msecs(jiffies))))) {
959 		ksmbd_put_durable_fd(fp);
960 		fp = NULL;
961 	}
962 
963 	return fp;
964 }
965 
966 void ksmbd_put_durable_fd(struct ksmbd_file *fp)
967 {
968 	if (!atomic_dec_and_test(&fp->refcount))
969 		return;
970 
971 	__ksmbd_close_fd(NULL, fp);
972 }
973 
974 bool ksmbd_has_other_active_fd(struct ksmbd_file *fp)
975 {
976 	struct ksmbd_file *lfp;
977 	struct ksmbd_inode *ci = fp->f_ci;
978 	bool ret = false;
979 
980 	down_read(&ci->m_lock);
981 	list_for_each_entry(lfp, &ci->m_fp_list, node) {
982 		if (lfp == fp)
983 			continue;
984 
985 		if (lfp->f_state == FP_INITED &&
986 		    (READ_ONCE(lfp->conn) || READ_ONCE(lfp->tcon))) {
987 			ret = true;
988 			break;
989 		}
990 	}
991 	up_read(&ci->m_lock);
992 
993 	return ret;
994 }
995 
996 static struct ksmbd_file *ksmbd_lookup_fd_app_instance_id(char *app_instance_id)
997 {
998 	struct ksmbd_file *fp = NULL;
999 	unsigned int id;
1000 
1001 	if (!memchr_inv(app_instance_id, 0, SMB2_CREATE_GUID_SIZE))
1002 		return NULL;
1003 
1004 	read_lock(&global_ft.lock);
1005 	idr_for_each_entry(global_ft.idr, fp, id) {
1006 		if (!memcmp(fp->app_instance_id, app_instance_id,
1007 			    SMB2_CREATE_GUID_SIZE)) {
1008 			fp = ksmbd_fp_get(fp);
1009 			break;
1010 		}
1011 	}
1012 	read_unlock(&global_ft.lock);
1013 
1014 	return fp;
1015 }
1016 
1017 int ksmbd_close_fd_app_instance_id(char *app_instance_id)
1018 {
1019 	struct ksmbd_file_table *ft;
1020 	struct ksmbd_file *fp;
1021 	struct oplock_info *opinfo;
1022 	int n_to_drop = 0;
1023 
1024 	fp = ksmbd_lookup_fd_app_instance_id(app_instance_id);
1025 	if (!fp)
1026 		return 0;
1027 
1028 	opinfo = opinfo_get(fp);
1029 	if (!opinfo)
1030 		goto out;
1031 
1032 	down_read(&fp->f_ci->m_lock);
1033 	if (!opinfo->conn) {
1034 		up_read(&fp->f_ci->m_lock);
1035 		goto out;
1036 	}
1037 
1038 	ft = &opinfo->sess->file_table;
1039 	write_lock(&ft->lock);
1040 	if (fp->f_state == FP_INITED && has_file_id(fp->volatile_id)) {
1041 		idr_remove(ft->idr, fp->volatile_id);
1042 		fp->volatile_id = KSMBD_NO_FID;
1043 		n_to_drop = ksmbd_mark_fp_closed(fp);
1044 	}
1045 	write_unlock(&ft->lock);
1046 	up_read(&fp->f_ci->m_lock);
1047 	opinfo_put(opinfo);
1048 	opinfo = NULL;
1049 
1050 	if (!n_to_drop)
1051 		goto out;
1052 
1053 	down_write(&fp->f_ci->m_lock);
1054 	list_del_init(&fp->node);
1055 	up_write(&fp->f_ci->m_lock);
1056 
1057 	if (atomic_sub_and_test(n_to_drop, &fp->refcount)) {
1058 		if (fp->conn)
1059 			atomic_dec(&fp->conn->stats.open_files_count);
1060 		__ksmbd_close_fd(NULL, fp);
1061 	}
1062 	return 0;
1063 
1064 out:
1065 	if (opinfo)
1066 		opinfo_put(opinfo);
1067 	ksmbd_put_durable_fd(fp);
1068 	return 0;
1069 }
1070 
1071 int ksmbd_invalidate_durable_fd(unsigned long long id)
1072 {
1073 	struct ksmbd_file *fp;
1074 
1075 	fp = ksmbd_lookup_global_fd(id);
1076 	if (!fp)
1077 		return -ENOENT;
1078 
1079 	fp->durable_reconnect_disabled = true;
1080 
1081 	if (fp->conn) {
1082 		ksmbd_put_durable_fd(fp);
1083 		return -ENOENT;
1084 	}
1085 
1086 	fp->durable_timeout = 1;
1087 	fp->durable_scavenger_timeout = jiffies_to_msecs(jiffies);
1088 	ksmbd_put_durable_fd(fp);
1089 	if (waitqueue_active(&dh_wq))
1090 		wake_up(&dh_wq);
1091 
1092 	return -ENOENT;
1093 }
1094 
1095 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid)
1096 {
1097 	struct ksmbd_file	*fp = NULL;
1098 	unsigned int		id;
1099 
1100 	read_lock(&global_ft.lock);
1101 	idr_for_each_entry(global_ft.idr, fp, id) {
1102 		if (!memcmp(fp->create_guid,
1103 			    cguid,
1104 			    SMB2_CREATE_GUID_SIZE)) {
1105 			fp = ksmbd_fp_get(fp);
1106 			break;
1107 		}
1108 	}
1109 	read_unlock(&global_ft.lock);
1110 
1111 	return fp;
1112 }
1113 
1114 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry)
1115 {
1116 	struct ksmbd_file	*lfp;
1117 	struct ksmbd_inode	*ci;
1118 	struct inode		*inode = d_inode(dentry);
1119 
1120 	read_lock(&inode_hash_lock);
1121 	ci = __ksmbd_inode_lookup(dentry);
1122 	read_unlock(&inode_hash_lock);
1123 	if (!ci)
1124 		return NULL;
1125 
1126 	down_read(&ci->m_lock);
1127 	list_for_each_entry(lfp, &ci->m_fp_list, node) {
1128 		if (inode == file_inode(lfp->filp)) {
1129 			lfp = ksmbd_fp_get(lfp);
1130 			up_read(&ci->m_lock);
1131 			ksmbd_inode_put(ci);
1132 			return lfp;
1133 		}
1134 	}
1135 	up_read(&ci->m_lock);
1136 	ksmbd_inode_put(ci);
1137 	return NULL;
1138 }
1139 
1140 bool ksmbd_has_open_files(struct dentry *dentry)
1141 {
1142 	struct ksmbd_file *fp;
1143 	unsigned int id;
1144 	bool ret = false;
1145 
1146 	read_lock(&global_ft.lock);
1147 	idr_for_each_entry(global_ft.idr, fp, id) {
1148 		struct dentry *fp_dentry = fp->filp->f_path.dentry;
1149 
1150 		if (fp->f_state != FP_INITED)
1151 			continue;
1152 		if (fp_dentry == dentry)
1153 			continue;
1154 		if (is_subdir(fp_dentry, dentry)) {
1155 			ret = true;
1156 			break;
1157 		}
1158 	}
1159 	read_unlock(&global_ft.lock);
1160 
1161 	return ret;
1162 }
1163 
1164 #define OPEN_ID_TYPE_VOLATILE_ID	(0)
1165 #define OPEN_ID_TYPE_PERSISTENT_ID	(1)
1166 
1167 static void __open_id_set(struct ksmbd_file *fp, u64 id, int type)
1168 {
1169 	if (type == OPEN_ID_TYPE_VOLATILE_ID)
1170 		fp->volatile_id = id;
1171 	if (type == OPEN_ID_TYPE_PERSISTENT_ID)
1172 		fp->persistent_id = id;
1173 }
1174 
1175 static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
1176 		     int type)
1177 {
1178 	u64			id = 0;
1179 	int			ret;
1180 
1181 	if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) {
1182 		__open_id_set(fp, KSMBD_NO_FID, type);
1183 		return -EMFILE;
1184 	}
1185 
1186 	idr_preload(KSMBD_DEFAULT_GFP);
1187 	write_lock(&ft->lock);
1188 	ret = idr_alloc_cyclic(ft->idr, fp, KSMBD_START_FID, INT_MAX - 1,
1189 			       GFP_NOWAIT);
1190 	if (ret >= 0) {
1191 		id = ret;
1192 		ret = 0;
1193 	} else {
1194 		id = KSMBD_NO_FID;
1195 		fd_limit_close();
1196 	}
1197 
1198 	__open_id_set(fp, id, type);
1199 	write_unlock(&ft->lock);
1200 	idr_preload_end();
1201 	return ret;
1202 }
1203 
1204 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp)
1205 {
1206 	__open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID);
1207 	return fp->persistent_id;
1208 }
1209 
1210 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp)
1211 {
1212 	struct ksmbd_file *fp;
1213 	int ret;
1214 
1215 	fp = kmem_cache_zalloc(filp_cache, KSMBD_DEFAULT_GFP);
1216 	if (!fp) {
1217 		pr_err("Failed to allocate memory\n");
1218 		return ERR_PTR(-ENOMEM);
1219 	}
1220 
1221 	INIT_LIST_HEAD(&fp->blocked_works);
1222 	INIT_LIST_HEAD(&fp->node);
1223 	INIT_LIST_HEAD(&fp->lock_list);
1224 	INIT_LIST_HEAD(&fp->notify_pendings);
1225 	spin_lock_init(&fp->f_lock);
1226 	mutex_init(&fp->readdir_lock);
1227 	atomic_set(&fp->refcount, 1);
1228 
1229 	fp->filp		= filp;
1230 	/*
1231 	 * fp owns a strong reference on fp->conn for as long as fp->conn is
1232 	 * non-NULL, so session_fd_check() and __ksmbd_close_fd() never
1233 	 * dereference a dangling pointer.  Paired with ksmbd_conn_put() in
1234 	 * session_fd_check() (durable preserve), in __ksmbd_close_fd()
1235 	 * (final close), and on the error paths below.
1236 	 */
1237 	fp->conn		= ksmbd_conn_get(work->conn);
1238 	fp->tcon		= work->tcon;
1239 	fp->volatile_id		= KSMBD_NO_FID;
1240 	fp->persistent_id	= KSMBD_NO_FID;
1241 	fp->f_state		= FP_NEW;
1242 	fp->f_ci		= ksmbd_inode_get(fp);
1243 
1244 	if (!fp->f_ci) {
1245 		ret = -ENOMEM;
1246 		goto err_out;
1247 	}
1248 
1249 	ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
1250 	if (ret) {
1251 		ksmbd_inode_put(fp->f_ci);
1252 		goto err_out;
1253 	}
1254 
1255 	atomic_inc(&work->conn->stats.open_files_count);
1256 	return fp;
1257 
1258 err_out:
1259 	/* fp->conn was set and refcounted before every branch here. */
1260 	ksmbd_conn_put(fp->conn);
1261 	kmem_cache_free(filp_cache, fp);
1262 	return ERR_PTR(ret);
1263 }
1264 
1265 /**
1266  * ksmbd_update_fstate() - update an fp state under the file-table lock
1267  * @ft: file table that publishes @fp's volatile id
1268  * @fp: file pointer to update
1269  * @state: new state
1270  *
1271  * Return: 0 on success.  The FP_NEW -> FP_INITED transition is special:
1272  * -ENOENT if teardown already unpublished @fp by advancing the state or
1273  * clearing the volatile id.  Other state updates preserve the historical
1274  * fire-and-forget behavior.
1275  */
1276 int ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
1277 			unsigned int state)
1278 {
1279 	int ret;
1280 
1281 	if (!fp)
1282 		return -ENOENT;
1283 
1284 	write_lock(&ft->lock);
1285 	if (state == FP_INITED &&
1286 	    (fp->f_state != FP_NEW || !has_file_id(fp->volatile_id))) {
1287 		ret = -ENOENT;
1288 	} else {
1289 		fp->f_state = state;
1290 		ret = 0;
1291 	}
1292 	write_unlock(&ft->lock);
1293 
1294 	return ret;
1295 }
1296 
1297 /*
1298  * ksmbd_mark_fp_closed() - mark fp closed under ft->lock and return how many
1299  * refs the teardown path owns.
1300  *
1301  * FP_INITED has a normal idr-owned reference, so teardown owns both that
1302  * reference and the transient lookup reference.  FP_NEW is still owned by the
1303  * in-flight opener/reopener, which will drop the original reference after
1304  * ksmbd_update_fstate(..., FP_INITED) observes the cleared volatile id.
1305  * FP_CLOSED on entry means an earlier ksmbd_close_fd() already consumed the
1306  * idr-owned ref.
1307  */
1308 static int ksmbd_mark_fp_closed(struct ksmbd_file *fp)
1309 {
1310 	if (fp->f_state == FP_INITED) {
1311 		set_close_state_blocked_works(fp);
1312 		fp->f_state = FP_CLOSED;
1313 		return 2;
1314 	}
1315 
1316 	return 1;
1317 }
1318 
1319 static int
1320 __close_file_table_ids(struct ksmbd_session *sess,
1321 		       struct ksmbd_tree_connect *tcon,
1322 		       bool (*skip)(struct ksmbd_tree_connect *tcon,
1323 				    struct ksmbd_file *fp,
1324 				    struct ksmbd_user *user),
1325 		       bool skip_preserves_fp)
1326 {
1327 	struct ksmbd_file_table *ft = &sess->file_table;
1328 	struct ksmbd_file *fp;
1329 	unsigned int id = 0;
1330 	int num = 0;
1331 
1332 	while (1) {
1333 		int n_to_drop;
1334 
1335 		write_lock(&ft->lock);
1336 		fp = idr_get_next(ft->idr, &id);
1337 		if (!fp) {
1338 			write_unlock(&ft->lock);
1339 			break;
1340 		}
1341 		if (!atomic_inc_not_zero(&fp->refcount)) {
1342 			id++;
1343 			write_unlock(&ft->lock);
1344 			continue;
1345 		}
1346 
1347 		if (skip_preserves_fp) {
1348 			/*
1349 			 * Session teardown: skip() is session_fd_check(),
1350 			 * which may sleep and mutates fp->conn / fp->tcon /
1351 			 * fp->volatile_id when it chooses to preserve fp
1352 			 * for durable reconnect.  Unpublish fp from the
1353 			 * session idr here, under ft->lock, so that
1354 			 * __ksmbd_lookup_fd() through this session cannot
1355 			 * grant a new ksmbd_fp_get() reference to an fp
1356 			 * whose fields are about to be rewritten outside
1357 			 * the lock.  Durable reconnect still reaches fp via
1358 			 * global_ft.
1359 			 */
1360 			idr_remove(ft->idr, id);
1361 			fp->durable_volatile_id = fp->volatile_id;
1362 			fp->volatile_id = KSMBD_NO_FID;
1363 			write_unlock(&ft->lock);
1364 
1365 			if (skip(tcon, fp, sess->user)) {
1366 				/*
1367 				 * session_fd_check() has converted fp to
1368 				 * durable-preserve state and cleared its
1369 				 * per-conn fields.  fp is already unpublished
1370 				 * above; the original idr-owned ref keeps it
1371 				 * alive for the durable scavenger.  Drop only
1372 				 * the transient ref.  atomic_dec() is safe --
1373 				 * atomic_inc_not_zero() succeeded on a
1374 				 * positive value and we added one more, so
1375 				 * refcount cannot be zero here.
1376 				 */
1377 				atomic_dec(&fp->refcount);
1378 				id++;
1379 				continue;
1380 			}
1381 
1382 			/*
1383 			 * Keep the close-state decision under the same lock
1384 			 * observed by ksmbd_update_fstate(), which is how an
1385 			 * in-flight FP_NEW opener learns that teardown has
1386 			 * cleared its volatile id.
1387 			 */
1388 			write_lock(&ft->lock);
1389 			n_to_drop = ksmbd_mark_fp_closed(fp);
1390 			write_unlock(&ft->lock);
1391 		} else {
1392 			/*
1393 			 * Tree teardown: skip() is tree_conn_fd_check(), a
1394 			 * cheap pointer compare that doesn't sleep and has
1395 			 * no side effects, so keep the skip decision plus
1396 			 * the unpublish-and-mark-closed sequence atomic
1397 			 * under ft->lock.  fps belonging to other tree
1398 			 * connects (skip() == true) stay fully published in
1399 			 * the session idr with no lock window.
1400 			 */
1401 			if (skip(tcon, fp, sess->user)) {
1402 				atomic_dec(&fp->refcount);
1403 				write_unlock(&ft->lock);
1404 				id++;
1405 				continue;
1406 			}
1407 			idr_remove(ft->idr, id);
1408 			fp->volatile_id = KSMBD_NO_FID;
1409 			n_to_drop = ksmbd_mark_fp_closed(fp);
1410 			write_unlock(&ft->lock);
1411 		}
1412 
1413 		/*
1414 		 * fp->volatile_id is already cleared to prevent stale idr
1415 		 * removal from a deferred final close.  Remove fp from
1416 		 * m_fp_list here because __ksmbd_remove_fd() will skip the
1417 		 * list unlink when volatile_id is KSMBD_NO_FID.
1418 		 */
1419 		down_write(&fp->f_ci->m_lock);
1420 		list_del_init(&fp->node);
1421 		up_write(&fp->f_ci->m_lock);
1422 
1423 		/*
1424 		 * Drop the references this iteration owns:
1425 		 *
1426 		 *   n_to_drop == 2: we observed FP_INITED and committed
1427 		 *     the FP_CLOSED transition ourselves, so we own the
1428 		 *     transient (+1) and the still-intact idr-owned ref.
1429 		 *
1430 		 *   n_to_drop == 1: either a prior ksmbd_close_fd()
1431 		 *     already consumed the idr-owned ref, or fp was still
1432 		 *     FP_NEW and the in-flight opener/reopener must keep
1433 		 *     the original reference until ksmbd_update_fstate()
1434 		 *     observes the cleared volatile id.
1435 		 *
1436 		 * If we end up as the final putter, finalize fp and
1437 		 * account the open_files_count decrement via the caller's
1438 		 * atomic_sub(num, ...).  Otherwise the remaining user's
1439 		 * ksmbd_fd_put() reaches __put_fd_final(), which does its
1440 		 * own atomic_dec(&open_files_count), so we must not count
1441 		 * this fp here -- doing so would double-decrement the
1442 		 * connection-wide counter.
1443 		 */
1444 		if (atomic_sub_and_test(n_to_drop, &fp->refcount)) {
1445 			__ksmbd_close_fd(NULL, fp);
1446 			num++;
1447 		}
1448 		id++;
1449 	}
1450 
1451 	return num;
1452 }
1453 
1454 static inline bool is_reconnectable(struct ksmbd_file *fp)
1455 {
1456 	struct oplock_info *opinfo = opinfo_get(fp);
1457 	bool reconn = false;
1458 
1459 	if (!opinfo)
1460 		return false;
1461 
1462 	if (opinfo->op_state != OPLOCK_STATE_NONE) {
1463 		opinfo_put(opinfo);
1464 		return false;
1465 	}
1466 
1467 	if (fp->is_resilient || fp->is_persistent)
1468 		reconn = true;
1469 	else if (fp->is_durable && opinfo->is_lease &&
1470 		 opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
1471 		reconn = true;
1472 
1473 	else if (fp->is_durable && opinfo->level == SMB2_OPLOCK_LEVEL_BATCH)
1474 		reconn = true;
1475 
1476 	opinfo_put(opinfo);
1477 	return reconn;
1478 }
1479 
1480 static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon,
1481 			       struct ksmbd_file *fp,
1482 			       struct ksmbd_user *user)
1483 {
1484 	return fp->tcon != tcon;
1485 }
1486 
1487 static bool ksmbd_durable_scavenger_alive(void)
1488 {
1489 	if (!durable_scavenger_running)
1490 		return false;
1491 
1492 	if (kthread_should_stop())
1493 		return false;
1494 
1495 	if (idr_is_empty(global_ft.idr))
1496 		return false;
1497 
1498 	return true;
1499 }
1500 
1501 static void ksmbd_scavenger_dispose_dh(struct ksmbd_file *fp)
1502 {
1503 	/*
1504 	 * Durable-preserved fp can remain linked on f_ci->m_fp_list for
1505 	 * share-mode checks.  Unlink it before final close; fp->node is not
1506 	 * available as a scavenger-private list node because re-adding it to
1507 	 * another list corrupts m_fp_list.
1508 	 */
1509 	down_write(&fp->f_ci->m_lock);
1510 	list_del_init(&fp->node);
1511 	up_write(&fp->f_ci->m_lock);
1512 
1513 	/*
1514 	 * Drop both the durable lifetime reference and the transient reference
1515 	 * taken by the scavenger under global_ft.lock.  If a concurrent
1516 	 * ksmbd_lookup_fd_inode() (or any other m_fp_list walker) snatched fp
1517 	 * before the unlink above, that holder owns the final close via
1518 	 * ksmbd_fd_put() -> __ksmbd_close_fd().  Otherwise the scavenger is
1519 	 * the last putter and finalises fp here.
1520 	 */
1521 	if (atomic_sub_and_test(2, &fp->refcount))
1522 		__ksmbd_close_fd(NULL, fp);
1523 }
1524 
1525 static int ksmbd_durable_scavenger(void *dummy)
1526 {
1527 	struct ksmbd_file *fp = NULL;
1528 	struct ksmbd_file *expired_fp;
1529 	unsigned int id;
1530 	unsigned int min_timeout = 1;
1531 	bool found_fp_timeout;
1532 	unsigned long remaining_jiffies;
1533 
1534 	__module_get(THIS_MODULE);
1535 
1536 	set_freezable();
1537 	while (ksmbd_durable_scavenger_alive()) {
1538 		if (try_to_freeze())
1539 			continue;
1540 
1541 		remaining_jiffies = wait_event_interruptible_timeout(dh_wq,
1542 				   ksmbd_durable_scavenger_alive() == false,
1543 				   __msecs_to_jiffies(min_timeout));
1544 		if ((long)remaining_jiffies > 0)
1545 			min_timeout = jiffies_to_msecs(remaining_jiffies);
1546 		else
1547 			min_timeout = DURABLE_HANDLE_MAX_TIMEOUT;
1548 
1549 		do {
1550 			expired_fp = NULL;
1551 			found_fp_timeout = false;
1552 
1553 			write_lock(&global_ft.lock);
1554 			idr_for_each_entry(global_ft.idr, fp, id) {
1555 				unsigned long durable_timeout;
1556 
1557 				if (!fp->durable_timeout)
1558 					continue;
1559 
1560 				if (atomic_read(&fp->refcount) > 1 ||
1561 				    fp->conn)
1562 					continue;
1563 
1564 				found_fp_timeout = true;
1565 				if (fp->durable_scavenger_timeout <=
1566 				    jiffies_to_msecs(jiffies)) {
1567 					__ksmbd_remove_durable_fd(fp);
1568 					/*
1569 					 * Take a transient reference so fp
1570 					 * cannot be freed by an in-flight
1571 					 * ksmbd_lookup_fd_inode() that found
1572 					 * it through f_ci->m_fp_list while we
1573 					 * drop global_ft.lock and reach the
1574 					 * m_fp_list unlink in
1575 					 * ksmbd_scavenger_dispose_dh().
1576 					 */
1577 					atomic_inc(&fp->refcount);
1578 					expired_fp = fp;
1579 					break;
1580 				}
1581 
1582 				durable_timeout =
1583 					fp->durable_scavenger_timeout -
1584 						jiffies_to_msecs(jiffies);
1585 
1586 				if (min_timeout > durable_timeout)
1587 					min_timeout = durable_timeout;
1588 			}
1589 			write_unlock(&global_ft.lock);
1590 
1591 			if (expired_fp)
1592 				ksmbd_scavenger_dispose_dh(expired_fp);
1593 		} while (expired_fp);
1594 
1595 		if (found_fp_timeout == false)
1596 			break;
1597 	}
1598 
1599 	durable_scavenger_running = false;
1600 
1601 	module_put(THIS_MODULE);
1602 
1603 	return 0;
1604 }
1605 
1606 void ksmbd_launch_ksmbd_durable_scavenger(void)
1607 {
1608 	if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE))
1609 		return;
1610 
1611 	mutex_lock(&durable_scavenger_lock);
1612 	if (durable_scavenger_running == true) {
1613 		mutex_unlock(&durable_scavenger_lock);
1614 		return;
1615 	}
1616 
1617 	durable_scavenger_running = true;
1618 
1619 	server_conf.dh_task = kthread_run(ksmbd_durable_scavenger,
1620 				     (void *)NULL, "ksmbd-durable-scavenger");
1621 	if (IS_ERR(server_conf.dh_task))
1622 		pr_err("cannot start conn thread, err : %ld\n",
1623 		       PTR_ERR(server_conf.dh_task));
1624 	mutex_unlock(&durable_scavenger_lock);
1625 }
1626 
1627 void ksmbd_stop_durable_scavenger(void)
1628 {
1629 	if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE))
1630 		return;
1631 
1632 	mutex_lock(&durable_scavenger_lock);
1633 	if (!durable_scavenger_running) {
1634 		mutex_unlock(&durable_scavenger_lock);
1635 		return;
1636 	}
1637 
1638 	durable_scavenger_running = false;
1639 	if (waitqueue_active(&dh_wq))
1640 		wake_up(&dh_wq);
1641 	mutex_unlock(&durable_scavenger_lock);
1642 	kthread_stop(server_conf.dh_task);
1643 }
1644 
1645 /*
1646  * ksmbd_vfs_set_durable_owner - Store owner info for durable replay/reconnect
1647  * @fp: ksmbd file pointer to store owner info
1648  * @user: user pointer to copy from
1649  *
1650  * This function binds the current user's identity to the file handle
1651  * to satisfy MS-SMB2 Step 8 (SecurityContext matching) during reconnect.
1652  *
1653  * Return: 0 on success, or negative error code on failure
1654  */
1655 int ksmbd_vfs_set_durable_owner(struct ksmbd_file *fp,
1656 				struct ksmbd_user *user)
1657 {
1658 	char *name;
1659 
1660 	if (!user)
1661 		return -EINVAL;
1662 
1663 	/* Duplicate the user name to ensure identity persistence */
1664 	name = kstrdup(user->name, GFP_KERNEL);
1665 	if (!name)
1666 		return -ENOMEM;
1667 
1668 	spin_lock(&fp->f_lock);
1669 	fp->owner.uid = user->uid;
1670 	fp->owner.gid = user->gid;
1671 	fp->owner.name = name;
1672 	spin_unlock(&fp->f_lock);
1673 
1674 	return 0;
1675 }
1676 
1677 /**
1678  * ksmbd_vfs_compare_durable_owner - Verify if the requester is original owner
1679  * @fp: existing ksmbd file pointer
1680  * @user: user pointer of the reconnect requester
1681  *
1682  * Compares the UID, GID, and name of the current requester against the
1683  * original owner stored in the file handle.
1684  *
1685  * Return: true if the user matches, false otherwise
1686  */
1687 bool ksmbd_vfs_compare_durable_owner(struct ksmbd_file *fp,
1688 		struct ksmbd_user *user)
1689 {
1690 	bool ret = false;
1691 
1692 	if (!user)
1693 		return false;
1694 
1695 	spin_lock(&fp->f_lock);
1696 	if (!fp->owner.name)
1697 		goto out;
1698 
1699 	/* Check if the UID and GID match first (fast path) */
1700 	if (fp->owner.uid != user->uid || fp->owner.gid != user->gid)
1701 		goto out;
1702 
1703 	/* Validate the account name to ensure the same SecurityContext */
1704 	ret = (strcmp(fp->owner.name, user->name) == 0);
1705 out:
1706 	spin_unlock(&fp->f_lock);
1707 	return ret;
1708 }
1709 
1710 static bool session_fd_check(struct ksmbd_tree_connect *tcon,
1711 			     struct ksmbd_file *fp, struct ksmbd_user *user)
1712 {
1713 	struct ksmbd_inode *ci;
1714 	struct oplock_info *op;
1715 	struct ksmbd_conn *conn;
1716 	struct ksmbd_lock *smb_lock, *tmp_lock;
1717 
1718 	if (!is_reconnectable(fp))
1719 		return false;
1720 
1721 	if (fp->f_state != FP_INITED)
1722 		return false;
1723 
1724 	if (WARN_ON_ONCE(!fp->conn))
1725 		return false;
1726 
1727 	if (ksmbd_vfs_set_durable_owner(fp, user))
1728 		return false;
1729 
1730 	/*
1731 	 * fp owns a strong reference on fp->conn (taken in ksmbd_open_fd()
1732 	 * / ksmbd_reopen_durable_fd()), so conn stays valid for the whole
1733 	 * body of this function regardless of any op->conn puts below.
1734 	 */
1735 	conn = fp->conn;
1736 	ci = fp->f_ci;
1737 	down_write(&ci->m_lock);
1738 	list_for_each_entry_rcu(op, &ci->m_op_list, op_entry,
1739 				lockdep_is_held(&ci->m_lock)) {
1740 		if (op->conn != conn)
1741 			continue;
1742 		ksmbd_conn_put(op->conn);
1743 		op->conn = NULL;
1744 		op->sess = NULL;
1745 	}
1746 	up_write(&ci->m_lock);
1747 
1748 	list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
1749 		struct ksmbd_conn *lock_conn = smb_lock->conn;
1750 
1751 		if (!lock_conn)
1752 			continue;
1753 		spin_lock(&lock_conn->llist_lock);
1754 		list_del_init(&smb_lock->clist);
1755 		smb_lock->conn = NULL;
1756 		spin_unlock(&lock_conn->llist_lock);
1757 		ksmbd_conn_put(lock_conn);
1758 	}
1759 
1760 	fp->conn = NULL;
1761 	fp->tcon = NULL;
1762 	fp->volatile_id = KSMBD_NO_FID;
1763 
1764 	if (fp->durable_timeout)
1765 		fp->durable_scavenger_timeout =
1766 			jiffies_to_msecs(jiffies) + fp->durable_timeout;
1767 
1768 	/* Drop fp's own reference on conn. */
1769 	ksmbd_conn_put(conn);
1770 	return true;
1771 }
1772 
1773 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work)
1774 {
1775 	int num = __close_file_table_ids(work->sess,
1776 					 work->tcon,
1777 					 tree_conn_fd_check,
1778 					 false);
1779 
1780 	atomic_sub(num, &work->conn->stats.open_files_count);
1781 }
1782 
1783 void ksmbd_close_session_fds(struct ksmbd_work *work)
1784 {
1785 	int num = __close_file_table_ids(work->sess,
1786 					 work->tcon,
1787 					 session_fd_check,
1788 					 true);
1789 
1790 	atomic_sub(num, &work->conn->stats.open_files_count);
1791 }
1792 
1793 int ksmbd_init_global_file_table(void)
1794 {
1795 	create_proc_files();
1796 	return ksmbd_init_file_table(&global_ft);
1797 }
1798 
1799 void ksmbd_free_global_file_table(void)
1800 {
1801 	struct ksmbd_file	*fp = NULL;
1802 	unsigned int		id;
1803 
1804 	idr_for_each_entry(global_ft.idr, fp, id) {
1805 		ksmbd_remove_durable_fd(fp);
1806 		__ksmbd_close_fd(NULL, fp);
1807 	}
1808 
1809 	idr_destroy(global_ft.idr);
1810 	kfree(global_ft.idr);
1811 }
1812 
1813 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
1814 				  struct ksmbd_file *fp, char *name)
1815 {
1816 	char *pathname, *ab_pathname;
1817 	int ret = 0;
1818 
1819 	pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
1820 	if (!pathname)
1821 		return -EACCES;
1822 
1823 	ab_pathname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
1824 	if (IS_ERR(ab_pathname)) {
1825 		kfree(pathname);
1826 		return -EACCES;
1827 	}
1828 
1829 	if (name && strcmp(&ab_pathname[share->path_sz + 1], name)) {
1830 		ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
1831 		ret = -EINVAL;
1832 	}
1833 
1834 	kfree(pathname);
1835 
1836 	return ret;
1837 }
1838 
1839 int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp)
1840 {
1841 	struct ksmbd_inode *ci;
1842 	struct oplock_info *op;
1843 	struct ksmbd_conn *conn = work->conn;
1844 	struct ksmbd_lock *smb_lock;
1845 	unsigned int old_f_state;
1846 
1847 	write_lock(&global_ft.lock);
1848 	if ((!fp->is_durable && !fp->is_persistent) || fp->conn || fp->tcon) {
1849 		write_unlock(&global_ft.lock);
1850 		pr_err("Invalid durable fd [%p:%p]\n", fp->conn, fp->tcon);
1851 		return -EBADF;
1852 	}
1853 
1854 	if (has_file_id(fp->volatile_id)) {
1855 		write_unlock(&global_ft.lock);
1856 		pr_err("Still in use durable fd: %llu\n", fp->volatile_id);
1857 		return -EBADF;
1858 	}
1859 
1860 	/*
1861 	 * Initialize fp's connection binding before publishing fp into the
1862 	 * session's file table.  If __open_id() is ordered first, a
1863 	 * concurrent teardown that iterates the table can observe a valid
1864 	 * volatile_id with fp->conn == NULL and preserve a
1865 	 * partially-initialized fp.  fp owns a strong reference on the new
1866 	 * conn (see ksmbd_open_fd()); undo it on __open_id() failure.
1867 	 */
1868 	fp->conn = ksmbd_conn_get(conn);
1869 	fp->tcon = work->tcon;
1870 	write_unlock(&global_ft.lock);
1871 
1872 	old_f_state = fp->f_state;
1873 	fp->f_state = FP_NEW;
1874 
1875 	__open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
1876 	if (!has_file_id(fp->volatile_id)) {
1877 		write_lock(&global_ft.lock);
1878 		fp->conn = NULL;
1879 		fp->tcon = NULL;
1880 		write_unlock(&global_ft.lock);
1881 		ksmbd_conn_put(conn);
1882 		fp->f_state = old_f_state;
1883 		return -EBADF;
1884 	}
1885 
1886 	list_for_each_entry(smb_lock, &fp->lock_list, flist) {
1887 		smb_lock->conn = ksmbd_conn_get(conn);
1888 		spin_lock(&conn->llist_lock);
1889 		list_add_tail(&smb_lock->clist, &conn->lock_list);
1890 		spin_unlock(&conn->llist_lock);
1891 	}
1892 
1893 	ci = fp->f_ci;
1894 	down_write(&ci->m_lock);
1895 	list_for_each_entry_rcu(op, &ci->m_op_list, op_entry,
1896 				lockdep_is_held(&ci->m_lock)) {
1897 		if (op->conn || op->o_fp != fp)
1898 			continue;
1899 		op->conn = ksmbd_conn_get(fp->conn);
1900 		op->sess = work->sess;
1901 	}
1902 	up_write(&ci->m_lock);
1903 
1904 	spin_lock(&fp->f_lock);
1905 	fp->owner.uid = fp->owner.gid = 0;
1906 	kfree(fp->owner.name);
1907 	fp->owner.name = NULL;
1908 	spin_unlock(&fp->f_lock);
1909 
1910 	return 0;
1911 }
1912 
1913 int ksmbd_init_file_table(struct ksmbd_file_table *ft)
1914 {
1915 	ft->idr = kzalloc_obj(struct idr, KSMBD_DEFAULT_GFP);
1916 	if (!ft->idr)
1917 		return -ENOMEM;
1918 
1919 	idr_init(ft->idr);
1920 	rwlock_init(&ft->lock);
1921 	return 0;
1922 }
1923 
1924 void ksmbd_destroy_file_table(struct ksmbd_session *sess)
1925 {
1926 	struct ksmbd_file_table *ft = &sess->file_table;
1927 
1928 	if (!ft->idr)
1929 		return;
1930 
1931 	__close_file_table_ids(sess, NULL, session_fd_check, true);
1932 	idr_destroy(ft->idr);
1933 	kfree(ft->idr);
1934 	ft->idr = NULL;
1935 }
1936 
1937 int ksmbd_init_file_cache(void)
1938 {
1939 	filp_cache = kmem_cache_create("ksmbd_file_cache",
1940 				       sizeof(struct ksmbd_file), 0,
1941 				       SLAB_HWCACHE_ALIGN, NULL);
1942 	if (!filp_cache)
1943 		goto out;
1944 
1945 	init_waitqueue_head(&dh_wq);
1946 
1947 	return 0;
1948 
1949 out:
1950 	pr_err("failed to allocate file cache\n");
1951 	return -ENOMEM;
1952 }
1953 
1954 void ksmbd_exit_file_cache(void)
1955 {
1956 	kmem_cache_destroy(filp_cache);
1957 }
1958