xfs_da_format.h 34 KB

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