xfs_da_format.h 29 KB

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