xref: /linux/fs/smb/server/vfs_cache.c (revision 50a400cff59f534254ace2828f2eb9d844517fbb)
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 	/*
634 	 * The zero fp reference count serializes access to fp->lock_list, but
635 	 * the VFS may still have blocked requests chained below these locks.
636 	 */
637 	list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
638 		struct ksmbd_conn *conn = smb_lock->conn;
639 
640 		if (conn) {
641 			spin_lock(&conn->llist_lock);
642 			list_del_init(&smb_lock->clist);
643 			smb_lock->conn = NULL;
644 			spin_unlock(&conn->llist_lock);
645 			ksmbd_conn_put(conn);
646 		}
647 
648 		list_del_init(&smb_lock->flist);
649 		ksmbd_vfs_posix_lock_unblock(smb_lock->fl);
650 		locks_free_lock(smb_lock->fl);
651 		kfree(smb_lock);
652 	}
653 
654 	/*
655 	 * Complete any CHANGE_NOTIFY left pending on this handle now that
656 	 * it is closed. KSMBD never completes CHANGE_NOTIFY spontaneously
657 	 * (no real change-notification backend), only on close -- matching
658 	 * genuine SMB2/macOS smbfs semantics and avoiding the Finder
659 	 * "directory changed, re-enumerate everything" loop.
660 	 *
661 	 * smb2_notify() on another connection can be adding to
662 	 * notify_pendings under fp->f_lock at the same time this handle is
663 	 * closed, and a client-sent CANCEL can concurrently be racing to
664 	 * claim the same entry via smb2_notify_cancel_fn() (smb2pdu.c).
665 	 * Pop one entry at a time under the lock via list_del_init() rather
666 	 * than a bulk list_splice_init(): list_del_init() leaves the node
667 	 * self-linked ("empty"), which is what the cancel path checks under
668 	 * the same lock to tell whether it lost the race -- a bulk splice
669 	 * would instead relink every entry into a shared local list, so an
670 	 * entry claimed here would still read as "not empty" to a racing
671 	 * cancel_fn, and both sides could end up freeing the same work.
672 	 * ksmbd_conn_write() can sleep (it takes conn's write mutex), so it
673 	 * must not be called while fp->f_lock is held -- release the lock
674 	 * before processing each popped entry, then reacquire it for the
675 	 * next.
676 	 */
677 	for (;;) {
678 		spin_lock(&fp->f_lock);
679 		if (list_empty(&fp->notify_pendings)) {
680 			spin_unlock(&fp->f_lock);
681 			break;
682 		}
683 		cn_work = list_first_entry(&fp->notify_pendings,
684 					   struct ksmbd_work, notify_entry);
685 		list_del_init(&cn_work->notify_entry);
686 		spin_unlock(&fp->f_lock);
687 
688 		ksmbd_conn_write(cn_work);
689 		/*
690 		 * release_async_work() removes cn_work from
691 		 * conn->async_requests, frees cancel_argv, and releases+zeroes
692 		 * async_id -- all needed before ksmbd_free_work_struct(), which
693 		 * only releases async_id itself if still nonzero (i.e. if this
694 		 * hadn't already been done).
695 		 */
696 		release_async_work(cn_work);
697 		ksmbd_free_work_struct(cn_work);
698 	}
699 
700 	/*
701 	 * Drop fp's strong reference on conn (taken in ksmbd_open_fd() /
702 	 * ksmbd_reopen_durable_fd()).  Durable fps that reached the
703 	 * scavenger have already had fp->conn cleared by session_fd_check(),
704 	 * in which case there is nothing to drop here.
705 	 */
706 	if (fp->conn) {
707 		ksmbd_conn_put(fp->conn);
708 		fp->conn = NULL;
709 	}
710 
711 	if (ksmbd_stream_fd(fp))
712 		kfree(fp->stream.name);
713 	kfree(fp->owner.name);
714 
715 	kmem_cache_free(filp_cache, fp);
716 }
717 
718 /**
719  * ksmbd_close_disconnected_durable_delete_on_close() - drop a delete-on-close
720  *	file kept present only by disconnected durable handles
721  * @dentry:	dentry of the file being opened
722  *
723  * A durable handle opened with delete-on-close is preserved across a
724  * disconnect so it can be reclaimed by a durable reconnect.  When a new
725  * (non-reconnect) open arrives for the same name instead, the disconnected
726  * handle has to give way.  Close such handles so their delete-on-close is
727  * applied and the file is removed once the last handle is gone, letting the
728  * new open create a fresh file.
729  *
730  * The caller's inode reference is dropped before closing so that the final
731  * close can promote S_DEL_ON_CLS to S_DEL_PENDING and unlink the file.
732  *
733  * Return:	true if a disconnected durable handle was closed.
734  */
735 bool ksmbd_close_disconnected_durable_delete_on_close(struct dentry *dentry)
736 {
737 	struct ksmbd_inode *ci;
738 	struct ksmbd_file *fp, *tmp;
739 	LIST_HEAD(dispose);
740 	bool closed = false;
741 
742 	ci = ksmbd_inode_lookup_lock(dentry);
743 	if (!ci)
744 		return false;
745 
746 	down_write(&ci->m_lock);
747 	if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_ON_CLS_STREAM | S_DEL_PENDING)) {
748 		list_for_each_entry_safe(fp, tmp, &ci->m_fp_list, node) {
749 			if (fp->conn || !fp->is_durable ||
750 			    fp->f_state != FP_INITED)
751 				continue;
752 
753 			/*
754 			 * Claim the close before unlinking fp from m_fp_list.
755 			 * refcount == 1 means only the durable lifetime ref is
756 			 * left. Add a transient ref so final close can drop both.
757 			 */
758 			write_lock(&global_ft.lock);
759 			if (atomic_read(&fp->refcount) == 1) {
760 				atomic_inc(&fp->refcount);
761 				__ksmbd_remove_durable_fd(fp);
762 				ksmbd_mark_fp_closed(fp);
763 				list_move_tail(&fp->node, &dispose);
764 			}
765 			write_unlock(&global_ft.lock);
766 		}
767 	}
768 	up_write(&ci->m_lock);
769 
770 	/*
771 	 * Drop our lookup reference before closing so the last __ksmbd_close_fd()
772 	 * can drop m_count to zero and unlink the delete-on-close file.  The
773 	 * collected handles still hold the transient reference taken above, so
774 	 * ci stays valid until they are closed below.
775 	 */
776 	ksmbd_inode_put(ci);
777 
778 	while (!list_empty(&dispose)) {
779 		fp = list_first_entry(&dispose, struct ksmbd_file, node);
780 		list_del_init(&fp->node);
781 		if (atomic_sub_and_test(2, &fp->refcount)) {
782 			__ksmbd_close_fd(NULL, fp);
783 			closed = true;
784 		}
785 	}
786 
787 	return closed;
788 }
789 
790 static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp)
791 {
792 	if (fp->f_state != FP_INITED)
793 		return NULL;
794 
795 	if (!atomic_inc_not_zero(&fp->refcount))
796 		return NULL;
797 	return fp;
798 }
799 
800 struct ksmbd_file *ksmbd_file_get(struct ksmbd_file *fp)
801 {
802 	return ksmbd_fp_get(fp);
803 }
804 
805 static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft,
806 					    u64 id)
807 {
808 	struct ksmbd_file *fp;
809 
810 	if (!has_file_id(id))
811 		return NULL;
812 
813 	read_lock(&ft->lock);
814 	fp = idr_find(ft->idr, id);
815 	if (fp)
816 		fp = ksmbd_fp_get(fp);
817 	read_unlock(&ft->lock);
818 	return fp;
819 }
820 
821 static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp)
822 {
823 	/*
824 	 * Detached durable fp -- session_fd_check() cleared fp->conn at
825 	 * preserve, so this fp is no longer tracked by any conn's
826 	 * stats.open_files_count.  This happens when
827 	 * ksmbd_scavenger_dispose_dh() hands the final close off to an
828 	 * m_fp_list walker (e.g. ksmbd_lookup_fd_inode()) whose work->conn
829 	 * is unrelated to the conn that originally opened the handle; close
830 	 * via the NULL-ft path so we do not underflow that unrelated
831 	 * counter.
832 	 */
833 	if (!fp->conn) {
834 		__ksmbd_close_fd(NULL, fp);
835 		return;
836 	}
837 	__ksmbd_close_fd(&work->sess->file_table, fp);
838 	atomic_dec(&work->conn->stats.open_files_count);
839 }
840 
841 static void set_close_state_blocked_works(struct ksmbd_file *fp)
842 {
843 	struct ksmbd_work *cancel_work;
844 
845 	spin_lock(&fp->f_lock);
846 	list_for_each_entry(cancel_work, &fp->blocked_works,
847 				 fp_entry) {
848 		cancel_work->state = KSMBD_WORK_CLOSED;
849 		cancel_work->cancel_fn(cancel_work->cancel_argv);
850 	}
851 	spin_unlock(&fp->f_lock);
852 }
853 
854 int ksmbd_close_fd(struct ksmbd_work *work, u64 id)
855 {
856 	struct ksmbd_file	*fp;
857 	struct ksmbd_file_table	*ft;
858 	bool closed = false;
859 
860 	if (!has_file_id(id))
861 		return 0;
862 
863 	ft = &work->sess->file_table;
864 	write_lock(&ft->lock);
865 	fp = idr_find(ft->idr, id);
866 	if (fp) {
867 		set_close_state_blocked_works(fp);
868 
869 		if (fp->f_state != FP_INITED)
870 			fp = NULL;
871 		else {
872 			fp->f_state = FP_CLOSED;
873 			idr_remove(ft->idr, id);
874 			fp->volatile_id = KSMBD_NO_FID;
875 			closed = true;
876 			if (!atomic_dec_and_test(&fp->refcount))
877 				fp = NULL;
878 		}
879 	}
880 	write_unlock(&ft->lock);
881 
882 	if (!fp)
883 		return closed ? 0 : -EINVAL;
884 
885 	__put_fd_final(work, fp);
886 	return 0;
887 }
888 
889 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp)
890 {
891 	if (!fp)
892 		return;
893 
894 	if (!atomic_dec_and_test(&fp->refcount))
895 		return;
896 	__put_fd_final(work, fp);
897 }
898 
899 static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp)
900 {
901 	if (!fp)
902 		return false;
903 	if (fp->tcon != tcon)
904 		return false;
905 	return true;
906 }
907 
908 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id)
909 {
910 	return __ksmbd_lookup_fd(&work->sess->file_table, id);
911 }
912 
913 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id)
914 {
915 	struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
916 
917 	if (__sanity_check(work->tcon, fp))
918 		return fp;
919 
920 	ksmbd_fd_put(work, fp);
921 	return NULL;
922 }
923 
924 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id,
925 					u64 pid)
926 {
927 	struct ksmbd_file *fp;
928 
929 	if (!has_file_id(id)) {
930 		id = work->compound_fid;
931 		pid = work->compound_pfid;
932 	}
933 
934 	fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
935 	if (!__sanity_check(work->tcon, fp)) {
936 		ksmbd_fd_put(work, fp);
937 		return NULL;
938 	}
939 	if (fp->persistent_id != pid) {
940 		ksmbd_fd_put(work, fp);
941 		return NULL;
942 	}
943 	return fp;
944 }
945 
946 struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id)
947 {
948 	return __ksmbd_lookup_fd(&global_ft, id);
949 }
950 
951 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id)
952 {
953 	struct ksmbd_file *fp;
954 
955 	fp = __ksmbd_lookup_fd(&global_ft, id);
956 	if (fp && (fp->durable_reconnect_disabled ||
957 		   fp->conn ||
958 		   (fp->durable_scavenger_timeout &&
959 		    (fp->durable_scavenger_timeout <
960 		     jiffies_to_msecs(jiffies))))) {
961 		ksmbd_put_durable_fd(fp);
962 		fp = NULL;
963 	}
964 
965 	return fp;
966 }
967 
968 void ksmbd_put_durable_fd(struct ksmbd_file *fp)
969 {
970 	if (!atomic_dec_and_test(&fp->refcount))
971 		return;
972 
973 	__ksmbd_close_fd(NULL, fp);
974 }
975 
976 bool ksmbd_has_other_active_fd(struct ksmbd_file *fp)
977 {
978 	struct ksmbd_file *lfp;
979 	struct ksmbd_inode *ci = fp->f_ci;
980 	bool ret = false;
981 
982 	down_read(&ci->m_lock);
983 	list_for_each_entry(lfp, &ci->m_fp_list, node) {
984 		if (lfp == fp)
985 			continue;
986 
987 		if (lfp->f_state == FP_INITED &&
988 		    (READ_ONCE(lfp->conn) || READ_ONCE(lfp->tcon))) {
989 			ret = true;
990 			break;
991 		}
992 	}
993 	up_read(&ci->m_lock);
994 
995 	return ret;
996 }
997 
998 struct ksmbd_file *ksmbd_lookup_fd_app_instance_id(char *app_instance_id)
999 {
1000 	struct ksmbd_file *fp = NULL;
1001 	unsigned int id;
1002 
1003 	read_lock(&global_ft.lock);
1004 	idr_for_each_entry(global_ft.idr, fp, id) {
1005 		if (!fp->has_app_instance_id)
1006 			continue;
1007 		if (!memcmp(fp->app_instance_id, app_instance_id,
1008 			    SMB2_CREATE_GUID_SIZE)) {
1009 			fp = ksmbd_fp_get(fp);
1010 			break;
1011 		}
1012 	}
1013 	read_unlock(&global_ft.lock);
1014 
1015 	return fp;
1016 }
1017 
1018 int ksmbd_close_fd_app_instance_id(char *app_instance_id)
1019 {
1020 	struct ksmbd_file_table *ft;
1021 	struct ksmbd_file *fp;
1022 	struct oplock_info *opinfo;
1023 	int n_to_drop = 0;
1024 
1025 	fp = ksmbd_lookup_fd_app_instance_id(app_instance_id);
1026 	if (!fp)
1027 		return 0;
1028 
1029 	opinfo = opinfo_get(fp);
1030 	if (!opinfo)
1031 		goto out;
1032 
1033 	down_read(&fp->f_ci->m_lock);
1034 	if (!opinfo->conn) {
1035 		up_read(&fp->f_ci->m_lock);
1036 		goto out;
1037 	}
1038 
1039 	ft = &opinfo->sess->file_table;
1040 	write_lock(&ft->lock);
1041 	if (fp->f_state == FP_INITED && has_file_id(fp->volatile_id)) {
1042 		idr_remove(ft->idr, fp->volatile_id);
1043 		fp->volatile_id = KSMBD_NO_FID;
1044 		n_to_drop = ksmbd_mark_fp_closed(fp);
1045 	}
1046 	write_unlock(&ft->lock);
1047 	up_read(&fp->f_ci->m_lock);
1048 	opinfo_put(opinfo);
1049 	opinfo = NULL;
1050 
1051 	if (!n_to_drop)
1052 		goto out;
1053 
1054 	down_write(&fp->f_ci->m_lock);
1055 	list_del_init(&fp->node);
1056 	up_write(&fp->f_ci->m_lock);
1057 
1058 	if (atomic_sub_and_test(n_to_drop, &fp->refcount)) {
1059 		if (fp->conn)
1060 			atomic_dec(&fp->conn->stats.open_files_count);
1061 		__ksmbd_close_fd(NULL, fp);
1062 	}
1063 	return 0;
1064 
1065 out:
1066 	if (opinfo)
1067 		opinfo_put(opinfo);
1068 	ksmbd_put_durable_fd(fp);
1069 	return 0;
1070 }
1071 
1072 int ksmbd_invalidate_durable_fd(unsigned long long id)
1073 {
1074 	struct ksmbd_file *fp;
1075 
1076 	fp = ksmbd_lookup_global_fd(id);
1077 	if (!fp)
1078 		return -ENOENT;
1079 
1080 	fp->durable_reconnect_disabled = true;
1081 
1082 	if (fp->conn) {
1083 		ksmbd_put_durable_fd(fp);
1084 		return -ENOENT;
1085 	}
1086 
1087 	fp->durable_timeout = 1;
1088 	fp->durable_scavenger_timeout = jiffies_to_msecs(jiffies);
1089 	ksmbd_put_durable_fd(fp);
1090 	if (waitqueue_active(&dh_wq))
1091 		wake_up(&dh_wq);
1092 
1093 	return -ENOENT;
1094 }
1095 
1096 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid)
1097 {
1098 	struct ksmbd_file	*fp = NULL;
1099 	unsigned int		id;
1100 
1101 	read_lock(&global_ft.lock);
1102 	idr_for_each_entry(global_ft.idr, fp, id) {
1103 		if (!memcmp(fp->create_guid,
1104 			    cguid,
1105 			    SMB2_CREATE_GUID_SIZE)) {
1106 			fp = ksmbd_fp_get(fp);
1107 			break;
1108 		}
1109 	}
1110 	read_unlock(&global_ft.lock);
1111 
1112 	return fp;
1113 }
1114 
1115 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry)
1116 {
1117 	struct ksmbd_file	*lfp;
1118 	struct ksmbd_inode	*ci;
1119 	struct inode		*inode = d_inode(dentry);
1120 
1121 	read_lock(&inode_hash_lock);
1122 	ci = __ksmbd_inode_lookup(dentry);
1123 	read_unlock(&inode_hash_lock);
1124 	if (!ci)
1125 		return NULL;
1126 
1127 	down_read(&ci->m_lock);
1128 	list_for_each_entry(lfp, &ci->m_fp_list, node) {
1129 		if (inode == file_inode(lfp->filp)) {
1130 			lfp = ksmbd_fp_get(lfp);
1131 			up_read(&ci->m_lock);
1132 			ksmbd_inode_put(ci);
1133 			return lfp;
1134 		}
1135 	}
1136 	up_read(&ci->m_lock);
1137 	ksmbd_inode_put(ci);
1138 	return NULL;
1139 }
1140 
1141 bool ksmbd_has_other_nonposix_open(struct dentry *dentry)
1142 {
1143 	struct ksmbd_file *fp;
1144 	struct inode *inode = d_inode(dentry);
1145 	unsigned int id;
1146 	bool ret = false;
1147 
1148 	if (!inode)
1149 		return false;
1150 
1151 	read_lock(&global_ft.lock);
1152 	idr_for_each_entry(global_ft.idr, fp, id) {
1153 		if (READ_ONCE(fp->f_state) != FP_INITED)
1154 			continue;
1155 		if (inode != file_inode(fp->filp))
1156 			continue;
1157 		if (fp->is_posix_ctxt)
1158 			continue;
1159 
1160 		ret = true;
1161 		break;
1162 	}
1163 	read_unlock(&global_ft.lock);
1164 
1165 	return ret;
1166 }
1167 
1168 bool ksmbd_has_nonposix_open_child(struct ksmbd_file *old_fp)
1169 {
1170 	struct dentry *dentry = old_fp->filp->f_path.dentry;
1171 	struct ksmbd_file *fp;
1172 	unsigned int id;
1173 	bool ret = false;
1174 
1175 	read_lock(&global_ft.lock);
1176 	idr_for_each_entry(global_ft.idr, fp, id) {
1177 		struct dentry *fp_dentry = fp->filp->f_path.dentry;
1178 
1179 		if (fp->f_state != FP_INITED)
1180 			continue;
1181 		if (fp_dentry == dentry)
1182 			continue;
1183 		if (old_fp->is_posix_ctxt && fp->is_posix_ctxt)
1184 			continue;
1185 		if (is_subdir(fp_dentry, dentry)) {
1186 			ret = true;
1187 			break;
1188 		}
1189 	}
1190 	read_unlock(&global_ft.lock);
1191 
1192 	return ret;
1193 }
1194 
1195 #define OPEN_ID_TYPE_VOLATILE_ID	(0)
1196 #define OPEN_ID_TYPE_PERSISTENT_ID	(1)
1197 
1198 static void __open_id_set(struct ksmbd_file *fp, u64 id, int type)
1199 {
1200 	if (type == OPEN_ID_TYPE_VOLATILE_ID)
1201 		fp->volatile_id = id;
1202 	if (type == OPEN_ID_TYPE_PERSISTENT_ID)
1203 		fp->persistent_id = id;
1204 }
1205 
1206 static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
1207 		     int type)
1208 {
1209 	u64			id = 0;
1210 	int			ret;
1211 
1212 	if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) {
1213 		__open_id_set(fp, KSMBD_NO_FID, type);
1214 		return -EMFILE;
1215 	}
1216 
1217 	idr_preload(KSMBD_DEFAULT_GFP);
1218 	write_lock(&ft->lock);
1219 	ret = idr_alloc_cyclic(ft->idr, fp, KSMBD_START_FID, INT_MAX - 1,
1220 			       GFP_NOWAIT);
1221 	if (ret >= 0) {
1222 		id = ret;
1223 		ret = 0;
1224 	} else {
1225 		id = KSMBD_NO_FID;
1226 		fd_limit_close();
1227 	}
1228 
1229 	__open_id_set(fp, id, type);
1230 	write_unlock(&ft->lock);
1231 	idr_preload_end();
1232 	return ret;
1233 }
1234 
1235 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp)
1236 {
1237 	__open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID);
1238 	return fp->persistent_id;
1239 }
1240 
1241 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp)
1242 {
1243 	struct ksmbd_file *fp;
1244 	int ret;
1245 
1246 	fp = kmem_cache_zalloc(filp_cache, KSMBD_DEFAULT_GFP);
1247 	if (!fp) {
1248 		pr_err("Failed to allocate memory\n");
1249 		return ERR_PTR(-ENOMEM);
1250 	}
1251 
1252 	INIT_LIST_HEAD(&fp->blocked_works);
1253 	INIT_LIST_HEAD(&fp->node);
1254 	INIT_LIST_HEAD(&fp->lock_list);
1255 	INIT_LIST_HEAD(&fp->notify_pendings);
1256 	spin_lock_init(&fp->f_lock);
1257 	mutex_init(&fp->readdir_lock);
1258 	atomic_set(&fp->refcount, 1);
1259 
1260 	fp->filp		= filp;
1261 	/*
1262 	 * fp owns a strong reference on fp->conn for as long as fp->conn is
1263 	 * non-NULL, so session_fd_check() and __ksmbd_close_fd() never
1264 	 * dereference a dangling pointer.  Paired with ksmbd_conn_put() in
1265 	 * session_fd_check() (durable preserve), in __ksmbd_close_fd()
1266 	 * (final close), and on the error paths below.
1267 	 */
1268 	fp->conn		= ksmbd_conn_get(work->conn);
1269 	fp->tcon		= work->tcon;
1270 	fp->volatile_id		= KSMBD_NO_FID;
1271 	fp->persistent_id	= KSMBD_NO_FID;
1272 	fp->f_state		= FP_NEW;
1273 	fp->f_ci		= ksmbd_inode_get(fp);
1274 
1275 	if (!fp->f_ci) {
1276 		ret = -ENOMEM;
1277 		goto err_out;
1278 	}
1279 
1280 	ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
1281 	if (ret) {
1282 		ksmbd_inode_put(fp->f_ci);
1283 		goto err_out;
1284 	}
1285 
1286 	atomic_inc(&work->conn->stats.open_files_count);
1287 	return fp;
1288 
1289 err_out:
1290 	/* fp->conn was set and refcounted before every branch here. */
1291 	ksmbd_conn_put(fp->conn);
1292 	kmem_cache_free(filp_cache, fp);
1293 	return ERR_PTR(ret);
1294 }
1295 
1296 /**
1297  * ksmbd_update_fstate() - update an fp state under the file-table lock
1298  * @ft: file table that publishes @fp's volatile id
1299  * @fp: file pointer to update
1300  * @state: new state
1301  *
1302  * Return: 0 on success.  The FP_NEW -> FP_INITED transition is special:
1303  * -ENOENT if teardown already unpublished @fp by advancing the state or
1304  * clearing the volatile id.  Other state updates preserve the historical
1305  * fire-and-forget behavior.
1306  */
1307 int ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
1308 			unsigned int state)
1309 {
1310 	int ret;
1311 
1312 	if (!fp)
1313 		return -ENOENT;
1314 
1315 	write_lock(&ft->lock);
1316 	if (state == FP_INITED &&
1317 	    (fp->f_state != FP_NEW || !has_file_id(fp->volatile_id))) {
1318 		ret = -ENOENT;
1319 	} else {
1320 		fp->f_state = state;
1321 		ret = 0;
1322 	}
1323 	write_unlock(&ft->lock);
1324 
1325 	return ret;
1326 }
1327 
1328 /*
1329  * ksmbd_mark_fp_closed() - mark fp closed under ft->lock and return how many
1330  * refs the teardown path owns.
1331  *
1332  * FP_INITED has a normal idr-owned reference, so teardown owns both that
1333  * reference and the transient lookup reference.  FP_NEW is still owned by the
1334  * in-flight opener/reopener, which will drop the original reference after
1335  * ksmbd_update_fstate(..., FP_INITED) observes the cleared volatile id.
1336  * FP_CLOSED on entry means an earlier ksmbd_close_fd() already consumed the
1337  * idr-owned ref.
1338  */
1339 static int ksmbd_mark_fp_closed(struct ksmbd_file *fp)
1340 {
1341 	if (fp->f_state == FP_INITED) {
1342 		set_close_state_blocked_works(fp);
1343 		fp->f_state = FP_CLOSED;
1344 		return 2;
1345 	}
1346 
1347 	return 1;
1348 }
1349 
1350 static int
1351 __close_file_table_ids(struct ksmbd_session *sess,
1352 		       struct ksmbd_tree_connect *tcon,
1353 		       bool (*skip)(struct ksmbd_tree_connect *tcon,
1354 				    struct ksmbd_file *fp,
1355 				    struct ksmbd_user *user),
1356 		       bool skip_preserves_fp)
1357 {
1358 	struct ksmbd_file_table *ft = &sess->file_table;
1359 	struct ksmbd_file *fp;
1360 	unsigned int id = 0;
1361 	int num = 0;
1362 
1363 	while (1) {
1364 		int n_to_drop;
1365 
1366 		write_lock(&ft->lock);
1367 		fp = idr_get_next(ft->idr, &id);
1368 		if (!fp) {
1369 			write_unlock(&ft->lock);
1370 			break;
1371 		}
1372 		if (!atomic_inc_not_zero(&fp->refcount)) {
1373 			id++;
1374 			write_unlock(&ft->lock);
1375 			continue;
1376 		}
1377 
1378 		if (skip_preserves_fp) {
1379 			/*
1380 			 * Session teardown: skip() is session_fd_check(),
1381 			 * which may sleep and mutates fp->conn / fp->tcon /
1382 			 * fp->volatile_id when it chooses to preserve fp
1383 			 * for durable reconnect.  Unpublish fp from the
1384 			 * session idr here, under ft->lock, so that
1385 			 * __ksmbd_lookup_fd() through this session cannot
1386 			 * grant a new ksmbd_fp_get() reference to an fp
1387 			 * whose fields are about to be rewritten outside
1388 			 * the lock.  Durable reconnect still reaches fp via
1389 			 * global_ft.
1390 			 */
1391 			idr_remove(ft->idr, id);
1392 			fp->durable_volatile_id = fp->volatile_id;
1393 			fp->volatile_id = KSMBD_NO_FID;
1394 			write_unlock(&ft->lock);
1395 
1396 			if (skip(tcon, fp, sess->user)) {
1397 				/*
1398 				 * session_fd_check() has converted fp to
1399 				 * durable-preserve state and cleared its
1400 				 * per-conn fields.  fp is already unpublished
1401 				 * above; the original idr-owned ref keeps it
1402 				 * alive for the durable scavenger.  Drop only
1403 				 * the transient ref.  atomic_dec() is safe --
1404 				 * atomic_inc_not_zero() succeeded on a
1405 				 * positive value and we added one more, so
1406 				 * refcount cannot be zero here.
1407 				 */
1408 				atomic_dec(&fp->refcount);
1409 				id++;
1410 				continue;
1411 			}
1412 
1413 			/*
1414 			 * Keep the close-state decision under the same lock
1415 			 * observed by ksmbd_update_fstate(), which is how an
1416 			 * in-flight FP_NEW opener learns that teardown has
1417 			 * cleared its volatile id.
1418 			 */
1419 			write_lock(&ft->lock);
1420 			n_to_drop = ksmbd_mark_fp_closed(fp);
1421 			write_unlock(&ft->lock);
1422 		} else {
1423 			/*
1424 			 * Tree teardown: skip() is tree_conn_fd_check(), a
1425 			 * cheap pointer compare that doesn't sleep and has
1426 			 * no side effects, so keep the skip decision plus
1427 			 * the unpublish-and-mark-closed sequence atomic
1428 			 * under ft->lock.  fps belonging to other tree
1429 			 * connects (skip() == true) stay fully published in
1430 			 * the session idr with no lock window.
1431 			 */
1432 			if (skip(tcon, fp, sess->user)) {
1433 				atomic_dec(&fp->refcount);
1434 				write_unlock(&ft->lock);
1435 				id++;
1436 				continue;
1437 			}
1438 			idr_remove(ft->idr, id);
1439 			fp->volatile_id = KSMBD_NO_FID;
1440 			n_to_drop = ksmbd_mark_fp_closed(fp);
1441 			write_unlock(&ft->lock);
1442 		}
1443 
1444 		/*
1445 		 * fp->volatile_id is already cleared to prevent stale idr
1446 		 * removal from a deferred final close.  Remove fp from
1447 		 * m_fp_list here because __ksmbd_remove_fd() will skip the
1448 		 * list unlink when volatile_id is KSMBD_NO_FID.
1449 		 */
1450 		down_write(&fp->f_ci->m_lock);
1451 		list_del_init(&fp->node);
1452 		up_write(&fp->f_ci->m_lock);
1453 
1454 		/*
1455 		 * Drop the references this iteration owns:
1456 		 *
1457 		 *   n_to_drop == 2: we observed FP_INITED and committed
1458 		 *     the FP_CLOSED transition ourselves, so we own the
1459 		 *     transient (+1) and the still-intact idr-owned ref.
1460 		 *
1461 		 *   n_to_drop == 1: either a prior ksmbd_close_fd()
1462 		 *     already consumed the idr-owned ref, or fp was still
1463 		 *     FP_NEW and the in-flight opener/reopener must keep
1464 		 *     the original reference until ksmbd_update_fstate()
1465 		 *     observes the cleared volatile id.
1466 		 *
1467 		 * If we end up as the final putter, finalize fp and
1468 		 * account the open_files_count decrement via the caller's
1469 		 * atomic_sub(num, ...).  Otherwise the remaining user's
1470 		 * ksmbd_fd_put() reaches __put_fd_final(), which does its
1471 		 * own atomic_dec(&open_files_count), so we must not count
1472 		 * this fp here -- doing so would double-decrement the
1473 		 * connection-wide counter.
1474 		 */
1475 		if (atomic_sub_and_test(n_to_drop, &fp->refcount)) {
1476 			__ksmbd_close_fd(NULL, fp);
1477 			num++;
1478 		}
1479 		id++;
1480 	}
1481 
1482 	return num;
1483 }
1484 
1485 static inline bool is_reconnectable(struct ksmbd_file *fp)
1486 {
1487 	struct oplock_info *opinfo = opinfo_get(fp);
1488 	bool reconn = false;
1489 
1490 	if (!opinfo)
1491 		return false;
1492 
1493 	if (opinfo->op_state != OPLOCK_STATE_NONE) {
1494 		opinfo_put(opinfo);
1495 		return false;
1496 	}
1497 
1498 	if (fp->is_resilient || fp->is_persistent)
1499 		reconn = true;
1500 	else if (fp->is_durable && opinfo->is_lease &&
1501 		 opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
1502 		reconn = true;
1503 
1504 	else if (fp->is_durable && opinfo->level == SMB2_OPLOCK_LEVEL_BATCH)
1505 		reconn = true;
1506 
1507 	opinfo_put(opinfo);
1508 	return reconn;
1509 }
1510 
1511 static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon,
1512 			       struct ksmbd_file *fp,
1513 			       struct ksmbd_user *user)
1514 {
1515 	return fp->tcon != tcon;
1516 }
1517 
1518 static bool ksmbd_durable_scavenger_alive(void)
1519 {
1520 	if (!durable_scavenger_running)
1521 		return false;
1522 
1523 	if (kthread_should_stop())
1524 		return false;
1525 
1526 	if (idr_is_empty(global_ft.idr))
1527 		return false;
1528 
1529 	return true;
1530 }
1531 
1532 static void ksmbd_scavenger_dispose_dh(struct ksmbd_file *fp)
1533 {
1534 	/*
1535 	 * Durable-preserved fp can remain linked on f_ci->m_fp_list for
1536 	 * share-mode checks.  Unlink it before final close; fp->node is not
1537 	 * available as a scavenger-private list node because re-adding it to
1538 	 * another list corrupts m_fp_list.
1539 	 */
1540 	down_write(&fp->f_ci->m_lock);
1541 	list_del_init(&fp->node);
1542 	up_write(&fp->f_ci->m_lock);
1543 
1544 	/*
1545 	 * Drop both the durable lifetime reference and the transient reference
1546 	 * taken by the scavenger under global_ft.lock.  If a concurrent
1547 	 * ksmbd_lookup_fd_inode() (or any other m_fp_list walker) snatched fp
1548 	 * before the unlink above, that holder owns the final close via
1549 	 * ksmbd_fd_put() -> __ksmbd_close_fd().  Otherwise the scavenger is
1550 	 * the last putter and finalises fp here.
1551 	 */
1552 	if (atomic_sub_and_test(2, &fp->refcount))
1553 		__ksmbd_close_fd(NULL, fp);
1554 }
1555 
1556 static int ksmbd_durable_scavenger(void *dummy)
1557 {
1558 	struct ksmbd_file *fp = NULL;
1559 	struct ksmbd_file *expired_fp;
1560 	unsigned int id;
1561 	unsigned int min_timeout = 1;
1562 	bool found_fp_timeout;
1563 	unsigned long remaining_jiffies;
1564 
1565 	__module_get(THIS_MODULE);
1566 
1567 	set_freezable();
1568 	while (ksmbd_durable_scavenger_alive()) {
1569 		if (try_to_freeze())
1570 			continue;
1571 
1572 		remaining_jiffies = wait_event_interruptible_timeout(dh_wq,
1573 				   ksmbd_durable_scavenger_alive() == false,
1574 				   __msecs_to_jiffies(min_timeout));
1575 		if ((long)remaining_jiffies > 0)
1576 			min_timeout = jiffies_to_msecs(remaining_jiffies);
1577 		else
1578 			min_timeout = DURABLE_HANDLE_MAX_TIMEOUT;
1579 
1580 		do {
1581 			expired_fp = NULL;
1582 			found_fp_timeout = false;
1583 
1584 			write_lock(&global_ft.lock);
1585 			idr_for_each_entry(global_ft.idr, fp, id) {
1586 				unsigned long durable_timeout;
1587 
1588 				if (!fp->durable_timeout)
1589 					continue;
1590 
1591 				if (atomic_read(&fp->refcount) > 1 ||
1592 				    fp->conn)
1593 					continue;
1594 
1595 				found_fp_timeout = true;
1596 				if (fp->durable_scavenger_timeout <=
1597 				    jiffies_to_msecs(jiffies)) {
1598 					__ksmbd_remove_durable_fd(fp);
1599 					/*
1600 					 * Take a transient reference so fp
1601 					 * cannot be freed by an in-flight
1602 					 * ksmbd_lookup_fd_inode() that found
1603 					 * it through f_ci->m_fp_list while we
1604 					 * drop global_ft.lock and reach the
1605 					 * m_fp_list unlink in
1606 					 * ksmbd_scavenger_dispose_dh().
1607 					 */
1608 					atomic_inc(&fp->refcount);
1609 					expired_fp = fp;
1610 					break;
1611 				}
1612 
1613 				durable_timeout =
1614 					fp->durable_scavenger_timeout -
1615 						jiffies_to_msecs(jiffies);
1616 
1617 				if (min_timeout > durable_timeout)
1618 					min_timeout = durable_timeout;
1619 			}
1620 			write_unlock(&global_ft.lock);
1621 
1622 			if (expired_fp)
1623 				ksmbd_scavenger_dispose_dh(expired_fp);
1624 		} while (expired_fp);
1625 
1626 		if (found_fp_timeout == false)
1627 			break;
1628 	}
1629 
1630 	durable_scavenger_running = false;
1631 
1632 	module_put(THIS_MODULE);
1633 
1634 	return 0;
1635 }
1636 
1637 void ksmbd_launch_ksmbd_durable_scavenger(void)
1638 {
1639 	if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE))
1640 		return;
1641 
1642 	mutex_lock(&durable_scavenger_lock);
1643 	if (durable_scavenger_running == true) {
1644 		mutex_unlock(&durable_scavenger_lock);
1645 		return;
1646 	}
1647 
1648 	durable_scavenger_running = true;
1649 
1650 	server_conf.dh_task = kthread_run(ksmbd_durable_scavenger,
1651 				     (void *)NULL, "ksmbd-durable-scavenger");
1652 	if (IS_ERR(server_conf.dh_task)) {
1653 		pr_err("cannot start conn thread, err : %ld\n",
1654 		       PTR_ERR(server_conf.dh_task));
1655 		server_conf.dh_task = NULL;
1656 		durable_scavenger_running = false;
1657 	}
1658 	mutex_unlock(&durable_scavenger_lock);
1659 }
1660 
1661 void ksmbd_stop_durable_scavenger(void)
1662 {
1663 	if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE))
1664 		return;
1665 
1666 	mutex_lock(&durable_scavenger_lock);
1667 	if (!durable_scavenger_running) {
1668 		mutex_unlock(&durable_scavenger_lock);
1669 		return;
1670 	}
1671 
1672 	durable_scavenger_running = false;
1673 	if (waitqueue_active(&dh_wq))
1674 		wake_up(&dh_wq);
1675 	mutex_unlock(&durable_scavenger_lock);
1676 	kthread_stop(server_conf.dh_task);
1677 }
1678 
1679 /*
1680  * ksmbd_vfs_set_durable_owner - Store owner info for durable replay/reconnect
1681  * @fp: ksmbd file pointer to store owner info
1682  * @user: user pointer to copy from
1683  *
1684  * This function binds the current user's identity to the file handle
1685  * to satisfy MS-SMB2 Step 8 (SecurityContext matching) during reconnect.
1686  *
1687  * Return: 0 on success, or negative error code on failure
1688  */
1689 int ksmbd_vfs_set_durable_owner(struct ksmbd_file *fp,
1690 				struct ksmbd_user *user)
1691 {
1692 	char *name, *old_name;
1693 
1694 	if (!user)
1695 		return -EINVAL;
1696 
1697 	/* Duplicate the user name to ensure identity persistence */
1698 	name = kstrdup(user->name, GFP_KERNEL);
1699 	if (!name)
1700 		return -ENOMEM;
1701 
1702 	spin_lock(&fp->f_lock);
1703 	old_name = fp->owner.name;
1704 	fp->owner.uid = user->uid;
1705 	fp->owner.gid = user->gid;
1706 	fp->owner.name = name;
1707 	spin_unlock(&fp->f_lock);
1708 	kfree(old_name);
1709 
1710 	return 0;
1711 }
1712 
1713 /**
1714  * ksmbd_vfs_compare_durable_owner - Verify if the requester is original owner
1715  * @fp: existing ksmbd file pointer
1716  * @user: user pointer of the reconnect requester
1717  *
1718  * Compares the UID, GID, and name of the current requester against the
1719  * original owner stored in the file handle.
1720  *
1721  * Return: true if the user matches, false otherwise
1722  */
1723 bool ksmbd_vfs_compare_durable_owner(struct ksmbd_file *fp,
1724 		struct ksmbd_user *user)
1725 {
1726 	bool ret = false;
1727 
1728 	if (!user)
1729 		return false;
1730 
1731 	spin_lock(&fp->f_lock);
1732 	if (!fp->owner.name)
1733 		goto out;
1734 
1735 	/* Check if the UID and GID match first (fast path) */
1736 	if (fp->owner.uid != user->uid || fp->owner.gid != user->gid)
1737 		goto out;
1738 
1739 	/* Validate the account name to ensure the same SecurityContext */
1740 	ret = (strcmp(fp->owner.name, user->name) == 0);
1741 out:
1742 	spin_unlock(&fp->f_lock);
1743 	return ret;
1744 }
1745 
1746 static bool session_fd_check(struct ksmbd_tree_connect *tcon,
1747 			     struct ksmbd_file *fp, struct ksmbd_user *user)
1748 {
1749 	struct ksmbd_inode *ci;
1750 	struct oplock_info *op;
1751 	struct ksmbd_conn *conn;
1752 	struct ksmbd_lock *smb_lock, *tmp_lock;
1753 
1754 	if (!is_reconnectable(fp))
1755 		return false;
1756 
1757 	if (fp->f_state != FP_INITED)
1758 		return false;
1759 
1760 	if (WARN_ON_ONCE(!fp->conn))
1761 		return false;
1762 
1763 	if (ksmbd_vfs_set_durable_owner(fp, user))
1764 		return false;
1765 
1766 	/*
1767 	 * fp owns a strong reference on fp->conn (taken in ksmbd_open_fd()
1768 	 * / ksmbd_reopen_durable_fd()), so conn stays valid for the whole
1769 	 * body of this function regardless of any op->conn puts below.
1770 	 */
1771 	conn = fp->conn;
1772 	ci = fp->f_ci;
1773 	down_write(&ci->m_lock);
1774 	list_for_each_entry_rcu(op, &ci->m_op_list, op_entry,
1775 				lockdep_is_held(&ci->m_lock)) {
1776 		if (op->conn != conn)
1777 			continue;
1778 		ksmbd_conn_put(op->conn);
1779 		op->conn = NULL;
1780 		op->sess = NULL;
1781 	}
1782 	up_write(&ci->m_lock);
1783 
1784 	list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
1785 		struct ksmbd_conn *lock_conn = smb_lock->conn;
1786 
1787 		if (!lock_conn)
1788 			continue;
1789 		spin_lock(&lock_conn->llist_lock);
1790 		list_del_init(&smb_lock->clist);
1791 		smb_lock->conn = NULL;
1792 		spin_unlock(&lock_conn->llist_lock);
1793 		ksmbd_conn_put(lock_conn);
1794 	}
1795 
1796 	fp->conn = NULL;
1797 	fp->tcon = NULL;
1798 	fp->volatile_id = KSMBD_NO_FID;
1799 
1800 	if (fp->durable_timeout)
1801 		fp->durable_scavenger_timeout =
1802 			jiffies_to_msecs(jiffies) + fp->durable_timeout;
1803 
1804 	/* Drop fp's own reference on conn. */
1805 	ksmbd_conn_put(conn);
1806 	return true;
1807 }
1808 
1809 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work)
1810 {
1811 	int num = __close_file_table_ids(work->sess,
1812 					 work->tcon,
1813 					 tree_conn_fd_check,
1814 					 false);
1815 
1816 	atomic_sub(num, &work->conn->stats.open_files_count);
1817 }
1818 
1819 void ksmbd_close_session_fds(struct ksmbd_work *work)
1820 {
1821 	int num = __close_file_table_ids(work->sess,
1822 					 work->tcon,
1823 					 session_fd_check,
1824 					 true);
1825 
1826 	atomic_sub(num, &work->conn->stats.open_files_count);
1827 }
1828 
1829 int ksmbd_init_global_file_table(void)
1830 {
1831 	create_proc_files();
1832 	return ksmbd_init_file_table(&global_ft);
1833 }
1834 
1835 void ksmbd_free_global_file_table(void)
1836 {
1837 	struct ksmbd_file	*fp = NULL;
1838 	unsigned int		id;
1839 
1840 	idr_for_each_entry(global_ft.idr, fp, id) {
1841 		ksmbd_remove_durable_fd(fp);
1842 		__ksmbd_close_fd(NULL, fp);
1843 	}
1844 
1845 	idr_destroy(global_ft.idr);
1846 	kfree(global_ft.idr);
1847 }
1848 
1849 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
1850 				  struct ksmbd_file *fp, char *name)
1851 {
1852 	char *pathname, *ab_pathname;
1853 	int ret = 0;
1854 
1855 	pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
1856 	if (!pathname)
1857 		return -EACCES;
1858 
1859 	ab_pathname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
1860 	if (IS_ERR(ab_pathname)) {
1861 		kfree(pathname);
1862 		return -EACCES;
1863 	}
1864 
1865 	if (name && strcmp(&ab_pathname[share->path_sz + 1], name)) {
1866 		ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
1867 		ret = -EINVAL;
1868 	}
1869 
1870 	kfree(pathname);
1871 
1872 	return ret;
1873 }
1874 
1875 int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp)
1876 {
1877 	struct ksmbd_inode *ci;
1878 	struct oplock_info *op;
1879 	struct ksmbd_conn *conn = work->conn;
1880 	struct ksmbd_lock *smb_lock;
1881 	unsigned int old_f_state;
1882 
1883 	write_lock(&global_ft.lock);
1884 	if ((!fp->is_durable && !fp->is_persistent) || fp->conn || fp->tcon) {
1885 		write_unlock(&global_ft.lock);
1886 		pr_err("Invalid durable fd [%p:%p]\n", fp->conn, fp->tcon);
1887 		return -EBADF;
1888 	}
1889 
1890 	if (has_file_id(fp->volatile_id)) {
1891 		write_unlock(&global_ft.lock);
1892 		pr_err("Still in use durable fd: %llu\n", fp->volatile_id);
1893 		return -EBADF;
1894 	}
1895 
1896 	/*
1897 	 * Initialize fp's connection binding before publishing fp into the
1898 	 * session's file table.  If __open_id() is ordered first, a
1899 	 * concurrent teardown that iterates the table can observe a valid
1900 	 * volatile_id with fp->conn == NULL and preserve a
1901 	 * partially-initialized fp.  fp owns a strong reference on the new
1902 	 * conn (see ksmbd_open_fd()); undo it on __open_id() failure.
1903 	 */
1904 	fp->conn = ksmbd_conn_get(conn);
1905 	fp->tcon = work->tcon;
1906 	write_unlock(&global_ft.lock);
1907 
1908 	old_f_state = fp->f_state;
1909 	fp->f_state = FP_NEW;
1910 
1911 	__open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
1912 	if (!has_file_id(fp->volatile_id)) {
1913 		write_lock(&global_ft.lock);
1914 		fp->conn = NULL;
1915 		fp->tcon = NULL;
1916 		write_unlock(&global_ft.lock);
1917 		ksmbd_conn_put(conn);
1918 		fp->f_state = old_f_state;
1919 		return -EBADF;
1920 	}
1921 
1922 	list_for_each_entry(smb_lock, &fp->lock_list, flist) {
1923 		smb_lock->conn = ksmbd_conn_get(conn);
1924 		spin_lock(&conn->llist_lock);
1925 		list_add_tail(&smb_lock->clist, &conn->lock_list);
1926 		spin_unlock(&conn->llist_lock);
1927 	}
1928 
1929 	ci = fp->f_ci;
1930 	down_write(&ci->m_lock);
1931 	list_for_each_entry_rcu(op, &ci->m_op_list, op_entry,
1932 				lockdep_is_held(&ci->m_lock)) {
1933 		if (op->conn || op->o_fp != fp)
1934 			continue;
1935 		op->conn = ksmbd_conn_get(fp->conn);
1936 		op->sess = work->sess;
1937 	}
1938 	up_write(&ci->m_lock);
1939 
1940 	spin_lock(&fp->f_lock);
1941 	fp->owner.uid = fp->owner.gid = 0;
1942 	kfree(fp->owner.name);
1943 	fp->owner.name = NULL;
1944 	spin_unlock(&fp->f_lock);
1945 
1946 	return 0;
1947 }
1948 
1949 int ksmbd_init_file_table(struct ksmbd_file_table *ft)
1950 {
1951 	ft->idr = kzalloc_obj(struct idr, KSMBD_DEFAULT_GFP);
1952 	if (!ft->idr)
1953 		return -ENOMEM;
1954 
1955 	idr_init(ft->idr);
1956 	rwlock_init(&ft->lock);
1957 	return 0;
1958 }
1959 
1960 void ksmbd_destroy_file_table(struct ksmbd_session *sess)
1961 {
1962 	struct ksmbd_file_table *ft = &sess->file_table;
1963 
1964 	if (!ft->idr)
1965 		return;
1966 
1967 	__close_file_table_ids(sess, NULL, session_fd_check, true);
1968 	idr_destroy(ft->idr);
1969 	kfree(ft->idr);
1970 	ft->idr = NULL;
1971 }
1972 
1973 int ksmbd_init_file_cache(void)
1974 {
1975 	filp_cache = kmem_cache_create("ksmbd_file_cache",
1976 				       sizeof(struct ksmbd_file), 0,
1977 				       SLAB_HWCACHE_ALIGN, NULL);
1978 	if (!filp_cache)
1979 		goto out;
1980 
1981 	init_waitqueue_head(&dh_wq);
1982 
1983 	return 0;
1984 
1985 out:
1986 	pr_err("failed to allocate file cache\n");
1987 	return -ENOMEM;
1988 }
1989 
1990 void ksmbd_exit_file_cache(void)
1991 {
1992 	kmem_cache_destroy(filp_cache);
1993 }
1994