]>
Commit | Line | Data |
---|---|---|
716154c5 BB |
1 | /*****************************************************************************\ |
2 | * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. | |
3 | * Copyright (C) 2007 The Regents of the University of California. | |
4 | * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). | |
5 | * Written by Brian Behlendorf <behlendorf1@llnl.gov>. | |
715f6251 | 6 | * UCRL-CODE-235197 |
7 | * | |
716154c5 | 8 | * This file is part of the SPL, Solaris Porting Layer. |
3d6af2dd | 9 | * For details, see <http://zfsonlinux.org/>. |
716154c5 BB |
10 | * |
11 | * The SPL is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU General Public License as published by the | |
13 | * Free Software Foundation; either version 2 of the License, or (at your | |
14 | * option) any later version. | |
715f6251 | 15 | * |
716154c5 | 16 | * The SPL is distributed in the hope that it will be useful, but WITHOUT |
715f6251 | 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
18 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | * for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License along | |
716154c5 BB |
22 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. |
23 | ***************************************************************************** | |
24 | * Solaris Porting LAyer Tests (SPLAT) Test Control Interface. | |
25 | * | |
26 | * The 'splat' (Solaris Porting LAyer Tests) module is designed as a | |
27 | * framework which runs various in kernel regression tests to validate | |
28 | * the SPL primitives honor the Solaris ABI. | |
f1ca4da6 | 29 | * |
716154c5 BB |
30 | * The splat module is constructed of various splat_* source files each |
31 | * of which contain regression tests for a particular subsystem. For | |
32 | * example, the splat_kmem.c file contains all the tests for validating | |
33 | * the kmem interfaces have been implemented correctly. When the splat | |
34 | * module is loaded splat_*_init() will be called for each subsystems | |
35 | * tests. It is the responsibility of splat_*_init() to register all | |
ec06701b | 36 | * the tests for this subsystem using the splat_test_init(). |
716154c5 | 37 | * Similarly splat_*_fini() is called when the splat module is removed |
ec06701b AX |
38 | * and is responsible for unregistering its tests via the splat_test_fini. |
39 | * Once a test is registered it can then be run with an ioctl() | |
716154c5 BB |
40 | * call which specifies the subsystem and test to be run. The provided |
41 | * splat command line tool can be used to display all available | |
42 | * subsystems and tests. It can also be used to run the full suite | |
43 | * of regression tests or particular tests. | |
44 | \*****************************************************************************/ | |
f1ca4da6 | 45 | |
10946b02 AX |
46 | #include <sys/debug.h> |
47 | #include <sys/mutex.h> | |
48 | #include <sys/types.h> | |
df870a69 BB |
49 | #include <linux/cdev.h> |
50 | #include <linux/fs.h> | |
10946b02 AX |
51 | #include <linux/miscdevice.h> |
52 | #include <linux/module.h> | |
53 | #include <linux/slab.h> | |
df870a69 | 54 | #include <linux/uaccess.h> |
10946b02 | 55 | #include <linux/vmalloc.h> |
7c50328b | 56 | #include "splat-internal.h" |
f1ca4da6 | 57 | |
7c50328b | 58 | static struct list_head splat_module_list; |
59 | static spinlock_t splat_module_lock; | |
f1ca4da6 | 60 | |
61 | static int | |
7c50328b | 62 | splat_open(struct inode *inode, struct file *file) |
f1ca4da6 | 63 | { |
7c50328b | 64 | splat_info_t *info; |
f1ca4da6 | 65 | |
7c50328b | 66 | info = (splat_info_t *)kmalloc(sizeof(*info), GFP_KERNEL); |
f1ca4da6 | 67 | if (info == NULL) |
68 | return -ENOMEM; | |
69 | ||
10946b02 | 70 | mutex_init(&info->info_lock, SPLAT_NAME, MUTEX_DEFAULT, NULL); |
7c50328b | 71 | info->info_size = SPLAT_INFO_BUFFER_SIZE; |
72 | info->info_buffer = (char *)vmalloc(SPLAT_INFO_BUFFER_SIZE); | |
f1ca4da6 | 73 | if (info->info_buffer == NULL) { |
74 | kfree(info); | |
75 | return -ENOMEM; | |
76 | } | |
1a73940d | 77 | memset(info->info_buffer, 0, info->info_size); |
f1ca4da6 | 78 | |
79 | info->info_head = info->info_buffer; | |
80 | file->private_data = (void *)info; | |
81 | ||
1a73940d NB |
82 | splat_print(file, "%s\n", spl_version); |
83 | ||
84 | return 0; | |
f1ca4da6 | 85 | } |
86 | ||
87 | static int | |
7c50328b | 88 | splat_release(struct inode *inode, struct file *file) |
f1ca4da6 | 89 | { |
7c50328b | 90 | splat_info_t *info = (splat_info_t *)file->private_data; |
f1ca4da6 | 91 | |
f1ca4da6 | 92 | ASSERT(info); |
93 | ASSERT(info->info_buffer); | |
94 | ||
0e149d42 | 95 | mutex_destroy(&info->info_lock); |
f1ca4da6 | 96 | vfree(info->info_buffer); |
97 | kfree(info); | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
102 | static int | |
7c50328b | 103 | splat_buffer_clear(struct file *file, splat_cfg_t *kcfg, unsigned long arg) |
f1ca4da6 | 104 | { |
7c50328b | 105 | splat_info_t *info = (splat_info_t *)file->private_data; |
f1ca4da6 | 106 | |
107 | ASSERT(info); | |
108 | ASSERT(info->info_buffer); | |
109 | ||
10946b02 | 110 | mutex_enter(&info->info_lock); |
f1ca4da6 | 111 | memset(info->info_buffer, 0, info->info_size); |
112 | info->info_head = info->info_buffer; | |
10946b02 | 113 | mutex_exit(&info->info_lock); |
f1ca4da6 | 114 | |
115 | return 0; | |
116 | } | |
117 | ||
118 | static int | |
7c50328b | 119 | splat_buffer_size(struct file *file, splat_cfg_t *kcfg, unsigned long arg) |
f1ca4da6 | 120 | { |
7c50328b | 121 | splat_info_t *info = (splat_info_t *)file->private_data; |
f1ca4da6 | 122 | char *buf; |
123 | int min, size, rc = 0; | |
124 | ||
125 | ASSERT(info); | |
126 | ASSERT(info->info_buffer); | |
127 | ||
10946b02 | 128 | mutex_enter(&info->info_lock); |
f1ca4da6 | 129 | if (kcfg->cfg_arg1 > 0) { |
130 | ||
131 | size = kcfg->cfg_arg1; | |
132 | buf = (char *)vmalloc(size); | |
133 | if (buf == NULL) { | |
134 | rc = -ENOMEM; | |
135 | goto out; | |
136 | } | |
137 | ||
138 | /* Zero fill and truncate contents when coping buffer */ | |
139 | min = ((size < info->info_size) ? size : info->info_size); | |
140 | memset(buf, 0, size); | |
141 | memcpy(buf, info->info_buffer, min); | |
142 | vfree(info->info_buffer); | |
143 | info->info_size = size; | |
144 | info->info_buffer = buf; | |
145 | info->info_head = info->info_buffer; | |
146 | } | |
147 | ||
148 | kcfg->cfg_rc1 = info->info_size; | |
149 | ||
7c50328b | 150 | if (copy_to_user((struct splat_cfg_t __user *)arg, kcfg, sizeof(*kcfg))) |
f1ca4da6 | 151 | rc = -EFAULT; |
152 | out: | |
10946b02 | 153 | mutex_exit(&info->info_lock); |
f1ca4da6 | 154 | |
155 | return rc; | |
156 | } | |
157 | ||
158 | ||
7c50328b | 159 | static splat_subsystem_t * |
160 | splat_subsystem_find(int id) { | |
161 | splat_subsystem_t *sub; | |
f1ca4da6 | 162 | |
7c50328b | 163 | spin_lock(&splat_module_lock); |
164 | list_for_each_entry(sub, &splat_module_list, subsystem_list) { | |
f1ca4da6 | 165 | if (id == sub->desc.id) { |
7c50328b | 166 | spin_unlock(&splat_module_lock); |
f1ca4da6 | 167 | return sub; |
168 | } | |
169 | } | |
7c50328b | 170 | spin_unlock(&splat_module_lock); |
f1ca4da6 | 171 | |
172 | return NULL; | |
173 | } | |
174 | ||
175 | static int | |
7c50328b | 176 | splat_subsystem_count(splat_cfg_t *kcfg, unsigned long arg) |
f1ca4da6 | 177 | { |
7c50328b | 178 | splat_subsystem_t *sub; |
f1ca4da6 | 179 | int i = 0; |
180 | ||
7c50328b | 181 | spin_lock(&splat_module_lock); |
182 | list_for_each_entry(sub, &splat_module_list, subsystem_list) | |
f1ca4da6 | 183 | i++; |
184 | ||
7c50328b | 185 | spin_unlock(&splat_module_lock); |
f1ca4da6 | 186 | kcfg->cfg_rc1 = i; |
187 | ||
7c50328b | 188 | if (copy_to_user((struct splat_cfg_t __user *)arg, kcfg, sizeof(*kcfg))) |
f1ca4da6 | 189 | return -EFAULT; |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | static int | |
7c50328b | 195 | splat_subsystem_list(splat_cfg_t *kcfg, unsigned long arg) |
f1ca4da6 | 196 | { |
7c50328b | 197 | splat_subsystem_t *sub; |
198 | splat_cfg_t *tmp; | |
f1ca4da6 | 199 | int size, i = 0; |
200 | ||
201 | /* Structure will be sized large enough for N subsystem entries | |
202 | * which is passed in by the caller. On exit the number of | |
203 | * entries filled in with valid subsystems will be stored in | |
204 | * cfg_rc1. If the caller does not provide enough entries | |
205 | * for all subsystems we will truncate the list to avoid overrun. | |
206 | */ | |
7c50328b | 207 | size = sizeof(*tmp) + kcfg->cfg_data.splat_subsystems.size * |
208 | sizeof(splat_user_t); | |
f1ca4da6 | 209 | tmp = kmalloc(size, GFP_KERNEL); |
210 | if (tmp == NULL) | |
211 | return -ENOMEM; | |
212 | ||
213 | /* Local 'tmp' is used as the structure copied back to user space */ | |
214 | memset(tmp, 0, size); | |
215 | memcpy(tmp, kcfg, sizeof(*kcfg)); | |
216 | ||
7c50328b | 217 | spin_lock(&splat_module_lock); |
218 | list_for_each_entry(sub, &splat_module_list, subsystem_list) { | |
219 | strncpy(tmp->cfg_data.splat_subsystems.descs[i].name, | |
220 | sub->desc.name, SPLAT_NAME_SIZE); | |
221 | strncpy(tmp->cfg_data.splat_subsystems.descs[i].desc, | |
222 | sub->desc.desc, SPLAT_DESC_SIZE); | |
223 | tmp->cfg_data.splat_subsystems.descs[i].id = sub->desc.id; | |
f1ca4da6 | 224 | |
225 | /* Truncate list if we are about to overrun alloc'ed memory */ | |
7c50328b | 226 | if ((i++) == kcfg->cfg_data.splat_subsystems.size) |
f1ca4da6 | 227 | break; |
228 | } | |
7c50328b | 229 | spin_unlock(&splat_module_lock); |
f1ca4da6 | 230 | tmp->cfg_rc1 = i; |
231 | ||
7c50328b | 232 | if (copy_to_user((struct splat_cfg_t __user *)arg, tmp, size)) { |
f1ca4da6 | 233 | kfree(tmp); |
234 | return -EFAULT; | |
235 | } | |
236 | ||
237 | kfree(tmp); | |
238 | return 0; | |
239 | } | |
240 | ||
241 | static int | |
7c50328b | 242 | splat_test_count(splat_cfg_t *kcfg, unsigned long arg) |
f1ca4da6 | 243 | { |
7c50328b | 244 | splat_subsystem_t *sub; |
245 | splat_test_t *test; | |
f1b59d26 | 246 | int i = 0; |
f1ca4da6 | 247 | |
248 | /* Subsystem ID passed as arg1 */ | |
7c50328b | 249 | sub = splat_subsystem_find(kcfg->cfg_arg1); |
f1ca4da6 | 250 | if (sub == NULL) |
251 | return -EINVAL; | |
252 | ||
253 | spin_lock(&(sub->test_lock)); | |
254 | list_for_each_entry(test, &(sub->test_list), test_list) | |
255 | i++; | |
256 | ||
257 | spin_unlock(&(sub->test_lock)); | |
258 | kcfg->cfg_rc1 = i; | |
259 | ||
7c50328b | 260 | if (copy_to_user((struct splat_cfg_t __user *)arg, kcfg, sizeof(*kcfg))) |
f1ca4da6 | 261 | return -EFAULT; |
262 | ||
263 | return 0; | |
264 | } | |
265 | ||
266 | static int | |
7c50328b | 267 | splat_test_list(splat_cfg_t *kcfg, unsigned long arg) |
f1ca4da6 | 268 | { |
7c50328b | 269 | splat_subsystem_t *sub; |
270 | splat_test_t *test; | |
271 | splat_cfg_t *tmp; | |
f1b59d26 | 272 | int size, i = 0; |
f1ca4da6 | 273 | |
274 | /* Subsystem ID passed as arg1 */ | |
7c50328b | 275 | sub = splat_subsystem_find(kcfg->cfg_arg1); |
f1ca4da6 | 276 | if (sub == NULL) |
277 | return -EINVAL; | |
278 | ||
279 | /* Structure will be sized large enough for N test entries | |
280 | * which is passed in by the caller. On exit the number of | |
281 | * entries filled in with valid tests will be stored in | |
282 | * cfg_rc1. If the caller does not provide enough entries | |
283 | * for all tests we will truncate the list to avoid overrun. | |
284 | */ | |
7c50328b | 285 | size = sizeof(*tmp)+kcfg->cfg_data.splat_tests.size*sizeof(splat_user_t); |
f1ca4da6 | 286 | tmp = kmalloc(size, GFP_KERNEL); |
287 | if (tmp == NULL) | |
288 | return -ENOMEM; | |
289 | ||
290 | /* Local 'tmp' is used as the structure copied back to user space */ | |
291 | memset(tmp, 0, size); | |
292 | memcpy(tmp, kcfg, sizeof(*kcfg)); | |
293 | ||
294 | spin_lock(&(sub->test_lock)); | |
295 | list_for_each_entry(test, &(sub->test_list), test_list) { | |
7c50328b | 296 | strncpy(tmp->cfg_data.splat_tests.descs[i].name, |
297 | test->desc.name, SPLAT_NAME_SIZE); | |
298 | strncpy(tmp->cfg_data.splat_tests.descs[i].desc, | |
299 | test->desc.desc, SPLAT_DESC_SIZE); | |
300 | tmp->cfg_data.splat_tests.descs[i].id = test->desc.id; | |
f1ca4da6 | 301 | |
302 | /* Truncate list if we are about to overrun alloc'ed memory */ | |
7c50328b | 303 | if ((i++) == kcfg->cfg_data.splat_tests.size) |
f1ca4da6 | 304 | break; |
305 | } | |
306 | spin_unlock(&(sub->test_lock)); | |
307 | tmp->cfg_rc1 = i; | |
308 | ||
7c50328b | 309 | if (copy_to_user((struct splat_cfg_t __user *)arg, tmp, size)) { |
f1ca4da6 | 310 | kfree(tmp); |
311 | return -EFAULT; | |
312 | } | |
313 | ||
314 | kfree(tmp); | |
315 | return 0; | |
316 | } | |
317 | ||
318 | static int | |
7c50328b | 319 | splat_validate(struct file *file, splat_subsystem_t *sub, int cmd, void *arg) |
f1ca4da6 | 320 | { |
7c50328b | 321 | splat_test_t *test; |
f1ca4da6 | 322 | |
323 | spin_lock(&(sub->test_lock)); | |
324 | list_for_each_entry(test, &(sub->test_list), test_list) { | |
325 | if (test->desc.id == cmd) { | |
326 | spin_unlock(&(sub->test_lock)); | |
327 | return test->test(file, arg); | |
328 | } | |
329 | } | |
330 | spin_unlock(&(sub->test_lock)); | |
331 | ||
332 | return -EINVAL; | |
333 | } | |
334 | ||
335 | static int | |
e554dffa | 336 | splat_ioctl_cfg(struct file *file, unsigned int cmd, unsigned long arg) |
f1ca4da6 | 337 | { |
7c50328b | 338 | splat_cfg_t kcfg; |
f1ca4da6 | 339 | int rc = 0; |
340 | ||
e554dffa BB |
341 | /* User and kernel space agree about arg size */ |
342 | if (_IOC_SIZE(cmd) != sizeof(kcfg)) | |
343 | return -EBADMSG; | |
344 | ||
7c50328b | 345 | if (copy_from_user(&kcfg, (splat_cfg_t *)arg, sizeof(kcfg))) |
f1ca4da6 | 346 | return -EFAULT; |
347 | ||
7c50328b | 348 | if (kcfg.cfg_magic != SPLAT_CFG_MAGIC) { |
349 | splat_print(file, "Bad config magic 0x%x != 0x%x\n", | |
350 | kcfg.cfg_magic, SPLAT_CFG_MAGIC); | |
f1ca4da6 | 351 | return -EINVAL; |
352 | } | |
353 | ||
354 | switch (kcfg.cfg_cmd) { | |
7c50328b | 355 | case SPLAT_CFG_BUFFER_CLEAR: |
f1ca4da6 | 356 | /* cfg_arg1 - Unused |
357 | * cfg_rc1 - Unused | |
358 | */ | |
7c50328b | 359 | rc = splat_buffer_clear(file, &kcfg, arg); |
f1ca4da6 | 360 | break; |
7c50328b | 361 | case SPLAT_CFG_BUFFER_SIZE: |
f1ca4da6 | 362 | /* cfg_arg1 - 0 - query size; >0 resize |
363 | * cfg_rc1 - Set to current buffer size | |
364 | */ | |
7c50328b | 365 | rc = splat_buffer_size(file, &kcfg, arg); |
f1ca4da6 | 366 | break; |
7c50328b | 367 | case SPLAT_CFG_SUBSYSTEM_COUNT: |
f1ca4da6 | 368 | /* cfg_arg1 - Unused |
369 | * cfg_rc1 - Set to number of subsystems | |
370 | */ | |
7c50328b | 371 | rc = splat_subsystem_count(&kcfg, arg); |
f1ca4da6 | 372 | break; |
7c50328b | 373 | case SPLAT_CFG_SUBSYSTEM_LIST: |
f1ca4da6 | 374 | /* cfg_arg1 - Unused |
375 | * cfg_rc1 - Set to number of subsystems | |
e554dffa | 376 | * cfg_data.splat_subsystems - Set with subsystems |
f1ca4da6 | 377 | */ |
7c50328b | 378 | rc = splat_subsystem_list(&kcfg, arg); |
f1ca4da6 | 379 | break; |
7c50328b | 380 | case SPLAT_CFG_TEST_COUNT: |
f1ca4da6 | 381 | /* cfg_arg1 - Set to a target subsystem |
382 | * cfg_rc1 - Set to number of tests | |
383 | */ | |
7c50328b | 384 | rc = splat_test_count(&kcfg, arg); |
f1ca4da6 | 385 | break; |
7c50328b | 386 | case SPLAT_CFG_TEST_LIST: |
f1ca4da6 | 387 | /* cfg_arg1 - Set to a target subsystem |
388 | * cfg_rc1 - Set to number of tests | |
7c50328b | 389 | * cfg_data.splat_subsystems - Populated with tests |
f1ca4da6 | 390 | */ |
7c50328b | 391 | rc = splat_test_list(&kcfg, arg); |
f1ca4da6 | 392 | break; |
393 | default: | |
e554dffa BB |
394 | splat_print(file, "Bad config command %d\n", |
395 | kcfg.cfg_cmd); | |
f1ca4da6 | 396 | rc = -EINVAL; |
397 | break; | |
398 | } | |
399 | ||
400 | return rc; | |
401 | } | |
402 | ||
403 | static int | |
e554dffa | 404 | splat_ioctl_cmd(struct file *file, unsigned int cmd, unsigned long arg) |
f1ca4da6 | 405 | { |
7c50328b | 406 | splat_subsystem_t *sub; |
407 | splat_cmd_t kcmd; | |
f1ca4da6 | 408 | int rc = -EINVAL; |
409 | void *data = NULL; | |
410 | ||
e554dffa BB |
411 | /* User and kernel space agree about arg size */ |
412 | if (_IOC_SIZE(cmd) != sizeof(kcmd)) | |
413 | return -EBADMSG; | |
414 | ||
7c50328b | 415 | if (copy_from_user(&kcmd, (splat_cfg_t *)arg, sizeof(kcmd))) |
f1ca4da6 | 416 | return -EFAULT; |
417 | ||
7c50328b | 418 | if (kcmd.cmd_magic != SPLAT_CMD_MAGIC) { |
419 | splat_print(file, "Bad command magic 0x%x != 0x%x\n", | |
420 | kcmd.cmd_magic, SPLAT_CFG_MAGIC); | |
f1ca4da6 | 421 | return -EINVAL; |
422 | } | |
423 | ||
424 | /* Allocate memory for any opaque data the caller needed to pass on */ | |
425 | if (kcmd.cmd_data_size > 0) { | |
426 | data = (void *)kmalloc(kcmd.cmd_data_size, GFP_KERNEL); | |
427 | if (data == NULL) | |
428 | return -ENOMEM; | |
429 | ||
7c50328b | 430 | if (copy_from_user(data, (void *)(arg + offsetof(splat_cmd_t, |
f1ca4da6 | 431 | cmd_data_str)), kcmd.cmd_data_size)) { |
432 | kfree(data); | |
433 | return -EFAULT; | |
434 | } | |
435 | } | |
436 | ||
7c50328b | 437 | sub = splat_subsystem_find(kcmd.cmd_subsystem); |
f1ca4da6 | 438 | if (sub != NULL) |
7c50328b | 439 | rc = splat_validate(file, sub, kcmd.cmd_test, data); |
f1ca4da6 | 440 | else |
441 | rc = -EINVAL; | |
442 | ||
443 | if (data != NULL) | |
444 | kfree(data); | |
445 | ||
446 | return rc; | |
447 | } | |
448 | ||
8655ce49 BB |
449 | static long |
450 | splat_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
f1ca4da6 | 451 | { |
f1b59d26 | 452 | int rc = 0; |
f1ca4da6 | 453 | |
454 | /* Ignore tty ioctls */ | |
455 | if ((cmd & 0xffffff00) == ((int)'T') << 8) | |
456 | return -ENOTTY; | |
457 | ||
f1ca4da6 | 458 | switch (cmd) { |
7c50328b | 459 | case SPLAT_CFG: |
e554dffa | 460 | rc = splat_ioctl_cfg(file, cmd, arg); |
f1ca4da6 | 461 | break; |
7c50328b | 462 | case SPLAT_CMD: |
e554dffa | 463 | rc = splat_ioctl_cmd(file, cmd, arg); |
f1ca4da6 | 464 | break; |
465 | default: | |
7c50328b | 466 | splat_print(file, "Bad ioctl command %d\n", cmd); |
f1ca4da6 | 467 | rc = -EINVAL; |
468 | break; | |
469 | } | |
470 | ||
471 | return rc; | |
472 | } | |
473 | ||
9593ef76 BB |
474 | #ifdef CONFIG_COMPAT |
475 | /* Compatibility handler for ioctls from 32-bit ELF binaries */ | |
476 | static long | |
477 | splat_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
478 | { | |
8655ce49 | 479 | return splat_unlocked_ioctl(file, cmd, arg); |
9593ef76 BB |
480 | } |
481 | #endif /* CONFIG_COMPAT */ | |
482 | ||
f1ca4da6 | 483 | /* I'm not sure why you would want to write in to this buffer from |
484 | * user space since its principle use is to pass test status info | |
485 | * back to the user space, but I don't see any reason to prevent it. | |
486 | */ | |
7c50328b | 487 | static ssize_t splat_write(struct file *file, const char __user *buf, |
f1ca4da6 | 488 | size_t count, loff_t *ppos) |
489 | { | |
7c50328b | 490 | splat_info_t *info = (splat_info_t *)file->private_data; |
f1ca4da6 | 491 | int rc = 0; |
492 | ||
f1ca4da6 | 493 | ASSERT(info); |
494 | ASSERT(info->info_buffer); | |
495 | ||
10946b02 | 496 | mutex_enter(&info->info_lock); |
f1ca4da6 | 497 | |
498 | /* Write beyond EOF */ | |
499 | if (*ppos >= info->info_size) { | |
500 | rc = -EFBIG; | |
501 | goto out; | |
502 | } | |
503 | ||
504 | /* Resize count if beyond EOF */ | |
505 | if (*ppos + count > info->info_size) | |
506 | count = info->info_size - *ppos; | |
507 | ||
508 | if (copy_from_user(info->info_buffer, buf, count)) { | |
509 | rc = -EFAULT; | |
510 | goto out; | |
511 | } | |
512 | ||
513 | *ppos += count; | |
514 | rc = count; | |
515 | out: | |
10946b02 | 516 | mutex_exit(&info->info_lock); |
f1ca4da6 | 517 | return rc; |
518 | } | |
519 | ||
7c50328b | 520 | static ssize_t splat_read(struct file *file, char __user *buf, |
f1ca4da6 | 521 | size_t count, loff_t *ppos) |
522 | { | |
7c50328b | 523 | splat_info_t *info = (splat_info_t *)file->private_data; |
f1ca4da6 | 524 | int rc = 0; |
525 | ||
f1ca4da6 | 526 | ASSERT(info); |
527 | ASSERT(info->info_buffer); | |
528 | ||
10946b02 | 529 | mutex_enter(&info->info_lock); |
f1ca4da6 | 530 | |
531 | /* Read beyond EOF */ | |
532 | if (*ppos >= info->info_size) | |
533 | goto out; | |
534 | ||
535 | /* Resize count if beyond EOF */ | |
536 | if (*ppos + count > info->info_size) | |
537 | count = info->info_size - *ppos; | |
538 | ||
539 | if (copy_to_user(buf, info->info_buffer + *ppos, count)) { | |
540 | rc = -EFAULT; | |
541 | goto out; | |
542 | } | |
543 | ||
544 | *ppos += count; | |
545 | rc = count; | |
546 | out: | |
10946b02 | 547 | mutex_exit(&info->info_lock); |
f1ca4da6 | 548 | return rc; |
549 | } | |
550 | ||
7c50328b | 551 | static loff_t splat_seek(struct file *file, loff_t offset, int origin) |
f1ca4da6 | 552 | { |
7c50328b | 553 | splat_info_t *info = (splat_info_t *)file->private_data; |
f1ca4da6 | 554 | int rc = -EINVAL; |
555 | ||
f1ca4da6 | 556 | ASSERT(info); |
557 | ASSERT(info->info_buffer); | |
558 | ||
10946b02 | 559 | mutex_enter(&info->info_lock); |
f1ca4da6 | 560 | |
561 | switch (origin) { | |
562 | case 0: /* SEEK_SET - No-op just do it */ | |
563 | break; | |
564 | case 1: /* SEEK_CUR - Seek from current */ | |
565 | offset = file->f_pos + offset; | |
566 | break; | |
567 | case 2: /* SEEK_END - Seek from end */ | |
568 | offset = info->info_size + offset; | |
569 | break; | |
570 | } | |
571 | ||
572 | if (offset >= 0) { | |
573 | file->f_pos = offset; | |
574 | file->f_version = 0; | |
575 | rc = offset; | |
576 | } | |
577 | ||
10946b02 | 578 | mutex_exit(&info->info_lock); |
f1ca4da6 | 579 | |
580 | return rc; | |
581 | } | |
582 | ||
7c50328b | 583 | static struct file_operations splat_fops = { |
9593ef76 BB |
584 | .owner = THIS_MODULE, |
585 | .open = splat_open, | |
586 | .release = splat_release, | |
8655ce49 | 587 | .unlocked_ioctl = splat_unlocked_ioctl, |
9593ef76 BB |
588 | #ifdef CONFIG_COMPAT |
589 | .compat_ioctl = splat_compat_ioctl, | |
590 | #endif | |
591 | .read = splat_read, | |
592 | .write = splat_write, | |
593 | .llseek = splat_seek, | |
f1ca4da6 | 594 | }; |
595 | ||
10946b02 AX |
596 | static struct miscdevice splat_misc = { |
597 | .minor = MISC_DYNAMIC_MINOR, | |
598 | .name = SPLAT_NAME, | |
599 | .fops = &splat_fops, | |
600 | }; | |
601 | ||
ec06701b AX |
602 | static void splat_subsystem_init(const char *name, |
603 | splat_subsystem_t *(*init)(void)) | |
604 | { | |
605 | splat_subsystem_t *sub; | |
606 | sub = init(); | |
607 | if (sub == NULL) { | |
608 | printk(KERN_ERR "splat: Error initializing: %s\n", name); | |
609 | return; | |
610 | } | |
611 | spin_lock(&splat_module_lock); | |
612 | list_add_tail(&sub->subsystem_list, &splat_module_list); | |
613 | spin_unlock(&splat_module_lock); | |
614 | } | |
615 | ||
616 | static void splat_subsystem_fini(const char *name, | |
617 | int (*id_func)(void), void (*fini)(splat_subsystem_t *)) | |
618 | { | |
619 | splat_subsystem_t *sub, *tmp; | |
620 | int id, flag = 0; | |
621 | ||
622 | id = id_func(); | |
623 | spin_lock(&splat_module_lock); | |
624 | list_for_each_entry_safe(sub, tmp, &splat_module_list, subsystem_list) { | |
625 | if (sub->desc.id == id) { | |
626 | list_del_init(&sub->subsystem_list); | |
627 | flag = 1; | |
628 | break; | |
629 | } | |
630 | } | |
631 | spin_unlock(&splat_module_lock); | |
632 | if (flag == 0) | |
633 | printk(KERN_ERR "splat: Error finalizing: %s\n", name); | |
634 | else | |
635 | fini(sub); | |
636 | } | |
637 | ||
638 | #define SPLAT_SUBSYSTEM_INIT(type) \ | |
639 | splat_subsystem_init(#type, splat_##type##_init) | |
640 | #define SPLAT_SUBSYSTEM_FINI(type) \ | |
641 | splat_subsystem_fini(#type, splat_##type##_id, splat_##type##_fini) | |
642 | ||
643 | void splat_test_init(splat_subsystem_t *sub, const char *name, | |
644 | const char *desc, unsigned int tid, splat_test_func_t func) | |
645 | { | |
646 | splat_test_t *test; | |
647 | test = kmalloc(sizeof (splat_test_t), GFP_KERNEL); | |
648 | if (test == NULL) { | |
649 | printk(KERN_ERR "splat: Error initializing: %s/%u\n", | |
650 | name, tid); | |
651 | return; | |
652 | } | |
653 | memset(test, 0, sizeof (splat_test_t)); | |
654 | strncpy(test->desc.name, name, SPLAT_NAME_SIZE-1); | |
655 | strncpy(test->desc.desc, desc, SPLAT_DESC_SIZE-1); | |
656 | test->desc.id = tid; | |
657 | test->test = func; | |
658 | INIT_LIST_HEAD(&test->test_list); | |
659 | spin_lock(&sub->test_lock); | |
660 | list_add_tail(&test->test_list, &sub->test_list); | |
661 | spin_unlock(&sub->test_lock); | |
662 | } | |
663 | ||
664 | void splat_test_fini(splat_subsystem_t *sub, unsigned int tid) | |
665 | { | |
666 | splat_test_t *test, *tmp; | |
667 | int flag = 0; | |
668 | ||
669 | spin_lock(&sub->test_lock); | |
670 | list_for_each_entry_safe(test, tmp, &sub->test_list, test_list) { | |
671 | if (test->desc.id == tid) { | |
672 | list_del_init(&test->test_list); | |
673 | kfree(test); | |
674 | flag = 1; | |
675 | break; | |
676 | } | |
677 | } | |
678 | spin_unlock(&sub->test_lock); | |
679 | ||
680 | if (flag == 0) | |
681 | printk(KERN_ERR "splat: Error finalizing: %u\n", tid); | |
682 | } | |
683 | ||
10946b02 | 684 | static int __init |
7c50328b | 685 | splat_init(void) |
f1ca4da6 | 686 | { |
10946b02 | 687 | int error; |
f1ca4da6 | 688 | |
7c50328b | 689 | spin_lock_init(&splat_module_lock); |
690 | INIT_LIST_HEAD(&splat_module_list); | |
f1ca4da6 | 691 | |
7c50328b | 692 | SPLAT_SUBSYSTEM_INIT(kmem); |
693 | SPLAT_SUBSYSTEM_INIT(taskq); | |
694 | SPLAT_SUBSYSTEM_INIT(krng); | |
695 | SPLAT_SUBSYSTEM_INIT(mutex); | |
696 | SPLAT_SUBSYSTEM_INIT(condvar); | |
697 | SPLAT_SUBSYSTEM_INIT(thread); | |
698 | SPLAT_SUBSYSTEM_INIT(rwlock); | |
699 | SPLAT_SUBSYSTEM_INIT(time); | |
4b171585 | 700 | SPLAT_SUBSYSTEM_INIT(vnode); |
9490c148 | 701 | SPLAT_SUBSYSTEM_INIT(kobj); |
9f4c835a | 702 | SPLAT_SUBSYSTEM_INIT(atomic); |
d702c04f | 703 | SPLAT_SUBSYSTEM_INIT(list); |
b871b8cd | 704 | SPLAT_SUBSYSTEM_INIT(generic); |
ec7d53e9 | 705 | SPLAT_SUBSYSTEM_INIT(cred); |
19c1eb82 | 706 | SPLAT_SUBSYSTEM_INIT(zlib); |
bf0c60c0 | 707 | SPLAT_SUBSYSTEM_INIT(linux); |
f1ca4da6 | 708 | |
10946b02 AX |
709 | error = misc_register(&splat_misc); |
710 | if (error) { | |
711 | printk(KERN_INFO "SPLAT: misc_register() failed %d\n", error); | |
712 | } else { | |
713 | printk(KERN_INFO "SPLAT: Loaded module v%s-%s%s\n", | |
714 | SPL_META_VERSION, SPL_META_RELEASE, SPL_DEBUG_STR); | |
f1ca4da6 | 715 | } |
716 | ||
10946b02 | 717 | return (error); |
f1ca4da6 | 718 | } |
719 | ||
10946b02 | 720 | static void __exit |
7c50328b | 721 | splat_fini(void) |
f1ca4da6 | 722 | { |
f6188ddd | 723 | misc_deregister(&splat_misc); |
7c50328b | 724 | |
bf0c60c0 | 725 | SPLAT_SUBSYSTEM_FINI(linux); |
19c1eb82 | 726 | SPLAT_SUBSYSTEM_FINI(zlib); |
ec7d53e9 | 727 | SPLAT_SUBSYSTEM_FINI(cred); |
b871b8cd | 728 | SPLAT_SUBSYSTEM_FINI(generic); |
d702c04f | 729 | SPLAT_SUBSYSTEM_FINI(list); |
9f4c835a | 730 | SPLAT_SUBSYSTEM_FINI(atomic); |
9490c148 | 731 | SPLAT_SUBSYSTEM_FINI(kobj); |
4b171585 | 732 | SPLAT_SUBSYSTEM_FINI(vnode); |
7c50328b | 733 | SPLAT_SUBSYSTEM_FINI(time); |
734 | SPLAT_SUBSYSTEM_FINI(rwlock); | |
735 | SPLAT_SUBSYSTEM_FINI(thread); | |
736 | SPLAT_SUBSYSTEM_FINI(condvar); | |
737 | SPLAT_SUBSYSTEM_FINI(mutex); | |
738 | SPLAT_SUBSYSTEM_FINI(krng); | |
739 | SPLAT_SUBSYSTEM_FINI(taskq); | |
740 | SPLAT_SUBSYSTEM_FINI(kmem); | |
741 | ||
742 | ASSERT(list_empty(&splat_module_list)); | |
0835057e | 743 | printk(KERN_INFO "SPLAT: Unloaded module v%s-%s%s\n", |
10946b02 | 744 | SPL_META_VERSION, SPL_META_RELEASE, SPL_DEBUG_STR); |
f1ca4da6 | 745 | } |
746 | ||
10946b02 AX |
747 | module_init(splat_init); |
748 | module_exit(splat_fini); | |
f1ca4da6 | 749 | |
715f6251 | 750 | MODULE_DESCRIPTION("Solaris Porting LAyer Tests"); |
10946b02 AX |
751 | MODULE_AUTHOR(SPL_META_AUTHOR); |
752 | MODULE_LICENSE(SPL_META_LICENSE); | |
33a20369 | 753 | MODULE_VERSION(SPL_META_VERSION "-" SPL_META_RELEASE); |