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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
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.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Test Control Interface.
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.
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
36 * the tests for this subsystem using the SPLAT_TEST_INIT() macro.
37 * Similarly splat_*_fini() is called when the splat module is removed
38 * and is responsible for unregistering its tests via the SPLAT_TEST_FINI
39 * macro. Once a test is registered it can then be run with an ioctl()
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 \*****************************************************************************/
46 #include "splat-internal.h"
48 static spl_class
*splat_class
;
49 static spl_device
*splat_device
;
50 static struct list_head splat_module_list
;
51 static spinlock_t splat_module_lock
;
54 splat_open(struct inode
*inode
, struct file
*file
)
56 unsigned int minor
= iminor(inode
);
59 if (minor
>= SPLAT_MINORS
)
62 info
= (splat_info_t
*)kmalloc(sizeof(*info
), GFP_KERNEL
);
66 spin_lock_init(&info
->info_lock
);
67 info
->info_size
= SPLAT_INFO_BUFFER_SIZE
;
68 info
->info_buffer
= (char *)vmalloc(SPLAT_INFO_BUFFER_SIZE
);
69 if (info
->info_buffer
== NULL
) {
74 info
->info_head
= info
->info_buffer
;
75 file
->private_data
= (void *)info
;
81 splat_release(struct inode
*inode
, struct file
*file
)
83 unsigned int minor
= iminor(inode
);
84 splat_info_t
*info
= (splat_info_t
*)file
->private_data
;
86 if (minor
>= SPLAT_MINORS
)
90 ASSERT(info
->info_buffer
);
92 vfree(info
->info_buffer
);
99 splat_buffer_clear(struct file
*file
, splat_cfg_t
*kcfg
, unsigned long arg
)
101 splat_info_t
*info
= (splat_info_t
*)file
->private_data
;
104 ASSERT(info
->info_buffer
);
106 spin_lock(&info
->info_lock
);
107 memset(info
->info_buffer
, 0, info
->info_size
);
108 info
->info_head
= info
->info_buffer
;
109 spin_unlock(&info
->info_lock
);
115 splat_buffer_size(struct file
*file
, splat_cfg_t
*kcfg
, unsigned long arg
)
117 splat_info_t
*info
= (splat_info_t
*)file
->private_data
;
119 int min
, size
, rc
= 0;
122 ASSERT(info
->info_buffer
);
124 spin_lock(&info
->info_lock
);
125 if (kcfg
->cfg_arg1
> 0) {
127 size
= kcfg
->cfg_arg1
;
128 buf
= (char *)vmalloc(size
);
134 /* Zero fill and truncate contents when coping buffer */
135 min
= ((size
< info
->info_size
) ? size
: info
->info_size
);
136 memset(buf
, 0, size
);
137 memcpy(buf
, info
->info_buffer
, min
);
138 vfree(info
->info_buffer
);
139 info
->info_size
= size
;
140 info
->info_buffer
= buf
;
141 info
->info_head
= info
->info_buffer
;
144 kcfg
->cfg_rc1
= info
->info_size
;
146 if (copy_to_user((struct splat_cfg_t __user
*)arg
, kcfg
, sizeof(*kcfg
)))
149 spin_unlock(&info
->info_lock
);
155 static splat_subsystem_t
*
156 splat_subsystem_find(int id
) {
157 splat_subsystem_t
*sub
;
159 spin_lock(&splat_module_lock
);
160 list_for_each_entry(sub
, &splat_module_list
, subsystem_list
) {
161 if (id
== sub
->desc
.id
) {
162 spin_unlock(&splat_module_lock
);
166 spin_unlock(&splat_module_lock
);
172 splat_subsystem_count(splat_cfg_t
*kcfg
, unsigned long arg
)
174 splat_subsystem_t
*sub
;
177 spin_lock(&splat_module_lock
);
178 list_for_each_entry(sub
, &splat_module_list
, subsystem_list
)
181 spin_unlock(&splat_module_lock
);
184 if (copy_to_user((struct splat_cfg_t __user
*)arg
, kcfg
, sizeof(*kcfg
)))
191 splat_subsystem_list(splat_cfg_t
*kcfg
, unsigned long arg
)
193 splat_subsystem_t
*sub
;
197 /* Structure will be sized large enough for N subsystem entries
198 * which is passed in by the caller. On exit the number of
199 * entries filled in with valid subsystems will be stored in
200 * cfg_rc1. If the caller does not provide enough entries
201 * for all subsystems we will truncate the list to avoid overrun.
203 size
= sizeof(*tmp
) + kcfg
->cfg_data
.splat_subsystems
.size
*
204 sizeof(splat_user_t
);
205 tmp
= kmalloc(size
, GFP_KERNEL
);
209 /* Local 'tmp' is used as the structure copied back to user space */
210 memset(tmp
, 0, size
);
211 memcpy(tmp
, kcfg
, sizeof(*kcfg
));
213 spin_lock(&splat_module_lock
);
214 list_for_each_entry(sub
, &splat_module_list
, subsystem_list
) {
215 strncpy(tmp
->cfg_data
.splat_subsystems
.descs
[i
].name
,
216 sub
->desc
.name
, SPLAT_NAME_SIZE
);
217 strncpy(tmp
->cfg_data
.splat_subsystems
.descs
[i
].desc
,
218 sub
->desc
.desc
, SPLAT_DESC_SIZE
);
219 tmp
->cfg_data
.splat_subsystems
.descs
[i
].id
= sub
->desc
.id
;
221 /* Truncate list if we are about to overrun alloc'ed memory */
222 if ((i
++) == kcfg
->cfg_data
.splat_subsystems
.size
)
225 spin_unlock(&splat_module_lock
);
228 if (copy_to_user((struct splat_cfg_t __user
*)arg
, tmp
, size
)) {
238 splat_test_count(splat_cfg_t
*kcfg
, unsigned long arg
)
240 splat_subsystem_t
*sub
;
244 /* Subsystem ID passed as arg1 */
245 sub
= splat_subsystem_find(kcfg
->cfg_arg1
);
249 spin_lock(&(sub
->test_lock
));
250 list_for_each_entry(test
, &(sub
->test_list
), test_list
)
253 spin_unlock(&(sub
->test_lock
));
256 if (copy_to_user((struct splat_cfg_t __user
*)arg
, kcfg
, sizeof(*kcfg
)))
263 splat_test_list(splat_cfg_t
*kcfg
, unsigned long arg
)
265 splat_subsystem_t
*sub
;
270 /* Subsystem ID passed as arg1 */
271 sub
= splat_subsystem_find(kcfg
->cfg_arg1
);
275 /* Structure will be sized large enough for N test entries
276 * which is passed in by the caller. On exit the number of
277 * entries filled in with valid tests will be stored in
278 * cfg_rc1. If the caller does not provide enough entries
279 * for all tests we will truncate the list to avoid overrun.
281 size
= sizeof(*tmp
)+kcfg
->cfg_data
.splat_tests
.size
*sizeof(splat_user_t
);
282 tmp
= kmalloc(size
, GFP_KERNEL
);
286 /* Local 'tmp' is used as the structure copied back to user space */
287 memset(tmp
, 0, size
);
288 memcpy(tmp
, kcfg
, sizeof(*kcfg
));
290 spin_lock(&(sub
->test_lock
));
291 list_for_each_entry(test
, &(sub
->test_list
), test_list
) {
292 strncpy(tmp
->cfg_data
.splat_tests
.descs
[i
].name
,
293 test
->desc
.name
, SPLAT_NAME_SIZE
);
294 strncpy(tmp
->cfg_data
.splat_tests
.descs
[i
].desc
,
295 test
->desc
.desc
, SPLAT_DESC_SIZE
);
296 tmp
->cfg_data
.splat_tests
.descs
[i
].id
= test
->desc
.id
;
298 /* Truncate list if we are about to overrun alloc'ed memory */
299 if ((i
++) == kcfg
->cfg_data
.splat_tests
.size
)
302 spin_unlock(&(sub
->test_lock
));
305 if (copy_to_user((struct splat_cfg_t __user
*)arg
, tmp
, size
)) {
315 splat_validate(struct file
*file
, splat_subsystem_t
*sub
, int cmd
, void *arg
)
319 spin_lock(&(sub
->test_lock
));
320 list_for_each_entry(test
, &(sub
->test_list
), test_list
) {
321 if (test
->desc
.id
== cmd
) {
322 spin_unlock(&(sub
->test_lock
));
323 return test
->test(file
, arg
);
326 spin_unlock(&(sub
->test_lock
));
332 splat_ioctl_cfg(struct file
*file
, unsigned int cmd
, unsigned long arg
)
337 /* User and kernel space agree about arg size */
338 if (_IOC_SIZE(cmd
) != sizeof(kcfg
))
341 if (copy_from_user(&kcfg
, (splat_cfg_t
*)arg
, sizeof(kcfg
)))
344 if (kcfg
.cfg_magic
!= SPLAT_CFG_MAGIC
) {
345 splat_print(file
, "Bad config magic 0x%x != 0x%x\n",
346 kcfg
.cfg_magic
, SPLAT_CFG_MAGIC
);
350 switch (kcfg
.cfg_cmd
) {
351 case SPLAT_CFG_BUFFER_CLEAR
:
355 rc
= splat_buffer_clear(file
, &kcfg
, arg
);
357 case SPLAT_CFG_BUFFER_SIZE
:
358 /* cfg_arg1 - 0 - query size; >0 resize
359 * cfg_rc1 - Set to current buffer size
361 rc
= splat_buffer_size(file
, &kcfg
, arg
);
363 case SPLAT_CFG_SUBSYSTEM_COUNT
:
365 * cfg_rc1 - Set to number of subsystems
367 rc
= splat_subsystem_count(&kcfg
, arg
);
369 case SPLAT_CFG_SUBSYSTEM_LIST
:
371 * cfg_rc1 - Set to number of subsystems
372 * cfg_data.splat_subsystems - Set with subsystems
374 rc
= splat_subsystem_list(&kcfg
, arg
);
376 case SPLAT_CFG_TEST_COUNT
:
377 /* cfg_arg1 - Set to a target subsystem
378 * cfg_rc1 - Set to number of tests
380 rc
= splat_test_count(&kcfg
, arg
);
382 case SPLAT_CFG_TEST_LIST
:
383 /* cfg_arg1 - Set to a target subsystem
384 * cfg_rc1 - Set to number of tests
385 * cfg_data.splat_subsystems - Populated with tests
387 rc
= splat_test_list(&kcfg
, arg
);
390 splat_print(file
, "Bad config command %d\n",
400 splat_ioctl_cmd(struct file
*file
, unsigned int cmd
, unsigned long arg
)
402 splat_subsystem_t
*sub
;
407 /* User and kernel space agree about arg size */
408 if (_IOC_SIZE(cmd
) != sizeof(kcmd
))
411 if (copy_from_user(&kcmd
, (splat_cfg_t
*)arg
, sizeof(kcmd
)))
414 if (kcmd
.cmd_magic
!= SPLAT_CMD_MAGIC
) {
415 splat_print(file
, "Bad command magic 0x%x != 0x%x\n",
416 kcmd
.cmd_magic
, SPLAT_CFG_MAGIC
);
420 /* Allocate memory for any opaque data the caller needed to pass on */
421 if (kcmd
.cmd_data_size
> 0) {
422 data
= (void *)kmalloc(kcmd
.cmd_data_size
, GFP_KERNEL
);
426 if (copy_from_user(data
, (void *)(arg
+ offsetof(splat_cmd_t
,
427 cmd_data_str
)), kcmd
.cmd_data_size
)) {
433 sub
= splat_subsystem_find(kcmd
.cmd_subsystem
);
435 rc
= splat_validate(file
, sub
, kcmd
.cmd_test
, data
);
446 splat_ioctl(struct inode
*inode
, struct file
*file
,
447 unsigned int cmd
, unsigned long arg
)
449 unsigned int minor
= iminor(file
->f_dentry
->d_inode
);
452 /* Ignore tty ioctls */
453 if ((cmd
& 0xffffff00) == ((int)'T') << 8)
456 if (minor
>= SPLAT_MINORS
)
461 rc
= splat_ioctl_cfg(file
, cmd
, arg
);
464 rc
= splat_ioctl_cmd(file
, cmd
, arg
);
467 splat_print(file
, "Bad ioctl command %d\n", cmd
);
476 /* Compatibility handler for ioctls from 32-bit ELF binaries */
478 splat_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
480 return splat_ioctl(NULL
, file
, cmd
, arg
);
482 #endif /* CONFIG_COMPAT */
484 /* I'm not sure why you would want to write in to this buffer from
485 * user space since its principle use is to pass test status info
486 * back to the user space, but I don't see any reason to prevent it.
488 static ssize_t
splat_write(struct file
*file
, const char __user
*buf
,
489 size_t count
, loff_t
*ppos
)
491 unsigned int minor
= iminor(file
->f_dentry
->d_inode
);
492 splat_info_t
*info
= (splat_info_t
*)file
->private_data
;
495 if (minor
>= SPLAT_MINORS
)
499 ASSERT(info
->info_buffer
);
501 spin_lock(&info
->info_lock
);
503 /* Write beyond EOF */
504 if (*ppos
>= info
->info_size
) {
509 /* Resize count if beyond EOF */
510 if (*ppos
+ count
> info
->info_size
)
511 count
= info
->info_size
- *ppos
;
513 if (copy_from_user(info
->info_buffer
, buf
, count
)) {
521 spin_unlock(&info
->info_lock
);
525 static ssize_t
splat_read(struct file
*file
, char __user
*buf
,
526 size_t count
, loff_t
*ppos
)
528 unsigned int minor
= iminor(file
->f_dentry
->d_inode
);
529 splat_info_t
*info
= (splat_info_t
*)file
->private_data
;
532 if (minor
>= SPLAT_MINORS
)
536 ASSERT(info
->info_buffer
);
538 spin_lock(&info
->info_lock
);
540 /* Read beyond EOF */
541 if (*ppos
>= info
->info_size
)
544 /* Resize count if beyond EOF */
545 if (*ppos
+ count
> info
->info_size
)
546 count
= info
->info_size
- *ppos
;
548 if (copy_to_user(buf
, info
->info_buffer
+ *ppos
, count
)) {
556 spin_unlock(&info
->info_lock
);
560 static loff_t
splat_seek(struct file
*file
, loff_t offset
, int origin
)
562 unsigned int minor
= iminor(file
->f_dentry
->d_inode
);
563 splat_info_t
*info
= (splat_info_t
*)file
->private_data
;
566 if (minor
>= SPLAT_MINORS
)
570 ASSERT(info
->info_buffer
);
572 spin_lock(&info
->info_lock
);
575 case 0: /* SEEK_SET - No-op just do it */
577 case 1: /* SEEK_CUR - Seek from current */
578 offset
= file
->f_pos
+ offset
;
580 case 2: /* SEEK_END - Seek from end */
581 offset
= info
->info_size
+ offset
;
586 file
->f_pos
= offset
;
591 spin_unlock(&info
->info_lock
);
596 static struct cdev splat_cdev
;
597 static struct file_operations splat_fops
= {
598 .owner
= THIS_MODULE
,
600 .release
= splat_release
,
601 .ioctl
= splat_ioctl
,
603 .compat_ioctl
= splat_compat_ioctl
,
606 .write
= splat_write
,
607 .llseek
= splat_seek
,
616 spin_lock_init(&splat_module_lock
);
617 INIT_LIST_HEAD(&splat_module_list
);
619 SPLAT_SUBSYSTEM_INIT(kmem
);
620 SPLAT_SUBSYSTEM_INIT(taskq
);
621 SPLAT_SUBSYSTEM_INIT(krng
);
622 SPLAT_SUBSYSTEM_INIT(mutex
);
623 SPLAT_SUBSYSTEM_INIT(condvar
);
624 SPLAT_SUBSYSTEM_INIT(thread
);
625 SPLAT_SUBSYSTEM_INIT(rwlock
);
626 SPLAT_SUBSYSTEM_INIT(time
);
627 SPLAT_SUBSYSTEM_INIT(vnode
);
628 SPLAT_SUBSYSTEM_INIT(kobj
);
629 SPLAT_SUBSYSTEM_INIT(atomic
);
630 SPLAT_SUBSYSTEM_INIT(list
);
631 SPLAT_SUBSYSTEM_INIT(generic
);
632 SPLAT_SUBSYSTEM_INIT(cred
);
634 dev
= MKDEV(SPLAT_MAJOR
, 0);
635 if ((rc
= register_chrdev_region(dev
, SPLAT_MINORS
, SPLAT_NAME
)))
638 /* Support for registering a character driver */
639 cdev_init(&splat_cdev
, &splat_fops
);
640 splat_cdev
.owner
= THIS_MODULE
;
641 kobject_set_name(&splat_cdev
.kobj
, SPLAT_NAME
);
642 if ((rc
= cdev_add(&splat_cdev
, dev
, SPLAT_MINORS
))) {
643 printk(KERN_ERR
"SPLAT: Error adding cdev, %d\n", rc
);
644 kobject_put(&splat_cdev
.kobj
);
645 unregister_chrdev_region(dev
, SPLAT_MINORS
);
649 /* Support for udev make driver info available in sysfs */
650 splat_class
= spl_class_create(THIS_MODULE
, "splat");
651 if (IS_ERR(splat_class
)) {
652 rc
= PTR_ERR(splat_class
);
653 printk(KERN_ERR
"SPLAT: Error creating splat class, %d\n", rc
);
654 cdev_del(&splat_cdev
);
655 unregister_chrdev_region(dev
, SPLAT_MINORS
);
659 splat_device
= spl_device_create(splat_class
, NULL
,
660 MKDEV(SPLAT_MAJOR
, 0),
663 printk(KERN_INFO
"SPLAT: Loaded Solaris Porting LAyer "
664 "Tests v%s\n", SPL_META_VERSION
);
667 printk(KERN_ERR
"SPLAT: Error registering splat device, %d\n", rc
);
674 dev_t dev
= MKDEV(SPLAT_MAJOR
, 0);
676 spl_device_destroy(splat_class
, splat_device
, dev
);
677 spl_class_destroy(splat_class
);
678 cdev_del(&splat_cdev
);
679 unregister_chrdev_region(dev
, SPLAT_MINORS
);
681 SPLAT_SUBSYSTEM_FINI(cred
);
682 SPLAT_SUBSYSTEM_FINI(generic
);
683 SPLAT_SUBSYSTEM_FINI(list
);
684 SPLAT_SUBSYSTEM_FINI(atomic
);
685 SPLAT_SUBSYSTEM_FINI(kobj
);
686 SPLAT_SUBSYSTEM_FINI(vnode
);
687 SPLAT_SUBSYSTEM_FINI(time
);
688 SPLAT_SUBSYSTEM_FINI(rwlock
);
689 SPLAT_SUBSYSTEM_FINI(thread
);
690 SPLAT_SUBSYSTEM_FINI(condvar
);
691 SPLAT_SUBSYSTEM_FINI(mutex
);
692 SPLAT_SUBSYSTEM_FINI(krng
);
693 SPLAT_SUBSYSTEM_FINI(taskq
);
694 SPLAT_SUBSYSTEM_FINI(kmem
);
696 ASSERT(list_empty(&splat_module_list
));
697 printk(KERN_INFO
"SPLAT: Unloaded Solaris Porting LAyer "
698 "Tests v%s\n", SPL_META_VERSION
);
703 spl_module_init(splat_init
);
704 spl_module_exit(splat_fini
);
706 MODULE_AUTHOR("Lawrence Livermore National Labs");
707 MODULE_DESCRIPTION("Solaris Porting LAyer Tests");
708 MODULE_LICENSE("GPL");