xfs_da_format.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
  176. * Only need 16 bits, this is the byte offset into the single block form.
  177. */
  178. typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
  179. /*
  180. * Offset in data space of a data entry.
  181. */
  182. typedef __uint32_t xfs_dir2_dataptr_t;
  183. #define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
  184. #define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
  185. /*
  186. * Byte offset in a directory.
  187. */
  188. typedef xfs_off_t xfs_dir2_off_t;
  189. /*
  190. * Directory block number (logical dirblk in file)
  191. */
  192. typedef __uint32_t xfs_dir2_db_t;
  193. /*
  194. * Inode number stored as 8 8-bit values.
  195. */
  196. typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
  197. /*
  198. * Inode number stored as 4 8-bit values.
  199. * Works a lot of the time, when all the inode numbers in a directory
  200. * fit in 32 bits.
  201. */
  202. typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
  203. typedef union {
  204. xfs_dir2_ino8_t i8;
  205. xfs_dir2_ino4_t i4;
  206. } xfs_dir2_inou_t;
  207. #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
  208. /*
  209. * Directory layout when stored internal to an inode.
  210. *
  211. * Small directories are packed as tightly as possible so as to fit into the
  212. * literal area of the inode. These "shortform" directories consist of a
  213. * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
  214. * structures. Due the different inode number storage size and the variable
  215. * length name field in the xfs_dir2_sf_entry all these structure are
  216. * variable length, and the accessors in this file should be used to iterate
  217. * over them.
  218. */
  219. typedef struct xfs_dir2_sf_hdr {
  220. __uint8_t count; /* count of entries */
  221. __uint8_t i8count; /* count of 8-byte inode #s */
  222. xfs_dir2_inou_t parent; /* parent dir inode number */
  223. } __arch_pack xfs_dir2_sf_hdr_t;
  224. typedef struct xfs_dir2_sf_entry {
  225. __u8 namelen; /* actual name length */
  226. xfs_dir2_sf_off_t offset; /* saved offset */
  227. __u8 name[]; /* name, variable size */
  228. /*
  229. * A single byte containing the file type field follows the inode
  230. * number for version 3 directory entries.
  231. *
  232. * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
  233. * variable offset after the name.
  234. */
  235. } __arch_pack xfs_dir2_sf_entry_t;
  236. static inline int xfs_dir2_sf_hdr_size(int i8count)
  237. {
  238. return sizeof(struct xfs_dir2_sf_hdr) -
  239. (i8count == 0) *
  240. (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
  241. }
  242. static inline xfs_dir2_data_aoff_t
  243. xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
  244. {
  245. return get_unaligned_be16(&sfep->offset.i);
  246. }
  247. static inline void
  248. xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
  249. {
  250. put_unaligned_be16(off, &sfep->offset.i);
  251. }
  252. static inline struct xfs_dir2_sf_entry *
  253. xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
  254. {
  255. return (struct xfs_dir2_sf_entry *)
  256. ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
  257. }
  258. /*
  259. * Data block structures.
  260. *
  261. * A pure data block looks like the following drawing on disk:
  262. *
  263. * +-------------------------------------------------+
  264. * | xfs_dir2_data_hdr_t |
  265. * +-------------------------------------------------+
  266. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  267. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  268. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  269. * | ... |
  270. * +-------------------------------------------------+
  271. * | unused space |
  272. * +-------------------------------------------------+
  273. *
  274. * As all the entries are variable size structures the accessors below should
  275. * be used to iterate over them.
  276. *
  277. * In addition to the pure data blocks for the data and node formats,
  278. * most structures are also used for the combined data/freespace "block"
  279. * format below.
  280. */
  281. #define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
  282. #define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
  283. #define XFS_DIR2_DATA_FREE_TAG 0xffff
  284. #define XFS_DIR2_DATA_FD_COUNT 3
  285. /*
  286. * Directory address space divided into sections,
  287. * spaces separated by 32GB.
  288. */
  289. #define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
  290. #define XFS_DIR2_DATA_SPACE 0
  291. #define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
  292. /*
  293. * Describe a free area in the data block.
  294. *
  295. * The freespace will be formatted as a xfs_dir2_data_unused_t.
  296. */
  297. typedef struct xfs_dir2_data_free {
  298. __be16 offset; /* start of freespace */
  299. __be16 length; /* length of freespace */
  300. } xfs_dir2_data_free_t;
  301. /*
  302. * Header for the data blocks.
  303. *
  304. * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
  305. */
  306. typedef struct xfs_dir2_data_hdr {
  307. __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
  308. /* XFS_DIR2_BLOCK_MAGIC */
  309. xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
  310. } xfs_dir2_data_hdr_t;
  311. /*
  312. * define a structure for all the verification fields we are adding to the
  313. * directory block structures. This will be used in several structures.
  314. * The magic number must be the first entry to align with all the dir2
  315. * structures so we determine how to decode them just by the magic number.
  316. */
  317. struct xfs_dir3_blk_hdr {
  318. __be32 magic; /* magic number */
  319. __be32 crc; /* CRC of block */
  320. __be64 blkno; /* first block of the buffer */
  321. __be64 lsn; /* sequence number of last write */
  322. uuid_t uuid; /* filesystem we belong to */
  323. __be64 owner; /* inode that owns the block */
  324. };
  325. struct xfs_dir3_data_hdr {
  326. struct xfs_dir3_blk_hdr hdr;
  327. xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
  328. __be32 pad; /* 64 bit alignment */
  329. };
  330. #define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
  331. /*
  332. * Active entry in a data block.
  333. *
  334. * Aligned to 8 bytes. After the variable length name field there is a
  335. * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
  336. *
  337. * For dir3 structures, there is file type field between the name and the tag.
  338. * This can only be manipulated by helper functions. It is packed hard against
  339. * the end of the name so any padding for rounding is between the file type and
  340. * the tag.
  341. */
  342. typedef struct xfs_dir2_data_entry {
  343. __be64 inumber; /* inode number */
  344. __u8 namelen; /* name length */
  345. __u8 name[]; /* name bytes, no null */
  346. /* __u8 filetype; */ /* type of inode we point to */
  347. /* __be16 tag; */ /* starting offset of us */
  348. } xfs_dir2_data_entry_t;
  349. /*
  350. * Unused entry in a data block.
  351. *
  352. * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
  353. * using xfs_dir2_data_unused_tag_p.
  354. */
  355. typedef struct xfs_dir2_data_unused {
  356. __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
  357. __be16 length; /* total free length */
  358. /* variable offset */
  359. __be16 tag; /* starting offset of us */
  360. } xfs_dir2_data_unused_t;
  361. /*
  362. * Pointer to a freespace's tag word.
  363. */
  364. static inline __be16 *
  365. xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
  366. {
  367. return (__be16 *)((char *)dup +
  368. be16_to_cpu(dup->length) - sizeof(__be16));
  369. }
  370. /*
  371. * Leaf block structures.
  372. *
  373. * A pure leaf block looks like the following drawing on disk:
  374. *
  375. * +---------------------------+
  376. * | xfs_dir2_leaf_hdr_t |
  377. * +---------------------------+
  378. * | xfs_dir2_leaf_entry_t |
  379. * | xfs_dir2_leaf_entry_t |
  380. * | xfs_dir2_leaf_entry_t |
  381. * | xfs_dir2_leaf_entry_t |
  382. * | ... |
  383. * +---------------------------+
  384. * | xfs_dir2_data_off_t |
  385. * | xfs_dir2_data_off_t |
  386. * | xfs_dir2_data_off_t |
  387. * | ... |
  388. * +---------------------------+
  389. * | xfs_dir2_leaf_tail_t |
  390. * +---------------------------+
  391. *
  392. * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
  393. * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
  394. * for directories with separate leaf nodes and free space blocks
  395. * (magic = XFS_DIR2_LEAFN_MAGIC).
  396. *
  397. * As all the entries are variable size structures the accessors below should
  398. * be used to iterate over them.
  399. */
  400. /*
  401. * Offset of the leaf/node space. First block in this space
  402. * is the btree root.
  403. */
  404. #define XFS_DIR2_LEAF_SPACE 1
  405. #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
  406. /*
  407. * Leaf block header.
  408. */
  409. typedef struct xfs_dir2_leaf_hdr {
  410. xfs_da_blkinfo_t info; /* header for da routines */
  411. __be16 count; /* count of entries */
  412. __be16 stale; /* count of stale entries */
  413. } xfs_dir2_leaf_hdr_t;
  414. struct xfs_dir3_leaf_hdr {
  415. struct xfs_da3_blkinfo info; /* header for da routines */
  416. __be16 count; /* count of entries */
  417. __be16 stale; /* count of stale entries */
  418. __be32 pad; /* 64 bit alignment */
  419. };
  420. struct xfs_dir3_icleaf_hdr {
  421. __uint32_t forw;
  422. __uint32_t back;
  423. __uint16_t magic;
  424. __uint16_t count;
  425. __uint16_t stale;
  426. };
  427. /*
  428. * Leaf block entry.
  429. */
  430. typedef struct xfs_dir2_leaf_entry {
  431. __be32 hashval; /* hash value of name */
  432. __be32 address; /* address of data entry */
  433. } xfs_dir2_leaf_entry_t;
  434. /*
  435. * Leaf block tail.
  436. */
  437. typedef struct xfs_dir2_leaf_tail {
  438. __be32 bestcount;
  439. } xfs_dir2_leaf_tail_t;
  440. /*
  441. * Leaf block.
  442. */
  443. typedef struct xfs_dir2_leaf {
  444. xfs_dir2_leaf_hdr_t hdr; /* leaf header */
  445. xfs_dir2_leaf_entry_t __ents[]; /* entries */
  446. } xfs_dir2_leaf_t;
  447. struct xfs_dir3_leaf {
  448. struct xfs_dir3_leaf_hdr hdr; /* leaf header */
  449. struct xfs_dir2_leaf_entry __ents[]; /* entries */
  450. };
  451. #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
  452. /*
  453. * Get address of the bests array in the single-leaf block.
  454. */
  455. static inline __be16 *
  456. xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
  457. {
  458. return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
  459. }
  460. /*
  461. * Free space block defintions for the node format.
  462. */
  463. /*
  464. * Offset of the freespace index.
  465. */
  466. #define XFS_DIR2_FREE_SPACE 2
  467. #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
  468. typedef struct xfs_dir2_free_hdr {
  469. __be32 magic; /* XFS_DIR2_FREE_MAGIC */
  470. __be32 firstdb; /* db of first entry */
  471. __be32 nvalid; /* count of valid entries */
  472. __be32 nused; /* count of used entries */
  473. } xfs_dir2_free_hdr_t;
  474. typedef struct xfs_dir2_free {
  475. xfs_dir2_free_hdr_t hdr; /* block header */
  476. __be16 bests[]; /* best free counts */
  477. /* unused entries are -1 */
  478. } xfs_dir2_free_t;
  479. struct xfs_dir3_free_hdr {
  480. struct xfs_dir3_blk_hdr hdr;
  481. __be32 firstdb; /* db of first entry */
  482. __be32 nvalid; /* count of valid entries */
  483. __be32 nused; /* count of used entries */
  484. __be32 pad; /* 64 bit alignment */
  485. };
  486. struct xfs_dir3_free {
  487. struct xfs_dir3_free_hdr hdr;
  488. __be16 bests[]; /* best free counts */
  489. /* unused entries are -1 */
  490. };
  491. #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
  492. /*
  493. * In core version of the free block header, abstracted away from on-disk format
  494. * differences. Use this in the code, and convert to/from the disk version using
  495. * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
  496. */
  497. struct xfs_dir3_icfree_hdr {
  498. __uint32_t magic;
  499. __uint32_t firstdb;
  500. __uint32_t nvalid;
  501. __uint32_t nused;
  502. };
  503. /*
  504. * Single block format.
  505. *
  506. * The single block format looks like the following drawing on disk:
  507. *
  508. * +-------------------------------------------------+
  509. * | xfs_dir2_data_hdr_t |
  510. * +-------------------------------------------------+
  511. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  512. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  513. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
  514. * | ... |
  515. * +-------------------------------------------------+
  516. * | unused space |
  517. * +-------------------------------------------------+
  518. * | ... |
  519. * | xfs_dir2_leaf_entry_t |
  520. * | xfs_dir2_leaf_entry_t |
  521. * +-------------------------------------------------+
  522. * | xfs_dir2_block_tail_t |
  523. * +-------------------------------------------------+
  524. *
  525. * As all the entries are variable size structures the accessors below should
  526. * be used to iterate over them.
  527. */
  528. typedef struct xfs_dir2_block_tail {
  529. __be32 count; /* count of leaf entries */
  530. __be32 stale; /* count of stale lf entries */
  531. } xfs_dir2_block_tail_t;
  532. /*
  533. * Pointer to the leaf entries embedded in a data block (1-block format)
  534. */
  535. static inline struct xfs_dir2_leaf_entry *
  536. xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
  537. {
  538. return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
  539. }
  540. /*
  541. * Attribute storage layout
  542. *
  543. * Attribute lists are structured around Btrees where all the data
  544. * elements are in the leaf nodes. Attribute names are hashed into an int,
  545. * then that int is used as the index into the Btree. Since the hashval
  546. * of an attribute name may not be unique, we may have duplicate keys. The
  547. * internal links in the Btree are logical block offsets into the file.
  548. *
  549. * Struct leaf_entry's are packed from the top. Name/values grow from the
  550. * bottom but are not packed. The freemap contains run-length-encoded entries
  551. * for the free bytes after the leaf_entry's, but only the N largest such,
  552. * smaller runs are dropped. When the freemap doesn't show enough space
  553. * for an allocation, we compact the name/value area and try again. If we
  554. * still don't have enough space, then we have to split the block. The
  555. * name/value structs (both local and remote versions) must be 32bit aligned.
  556. *
  557. * Since we have duplicate hash keys, for each key that matches, compare
  558. * the actual name string. The root and intermediate node search always
  559. * takes the first-in-the-block key match found, so we should only have
  560. * to work "forw"ard. If none matches, continue with the "forw"ard leaf
  561. * nodes until the hash key changes or the attribute name is found.
  562. *
  563. * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
  564. * the leaf_entry. The namespaces are independent only because we also look
  565. * at the namespace bit when we are looking for a matching attribute name.
  566. *
  567. * We also store an "incomplete" bit in the leaf_entry. It shows that an
  568. * attribute is in the middle of being created and should not be shown to
  569. * the user if we crash during the time that the bit is set. We clear the
  570. * bit when we have finished setting up the attribute. We do this because
  571. * we cannot create some large attributes inside a single transaction, and we
  572. * need some indication that we weren't finished if we crash in the middle.
  573. */
  574. #define XFS_ATTR_LEAF_MAPSIZE 3 /* how many freespace slots */
  575. typedef struct xfs_attr_leaf_map { /* RLE map of free bytes */
  576. __be16 base; /* base of free region */
  577. __be16 size; /* length of free region */
  578. } xfs_attr_leaf_map_t;
  579. typedef struct xfs_attr_leaf_hdr { /* constant-structure header block */
  580. xfs_da_blkinfo_t info; /* block type, links, etc. */
  581. __be16 count; /* count of active leaf_entry's */
  582. __be16 usedbytes; /* num bytes of names/values stored */
  583. __be16 firstused; /* first used byte in name area */
  584. __u8 holes; /* != 0 if blk needs compaction */
  585. __u8 pad1;
  586. xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
  587. /* N largest free regions */
  588. } xfs_attr_leaf_hdr_t;
  589. typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */
  590. __be32 hashval; /* hash value of name */
  591. __be16 nameidx; /* index into buffer of name/value */
  592. __u8 flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
  593. __u8 pad2; /* unused pad byte */
  594. } xfs_attr_leaf_entry_t;
  595. typedef struct xfs_attr_leaf_name_local {
  596. __be16 valuelen; /* number of bytes in value */
  597. __u8 namelen; /* length of name bytes */
  598. __u8 nameval[1]; /* name/value bytes */
  599. } xfs_attr_leaf_name_local_t;
  600. typedef struct xfs_attr_leaf_name_remote {
  601. __be32 valueblk; /* block number of value bytes */
  602. __be32 valuelen; /* number of bytes in value */
  603. __u8 namelen; /* length of name bytes */
  604. __u8 name[1]; /* name bytes */
  605. } xfs_attr_leaf_name_remote_t;
  606. typedef struct xfs_attr_leafblock {
  607. xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */
  608. xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */
  609. /*
  610. * The rest of the block contains the following structures after the
  611. * leaf entries, growing from the bottom up. The variables are never
  612. * referenced and definining them can actually make gcc optimize away
  613. * accesses to the 'entries' array above index 0 so don't do that.
  614. *
  615. * xfs_attr_leaf_name_local_t namelist;
  616. * xfs_attr_leaf_name_remote_t valuelist;
  617. */
  618. } xfs_attr_leafblock_t;
  619. /*
  620. * CRC enabled leaf structures. Called "version 3" structures to match the
  621. * version number of the directory and dablk structures for this feature, and
  622. * attr2 is already taken by the variable inode attribute fork size feature.
  623. */
  624. struct xfs_attr3_leaf_hdr {
  625. struct xfs_da3_blkinfo info;
  626. __be16 count;
  627. __be16 usedbytes;
  628. __be16 firstused;
  629. __u8 holes;
  630. __u8 pad1;
  631. struct xfs_attr_leaf_map freemap[XFS_ATTR_LEAF_MAPSIZE];
  632. __be32 pad2; /* 64 bit alignment */
  633. };
  634. #define XFS_ATTR3_LEAF_CRC_OFF (offsetof(struct xfs_attr3_leaf_hdr, info.crc))
  635. struct xfs_attr3_leafblock {
  636. struct xfs_attr3_leaf_hdr hdr;
  637. struct xfs_attr_leaf_entry entries[1];
  638. /*
  639. * The rest of the block contains the following structures after the
  640. * leaf entries, growing from the bottom up. The variables are never
  641. * referenced, the locations accessed purely from helper functions.
  642. *
  643. * struct xfs_attr_leaf_name_local
  644. * struct xfs_attr_leaf_name_remote
  645. */
  646. };
  647. /*
  648. * incore, neutral version of the attribute leaf header
  649. */
  650. struct xfs_attr3_icleaf_hdr {
  651. __uint32_t forw;
  652. __uint32_t back;
  653. __uint16_t magic;
  654. __uint16_t count;
  655. __uint16_t usedbytes;
  656. /*
  657. * firstused is 32-bit here instead of 16-bit like the on-disk variant
  658. * to support maximum fsb size of 64k without overflow issues throughout
  659. * the attr code. Instead, the overflow condition is handled on
  660. * conversion to/from disk.
  661. */
  662. __uint32_t firstused;
  663. __u8 holes;
  664. struct {
  665. __uint16_t base;
  666. __uint16_t size;
  667. } freemap[XFS_ATTR_LEAF_MAPSIZE];
  668. };
  669. /*
  670. * Special value to represent fs block size in the leaf header firstused field.
  671. * Only used when block size overflows the 2-bytes available on disk.
  672. */
  673. #define XFS_ATTR3_LEAF_NULLOFF 0
  674. /*
  675. * Flags used in the leaf_entry[i].flags field.
  676. * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
  677. * on the system call, they are "or"ed together for various operations.
  678. */
  679. #define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
  680. #define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
  681. #define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
  682. #define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
  683. #define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
  684. #define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
  685. #define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
  686. #define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
  687. /*
  688. * Conversion macros for converting namespace bits from argument flags
  689. * to ondisk flags.
  690. */
  691. #define XFS_ATTR_NSP_ARGS_MASK (ATTR_ROOT | ATTR_SECURE)
  692. #define XFS_ATTR_NSP_ONDISK_MASK (XFS_ATTR_ROOT | XFS_ATTR_SECURE)
  693. #define XFS_ATTR_NSP_ONDISK(flags) ((flags) & XFS_ATTR_NSP_ONDISK_MASK)
  694. #define XFS_ATTR_NSP_ARGS(flags) ((flags) & XFS_ATTR_NSP_ARGS_MASK)
  695. #define XFS_ATTR_NSP_ARGS_TO_ONDISK(x) (((x) & ATTR_ROOT ? XFS_ATTR_ROOT : 0) |\
  696. ((x) & ATTR_SECURE ? XFS_ATTR_SECURE : 0))
  697. #define XFS_ATTR_NSP_ONDISK_TO_ARGS(x) (((x) & XFS_ATTR_ROOT ? ATTR_ROOT : 0) |\
  698. ((x) & XFS_ATTR_SECURE ? ATTR_SECURE : 0))
  699. /*
  700. * Alignment for namelist and valuelist entries (since they are mixed
  701. * there can be only one alignment value)
  702. */
  703. #define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
  704. static inline int
  705. xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock *leafp)
  706. {
  707. if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
  708. return sizeof(struct xfs_attr3_leaf_hdr);
  709. return sizeof(struct xfs_attr_leaf_hdr);
  710. }
  711. static inline struct xfs_attr_leaf_entry *
  712. xfs_attr3_leaf_entryp(xfs_attr_leafblock_t *leafp)
  713. {
  714. if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
  715. return &((struct xfs_attr3_leafblock *)leafp)->entries[0];
  716. return &leafp->entries[0];
  717. }
  718. /*
  719. * Cast typed pointers for "local" and "remote" name/value structs.
  720. */
  721. static inline char *
  722. xfs_attr3_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
  723. {
  724. struct xfs_attr_leaf_entry *entries = xfs_attr3_leaf_entryp(leafp);
  725. return &((char *)leafp)[be16_to_cpu(entries[idx].nameidx)];
  726. }
  727. static inline xfs_attr_leaf_name_remote_t *
  728. xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
  729. {
  730. return (xfs_attr_leaf_name_remote_t *)xfs_attr3_leaf_name(leafp, idx);
  731. }
  732. static inline xfs_attr_leaf_name_local_t *
  733. xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
  734. {
  735. return (xfs_attr_leaf_name_local_t *)xfs_attr3_leaf_name(leafp, idx);
  736. }
  737. /*
  738. * Calculate total bytes used (including trailing pad for alignment) for
  739. * a "local" name/value structure, a "remote" name/value structure, and
  740. * a pointer which might be either.
  741. */
  742. static inline int xfs_attr_leaf_entsize_remote(int nlen)
  743. {
  744. return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
  745. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  746. }
  747. static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
  748. {
  749. return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
  750. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  751. }
  752. static inline int xfs_attr_leaf_entsize_local_max(int bsize)
  753. {
  754. return (((bsize) >> 1) + ((bsize) >> 2));
  755. }
  756. /*
  757. * Remote attribute block format definition
  758. *
  759. * There is one of these headers per filesystem block in a remote attribute.
  760. * This is done to ensure there is a 1:1 mapping between the attribute value
  761. * length and the number of blocks needed to store the attribute. This makes the
  762. * verification of a buffer a little more complex, but greatly simplifies the
  763. * allocation, reading and writing of these attributes as we don't have to guess
  764. * the number of blocks needed to store the attribute data.
  765. */
  766. #define XFS_ATTR3_RMT_MAGIC 0x5841524d /* XARM */
  767. struct xfs_attr3_rmt_hdr {
  768. __be32 rm_magic;
  769. __be32 rm_offset;
  770. __be32 rm_bytes;
  771. __be32 rm_crc;
  772. uuid_t rm_uuid;
  773. __be64 rm_owner;
  774. __be64 rm_blkno;
  775. __be64 rm_lsn;
  776. };
  777. #define XFS_ATTR3_RMT_CRC_OFF offsetof(struct xfs_attr3_rmt_hdr, rm_crc)
  778. #define XFS_ATTR3_RMT_BUF_SPACE(mp, bufsize) \
  779. ((bufsize) - (xfs_sb_version_hascrc(&(mp)->m_sb) ? \
  780. sizeof(struct xfs_attr3_rmt_hdr) : 0))
  781. #endif /* __XFS_DA_FORMAT_H__ */