1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "tipc_service.h"
18 
19 #include <interface/storage/storage.h>
20 #include <lib/tipc/tipc.h>
21 
22 #include "block_device_tipc.h"
23 #include "client_tipc.h"
24 
25 #define SS_ERR(args...) fprintf(stderr, "ss: " args)
26 
27 // TODO: Put this somewhere central?
port_name(enum storage_filesystem_type fs_type)28 static const char* port_name(enum storage_filesystem_type fs_type) {
29     switch (fs_type) {
30     case STORAGE_TP:
31         return STORAGE_CLIENT_TP_PORT;
32     case STORAGE_TDEA:
33         return STORAGE_CLIENT_TDEA_PORT;
34     case STORAGE_TDP:
35         return STORAGE_CLIENT_TDP_PORT;
36     case STORAGE_TD:
37         return STORAGE_CLIENT_TD_PORT;
38     case STORAGE_NSP:
39         return STORAGE_CLIENT_NSP_PORT;
40     case STORAGE_FILESYSTEMS_COUNT:
41     default:
42         SS_ERR("%s: Tried to get port for unrecognized storage_filesystem_type type: (%d)\n",
43                __func__, fs_type);
44         return NULL;
45     }
46 }
47 
client_port_context_init(struct client_port_context * self,enum storage_filesystem_type fs_type,struct block_device_tipc * ctx,struct tipc_hset * hset)48 static int client_port_context_init(struct client_port_context* self,
49                                     enum storage_filesystem_type fs_type,
50                                     struct block_device_tipc* ctx,
51                                     struct tipc_hset* hset) {
52     if (!block_device_tipc_fs_connected(ctx, fs_type)) {
53         return 0;
54     }
55     self->tr_state = block_device_tipc_get_fs(ctx, fs_type);
56     return client_create_port(hset, &self->client_ctx, port_name(fs_type));
57 }
58 
client_port_context_destroy(struct client_port_context * self,enum storage_filesystem_type fs_type,struct block_device_tipc * ctx)59 static void client_port_context_destroy(struct client_port_context* self,
60                                         enum storage_filesystem_type fs_type,
61                                         struct block_device_tipc* ctx) {
62     if (!block_device_tipc_fs_connected(ctx, fs_type)) {
63         return;
64     }
65     ipc_port_destroy(&self->client_ctx);
66 }
67 
storage_tipc_service_init(struct storage_tipc_service * self,struct block_device_tipc * ctx,struct tipc_hset * hset)68 int storage_tipc_service_init(struct storage_tipc_service* self,
69                               struct block_device_tipc* ctx,
70                               struct tipc_hset* hset) {
71     int ret = client_port_context_init(&self->fs_rpmb, STORAGE_TP, ctx, hset);
72     if (ret < 0) {
73         goto err_fs_rpmb_create_port;
74     }
75 
76     ret = client_port_context_init(&self->fs_rpmb_boot, STORAGE_TDEA, ctx,
77                                    hset);
78     if (ret < 0) {
79         goto err_fs_rpmb_boot_create_port;
80     }
81 
82     ret = client_port_context_init(&self->fs_tdp, STORAGE_TDP, ctx, hset);
83     if (ret < 0) {
84         goto err_fs_tdp_create_port;
85     }
86 
87     ret = client_port_context_init(&self->fs_ns, STORAGE_TD, ctx, hset);
88     if (ret < 0) {
89         goto err_fs_ns_create_port;
90     }
91 
92     ret = client_port_context_init(&self->fs_nsp, STORAGE_NSP, ctx, hset);
93     if (ret < 0) {
94         goto err_fs_nsp_create_port;
95     }
96     return 0;
97 
98 err_fs_nsp_create_port:
99     client_port_context_destroy(&self->fs_ns, STORAGE_TD, ctx);
100 err_fs_ns_create_port:
101     client_port_context_destroy(&self->fs_tdp, STORAGE_TDP, ctx);
102 err_fs_tdp_create_port:
103     client_port_context_destroy(&self->fs_rpmb_boot, STORAGE_TDEA, ctx);
104 err_fs_rpmb_boot_create_port:
105     client_port_context_destroy(&self->fs_rpmb, STORAGE_TP, ctx);
106 err_fs_rpmb_create_port:
107     return ret;
108 }
109 
storage_tipc_service_destroy(struct storage_tipc_service * self,struct block_device_tipc * ctx)110 void storage_tipc_service_destroy(struct storage_tipc_service* self,
111                                   struct block_device_tipc* ctx) {
112     client_port_context_destroy(&self->fs_nsp, STORAGE_NSP, ctx);
113     client_port_context_destroy(&self->fs_ns, STORAGE_TD, ctx);
114     client_port_context_destroy(&self->fs_tdp, STORAGE_TDP, ctx);
115     client_port_context_destroy(&self->fs_rpmb_boot, STORAGE_TDEA, ctx);
116     client_port_context_destroy(&self->fs_rpmb, STORAGE_TP, ctx);
117 }
118