Whamcloud - gitweb
1e572f4b0b43893d3edc9282b848ddee141c0cdf
[fs/lustre-release.git] / libcfs / libcfs / kernel_user_comm.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * Author: Nathan Rutman <nathan.rutman@sun.com>
35  *
36  * Kernel <-> userspace communication routines.
37  * Using pipes for all arches.
38  */
39
40 #define DEBUG_SUBSYSTEM S_CLASS
41 #define D_KUC D_OTHER
42
43 #include <libcfs/libcfs.h>
44
45 #ifdef LUSTRE_UTILS
46 /* This is the userspace side. */
47
48 /** Start the userspace side of a KUC pipe.
49  * @param link Private descriptor for pipe/socket.
50  * @param groups KUC broadcast group to listen to
51  *          (can be null for unicast to this pid)
52  */
53 int libcfs_ukuc_start(lustre_kernelcomm *link, int group)
54 {
55         int pfd[2];
56
57         if (pipe(pfd) < 0)
58                 return -errno;
59
60         memset(link, 0, sizeof(*link));
61         link->lk_rfd = pfd[0];
62         link->lk_wfd = pfd[1];
63         link->lk_group = group;
64         link->lk_uid = getpid();
65         return 0;
66 }
67
68 int libcfs_ukuc_stop(lustre_kernelcomm *link)
69 {
70         if (link->lk_wfd > 0)
71                 close(link->lk_wfd);
72         return close(link->lk_rfd);
73 }
74
75 #define lhsz sizeof(*kuch)
76
77 /** Read a message from the link.
78  * Allocates memory, returns handle
79  *
80  * @param link Private descriptor for pipe/socket.
81  * @param buf Buffer to read into, must include size for kuc_hdr
82  * @param maxsize Maximum message size allowed
83  * @param transport Only listen to messages on this transport
84  *      (and the generic transport)
85  */
86 int libcfs_ukuc_msg_get(lustre_kernelcomm *link, char *buf, int maxsize,
87                         int transport)
88 {
89         struct kuc_hdr *kuch;
90         int rc = 0;
91
92         memset(buf, 0, maxsize);
93
94         CDEBUG(D_KUC, "Waiting for message from kernel on fd %d\n",
95                link->lk_rfd);
96
97         while (1) {
98                 /* Read header first to get message size */
99                 rc = read(link->lk_rfd, buf, lhsz);
100                 if (rc <= 0) {
101                         rc = -errno;
102                         break;
103                 }
104                 kuch = (struct kuc_hdr *)buf;
105
106                 CDEBUG(D_KUC, "Received message mg=%x t=%d m=%d l=%d\n",
107                        kuch->kuc_magic, kuch->kuc_transport, kuch->kuc_msgtype,
108                        kuch->kuc_msglen);
109
110                 if (kuch->kuc_magic != KUC_MAGIC) {
111                         CERROR("bad message magic %x != %x\n",
112                                kuch->kuc_magic, KUC_MAGIC);
113                         rc = -EPROTO;
114                         break;
115                 }
116
117                 if (kuch->kuc_msglen > maxsize) {
118                         rc = -EMSGSIZE;
119                         break;
120                 }
121
122                 /* Read payload */
123                 rc = read(link->lk_rfd, buf + lhsz, kuch->kuc_msglen - lhsz);
124                 if (rc < 0) {
125                         rc = -errno;
126                         break;
127                 }
128                 if (rc < (kuch->kuc_msglen - lhsz)) {
129                         CERROR("short read: got %d of %d bytes\n",
130                                rc, kuch->kuc_msglen);
131                         rc = -EPROTO;
132                         break;
133                 }
134
135                 if (kuch->kuc_transport == transport ||
136                     kuch->kuc_transport == KUC_TRANSPORT_GENERIC) {
137                         return 0;
138                 }
139                 /* Drop messages for other transports */
140         }
141         return rc;
142 }
143
144 #else /* LUSTRE_UTILS */
145 /* This is the kernel side (liblustre as well). */
146
147 /**
148  * libcfs_kkuc_msg_put - send an message from kernel to userspace
149  * @param fp to send the message to
150  * @param payload Payload data.  First field of payload is always
151  *   struct kuc_hdr
152  */
153 int libcfs_kkuc_msg_put(cfs_file_t *filp, void *payload)
154 {
155         struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
156         int rc = -ENOSYS;
157
158         if (filp == NULL || IS_ERR(filp))
159                 return -EBADF;
160
161         if (kuch->kuc_magic != KUC_MAGIC) {
162                 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
163                 return -ENOSYS;
164         }
165
166 #ifdef __KERNEL__
167         {
168                 loff_t offset = 0;
169                 rc = cfs_user_write(filp, (char *)payload, kuch->kuc_msglen,
170                                     &offset);
171         }
172 #endif
173
174         if (rc < 0)
175                 CWARN("message send failed (%d)\n", rc);
176         else
177                 CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp);
178
179         return rc;
180 }
181 CFS_EXPORT_SYMBOL(libcfs_kkuc_msg_put);
182
183 /* Broadcast groups are global across all mounted filesystems;
184  * i.e. registering for a group on 1 fs will get messages for that
185  * group from any fs */
186 /** A single group reigstration has a uid and a file pointer */
187 struct kkuc_reg {
188         cfs_list_t  kr_chain;
189         int         kr_uid;
190         cfs_file_t *kr_fp;
191         __u32       kr_data;
192 };
193 static cfs_list_t kkuc_groups[KUC_GRP_MAX+1] = {};
194 /* Protect message sending against remove and adds */
195 static CFS_DECLARE_RWSEM(kg_sem);
196
197 /** Add a receiver to a broadcast group
198  * @param filp pipe to write into
199  * @param uid identidier for this receiver
200  * @param group group number
201  */
202 int libcfs_kkuc_group_add(cfs_file_t *filp, int uid, int group, __u32 data)
203 {
204         struct kkuc_reg *reg;
205
206         if (group > KUC_GRP_MAX) {
207                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
208                 return -EINVAL;
209         }
210
211         /* fput in group_rem */
212         if (filp == NULL)
213                 return -EBADF;
214
215         /* freed in group_rem */
216         reg = cfs_alloc(sizeof(*reg), 0);
217         if (reg == NULL)
218                 return -ENOMEM;
219
220         reg->kr_fp = filp;
221         reg->kr_uid = uid;
222         reg->kr_data = data;
223
224         cfs_down_write(&kg_sem);
225         if (kkuc_groups[group].next == NULL)
226                 CFS_INIT_LIST_HEAD(&kkuc_groups[group]);
227         cfs_list_add(&reg->kr_chain, &kkuc_groups[group]);
228         cfs_up_write(&kg_sem);
229
230         CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
231
232         return 0;
233 }
234 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_add);
235
236 int libcfs_kkuc_group_rem(int uid, int group)
237 {
238         struct kkuc_reg *reg, *next;
239         ENTRY;
240
241         if (kkuc_groups[group].next == NULL)
242                 RETURN(0);
243
244         if (uid == 0) {
245                 /* Broadcast a shutdown message */
246                 struct kuc_hdr lh;
247
248                 lh.kuc_magic = KUC_MAGIC;
249                 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
250                 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
251                 lh.kuc_msglen = sizeof(lh);
252                 libcfs_kkuc_group_put(group, &lh);
253         }
254
255         cfs_down_write(&kg_sem);
256         cfs_list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
257                 if ((uid == 0) || (uid == reg->kr_uid)) {
258                         cfs_list_del(&reg->kr_chain);
259                         CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
260                                reg->kr_uid, reg->kr_fp, group);
261                         if (reg->kr_fp != NULL)
262                                 cfs_put_file(reg->kr_fp);
263                         cfs_free(reg);
264                 }
265         }
266         cfs_up_write(&kg_sem);
267
268         RETURN(0);
269 }
270 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_rem);
271
272 int libcfs_kkuc_group_put(int group, void *payload)
273 {
274         struct kkuc_reg *reg;
275         int rc = 0;
276         ENTRY;
277
278         cfs_down_read(&kg_sem);
279         cfs_list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
280                 if (reg->kr_fp != NULL) {
281                 rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
282                         if (rc == -EPIPE) {
283                                 cfs_put_file(reg->kr_fp);
284                                 reg->kr_fp = NULL;
285                         }
286                 }
287         }
288         cfs_up_read(&kg_sem);
289
290         RETURN(rc);
291 }
292 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_put);
293
294 /**
295  * Calls a callback function for each link of the given kuc group.
296  * @param group the group to call the function on.
297  * @param cb_func the function to be called.
298  * @param cb_arg iextra argument to be passed to the callback function.
299  */
300 int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func,
301                               void *cb_arg)
302 {
303         struct kkuc_reg *reg;
304         int rc = 0;
305         ENTRY;
306
307         if (group > KUC_GRP_MAX) {
308                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
309                 RETURN(-EINVAL);
310         }
311
312         /* no link for this group */
313         if (kkuc_groups[group].next == NULL)
314                 RETURN(0);
315
316         cfs_down_read(&kg_sem);
317         cfs_list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
318                 if (reg->kr_fp != NULL) {
319                         rc = cb_func(reg->kr_data, cb_arg);
320                 }
321         }
322         cfs_up_read(&kg_sem);
323
324         RETURN(rc);
325 }
326 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_foreach);
327
328 #endif /* LUSTRE_UTILS */
329