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