1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2019 Samsung Electronics Co., Ltd.
8 #include <linux/slab.h>
9 #include <linux/vmalloc.h>
12 #include "vfs_cache.h"
15 #include "connection.h"
16 #include "mgmt/tree_connect.h"
17 #include "mgmt/user_session.h"
18 #include "smb_common.h"
20 #define S_DEL_PENDING 1
21 #define S_DEL_ON_CLS 2
22 #define S_DEL_ON_CLS_STREAM 8
24 static unsigned int inode_hash_mask __read_mostly;
25 static unsigned int inode_hash_shift __read_mostly;
26 static struct hlist_head *inode_hashtable __read_mostly;
27 static DEFINE_RWLOCK(inode_hash_lock);
29 static struct ksmbd_file_table global_ft;
30 static atomic_long_t fd_limit;
31 static struct kmem_cache *filp_cache;
33 void ksmbd_set_fd_limit(unsigned long limit)
35 limit = min(limit, get_max_files());
36 atomic_long_set(&fd_limit, limit);
39 static bool fd_limit_depleted(void)
41 long v = atomic_long_dec_return(&fd_limit);
45 atomic_long_inc(&fd_limit);
49 static void fd_limit_close(void)
51 atomic_long_inc(&fd_limit);
58 static unsigned long inode_hash(struct super_block *sb, unsigned long hashval)
62 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
64 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> inode_hash_shift);
65 return tmp & inode_hash_mask;
68 static struct ksmbd_inode *__ksmbd_inode_lookup(struct dentry *de)
70 struct hlist_head *head = inode_hashtable +
71 inode_hash(d_inode(de)->i_sb, (unsigned long)de);
72 struct ksmbd_inode *ci = NULL, *ret_ci = NULL;
74 hlist_for_each_entry(ci, head, m_hash) {
76 if (atomic_inc_not_zero(&ci->m_count))
84 static struct ksmbd_inode *ksmbd_inode_lookup(struct ksmbd_file *fp)
86 return __ksmbd_inode_lookup(fp->filp->f_path.dentry);
89 struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d)
91 struct ksmbd_inode *ci;
93 read_lock(&inode_hash_lock);
94 ci = __ksmbd_inode_lookup(d);
95 read_unlock(&inode_hash_lock);
100 int ksmbd_query_inode_status(struct dentry *dentry)
102 struct ksmbd_inode *ci;
103 int ret = KSMBD_INODE_STATUS_UNKNOWN;
105 read_lock(&inode_hash_lock);
106 ci = __ksmbd_inode_lookup(dentry);
108 ret = KSMBD_INODE_STATUS_OK;
109 if (ci->m_flags & (S_DEL_PENDING | S_DEL_ON_CLS))
110 ret = KSMBD_INODE_STATUS_PENDING_DELETE;
111 atomic_dec(&ci->m_count);
113 read_unlock(&inode_hash_lock);
117 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp)
119 return (fp->f_ci->m_flags & (S_DEL_PENDING | S_DEL_ON_CLS));
122 void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp)
124 fp->f_ci->m_flags |= S_DEL_PENDING;
127 void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp)
129 fp->f_ci->m_flags &= ~S_DEL_PENDING;
132 void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp,
135 if (ksmbd_stream_fd(fp)) {
136 fp->f_ci->m_flags |= S_DEL_ON_CLS_STREAM;
140 fp->f_ci->m_flags |= S_DEL_ON_CLS;
143 static void ksmbd_inode_hash(struct ksmbd_inode *ci)
145 struct hlist_head *b = inode_hashtable +
146 inode_hash(d_inode(ci->m_de)->i_sb, (unsigned long)ci->m_de);
148 hlist_add_head(&ci->m_hash, b);
151 static void ksmbd_inode_unhash(struct ksmbd_inode *ci)
153 write_lock(&inode_hash_lock);
154 hlist_del_init(&ci->m_hash);
155 write_unlock(&inode_hash_lock);
158 static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp)
160 atomic_set(&ci->m_count, 1);
161 atomic_set(&ci->op_count, 0);
162 atomic_set(&ci->sop_count, 0);
165 INIT_LIST_HEAD(&ci->m_fp_list);
166 INIT_LIST_HEAD(&ci->m_op_list);
167 rwlock_init(&ci->m_lock);
168 ci->m_de = fp->filp->f_path.dentry;
172 static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp)
174 struct ksmbd_inode *ci, *tmpci;
177 read_lock(&inode_hash_lock);
178 ci = ksmbd_inode_lookup(fp);
179 read_unlock(&inode_hash_lock);
183 ci = kmalloc(sizeof(struct ksmbd_inode), GFP_KERNEL);
187 rc = ksmbd_inode_init(ci, fp);
189 pr_err("inode initialized failed\n");
194 write_lock(&inode_hash_lock);
195 tmpci = ksmbd_inode_lookup(fp);
197 ksmbd_inode_hash(ci);
202 write_unlock(&inode_hash_lock);
206 static void ksmbd_inode_free(struct ksmbd_inode *ci)
208 ksmbd_inode_unhash(ci);
212 void ksmbd_inode_put(struct ksmbd_inode *ci)
214 if (atomic_dec_and_test(&ci->m_count))
215 ksmbd_inode_free(ci);
218 int __init ksmbd_inode_hash_init(void)
221 unsigned long numentries = 16384;
222 unsigned long bucketsize = sizeof(struct hlist_head);
225 inode_hash_shift = ilog2(numentries);
226 inode_hash_mask = (1 << inode_hash_shift) - 1;
228 size = bucketsize << inode_hash_shift;
230 /* init master fp hash table */
231 inode_hashtable = vmalloc(size);
232 if (!inode_hashtable)
235 for (loop = 0; loop < (1U << inode_hash_shift); loop++)
236 INIT_HLIST_HEAD(&inode_hashtable[loop]);
240 void ksmbd_release_inode_hash(void)
242 vfree(inode_hashtable);
245 static void __ksmbd_inode_close(struct ksmbd_file *fp)
247 struct ksmbd_inode *ci = fp->f_ci;
252 if (ksmbd_stream_fd(fp) && (ci->m_flags & S_DEL_ON_CLS_STREAM)) {
253 ci->m_flags &= ~S_DEL_ON_CLS_STREAM;
254 err = ksmbd_vfs_remove_xattr(file_mnt_user_ns(filp),
258 pr_err("remove xattr failed : %s\n",
262 if (atomic_dec_and_test(&ci->m_count)) {
263 write_lock(&ci->m_lock);
264 if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_PENDING)) {
265 ci->m_flags &= ~(S_DEL_ON_CLS | S_DEL_PENDING);
266 write_unlock(&ci->m_lock);
267 ksmbd_vfs_unlink(filp);
268 write_lock(&ci->m_lock);
270 write_unlock(&ci->m_lock);
272 ksmbd_inode_free(ci);
276 static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp)
278 if (!has_file_id(fp->persistent_id))
281 write_lock(&global_ft.lock);
282 idr_remove(global_ft.idr, fp->persistent_id);
283 write_unlock(&global_ft.lock);
286 static void __ksmbd_remove_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
288 if (!has_file_id(fp->volatile_id))
291 write_lock(&fp->f_ci->m_lock);
292 list_del_init(&fp->node);
293 write_unlock(&fp->f_ci->m_lock);
295 write_lock(&ft->lock);
296 idr_remove(ft->idr, fp->volatile_id);
297 write_unlock(&ft->lock);
300 static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
303 struct ksmbd_lock *smb_lock, *tmp_lock;
306 __ksmbd_remove_durable_fd(fp);
307 __ksmbd_remove_fd(ft, fp);
309 close_id_del_oplock(fp);
312 __ksmbd_inode_close(fp);
313 if (!IS_ERR_OR_NULL(filp))
316 /* because the reference count of fp is 0, it is guaranteed that
317 * there are not accesses to fp->lock_list.
319 list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
320 spin_lock(&fp->conn->llist_lock);
321 list_del(&smb_lock->clist);
322 spin_unlock(&fp->conn->llist_lock);
324 list_del(&smb_lock->flist);
325 locks_free_lock(smb_lock->fl);
329 if (ksmbd_stream_fd(fp))
330 kfree(fp->stream.name);
331 kmem_cache_free(filp_cache, fp);
334 static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp)
336 if (fp->f_state != FP_INITED)
339 if (!atomic_inc_not_zero(&fp->refcount))
344 static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft,
347 struct ksmbd_file *fp;
349 if (!has_file_id(id))
352 read_lock(&ft->lock);
353 fp = idr_find(ft->idr, id);
355 fp = ksmbd_fp_get(fp);
356 read_unlock(&ft->lock);
360 static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp)
362 __ksmbd_close_fd(&work->sess->file_table, fp);
363 atomic_dec(&work->conn->stats.open_files_count);
366 static void set_close_state_blocked_works(struct ksmbd_file *fp)
368 struct ksmbd_work *cancel_work;
370 spin_lock(&fp->f_lock);
371 list_for_each_entry(cancel_work, &fp->blocked_works,
373 cancel_work->state = KSMBD_WORK_CLOSED;
374 cancel_work->cancel_fn(cancel_work->cancel_argv);
376 spin_unlock(&fp->f_lock);
379 int ksmbd_close_fd(struct ksmbd_work *work, u64 id)
381 struct ksmbd_file *fp;
382 struct ksmbd_file_table *ft;
384 if (!has_file_id(id))
387 ft = &work->sess->file_table;
388 write_lock(&ft->lock);
389 fp = idr_find(ft->idr, id);
391 set_close_state_blocked_works(fp);
393 if (fp->f_state != FP_INITED)
396 fp->f_state = FP_CLOSED;
397 if (!atomic_dec_and_test(&fp->refcount))
401 write_unlock(&ft->lock);
406 __put_fd_final(work, fp);
410 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp)
415 if (!atomic_dec_and_test(&fp->refcount))
417 __put_fd_final(work, fp);
420 static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp)
424 if (fp->tcon != tcon)
429 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id)
431 return __ksmbd_lookup_fd(&work->sess->file_table, id);
434 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id)
436 struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
438 if (__sanity_check(work->tcon, fp))
441 ksmbd_fd_put(work, fp);
445 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id,
448 struct ksmbd_file *fp;
450 if (!has_file_id(id)) {
451 id = work->compound_fid;
452 pid = work->compound_pfid;
455 fp = __ksmbd_lookup_fd(&work->sess->file_table, id);
456 if (!__sanity_check(work->tcon, fp)) {
457 ksmbd_fd_put(work, fp);
460 if (fp->persistent_id != pid) {
461 ksmbd_fd_put(work, fp);
467 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id)
469 return __ksmbd_lookup_fd(&global_ft, id);
472 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid)
474 struct ksmbd_file *fp = NULL;
477 read_lock(&global_ft.lock);
478 idr_for_each_entry(global_ft.idr, fp, id) {
479 if (!memcmp(fp->create_guid,
481 SMB2_CREATE_GUID_SIZE)) {
482 fp = ksmbd_fp_get(fp);
486 read_unlock(&global_ft.lock);
491 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry)
493 struct ksmbd_file *lfp;
494 struct ksmbd_inode *ci;
495 struct inode *inode = d_inode(dentry);
497 read_lock(&inode_hash_lock);
498 ci = __ksmbd_inode_lookup(dentry);
499 read_unlock(&inode_hash_lock);
503 read_lock(&ci->m_lock);
504 list_for_each_entry(lfp, &ci->m_fp_list, node) {
505 if (inode == file_inode(lfp->filp)) {
506 atomic_dec(&ci->m_count);
507 lfp = ksmbd_fp_get(lfp);
508 read_unlock(&ci->m_lock);
512 atomic_dec(&ci->m_count);
513 read_unlock(&ci->m_lock);
517 #define OPEN_ID_TYPE_VOLATILE_ID (0)
518 #define OPEN_ID_TYPE_PERSISTENT_ID (1)
520 static void __open_id_set(struct ksmbd_file *fp, u64 id, int type)
522 if (type == OPEN_ID_TYPE_VOLATILE_ID)
523 fp->volatile_id = id;
524 if (type == OPEN_ID_TYPE_PERSISTENT_ID)
525 fp->persistent_id = id;
528 static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
534 if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) {
535 __open_id_set(fp, KSMBD_NO_FID, type);
539 idr_preload(GFP_KERNEL);
540 write_lock(&ft->lock);
541 ret = idr_alloc_cyclic(ft->idr, fp, 0, INT_MAX - 1, GFP_NOWAIT);
550 __open_id_set(fp, id, type);
551 write_unlock(&ft->lock);
556 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp)
558 __open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID);
559 return fp->persistent_id;
562 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp)
564 struct ksmbd_file *fp;
567 fp = kmem_cache_zalloc(filp_cache, GFP_KERNEL);
569 pr_err("Failed to allocate memory\n");
570 return ERR_PTR(-ENOMEM);
573 INIT_LIST_HEAD(&fp->blocked_works);
574 INIT_LIST_HEAD(&fp->node);
575 INIT_LIST_HEAD(&fp->lock_list);
576 spin_lock_init(&fp->f_lock);
577 atomic_set(&fp->refcount, 1);
580 fp->conn = work->conn;
581 fp->tcon = work->tcon;
582 fp->volatile_id = KSMBD_NO_FID;
583 fp->persistent_id = KSMBD_NO_FID;
584 fp->f_state = FP_NEW;
585 fp->f_ci = ksmbd_inode_get(fp);
592 ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
594 ksmbd_inode_put(fp->f_ci);
598 atomic_inc(&work->conn->stats.open_files_count);
602 kmem_cache_free(filp_cache, fp);
606 void ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp,
612 write_lock(&ft->lock);
614 write_unlock(&ft->lock);
618 __close_file_table_ids(struct ksmbd_file_table *ft,
619 struct ksmbd_tree_connect *tcon,
620 bool (*skip)(struct ksmbd_tree_connect *tcon,
621 struct ksmbd_file *fp))
624 struct ksmbd_file *fp;
627 idr_for_each_entry(ft->idr, fp, id) {
631 set_close_state_blocked_works(fp);
633 if (!atomic_dec_and_test(&fp->refcount))
635 __ksmbd_close_fd(ft, fp);
641 static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon,
642 struct ksmbd_file *fp)
644 return fp->tcon != tcon;
647 static bool session_fd_check(struct ksmbd_tree_connect *tcon,
648 struct ksmbd_file *fp)
653 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work)
655 int num = __close_file_table_ids(&work->sess->file_table,
659 atomic_sub(num, &work->conn->stats.open_files_count);
662 void ksmbd_close_session_fds(struct ksmbd_work *work)
664 int num = __close_file_table_ids(&work->sess->file_table,
668 atomic_sub(num, &work->conn->stats.open_files_count);
671 int ksmbd_init_global_file_table(void)
673 return ksmbd_init_file_table(&global_ft);
676 void ksmbd_free_global_file_table(void)
678 struct ksmbd_file *fp = NULL;
681 idr_for_each_entry(global_ft.idr, fp, id) {
682 __ksmbd_remove_durable_fd(fp);
683 kmem_cache_free(filp_cache, fp);
686 ksmbd_destroy_file_table(&global_ft);
689 int ksmbd_init_file_table(struct ksmbd_file_table *ft)
691 ft->idr = kzalloc(sizeof(struct idr), GFP_KERNEL);
696 rwlock_init(&ft->lock);
700 void ksmbd_destroy_file_table(struct ksmbd_file_table *ft)
705 __close_file_table_ids(ft, NULL, session_fd_check);
706 idr_destroy(ft->idr);
711 int ksmbd_init_file_cache(void)
713 filp_cache = kmem_cache_create("ksmbd_file_cache",
714 sizeof(struct ksmbd_file), 0,
715 SLAB_HWCACHE_ALIGN, NULL);
722 pr_err("failed to allocate file cache\n");
726 void ksmbd_exit_file_cache(void)
728 kmem_cache_destroy(filp_cache);