kfile.c 2.5 KB

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