1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Namjae Jeon <namjae.jeon@protocolfreedom.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 #include <linux/mutex.h>
8 #include <linux/freezer.h>
9 #include <linux/module.h>
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"
18 static DEFINE_MUTEX(init_lock);
20 static struct ksmbd_conn_ops default_conn_ops;
23 DEFINE_RWLOCK(conn_list_lock);
26 * ksmbd_conn_free() - free resources of the connection instance
28 * @conn: connection instance to be cleand up
30 * During the thread termination, the corresponding conn instance
31 * resources(sock/memory) are released and finally the conn object is freed.
33 void ksmbd_conn_free(struct ksmbd_conn *conn)
35 write_lock(&conn_list_lock);
36 list_del(&conn->conns_list);
37 write_unlock(&conn_list_lock);
39 kvfree(conn->request_buf);
40 kfree(conn->preauth_info);
45 * ksmbd_conn_alloc() - initialize a new connection instance
47 * Return: ksmbd_conn struct on success, otherwise NULL
49 struct ksmbd_conn *ksmbd_conn_alloc(void)
51 struct ksmbd_conn *conn;
53 conn = kzalloc(sizeof(struct ksmbd_conn), GFP_KERNEL);
57 conn->need_neg = true;
58 conn->status = KSMBD_SESS_NEW;
59 conn->local_nls = load_nls("utf8");
61 conn->local_nls = load_nls_default();
62 atomic_set(&conn->req_running, 0);
63 atomic_set(&conn->r_count, 0);
64 conn->total_credits = 1;
66 init_waitqueue_head(&conn->req_running_q);
67 INIT_LIST_HEAD(&conn->conns_list);
68 INIT_LIST_HEAD(&conn->sessions);
69 INIT_LIST_HEAD(&conn->requests);
70 INIT_LIST_HEAD(&conn->async_requests);
71 spin_lock_init(&conn->request_lock);
72 spin_lock_init(&conn->credits_lock);
73 ida_init(&conn->async_ida);
75 spin_lock_init(&conn->llist_lock);
76 INIT_LIST_HEAD(&conn->lock_list);
78 write_lock(&conn_list_lock);
79 list_add(&conn->conns_list, &conn_list);
80 write_unlock(&conn_list_lock);
84 bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c)
89 read_lock(&conn_list_lock);
90 list_for_each_entry(t, &conn_list, conns_list) {
91 if (memcmp(t->ClientGUID, c->ClientGUID, SMB2_CLIENT_GUID_SIZE))
97 read_unlock(&conn_list_lock);
101 void ksmbd_conn_enqueue_request(struct ksmbd_work *work)
103 struct ksmbd_conn *conn = work->conn;
104 struct list_head *requests_queue = NULL;
106 if (conn->ops->get_cmd_val(work) != SMB2_CANCEL_HE) {
107 requests_queue = &conn->requests;
108 work->syncronous = true;
111 if (requests_queue) {
112 atomic_inc(&conn->req_running);
113 spin_lock(&conn->request_lock);
114 list_add_tail(&work->request_entry, requests_queue);
115 spin_unlock(&conn->request_lock);
119 int ksmbd_conn_try_dequeue_request(struct ksmbd_work *work)
121 struct ksmbd_conn *conn = work->conn;
124 if (list_empty(&work->request_entry) &&
125 list_empty(&work->async_request_entry))
129 atomic_dec(&conn->req_running);
130 spin_lock(&conn->request_lock);
131 if (!work->multiRsp) {
132 list_del_init(&work->request_entry);
133 if (work->syncronous == false)
134 list_del_init(&work->async_request_entry);
137 spin_unlock(&conn->request_lock);
139 wake_up_all(&conn->req_running_q);
143 static void ksmbd_conn_lock(struct ksmbd_conn *conn)
145 mutex_lock(&conn->srv_mutex);
148 static void ksmbd_conn_unlock(struct ksmbd_conn *conn)
150 mutex_unlock(&conn->srv_mutex);
153 void ksmbd_conn_wait_idle(struct ksmbd_conn *conn)
155 wait_event(conn->req_running_q, atomic_read(&conn->req_running) < 2);
158 int ksmbd_conn_write(struct ksmbd_work *work)
160 struct ksmbd_conn *conn = work->conn;
166 ksmbd_conn_try_dequeue_request(work);
167 if (!work->response_buf) {
168 pr_err("NULL response header\n");
173 iov[iov_idx] = (struct kvec) { work->tr_buf,
174 sizeof(struct smb2_transform_hdr) + 4 };
175 len += iov[iov_idx++].iov_len;
178 if (work->aux_payload_sz) {
179 iov[iov_idx] = (struct kvec) { work->response_buf, work->resp_hdr_sz };
180 len += iov[iov_idx++].iov_len;
181 iov[iov_idx] = (struct kvec) { work->aux_payload_buf, work->aux_payload_sz };
182 len += iov[iov_idx++].iov_len;
185 iov[iov_idx].iov_len = work->resp_hdr_sz;
187 iov[iov_idx].iov_len = get_rfc1002_len(work->response_buf) + 4;
188 iov[iov_idx].iov_base = work->response_buf;
189 len += iov[iov_idx++].iov_len;
192 ksmbd_conn_lock(conn);
193 sent = conn->transport->ops->writev(conn->transport, &iov[0],
195 work->need_invalidate_rkey,
197 ksmbd_conn_unlock(conn);
200 pr_err("Failed to send message: %d\n", sent);
207 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn, void *buf,
208 unsigned int buflen, u32 remote_key, u64 remote_offset,
213 if (conn->transport->ops->rdma_read)
214 ret = conn->transport->ops->rdma_read(conn->transport,
216 remote_key, remote_offset,
221 int ksmbd_conn_rdma_write(struct ksmbd_conn *conn, void *buf,
222 unsigned int buflen, u32 remote_key,
223 u64 remote_offset, u32 remote_len)
227 if (conn->transport->ops->rdma_write)
228 ret = conn->transport->ops->rdma_write(conn->transport,
230 remote_key, remote_offset,
235 bool ksmbd_conn_alive(struct ksmbd_conn *conn)
237 if (!ksmbd_server_running())
240 if (conn->status == KSMBD_SESS_EXITING)
243 if (kthread_should_stop())
246 if (atomic_read(&conn->stats.open_files_count) > 0)
250 * Stop current session if the time that get last request from client
251 * is bigger than deadtime user configured and opening file count is
254 if (server_conf.deadtime > 0 &&
255 time_after(jiffies, conn->last_active + server_conf.deadtime)) {
256 ksmbd_debug(CONN, "No response from client in %lu minutes\n",
257 server_conf.deadtime / SMB_ECHO_INTERVAL);
264 * ksmbd_conn_handler_loop() - session thread to listen on new smb requests
265 * @p: connection instance
267 * One thread each per connection
269 * Return: 0 on success
271 int ksmbd_conn_handler_loop(void *p)
273 struct ksmbd_conn *conn = (struct ksmbd_conn *)p;
274 struct ksmbd_transport *t = conn->transport;
275 unsigned int pdu_size;
276 char hdr_buf[4] = {0,};
279 mutex_init(&conn->srv_mutex);
280 __module_get(THIS_MODULE);
282 if (t->ops->prepare && t->ops->prepare(t))
285 conn->last_active = jiffies;
286 while (ksmbd_conn_alive(conn)) {
290 kvfree(conn->request_buf);
291 conn->request_buf = NULL;
293 size = t->ops->read(t, hdr_buf, sizeof(hdr_buf));
294 if (size != sizeof(hdr_buf))
297 pdu_size = get_rfc1002_len(hdr_buf);
298 ksmbd_debug(CONN, "RFC1002 header %u bytes\n", pdu_size);
301 * Check if pdu size is valid (min : smb header size,
304 if (pdu_size < __SMB2_HEADER_STRUCTURE_SIZE ||
305 pdu_size > MAX_STREAM_PROT_LEN) {
309 /* 4 for rfc1002 length field */
311 conn->request_buf = kvmalloc(size, GFP_KERNEL);
312 if (!conn->request_buf)
315 memcpy(conn->request_buf, hdr_buf, sizeof(hdr_buf));
316 if (!ksmbd_smb_request(conn))
320 * We already read 4 bytes to find out PDU size, now
323 size = t->ops->read(t, conn->request_buf + 4, pdu_size);
325 pr_err("sock_read failed: %d\n", size);
329 if (size != pdu_size) {
330 pr_err("PDU error. Read: %d, Expected: %d\n",
335 if (!default_conn_ops.process_fn) {
336 pr_err("No connection request callback\n");
340 if (default_conn_ops.process_fn(conn)) {
341 pr_err("Cannot handle request\n");
347 /* Wait till all reference dropped to the Server object*/
348 while (atomic_read(&conn->r_count) > 0)
349 schedule_timeout(HZ);
351 unload_nls(conn->local_nls);
352 if (default_conn_ops.terminate_fn)
353 default_conn_ops.terminate_fn(conn);
354 t->ops->disconnect(t);
355 module_put(THIS_MODULE);
359 void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops)
361 default_conn_ops.process_fn = ops->process_fn;
362 default_conn_ops.terminate_fn = ops->terminate_fn;
365 int ksmbd_conn_transport_init(void)
369 mutex_lock(&init_lock);
370 ret = ksmbd_tcp_init();
372 pr_err("Failed to init TCP subsystem: %d\n", ret);
376 ret = ksmbd_rdma_init();
378 pr_err("Failed to init RDMA subsystem: %d\n", ret);
382 mutex_unlock(&init_lock);
386 static void stop_sessions(void)
388 struct ksmbd_conn *conn;
391 read_lock(&conn_list_lock);
392 list_for_each_entry(conn, &conn_list, conns_list) {
393 struct task_struct *task;
395 task = conn->transport->handler;
397 ksmbd_debug(CONN, "Stop session handler %s/%d\n",
398 task->comm, task_pid_nr(task));
399 conn->status = KSMBD_SESS_EXITING;
401 read_unlock(&conn_list_lock);
403 if (!list_empty(&conn_list)) {
404 schedule_timeout_interruptible(HZ / 10); /* 100ms */
409 void ksmbd_conn_transport_destroy(void)
411 mutex_lock(&init_lock);
413 ksmbd_rdma_destroy();
415 mutex_unlock(&init_lock);