4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2015, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * Author: Nathan Rutman <nathan.rutman@sun.com>
33 * Kernel <-> userspace communication routines.
34 * Using pipes for all arches.
37 #define DEBUG_SUBSYSTEM S_CLASS
39 #include <linux/file.h>
40 #include <linux/glob.h>
41 #include <net/genetlink.h>
44 #include <libcfs/linux/linux-net.h>
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_kernelcomm.h>
49 static struct genl_family lustre_family;
51 static struct ln_key_list device_list = {
52 .lkl_maxattr = LUSTRE_DEVICE_ATTR_MAX,
54 [LUSTRE_DEVICE_ATTR_HDR] = {
55 .lkp_value = "devices",
56 .lkp_key_format = LNKF_SEQUENCE | LNKF_MAPPING,
57 .lkp_data_type = NLA_NUL_STRING,
59 [LUSTRE_DEVICE_ATTR_INDEX] = {
61 .lkp_data_type = NLA_U16
63 [LUSTRE_DEVICE_ATTR_STATUS] = {
64 .lkp_value = "status",
65 .lkp_data_type = NLA_STRING
67 [LUSTRE_DEVICE_ATTR_CLASS] = {
69 .lkp_data_type = NLA_STRING
71 [LUSTRE_DEVICE_ATTR_NAME] = {
73 .lkp_data_type = NLA_STRING
75 [LUSTRE_DEVICE_ATTR_UUID] = {
77 .lkp_data_type = NLA_STRING
79 [LUSTRE_DEVICE_ATTR_REFCOUNT] = {
80 .lkp_value = "refcount",
81 .lkp_data_type = NLA_U32
86 struct genl_dev_list {
87 struct obd_device *gdl_target;
88 unsigned int gdl_start;
91 static inline struct genl_dev_list *
92 device_dump_ctx(struct netlink_callback *cb)
94 return (struct genl_dev_list *)cb->args[0];
97 /* generic ->start() handler for GET requests */
98 static int lustre_device_list_start(struct netlink_callback *cb)
100 struct genlmsghdr *gnlh = nlmsg_data(cb->nlh);
101 #ifdef HAVE_NL_PARSE_WITH_EXT_ACK
102 struct netlink_ext_ack *extack = NULL;
104 struct genl_dev_list *glist;
107 #ifdef HAVE_NL_DUMP_WITH_EXT_ACK
110 OBD_ALLOC(glist, sizeof(*glist));
114 cb->args[0] = (long)glist;
115 glist->gdl_target = NULL;
116 glist->gdl_start = 0;
118 msg_len = genlmsg_len(gnlh);
120 struct nlattr *params = genlmsg_data(gnlh);
124 if (!(nla_type(params) & LN_SCALAR_ATTR_LIST)) {
125 NL_SET_ERR_MSG(extack, "no configuration");
126 GOTO(report_err, rc);
129 nla_for_each_nested(dev, params, rem) {
133 nla_for_each_nested(prop, dev, rem2) {
134 char name[MAX_OBD_NAME];
135 struct obd_device *obd;
137 if (nla_type(prop) != LN_SCALAR_ATTR_VALUE ||
138 nla_strcmp(prop, "name") != 0)
141 prop = nla_next(prop, &rem2);
142 if (nla_type(prop) != LN_SCALAR_ATTR_VALUE)
143 GOTO(report_err, rc = -EINVAL);
145 rc = nla_strscpy(name, prop, sizeof(name));
147 GOTO(report_err, rc);
150 obd = class_name2obd(name);
152 glist->gdl_target = obd;
155 if (!glist->gdl_target) {
156 NL_SET_ERR_MSG(extack, "No devices found");
162 OBD_FREE(glist, sizeof(*glist));
168 static int lustre_device_list_dump(struct sk_buff *msg,
169 struct netlink_callback *cb)
171 struct genl_dev_list *glist = device_dump_ctx(cb);
172 struct obd_device *filter = glist->gdl_target;
173 #ifdef HAVE_NL_PARSE_WITH_EXT_ACK
174 struct netlink_ext_ack *extack = NULL;
176 int portid = NETLINK_CB(cb->skb).portid;
177 int seq = cb->nlh->nlmsg_seq;
180 #ifdef HAVE_NL_DUMP_WITH_EXT_ACK
183 if (glist->gdl_start == 0) {
184 const struct ln_key_list *all[] = {
188 rc = lnet_genl_send_scalar_list(msg, portid, seq,
190 NLM_F_CREATE | NLM_F_MULTI,
191 LUSTRE_CMD_DEVICES, all);
193 NL_SET_ERR_MSG(extack, "failed to send key table");
198 for (idx = glist->gdl_start; idx < class_devno_max(); idx++) {
199 struct obd_device *obd;
203 obd = class_num2obd(idx);
207 if (filter && filter != obd)
210 hdr = genlmsg_put(msg, portid, seq, &lustre_family,
211 NLM_F_MULTI, LUSTRE_CMD_DEVICES);
213 NL_SET_ERR_MSG(extack, "failed to send values");
214 genlmsg_cancel(msg, hdr);
220 nla_put_string(msg, LUSTRE_DEVICE_ATTR_HDR, "");
222 nla_put_u16(msg, LUSTRE_DEVICE_ATTR_INDEX, obd->obd_minor);
224 /* Collect only the index value for a single obd */
226 genlmsg_end(msg, hdr);
231 if (obd->obd_stopping)
233 else if (obd->obd_inactive)
235 else if (obd->obd_set_up)
237 else if (obd->obd_attached)
242 nla_put_string(msg, LUSTRE_DEVICE_ATTR_STATUS, status);
244 nla_put_string(msg, LUSTRE_DEVICE_ATTR_CLASS,
245 obd->obd_type->typ_name);
247 nla_put_string(msg, LUSTRE_DEVICE_ATTR_NAME,
250 nla_put_string(msg, LUSTRE_DEVICE_ATTR_UUID,
253 nla_put_u32(msg, LUSTRE_DEVICE_ATTR_REFCOUNT,
254 atomic_read(&obd->obd_refcount));
256 genlmsg_end(msg, hdr);
259 glist->gdl_start = idx;
260 rc = lnet_nl_send_error(cb->skb, portid, seq, rc);
261 return rc < 0 ? rc : msg->len;
264 #ifndef HAVE_NETLINK_CALLBACK_START
265 int lustre_old_device_list_dump(struct sk_buff *msg,
266 struct netlink_callback *cb)
269 int rc = lustre_device_list_start(cb);
275 return lustre_device_list_dump(msg, cb);
279 int lustre_device_done(struct netlink_callback *cb)
281 struct genl_dev_list *glist;
283 glist = device_dump_ctx(cb);
285 OBD_FREE(glist, sizeof(*glist));
292 static const struct genl_multicast_group lustre_mcast_grps[] = {
293 { .name = "devices", },
296 static const struct genl_ops lustre_genl_ops[] = {
298 .cmd = LUSTRE_CMD_DEVICES,
299 #ifdef HAVE_NETLINK_CALLBACK_START
300 .start = lustre_device_list_start,
301 .dumpit = lustre_device_list_dump,
303 .dumpit = lustre_old_device_list_dump,
305 .done = lustre_device_done,
309 static struct genl_family lustre_family = {
310 .name = LUSTRE_GENL_NAME,
311 .version = LUSTRE_GENL_VERSION,
312 .module = THIS_MODULE,
313 .ops = lustre_genl_ops,
314 .n_ops = ARRAY_SIZE(lustre_genl_ops),
315 .mcgrps = lustre_mcast_grps,
316 .n_mcgrps = ARRAY_SIZE(lustre_mcast_grps),
320 * libcfs_kkuc_msg_put - send an message from kernel to userspace
321 * @param fp to send the message to
322 * @param payload Payload data. First field of payload is always
325 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
327 struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
328 ssize_t count = kuch->kuc_msglen;
332 if (IS_ERR_OR_NULL(filp))
335 if (kuch->kuc_magic != KUC_MAGIC) {
336 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
341 rc = cfs_kernel_write(filp, payload, count, &offset);
350 CWARN("message send failed (%d)\n", rc);
352 CDEBUG(D_HSM, "Sent message rc=%d, fp=%p\n", rc, filp);
356 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
358 /* Broadcast groups are global across all mounted filesystems;
359 * i.e. registering for a group on 1 fs will get messages for that
360 * group from any fs */
361 /** A single group registration has a uid and a file pointer */
363 struct list_head kr_chain;
364 struct obd_uuid kr_uuid;
370 static struct list_head kkuc_groups[KUC_GRP_MAX + 1];
371 /* Protect message sending against remove and adds */
372 static DECLARE_RWSEM(kg_sem);
374 static inline bool libcfs_kkuc_group_is_valid(int group)
376 return 0 <= group && group < ARRAY_SIZE(kkuc_groups);
379 int libcfs_kkuc_init(void)
383 for (group = 0; group < ARRAY_SIZE(kkuc_groups); group++)
384 INIT_LIST_HEAD(&kkuc_groups[group]);
386 return genl_register_family(&lustre_family);
389 void libcfs_kkuc_fini(void)
391 genl_unregister_family(&lustre_family);
394 /** Add a receiver to a broadcast group
395 * @param filp pipe to write into
396 * @param uid identifier for this receiver
397 * @param group group number
398 * @param data user data
400 int libcfs_kkuc_group_add(struct file *filp, const struct obd_uuid *uuid,
401 int uid, int group, void *data, size_t data_len)
403 struct kkuc_reg *reg;
405 if (!libcfs_kkuc_group_is_valid(group)) {
406 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
410 /* fput in group_rem */
414 /* freed in group_rem */
415 reg = kzalloc(sizeof(*reg) + data_len, 0);
419 reg->kr_uuid = *uuid;
422 memcpy(reg->kr_data, data, data_len);
425 list_add(®->kr_chain, &kkuc_groups[group]);
428 CDEBUG(D_HSM, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
432 EXPORT_SYMBOL(libcfs_kkuc_group_add);
434 int libcfs_kkuc_group_rem(const struct obd_uuid *uuid, int uid, int group)
436 struct kkuc_reg *reg, *next;
439 if (!libcfs_kkuc_group_is_valid(group)) {
440 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
445 /* Broadcast a shutdown message */
448 lh.kuc_magic = KUC_MAGIC;
449 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
450 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
451 lh.kuc_msglen = sizeof(lh);
452 libcfs_kkuc_group_put(uuid, group, &lh);
456 list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
457 if (obd_uuid_equals(uuid, ®->kr_uuid) &&
458 (uid == 0 || uid == reg->kr_uid)) {
459 list_del(®->kr_chain);
460 CDEBUG(D_HSM, "Removed uid=%d fp=%p from group %d\n",
461 reg->kr_uid, reg->kr_fp, group);
462 if (reg->kr_fp != NULL)
471 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
473 int libcfs_kkuc_group_put(const struct obd_uuid *uuid, int group, void *payload)
475 struct kkuc_reg *reg;
480 if (!libcfs_kkuc_group_is_valid(group)) {
481 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
487 if (unlikely(list_empty(&kkuc_groups[group])) ||
488 unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_CT_REGISTER_NET))) {
489 /* no agent have fully registered, CDT will retry */
494 list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
495 if (obd_uuid_equals(uuid, ®->kr_uuid) &&
496 reg->kr_fp != NULL) {
497 rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
500 else if (rc == -EPIPE) {
508 /* don't return an error if the message has been delivered
509 * at least to one agent */
515 EXPORT_SYMBOL(libcfs_kkuc_group_put);
518 * Calls a callback function for each link of the given kuc group.
519 * @param group the group to call the function on.
520 * @param cb_func the function to be called.
521 * @param cb_arg extra argument to be passed to the callback function.
523 int libcfs_kkuc_group_foreach(const struct obd_uuid *uuid, int group,
524 libcfs_kkuc_cb_t cb_func, void *cb_arg)
526 struct kkuc_reg *reg;
530 if (!libcfs_kkuc_group_is_valid(group)) {
531 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
536 list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
537 if (obd_uuid_equals(uuid, ®->kr_uuid) && reg->kr_fp != NULL)
538 rc = cb_func(reg->kr_data, cb_arg);
544 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);