Merge tag 'f2fs-for-6.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk...
[platform/kernel/linux-starfive.git] / fs / dlm / netlink.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2007 Red Hat, Inc.  All rights reserved.
4  */
5
6 #include <net/genetlink.h>
7 #include <linux/dlm.h>
8 #include <linux/dlm_netlink.h>
9 #include <linux/gfp.h>
10
11 #include "dlm_internal.h"
12
13 static uint32_t dlm_nl_seqnum;
14 static uint32_t listener_nlportid;
15
16 static struct genl_family family;
17
18 static int prepare_data(u8 cmd, struct sk_buff **skbp, size_t size)
19 {
20         struct sk_buff *skb;
21         void *data;
22
23         skb = genlmsg_new(size, GFP_NOFS);
24         if (!skb)
25                 return -ENOMEM;
26
27         /* add the message headers */
28         data = genlmsg_put(skb, 0, dlm_nl_seqnum++, &family, 0, cmd);
29         if (!data) {
30                 nlmsg_free(skb);
31                 return -EINVAL;
32         }
33
34         *skbp = skb;
35         return 0;
36 }
37
38 static struct dlm_lock_data *mk_data(struct sk_buff *skb)
39 {
40         struct nlattr *ret;
41
42         ret = nla_reserve(skb, DLM_TYPE_LOCK, sizeof(struct dlm_lock_data));
43         if (!ret)
44                 return NULL;
45         return nla_data(ret);
46 }
47
48 static int send_data(struct sk_buff *skb)
49 {
50         struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
51         void *data = genlmsg_data(genlhdr);
52
53         genlmsg_end(skb, data);
54
55         return genlmsg_unicast(&init_net, skb, listener_nlportid);
56 }
57
58 static int user_cmd(struct sk_buff *skb, struct genl_info *info)
59 {
60         listener_nlportid = info->snd_portid;
61         printk("user_cmd nlpid %u\n", listener_nlportid);
62         return 0;
63 }
64
65 static const struct genl_small_ops dlm_nl_ops[] = {
66         {
67                 .cmd    = DLM_CMD_HELLO,
68                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
69                 .doit   = user_cmd,
70         },
71 };
72
73 static struct genl_family family __ro_after_init = {
74         .name           = DLM_GENL_NAME,
75         .version        = DLM_GENL_VERSION,
76         .small_ops      = dlm_nl_ops,
77         .n_small_ops    = ARRAY_SIZE(dlm_nl_ops),
78         .resv_start_op  = DLM_CMD_HELLO + 1,
79         .module         = THIS_MODULE,
80 };
81
82 int __init dlm_netlink_init(void)
83 {
84         return genl_register_family(&family);
85 }
86
87 void dlm_netlink_exit(void)
88 {
89         genl_unregister_family(&family);
90 }
91
92 static void fill_data(struct dlm_lock_data *data, struct dlm_lkb *lkb)
93 {
94         struct dlm_rsb *r = lkb->lkb_resource;
95
96         memset(data, 0, sizeof(struct dlm_lock_data));
97
98         data->version = DLM_LOCK_DATA_VERSION;
99         data->nodeid = lkb->lkb_nodeid;
100         data->ownpid = lkb->lkb_ownpid;
101         data->id = lkb->lkb_id;
102         data->remid = lkb->lkb_remid;
103         data->status = lkb->lkb_status;
104         data->grmode = lkb->lkb_grmode;
105         data->rqmode = lkb->lkb_rqmode;
106         if (lkb->lkb_ua)
107                 data->xid = lkb->lkb_ua->xid;
108         if (r) {
109                 data->lockspace_id = r->res_ls->ls_global_id;
110                 data->resource_namelen = r->res_length;
111                 memcpy(data->resource_name, r->res_name, r->res_length);
112         }
113 }
114
115 void dlm_timeout_warn(struct dlm_lkb *lkb)
116 {
117         struct sk_buff *send_skb;
118         struct dlm_lock_data *data;
119         size_t size;
120         int rv;
121
122         size = nla_total_size(sizeof(struct dlm_lock_data)) +
123                nla_total_size(0); /* why this? */
124
125         rv = prepare_data(DLM_CMD_TIMEOUT, &send_skb, size);
126         if (rv < 0)
127                 return;
128
129         data = mk_data(send_skb);
130         if (!data) {
131                 nlmsg_free(send_skb);
132                 return;
133         }
134
135         fill_data(data, lkb);
136
137         send_data(send_skb);
138 }
139