kfile.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <linux/kernel.h> // printk()
  2. #include <linux/syscalls.h>
  3. #include "kfile.h"
  4. /////////////////////////////////////////////////////////////////////////////
  5. static long _vfs_ioctl(struct file *pf, unsigned int cmd, unsigned long arg)
  6. {
  7. int ret = -ENOTTY;
  8. if(pf->f_op->unlocked_ioctl)
  9. {
  10. ret = pf->f_op->unlocked_ioctl(pf, cmd, arg);
  11. if(ret == -ENOIOCTLCMD)
  12. ret = -ENOTTY;
  13. }
  14. return ret;
  15. }
  16. /////////////////////////////////////////////////////////////////////////////
  17. struct file* kf_open(const char *path, int flags, int rights)
  18. {
  19. struct file *pf = NULL;
  20. mm_segment_t oldfs;
  21. int err = 0;
  22. oldfs = get_fs();
  23. set_fs(get_ds());
  24. pf = filp_open(path, flags, rights);
  25. set_fs(oldfs);
  26. if(IS_ERR(pf))
  27. {
  28. err = PTR_ERR(pf);
  29. set_fs(oldfs);
  30. return NULL;
  31. }
  32. return pf;
  33. }
  34. /////////////////////////////////////////////////////////////////////////////
  35. void kf_close(struct file *pf)
  36. {
  37. if(pf)
  38. filp_close(pf, NULL);
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. int kf_read(struct file *pf, unsigned long long offset, unsigned char *data, unsigned int size)
  42. {
  43. int ret;
  44. mm_segment_t oldfs;
  45. oldfs = get_fs();
  46. set_fs(get_ds());
  47. ret = vfs_read(pf, data, size, &offset);
  48. set_fs(oldfs);
  49. return ret;
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. int kf_write(struct file *pf, unsigned long long offset, unsigned char *data, unsigned int size)
  53. {
  54. int ret;
  55. mm_segment_t oldfs;
  56. oldfs = get_fs();
  57. set_fs(get_ds());
  58. ret = vfs_write(pf, data, size, &offset);
  59. set_fs(oldfs);
  60. return ret;
  61. }
  62. /////////////////////////////////////////////////////////////////////////////
  63. long kf_ioctl(struct file *pf, unsigned int cmd, unsigned long arg)
  64. {
  65. long ret;
  66. mm_segment_t oldfs;
  67. oldfs = get_fs();
  68. set_fs(get_ds());
  69. ret = _vfs_ioctl(pf, cmd, arg);
  70. set_fs(oldfs);
  71. return ret;
  72. }
  73. /////////////////////////////////////////////////////////////////////////////
  74. int kf_sync(struct file *pf)
  75. {
  76. vfs_fsync(pf, 0);
  77. return 0;
  78. }