reiserfs_private.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright 2000-2002 by Hans Reiser, licensing governed by reiserfs/README
  4. *
  5. * GRUB -- GRand Unified Bootloader
  6. * Copyright (C) 2000, 2001 Free Software Foundation, Inc.
  7. *
  8. * (C) Copyright 2003 - 2004
  9. * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
  10. *
  11. */
  12. /* An implementation for the ReiserFS filesystem ported from GRUB.
  13. * Some parts of this code (mainly the structures and defines) are
  14. * from the original reiser fs code, as found in the linux kernel.
  15. */
  16. #include <compiler.h>
  17. #ifndef __BYTE_ORDER
  18. #if defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
  19. #define __BYTE_ORDER __LITTLE_ENDIAN
  20. #elif defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
  21. #define __BYTE_ORDER __BIG_ENDIAN
  22. #else
  23. #error "unable to define __BYTE_ORDER"
  24. #endif
  25. #endif /* not __BYTE_ORDER */
  26. #define FSYS_BUFLEN 0x8000
  27. #define FSYS_BUF fsys_buf
  28. /* This is the new super block of a journaling reiserfs system */
  29. struct reiserfs_super_block
  30. {
  31. __u32 s_block_count; /* blocks count */
  32. __u32 s_free_blocks; /* free blocks count */
  33. __u32 s_root_block; /* root block number */
  34. __u32 s_journal_block; /* journal block number */
  35. __u32 s_journal_dev; /* journal device number */
  36. __u32 s_journal_size; /* size of the journal on FS creation. used to make sure they don't overflow it */
  37. __u32 s_journal_trans_max; /* max number of blocks in a transaction. */
  38. __u32 s_journal_magic; /* random value made on fs creation */
  39. __u32 s_journal_max_batch; /* max number of blocks to batch into a trans */
  40. __u32 s_journal_max_commit_age; /* in seconds, how old can an async commit be */
  41. __u32 s_journal_max_trans_age; /* in seconds, how old can a transaction be */
  42. __u16 s_blocksize; /* block size */
  43. __u16 s_oid_maxsize; /* max size of object id array */
  44. __u16 s_oid_cursize; /* current size of object id array */
  45. __u16 s_state; /* valid or error */
  46. char s_magic[16]; /* reiserfs magic string indicates that file system is reiserfs */
  47. __u16 s_tree_height; /* height of disk tree */
  48. __u16 s_bmap_nr; /* amount of bitmap blocks needed to address each block of file system */
  49. __u16 s_version;
  50. char s_unused[128]; /* zero filled by mkreiserfs */
  51. };
  52. #define sb_root_block(sbp) (__le32_to_cpu((sbp)->s_root_block))
  53. #define sb_journal_block(sbp) (__le32_to_cpu((sbp)->s_journal_block))
  54. #define set_sb_journal_block(sbp,v) ((sbp)->s_journal_block = __cpu_to_le32(v))
  55. #define sb_journal_size(sbp) (__le32_to_cpu((sbp)->s_journal_size))
  56. #define sb_blocksize(sbp) (__le16_to_cpu((sbp)->s_blocksize))
  57. #define set_sb_blocksize(sbp,v) ((sbp)->s_blocksize = __cpu_to_le16(v))
  58. #define sb_version(sbp) (__le16_to_cpu((sbp)->s_version))
  59. #define set_sb_version(sbp,v) ((sbp)->s_version = __cpu_to_le16(v))
  60. #define REISERFS_MAX_SUPPORTED_VERSION 2
  61. #define REISERFS_SUPER_MAGIC_STRING "ReIsErFs"
  62. #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
  63. #define REISER3FS_SUPER_MAGIC_STRING "ReIsEr3Fs"
  64. #define MAX_HEIGHT 7
  65. /* must be correct to keep the desc and commit structs at 4k */
  66. #define JOURNAL_TRANS_HALF 1018
  67. /* first block written in a commit. */
  68. struct reiserfs_journal_desc {
  69. __u32 j_trans_id; /* id of commit */
  70. __u32 j_len; /* length of commit. len +1 is the commit block */
  71. __u32 j_mount_id; /* mount id of this trans*/
  72. __u32 j_realblock[JOURNAL_TRANS_HALF]; /* real locations for the first blocks */
  73. char j_magic[12];
  74. };
  75. /* last block written in a commit */
  76. struct reiserfs_journal_commit {
  77. __u32 j_trans_id; /* must match j_trans_id from the desc block */
  78. __u32 j_len; /* ditto */
  79. __u32 j_realblock[JOURNAL_TRANS_HALF]; /* real locations for the last blocks */
  80. char j_digest[16]; /* md5 sum of all the blocks involved, including desc and commit. not used, kill it */
  81. };
  82. /* this header block gets written whenever a transaction is considered
  83. fully flushed, and is more recent than the last fully flushed
  84. transaction.
  85. fully flushed means all the log blocks and all the real blocks are
  86. on disk, and this transaction does not need to be replayed.
  87. */
  88. struct reiserfs_journal_header {
  89. /* id of last fully flushed transaction */
  90. __u32 j_last_flush_trans_id;
  91. /* offset in the log of where to start replay after a crash */
  92. __u32 j_first_unflushed_offset;
  93. /* mount id to detect very old transactions */
  94. __u32 j_mount_id;
  95. };
  96. /* magic string to find desc blocks in the journal */
  97. #define JOURNAL_DESC_MAGIC "ReIsErLB"
  98. /*
  99. * directories use this key as well as old files
  100. */
  101. struct offset_v1
  102. {
  103. /*
  104. * for regular files this is the offset to the first byte of the
  105. * body, contained in the object-item, as measured from the start of
  106. * the entire body of the object.
  107. *
  108. * for directory entries, k_offset consists of hash derived from
  109. * hashing the name and using few bits (23 or more) of the resulting
  110. * hash, and generation number that allows distinguishing names with
  111. * hash collisions. If number of collisions overflows generation
  112. * number, we return EEXIST. High order bit is 0 always
  113. */
  114. __u32 k_offset;
  115. __u32 k_uniqueness;
  116. };
  117. struct offset_v2 {
  118. /*
  119. * for regular files this is the offset to the first byte of the
  120. * body, contained in the object-item, as measured from the start of
  121. * the entire body of the object.
  122. *
  123. * for directory entries, k_offset consists of hash derived from
  124. * hashing the name and using few bits (23 or more) of the resulting
  125. * hash, and generation number that allows distinguishing names with
  126. * hash collisions. If number of collisions overflows generation
  127. * number, we return EEXIST. High order bit is 0 always
  128. */
  129. #if defined(__LITTLE_ENDIAN_BITFIELD)
  130. /* little endian version */
  131. __u64 k_offset:60;
  132. __u64 k_type: 4;
  133. #elif defined(__BIG_ENDIAN_BITFIELD)
  134. /* big endian version */
  135. __u64 k_type: 4;
  136. __u64 k_offset:60;
  137. #else
  138. #error "__LITTLE_ENDIAN_BITFIELD or __BIG_ENDIAN_BITFIELD must be defined"
  139. #endif
  140. } __attribute__ ((__packed__));
  141. #define TYPE_MAXTYPE 3
  142. #define TYPE_ANY 15
  143. #if (__BYTE_ORDER == __BIG_ENDIAN)
  144. typedef union {
  145. struct offset_v2 offset_v2;
  146. __u64 linear;
  147. } __attribute__ ((__packed__)) offset_v2_esafe_overlay;
  148. static inline __u16 offset_v2_k_type( const struct offset_v2 *v2 )
  149. {
  150. offset_v2_esafe_overlay tmp = *(const offset_v2_esafe_overlay *)v2;
  151. tmp.linear = __le64_to_cpu( tmp.linear );
  152. return (tmp.offset_v2.k_type <= TYPE_MAXTYPE)?tmp.offset_v2.k_type:TYPE_ANY;
  153. }
  154. static inline loff_t offset_v2_k_offset( const struct offset_v2 *v2 )
  155. {
  156. offset_v2_esafe_overlay tmp = *(const offset_v2_esafe_overlay *)v2;
  157. tmp.linear = __le64_to_cpu( tmp.linear );
  158. return tmp.offset_v2.k_offset;
  159. }
  160. #elif (__BYTE_ORDER == __LITTLE_ENDIAN)
  161. # define offset_v2_k_type(v2) ((v2)->k_type)
  162. # define offset_v2_k_offset(v2) ((v2)->k_offset)
  163. #else
  164. #error "__BYTE_ORDER must be __LITTLE_ENDIAN or __BIG_ENDIAN"
  165. #endif
  166. struct key
  167. {
  168. /* packing locality: by default parent directory object id */
  169. __u32 k_dir_id;
  170. /* object identifier */
  171. __u32 k_objectid;
  172. /* the offset and node type (old and new form) */
  173. union
  174. {
  175. struct offset_v1 v1;
  176. struct offset_v2 v2;
  177. }
  178. u;
  179. };
  180. #define KEY_SIZE (sizeof (struct key))
  181. /* Header of a disk block. More precisely, header of a formatted leaf
  182. or internal node, and not the header of an unformatted node. */
  183. struct block_head
  184. {
  185. __u16 blk_level; /* Level of a block in the tree. */
  186. __u16 blk_nr_item; /* Number of keys/items in a block. */
  187. __u16 blk_free_space; /* Block free space in bytes. */
  188. struct key blk_right_delim_key; /* Right delimiting key for this block (supported for leaf level nodes
  189. only) */
  190. };
  191. #define BLKH_SIZE (sizeof (struct block_head))
  192. #define DISK_LEAF_NODE_LEVEL 1 /* Leaf node level. */
  193. struct item_head
  194. {
  195. /* Everything in the tree is found by searching for it based on
  196. * its key.*/
  197. struct key ih_key;
  198. union {
  199. /* The free space in the last unformatted node of an
  200. indirect item if this is an indirect item. This
  201. equals 0xFFFF iff this is a direct item or stat data
  202. item. Note that the key, not this field, is used to
  203. determine the item type, and thus which field this
  204. union contains. */
  205. __u16 ih_free_space;
  206. /* Iff this is a directory item, this field equals the
  207. number of directory entries in the directory item. */
  208. __u16 ih_entry_count;
  209. } __attribute__ ((__packed__)) u;
  210. __u16 ih_item_len; /* total size of the item body */
  211. __u16 ih_item_location; /* an offset to the item body
  212. * within the block */
  213. __u16 ih_version; /* 0 for all old items, 2 for new
  214. ones. Highest bit is set by fsck
  215. temporary, cleaned after all
  216. done */
  217. } __attribute__ ((__packed__));
  218. /* size of item header */
  219. #define IH_SIZE (sizeof (struct item_head))
  220. #define ITEM_VERSION_1 0
  221. #define ITEM_VERSION_2 1
  222. #define ih_version(ih) (__le16_to_cpu((ih)->ih_version))
  223. #define IH_KEY_OFFSET(ih) (ih_version(ih) == ITEM_VERSION_1 \
  224. ? __le32_to_cpu((ih)->ih_key.u.v1.k_offset) \
  225. : offset_v2_k_offset(&((ih)->ih_key.u.v2)))
  226. #define IH_KEY_ISTYPE(ih, type) (ih_version(ih) == ITEM_VERSION_1 \
  227. ? __le32_to_cpu((ih)->ih_key.u.v1.k_uniqueness) == V1_##type \
  228. : offset_v2_k_type(&((ih)->ih_key.u.v2)) == V2_##type)
  229. /***************************************************************************/
  230. /* DISK CHILD */
  231. /***************************************************************************/
  232. /* Disk child pointer: The pointer from an internal node of the tree
  233. to a node that is on disk. */
  234. struct disk_child {
  235. __u32 dc_block_number; /* Disk child's block number. */
  236. __u16 dc_size; /* Disk child's used space. */
  237. __u16 dc_reserved;
  238. };
  239. #define DC_SIZE (sizeof(struct disk_child))
  240. #define dc_block_number(dc_p) (__le32_to_cpu((dc_p)->dc_block_number))
  241. /*
  242. * old stat data is 32 bytes long. We are going to distinguish new one by
  243. * different size
  244. */
  245. struct stat_data_v1
  246. {
  247. __u16 sd_mode; /* file type, permissions */
  248. __u16 sd_nlink; /* number of hard links */
  249. __u16 sd_uid; /* owner */
  250. __u16 sd_gid; /* group */
  251. __u32 sd_size; /* file size */
  252. __u32 sd_atime; /* time of last access */
  253. __u32 sd_mtime; /* time file was last modified */
  254. __u32 sd_ctime; /* time inode (stat data) was last changed (except changes to sd_atime and sd_mtime) */
  255. union {
  256. __u32 sd_rdev;
  257. __u32 sd_blocks; /* number of blocks file uses */
  258. } __attribute__ ((__packed__)) u;
  259. __u32 sd_first_direct_byte; /* first byte of file which is stored
  260. in a direct item: except that if it
  261. equals 1 it is a symlink and if it
  262. equals ~(__u32)0 there is no
  263. direct item. The existence of this
  264. field really grates on me. Let's
  265. replace it with a macro based on
  266. sd_size and our tail suppression
  267. policy. Someday. -Hans */
  268. } __attribute__ ((__packed__));
  269. #define stat_data_v1(ih) (ih_version(ih) == ITEM_VERSION_1)
  270. #define sd_v1_mode(sdp) ((sdp)->sd_mode)
  271. #define sd_v1_nlink(sdp) (__le16_to_cpu((sdp)->sd_nlink))
  272. #define sd_v1_uid(sdp) (__le16_to_cpu((sdp)->sd_uid))
  273. #define sd_v1_gid(sdp) (__le16_to_cpu((sdp)->sd_gid))
  274. #define sd_v1_size(sdp) (__le32_to_cpu((sdp)->sd_size))
  275. #define sd_v1_mtime(sdp) (__le32_to_cpu((sdp)->sd_mtime))
  276. /* Stat Data on disk (reiserfs version of UFS disk inode minus the
  277. address blocks) */
  278. struct stat_data {
  279. __u16 sd_mode; /* file type, permissions */
  280. __u16 sd_attrs; /* persistent inode flags */
  281. __u32 sd_nlink; /* number of hard links */
  282. __u64 sd_size; /* file size */
  283. __u32 sd_uid; /* owner */
  284. __u32 sd_gid; /* group */
  285. __u32 sd_atime; /* time of last access */
  286. __u32 sd_mtime; /* time file was last modified */
  287. __u32 sd_ctime; /* time inode (stat data) was last changed (except changes to sd_atime and sd_mtime) */
  288. __u32 sd_blocks;
  289. union {
  290. __u32 sd_rdev;
  291. __u32 sd_generation;
  292. /*__u32 sd_first_direct_byte; */
  293. /* first byte of file which is stored in a
  294. direct item: except that if it equals 1
  295. it is a symlink and if it equals
  296. ~(__u32)0 there is no direct item. The
  297. existence of this field really grates
  298. on me. Let's replace it with a macro
  299. based on sd_size and our tail
  300. suppression policy? */
  301. } __attribute__ ((__packed__)) u;
  302. } __attribute__ ((__packed__));
  303. #define stat_data_v2(ih) (ih_version(ih) == ITEM_VERSION_2)
  304. #define sd_v2_mode(sdp) (__le16_to_cpu((sdp)->sd_mode))
  305. #define sd_v2_nlink(sdp) (__le32_to_cpu((sdp)->sd_nlink))
  306. #define sd_v2_size(sdp) (__le64_to_cpu((sdp)->sd_size))
  307. #define sd_v2_uid(sdp) (__le32_to_cpu((sdp)->sd_uid))
  308. #define sd_v2_gid(sdp) (__le32_to_cpu((sdp)->sd_gid))
  309. #define sd_v2_mtime(sdp) (__le32_to_cpu((sdp)->sd_mtime))
  310. #define sd_mode(sdp) (__le16_to_cpu((sdp)->sd_mode))
  311. #define sd_size(sdp) (__le32_to_cpu((sdp)->sd_size))
  312. #define sd_size_hi(sdp) (__le32_to_cpu((sdp)->sd_size_hi))
  313. struct reiserfs_de_head
  314. {
  315. __u32 deh_offset; /* third component of the directory entry key */
  316. __u32 deh_dir_id; /* objectid of the parent directory of the
  317. object, that is referenced by directory entry */
  318. __u32 deh_objectid;/* objectid of the object, that is referenced by
  319. directory entry */
  320. __u16 deh_location;/* offset of name in the whole item */
  321. __u16 deh_state; /* whether 1) entry contains stat data (for
  322. future), and 2) whether entry is hidden
  323. (unlinked) */
  324. };
  325. #define DEH_SIZE (sizeof (struct reiserfs_de_head))
  326. #define deh_offset(p_deh) (__le32_to_cpu((p_deh)->deh_offset))
  327. #define deh_dir_id(p_deh) (__le32_to_cpu((p_deh)->deh_dir_id))
  328. #define deh_objectid(p_deh) (__le32_to_cpu((p_deh)->deh_objectid))
  329. #define deh_location(p_deh) (__le16_to_cpu((p_deh)->deh_location))
  330. #define deh_state(p_deh) (__le16_to_cpu((p_deh)->deh_state))
  331. #define DEH_Statdata (1 << 0) /* not used now */
  332. #define DEH_Visible (1 << 2)
  333. #define SD_OFFSET 0
  334. #define SD_UNIQUENESS 0
  335. #define DOT_OFFSET 1
  336. #define DOT_DOT_OFFSET 2
  337. #define DIRENTRY_UNIQUENESS 500
  338. #define V1_TYPE_STAT_DATA 0x0
  339. #define V1_TYPE_DIRECT 0xffffffff
  340. #define V1_TYPE_INDIRECT 0xfffffffe
  341. #define V1_TYPE_DIRECTORY_MAX 0xfffffffd
  342. #define V2_TYPE_STAT_DATA 0
  343. #define V2_TYPE_INDIRECT 1
  344. #define V2_TYPE_DIRECT 2
  345. #define V2_TYPE_DIRENTRY 3
  346. #define REISERFS_ROOT_OBJECTID 2
  347. #define REISERFS_ROOT_PARENT_OBJECTID 1
  348. #define REISERFS_DISK_OFFSET_IN_BYTES (64 * 1024)
  349. /* the spot for the super in versions 3.5 - 3.5.11 (inclusive) */
  350. #define REISERFS_OLD_DISK_OFFSET_IN_BYTES (8 * 1024)
  351. #define REISERFS_OLD_BLOCKSIZE 4096
  352. #define S_ISREG(mode) (((mode) & 0170000) == 0100000)
  353. #define S_ISDIR(mode) (((mode) & 0170000) == 0040000)
  354. #define S_ISLNK(mode) (((mode) & 0170000) == 0120000)
  355. #define PATH_MAX 1024 /* include/linux/limits.h */
  356. #define MAX_LINK_COUNT 5 /* number of symbolic links to follow */
  357. /* The size of the node cache */
  358. #define FSYSREISER_CACHE_SIZE 24*1024
  359. #define FSYSREISER_MIN_BLOCKSIZE SECTOR_SIZE
  360. #define FSYSREISER_MAX_BLOCKSIZE FSYSREISER_CACHE_SIZE / 3
  361. /* Info about currently opened file */
  362. struct fsys_reiser_fileinfo
  363. {
  364. __u32 k_dir_id;
  365. __u32 k_objectid;
  366. };
  367. /* In memory info about the currently mounted filesystem */
  368. struct fsys_reiser_info
  369. {
  370. /* The last read item head */
  371. struct item_head *current_ih;
  372. /* The last read item */
  373. char *current_item;
  374. /* The information for the currently opened file */
  375. struct fsys_reiser_fileinfo fileinfo;
  376. /* The start of the journal */
  377. __u32 journal_block;
  378. /* The size of the journal */
  379. __u32 journal_block_count;
  380. /* The first valid descriptor block in journal
  381. (relative to journal_block) */
  382. __u32 journal_first_desc;
  383. /* The ReiserFS version. */
  384. __u16 version;
  385. /* The current depth of the reiser tree. */
  386. __u16 tree_depth;
  387. /* SECTOR_SIZE << blocksize_shift == blocksize. */
  388. __u8 blocksize_shift;
  389. /* 1 << full_blocksize_shift == blocksize. */
  390. __u8 fullblocksize_shift;
  391. /* The reiserfs block size (must be a power of 2) */
  392. __u16 blocksize;
  393. /* The number of cached tree nodes */
  394. __u16 cached_slots;
  395. /* The number of valid transactions in journal */
  396. __u16 journal_transactions;
  397. unsigned int blocks[MAX_HEIGHT];
  398. unsigned int next_key_nr[MAX_HEIGHT];
  399. };
  400. /* The cached s+tree blocks in FSYS_BUF, see below
  401. * for a more detailed description.
  402. */
  403. #define ROOT ((char *) ((int) FSYS_BUF))
  404. #define CACHE(i) (ROOT + ((i) << INFO->fullblocksize_shift))
  405. #define LEAF CACHE (DISK_LEAF_NODE_LEVEL)
  406. #define BLOCKHEAD(cache) ((struct block_head *) cache)
  407. #define ITEMHEAD ((struct item_head *) ((int) LEAF + BLKH_SIZE))
  408. #define KEY(cache) ((struct key *) ((int) cache + BLKH_SIZE))
  409. #define DC(cache) ((struct disk_child *) \
  410. ((int) cache + BLKH_SIZE + KEY_SIZE * nr_item))
  411. /* The fsys_reiser_info block.
  412. */
  413. #define INFO \
  414. ((struct fsys_reiser_info *) ((int) FSYS_BUF + FSYSREISER_CACHE_SIZE))
  415. /*
  416. * The journal cache. For each transaction it contains the number of
  417. * blocks followed by the real block numbers of this transaction.
  418. *
  419. * If the block numbers of some transaction won't fit in this space,
  420. * this list is stopped with a 0xffffffff marker and the remaining
  421. * uncommitted transactions aren't cached.
  422. */
  423. #define JOURNAL_START ((__u32 *) (INFO + 1))
  424. #define JOURNAL_END ((__u32 *) (FSYS_BUF + FSYS_BUFLEN))
  425. static __inline__ unsigned long
  426. log2 (unsigned long word)
  427. {
  428. #ifdef __I386__
  429. __asm__ ("bsfl %1,%0"
  430. : "=r" (word)
  431. : "r" (word));
  432. return word;
  433. #else
  434. int i;
  435. for(i=0; i<(8*sizeof(word)); i++)
  436. if ((1<<i) & word)
  437. return i;
  438. return 0;
  439. #endif
  440. }
  441. static __inline__ int
  442. is_power_of_two (unsigned long word)
  443. {
  444. return (word & -word) == word;
  445. }
  446. extern const char *bb_mode_string(int mode);
  447. extern int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf);