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