xfs_dir2_format.h 24 KB

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