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