fops.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Basic fops example, with a fixed size static data buffer.
  3. Usage:
  4. /fops.sh
  5. The buffer can be written and read from. If data overflows, data is thrown away.
  6. No, there ain't no official docs:
  7. http://stackoverflow.com/questions/15213932/what-are-the-struct-file-operations-arguments
  8. fops define what the kernel will do on filesystem system calls on all of
  9. /dev, /proc, /sys, and consistute the main method of userland communication
  10. in drivers (syscalls being the other one).
  11. Here we use debugfs.
  12. */
  13. #include <asm/uaccess.h> /* copy_from_user, copy_to_user */
  14. #include <linux/debugfs.h>
  15. #include <linux/errno.h> /* EFAULT */
  16. #include <linux/fs.h>
  17. #include <linux/kernel.h> /* min */
  18. #include <linux/module.h>
  19. #include <linux/printk.h> /* printk */
  20. #include <uapi/linux/stat.h> /* S_IRUSR */
  21. MODULE_LICENSE("GPL");
  22. static struct dentry *dir;
  23. static char data[] = {'a', 'b', 'c', 'd'};
  24. static int open(struct inode *inode, struct file *filp)
  25. {
  26. printk(KERN_INFO "open\n");
  27. return 0;
  28. }
  29. /* @param[in,out] off: gives the initial position into the buffer.
  30. * We must increment this by the ammount of bytes read.
  31. * Then when userland reads the same file descriptor again,
  32. * we start from that point instead.
  33. * */
  34. static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  35. {
  36. ssize_t ret;
  37. printk(KERN_INFO "read\n");
  38. printk(KERN_INFO "len = %zu\n", len);
  39. printk(KERN_INFO "off = %lld\n", (long long)*off);
  40. if (sizeof(data) <= *off) {
  41. ret = 0;
  42. } else {
  43. ret = min(len, sizeof(data) - (size_t)*off);
  44. if (copy_to_user(buf, data + *off, ret)) {
  45. ret = -EFAULT;
  46. } else {
  47. *off += ret;
  48. }
  49. }
  50. printk(KERN_INFO "buf = %.*s\n", (int)len, buf);
  51. printk(KERN_INFO "ret = %lld\n", (long long)ret);
  52. return ret;
  53. }
  54. /* Similar to read, but with one notable difference:
  55. * we must return ENOSPC if the user tries to write more
  56. * than the size of our buffer. Otherwise, Bash > just
  57. * keeps trying to write to it infinitely. */
  58. static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
  59. {
  60. ssize_t ret;
  61. printk(KERN_INFO "write\n");
  62. printk(KERN_INFO "len = %zu\n", len);
  63. printk(KERN_INFO "off = %lld\n", (long long)*off);
  64. if (sizeof(data) <= *off) {
  65. ret = 0;
  66. } else {
  67. if (sizeof(data) - (size_t)*off < len) {
  68. ret = -ENOSPC;
  69. } else {
  70. if (copy_from_user(data + *off, buf, len)) {
  71. ret = -EFAULT;
  72. } else {
  73. ret = len;
  74. printk(KERN_INFO "buf = %.*s\n", (int)len, data + *off);
  75. *off += ret;
  76. }
  77. }
  78. }
  79. printk(KERN_INFO "ret = %lld\n", (long long)ret);
  80. return ret;
  81. }
  82. /*
  83. Called on the last close:
  84. http://stackoverflow.com/questions/11393674/why-is-the-close-function-is-called-release-in-struct-file-operations-in-the-l
  85. */
  86. static int release(struct inode *inode, struct file *filp)
  87. {
  88. printk(KERN_INFO "release\n");
  89. return 0;
  90. }
  91. static loff_t llseek(struct file *filp, loff_t off, int whence)
  92. {
  93. loff_t newpos;
  94. printk(KERN_INFO "llseek\n");
  95. printk(KERN_INFO "off = %lld\n", (long long)off);
  96. printk(KERN_INFO "whence = %lld\n", (long long)whence);
  97. switch(whence) {
  98. case SEEK_SET:
  99. newpos = off;
  100. break;
  101. case SEEK_CUR:
  102. newpos = filp->f_pos + off;
  103. break;
  104. case SEEK_END:
  105. newpos = sizeof(data) + off;
  106. break;
  107. default:
  108. return -EINVAL;
  109. }
  110. if (newpos < 0) return -EINVAL;
  111. filp->f_pos = newpos;
  112. printk(KERN_INFO "newpos = %lld\n", (long long)newpos);
  113. return newpos;
  114. }
  115. static const struct file_operations fops = {
  116. .llseek = llseek,
  117. .open = open,
  118. .read = read,
  119. .release = release,
  120. .write = write,
  121. };
  122. static int myinit(void)
  123. {
  124. dir = debugfs_create_dir("lkmc_fops", 0);
  125. debugfs_create_file("f", S_IRUSR | S_IWUSR, dir, NULL, &fops);
  126. return 0;
  127. }
  128. static void myexit(void)
  129. {
  130. debugfs_remove_recursive(dir);
  131. }
  132. module_init(myinit)
  133. module_exit(myexit)