Whamcloud - gitweb
b=23004 bad cfs_user_write call causes oops
[fs/lustre-release.git] / libcfs / libcfs / kernel_user_comm.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 0
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  */
55 int libcfs_ukuc_start(lustre_kernelcomm *link, int group)
56 {
57         int pfd[2];
58
59         if (pipe(pfd) < 0)
60                 return -errno;
61
62         link->lk_rfd = pfd[0];
63         link->lk_wfd = pfd[1];
64         link->lk_group = group;
65         link->lk_uid = getpid();
66         return 0;
67 }
68
69 int libcfs_ukuc_stop(lustre_kernelcomm *link)
70 {
71         if (link->lk_wfd > 0)
72                 close(link->lk_wfd);
73         return close(link->lk_rfd);
74 }
75
76 #define lhsz sizeof(*kuch)
77
78 /** Read a message from the link.
79  * Allocates memory, returns handle
80  *
81  * @param link Private descriptor for pipe/socket.
82  * @param buf Buffer to read into
83  * @param maxsize Maximum message size allowed
84  * @param transport Only listen to messages on this transport
85  *      (and the generic transport)
86  */
87 int libcfs_ukuc_msg_get(lustre_kernelcomm *link, char *buf, int maxsize,
88                         int transport)
89 {
90         struct kuc_hdr *kuch;
91         int rc = 0;
92
93         memset(buf, 0, maxsize);
94
95         CDEBUG(D_KUC, "Waiting for message from kernel on fd %d\n",
96                link->lk_rfd);
97
98         while (1) {
99                 /* Read header first to get message size */
100                 rc = read(link->lk_rfd, buf, lhsz);
101                 if (rc <= 0) {
102                         rc = -errno;
103                         break;
104                 }
105                 kuch = (struct kuc_hdr *)buf;
106
107                 CDEBUG(D_KUC, " Received message mg=%x t=%d m=%d l=%d\n",
108                        kuch->kuc_magic, kuch->kuc_transport, kuch->kuc_msgtype,
109                        kuch->kuc_msglen);
110
111                 if (kuch->kuc_magic != KUC_MAGIC) {
112                         CERROR("bad message magic %x != %x\n",
113                                kuch->kuc_magic, KUC_MAGIC);
114                         rc = -EPROTO;
115                         break;
116                 }
117
118                 if (kuch->kuc_msglen > maxsize) {
119                         rc = -EMSGSIZE;
120                         break;
121                 }
122
123                 /* Read payload */
124                 rc = read(link->lk_rfd, buf + lhsz, kuch->kuc_msglen - lhsz);
125                 if (rc < 0) {
126                         rc = -errno;
127                         break;
128                 }
129                 if (rc < (kuch->kuc_msglen - lhsz)) {
130                         CERROR("short read: got %d of %d bytes\n",
131                                rc, kuch->kuc_msglen);
132                         rc = -EPROTO;
133                         break;
134                 }
135
136                 if (kuch->kuc_transport == transport ||
137                     kuch->kuc_transport == KUC_TRANSPORT_GENERIC) {
138                         return 0;
139                 }
140                 /* Drop messages for other transports */
141         }
142         return rc;
143 }
144
145 #else /* LUSTRE_UTILS */
146 /* This is the kernel side (liblustre as well). */
147
148 /**
149  * libcfs_kkuc_msg_put - send an message from kernel to userspace
150  * @param fp to send the message to
151  * @param payload Payload data.  First field of payload is always
152  *   struct kuc_hdr
153  */
154 int libcfs_kkuc_msg_put(cfs_file_t *filp, void *payload)
155 {
156         struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
157         int rc = -ENOSYS;
158
159         if (filp == NULL || IS_ERR(filp))
160                 return -EBADF;
161
162         if (kuch->kuc_magic != KUC_MAGIC) {
163                 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
164                 return -ENOSYS;
165         }
166
167 #ifdef __KERNEL__
168         {
169                 loff_t offset = 0;
170                 rc = cfs_user_write(filp, (char *)payload, kuch->kuc_msglen, 
171                                     &offset);
172         }
173 #endif
174
175         if (rc < 0)
176                 CWARN("message send failed (%d)\n", rc);
177         else
178                 CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp);
179
180         return rc;
181 }
182 CFS_EXPORT_SYMBOL(libcfs_kkuc_msg_put);
183
184 /* Broadcast groups are global across all mounted filesystems;
185  * i.e. registering for a group on 1 fs will get messages for that
186  * group from any fs */
187 /** A single group reigstration has a uid and a file pointer */
188 struct kkuc_reg {
189         cfs_list_t  kr_chain;
190         int         kr_uid;
191         cfs_file_t *kr_fp;
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)
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
223         cfs_down_write(&kg_sem);
224         if (kkuc_groups[group].next == NULL)
225                 CFS_INIT_LIST_HEAD(&kkuc_groups[group]);
226         cfs_list_add(&reg->kr_chain, &kkuc_groups[group]);
227         cfs_up_write(&kg_sem);
228
229         CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
230
231         return 0;
232 }
233 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_add);
234
235 int libcfs_kkuc_group_rem(int uid, int group)
236 {
237         struct kkuc_reg *reg, *next;
238         ENTRY;
239
240         if (kkuc_groups[group].next == NULL)
241                 RETURN(0);
242
243         if (uid == 0) {
244                 /* Broadcast a shutdown message */
245                 struct kuc_hdr lh;
246
247                 lh.kuc_magic = KUC_MAGIC;
248                 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
249                 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
250                 lh.kuc_msglen = sizeof(lh);
251                 libcfs_kkuc_group_put(group, &lh);
252         }
253
254         cfs_down_write(&kg_sem);
255         cfs_list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
256                 if ((uid == 0) || (uid == reg->kr_uid)) {
257                         cfs_list_del(&reg->kr_chain);
258                         CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
259                                reg->kr_uid, reg->kr_fp, group);
260                         cfs_put_file(reg->kr_fp);
261                         cfs_free(reg);
262                 }
263         }
264         cfs_up_write(&kg_sem);
265
266         RETURN(0);
267 }
268 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_rem);
269
270 int libcfs_kkuc_group_put(int group, void *payload)
271 {
272         struct kkuc_reg *reg;
273         int rc = 0;
274         ENTRY;
275
276         cfs_down_read(&kg_sem);
277         cfs_list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
278                 rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
279         }
280         cfs_up_read(&kg_sem);
281
282         RETURN(rc);
283 }
284 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_put);
285
286 #endif /* LUSTRE_UTILS */
287