e2lib.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #ifndef EXT2_LIB_H
  2. #define EXT2_LIB_H
  3. #include <fcntl.h>
  4. /* Definitions cribbed from ext2_fs.h, modified so's to be 64-bit clean
  5. * when cross-compiling on Alpha
  6. */
  7. /*
  8. * The second extended filesystem constants/structures
  9. */
  10. /*
  11. * Define EXT2FS_DEBUG to produce debug messages
  12. */
  13. #undef EXT2FS_DEBUG
  14. /*
  15. * Define EXT2FS_DEBUG_CACHE to produce cache debug messages
  16. */
  17. #undef EXT2FS_DEBUG_CACHE
  18. /*
  19. * Define EXT2FS_CHECK_CACHE to add some checks to the name cache code
  20. */
  21. #undef EXT2FS_CHECK_CACHE
  22. /*
  23. * Define EXT2FS_PRE_02B_COMPAT to convert ext 2 fs prior to 0.2b
  24. */
  25. #undef EXT2FS_PRE_02B_COMPAT
  26. /*
  27. * Define DONT_USE_DCACHE to inhibit the directory cache
  28. */
  29. #define DONT_USE_DCACHE
  30. /*
  31. * Define EXT2_PREALLOCATE to preallocate data blocks for expanding files
  32. */
  33. #define EXT2_PREALLOCATE
  34. /*
  35. * The second extended file system version
  36. */
  37. #define EXT2FS_DATE "94/03/10"
  38. #define EXT2FS_VERSION "0.5"
  39. /*
  40. * Special inodes numbers
  41. */
  42. #define EXT2_BAD_INO 1 /* Bad blocks inode */
  43. #define EXT2_ROOT_INO 2 /* Root inode */
  44. #define EXT2_ACL_IDX_INO 3 /* ACL inode */
  45. #define EXT2_ACL_DATA_INO 4 /* ACL inode */
  46. #define EXT2_BOOT_LOADER_INO 5 /* Boot loader inode */
  47. #define EXT2_UNDEL_DIR_INO 6 /* Undelete directory inode */
  48. #define EXT2_FIRST_INO 11 /* First non reserved inode */
  49. /*
  50. * The second extended file system magic number
  51. */
  52. #define EXT2_PRE_02B_MAGIC 0xEF51
  53. #define EXT2_SUPER_MAGIC 0xEF53
  54. #define EXT2_SUPER_BIGMAGIC 0x53EF /* Accessing on big-endian system... */
  55. /*
  56. * Maximal count of links to a file
  57. */
  58. #define EXT2_LINK_MAX 32000
  59. /*
  60. * Macro-instructions used to manage several block sizes
  61. */
  62. #define EXT2_MIN_BLOCK_SIZE 1024
  63. #define EXT2_MAX_BLOCK_SIZE 4096
  64. #define EXT2_MIN_BLOCK_LOG_SIZE 10
  65. # define EXT2_BLOCK_SIZE(s) (EXT2_MIN_BLOCK_SIZE << (s)->s_log_block_size)
  66. #define EXT2_ACLE_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_acl_entry))
  67. #define EXT2_ADDR_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (unsigned int))
  68. # define EXT2_BLOCK_SIZE_BITS(s) ((s)->s_log_block_size + 10)
  69. #define EXT2_INODES_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_inode))
  70. /*
  71. * Macro-instructions used to manage fragments
  72. */
  73. #define EXT2_MIN_FRAG_SIZE 1024
  74. #define EXT2_MAX_FRAG_SIZE 4096
  75. #define EXT2_MIN_FRAG_LOG_SIZE 10
  76. # define EXT2_FRAG_SIZE(s) (EXT2_MIN_FRAG_SIZE << (s)->s_log_frag_size)
  77. # define EXT2_FRAGS_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / EXT2_FRAG_SIZE(s))
  78. /*
  79. * ACL structures
  80. */
  81. struct ext2_acl_header /* Header of Access Control Lists */
  82. {
  83. unsigned int aclh_size;
  84. unsigned int aclh_file_count;
  85. unsigned int aclh_acle_count;
  86. unsigned int aclh_first_acle;
  87. };
  88. struct ext2_acl_entry /* Access Control List Entry */
  89. {
  90. unsigned int acle_size;
  91. unsigned short acle_perms; /* Access permissions */
  92. unsigned short acle_type; /* Type of entry */
  93. unsigned short acle_tag; /* User or group identity */
  94. unsigned short acle_pad1;
  95. unsigned int acle_next; /* Pointer on next entry for the */
  96. /* same inode or on next free entry */
  97. };
  98. /*
  99. * Structure of a blocks group descriptor
  100. */
  101. struct ext2_old_group_desc
  102. {
  103. unsigned int bg_block_bitmap; /* Blocks bitmap block */
  104. unsigned int bg_inode_bitmap; /* Inodes bitmap block */
  105. unsigned int bg_inode_table; /* Inodes table block */
  106. unsigned short bg_free_blocks_count; /* Free blocks count */
  107. unsigned short bg_free_inodes_count; /* Free inodes count */
  108. };
  109. struct ext2_group_desc
  110. {
  111. unsigned int bg_block_bitmap; /* Blocks bitmap block */
  112. unsigned int bg_inode_bitmap; /* Inodes bitmap block */
  113. unsigned int bg_inode_table; /* Inodes table block */
  114. unsigned short bg_free_blocks_count; /* Free blocks count */
  115. unsigned short bg_free_inodes_count; /* Free inodes count */
  116. unsigned short bg_used_dirs_count; /* Directories count */
  117. unsigned short bg_pad;
  118. unsigned int bg_reserved[3];
  119. };
  120. /*
  121. * Macro-instructions used to manage group descriptors
  122. */
  123. # define EXT2_BLOCKS_PER_GROUP(s) ((s)->s_blocks_per_group)
  124. # define EXT2_DESC_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_group_desc))
  125. # define EXT2_INODES_PER_GROUP(s) ((s)->s_inodes_per_group)
  126. /*
  127. * Constants relative to the data blocks
  128. */
  129. #define EXT2_NDIR_BLOCKS 12
  130. #define EXT2_IND_BLOCK EXT2_NDIR_BLOCKS
  131. #define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1)
  132. #define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1)
  133. #define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1)
  134. /*
  135. * Inode flags
  136. */
  137. #define EXT2_SECRM_FL 0x0001 /* Secure deletion */
  138. #define EXT2_UNRM_FL 0x0002 /* Undelete */
  139. #define EXT2_COMPR_FL 0x0004 /* Compress file */
  140. #define EXT2_SYNC_FL 0x0008 /* Synchronous updates */
  141. /*
  142. * ioctl commands
  143. */
  144. #define EXT2_IOC_GETFLAGS _IOR('f', 1, int)
  145. #define EXT2_IOC_SETFLAGS _IOW('f', 2, int)
  146. #define EXT2_IOC_GETVERSION _IOR('v', 1, int)
  147. #define EXT2_IOC_SETVERSION _IOW('v', 2, int)
  148. /*
  149. * Structure of an inode on the disk
  150. */
  151. struct ext2_inode {
  152. unsigned short i_mode; /* File mode */
  153. unsigned short i_uid; /* Owner Uid */
  154. unsigned int i_size; /* Size in bytes */
  155. unsigned int i_atime; /* Access time */
  156. unsigned int i_ctime; /* Creation time */
  157. unsigned int i_mtime; /* Modification time */
  158. unsigned int i_dtime; /* Deletion Time */
  159. unsigned short i_gid; /* Group Id */
  160. unsigned short i_links_count; /* Links count */
  161. unsigned int i_blocks; /* Blocks count */
  162. unsigned int i_flags; /* File flags */
  163. unsigned int i_reserved1;
  164. unsigned int i_block[EXT2_N_BLOCKS];/* Pointers to blocks */
  165. unsigned int i_version; /* File version (for NFS) */
  166. unsigned int i_file_acl; /* File ACL */
  167. unsigned int i_dir_acl; /* Directory ACL */
  168. unsigned int i_faddr; /* Fragment address */
  169. unsigned char i_frag; /* Fragment number */
  170. unsigned char i_fsize; /* Fragment size */
  171. unsigned short i_pad1;
  172. unsigned int i_reserved2[2];
  173. };
  174. /*
  175. * File system states
  176. */
  177. #define EXT2_VALID_FS 0x0001 /* Unmounted cleany */
  178. #define EXT2_ERROR_FS 0x0002 /* Errors detected */
  179. /*
  180. * Mount flags
  181. */
  182. #define EXT2_MOUNT_CHECK_NORMAL 0x0001 /* Do some more checks */
  183. #define EXT2_MOUNT_CHECK_STRICT 0x0002 /* Do again more checks */
  184. #define EXT2_MOUNT_CHECK (EXT2_MOUNT_CHECK_NORMAL | \
  185. EXT2_MOUNT_CHECK_STRICT)
  186. #define EXT2_MOUNT_GRPID 0x0004 /* Create files with directory's group */
  187. #define EXT2_MOUNT_DEBUG 0x0008 /* Some debugging messages */
  188. #define EXT2_MOUNT_ERRORS_CONT 0x0010 /* Continue on errors */
  189. #define EXT2_MOUNT_ERRORS_RO 0x0020 /* Remount fs ro on errors */
  190. #define EXT2_MOUNT_ERRORS_PANIC 0x0040 /* Panic on errors */
  191. #define clear_opt(o, opt) o &= ~EXT2_MOUNT_##opt
  192. #define set_opt(o, opt) o |= EXT2_MOUNT_##opt
  193. #define test_opt(sb, opt) ((sb)->u.ext2_sb.s_mount_opt & \
  194. EXT2_MOUNT_##opt)
  195. /*
  196. * Maximal mount counts between two filesystem checks
  197. */
  198. #define EXT2_DFL_MAX_MNT_COUNT 20 /* Allow 20 mounts */
  199. #define EXT2_DFL_CHECKINTERVAL 0 /* Don't use interval check */
  200. /*
  201. * Behaviour when detecting errors
  202. */
  203. #define EXT2_ERRORS_CONTINUE 1 /* Continue execution */
  204. #define EXT2_ERRORS_RO 2 /* Remount fs read-only */
  205. #define EXT2_ERRORS_PANIC 3 /* Panic */
  206. #define EXT2_ERRORS_DEFAULT EXT2_ERRORS_CONTINUE
  207. /*
  208. * Structure of the super block
  209. */
  210. struct ext2_super_block {
  211. unsigned int s_inodes_count; /* 0: Inodes count */
  212. unsigned int s_blocks_count; /* 4: Blocks count */
  213. unsigned int s_r_blocks_count;/* 8: Reserved blocks count */
  214. unsigned int s_free_blocks_count;/* 12: Free blocks count */
  215. unsigned int s_free_inodes_count;/* 16: Free inodes count */
  216. unsigned int s_first_data_block;/* 20: First Data Block */
  217. unsigned int s_log_block_size;/* 24: Block size */
  218. int s_log_frag_size; /* 28: Fragment size */
  219. unsigned int s_blocks_per_group;/* 32: # Blocks per group */
  220. unsigned int s_frags_per_group;/* 36: # Fragments per group */
  221. unsigned int s_inodes_per_group;/* 40: # Inodes per group */
  222. unsigned int s_mtime; /* 44: Mount time */
  223. unsigned int s_wtime; /* 48: Write time */
  224. unsigned short s_mnt_count; /* 52: Mount count */
  225. short s_max_mnt_count; /* 54: Maximal mount count */
  226. unsigned short s_magic; /* 56: Magic signature */
  227. unsigned short s_state; /* 58: File system state */
  228. unsigned short s_errors; /* 60: Behaviour when detecting errors */
  229. unsigned short s_pad; /* 62: */
  230. unsigned int s_lastcheck; /* 64: time of last check */
  231. unsigned int s_checkinterval; /* 68: max. time between checks */
  232. unsigned int s_reserved[238]; /* 72: Padding to the end of the block */
  233. };
  234. /*
  235. * Structure of a directory entry
  236. */
  237. #define EXT2_NAME_LEN 255
  238. struct ext2_dir_entry {
  239. unsigned int inode; /* Inode number */
  240. unsigned short rec_len; /* Directory entry length */
  241. unsigned short name_len; /* Name length */
  242. char name[EXT2_NAME_LEN]; /* File name */
  243. };
  244. /*
  245. * EXT2_DIR_PAD defines the directory entries boundaries
  246. *
  247. * NOTE: It must be a multiple of 4
  248. */
  249. #define EXT2_DIR_PAD 4
  250. #define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1)
  251. #define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \
  252. ~EXT2_DIR_ROUND)
  253. /* These definitions are cribbed from other file system include files, so that
  254. * we can take a stab at identifying non-ext2 file systems as well...
  255. */
  256. /*
  257. * minix super-block data on disk
  258. */
  259. struct minix_super_block {
  260. unsigned short s_ninodes;
  261. unsigned short s_nzones;
  262. unsigned short s_imap_blocks;
  263. unsigned short s_zmap_blocks;
  264. unsigned short s_firstdatazone;
  265. unsigned short s_log_zone_size;
  266. unsigned int s_max_size;
  267. unsigned short s_magic;
  268. unsigned short s_state;
  269. };
  270. #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
  271. #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
  272. #define NEW_MINIX_SUPER_MAGIC 0x2468 /* minix V2 - not implemented */
  273. extern int ext2_init(char * name, int access);
  274. extern void ext2_close();
  275. extern struct ext2_inode * ext2_iget(int ino);
  276. extern void ext2_iput(struct ext2_inode *ip);
  277. extern int ext2_balloc(void);
  278. extern int ext2_ialloc(void);
  279. extern int ext2_blocksize(void);
  280. extern int ext2_blkno(struct ext2_inode *ip, int blkoff,
  281. int allocate);
  282. extern void ext2_bread(struct ext2_inode *ip, int blkno,
  283. char * buffer);
  284. extern void ext2_bwrite(struct ext2_inode *ip, int blkno,
  285. char * buffer);
  286. extern struct ext2_inode * ext2_namei(char * name);
  287. extern void ext2_truncate(struct ext2_inode *ip);
  288. extern void ext2_mknod(struct ext2_inode *dip,
  289. char * name, int ino);
  290. extern int ext2_fill_contiguous(struct ext2_inode * ip,
  291. int nblocks);
  292. extern void ext2_write_bootblock(char *bb);
  293. #endif /* EXT2_LIB_H */