xfs_da_format.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. #define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
  112. /*
  113. * Directory version 2.
  114. *
  115. * There are 4 possible formats:
  116. * - shortform - embedded into the inode
  117. * - single block - data with embedded leaf at the end
  118. * - multiple data blocks, single leaf+freeindex block
  119. * - data blocks, node and leaf blocks (btree), freeindex blocks
  120. *
  121. * Note: many node blocks structures and constants are shared with the attr
  122. * code and defined in xfs_da_btree.h.
  123. */
  124. #define XFS_DIR2_BLOCK_MAGIC 0x58443242 /* XD2B: single block dirs */
  125. #define XFS_DIR2_DATA_MAGIC 0x58443244 /* XD2D: multiblock dirs */
  126. #define XFS_DIR2_FREE_MAGIC 0x58443246 /* XD2F: free index blocks */
  127. /*
  128. * Directory Version 3 With CRCs.
  129. *
  130. * The tree formats are the same as for version 2 directories. The difference
  131. * is in the block header and dirent formats. In many cases the v3 structures
  132. * use v2 definitions as they are no different and this makes code sharing much
  133. * easier.
  134. *
  135. * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
  136. * format is v2 then they switch to the existing v2 code, or the format is v3
  137. * they implement the v3 functionality. This means the existing dir2 is a mix of
  138. * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
  139. * where there is a difference in the formats, otherwise the code is unchanged.
  140. *
  141. * Where it is possible, the code decides what to do based on the magic numbers
  142. * in the blocks rather than feature bits in the superblock. This means the code
  143. * is as independent of the external XFS code as possible as doesn't require
  144. * passing struct xfs_mount pointers into places where it isn't really
  145. * necessary.
  146. *
  147. * Version 3 includes:
  148. *
  149. * - a larger block header for CRC and identification purposes and so the
  150. * offsets of all the structures inside the blocks are different.
  151. *
  152. * - new magic numbers to be able to detect the v2/v3 types on the fly.
  153. */
  154. #define XFS_DIR3_BLOCK_MAGIC 0x58444233 /* XDB3: single block dirs */
  155. #define XFS_DIR3_DATA_MAGIC 0x58444433 /* XDD3: multiblock dirs */
  156. #define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */
  157. /*
  158. * Dirents in version 3 directories have a file type field. Additions to this
  159. * list are an on-disk format change, requiring feature bits. Valid values
  160. * are as follows:
  161. */
  162. #define XFS_DIR3_FT_UNKNOWN 0
  163. #define XFS_DIR3_FT_REG_FILE 1
  164. #define XFS_DIR3_FT_DIR 2
  165. #define XFS_DIR3_FT_CHRDEV 3
  166. #define XFS_DIR3_FT_BLKDEV 4
  167. #define XFS_DIR3_FT_FIFO 5
  168. #define XFS_DIR3_FT_SOCK 6
  169. #define XFS_DIR3_FT_SYMLINK 7
  170. #define XFS_DIR3_FT_WHT 8
  171. #define XFS_DIR3_FT_MAX 9
  172. /*
  173. * Byte offset in data block and shortform entry.
  174. */
  175. typedef __uint16_t xfs_dir2_data_off_t;
  176. #define NULLDATAOFF 0xffffU
  177. typedef uint xfs_dir2_data_aoff_t; /* argument form */
  178. /*
  179. * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
  180. * Only need 16 bits, this is the byte offset into the single block form.
  181. */
  182. typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
  183. /*
  184. * Offset in data space of a data entry.
  185. */
  186. typedef __uint32_t xfs_dir2_dataptr_t;
  187. #define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
  188. #define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
  189. /*
  190. * Byte offset in a directory.
  191. */
  192. typedef xfs_off_t xfs_dir2_off_t;
  193. /*
  194. * Directory block number (logical dirblk in file)
  195. */
  196. typedef __uint32_t xfs_dir2_db_t;
  197. /*
  198. * Inode number stored as 8 8-bit values.
  199. */
  200. typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
  201. /*
  202. * Inode number stored as 4 8-bit values.
  203. * Works a lot of the time, when all the inode numbers in a directory
  204. * fit in 32 bits.
  205. */
  206. typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
  207. typedef union {
  208. xfs_dir2_ino8_t i8;
  209. xfs_dir2_ino4_t i4;
  210. } xfs_dir2_inou_t;
  211. #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
  212. /*
  213. * Directory layout when stored internal to an inode.
  214. *
  215. * Small directories are packed as tightly as possible so as to fit into the
  216. * literal area of the inode. These "shortform" directories consist of a
  217. * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
  218. * structures. Due the different inode number storage size and the variable
  219. * length name field in the xfs_dir2_sf_entry all these structure are
  220. * variable length, and the accessors in this file should be used to iterate
  221. * over them.
  222. */
  223. typedef struct xfs_dir2_sf_hdr {
  224. __uint8_t count; /* count of entries */
  225. __uint8_t i8count; /* count of 8-byte inode #s */
  226. xfs_dir2_inou_t parent; /* parent dir inode number */
  227. } __arch_pack xfs_dir2_sf_hdr_t;
  228. typedef struct xfs_dir2_sf_entry {
  229. __u8 namelen; /* actual name length */
  230. xfs_dir2_sf_off_t offset; /* saved offset */
  231. __u8 name[]; /* name, variable size */
  232. /*
  233. * A single byte containing the file type field follows the inode
  234. * number for version 3 directory entries.
  235. *
  236. * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
  237. * variable offset after the name.
  238. */
  239. } __arch_pack xfs_dir2_sf_entry_t;
  240. static inline int xfs_dir2_sf_hdr_size(int i8count)
  241. {
  242. return sizeof(struct xfs_dir2_sf_hdr) -
  243. (i8count == 0) *
  244. (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
  245. }
  246. static inline xfs_dir2_data_aoff_t
  247. xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
  248. {
  249. return get_unaligned_be16(&sfep->offset.i);
  250. }
  251. static inline void
  252. xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
  253. {
  254. put_unaligned_be16(off, &sfep->offset.i);
  255. }
  256. static inline struct xfs_dir2_sf_entry *
  257. xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
  258. {
  259. return (struct xfs_dir2_sf_entry *)
  260. ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
  261. }
  262. /*
  263. * Data block structures.
  264. *
  265. * A pure data block looks like the following drawing on disk:
  266. *
  267. * +-------------------------------------------------+
  268. * | xfs_dir2_data_hdr_t |
  269. * +-------------------------------------------------+
  270. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  271. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  272. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  273. * | ... |
  274. * +-------------------------------------------------+
  275. * | unused space |
  276. * +-------------------------------------------------+
  277. *
  278. * As all the entries are variable size structures the accessors below should
  279. * be used to iterate over them.
  280. *
  281. * In addition to the pure data blocks for the data and node formats,
  282. * most structures are also used for the combined data/freespace "block"
  283. * format below.
  284. */
  285. #define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
  286. #define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
  287. #define XFS_DIR2_DATA_FREE_TAG 0xffff
  288. #define XFS_DIR2_DATA_FD_COUNT 3
  289. /*
  290. * Directory address space divided into sections,
  291. * spaces separated by 32GB.
  292. */
  293. #define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
  294. #define XFS_DIR2_DATA_SPACE 0
  295. #define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
  296. #define XFS_DIR2_DATA_FIRSTDB(mp) \
  297. xfs_dir2_byte_to_db(mp, XFS_DIR2_DATA_OFFSET)
  298. /*
  299. * Describe a free area in the data block.
  300. *
  301. * The freespace will be formatted as a xfs_dir2_data_unused_t.
  302. */
  303. typedef struct xfs_dir2_data_free {
  304. __be16 offset; /* start of freespace */
  305. __be16 length; /* length of freespace */
  306. } xfs_dir2_data_free_t;
  307. /*
  308. * Header for the data blocks.
  309. *
  310. * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
  311. */
  312. typedef struct xfs_dir2_data_hdr {
  313. __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
  314. /* XFS_DIR2_BLOCK_MAGIC */
  315. xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
  316. } xfs_dir2_data_hdr_t;
  317. /*
  318. * define a structure for all the verification fields we are adding to the
  319. * directory block structures. This will be used in several structures.
  320. * The magic number must be the first entry to align with all the dir2
  321. * structures so we determine how to decode them just by the magic number.
  322. */
  323. struct xfs_dir3_blk_hdr {
  324. __be32 magic; /* magic number */
  325. __be32 crc; /* CRC of block */
  326. __be64 blkno; /* first block of the buffer */
  327. __be64 lsn; /* sequence number of last write */
  328. uuid_t uuid; /* filesystem we belong to */
  329. __be64 owner; /* inode that owns the block */
  330. };
  331. struct xfs_dir3_data_hdr {
  332. struct xfs_dir3_blk_hdr hdr;
  333. xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
  334. __be32 pad; /* 64 bit alignment */
  335. };
  336. #define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
  337. /*
  338. * Active entry in a data block.
  339. *
  340. * Aligned to 8 bytes. After the variable length name field there is a
  341. * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
  342. *
  343. * For dir3 structures, there is file type field between the name and the tag.
  344. * This can only be manipulated by helper functions. It is packed hard against
  345. * the end of the name so any padding for rounding is between the file type and
  346. * the tag.
  347. */
  348. typedef struct xfs_dir2_data_entry {
  349. __be64 inumber; /* inode number */
  350. __u8 namelen; /* name length */
  351. __u8 name[]; /* name bytes, no null */
  352. /* __u8 filetype; */ /* type of inode we point to */
  353. /* __be16 tag; */ /* starting offset of us */
  354. } xfs_dir2_data_entry_t;
  355. /*
  356. * Unused entry in a data block.
  357. *
  358. * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
  359. * using xfs_dir2_data_unused_tag_p.
  360. */
  361. typedef struct xfs_dir2_data_unused {
  362. __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
  363. __be16 length; /* total free length */
  364. /* variable offset */
  365. __be16 tag; /* starting offset of us */
  366. } xfs_dir2_data_unused_t;
  367. /*
  368. * Pointer to a freespace's tag word.
  369. */
  370. static inline __be16 *
  371. xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
  372. {
  373. return (__be16 *)((char *)dup +
  374. be16_to_cpu(dup->length) - sizeof(__be16));
  375. }
  376. /*
  377. * Leaf block structures.
  378. *
  379. * A pure leaf block looks like the following drawing on disk:
  380. *
  381. * +---------------------------+
  382. * | xfs_dir2_leaf_hdr_t |
  383. * +---------------------------+
  384. * | xfs_dir2_leaf_entry_t |
  385. * | xfs_dir2_leaf_entry_t |
  386. * | xfs_dir2_leaf_entry_t |
  387. * | xfs_dir2_leaf_entry_t |
  388. * | ... |
  389. * +---------------------------+
  390. * | xfs_dir2_data_off_t |
  391. * | xfs_dir2_data_off_t |
  392. * | xfs_dir2_data_off_t |
  393. * | ... |
  394. * +---------------------------+
  395. * | xfs_dir2_leaf_tail_t |
  396. * +---------------------------+
  397. *
  398. * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
  399. * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
  400. * for directories with separate leaf nodes and free space blocks
  401. * (magic = XFS_DIR2_LEAFN_MAGIC).
  402. *
  403. * As all the entries are variable size structures the accessors below should
  404. * be used to iterate over them.
  405. */
  406. /*
  407. * Offset of the leaf/node space. First block in this space
  408. * is the btree root.
  409. */
  410. #define XFS_DIR2_LEAF_SPACE 1
  411. #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
  412. #define XFS_DIR2_LEAF_FIRSTDB(mp) \
  413. xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
  414. /*
  415. * Leaf block header.
  416. */
  417. typedef struct xfs_dir2_leaf_hdr {
  418. xfs_da_blkinfo_t info; /* header for da routines */
  419. __be16 count; /* count of entries */
  420. __be16 stale; /* count of stale entries */
  421. } xfs_dir2_leaf_hdr_t;
  422. struct xfs_dir3_leaf_hdr {
  423. struct xfs_da3_blkinfo info; /* header for da routines */
  424. __be16 count; /* count of entries */
  425. __be16 stale; /* count of stale entries */
  426. __be32 pad; /* 64 bit alignment */
  427. };
  428. struct xfs_dir3_icleaf_hdr {
  429. __uint32_t forw;
  430. __uint32_t back;
  431. __uint16_t magic;
  432. __uint16_t count;
  433. __uint16_t stale;
  434. };
  435. /*
  436. * Leaf block entry.
  437. */
  438. typedef struct xfs_dir2_leaf_entry {
  439. __be32 hashval; /* hash value of name */
  440. __be32 address; /* address of data entry */
  441. } xfs_dir2_leaf_entry_t;
  442. /*
  443. * Leaf block tail.
  444. */
  445. typedef struct xfs_dir2_leaf_tail {
  446. __be32 bestcount;
  447. } xfs_dir2_leaf_tail_t;
  448. /*
  449. * Leaf block.
  450. */
  451. typedef struct xfs_dir2_leaf {
  452. xfs_dir2_leaf_hdr_t hdr; /* leaf header */
  453. xfs_dir2_leaf_entry_t __ents[]; /* entries */
  454. } xfs_dir2_leaf_t;
  455. struct xfs_dir3_leaf {
  456. struct xfs_dir3_leaf_hdr hdr; /* leaf header */
  457. struct xfs_dir2_leaf_entry __ents[]; /* entries */
  458. };
  459. #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
  460. /*
  461. * Get address of the bestcount field in the single-leaf block.
  462. */
  463. static inline struct xfs_dir2_leaf_tail *
  464. xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
  465. {
  466. return (struct xfs_dir2_leaf_tail *)
  467. ((char *)lp + mp->m_dirblksize -
  468. sizeof(struct xfs_dir2_leaf_tail));
  469. }
  470. /*
  471. * Get address of the bests array in the single-leaf block.
  472. */
  473. static inline __be16 *
  474. xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
  475. {
  476. return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
  477. }
  478. /*
  479. * DB blocks here are logical directory block numbers, not filesystem blocks.
  480. */
  481. /*
  482. * Convert dataptr to byte in file space
  483. */
  484. static inline xfs_dir2_off_t
  485. xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  486. {
  487. return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
  488. }
  489. /*
  490. * Convert byte in file space to dataptr. It had better be aligned.
  491. */
  492. static inline xfs_dir2_dataptr_t
  493. xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
  494. {
  495. return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
  496. }
  497. /*
  498. * Convert byte in space to (DB) block
  499. */
  500. static inline xfs_dir2_db_t
  501. xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
  502. {
  503. return (xfs_dir2_db_t)
  504. (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
  505. }
  506. /*
  507. * Convert dataptr to a block number
  508. */
  509. static inline xfs_dir2_db_t
  510. xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  511. {
  512. return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  513. }
  514. /*
  515. * Convert byte in space to offset in a block
  516. */
  517. static inline xfs_dir2_data_aoff_t
  518. xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
  519. {
  520. return (xfs_dir2_data_aoff_t)(by &
  521. ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
  522. }
  523. /*
  524. * Convert dataptr to a byte offset in a block
  525. */
  526. static inline xfs_dir2_data_aoff_t
  527. xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  528. {
  529. return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  530. }
  531. /*
  532. * Convert block and offset to byte in space
  533. */
  534. static inline xfs_dir2_off_t
  535. xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
  536. xfs_dir2_data_aoff_t o)
  537. {
  538. return ((xfs_dir2_off_t)db <<
  539. (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
  540. }
  541. /*
  542. * Convert block (DB) to block (dablk)
  543. */
  544. static inline xfs_dablk_t
  545. xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
  546. {
  547. return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
  548. }
  549. /*
  550. * Convert byte in space to (DA) block
  551. */
  552. static inline xfs_dablk_t
  553. xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
  554. {
  555. return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
  556. }
  557. /*
  558. * Convert block and offset to dataptr
  559. */
  560. static inline xfs_dir2_dataptr_t
  561. xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
  562. xfs_dir2_data_aoff_t o)
  563. {
  564. return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
  565. }
  566. /*
  567. * Convert block (dablk) to block (DB)
  568. */
  569. static inline xfs_dir2_db_t
  570. xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
  571. {
  572. return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
  573. }
  574. /*
  575. * Convert block (dablk) to byte offset in space
  576. */
  577. static inline xfs_dir2_off_t
  578. xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
  579. {
  580. return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
  581. }
  582. /*
  583. * Free space block defintions for the node format.
  584. */
  585. /*
  586. * Offset of the freespace index.
  587. */
  588. #define XFS_DIR2_FREE_SPACE 2
  589. #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
  590. #define XFS_DIR2_FREE_FIRSTDB(mp) \
  591. xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
  592. typedef struct xfs_dir2_free_hdr {
  593. __be32 magic; /* XFS_DIR2_FREE_MAGIC */
  594. __be32 firstdb; /* db of first entry */
  595. __be32 nvalid; /* count of valid entries */
  596. __be32 nused; /* count of used entries */
  597. } xfs_dir2_free_hdr_t;
  598. typedef struct xfs_dir2_free {
  599. xfs_dir2_free_hdr_t hdr; /* block header */
  600. __be16 bests[]; /* best free counts */
  601. /* unused entries are -1 */
  602. } xfs_dir2_free_t;
  603. struct xfs_dir3_free_hdr {
  604. struct xfs_dir3_blk_hdr hdr;
  605. __be32 firstdb; /* db of first entry */
  606. __be32 nvalid; /* count of valid entries */
  607. __be32 nused; /* count of used entries */
  608. __be32 pad; /* 64 bit alignment */
  609. };
  610. struct xfs_dir3_free {
  611. struct xfs_dir3_free_hdr hdr;
  612. __be16 bests[]; /* best free counts */
  613. /* unused entries are -1 */
  614. };
  615. #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
  616. /*
  617. * In core version of the free block header, abstracted away from on-disk format
  618. * differences. Use this in the code, and convert to/from the disk version using
  619. * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
  620. */
  621. struct xfs_dir3_icfree_hdr {
  622. __uint32_t magic;
  623. __uint32_t firstdb;
  624. __uint32_t nvalid;
  625. __uint32_t nused;
  626. };
  627. /*
  628. * Single block format.
  629. *
  630. * The single block format looks like the following drawing on disk:
  631. *
  632. * +-------------------------------------------------+
  633. * | xfs_dir2_data_hdr_t |
  634. * +-------------------------------------------------+
  635. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  636. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  637. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
  638. * | ... |
  639. * +-------------------------------------------------+
  640. * | unused space |
  641. * +-------------------------------------------------+
  642. * | ... |
  643. * | xfs_dir2_leaf_entry_t |
  644. * | xfs_dir2_leaf_entry_t |
  645. * +-------------------------------------------------+
  646. * | xfs_dir2_block_tail_t |
  647. * +-------------------------------------------------+
  648. *
  649. * As all the entries are variable size structures the accessors below should
  650. * be used to iterate over them.
  651. */
  652. typedef struct xfs_dir2_block_tail {
  653. __be32 count; /* count of leaf entries */
  654. __be32 stale; /* count of stale lf entries */
  655. } xfs_dir2_block_tail_t;
  656. /*
  657. * Pointer to the leaf header embedded in a data block (1-block format)
  658. */
  659. static inline struct xfs_dir2_block_tail *
  660. xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
  661. {
  662. return ((struct xfs_dir2_block_tail *)
  663. ((char *)hdr + mp->m_dirblksize)) - 1;
  664. }
  665. /*
  666. * Pointer to the leaf entries embedded in a data block (1-block format)
  667. */
  668. static inline struct xfs_dir2_leaf_entry *
  669. xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
  670. {
  671. return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
  672. }
  673. /*
  674. * Attribute storage layout
  675. *
  676. * Attribute lists are structured around Btrees where all the data
  677. * elements are in the leaf nodes. Attribute names are hashed into an int,
  678. * then that int is used as the index into the Btree. Since the hashval
  679. * of an attribute name may not be unique, we may have duplicate keys. The
  680. * internal links in the Btree are logical block offsets into the file.
  681. *
  682. *========================================================================
  683. * Attribute structure when equal to XFS_LBSIZE(mp) bytes.
  684. *========================================================================
  685. *
  686. * Struct leaf_entry's are packed from the top. Name/values grow from the
  687. * bottom but are not packed. The freemap contains run-length-encoded entries
  688. * for the free bytes after the leaf_entry's, but only the N largest such,
  689. * smaller runs are dropped. When the freemap doesn't show enough space
  690. * for an allocation, we compact the name/value area and try again. If we
  691. * still don't have enough space, then we have to split the block. The
  692. * name/value structs (both local and remote versions) must be 32bit aligned.
  693. *
  694. * Since we have duplicate hash keys, for each key that matches, compare
  695. * the actual name string. The root and intermediate node search always
  696. * takes the first-in-the-block key match found, so we should only have
  697. * to work "forw"ard. If none matches, continue with the "forw"ard leaf
  698. * nodes until the hash key changes or the attribute name is found.
  699. *
  700. * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
  701. * the leaf_entry. The namespaces are independent only because we also look
  702. * at the namespace bit when we are looking for a matching attribute name.
  703. *
  704. * We also store an "incomplete" bit in the leaf_entry. It shows that an
  705. * attribute is in the middle of being created and should not be shown to
  706. * the user if we crash during the time that the bit is set. We clear the
  707. * bit when we have finished setting up the attribute. We do this because
  708. * we cannot create some large attributes inside a single transaction, and we
  709. * need some indication that we weren't finished if we crash in the middle.
  710. */
  711. #define XFS_ATTR_LEAF_MAPSIZE 3 /* how many freespace slots */
  712. typedef struct xfs_attr_leaf_map { /* RLE map of free bytes */
  713. __be16 base; /* base of free region */
  714. __be16 size; /* length of free region */
  715. } xfs_attr_leaf_map_t;
  716. typedef struct xfs_attr_leaf_hdr { /* constant-structure header block */
  717. xfs_da_blkinfo_t info; /* block type, links, etc. */
  718. __be16 count; /* count of active leaf_entry's */
  719. __be16 usedbytes; /* num bytes of names/values stored */
  720. __be16 firstused; /* first used byte in name area */
  721. __u8 holes; /* != 0 if blk needs compaction */
  722. __u8 pad1;
  723. xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
  724. /* N largest free regions */
  725. } xfs_attr_leaf_hdr_t;
  726. typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */
  727. __be32 hashval; /* hash value of name */
  728. __be16 nameidx; /* index into buffer of name/value */
  729. __u8 flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
  730. __u8 pad2; /* unused pad byte */
  731. } xfs_attr_leaf_entry_t;
  732. typedef struct xfs_attr_leaf_name_local {
  733. __be16 valuelen; /* number of bytes in value */
  734. __u8 namelen; /* length of name bytes */
  735. __u8 nameval[1]; /* name/value bytes */
  736. } xfs_attr_leaf_name_local_t;
  737. typedef struct xfs_attr_leaf_name_remote {
  738. __be32 valueblk; /* block number of value bytes */
  739. __be32 valuelen; /* number of bytes in value */
  740. __u8 namelen; /* length of name bytes */
  741. __u8 name[1]; /* name bytes */
  742. } xfs_attr_leaf_name_remote_t;
  743. typedef struct xfs_attr_leafblock {
  744. xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */
  745. xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */
  746. xfs_attr_leaf_name_local_t namelist; /* grows from bottom of buf */
  747. xfs_attr_leaf_name_remote_t valuelist; /* grows from bottom of buf */
  748. } xfs_attr_leafblock_t;
  749. /*
  750. * CRC enabled leaf structures. Called "version 3" structures to match the
  751. * version number of the directory and dablk structures for this feature, and
  752. * attr2 is already taken by the variable inode attribute fork size feature.
  753. */
  754. struct xfs_attr3_leaf_hdr {
  755. struct xfs_da3_blkinfo info;
  756. __be16 count;
  757. __be16 usedbytes;
  758. __be16 firstused;
  759. __u8 holes;
  760. __u8 pad1;
  761. struct xfs_attr_leaf_map freemap[XFS_ATTR_LEAF_MAPSIZE];
  762. __be32 pad2; /* 64 bit alignment */
  763. };
  764. #define XFS_ATTR3_LEAF_CRC_OFF (offsetof(struct xfs_attr3_leaf_hdr, info.crc))
  765. struct xfs_attr3_leafblock {
  766. struct xfs_attr3_leaf_hdr hdr;
  767. struct xfs_attr_leaf_entry entries[1];
  768. /*
  769. * The rest of the block contains the following structures after the
  770. * leaf entries, growing from the bottom up. The variables are never
  771. * referenced, the locations accessed purely from helper functions.
  772. *
  773. * struct xfs_attr_leaf_name_local
  774. * struct xfs_attr_leaf_name_remote
  775. */
  776. };
  777. /*
  778. * incore, neutral version of the attribute leaf header
  779. */
  780. struct xfs_attr3_icleaf_hdr {
  781. __uint32_t forw;
  782. __uint32_t back;
  783. __uint16_t magic;
  784. __uint16_t count;
  785. __uint16_t usedbytes;
  786. __uint16_t firstused;
  787. __u8 holes;
  788. struct {
  789. __uint16_t base;
  790. __uint16_t size;
  791. } freemap[XFS_ATTR_LEAF_MAPSIZE];
  792. };
  793. /*
  794. * Flags used in the leaf_entry[i].flags field.
  795. * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
  796. * on the system call, they are "or"ed together for various operations.
  797. */
  798. #define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
  799. #define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
  800. #define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
  801. #define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
  802. #define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
  803. #define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
  804. #define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
  805. #define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
  806. /*
  807. * Conversion macros for converting namespace bits from argument flags
  808. * to ondisk flags.
  809. */
  810. #define XFS_ATTR_NSP_ARGS_MASK (ATTR_ROOT | ATTR_SECURE)
  811. #define XFS_ATTR_NSP_ONDISK_MASK (XFS_ATTR_ROOT | XFS_ATTR_SECURE)
  812. #define XFS_ATTR_NSP_ONDISK(flags) ((flags) & XFS_ATTR_NSP_ONDISK_MASK)
  813. #define XFS_ATTR_NSP_ARGS(flags) ((flags) & XFS_ATTR_NSP_ARGS_MASK)
  814. #define XFS_ATTR_NSP_ARGS_TO_ONDISK(x) (((x) & ATTR_ROOT ? XFS_ATTR_ROOT : 0) |\
  815. ((x) & ATTR_SECURE ? XFS_ATTR_SECURE : 0))
  816. #define XFS_ATTR_NSP_ONDISK_TO_ARGS(x) (((x) & XFS_ATTR_ROOT ? ATTR_ROOT : 0) |\
  817. ((x) & XFS_ATTR_SECURE ? ATTR_SECURE : 0))
  818. /*
  819. * Alignment for namelist and valuelist entries (since they are mixed
  820. * there can be only one alignment value)
  821. */
  822. #define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
  823. static inline int
  824. xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock *leafp)
  825. {
  826. if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
  827. return sizeof(struct xfs_attr3_leaf_hdr);
  828. return sizeof(struct xfs_attr_leaf_hdr);
  829. }
  830. static inline struct xfs_attr_leaf_entry *
  831. xfs_attr3_leaf_entryp(xfs_attr_leafblock_t *leafp)
  832. {
  833. if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
  834. return &((struct xfs_attr3_leafblock *)leafp)->entries[0];
  835. return &leafp->entries[0];
  836. }
  837. /*
  838. * Cast typed pointers for "local" and "remote" name/value structs.
  839. */
  840. static inline char *
  841. xfs_attr3_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
  842. {
  843. struct xfs_attr_leaf_entry *entries = xfs_attr3_leaf_entryp(leafp);
  844. return &((char *)leafp)[be16_to_cpu(entries[idx].nameidx)];
  845. }
  846. static inline xfs_attr_leaf_name_remote_t *
  847. xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
  848. {
  849. return (xfs_attr_leaf_name_remote_t *)xfs_attr3_leaf_name(leafp, idx);
  850. }
  851. static inline xfs_attr_leaf_name_local_t *
  852. xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
  853. {
  854. return (xfs_attr_leaf_name_local_t *)xfs_attr3_leaf_name(leafp, idx);
  855. }
  856. /*
  857. * Calculate total bytes used (including trailing pad for alignment) for
  858. * a "local" name/value structure, a "remote" name/value structure, and
  859. * a pointer which might be either.
  860. */
  861. static inline int xfs_attr_leaf_entsize_remote(int nlen)
  862. {
  863. return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
  864. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  865. }
  866. static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
  867. {
  868. return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
  869. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  870. }
  871. static inline int xfs_attr_leaf_entsize_local_max(int bsize)
  872. {
  873. return (((bsize) >> 1) + ((bsize) >> 2));
  874. }
  875. /*
  876. * Remote attribute block format definition
  877. *
  878. * There is one of these headers per filesystem block in a remote attribute.
  879. * This is done to ensure there is a 1:1 mapping between the attribute value
  880. * length and the number of blocks needed to store the attribute. This makes the
  881. * verification of a buffer a little more complex, but greatly simplifies the
  882. * allocation, reading and writing of these attributes as we don't have to guess
  883. * the number of blocks needed to store the attribute data.
  884. */
  885. #define XFS_ATTR3_RMT_MAGIC 0x5841524d /* XARM */
  886. struct xfs_attr3_rmt_hdr {
  887. __be32 rm_magic;
  888. __be32 rm_offset;
  889. __be32 rm_bytes;
  890. __be32 rm_crc;
  891. uuid_t rm_uuid;
  892. __be64 rm_owner;
  893. __be64 rm_blkno;
  894. __be64 rm_lsn;
  895. };
  896. #define XFS_ATTR3_RMT_CRC_OFF offsetof(struct xfs_attr3_rmt_hdr, rm_crc)
  897. #define XFS_ATTR3_RMT_BUF_SPACE(mp, bufsize) \
  898. ((bufsize) - (xfs_sb_version_hascrc(&(mp)->m_sb) ? \
  899. sizeof(struct xfs_attr3_rmt_hdr) : 0))
  900. #endif /* __XFS_DA_FORMAT_H__ */