Merge tag 'for-v6.0' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power...
[platform/kernel/linux-starfive.git] / fs / ksmbd / mgmt / tree_connect.c
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_conn *conn, struct ksmbd_session *sess,
20                         char *share_name)
21 {
22         struct ksmbd_tree_conn_status status = {-EINVAL, NULL};
23         struct ksmbd_tree_connect_response *resp = NULL;
24         struct ksmbd_share_config *sc;
25         struct ksmbd_tree_connect *tree_conn = NULL;
26         struct sockaddr *peer_addr;
27         int ret;
28
29         sc = ksmbd_share_config_get(share_name);
30         if (!sc)
31                 return status;
32
33         tree_conn = kzalloc(sizeof(struct ksmbd_tree_connect), GFP_KERNEL);
34         if (!tree_conn) {
35                 status.ret = -ENOMEM;
36                 goto out_error;
37         }
38
39         tree_conn->id = ksmbd_acquire_tree_conn_id(sess);
40         if (tree_conn->id < 0) {
41                 status.ret = -EINVAL;
42                 goto out_error;
43         }
44
45         peer_addr = KSMBD_TCP_PEER_SOCKADDR(conn);
46         resp = ksmbd_ipc_tree_connect_request(sess,
47                                               sc,
48                                               tree_conn,
49                                               peer_addr);
50         if (!resp) {
51                 status.ret = -EINVAL;
52                 goto out_error;
53         }
54
55         status.ret = resp->status;
56         if (status.ret != KSMBD_TREE_CONN_STATUS_OK)
57                 goto out_error;
58
59         tree_conn->flags = resp->connection_flags;
60         tree_conn->user = sess->user;
61         tree_conn->share_conf = sc;
62         status.tree_conn = tree_conn;
63
64         ret = xa_err(xa_store(&sess->tree_conns, tree_conn->id, tree_conn,
65                               GFP_KERNEL));
66         if (ret) {
67                 status.ret = -ENOMEM;
68                 goto out_error;
69         }
70         kvfree(resp);
71         return status;
72
73 out_error:
74         if (tree_conn)
75                 ksmbd_release_tree_conn_id(sess, tree_conn->id);
76         ksmbd_share_config_put(sc);
77         kfree(tree_conn);
78         kvfree(resp);
79         return status;
80 }
81
82 int ksmbd_tree_conn_disconnect(struct ksmbd_session *sess,
83                                struct ksmbd_tree_connect *tree_conn)
84 {
85         int ret;
86
87         ret = ksmbd_ipc_tree_disconnect_request(sess->id, tree_conn->id);
88         ksmbd_release_tree_conn_id(sess, tree_conn->id);
89         xa_erase(&sess->tree_conns, tree_conn->id);
90         ksmbd_share_config_put(tree_conn->share_conf);
91         kfree(tree_conn);
92         return ret;
93 }
94
95 struct ksmbd_tree_connect *ksmbd_tree_conn_lookup(struct ksmbd_session *sess,
96                                                   unsigned int id)
97 {
98         return xa_load(&sess->tree_conns, id);
99 }
100
101 struct ksmbd_share_config *ksmbd_tree_conn_share(struct ksmbd_session *sess,
102                                                  unsigned int id)
103 {
104         struct ksmbd_tree_connect *tc;
105
106         tc = ksmbd_tree_conn_lookup(sess, id);
107         if (tc)
108                 return tc->share_conf;
109         return NULL;
110 }
111
112 int ksmbd_tree_conn_session_logoff(struct ksmbd_session *sess)
113 {
114         int ret = 0;
115         struct ksmbd_tree_connect *tc;
116         unsigned long id;
117
118         xa_for_each(&sess->tree_conns, id, tc)
119                 ret |= ksmbd_tree_conn_disconnect(sess, tc);
120         xa_destroy(&sess->tree_conns);
121         return ret;
122 }