Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/fs.h>
  5. #include <linux/blkdev.h>
  6. #include <linux/cdev.h>
  7. #include <linux/kthread.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/bio.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/types.h>
  12. #include <linux/major.h>
  13. #include <linux/file.h>
  14. #include <linux/slab.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/fs.h>
  17. #include <linux/mm.h>
  18. #include <asm/uaccess.h>
  19. #include <linux/types.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/delay.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/sched.h>
  24. #include <linux/mm.h>
  25.  
  26. #define PARTITION_NAME "/dev/sda1"
  27.  
  28. #define DELAY(); \
  29. printk(KERN_INFO "filter: FUNCTION=%s LINE=%d\n", __FUNCTION__, __LINE__); \
  30. set_current_state(TASK_UNINTERRUPTIBLE); \
  31. schedule_timeout(HZ * 5);
  32.  
  33. struct block_device *bd;
  34.  
  35. make_request_fn *orig_mrqfn;
  36.  
  37. static void filter_mrqfn(struct request_queue *rq, struct bio *b)
  38. {
  39. orig_mrqfn(rq, b); /* calling the original make_request_fn() */
  40. }
  41.  
  42. /*
  43. * set_mrqfn() - Change the original make_request_fn() to our
  44. * modules request function
  45. */
  46. static void set_mrqfn(void)
  47. {
  48. int ret;
  49. struct super_block *sb = bd->bd_super;
  50. printk(KERN_DEBUG "filter: %s\n", __FUNCTION__);
  51.  
  52. DELAY();
  53. /* lock filesystem to prevent any further changes */
  54. sb = freeze_bdev(bd);
  55. if (sb)
  56. printk(KERN_INFO "filter: freez block device\n");
  57. DELAY();
  58.  
  59. if (bd->bd_disk->queue->make_request_fn == filter_mrqfn) {
  60. printk(KERN_INFO "filter: modules request function is already active\n");
  61. } else {
  62. /* save the pointer to the original make_request_fn() */
  63. orig_mrqfn = bd->bd_disk->queue->make_request_fn;
  64. /* replace the original with our modules request function */
  65. bd->bd_disk->queue->make_request_fn = filter_mrqfn;
  66. }
  67.  
  68. /* unlock filesystem */
  69. thaw_bdev(bd, sb);
  70. }
  71.  
  72. /*
  73. * restore_mrqfn() - Restore the original make_request_fn()
  74. */
  75. static void restore_mrqfn(void)
  76. {
  77. struct super_block *sb = bd->bd_super;
  78. printk(KERN_DEBUG "filter: %s\n", __FUNCTION__);
  79.  
  80. if (orig_mrqfn) {
  81. /* lock filesystem to prevent any further changes */
  82. freeze_super(sb);
  83.  
  84. /* restore the original request function */
  85. bd->bd_disk->queue->make_request_fn = orig_mrqfn;
  86.  
  87. /* unlock filesystem */
  88. thaw_super(sb);
  89. }
  90. orig_mrqfn = NULL;
  91. }
  92.  
  93. static int __init filter_init(void)
  94. {
  95. printk(KERN_DEBUG "filter: %s\n", __FUNCTION__);
  96.  
  97. orig_mrqfn = NULL;
  98.  
  99. // Read block device from path
  100. bd = blkdev_get_by_path(PARTITION_NAME, FMODE_READ, NULL);
  101.  
  102. if (IS_ERR(bd)) {
  103. printk(KERN_ERR "filter: failed to get block device\n");
  104. return 0;
  105. }
  106.  
  107. set_mrqfn();
  108.  
  109. return 0;
  110. }
  111.  
  112. static void __exit filter_exit(void)
  113. {
  114. printk(KERN_DEBUG "filter: %s\n", __FUNCTION__);
  115.  
  116. restore_mrqfn();
  117.  
  118. if (!IS_ERR(bd)) {
  119. blkdev_put(bd, FMODE_READ);
  120. }
  121. }
  122.  
  123. module_init(filter_init);
  124. module_exit(filter_exit);
  125.  
  126. MODULE_LICENSE("GPL");
  127. MODULE_AUTHOR("Me");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement