xfs_dir2_format.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. * XXX: there is scope for significant optimisation of the logic here. Right
  446. * now we are checking for "dir3 format" over and over again. Ideally we should
  447. * only do it once for each operation.
  448. */
  449. static inline xfs_dir2_data_aoff_t
  450. xfs_dir3_data_dot_offset(struct xfs_mount *mp)
  451. {
  452. return xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb));
  453. }
  454. static inline xfs_dir2_data_aoff_t
  455. xfs_dir3_data_dotdot_offset(struct xfs_mount *mp)
  456. {
  457. return xfs_dir3_data_dot_offset(mp) +
  458. xfs_dir3_data_entsize(mp, 1);
  459. }
  460. static inline xfs_dir2_data_aoff_t
  461. xfs_dir3_data_first_offset(struct xfs_mount *mp)
  462. {
  463. return xfs_dir3_data_dotdot_offset(mp) +
  464. xfs_dir3_data_entsize(mp, 2);
  465. }
  466. /*
  467. * location of . and .. in data space (always block 0)
  468. */
  469. static inline struct xfs_dir2_data_entry *
  470. xfs_dir3_data_dot_entry_p(
  471. struct xfs_mount *mp,
  472. struct xfs_dir2_data_hdr *hdr)
  473. {
  474. return (struct xfs_dir2_data_entry *)
  475. ((char *)hdr + xfs_dir3_data_dot_offset(mp));
  476. }
  477. static inline struct xfs_dir2_data_entry *
  478. xfs_dir3_data_dotdot_entry_p(
  479. struct xfs_mount *mp,
  480. struct xfs_dir2_data_hdr *hdr)
  481. {
  482. return (struct xfs_dir2_data_entry *)
  483. ((char *)hdr + xfs_dir3_data_dotdot_offset(mp));
  484. }
  485. static inline struct xfs_dir2_data_entry *
  486. xfs_dir3_data_first_entry_p(
  487. struct xfs_mount *mp,
  488. struct xfs_dir2_data_hdr *hdr)
  489. {
  490. return (struct xfs_dir2_data_entry *)
  491. ((char *)hdr + xfs_dir3_data_first_offset(mp));
  492. }
  493. /*
  494. * Leaf block structures.
  495. *
  496. * A pure leaf block looks like the following drawing on disk:
  497. *
  498. * +---------------------------+
  499. * | xfs_dir2_leaf_hdr_t |
  500. * +---------------------------+
  501. * | xfs_dir2_leaf_entry_t |
  502. * | xfs_dir2_leaf_entry_t |
  503. * | xfs_dir2_leaf_entry_t |
  504. * | xfs_dir2_leaf_entry_t |
  505. * | ... |
  506. * +---------------------------+
  507. * | xfs_dir2_data_off_t |
  508. * | xfs_dir2_data_off_t |
  509. * | xfs_dir2_data_off_t |
  510. * | ... |
  511. * +---------------------------+
  512. * | xfs_dir2_leaf_tail_t |
  513. * +---------------------------+
  514. *
  515. * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
  516. * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
  517. * for directories with separate leaf nodes and free space blocks
  518. * (magic = XFS_DIR2_LEAFN_MAGIC).
  519. *
  520. * As all the entries are variable size structures the accessors below should
  521. * be used to iterate over them.
  522. */
  523. /*
  524. * Offset of the leaf/node space. First block in this space
  525. * is the btree root.
  526. */
  527. #define XFS_DIR2_LEAF_SPACE 1
  528. #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
  529. #define XFS_DIR2_LEAF_FIRSTDB(mp) \
  530. xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
  531. /*
  532. * Leaf block header.
  533. */
  534. typedef struct xfs_dir2_leaf_hdr {
  535. xfs_da_blkinfo_t info; /* header for da routines */
  536. __be16 count; /* count of entries */
  537. __be16 stale; /* count of stale entries */
  538. } xfs_dir2_leaf_hdr_t;
  539. struct xfs_dir3_leaf_hdr {
  540. struct xfs_da3_blkinfo info; /* header for da routines */
  541. __be16 count; /* count of entries */
  542. __be16 stale; /* count of stale entries */
  543. __be32 pad; /* 64 bit alignment */
  544. };
  545. struct xfs_dir3_icleaf_hdr {
  546. __uint32_t forw;
  547. __uint32_t back;
  548. __uint16_t magic;
  549. __uint16_t count;
  550. __uint16_t stale;
  551. };
  552. /*
  553. * Leaf block entry.
  554. */
  555. typedef struct xfs_dir2_leaf_entry {
  556. __be32 hashval; /* hash value of name */
  557. __be32 address; /* address of data entry */
  558. } xfs_dir2_leaf_entry_t;
  559. /*
  560. * Leaf block tail.
  561. */
  562. typedef struct xfs_dir2_leaf_tail {
  563. __be32 bestcount;
  564. } xfs_dir2_leaf_tail_t;
  565. /*
  566. * Leaf block.
  567. */
  568. typedef struct xfs_dir2_leaf {
  569. xfs_dir2_leaf_hdr_t hdr; /* leaf header */
  570. xfs_dir2_leaf_entry_t __ents[]; /* entries */
  571. } xfs_dir2_leaf_t;
  572. struct xfs_dir3_leaf {
  573. struct xfs_dir3_leaf_hdr hdr; /* leaf header */
  574. struct xfs_dir2_leaf_entry __ents[]; /* entries */
  575. };
  576. #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
  577. extern void xfs_dir3_leaf_hdr_from_disk(struct xfs_dir3_icleaf_hdr *to,
  578. struct xfs_dir2_leaf *from);
  579. static inline int
  580. xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
  581. {
  582. if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  583. lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
  584. return sizeof(struct xfs_dir3_leaf_hdr);
  585. return sizeof(struct xfs_dir2_leaf_hdr);
  586. }
  587. static inline int
  588. xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
  589. {
  590. return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
  591. (uint)sizeof(struct xfs_dir2_leaf_entry);
  592. }
  593. /*
  594. * Get address of the bestcount field in the single-leaf block.
  595. */
  596. static inline struct xfs_dir2_leaf_entry *
  597. xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
  598. {
  599. if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  600. lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
  601. struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp;
  602. return lp3->__ents;
  603. }
  604. return lp->__ents;
  605. }
  606. /*
  607. * Get address of the bestcount field in the single-leaf block.
  608. */
  609. static inline struct xfs_dir2_leaf_tail *
  610. xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
  611. {
  612. return (struct xfs_dir2_leaf_tail *)
  613. ((char *)lp + mp->m_dirblksize -
  614. sizeof(struct xfs_dir2_leaf_tail));
  615. }
  616. /*
  617. * Get address of the bests array in the single-leaf block.
  618. */
  619. static inline __be16 *
  620. xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
  621. {
  622. return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
  623. }
  624. /*
  625. * DB blocks here are logical directory block numbers, not filesystem blocks.
  626. */
  627. /*
  628. * Convert dataptr to byte in file space
  629. */
  630. static inline xfs_dir2_off_t
  631. xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  632. {
  633. return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
  634. }
  635. /*
  636. * Convert byte in file space to dataptr. It had better be aligned.
  637. */
  638. static inline xfs_dir2_dataptr_t
  639. xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
  640. {
  641. return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
  642. }
  643. /*
  644. * Convert byte in space to (DB) block
  645. */
  646. static inline xfs_dir2_db_t
  647. xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
  648. {
  649. return (xfs_dir2_db_t)
  650. (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
  651. }
  652. /*
  653. * Convert dataptr to a block number
  654. */
  655. static inline xfs_dir2_db_t
  656. xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  657. {
  658. return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  659. }
  660. /*
  661. * Convert byte in space to offset in a block
  662. */
  663. static inline xfs_dir2_data_aoff_t
  664. xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
  665. {
  666. return (xfs_dir2_data_aoff_t)(by &
  667. ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
  668. }
  669. /*
  670. * Convert dataptr to a byte offset in a block
  671. */
  672. static inline xfs_dir2_data_aoff_t
  673. xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  674. {
  675. return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
  676. }
  677. /*
  678. * Convert block and offset to byte in space
  679. */
  680. static inline xfs_dir2_off_t
  681. xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
  682. xfs_dir2_data_aoff_t o)
  683. {
  684. return ((xfs_dir2_off_t)db <<
  685. (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
  686. }
  687. /*
  688. * Convert block (DB) to block (dablk)
  689. */
  690. static inline xfs_dablk_t
  691. xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
  692. {
  693. return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
  694. }
  695. /*
  696. * Convert byte in space to (DA) block
  697. */
  698. static inline xfs_dablk_t
  699. xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
  700. {
  701. return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
  702. }
  703. /*
  704. * Convert block and offset to dataptr
  705. */
  706. static inline xfs_dir2_dataptr_t
  707. xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
  708. xfs_dir2_data_aoff_t o)
  709. {
  710. return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
  711. }
  712. /*
  713. * Convert block (dablk) to block (DB)
  714. */
  715. static inline xfs_dir2_db_t
  716. xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
  717. {
  718. return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
  719. }
  720. /*
  721. * Convert block (dablk) to byte offset in space
  722. */
  723. static inline xfs_dir2_off_t
  724. xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
  725. {
  726. return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
  727. }
  728. /*
  729. * Free space block defintions for the node format.
  730. */
  731. /*
  732. * Offset of the freespace index.
  733. */
  734. #define XFS_DIR2_FREE_SPACE 2
  735. #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
  736. #define XFS_DIR2_FREE_FIRSTDB(mp) \
  737. xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
  738. typedef struct xfs_dir2_free_hdr {
  739. __be32 magic; /* XFS_DIR2_FREE_MAGIC */
  740. __be32 firstdb; /* db of first entry */
  741. __be32 nvalid; /* count of valid entries */
  742. __be32 nused; /* count of used entries */
  743. } xfs_dir2_free_hdr_t;
  744. typedef struct xfs_dir2_free {
  745. xfs_dir2_free_hdr_t hdr; /* block header */
  746. __be16 bests[]; /* best free counts */
  747. /* unused entries are -1 */
  748. } xfs_dir2_free_t;
  749. struct xfs_dir3_free_hdr {
  750. struct xfs_dir3_blk_hdr hdr;
  751. __be32 firstdb; /* db of first entry */
  752. __be32 nvalid; /* count of valid entries */
  753. __be32 nused; /* count of used entries */
  754. __be32 pad; /* 64 bit alignment */
  755. };
  756. struct xfs_dir3_free {
  757. struct xfs_dir3_free_hdr hdr;
  758. __be16 bests[]; /* best free counts */
  759. /* unused entries are -1 */
  760. };
  761. #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
  762. /*
  763. * In core version of the free block header, abstracted away from on-disk format
  764. * differences. Use this in the code, and convert to/from the disk version using
  765. * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
  766. */
  767. struct xfs_dir3_icfree_hdr {
  768. __uint32_t magic;
  769. __uint32_t firstdb;
  770. __uint32_t nvalid;
  771. __uint32_t nused;
  772. };
  773. void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
  774. struct xfs_dir2_free *from);
  775. static inline int
  776. xfs_dir3_free_hdr_size(struct xfs_mount *mp)
  777. {
  778. if (xfs_sb_version_hascrc(&mp->m_sb))
  779. return sizeof(struct xfs_dir3_free_hdr);
  780. return sizeof(struct xfs_dir2_free_hdr);
  781. }
  782. static inline int
  783. xfs_dir3_free_max_bests(struct xfs_mount *mp)
  784. {
  785. return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
  786. sizeof(xfs_dir2_data_off_t);
  787. }
  788. static inline __be16 *
  789. xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
  790. {
  791. return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
  792. }
  793. /*
  794. * Convert data space db to the corresponding free db.
  795. */
  796. static inline xfs_dir2_db_t
  797. xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
  798. {
  799. return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
  800. }
  801. /*
  802. * Convert data space db to the corresponding index in a free db.
  803. */
  804. static inline int
  805. xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
  806. {
  807. return db % xfs_dir3_free_max_bests(mp);
  808. }
  809. /*
  810. * Single block format.
  811. *
  812. * The single block format looks like the following drawing on disk:
  813. *
  814. * +-------------------------------------------------+
  815. * | xfs_dir2_data_hdr_t |
  816. * +-------------------------------------------------+
  817. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  818. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
  819. * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
  820. * | ... |
  821. * +-------------------------------------------------+
  822. * | unused space |
  823. * +-------------------------------------------------+
  824. * | ... |
  825. * | xfs_dir2_leaf_entry_t |
  826. * | xfs_dir2_leaf_entry_t |
  827. * +-------------------------------------------------+
  828. * | xfs_dir2_block_tail_t |
  829. * +-------------------------------------------------+
  830. *
  831. * As all the entries are variable size structures the accessors below should
  832. * be used to iterate over them.
  833. */
  834. typedef struct xfs_dir2_block_tail {
  835. __be32 count; /* count of leaf entries */
  836. __be32 stale; /* count of stale lf entries */
  837. } xfs_dir2_block_tail_t;
  838. /*
  839. * Pointer to the leaf header embedded in a data block (1-block format)
  840. */
  841. static inline struct xfs_dir2_block_tail *
  842. xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
  843. {
  844. return ((struct xfs_dir2_block_tail *)
  845. ((char *)hdr + mp->m_dirblksize)) - 1;
  846. }
  847. /*
  848. * Pointer to the leaf entries embedded in a data block (1-block format)
  849. */
  850. static inline struct xfs_dir2_leaf_entry *
  851. xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
  852. {
  853. return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
  854. }
  855. #endif /* __XFS_DIR2_FORMAT_H__ */