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, 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 <obd_support.h>
40 #include <lustre_kernelcomm.h>
42 /* write a userspace buffer to disk.
43 * NOTE: this returns 0 on success, not the number of bytes written. */
45 filp_user_write(struct file *filp, const void *buf, size_t count,
53 while ((ssize_t)count > 0) {
54 size = vfs_write(filp, (const void __user *)buf, count, offset);
67 * libcfs_kkuc_msg_put - send an message from kernel to userspace
68 * @param fp to send the message to
69 * @param payload Payload data. First field of payload is always
72 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
74 struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
78 if (filp == NULL || IS_ERR(filp))
81 if (kuch->kuc_magic != KUC_MAGIC) {
82 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
86 rc = filp_user_write(filp, payload, kuch->kuc_msglen, &offset);
88 CWARN("message send failed (%d)\n", rc);
90 CDEBUG(D_HSM, "Sent message rc=%d, fp=%p\n", rc, filp);
94 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
96 /* Broadcast groups are global across all mounted filesystems;
97 * i.e. registering for a group on 1 fs will get messages for that
98 * group from any fs */
99 /** A single group registration has a uid and a file pointer */
101 struct list_head kr_chain;
107 static struct list_head kkuc_groups[KUC_GRP_MAX + 1];
108 /* Protect message sending against remove and adds */
109 static DECLARE_RWSEM(kg_sem);
111 static inline bool libcfs_kkuc_group_is_valid(int group)
113 return 0 <= group && group < ARRAY_SIZE(kkuc_groups);
116 void libcfs_kkuc_init(void)
120 for (group = 0; group < ARRAY_SIZE(kkuc_groups); group++)
121 INIT_LIST_HEAD(&kkuc_groups[group]);
124 /** Add a receiver to a broadcast group
125 * @param filp pipe to write into
126 * @param uid identifier for this receiver
127 * @param group group number
128 * @param data user data
130 int libcfs_kkuc_group_add(struct file *filp, int uid, int group,
131 void *data, size_t data_len)
133 struct kkuc_reg *reg;
135 if (!libcfs_kkuc_group_is_valid(group)) {
136 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
140 /* fput in group_rem */
144 /* freed in group_rem */
145 reg = kmalloc(sizeof(*reg) + data_len, 0);
151 memcpy(reg->kr_data, data, data_len);
154 list_add(®->kr_chain, &kkuc_groups[group]);
157 CDEBUG(D_HSM, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
161 EXPORT_SYMBOL(libcfs_kkuc_group_add);
163 int libcfs_kkuc_group_rem(int uid, int group)
165 struct kkuc_reg *reg, *next;
168 if (!libcfs_kkuc_group_is_valid(group)) {
169 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
174 /* Broadcast a shutdown message */
177 lh.kuc_magic = KUC_MAGIC;
178 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
179 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
180 lh.kuc_msglen = sizeof(lh);
181 libcfs_kkuc_group_put(group, &lh);
185 list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
186 if ((uid == 0) || (uid == reg->kr_uid)) {
187 list_del(®->kr_chain);
188 CDEBUG(D_HSM, "Removed uid=%d fp=%p from group %d\n",
189 reg->kr_uid, reg->kr_fp, group);
190 if (reg->kr_fp != NULL)
199 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
201 int libcfs_kkuc_group_put(int group, void *payload)
203 struct kkuc_reg *reg;
208 if (!libcfs_kkuc_group_is_valid(group)) {
209 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
215 if (unlikely(list_empty(&kkuc_groups[group])) ||
216 unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_CT_REGISTER_NET))) {
217 /* no agent have fully registered, CDT will retry */
222 list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
223 if (reg->kr_fp != NULL) {
224 rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
227 else if (rc == -EPIPE) {
235 /* don't return an error if the message has been delivered
236 * at least to one agent */
242 EXPORT_SYMBOL(libcfs_kkuc_group_put);
245 * Calls a callback function for each link of the given kuc group.
246 * @param group the group to call the function on.
247 * @param cb_func the function to be called.
248 * @param cb_arg extra argument to be passed to the callback function.
250 int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func,
253 struct kkuc_reg *reg;
257 if (!libcfs_kkuc_group_is_valid(group)) {
258 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
263 list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
264 if (reg->kr_fp != NULL)
265 rc = cb_func(reg->kr_data, cb_arg);
271 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);