/filesystems/unixfs/common/unixfs/unixfs_internal.h

http://macfuse.googlecode.com/ · C Header · 234 lines · 174 code · 35 blank · 25 comment · 14 complexity · 6a9e22cd48bc15c9c4d5665bf2f022c2 MD5 · raw file

  1. /*
  2. * UnixFS
  3. *
  4. * A general-purpose file system layer for writing/reimplementing/porting
  5. * Unix file systems through MacFUSE.
  6. * Copyright (c) 2008 Amit Singh. All Rights Reserved.
  7. * http://osxbook.com
  8. */
  9. #ifndef _UNIXFS_INTERNAL_H_
  10. #define _UNIXFS_INTERNAL_H_
  11. #include "unixfs.h"
  12. #include <libgen.h>
  13. #if __linux__
  14. #include <darwin/queue.h>
  15. #else
  16. #include <sys/queue.h>
  17. #endif
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <pthread.h>
  21. #if __APPLE__
  22. #define ino64_t ino_t
  23. #include <libkern/OSByteOrder.h>
  24. #elif __linux__
  25. #define ino64_t ino_t
  26. extern ssize_t pread(int fd, void *buf, size_t count, off_t offset);
  27. #include <endian.h>
  28. #include <asm/byteorder.h>
  29. #define OSSwapLittleToHostInt64(x) __le64_to_cpu(x)
  30. #define OSSwapBigToHostInt64(x) __be64_to_cpu(x)
  31. #define OSSwapHostToLittleInt64(x) __cpu_to_le64(x)
  32. #define OSSwapHostToBigInt64(x) __cpu_to_be64(x)
  33. #define OSSwapLittleToHostInt32(x) __le32_to_cpu(x)
  34. #define OSSwapBigToHostInt32(x) __be32_to_cpu(x)
  35. #define OSSwapHostToLittleInt32(x) __cpu_to_le32(x)
  36. #define OSSwapHostToBigInt32(x) __cpu_to_be32(x)
  37. #define OSSwapLittleToHostInt16(x) __le16_to_cpu(x)
  38. #define OSSwapBigToHostInt16(x) __be16_to_cpu(x)
  39. #define OSSwapHostToLittleInt16(x) __cpu_to_le16(x)
  40. #define OSSwapHostToBigInt16(x) __cpu_to_be16(x)
  41. #if __BYTE_ORDER == __BIG_ENDIAN
  42. #define __BIG_ENDIAN__ 1
  43. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  44. #define __LITTLE_ENDIAN__ 1
  45. #else
  46. #error Endian Problem
  47. #endif
  48. #elif __FreeBSD__
  49. #define ino64_t uint64_t
  50. #include <sys/endian.h>
  51. #if BYTE_ORDER == LITTLE_ENDIAN
  52. #define __LITTLE_ENDIAN__ 1
  53. #elif BYTE_ORDER == BIG_ENDIAN
  54. #define __BIG_ENDIAN__ 1
  55. #else
  56. #error Endian Problem
  57. #endif
  58. #define OSSwapLittleToHostInt64(x) le64toh(x)
  59. #define OSSwapBigToHostInt64(x) be64toh(x)
  60. #define OSSwapLittleToHostInt32(x) le32toh(x)
  61. #define OSSwapBigToHostInt32(x) be32toh(x)
  62. #define OSSwapLittleToHostInt16(x) le16toh(x)
  63. #define OSSwapBigToHostInt16(x) be16toh(x)
  64. #endif
  65. #define UNIXFS_ENABLE_INODEHASH 1 /* 1 => enable; 0 => disable */
  66. #define UNIXFS_BADBLOCK(blk, err) ((err != 0))
  67. #define UNIXFS_IOSIZE(unixfs) (uint32_t)(unixfs->s_statvfs.f_bsize)
  68. #define UNIXFS_NADDR_MAX 13
  69. struct super_block {
  70. u_long s_magic;
  71. u_long s_flags;
  72. u_long s_blocksize;
  73. uint8_t s_blocksize_bits;
  74. uint8_t s_dirt;
  75. fs_endian_t s_endian;
  76. void* s_fs_info;
  77. int s_bdev; /* block device fd */
  78. int s_kdev; /* kernel IPC descriptor */
  79. uint32_t s_dentsize;
  80. char s_fsname[UNIXFS_MNAMELEN];
  81. char s_volname[UNIXFS_MAXNAMLEN];
  82. struct statvfs s_statvfs;
  83. };
  84. #define s_id s_fsname
  85. #define MACFUSE_ROOTINO 1
  86. /*
  87. * The I node is the focus of all
  88. * file activity in unix. There is a unique
  89. * inode allocated for each active file,
  90. * each current directory, each mounted-on
  91. * file, text file, and the root. An inode is 'named'
  92. * by its dev/inumber pair. (iget/iget.c)
  93. * Data, from mode on, is read in
  94. * from permanent inode on volume.
  95. *
  96. * This is a modern version of the in-core inode.
  97. * We use this for all ancient file systems we support.
  98. * We don't need a 'dev' though.
  99. */
  100. typedef struct inode {
  101. LIST_ENTRY(inode) I_hashlink;
  102. pthread_cond_t I_state_cond;
  103. uint32_t I_initialized;
  104. uint32_t I_attachoutstanding;
  105. uint32_t I_waiting;
  106. uint32_t I_count;
  107. uint32_t I_blkbits;
  108. struct super_block* I_sb;
  109. struct stat I_stat;
  110. union {
  111. uint32_t I_daddr[UNIXFS_NADDR_MAX];
  112. uint8_t I_addr[UNIXFS_NADDR_MAX];
  113. } I_addr_un;
  114. void* I_private;
  115. } inode;
  116. #define I_mode I_stat.st_mode
  117. #define I_nlink I_stat.st_nlink
  118. #define I_number I_stat.st_ino
  119. #define I_ino I_stat.st_ino
  120. #define I_uid I_stat.st_uid
  121. #define I_gid I_stat.st_gid
  122. #define I_rdev I_stat.st_rdev
  123. #define I_atime I_stat.st_atimespec
  124. #define I_mtime I_stat.st_mtimespec
  125. #define I_ctime I_stat.st_ctimespec
  126. #define I_crtime I_stat.st_birthtimespec
  127. #if __linux__
  128. #define I_atime_sec I_stat.st_atime
  129. #define I_mtime_sec I_stat.st_mtime
  130. #define I_ctime_sec I_stat.st_ctime
  131. #else
  132. #define I_atime_sec I_stat.st_atimespec.tv_sec
  133. #define I_mtime_sec I_stat.st_mtimespec.tv_sec
  134. #define I_ctime_sec I_stat.st_ctimespec.tv_sec
  135. #define I_crtime_sec I_stat.st_birthtimespec.tv_sec
  136. #endif
  137. #define I_size I_stat.st_size
  138. #define I_blocks I_stat.st_blocks
  139. #define I_blksize I_stat.st_blksize
  140. #define I_flags I_stat.st_flags
  141. #define I_gen I_stat.st_gen
  142. #define I_generation I_stat.st_gen
  143. #define I_version I_stat.st_gen
  144. #define I_addr I_addr_un.I_addr
  145. #define I_daddr I_addr_un.I_daddr
  146. #define i_ino I_stat.st_ino /* special case */
  147. /* Inode layer interface. */
  148. typedef int (*unixfs_inodelayer_iterator_t)(struct inode*, void*);
  149. int unixfs_inodelayer_init(size_t privsize);
  150. void unixfs_inodelayer_fini(void);
  151. struct inode* unixfs_inodelayer_iget(ino_t ino);
  152. void unixfs_inodelayer_iput(struct inode* ip);
  153. void unixfs_inodelayer_isucceeded(struct inode* ip);
  154. void unixfs_inodelayer_ifailed(struct inode* ip);
  155. void unixfs_inodelayer_dump(unixfs_inodelayer_iterator_t);
  156. /* Byte Swappers */
  157. #define cpu_to_le32(x) OSSwapHostToLittleInt32(x)
  158. #define cpu_to_be32(x) OSSwapHostToBigInt32(x)
  159. static inline uint32_t
  160. pdp11_to_host(uint32_t x)
  161. {
  162. #ifdef __LITTLE_ENDIAN__
  163. return ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16);
  164. #else
  165. #ifdef __BIG_ENDIAN__
  166. return ((x & 0xff00ff) << 8) | ((x & 0xff00ff00) >> 8);
  167. #else
  168. #error Endian Problem
  169. #endif
  170. #endif
  171. }
  172. static inline uint64_t
  173. fs64_to_host(fs_endian_t e, uint64_t x)
  174. {
  175. if (e == UNIXFS_FS_BIG)
  176. return OSSwapLittleToHostInt64(x);
  177. else
  178. return OSSwapBigToHostInt64(x);
  179. }
  180. static inline uint32_t
  181. fs32_to_host(fs_endian_t e, uint32_t x)
  182. {
  183. if (e == UNIXFS_FS_PDP)
  184. return pdp11_to_host(x);
  185. else if (e == UNIXFS_FS_LITTLE)
  186. return OSSwapLittleToHostInt32(x);
  187. else
  188. return OSSwapBigToHostInt32(x);
  189. }
  190. static inline uint16_t
  191. fs16_to_host(fs_endian_t e, uint16_t x)
  192. {
  193. if (e != UNIXFS_FS_BIG)
  194. return OSSwapLittleToHostInt16(x);
  195. else
  196. return OSSwapBigToHostInt16(x);
  197. }
  198. #endif /* _UNIXFS_INTERNAL_H_ */