fops.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <linux/debugfs.h>
  14. #include <linux/errno.h> /* EFAULT */
  15. #include <linux/fs.h> /* file_operations */
  16. #include <linux/kernel.h> /* min */
  17. #include <linux/module.h>
  18. #include <linux/printk.h> /* printk */
  19. #include <linux/uaccess.h> /* copy_from_user, copy_to_user */
  20. #include <uapi/linux/stat.h> /* S_IRUSR */
  21. static struct dentry *debugfs_file;
  22. static char data[] = {'a', 'b', 'c', 'd'};
  23. static int open(struct inode *inode, struct file *filp)
  24. {
  25. pr_info("open\n");
  26. return 0;
  27. }
  28. /* @param[in,out] off: gives the initial position into the buffer.
  29. * We must increment this by the ammount of bytes read.
  30. * Then when userland reads the same file descriptor again,
  31. * we start from that point instead.
  32. * */
  33. static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  34. {
  35. ssize_t ret;
  36. pr_info("read\n");
  37. pr_info("len = %zu\n", len);
  38. pr_info("off = %lld\n", (long long)*off);
  39. if (sizeof(data) <= *off) {
  40. ret = 0;
  41. } else {
  42. ret = min(len, sizeof(data) - (size_t)*off);
  43. if (copy_to_user(buf, data + *off, ret)) {
  44. ret = -EFAULT;
  45. } else {
  46. *off += ret;
  47. }
  48. }
  49. pr_info("buf = %.*s\n", (int)len, buf);
  50. pr_info("ret = %lld\n", (long long)ret);
  51. return ret;
  52. }
  53. /* Similar to read, but with one notable difference:
  54. * we must return ENOSPC if the user tries to write more
  55. * than the size of our buffer. Otherwise, Bash > just
  56. * keeps trying to write to it infinitely. */
  57. static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
  58. {
  59. ssize_t ret;
  60. pr_info("write\n");
  61. pr_info("len = %zu\n", len);
  62. pr_info("off = %lld\n", (long long)*off);
  63. if (sizeof(data) <= *off) {
  64. ret = 0;
  65. } else {
  66. if (sizeof(data) - (size_t)*off < len) {
  67. ret = -ENOSPC;
  68. } else {
  69. if (copy_from_user(data + *off, buf, len)) {
  70. ret = -EFAULT;
  71. } else {
  72. ret = len;
  73. pr_info("buf = %.*s\n", (int)len, data + *off);
  74. *off += ret;
  75. }
  76. }
  77. }
  78. pr_info("ret = %lld\n", (long long)ret);
  79. return ret;
  80. }
  81. /*
  82. Called on the last close:
  83. http://stackoverflow.com/questions/11393674/why-is-the-close-function-is-called-release-in-struct-file-operations-in-the-l
  84. */
  85. static int release(struct inode *inode, struct file *filp)
  86. {
  87. pr_info("release\n");
  88. return 0;
  89. }
  90. static loff_t llseek(struct file *filp, loff_t off, int whence)
  91. {
  92. loff_t newpos;
  93. pr_info("llseek\n");
  94. pr_info("off = %lld\n", (long long)off);
  95. pr_info("whence = %lld\n", (long long)whence);
  96. switch(whence) {
  97. case SEEK_SET:
  98. newpos = off;
  99. break;
  100. case SEEK_CUR:
  101. newpos = filp->f_pos + off;
  102. break;
  103. case SEEK_END:
  104. newpos = sizeof(data) + off;
  105. break;
  106. default:
  107. return -EINVAL;
  108. }
  109. if (newpos < 0) return -EINVAL;
  110. filp->f_pos = newpos;
  111. pr_info("newpos = %lld\n", (long long)newpos);
  112. return newpos;
  113. }
  114. static const struct file_operations fops = {
  115. /* Prevents rmmod while fops are running.
  116. * Try removing this for poll, which waits a lot. */
  117. .owner = THIS_MODULE,
  118. .llseek = llseek,
  119. .open = open,
  120. .read = read,
  121. .release = release,
  122. .write = write,
  123. };
  124. static int myinit(void)
  125. {
  126. debugfs_file = debugfs_create_file("lkmc_fops", S_IRUSR | S_IWUSR, NULL, NULL, &fops);
  127. return 0;
  128. }
  129. static void myexit(void)
  130. {
  131. debugfs_remove_recursive(debugfs_file);
  132. }
  133. module_init(myinit)
  134. module_exit(myexit)
  135. MODULE_LICENSE("GPL");