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)
54 * @param rfd_flags flags for read side of pipe (e.g. O_NONBLOCK)
56 int libcfs_ukuc_start(lustre_kernelcomm *link, int group, int rfd_flags)
61 link->lk_rfd = link->lk_wfd = LK_NOFD;
66 if (fcntl(pfd[0], F_SETFL, rfd_flags) < 0) {
73 memset(link, 0, sizeof(*link));
74 link->lk_rfd = pfd[0];
75 link->lk_wfd = pfd[1];
76 link->lk_group = group;
77 link->lk_uid = getpid();
81 int libcfs_ukuc_stop(lustre_kernelcomm *link)
85 if (link->lk_wfd != LK_NOFD)
87 rc = close(link->lk_rfd);
88 link->lk_rfd = link->lk_wfd = LK_NOFD;
92 /** Returns the file descriptor for the read side of the pipe,
93 * to be used with poll/select.
94 * @param link Private descriptor for pipe/socket.
96 int libcfs_ukuc_get_rfd(lustre_kernelcomm *link)
101 #define lhsz sizeof(*kuch)
103 /** Read a message from the link.
104 * Allocates memory, returns handle
106 * @param link Private descriptor for pipe/socket.
107 * @param buf Buffer to read into, must include size for kuc_hdr
108 * @param maxsize Maximum message size allowed
109 * @param transport Only listen to messages on this transport
110 * (and the generic transport)
112 int libcfs_ukuc_msg_get(lustre_kernelcomm *link, char *buf, int maxsize,
115 struct kuc_hdr *kuch;
118 memset(buf, 0, maxsize);
120 CDEBUG(D_KUC, "Waiting for message from kernel on fd %d\n",
124 /* Read header first to get message size */
125 rc = read(link->lk_rfd, buf, lhsz);
130 kuch = (struct kuc_hdr *)buf;
132 CDEBUG(D_KUC, "Received message mg=%x t=%d m=%d l=%d\n",
133 kuch->kuc_magic, kuch->kuc_transport, kuch->kuc_msgtype,
136 if (kuch->kuc_magic != KUC_MAGIC) {
137 CERROR("bad message magic %x != %x\n",
138 kuch->kuc_magic, KUC_MAGIC);
143 if (kuch->kuc_msglen > maxsize) {
149 rc = read(link->lk_rfd, buf + lhsz, kuch->kuc_msglen - lhsz);
154 if (rc < (kuch->kuc_msglen - lhsz)) {
155 CERROR("short read: got %d of %d bytes\n",
156 rc, kuch->kuc_msglen);
161 if (kuch->kuc_transport == transport ||
162 kuch->kuc_transport == KUC_TRANSPORT_GENERIC) {
165 /* Drop messages for other transports */
170 #else /* LUSTRE_UTILS */
171 /* This is the kernel side (liblustre as well). */
174 * libcfs_kkuc_msg_put - send an message from kernel to userspace
175 * @param fp to send the message to
176 * @param payload Payload data. First field of payload is always
179 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
181 struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
184 if (filp == NULL || IS_ERR(filp))
187 if (kuch->kuc_magic != KUC_MAGIC) {
188 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
195 rc = filp_user_write(filp, payload, kuch->kuc_msglen,
201 CWARN("message send failed (%d)\n", rc);
203 CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp);
207 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
209 /* Broadcast groups are global across all mounted filesystems;
210 * i.e. registering for a group on 1 fs will get messages for that
211 * group from any fs */
212 /** A single group registration has a uid and a file pointer */
214 struct list_head kr_chain;
220 static struct list_head kkuc_groups[KUC_GRP_MAX+1] = {};
221 /* Protect message sending against remove and adds */
222 static DECLARE_RWSEM(kg_sem);
224 /** Add a receiver to a broadcast group
225 * @param filp pipe to write into
226 * @param uid identifier for this receiver
227 * @param group group number
228 * @param data user data
230 int libcfs_kkuc_group_add(struct file *filp, int uid, int group, void *data)
232 struct kkuc_reg *reg;
234 if (group > KUC_GRP_MAX) {
235 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
239 /* fput in group_rem */
243 /* freed in group_rem */
244 reg = kmalloc(sizeof(*reg), 0);
253 if (kkuc_groups[group].next == NULL)
254 INIT_LIST_HEAD(&kkuc_groups[group]);
255 list_add(®->kr_chain, &kkuc_groups[group]);
258 CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
262 EXPORT_SYMBOL(libcfs_kkuc_group_add);
264 int libcfs_kkuc_group_rem(int uid, int group, void **pdata)
266 struct kkuc_reg *reg, *next;
269 if (kkuc_groups[group].next == NULL)
273 /* Broadcast a shutdown message */
276 lh.kuc_magic = KUC_MAGIC;
277 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
278 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
279 lh.kuc_msglen = sizeof(lh);
280 libcfs_kkuc_group_put(group, &lh);
284 list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
285 if ((uid == 0) || (uid == reg->kr_uid)) {
286 list_del(®->kr_chain);
287 CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
288 reg->kr_uid, reg->kr_fp, group);
289 if (reg->kr_fp != NULL)
292 *pdata = reg->kr_data;
300 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
302 int libcfs_kkuc_group_put(int group, void *payload)
304 struct kkuc_reg *reg;
310 list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
311 if (reg->kr_fp != NULL) {
312 rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
315 else if (rc == -EPIPE) {
323 /* don't return an error if the message has been delivered
324 * at least to one agent */
330 EXPORT_SYMBOL(libcfs_kkuc_group_put);
333 * Calls a callback function for each link of the given kuc group.
334 * @param group the group to call the function on.
335 * @param cb_func the function to be called.
336 * @param cb_arg extra argument to be passed to the callback function.
338 int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func,
341 struct kkuc_reg *reg;
345 if (group > KUC_GRP_MAX) {
346 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
350 /* no link for this group */
351 if (kkuc_groups[group].next == NULL)
355 list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
356 if (reg->kr_fp != NULL) {
357 rc = cb_func(reg->kr_data, cb_arg);
364 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);
366 #endif /* LUSTRE_UTILS */