Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2016 Namjae Jeon <namjae.jeon@protocolfreedom.org> |
| 4 | * Copyright (C) 2018 Samsung Electronics Co., Ltd. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/mutex.h> |
| 8 | #include <linux/freezer.h> |
| 9 | #include <linux/module.h> |
| 10 | |
| 11 | #include "server.h" |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 12 | #include "smb_common.h" |
| 13 | #include "mgmt/ksmbd_ida.h" |
| 14 | #include "connection.h" |
| 15 | #include "transport_tcp.h" |
| 16 | #include "transport_rdma.h" |
| 17 | |
| 18 | static DEFINE_MUTEX(init_lock); |
| 19 | |
| 20 | static struct ksmbd_conn_ops default_conn_ops; |
| 21 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 22 | LIST_HEAD(conn_list); |
| 23 | DEFINE_RWLOCK(conn_list_lock); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * ksmbd_conn_free() - free resources of the connection instance |
| 27 | * |
| 28 | * @conn: connection instance to be cleand up |
| 29 | * |
| 30 | * During the thread termination, the corresponding conn instance |
| 31 | * resources(sock/memory) are released and finally the conn object is freed. |
| 32 | */ |
| 33 | void ksmbd_conn_free(struct ksmbd_conn *conn) |
| 34 | { |
| 35 | write_lock(&conn_list_lock); |
| 36 | list_del(&conn->conns_list); |
| 37 | write_unlock(&conn_list_lock); |
| 38 | |
Namjae Jeon | e4d3e6b | 2022-07-22 10:15:10 +0900 | [diff] [blame] | 39 | xa_destroy(&conn->sessions); |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 40 | kvfree(conn->request_buf); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 41 | kfree(conn->preauth_info); |
| 42 | kfree(conn); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * ksmbd_conn_alloc() - initialize a new connection instance |
| 47 | * |
| 48 | * Return: ksmbd_conn struct on success, otherwise NULL |
| 49 | */ |
| 50 | struct ksmbd_conn *ksmbd_conn_alloc(void) |
| 51 | { |
| 52 | struct ksmbd_conn *conn; |
| 53 | |
| 54 | conn = kzalloc(sizeof(struct ksmbd_conn), GFP_KERNEL); |
| 55 | if (!conn) |
| 56 | return NULL; |
| 57 | |
| 58 | conn->need_neg = true; |
| 59 | conn->status = KSMBD_SESS_NEW; |
| 60 | conn->local_nls = load_nls("utf8"); |
| 61 | if (!conn->local_nls) |
| 62 | conn->local_nls = load_nls_default(); |
Atte Heikkilä | 16b5f54 | 2022-09-15 22:49:11 +0900 | [diff] [blame] | 63 | if (IS_ENABLED(CONFIG_UNICODE)) |
| 64 | conn->um = utf8_load(UNICODE_AGE(12, 1, 0)); |
| 65 | else |
| 66 | conn->um = ERR_PTR(-EOPNOTSUPP); |
| 67 | if (IS_ERR(conn->um)) |
| 68 | conn->um = NULL; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 69 | atomic_set(&conn->req_running, 0); |
| 70 | atomic_set(&conn->r_count, 0); |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 71 | conn->total_credits = 1; |
Hyunchul Lee | 376b913 | 2022-05-20 14:35:47 +0900 | [diff] [blame] | 72 | conn->outstanding_credits = 0; |
Hyunchul Lee | bf8acc9 | 2021-10-07 16:26:58 +0900 | [diff] [blame] | 73 | |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 74 | init_waitqueue_head(&conn->req_running_q); |
Namjae Jeon | a14c573 | 2022-07-28 23:35:18 +0900 | [diff] [blame] | 75 | init_waitqueue_head(&conn->r_count_q); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 76 | INIT_LIST_HEAD(&conn->conns_list); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 77 | INIT_LIST_HEAD(&conn->requests); |
| 78 | INIT_LIST_HEAD(&conn->async_requests); |
| 79 | spin_lock_init(&conn->request_lock); |
| 80 | spin_lock_init(&conn->credits_lock); |
Namjae Jeon | d40012a | 2021-04-13 13:06:30 +0900 | [diff] [blame] | 81 | ida_init(&conn->async_ida); |
Namjae Jeon | e4d3e6b | 2022-07-22 10:15:10 +0900 | [diff] [blame] | 82 | xa_init(&conn->sessions); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 83 | |
Hyunchul Lee | d63528e | 2021-07-10 16:22:41 +0900 | [diff] [blame] | 84 | spin_lock_init(&conn->llist_lock); |
| 85 | INIT_LIST_HEAD(&conn->lock_list); |
| 86 | |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 87 | write_lock(&conn_list_lock); |
| 88 | list_add(&conn->conns_list, &conn_list); |
| 89 | write_unlock(&conn_list_lock); |
| 90 | return conn; |
| 91 | } |
| 92 | |
| 93 | bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c) |
| 94 | { |
| 95 | struct ksmbd_conn *t; |
| 96 | bool ret = false; |
| 97 | |
| 98 | read_lock(&conn_list_lock); |
| 99 | list_for_each_entry(t, &conn_list, conns_list) { |
| 100 | if (memcmp(t->ClientGUID, c->ClientGUID, SMB2_CLIENT_GUID_SIZE)) |
| 101 | continue; |
| 102 | |
| 103 | ret = true; |
| 104 | break; |
| 105 | } |
| 106 | read_unlock(&conn_list_lock); |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | void ksmbd_conn_enqueue_request(struct ksmbd_work *work) |
| 111 | { |
| 112 | struct ksmbd_conn *conn = work->conn; |
| 113 | struct list_head *requests_queue = NULL; |
| 114 | |
| 115 | if (conn->ops->get_cmd_val(work) != SMB2_CANCEL_HE) { |
| 116 | requests_queue = &conn->requests; |
Dawei Li | f8d6e74 | 2023-01-15 18:32:09 +0800 | [diff] [blame] | 117 | work->synchronous = true; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | if (requests_queue) { |
| 121 | atomic_inc(&conn->req_running); |
| 122 | spin_lock(&conn->request_lock); |
| 123 | list_add_tail(&work->request_entry, requests_queue); |
| 124 | spin_unlock(&conn->request_lock); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | int ksmbd_conn_try_dequeue_request(struct ksmbd_work *work) |
| 129 | { |
| 130 | struct ksmbd_conn *conn = work->conn; |
| 131 | int ret = 1; |
| 132 | |
| 133 | if (list_empty(&work->request_entry) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 134 | list_empty(&work->async_request_entry)) |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 135 | return 0; |
| 136 | |
Hyunchul Lee | 4b92841 | 2021-07-10 09:31:08 +0900 | [diff] [blame] | 137 | if (!work->multiRsp) |
| 138 | atomic_dec(&conn->req_running); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 139 | spin_lock(&conn->request_lock); |
| 140 | if (!work->multiRsp) { |
| 141 | list_del_init(&work->request_entry); |
Dawei Li | f8d6e74 | 2023-01-15 18:32:09 +0800 | [diff] [blame] | 142 | if (!work->synchronous) |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 143 | list_del_init(&work->async_request_entry); |
| 144 | ret = 0; |
| 145 | } |
| 146 | spin_unlock(&conn->request_lock); |
| 147 | |
| 148 | wake_up_all(&conn->req_running_q); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | static void ksmbd_conn_lock(struct ksmbd_conn *conn) |
| 153 | { |
| 154 | mutex_lock(&conn->srv_mutex); |
| 155 | } |
| 156 | |
| 157 | static void ksmbd_conn_unlock(struct ksmbd_conn *conn) |
| 158 | { |
| 159 | mutex_unlock(&conn->srv_mutex); |
| 160 | } |
| 161 | |
| 162 | void ksmbd_conn_wait_idle(struct ksmbd_conn *conn) |
| 163 | { |
| 164 | wait_event(conn->req_running_q, atomic_read(&conn->req_running) < 2); |
| 165 | } |
| 166 | |
| 167 | int ksmbd_conn_write(struct ksmbd_work *work) |
| 168 | { |
| 169 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 170 | size_t len = 0; |
| 171 | int sent; |
| 172 | struct kvec iov[3]; |
| 173 | int iov_idx = 0; |
| 174 | |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 175 | if (!work->response_buf) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 176 | pr_err("NULL response header\n"); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 177 | return -EINVAL; |
| 178 | } |
| 179 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 180 | if (work->tr_buf) { |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 181 | iov[iov_idx] = (struct kvec) { work->tr_buf, |
Namjae Jeon | 2dd9129f | 2021-11-03 08:25:54 +0900 | [diff] [blame] | 182 | sizeof(struct smb2_transform_hdr) + 4 }; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 183 | len += iov[iov_idx++].iov_len; |
| 184 | } |
| 185 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 186 | if (work->aux_payload_sz) { |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 187 | iov[iov_idx] = (struct kvec) { work->response_buf, work->resp_hdr_sz }; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 188 | len += iov[iov_idx++].iov_len; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 189 | iov[iov_idx] = (struct kvec) { work->aux_payload_buf, work->aux_payload_sz }; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 190 | len += iov[iov_idx++].iov_len; |
| 191 | } else { |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 192 | if (work->tr_buf) |
| 193 | iov[iov_idx].iov_len = work->resp_hdr_sz; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 194 | else |
Namjae Jeon | cb45172 | 2021-11-03 08:08:44 +0900 | [diff] [blame] | 195 | iov[iov_idx].iov_len = get_rfc1002_len(work->response_buf) + 4; |
| 196 | iov[iov_idx].iov_base = work->response_buf; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 197 | len += iov[iov_idx++].iov_len; |
| 198 | } |
| 199 | |
| 200 | ksmbd_conn_lock(conn); |
| 201 | sent = conn->transport->ops->writev(conn->transport, &iov[0], |
| 202 | iov_idx, len, |
| 203 | work->need_invalidate_rkey, |
| 204 | work->remote_key); |
| 205 | ksmbd_conn_unlock(conn); |
| 206 | |
| 207 | if (sent < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 208 | pr_err("Failed to send message: %d\n", sent); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 209 | return sent; |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Hyunchul Lee | 1807abc | 2022-04-30 08:30:25 +0900 | [diff] [blame] | 215 | int ksmbd_conn_rdma_read(struct ksmbd_conn *conn, |
| 216 | void *buf, unsigned int buflen, |
| 217 | struct smb2_buffer_desc_v1 *desc, |
| 218 | unsigned int desc_len) |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 219 | { |
| 220 | int ret = -EINVAL; |
| 221 | |
| 222 | if (conn->transport->ops->rdma_read) |
| 223 | ret = conn->transport->ops->rdma_read(conn->transport, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 224 | buf, buflen, |
Hyunchul Lee | 1807abc | 2022-04-30 08:30:25 +0900 | [diff] [blame] | 225 | desc, desc_len); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 226 | return ret; |
| 227 | } |
| 228 | |
Hyunchul Lee | 1807abc | 2022-04-30 08:30:25 +0900 | [diff] [blame] | 229 | int ksmbd_conn_rdma_write(struct ksmbd_conn *conn, |
| 230 | void *buf, unsigned int buflen, |
| 231 | struct smb2_buffer_desc_v1 *desc, |
| 232 | unsigned int desc_len) |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 233 | { |
| 234 | int ret = -EINVAL; |
| 235 | |
| 236 | if (conn->transport->ops->rdma_write) |
| 237 | ret = conn->transport->ops->rdma_write(conn->transport, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 238 | buf, buflen, |
Hyunchul Lee | 1807abc | 2022-04-30 08:30:25 +0900 | [diff] [blame] | 239 | desc, desc_len); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | bool ksmbd_conn_alive(struct ksmbd_conn *conn) |
| 244 | { |
| 245 | if (!ksmbd_server_running()) |
| 246 | return false; |
| 247 | |
| 248 | if (conn->status == KSMBD_SESS_EXITING) |
| 249 | return false; |
| 250 | |
| 251 | if (kthread_should_stop()) |
| 252 | return false; |
| 253 | |
| 254 | if (atomic_read(&conn->stats.open_files_count) > 0) |
| 255 | return true; |
| 256 | |
| 257 | /* |
| 258 | * Stop current session if the time that get last request from client |
Namjae Jeon | a9c241d | 2021-07-15 10:22:20 +0900 | [diff] [blame] | 259 | * is bigger than deadtime user configured and opening file count is |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 260 | * zero. |
| 261 | */ |
| 262 | if (server_conf.deadtime > 0 && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 263 | time_after(jiffies, conn->last_active + server_conf.deadtime)) { |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 264 | ksmbd_debug(CONN, "No response from client in %lu minutes\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 265 | server_conf.deadtime / SMB_ECHO_INTERVAL); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 266 | return false; |
| 267 | } |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * ksmbd_conn_handler_loop() - session thread to listen on new smb requests |
| 273 | * @p: connection instance |
| 274 | * |
| 275 | * One thread each per connection |
| 276 | * |
| 277 | * Return: 0 on success |
| 278 | */ |
| 279 | int ksmbd_conn_handler_loop(void *p) |
| 280 | { |
| 281 | struct ksmbd_conn *conn = (struct ksmbd_conn *)p; |
| 282 | struct ksmbd_transport *t = conn->transport; |
Namjae Jeon | 62c487b | 2023-01-25 00:13:20 +0900 | [diff] [blame] | 283 | unsigned int pdu_size, max_allowed_pdu_size; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 284 | char hdr_buf[4] = {0,}; |
| 285 | int size; |
| 286 | |
| 287 | mutex_init(&conn->srv_mutex); |
| 288 | __module_get(THIS_MODULE); |
| 289 | |
| 290 | if (t->ops->prepare && t->ops->prepare(t)) |
| 291 | goto out; |
| 292 | |
| 293 | conn->last_active = jiffies; |
| 294 | while (ksmbd_conn_alive(conn)) { |
| 295 | if (try_to_freeze()) |
| 296 | continue; |
| 297 | |
Namjae Jeon | 79f6b11 | 2021-04-02 12:47:14 +0900 | [diff] [blame] | 298 | kvfree(conn->request_buf); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 299 | conn->request_buf = NULL; |
| 300 | |
| 301 | size = t->ops->read(t, hdr_buf, sizeof(hdr_buf)); |
| 302 | if (size != sizeof(hdr_buf)) |
| 303 | break; |
| 304 | |
| 305 | pdu_size = get_rfc1002_len(hdr_buf); |
| 306 | ksmbd_debug(CONN, "RFC1002 header %u bytes\n", pdu_size); |
| 307 | |
Namjae Jeon | 62c487b | 2023-01-25 00:13:20 +0900 | [diff] [blame] | 308 | if (conn->status == KSMBD_SESS_GOOD) |
| 309 | max_allowed_pdu_size = |
| 310 | SMB3_MAX_MSGSIZE + conn->vals->max_write_size; |
| 311 | else |
| 312 | max_allowed_pdu_size = SMB3_MAX_MSGSIZE; |
| 313 | |
| 314 | if (pdu_size > max_allowed_pdu_size) { |
Colin Ian King | 7a17c61 | 2023-01-30 09:23:57 +0000 | [diff] [blame] | 315 | pr_err_ratelimited("PDU length(%u) exceeded maximum allowed pdu size(%u) on connection(%d)\n", |
Namjae Jeon | 62c487b | 2023-01-25 00:13:20 +0900 | [diff] [blame] | 316 | pdu_size, max_allowed_pdu_size, |
| 317 | conn->status); |
| 318 | break; |
| 319 | } |
| 320 | |
Namjae Jeon | 3639999 | 2021-09-24 09:17:30 +0900 | [diff] [blame] | 321 | /* |
| 322 | * Check if pdu size is valid (min : smb header size, |
| 323 | * max : 0x00FFFFFF). |
| 324 | */ |
| 325 | if (pdu_size < __SMB2_HEADER_STRUCTURE_SIZE || |
| 326 | pdu_size > MAX_STREAM_PROT_LEN) { |
Namjae Jeon | 62c487b | 2023-01-25 00:13:20 +0900 | [diff] [blame] | 327 | break; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* 4 for rfc1002 length field */ |
| 331 | size = pdu_size + 4; |
Namjae Jeon | 83dcedd | 2022-12-31 17:32:31 +0900 | [diff] [blame] | 332 | conn->request_buf = kvmalloc(size, |
| 333 | GFP_KERNEL | |
| 334 | __GFP_NOWARN | |
| 335 | __GFP_NORETRY); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 336 | if (!conn->request_buf) |
Namjae Jeon | 83dcedd | 2022-12-31 17:32:31 +0900 | [diff] [blame] | 337 | break; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 338 | |
| 339 | memcpy(conn->request_buf, hdr_buf, sizeof(hdr_buf)); |
| 340 | if (!ksmbd_smb_request(conn)) |
| 341 | break; |
| 342 | |
| 343 | /* |
| 344 | * We already read 4 bytes to find out PDU size, now |
| 345 | * read in PDU |
| 346 | */ |
| 347 | size = t->ops->read(t, conn->request_buf + 4, pdu_size); |
| 348 | if (size < 0) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 349 | pr_err("sock_read failed: %d\n", size); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | |
| 353 | if (size != pdu_size) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 354 | pr_err("PDU error. Read: %d, Expected: %d\n", |
| 355 | size, pdu_size); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 356 | continue; |
| 357 | } |
| 358 | |
| 359 | if (!default_conn_ops.process_fn) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 360 | pr_err("No connection request callback\n"); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 361 | break; |
| 362 | } |
| 363 | |
| 364 | if (default_conn_ops.process_fn(conn)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 365 | pr_err("Cannot handle request\n"); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 366 | break; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | out: |
| 371 | /* Wait till all reference dropped to the Server object*/ |
Namjae Jeon | a14c573 | 2022-07-28 23:35:18 +0900 | [diff] [blame] | 372 | wait_event(conn->r_count_q, atomic_read(&conn->r_count) == 0); |
| 373 | |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 374 | |
Atte Heikkilä | 16b5f54 | 2022-09-15 22:49:11 +0900 | [diff] [blame] | 375 | if (IS_ENABLED(CONFIG_UNICODE)) |
| 376 | utf8_unload(conn->um); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 377 | unload_nls(conn->local_nls); |
| 378 | if (default_conn_ops.terminate_fn) |
| 379 | default_conn_ops.terminate_fn(conn); |
| 380 | t->ops->disconnect(t); |
| 381 | module_put(THIS_MODULE); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops) |
| 386 | { |
| 387 | default_conn_ops.process_fn = ops->process_fn; |
| 388 | default_conn_ops.terminate_fn = ops->terminate_fn; |
| 389 | } |
| 390 | |
| 391 | int ksmbd_conn_transport_init(void) |
| 392 | { |
| 393 | int ret; |
| 394 | |
| 395 | mutex_lock(&init_lock); |
| 396 | ret = ksmbd_tcp_init(); |
| 397 | if (ret) { |
| 398 | pr_err("Failed to init TCP subsystem: %d\n", ret); |
| 399 | goto out; |
| 400 | } |
| 401 | |
| 402 | ret = ksmbd_rdma_init(); |
| 403 | if (ret) { |
Hyunchul Lee | 0a427cc | 2021-07-13 15:38:30 +0900 | [diff] [blame] | 404 | pr_err("Failed to init RDMA subsystem: %d\n", ret); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 405 | goto out; |
| 406 | } |
| 407 | out: |
| 408 | mutex_unlock(&init_lock); |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | static void stop_sessions(void) |
| 413 | { |
| 414 | struct ksmbd_conn *conn; |
Yufan Chen | 136dff3 | 2022-01-09 11:34:16 +0900 | [diff] [blame] | 415 | struct ksmbd_transport *t; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 416 | |
| 417 | again: |
| 418 | read_lock(&conn_list_lock); |
| 419 | list_for_each_entry(conn, &conn_list, conns_list) { |
| 420 | struct task_struct *task; |
| 421 | |
Yufan Chen | 136dff3 | 2022-01-09 11:34:16 +0900 | [diff] [blame] | 422 | t = conn->transport; |
| 423 | task = t->handler; |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 424 | if (task) |
| 425 | ksmbd_debug(CONN, "Stop session handler %s/%d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 426 | task->comm, task_pid_nr(task)); |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 427 | conn->status = KSMBD_SESS_EXITING; |
Yufan Chen | 136dff3 | 2022-01-09 11:34:16 +0900 | [diff] [blame] | 428 | if (t->ops->shutdown) { |
| 429 | read_unlock(&conn_list_lock); |
| 430 | t->ops->shutdown(t); |
| 431 | read_lock(&conn_list_lock); |
| 432 | } |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 433 | } |
| 434 | read_unlock(&conn_list_lock); |
| 435 | |
| 436 | if (!list_empty(&conn_list)) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 437 | schedule_timeout_interruptible(HZ / 10); /* 100ms */ |
Namjae Jeon | 0626e66 | 2021-03-16 13:07:11 +0900 | [diff] [blame] | 438 | goto again; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | void ksmbd_conn_transport_destroy(void) |
| 443 | { |
| 444 | mutex_lock(&init_lock); |
| 445 | ksmbd_tcp_destroy(); |
| 446 | ksmbd_rdma_destroy(); |
| 447 | stop_sessions(); |
| 448 | mutex_unlock(&init_lock); |
| 449 | } |