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 nla_for_each_attr(dev, params, msg_len, rem) {
128 nla_for_each_nested(prop, dev, rem2) {
129 char name[MAX_OBD_NAME];
130 struct obd_device *obd;
132 if (nla_type(prop) != LN_SCALAR_ATTR_VALUE ||
133 nla_strcmp(prop, "name") != 0)
136 prop = nla_next(prop, &rem2);
137 if (nla_type(prop) != LN_SCALAR_ATTR_VALUE)
138 GOTO(report_err, rc = -EINVAL);
140 rc = nla_strscpy(name, prop, sizeof(name));
142 GOTO(report_err, rc);
145 obd = class_name2obd(name);
147 glist->gdl_target = obd;
150 if (!glist->gdl_target) {
151 NL_SET_ERR_MSG(extack, "No devices found");
157 OBD_FREE(glist, sizeof(*glist));
163 static int lustre_device_list_dump(struct sk_buff *msg,
164 struct netlink_callback *cb)
166 struct genl_dev_list *glist = device_dump_ctx(cb);
167 struct obd_device *filter = glist->gdl_target;
168 #ifdef HAVE_NL_PARSE_WITH_EXT_ACK
169 struct netlink_ext_ack *extack = NULL;
171 int portid = NETLINK_CB(cb->skb).portid;
172 int seq = cb->nlh->nlmsg_seq;
175 #ifdef HAVE_NL_DUMP_WITH_EXT_ACK
178 if (glist->gdl_start == 0) {
179 const struct ln_key_list *all[] = {
183 rc = lnet_genl_send_scalar_list(msg, portid, seq,
185 NLM_F_CREATE | NLM_F_MULTI,
186 LUSTRE_CMD_DEVICES, all);
188 NL_SET_ERR_MSG(extack, "failed to send key table");
193 for (idx = glist->gdl_start; idx < class_devno_max(); idx++) {
194 struct obd_device *obd;
198 obd = class_num2obd(idx);
202 if (filter && filter != obd)
205 hdr = genlmsg_put(msg, portid, seq, &lustre_family,
206 NLM_F_MULTI, LUSTRE_CMD_DEVICES);
208 NL_SET_ERR_MSG(extack, "failed to send values");
209 genlmsg_cancel(msg, hdr);
215 nla_put_string(msg, LUSTRE_DEVICE_ATTR_HDR, "");
217 nla_put_u16(msg, LUSTRE_DEVICE_ATTR_INDEX, obd->obd_minor);
219 /* Collect only the index value for a single obd */
221 genlmsg_end(msg, hdr);
226 if (obd->obd_stopping)
228 else if (obd->obd_inactive)
230 else if (obd->obd_set_up)
232 else if (obd->obd_attached)
237 nla_put_string(msg, LUSTRE_DEVICE_ATTR_STATUS, status);
239 nla_put_string(msg, LUSTRE_DEVICE_ATTR_CLASS,
240 obd->obd_type->typ_name);
242 nla_put_string(msg, LUSTRE_DEVICE_ATTR_NAME,
245 nla_put_string(msg, LUSTRE_DEVICE_ATTR_UUID,
248 nla_put_u32(msg, LUSTRE_DEVICE_ATTR_REFCOUNT,
249 atomic_read(&obd->obd_refcount));
251 genlmsg_end(msg, hdr);
254 glist->gdl_start = idx;
255 rc = lnet_nl_send_error(cb->skb, portid, seq, rc);
256 return rc < 0 ? rc : msg->len;
259 #ifndef HAVE_NETLINK_CALLBACK_START
260 int lustre_old_device_list_dump(struct sk_buff *msg,
261 struct netlink_callback *cb)
264 int rc = lustre_device_list_start(cb);
270 return lustre_device_list_dump(msg, cb);
274 int lustre_device_done(struct netlink_callback *cb)
276 struct genl_dev_list *glist;
278 glist = device_dump_ctx(cb);
280 OBD_FREE(glist, sizeof(*glist));
287 static const struct genl_multicast_group lustre_mcast_grps[] = {
288 { .name = "devices", },
291 static const struct genl_ops lustre_genl_ops[] = {
293 .cmd = LUSTRE_CMD_DEVICES,
294 #ifdef HAVE_NETLINK_CALLBACK_START
295 .start = lustre_device_list_start,
296 .dumpit = lustre_device_list_dump,
298 .dumpit = lustre_old_device_list_dump,
300 .done = lustre_device_done,
304 static struct genl_family lustre_family = {
305 .name = LUSTRE_GENL_NAME,
306 .version = LUSTRE_GENL_VERSION,
307 .module = THIS_MODULE,
308 .ops = lustre_genl_ops,
309 .n_ops = ARRAY_SIZE(lustre_genl_ops),
310 .mcgrps = lustre_mcast_grps,
311 .n_mcgrps = ARRAY_SIZE(lustre_mcast_grps),
315 * libcfs_kkuc_msg_put - send an message from kernel to userspace
316 * @param fp to send the message to
317 * @param payload Payload data. First field of payload is always
320 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
322 struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
323 ssize_t count = kuch->kuc_msglen;
327 if (IS_ERR_OR_NULL(filp))
330 if (kuch->kuc_magic != KUC_MAGIC) {
331 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
336 rc = cfs_kernel_write(filp, payload, count, &offset);
345 CWARN("message send failed (%d)\n", rc);
347 CDEBUG(D_HSM, "Sent message rc=%d, fp=%p\n", rc, filp);
351 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
353 /* Broadcast groups are global across all mounted filesystems;
354 * i.e. registering for a group on 1 fs will get messages for that
355 * group from any fs */
356 /** A single group registration has a uid and a file pointer */
358 struct list_head kr_chain;
359 struct obd_uuid kr_uuid;
365 static struct list_head kkuc_groups[KUC_GRP_MAX + 1];
366 /* Protect message sending against remove and adds */
367 static DECLARE_RWSEM(kg_sem);
369 static inline bool libcfs_kkuc_group_is_valid(int group)
371 return 0 <= group && group < ARRAY_SIZE(kkuc_groups);
374 int libcfs_kkuc_init(void)
378 for (group = 0; group < ARRAY_SIZE(kkuc_groups); group++)
379 INIT_LIST_HEAD(&kkuc_groups[group]);
381 return genl_register_family(&lustre_family);
384 void libcfs_kkuc_fini(void)
386 genl_unregister_family(&lustre_family);
389 /** Add a receiver to a broadcast group
390 * @param filp pipe to write into
391 * @param uid identifier for this receiver
392 * @param group group number
393 * @param data user data
395 int libcfs_kkuc_group_add(struct file *filp, const struct obd_uuid *uuid,
396 int uid, int group, void *data, size_t data_len)
398 struct kkuc_reg *reg;
400 if (!libcfs_kkuc_group_is_valid(group)) {
401 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
405 /* fput in group_rem */
409 /* freed in group_rem */
410 reg = kzalloc(sizeof(*reg) + data_len, 0);
414 reg->kr_uuid = *uuid;
417 memcpy(reg->kr_data, data, data_len);
420 list_add(®->kr_chain, &kkuc_groups[group]);
423 CDEBUG(D_HSM, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
427 EXPORT_SYMBOL(libcfs_kkuc_group_add);
429 int libcfs_kkuc_group_rem(const struct obd_uuid *uuid, int uid, int group)
431 struct kkuc_reg *reg, *next;
434 if (!libcfs_kkuc_group_is_valid(group)) {
435 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
440 /* Broadcast a shutdown message */
443 lh.kuc_magic = KUC_MAGIC;
444 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
445 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
446 lh.kuc_msglen = sizeof(lh);
447 libcfs_kkuc_group_put(uuid, group, &lh);
451 list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
452 if (obd_uuid_equals(uuid, ®->kr_uuid) &&
453 (uid == 0 || uid == reg->kr_uid)) {
454 list_del(®->kr_chain);
455 CDEBUG(D_HSM, "Removed uid=%d fp=%p from group %d\n",
456 reg->kr_uid, reg->kr_fp, group);
457 if (reg->kr_fp != NULL)
466 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
468 int libcfs_kkuc_group_put(const struct obd_uuid *uuid, int group, void *payload)
470 struct kkuc_reg *reg;
475 if (!libcfs_kkuc_group_is_valid(group)) {
476 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
482 if (unlikely(list_empty(&kkuc_groups[group])) ||
483 unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_CT_REGISTER_NET))) {
484 /* no agent have fully registered, CDT will retry */
489 list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
490 if (obd_uuid_equals(uuid, ®->kr_uuid) &&
491 reg->kr_fp != NULL) {
492 rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
495 else if (rc == -EPIPE) {
503 /* don't return an error if the message has been delivered
504 * at least to one agent */
510 EXPORT_SYMBOL(libcfs_kkuc_group_put);
513 * Calls a callback function for each link of the given kuc group.
514 * @param group the group to call the function on.
515 * @param cb_func the function to be called.
516 * @param cb_arg extra argument to be passed to the callback function.
518 int libcfs_kkuc_group_foreach(const struct obd_uuid *uuid, int group,
519 libcfs_kkuc_cb_t cb_func, void *cb_arg)
521 struct kkuc_reg *reg;
525 if (!libcfs_kkuc_group_is_valid(group)) {
526 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
531 list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
532 if (obd_uuid_equals(uuid, ®->kr_uuid) && reg->kr_fp != NULL)
533 rc = cb_func(reg->kr_data, cb_arg);
539 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);