Debian Codegraph @

fs/smb/server/vfs_cache.c

raw
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2016 Namjae Jeon <[email protected]>
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
14 #include "glob.h"
15 #include "vfs_cache.h"
16 #include "oplock.h"
17 #include "vfs.h"
18 #include "connection.h"
19 #include "misc.h"
20 #include "mgmt/tree_connect.h"
21 #include "mgmt/user_session.h"
22 #include "mgmt/user_config.h"
23 #include "smb_common.h"
24 #include "server.h"
25 #include "smb2pdu.h"
26
27 #define S_DEL_PENDING 1
28 #define S_DEL_ON_CLS 2
29 #define S_DEL_ON_CLS_STREAM 8
30
31 static unsigned int inode_hash_mask __read_mostly;
32 static unsigned int inode_hash_shift __read_mostly;
33 static struct hlist_head *inode_hashtable __read_mostly;
34 static DEFINE_RWLOCK(inode_hash_lock);
35
36 static struct ksmbd_file_table global_ft;
37 static atomic_long_t fd_limit;
38 static struct kmem_cache *filp_cache;
39
40 #define OPLOCK_NONE 0
41 #define OPLOCK_EXCLUSIVE 1
42 #define OPLOCK_BATCH 2
43 #define OPLOCK_READ 3 /* level 2 oplock */
44
45 #ifdef CONFIG_PROC_FS
46
47 static const struct ksmbd_const_name ksmbd_lease_const_names[] = {
48 {le32_to_cpu(SMB2_LEASE_NONE_LE), "LEASE_NONE"},
49 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE), "LEASE_R"},
50 {le32_to_cpu(SMB2_LEASE_HANDLE_CACHING_LE), "LEASE_H"},
51 {le32_to_cpu(SMB2_LEASE_WRITE_CACHING_LE), "LEASE_W"},
52 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE |
53 SMB2_LEASE_HANDLE_CACHING_LE), "LEASE_RH"},
54 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE |
55 SMB2_LEASE_WRITE_CACHING_LE), "LEASE_RW"},
56 {le32_to_cpu(SMB2_LEASE_HANDLE_CACHING_LE |
57 SMB2_LEASE_WRITE_CACHING_LE), "LEASE_WH"},
58 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE |
59 SMB2_LEASE_HANDLE_CACHING_LE |
60 SMB2_LEASE_WRITE_CACHING_LE), "LEASE_RWH"},
61 };
62
63 static const struct ksmbd_const_name ksmbd_oplock_const_names[] = {
64 {SMB2_OPLOCK_LEVEL_NONE, "OPLOCK_NONE"},
65 {SMB2_OPLOCK_LEVEL_II, "OPLOCK_II"},
66 {SMB2_OPLOCK_LEVEL_EXCLUSIVE, "OPLOCK_EXECL"},
67 {SMB2_OPLOCK_LEVEL_BATCH, "OPLOCK_BATCH"},
68 };
69
70 static int proc_show_files(struct seq_file *m, void *v)
71 {
72 struct ksmbd_file *fp = NULL;
73 unsigned int id;
74 struct oplock_info *opinfo;
75
76 seq_printf(m, "#%-10s %-10s %-10s %-10s %-15s %-10s %-10s %s\n",
77 "<tree id>", "<pid>", "<vid>", "<refcnt>",
78 "<oplock>", "<daccess>", "<saccess>",
79 "<name>");
80
81 read_lock(&global_ft.lock);
82 idr_for_each_entry(global_ft.idr, fp, id) {
83 seq_printf(m, "%#-10x %#-10llx %#-10llx %#-10x",
84 fp->tcon ? fp->tcon->id : 0,
85 fp->persistent_id,
86 fp->volatile_id,
87 atomic_read(&fp->refcount));
88
89 rcu_read_lock();
90 opinfo = rcu_dereference(fp->f_opinfo);
91 if (opinfo) {
92 const struct ksmbd_const_name *const_names;
93 int count;
94 unsigned int level;
95
96 if (opinfo->is_lease) {
97 const_names = ksmbd_lease_const_names;
98 count = ARRAY_SIZE(ksmbd_lease_const_names);
99 level = le32_to_cpu(opinfo->o_lease->state);
100 } else {
101 const_names = ksmbd_oplock_const_names;
102 count = ARRAY_SIZE(ksmbd_oplock_const_names);
103 level = opinfo->level;
104 }
105 rcu_read_unlock();
106 ksmbd_proc_show_const_name(m, " %-15s",
107 const_names, count, level);
108 } else {
109 rcu_read_unlock();
110 seq_printf(m, " %-15s", " ");
111 }
112
113 seq_printf(m, " %#010x %#010x %s\n",
114 le32_to_cpu(fp->daccess),
115 le32_to_cpu(fp->saccess),
116 fp->filp->f_path.dentry->d_name.name);
117 }
118 read_unlock(&global_ft.lock);
119 return 0;
120 }
121
122 static int create_proc_files(void)
123 {
124 ksmbd_proc_create("files", proc_show_files, NULL);
125 return 0;
126 }
127 #else
128 static int create_proc_files(void) { return 0; }
129 #endif
130
131 static bool durable_scavenger_running;
132 static DEFINE_MUTEX(durable_scavenger_lock);
133 static wait_queue_head_t dh_wq;
134
135 void ksmbd_set_fd_limit(unsigned long limit)
136 {
137 limit = min(limit, get_max_files());
138 atomic_long_set(&fd_limit, limit);
139 }
140
141 static bool fd_limit_depleted(void)
142 {
143 long v = atomic_long_dec_return(&fd_limit);
144
145 if (v >= 0)
146 return false;
147 atomic_long_inc(&fd_limit);
148 return true;
149 }
150
151 static void fd_limit_close(void)
152 {
153 atomic_long_inc(&fd_limit);
154 }
155
156 /*
157 * INODE hash
158 */
159
160 static unsigned long inode_hash(struct super_block *sb, unsigned long hashval)
161 {
162 unsigned long tmp;
163
164 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
165 L1_CACHE_BYTES;
166 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> inode_hash_shift);
167 return tmp & inode_hash_mask;
168 }
169
170 static struct ksmbd_inode *__ksmbd_inode_lookup(struct dentry *de)
171 {
172 struct hlist_head *head = inode_hashtable +
173 inode_hash(d_inode(de)->i_sb, (unsigned long)de);
174 struct ksmbd_inode *ci = NULL, *ret_ci = NULL;
175
176 hlist_for_each_entry(ci, head, m_hash) {
177 if (ci->m_de == de) {
178 if (atomic_inc_not_zero(&ci->m_count))
179 ret_ci = ci;
180 break;
181 }
182 }
183 return ret_ci;
184 }
185
186 static struct ksmbd_inode *ksmbd_inode_lookup(struct ksmbd_file *fp)
187 {
188 return __ksmbd_inode_lookup(fp->filp->f_path.dentry);
189 }
190
191 struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d)
192 {
193 struct ksmbd_inode *ci;
194
195 read_lock(&inode_hash_lock);
196 ci = __ksmbd_inode_lookup(d);
197 read_unlock(&inode_hash_lock);
198
199 return ci;
200 }
201
202 int ksmbd_query_inode_status(struct dentry *dentry)
203 {
204 struct ksmbd_inode *ci;
205 int ret = KSMBD_INODE_STATUS_UNKNOWN;
206
207 read_lock(&inode_hash_lock);
208 ci = __ksmbd_inode_lookup(dentry);
209 read_unlock(&inode_hash_lock);
210 if (!ci)
211 return ret;
212
213 down_read(&ci->m_lock);
214 if (ci->m_flags & S_DEL_PENDING)
215 ret = KSMBD_INODE_STATUS_PENDING_DELETE;
216 else
217 ret = KSMBD_INODE_STATUS_OK;
218 up_read(&ci->m_lock);
219
220 atomic_dec(&ci->m_count);
221 return ret;
222 }
223
224 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp)
225 {
226 struct ksmbd_inode *ci = fp->f_ci;
227 int ret;
228
229 down_read(&ci->m_lock);
230 ret = (ci->m_flags & S_DEL_PENDING);
231 up_read(&ci->m_lock);
232
233 return ret;
234 }
235
236 void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp)
237 {
238 struct ksmbd_inode *ci = fp->f_ci;
239
240 down_write(&ci->m_lock);
241 ci->m_flags |= S_DEL_PENDING;
242 up_write(&ci->m_lock);
243 }
244
245 void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp)
246 {
247 struct ksmbd_inode *ci = fp->f_ci;
248
249 down_write(&ci->m_lock);
250 ci->m_flags &= ~S_DEL_PENDING;
251 up_write(&ci->m_lock);
252 }
253
254 void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp,
255 int file_info)
256 {
257 struct ksmbd_inode *ci = fp->f_ci;
258
259 down_write(&ci->m_lock);
260 if (ksmbd_stream_fd(fp))
261 ci->m_flags |= S_DEL_ON_CLS_STREAM;
262 else
263 ci->m_flags |= S_DEL_ON_CLS;
264 up_write(&ci->m_lock);
265 }
266
267 static void ksmbd_inode_hash(struct ksmbd_inode *ci)
268 {
269 struct hlist_head *b = inode_hashtable +
270 inode_hash(d_inode(ci->m_de)->i_sb, (unsigned long)ci->m_de);
271
272 hlist_add_head(&ci->m_hash, b);
273 }
274
275 static void ksmbd_inode_unhash(struct ksmbd_inode *ci)
276 {
277 write_lock(&inode_hash_lock);
278 hlist_del_init(&ci->m_hash);
279 write_unlock(&inode_hash_lock);
280 }
281
282 static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp)
283 {
284 atomic_set(&ci->m_count, 1);
285 atomic_set(&ci->op_count, 0);
286 atomic_set(&ci->sop_count, 0);
287 ci->m_flags = 0;
288 ci->m_fattr = 0;
289 INIT_LIST_HEAD(&ci->m_fp_list);
290 INIT_LIST_HEAD(&ci->m_op_list);
291 init_rwsem(&ci->m_lock);
292 ci->m_de = fp->filp->f_path.dentry;
293 return 0;
294 }
295
296 static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp)
297 {
298 struct ksmbd_inode *ci, *tmpci;
299 int rc;
300
301 read_lock(&inode_hash_lock);
302 ci = ksmbd_inode_lookup(fp);
303 read_unlock(&inode_hash_lock);
304 if (ci)
305 return ci;
306
307 ci = kmalloc_obj(struct ksmbd_inode, KSMBD_DEFAULT_GFP);
308 if (!ci)
309 return NULL;
310
311 rc = ksmbd_inode_init(ci, fp);
312 if (rc) {
313 pr_err("inode initialized failed\n");
314 kfree(ci);
315 return NULL;
316 }
317
318 write_lock(&inode_hash_lock);
319 tmpci = ksmbd_inode_lookup(fp);
320 if (!tmpci) {
321 ksmbd_inode_hash(ci);
322 } else {
323 kfree(ci);
324 ci = tmpci;
325 }
326 write_unlock(&inode_hash_lock);
327 return ci;
328 }
329
330 static void ksmbd_inode_free(struct ksmbd_inode *ci)
331 {
332 ksmbd_inode_unhash(ci);
333 kfree(ci);
334 }
335
336 void ksmbd_inode_put(struct ksmbd_inode *ci)
337 {
338 if (atomic_dec_and_test(&ci->m_count))
339 ksmbd_inode_free(ci);
340 }
341
342 int __init ksmbd_inode_hash_init(void)
343 {
344 unsigned int loop;
345 unsigned long numentries = 16384;
346 unsigned long bucketsize = sizeof(struct hlist_head);
347 unsigned long size;
348
349 inode_hash_shift = ilog2(numentries);
350 inode_hash_mask = (1 << inode_hash_shift) - 1;
351
352 size = bucketsize << inode_hash_shift;
353
354 /* init master fp hash table */
355 inode_hashtable = vmalloc(size);
356 if (!inode_hashtable)
357 return -ENOMEM;
358
359 for (loop = 0; loop < (1U << inode_hash_shift); loop++)
360 INIT_HLIST_HEAD(&inode_hashtable[loop]);
361 return 0;
362 }
363
364 void ksmbd_release_inode_hash(void)
365 {
366 vfree(inode_hashtable);
367 }
368
369 static void __ksmbd_inode_close(struct ksmbd_file *fp)
370 {
371 struct ksmbd_inode *ci = fp->f_ci;
372 int err;
373 struct file *filp;
374
375 filp = fp->filp;
376
377 if (ksmbd_stream_fd(fp)) {
378 bool remove_stream_xattr = false;
379
380 down_write(&ci->m_lock);
381 if (ci->m_flags & S_DEL_ON_CLS_STREAM) {
382 ci->m_flags &= ~S_DEL_ON_CLS_STREAM;
383 remove_stream_xattr = true;
384 }
385 up_write(&ci->m_lock);
386
387 if (remove_stream_xattr) {
388 err = ksmbd_vfs_remove_xattr(file_mnt_idmap(filp),
389 &filp->f_path,
390 fp->stream.name,
391 true);
392 if (err)
393 pr_err("remove xattr failed : %s\n",
394 fp->stream.name);
395 }
396 }
397
398 down_write(&ci->m_lock);
399 /* Promote S_DEL_ON_CLS to S_DEL_PENDING when close */
400 if (ci->m_flags & S_DEL_ON_CLS) {
401 ci->m_flags &= ~S_DEL_ON_CLS;
402 ci->m_flags |= S_DEL_PENDING;
403 }
404 up_write(&ci->m_lock);
405
406 if (atomic_dec_and_test(&ci->m_count)) {
407 bool do_unlink = false;
408
409 down_write(&ci->m_lock);
410 if (ci->m_flags & S_DEL_PENDING) {
411 ci->m_flags &= ~S_DEL_PENDING;
412 do_unlink = true;
413 }
414 up_write(&ci->m_lock);
415
416 if (do_unlink)
417 ksmbd_vfs_unlink(filp);
418
419 ksmbd_inode_free(ci);
420 }
421 }
422
423 static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp)
424 {
425 if (!has_file_id(fp->persistent_id))
426 return;
427
428 idr_remove(global_ft.idr, fp->persistent_id);
429 /*
430 * Clear persistent_id so a later __ksmbd_close_fd() that runs from a
431 * delayed putter (e.g. when a concurrent ksmbd_lookup_fd_inode()
432 * walker held the final reference) does not re-issue idr_remove() on
433 * an id that idr_alloc_cyclic() may have already handed out to a new
434 * durable handle.
435 */
436 fp->persistent_id = KSMBD_NO_FID;
437 }
438
439 static void ksmbd_remove_durable_fd(struct ksmbd_file *fp)
440 {
441 write_lock(&global_ft.lock);
442 __ksmbd_remove_durable_fd(fp);
443 write_unlock(&global_ft.lock);
444 if (waitqueue_active(&dh_wq))
445 wake_up(&dh_wq);
446 }
447
448 static void __ksmbd_remove_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
449 {
450 if (!has_file_id(fp->volatile_id))
451 return;
452
453 down_write(&fp->f_ci->m_lock);
454 list_del_init(&fp->node);
455 up_write(&fp->f_ci->m_lock);
456
457 write_lock(&ft->lock);
458 idr_remove(ft->idr, fp->volatile_id);
459 write_unlock(&ft->lock);
460 }
461
462 static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
463 {
464 struct file *filp;
465 struct ksmbd_lock *smb_lock, *tmp_lock;
466
467 fd_limit_close();
468 ksmbd_remove_durable_fd(fp);
469 if (ft)
470 __ksmbd_remove_fd(ft, fp);
471
472 close_id_del_oplock(fp);
473 filp = fp->filp;
474
475 __ksmbd_inode_close(fp);
476 if (!IS_ERR_OR_NULL(filp))
477 fput(filp);
478
479 /* because the reference count of fp is 0, it is guaranteed that
480 * there are not accesses to fp->lock_list.
481 */
482 list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
483 if (!list_empty(&smb_lock->clist) && fp->conn) {
484 spin_lock(&fp->conn->llist_lock);
485 list_del(&smb_lock->clist);
486 spin_unlock(&fp->conn->llist_lock);
487 }
488
489 list_del(&smb_lock->flist);
490 locks_free_lock(smb_lock->fl);
491 kfree(smb_lock);
492 }
493
494 if (ksmbd_stream_fd(fp))
495 kfree(fp->stream.name);
496 kfree(fp->owner.name);
497
498 kmem_cache_free(filp_cache, fp);
499 }
500
501 static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp)
502 {
503 if (fp->f_state != FP_INITED)
504 return NULL;
505
506 if (!atomic_inc_not_zero(&fp->refcount))
507 return NULL;
508 return fp;
509 }
510
511 static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft,
512 u64 id)
513 {
514 struct ksmbd_file *fp;
515
516 if (!has_file_id(id))
517 return NULL;
518
519 read_lock(&ft->lock);
520 fp = idr_find(ft->idr, id);
521 if (fp)
522 fp = ksmbd_fp_get(fp);
523 read_unlock(&ft->lock);
524 return fp;
525 }
526
527 static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp)
528 {
529 /*
530 * Detached durable fp -- session_fd_check() cleared fp->conn at
531 * preserve, so this fp is no longer tracked by any conn's
532 * stats.open_files_count. This happens when
533 * ksmbd_scavenger_dispose_dh() hands the final close off to an
534 * m_fp_list walker (e.g. ksmbd_lookup_fd_inode()) whose work->conn
535 * is unrelated to the conn that originally opened the handle; close
536 * via the NULL-ft path so we do not underflow that unrelated
537 * counter.
538 */
539 if (!fp->conn) {
540 __ksmbd_close_fd(NULL, fp);
541 return;
542 }
543 __ksmbd_close_fd(&work->sess->file_table, fp);
544 atomic_dec(&work->conn->stats.open_files_count);
545 }
546
547 static void set_close_state_blocked_works(struct ksmbd_file *fp)
548 {
549 struct ksmbd_work *cancel_work;
550
551 spin_lock(&fp->f_lock);
552 list_for_each_entry(cancel_work, &fp->blocked_works,
553 fp_entry) {
554 cancel_work->state = KSMBD_WORK_CLOSED;
555 cancel_work->cancel_fn(cancel_work->cancel_argv);
556 }
557 spin_unlock(&fp->f_lock);
558 }
559
560 int ksmbd_close_fd(struct ksmbd_work *work, u64 id)
561 {
562 struct ksmbd_file *fp;
563 struct ksmbd_file_table *ft;
564
565 if (!has_file_id(id))
566 return 0;
567
568 ft = &work->sess->file_table;
569 write_lock(&ft->lock);
570 fp = idr_find(ft->idr, id);
571 if (fp) {
572 set_close_state_blocked_works(fp);
573
574 if (fp->f_state != FP_INITED)
575 fp = NULL;
576 else {
577 fp->f_state = FP_CLOSED;
578 if (!atomic_dec_and_test(&fp->refcount))
579 fp = NULL;
580 }
581 }
582 write_unlock(&ft->lock);
583
584 if (!fp)
585 return -EINVAL;
586
587 __put_fd_final(work, fp);
588 return 0;
589 }
590
591 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp)
592 {
593 if (!fp)
594 return;
595
596 if (!atomic_dec_and_test(&fp->refcount))
597 return;
598 __put_fd_final(work, fp);
599 }
600
601 static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp)
602 {
603 if (!fp)
604 return false;
605 if (fp->tcon != tcon)
606 return false;
607 return true;
608 }
609
610 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id)
611 {
612 return __ksmbd_lookup_fd(&work->sess->file_table, id);
613 }
614
615 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id)
616 {
617 struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
618
619 if (__sanity_check(work->tcon, fp))
620 return fp;
621
622 ksmbd_fd_put(work, fp);
623 return NULL;
624 }
625
626 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id,
627 u64 pid)
628 {
629 struct ksmbd_file *fp;
630
631 if (!has_file_id(id)) {
632 id = work->compound_fid;
633 pid = work->compound_pfid;
634 }
635
636 fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
637 if (!__sanity_check(work->tcon, fp)) {
638 ksmbd_fd_put(work, fp);
639 return NULL;
640 }
641 if (fp->persistent_id != pid) {
642 ksmbd_fd_put(work, fp);
643 return NULL;
644 }
645 return fp;
646 }
647
648 struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id)
649 {
650 return __ksmbd_lookup_fd(&global_ft, id);
651 }
652
653 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id)
654 {
655 struct ksmbd_file *fp;
656
657 fp = __ksmbd_lookup_fd(&global_ft, id);
658 if (fp && (fp->conn ||
659 (fp->durable_scavenger_timeout &&
660 (fp->durable_scavenger_timeout <
661 jiffies_to_msecs(jiffies))))) {
662 ksmbd_put_durable_fd(fp);
663 fp = NULL;
664 }
665
666 return fp;
667 }
668
669 void ksmbd_put_durable_fd(struct ksmbd_file *fp)
670 {
671 if (!atomic_dec_and_test(&fp->refcount))
672 return;
673
674 __ksmbd_close_fd(NULL, fp);
675 }
676
677 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid)
678 {
679 struct ksmbd_file *fp = NULL;
680 unsigned int id;
681
682 read_lock(&global_ft.lock);
683 idr_for_each_entry(global_ft.idr, fp, id) {
684 if (!memcmp(fp->create_guid,
685 cguid,
686 SMB2_CREATE_GUID_SIZE)) {
687 fp = ksmbd_fp_get(fp);
688 break;
689 }
690 }
691 read_unlock(&global_ft.lock);
692
693 return fp;
694 }
695
696 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry)
697 {
698 struct ksmbd_file *lfp;
699 struct ksmbd_inode *ci;
700 struct inode *inode = d_inode(dentry);
701
702 read_lock(&inode_hash_lock);
703 ci = __ksmbd_inode_lookup(dentry);
704 read_unlock(&inode_hash_lock);
705 if (!ci)
706 return NULL;
707
708 down_read(&ci->m_lock);
709 list_for_each_entry(lfp, &ci->m_fp_list, node) {
710 if (inode == file_inode(lfp->filp)) {
711 atomic_dec(&ci->m_count);
712 lfp = ksmbd_fp_get(lfp);
713 up_read(&ci->m_lock);
714 return lfp;
715 }
716 }
717 atomic_dec(&ci->m_count);
718 up_read(&ci->m_lock);
719 return NULL;
720 }
721
722 #define OPEN_ID_TYPE_VOLATILE_ID (0)
723 #define OPEN_ID_TYPE_PERSISTENT_ID (1)
724
725 static void __open_id_set(struct ksmbd_file *fp, u64 id, int type)
726 {
727 if (type == OPEN_ID_TYPE_VOLATILE_ID)
728 fp->volatile_id = id;
729 if (type == OPEN_ID_TYPE_PERSISTENT_ID)
730 fp->persistent_id = id;
731 }
732
733 static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
734 int type)
735 {
736 u64 id = 0;
737 int ret;
738
739 if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) {
740 __open_id_set(fp, KSMBD_NO_FID, type);
741 return -EMFILE;
742 }
743
744 idr_preload(KSMBD_DEFAULT_GFP);
745 write_lock(&ft->lock);
746 ret = idr_alloc_cyclic(ft->idr, fp, 0, INT_MAX - 1, GFP_NOWAIT);
747 if (ret >= 0) {
748 id = ret;
749 ret = 0;
750 } else {
751 id = KSMBD_NO_FID;
752 fd_limit_close();
753 }
754
755 __open_id_set(fp, id, type);
756 write_unlock(&ft->lock);
757 idr_preload_end();
758 return ret;
759 }
760
761 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp)
762 {
763 __open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID);
764 return fp->persistent_id;
765 }
766
767 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp)
768 {
769 struct ksmbd_file *fp;
770 int ret;
771
772 fp = kmem_cache_zalloc(filp_cache, KSMBD_DEFAULT_GFP);
773 if (!fp) {
774 pr_err("Failed to allocate memory\n");
775 return ERR_PTR(-ENOMEM);
776 }
777
778 INIT_LIST_HEAD(&fp->blocked_works);
779 INIT_LIST_HEAD(&fp->node);
780 INIT_LIST_HEAD(&fp->lock_list);
781 spin_lock_init(&fp->f_lock);
782 atomic_set(&fp->refcount, 1);
783
784 fp->filp = filp;
785 fp->conn = work->conn;
786 fp->tcon = work->tcon;
787 fp->volatile_id = KSMBD_NO_FID;
788 fp->persistent_id = KSMBD_NO_FID;
789 fp->f_state = FP_NEW;
790 fp->f_ci = ksmbd_inode_get(fp);
791
792 if (!fp->f_ci) {
793 ret = -ENOMEM;
794 goto err_out;
795 }
796
797 ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
798 if (ret) {
799 ksmbd_inode_put(fp->f_ci);
800 goto err_out;
801 }
802
803 atomic_inc(&work->conn->stats.open_files_count);
804 return fp;
805
806 err_out:
807 kmem_cache_free(filp_cache, fp);
808 return ERR_PTR(ret);
809 }
810
811 void ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
812 unsigned int state)
813 {
814 if (!fp)
815 return;
816
817 write_lock(&ft->lock);
818 fp->f_state = state;
819 write_unlock(&ft->lock);
820 }
821
822 static int
823 __close_file_table_ids(struct ksmbd_session *sess,
824 struct ksmbd_tree_connect *tcon,
825 bool (*skip)(struct ksmbd_tree_connect *tcon,
826 struct ksmbd_file *fp,
827 struct ksmbd_user *user))
828 {
829 struct ksmbd_file_table *ft = &sess->file_table;
830 struct ksmbd_file *fp;
831 unsigned int id = 0;
832 int num = 0;
833
834 while (1) {
835 write_lock(&ft->lock);
836 fp = idr_get_next(ft->idr, &id);
837 if (!fp) {
838 write_unlock(&ft->lock);
839 break;
840 }
841
842 if (skip(tcon, fp, sess->user) ||
843 !atomic_dec_and_test(&fp->refcount)) {
844 id++;
845 write_unlock(&ft->lock);
846 continue;
847 }
848
849 set_close_state_blocked_works(fp);
850 idr_remove(ft->idr, fp->volatile_id);
851 fp->volatile_id = KSMBD_NO_FID;
852 write_unlock(&ft->lock);
853
854 down_write(&fp->f_ci->m_lock);
855 list_del_init(&fp->node);
856 up_write(&fp->f_ci->m_lock);
857
858 __ksmbd_close_fd(ft, fp);
859
860 num++;
861 id++;
862 }
863
864 return num;
865 }
866
867 static inline bool is_reconnectable(struct ksmbd_file *fp)
868 {
869 struct oplock_info *opinfo = opinfo_get(fp);
870 bool reconn = false;
871
872 if (!opinfo)
873 return false;
874
875 if (opinfo->op_state != OPLOCK_STATE_NONE) {
876 opinfo_put(opinfo);
877 return false;
878 }
879
880 if (fp->is_resilient || fp->is_persistent)
881 reconn = true;
882 else if (fp->is_durable && opinfo->is_lease &&
883 opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
884 reconn = true;
885
886 else if (fp->is_durable && opinfo->level == SMB2_OPLOCK_LEVEL_BATCH)
887 reconn = true;
888
889 opinfo_put(opinfo);
890 return reconn;
891 }
892
893 static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon,
894 struct ksmbd_file *fp,
895 struct ksmbd_user *user)
896 {
897 return fp->tcon != tcon;
898 }
899
900 static bool ksmbd_durable_scavenger_alive(void)
901 {
902 if (!durable_scavenger_running)
903 return false;
904
905 if (kthread_should_stop())
906 return false;
907
908 if (idr_is_empty(global_ft.idr))
909 return false;
910
911 return true;
912 }
913
914 static void ksmbd_scavenger_dispose_dh(struct ksmbd_file *fp)
915 {
916 /*
917 * Durable-preserved fp can remain linked on f_ci->m_fp_list for
918 * share-mode checks. Unlink it before final close; fp->node is not
919 * available as a scavenger-private list node because re-adding it to
920 * another list corrupts m_fp_list.
921 */
922 down_write(&fp->f_ci->m_lock);
923 list_del_init(&fp->node);
924 up_write(&fp->f_ci->m_lock);
925
926 /*
927 * Drop both the durable lifetime reference and the transient reference
928 * taken by the scavenger under global_ft.lock. If a concurrent
929 * ksmbd_lookup_fd_inode() (or any other m_fp_list walker) snatched fp
930 * before the unlink above, that holder owns the final close via
931 * ksmbd_fd_put() -> __ksmbd_close_fd(). Otherwise the scavenger is
932 * the last putter and finalises fp here.
933 */
934 if (atomic_sub_and_test(2, &fp->refcount))
935 __ksmbd_close_fd(NULL, fp);
936 }
937
938 static int ksmbd_durable_scavenger(void *dummy)
939 {
940 struct ksmbd_file *fp = NULL;
941 struct ksmbd_file *expired_fp;
942 unsigned int id;
943 unsigned int min_timeout = 1;
944 bool found_fp_timeout;
945 unsigned long remaining_jiffies;
946
947 __module_get(THIS_MODULE);
948
949 set_freezable();
950 while (ksmbd_durable_scavenger_alive()) {
951 if (try_to_freeze())
952 continue;
953
954 remaining_jiffies = wait_event_timeout(dh_wq,
955 ksmbd_durable_scavenger_alive() == false,
956 __msecs_to_jiffies(min_timeout));
957 if (remaining_jiffies)
958 min_timeout = jiffies_to_msecs(remaining_jiffies);
959 else
960 min_timeout = DURABLE_HANDLE_MAX_TIMEOUT;
961
962 do {
963 expired_fp = NULL;
964 found_fp_timeout = false;
965
966 write_lock(&global_ft.lock);
967 idr_for_each_entry(global_ft.idr, fp, id) {
968 unsigned long durable_timeout;
969
970 if (!fp->durable_timeout)
971 continue;
972
973 if (atomic_read(&fp->refcount) > 1 ||
974 fp->conn)
975 continue;
976
977 found_fp_timeout = true;
978 if (fp->durable_scavenger_timeout <=
979 jiffies_to_msecs(jiffies)) {
980 __ksmbd_remove_durable_fd(fp);
981 /*
982 * Take a transient reference so fp
983 * cannot be freed by an in-flight
984 * ksmbd_lookup_fd_inode() that found
985 * it through f_ci->m_fp_list while we
986 * drop global_ft.lock and reach the
987 * m_fp_list unlink in
988 * ksmbd_scavenger_dispose_dh().
989 */
990 atomic_inc(&fp->refcount);
991 expired_fp = fp;
992 break;
993 }
994
995 durable_timeout =
996 fp->durable_scavenger_timeout -
997 jiffies_to_msecs(jiffies);
998
999 if (min_timeout > durable_timeout)
1000 min_timeout = durable_timeout;
1001 }
1002 write_unlock(&global_ft.lock);
1003
1004 if (expired_fp)
1005 ksmbd_scavenger_dispose_dh(expired_fp);
1006 } while (expired_fp);
1007
1008 if (found_fp_timeout == false)
1009 break;
1010 }
1011
1012 durable_scavenger_running = false;
1013
1014 module_put(THIS_MODULE);
1015
1016 return 0;
1017 }
1018
1019 void ksmbd_launch_ksmbd_durable_scavenger(void)
1020 {
1021 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE))
1022 return;
1023
1024 mutex_lock(&durable_scavenger_lock);
1025 if (durable_scavenger_running == true) {
1026 mutex_unlock(&durable_scavenger_lock);
1027 return;
1028 }
1029
1030 durable_scavenger_running = true;
1031
1032 server_conf.dh_task = kthread_run(ksmbd_durable_scavenger,
1033 (void *)NULL, "ksmbd-durable-scavenger");
1034 if (IS_ERR(server_conf.dh_task))
1035 pr_err("cannot start conn thread, err : %ld\n",
1036 PTR_ERR(server_conf.dh_task));
1037 mutex_unlock(&durable_scavenger_lock);
1038 }
1039
1040 void ksmbd_stop_durable_scavenger(void)
1041 {
1042 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE))
1043 return;
1044
1045 mutex_lock(&durable_scavenger_lock);
1046 if (!durable_scavenger_running) {
1047 mutex_unlock(&durable_scavenger_lock);
1048 return;
1049 }
1050
1051 durable_scavenger_running = false;
1052 if (waitqueue_active(&dh_wq))
1053 wake_up(&dh_wq);
1054 mutex_unlock(&durable_scavenger_lock);
1055 kthread_stop(server_conf.dh_task);
1056 }
1057
1058 /*
1059 * ksmbd_vfs_copy_durable_owner - Copy owner info for durable reconnect
1060 * @fp: ksmbd file pointer to store owner info
1061 * @user: user pointer to copy from
1062 *
1063 * This function binds the current user's identity to the file handle
1064 * to satisfy MS-SMB2 Step 8 (SecurityContext matching) during reconnect.
1065 *
1066 * Return: 0 on success, or negative error code on failure
1067 */
1068 static int ksmbd_vfs_copy_durable_owner(struct ksmbd_file *fp,
1069 struct ksmbd_user *user)
1070 {
1071 if (!user)
1072 return -EINVAL;
1073
1074 /* Duplicate the user name to ensure identity persistence */
1075 fp->owner.name = kstrdup(user->name, GFP_KERNEL);
1076 if (!fp->owner.name)
1077 return -ENOMEM;
1078
1079 fp->owner.uid = user->uid;
1080 fp->owner.gid = user->gid;
1081
1082 return 0;
1083 }
1084
1085 /**
1086 * ksmbd_vfs_compare_durable_owner - Verify if the requester is original owner
1087 * @fp: existing ksmbd file pointer
1088 * @user: user pointer of the reconnect requester
1089 *
1090 * Compares the UID, GID, and name of the current requester against the
1091 * original owner stored in the file handle.
1092 *
1093 * Return: true if the user matches, false otherwise
1094 */
1095 bool ksmbd_vfs_compare_durable_owner(struct ksmbd_file *fp,
1096 struct ksmbd_user *user)
1097 {
1098 if (!user || !fp->owner.name)
1099 return false;
1100
1101 /* Check if the UID and GID match first (fast path) */
1102 if (fp->owner.uid != user->uid || fp->owner.gid != user->gid)
1103 return false;
1104
1105 /* Validate the account name to ensure the same SecurityContext */
1106 if (strcmp(fp->owner.name, user->name))
1107 return false;
1108
1109 return true;
1110 }
1111
1112 static bool session_fd_check(struct ksmbd_tree_connect *tcon,
1113 struct ksmbd_file *fp, struct ksmbd_user *user)
1114 {
1115 struct ksmbd_inode *ci;
1116 struct oplock_info *op;
1117 struct ksmbd_conn *conn;
1118 struct ksmbd_lock *smb_lock, *tmp_lock;
1119
1120 if (!is_reconnectable(fp))
1121 return false;
1122
1123 if (ksmbd_vfs_copy_durable_owner(fp, user))
1124 return false;
1125
1126 conn = fp->conn;
1127 ci = fp->f_ci;
1128 down_write(&ci->m_lock);
1129 list_for_each_entry_rcu(op, &ci->m_op_list, op_entry) {
1130 if (op->conn != conn)
1131 continue;
1132 if (op->conn && atomic_dec_and_test(&op->conn->refcnt))
1133 kfree(op->conn);
1134 op->conn = NULL;
1135 }
1136 up_write(&ci->m_lock);
1137
1138 list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
1139 spin_lock(&fp->conn->llist_lock);
1140 list_del_init(&smb_lock->clist);
1141 spin_unlock(&fp->conn->llist_lock);
1142 }
1143
1144 fp->conn = NULL;
1145 fp->tcon = NULL;
1146 fp->volatile_id = KSMBD_NO_FID;
1147
1148 if (fp->durable_timeout)
1149 fp->durable_scavenger_timeout =
1150 jiffies_to_msecs(jiffies) + fp->durable_timeout;
1151
1152 return true;
1153 }
1154
1155 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work)
1156 {
1157 int num = __close_file_table_ids(work->sess,
1158 work->tcon,
1159 tree_conn_fd_check);
1160
1161 atomic_sub(num, &work->conn->stats.open_files_count);
1162 }
1163
1164 void ksmbd_close_session_fds(struct ksmbd_work *work)
1165 {
1166 int num = __close_file_table_ids(work->sess,
1167 work->tcon,
1168 session_fd_check);
1169
1170 atomic_sub(num, &work->conn->stats.open_files_count);
1171 }
1172
1173 int ksmbd_init_global_file_table(void)
1174 {
1175 create_proc_files();
1176 return ksmbd_init_file_table(&global_ft);
1177 }
1178
1179 void ksmbd_free_global_file_table(void)
1180 {
1181 struct ksmbd_file *fp = NULL;
1182 unsigned int id;
1183
1184 idr_for_each_entry(global_ft.idr, fp, id) {
1185 ksmbd_remove_durable_fd(fp);
1186 __ksmbd_close_fd(NULL, fp);
1187 }
1188
1189 idr_destroy(global_ft.idr);
1190 kfree(global_ft.idr);
1191 }
1192
1193 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
1194 struct ksmbd_file *fp, char *name)
1195 {
1196 char *pathname, *ab_pathname;
1197 int ret = 0;
1198
1199 pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
1200 if (!pathname)
1201 return -EACCES;
1202
1203 ab_pathname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
1204 if (IS_ERR(ab_pathname)) {
1205 kfree(pathname);
1206 return -EACCES;
1207 }
1208
1209 if (name && strcmp(&ab_pathname[share->path_sz + 1], name)) {
1210 ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
1211 ret = -EINVAL;
1212 }
1213
1214 kfree(pathname);
1215
1216 return ret;
1217 }
1218
1219 int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp)
1220 {
1221 struct ksmbd_inode *ci;
1222 struct oplock_info *op;
1223 struct ksmbd_conn *conn = work->conn;
1224 struct ksmbd_lock *smb_lock;
1225 unsigned int old_f_state;
1226
1227 if (!fp->is_durable || fp->conn || fp->tcon) {
1228 pr_err("Invalid durable fd [%p:%p]\n", fp->conn, fp->tcon);
1229 return -EBADF;
1230 }
1231
1232 if (has_file_id(fp->volatile_id)) {
1233 pr_err("Still in use durable fd: %llu\n", fp->volatile_id);
1234 return -EBADF;
1235 }
1236
1237 old_f_state = fp->f_state;
1238 fp->f_state = FP_NEW;
1239 __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
1240 if (!has_file_id(fp->volatile_id)) {
1241 fp->f_state = old_f_state;
1242 return -EBADF;
1243 }
1244
1245 fp->conn = conn;
1246 fp->tcon = work->tcon;
1247
1248 list_for_each_entry(smb_lock, &fp->lock_list, flist) {
1249 spin_lock(&conn->llist_lock);
1250 list_add_tail(&smb_lock->clist, &conn->lock_list);
1251 spin_unlock(&conn->llist_lock);
1252 }
1253
1254 ci = fp->f_ci;
1255 down_write(&ci->m_lock);
1256 list_for_each_entry_rcu(op, &ci->m_op_list, op_entry) {
1257 if (op->conn)
1258 continue;
1259 op->conn = fp->conn;
1260 atomic_inc(&op->conn->refcnt);
1261 }
1262 up_write(&ci->m_lock);
1263
1264 fp->owner.uid = fp->owner.gid = 0;
1265 kfree(fp->owner.name);
1266 fp->owner.name = NULL;
1267
1268 return 0;
1269 }
1270
1271 int ksmbd_init_file_table(struct ksmbd_file_table *ft)
1272 {
1273 ft->idr = kzalloc_obj(struct idr, KSMBD_DEFAULT_GFP);
1274 if (!ft->idr)
1275 return -ENOMEM;
1276
1277 idr_init(ft->idr);
1278 rwlock_init(&ft->lock);
1279 return 0;
1280 }
1281
1282 void ksmbd_destroy_file_table(struct ksmbd_session *sess)
1283 {
1284 struct ksmbd_file_table *ft = &sess->file_table;
1285
1286 if (!ft->idr)
1287 return;
1288
1289 __close_file_table_ids(sess, NULL, session_fd_check);
1290 idr_destroy(ft->idr);
1291 kfree(ft->idr);
1292 ft->idr = NULL;
1293 }
1294
1295 int ksmbd_init_file_cache(void)
1296 {
1297 filp_cache = kmem_cache_create("ksmbd_file_cache",
1298 sizeof(struct ksmbd_file), 0,
1299 SLAB_HWCACHE_ALIGN, NULL);
1300 if (!filp_cache)
1301 goto out;
1302
1303 init_waitqueue_head(&dh_wq);
1304
1305 return 0;
1306
1307 out:
1308 pr_err("failed to allocate file cache\n");
1309 return -ENOMEM;
1310 }
1311
1312 void ksmbd_exit_file_cache(void)
1313 {
1314 kmem_cache_destroy(filp_cache);
1315 }