/core/10.5/fusefs/fuse_file.h

http://macfuse.googlecode.com/ · C Header · 174 lines · 130 code · 28 blank · 16 comment · 24 complexity · 5756de9c66474cba86f6700ee3ff2a48 MD5 · raw file

  1. /*
  2. * Copyright (C) 2006-2008 Google. All Rights Reserved.
  3. * Amit Singh <singh@>
  4. */
  5. #ifndef _FUSE_FILE_H_
  6. #define _FUSE_FILE_H_
  7. #include <sys/fcntl.h>
  8. #include <sys/kauth.h>
  9. #include <sys/stat.h>
  10. #include <sys/mman.h>
  11. #include <sys/mount.h>
  12. #include <sys/types.h>
  13. #include <sys/vnode.h>
  14. typedef enum fufh_type {
  15. FUFH_INVALID = -1,
  16. FUFH_RDONLY = 0,
  17. FUFH_WRONLY = 1,
  18. FUFH_RDWR = 2,
  19. FUFH_MAXTYPE = 3,
  20. } fufh_type_t;
  21. struct fuse_filehandle {
  22. uint64_t fh_id;
  23. int32_t open_count;
  24. int32_t open_flags;
  25. int32_t fuse_open_flags;
  26. int32_t aux_count;
  27. };
  28. typedef struct fuse_filehandle * fuse_filehandle_t;
  29. #define FUFH_IS_VALID(f) ((f)->open_count > 0)
  30. #define FUFH_USE_INC(f) ((f)->open_count++)
  31. #define FUFH_USE_DEC(f) ((f)->open_count--)
  32. #define FUFH_USE_RESET(f) ((f)->open_count = 0)
  33. #define FUFH_AUX_INC(f) ((f)->aux_count++)
  34. static __inline__
  35. fufh_type_t
  36. fuse_filehandle_xlate_from_mmap(int fflags)
  37. {
  38. if (fflags & PROT_WRITE) {
  39. if (fflags & (PROT_READ | PROT_EXEC)) {
  40. return FUFH_RDWR;
  41. } else {
  42. return FUFH_WRONLY;
  43. }
  44. } else if (fflags & (PROT_READ | PROT_EXEC)) {
  45. return FUFH_RDONLY;
  46. } else {
  47. IOLog("MacFUSE: mmap being attempted with no region accessibility\n");
  48. return FUFH_INVALID;
  49. }
  50. }
  51. static __inline__
  52. fufh_type_t
  53. fuse_filehandle_xlate_from_fflags(int fflags)
  54. {
  55. if ((fflags & FREAD) && (fflags & FWRITE)) {
  56. return FUFH_RDWR;
  57. } else if (fflags & (FWRITE)) {
  58. return FUFH_WRONLY;
  59. } else if (fflags & (FREAD)) {
  60. return FUFH_RDONLY;
  61. } else {
  62. /*
  63. * Looks like there might be a code path in Apple's
  64. * IOHDIXController/AppleDiskImagesFileBackingStore
  65. * that calls vnode_open() with a 0 fmode argument.
  66. * Translate 0 to FREAD, which is most likely what
  67. * that kext intends to do anyway. Lets hope the
  68. * calls to VNOP_OPEN and VNOP_CLOSE do match up
  69. * even with this fudging.
  70. */
  71. if (fflags == 0) {
  72. return FUFH_RDONLY;
  73. } else {
  74. panic("MacFUSE: What kind of a flag is this (%x)?", fflags);
  75. }
  76. }
  77. return FUFH_INVALID;
  78. }
  79. static __inline__
  80. int
  81. fuse_filehandle_xlate_to_oflags(fufh_type_t type)
  82. {
  83. int oflags = -1;
  84. switch (type) {
  85. case FUFH_RDONLY:
  86. oflags = O_RDONLY;
  87. break;
  88. case FUFH_WRONLY:
  89. oflags = O_WRONLY;
  90. break;
  91. case FUFH_RDWR:
  92. oflags = O_RDWR;
  93. break;
  94. default:
  95. break;
  96. }
  97. return oflags;
  98. }
  99. /*
  100. * 0 return => can proceed
  101. */
  102. static __inline__
  103. int
  104. fuse_filehandle_preflight_status(vnode_t vp, vnode_t dvp, vfs_context_t context,
  105. fufh_type_t fufh_type)
  106. {
  107. vfs_context_t icontext = context;
  108. kauth_action_t action = 0;
  109. mount_t mp = vnode_mount(vp);
  110. int err = 0;
  111. if (vfs_authopaque(mp) || !vfs_issynchronous(mp) || !vnode_isreg(vp)) {
  112. goto out;
  113. }
  114. #if M_MACFUSE_ENABLE_UNSUPPORTED
  115. if (!icontext) {
  116. icontext = vfs_context_current();
  117. }
  118. #endif /* M_MACFUSE_ENABLE_UNSUPPORTED */
  119. if (!icontext) {
  120. goto out;
  121. }
  122. switch (fufh_type) {
  123. case FUFH_RDONLY:
  124. action |= KAUTH_VNODE_READ_DATA;
  125. break;
  126. case FUFH_WRONLY:
  127. action |= KAUTH_VNODE_WRITE_DATA;
  128. break;
  129. case FUFH_RDWR:
  130. action |= (KAUTH_VNODE_READ_DATA | KAUTH_VNODE_WRITE_DATA);
  131. break;
  132. default:
  133. err = EINVAL;
  134. break;
  135. }
  136. if (!err) {
  137. err = vnode_authorize(vp, dvp, action, icontext);
  138. }
  139. out:
  140. return err;
  141. }
  142. int fuse_filehandle_get(vnode_t vp, vfs_context_t context,
  143. fufh_type_t fufh_type, int mode);
  144. int fuse_filehandle_put(vnode_t vp, vfs_context_t context,
  145. fufh_type_t fufh_type, fuse_op_waitfor_t waitfor);
  146. #endif /* _FUSE_FILE_H_ */