xfs_dir2_format.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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_DIR2_FORMAT_H__
  20. #define __XFS_DIR2_FORMAT_H__
  21. /*
  22. * Directory version 2.
  23. *
  24. * There are 4 possible formats:
  25. * - shortform - embedded into the inode
  26. * - single block - data with embedded leaf at the end
  27. * - multiple data blocks, single leaf+freeindex block
  28. * - data blocks, node and leaf blocks (btree), freeindex blocks
  29. *
  30. * Note: many node blocks structures and constants are shared with the attr
  31. * code and defined in xfs_da_btree.h.
  32. */
  33. #define XFS_DIR2_BLOCK_MAGIC 0x58443242 /* XD2B: single block dirs */
  34. #define XFS_DIR2_DATA_MAGIC 0x58443244 /* XD2D: multiblock dirs */
  35. #define XFS_DIR2_FREE_MAGIC 0x58443246 /* XD2F: free index blocks */
  36. /*
  37. * Directory Version 3 With CRCs.
  38. *
  39. * The tree formats are the same as for version 2 directories. The difference
  40. * is in the block header and dirent formats. In many cases the v3 structures
  41. * use v2 definitions as they are no different and this makes code sharing much
  42. * easier.
  43. *
  44. * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
  45. * format is v2 then they switch to the existing v2 code, or the format is v3
  46. * they implement the v3 functionality. This means the existing dir2 is a mix of
  47. * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
  48. * where there is a difference in the formats, otherwise the code is unchanged.
  49. *
  50. * Where it is possible, the code decides what to do based on the magic numbers
  51. * in the blocks rather than feature bits in the superblock. This means the code
  52. * is as independent of the external XFS code as possible as doesn't require
  53. * passing struct xfs_mount pointers into places where it isn't really
  54. * necessary.
  55. *
  56. * Version 3 includes:
  57. *
  58. * - a larger block header for CRC and identification purposes and so the
  59. * offsets of all the structures inside the blocks are different.
  60. *
  61. * - new magic numbers to be able to detect the v2/v3 types on the fly.
  62. */
  63. #define XFS_DIR3_BLOCK_MAGIC 0x58444233 /* XDB3: single block dirs */
  64. #define XFS_DIR3_DATA_MAGIC 0x58444433 /* XDD3: multiblock dirs */
  65. #define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */
  66. /*
  67. * Dirents in version 3 directories have a file type field. Additions to this
  68. * list are an on-disk format change, requiring feature bits. Valid values
  69. * are as follows:
  70. */
  71. #define XFS_DIR3_FT_UNKNOWN 0
  72. #define XFS_DIR3_FT_REG_FILE 1
  73. #define XFS_DIR3_FT_DIR 2
  74. #define XFS_DIR3_FT_CHRDEV 3
  75. #define XFS_DIR3_FT_BLKDEV 4
  76. #define XFS_DIR3_FT_FIFO 5
  77. #define XFS_DIR3_FT_SOCK 6
  78. #define XFS_DIR3_FT_SYMLINK 7
  79. #define XFS_DIR3_FT_WHT 8
  80. #define XFS_DIR3_FT_MAX 9
  81. /*
  82. * Byte offset in data block and shortform entry.
  83. */
  84. typedef __uint16_t xfs_dir2_data_off_t;
  85. #define NULLDATAOFF 0xffffU
  86. typedef uint xfs_dir2_data_aoff_t; /* argument form */
  87. /*
  88. * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
  89. * Only need 16 bits, this is the byte offset into the single block form.
  90. */
  91. typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
  92. /*
  93. * Offset in data space of a data entry.
  94. */
  95. typedef __uint32_t xfs_dir2_dataptr_t;
  96. #define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
  97. #define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
  98. /*
  99. * Byte offset in a directory.
  100. */
  101. typedef xfs_off_t xfs_dir2_off_t;
  102. /*
  103. * Directory block number (logical dirblk in file)
  104. */
  105. typedef __uint32_t xfs_dir2_db_t;
  106. /*
  107. * Inode number stored as 8 8-bit values.
  108. */
  109. typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
  110. /*
  111. * Inode number stored as 4 8-bit values.
  112. * Works a lot of the time, when all the inode numbers in a directory
  113. * fit in 32 bits.
  114. */
  115. typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
  116. typedef union {
  117. xfs_dir2_ino8_t i8;
  118. xfs_dir2_ino4_t i4;
  119. } xfs_dir2_inou_t;
  120. #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
  121. /*
  122. * Directory layout when stored internal to an inode.
  123. *
  124. * Small directories are packed as tightly as possible so as to fit into the
  125. * literal area of the inode. These "shortform" directories consist of a
  126. * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
  127. * structures. Due the different inode number storage size and the variable
  128. * length name field in the xfs_dir2_sf_entry all these structure are
  129. * variable length, and the accessors in this file should be used to iterate
  130. * over them.
  131. */
  132. typedef struct xfs_dir2_sf_hdr {
  133. __uint8_t count; /* count of entries */
  134. __uint8_t i8count; /* count of 8-byte inode #s */
  135. xfs_dir2_inou_t parent; /* parent dir inode number */
  136. } __arch_pack xfs_dir2_sf_hdr_t;
  137. typedef struct xfs_dir2_sf_entry {
  138. __u8 namelen; /* actual name length */
  139. xfs_dir2_sf_off_t offset; /* saved offset */
  140. __u8 name[]; /* name, variable size */
  141. /*
  142. * A single byte containing the file type field follows the inode
  143. * number for version 3 directory entries.
  144. *
  145. * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
  146. * variable offset after the name.
  147. */
  148. } __arch_pack xfs_dir2_sf_entry_t;
  149. static inline int xfs_dir2_sf_hdr_size(int i8count)
  150. {
  151. return sizeof(struct xfs_dir2_sf_hdr) -
  152. (i8count == 0) *
  153. (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
  154. }
  155. static inline xfs_dir2_data_aoff_t
  156. xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
  157. {
  158. return get_unaligned_be16(&sfep->offset.i);
  159. }
  160. static inline void
  161. xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
  162. {
  163. put_unaligned_be16(off, &sfep->offset.i);
  164. }
  165. static inline struct xfs_dir2_sf_entry *
  166. xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
  167. {
  168. return (struct xfs_dir2_sf_entry *)
  169. ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
  170. }
  171. static inline int
  172. xfs_dir3_sf_entsize(
  173. struct xfs_mount *mp,
  174. struct xfs_dir2_sf_hdr *hdr,
  175. int len)
  176. {
  177. int count = sizeof(struct xfs_dir2_sf_entry); /* namelen + offset */
  178. count += len; /* name */
  179. count += hdr->i8count ? sizeof(xfs_dir2_ino8_t) :
  180. sizeof(xfs_dir2_ino4_t); /* ino # */
  181. if (xfs_sb_version_hasftype(&mp->m_sb))
  182. count += sizeof(__uint8_t); /* file type */
  183. return count;
  184. }
  185. static inline struct xfs_dir2_sf_entry *
  186. xfs_dir3_sf_nextentry(
  187. struct xfs_mount *mp,
  188. struct xfs_dir2_sf_hdr *hdr,
  189. struct xfs_dir2_sf_entry *sfep)
  190. {
  191. return (struct xfs_dir2_sf_entry *)
  192. ((char *)sfep + xfs_dir3_sf_entsize(mp, hdr, sfep->namelen));
  193. }
  194. /*
  195. * in dir3 shortform directories, the file type field is stored at a variable
  196. * offset after the inode number. Because it's only a single byte, endian
  197. * conversion is not necessary.
  198. */
  199. static inline __uint8_t *
  200. xfs_dir3_sfe_ftypep(
  201. struct xfs_dir2_sf_hdr *hdr,
  202. struct xfs_dir2_sf_entry *sfep)
  203. {
  204. return (__uint8_t *)&sfep->name[sfep->namelen];
  205. }
  206. static inline __uint8_t
  207. xfs_dir3_sfe_get_ftype(
  208. struct xfs_mount *mp,
  209. struct xfs_dir2_sf_hdr *hdr,
  210. struct xfs_dir2_sf_entry *sfep)
  211. {
  212. __uint8_t *ftp;
  213. if (!xfs_sb_version_hasftype(&mp->m_sb))
  214. return XFS_DIR3_FT_UNKNOWN;
  215. ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
  216. if (*ftp >= XFS_DIR3_FT_MAX)
  217. return XFS_DIR3_FT_UNKNOWN;
  218. return *ftp;
  219. }
  220. static inline void
  221. xfs_dir3_sfe_put_ftype(
  222. struct xfs_mount *mp,
  223. struct xfs_dir2_sf_hdr *hdr,
  224. struct xfs_dir2_sf_entry *sfep,
  225. __uint8_t ftype)
  226. {
  227. __uint8_t *ftp;
  228. ASSERT(ftype < XFS_DIR3_FT_MAX);
  229. if (!xfs_sb_version_hasftype(&mp->m_sb))
  230. return;
  231. ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
  232. *ftp = ftype;
  233. }
  234. /*
  235. * Data block structures.
  236. *
  237. * A pure data block looks like the following drawing on disk:
  238. *
  239. * +-------------------------------------------------+
  240. * | xfs_dir2_data_hdr_t |
  241. * +-------------------------------------------------+
  242. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  243. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  244. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  245. * | ... |
  246. * +-------------------------------------------------+
  247. * | unused space |
  248. * +-------------------------------------------------+
  249. *
  250. * As all the entries are variable size structures the accessors below should
  251. * be used to iterate over them.
  252. *
  253. * In addition to the pure data blocks for the data and node formats,
  254. * most structures are also used for the combined data/freespace "block"
  255. * format below.
  256. */
  257. #define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
  258. #define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
  259. #define XFS_DIR2_DATA_FREE_TAG 0xffff
  260. #define XFS_DIR2_DATA_FD_COUNT 3
  261. /*
  262. * Directory address space divided into sections,
  263. * spaces separated by 32GB.
  264. */
  265. #define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
  266. #define XFS_DIR2_DATA_SPACE 0
  267. #define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
  268. #define XFS_DIR2_DATA_FIRSTDB(mp) \
  269. xfs_dir2_byte_to_db(mp, XFS_DIR2_DATA_OFFSET)
  270. /*
  271. * Describe a free area in the data block.
  272. *
  273. * The freespace will be formatted as a xfs_dir2_data_unused_t.
  274. */
  275. typedef struct xfs_dir2_data_free {
  276. __be16 offset; /* start of freespace */
  277. __be16 length; /* length of freespace */
  278. } xfs_dir2_data_free_t;
  279. /*
  280. * Header for the data blocks.
  281. *
  282. * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
  283. */
  284. typedef struct xfs_dir2_data_hdr {
  285. __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
  286. /* XFS_DIR2_BLOCK_MAGIC */
  287. xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
  288. } xfs_dir2_data_hdr_t;
  289. /*
  290. * define a structure for all the verification fields we are adding to the
  291. * directory block structures. This will be used in several structures.
  292. * The magic number must be the first entry to align with all the dir2
  293. * structures so we determine how to decode them just by the magic number.
  294. */
  295. struct xfs_dir3_blk_hdr {
  296. __be32 magic; /* magic number */
  297. __be32 crc; /* CRC of block */
  298. __be64 blkno; /* first block of the buffer */
  299. __be64 lsn; /* sequence number of last write */
  300. uuid_t uuid; /* filesystem we belong to */
  301. __be64 owner; /* inode that owns the block */
  302. };
  303. struct xfs_dir3_data_hdr {
  304. struct xfs_dir3_blk_hdr hdr;
  305. xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
  306. __be32 pad; /* 64 bit alignment */
  307. };
  308. #define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
  309. static inline struct xfs_dir2_data_free *
  310. xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
  311. {
  312. if (hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  313. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  314. struct xfs_dir3_data_hdr *hdr3 = (struct xfs_dir3_data_hdr *)hdr;
  315. return hdr3->best_free;
  316. }
  317. return hdr->bestfree;
  318. }
  319. /*
  320. * Active entry in a data block.
  321. *
  322. * Aligned to 8 bytes. After the variable length name field there is a
  323. * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
  324. *
  325. * For dir3 structures, there is file type field between the name and the tag.
  326. * This can only be manipulated by helper functions. It is packed hard against
  327. * the end of the name so any padding for rounding is between the file type and
  328. * the tag.
  329. */
  330. typedef struct xfs_dir2_data_entry {
  331. __be64 inumber; /* inode number */
  332. __u8 namelen; /* name length */
  333. __u8 name[]; /* name bytes, no null */
  334. /* __u8 filetype; */ /* type of inode we point to */
  335. /* __be16 tag; */ /* starting offset of us */
  336. } xfs_dir2_data_entry_t;
  337. /*
  338. * Unused entry in a data block.
  339. *
  340. * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
  341. * using xfs_dir2_data_unused_tag_p.
  342. */
  343. typedef struct xfs_dir2_data_unused {
  344. __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
  345. __be16 length; /* total free length */
  346. /* variable offset */
  347. __be16 tag; /* starting offset of us */
  348. } xfs_dir2_data_unused_t;
  349. /*
  350. * Size of a data entry.
  351. */
  352. static inline int
  353. __xfs_dir3_data_entsize(
  354. bool ftype,
  355. int n)
  356. {
  357. int size = offsetof(struct xfs_dir2_data_entry, name[0]);
  358. size += n;
  359. size += sizeof(xfs_dir2_data_off_t);
  360. if (ftype)
  361. size += sizeof(__uint8_t);
  362. return roundup(size, XFS_DIR2_DATA_ALIGN);
  363. }
  364. static inline int
  365. xfs_dir3_data_entsize(
  366. struct xfs_mount *mp,
  367. int n)
  368. {
  369. bool ftype = xfs_sb_version_hasftype(&mp->m_sb) ? true : false;
  370. return __xfs_dir3_data_entsize(ftype, n);
  371. }
  372. static inline __uint8_t
  373. xfs_dir3_dirent_get_ftype(
  374. struct xfs_mount *mp,
  375. struct xfs_dir2_data_entry *dep)
  376. {
  377. if (xfs_sb_version_hasftype(&mp->m_sb)) {
  378. __uint8_t type = dep->name[dep->namelen];
  379. ASSERT(type < XFS_DIR3_FT_MAX);
  380. if (type < XFS_DIR3_FT_MAX)
  381. return type;
  382. }
  383. return XFS_DIR3_FT_UNKNOWN;
  384. }
  385. static inline void
  386. xfs_dir3_dirent_put_ftype(
  387. struct xfs_mount *mp,
  388. struct xfs_dir2_data_entry *dep,
  389. __uint8_t type)
  390. {
  391. ASSERT(type < XFS_DIR3_FT_MAX);
  392. ASSERT(dep->namelen != 0);
  393. if (xfs_sb_version_hasftype(&mp->m_sb))
  394. dep->name[dep->namelen] = type;
  395. }
  396. /*
  397. * Pointer to an entry's tag word.
  398. */
  399. static inline __be16 *
  400. xfs_dir3_data_entry_tag_p(
  401. struct xfs_mount *mp,
  402. struct xfs_dir2_data_entry *dep)
  403. {
  404. return (__be16 *)((char *)dep +
  405. xfs_dir3_data_entsize(mp, dep->namelen) - sizeof(__be16));
  406. }
  407. /*
  408. * Pointer to a freespace's tag word.
  409. */
  410. static inline __be16 *
  411. xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
  412. {
  413. return (__be16 *)((char *)dup +
  414. be16_to_cpu(dup->length) - sizeof(__be16));
  415. }
  416. static inline size_t
  417. xfs_dir3_data_hdr_size(bool dir3)
  418. {
  419. if (dir3)
  420. return sizeof(struct xfs_dir3_data_hdr);
  421. return sizeof(struct xfs_dir2_data_hdr);
  422. }
  423. static inline size_t
  424. xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr)
  425. {
  426. bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  427. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
  428. return xfs_dir3_data_hdr_size(dir3);
  429. }
  430. static inline struct xfs_dir2_data_entry *
  431. xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
  432. {
  433. return (struct xfs_dir2_data_entry *)
  434. ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
  435. }
  436. static inline struct xfs_dir2_data_unused *
  437. xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
  438. {
  439. return (struct xfs_dir2_data_unused *)
  440. ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
  441. }
  442. /*
  443. * Offsets of . and .. in data space (always block 0)
  444. *
  445. * The macros are used for shortform directories as they have no headers to read
  446. * the magic number out of. Shortform directories need to know the size of the
  447. * data block header because the sfe embeds the block offset of the entry into
  448. * it so that it doesn't change when format conversion occurs. Bad Things Happen
  449. * if we don't follow this rule.
  450. *
  451. * XXX: there is scope for significant optimisation of the logic here. Right
  452. * now we are checking for "dir3 format" over and over again. Ideally we should
  453. * only do it once for each operation.
  454. */
  455. #define XFS_DIR3_DATA_DOT_OFFSET(mp) \
  456. xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&(mp)->m_sb))
  457. #define XFS_DIR3_DATA_DOTDOT_OFFSET(mp) \
  458. (XFS_DIR3_DATA_DOT_OFFSET(mp) + xfs_dir3_data_entsize(mp, 1))
  459. #define XFS_DIR3_DATA_FIRST_OFFSET(mp) \
  460. (XFS_DIR3_DATA_DOTDOT_OFFSET(mp) + xfs_dir3_data_entsize(mp, 2))
  461. static inline xfs_dir2_data_aoff_t
  462. xfs_dir3_data_dot_offset(struct xfs_dir2_data_hdr *hdr)
  463. {
  464. return xfs_dir3_data_entry_offset(hdr);
  465. }
  466. static inline xfs_dir2_data_aoff_t
  467. xfs_dir3_data_dotdot_offset(struct xfs_dir2_data_hdr *hdr)
  468. {
  469. bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  470. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
  471. return xfs_dir3_data_dot_offset(hdr) +
  472. __xfs_dir3_data_entsize(dir3, 1);
  473. }
  474. static inline xfs_dir2_data_aoff_t
  475. xfs_dir3_data_first_offset(struct xfs_dir2_data_hdr *hdr)
  476. {
  477. bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  478. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
  479. return xfs_dir3_data_dotdot_offset(hdr) +
  480. __xfs_dir3_data_entsize(dir3, 2);
  481. }
  482. /*
  483. * location of . and .. in data space (always block 0)
  484. */
  485. static inline struct xfs_dir2_data_entry *
  486. xfs_dir3_data_dot_entry_p(struct xfs_dir2_data_hdr *hdr)
  487. {
  488. return (struct xfs_dir2_data_entry *)
  489. ((char *)hdr + xfs_dir3_data_dot_offset(hdr));
  490. }
  491. static inline struct xfs_dir2_data_entry *
  492. xfs_dir3_data_dotdot_entry_p(struct xfs_dir2_data_hdr *hdr)
  493. {
  494. return (struct xfs_dir2_data_entry *)
  495. ((char *)hdr + xfs_dir3_data_dotdot_offset(hdr));
  496. }
  497. static inline struct xfs_dir2_data_entry *
  498. xfs_dir3_data_first_entry_p(struct xfs_dir2_data_hdr *hdr)
  499. {
  500. return (struct xfs_dir2_data_entry *)
  501. ((char *)hdr + xfs_dir3_data_first_offset(hdr));
  502. }
  503. /*
  504. * Leaf block structures.
  505. *
  506. * A pure leaf block looks like the following drawing on disk:
  507. *
  508. * +---------------------------+
  509. * | xfs_dir2_leaf_hdr_t |
  510. * +---------------------------+
  511. * | xfs_dir2_leaf_entry_t |
  512. * | xfs_dir2_leaf_entry_t |
  513. * | xfs_dir2_leaf_entry_t |
  514. * | xfs_dir2_leaf_entry_t |
  515. * | ... |
  516. * +---------------------------+
  517. * | xfs_dir2_data_off_t |
  518. * | xfs_dir2_data_off_t |
  519. * | xfs_dir2_data_off_t |
  520. * | ... |
  521. * +---------------------------+
  522. * | xfs_dir2_leaf_tail_t |
  523. * +---------------------------+
  524. *
  525. * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
  526. * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
  527. * for directories with separate leaf nodes and free space blocks
  528. * (magic = XFS_DIR2_LEAFN_MAGIC).
  529. *
  530. * As all the entries are variable size structures the accessors below should
  531. * be used to iterate over them.
  532. */
  533. /*
  534. * Offset of the leaf/node space. First block in this space
  535. * is the btree root.
  536. */
  537. #define XFS_DIR2_LEAF_SPACE 1
  538. #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
  539. #define XFS_DIR2_LEAF_FIRSTDB(mp) \
  540. xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
  541. /*
  542. * Leaf block header.
  543. */
  544. typedef struct xfs_dir2_leaf_hdr {
  545. xfs_da_blkinfo_t info; /* header for da routines */
  546. __be16 count; /* count of entries */
  547. __be16 stale; /* count of stale entries */
  548. } xfs_dir2_leaf_hdr_t;
  549. struct xfs_dir3_leaf_hdr {
  550. struct xfs_da3_blkinfo info; /* header for da routines */
  551. __be16 count; /* count of entries */
  552. __be16 stale; /* count of stale entries */
  553. __be32 pad; /* 64 bit alignment */
  554. };
  555. struct xfs_dir3_icleaf_hdr {
  556. __uint32_t forw;
  557. __uint32_t back;
  558. __uint16_t magic;
  559. __uint16_t count;
  560. __uint16_t stale;
  561. };
  562. /*
  563. * Leaf block entry.
  564. */
  565. typedef struct xfs_dir2_leaf_entry {
  566. __be32 hashval; /* hash value of name */
  567. __be32 address; /* address of data entry */
  568. } xfs_dir2_leaf_entry_t;
  569. /*
  570. * Leaf block tail.
  571. */
  572. typedef struct xfs_dir2_leaf_tail {
  573. __be32 bestcount;
  574. } xfs_dir2_leaf_tail_t;
  575. /*
  576. * Leaf block.
  577. */
  578. typedef struct xfs_dir2_leaf {
  579. xfs_dir2_leaf_hdr_t hdr; /* leaf header */
  580. xfs_dir2_leaf_entry_t __ents[]; /* entries */
  581. } xfs_dir2_leaf_t;
  582. struct xfs_dir3_leaf {
  583. struct xfs_dir3_leaf_hdr hdr; /* leaf header */
  584. struct xfs_dir2_leaf_entry __ents[]; /* entries */
  585. };
  586. #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
  587. extern void xfs_dir3_leaf_hdr_from_disk(struct xfs_dir3_icleaf_hdr *to,
  588. struct xfs_dir2_leaf *from);
  589. static inline int
  590. xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
  591. {
  592. if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  593. lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
  594. return sizeof(struct xfs_dir3_leaf_hdr);
  595. return sizeof(struct xfs_dir2_leaf_hdr);
  596. }
  597. static inline int
  598. xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
  599. {
  600. return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
  601. (uint)sizeof(struct xfs_dir2_leaf_entry);
  602. }
  603. /*
  604. * Get address of the bestcount field in the single-leaf block.
  605. */
  606. static inline struct xfs_dir2_leaf_entry *
  607. xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
  608. {
  609. if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  610. lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
  611. struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp;
  612. return lp3->__ents;
  613. }
  614. return lp->__ents;
  615. }
  616. /*
  617. * Get address of the bestcount field in the single-leaf block.
  618. */
  619. static inline struct xfs_dir2_leaf_tail *
  620. xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
  621. {
  622. return (struct xfs_dir2_leaf_tail *)
  623. ((char *)lp + mp->m_dirblksize -
  624. sizeof(struct xfs_dir2_leaf_tail));
  625. }
  626. /*
  627. * Get address of the bests array in the single-leaf block.
  628. */
  629. static inline __be16 *
  630. xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
  631. {
  632. return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
  633. }
  634. /*
  635. * DB blocks here are logical directory block numbers, not filesystem blocks.
  636. */
  637. /*
  638. * Convert dataptr to byte in file space
  639. */
  640. static inline xfs_dir2_off_t
  641. xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  642. {
  643. return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
  644. }
  645. /*
  646. * Convert byte in file space to dataptr. It had better be aligned.
  647. */
  648. static inline xfs_dir2_dataptr_t
  649. xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
  650. {
  651. return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
  652. }
  653. /*
  654. * Convert byte in space to (DB) block
  655. */
  656. static inline xfs_dir2_db_t
  657. xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
  658. {
  659. return (xfs_dir2_db_t)
  660. (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
  661. }
  662. /*
  663. * Convert dataptr to a block number
  664. */
  665. static inline xfs_dir2_db_t
  666. xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  667. {
  668. return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  669. }
  670. /*
  671. * Convert byte in space to offset in a block
  672. */
  673. static inline xfs_dir2_data_aoff_t
  674. xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
  675. {
  676. return (xfs_dir2_data_aoff_t)(by &
  677. ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
  678. }
  679. /*
  680. * Convert dataptr to a byte offset in a block
  681. */
  682. static inline xfs_dir2_data_aoff_t
  683. xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  684. {
  685. return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  686. }
  687. /*
  688. * Convert block and offset to byte in space
  689. */
  690. static inline xfs_dir2_off_t
  691. xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
  692. xfs_dir2_data_aoff_t o)
  693. {
  694. return ((xfs_dir2_off_t)db <<
  695. (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
  696. }
  697. /*
  698. * Convert block (DB) to block (dablk)
  699. */
  700. static inline xfs_dablk_t
  701. xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
  702. {
  703. return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
  704. }
  705. /*
  706. * Convert byte in space to (DA) block
  707. */
  708. static inline xfs_dablk_t
  709. xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
  710. {
  711. return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
  712. }
  713. /*
  714. * Convert block and offset to dataptr
  715. */
  716. static inline xfs_dir2_dataptr_t
  717. xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
  718. xfs_dir2_data_aoff_t o)
  719. {
  720. return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
  721. }
  722. /*
  723. * Convert block (dablk) to block (DB)
  724. */
  725. static inline xfs_dir2_db_t
  726. xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
  727. {
  728. return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
  729. }
  730. /*
  731. * Convert block (dablk) to byte offset in space
  732. */
  733. static inline xfs_dir2_off_t
  734. xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
  735. {
  736. return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
  737. }
  738. /*
  739. * Free space block defintions for the node format.
  740. */
  741. /*
  742. * Offset of the freespace index.
  743. */
  744. #define XFS_DIR2_FREE_SPACE 2
  745. #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
  746. #define XFS_DIR2_FREE_FIRSTDB(mp) \
  747. xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
  748. typedef struct xfs_dir2_free_hdr {
  749. __be32 magic; /* XFS_DIR2_FREE_MAGIC */
  750. __be32 firstdb; /* db of first entry */
  751. __be32 nvalid; /* count of valid entries */
  752. __be32 nused; /* count of used entries */
  753. } xfs_dir2_free_hdr_t;
  754. typedef struct xfs_dir2_free {
  755. xfs_dir2_free_hdr_t hdr; /* block header */
  756. __be16 bests[]; /* best free counts */
  757. /* unused entries are -1 */
  758. } xfs_dir2_free_t;
  759. struct xfs_dir3_free_hdr {
  760. struct xfs_dir3_blk_hdr hdr;
  761. __be32 firstdb; /* db of first entry */
  762. __be32 nvalid; /* count of valid entries */
  763. __be32 nused; /* count of used entries */
  764. __be32 pad; /* 64 bit alignment */
  765. };
  766. struct xfs_dir3_free {
  767. struct xfs_dir3_free_hdr hdr;
  768. __be16 bests[]; /* best free counts */
  769. /* unused entries are -1 */
  770. };
  771. #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
  772. /*
  773. * In core version of the free block header, abstracted away from on-disk format
  774. * differences. Use this in the code, and convert to/from the disk version using
  775. * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
  776. */
  777. struct xfs_dir3_icfree_hdr {
  778. __uint32_t magic;
  779. __uint32_t firstdb;
  780. __uint32_t nvalid;
  781. __uint32_t nused;
  782. };
  783. void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
  784. struct xfs_dir2_free *from);
  785. static inline int
  786. xfs_dir3_free_hdr_size(struct xfs_mount *mp)
  787. {
  788. if (xfs_sb_version_hascrc(&mp->m_sb))
  789. return sizeof(struct xfs_dir3_free_hdr);
  790. return sizeof(struct xfs_dir2_free_hdr);
  791. }
  792. static inline int
  793. xfs_dir3_free_max_bests(struct xfs_mount *mp)
  794. {
  795. return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
  796. sizeof(xfs_dir2_data_off_t);
  797. }
  798. static inline __be16 *
  799. xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
  800. {
  801. return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
  802. }
  803. /*
  804. * Convert data space db to the corresponding free db.
  805. */
  806. static inline xfs_dir2_db_t
  807. xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
  808. {
  809. return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
  810. }
  811. /*
  812. * Convert data space db to the corresponding index in a free db.
  813. */
  814. static inline int
  815. xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
  816. {
  817. return db % xfs_dir3_free_max_bests(mp);
  818. }
  819. /*
  820. * Single block format.
  821. *
  822. * The single block format looks like the following drawing on disk:
  823. *
  824. * +-------------------------------------------------+
  825. * | xfs_dir2_data_hdr_t |
  826. * +-------------------------------------------------+
  827. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  828. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  829. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
  830. * | ... |
  831. * +-------------------------------------------------+
  832. * | unused space |
  833. * +-------------------------------------------------+
  834. * | ... |
  835. * | xfs_dir2_leaf_entry_t |
  836. * | xfs_dir2_leaf_entry_t |
  837. * +-------------------------------------------------+
  838. * | xfs_dir2_block_tail_t |
  839. * +-------------------------------------------------+
  840. *
  841. * As all the entries are variable size structures the accessors below should
  842. * be used to iterate over them.
  843. */
  844. typedef struct xfs_dir2_block_tail {
  845. __be32 count; /* count of leaf entries */
  846. __be32 stale; /* count of stale lf entries */
  847. } xfs_dir2_block_tail_t;
  848. /*
  849. * Pointer to the leaf header embedded in a data block (1-block format)
  850. */
  851. static inline struct xfs_dir2_block_tail *
  852. xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
  853. {
  854. return ((struct xfs_dir2_block_tail *)
  855. ((char *)hdr + mp->m_dirblksize)) - 1;
  856. }
  857. /*
  858. * Pointer to the leaf entries embedded in a data block (1-block format)
  859. */
  860. static inline struct xfs_dir2_leaf_entry *
  861. xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
  862. {
  863. return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
  864. }
  865. #endif /* __XFS_DIR2_FORMAT_H__ */