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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Author: Nathan Rutman <nathan.rutman@sun.com>
38 * Kernel <-> userspace communication routines.
39 * Using pipes for all arches.
42 #define DEBUG_SUBSYSTEM S_CLASS
45 #include "../../include/linux/libcfs/libcfs.h"
47 /* This is the kernel side (liblustre as well). */
50 * libcfs_kkuc_msg_put - send an message from kernel to userspace
51 * @param fp to send the message to
52 * @param payload Payload data. First field of payload is always
55 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
57 struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
58 ssize_t count = kuch->kuc_msglen;
63 if (filp == NULL || IS_ERR(filp))
66 if (kuch->kuc_magic != KUC_MAGIC) {
67 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
74 rc = vfs_write(filp, (void __force __user *)payload,
85 CWARN("message send failed (%d)\n", rc);
87 CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp);
91 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
93 /* Broadcast groups are global across all mounted filesystems;
94 * i.e. registering for a group on 1 fs will get messages for that
95 * group from any fs */
96 /** A single group registration has a uid and a file pointer */
98 struct list_head kr_chain;
104 static struct list_head kkuc_groups[KUC_GRP_MAX+1] = {};
105 /* Protect message sending against remove and adds */
106 static DECLARE_RWSEM(kg_sem);
108 /** Add a receiver to a broadcast group
109 * @param filp pipe to write into
110 * @param uid identifier for this receiver
111 * @param group group number
113 int libcfs_kkuc_group_add(struct file *filp, int uid, int group, __u32 data)
115 struct kkuc_reg *reg;
117 if (group > KUC_GRP_MAX) {
118 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
122 /* fput in group_rem */
126 /* freed in group_rem */
127 reg = kmalloc(sizeof(*reg), 0);
136 if (kkuc_groups[group].next == NULL)
137 INIT_LIST_HEAD(&kkuc_groups[group]);
138 list_add(®->kr_chain, &kkuc_groups[group]);
141 CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
145 EXPORT_SYMBOL(libcfs_kkuc_group_add);
147 int libcfs_kkuc_group_rem(int uid, int group)
149 struct kkuc_reg *reg, *next;
151 if (kkuc_groups[group].next == NULL)
155 /* Broadcast a shutdown message */
158 lh.kuc_magic = KUC_MAGIC;
159 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
160 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
161 lh.kuc_msglen = sizeof(lh);
162 libcfs_kkuc_group_put(group, &lh);
166 list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
167 if ((uid == 0) || (uid == reg->kr_uid)) {
168 list_del(®->kr_chain);
169 CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
170 reg->kr_uid, reg->kr_fp, group);
171 if (reg->kr_fp != NULL)
180 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
182 int libcfs_kkuc_group_put(int group, void *payload)
184 struct kkuc_reg *reg;
189 list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
190 if (reg->kr_fp != NULL) {
191 rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
194 else if (rc == -EPIPE) {
202 /* don't return an error if the message has been delivered
203 * at least to one agent */
209 EXPORT_SYMBOL(libcfs_kkuc_group_put);
212 * Calls a callback function for each link of the given kuc group.
213 * @param group the group to call the function on.
214 * @param cb_func the function to be called.
215 * @param cb_arg iextra argument to be passed to the callback function.
217 int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func,
220 struct kkuc_reg *reg;
223 if (group > KUC_GRP_MAX) {
224 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
228 /* no link for this group */
229 if (kkuc_groups[group].next == NULL)
233 list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
234 if (reg->kr_fp != NULL)
235 rc = cb_func(reg->kr_data, cb_arg);
241 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);