1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2019 Samsung Electronics Co., Ltd.
4  */
5 
6 #include <linux/list.h>
7 #include <linux/mm.h>
8 #include <linux/slab.h>
9 #include <linux/workqueue.h>
10 
11 #include "server.h"
12 #include "connection.h"
13 #include "ksmbd_work.h"
14 #include "mgmt/ksmbd_ida.h"
15 
16 static struct kmem_cache *work_cache;
17 static struct workqueue_struct *ksmbd_wq;
18 
ksmbd_alloc_work_struct(void)19 struct ksmbd_work *ksmbd_alloc_work_struct(void)
20 {
21 	struct ksmbd_work *work = kmem_cache_zalloc(work_cache, GFP_KERNEL);
22 
23 	if (work) {
24 		work->compound_fid = KSMBD_NO_FID;
25 		work->compound_pfid = KSMBD_NO_FID;
26 		INIT_LIST_HEAD(&work->request_entry);
27 		INIT_LIST_HEAD(&work->async_request_entry);
28 		INIT_LIST_HEAD(&work->fp_entry);
29 		INIT_LIST_HEAD(&work->interim_entry);
30 	}
31 	return work;
32 }
33 
ksmbd_free_work_struct(struct ksmbd_work * work)34 void ksmbd_free_work_struct(struct ksmbd_work *work)
35 {
36 	WARN_ON(work->saved_cred != NULL);
37 
38 	kvfree(work->response_buf);
39 	kvfree(work->aux_payload_buf);
40 	kfree(work->tr_buf);
41 	kvfree(work->request_buf);
42 	if (work->async_id)
43 		ksmbd_release_id(&work->conn->async_ida, work->async_id);
44 	kmem_cache_free(work_cache, work);
45 }
46 
ksmbd_work_pool_destroy(void)47 void ksmbd_work_pool_destroy(void)
48 {
49 	kmem_cache_destroy(work_cache);
50 }
51 
ksmbd_work_pool_init(void)52 int ksmbd_work_pool_init(void)
53 {
54 	work_cache = kmem_cache_create("ksmbd_work_cache",
55 				       sizeof(struct ksmbd_work), 0,
56 				       SLAB_HWCACHE_ALIGN, NULL);
57 	if (!work_cache)
58 		return -ENOMEM;
59 	return 0;
60 }
61 
ksmbd_workqueue_init(void)62 int ksmbd_workqueue_init(void)
63 {
64 	ksmbd_wq = alloc_workqueue("ksmbd-io", 0, 0);
65 	if (!ksmbd_wq)
66 		return -ENOMEM;
67 	return 0;
68 }
69 
ksmbd_workqueue_destroy(void)70 void ksmbd_workqueue_destroy(void)
71 {
72 	destroy_workqueue(ksmbd_wq);
73 	ksmbd_wq = NULL;
74 }
75 
ksmbd_queue_work(struct ksmbd_work * work)76 bool ksmbd_queue_work(struct ksmbd_work *work)
77 {
78 	return queue_work(ksmbd_wq, &work->work);
79 }
80