xfs_dir2_format.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. * Describe a free area in the data block.
  207. *
  208. * The freespace will be formatted as a xfs_dir2_data_unused_t.
  209. */
  210. typedef struct xfs_dir2_data_free {
  211. __be16 offset; /* start of freespace */
  212. __be16 length; /* length of freespace */
  213. } xfs_dir2_data_free_t;
  214. /*
  215. * Header for the data blocks.
  216. *
  217. * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
  218. */
  219. typedef struct xfs_dir2_data_hdr {
  220. __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
  221. /* XFS_DIR2_BLOCK_MAGIC */
  222. xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
  223. } xfs_dir2_data_hdr_t;
  224. /*
  225. * define a structure for all the verification fields we are adding to the
  226. * directory block structures. This will be used in several structures.
  227. * The magic number must be the first entry to align with all the dir2
  228. * structures so we determine how to decode them just by the magic number.
  229. */
  230. struct xfs_dir3_blk_hdr {
  231. __be32 magic; /* magic number */
  232. __be32 crc; /* CRC of block */
  233. __be64 blkno; /* first block of the buffer */
  234. __be64 lsn; /* sequence number of last write */
  235. uuid_t uuid; /* filesystem we belong to */
  236. __be64 owner; /* inode that owns the block */
  237. };
  238. struct xfs_dir3_data_hdr {
  239. struct xfs_dir3_blk_hdr hdr;
  240. xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
  241. };
  242. #define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
  243. static inline struct xfs_dir2_data_free *
  244. xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
  245. {
  246. if (hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  247. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  248. struct xfs_dir3_data_hdr *hdr3 = (struct xfs_dir3_data_hdr *)hdr;
  249. return hdr3->best_free;
  250. }
  251. return hdr->bestfree;
  252. }
  253. /*
  254. * Active entry in a data block.
  255. *
  256. * Aligned to 8 bytes. After the variable length name field there is a
  257. * 2 byte tag field, which can be accessed using xfs_dir2_data_entry_tag_p.
  258. */
  259. typedef struct xfs_dir2_data_entry {
  260. __be64 inumber; /* inode number */
  261. __u8 namelen; /* name length */
  262. __u8 name[]; /* name bytes, no null */
  263. /* __be16 tag; */ /* starting offset of us */
  264. } xfs_dir2_data_entry_t;
  265. /*
  266. * Unused entry in a data block.
  267. *
  268. * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
  269. * using xfs_dir2_data_unused_tag_p.
  270. */
  271. typedef struct xfs_dir2_data_unused {
  272. __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
  273. __be16 length; /* total free length */
  274. /* variable offset */
  275. __be16 tag; /* starting offset of us */
  276. } xfs_dir2_data_unused_t;
  277. /*
  278. * Size of a data entry.
  279. */
  280. static inline int xfs_dir2_data_entsize(int n)
  281. {
  282. return (int)roundup(offsetof(struct xfs_dir2_data_entry, name[0]) + n +
  283. (uint)sizeof(xfs_dir2_data_off_t), XFS_DIR2_DATA_ALIGN);
  284. }
  285. /*
  286. * Pointer to an entry's tag word.
  287. */
  288. static inline __be16 *
  289. xfs_dir2_data_entry_tag_p(struct xfs_dir2_data_entry *dep)
  290. {
  291. return (__be16 *)((char *)dep +
  292. xfs_dir2_data_entsize(dep->namelen) - sizeof(__be16));
  293. }
  294. /*
  295. * Pointer to a freespace's tag word.
  296. */
  297. static inline __be16 *
  298. xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
  299. {
  300. return (__be16 *)((char *)dup +
  301. be16_to_cpu(dup->length) - sizeof(__be16));
  302. }
  303. static inline size_t
  304. xfs_dir3_data_hdr_size(bool dir3)
  305. {
  306. if (dir3)
  307. return sizeof(struct xfs_dir3_data_hdr);
  308. return sizeof(struct xfs_dir2_data_hdr);
  309. }
  310. static inline size_t
  311. xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr)
  312. {
  313. bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  314. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
  315. return xfs_dir3_data_hdr_size(dir3);
  316. }
  317. static inline struct xfs_dir2_data_entry *
  318. xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
  319. {
  320. return (struct xfs_dir2_data_entry *)
  321. ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
  322. }
  323. static inline struct xfs_dir2_data_unused *
  324. xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
  325. {
  326. return (struct xfs_dir2_data_unused *)
  327. ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
  328. }
  329. /*
  330. * Offsets of . and .. in data space (always block 0)
  331. *
  332. * The macros are used for shortform directories as they have no headers to read
  333. * the magic number out of. Shortform directories need to know the size of the
  334. * data block header because the sfe embeds the block offset of the entry into
  335. * it so that it doesn't change when format conversion occurs. Bad Things Happen
  336. * if we don't follow this rule.
  337. */
  338. #define XFS_DIR3_DATA_DOT_OFFSET(mp) \
  339. xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&(mp)->m_sb))
  340. #define XFS_DIR3_DATA_DOTDOT_OFFSET(mp) \
  341. (XFS_DIR3_DATA_DOT_OFFSET(mp) + xfs_dir2_data_entsize(1))
  342. #define XFS_DIR3_DATA_FIRST_OFFSET(mp) \
  343. (XFS_DIR3_DATA_DOTDOT_OFFSET(mp) + xfs_dir2_data_entsize(2))
  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. struct xfs_dir3_leaf_hdr {
  427. struct xfs_da3_blkinfo info; /* header for da routines */
  428. __be16 count; /* count of entries */
  429. __be16 stale; /* count of stale entries */
  430. __be32 pad;
  431. };
  432. struct xfs_dir3_icleaf_hdr {
  433. __uint32_t forw;
  434. __uint32_t back;
  435. __uint16_t magic;
  436. __uint16_t count;
  437. __uint16_t stale;
  438. };
  439. /*
  440. * Leaf block entry.
  441. */
  442. typedef struct xfs_dir2_leaf_entry {
  443. __be32 hashval; /* hash value of name */
  444. __be32 address; /* address of data entry */
  445. } xfs_dir2_leaf_entry_t;
  446. /*
  447. * Leaf block tail.
  448. */
  449. typedef struct xfs_dir2_leaf_tail {
  450. __be32 bestcount;
  451. } xfs_dir2_leaf_tail_t;
  452. /*
  453. * Leaf block.
  454. */
  455. typedef struct xfs_dir2_leaf {
  456. xfs_dir2_leaf_hdr_t hdr; /* leaf header */
  457. xfs_dir2_leaf_entry_t __ents[]; /* entries */
  458. } xfs_dir2_leaf_t;
  459. struct xfs_dir3_leaf {
  460. struct xfs_dir3_leaf_hdr hdr; /* leaf header */
  461. struct xfs_dir2_leaf_entry __ents[]; /* entries */
  462. };
  463. #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
  464. static inline int
  465. xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
  466. {
  467. if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  468. lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
  469. return sizeof(struct xfs_dir3_leaf_hdr);
  470. return sizeof(struct xfs_dir2_leaf_hdr);
  471. }
  472. static inline int
  473. xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
  474. {
  475. return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
  476. (uint)sizeof(struct xfs_dir2_leaf_entry);
  477. }
  478. /*
  479. * Get address of the bestcount field in the single-leaf block.
  480. */
  481. static inline struct xfs_dir2_leaf_entry *
  482. xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
  483. {
  484. if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  485. lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
  486. struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp;
  487. return lp3->__ents;
  488. }
  489. return lp->__ents;
  490. }
  491. /*
  492. * Get address of the bestcount field in the single-leaf block.
  493. */
  494. static inline struct xfs_dir2_leaf_tail *
  495. xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
  496. {
  497. return (struct xfs_dir2_leaf_tail *)
  498. ((char *)lp + mp->m_dirblksize -
  499. sizeof(struct xfs_dir2_leaf_tail));
  500. }
  501. /*
  502. * Get address of the bests array in the single-leaf block.
  503. */
  504. static inline __be16 *
  505. xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
  506. {
  507. return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
  508. }
  509. /*
  510. * DB blocks here are logical directory block numbers, not filesystem blocks.
  511. */
  512. /*
  513. * Convert dataptr to byte in file space
  514. */
  515. static inline xfs_dir2_off_t
  516. xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  517. {
  518. return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
  519. }
  520. /*
  521. * Convert byte in file space to dataptr. It had better be aligned.
  522. */
  523. static inline xfs_dir2_dataptr_t
  524. xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
  525. {
  526. return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
  527. }
  528. /*
  529. * Convert byte in space to (DB) block
  530. */
  531. static inline xfs_dir2_db_t
  532. xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
  533. {
  534. return (xfs_dir2_db_t)
  535. (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
  536. }
  537. /*
  538. * Convert dataptr to a block number
  539. */
  540. static inline xfs_dir2_db_t
  541. xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  542. {
  543. return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  544. }
  545. /*
  546. * Convert byte in space to offset in a block
  547. */
  548. static inline xfs_dir2_data_aoff_t
  549. xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
  550. {
  551. return (xfs_dir2_data_aoff_t)(by &
  552. ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
  553. }
  554. /*
  555. * Convert dataptr to a byte offset in a block
  556. */
  557. static inline xfs_dir2_data_aoff_t
  558. xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  559. {
  560. return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  561. }
  562. /*
  563. * Convert block and offset to byte in space
  564. */
  565. static inline xfs_dir2_off_t
  566. xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
  567. xfs_dir2_data_aoff_t o)
  568. {
  569. return ((xfs_dir2_off_t)db <<
  570. (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
  571. }
  572. /*
  573. * Convert block (DB) to block (dablk)
  574. */
  575. static inline xfs_dablk_t
  576. xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
  577. {
  578. return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
  579. }
  580. /*
  581. * Convert byte in space to (DA) block
  582. */
  583. static inline xfs_dablk_t
  584. xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
  585. {
  586. return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
  587. }
  588. /*
  589. * Convert block and offset to dataptr
  590. */
  591. static inline xfs_dir2_dataptr_t
  592. xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
  593. xfs_dir2_data_aoff_t o)
  594. {
  595. return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
  596. }
  597. /*
  598. * Convert block (dablk) to block (DB)
  599. */
  600. static inline xfs_dir2_db_t
  601. xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
  602. {
  603. return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
  604. }
  605. /*
  606. * Convert block (dablk) to byte offset in space
  607. */
  608. static inline xfs_dir2_off_t
  609. xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
  610. {
  611. return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
  612. }
  613. /*
  614. * Free space block defintions for the node format.
  615. */
  616. /*
  617. * Offset of the freespace index.
  618. */
  619. #define XFS_DIR2_FREE_SPACE 2
  620. #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
  621. #define XFS_DIR2_FREE_FIRSTDB(mp) \
  622. xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
  623. typedef struct xfs_dir2_free_hdr {
  624. __be32 magic; /* XFS_DIR2_FREE_MAGIC */
  625. __be32 firstdb; /* db of first entry */
  626. __be32 nvalid; /* count of valid entries */
  627. __be32 nused; /* count of used entries */
  628. } xfs_dir2_free_hdr_t;
  629. typedef struct xfs_dir2_free {
  630. xfs_dir2_free_hdr_t hdr; /* block header */
  631. __be16 bests[]; /* best free counts */
  632. /* unused entries are -1 */
  633. } xfs_dir2_free_t;
  634. struct xfs_dir3_free_hdr {
  635. struct xfs_dir3_blk_hdr hdr;
  636. __be32 firstdb; /* db of first entry */
  637. __be32 nvalid; /* count of valid entries */
  638. __be32 nused; /* count of used entries */
  639. };
  640. struct xfs_dir3_free {
  641. struct xfs_dir3_free_hdr hdr;
  642. __be16 bests[]; /* best free counts */
  643. /* unused entries are -1 */
  644. };
  645. #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
  646. /*
  647. * In core version of the free block header, abstracted away from on-disk format
  648. * differences. Use this in the code, and convert to/from the disk version using
  649. * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
  650. */
  651. struct xfs_dir3_icfree_hdr {
  652. __uint32_t magic;
  653. __uint32_t firstdb;
  654. __uint32_t nvalid;
  655. __uint32_t nused;
  656. };
  657. void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
  658. struct xfs_dir2_free *from);
  659. static inline int
  660. xfs_dir3_free_hdr_size(struct xfs_mount *mp)
  661. {
  662. if (xfs_sb_version_hascrc(&mp->m_sb))
  663. return sizeof(struct xfs_dir3_free_hdr);
  664. return sizeof(struct xfs_dir2_free_hdr);
  665. }
  666. static inline int
  667. xfs_dir3_free_max_bests(struct xfs_mount *mp)
  668. {
  669. return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
  670. sizeof(xfs_dir2_data_off_t);
  671. }
  672. static inline __be16 *
  673. xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
  674. {
  675. return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
  676. }
  677. /*
  678. * Convert data space db to the corresponding free db.
  679. */
  680. static inline xfs_dir2_db_t
  681. xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
  682. {
  683. return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
  684. }
  685. /*
  686. * Convert data space db to the corresponding index in a free db.
  687. */
  688. static inline int
  689. xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
  690. {
  691. return db % xfs_dir3_free_max_bests(mp);
  692. }
  693. /*
  694. * Single block format.
  695. *
  696. * The single block format looks like the following drawing on disk:
  697. *
  698. * +-------------------------------------------------+
  699. * | xfs_dir2_data_hdr_t |
  700. * +-------------------------------------------------+
  701. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  702. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  703. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
  704. * | ... |
  705. * +-------------------------------------------------+
  706. * | unused space |
  707. * +-------------------------------------------------+
  708. * | ... |
  709. * | xfs_dir2_leaf_entry_t |
  710. * | xfs_dir2_leaf_entry_t |
  711. * +-------------------------------------------------+
  712. * | xfs_dir2_block_tail_t |
  713. * +-------------------------------------------------+
  714. *
  715. * As all the entries are variable size structures the accessors below should
  716. * be used to iterate over them.
  717. */
  718. typedef struct xfs_dir2_block_tail {
  719. __be32 count; /* count of leaf entries */
  720. __be32 stale; /* count of stale lf entries */
  721. } xfs_dir2_block_tail_t;
  722. /*
  723. * Pointer to the leaf header embedded in a data block (1-block format)
  724. */
  725. static inline struct xfs_dir2_block_tail *
  726. xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
  727. {
  728. return ((struct xfs_dir2_block_tail *)
  729. ((char *)hdr + mp->m_dirblksize)) - 1;
  730. }
  731. /*
  732. * Pointer to the leaf entries embedded in a data block (1-block format)
  733. */
  734. static inline struct xfs_dir2_leaf_entry *
  735. xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
  736. {
  737. return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
  738. }
  739. #endif /* __XFS_DIR2_FORMAT_H__ */