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 <libcfs/libcfs.h>
48 /* This is the userspace side. */
50 /** Start the userspace side of a KUC pipe.
51 * @param link Private descriptor for pipe/socket.
52 * @param groups KUC broadcast group to listen to
53 * (can be null for unicast to this pid)
55 int libcfs_ukuc_start(lustre_kernelcomm *link, int group)
59 link->lk_rfd = link->lk_wfd = LK_NOFD;
64 memset(link, 0, sizeof(*link));
65 link->lk_rfd = pfd[0];
66 link->lk_wfd = pfd[1];
67 link->lk_group = group;
68 link->lk_uid = getpid();
72 int libcfs_ukuc_stop(lustre_kernelcomm *link)
76 if (link->lk_wfd != LK_NOFD)
78 rc = close(link->lk_rfd);
79 link->lk_rfd = link->lk_wfd = LK_NOFD;
83 #define lhsz sizeof(*kuch)
85 /** Read a message from the link.
86 * Allocates memory, returns handle
88 * @param link Private descriptor for pipe/socket.
89 * @param buf Buffer to read into, must include size for kuc_hdr
90 * @param maxsize Maximum message size allowed
91 * @param transport Only listen to messages on this transport
92 * (and the generic transport)
94 int libcfs_ukuc_msg_get(lustre_kernelcomm *link, char *buf, int maxsize,
100 memset(buf, 0, maxsize);
102 CDEBUG(D_KUC, "Waiting for message from kernel on fd %d\n",
106 /* Read header first to get message size */
107 rc = read(link->lk_rfd, buf, lhsz);
112 kuch = (struct kuc_hdr *)buf;
114 CDEBUG(D_KUC, "Received message mg=%x t=%d m=%d l=%d\n",
115 kuch->kuc_magic, kuch->kuc_transport, kuch->kuc_msgtype,
118 if (kuch->kuc_magic != KUC_MAGIC) {
119 CERROR("bad message magic %x != %x\n",
120 kuch->kuc_magic, KUC_MAGIC);
125 if (kuch->kuc_msglen > maxsize) {
131 rc = read(link->lk_rfd, buf + lhsz, kuch->kuc_msglen - lhsz);
136 if (rc < (kuch->kuc_msglen - lhsz)) {
137 CERROR("short read: got %d of %d bytes\n",
138 rc, kuch->kuc_msglen);
143 if (kuch->kuc_transport == transport ||
144 kuch->kuc_transport == KUC_TRANSPORT_GENERIC) {
147 /* Drop messages for other transports */
152 #else /* LUSTRE_UTILS */
153 /* This is the kernel side (liblustre as well). */
156 * libcfs_kkuc_msg_put - send an message from kernel to userspace
157 * @param fp to send the message to
158 * @param payload Payload data. First field of payload is always
161 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
163 struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
166 if (filp == NULL || IS_ERR(filp))
169 if (kuch->kuc_magic != KUC_MAGIC) {
170 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
177 rc = filp_user_write(filp, payload, kuch->kuc_msglen,
183 CWARN("message send failed (%d)\n", rc);
185 CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp);
189 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
191 /* Broadcast groups are global across all mounted filesystems;
192 * i.e. registering for a group on 1 fs will get messages for that
193 * group from any fs */
194 /** A single group registration has a uid and a file pointer */
202 static cfs_list_t kkuc_groups[KUC_GRP_MAX+1] = {};
203 /* Protect message sending against remove and adds */
204 static DECLARE_RWSEM(kg_sem);
206 /** Add a receiver to a broadcast group
207 * @param filp pipe to write into
208 * @param uid identifier for this receiver
209 * @param group group number
210 * @param data user data
212 int libcfs_kkuc_group_add(struct file *filp, int uid, int group, void *data)
214 struct kkuc_reg *reg;
216 if (group > KUC_GRP_MAX) {
217 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
221 /* fput in group_rem */
225 /* freed in group_rem */
226 reg = kmalloc(sizeof(*reg), 0);
235 if (kkuc_groups[group].next == NULL)
236 CFS_INIT_LIST_HEAD(&kkuc_groups[group]);
237 cfs_list_add(®->kr_chain, &kkuc_groups[group]);
240 CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
244 EXPORT_SYMBOL(libcfs_kkuc_group_add);
246 int libcfs_kkuc_group_rem(int uid, int group, void **pdata)
248 struct kkuc_reg *reg, *next;
251 if (kkuc_groups[group].next == NULL)
255 /* Broadcast a shutdown message */
258 lh.kuc_magic = KUC_MAGIC;
259 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
260 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
261 lh.kuc_msglen = sizeof(lh);
262 libcfs_kkuc_group_put(group, &lh);
266 cfs_list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
267 if ((uid == 0) || (uid == reg->kr_uid)) {
268 cfs_list_del(®->kr_chain);
269 CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
270 reg->kr_uid, reg->kr_fp, group);
271 if (reg->kr_fp != NULL)
274 *pdata = reg->kr_data;
282 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
284 int libcfs_kkuc_group_put(int group, void *payload)
286 struct kkuc_reg *reg;
292 cfs_list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
293 if (reg->kr_fp != NULL) {
294 rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
297 else if (rc == -EPIPE) {
305 /* don't return an error if the message has been delivered
306 * at least to one agent */
312 EXPORT_SYMBOL(libcfs_kkuc_group_put);
315 * Calls a callback function for each link of the given kuc group.
316 * @param group the group to call the function on.
317 * @param cb_func the function to be called.
318 * @param cb_arg extra argument to be passed to the callback function.
320 int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func,
323 struct kkuc_reg *reg;
327 if (group > KUC_GRP_MAX) {
328 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
332 /* no link for this group */
333 if (kkuc_groups[group].next == NULL)
337 cfs_list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
338 if (reg->kr_fp != NULL) {
339 rc = cb_func(reg->kr_data, cb_arg);
346 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);
348 #endif /* LUSTRE_UTILS */