Whamcloud - gitweb
LU-6068 misc: update Intel copyright messages 2014
[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  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Author: Nathan Rutman <nathan.rutman@sun.com>
37  *
38  * Kernel <-> userspace communication routines.
39  * Using pipes for all arches.
40  */
41
42 #define DEBUG_SUBSYSTEM S_CLASS
43 #define D_KUC D_OTHER
44
45 #include <libcfs/libcfs.h>
46
47 #ifdef LUSTRE_UTILS
48 /* This is the userspace side. */
49
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)
55  */
56 int libcfs_ukuc_start(lustre_kernelcomm *link, int group, int rfd_flags)
57 {
58         int pfd[2];
59         int rc;
60
61         link->lk_rfd = link->lk_wfd = LK_NOFD;
62
63         if (pipe(pfd) < 0)
64                 return -errno;
65
66         if (fcntl(pfd[0], F_SETFL, rfd_flags) < 0) {
67                 rc = -errno;
68                 close(pfd[0]);
69                 close(pfd[1]);
70                 return rc;
71         }
72
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();
78         return 0;
79 }
80
81 int libcfs_ukuc_stop(lustre_kernelcomm *link)
82 {
83         int rc;
84
85         if (link->lk_wfd != LK_NOFD)
86                 close(link->lk_wfd);
87         rc = close(link->lk_rfd);
88         link->lk_rfd = link->lk_wfd = LK_NOFD;
89         return rc;
90 }
91
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.
95  */
96 int libcfs_ukuc_get_rfd(lustre_kernelcomm *link)
97 {
98         return link->lk_rfd;
99 }
100
101 #define lhsz sizeof(*kuch)
102
103 /** Read a message from the link.
104  * Allocates memory, returns handle
105  *
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)
111  */
112 int libcfs_ukuc_msg_get(lustre_kernelcomm *link, char *buf, int maxsize,
113                         int transport)
114 {
115         struct kuc_hdr *kuch;
116         int rc = 0;
117
118         memset(buf, 0, maxsize);
119
120         CDEBUG(D_KUC, "Waiting for message from kernel on fd %d\n",
121                link->lk_rfd);
122
123         while (1) {
124                 /* Read header first to get message size */
125                 rc = read(link->lk_rfd, buf, lhsz);
126                 if (rc <= 0) {
127                         rc = -errno;
128                         break;
129                 }
130                 kuch = (struct kuc_hdr *)buf;
131
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,
134                        kuch->kuc_msglen);
135
136                 if (kuch->kuc_magic != KUC_MAGIC) {
137                         CERROR("bad message magic %x != %x\n",
138                                kuch->kuc_magic, KUC_MAGIC);
139                         rc = -EPROTO;
140                         break;
141                 }
142
143                 if (kuch->kuc_msglen > maxsize) {
144                         rc = -EMSGSIZE;
145                         break;
146                 }
147
148                 /* Read payload */
149                 rc = read(link->lk_rfd, buf + lhsz, kuch->kuc_msglen - lhsz);
150                 if (rc < 0) {
151                         rc = -errno;
152                         break;
153                 }
154                 if (rc < (kuch->kuc_msglen - lhsz)) {
155                         CERROR("short read: got %d of %d bytes\n",
156                                rc, kuch->kuc_msglen);
157                         rc = -EPROTO;
158                         break;
159                 }
160
161                 if (kuch->kuc_transport == transport ||
162                     kuch->kuc_transport == KUC_TRANSPORT_GENERIC) {
163                         return 0;
164                 }
165                 /* Drop messages for other transports */
166         }
167         return rc;
168 }
169
170 #else /* LUSTRE_UTILS */
171 /* This is the kernel side (liblustre as well). */
172
173 /**
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
177  *   struct kuc_hdr
178  */
179 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
180 {
181         struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
182         int rc = -ENOSYS;
183
184         if (filp == NULL || IS_ERR(filp))
185                 return -EBADF;
186
187         if (kuch->kuc_magic != KUC_MAGIC) {
188                 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
189                 return -ENOSYS;
190         }
191
192 #ifdef __KERNEL__
193         {
194                 loff_t offset = 0;
195                 rc = filp_user_write(filp, payload, kuch->kuc_msglen,
196                                      &offset);
197         }
198 #endif
199
200         if (rc < 0)
201                 CWARN("message send failed (%d)\n", rc);
202         else
203                 CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp);
204
205         return rc;
206 }
207 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
208
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 */
213 struct kkuc_reg {
214         struct list_head kr_chain;
215         int              kr_uid;
216         struct file     *kr_fp;
217         void            *kr_data;
218 };
219
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);
223
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
229  */
230 int libcfs_kkuc_group_add(struct file *filp, int uid, int group, void *data)
231 {
232         struct kkuc_reg *reg;
233
234         if (group > KUC_GRP_MAX) {
235                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
236                 return -EINVAL;
237         }
238
239         /* fput in group_rem */
240         if (filp == NULL)
241                 return -EBADF;
242
243         /* freed in group_rem */
244         reg = kmalloc(sizeof(*reg), 0);
245         if (reg == NULL)
246                 return -ENOMEM;
247
248         reg->kr_fp = filp;
249         reg->kr_uid = uid;
250         reg->kr_data = data;
251
252         down_write(&kg_sem);
253         if (kkuc_groups[group].next == NULL)
254                 INIT_LIST_HEAD(&kkuc_groups[group]);
255         list_add(&reg->kr_chain, &kkuc_groups[group]);
256         up_write(&kg_sem);
257
258         CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
259
260         return 0;
261 }
262 EXPORT_SYMBOL(libcfs_kkuc_group_add);
263
264 int libcfs_kkuc_group_rem(int uid, int group, void **pdata)
265 {
266         struct kkuc_reg *reg, *next;
267         ENTRY;
268
269         if (kkuc_groups[group].next == NULL)
270                 RETURN(0);
271
272         if (uid == 0) {
273                 /* Broadcast a shutdown message */
274                 struct kuc_hdr lh;
275
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);
281         }
282
283         down_write(&kg_sem);
284         list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
285                 if ((uid == 0) || (uid == reg->kr_uid)) {
286                         list_del(&reg->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)
290                                 fput(reg->kr_fp);
291                         if (pdata != NULL)
292                                 *pdata = reg->kr_data;
293                         kfree(reg);
294                 }
295         }
296         up_write(&kg_sem);
297
298         RETURN(0);
299 }
300 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
301
302 int libcfs_kkuc_group_put(int group, void *payload)
303 {
304         struct kkuc_reg *reg;
305         int              rc = 0;
306         int one_success = 0;
307         ENTRY;
308
309         down_write(&kg_sem);
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);
313                         if (rc == 0)
314                                 one_success = 1;
315                         else if (rc == -EPIPE) {
316                                 fput(reg->kr_fp);
317                                 reg->kr_fp = NULL;
318                         }
319                 }
320         }
321         up_write(&kg_sem);
322
323         /* don't return an error if the message has been delivered
324          * at least to one agent */
325         if (one_success)
326                 rc = 0;
327
328         RETURN(rc);
329 }
330 EXPORT_SYMBOL(libcfs_kkuc_group_put);
331
332 /**
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.
337  */
338 int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func,
339                               void *cb_arg)
340 {
341         struct kkuc_reg *reg;
342         int              rc = 0;
343         ENTRY;
344
345         if (group > KUC_GRP_MAX) {
346                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
347                 RETURN(-EINVAL);
348         }
349
350         /* no link for this group */
351         if (kkuc_groups[group].next == NULL)
352                 RETURN(0);
353
354         down_read(&kg_sem);
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);
358                 }
359         }
360         up_read(&kg_sem);
361
362         RETURN(rc);
363 }
364 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);
365
366 #endif /* LUSTRE_UTILS */
367