xfs_da_format.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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. * Directory Structure when greater than XFS_LBSIZE(mp) bytes.
  23. *========================================================================*/
  24. /*
  25. * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
  26. *
  27. * It is used to manage a doubly linked list of all blocks at the same
  28. * level in the Btree, and to identify which type of block this is.
  29. */
  30. #define XFS_DA_NODE_MAGIC 0xfebe /* magic number: non-leaf blocks */
  31. #define XFS_ATTR_LEAF_MAGIC 0xfbee /* magic number: attribute leaf blks */
  32. #define XFS_DIR2_LEAF1_MAGIC 0xd2f1 /* magic number: v2 dirlf single blks */
  33. #define XFS_DIR2_LEAFN_MAGIC 0xd2ff /* magic number: v2 dirlf multi blks */
  34. typedef struct xfs_da_blkinfo {
  35. __be32 forw; /* previous block in list */
  36. __be32 back; /* following block in list */
  37. __be16 magic; /* validity check on block */
  38. __be16 pad; /* unused */
  39. } xfs_da_blkinfo_t;
  40. /*
  41. * CRC enabled directory structure types
  42. *
  43. * The headers change size for the additional verification information, but
  44. * otherwise the tree layouts and contents are unchanged. Hence the da btree
  45. * code can use the struct xfs_da_blkinfo for manipulating the tree links and
  46. * magic numbers without modification for both v2 and v3 nodes.
  47. */
  48. #define XFS_DA3_NODE_MAGIC 0x3ebe /* magic number: non-leaf blocks */
  49. #define XFS_ATTR3_LEAF_MAGIC 0x3bee /* magic number: attribute leaf blks */
  50. #define XFS_DIR3_LEAF1_MAGIC 0x3df1 /* magic number: v2 dirlf single blks */
  51. #define XFS_DIR3_LEAFN_MAGIC 0x3dff /* magic number: v2 dirlf multi blks */
  52. struct xfs_da3_blkinfo {
  53. /*
  54. * the node link manipulation code relies on the fact that the first
  55. * element of this structure is the struct xfs_da_blkinfo so it can
  56. * ignore the differences in the rest of the structures.
  57. */
  58. struct xfs_da_blkinfo hdr;
  59. __be32 crc; /* CRC of block */
  60. __be64 blkno; /* first block of the buffer */
  61. __be64 lsn; /* sequence number of last write */
  62. uuid_t uuid; /* filesystem we belong to */
  63. __be64 owner; /* inode that owns the block */
  64. };
  65. /*
  66. * This is the structure of the root and intermediate nodes in the Btree.
  67. * The leaf nodes are defined above.
  68. *
  69. * Entries are not packed.
  70. *
  71. * Since we have duplicate keys, use a binary search but always follow
  72. * all match in the block, not just the first match found.
  73. */
  74. #define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
  75. typedef struct xfs_da_node_hdr {
  76. struct xfs_da_blkinfo info; /* block type, links, etc. */
  77. __be16 __count; /* count of active entries */
  78. __be16 __level; /* level above leaves (leaf == 0) */
  79. } xfs_da_node_hdr_t;
  80. struct xfs_da3_node_hdr {
  81. struct xfs_da3_blkinfo info; /* block type, links, etc. */
  82. __be16 __count; /* count of active entries */
  83. __be16 __level; /* level above leaves (leaf == 0) */
  84. __be32 __pad32;
  85. };
  86. #define XFS_DA3_NODE_CRC_OFF (offsetof(struct xfs_da3_node_hdr, info.crc))
  87. typedef struct xfs_da_node_entry {
  88. __be32 hashval; /* hash value for this descendant */
  89. __be32 before; /* Btree block before this key */
  90. } xfs_da_node_entry_t;
  91. typedef struct xfs_da_intnode {
  92. struct xfs_da_node_hdr hdr;
  93. struct xfs_da_node_entry __btree[];
  94. } xfs_da_intnode_t;
  95. struct xfs_da3_intnode {
  96. struct xfs_da3_node_hdr hdr;
  97. struct xfs_da_node_entry __btree[];
  98. };
  99. /*
  100. * In-core version of the node header to abstract the differences in the v2 and
  101. * v3 disk format of the headers. Callers need to convert to/from disk format as
  102. * appropriate.
  103. */
  104. struct xfs_da3_icnode_hdr {
  105. __uint32_t forw;
  106. __uint32_t back;
  107. __uint16_t magic;
  108. __uint16_t count;
  109. __uint16_t level;
  110. };
  111. extern void xfs_da3_node_hdr_from_disk(struct xfs_da3_icnode_hdr *to,
  112. struct xfs_da_intnode *from);
  113. extern void xfs_da3_node_hdr_to_disk(struct xfs_da_intnode *to,
  114. struct xfs_da3_icnode_hdr *from);
  115. static inline int
  116. __xfs_da3_node_hdr_size(bool v3)
  117. {
  118. if (v3)
  119. return sizeof(struct xfs_da3_node_hdr);
  120. return sizeof(struct xfs_da_node_hdr);
  121. }
  122. static inline int
  123. xfs_da3_node_hdr_size(struct xfs_da_intnode *dap)
  124. {
  125. bool v3 = dap->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC);
  126. return __xfs_da3_node_hdr_size(v3);
  127. }
  128. static inline struct xfs_da_node_entry *
  129. xfs_da3_node_tree_p(struct xfs_da_intnode *dap)
  130. {
  131. if (dap->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
  132. struct xfs_da3_intnode *dap3 = (struct xfs_da3_intnode *)dap;
  133. return dap3->__btree;
  134. }
  135. return dap->__btree;
  136. }
  137. extern void xfs_da3_intnode_from_disk(struct xfs_da3_icnode_hdr *to,
  138. struct xfs_da_intnode *from);
  139. extern void xfs_da3_intnode_to_disk(struct xfs_da_intnode *to,
  140. struct xfs_da3_icnode_hdr *from);
  141. #define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
  142. /*
  143. * Directory version 2.
  144. *
  145. * There are 4 possible formats:
  146. * - shortform - embedded into the inode
  147. * - single block - data with embedded leaf at the end
  148. * - multiple data blocks, single leaf+freeindex block
  149. * - data blocks, node and leaf blocks (btree), freeindex blocks
  150. *
  151. * Note: many node blocks structures and constants are shared with the attr
  152. * code and defined in xfs_da_btree.h.
  153. */
  154. #define XFS_DIR2_BLOCK_MAGIC 0x58443242 /* XD2B: single block dirs */
  155. #define XFS_DIR2_DATA_MAGIC 0x58443244 /* XD2D: multiblock dirs */
  156. #define XFS_DIR2_FREE_MAGIC 0x58443246 /* XD2F: free index blocks */
  157. /*
  158. * Directory Version 3 With CRCs.
  159. *
  160. * The tree formats are the same as for version 2 directories. The difference
  161. * is in the block header and dirent formats. In many cases the v3 structures
  162. * use v2 definitions as they are no different and this makes code sharing much
  163. * easier.
  164. *
  165. * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
  166. * format is v2 then they switch to the existing v2 code, or the format is v3
  167. * they implement the v3 functionality. This means the existing dir2 is a mix of
  168. * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
  169. * where there is a difference in the formats, otherwise the code is unchanged.
  170. *
  171. * Where it is possible, the code decides what to do based on the magic numbers
  172. * in the blocks rather than feature bits in the superblock. This means the code
  173. * is as independent of the external XFS code as possible as doesn't require
  174. * passing struct xfs_mount pointers into places where it isn't really
  175. * necessary.
  176. *
  177. * Version 3 includes:
  178. *
  179. * - a larger block header for CRC and identification purposes and so the
  180. * offsets of all the structures inside the blocks are different.
  181. *
  182. * - new magic numbers to be able to detect the v2/v3 types on the fly.
  183. */
  184. #define XFS_DIR3_BLOCK_MAGIC 0x58444233 /* XDB3: single block dirs */
  185. #define XFS_DIR3_DATA_MAGIC 0x58444433 /* XDD3: multiblock dirs */
  186. #define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */
  187. /*
  188. * Dirents in version 3 directories have a file type field. Additions to this
  189. * list are an on-disk format change, requiring feature bits. Valid values
  190. * are as follows:
  191. */
  192. #define XFS_DIR3_FT_UNKNOWN 0
  193. #define XFS_DIR3_FT_REG_FILE 1
  194. #define XFS_DIR3_FT_DIR 2
  195. #define XFS_DIR3_FT_CHRDEV 3
  196. #define XFS_DIR3_FT_BLKDEV 4
  197. #define XFS_DIR3_FT_FIFO 5
  198. #define XFS_DIR3_FT_SOCK 6
  199. #define XFS_DIR3_FT_SYMLINK 7
  200. #define XFS_DIR3_FT_WHT 8
  201. #define XFS_DIR3_FT_MAX 9
  202. /*
  203. * Byte offset in data block and shortform entry.
  204. */
  205. typedef __uint16_t xfs_dir2_data_off_t;
  206. #define NULLDATAOFF 0xffffU
  207. typedef uint xfs_dir2_data_aoff_t; /* argument form */
  208. /*
  209. * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
  210. * Only need 16 bits, this is the byte offset into the single block form.
  211. */
  212. typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
  213. /*
  214. * Offset in data space of a data entry.
  215. */
  216. typedef __uint32_t xfs_dir2_dataptr_t;
  217. #define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
  218. #define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
  219. /*
  220. * Byte offset in a directory.
  221. */
  222. typedef xfs_off_t xfs_dir2_off_t;
  223. /*
  224. * Directory block number (logical dirblk in file)
  225. */
  226. typedef __uint32_t xfs_dir2_db_t;
  227. /*
  228. * Inode number stored as 8 8-bit values.
  229. */
  230. typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
  231. /*
  232. * Inode number stored as 4 8-bit values.
  233. * Works a lot of the time, when all the inode numbers in a directory
  234. * fit in 32 bits.
  235. */
  236. typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
  237. typedef union {
  238. xfs_dir2_ino8_t i8;
  239. xfs_dir2_ino4_t i4;
  240. } xfs_dir2_inou_t;
  241. #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
  242. /*
  243. * Directory layout when stored internal to an inode.
  244. *
  245. * Small directories are packed as tightly as possible so as to fit into the
  246. * literal area of the inode. These "shortform" directories consist of a
  247. * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
  248. * structures. Due the different inode number storage size and the variable
  249. * length name field in the xfs_dir2_sf_entry all these structure are
  250. * variable length, and the accessors in this file should be used to iterate
  251. * over them.
  252. */
  253. typedef struct xfs_dir2_sf_hdr {
  254. __uint8_t count; /* count of entries */
  255. __uint8_t i8count; /* count of 8-byte inode #s */
  256. xfs_dir2_inou_t parent; /* parent dir inode number */
  257. } __arch_pack xfs_dir2_sf_hdr_t;
  258. typedef struct xfs_dir2_sf_entry {
  259. __u8 namelen; /* actual name length */
  260. xfs_dir2_sf_off_t offset; /* saved offset */
  261. __u8 name[]; /* name, variable size */
  262. /*
  263. * A single byte containing the file type field follows the inode
  264. * number for version 3 directory entries.
  265. *
  266. * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
  267. * variable offset after the name.
  268. */
  269. } __arch_pack xfs_dir2_sf_entry_t;
  270. static inline int xfs_dir2_sf_hdr_size(int i8count)
  271. {
  272. return sizeof(struct xfs_dir2_sf_hdr) -
  273. (i8count == 0) *
  274. (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
  275. }
  276. static inline xfs_dir2_data_aoff_t
  277. xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
  278. {
  279. return get_unaligned_be16(&sfep->offset.i);
  280. }
  281. static inline void
  282. xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
  283. {
  284. put_unaligned_be16(off, &sfep->offset.i);
  285. }
  286. static inline struct xfs_dir2_sf_entry *
  287. xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
  288. {
  289. return (struct xfs_dir2_sf_entry *)
  290. ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
  291. }
  292. static inline int
  293. xfs_dir3_sf_entsize(
  294. struct xfs_mount *mp,
  295. struct xfs_dir2_sf_hdr *hdr,
  296. int len)
  297. {
  298. int count = sizeof(struct xfs_dir2_sf_entry); /* namelen + offset */
  299. count += len; /* name */
  300. count += hdr->i8count ? sizeof(xfs_dir2_ino8_t) :
  301. sizeof(xfs_dir2_ino4_t); /* ino # */
  302. if (xfs_sb_version_hasftype(&mp->m_sb))
  303. count += sizeof(__uint8_t); /* file type */
  304. return count;
  305. }
  306. static inline struct xfs_dir2_sf_entry *
  307. xfs_dir3_sf_nextentry(
  308. struct xfs_mount *mp,
  309. struct xfs_dir2_sf_hdr *hdr,
  310. struct xfs_dir2_sf_entry *sfep)
  311. {
  312. return (struct xfs_dir2_sf_entry *)
  313. ((char *)sfep + xfs_dir3_sf_entsize(mp, hdr, sfep->namelen));
  314. }
  315. /*
  316. * in dir3 shortform directories, the file type field is stored at a variable
  317. * offset after the inode number. Because it's only a single byte, endian
  318. * conversion is not necessary.
  319. */
  320. static inline __uint8_t *
  321. xfs_dir3_sfe_ftypep(
  322. struct xfs_dir2_sf_hdr *hdr,
  323. struct xfs_dir2_sf_entry *sfep)
  324. {
  325. return (__uint8_t *)&sfep->name[sfep->namelen];
  326. }
  327. static inline __uint8_t
  328. xfs_dir3_sfe_get_ftype(
  329. struct xfs_mount *mp,
  330. struct xfs_dir2_sf_hdr *hdr,
  331. struct xfs_dir2_sf_entry *sfep)
  332. {
  333. __uint8_t *ftp;
  334. if (!xfs_sb_version_hasftype(&mp->m_sb))
  335. return XFS_DIR3_FT_UNKNOWN;
  336. ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
  337. if (*ftp >= XFS_DIR3_FT_MAX)
  338. return XFS_DIR3_FT_UNKNOWN;
  339. return *ftp;
  340. }
  341. static inline void
  342. xfs_dir3_sfe_put_ftype(
  343. struct xfs_mount *mp,
  344. struct xfs_dir2_sf_hdr *hdr,
  345. struct xfs_dir2_sf_entry *sfep,
  346. __uint8_t ftype)
  347. {
  348. __uint8_t *ftp;
  349. ASSERT(ftype < XFS_DIR3_FT_MAX);
  350. if (!xfs_sb_version_hasftype(&mp->m_sb))
  351. return;
  352. ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
  353. *ftp = ftype;
  354. }
  355. /*
  356. * Data block structures.
  357. *
  358. * A pure data block looks like the following drawing on disk:
  359. *
  360. * +-------------------------------------------------+
  361. * | xfs_dir2_data_hdr_t |
  362. * +-------------------------------------------------+
  363. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  364. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  365. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  366. * | ... |
  367. * +-------------------------------------------------+
  368. * | unused space |
  369. * +-------------------------------------------------+
  370. *
  371. * As all the entries are variable size structures the accessors below should
  372. * be used to iterate over them.
  373. *
  374. * In addition to the pure data blocks for the data and node formats,
  375. * most structures are also used for the combined data/freespace "block"
  376. * format below.
  377. */
  378. #define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
  379. #define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
  380. #define XFS_DIR2_DATA_FREE_TAG 0xffff
  381. #define XFS_DIR2_DATA_FD_COUNT 3
  382. /*
  383. * Directory address space divided into sections,
  384. * spaces separated by 32GB.
  385. */
  386. #define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
  387. #define XFS_DIR2_DATA_SPACE 0
  388. #define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
  389. #define XFS_DIR2_DATA_FIRSTDB(mp) \
  390. xfs_dir2_byte_to_db(mp, XFS_DIR2_DATA_OFFSET)
  391. /*
  392. * Describe a free area in the data block.
  393. *
  394. * The freespace will be formatted as a xfs_dir2_data_unused_t.
  395. */
  396. typedef struct xfs_dir2_data_free {
  397. __be16 offset; /* start of freespace */
  398. __be16 length; /* length of freespace */
  399. } xfs_dir2_data_free_t;
  400. /*
  401. * Header for the data blocks.
  402. *
  403. * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
  404. */
  405. typedef struct xfs_dir2_data_hdr {
  406. __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
  407. /* XFS_DIR2_BLOCK_MAGIC */
  408. xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
  409. } xfs_dir2_data_hdr_t;
  410. /*
  411. * define a structure for all the verification fields we are adding to the
  412. * directory block structures. This will be used in several structures.
  413. * The magic number must be the first entry to align with all the dir2
  414. * structures so we determine how to decode them just by the magic number.
  415. */
  416. struct xfs_dir3_blk_hdr {
  417. __be32 magic; /* magic number */
  418. __be32 crc; /* CRC of block */
  419. __be64 blkno; /* first block of the buffer */
  420. __be64 lsn; /* sequence number of last write */
  421. uuid_t uuid; /* filesystem we belong to */
  422. __be64 owner; /* inode that owns the block */
  423. };
  424. struct xfs_dir3_data_hdr {
  425. struct xfs_dir3_blk_hdr hdr;
  426. xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
  427. __be32 pad; /* 64 bit alignment */
  428. };
  429. #define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
  430. static inline struct xfs_dir2_data_free *
  431. xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
  432. {
  433. if (hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  434. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  435. struct xfs_dir3_data_hdr *hdr3 = (struct xfs_dir3_data_hdr *)hdr;
  436. return hdr3->best_free;
  437. }
  438. return hdr->bestfree;
  439. }
  440. /*
  441. * Active entry in a data block.
  442. *
  443. * Aligned to 8 bytes. After the variable length name field there is a
  444. * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
  445. *
  446. * For dir3 structures, there is file type field between the name and the tag.
  447. * This can only be manipulated by helper functions. It is packed hard against
  448. * the end of the name so any padding for rounding is between the file type and
  449. * the tag.
  450. */
  451. typedef struct xfs_dir2_data_entry {
  452. __be64 inumber; /* inode number */
  453. __u8 namelen; /* name length */
  454. __u8 name[]; /* name bytes, no null */
  455. /* __u8 filetype; */ /* type of inode we point to */
  456. /* __be16 tag; */ /* starting offset of us */
  457. } xfs_dir2_data_entry_t;
  458. /*
  459. * Unused entry in a data block.
  460. *
  461. * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
  462. * using xfs_dir2_data_unused_tag_p.
  463. */
  464. typedef struct xfs_dir2_data_unused {
  465. __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
  466. __be16 length; /* total free length */
  467. /* variable offset */
  468. __be16 tag; /* starting offset of us */
  469. } xfs_dir2_data_unused_t;
  470. /*
  471. * Size of a data entry.
  472. */
  473. static inline int
  474. __xfs_dir3_data_entsize(
  475. bool ftype,
  476. int n)
  477. {
  478. int size = offsetof(struct xfs_dir2_data_entry, name[0]);
  479. size += n;
  480. size += sizeof(xfs_dir2_data_off_t);
  481. if (ftype)
  482. size += sizeof(__uint8_t);
  483. return roundup(size, XFS_DIR2_DATA_ALIGN);
  484. }
  485. static inline int
  486. xfs_dir3_data_entsize(
  487. struct xfs_mount *mp,
  488. int n)
  489. {
  490. bool ftype = xfs_sb_version_hasftype(&mp->m_sb) ? true : false;
  491. return __xfs_dir3_data_entsize(ftype, n);
  492. }
  493. static inline __uint8_t
  494. xfs_dir3_dirent_get_ftype(
  495. struct xfs_mount *mp,
  496. struct xfs_dir2_data_entry *dep)
  497. {
  498. if (xfs_sb_version_hasftype(&mp->m_sb)) {
  499. __uint8_t type = dep->name[dep->namelen];
  500. ASSERT(type < XFS_DIR3_FT_MAX);
  501. if (type < XFS_DIR3_FT_MAX)
  502. return type;
  503. }
  504. return XFS_DIR3_FT_UNKNOWN;
  505. }
  506. static inline void
  507. xfs_dir3_dirent_put_ftype(
  508. struct xfs_mount *mp,
  509. struct xfs_dir2_data_entry *dep,
  510. __uint8_t type)
  511. {
  512. ASSERT(type < XFS_DIR3_FT_MAX);
  513. ASSERT(dep->namelen != 0);
  514. if (xfs_sb_version_hasftype(&mp->m_sb))
  515. dep->name[dep->namelen] = type;
  516. }
  517. /*
  518. * Pointer to an entry's tag word.
  519. */
  520. static inline __be16 *
  521. xfs_dir3_data_entry_tag_p(
  522. struct xfs_mount *mp,
  523. struct xfs_dir2_data_entry *dep)
  524. {
  525. return (__be16 *)((char *)dep +
  526. xfs_dir3_data_entsize(mp, dep->namelen) - sizeof(__be16));
  527. }
  528. /*
  529. * Pointer to a freespace's tag word.
  530. */
  531. static inline __be16 *
  532. xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
  533. {
  534. return (__be16 *)((char *)dup +
  535. be16_to_cpu(dup->length) - sizeof(__be16));
  536. }
  537. static inline size_t
  538. xfs_dir3_data_hdr_size(bool dir3)
  539. {
  540. if (dir3)
  541. return sizeof(struct xfs_dir3_data_hdr);
  542. return sizeof(struct xfs_dir2_data_hdr);
  543. }
  544. static inline size_t
  545. xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr)
  546. {
  547. bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  548. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
  549. return xfs_dir3_data_hdr_size(dir3);
  550. }
  551. static inline struct xfs_dir2_data_entry *
  552. xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
  553. {
  554. return (struct xfs_dir2_data_entry *)
  555. ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
  556. }
  557. static inline struct xfs_dir2_data_unused *
  558. xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
  559. {
  560. return (struct xfs_dir2_data_unused *)
  561. ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
  562. }
  563. /*
  564. * Offsets of . and .. in data space (always block 0)
  565. *
  566. * XXX: there is scope for significant optimisation of the logic here. Right
  567. * now we are checking for "dir3 format" over and over again. Ideally we should
  568. * only do it once for each operation.
  569. */
  570. static inline xfs_dir2_data_aoff_t
  571. xfs_dir3_data_dot_offset(struct xfs_mount *mp)
  572. {
  573. return xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb));
  574. }
  575. static inline xfs_dir2_data_aoff_t
  576. xfs_dir3_data_dotdot_offset(struct xfs_mount *mp)
  577. {
  578. return xfs_dir3_data_dot_offset(mp) +
  579. xfs_dir3_data_entsize(mp, 1);
  580. }
  581. static inline xfs_dir2_data_aoff_t
  582. xfs_dir3_data_first_offset(struct xfs_mount *mp)
  583. {
  584. return xfs_dir3_data_dotdot_offset(mp) +
  585. xfs_dir3_data_entsize(mp, 2);
  586. }
  587. /*
  588. * location of . and .. in data space (always block 0)
  589. */
  590. static inline struct xfs_dir2_data_entry *
  591. xfs_dir3_data_dot_entry_p(
  592. struct xfs_mount *mp,
  593. struct xfs_dir2_data_hdr *hdr)
  594. {
  595. return (struct xfs_dir2_data_entry *)
  596. ((char *)hdr + xfs_dir3_data_dot_offset(mp));
  597. }
  598. static inline struct xfs_dir2_data_entry *
  599. xfs_dir3_data_dotdot_entry_p(
  600. struct xfs_mount *mp,
  601. struct xfs_dir2_data_hdr *hdr)
  602. {
  603. return (struct xfs_dir2_data_entry *)
  604. ((char *)hdr + xfs_dir3_data_dotdot_offset(mp));
  605. }
  606. static inline struct xfs_dir2_data_entry *
  607. xfs_dir3_data_first_entry_p(
  608. struct xfs_mount *mp,
  609. struct xfs_dir2_data_hdr *hdr)
  610. {
  611. return (struct xfs_dir2_data_entry *)
  612. ((char *)hdr + xfs_dir3_data_first_offset(mp));
  613. }
  614. /*
  615. * Leaf block structures.
  616. *
  617. * A pure leaf block looks like the following drawing on disk:
  618. *
  619. * +---------------------------+
  620. * | xfs_dir2_leaf_hdr_t |
  621. * +---------------------------+
  622. * | xfs_dir2_leaf_entry_t |
  623. * | xfs_dir2_leaf_entry_t |
  624. * | xfs_dir2_leaf_entry_t |
  625. * | xfs_dir2_leaf_entry_t |
  626. * | ... |
  627. * +---------------------------+
  628. * | xfs_dir2_data_off_t |
  629. * | xfs_dir2_data_off_t |
  630. * | xfs_dir2_data_off_t |
  631. * | ... |
  632. * +---------------------------+
  633. * | xfs_dir2_leaf_tail_t |
  634. * +---------------------------+
  635. *
  636. * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
  637. * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
  638. * for directories with separate leaf nodes and free space blocks
  639. * (magic = XFS_DIR2_LEAFN_MAGIC).
  640. *
  641. * As all the entries are variable size structures the accessors below should
  642. * be used to iterate over them.
  643. */
  644. /*
  645. * Offset of the leaf/node space. First block in this space
  646. * is the btree root.
  647. */
  648. #define XFS_DIR2_LEAF_SPACE 1
  649. #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
  650. #define XFS_DIR2_LEAF_FIRSTDB(mp) \
  651. xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
  652. /*
  653. * Leaf block header.
  654. */
  655. typedef struct xfs_dir2_leaf_hdr {
  656. xfs_da_blkinfo_t info; /* header for da routines */
  657. __be16 count; /* count of entries */
  658. __be16 stale; /* count of stale entries */
  659. } xfs_dir2_leaf_hdr_t;
  660. struct xfs_dir3_leaf_hdr {
  661. struct xfs_da3_blkinfo info; /* header for da routines */
  662. __be16 count; /* count of entries */
  663. __be16 stale; /* count of stale entries */
  664. __be32 pad; /* 64 bit alignment */
  665. };
  666. struct xfs_dir3_icleaf_hdr {
  667. __uint32_t forw;
  668. __uint32_t back;
  669. __uint16_t magic;
  670. __uint16_t count;
  671. __uint16_t stale;
  672. };
  673. /*
  674. * Leaf block entry.
  675. */
  676. typedef struct xfs_dir2_leaf_entry {
  677. __be32 hashval; /* hash value of name */
  678. __be32 address; /* address of data entry */
  679. } xfs_dir2_leaf_entry_t;
  680. /*
  681. * Leaf block tail.
  682. */
  683. typedef struct xfs_dir2_leaf_tail {
  684. __be32 bestcount;
  685. } xfs_dir2_leaf_tail_t;
  686. /*
  687. * Leaf block.
  688. */
  689. typedef struct xfs_dir2_leaf {
  690. xfs_dir2_leaf_hdr_t hdr; /* leaf header */
  691. xfs_dir2_leaf_entry_t __ents[]; /* entries */
  692. } xfs_dir2_leaf_t;
  693. struct xfs_dir3_leaf {
  694. struct xfs_dir3_leaf_hdr hdr; /* leaf header */
  695. struct xfs_dir2_leaf_entry __ents[]; /* entries */
  696. };
  697. #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
  698. extern void xfs_dir3_leaf_hdr_from_disk(struct xfs_dir3_icleaf_hdr *to,
  699. struct xfs_dir2_leaf *from);
  700. static inline int
  701. xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
  702. {
  703. if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  704. lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
  705. return sizeof(struct xfs_dir3_leaf_hdr);
  706. return sizeof(struct xfs_dir2_leaf_hdr);
  707. }
  708. static inline int
  709. xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
  710. {
  711. return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
  712. (uint)sizeof(struct xfs_dir2_leaf_entry);
  713. }
  714. /*
  715. * Get address of the bestcount field in the single-leaf block.
  716. */
  717. static inline struct xfs_dir2_leaf_entry *
  718. xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
  719. {
  720. if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  721. lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
  722. struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp;
  723. return lp3->__ents;
  724. }
  725. return lp->__ents;
  726. }
  727. /*
  728. * Get address of the bestcount field in the single-leaf block.
  729. */
  730. static inline struct xfs_dir2_leaf_tail *
  731. xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
  732. {
  733. return (struct xfs_dir2_leaf_tail *)
  734. ((char *)lp + mp->m_dirblksize -
  735. sizeof(struct xfs_dir2_leaf_tail));
  736. }
  737. /*
  738. * Get address of the bests array in the single-leaf block.
  739. */
  740. static inline __be16 *
  741. xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
  742. {
  743. return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
  744. }
  745. /*
  746. * DB blocks here are logical directory block numbers, not filesystem blocks.
  747. */
  748. /*
  749. * Convert dataptr to byte in file space
  750. */
  751. static inline xfs_dir2_off_t
  752. xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  753. {
  754. return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
  755. }
  756. /*
  757. * Convert byte in file space to dataptr. It had better be aligned.
  758. */
  759. static inline xfs_dir2_dataptr_t
  760. xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
  761. {
  762. return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
  763. }
  764. /*
  765. * Convert byte in space to (DB) block
  766. */
  767. static inline xfs_dir2_db_t
  768. xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
  769. {
  770. return (xfs_dir2_db_t)
  771. (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
  772. }
  773. /*
  774. * Convert dataptr to a block number
  775. */
  776. static inline xfs_dir2_db_t
  777. xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  778. {
  779. return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  780. }
  781. /*
  782. * Convert byte in space to offset in a block
  783. */
  784. static inline xfs_dir2_data_aoff_t
  785. xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
  786. {
  787. return (xfs_dir2_data_aoff_t)(by &
  788. ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
  789. }
  790. /*
  791. * Convert dataptr to a byte offset in a block
  792. */
  793. static inline xfs_dir2_data_aoff_t
  794. xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  795. {
  796. return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  797. }
  798. /*
  799. * Convert block and offset to byte in space
  800. */
  801. static inline xfs_dir2_off_t
  802. xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
  803. xfs_dir2_data_aoff_t o)
  804. {
  805. return ((xfs_dir2_off_t)db <<
  806. (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
  807. }
  808. /*
  809. * Convert block (DB) to block (dablk)
  810. */
  811. static inline xfs_dablk_t
  812. xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
  813. {
  814. return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
  815. }
  816. /*
  817. * Convert byte in space to (DA) block
  818. */
  819. static inline xfs_dablk_t
  820. xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
  821. {
  822. return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
  823. }
  824. /*
  825. * Convert block and offset to dataptr
  826. */
  827. static inline xfs_dir2_dataptr_t
  828. xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
  829. xfs_dir2_data_aoff_t o)
  830. {
  831. return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
  832. }
  833. /*
  834. * Convert block (dablk) to block (DB)
  835. */
  836. static inline xfs_dir2_db_t
  837. xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
  838. {
  839. return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
  840. }
  841. /*
  842. * Convert block (dablk) to byte offset in space
  843. */
  844. static inline xfs_dir2_off_t
  845. xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
  846. {
  847. return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
  848. }
  849. /*
  850. * Free space block defintions for the node format.
  851. */
  852. /*
  853. * Offset of the freespace index.
  854. */
  855. #define XFS_DIR2_FREE_SPACE 2
  856. #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
  857. #define XFS_DIR2_FREE_FIRSTDB(mp) \
  858. xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
  859. typedef struct xfs_dir2_free_hdr {
  860. __be32 magic; /* XFS_DIR2_FREE_MAGIC */
  861. __be32 firstdb; /* db of first entry */
  862. __be32 nvalid; /* count of valid entries */
  863. __be32 nused; /* count of used entries */
  864. } xfs_dir2_free_hdr_t;
  865. typedef struct xfs_dir2_free {
  866. xfs_dir2_free_hdr_t hdr; /* block header */
  867. __be16 bests[]; /* best free counts */
  868. /* unused entries are -1 */
  869. } xfs_dir2_free_t;
  870. struct xfs_dir3_free_hdr {
  871. struct xfs_dir3_blk_hdr hdr;
  872. __be32 firstdb; /* db of first entry */
  873. __be32 nvalid; /* count of valid entries */
  874. __be32 nused; /* count of used entries */
  875. __be32 pad; /* 64 bit alignment */
  876. };
  877. struct xfs_dir3_free {
  878. struct xfs_dir3_free_hdr hdr;
  879. __be16 bests[]; /* best free counts */
  880. /* unused entries are -1 */
  881. };
  882. #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
  883. /*
  884. * In core version of the free block header, abstracted away from on-disk format
  885. * differences. Use this in the code, and convert to/from the disk version using
  886. * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
  887. */
  888. struct xfs_dir3_icfree_hdr {
  889. __uint32_t magic;
  890. __uint32_t firstdb;
  891. __uint32_t nvalid;
  892. __uint32_t nused;
  893. };
  894. void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
  895. struct xfs_dir2_free *from);
  896. static inline int
  897. xfs_dir3_free_hdr_size(struct xfs_mount *mp)
  898. {
  899. if (xfs_sb_version_hascrc(&mp->m_sb))
  900. return sizeof(struct xfs_dir3_free_hdr);
  901. return sizeof(struct xfs_dir2_free_hdr);
  902. }
  903. static inline int
  904. xfs_dir3_free_max_bests(struct xfs_mount *mp)
  905. {
  906. return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
  907. sizeof(xfs_dir2_data_off_t);
  908. }
  909. static inline __be16 *
  910. xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
  911. {
  912. return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
  913. }
  914. /*
  915. * Convert data space db to the corresponding free db.
  916. */
  917. static inline xfs_dir2_db_t
  918. xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
  919. {
  920. return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
  921. }
  922. /*
  923. * Convert data space db to the corresponding index in a free db.
  924. */
  925. static inline int
  926. xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
  927. {
  928. return db % xfs_dir3_free_max_bests(mp);
  929. }
  930. /*
  931. * Single block format.
  932. *
  933. * The single block format looks like the following drawing on disk:
  934. *
  935. * +-------------------------------------------------+
  936. * | xfs_dir2_data_hdr_t |
  937. * +-------------------------------------------------+
  938. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  939. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  940. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
  941. * | ... |
  942. * +-------------------------------------------------+
  943. * | unused space |
  944. * +-------------------------------------------------+
  945. * | ... |
  946. * | xfs_dir2_leaf_entry_t |
  947. * | xfs_dir2_leaf_entry_t |
  948. * +-------------------------------------------------+
  949. * | xfs_dir2_block_tail_t |
  950. * +-------------------------------------------------+
  951. *
  952. * As all the entries are variable size structures the accessors below should
  953. * be used to iterate over them.
  954. */
  955. typedef struct xfs_dir2_block_tail {
  956. __be32 count; /* count of leaf entries */
  957. __be32 stale; /* count of stale lf entries */
  958. } xfs_dir2_block_tail_t;
  959. /*
  960. * Pointer to the leaf header embedded in a data block (1-block format)
  961. */
  962. static inline struct xfs_dir2_block_tail *
  963. xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
  964. {
  965. return ((struct xfs_dir2_block_tail *)
  966. ((char *)hdr + mp->m_dirblksize)) - 1;
  967. }
  968. /*
  969. * Pointer to the leaf entries embedded in a data block (1-block format)
  970. */
  971. static inline struct xfs_dir2_leaf_entry *
  972. xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
  973. {
  974. return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
  975. }
  976. /*
  977. * Attribute storage layout
  978. *
  979. * Attribute lists are structured around Btrees where all the data
  980. * elements are in the leaf nodes. Attribute names are hashed into an int,
  981. * then that int is used as the index into the Btree. Since the hashval
  982. * of an attribute name may not be unique, we may have duplicate keys. The
  983. * internal links in the Btree are logical block offsets into the file.
  984. *
  985. *========================================================================
  986. * Attribute structure when equal to XFS_LBSIZE(mp) bytes.
  987. *========================================================================
  988. *
  989. * Struct leaf_entry's are packed from the top. Name/values grow from the
  990. * bottom but are not packed. The freemap contains run-length-encoded entries
  991. * for the free bytes after the leaf_entry's, but only the N largest such,
  992. * smaller runs are dropped. When the freemap doesn't show enough space
  993. * for an allocation, we compact the name/value area and try again. If we
  994. * still don't have enough space, then we have to split the block. The
  995. * name/value structs (both local and remote versions) must be 32bit aligned.
  996. *
  997. * Since we have duplicate hash keys, for each key that matches, compare
  998. * the actual name string. The root and intermediate node search always
  999. * takes the first-in-the-block key match found, so we should only have
  1000. * to work "forw"ard. If none matches, continue with the "forw"ard leaf
  1001. * nodes until the hash key changes or the attribute name is found.
  1002. *
  1003. * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
  1004. * the leaf_entry. The namespaces are independent only because we also look
  1005. * at the namespace bit when we are looking for a matching attribute name.
  1006. *
  1007. * We also store an "incomplete" bit in the leaf_entry. It shows that an
  1008. * attribute is in the middle of being created and should not be shown to
  1009. * the user if we crash during the time that the bit is set. We clear the
  1010. * bit when we have finished setting up the attribute. We do this because
  1011. * we cannot create some large attributes inside a single transaction, and we
  1012. * need some indication that we weren't finished if we crash in the middle.
  1013. */
  1014. #define XFS_ATTR_LEAF_MAPSIZE 3 /* how many freespace slots */
  1015. typedef struct xfs_attr_leaf_map { /* RLE map of free bytes */
  1016. __be16 base; /* base of free region */
  1017. __be16 size; /* length of free region */
  1018. } xfs_attr_leaf_map_t;
  1019. typedef struct xfs_attr_leaf_hdr { /* constant-structure header block */
  1020. xfs_da_blkinfo_t info; /* block type, links, etc. */
  1021. __be16 count; /* count of active leaf_entry's */
  1022. __be16 usedbytes; /* num bytes of names/values stored */
  1023. __be16 firstused; /* first used byte in name area */
  1024. __u8 holes; /* != 0 if blk needs compaction */
  1025. __u8 pad1;
  1026. xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
  1027. /* N largest free regions */
  1028. } xfs_attr_leaf_hdr_t;
  1029. typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */
  1030. __be32 hashval; /* hash value of name */
  1031. __be16 nameidx; /* index into buffer of name/value */
  1032. __u8 flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
  1033. __u8 pad2; /* unused pad byte */
  1034. } xfs_attr_leaf_entry_t;
  1035. typedef struct xfs_attr_leaf_name_local {
  1036. __be16 valuelen; /* number of bytes in value */
  1037. __u8 namelen; /* length of name bytes */
  1038. __u8 nameval[1]; /* name/value bytes */
  1039. } xfs_attr_leaf_name_local_t;
  1040. typedef struct xfs_attr_leaf_name_remote {
  1041. __be32 valueblk; /* block number of value bytes */
  1042. __be32 valuelen; /* number of bytes in value */
  1043. __u8 namelen; /* length of name bytes */
  1044. __u8 name[1]; /* name bytes */
  1045. } xfs_attr_leaf_name_remote_t;
  1046. typedef struct xfs_attr_leafblock {
  1047. xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */
  1048. xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */
  1049. xfs_attr_leaf_name_local_t namelist; /* grows from bottom of buf */
  1050. xfs_attr_leaf_name_remote_t valuelist; /* grows from bottom of buf */
  1051. } xfs_attr_leafblock_t;
  1052. /*
  1053. * CRC enabled leaf structures. Called "version 3" structures to match the
  1054. * version number of the directory and dablk structures for this feature, and
  1055. * attr2 is already taken by the variable inode attribute fork size feature.
  1056. */
  1057. struct xfs_attr3_leaf_hdr {
  1058. struct xfs_da3_blkinfo info;
  1059. __be16 count;
  1060. __be16 usedbytes;
  1061. __be16 firstused;
  1062. __u8 holes;
  1063. __u8 pad1;
  1064. struct xfs_attr_leaf_map freemap[XFS_ATTR_LEAF_MAPSIZE];
  1065. __be32 pad2; /* 64 bit alignment */
  1066. };
  1067. #define XFS_ATTR3_LEAF_CRC_OFF (offsetof(struct xfs_attr3_leaf_hdr, info.crc))
  1068. struct xfs_attr3_leafblock {
  1069. struct xfs_attr3_leaf_hdr hdr;
  1070. struct xfs_attr_leaf_entry entries[1];
  1071. /*
  1072. * The rest of the block contains the following structures after the
  1073. * leaf entries, growing from the bottom up. The variables are never
  1074. * referenced, the locations accessed purely from helper functions.
  1075. *
  1076. * struct xfs_attr_leaf_name_local
  1077. * struct xfs_attr_leaf_name_remote
  1078. */
  1079. };
  1080. /*
  1081. * incore, neutral version of the attribute leaf header
  1082. */
  1083. struct xfs_attr3_icleaf_hdr {
  1084. __uint32_t forw;
  1085. __uint32_t back;
  1086. __uint16_t magic;
  1087. __uint16_t count;
  1088. __uint16_t usedbytes;
  1089. __uint16_t firstused;
  1090. __u8 holes;
  1091. struct {
  1092. __uint16_t base;
  1093. __uint16_t size;
  1094. } freemap[XFS_ATTR_LEAF_MAPSIZE];
  1095. };
  1096. /*
  1097. * Flags used in the leaf_entry[i].flags field.
  1098. * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
  1099. * on the system call, they are "or"ed together for various operations.
  1100. */
  1101. #define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
  1102. #define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
  1103. #define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
  1104. #define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
  1105. #define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
  1106. #define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
  1107. #define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
  1108. #define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
  1109. /*
  1110. * Conversion macros for converting namespace bits from argument flags
  1111. * to ondisk flags.
  1112. */
  1113. #define XFS_ATTR_NSP_ARGS_MASK (ATTR_ROOT | ATTR_SECURE)
  1114. #define XFS_ATTR_NSP_ONDISK_MASK (XFS_ATTR_ROOT | XFS_ATTR_SECURE)
  1115. #define XFS_ATTR_NSP_ONDISK(flags) ((flags) & XFS_ATTR_NSP_ONDISK_MASK)
  1116. #define XFS_ATTR_NSP_ARGS(flags) ((flags) & XFS_ATTR_NSP_ARGS_MASK)
  1117. #define XFS_ATTR_NSP_ARGS_TO_ONDISK(x) (((x) & ATTR_ROOT ? XFS_ATTR_ROOT : 0) |\
  1118. ((x) & ATTR_SECURE ? XFS_ATTR_SECURE : 0))
  1119. #define XFS_ATTR_NSP_ONDISK_TO_ARGS(x) (((x) & XFS_ATTR_ROOT ? ATTR_ROOT : 0) |\
  1120. ((x) & XFS_ATTR_SECURE ? ATTR_SECURE : 0))
  1121. /*
  1122. * Alignment for namelist and valuelist entries (since they are mixed
  1123. * there can be only one alignment value)
  1124. */
  1125. #define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
  1126. static inline int
  1127. xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock *leafp)
  1128. {
  1129. if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
  1130. return sizeof(struct xfs_attr3_leaf_hdr);
  1131. return sizeof(struct xfs_attr_leaf_hdr);
  1132. }
  1133. static inline struct xfs_attr_leaf_entry *
  1134. xfs_attr3_leaf_entryp(xfs_attr_leafblock_t *leafp)
  1135. {
  1136. if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
  1137. return &((struct xfs_attr3_leafblock *)leafp)->entries[0];
  1138. return &leafp->entries[0];
  1139. }
  1140. /*
  1141. * Cast typed pointers for "local" and "remote" name/value structs.
  1142. */
  1143. static inline char *
  1144. xfs_attr3_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
  1145. {
  1146. struct xfs_attr_leaf_entry *entries = xfs_attr3_leaf_entryp(leafp);
  1147. return &((char *)leafp)[be16_to_cpu(entries[idx].nameidx)];
  1148. }
  1149. static inline xfs_attr_leaf_name_remote_t *
  1150. xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
  1151. {
  1152. return (xfs_attr_leaf_name_remote_t *)xfs_attr3_leaf_name(leafp, idx);
  1153. }
  1154. static inline xfs_attr_leaf_name_local_t *
  1155. xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
  1156. {
  1157. return (xfs_attr_leaf_name_local_t *)xfs_attr3_leaf_name(leafp, idx);
  1158. }
  1159. /*
  1160. * Calculate total bytes used (including trailing pad for alignment) for
  1161. * a "local" name/value structure, a "remote" name/value structure, and
  1162. * a pointer which might be either.
  1163. */
  1164. static inline int xfs_attr_leaf_entsize_remote(int nlen)
  1165. {
  1166. return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
  1167. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  1168. }
  1169. static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
  1170. {
  1171. return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
  1172. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  1173. }
  1174. static inline int xfs_attr_leaf_entsize_local_max(int bsize)
  1175. {
  1176. return (((bsize) >> 1) + ((bsize) >> 2));
  1177. }
  1178. /*
  1179. * Remote attribute block format definition
  1180. *
  1181. * There is one of these headers per filesystem block in a remote attribute.
  1182. * This is done to ensure there is a 1:1 mapping between the attribute value
  1183. * length and the number of blocks needed to store the attribute. This makes the
  1184. * verification of a buffer a little more complex, but greatly simplifies the
  1185. * allocation, reading and writing of these attributes as we don't have to guess
  1186. * the number of blocks needed to store the attribute data.
  1187. */
  1188. #define XFS_ATTR3_RMT_MAGIC 0x5841524d /* XARM */
  1189. struct xfs_attr3_rmt_hdr {
  1190. __be32 rm_magic;
  1191. __be32 rm_offset;
  1192. __be32 rm_bytes;
  1193. __be32 rm_crc;
  1194. uuid_t rm_uuid;
  1195. __be64 rm_owner;
  1196. __be64 rm_blkno;
  1197. __be64 rm_lsn;
  1198. };
  1199. #define XFS_ATTR3_RMT_CRC_OFF offsetof(struct xfs_attr3_rmt_hdr, rm_crc)
  1200. #define XFS_ATTR3_RMT_BUF_SPACE(mp, bufsize) \
  1201. ((bufsize) - (xfs_sb_version_hascrc(&(mp)->m_sb) ? \
  1202. sizeof(struct xfs_attr3_rmt_hdr) : 0))
  1203. #endif /* __XFS_DA_FORMAT_H__ */