1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2018 Samsung Electronics Co., Ltd. 4 */ 5 6 #include <linux/list.h> 7 #include <linux/slab.h> 8 #include <linux/xarray.h> 9 10 #include "../transport_ipc.h" 11 #include "../connection.h" 12 13 #include "tree_connect.h" 14 #include "user_config.h" 15 #include "share_config.h" 16 #include "user_session.h" 17 18 struct ksmbd_tree_conn_status 19 ksmbd_tree_conn_connect(struct ksmbd_work *work, const char *share_name) 20 { 21 struct ksmbd_tree_conn_status status = {-ENOENT, NULL}; 22 struct ksmbd_tree_connect_response *resp = NULL; 23 struct ksmbd_share_config *sc; 24 struct ksmbd_tree_connect *tree_conn = NULL; 25 struct sockaddr *peer_addr; 26 struct ksmbd_conn *conn = work->conn; 27 struct ksmbd_session *sess = work->sess; 28 int ret; 29 30 sc = ksmbd_share_config_get(work, share_name); 31 if (!sc) 32 return status; 33 34 tree_conn = kzalloc(sizeof(struct ksmbd_tree_connect), 35 KSMBD_DEFAULT_GFP); 36 if (!tree_conn) { 37 status.ret = -ENOMEM; 38 goto out_error; 39 } 40 41 tree_conn->id = ksmbd_acquire_tree_conn_id(sess); 42 if (tree_conn->id < 0) { 43 status.ret = -EINVAL; 44 goto out_error; 45 } 46 47 peer_addr = KSMBD_TCP_PEER_SOCKADDR(conn); 48 resp = ksmbd_ipc_tree_connect_request(sess, 49 sc, 50 tree_conn, 51 peer_addr); 52 if (!resp) { 53 status.ret = -EINVAL; 54 goto out_error; 55 } 56 57 status.ret = resp->status; 58 if (status.ret != KSMBD_TREE_CONN_STATUS_OK) 59 goto out_error; 60 61 tree_conn->flags = resp->connection_flags; 62 if (test_tree_conn_flag(tree_conn, KSMBD_TREE_CONN_FLAG_UPDATE)) { 63 struct ksmbd_share_config *new_sc; 64 65 ksmbd_share_config_del(sc); 66 new_sc = ksmbd_share_config_get(work, share_name); 67 if (!new_sc) { 68 pr_err("Failed to update stale share config\n"); 69 status.ret = -ESTALE; 70 goto out_error; 71 } 72 ksmbd_share_config_put(sc); 73 sc = new_sc; 74 } 75 76 tree_conn->user = sess->user; 77 tree_conn->share_conf = sc; 78 tree_conn->t_state = TREE_NEW; 79 status.tree_conn = tree_conn; 80 atomic_set(&tree_conn->refcount, 1); 81 init_waitqueue_head(&tree_conn->refcount_q); 82 83 ret = xa_err(xa_store(&sess->tree_conns, tree_conn->id, tree_conn, 84 KSMBD_DEFAULT_GFP)); 85 if (ret) { 86 status.ret = -ENOMEM; 87 goto out_error; 88 } 89 kvfree(resp); 90 return status; 91 92 out_error: 93 if (tree_conn) 94 ksmbd_release_tree_conn_id(sess, tree_conn->id); 95 ksmbd_share_config_put(sc); 96 kfree(tree_conn); 97 kvfree(resp); 98 return status; 99 } 100 101 void ksmbd_tree_connect_put(struct ksmbd_tree_connect *tcon) 102 { 103 /* 104 * Checking waitqueue to releasing tree connect on 105 * tree disconnect. waitqueue_active is safe because it 106 * uses atomic operation for condition. 107 */ 108 if (!atomic_dec_return(&tcon->refcount) && 109 waitqueue_active(&tcon->refcount_q)) 110 wake_up(&tcon->refcount_q); 111 } 112 113 int ksmbd_tree_conn_disconnect(struct ksmbd_session *sess, 114 struct ksmbd_tree_connect *tree_conn) 115 { 116 int ret; 117 118 write_lock(&sess->tree_conns_lock); 119 xa_erase(&sess->tree_conns, tree_conn->id); 120 write_unlock(&sess->tree_conns_lock); 121 122 if (!atomic_dec_and_test(&tree_conn->refcount)) 123 wait_event(tree_conn->refcount_q, 124 atomic_read(&tree_conn->refcount) == 0); 125 126 ret = ksmbd_ipc_tree_disconnect_request(sess->id, tree_conn->id); 127 ksmbd_release_tree_conn_id(sess, tree_conn->id); 128 ksmbd_share_config_put(tree_conn->share_conf); 129 kfree(tree_conn); 130 return ret; 131 } 132 133 struct ksmbd_tree_connect *ksmbd_tree_conn_lookup(struct ksmbd_session *sess, 134 unsigned int id) 135 { 136 struct ksmbd_tree_connect *tcon; 137 138 read_lock(&sess->tree_conns_lock); 139 tcon = xa_load(&sess->tree_conns, id); 140 if (tcon) { 141 if (tcon->t_state != TREE_CONNECTED) 142 tcon = NULL; 143 else if (!atomic_inc_not_zero(&tcon->refcount)) 144 tcon = NULL; 145 } 146 read_unlock(&sess->tree_conns_lock); 147 148 return tcon; 149 } 150 151 int ksmbd_tree_conn_session_logoff(struct ksmbd_session *sess) 152 { 153 int ret = 0; 154 struct ksmbd_tree_connect *tc; 155 unsigned long id; 156 157 if (!sess) 158 return -EINVAL; 159 160 xa_for_each(&sess->tree_conns, id, tc) { 161 write_lock(&sess->tree_conns_lock); 162 if (tc->t_state == TREE_DISCONNECTED) { 163 write_unlock(&sess->tree_conns_lock); 164 ret = -ENOENT; 165 continue; 166 } 167 tc->t_state = TREE_DISCONNECTED; 168 write_unlock(&sess->tree_conns_lock); 169 170 ret |= ksmbd_tree_conn_disconnect(sess, tc); 171 } 172 xa_destroy(&sess->tree_conns); 173 return ret; 174 } 175