├── README.md └── misc ├── Makefile ├── misc.c └── misc.h /README.md: -------------------------------------------------------------------------------- 1 | Block filter driver 2 | ------------------- 3 | 4 | This is a barebones filter driver for Linux 3.x driver. This is partially inspired from the misc driver in FGFT project (in this github). A block filter is one that interposes block request. By default this driver interposes /dev/sda1. You can change this in misc.c or provide it as a module parameter. 5 | 6 | 7 | 8 | How it works 9 | ------------ 10 | 11 | misc is a Linux misc device. It registers with the kernel and finds the appropriate block device from the given block device string (like /dev/sda). It then obtains the block device queue and replaces the request function with its own. The dummy request function just calls back the original function. Fairly straightforward. 12 | 13 | 14 | Backstory 15 | --------- 16 | 17 | I wanted to use a block filter driver and assumed I would find one over the internet. However, there was none to be found and I wrote one of my own. I am putting this on my github if anyone needs it. NO WARRANTIES. COMPILE TESTED ONLY. 18 | 19 | 20 | Keywords 21 | -------- 22 | 23 | Linux kernel block filter driver 24 | -------------------------------------------------------------------------------- /misc/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(KSRC),) 2 | KSRC := /lib/modules/$(shell uname -r)/build 3 | endif 4 | 5 | obj-m += misc.o 6 | 7 | all: 8 | $(MAKE) -C$(KSRC) M=$(PWD) 9 | 10 | install: 11 | install -o root -g root -m 0755 misc.ko $(DESTDIR)/lib/modules/$(shell uname -r)/kernel/drivers/block/ 12 | depmod -a 13 | 14 | uninstall: 15 | rm -f $(DESTDIR)/lib/modules/$(shell uname -r)/kernel/drivers/block/misc.ko 16 | depmod -a 17 | 18 | clean: 19 | rm -rf *.o *.ko *.symvers *.mod.c .*.cmd Module.markers modules.order .tmp_versions .misc.o.d 20 | -------------------------------------------------------------------------------- /misc/misc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2013, Asim Kadav, asimkadav@gmail.com 4 | * 5 | * A very simple block filter driver 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | * SOFTWARE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | #include "misc.h" 42 | 43 | MODULE_AUTHOR("Asim Kadav"); 44 | MODULE_LICENSE("GPL"); 45 | 46 | static struct miscdevice misc_help; 47 | static struct block_device *blkdev; 48 | static void (*original_request_fn) (struct request_queue*, struct bio*); 49 | 50 | 51 | /* Sample ioctl code - not used. Can be used to trigger on/off filtering. */ 52 | static long mischelp_ioctl(/*struct inode *inode,*/ struct file *fp, 53 | unsigned int cmd, unsigned long arg) { 54 | 55 | if (cmd == MISC_GET) { 56 | printk ("Can perform get ops %d.\n", (int) arg); 57 | } 58 | 59 | if (cmd == MISC_PUT) { 60 | printk ("Can perform put ops %d.\n", (int) arg); 61 | } 62 | 63 | return 0; 64 | } 65 | 66 | 67 | struct file_operations misc_fops = { 68 | .unlocked_ioctl = mischelp_ioctl, 69 | .owner = THIS_MODULE, 70 | .mmap = NULL, 71 | }; 72 | 73 | void misc_request_fn(struct request_queue *q, struct bio *bio) { 74 | printk ("we are passing bios.\n"); 75 | // here is where we trace requests... 76 | original_request_fn (q, bio); 77 | return; 78 | } 79 | 80 | 81 | void register_block_device(char *path) { 82 | 83 | struct request_queue *blkdev_queue = NULL; 84 | 85 | if (path == NULL) { 86 | printk ("Block device empty.\n"); 87 | return; 88 | } 89 | 90 | printk ("Will open %s.\n", path); 91 | 92 | blkdev = lookup_bdev(path); 93 | 94 | if (IS_ERR(blkdev)) { 95 | printk ("No such block device.\n"); 96 | return; 97 | } 98 | 99 | printk ("Found block device %p with bs %d.\n", blkdev, blkdev->bd_block_size); 100 | blkdev_queue = bdev_get_queue(blkdev); 101 | original_request_fn = blkdev_queue->request_fn; 102 | blkdev_queue->request_fn = misc_request_fn; 103 | } 104 | 105 | void unregister_block_device(void) { 106 | struct request_queue *blkdev_queue = NULL; 107 | 108 | blkdev_queue = bdev_get_queue(blkdev); 109 | 110 | if ((blkdev_queue->request_fn != NULL) && 111 | (original_request_fn != NULL)) { 112 | 113 | blkdev_queue->request_fn = original_request_fn; 114 | printk ("Successfully unregistered block device.\n"); 115 | } 116 | } 117 | 118 | 119 | 120 | int init_module(void) { 121 | int retval = 0; 122 | static char *mischelp_name = "mischelp"; 123 | 124 | misc_help.minor = MISC_MINOR; 125 | misc_help.name = mischelp_name; 126 | misc_help.fops = &misc_fops; 127 | retval = misc_register(&misc_help); 128 | 129 | if (retval) 130 | return retval; 131 | 132 | register_block_device("/dev/sda"); 133 | 134 | printk ("block tracer initialized successfully.\n"); 135 | return 0; 136 | } 137 | 138 | void cleanup_module(void){ 139 | int number = 0; 140 | 141 | unregister_block_device(); 142 | 143 | number = misc_deregister(&misc_help); 144 | if (number < 0) { 145 | printk ("misc_deregister failed. %d\n", number); 146 | } 147 | 148 | printk ("It's over for block tracer.. \n"); 149 | } 150 | 151 | 152 | -------------------------------------------------------------------------------- /misc/misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2013, Asim Kadav, asimkadav@gmail.com 4 | * 5 | * A very simple block filter driver 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | * SOFTWARE. 24 | */ 25 | 26 | #ifndef _MISCDEVICE_H 27 | #define _MISCDEVICE_H 28 | 29 | // Driver number 30 | #define MISC_MINOR 45 31 | 32 | 33 | // Fault injection op-codes 34 | // 35 | #define MISC_GET 0x101 36 | #define MISC_PUT 0x102 37 | 38 | #endif 39 | --------------------------------------------------------------------------------