Commit | Line | Data |
---|---|---|
d7e09d03 PT |
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 | ||
e2780478 | 45 | #include "../include/obd_support.h" |
46 | #include "../include/lustre_kernelcomm.h" | |
d7e09d03 PT |
47 | |
48 | /** | |
49 | * libcfs_kkuc_msg_put - send an message from kernel to userspace | |
50 | * @param fp to send the message to | |
51 | * @param payload Payload data. First field of payload is always | |
52 | * struct kuc_hdr | |
53 | */ | |
54 | int libcfs_kkuc_msg_put(struct file *filp, void *payload) | |
55 | { | |
56 | struct kuc_hdr *kuch = (struct kuc_hdr *)payload; | |
7d5ed06b PT |
57 | ssize_t count = kuch->kuc_msglen; |
58 | loff_t offset = 0; | |
59 | mm_segment_t fs; | |
2bab480d | 60 | int rc = -ENXIO; |
d7e09d03 | 61 | |
acdd1b8e | 62 | if (IS_ERR_OR_NULL(filp)) |
d7e09d03 PT |
63 | return -EBADF; |
64 | ||
65 | if (kuch->kuc_magic != KUC_MAGIC) { | |
66 | CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic); | |
2bab480d | 67 | return rc; |
d7e09d03 PT |
68 | } |
69 | ||
7d5ed06b PT |
70 | fs = get_fs(); |
71 | set_fs(KERNEL_DS); | |
e543d2ea PT |
72 | while (count > 0) { |
73 | rc = vfs_write(filp, (void __force __user *)payload, | |
7d5ed06b PT |
74 | count, &offset); |
75 | if (rc < 0) | |
76 | break; | |
77 | count -= rc; | |
78 | payload += rc; | |
79 | rc = 0; | |
d7e09d03 | 80 | } |
7d5ed06b | 81 | set_fs(fs); |
d7e09d03 PT |
82 | |
83 | if (rc < 0) | |
84 | CWARN("message send failed (%d)\n", rc); | |
85 | else | |
86 | CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp); | |
87 | ||
88 | return rc; | |
89 | } | |
90 | EXPORT_SYMBOL(libcfs_kkuc_msg_put); | |
91 | ||
25c450ea JS |
92 | /* |
93 | * Broadcast groups are global across all mounted filesystems; | |
d7e09d03 | 94 | * i.e. registering for a group on 1 fs will get messages for that |
25c450ea JS |
95 | * group from any fs |
96 | */ | |
17afd17b | 97 | /** A single group registration has a uid and a file pointer */ |
d7e09d03 | 98 | struct kkuc_reg { |
406fc913 HD |
99 | struct list_head kr_chain; |
100 | int kr_uid; | |
d7e09d03 | 101 | struct file *kr_fp; |
17328956 | 102 | char kr_data[0]; |
d7e09d03 | 103 | }; |
c9f6bb96 | 104 | |
13c26b26 | 105 | static struct list_head kkuc_groups[KUC_GRP_MAX + 1] = {}; |
d7e09d03 PT |
106 | /* Protect message sending against remove and adds */ |
107 | static DECLARE_RWSEM(kg_sem); | |
108 | ||
109 | /** Add a receiver to a broadcast group | |
110 | * @param filp pipe to write into | |
17afd17b | 111 | * @param uid identifier for this receiver |
d7e09d03 | 112 | * @param group group number |
406fc913 | 113 | * @param data user data |
d7e09d03 | 114 | */ |
54cc3794 | 115 | int libcfs_kkuc_group_add(struct file *filp, int uid, unsigned int group, |
17328956 | 116 | void *data, size_t data_len) |
d7e09d03 PT |
117 | { |
118 | struct kkuc_reg *reg; | |
119 | ||
120 | if (group > KUC_GRP_MAX) { | |
121 | CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group); | |
122 | return -EINVAL; | |
123 | } | |
124 | ||
125 | /* fput in group_rem */ | |
fb81e732 | 126 | if (!filp) |
d7e09d03 PT |
127 | return -EBADF; |
128 | ||
129 | /* freed in group_rem */ | |
17328956 | 130 | reg = kmalloc(sizeof(*reg) + data_len, 0); |
fb81e732 | 131 | if (!reg) |
d7e09d03 PT |
132 | return -ENOMEM; |
133 | ||
134 | reg->kr_fp = filp; | |
135 | reg->kr_uid = uid; | |
17328956 | 136 | memcpy(reg->kr_data, data, data_len); |
d7e09d03 PT |
137 | |
138 | down_write(&kg_sem); | |
fb81e732 | 139 | if (!kkuc_groups[group].next) |
d7e09d03 PT |
140 | INIT_LIST_HEAD(&kkuc_groups[group]); |
141 | list_add(®->kr_chain, &kkuc_groups[group]); | |
142 | up_write(&kg_sem); | |
143 | ||
144 | CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group); | |
145 | ||
146 | return 0; | |
147 | } | |
148 | EXPORT_SYMBOL(libcfs_kkuc_group_add); | |
149 | ||
16b376bb | 150 | int libcfs_kkuc_group_rem(int uid, unsigned int group) |
d7e09d03 PT |
151 | { |
152 | struct kkuc_reg *reg, *next; | |
d7e09d03 | 153 | |
fb81e732 | 154 | if (!kkuc_groups[group].next) |
0a3bdb00 | 155 | return 0; |
d7e09d03 | 156 | |
a5f533f5 | 157 | if (!uid) { |
d7e09d03 PT |
158 | /* Broadcast a shutdown message */ |
159 | struct kuc_hdr lh; | |
160 | ||
161 | lh.kuc_magic = KUC_MAGIC; | |
162 | lh.kuc_transport = KUC_TRANSPORT_GENERIC; | |
163 | lh.kuc_msgtype = KUC_MSG_SHUTDOWN; | |
164 | lh.kuc_msglen = sizeof(lh); | |
165 | libcfs_kkuc_group_put(group, &lh); | |
166 | } | |
167 | ||
168 | down_write(&kg_sem); | |
169 | list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) { | |
a5f533f5 | 170 | if (!uid || (uid == reg->kr_uid)) { |
d7e09d03 PT |
171 | list_del(®->kr_chain); |
172 | CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n", | |
173 | reg->kr_uid, reg->kr_fp, group); | |
fb81e732 | 174 | if (reg->kr_fp) |
d7e09d03 PT |
175 | fput(reg->kr_fp); |
176 | kfree(reg); | |
177 | } | |
178 | } | |
179 | up_write(&kg_sem); | |
180 | ||
0a3bdb00 | 181 | return 0; |
d7e09d03 PT |
182 | } |
183 | EXPORT_SYMBOL(libcfs_kkuc_group_rem); | |
184 | ||
16b376bb | 185 | int libcfs_kkuc_group_put(unsigned int group, void *payload) |
d7e09d03 PT |
186 | { |
187 | struct kkuc_reg *reg; | |
85de1f55 | 188 | int rc = 0; |
d7e09d03 | 189 | int one_success = 0; |
d7e09d03 | 190 | |
50a10043 | 191 | down_write(&kg_sem); |
d7e09d03 | 192 | list_for_each_entry(reg, &kkuc_groups[group], kr_chain) { |
fb81e732 | 193 | if (reg->kr_fp) { |
d7e09d03 | 194 | rc = libcfs_kkuc_msg_put(reg->kr_fp, payload); |
a5f533f5 | 195 | if (!rc) { |
d7e09d03 | 196 | one_success = 1; |
a0d5e63e | 197 | } else if (rc == -EPIPE) { |
d7e09d03 PT |
198 | fput(reg->kr_fp); |
199 | reg->kr_fp = NULL; | |
200 | } | |
201 | } | |
202 | } | |
50a10043 | 203 | up_write(&kg_sem); |
d7e09d03 | 204 | |
25c450ea JS |
205 | /* |
206 | * don't return an error if the message has been delivered | |
207 | * at least to one agent | |
208 | */ | |
d7e09d03 PT |
209 | if (one_success) |
210 | rc = 0; | |
211 | ||
0a3bdb00 | 212 | return rc; |
d7e09d03 PT |
213 | } |
214 | EXPORT_SYMBOL(libcfs_kkuc_group_put); | |
215 | ||
216 | /** | |
217 | * Calls a callback function for each link of the given kuc group. | |
218 | * @param group the group to call the function on. | |
219 | * @param cb_func the function to be called. | |
406fc913 | 220 | * @param cb_arg extra argument to be passed to the callback function. |
d7e09d03 | 221 | */ |
16b376bb | 222 | int libcfs_kkuc_group_foreach(unsigned int group, libcfs_kkuc_cb_t cb_func, |
d7e09d03 PT |
223 | void *cb_arg) |
224 | { | |
225 | struct kkuc_reg *reg; | |
226 | int rc = 0; | |
d7e09d03 PT |
227 | |
228 | if (group > KUC_GRP_MAX) { | |
229 | CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group); | |
0a3bdb00 | 230 | return -EINVAL; |
d7e09d03 PT |
231 | } |
232 | ||
233 | /* no link for this group */ | |
fb81e732 | 234 | if (!kkuc_groups[group].next) |
0a3bdb00 | 235 | return 0; |
d7e09d03 | 236 | |
50a10043 | 237 | down_read(&kg_sem); |
d7e09d03 | 238 | list_for_each_entry(reg, &kkuc_groups[group], kr_chain) { |
fb81e732 | 239 | if (reg->kr_fp) |
d7e09d03 | 240 | rc = cb_func(reg->kr_data, cb_arg); |
d7e09d03 | 241 | } |
50a10043 | 242 | up_read(&kg_sem); |
d7e09d03 | 243 | |
0a3bdb00 | 244 | return rc; |
d7e09d03 PT |
245 | } |
246 | EXPORT_SYMBOL(libcfs_kkuc_group_foreach); |