Whamcloud - gitweb
LU-3683 libcfs: improve validity test for valid file descriptors
[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, 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  */
55 int libcfs_ukuc_start(lustre_kernelcomm *link, int group)
56 {
57         int pfd[2];
58
59         link->lk_rfd = link->lk_wfd = LK_NOFD;
60
61         if (pipe(pfd) < 0)
62                 return -errno;
63
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();
69         return 0;
70 }
71
72 int libcfs_ukuc_stop(lustre_kernelcomm *link)
73 {
74         int rc;
75
76         if (link->lk_wfd != LK_NOFD)
77                 close(link->lk_wfd);
78         rc = close(link->lk_rfd);
79         link->lk_rfd = link->lk_wfd = LK_NOFD;
80         return rc;
81 }
82
83 #define lhsz sizeof(*kuch)
84
85 /** Read a message from the link.
86  * Allocates memory, returns handle
87  *
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)
93  */
94 int libcfs_ukuc_msg_get(lustre_kernelcomm *link, char *buf, int maxsize,
95                         int transport)
96 {
97         struct kuc_hdr *kuch;
98         int rc = 0;
99
100         memset(buf, 0, maxsize);
101
102         CDEBUG(D_KUC, "Waiting for message from kernel on fd %d\n",
103                link->lk_rfd);
104
105         while (1) {
106                 /* Read header first to get message size */
107                 rc = read(link->lk_rfd, buf, lhsz);
108                 if (rc <= 0) {
109                         rc = -errno;
110                         break;
111                 }
112                 kuch = (struct kuc_hdr *)buf;
113
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,
116                        kuch->kuc_msglen);
117
118                 if (kuch->kuc_magic != KUC_MAGIC) {
119                         CERROR("bad message magic %x != %x\n",
120                                kuch->kuc_magic, KUC_MAGIC);
121                         rc = -EPROTO;
122                         break;
123                 }
124
125                 if (kuch->kuc_msglen > maxsize) {
126                         rc = -EMSGSIZE;
127                         break;
128                 }
129
130                 /* Read payload */
131                 rc = read(link->lk_rfd, buf + lhsz, kuch->kuc_msglen - lhsz);
132                 if (rc < 0) {
133                         rc = -errno;
134                         break;
135                 }
136                 if (rc < (kuch->kuc_msglen - lhsz)) {
137                         CERROR("short read: got %d of %d bytes\n",
138                                rc, kuch->kuc_msglen);
139                         rc = -EPROTO;
140                         break;
141                 }
142
143                 if (kuch->kuc_transport == transport ||
144                     kuch->kuc_transport == KUC_TRANSPORT_GENERIC) {
145                         return 0;
146                 }
147                 /* Drop messages for other transports */
148         }
149         return rc;
150 }
151
152 #else /* LUSTRE_UTILS */
153 /* This is the kernel side (liblustre as well). */
154
155 /**
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
159  *   struct kuc_hdr
160  */
161 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
162 {
163         struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
164         int rc = -ENOSYS;
165
166         if (filp == NULL || IS_ERR(filp))
167                 return -EBADF;
168
169         if (kuch->kuc_magic != KUC_MAGIC) {
170                 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
171                 return -ENOSYS;
172         }
173
174 #ifdef __KERNEL__
175         {
176                 loff_t offset = 0;
177                 rc = filp_user_write(filp, payload, kuch->kuc_msglen,
178                                      &offset);
179         }
180 #endif
181
182         if (rc < 0)
183                 CWARN("message send failed (%d)\n", rc);
184         else
185                 CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp);
186
187         return rc;
188 }
189 CFS_EXPORT_SYMBOL(libcfs_kkuc_msg_put);
190
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 reigstration has a uid and a file pointer */
195 struct kkuc_reg {
196         cfs_list_t      kr_chain;
197         int             kr_uid;
198         struct file     *kr_fp;
199         __u32           kr_data;
200 };
201 static cfs_list_t kkuc_groups[KUC_GRP_MAX+1] = {};
202 /* Protect message sending against remove and adds */
203 static DECLARE_RWSEM(kg_sem);
204
205 /** Add a receiver to a broadcast group
206  * @param filp pipe to write into
207  * @param uid identidier for this receiver
208  * @param group group number
209  */
210 int libcfs_kkuc_group_add(struct file *filp, int uid, int group, __u32 data)
211 {
212         struct kkuc_reg *reg;
213
214         if (group > KUC_GRP_MAX) {
215                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
216                 return -EINVAL;
217         }
218
219         /* fput in group_rem */
220         if (filp == NULL)
221                 return -EBADF;
222
223         /* freed in group_rem */
224         reg = kmalloc(sizeof(*reg), 0);
225         if (reg == NULL)
226                 return -ENOMEM;
227
228         reg->kr_fp = filp;
229         reg->kr_uid = uid;
230         reg->kr_data = data;
231
232         down_write(&kg_sem);
233         if (kkuc_groups[group].next == NULL)
234                 CFS_INIT_LIST_HEAD(&kkuc_groups[group]);
235         cfs_list_add(&reg->kr_chain, &kkuc_groups[group]);
236         up_write(&kg_sem);
237
238         CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
239
240         return 0;
241 }
242 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_add);
243
244 int libcfs_kkuc_group_rem(int uid, int group)
245 {
246         struct kkuc_reg *reg, *next;
247         ENTRY;
248
249         if (kkuc_groups[group].next == NULL)
250                 RETURN(0);
251
252         if (uid == 0) {
253                 /* Broadcast a shutdown message */
254                 struct kuc_hdr lh;
255
256                 lh.kuc_magic = KUC_MAGIC;
257                 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
258                 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
259                 lh.kuc_msglen = sizeof(lh);
260                 libcfs_kkuc_group_put(group, &lh);
261         }
262
263         down_write(&kg_sem);
264         cfs_list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
265                 if ((uid == 0) || (uid == reg->kr_uid)) {
266                         cfs_list_del(&reg->kr_chain);
267                         CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
268                                reg->kr_uid, reg->kr_fp, group);
269                         if (reg->kr_fp != NULL)
270                                 fput(reg->kr_fp);
271                         kfree(reg);
272                 }
273         }
274         up_write(&kg_sem);
275
276         RETURN(0);
277 }
278 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_rem);
279
280 int libcfs_kkuc_group_put(int group, void *payload)
281 {
282         struct kkuc_reg *reg;
283         int              rc = 0;
284         int one_success = 0;
285         ENTRY;
286
287         down_read(&kg_sem);
288         cfs_list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
289                 if (reg->kr_fp != NULL) {
290                         rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
291                         if (rc == 0)
292                                 one_success = 1;
293                         else if (rc == -EPIPE) {
294                                 fput(reg->kr_fp);
295                                 reg->kr_fp = NULL;
296                         }
297                 }
298         }
299         up_read(&kg_sem);
300
301         /* don't return an error if the message has been delivered
302          * at least to one agent */
303         if (one_success)
304                 rc = 0;
305
306         RETURN(rc);
307 }
308 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_put);
309
310 /**
311  * Calls a callback function for each link of the given kuc group.
312  * @param group the group to call the function on.
313  * @param cb_func the function to be called.
314  * @param cb_arg iextra argument to be passed to the callback function.
315  */
316 int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func,
317                               void *cb_arg)
318 {
319         struct kkuc_reg *reg;
320         int rc = 0;
321         ENTRY;
322
323         if (group > KUC_GRP_MAX) {
324                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
325                 RETURN(-EINVAL);
326         }
327
328         /* no link for this group */
329         if (kkuc_groups[group].next == NULL)
330                 RETURN(0);
331
332         down_read(&kg_sem);
333         cfs_list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
334                 if (reg->kr_fp != NULL) {
335                         rc = cb_func(reg->kr_data, cb_arg);
336                 }
337         }
338         up_read(&kg_sem);
339
340         RETURN(rc);
341 }
342 CFS_EXPORT_SYMBOL(libcfs_kkuc_group_foreach);
343
344 #endif /* LUSTRE_UTILS */
345