xref: /linux/fs/smb/server/ksmbd_work.c (revision 08f641e2e2e092cbda5ce7f7b5280e327e46823d)
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 
19 struct ksmbd_work *ksmbd_alloc_work_struct(void)
20 {
21 	struct ksmbd_work *work = kmem_cache_zalloc(work_cache, KSMBD_DEFAULT_GFP);
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->aux_read_list);
30 		work->iov_alloc_cnt = 4;
31 		work->iov = kzalloc_objs(struct kvec, work->iov_alloc_cnt,
32 					 KSMBD_DEFAULT_GFP);
33 		if (!work->iov) {
34 			kmem_cache_free(work_cache, work);
35 			work = NULL;
36 		}
37 	}
38 	return work;
39 }
40 
41 void ksmbd_free_work_struct(struct ksmbd_work *work)
42 {
43 	struct aux_read *ar, *tmp;
44 
45 	WARN_ON(work->saved_cred != NULL);
46 
47 	kvfree(work->response_buf);
48 
49 	list_for_each_entry_safe(ar, tmp, &work->aux_read_list, entry) {
50 		kvfree(ar->buf);
51 		list_del(&ar->entry);
52 		kfree(ar);
53 	}
54 
55 	kfree(work->tr_buf);
56 	kvfree(work->compress_buf);
57 	kvfree(work->request_buf);
58 	kfree(work->iov);
59 
60 	if (work->async_id)
61 		ksmbd_release_id(&work->conn->async_ida, work->async_id);
62 	kmem_cache_free(work_cache, work);
63 }
64 
65 void ksmbd_work_pool_destroy(void)
66 {
67 	kmem_cache_destroy(work_cache);
68 }
69 
70 int ksmbd_work_pool_init(void)
71 {
72 	work_cache = kmem_cache_create("ksmbd_work_cache",
73 				       sizeof(struct ksmbd_work), 0,
74 				       SLAB_HWCACHE_ALIGN, NULL);
75 	if (!work_cache)
76 		return -ENOMEM;
77 	return 0;
78 }
79 
80 int ksmbd_workqueue_init(void)
81 {
82 	ksmbd_wq = alloc_workqueue("ksmbd-io", WQ_PERCPU, 0);
83 	if (!ksmbd_wq)
84 		return -ENOMEM;
85 	return 0;
86 }
87 
88 void ksmbd_workqueue_destroy(void)
89 {
90 	destroy_workqueue(ksmbd_wq);
91 	ksmbd_wq = NULL;
92 }
93 
94 bool ksmbd_queue_work(struct ksmbd_work *work)
95 {
96 	return queue_work(ksmbd_wq, &work->work);
97 }
98 
99 static inline void __ksmbd_iov_pin(struct ksmbd_work *work, void *ib,
100 				   unsigned int ib_len)
101 {
102 	work->iov[++work->iov_idx].iov_base = ib;
103 	work->iov[work->iov_idx].iov_len = ib_len;
104 	work->iov_cnt++;
105 }
106 
107 static int __ksmbd_iov_pin_rsp(struct ksmbd_work *work, void *ib, int len,
108 			       void *aux_buf, unsigned int aux_size)
109 {
110 	struct aux_read *ar = NULL;
111 	int need_iov_cnt = 1;
112 
113 	if (aux_size) {
114 		need_iov_cnt++;
115 		ar = kmalloc_obj(struct aux_read, KSMBD_DEFAULT_GFP);
116 		if (!ar)
117 			return -ENOMEM;
118 	}
119 
120 	if (work->iov_alloc_cnt < work->iov_cnt + need_iov_cnt) {
121 		struct kvec *new;
122 
123 		work->iov_alloc_cnt += 4;
124 		new = krealloc(work->iov,
125 			       sizeof(struct kvec) * work->iov_alloc_cnt,
126 			       KSMBD_DEFAULT_GFP | __GFP_ZERO);
127 		if (!new) {
128 			kfree(ar);
129 			work->iov_alloc_cnt -= 4;
130 			return -ENOMEM;
131 		}
132 		work->iov = new;
133 	}
134 
135 	/* Plus rfc_length size on first iov */
136 	if (!work->iov_idx) {
137 		work->iov[work->iov_idx].iov_base = work->response_buf;
138 		*(__be32 *)work->iov[0].iov_base = 0;
139 		work->iov[work->iov_idx].iov_len = 4;
140 		work->iov_cnt++;
141 	}
142 
143 	__ksmbd_iov_pin(work, ib, len);
144 	inc_rfc1001_len(work->iov[0].iov_base, len);
145 
146 	if (aux_size) {
147 		__ksmbd_iov_pin(work, aux_buf, aux_size);
148 		inc_rfc1001_len(work->iov[0].iov_base, aux_size);
149 
150 		ar->buf = aux_buf;
151 		list_add(&ar->entry, &work->aux_read_list);
152 	}
153 
154 	return 0;
155 }
156 
157 int ksmbd_iov_pin_rsp(struct ksmbd_work *work, void *ib, int len)
158 {
159 	return __ksmbd_iov_pin_rsp(work, ib, len, NULL, 0);
160 }
161 
162 int ksmbd_iov_pin_rsp_read(struct ksmbd_work *work, void *ib, int len,
163 			   void *aux_buf, unsigned int aux_size)
164 {
165 	return __ksmbd_iov_pin_rsp(work, ib, len, aux_buf, aux_size);
166 }
167 
168 int allocate_interim_rsp_buf(struct ksmbd_work *work)
169 {
170 	work->response_buf = kzalloc(MAX_CIFS_SMALL_BUFFER_SIZE, KSMBD_DEFAULT_GFP);
171 	if (!work->response_buf)
172 		return -ENOMEM;
173 	work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
174 	return 0;
175 }
176