xfs_dir2_format.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /*
  2. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #ifndef __XFS_DIR2_FORMAT_H__
  20. #define __XFS_DIR2_FORMAT_H__
  21. /*
  22. * Directory version 2.
  23. *
  24. * There are 4 possible formats:
  25. * - shortform - embedded into the inode
  26. * - single block - data with embedded leaf at the end
  27. * - multiple data blocks, single leaf+freeindex block
  28. * - data blocks, node and leaf blocks (btree), freeindex blocks
  29. *
  30. * Note: many node blocks structures and constants are shared with the attr
  31. * code and defined in xfs_da_btree.h.
  32. */
  33. #define XFS_DIR2_BLOCK_MAGIC 0x58443242 /* XD2B: single block dirs */
  34. #define XFS_DIR2_DATA_MAGIC 0x58443244 /* XD2D: multiblock dirs */
  35. #define XFS_DIR2_FREE_MAGIC 0x58443246 /* XD2F: free index blocks */
  36. /*
  37. * Directory Version 3 With CRCs.
  38. *
  39. * The tree formats are the same as for version 2 directories. The difference
  40. * is in the block header and dirent formats. In many cases the v3 structures
  41. * use v2 definitions as they are no different and this makes code sharing much
  42. * easier.
  43. *
  44. * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
  45. * format is v2 then they switch to the existing v2 code, or the format is v3
  46. * they implement the v3 functionality. This means the existing dir2 is a mix of
  47. * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
  48. * where there is a difference in the formats, otherwise the code is unchanged.
  49. *
  50. * Where it is possible, the code decides what to do based on the magic numbers
  51. * in the blocks rather than feature bits in the superblock. This means the code
  52. * is as independent of the external XFS code as possible as doesn't require
  53. * passing struct xfs_mount pointers into places where it isn't really
  54. * necessary.
  55. *
  56. * Version 3 includes:
  57. *
  58. * - a larger block header for CRC and identification purposes and so the
  59. * offsets of all the structures inside the blocks are different.
  60. *
  61. * - new magic numbers to be able to detect the v2/v3 types on the fly.
  62. */
  63. #define XFS_DIR3_BLOCK_MAGIC 0x58444233 /* XDB3: single block dirs */
  64. #define XFS_DIR3_DATA_MAGIC 0x58444433 /* XDD3: multiblock dirs */
  65. #define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */
  66. /*
  67. * Byte offset in data block and shortform entry.
  68. */
  69. typedef __uint16_t xfs_dir2_data_off_t;
  70. #define NULLDATAOFF 0xffffU
  71. typedef uint xfs_dir2_data_aoff_t; /* argument form */
  72. /*
  73. * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
  74. * Only need 16 bits, this is the byte offset into the single block form.
  75. */
  76. typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
  77. /*
  78. * Offset in data space of a data entry.
  79. */
  80. typedef __uint32_t xfs_dir2_dataptr_t;
  81. #define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
  82. #define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
  83. /*
  84. * Byte offset in a directory.
  85. */
  86. typedef xfs_off_t xfs_dir2_off_t;
  87. /*
  88. * Directory block number (logical dirblk in file)
  89. */
  90. typedef __uint32_t xfs_dir2_db_t;
  91. /*
  92. * Inode number stored as 8 8-bit values.
  93. */
  94. typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
  95. /*
  96. * Inode number stored as 4 8-bit values.
  97. * Works a lot of the time, when all the inode numbers in a directory
  98. * fit in 32 bits.
  99. */
  100. typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
  101. typedef union {
  102. xfs_dir2_ino8_t i8;
  103. xfs_dir2_ino4_t i4;
  104. } xfs_dir2_inou_t;
  105. #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
  106. /*
  107. * Directory layout when stored internal to an inode.
  108. *
  109. * Small directories are packed as tightly as possible so as to fit into the
  110. * literal area of the inode. These "shortform" directories consist of a
  111. * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
  112. * structures. Due the different inode number storage size and the variable
  113. * length name field in the xfs_dir2_sf_entry all these structure are
  114. * variable length, and the accessors in this file should be used to iterate
  115. * over them.
  116. */
  117. typedef struct xfs_dir2_sf_hdr {
  118. __uint8_t count; /* count of entries */
  119. __uint8_t i8count; /* count of 8-byte inode #s */
  120. xfs_dir2_inou_t parent; /* parent dir inode number */
  121. } __arch_pack xfs_dir2_sf_hdr_t;
  122. typedef struct xfs_dir2_sf_entry {
  123. __u8 namelen; /* actual name length */
  124. xfs_dir2_sf_off_t offset; /* saved offset */
  125. __u8 name[]; /* name, variable size */
  126. /*
  127. * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
  128. * variable offset after the name.
  129. */
  130. } __arch_pack xfs_dir2_sf_entry_t;
  131. static inline int xfs_dir2_sf_hdr_size(int i8count)
  132. {
  133. return sizeof(struct xfs_dir2_sf_hdr) -
  134. (i8count == 0) *
  135. (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
  136. }
  137. static inline xfs_dir2_data_aoff_t
  138. xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
  139. {
  140. return get_unaligned_be16(&sfep->offset.i);
  141. }
  142. static inline void
  143. xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
  144. {
  145. put_unaligned_be16(off, &sfep->offset.i);
  146. }
  147. static inline int
  148. xfs_dir2_sf_entsize(struct xfs_dir2_sf_hdr *hdr, int len)
  149. {
  150. return sizeof(struct xfs_dir2_sf_entry) + /* namelen + offset */
  151. len + /* name */
  152. (hdr->i8count ? /* ino */
  153. sizeof(xfs_dir2_ino8_t) :
  154. sizeof(xfs_dir2_ino4_t));
  155. }
  156. static inline struct xfs_dir2_sf_entry *
  157. xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
  158. {
  159. return (struct xfs_dir2_sf_entry *)
  160. ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
  161. }
  162. static inline struct xfs_dir2_sf_entry *
  163. xfs_dir2_sf_nextentry(struct xfs_dir2_sf_hdr *hdr,
  164. struct xfs_dir2_sf_entry *sfep)
  165. {
  166. return (struct xfs_dir2_sf_entry *)
  167. ((char *)sfep + xfs_dir2_sf_entsize(hdr, sfep->namelen));
  168. }
  169. /*
  170. * Data block structures.
  171. *
  172. * A pure data block looks like the following drawing on disk:
  173. *
  174. * +-------------------------------------------------+
  175. * | xfs_dir2_data_hdr_t |
  176. * +-------------------------------------------------+
  177. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  178. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  179. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  180. * | ... |
  181. * +-------------------------------------------------+
  182. * | unused space |
  183. * +-------------------------------------------------+
  184. *
  185. * As all the entries are variable size structures the accessors below should
  186. * be used to iterate over them.
  187. *
  188. * In addition to the pure data blocks for the data and node formats,
  189. * most structures are also used for the combined data/freespace "block"
  190. * format below.
  191. */
  192. #define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
  193. #define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
  194. #define XFS_DIR2_DATA_FREE_TAG 0xffff
  195. #define XFS_DIR2_DATA_FD_COUNT 3
  196. /*
  197. * Directory address space divided into sections,
  198. * spaces separated by 32GB.
  199. */
  200. #define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
  201. #define XFS_DIR2_DATA_SPACE 0
  202. #define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
  203. #define XFS_DIR2_DATA_FIRSTDB(mp) \
  204. xfs_dir2_byte_to_db(mp, XFS_DIR2_DATA_OFFSET)
  205. /*
  206. * Offsets of . and .. in data space (always block 0)
  207. */
  208. #define XFS_DIR2_DATA_DOT_OFFSET \
  209. ((xfs_dir2_data_aoff_t)sizeof(struct xfs_dir2_data_hdr))
  210. #define XFS_DIR2_DATA_DOTDOT_OFFSET \
  211. (XFS_DIR2_DATA_DOT_OFFSET + xfs_dir2_data_entsize(1))
  212. #define XFS_DIR2_DATA_FIRST_OFFSET \
  213. (XFS_DIR2_DATA_DOTDOT_OFFSET + xfs_dir2_data_entsize(2))
  214. /*
  215. * Describe a free area in the data block.
  216. *
  217. * The freespace will be formatted as a xfs_dir2_data_unused_t.
  218. */
  219. typedef struct xfs_dir2_data_free {
  220. __be16 offset; /* start of freespace */
  221. __be16 length; /* length of freespace */
  222. } xfs_dir2_data_free_t;
  223. /*
  224. * Header for the data blocks.
  225. *
  226. * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
  227. */
  228. typedef struct xfs_dir2_data_hdr {
  229. __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
  230. /* XFS_DIR2_BLOCK_MAGIC */
  231. xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
  232. } xfs_dir2_data_hdr_t;
  233. /*
  234. * define a structure for all the verification fields we are adding to the
  235. * directory block structures. This will be used in several structures.
  236. * The magic number must be the first entry to align with all the dir2
  237. * structures so we determine how to decode them just by the magic number.
  238. */
  239. struct xfs_dir3_blk_hdr {
  240. __be32 magic; /* magic number */
  241. __be32 crc; /* CRC of block */
  242. __be64 blkno; /* first block of the buffer */
  243. __be64 lsn; /* sequence number of last write */
  244. uuid_t uuid; /* filesystem we belong to */
  245. __be64 owner; /* inode that owns the block */
  246. };
  247. struct xfs_dir3_data_hdr {
  248. struct xfs_dir3_blk_hdr hdr;
  249. xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
  250. };
  251. #define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
  252. static inline struct xfs_dir2_data_free *
  253. xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
  254. {
  255. if (hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  256. struct xfs_dir3_data_hdr *hdr3 = (struct xfs_dir3_data_hdr *)hdr;
  257. return hdr3->best_free;
  258. }
  259. return hdr->bestfree;
  260. }
  261. /*
  262. * Active entry in a data block.
  263. *
  264. * Aligned to 8 bytes. After the variable length name field there is a
  265. * 2 byte tag field, which can be accessed using xfs_dir2_data_entry_tag_p.
  266. */
  267. typedef struct xfs_dir2_data_entry {
  268. __be64 inumber; /* inode number */
  269. __u8 namelen; /* name length */
  270. __u8 name[]; /* name bytes, no null */
  271. /* __be16 tag; */ /* starting offset of us */
  272. } xfs_dir2_data_entry_t;
  273. /*
  274. * Unused entry in a data block.
  275. *
  276. * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
  277. * using xfs_dir2_data_unused_tag_p.
  278. */
  279. typedef struct xfs_dir2_data_unused {
  280. __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
  281. __be16 length; /* total free length */
  282. /* variable offset */
  283. __be16 tag; /* starting offset of us */
  284. } xfs_dir2_data_unused_t;
  285. /*
  286. * Size of a data entry.
  287. */
  288. static inline int xfs_dir2_data_entsize(int n)
  289. {
  290. return (int)roundup(offsetof(struct xfs_dir2_data_entry, name[0]) + n +
  291. (uint)sizeof(xfs_dir2_data_off_t), XFS_DIR2_DATA_ALIGN);
  292. }
  293. /*
  294. * Pointer to an entry's tag word.
  295. */
  296. static inline __be16 *
  297. xfs_dir2_data_entry_tag_p(struct xfs_dir2_data_entry *dep)
  298. {
  299. return (__be16 *)((char *)dep +
  300. xfs_dir2_data_entsize(dep->namelen) - sizeof(__be16));
  301. }
  302. /*
  303. * Pointer to a freespace's tag word.
  304. */
  305. static inline __be16 *
  306. xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
  307. {
  308. return (__be16 *)((char *)dup +
  309. be16_to_cpu(dup->length) - sizeof(__be16));
  310. }
  311. static inline struct xfs_dir2_data_unused *
  312. xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
  313. {
  314. if (hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  315. return (struct xfs_dir2_data_unused *)
  316. ((char *)hdr + sizeof(struct xfs_dir3_data_hdr));
  317. }
  318. return (struct xfs_dir2_data_unused *)
  319. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr));
  320. }
  321. static inline size_t
  322. xfs_dir3_data_hdr_size(bool dir3)
  323. {
  324. if (dir3)
  325. return sizeof(struct xfs_dir3_data_hdr);
  326. return sizeof(struct xfs_dir2_data_hdr);
  327. }
  328. static inline size_t
  329. xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr)
  330. {
  331. bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  332. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
  333. return xfs_dir3_data_hdr_size(dir3);
  334. }
  335. static inline struct xfs_dir2_data_entry *
  336. xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
  337. {
  338. return (struct xfs_dir2_data_entry *)
  339. ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
  340. }
  341. /*
  342. * Offsets of . and .. in data space (always block 0)
  343. */
  344. static inline xfs_dir2_data_aoff_t
  345. xfs_dir3_data_dot_offset(struct xfs_dir2_data_hdr *hdr)
  346. {
  347. return xfs_dir3_data_entry_offset(hdr);
  348. }
  349. static inline xfs_dir2_data_aoff_t
  350. xfs_dir3_data_dotdot_offset(struct xfs_dir2_data_hdr *hdr)
  351. {
  352. return xfs_dir3_data_dot_offset(hdr) + xfs_dir2_data_entsize(1);
  353. }
  354. static inline xfs_dir2_data_aoff_t
  355. xfs_dir3_data_first_offset(struct xfs_dir2_data_hdr *hdr)
  356. {
  357. return xfs_dir3_data_dotdot_offset(hdr) + xfs_dir2_data_entsize(2);
  358. }
  359. /*
  360. * location of . and .. in data space (always block 0)
  361. */
  362. static inline struct xfs_dir2_data_entry *
  363. xfs_dir3_data_dot_entry_p(struct xfs_dir2_data_hdr *hdr)
  364. {
  365. return (struct xfs_dir2_data_entry *)
  366. ((char *)hdr + xfs_dir3_data_dot_offset(hdr));
  367. }
  368. static inline struct xfs_dir2_data_entry *
  369. xfs_dir3_data_dotdot_entry_p(struct xfs_dir2_data_hdr *hdr)
  370. {
  371. return (struct xfs_dir2_data_entry *)
  372. ((char *)hdr + xfs_dir3_data_dotdot_offset(hdr));
  373. }
  374. static inline struct xfs_dir2_data_entry *
  375. xfs_dir3_data_first_entry_p(struct xfs_dir2_data_hdr *hdr)
  376. {
  377. return (struct xfs_dir2_data_entry *)
  378. ((char *)hdr + xfs_dir3_data_first_offset(hdr));
  379. }
  380. /*
  381. * Leaf block structures.
  382. *
  383. * A pure leaf block looks like the following drawing on disk:
  384. *
  385. * +---------------------------+
  386. * | xfs_dir2_leaf_hdr_t |
  387. * +---------------------------+
  388. * | xfs_dir2_leaf_entry_t |
  389. * | xfs_dir2_leaf_entry_t |
  390. * | xfs_dir2_leaf_entry_t |
  391. * | xfs_dir2_leaf_entry_t |
  392. * | ... |
  393. * +---------------------------+
  394. * | xfs_dir2_data_off_t |
  395. * | xfs_dir2_data_off_t |
  396. * | xfs_dir2_data_off_t |
  397. * | ... |
  398. * +---------------------------+
  399. * | xfs_dir2_leaf_tail_t |
  400. * +---------------------------+
  401. *
  402. * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
  403. * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
  404. * for directories with separate leaf nodes and free space blocks
  405. * (magic = XFS_DIR2_LEAFN_MAGIC).
  406. *
  407. * As all the entries are variable size structures the accessors below should
  408. * be used to iterate over them.
  409. */
  410. /*
  411. * Offset of the leaf/node space. First block in this space
  412. * is the btree root.
  413. */
  414. #define XFS_DIR2_LEAF_SPACE 1
  415. #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
  416. #define XFS_DIR2_LEAF_FIRSTDB(mp) \
  417. xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
  418. /*
  419. * Leaf block header.
  420. */
  421. typedef struct xfs_dir2_leaf_hdr {
  422. xfs_da_blkinfo_t info; /* header for da routines */
  423. __be16 count; /* count of entries */
  424. __be16 stale; /* count of stale entries */
  425. } xfs_dir2_leaf_hdr_t;
  426. /*
  427. * Leaf block entry.
  428. */
  429. typedef struct xfs_dir2_leaf_entry {
  430. __be32 hashval; /* hash value of name */
  431. __be32 address; /* address of data entry */
  432. } xfs_dir2_leaf_entry_t;
  433. /*
  434. * Leaf block tail.
  435. */
  436. typedef struct xfs_dir2_leaf_tail {
  437. __be32 bestcount;
  438. } xfs_dir2_leaf_tail_t;
  439. /*
  440. * Leaf block.
  441. */
  442. typedef struct xfs_dir2_leaf {
  443. xfs_dir2_leaf_hdr_t hdr; /* leaf header */
  444. xfs_dir2_leaf_entry_t ents[]; /* entries */
  445. } xfs_dir2_leaf_t;
  446. /*
  447. * DB blocks here are logical directory block numbers, not filesystem blocks.
  448. */
  449. static inline int xfs_dir2_max_leaf_ents(struct xfs_mount *mp)
  450. {
  451. return (mp->m_dirblksize - (uint)sizeof(struct xfs_dir2_leaf_hdr)) /
  452. (uint)sizeof(struct xfs_dir2_leaf_entry);
  453. }
  454. /*
  455. * Get address of the bestcount field in the single-leaf block.
  456. */
  457. static inline struct xfs_dir2_leaf_tail *
  458. xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
  459. {
  460. return (struct xfs_dir2_leaf_tail *)
  461. ((char *)lp + mp->m_dirblksize -
  462. sizeof(struct xfs_dir2_leaf_tail));
  463. }
  464. /*
  465. * Get address of the bests array in the single-leaf block.
  466. */
  467. static inline __be16 *
  468. xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
  469. {
  470. return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
  471. }
  472. /*
  473. * Convert dataptr to byte in file space
  474. */
  475. static inline xfs_dir2_off_t
  476. xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  477. {
  478. return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
  479. }
  480. /*
  481. * Convert byte in file space to dataptr. It had better be aligned.
  482. */
  483. static inline xfs_dir2_dataptr_t
  484. xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
  485. {
  486. return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
  487. }
  488. /*
  489. * Convert byte in space to (DB) block
  490. */
  491. static inline xfs_dir2_db_t
  492. xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
  493. {
  494. return (xfs_dir2_db_t)
  495. (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
  496. }
  497. /*
  498. * Convert dataptr to a block number
  499. */
  500. static inline xfs_dir2_db_t
  501. xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  502. {
  503. return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  504. }
  505. /*
  506. * Convert byte in space to offset in a block
  507. */
  508. static inline xfs_dir2_data_aoff_t
  509. xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
  510. {
  511. return (xfs_dir2_data_aoff_t)(by &
  512. ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
  513. }
  514. /*
  515. * Convert dataptr to a byte offset in a block
  516. */
  517. static inline xfs_dir2_data_aoff_t
  518. xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  519. {
  520. return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  521. }
  522. /*
  523. * Convert block and offset to byte in space
  524. */
  525. static inline xfs_dir2_off_t
  526. xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
  527. xfs_dir2_data_aoff_t o)
  528. {
  529. return ((xfs_dir2_off_t)db <<
  530. (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
  531. }
  532. /*
  533. * Convert block (DB) to block (dablk)
  534. */
  535. static inline xfs_dablk_t
  536. xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
  537. {
  538. return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
  539. }
  540. /*
  541. * Convert byte in space to (DA) block
  542. */
  543. static inline xfs_dablk_t
  544. xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
  545. {
  546. return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
  547. }
  548. /*
  549. * Convert block and offset to dataptr
  550. */
  551. static inline xfs_dir2_dataptr_t
  552. xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
  553. xfs_dir2_data_aoff_t o)
  554. {
  555. return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
  556. }
  557. /*
  558. * Convert block (dablk) to block (DB)
  559. */
  560. static inline xfs_dir2_db_t
  561. xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
  562. {
  563. return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
  564. }
  565. /*
  566. * Convert block (dablk) to byte offset in space
  567. */
  568. static inline xfs_dir2_off_t
  569. xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
  570. {
  571. return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
  572. }
  573. /*
  574. * Free space block defintions for the node format.
  575. */
  576. /*
  577. * Offset of the freespace index.
  578. */
  579. #define XFS_DIR2_FREE_SPACE 2
  580. #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
  581. #define XFS_DIR2_FREE_FIRSTDB(mp) \
  582. xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
  583. typedef struct xfs_dir2_free_hdr {
  584. __be32 magic; /* XFS_DIR2_FREE_MAGIC */
  585. __be32 firstdb; /* db of first entry */
  586. __be32 nvalid; /* count of valid entries */
  587. __be32 nused; /* count of used entries */
  588. } xfs_dir2_free_hdr_t;
  589. typedef struct xfs_dir2_free {
  590. xfs_dir2_free_hdr_t hdr; /* block header */
  591. __be16 bests[]; /* best free counts */
  592. /* unused entries are -1 */
  593. } xfs_dir2_free_t;
  594. struct xfs_dir3_free_hdr {
  595. struct xfs_dir3_blk_hdr hdr;
  596. __be32 firstdb; /* db of first entry */
  597. __be32 nvalid; /* count of valid entries */
  598. __be32 nused; /* count of used entries */
  599. };
  600. struct xfs_dir3_free {
  601. struct xfs_dir3_free_hdr hdr;
  602. __be16 bests[]; /* best free counts */
  603. /* unused entries are -1 */
  604. };
  605. #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
  606. /*
  607. * In core version of the free block header, abstracted away from on-disk format
  608. * differences. Use this in the code, and convert to/from the disk version using
  609. * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
  610. */
  611. struct xfs_dir3_icfree_hdr {
  612. __uint32_t magic;
  613. __uint32_t firstdb;
  614. __uint32_t nvalid;
  615. __uint32_t nused;
  616. };
  617. void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
  618. struct xfs_dir2_free *from);
  619. static inline int
  620. xfs_dir3_free_hdr_size(struct xfs_mount *mp)
  621. {
  622. if (xfs_sb_version_hascrc(&mp->m_sb))
  623. return sizeof(struct xfs_dir3_free_hdr);
  624. return sizeof(struct xfs_dir2_free_hdr);
  625. }
  626. static inline int
  627. xfs_dir3_free_max_bests(struct xfs_mount *mp)
  628. {
  629. return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
  630. sizeof(xfs_dir2_data_off_t);
  631. }
  632. static inline __be16 *
  633. xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
  634. {
  635. return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
  636. }
  637. /*
  638. * Convert data space db to the corresponding free db.
  639. */
  640. static inline xfs_dir2_db_t
  641. xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
  642. {
  643. return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
  644. }
  645. /*
  646. * Convert data space db to the corresponding index in a free db.
  647. */
  648. static inline int
  649. xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
  650. {
  651. return db % xfs_dir3_free_max_bests(mp);
  652. }
  653. /*
  654. * Single block format.
  655. *
  656. * The single block format looks like the following drawing on disk:
  657. *
  658. * +-------------------------------------------------+
  659. * | xfs_dir2_data_hdr_t |
  660. * +-------------------------------------------------+
  661. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  662. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  663. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
  664. * | ... |
  665. * +-------------------------------------------------+
  666. * | unused space |
  667. * +-------------------------------------------------+
  668. * | ... |
  669. * | xfs_dir2_leaf_entry_t |
  670. * | xfs_dir2_leaf_entry_t |
  671. * +-------------------------------------------------+
  672. * | xfs_dir2_block_tail_t |
  673. * +-------------------------------------------------+
  674. *
  675. * As all the entries are variable size structures the accessors below should
  676. * be used to iterate over them.
  677. */
  678. typedef struct xfs_dir2_block_tail {
  679. __be32 count; /* count of leaf entries */
  680. __be32 stale; /* count of stale lf entries */
  681. } xfs_dir2_block_tail_t;
  682. /*
  683. * Pointer to the leaf header embedded in a data block (1-block format)
  684. */
  685. static inline struct xfs_dir2_block_tail *
  686. xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
  687. {
  688. return ((struct xfs_dir2_block_tail *)
  689. ((char *)hdr + mp->m_dirblksize)) - 1;
  690. }
  691. /*
  692. * Pointer to the leaf entries embedded in a data block (1-block format)
  693. */
  694. static inline struct xfs_dir2_leaf_entry *
  695. xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
  696. {
  697. return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
  698. }
  699. #endif /* __XFS_DIR2_FORMAT_H__ */