xref: /linux/fs/smb/server/server.c (revision dc6aa1ba8b7fff8800ad9bf143f17d308e02ec68)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
5  */
6 
7 #include "glob.h"
8 #include "oplock.h"
9 #include "misc.h"
10 #include <linux/sched/signal.h>
11 #include <linux/workqueue.h>
12 #include <linux/sysfs.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 
16 #include "server.h"
17 #include "smb_common.h"
18 #include "smb2pdu.h"
19 #include "../common/smb2status.h"
20 #include "connection.h"
21 #include "transport_ipc.h"
22 #include "mgmt/user_session.h"
23 #include "crypto_ctx.h"
24 #include "auth.h"
25 #include "stats.h"
26 #include "compress.h"
27 #include "mgmt/share_config.h"
28 #include "mgmt/tree_connect.h"
29 
30 int ksmbd_debug_types;
31 
32 struct ksmbd_server_config server_conf;
33 
34 enum SERVER_CTRL_TYPE {
35 	SERVER_CTRL_TYPE_INIT,
36 	SERVER_CTRL_TYPE_RESET,
37 };
38 
39 struct server_ctrl_struct {
40 	int			type;
41 	struct work_struct	ctrl_work;
42 };
43 
44 static DEFINE_MUTEX(ctrl_lock);
45 
46 static int ___server_conf_set(int idx, char *val)
47 {
48 	if (idx >= ARRAY_SIZE(server_conf.conf))
49 		return -EINVAL;
50 
51 	if (!val || val[0] == 0x00)
52 		return -EINVAL;
53 
54 	kfree(server_conf.conf[idx]);
55 	server_conf.conf[idx] = kstrdup(val, KSMBD_DEFAULT_GFP);
56 	if (!server_conf.conf[idx])
57 		return -ENOMEM;
58 	return 0;
59 }
60 
61 int ksmbd_set_netbios_name(char *v)
62 {
63 	return ___server_conf_set(SERVER_CONF_NETBIOS_NAME, v);
64 }
65 
66 int ksmbd_set_server_string(char *v)
67 {
68 	return ___server_conf_set(SERVER_CONF_SERVER_STRING, v);
69 }
70 
71 int ksmbd_set_work_group(char *v)
72 {
73 	return ___server_conf_set(SERVER_CONF_WORK_GROUP, v);
74 }
75 
76 char *ksmbd_netbios_name(void)
77 {
78 	return server_conf.conf[SERVER_CONF_NETBIOS_NAME];
79 }
80 
81 char *ksmbd_server_string(void)
82 {
83 	return server_conf.conf[SERVER_CONF_SERVER_STRING];
84 }
85 
86 char *ksmbd_work_group(void)
87 {
88 	return server_conf.conf[SERVER_CONF_WORK_GROUP];
89 }
90 
91 /**
92  * check_conn_state() - check state of server thread connection
93  * @work:     smb work containing server thread information
94  *
95  * Return:	0 on valid connection, otherwise 1 to reconnect
96  */
97 static inline int check_conn_state(struct ksmbd_work *work)
98 {
99 	struct smb_hdr *rsp_hdr;
100 
101 	if (ksmbd_conn_exiting(work->conn) ||
102 	    ksmbd_conn_need_reconnect(work->conn)) {
103 		rsp_hdr = smb_get_msg(work->response_buf);
104 		rsp_hdr->Status.CifsError = STATUS_CONNECTION_DISCONNECTED;
105 		return 1;
106 	}
107 	return 0;
108 }
109 
110 #define SERVER_HANDLER_CONTINUE		0
111 #define SERVER_HANDLER_ABORT		1
112 
113 static int __process_request(struct ksmbd_work *work, struct ksmbd_conn *conn,
114 			     u16 *cmd)
115 {
116 	struct smb_version_cmds *cmds;
117 	u16 command;
118 	bool signed_req;
119 	int ret;
120 
121 	if (check_conn_state(work))
122 		return SERVER_HANDLER_CONTINUE;
123 
124 	if (ksmbd_verify_smb_message(work)) {
125 		conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
126 		return SERVER_HANDLER_ABORT;
127 	}
128 
129 	command = conn->ops->get_cmd_val(work);
130 	*cmd = command;
131 
132 andx_again:
133 	if (command >= conn->max_cmds) {
134 		conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
135 		return SERVER_HANDLER_ABORT;
136 	}
137 
138 	cmds = &conn->cmds[command];
139 	if (!cmds->proc) {
140 		ksmbd_debug(SMB, "*** not implemented yet cmd = %x\n", command);
141 		conn->ops->set_rsp_status(work, STATUS_NOT_IMPLEMENTED);
142 		return SERVER_HANDLER_ABORT;
143 	}
144 
145 	signed_req = conn->ops->is_sign_req && conn->ops->is_sign_req(work, command);
146 	if (work->sess && work->sess->sign && !work->encrypted &&
147 	    !signed_req) {
148 		conn->ops->set_rsp_status(work, STATUS_ACCESS_DENIED);
149 		return SERVER_HANDLER_ABORT;
150 	}
151 
152 	if (work->sess && signed_req) {
153 		ret = conn->ops->check_sign_req(work);
154 		if (!ret) {
155 			conn->ops->set_rsp_status(work, STATUS_ACCESS_DENIED);
156 			return SERVER_HANDLER_ABORT;
157 		}
158 	}
159 
160 	if (smb2_check_compound_fid(work))
161 		ret = -EBADF;
162 	else
163 		ret = cmds->proc(work);
164 	if (conn->ops->inc_reqs) {
165 		struct smb2_hdr *rsp = ksmbd_resp_buf_curr(work);
166 
167 		conn->ops->inc_reqs(command, rsp->Status);
168 	}
169 
170 	if (ret < 0)
171 		ksmbd_debug(CONN, "Failed to process %u [%d]\n", command, ret);
172 	/* AndX commands - chained request can return positive values */
173 	else if (ret > 0) {
174 		command = ret;
175 		*cmd = command;
176 		goto andx_again;
177 	}
178 
179 	if (work->send_no_response)
180 		return SERVER_HANDLER_ABORT;
181 	return SERVER_HANDLER_CONTINUE;
182 }
183 
184 static void __handle_ksmbd_work(struct ksmbd_work *work,
185 				struct ksmbd_conn *conn)
186 {
187 	u16 command = 0;
188 	int rc;
189 	bool is_chained = false;
190 
191 	if (conn->ops->is_transform_hdr &&
192 	    conn->ops->is_transform_hdr(work->request_buf)) {
193 		u64 tr_sess_id;
194 
195 		if (get_rfc1002_len(work->request_buf) <
196 		    sizeof(struct smb2_transform_hdr)) {
197 			ksmbd_conn_abort(conn);
198 			return;
199 		}
200 		tr_sess_id = le64_to_cpu(((struct smb2_transform_hdr *)
201 				smb_get_msg(work->request_buf))->SessionId);
202 
203 		rc = conn->ops->decrypt_req(work);
204 		if (rc < 0) {
205 			ksmbd_conn_abort(conn);
206 			return;
207 		}
208 		work->encrypted = true;
209 
210 		/*
211 		 * SMB3 applies compression before encryption.  The receive loop
212 		 * handles a plain compression transform before allocating work, but
213 		 * an encrypted request exposes that transform only after decryption.
214 		 */
215 		if (((struct smb2_hdr *)smb_get_msg(work->request_buf))->ProtocolId ==
216 		    SMB2_COMPRESSION_TRANSFORM_ID) {
217 			rc = ksmbd_decompress_work_request(work);
218 			if (rc < 0) {
219 				ksmbd_conn_abort(conn);
220 				return;
221 			}
222 		}
223 
224 		/* The decrypted payload must now be a complete SMB2 request. */
225 		if (((struct smb2_hdr *)smb_get_msg(work->request_buf))->ProtocolId !=
226 			SMB2_PROTO_NUMBER ||
227 		    get_rfc1002_len(work->request_buf) < sizeof(struct smb2_pdu)) {
228 			ksmbd_conn_abort(conn);
229 			return;
230 		}
231 
232 		rc = ksmbd_check_transform_session(work, tr_sess_id);
233 		if (rc < 0) {
234 			ksmbd_conn_abort(conn);
235 			return;
236 		}
237 	}
238 
239 	if (conn->ops->allocate_rsp_buf(work))
240 		return;
241 
242 	rc = conn->ops->init_rsp_hdr(work);
243 	if (rc) {
244 		/* either uid or tid is not correct */
245 		conn->ops->set_rsp_status(work, STATUS_INVALID_HANDLE);
246 		goto send;
247 	}
248 
249 	do {
250 		if (conn->ops->check_user_session) {
251 			rc = conn->ops->check_user_session(work);
252 			if (rc < 0) {
253 				if (rc == -EINVAL)
254 					conn->ops->set_rsp_status(work,
255 						STATUS_INVALID_PARAMETER);
256 				else if (rc == -EKEYEXPIRED)
257 					conn->ops->set_rsp_status(work,
258 						STATUS_NETWORK_SESSION_EXPIRED);
259 				else
260 					conn->ops->set_rsp_status(work,
261 						STATUS_USER_SESSION_DELETED);
262 				if (conn->ops->is_sign_req(work, conn->ops->get_cmd_val(work))) {
263 					struct smb2_hdr *rsp_hdr;
264 
265 					rsp_hdr = ksmbd_resp_buf_curr(work);
266 					if (rc == -EKEYEXPIRED && work->sess &&
267 					    conn->ops->set_sign_rsp)
268 						conn->ops->set_sign_rsp(work);
269 					else
270 						rsp_hdr->Flags |= SMB2_FLAGS_SIGNED;
271 				}
272 				goto send;
273 			} else if (rc > 0) {
274 				rc = conn->ops->get_ksmbd_tcon(work);
275 				if (rc < 0) {
276 					if (rc == -EINVAL)
277 						conn->ops->set_rsp_status(work,
278 							STATUS_INVALID_PARAMETER);
279 					else
280 						conn->ops->set_rsp_status(work,
281 							STATUS_NETWORK_NAME_DELETED);
282 					goto send;
283 				}
284 
285 				if (work->tcon &&
286 				    test_share_config_flag(work->tcon->share_conf,
287 							   KSMBD_SHARE_FLAG_ENCRYPT_DATA) &&
288 				    !work->encrypted) {
289 					conn->ops->set_rsp_status(work,
290 								  STATUS_ACCESS_DENIED);
291 					goto send;
292 				}
293 			}
294 		}
295 
296 		rc = __process_request(work, conn, &command);
297 		if (rc == SERVER_HANDLER_ABORT) {
298 			smb2_complete_request_open(work);
299 			break;
300 		}
301 
302 		/*
303 		 * Call smb2_set_rsp_credits() function to set number of credits
304 		 * granted in hdr of smb2 response.
305 		 */
306 		if (conn->ops->set_rsp_credits) {
307 			spin_lock(&conn->credits_lock);
308 			rc = conn->ops->set_rsp_credits(work);
309 			spin_unlock(&conn->credits_lock);
310 			if (rc < 0) {
311 				conn->ops->set_rsp_status(work,
312 					STATUS_INVALID_PARAMETER);
313 				smb2_complete_request_open(work);
314 				goto send;
315 			}
316 		}
317 
318 		smb2_complete_request_open(work);
319 
320 		is_chained = is_chained_smb2_message(work);
321 
322 		if (work->sess &&
323 		    (work->sess->sign || smb3_11_final_sess_setup_resp(work) ||
324 		     conn->ops->is_sign_req(work, command))) {
325 			if (command == SMB2_SESSION_SETUP_HE &&
326 			    work->sess->dialect >= SMB30_PROT_ID &&
327 			    conn->dialect < SMB30_PROT_ID)
328 				smb3_set_sign_rsp(work);
329 			else
330 				conn->ops->set_sign_rsp(work);
331 		}
332 	} while (is_chained == true);
333 
334 send:
335 	smb2_complete_request_open(work);
336 	/*
337 	 * Release any credit charge still outstanding for this request.  On
338 	 * the normal path smb2_set_rsp_credits() already returned it, but the
339 	 * abort, error and send-no-response paths skip that call, so the
340 	 * charge would otherwise leak and eventually exhaust the connection's
341 	 * outstanding credit window.
342 	 */
343 	if (work->credit_charge) {
344 		spin_lock(&conn->credits_lock);
345 		conn->outstanding_credits -= work->credit_charge;
346 		work->credit_charge = 0;
347 		spin_unlock(&conn->credits_lock);
348 	}
349 
350 	if (work->tcon)
351 		ksmbd_tree_connect_put(work->tcon);
352 	smb3_preauth_hash_rsp(work);
353 	/*
354 	 * Preauthentication hashes cover the original SMB2 response. Apply the
355 	 * transport compression wrapper only after updating the hash.
356 	 */
357 	if (work->compress_response) {
358 		rc = ksmbd_compress_response(work);
359 		if (rc < 0)
360 			ksmbd_debug(CONN, "Failed to compress response: %d\n", rc);
361 	}
362 	if (work->sess && work->sess->enc && work->encrypted &&
363 	    conn->ops->encrypt_resp) {
364 		rc = conn->ops->encrypt_resp(work);
365 		if (rc < 0)
366 			conn->ops->set_rsp_status(work, STATUS_DATA_ERROR);
367 	}
368 	if (work->sess)
369 		ksmbd_user_session_put(work->sess);
370 
371 	ksmbd_conn_write(work);
372 }
373 
374 /**
375  * handle_ksmbd_work() - process pending smb work requests
376  * @wk:	smb work containing request command buffer
377  *
378  * called by kworker threads to processing remaining smb work requests
379  */
380 static void handle_ksmbd_work(struct work_struct *wk)
381 {
382 	struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
383 	struct ksmbd_conn *conn = work->conn;
384 
385 	atomic64_inc(&conn->stats.request_served);
386 
387 	__handle_ksmbd_work(work, conn);
388 
389 	ksmbd_conn_try_dequeue_request(work);
390 	ksmbd_free_work_struct(work);
391 	ksmbd_conn_r_count_dec(conn);
392 }
393 
394 /**
395  * queue_ksmbd_work() - queue a smb request to worker thread queue
396  *		for processing smb command and sending response
397  * @conn:	connection instance
398  *
399  * read remaining data from socket create and submit work.
400  */
401 static int queue_ksmbd_work(struct ksmbd_conn *conn)
402 {
403 	struct ksmbd_work *work;
404 	bool wait_for_neg = ksmbd_conn_new(conn) || ksmbd_conn_need_negotiate(conn);
405 	int err;
406 
407 	err = ksmbd_init_smb_server(conn);
408 	if (err)
409 		return 0;
410 
411 	work = ksmbd_alloc_work_struct();
412 	if (!work) {
413 		pr_err("allocation for work failed\n");
414 		return -ENOMEM;
415 	}
416 
417 	work->conn = conn;
418 	work->request_buf = conn->request_buf;
419 	conn->request_buf = NULL;
420 
421 	ksmbd_conn_enqueue_request(work);
422 	ksmbd_conn_r_count_inc(conn);
423 	/* update activity on connection */
424 	conn->last_active = jiffies;
425 	INIT_WORK(&work->work, handle_ksmbd_work);
426 	ksmbd_queue_work(work);
427 	/* Negotiation can change conn->ops, finish it before reading another PDU. */
428 	if (wait_for_neg)
429 		wait_event(conn->req_running_q,
430 			   atomic_read(&conn->req_running) == 0);
431 	return 0;
432 }
433 
434 static int ksmbd_server_process_request(struct ksmbd_conn *conn)
435 {
436 	return queue_ksmbd_work(conn);
437 }
438 
439 static int ksmbd_server_terminate_conn(struct ksmbd_conn *conn)
440 {
441 	ksmbd_conn_sessions_cleanup(conn);
442 	return 0;
443 }
444 
445 static void ksmbd_server_tcp_callbacks_init(void)
446 {
447 	struct ksmbd_conn_ops ops;
448 
449 	ops.process_fn = ksmbd_server_process_request;
450 	ops.terminate_fn = ksmbd_server_terminate_conn;
451 
452 	ksmbd_conn_init_server_callbacks(&ops);
453 }
454 
455 static void server_conf_free(void)
456 {
457 	int i;
458 
459 	for (i = 0; i < ARRAY_SIZE(server_conf.conf); i++) {
460 		kfree(server_conf.conf[i]);
461 		server_conf.conf[i] = NULL;
462 	}
463 }
464 
465 static int server_conf_init(void)
466 {
467 	WRITE_ONCE(server_conf.state, SERVER_STATE_STARTING_UP);
468 	server_conf.enforced_signing = 0;
469 	server_conf.min_protocol = ksmbd_min_protocol();
470 	server_conf.max_protocol = ksmbd_max_protocol();
471 	server_conf.auth_mechs = KSMBD_AUTH_NTLMSSP;
472 #ifdef CONFIG_SMB_SERVER_KERBEROS5
473 	server_conf.auth_mechs |= KSMBD_AUTH_KRB5 |
474 				KSMBD_AUTH_MSKRB5;
475 #endif
476 	server_conf.max_inflight_req = SMB2_MAX_CREDITS;
477 	server_conf.max_sessions_per_connection =
478 		KSMBD_DEFAULT_MAX_SESSIONS_PER_CONNECTION;
479 	return 0;
480 }
481 
482 static void server_ctrl_handle_init(struct server_ctrl_struct *ctrl)
483 {
484 	int ret;
485 
486 	ksmbd_proc_reset();
487 	ret = ksmbd_conn_transport_init();
488 	if (ret) {
489 		server_queue_ctrl_reset_work();
490 		return;
491 	}
492 
493 	pr_info("running\n");
494 	WRITE_ONCE(server_conf.state, SERVER_STATE_RUNNING);
495 }
496 
497 static void server_ctrl_handle_reset(struct server_ctrl_struct *ctrl)
498 {
499 	ksmbd_ipc_soft_reset();
500 	ksmbd_conn_transport_destroy();
501 	ksmbd_stop_durable_scavenger();
502 	server_conf_free();
503 	server_conf_init();
504 	WRITE_ONCE(server_conf.state, SERVER_STATE_STARTING_UP);
505 }
506 
507 static void server_ctrl_handle_work(struct work_struct *work)
508 {
509 	struct server_ctrl_struct *ctrl;
510 
511 	ctrl = container_of(work, struct server_ctrl_struct, ctrl_work);
512 
513 	mutex_lock(&ctrl_lock);
514 	switch (ctrl->type) {
515 	case SERVER_CTRL_TYPE_INIT:
516 		server_ctrl_handle_init(ctrl);
517 		break;
518 	case SERVER_CTRL_TYPE_RESET:
519 		server_ctrl_handle_reset(ctrl);
520 		break;
521 	default:
522 		pr_err("Unknown server work type: %d\n", ctrl->type);
523 	}
524 	mutex_unlock(&ctrl_lock);
525 	kfree(ctrl);
526 	module_put(THIS_MODULE);
527 }
528 
529 static int __queue_ctrl_work(int type)
530 {
531 	struct server_ctrl_struct *ctrl;
532 
533 	ctrl = kmalloc_obj(struct server_ctrl_struct, KSMBD_DEFAULT_GFP);
534 	if (!ctrl)
535 		return -ENOMEM;
536 
537 	__module_get(THIS_MODULE);
538 	ctrl->type = type;
539 	INIT_WORK(&ctrl->ctrl_work, server_ctrl_handle_work);
540 	queue_work(system_long_wq, &ctrl->ctrl_work);
541 	return 0;
542 }
543 
544 int server_queue_ctrl_init_work(void)
545 {
546 	return __queue_ctrl_work(SERVER_CTRL_TYPE_INIT);
547 }
548 
549 int server_queue_ctrl_reset_work(void)
550 {
551 	return __queue_ctrl_work(SERVER_CTRL_TYPE_RESET);
552 }
553 
554 static ssize_t stats_show(const struct class *class, const struct class_attribute *attr,
555 			  char *buf)
556 {
557 	/*
558 	 * Inc this each time you change stats output format,
559 	 * so user space will know what to do.
560 	 */
561 	static int stats_version = 2;
562 	static const char * const state[] = {
563 		"startup",
564 		"running",
565 		"reset",
566 		"shutdown"
567 	};
568 	return sysfs_emit(buf, "%d %s %d %lu\n", stats_version,
569 			  state[server_conf.state], server_conf.tcp_port,
570 			  server_conf.ipc_last_active / HZ);
571 }
572 
573 static ssize_t kill_server_store(const struct class *class,
574 				 const struct class_attribute *attr, const char *buf,
575 				 size_t len)
576 {
577 	if (!sysfs_streq(buf, "hard"))
578 		return len;
579 
580 	pr_info("kill command received\n");
581 	mutex_lock(&ctrl_lock);
582 	WRITE_ONCE(server_conf.state, SERVER_STATE_RESETTING);
583 	__module_get(THIS_MODULE);
584 	server_ctrl_handle_reset(NULL);
585 	module_put(THIS_MODULE);
586 	mutex_unlock(&ctrl_lock);
587 	return len;
588 }
589 
590 static const char * const debug_type_strings[] = {"smb", "auth", "vfs",
591 						  "oplock", "ipc", "conn",
592 						  "rdma"};
593 
594 static ssize_t debug_show(const struct class *class, const struct class_attribute *attr,
595 			  char *buf)
596 {
597 	ssize_t sz = 0;
598 	int i, pos = 0;
599 
600 	for (i = 0; i < ARRAY_SIZE(debug_type_strings); i++) {
601 		if ((ksmbd_debug_types >> i) & 1) {
602 			pos = sysfs_emit_at(buf, sz, "[%s] ", debug_type_strings[i]);
603 		} else {
604 			pos = sysfs_emit_at(buf, sz, "%s ", debug_type_strings[i]);
605 		}
606 		sz += pos;
607 	}
608 	sz += sysfs_emit_at(buf, sz, "\n");
609 	return sz;
610 }
611 
612 static ssize_t debug_store(const struct class *class, const struct class_attribute *attr,
613 			   const char *buf, size_t len)
614 {
615 	int i;
616 
617 	for (i = 0; i < ARRAY_SIZE(debug_type_strings); i++) {
618 		if (sysfs_streq(buf, "all")) {
619 			if (ksmbd_debug_types == KSMBD_DEBUG_ALL)
620 				ksmbd_debug_types = 0;
621 			else
622 				ksmbd_debug_types = KSMBD_DEBUG_ALL;
623 			break;
624 		}
625 
626 		if (sysfs_streq(buf, debug_type_strings[i])) {
627 			if (ksmbd_debug_types & (1 << i))
628 				ksmbd_debug_types &= ~(1 << i);
629 			else
630 				ksmbd_debug_types |= (1 << i);
631 			break;
632 		}
633 	}
634 
635 	return len;
636 }
637 
638 static CLASS_ATTR_RO(stats);
639 static CLASS_ATTR_WO(kill_server);
640 static CLASS_ATTR_RW(debug);
641 
642 static struct attribute *ksmbd_control_class_attrs[] = {
643 	&class_attr_stats.attr,
644 	&class_attr_kill_server.attr,
645 	&class_attr_debug.attr,
646 	NULL,
647 };
648 ATTRIBUTE_GROUPS(ksmbd_control_class);
649 
650 static struct class ksmbd_control_class = {
651 	.name		= "ksmbd-control",
652 	.class_groups	= ksmbd_control_class_groups,
653 };
654 
655 static int ksmbd_server_shutdown(void)
656 {
657 	WRITE_ONCE(server_conf.state, SERVER_STATE_SHUTTING_DOWN);
658 
659 	class_unregister(&ksmbd_control_class);
660 	ksmbd_workqueue_destroy();
661 	ksmbd_ipc_release();
662 	ksmbd_conn_transport_destroy();
663 	/*
664 	 * ksmbd_conn_transport_destroy() calls delete_proc_clients() and destroys
665 	 * sessions. ksmbd_session_destroy() removes each session's proc entry.
666 	 * Keep the procfs tree alive until these entries have been removed.
667 	 */
668 	ksmbd_proc_cleanup();
669 	ksmbd_crypto_destroy();
670 	ksmbd_free_global_file_table();
671 	destroy_lease_table();
672 	ksmbd_work_pool_destroy();
673 	ksmbd_exit_file_cache();
674 	server_conf_free();
675 	return 0;
676 }
677 
678 static int __init ksmbd_server_init(void)
679 {
680 	int ret;
681 
682 	ret = class_register(&ksmbd_control_class);
683 	if (ret) {
684 		pr_err("Unable to register ksmbd-control class\n");
685 		return ret;
686 	}
687 
688 	ret = ksmbd_proc_init();
689 	if (ret)
690 		goto err_unregister;
691 
692 	if (create_proc_sessions())
693 		pr_warn("Unable to create sessions procfs entry\n");
694 
695 	if (create_proc_shares())
696 		pr_warn("Unable to create shares procfs entry\n");
697 
698 	ksmbd_server_tcp_callbacks_init();
699 
700 	ret = server_conf_init();
701 	if (ret)
702 		goto err_proc_cleanup;
703 
704 	ret = ksmbd_work_pool_init();
705 	if (ret)
706 		goto err_proc_cleanup;
707 
708 	ret = ksmbd_init_file_cache();
709 	if (ret)
710 		goto err_destroy_work_pools;
711 
712 	ret = ksmbd_ipc_init();
713 	if (ret)
714 		goto err_exit_file_cache;
715 
716 	ret = ksmbd_init_global_file_table();
717 	if (ret)
718 		goto err_ipc_release;
719 
720 	ret = ksmbd_inode_hash_init();
721 	if (ret)
722 		goto err_destroy_file_table;
723 
724 	ret = ksmbd_crypto_create();
725 	if (ret)
726 		goto err_release_inode_hash;
727 
728 	ret = ksmbd_workqueue_init();
729 	if (ret)
730 		goto err_crypto_destroy;
731 
732 	ret = ksmbd_conn_wq_init();
733 	if (ret)
734 		goto err_workqueue_destroy;
735 
736 	return 0;
737 
738 err_workqueue_destroy:
739 	ksmbd_workqueue_destroy();
740 err_crypto_destroy:
741 	ksmbd_crypto_destroy();
742 err_release_inode_hash:
743 	ksmbd_release_inode_hash();
744 err_destroy_file_table:
745 	ksmbd_free_global_file_table();
746 err_ipc_release:
747 	ksmbd_ipc_release();
748 err_exit_file_cache:
749 	ksmbd_exit_file_cache();
750 err_destroy_work_pools:
751 	ksmbd_work_pool_destroy();
752 err_proc_cleanup:
753 	ksmbd_proc_cleanup();
754 err_unregister:
755 	class_unregister(&ksmbd_control_class);
756 
757 	return ret;
758 }
759 
760 /**
761  * ksmbd_server_exit() - shutdown forker thread and free memory at module exit
762  */
763 static void __exit ksmbd_server_exit(void)
764 {
765 	ksmbd_server_shutdown();
766 	rcu_barrier();
767 	/*
768 	 * ksmbd_conn_put() defers the final release onto ksmbd_conn_wq,
769 	 * so drain it after rcu_barrier() has fired any pending RCU
770 	 * callbacks that may have queued a release.
771 	 */
772 	ksmbd_conn_wq_destroy();
773 	ksmbd_release_inode_hash();
774 }
775 
776 MODULE_AUTHOR("Namjae Jeon <linkinjeon@kernel.org>");
777 MODULE_DESCRIPTION("Linux kernel CIFS/SMB SERVER");
778 MODULE_LICENSE("GPL");
779 MODULE_SOFTDEP("pre: nls");
780 MODULE_SOFTDEP("pre: aes");
781 MODULE_SOFTDEP("pre: aead2");
782 MODULE_SOFTDEP("pre: ccm");
783 MODULE_SOFTDEP("pre: gcm");
784 module_init(ksmbd_server_init)
785 module_exit(ksmbd_server_exit)
786