1 /*
2 * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12 #include <malloc.h>
13 #include <string.h>
14 #include "fsm_client_cap.h"
15 #include <errno.h>
16
17 struct list_head fsm_client_cap_table;
18
19 /* Return mount_id */
fsm_set_client_cap(badge_t client_badge,cap_t cap)20 int fsm_set_client_cap(badge_t client_badge, cap_t cap)
21 {
22 struct fsm_client_cap_node *n;
23
24 for_each_in_list (
25 n, struct fsm_client_cap_node, node, &fsm_client_cap_table) {
26 if (n->client_badge == client_badge) {
27 /* Client already visited */
28 BUG_ON(n->cap_num >= 16);
29 n->cap_table[n->cap_num] = cap;
30 n->cap_num++;
31 return n->cap_num - 1;
32 }
33 }
34
35 /* Client is not visited, create a new fsm_client_cap_node */
36 n = (struct fsm_client_cap_node *)malloc(sizeof(*n));
37 if (n == NULL) {
38 return -ENOMEM;
39 }
40 n->client_badge = client_badge;
41 memset(n->cap_table, 0, sizeof(n->cap_table));
42 n->cap_table[0] = cap;
43 n->cap_num = 1;
44
45 list_append(&n->node, &fsm_client_cap_table);
46
47 return 0;
48 }
49
50 /* Return mount_id if record exists, otherwise -1 */
fsm_get_client_cap(badge_t client_badge,cap_t cap)51 int fsm_get_client_cap(badge_t client_badge, cap_t cap)
52 {
53 struct fsm_client_cap_node *n;
54 int i;
55
56 for_each_in_list (
57 n, struct fsm_client_cap_node, node, &fsm_client_cap_table)
58 if (n->client_badge == client_badge)
59 for (i = 0; i < n->cap_num; i++)
60 if (n->cap_table[i] == cap)
61 return i;
62
63 return -1;
64 }
65