kfile.c 2.0 KB

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