PageRenderTime 47ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/include/linux/reiserfs_fs.h

https://github.com/mstsirkin/kvm
C Header | 1311 lines | 766 code | 176 blank | 369 comment | 56 complexity | 94e53422cf032d9eedb6c2634dba25dd MD5 | raw file
  1. /*
  2. * Copyright 1996, 1997, 1998 Hans Reiser, see reiserfs/README for licensing and copyright details
  3. */
  4. /* this file has an amazingly stupid
  5. name, yura please fix it to be
  6. reiserfs.h, and merge all the rest
  7. of our .h files that are in this
  8. directory into it. */
  9. #ifndef _LINUX_REISER_FS_H
  10. #define _LINUX_REISER_FS_H
  11. #include <linux/types.h>
  12. #include <linux/magic.h>
  13. #ifdef __KERNEL__
  14. #include <linux/slab.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/sched.h>
  17. #include <linux/workqueue.h>
  18. #include <asm/unaligned.h>
  19. #include <linux/bitops.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/reiserfs_fs_i.h>
  23. #include <linux/reiserfs_fs_sb.h>
  24. #endif
  25. /*
  26. * include/linux/reiser_fs.h
  27. *
  28. * Reiser File System constants and structures
  29. *
  30. */
  31. /* ioctl's command */
  32. #define REISERFS_IOC_UNPACK _IOW(0xCD,1,long)
  33. /* define following flags to be the same as in ext2, so that chattr(1),
  34. lsattr(1) will work with us. */
  35. #define REISERFS_IOC_GETFLAGS FS_IOC_GETFLAGS
  36. #define REISERFS_IOC_SETFLAGS FS_IOC_SETFLAGS
  37. #define REISERFS_IOC_GETVERSION FS_IOC_GETVERSION
  38. #define REISERFS_IOC_SETVERSION FS_IOC_SETVERSION
  39. #ifdef __KERNEL__
  40. /* the 32 bit compat definitions with int argument */
  41. #define REISERFS_IOC32_UNPACK _IOW(0xCD, 1, int)
  42. #define REISERFS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
  43. #define REISERFS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
  44. #define REISERFS_IOC32_GETVERSION FS_IOC32_GETVERSION
  45. #define REISERFS_IOC32_SETVERSION FS_IOC32_SETVERSION
  46. /*
  47. * Locking primitives. The write lock is a per superblock
  48. * special mutex that has properties close to the Big Kernel Lock
  49. * which was used in the previous locking scheme.
  50. */
  51. void reiserfs_write_lock(struct super_block *s);
  52. void reiserfs_write_unlock(struct super_block *s);
  53. int reiserfs_write_lock_once(struct super_block *s);
  54. void reiserfs_write_unlock_once(struct super_block *s, int lock_depth);
  55. #ifdef CONFIG_REISERFS_CHECK
  56. void reiserfs_lock_check_recursive(struct super_block *s);
  57. #else
  58. static inline void reiserfs_lock_check_recursive(struct super_block *s) { }
  59. #endif
  60. /*
  61. * Several mutexes depend on the write lock.
  62. * However sometimes we want to relax the write lock while we hold
  63. * these mutexes, according to the release/reacquire on schedule()
  64. * properties of the Bkl that were used.
  65. * Reiserfs performances and locking were based on this scheme.
  66. * Now that the write lock is a mutex and not the bkl anymore, doing so
  67. * may result in a deadlock:
  68. *
  69. * A acquire write_lock
  70. * A acquire j_commit_mutex
  71. * A release write_lock and wait for something
  72. * B acquire write_lock
  73. * B can't acquire j_commit_mutex and sleep
  74. * A can't acquire write lock anymore
  75. * deadlock
  76. *
  77. * What we do here is avoiding such deadlock by playing the same game
  78. * than the Bkl: if we can't acquire a mutex that depends on the write lock,
  79. * we release the write lock, wait a bit and then retry.
  80. *
  81. * The mutexes concerned by this hack are:
  82. * - The commit mutex of a journal list
  83. * - The flush mutex
  84. * - The journal lock
  85. * - The inode mutex
  86. */
  87. static inline void reiserfs_mutex_lock_safe(struct mutex *m,
  88. struct super_block *s)
  89. {
  90. reiserfs_lock_check_recursive(s);
  91. reiserfs_write_unlock(s);
  92. mutex_lock(m);
  93. reiserfs_write_lock(s);
  94. }
  95. static inline void
  96. reiserfs_mutex_lock_nested_safe(struct mutex *m, unsigned int subclass,
  97. struct super_block *s)
  98. {
  99. reiserfs_lock_check_recursive(s);
  100. reiserfs_write_unlock(s);
  101. mutex_lock_nested(m, subclass);
  102. reiserfs_write_lock(s);
  103. }
  104. static inline void
  105. reiserfs_down_read_safe(struct rw_semaphore *sem, struct super_block *s)
  106. {
  107. reiserfs_lock_check_recursive(s);
  108. reiserfs_write_unlock(s);
  109. down_read(sem);
  110. reiserfs_write_lock(s);
  111. }
  112. /*
  113. * When we schedule, we usually want to also release the write lock,
  114. * according to the previous bkl based locking scheme of reiserfs.
  115. */
  116. static inline void reiserfs_cond_resched(struct super_block *s)
  117. {
  118. if (need_resched()) {
  119. reiserfs_write_unlock(s);
  120. schedule();
  121. reiserfs_write_lock(s);
  122. }
  123. }
  124. struct fid;
  125. /* in reading the #defines, it may help to understand that they employ
  126. the following abbreviations:
  127. B = Buffer
  128. I = Item header
  129. H = Height within the tree (should be changed to LEV)
  130. N = Number of the item in the node
  131. STAT = stat data
  132. DEH = Directory Entry Header
  133. EC = Entry Count
  134. E = Entry number
  135. UL = Unsigned Long
  136. BLKH = BLocK Header
  137. UNFM = UNForMatted node
  138. DC = Disk Child
  139. P = Path
  140. These #defines are named by concatenating these abbreviations,
  141. where first comes the arguments, and last comes the return value,
  142. of the macro.
  143. */
  144. #define USE_INODE_GENERATION_COUNTER
  145. #define REISERFS_PREALLOCATE
  146. #define DISPLACE_NEW_PACKING_LOCALITIES
  147. #define PREALLOCATION_SIZE 9
  148. /* n must be power of 2 */
  149. #define _ROUND_UP(x,n) (((x)+(n)-1u) & ~((n)-1u))
  150. // to be ok for alpha and others we have to align structures to 8 byte
  151. // boundary.
  152. // FIXME: do not change 4 by anything else: there is code which relies on that
  153. #define ROUND_UP(x) _ROUND_UP(x,8LL)
  154. /* debug levels. Right now, CONFIG_REISERFS_CHECK means print all debug
  155. ** messages.
  156. */
  157. #define REISERFS_DEBUG_CODE 5 /* extra messages to help find/debug errors */
  158. void __reiserfs_warning(struct super_block *s, const char *id,
  159. const char *func, const char *fmt, ...);
  160. #define reiserfs_warning(s, id, fmt, args...) \
  161. __reiserfs_warning(s, id, __func__, fmt, ##args)
  162. /* assertions handling */
  163. /** always check a condition and panic if it's false. */
  164. #define __RASSERT(cond, scond, format, args...) \
  165. do { \
  166. if (!(cond)) \
  167. reiserfs_panic(NULL, "assertion failure", "(" #cond ") at " \
  168. __FILE__ ":%i:%s: " format "\n", \
  169. in_interrupt() ? -1 : task_pid_nr(current), \
  170. __LINE__, __func__ , ##args); \
  171. } while (0)
  172. #define RASSERT(cond, format, args...) __RASSERT(cond, #cond, format, ##args)
  173. #if defined( CONFIG_REISERFS_CHECK )
  174. #define RFALSE(cond, format, args...) __RASSERT(!(cond), "!(" #cond ")", format, ##args)
  175. #else
  176. #define RFALSE( cond, format, args... ) do {;} while( 0 )
  177. #endif
  178. #define CONSTF __attribute_const__
  179. /*
  180. * Disk Data Structures
  181. */
  182. /***************************************************************************/
  183. /* SUPER BLOCK */
  184. /***************************************************************************/
  185. /*
  186. * Structure of super block on disk, a version of which in RAM is often accessed as REISERFS_SB(s)->s_rs
  187. * the version in RAM is part of a larger structure containing fields never written to disk.
  188. */
  189. #define UNSET_HASH 0 // read_super will guess about, what hash names
  190. // in directories were sorted with
  191. #define TEA_HASH 1
  192. #define YURA_HASH 2
  193. #define R5_HASH 3
  194. #define DEFAULT_HASH R5_HASH
  195. struct journal_params {
  196. __le32 jp_journal_1st_block; /* where does journal start from on its
  197. * device */
  198. __le32 jp_journal_dev; /* journal device st_rdev */
  199. __le32 jp_journal_size; /* size of the journal */
  200. __le32 jp_journal_trans_max; /* max number of blocks in a transaction. */
  201. __le32 jp_journal_magic; /* random value made on fs creation (this
  202. * was sb_journal_block_count) */
  203. __le32 jp_journal_max_batch; /* max number of blocks to batch into a
  204. * trans */
  205. __le32 jp_journal_max_commit_age; /* in seconds, how old can an async
  206. * commit be */
  207. __le32 jp_journal_max_trans_age; /* in seconds, how old can a transaction
  208. * be */
  209. };
  210. /* this is the super from 3.5.X, where X >= 10 */
  211. struct reiserfs_super_block_v1 {
  212. __le32 s_block_count; /* blocks count */
  213. __le32 s_free_blocks; /* free blocks count */
  214. __le32 s_root_block; /* root block number */
  215. struct journal_params s_journal;
  216. __le16 s_blocksize; /* block size */
  217. __le16 s_oid_maxsize; /* max size of object id array, see
  218. * get_objectid() commentary */
  219. __le16 s_oid_cursize; /* current size of object id array */
  220. __le16 s_umount_state; /* this is set to 1 when filesystem was
  221. * umounted, to 2 - when not */
  222. char s_magic[10]; /* reiserfs magic string indicates that
  223. * file system is reiserfs:
  224. * "ReIsErFs" or "ReIsEr2Fs" or "ReIsEr3Fs" */
  225. __le16 s_fs_state; /* it is set to used by fsck to mark which
  226. * phase of rebuilding is done */
  227. __le32 s_hash_function_code; /* indicate, what hash function is being use
  228. * to sort names in a directory*/
  229. __le16 s_tree_height; /* height of disk tree */
  230. __le16 s_bmap_nr; /* amount of bitmap blocks needed to address
  231. * each block of file system */
  232. __le16 s_version; /* this field is only reliable on filesystem
  233. * with non-standard journal */
  234. __le16 s_reserved_for_journal; /* size in blocks of journal area on main
  235. * device, we need to keep after
  236. * making fs with non-standard journal */
  237. } __attribute__ ((__packed__));
  238. #define SB_SIZE_V1 (sizeof(struct reiserfs_super_block_v1))
  239. /* this is the on disk super block */
  240. struct reiserfs_super_block {
  241. struct reiserfs_super_block_v1 s_v1;
  242. __le32 s_inode_generation;
  243. __le32 s_flags; /* Right now used only by inode-attributes, if enabled */
  244. unsigned char s_uuid[16]; /* filesystem unique identifier */
  245. unsigned char s_label[16]; /* filesystem volume label */
  246. __le16 s_mnt_count; /* Count of mounts since last fsck */
  247. __le16 s_max_mnt_count; /* Maximum mounts before check */
  248. __le32 s_lastcheck; /* Timestamp of last fsck */
  249. __le32 s_check_interval; /* Interval between checks */
  250. char s_unused[76]; /* zero filled by mkreiserfs and
  251. * reiserfs_convert_objectid_map_v1()
  252. * so any additions must be updated
  253. * there as well. */
  254. } __attribute__ ((__packed__));
  255. #define SB_SIZE (sizeof(struct reiserfs_super_block))
  256. #define REISERFS_VERSION_1 0
  257. #define REISERFS_VERSION_2 2
  258. // on-disk super block fields converted to cpu form
  259. #define SB_DISK_SUPER_BLOCK(s) (REISERFS_SB(s)->s_rs)
  260. #define SB_V1_DISK_SUPER_BLOCK(s) (&(SB_DISK_SUPER_BLOCK(s)->s_v1))
  261. #define SB_BLOCKSIZE(s) \
  262. le32_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_blocksize))
  263. #define SB_BLOCK_COUNT(s) \
  264. le32_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_block_count))
  265. #define SB_FREE_BLOCKS(s) \
  266. le32_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_free_blocks))
  267. #define SB_REISERFS_MAGIC(s) \
  268. (SB_V1_DISK_SUPER_BLOCK(s)->s_magic)
  269. #define SB_ROOT_BLOCK(s) \
  270. le32_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_root_block))
  271. #define SB_TREE_HEIGHT(s) \
  272. le16_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_tree_height))
  273. #define SB_REISERFS_STATE(s) \
  274. le16_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_umount_state))
  275. #define SB_VERSION(s) le16_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_version))
  276. #define SB_BMAP_NR(s) le16_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_bmap_nr))
  277. #define PUT_SB_BLOCK_COUNT(s, val) \
  278. do { SB_V1_DISK_SUPER_BLOCK(s)->s_block_count = cpu_to_le32(val); } while (0)
  279. #define PUT_SB_FREE_BLOCKS(s, val) \
  280. do { SB_V1_DISK_SUPER_BLOCK(s)->s_free_blocks = cpu_to_le32(val); } while (0)
  281. #define PUT_SB_ROOT_BLOCK(s, val) \
  282. do { SB_V1_DISK_SUPER_BLOCK(s)->s_root_block = cpu_to_le32(val); } while (0)
  283. #define PUT_SB_TREE_HEIGHT(s, val) \
  284. do { SB_V1_DISK_SUPER_BLOCK(s)->s_tree_height = cpu_to_le16(val); } while (0)
  285. #define PUT_SB_REISERFS_STATE(s, val) \
  286. do { SB_V1_DISK_SUPER_BLOCK(s)->s_umount_state = cpu_to_le16(val); } while (0)
  287. #define PUT_SB_VERSION(s, val) \
  288. do { SB_V1_DISK_SUPER_BLOCK(s)->s_version = cpu_to_le16(val); } while (0)
  289. #define PUT_SB_BMAP_NR(s, val) \
  290. do { SB_V1_DISK_SUPER_BLOCK(s)->s_bmap_nr = cpu_to_le16 (val); } while (0)
  291. #define SB_ONDISK_JP(s) (&SB_V1_DISK_SUPER_BLOCK(s)->s_journal)
  292. #define SB_ONDISK_JOURNAL_SIZE(s) \
  293. le32_to_cpu ((SB_ONDISK_JP(s)->jp_journal_size))
  294. #define SB_ONDISK_JOURNAL_1st_BLOCK(s) \
  295. le32_to_cpu ((SB_ONDISK_JP(s)->jp_journal_1st_block))
  296. #define SB_ONDISK_JOURNAL_DEVICE(s) \
  297. le32_to_cpu ((SB_ONDISK_JP(s)->jp_journal_dev))
  298. #define SB_ONDISK_RESERVED_FOR_JOURNAL(s) \
  299. le16_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_reserved_for_journal))
  300. #define is_block_in_log_or_reserved_area(s, block) \
  301. block >= SB_JOURNAL_1st_RESERVED_BLOCK(s) \
  302. && block < SB_JOURNAL_1st_RESERVED_BLOCK(s) + \
  303. ((!is_reiserfs_jr(SB_DISK_SUPER_BLOCK(s)) ? \
  304. SB_ONDISK_JOURNAL_SIZE(s) + 1 : SB_ONDISK_RESERVED_FOR_JOURNAL(s)))
  305. int is_reiserfs_3_5(struct reiserfs_super_block *rs);
  306. int is_reiserfs_3_6(struct reiserfs_super_block *rs);
  307. int is_reiserfs_jr(struct reiserfs_super_block *rs);
  308. /* ReiserFS leaves the first 64k unused, so that partition labels have
  309. enough space. If someone wants to write a fancy bootloader that
  310. needs more than 64k, let us know, and this will be increased in size.
  311. This number must be larger than than the largest block size on any
  312. platform, or code will break. -Hans */
  313. #define REISERFS_DISK_OFFSET_IN_BYTES (64 * 1024)
  314. #define REISERFS_FIRST_BLOCK unused_define
  315. #define REISERFS_JOURNAL_OFFSET_IN_BYTES REISERFS_DISK_OFFSET_IN_BYTES
  316. /* the spot for the super in versions 3.5 - 3.5.10 (inclusive) */
  317. #define REISERFS_OLD_DISK_OFFSET_IN_BYTES (8 * 1024)
  318. /* reiserfs internal error code (used by search_by_key and fix_nodes)) */
  319. #define CARRY_ON 0
  320. #define REPEAT_SEARCH -1
  321. #define IO_ERROR -2
  322. #define NO_DISK_SPACE -3
  323. #define NO_BALANCING_NEEDED (-4)
  324. #define NO_MORE_UNUSED_CONTIGUOUS_BLOCKS (-5)
  325. #define QUOTA_EXCEEDED -6
  326. typedef __u32 b_blocknr_t;
  327. typedef __le32 unp_t;
  328. struct unfm_nodeinfo {
  329. unp_t unfm_nodenum;
  330. unsigned short unfm_freespace;
  331. };
  332. /* there are two formats of keys: 3.5 and 3.6
  333. */
  334. #define KEY_FORMAT_3_5 0
  335. #define KEY_FORMAT_3_6 1
  336. /* there are two stat datas */
  337. #define STAT_DATA_V1 0
  338. #define STAT_DATA_V2 1
  339. static inline struct reiserfs_inode_info *REISERFS_I(const struct inode *inode)
  340. {
  341. return container_of(inode, struct reiserfs_inode_info, vfs_inode);
  342. }
  343. static inline struct reiserfs_sb_info *REISERFS_SB(const struct super_block *sb)
  344. {
  345. return sb->s_fs_info;
  346. }
  347. /* Don't trust REISERFS_SB(sb)->s_bmap_nr, it's a u16
  348. * which overflows on large file systems. */
  349. static inline __u32 reiserfs_bmap_count(struct super_block *sb)
  350. {
  351. return (SB_BLOCK_COUNT(sb) - 1) / (sb->s_blocksize * 8) + 1;
  352. }
  353. static inline int bmap_would_wrap(unsigned bmap_nr)
  354. {
  355. return bmap_nr > ((1LL << 16) - 1);
  356. }
  357. /** this says about version of key of all items (but stat data) the
  358. object consists of */
  359. #define get_inode_item_key_version( inode ) \
  360. ((REISERFS_I(inode)->i_flags & i_item_key_version_mask) ? KEY_FORMAT_3_6 : KEY_FORMAT_3_5)
  361. #define set_inode_item_key_version( inode, version ) \
  362. ({ if((version)==KEY_FORMAT_3_6) \
  363. REISERFS_I(inode)->i_flags |= i_item_key_version_mask; \
  364. else \
  365. REISERFS_I(inode)->i_flags &= ~i_item_key_version_mask; })
  366. #define get_inode_sd_version(inode) \
  367. ((REISERFS_I(inode)->i_flags & i_stat_data_version_mask) ? STAT_DATA_V2 : STAT_DATA_V1)
  368. #define set_inode_sd_version(inode, version) \
  369. ({ if((version)==STAT_DATA_V2) \
  370. REISERFS_I(inode)->i_flags |= i_stat_data_version_mask; \
  371. else \
  372. REISERFS_I(inode)->i_flags &= ~i_stat_data_version_mask; })
  373. /* This is an aggressive tail suppression policy, I am hoping it
  374. improves our benchmarks. The principle behind it is that percentage
  375. space saving is what matters, not absolute space saving. This is
  376. non-intuitive, but it helps to understand it if you consider that the
  377. cost to access 4 blocks is not much more than the cost to access 1
  378. block, if you have to do a seek and rotate. A tail risks a
  379. non-linear disk access that is significant as a percentage of total
  380. time cost for a 4 block file and saves an amount of space that is
  381. less significant as a percentage of space, or so goes the hypothesis.
  382. -Hans */
  383. #define STORE_TAIL_IN_UNFM_S1(n_file_size,n_tail_size,n_block_size) \
  384. (\
  385. (!(n_tail_size)) || \
  386. (((n_tail_size) > MAX_DIRECT_ITEM_LEN(n_block_size)) || \
  387. ( (n_file_size) >= (n_block_size) * 4 ) || \
  388. ( ( (n_file_size) >= (n_block_size) * 3 ) && \
  389. ( (n_tail_size) >= (MAX_DIRECT_ITEM_LEN(n_block_size))/4) ) || \
  390. ( ( (n_file_size) >= (n_block_size) * 2 ) && \
  391. ( (n_tail_size) >= (MAX_DIRECT_ITEM_LEN(n_block_size))/2) ) || \
  392. ( ( (n_file_size) >= (n_block_size) ) && \
  393. ( (n_tail_size) >= (MAX_DIRECT_ITEM_LEN(n_block_size) * 3)/4) ) ) \
  394. )
  395. /* Another strategy for tails, this one means only create a tail if all the
  396. file would fit into one DIRECT item.
  397. Primary intention for this one is to increase performance by decreasing
  398. seeking.
  399. */
  400. #define STORE_TAIL_IN_UNFM_S2(n_file_size,n_tail_size,n_block_size) \
  401. (\
  402. (!(n_tail_size)) || \
  403. (((n_file_size) > MAX_DIRECT_ITEM_LEN(n_block_size)) ) \
  404. )
  405. /*
  406. * values for s_umount_state field
  407. */
  408. #define REISERFS_VALID_FS 1
  409. #define REISERFS_ERROR_FS 2
  410. //
  411. // there are 5 item types currently
  412. //
  413. #define TYPE_STAT_DATA 0
  414. #define TYPE_INDIRECT 1
  415. #define TYPE_DIRECT 2
  416. #define TYPE_DIRENTRY 3
  417. #define TYPE_MAXTYPE 3
  418. #define TYPE_ANY 15 // FIXME: comment is required
  419. /***************************************************************************/
  420. /* KEY & ITEM HEAD */
  421. /***************************************************************************/
  422. //
  423. // directories use this key as well as old files
  424. //
  425. struct offset_v1 {
  426. __le32 k_offset;
  427. __le32 k_uniqueness;
  428. } __attribute__ ((__packed__));
  429. struct offset_v2 {
  430. __le64 v;
  431. } __attribute__ ((__packed__));
  432. static inline __u16 offset_v2_k_type(const struct offset_v2 *v2)
  433. {
  434. __u8 type = le64_to_cpu(v2->v) >> 60;
  435. return (type <= TYPE_MAXTYPE) ? type : TYPE_ANY;
  436. }
  437. static inline void set_offset_v2_k_type(struct offset_v2 *v2, int type)
  438. {
  439. v2->v =
  440. (v2->v & cpu_to_le64(~0ULL >> 4)) | cpu_to_le64((__u64) type << 60);
  441. }
  442. static inline loff_t offset_v2_k_offset(const struct offset_v2 *v2)
  443. {
  444. return le64_to_cpu(v2->v) & (~0ULL >> 4);
  445. }
  446. static inline void set_offset_v2_k_offset(struct offset_v2 *v2, loff_t offset)
  447. {
  448. offset &= (~0ULL >> 4);
  449. v2->v = (v2->v & cpu_to_le64(15ULL << 60)) | cpu_to_le64(offset);
  450. }
  451. /* Key of an item determines its location in the S+tree, and
  452. is composed of 4 components */
  453. struct reiserfs_key {
  454. __le32 k_dir_id; /* packing locality: by default parent
  455. directory object id */
  456. __le32 k_objectid; /* object identifier */
  457. union {
  458. struct offset_v1 k_offset_v1;
  459. struct offset_v2 k_offset_v2;
  460. } __attribute__ ((__packed__)) u;
  461. } __attribute__ ((__packed__));
  462. struct in_core_key {
  463. __u32 k_dir_id; /* packing locality: by default parent
  464. directory object id */
  465. __u32 k_objectid; /* object identifier */
  466. __u64 k_offset;
  467. __u8 k_type;
  468. };
  469. struct cpu_key {
  470. struct in_core_key on_disk_key;
  471. int version;
  472. int key_length; /* 3 in all cases but direct2indirect and
  473. indirect2direct conversion */
  474. };
  475. /* Our function for comparing keys can compare keys of different
  476. lengths. It takes as a parameter the length of the keys it is to
  477. compare. These defines are used in determining what is to be passed
  478. to it as that parameter. */
  479. #define REISERFS_FULL_KEY_LEN 4
  480. #define REISERFS_SHORT_KEY_LEN 2
  481. /* The result of the key compare */
  482. #define FIRST_GREATER 1
  483. #define SECOND_GREATER -1
  484. #define KEYS_IDENTICAL 0
  485. #define KEY_FOUND 1
  486. #define KEY_NOT_FOUND 0
  487. #define KEY_SIZE (sizeof(struct reiserfs_key))
  488. #define SHORT_KEY_SIZE (sizeof (__u32) + sizeof (__u32))
  489. /* return values for search_by_key and clones */
  490. #define ITEM_FOUND 1
  491. #define ITEM_NOT_FOUND 0
  492. #define ENTRY_FOUND 1
  493. #define ENTRY_NOT_FOUND 0
  494. #define DIRECTORY_NOT_FOUND -1
  495. #define REGULAR_FILE_FOUND -2
  496. #define DIRECTORY_FOUND -3
  497. #define BYTE_FOUND 1
  498. #define BYTE_NOT_FOUND 0
  499. #define FILE_NOT_FOUND -1
  500. #define POSITION_FOUND 1
  501. #define POSITION_NOT_FOUND 0
  502. // return values for reiserfs_find_entry and search_by_entry_key
  503. #define NAME_FOUND 1
  504. #define NAME_NOT_FOUND 0
  505. #define GOTO_PREVIOUS_ITEM 2
  506. #define NAME_FOUND_INVISIBLE 3
  507. /* Everything in the filesystem is stored as a set of items. The
  508. item head contains the key of the item, its free space (for
  509. indirect items) and specifies the location of the item itself
  510. within the block. */
  511. struct item_head {
  512. /* Everything in the tree is found by searching for it based on
  513. * its key.*/
  514. struct reiserfs_key ih_key;
  515. union {
  516. /* The free space in the last unformatted node of an
  517. indirect item if this is an indirect item. This
  518. equals 0xFFFF iff this is a direct item or stat data
  519. item. Note that the key, not this field, is used to
  520. determine the item type, and thus which field this
  521. union contains. */
  522. __le16 ih_free_space_reserved;
  523. /* Iff this is a directory item, this field equals the
  524. number of directory entries in the directory item. */
  525. __le16 ih_entry_count;
  526. } __attribute__ ((__packed__)) u;
  527. __le16 ih_item_len; /* total size of the item body */
  528. __le16 ih_item_location; /* an offset to the item body
  529. * within the block */
  530. __le16 ih_version; /* 0 for all old items, 2 for new
  531. ones. Highest bit is set by fsck
  532. temporary, cleaned after all
  533. done */
  534. } __attribute__ ((__packed__));
  535. /* size of item header */
  536. #define IH_SIZE (sizeof(struct item_head))
  537. #define ih_free_space(ih) le16_to_cpu((ih)->u.ih_free_space_reserved)
  538. #define ih_version(ih) le16_to_cpu((ih)->ih_version)
  539. #define ih_entry_count(ih) le16_to_cpu((ih)->u.ih_entry_count)
  540. #define ih_location(ih) le16_to_cpu((ih)->ih_item_location)
  541. #define ih_item_len(ih) le16_to_cpu((ih)->ih_item_len)
  542. #define put_ih_free_space(ih, val) do { (ih)->u.ih_free_space_reserved = cpu_to_le16(val); } while(0)
  543. #define put_ih_version(ih, val) do { (ih)->ih_version = cpu_to_le16(val); } while (0)
  544. #define put_ih_entry_count(ih, val) do { (ih)->u.ih_entry_count = cpu_to_le16(val); } while (0)
  545. #define put_ih_location(ih, val) do { (ih)->ih_item_location = cpu_to_le16(val); } while (0)
  546. #define put_ih_item_len(ih, val) do { (ih)->ih_item_len = cpu_to_le16(val); } while (0)
  547. #define unreachable_item(ih) (ih_version(ih) & (1 << 15))
  548. #define get_ih_free_space(ih) (ih_version (ih) == KEY_FORMAT_3_6 ? 0 : ih_free_space (ih))
  549. #define set_ih_free_space(ih,val) put_ih_free_space((ih), ((ih_version(ih) == KEY_FORMAT_3_6) ? 0 : (val)))
  550. /* these operate on indirect items, where you've got an array of ints
  551. ** at a possibly unaligned location. These are a noop on ia32
  552. **
  553. ** p is the array of __u32, i is the index into the array, v is the value
  554. ** to store there.
  555. */
  556. #define get_block_num(p, i) get_unaligned_le32((p) + (i))
  557. #define put_block_num(p, i, v) put_unaligned_le32((v), (p) + (i))
  558. //
  559. // in old version uniqueness field shows key type
  560. //
  561. #define V1_SD_UNIQUENESS 0
  562. #define V1_INDIRECT_UNIQUENESS 0xfffffffe
  563. #define V1_DIRECT_UNIQUENESS 0xffffffff
  564. #define V1_DIRENTRY_UNIQUENESS 500
  565. #define V1_ANY_UNIQUENESS 555 // FIXME: comment is required
  566. //
  567. // here are conversion routines
  568. //
  569. static inline int uniqueness2type(__u32 uniqueness) CONSTF;
  570. static inline int uniqueness2type(__u32 uniqueness)
  571. {
  572. switch ((int)uniqueness) {
  573. case V1_SD_UNIQUENESS:
  574. return TYPE_STAT_DATA;
  575. case V1_INDIRECT_UNIQUENESS:
  576. return TYPE_INDIRECT;
  577. case V1_DIRECT_UNIQUENESS:
  578. return TYPE_DIRECT;
  579. case V1_DIRENTRY_UNIQUENESS:
  580. return TYPE_DIRENTRY;
  581. case V1_ANY_UNIQUENESS:
  582. default:
  583. return TYPE_ANY;
  584. }
  585. }
  586. static inline __u32 type2uniqueness(int type) CONSTF;
  587. static inline __u32 type2uniqueness(int type)
  588. {
  589. switch (type) {
  590. case TYPE_STAT_DATA:
  591. return V1_SD_UNIQUENESS;
  592. case TYPE_INDIRECT:
  593. return V1_INDIRECT_UNIQUENESS;
  594. case TYPE_DIRECT:
  595. return V1_DIRECT_UNIQUENESS;
  596. case TYPE_DIRENTRY:
  597. return V1_DIRENTRY_UNIQUENESS;
  598. case TYPE_ANY:
  599. default:
  600. return V1_ANY_UNIQUENESS;
  601. }
  602. }
  603. //
  604. // key is pointer to on disk key which is stored in le, result is cpu,
  605. // there is no way to get version of object from key, so, provide
  606. // version to these defines
  607. //
  608. static inline loff_t le_key_k_offset(int version,
  609. const struct reiserfs_key *key)
  610. {
  611. return (version == KEY_FORMAT_3_5) ?
  612. le32_to_cpu(key->u.k_offset_v1.k_offset) :
  613. offset_v2_k_offset(&(key->u.k_offset_v2));
  614. }
  615. static inline loff_t le_ih_k_offset(const struct item_head *ih)
  616. {
  617. return le_key_k_offset(ih_version(ih), &(ih->ih_key));
  618. }
  619. static inline loff_t le_key_k_type(int version, const struct reiserfs_key *key)
  620. {
  621. return (version == KEY_FORMAT_3_5) ?
  622. uniqueness2type(le32_to_cpu(key->u.k_offset_v1.k_uniqueness)) :
  623. offset_v2_k_type(&(key->u.k_offset_v2));
  624. }
  625. static inline loff_t le_ih_k_type(const struct item_head *ih)
  626. {
  627. return le_key_k_type(ih_version(ih), &(ih->ih_key));
  628. }
  629. static inline void set_le_key_k_offset(int version, struct reiserfs_key *key,
  630. loff_t offset)
  631. {
  632. (version == KEY_FORMAT_3_5) ? (void)(key->u.k_offset_v1.k_offset = cpu_to_le32(offset)) : /* jdm check */
  633. (void)(set_offset_v2_k_offset(&(key->u.k_offset_v2), offset));
  634. }
  635. static inline void set_le_ih_k_offset(struct item_head *ih, loff_t offset)
  636. {
  637. set_le_key_k_offset(ih_version(ih), &(ih->ih_key), offset);
  638. }
  639. static inline void set_le_key_k_type(int version, struct reiserfs_key *key,
  640. int type)
  641. {
  642. (version == KEY_FORMAT_3_5) ?
  643. (void)(key->u.k_offset_v1.k_uniqueness =
  644. cpu_to_le32(type2uniqueness(type)))
  645. : (void)(set_offset_v2_k_type(&(key->u.k_offset_v2), type));
  646. }
  647. static inline void set_le_ih_k_type(struct item_head *ih, int type)
  648. {
  649. set_le_key_k_type(ih_version(ih), &(ih->ih_key), type);
  650. }
  651. static inline int is_direntry_le_key(int version, struct reiserfs_key *key)
  652. {
  653. return le_key_k_type(version, key) == TYPE_DIRENTRY;
  654. }
  655. static inline int is_direct_le_key(int version, struct reiserfs_key *key)
  656. {
  657. return le_key_k_type(version, key) == TYPE_DIRECT;
  658. }
  659. static inline int is_indirect_le_key(int version, struct reiserfs_key *key)
  660. {
  661. return le_key_k_type(version, key) == TYPE_INDIRECT;
  662. }
  663. static inline int is_statdata_le_key(int version, struct reiserfs_key *key)
  664. {
  665. return le_key_k_type(version, key) == TYPE_STAT_DATA;
  666. }
  667. //
  668. // item header has version.
  669. //
  670. static inline int is_direntry_le_ih(struct item_head *ih)
  671. {
  672. return is_direntry_le_key(ih_version(ih), &ih->ih_key);
  673. }
  674. static inline int is_direct_le_ih(struct item_head *ih)
  675. {
  676. return is_direct_le_key(ih_version(ih), &ih->ih_key);
  677. }
  678. static inline int is_indirect_le_ih(struct item_head *ih)
  679. {
  680. return is_indirect_le_key(ih_version(ih), &ih->ih_key);
  681. }
  682. static inline int is_statdata_le_ih(struct item_head *ih)
  683. {
  684. return is_statdata_le_key(ih_version(ih), &ih->ih_key);
  685. }
  686. //
  687. // key is pointer to cpu key, result is cpu
  688. //
  689. static inline loff_t cpu_key_k_offset(const struct cpu_key *key)
  690. {
  691. return key->on_disk_key.k_offset;
  692. }
  693. static inline loff_t cpu_key_k_type(const struct cpu_key *key)
  694. {
  695. return key->on_disk_key.k_type;
  696. }
  697. static inline void set_cpu_key_k_offset(struct cpu_key *key, loff_t offset)
  698. {
  699. key->on_disk_key.k_offset = offset;
  700. }
  701. static inline void set_cpu_key_k_type(struct cpu_key *key, int type)
  702. {
  703. key->on_disk_key.k_type = type;
  704. }
  705. static inline void cpu_key_k_offset_dec(struct cpu_key *key)
  706. {
  707. key->on_disk_key.k_offset--;
  708. }
  709. #define is_direntry_cpu_key(key) (cpu_key_k_type (key) == TYPE_DIRENTRY)
  710. #define is_direct_cpu_key(key) (cpu_key_k_type (key) == TYPE_DIRECT)
  711. #define is_indirect_cpu_key(key) (cpu_key_k_type (key) == TYPE_INDIRECT)
  712. #define is_statdata_cpu_key(key) (cpu_key_k_type (key) == TYPE_STAT_DATA)
  713. /* are these used ? */
  714. #define is_direntry_cpu_ih(ih) (is_direntry_cpu_key (&((ih)->ih_key)))
  715. #define is_direct_cpu_ih(ih) (is_direct_cpu_key (&((ih)->ih_key)))
  716. #define is_indirect_cpu_ih(ih) (is_indirect_cpu_key (&((ih)->ih_key)))
  717. #define is_statdata_cpu_ih(ih) (is_statdata_cpu_key (&((ih)->ih_key)))
  718. #define I_K_KEY_IN_ITEM(ih, key, n_blocksize) \
  719. (!COMP_SHORT_KEYS(ih, key) && \
  720. I_OFF_BYTE_IN_ITEM(ih, k_offset(key), n_blocksize))
  721. /* maximal length of item */
  722. #define MAX_ITEM_LEN(block_size) (block_size - BLKH_SIZE - IH_SIZE)
  723. #define MIN_ITEM_LEN 1
  724. /* object identifier for root dir */
  725. #define REISERFS_ROOT_OBJECTID 2
  726. #define REISERFS_ROOT_PARENT_OBJECTID 1
  727. extern struct reiserfs_key root_key;
  728. /*
  729. * Picture represents a leaf of the S+tree
  730. * ______________________________________________________
  731. * | | Array of | | |
  732. * |Block | Object-Item | F r e e | Objects- |
  733. * | head | Headers | S p a c e | Items |
  734. * |______|_______________|___________________|___________|
  735. */
  736. /* Header of a disk block. More precisely, header of a formatted leaf
  737. or internal node, and not the header of an unformatted node. */
  738. struct block_head {
  739. __le16 blk_level; /* Level of a block in the tree. */
  740. __le16 blk_nr_item; /* Number of keys/items in a block. */
  741. __le16 blk_free_space; /* Block free space in bytes. */
  742. __le16 blk_reserved;
  743. /* dump this in v4/planA */
  744. struct reiserfs_key blk_right_delim_key; /* kept only for compatibility */
  745. };
  746. #define BLKH_SIZE (sizeof(struct block_head))
  747. #define blkh_level(p_blkh) (le16_to_cpu((p_blkh)->blk_level))
  748. #define blkh_nr_item(p_blkh) (le16_to_cpu((p_blkh)->blk_nr_item))
  749. #define blkh_free_space(p_blkh) (le16_to_cpu((p_blkh)->blk_free_space))
  750. #define blkh_reserved(p_blkh) (le16_to_cpu((p_blkh)->blk_reserved))
  751. #define set_blkh_level(p_blkh,val) ((p_blkh)->blk_level = cpu_to_le16(val))
  752. #define set_blkh_nr_item(p_blkh,val) ((p_blkh)->blk_nr_item = cpu_to_le16(val))
  753. #define set_blkh_free_space(p_blkh,val) ((p_blkh)->blk_free_space = cpu_to_le16(val))
  754. #define set_blkh_reserved(p_blkh,val) ((p_blkh)->blk_reserved = cpu_to_le16(val))
  755. #define blkh_right_delim_key(p_blkh) ((p_blkh)->blk_right_delim_key)
  756. #define set_blkh_right_delim_key(p_blkh,val) ((p_blkh)->blk_right_delim_key = val)
  757. /*
  758. * values for blk_level field of the struct block_head
  759. */
  760. #define FREE_LEVEL 0 /* when node gets removed from the tree its
  761. blk_level is set to FREE_LEVEL. It is then
  762. used to see whether the node is still in the
  763. tree */
  764. #define DISK_LEAF_NODE_LEVEL 1 /* Leaf node level. */
  765. /* Given the buffer head of a formatted node, resolve to the block head of that node. */
  766. #define B_BLK_HEAD(bh) ((struct block_head *)((bh)->b_data))
  767. /* Number of items that are in buffer. */
  768. #define B_NR_ITEMS(bh) (blkh_nr_item(B_BLK_HEAD(bh)))
  769. #define B_LEVEL(bh) (blkh_level(B_BLK_HEAD(bh)))
  770. #define B_FREE_SPACE(bh) (blkh_free_space(B_BLK_HEAD(bh)))
  771. #define PUT_B_NR_ITEMS(bh, val) do { set_blkh_nr_item(B_BLK_HEAD(bh), val); } while (0)
  772. #define PUT_B_LEVEL(bh, val) do { set_blkh_level(B_BLK_HEAD(bh), val); } while (0)
  773. #define PUT_B_FREE_SPACE(bh, val) do { set_blkh_free_space(B_BLK_HEAD(bh), val); } while (0)
  774. /* Get right delimiting key. -- little endian */
  775. #define B_PRIGHT_DELIM_KEY(bh) (&(blk_right_delim_key(B_BLK_HEAD(bh))))
  776. /* Does the buffer contain a disk leaf. */
  777. #define B_IS_ITEMS_LEVEL(bh) (B_LEVEL(bh) == DISK_LEAF_NODE_LEVEL)
  778. /* Does the buffer contain a disk internal node */
  779. #define B_IS_KEYS_LEVEL(bh) (B_LEVEL(bh) > DISK_LEAF_NODE_LEVEL \
  780. && B_LEVEL(bh) <= MAX_HEIGHT)
  781. /***************************************************************************/
  782. /* STAT DATA */
  783. /***************************************************************************/
  784. //
  785. // old stat data is 32 bytes long. We are going to distinguish new one by
  786. // different size
  787. //
  788. struct stat_data_v1 {
  789. __le16 sd_mode; /* file type, permissions */
  790. __le16 sd_nlink; /* number of hard links */
  791. __le16 sd_uid; /* owner */
  792. __le16 sd_gid; /* group */
  793. __le32 sd_size; /* file size */
  794. __le32 sd_atime; /* time of last access */
  795. __le32 sd_mtime; /* time file was last modified */
  796. __le32 sd_ctime; /* time inode (stat data) was last changed (except changes to sd_atime and sd_mtime) */
  797. union {
  798. __le32 sd_rdev;
  799. __le32 sd_blocks; /* number of blocks file uses */
  800. } __attribute__ ((__packed__)) u;
  801. __le32 sd_first_direct_byte; /* first byte of file which is stored
  802. in a direct item: except that if it
  803. equals 1 it is a symlink and if it
  804. equals ~(__u32)0 there is no
  805. direct item. The existence of this
  806. field really grates on me. Let's
  807. replace it with a macro based on
  808. sd_size and our tail suppression
  809. policy. Someday. -Hans */
  810. } __attribute__ ((__packed__));
  811. #define SD_V1_SIZE (sizeof(struct stat_data_v1))
  812. #define stat_data_v1(ih) (ih_version (ih) == KEY_FORMAT_3_5)
  813. #define sd_v1_mode(sdp) (le16_to_cpu((sdp)->sd_mode))
  814. #define set_sd_v1_mode(sdp,v) ((sdp)->sd_mode = cpu_to_le16(v))
  815. #define sd_v1_nlink(sdp) (le16_to_cpu((sdp)->sd_nlink))
  816. #define set_sd_v1_nlink(sdp,v) ((sdp)->sd_nlink = cpu_to_le16(v))
  817. #define sd_v1_uid(sdp) (le16_to_cpu((sdp)->sd_uid))
  818. #define set_sd_v1_uid(sdp,v) ((sdp)->sd_uid = cpu_to_le16(v))
  819. #define sd_v1_gid(sdp) (le16_to_cpu((sdp)->sd_gid))
  820. #define set_sd_v1_gid(sdp,v) ((sdp)->sd_gid = cpu_to_le16(v))
  821. #define sd_v1_size(sdp) (le32_to_cpu((sdp)->sd_size))
  822. #define set_sd_v1_size(sdp,v) ((sdp)->sd_size = cpu_to_le32(v))
  823. #define sd_v1_atime(sdp) (le32_to_cpu((sdp)->sd_atime))
  824. #define set_sd_v1_atime(sdp,v) ((sdp)->sd_atime = cpu_to_le32(v))
  825. #define sd_v1_mtime(sdp) (le32_to_cpu((sdp)->sd_mtime))
  826. #define set_sd_v1_mtime(sdp,v) ((sdp)->sd_mtime = cpu_to_le32(v))
  827. #define sd_v1_ctime(sdp) (le32_to_cpu((sdp)->sd_ctime))
  828. #define set_sd_v1_ctime(sdp,v) ((sdp)->sd_ctime = cpu_to_le32(v))
  829. #define sd_v1_rdev(sdp) (le32_to_cpu((sdp)->u.sd_rdev))
  830. #define set_sd_v1_rdev(sdp,v) ((sdp)->u.sd_rdev = cpu_to_le32(v))
  831. #define sd_v1_blocks(sdp) (le32_to_cpu((sdp)->u.sd_blocks))
  832. #define set_sd_v1_blocks(sdp,v) ((sdp)->u.sd_blocks = cpu_to_le32(v))
  833. #define sd_v1_first_direct_byte(sdp) \
  834. (le32_to_cpu((sdp)->sd_first_direct_byte))
  835. #define set_sd_v1_first_direct_byte(sdp,v) \
  836. ((sdp)->sd_first_direct_byte = cpu_to_le32(v))
  837. /* inode flags stored in sd_attrs (nee sd_reserved) */
  838. /* we want common flags to have the same values as in ext2,
  839. so chattr(1) will work without problems */
  840. #define REISERFS_IMMUTABLE_FL FS_IMMUTABLE_FL
  841. #define REISERFS_APPEND_FL FS_APPEND_FL
  842. #define REISERFS_SYNC_FL FS_SYNC_FL
  843. #define REISERFS_NOATIME_FL FS_NOATIME_FL
  844. #define REISERFS_NODUMP_FL FS_NODUMP_FL
  845. #define REISERFS_SECRM_FL FS_SECRM_FL
  846. #define REISERFS_UNRM_FL FS_UNRM_FL
  847. #define REISERFS_COMPR_FL FS_COMPR_FL
  848. #define REISERFS_NOTAIL_FL FS_NOTAIL_FL
  849. /* persistent flags that file inherits from the parent directory */
  850. #define REISERFS_INHERIT_MASK ( REISERFS_IMMUTABLE_FL | \
  851. REISERFS_SYNC_FL | \
  852. REISERFS_NOATIME_FL | \
  853. REISERFS_NODUMP_FL | \
  854. REISERFS_SECRM_FL | \
  855. REISERFS_COMPR_FL | \
  856. REISERFS_NOTAIL_FL )
  857. /* Stat Data on disk (reiserfs version of UFS disk inode minus the
  858. address blocks) */
  859. struct stat_data {
  860. __le16 sd_mode; /* file type, permissions */
  861. __le16 sd_attrs; /* persistent inode flags */
  862. __le32 sd_nlink; /* number of hard links */
  863. __le64 sd_size; /* file size */
  864. __le32 sd_uid; /* owner */
  865. __le32 sd_gid; /* group */
  866. __le32 sd_atime; /* time of last access */
  867. __le32 sd_mtime; /* time file was last modified */
  868. __le32 sd_ctime; /* time inode (stat data) was last changed (except changes to sd_atime and sd_mtime) */
  869. __le32 sd_blocks;
  870. union {
  871. __le32 sd_rdev;
  872. __le32 sd_generation;
  873. //__le32 sd_first_direct_byte;
  874. /* first byte of file which is stored in a
  875. direct item: except that if it equals 1
  876. it is a symlink and if it equals
  877. ~(__u32)0 there is no direct item. The
  878. existence of this field really grates
  879. on me. Let's replace it with a macro
  880. based on sd_size and our tail
  881. suppression policy? */
  882. } __attribute__ ((__packed__)) u;
  883. } __attribute__ ((__packed__));
  884. //
  885. // this is 44 bytes long
  886. //
  887. #define SD_SIZE (sizeof(struct stat_data))
  888. #define SD_V2_SIZE SD_SIZE
  889. #define stat_data_v2(ih) (ih_version (ih) == KEY_FORMAT_3_6)
  890. #define sd_v2_mode(sdp) (le16_to_cpu((sdp)->sd_mode))
  891. #define set_sd_v2_mode(sdp,v) ((sdp)->sd_mode = cpu_to_le16(v))
  892. /* sd_reserved */
  893. /* set_sd_reserved */
  894. #define sd_v2_nlink(sdp) (le32_to_cpu((sdp)->sd_nlink))
  895. #define set_sd_v2_nlink(sdp,v) ((sdp)->sd_nlink = cpu_to_le32(v))
  896. #define sd_v2_size(sdp) (le64_to_cpu((sdp)->sd_size))
  897. #define set_sd_v2_size(sdp,v) ((sdp)->sd_size = cpu_to_le64(v))
  898. #define sd_v2_uid(sdp) (le32_to_cpu((sdp)->sd_uid))
  899. #define set_sd_v2_uid(sdp,v) ((sdp)->sd_uid = cpu_to_le32(v))
  900. #define sd_v2_gid(sdp) (le32_to_cpu((sdp)->sd_gid))
  901. #define set_sd_v2_gid(sdp,v) ((sdp)->sd_gid = cpu_to_le32(v))
  902. #define sd_v2_atime(sdp) (le32_to_cpu((sdp)->sd_atime))
  903. #define set_sd_v2_atime(sdp,v) ((sdp)->sd_atime = cpu_to_le32(v))
  904. #define sd_v2_mtime(sdp) (le32_to_cpu((sdp)->sd_mtime))
  905. #define set_sd_v2_mtime(sdp,v) ((sdp)->sd_mtime = cpu_to_le32(v))
  906. #define sd_v2_ctime(sdp) (le32_to_cpu((sdp)->sd_ctime))
  907. #define set_sd_v2_ctime(sdp,v) ((sdp)->sd_ctime = cpu_to_le32(v))
  908. #define sd_v2_blocks(sdp) (le32_to_cpu((sdp)->sd_blocks))
  909. #define set_sd_v2_blocks(sdp,v) ((sdp)->sd_blocks = cpu_to_le32(v))
  910. #define sd_v2_rdev(sdp) (le32_to_cpu((sdp)->u.sd_rdev))
  911. #define set_sd_v2_rdev(sdp,v) ((sdp)->u.sd_rdev = cpu_to_le32(v))
  912. #define sd_v2_generation(sdp) (le32_to_cpu((sdp)->u.sd_generation))
  913. #define set_sd_v2_generation(sdp,v) ((sdp)->u.sd_generation = cpu_to_le32(v))
  914. #define sd_v2_attrs(sdp) (le16_to_cpu((sdp)->sd_attrs))
  915. #define set_sd_v2_attrs(sdp,v) ((sdp)->sd_attrs = cpu_to_le16(v))
  916. /***************************************************************************/
  917. /* DIRECTORY STRUCTURE */
  918. /***************************************************************************/
  919. /*
  920. Picture represents the structure of directory items
  921. ________________________________________________
  922. | Array of | | | | | |
  923. | directory |N-1| N-2 | .... | 1st |0th|
  924. | entry headers | | | | | |
  925. |_______________|___|_____|________|_______|___|
  926. <---- directory entries ------>
  927. First directory item has k_offset component 1. We store "." and ".."
  928. in one item, always, we never split "." and ".." into differing
  929. items. This makes, among other things, the code for removing
  930. directories simpler. */
  931. #define SD_OFFSET 0
  932. #define SD_UNIQUENESS 0
  933. #define DOT_OFFSET 1
  934. #define DOT_DOT_OFFSET 2
  935. #define DIRENTRY_UNIQUENESS 500
  936. /* */
  937. #define FIRST_ITEM_OFFSET 1
  938. /*
  939. Q: How to get key of object pointed to by entry from entry?
  940. A: Each directory entry has its header. This header has deh_dir_id and deh_objectid fields, those are key
  941. of object, entry points to */
  942. /* NOT IMPLEMENTED:
  943. Directory will someday contain stat data of object */
  944. struct reiserfs_de_head {
  945. __le32 deh_offset; /* third component of the directory entry key */
  946. __le32 deh_dir_id; /* objectid of the parent directory of the object, that is referenced
  947. by directory entry */
  948. __le32 deh_objectid; /* objectid of the object, that is referenced by directory entry */
  949. __le16 deh_location; /* offset of name in the whole item */
  950. __le16 deh_state; /* whether 1) entry contains stat data (for future), and 2) whether
  951. entry is hidden (unlinked) */
  952. } __attribute__ ((__packed__));
  953. #define DEH_SIZE sizeof(struct reiserfs_de_head)
  954. #define deh_offset(p_deh) (le32_to_cpu((p_deh)->deh_offset))
  955. #define deh_dir_id(p_deh) (le32_to_cpu((p_deh)->deh_dir_id))
  956. #define deh_objectid(p_deh) (le32_to_cpu((p_deh)->deh_objectid))
  957. #define deh_location(p_deh) (le16_to_cpu((p_deh)->deh_location))
  958. #define deh_state(p_deh) (le16_to_cpu((p_deh)->deh_state))
  959. #define put_deh_offset(p_deh,v) ((p_deh)->deh_offset = cpu_to_le32((v)))
  960. #define put_deh_dir_id(p_deh,v) ((p_deh)->deh_dir_id = cpu_to_le32((v)))
  961. #define put_deh_objectid(p_deh,v) ((p_deh)->deh_objectid = cpu_to_le32((v)))
  962. #define put_deh_location(p_deh,v) ((p_deh)->deh_location = cpu_to_le16((v)))
  963. #define put_deh_state(p_deh,v) ((p_deh)->deh_state = cpu_to_le16((v)))
  964. /* empty directory contains two entries "." and ".." and their headers */
  965. #define EMPTY_DIR_SIZE \
  966. (DEH_SIZE * 2 + ROUND_UP (strlen (".")) + ROUND_UP (strlen ("..")))
  967. /* old format directories have this size when empty */
  968. #define EMPTY_DIR_SIZE_V1 (DEH_SIZE * 2 + 3)
  969. #define DEH_Statdata 0 /* not used now */
  970. #define DEH_Visible 2
  971. /* 64 bit systems (and the S/390) need to be aligned explicitly -jdm */
  972. #if BITS_PER_LONG == 64 || defined(__s390__) || defined(__hppa__)
  973. # define ADDR_UNALIGNED_BITS (3)
  974. #endif
  975. /* These are only used to manipulate deh_state.
  976. * Because of this, we'll use the ext2_ bit routines,
  977. * since they are little endian */
  978. #ifdef ADDR_UNALIGNED_BITS
  979. # define aligned_address(addr) ((void *)((long)(addr) & ~((1UL << ADDR_UNALIGNED_BITS) - 1)))
  980. # define unaligned_offset(addr) (((int)((long)(addr) & ((1 << ADDR_UNALIGNED_BITS) - 1))) << 3)
  981. # define set_bit_unaligned(nr, addr) \
  982. __test_and_set_bit_le((nr) + unaligned_offset(addr), aligned_address(addr))
  983. # define clear_bit_unaligned(nr, addr) \
  984. __test_and_clear_bit_le((nr) + unaligned_offset(addr), aligned_address(addr))
  985. # define test_bit_unaligned(nr, addr) \
  986. test_bit_le((nr) + unaligned_offset(addr), aligned_address(addr))
  987. #else
  988. # define set_bit_unaligned(nr, addr) __test_and_set_bit_le(nr, addr)
  989. # define clear_bit_unaligned(nr, addr) __test_and_clear_bit_le(nr, addr)
  990. # define test_bit_unaligned(nr, addr) test_bit_le(nr, addr)
  991. #endif
  992. #define mark_de_with_sd(deh) set_bit_unaligned (DEH_Statdata, &((deh)->deh_state))
  993. #define mark_de_without_sd(deh) clear_bit_unaligned (DEH_Statdata, &((deh)->deh_state))
  994. #define mark_de_visible(deh) set_bit_unaligned (DEH_Visible, &((deh)->deh_state))
  995. #define mark_de_hidden(deh) clear_bit_unaligned (DEH_Visible, &((deh)->deh_state))
  996. #define de_with_sd(deh) test_bit_unaligned (DEH_Statdata, &((deh)->deh_state))
  997. #define de_visible(deh) test_bit_unaligned (DEH_Visible, &((deh)->deh_state))
  998. #define de_hidden(deh) !test_bit_unaligned (DEH_Visible, &((deh)->deh_state))
  999. extern void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
  1000. __le32 par_dirid, __le32 par_objid);
  1001. extern void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
  1002. __le32 par_dirid, __le32 par_objid);
  1003. /* array of the entry headers */
  1004. /* get item body */
  1005. #define B_I_PITEM(bh,ih) ( (bh)->b_data + ih_location(ih) )
  1006. #define B_I_DEH(bh,ih) ((struct reiserfs_de_head *)(B_I_PITEM(bh,ih)))
  1007. /* length of the directory entry in directory item. This define
  1008. calculates length of i-th directory entry using directory entry
  1009. locations from dir entry head. When it calculates length of 0-th
  1010. directory entry, it uses length of whole item in place of entry
  1011. location of the non-existent following entry in the calculation.
  1012. See picture above.*/
  1013. /*
  1014. #define I_DEH_N_ENTRY_LENGTH(ih,deh,i) \
  1015. ((i) ? (deh_location((deh)-1) - deh_location((deh))) : (ih_item_len((ih)) - deh_location((deh))))
  1016. */
  1017. static inline int entry_length(const struct buffer_head *bh,
  1018. const struct item_head *ih, int pos_in_item)
  1019. {
  1020. struct reiserfs_de_head *deh;
  1021. deh = B_I_DEH(bh, ih) + pos_in_item;
  1022. if (pos_in_item)
  1023. return deh_location(deh - 1) - deh_location(deh);
  1024. return ih_item_len(ih) - deh_location(deh);
  1025. }
  1026. /* number of entries in the directory item, depends on ENTRY_COUNT being at the start of directory dynamic data. */
  1027. #define I_ENTRY_COUNT(ih) (ih_entry_count((ih)))
  1028. /* name by bh, ih and entry_num */
  1029. #define B_I_E_NAME(bh,ih,entry_num) ((char *)(bh->b_data + ih_location(ih) + deh_location(B_I_DEH(bh,ih)+(entry_num))))
  1030. // two entries per block (at least)
  1031. #define REISERFS_MAX_NAME(block_size) 255
  1032. /* this structure is used for operations on directory entries. It is
  1033. not a disk structure. */
  1034. /* When reiserfs_find_entry or search_by_entry_key find directory
  1035. entry, they return filled reiserfs_dir_entry structure */
  1036. struct reiserfs_dir_entry {
  1037. struct buffer_head *de_bh;
  1038. int de_item_num;
  1039. struct item_head *de_ih;
  1040. int de_entry_num;
  1041. struct reiserfs_de_head *de_deh;
  1042. int de_entrylen;
  1043. int de_namelen;
  1044. char *de_name;
  1045. unsigned long *de_gen_number_bit_string;
  1046. __u32 de_dir_id;
  1047. __u32 de_objectid;
  1048. struct cpu_key de_entry_key;
  1049. };
  1050. /* these defines are useful when a particular member of a reiserfs_dir_entry is needed */
  1051. /* pointer to file name, stored in entry */
  1052. #define B_I_DEH_ENTRY_FILE_NAME(bh,ih,deh) (B_I_PITEM (bh, ih) + deh_location(deh))
  1053. /* length of name */
  1054. #define I_DEH_N_ENTRY_FILE_NAME_LENGTH(ih,deh,entry_num) \
  1055. (I_DEH_N_ENTRY_LENGTH (ih, deh, entry_num) - (de_with_sd (deh) ? SD_SIZE : 0))
  1056. /* hash value occupies bits from 7 up to 30 */
  1057. #define GET_HASH_VALUE(offset) ((offset) & 0x7fffff80LL)
  1058. /* generation number occupies 7 bits starting from 0 up to 6 */
  1059. #define GET_GENERATION_NUMBER(offset) ((offset) & 0x7fLL)
  1060. #define MAX_GENERATION_NUMBER 127
  1061. #define SET_GENERATION_NUMBER(offset,gen_number) (GET_HASH_VALUE(offset)|(gen_number))
  1062. /*
  1063. * Picture represents an internal node of the reiserfs tree
  1064. * ______________________________________________________
  1065. * | | Array of | Array of | Free |
  1066. * |block | keys | pointers | space |
  1067. * | head | N | N+1 | |
  1068. * |______|_______________|___________________|___________|
  1069. */
  1070. /***************************************************************************/
  1071. /* DISK CHILD */
  1072. /***************************************************************************/
  1073. /* Disk child pointer: The pointer from an internal node of the tree
  1074. to a node that is on disk. */
  1075. struct disk_child {
  1076. __le32 dc_block_number; /* Disk child's block number. */
  1077. __le16 dc_size; /* Disk child's used space. */
  1078. __le16 dc_reserved;
  1079. };
  1080. #define DC_SIZE (sizeof(struct disk_child))
  1081. #define dc_block_number(dc_p) (le32_to_cpu((dc_p)->dc_block_number))
  1082. #define dc_size(dc_p) (le16_to_cpu((dc_p)->dc_size))
  1083. #define put_dc_block_number(dc_p, val) do { (dc_p)->dc_block_number = cpu_to_le32(val); } while(0)
  1084. #define put_dc_size(dc_p, val) do { (dc_p)->dc_size = cpu_to_le16(val); } while(0)
  1085. /* Get disk child by buffer header and position in the tree node. */
  1086. #define B_N_CHILD(bh, n_pos) ((struct disk_child *)\
  1087. ((bh)->b_data + BLKH_SIZE + B_NR_ITEMS(bh) * KEY_SIZE + DC_SIZE * (n_pos)))
  1088. /* Get disk child number by buffer header and position in the tree node. */
  1089. #define B_N_CHILD_NUM(bh, n_pos) (dc_block_number(B_N_CHILD(bh, n_pos)))
  1090. #define PUT_B_N_CHILD_NUM(bh, n_pos, val) \
  1091. (put_dc_block_number(B_N_CHILD(bh, n_pos), val))
  1092. /* maximal value of field child_size in structure disk_child */
  1093. /* child size is the combined size of all items and their headers */
  1094. #define MAX_CHILD_SIZE(bh) ((int)( (bh)->b_size - BLKH_SIZE ))
  1095. /* amount of used space in buffer (not including block head) */
  1096. #define B_CHILD_SIZE(cur) (MAX_CHILD_SIZE(cur)-(B_FREE_SPACE(cur)))
  1097. /* max and min number of keys in internal node */
  1098. #define MAX_NR_KEY(bh) ( (MAX_CHILD_SIZE(bh)-DC_SIZE)/(KEY_SIZE+DC_SIZE) )
  1099. #define MIN_NR_KEY(bh) (MAX_NR_KEY(bh)/2)
  1100. /***************************************************************************/
  1101. /* PATH STRUCTURES AND DEFINES */
  1102. /***************************************************************************/
  1103. /* Search_by_key fills up the path from the root to the leaf as it descends the tree looking for the
  1104. key. It uses reiserfs_bread to try to find buffers in the cache given their block number. If it
  1105. does not find them in the cache it reads them from disk. For each node search_by_key finds using
  1106. reiserfs_bread it then uses bin_search to look through that node. bin_search will find the
  1107. position of the block_number of the next node if it is looking through an internal node. If it
  1108. is looking through a leaf node bin_search will find the position of the item which has key either
  1109. equal to given key, or which is the maximal key less than the given key. */
  1110. struct path_element {
  1111. struct buffer_head *pe_buffer; /* Pointer to the buffer at the path in the tree. */
  1112. int pe_position; /* Position in the tree node which is placed in the */
  1113. /* buffer above. */
  1114. };
  1115. #define MAX_HEIGHT 5 /* maximal height of a tree. don't change this without changing JOURNAL_PER_BALANCE_CNT */
  1116. #define EXTENDED_MAX_HEIGHT 7 /* Must be equals MAX_HEIGHT + FIRST_PATH_ELEMENT_OFFSET */
  1117. #define FIRST_PATH_ELEMENT_OFFSET 2 /* Must be equal to at least 2. */
  1118. #define ILLEGAL_PATH_ELEMENT_OFFSET 1 /* Must be equal to FIRST_PATH_ELEMENT_OFFSET - 1 */
  1119. #define MAX_FEB_SIZE 6 /* this MUST be MAX_HEIGHT + 1. See about FEB below */
  1120. /* We need to keep track of who the ancestors of nodes are. When we
  1121. perform a search we record which nodes were visited while
  1122. descending the tree looking for the node we searched for. This list
  1123. of nodes is called the path. This information is used while
  1124. performing balancing. Note that this path information may become
  1125. invalid, and this means we must check it when using it to see if it
  1126. is still valid. You'll need to read search_by_key and the comments
  1127. in it, especially about decrement_counters_in_path(), to understand
  1128. this structure.
  1129. Paths make the code