xref: /kernel/linux/linux-6.6/fs/hmdfs/comm/node_cb.c (revision 62306a36)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * fs/hmdfs/comm/node_cb.c
4 *
5 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
6 */
7
8#include <linux/list.h>
9
10#include "node_cb.h"
11#include "connection.h"
12
13static struct list_head cb_head[NODE_EVT_NR][NODE_EVT_TYPE_NR];
14
15static const char *evt_str_tbl[NODE_EVT_NR] = {
16	"add", "online", "offline", "del",
17};
18
19static inline bool hmdfs_is_valid_node_evt(int evt)
20{
21	return (evt >= 0 && evt < NODE_EVT_NR);
22}
23
24static const char *hmdfs_evt_str(int evt)
25{
26	if (!hmdfs_is_valid_node_evt(evt))
27		return "unknown";
28	return evt_str_tbl[evt];
29}
30
31void hmdfs_node_evt_cb_init(void)
32{
33	int i;
34
35	for (i = 0; i < ARRAY_SIZE(cb_head); i++) {
36		int j;
37
38		for (j = 0; j < ARRAY_SIZE(cb_head[0]); j++)
39			INIT_LIST_HEAD(&cb_head[i][j]);
40	}
41}
42
43void hmdfs_node_add_evt_cb(struct hmdfs_node_cb_desc *desc, int nr)
44{
45	int i;
46
47	for (i = 0; i < nr; i++) {
48		int evt = desc[i].evt;
49		bool sync = desc[i].sync;
50
51		if (!hmdfs_is_valid_node_evt(evt))
52			continue;
53
54		list_add_tail(&desc[i].list, &cb_head[evt][sync]);
55	}
56}
57
58void hmdfs_node_call_evt_cb(struct hmdfs_peer *conn, int evt, bool sync,
59			    unsigned int seq)
60{
61	struct hmdfs_node_cb_desc *desc = NULL;
62
63	hmdfs_info("node 0x%x:0x%llx call %s %s cb seq %u",
64		   conn->owner, conn->device_id, hmdfs_evt_str(evt),
65		   sync ? "sync" : "async", seq);
66
67	if (!hmdfs_is_valid_node_evt(evt))
68		return;
69
70	list_for_each_entry(desc, &cb_head[evt][sync], list) {
71		desc->fn(conn, evt, seq);
72	}
73}
74