xfs_da_format.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #ifndef __XFS_DA_FORMAT_H__
  8. #define __XFS_DA_FORMAT_H__
  9. /*
  10. * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
  11. *
  12. * It is used to manage a doubly linked list of all blocks at the same
  13. * level in the Btree, and to identify which type of block this is.
  14. */
  15. #define XFS_DA_NODE_MAGIC 0xfebe /* magic number: non-leaf blocks */
  16. #define XFS_ATTR_LEAF_MAGIC 0xfbee /* magic number: attribute leaf blks */
  17. #define XFS_DIR2_LEAF1_MAGIC 0xd2f1 /* magic number: v2 dirlf single blks */
  18. #define XFS_DIR2_LEAFN_MAGIC 0xd2ff /* magic number: v2 dirlf multi blks */
  19. typedef struct xfs_da_blkinfo {
  20. __be32 forw; /* previous block in list */
  21. __be32 back; /* following block in list */
  22. __be16 magic; /* validity check on block */
  23. __be16 pad; /* unused */
  24. } xfs_da_blkinfo_t;
  25. /*
  26. * CRC enabled directory structure types
  27. *
  28. * The headers change size for the additional verification information, but
  29. * otherwise the tree layouts and contents are unchanged. Hence the da btree
  30. * code can use the struct xfs_da_blkinfo for manipulating the tree links and
  31. * magic numbers without modification for both v2 and v3 nodes.
  32. */
  33. #define XFS_DA3_NODE_MAGIC 0x3ebe /* magic number: non-leaf blocks */
  34. #define XFS_ATTR3_LEAF_MAGIC 0x3bee /* magic number: attribute leaf blks */
  35. #define XFS_DIR3_LEAF1_MAGIC 0x3df1 /* magic number: v2 dirlf single blks */
  36. #define XFS_DIR3_LEAFN_MAGIC 0x3dff /* magic number: v2 dirlf multi blks */
  37. struct xfs_da3_blkinfo {
  38. /*
  39. * the node link manipulation code relies on the fact that the first
  40. * element of this structure is the struct xfs_da_blkinfo so it can
  41. * ignore the differences in the rest of the structures.
  42. */
  43. struct xfs_da_blkinfo hdr;
  44. __be32 crc; /* CRC of block */
  45. __be64 blkno; /* first block of the buffer */
  46. __be64 lsn; /* sequence number of last write */
  47. uuid_t uuid; /* filesystem we belong to */
  48. __be64 owner; /* inode that owns the block */
  49. };
  50. /*
  51. * This is the structure of the root and intermediate nodes in the Btree.
  52. * The leaf nodes are defined above.
  53. *
  54. * Entries are not packed.
  55. *
  56. * Since we have duplicate keys, use a binary search but always follow
  57. * all match in the block, not just the first match found.
  58. */
  59. #define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
  60. typedef struct xfs_da_node_hdr {
  61. struct xfs_da_blkinfo info; /* block type, links, etc. */
  62. __be16 __count; /* count of active entries */
  63. __be16 __level; /* level above leaves (leaf == 0) */
  64. } xfs_da_node_hdr_t;
  65. struct xfs_da3_node_hdr {
  66. struct xfs_da3_blkinfo info; /* block type, links, etc. */
  67. __be16 __count; /* count of active entries */
  68. __be16 __level; /* level above leaves (leaf == 0) */
  69. __be32 __pad32;
  70. };
  71. #define XFS_DA3_NODE_CRC_OFF (offsetof(struct xfs_da3_node_hdr, info.crc))
  72. typedef struct xfs_da_node_entry {
  73. __be32 hashval; /* hash value for this descendant */
  74. __be32 before; /* Btree block before this key */
  75. } xfs_da_node_entry_t;
  76. typedef struct xfs_da_intnode {
  77. struct xfs_da_node_hdr hdr;
  78. struct xfs_da_node_entry __btree[];
  79. } xfs_da_intnode_t;
  80. struct xfs_da3_intnode {
  81. struct xfs_da3_node_hdr hdr;
  82. struct xfs_da_node_entry __btree[];
  83. };
  84. /*
  85. * In-core version of the node header to abstract the differences in the v2 and
  86. * v3 disk format of the headers. Callers need to convert to/from disk format as
  87. * appropriate.
  88. */
  89. struct xfs_da3_icnode_hdr {
  90. uint32_t forw;
  91. uint32_t back;
  92. uint16_t magic;
  93. uint16_t count;
  94. uint16_t level;
  95. };
  96. /*
  97. * Directory version 2.
  98. *
  99. * There are 4 possible formats:
  100. * - shortform - embedded into the inode
  101. * - single block - data with embedded leaf at the end
  102. * - multiple data blocks, single leaf+freeindex block
  103. * - data blocks, node and leaf blocks (btree), freeindex blocks
  104. *
  105. * Note: many node blocks structures and constants are shared with the attr
  106. * code and defined in xfs_da_btree.h.
  107. */
  108. #define XFS_DIR2_BLOCK_MAGIC 0x58443242 /* XD2B: single block dirs */
  109. #define XFS_DIR2_DATA_MAGIC 0x58443244 /* XD2D: multiblock dirs */
  110. #define XFS_DIR2_FREE_MAGIC 0x58443246 /* XD2F: free index blocks */
  111. /*
  112. * Directory Version 3 With CRCs.
  113. *
  114. * The tree formats are the same as for version 2 directories. The difference
  115. * is in the block header and dirent formats. In many cases the v3 structures
  116. * use v2 definitions as they are no different and this makes code sharing much
  117. * easier.
  118. *
  119. * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
  120. * format is v2 then they switch to the existing v2 code, or the format is v3
  121. * they implement the v3 functionality. This means the existing dir2 is a mix of
  122. * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
  123. * where there is a difference in the formats, otherwise the code is unchanged.
  124. *
  125. * Where it is possible, the code decides what to do based on the magic numbers
  126. * in the blocks rather than feature bits in the superblock. This means the code
  127. * is as independent of the external XFS code as possible as doesn't require
  128. * passing struct xfs_mount pointers into places where it isn't really
  129. * necessary.
  130. *
  131. * Version 3 includes:
  132. *
  133. * - a larger block header for CRC and identification purposes and so the
  134. * offsets of all the structures inside the blocks are different.
  135. *
  136. * - new magic numbers to be able to detect the v2/v3 types on the fly.
  137. */
  138. #define XFS_DIR3_BLOCK_MAGIC 0x58444233 /* XDB3: single block dirs */
  139. #define XFS_DIR3_DATA_MAGIC 0x58444433 /* XDD3: multiblock dirs */
  140. #define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */
  141. /*
  142. * Dirents in version 3 directories have a file type field. Additions to this
  143. * list are an on-disk format change, requiring feature bits. Valid values
  144. * are as follows:
  145. */
  146. #define XFS_DIR3_FT_UNKNOWN 0
  147. #define XFS_DIR3_FT_REG_FILE 1
  148. #define XFS_DIR3_FT_DIR 2
  149. #define XFS_DIR3_FT_CHRDEV 3
  150. #define XFS_DIR3_FT_BLKDEV 4
  151. #define XFS_DIR3_FT_FIFO 5
  152. #define XFS_DIR3_FT_SOCK 6
  153. #define XFS_DIR3_FT_SYMLINK 7
  154. #define XFS_DIR3_FT_WHT 8
  155. #define XFS_DIR3_FT_MAX 9
  156. /*
  157. * Byte offset in data block and shortform entry.
  158. */
  159. typedef uint16_t xfs_dir2_data_off_t;
  160. #define NULLDATAOFF 0xffffU
  161. typedef uint xfs_dir2_data_aoff_t; /* argument form */
  162. /*
  163. * Offset in data space of a data entry.
  164. */
  165. typedef uint32_t xfs_dir2_dataptr_t;
  166. #define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
  167. #define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
  168. /*
  169. * Byte offset in a directory.
  170. */
  171. typedef xfs_off_t xfs_dir2_off_t;
  172. /*
  173. * Directory block number (logical dirblk in file)
  174. */
  175. typedef uint32_t xfs_dir2_db_t;
  176. #define XFS_INO32_SIZE 4
  177. #define XFS_INO64_SIZE 8
  178. #define XFS_INO64_DIFF (XFS_INO64_SIZE - XFS_INO32_SIZE)
  179. #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
  180. /*
  181. * Directory layout when stored internal to an inode.
  182. *
  183. * Small directories are packed as tightly as possible so as to fit into the
  184. * literal area of the inode. These "shortform" directories consist of a
  185. * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
  186. * structures. Due the different inode number storage size and the variable
  187. * length name field in the xfs_dir2_sf_entry all these structure are
  188. * variable length, and the accessors in this file should be used to iterate
  189. * over them.
  190. */
  191. typedef struct xfs_dir2_sf_hdr {
  192. uint8_t count; /* count of entries */
  193. uint8_t i8count; /* count of 8-byte inode #s */
  194. uint8_t parent[8]; /* parent dir inode number */
  195. } __packed xfs_dir2_sf_hdr_t;
  196. typedef struct xfs_dir2_sf_entry {
  197. __u8 namelen; /* actual name length */
  198. __u8 offset[2]; /* saved offset */
  199. __u8 name[]; /* name, variable size */
  200. /*
  201. * A single byte containing the file type field follows the inode
  202. * number for version 3 directory entries.
  203. *
  204. * A 64-bit or 32-bit inode number follows here, at a variable offset
  205. * after the name.
  206. */
  207. } xfs_dir2_sf_entry_t;
  208. static inline int xfs_dir2_sf_hdr_size(int i8count)
  209. {
  210. return sizeof(struct xfs_dir2_sf_hdr) -
  211. (i8count == 0) * XFS_INO64_DIFF;
  212. }
  213. static inline xfs_dir2_data_aoff_t
  214. xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
  215. {
  216. return get_unaligned_be16(sfep->offset);
  217. }
  218. static inline void
  219. xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
  220. {
  221. put_unaligned_be16(off, sfep->offset);
  222. }
  223. static inline struct xfs_dir2_sf_entry *
  224. xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
  225. {
  226. return (struct xfs_dir2_sf_entry *)
  227. ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
  228. }
  229. /*
  230. * Data block structures.
  231. *
  232. * A pure data block looks like the following drawing on disk:
  233. *
  234. * +-------------------------------------------------+
  235. * | xfs_dir2_data_hdr_t |
  236. * +-------------------------------------------------+
  237. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  238. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  239. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  240. * | ... |
  241. * +-------------------------------------------------+
  242. * | unused space |
  243. * +-------------------------------------------------+
  244. *
  245. * As all the entries are variable size structures the accessors below should
  246. * be used to iterate over them.
  247. *
  248. * In addition to the pure data blocks for the data and node formats,
  249. * most structures are also used for the combined data/freespace "block"
  250. * format below.
  251. */
  252. #define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
  253. #define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
  254. #define XFS_DIR2_DATA_FREE_TAG 0xffff
  255. #define XFS_DIR2_DATA_FD_COUNT 3
  256. /*
  257. * Directory address space divided into sections,
  258. * spaces separated by 32GB.
  259. */
  260. #define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
  261. #define XFS_DIR2_DATA_SPACE 0
  262. #define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
  263. /*
  264. * Describe a free area in the data block.
  265. *
  266. * The freespace will be formatted as a xfs_dir2_data_unused_t.
  267. */
  268. typedef struct xfs_dir2_data_free {
  269. __be16 offset; /* start of freespace */
  270. __be16 length; /* length of freespace */
  271. } xfs_dir2_data_free_t;
  272. /*
  273. * Header for the data blocks.
  274. *
  275. * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
  276. */
  277. typedef struct xfs_dir2_data_hdr {
  278. __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
  279. /* XFS_DIR2_BLOCK_MAGIC */
  280. xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
  281. } xfs_dir2_data_hdr_t;
  282. /*
  283. * define a structure for all the verification fields we are adding to the
  284. * directory block structures. This will be used in several structures.
  285. * The magic number must be the first entry to align with all the dir2
  286. * structures so we determine how to decode them just by the magic number.
  287. */
  288. struct xfs_dir3_blk_hdr {
  289. __be32 magic; /* magic number */
  290. __be32 crc; /* CRC of block */
  291. __be64 blkno; /* first block of the buffer */
  292. __be64 lsn; /* sequence number of last write */
  293. uuid_t uuid; /* filesystem we belong to */
  294. __be64 owner; /* inode that owns the block */
  295. };
  296. struct xfs_dir3_data_hdr {
  297. struct xfs_dir3_blk_hdr hdr;
  298. xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
  299. __be32 pad; /* 64 bit alignment */
  300. };
  301. #define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
  302. /*
  303. * Active entry in a data block.
  304. *
  305. * Aligned to 8 bytes. After the variable length name field there is a
  306. * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
  307. *
  308. * For dir3 structures, there is file type field between the name and the tag.
  309. * This can only be manipulated by helper functions. It is packed hard against
  310. * the end of the name so any padding for rounding is between the file type and
  311. * the tag.
  312. */
  313. typedef struct xfs_dir2_data_entry {
  314. __be64 inumber; /* inode number */
  315. __u8 namelen; /* name length */
  316. __u8 name[]; /* name bytes, no null */
  317. /* __u8 filetype; */ /* type of inode we point to */
  318. /* __be16 tag; */ /* starting offset of us */
  319. } xfs_dir2_data_entry_t;
  320. /*
  321. * Unused entry in a data block.
  322. *
  323. * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
  324. * using xfs_dir2_data_unused_tag_p.
  325. */
  326. typedef struct xfs_dir2_data_unused {
  327. __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
  328. __be16 length; /* total free length */
  329. /* variable offset */
  330. __be16 tag; /* starting offset of us */
  331. } xfs_dir2_data_unused_t;
  332. /*
  333. * Pointer to a freespace's tag word.
  334. */
  335. static inline __be16 *
  336. xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
  337. {
  338. return (__be16 *)((char *)dup +
  339. be16_to_cpu(dup->length) - sizeof(__be16));
  340. }
  341. /*
  342. * Leaf block structures.
  343. *
  344. * A pure leaf block looks like the following drawing on disk:
  345. *
  346. * +---------------------------+
  347. * | xfs_dir2_leaf_hdr_t |
  348. * +---------------------------+
  349. * | xfs_dir2_leaf_entry_t |
  350. * | xfs_dir2_leaf_entry_t |
  351. * | xfs_dir2_leaf_entry_t |
  352. * | xfs_dir2_leaf_entry_t |
  353. * | ... |
  354. * +---------------------------+
  355. * | xfs_dir2_data_off_t |
  356. * | xfs_dir2_data_off_t |
  357. * | xfs_dir2_data_off_t |
  358. * | ... |
  359. * +---------------------------+
  360. * | xfs_dir2_leaf_tail_t |
  361. * +---------------------------+
  362. *
  363. * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
  364. * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
  365. * for directories with separate leaf nodes and free space blocks
  366. * (magic = XFS_DIR2_LEAFN_MAGIC).
  367. *
  368. * As all the entries are variable size structures the accessors below should
  369. * be used to iterate over them.
  370. */
  371. /*
  372. * Offset of the leaf/node space. First block in this space
  373. * is the btree root.
  374. */
  375. #define XFS_DIR2_LEAF_SPACE 1
  376. #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
  377. /*
  378. * Leaf block header.
  379. */
  380. typedef struct xfs_dir2_leaf_hdr {
  381. xfs_da_blkinfo_t info; /* header for da routines */
  382. __be16 count; /* count of entries */
  383. __be16 stale; /* count of stale entries */
  384. } xfs_dir2_leaf_hdr_t;
  385. struct xfs_dir3_leaf_hdr {
  386. struct xfs_da3_blkinfo info; /* header for da routines */
  387. __be16 count; /* count of entries */
  388. __be16 stale; /* count of stale entries */
  389. __be32 pad; /* 64 bit alignment */
  390. };
  391. struct xfs_dir3_icleaf_hdr {
  392. uint32_t forw;
  393. uint32_t back;
  394. uint16_t magic;
  395. uint16_t count;
  396. uint16_t stale;
  397. };
  398. /*
  399. * Leaf block entry.
  400. */
  401. typedef struct xfs_dir2_leaf_entry {
  402. __be32 hashval; /* hash value of name */
  403. __be32 address; /* address of data entry */
  404. } xfs_dir2_leaf_entry_t;
  405. /*
  406. * Leaf block tail.
  407. */
  408. typedef struct xfs_dir2_leaf_tail {
  409. __be32 bestcount;
  410. } xfs_dir2_leaf_tail_t;
  411. /*
  412. * Leaf block.
  413. */
  414. typedef struct xfs_dir2_leaf {
  415. xfs_dir2_leaf_hdr_t hdr; /* leaf header */
  416. xfs_dir2_leaf_entry_t __ents[]; /* entries */
  417. } xfs_dir2_leaf_t;
  418. struct xfs_dir3_leaf {
  419. struct xfs_dir3_leaf_hdr hdr; /* leaf header */
  420. struct xfs_dir2_leaf_entry __ents[]; /* entries */
  421. };
  422. #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
  423. /*
  424. * Get address of the bests array in the single-leaf block.
  425. */
  426. static inline __be16 *
  427. xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
  428. {
  429. return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
  430. }
  431. /*
  432. * Free space block defintions for the node format.
  433. */
  434. /*
  435. * Offset of the freespace index.
  436. */
  437. #define XFS_DIR2_FREE_SPACE 2
  438. #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
  439. typedef struct xfs_dir2_free_hdr {
  440. __be32 magic; /* XFS_DIR2_FREE_MAGIC */
  441. __be32 firstdb; /* db of first entry */
  442. __be32 nvalid; /* count of valid entries */
  443. __be32 nused; /* count of used entries */
  444. } xfs_dir2_free_hdr_t;
  445. typedef struct xfs_dir2_free {
  446. xfs_dir2_free_hdr_t hdr; /* block header */
  447. __be16 bests[]; /* best free counts */
  448. /* unused entries are -1 */
  449. } xfs_dir2_free_t;
  450. struct xfs_dir3_free_hdr {
  451. struct xfs_dir3_blk_hdr hdr;
  452. __be32 firstdb; /* db of first entry */
  453. __be32 nvalid; /* count of valid entries */
  454. __be32 nused; /* count of used entries */
  455. __be32 pad; /* 64 bit alignment */
  456. };
  457. struct xfs_dir3_free {
  458. struct xfs_dir3_free_hdr hdr;
  459. __be16 bests[]; /* best free counts */
  460. /* unused entries are -1 */
  461. };
  462. #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
  463. /*
  464. * In core version of the free block header, abstracted away from on-disk format
  465. * differences. Use this in the code, and convert to/from the disk version using
  466. * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
  467. */
  468. struct xfs_dir3_icfree_hdr {
  469. uint32_t magic;
  470. uint32_t firstdb;
  471. uint32_t nvalid;
  472. uint32_t nused;
  473. };
  474. /*
  475. * Single block format.
  476. *
  477. * The single block format looks like the following drawing on disk:
  478. *
  479. * +-------------------------------------------------+
  480. * | xfs_dir2_data_hdr_t |
  481. * +-------------------------------------------------+
  482. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  483. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  484. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
  485. * | ... |
  486. * +-------------------------------------------------+
  487. * | unused space |
  488. * +-------------------------------------------------+
  489. * | ... |
  490. * | xfs_dir2_leaf_entry_t |
  491. * | xfs_dir2_leaf_entry_t |
  492. * +-------------------------------------------------+
  493. * | xfs_dir2_block_tail_t |
  494. * +-------------------------------------------------+
  495. *
  496. * As all the entries are variable size structures the accessors below should
  497. * be used to iterate over them.
  498. */
  499. typedef struct xfs_dir2_block_tail {
  500. __be32 count; /* count of leaf entries */
  501. __be32 stale; /* count of stale lf entries */
  502. } xfs_dir2_block_tail_t;
  503. /*
  504. * Pointer to the leaf entries embedded in a data block (1-block format)
  505. */
  506. static inline struct xfs_dir2_leaf_entry *
  507. xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
  508. {
  509. return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
  510. }
  511. /*
  512. * Attribute storage layout
  513. *
  514. * Attribute lists are structured around Btrees where all the data
  515. * elements are in the leaf nodes. Attribute names are hashed into an int,
  516. * then that int is used as the index into the Btree. Since the hashval
  517. * of an attribute name may not be unique, we may have duplicate keys. The
  518. * internal links in the Btree are logical block offsets into the file.
  519. *
  520. * Struct leaf_entry's are packed from the top. Name/values grow from the
  521. * bottom but are not packed. The freemap contains run-length-encoded entries
  522. * for the free bytes after the leaf_entry's, but only the N largest such,
  523. * smaller runs are dropped. When the freemap doesn't show enough space
  524. * for an allocation, we compact the name/value area and try again. If we
  525. * still don't have enough space, then we have to split the block. The
  526. * name/value structs (both local and remote versions) must be 32bit aligned.
  527. *
  528. * Since we have duplicate hash keys, for each key that matches, compare
  529. * the actual name string. The root and intermediate node search always
  530. * takes the first-in-the-block key match found, so we should only have
  531. * to work "forw"ard. If none matches, continue with the "forw"ard leaf
  532. * nodes until the hash key changes or the attribute name is found.
  533. *
  534. * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
  535. * the leaf_entry. The namespaces are independent only because we also look
  536. * at the namespace bit when we are looking for a matching attribute name.
  537. *
  538. * We also store an "incomplete" bit in the leaf_entry. It shows that an
  539. * attribute is in the middle of being created and should not be shown to
  540. * the user if we crash during the time that the bit is set. We clear the
  541. * bit when we have finished setting up the attribute. We do this because
  542. * we cannot create some large attributes inside a single transaction, and we
  543. * need some indication that we weren't finished if we crash in the middle.
  544. */
  545. #define XFS_ATTR_LEAF_MAPSIZE 3 /* how many freespace slots */
  546. /*
  547. * Entries are packed toward the top as tight as possible.
  548. */
  549. typedef struct xfs_attr_shortform {
  550. struct xfs_attr_sf_hdr { /* constant-structure header block */
  551. __be16 totsize; /* total bytes in shortform list */
  552. __u8 count; /* count of active entries */
  553. __u8 padding;
  554. } hdr;
  555. struct xfs_attr_sf_entry {
  556. uint8_t namelen; /* actual length of name (no NULL) */
  557. uint8_t valuelen; /* actual length of value (no NULL) */
  558. uint8_t flags; /* flags bits (see xfs_attr_leaf.h) */
  559. uint8_t nameval[1]; /* name & value bytes concatenated */
  560. } list[1]; /* variable sized array */
  561. } xfs_attr_shortform_t;
  562. typedef struct xfs_attr_leaf_map { /* RLE map of free bytes */
  563. __be16 base; /* base of free region */
  564. __be16 size; /* length of free region */
  565. } xfs_attr_leaf_map_t;
  566. typedef struct xfs_attr_leaf_hdr { /* constant-structure header block */
  567. xfs_da_blkinfo_t info; /* block type, links, etc. */
  568. __be16 count; /* count of active leaf_entry's */
  569. __be16 usedbytes; /* num bytes of names/values stored */
  570. __be16 firstused; /* first used byte in name area */
  571. __u8 holes; /* != 0 if blk needs compaction */
  572. __u8 pad1;
  573. xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
  574. /* N largest free regions */
  575. } xfs_attr_leaf_hdr_t;
  576. typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */
  577. __be32 hashval; /* hash value of name */
  578. __be16 nameidx; /* index into buffer of name/value */
  579. __u8 flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
  580. __u8 pad2; /* unused pad byte */
  581. } xfs_attr_leaf_entry_t;
  582. typedef struct xfs_attr_leaf_name_local {
  583. __be16 valuelen; /* number of bytes in value */
  584. __u8 namelen; /* length of name bytes */
  585. __u8 nameval[1]; /* name/value bytes */
  586. } xfs_attr_leaf_name_local_t;
  587. typedef struct xfs_attr_leaf_name_remote {
  588. __be32 valueblk; /* block number of value bytes */
  589. __be32 valuelen; /* number of bytes in value */
  590. __u8 namelen; /* length of name bytes */
  591. __u8 name[1]; /* name bytes */
  592. } xfs_attr_leaf_name_remote_t;
  593. typedef struct xfs_attr_leafblock {
  594. xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */
  595. xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */
  596. /*
  597. * The rest of the block contains the following structures after the
  598. * leaf entries, growing from the bottom up. The variables are never
  599. * referenced and definining them can actually make gcc optimize away
  600. * accesses to the 'entries' array above index 0 so don't do that.
  601. *
  602. * xfs_attr_leaf_name_local_t namelist;
  603. * xfs_attr_leaf_name_remote_t valuelist;
  604. */
  605. } xfs_attr_leafblock_t;
  606. /*
  607. * CRC enabled leaf structures. Called "version 3" structures to match the
  608. * version number of the directory and dablk structures for this feature, and
  609. * attr2 is already taken by the variable inode attribute fork size feature.
  610. */
  611. struct xfs_attr3_leaf_hdr {
  612. struct xfs_da3_blkinfo info;
  613. __be16 count;
  614. __be16 usedbytes;
  615. __be16 firstused;
  616. __u8 holes;
  617. __u8 pad1;
  618. struct xfs_attr_leaf_map freemap[XFS_ATTR_LEAF_MAPSIZE];
  619. __be32 pad2; /* 64 bit alignment */
  620. };
  621. #define XFS_ATTR3_LEAF_CRC_OFF (offsetof(struct xfs_attr3_leaf_hdr, info.crc))
  622. struct xfs_attr3_leafblock {
  623. struct xfs_attr3_leaf_hdr hdr;
  624. struct xfs_attr_leaf_entry entries[1];
  625. /*
  626. * The rest of the block contains the following structures after the
  627. * leaf entries, growing from the bottom up. The variables are never
  628. * referenced, the locations accessed purely from helper functions.
  629. *
  630. * struct xfs_attr_leaf_name_local
  631. * struct xfs_attr_leaf_name_remote
  632. */
  633. };
  634. /*
  635. * incore, neutral version of the attribute leaf header
  636. */
  637. struct xfs_attr3_icleaf_hdr {
  638. uint32_t forw;
  639. uint32_t back;
  640. uint16_t magic;
  641. uint16_t count;
  642. uint16_t usedbytes;
  643. /*
  644. * firstused is 32-bit here instead of 16-bit like the on-disk variant
  645. * to support maximum fsb size of 64k without overflow issues throughout
  646. * the attr code. Instead, the overflow condition is handled on
  647. * conversion to/from disk.
  648. */
  649. uint32_t firstused;
  650. __u8 holes;
  651. struct {
  652. uint16_t base;
  653. uint16_t size;
  654. } freemap[XFS_ATTR_LEAF_MAPSIZE];
  655. };
  656. /*
  657. * Special value to represent fs block size in the leaf header firstused field.
  658. * Only used when block size overflows the 2-bytes available on disk.
  659. */
  660. #define XFS_ATTR3_LEAF_NULLOFF 0
  661. /*
  662. * Flags used in the leaf_entry[i].flags field.
  663. * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
  664. * on the system call, they are "or"ed together for various operations.
  665. */
  666. #define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
  667. #define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
  668. #define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
  669. #define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
  670. #define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
  671. #define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
  672. #define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
  673. #define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
  674. /*
  675. * Conversion macros for converting namespace bits from argument flags
  676. * to ondisk flags.
  677. */
  678. #define XFS_ATTR_NSP_ARGS_MASK (ATTR_ROOT | ATTR_SECURE)
  679. #define XFS_ATTR_NSP_ONDISK_MASK (XFS_ATTR_ROOT | XFS_ATTR_SECURE)
  680. #define XFS_ATTR_NSP_ONDISK(flags) ((flags) & XFS_ATTR_NSP_ONDISK_MASK)
  681. #define XFS_ATTR_NSP_ARGS(flags) ((flags) & XFS_ATTR_NSP_ARGS_MASK)
  682. #define XFS_ATTR_NSP_ARGS_TO_ONDISK(x) (((x) & ATTR_ROOT ? XFS_ATTR_ROOT : 0) |\
  683. ((x) & ATTR_SECURE ? XFS_ATTR_SECURE : 0))
  684. #define XFS_ATTR_NSP_ONDISK_TO_ARGS(x) (((x) & XFS_ATTR_ROOT ? ATTR_ROOT : 0) |\
  685. ((x) & XFS_ATTR_SECURE ? ATTR_SECURE : 0))
  686. /*
  687. * Alignment for namelist and valuelist entries (since they are mixed
  688. * there can be only one alignment value)
  689. */
  690. #define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
  691. static inline int
  692. xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock *leafp)
  693. {
  694. if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
  695. return sizeof(struct xfs_attr3_leaf_hdr);
  696. return sizeof(struct xfs_attr_leaf_hdr);
  697. }
  698. static inline struct xfs_attr_leaf_entry *
  699. xfs_attr3_leaf_entryp(xfs_attr_leafblock_t *leafp)
  700. {
  701. if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
  702. return &((struct xfs_attr3_leafblock *)leafp)->entries[0];
  703. return &leafp->entries[0];
  704. }
  705. /*
  706. * Cast typed pointers for "local" and "remote" name/value structs.
  707. */
  708. static inline char *
  709. xfs_attr3_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
  710. {
  711. struct xfs_attr_leaf_entry *entries = xfs_attr3_leaf_entryp(leafp);
  712. return &((char *)leafp)[be16_to_cpu(entries[idx].nameidx)];
  713. }
  714. static inline xfs_attr_leaf_name_remote_t *
  715. xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
  716. {
  717. return (xfs_attr_leaf_name_remote_t *)xfs_attr3_leaf_name(leafp, idx);
  718. }
  719. static inline xfs_attr_leaf_name_local_t *
  720. xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
  721. {
  722. return (xfs_attr_leaf_name_local_t *)xfs_attr3_leaf_name(leafp, idx);
  723. }
  724. /*
  725. * Calculate total bytes used (including trailing pad for alignment) for
  726. * a "local" name/value structure, a "remote" name/value structure, and
  727. * a pointer which might be either.
  728. */
  729. static inline int xfs_attr_leaf_entsize_remote(int nlen)
  730. {
  731. return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
  732. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  733. }
  734. static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
  735. {
  736. return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
  737. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  738. }
  739. static inline int xfs_attr_leaf_entsize_local_max(int bsize)
  740. {
  741. return (((bsize) >> 1) + ((bsize) >> 2));
  742. }
  743. /*
  744. * Remote attribute block format definition
  745. *
  746. * There is one of these headers per filesystem block in a remote attribute.
  747. * This is done to ensure there is a 1:1 mapping between the attribute value
  748. * length and the number of blocks needed to store the attribute. This makes the
  749. * verification of a buffer a little more complex, but greatly simplifies the
  750. * allocation, reading and writing of these attributes as we don't have to guess
  751. * the number of blocks needed to store the attribute data.
  752. */
  753. #define XFS_ATTR3_RMT_MAGIC 0x5841524d /* XARM */
  754. struct xfs_attr3_rmt_hdr {
  755. __be32 rm_magic;
  756. __be32 rm_offset;
  757. __be32 rm_bytes;
  758. __be32 rm_crc;
  759. uuid_t rm_uuid;
  760. __be64 rm_owner;
  761. __be64 rm_blkno;
  762. __be64 rm_lsn;
  763. };
  764. #define XFS_ATTR3_RMT_CRC_OFF offsetof(struct xfs_attr3_rmt_hdr, rm_crc)
  765. #define XFS_ATTR3_RMT_BUF_SPACE(mp, bufsize) \
  766. ((bufsize) - (xfs_sb_version_hascrc(&(mp)->m_sb) ? \
  767. sizeof(struct xfs_attr3_rmt_hdr) : 0))
  768. /* Number of bytes in a directory block. */
  769. static inline unsigned int xfs_dir2_dirblock_bytes(struct xfs_sb *sbp)
  770. {
  771. return 1 << (sbp->sb_blocklog + sbp->sb_dirblklog);
  772. }
  773. #endif /* __XFS_DA_FORMAT_H__ */