xfs_da_format.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_da_format.h"
  15. #include "xfs_da_btree.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_dir2.h"
  18. #include "xfs_dir2_priv.h"
  19. /*
  20. * Shortform directory ops
  21. */
  22. static int
  23. xfs_dir2_sf_entsize(
  24. struct xfs_dir2_sf_hdr *hdr,
  25. int len)
  26. {
  27. int count = sizeof(struct xfs_dir2_sf_entry); /* namelen + offset */
  28. count += len; /* name */
  29. count += hdr->i8count ? XFS_INO64_SIZE : XFS_INO32_SIZE; /* ino # */
  30. return count;
  31. }
  32. static int
  33. xfs_dir3_sf_entsize(
  34. struct xfs_dir2_sf_hdr *hdr,
  35. int len)
  36. {
  37. return xfs_dir2_sf_entsize(hdr, len) + sizeof(uint8_t);
  38. }
  39. static struct xfs_dir2_sf_entry *
  40. xfs_dir2_sf_nextentry(
  41. struct xfs_dir2_sf_hdr *hdr,
  42. struct xfs_dir2_sf_entry *sfep)
  43. {
  44. return (struct xfs_dir2_sf_entry *)
  45. ((char *)sfep + xfs_dir2_sf_entsize(hdr, sfep->namelen));
  46. }
  47. static struct xfs_dir2_sf_entry *
  48. xfs_dir3_sf_nextentry(
  49. struct xfs_dir2_sf_hdr *hdr,
  50. struct xfs_dir2_sf_entry *sfep)
  51. {
  52. return (struct xfs_dir2_sf_entry *)
  53. ((char *)sfep + xfs_dir3_sf_entsize(hdr, sfep->namelen));
  54. }
  55. /*
  56. * For filetype enabled shortform directories, the file type field is stored at
  57. * the end of the name. Because it's only a single byte, endian conversion is
  58. * not necessary. For non-filetype enable directories, the type is always
  59. * unknown and we never store the value.
  60. */
  61. static uint8_t
  62. xfs_dir2_sfe_get_ftype(
  63. struct xfs_dir2_sf_entry *sfep)
  64. {
  65. return XFS_DIR3_FT_UNKNOWN;
  66. }
  67. static void
  68. xfs_dir2_sfe_put_ftype(
  69. struct xfs_dir2_sf_entry *sfep,
  70. uint8_t ftype)
  71. {
  72. ASSERT(ftype < XFS_DIR3_FT_MAX);
  73. }
  74. static uint8_t
  75. xfs_dir3_sfe_get_ftype(
  76. struct xfs_dir2_sf_entry *sfep)
  77. {
  78. uint8_t ftype;
  79. ftype = sfep->name[sfep->namelen];
  80. if (ftype >= XFS_DIR3_FT_MAX)
  81. return XFS_DIR3_FT_UNKNOWN;
  82. return ftype;
  83. }
  84. static void
  85. xfs_dir3_sfe_put_ftype(
  86. struct xfs_dir2_sf_entry *sfep,
  87. uint8_t ftype)
  88. {
  89. ASSERT(ftype < XFS_DIR3_FT_MAX);
  90. sfep->name[sfep->namelen] = ftype;
  91. }
  92. /*
  93. * Inode numbers in short-form directories can come in two versions,
  94. * either 4 bytes or 8 bytes wide. These helpers deal with the
  95. * two forms transparently by looking at the headers i8count field.
  96. *
  97. * For 64-bit inode number the most significant byte must be zero.
  98. */
  99. static xfs_ino_t
  100. xfs_dir2_sf_get_ino(
  101. struct xfs_dir2_sf_hdr *hdr,
  102. uint8_t *from)
  103. {
  104. if (hdr->i8count)
  105. return get_unaligned_be64(from) & 0x00ffffffffffffffULL;
  106. else
  107. return get_unaligned_be32(from);
  108. }
  109. static void
  110. xfs_dir2_sf_put_ino(
  111. struct xfs_dir2_sf_hdr *hdr,
  112. uint8_t *to,
  113. xfs_ino_t ino)
  114. {
  115. ASSERT((ino & 0xff00000000000000ULL) == 0);
  116. if (hdr->i8count)
  117. put_unaligned_be64(ino, to);
  118. else
  119. put_unaligned_be32(ino, to);
  120. }
  121. static xfs_ino_t
  122. xfs_dir2_sf_get_parent_ino(
  123. struct xfs_dir2_sf_hdr *hdr)
  124. {
  125. return xfs_dir2_sf_get_ino(hdr, hdr->parent);
  126. }
  127. static void
  128. xfs_dir2_sf_put_parent_ino(
  129. struct xfs_dir2_sf_hdr *hdr,
  130. xfs_ino_t ino)
  131. {
  132. xfs_dir2_sf_put_ino(hdr, hdr->parent, ino);
  133. }
  134. /*
  135. * In short-form directory entries the inode numbers are stored at variable
  136. * offset behind the entry name. If the entry stores a filetype value, then it
  137. * sits between the name and the inode number. Hence the inode numbers may only
  138. * be accessed through the helpers below.
  139. */
  140. static xfs_ino_t
  141. xfs_dir2_sfe_get_ino(
  142. struct xfs_dir2_sf_hdr *hdr,
  143. struct xfs_dir2_sf_entry *sfep)
  144. {
  145. return xfs_dir2_sf_get_ino(hdr, &sfep->name[sfep->namelen]);
  146. }
  147. static void
  148. xfs_dir2_sfe_put_ino(
  149. struct xfs_dir2_sf_hdr *hdr,
  150. struct xfs_dir2_sf_entry *sfep,
  151. xfs_ino_t ino)
  152. {
  153. xfs_dir2_sf_put_ino(hdr, &sfep->name[sfep->namelen], ino);
  154. }
  155. static xfs_ino_t
  156. xfs_dir3_sfe_get_ino(
  157. struct xfs_dir2_sf_hdr *hdr,
  158. struct xfs_dir2_sf_entry *sfep)
  159. {
  160. return xfs_dir2_sf_get_ino(hdr, &sfep->name[sfep->namelen + 1]);
  161. }
  162. static void
  163. xfs_dir3_sfe_put_ino(
  164. struct xfs_dir2_sf_hdr *hdr,
  165. struct xfs_dir2_sf_entry *sfep,
  166. xfs_ino_t ino)
  167. {
  168. xfs_dir2_sf_put_ino(hdr, &sfep->name[sfep->namelen + 1], ino);
  169. }
  170. /*
  171. * Directory data block operations
  172. */
  173. /*
  174. * For special situations, the dirent size ends up fixed because we always know
  175. * what the size of the entry is. That's true for the "." and "..", and
  176. * therefore we know that they are a fixed size and hence their offsets are
  177. * constant, as is the first entry.
  178. *
  179. * Hence, this calculation is written as a macro to be able to be calculated at
  180. * compile time and so certain offsets can be calculated directly in the
  181. * structure initaliser via the macro. There are two macros - one for dirents
  182. * with ftype and without so there are no unresolvable conditionals in the
  183. * calculations. We also use round_up() as XFS_DIR2_DATA_ALIGN is always a power
  184. * of 2 and the compiler doesn't reject it (unlike roundup()).
  185. */
  186. #define XFS_DIR2_DATA_ENTSIZE(n) \
  187. round_up((offsetof(struct xfs_dir2_data_entry, name[0]) + (n) + \
  188. sizeof(xfs_dir2_data_off_t)), XFS_DIR2_DATA_ALIGN)
  189. #define XFS_DIR3_DATA_ENTSIZE(n) \
  190. round_up((offsetof(struct xfs_dir2_data_entry, name[0]) + (n) + \
  191. sizeof(xfs_dir2_data_off_t) + sizeof(uint8_t)), \
  192. XFS_DIR2_DATA_ALIGN)
  193. static int
  194. xfs_dir2_data_entsize(
  195. int n)
  196. {
  197. return XFS_DIR2_DATA_ENTSIZE(n);
  198. }
  199. static int
  200. xfs_dir3_data_entsize(
  201. int n)
  202. {
  203. return XFS_DIR3_DATA_ENTSIZE(n);
  204. }
  205. static uint8_t
  206. xfs_dir2_data_get_ftype(
  207. struct xfs_dir2_data_entry *dep)
  208. {
  209. return XFS_DIR3_FT_UNKNOWN;
  210. }
  211. static void
  212. xfs_dir2_data_put_ftype(
  213. struct xfs_dir2_data_entry *dep,
  214. uint8_t ftype)
  215. {
  216. ASSERT(ftype < XFS_DIR3_FT_MAX);
  217. }
  218. static uint8_t
  219. xfs_dir3_data_get_ftype(
  220. struct xfs_dir2_data_entry *dep)
  221. {
  222. uint8_t ftype = dep->name[dep->namelen];
  223. if (ftype >= XFS_DIR3_FT_MAX)
  224. return XFS_DIR3_FT_UNKNOWN;
  225. return ftype;
  226. }
  227. static void
  228. xfs_dir3_data_put_ftype(
  229. struct xfs_dir2_data_entry *dep,
  230. uint8_t type)
  231. {
  232. ASSERT(type < XFS_DIR3_FT_MAX);
  233. ASSERT(dep->namelen != 0);
  234. dep->name[dep->namelen] = type;
  235. }
  236. /*
  237. * Pointer to an entry's tag word.
  238. */
  239. static __be16 *
  240. xfs_dir2_data_entry_tag_p(
  241. struct xfs_dir2_data_entry *dep)
  242. {
  243. return (__be16 *)((char *)dep +
  244. xfs_dir2_data_entsize(dep->namelen) - sizeof(__be16));
  245. }
  246. static __be16 *
  247. xfs_dir3_data_entry_tag_p(
  248. struct xfs_dir2_data_entry *dep)
  249. {
  250. return (__be16 *)((char *)dep +
  251. xfs_dir3_data_entsize(dep->namelen) - sizeof(__be16));
  252. }
  253. /*
  254. * location of . and .. in data space (always block 0)
  255. */
  256. static struct xfs_dir2_data_entry *
  257. xfs_dir2_data_dot_entry_p(
  258. struct xfs_dir2_data_hdr *hdr)
  259. {
  260. return (struct xfs_dir2_data_entry *)
  261. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr));
  262. }
  263. static struct xfs_dir2_data_entry *
  264. xfs_dir2_data_dotdot_entry_p(
  265. struct xfs_dir2_data_hdr *hdr)
  266. {
  267. return (struct xfs_dir2_data_entry *)
  268. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr) +
  269. XFS_DIR2_DATA_ENTSIZE(1));
  270. }
  271. static struct xfs_dir2_data_entry *
  272. xfs_dir2_data_first_entry_p(
  273. struct xfs_dir2_data_hdr *hdr)
  274. {
  275. return (struct xfs_dir2_data_entry *)
  276. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr) +
  277. XFS_DIR2_DATA_ENTSIZE(1) +
  278. XFS_DIR2_DATA_ENTSIZE(2));
  279. }
  280. static struct xfs_dir2_data_entry *
  281. xfs_dir2_ftype_data_dotdot_entry_p(
  282. struct xfs_dir2_data_hdr *hdr)
  283. {
  284. return (struct xfs_dir2_data_entry *)
  285. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr) +
  286. XFS_DIR3_DATA_ENTSIZE(1));
  287. }
  288. static struct xfs_dir2_data_entry *
  289. xfs_dir2_ftype_data_first_entry_p(
  290. struct xfs_dir2_data_hdr *hdr)
  291. {
  292. return (struct xfs_dir2_data_entry *)
  293. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr) +
  294. XFS_DIR3_DATA_ENTSIZE(1) +
  295. XFS_DIR3_DATA_ENTSIZE(2));
  296. }
  297. static struct xfs_dir2_data_entry *
  298. xfs_dir3_data_dot_entry_p(
  299. struct xfs_dir2_data_hdr *hdr)
  300. {
  301. return (struct xfs_dir2_data_entry *)
  302. ((char *)hdr + sizeof(struct xfs_dir3_data_hdr));
  303. }
  304. static struct xfs_dir2_data_entry *
  305. xfs_dir3_data_dotdot_entry_p(
  306. struct xfs_dir2_data_hdr *hdr)
  307. {
  308. return (struct xfs_dir2_data_entry *)
  309. ((char *)hdr + sizeof(struct xfs_dir3_data_hdr) +
  310. XFS_DIR3_DATA_ENTSIZE(1));
  311. }
  312. static struct xfs_dir2_data_entry *
  313. xfs_dir3_data_first_entry_p(
  314. struct xfs_dir2_data_hdr *hdr)
  315. {
  316. return (struct xfs_dir2_data_entry *)
  317. ((char *)hdr + sizeof(struct xfs_dir3_data_hdr) +
  318. XFS_DIR3_DATA_ENTSIZE(1) +
  319. XFS_DIR3_DATA_ENTSIZE(2));
  320. }
  321. static struct xfs_dir2_data_free *
  322. xfs_dir2_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
  323. {
  324. return hdr->bestfree;
  325. }
  326. static struct xfs_dir2_data_free *
  327. xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
  328. {
  329. return ((struct xfs_dir3_data_hdr *)hdr)->best_free;
  330. }
  331. static struct xfs_dir2_data_entry *
  332. xfs_dir2_data_entry_p(struct xfs_dir2_data_hdr *hdr)
  333. {
  334. return (struct xfs_dir2_data_entry *)
  335. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr));
  336. }
  337. static struct xfs_dir2_data_unused *
  338. xfs_dir2_data_unused_p(struct xfs_dir2_data_hdr *hdr)
  339. {
  340. return (struct xfs_dir2_data_unused *)
  341. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr));
  342. }
  343. static struct xfs_dir2_data_entry *
  344. xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
  345. {
  346. return (struct xfs_dir2_data_entry *)
  347. ((char *)hdr + sizeof(struct xfs_dir3_data_hdr));
  348. }
  349. static struct xfs_dir2_data_unused *
  350. xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
  351. {
  352. return (struct xfs_dir2_data_unused *)
  353. ((char *)hdr + sizeof(struct xfs_dir3_data_hdr));
  354. }
  355. /*
  356. * Directory Leaf block operations
  357. */
  358. static int
  359. xfs_dir2_max_leaf_ents(struct xfs_da_geometry *geo)
  360. {
  361. return (geo->blksize - sizeof(struct xfs_dir2_leaf_hdr)) /
  362. (uint)sizeof(struct xfs_dir2_leaf_entry);
  363. }
  364. static struct xfs_dir2_leaf_entry *
  365. xfs_dir2_leaf_ents_p(struct xfs_dir2_leaf *lp)
  366. {
  367. return lp->__ents;
  368. }
  369. static int
  370. xfs_dir3_max_leaf_ents(struct xfs_da_geometry *geo)
  371. {
  372. return (geo->blksize - sizeof(struct xfs_dir3_leaf_hdr)) /
  373. (uint)sizeof(struct xfs_dir2_leaf_entry);
  374. }
  375. static struct xfs_dir2_leaf_entry *
  376. xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
  377. {
  378. return ((struct xfs_dir3_leaf *)lp)->__ents;
  379. }
  380. static void
  381. xfs_dir2_leaf_hdr_from_disk(
  382. struct xfs_dir3_icleaf_hdr *to,
  383. struct xfs_dir2_leaf *from)
  384. {
  385. to->forw = be32_to_cpu(from->hdr.info.forw);
  386. to->back = be32_to_cpu(from->hdr.info.back);
  387. to->magic = be16_to_cpu(from->hdr.info.magic);
  388. to->count = be16_to_cpu(from->hdr.count);
  389. to->stale = be16_to_cpu(from->hdr.stale);
  390. ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
  391. to->magic == XFS_DIR2_LEAFN_MAGIC);
  392. }
  393. static void
  394. xfs_dir2_leaf_hdr_to_disk(
  395. struct xfs_dir2_leaf *to,
  396. struct xfs_dir3_icleaf_hdr *from)
  397. {
  398. ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
  399. from->magic == XFS_DIR2_LEAFN_MAGIC);
  400. to->hdr.info.forw = cpu_to_be32(from->forw);
  401. to->hdr.info.back = cpu_to_be32(from->back);
  402. to->hdr.info.magic = cpu_to_be16(from->magic);
  403. to->hdr.count = cpu_to_be16(from->count);
  404. to->hdr.stale = cpu_to_be16(from->stale);
  405. }
  406. static void
  407. xfs_dir3_leaf_hdr_from_disk(
  408. struct xfs_dir3_icleaf_hdr *to,
  409. struct xfs_dir2_leaf *from)
  410. {
  411. struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)from;
  412. to->forw = be32_to_cpu(hdr3->info.hdr.forw);
  413. to->back = be32_to_cpu(hdr3->info.hdr.back);
  414. to->magic = be16_to_cpu(hdr3->info.hdr.magic);
  415. to->count = be16_to_cpu(hdr3->count);
  416. to->stale = be16_to_cpu(hdr3->stale);
  417. ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC ||
  418. to->magic == XFS_DIR3_LEAFN_MAGIC);
  419. }
  420. static void
  421. xfs_dir3_leaf_hdr_to_disk(
  422. struct xfs_dir2_leaf *to,
  423. struct xfs_dir3_icleaf_hdr *from)
  424. {
  425. struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)to;
  426. ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
  427. from->magic == XFS_DIR3_LEAFN_MAGIC);
  428. hdr3->info.hdr.forw = cpu_to_be32(from->forw);
  429. hdr3->info.hdr.back = cpu_to_be32(from->back);
  430. hdr3->info.hdr.magic = cpu_to_be16(from->magic);
  431. hdr3->count = cpu_to_be16(from->count);
  432. hdr3->stale = cpu_to_be16(from->stale);
  433. }
  434. /*
  435. * Directory/Attribute Node block operations
  436. */
  437. static struct xfs_da_node_entry *
  438. xfs_da2_node_tree_p(struct xfs_da_intnode *dap)
  439. {
  440. return dap->__btree;
  441. }
  442. static struct xfs_da_node_entry *
  443. xfs_da3_node_tree_p(struct xfs_da_intnode *dap)
  444. {
  445. return ((struct xfs_da3_intnode *)dap)->__btree;
  446. }
  447. static void
  448. xfs_da2_node_hdr_from_disk(
  449. struct xfs_da3_icnode_hdr *to,
  450. struct xfs_da_intnode *from)
  451. {
  452. ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
  453. to->forw = be32_to_cpu(from->hdr.info.forw);
  454. to->back = be32_to_cpu(from->hdr.info.back);
  455. to->magic = be16_to_cpu(from->hdr.info.magic);
  456. to->count = be16_to_cpu(from->hdr.__count);
  457. to->level = be16_to_cpu(from->hdr.__level);
  458. }
  459. static void
  460. xfs_da2_node_hdr_to_disk(
  461. struct xfs_da_intnode *to,
  462. struct xfs_da3_icnode_hdr *from)
  463. {
  464. ASSERT(from->magic == XFS_DA_NODE_MAGIC);
  465. to->hdr.info.forw = cpu_to_be32(from->forw);
  466. to->hdr.info.back = cpu_to_be32(from->back);
  467. to->hdr.info.magic = cpu_to_be16(from->magic);
  468. to->hdr.__count = cpu_to_be16(from->count);
  469. to->hdr.__level = cpu_to_be16(from->level);
  470. }
  471. static void
  472. xfs_da3_node_hdr_from_disk(
  473. struct xfs_da3_icnode_hdr *to,
  474. struct xfs_da_intnode *from)
  475. {
  476. struct xfs_da3_node_hdr *hdr3 = (struct xfs_da3_node_hdr *)from;
  477. ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
  478. to->forw = be32_to_cpu(hdr3->info.hdr.forw);
  479. to->back = be32_to_cpu(hdr3->info.hdr.back);
  480. to->magic = be16_to_cpu(hdr3->info.hdr.magic);
  481. to->count = be16_to_cpu(hdr3->__count);
  482. to->level = be16_to_cpu(hdr3->__level);
  483. }
  484. static void
  485. xfs_da3_node_hdr_to_disk(
  486. struct xfs_da_intnode *to,
  487. struct xfs_da3_icnode_hdr *from)
  488. {
  489. struct xfs_da3_node_hdr *hdr3 = (struct xfs_da3_node_hdr *)to;
  490. ASSERT(from->magic == XFS_DA3_NODE_MAGIC);
  491. hdr3->info.hdr.forw = cpu_to_be32(from->forw);
  492. hdr3->info.hdr.back = cpu_to_be32(from->back);
  493. hdr3->info.hdr.magic = cpu_to_be16(from->magic);
  494. hdr3->__count = cpu_to_be16(from->count);
  495. hdr3->__level = cpu_to_be16(from->level);
  496. }
  497. /*
  498. * Directory free space block operations
  499. */
  500. static int
  501. xfs_dir2_free_max_bests(struct xfs_da_geometry *geo)
  502. {
  503. return (geo->blksize - sizeof(struct xfs_dir2_free_hdr)) /
  504. sizeof(xfs_dir2_data_off_t);
  505. }
  506. static __be16 *
  507. xfs_dir2_free_bests_p(struct xfs_dir2_free *free)
  508. {
  509. return (__be16 *)((char *)free + sizeof(struct xfs_dir2_free_hdr));
  510. }
  511. /*
  512. * Convert data space db to the corresponding free db.
  513. */
  514. static xfs_dir2_db_t
  515. xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  516. {
  517. return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
  518. (db / xfs_dir2_free_max_bests(geo));
  519. }
  520. /*
  521. * Convert data space db to the corresponding index in a free db.
  522. */
  523. static int
  524. xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  525. {
  526. return db % xfs_dir2_free_max_bests(geo);
  527. }
  528. static int
  529. xfs_dir3_free_max_bests(struct xfs_da_geometry *geo)
  530. {
  531. return (geo->blksize - sizeof(struct xfs_dir3_free_hdr)) /
  532. sizeof(xfs_dir2_data_off_t);
  533. }
  534. static __be16 *
  535. xfs_dir3_free_bests_p(struct xfs_dir2_free *free)
  536. {
  537. return (__be16 *)((char *)free + sizeof(struct xfs_dir3_free_hdr));
  538. }
  539. /*
  540. * Convert data space db to the corresponding free db.
  541. */
  542. static xfs_dir2_db_t
  543. xfs_dir3_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  544. {
  545. return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
  546. (db / xfs_dir3_free_max_bests(geo));
  547. }
  548. /*
  549. * Convert data space db to the corresponding index in a free db.
  550. */
  551. static int
  552. xfs_dir3_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  553. {
  554. return db % xfs_dir3_free_max_bests(geo);
  555. }
  556. static void
  557. xfs_dir2_free_hdr_from_disk(
  558. struct xfs_dir3_icfree_hdr *to,
  559. struct xfs_dir2_free *from)
  560. {
  561. to->magic = be32_to_cpu(from->hdr.magic);
  562. to->firstdb = be32_to_cpu(from->hdr.firstdb);
  563. to->nvalid = be32_to_cpu(from->hdr.nvalid);
  564. to->nused = be32_to_cpu(from->hdr.nused);
  565. ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
  566. }
  567. static void
  568. xfs_dir2_free_hdr_to_disk(
  569. struct xfs_dir2_free *to,
  570. struct xfs_dir3_icfree_hdr *from)
  571. {
  572. ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
  573. to->hdr.magic = cpu_to_be32(from->magic);
  574. to->hdr.firstdb = cpu_to_be32(from->firstdb);
  575. to->hdr.nvalid = cpu_to_be32(from->nvalid);
  576. to->hdr.nused = cpu_to_be32(from->nused);
  577. }
  578. static void
  579. xfs_dir3_free_hdr_from_disk(
  580. struct xfs_dir3_icfree_hdr *to,
  581. struct xfs_dir2_free *from)
  582. {
  583. struct xfs_dir3_free_hdr *hdr3 = (struct xfs_dir3_free_hdr *)from;
  584. to->magic = be32_to_cpu(hdr3->hdr.magic);
  585. to->firstdb = be32_to_cpu(hdr3->firstdb);
  586. to->nvalid = be32_to_cpu(hdr3->nvalid);
  587. to->nused = be32_to_cpu(hdr3->nused);
  588. ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
  589. }
  590. static void
  591. xfs_dir3_free_hdr_to_disk(
  592. struct xfs_dir2_free *to,
  593. struct xfs_dir3_icfree_hdr *from)
  594. {
  595. struct xfs_dir3_free_hdr *hdr3 = (struct xfs_dir3_free_hdr *)to;
  596. ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
  597. hdr3->hdr.magic = cpu_to_be32(from->magic);
  598. hdr3->firstdb = cpu_to_be32(from->firstdb);
  599. hdr3->nvalid = cpu_to_be32(from->nvalid);
  600. hdr3->nused = cpu_to_be32(from->nused);
  601. }
  602. static const struct xfs_dir_ops xfs_dir2_ops = {
  603. .sf_entsize = xfs_dir2_sf_entsize,
  604. .sf_nextentry = xfs_dir2_sf_nextentry,
  605. .sf_get_ftype = xfs_dir2_sfe_get_ftype,
  606. .sf_put_ftype = xfs_dir2_sfe_put_ftype,
  607. .sf_get_ino = xfs_dir2_sfe_get_ino,
  608. .sf_put_ino = xfs_dir2_sfe_put_ino,
  609. .sf_get_parent_ino = xfs_dir2_sf_get_parent_ino,
  610. .sf_put_parent_ino = xfs_dir2_sf_put_parent_ino,
  611. .data_entsize = xfs_dir2_data_entsize,
  612. .data_get_ftype = xfs_dir2_data_get_ftype,
  613. .data_put_ftype = xfs_dir2_data_put_ftype,
  614. .data_entry_tag_p = xfs_dir2_data_entry_tag_p,
  615. .data_bestfree_p = xfs_dir2_data_bestfree_p,
  616. .data_dot_offset = sizeof(struct xfs_dir2_data_hdr),
  617. .data_dotdot_offset = sizeof(struct xfs_dir2_data_hdr) +
  618. XFS_DIR2_DATA_ENTSIZE(1),
  619. .data_first_offset = sizeof(struct xfs_dir2_data_hdr) +
  620. XFS_DIR2_DATA_ENTSIZE(1) +
  621. XFS_DIR2_DATA_ENTSIZE(2),
  622. .data_entry_offset = sizeof(struct xfs_dir2_data_hdr),
  623. .data_dot_entry_p = xfs_dir2_data_dot_entry_p,
  624. .data_dotdot_entry_p = xfs_dir2_data_dotdot_entry_p,
  625. .data_first_entry_p = xfs_dir2_data_first_entry_p,
  626. .data_entry_p = xfs_dir2_data_entry_p,
  627. .data_unused_p = xfs_dir2_data_unused_p,
  628. .leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr),
  629. .leaf_hdr_to_disk = xfs_dir2_leaf_hdr_to_disk,
  630. .leaf_hdr_from_disk = xfs_dir2_leaf_hdr_from_disk,
  631. .leaf_max_ents = xfs_dir2_max_leaf_ents,
  632. .leaf_ents_p = xfs_dir2_leaf_ents_p,
  633. .node_hdr_size = sizeof(struct xfs_da_node_hdr),
  634. .node_hdr_to_disk = xfs_da2_node_hdr_to_disk,
  635. .node_hdr_from_disk = xfs_da2_node_hdr_from_disk,
  636. .node_tree_p = xfs_da2_node_tree_p,
  637. .free_hdr_size = sizeof(struct xfs_dir2_free_hdr),
  638. .free_hdr_to_disk = xfs_dir2_free_hdr_to_disk,
  639. .free_hdr_from_disk = xfs_dir2_free_hdr_from_disk,
  640. .free_max_bests = xfs_dir2_free_max_bests,
  641. .free_bests_p = xfs_dir2_free_bests_p,
  642. .db_to_fdb = xfs_dir2_db_to_fdb,
  643. .db_to_fdindex = xfs_dir2_db_to_fdindex,
  644. };
  645. static const struct xfs_dir_ops xfs_dir2_ftype_ops = {
  646. .sf_entsize = xfs_dir3_sf_entsize,
  647. .sf_nextentry = xfs_dir3_sf_nextentry,
  648. .sf_get_ftype = xfs_dir3_sfe_get_ftype,
  649. .sf_put_ftype = xfs_dir3_sfe_put_ftype,
  650. .sf_get_ino = xfs_dir3_sfe_get_ino,
  651. .sf_put_ino = xfs_dir3_sfe_put_ino,
  652. .sf_get_parent_ino = xfs_dir2_sf_get_parent_ino,
  653. .sf_put_parent_ino = xfs_dir2_sf_put_parent_ino,
  654. .data_entsize = xfs_dir3_data_entsize,
  655. .data_get_ftype = xfs_dir3_data_get_ftype,
  656. .data_put_ftype = xfs_dir3_data_put_ftype,
  657. .data_entry_tag_p = xfs_dir3_data_entry_tag_p,
  658. .data_bestfree_p = xfs_dir2_data_bestfree_p,
  659. .data_dot_offset = sizeof(struct xfs_dir2_data_hdr),
  660. .data_dotdot_offset = sizeof(struct xfs_dir2_data_hdr) +
  661. XFS_DIR3_DATA_ENTSIZE(1),
  662. .data_first_offset = sizeof(struct xfs_dir2_data_hdr) +
  663. XFS_DIR3_DATA_ENTSIZE(1) +
  664. XFS_DIR3_DATA_ENTSIZE(2),
  665. .data_entry_offset = sizeof(struct xfs_dir2_data_hdr),
  666. .data_dot_entry_p = xfs_dir2_data_dot_entry_p,
  667. .data_dotdot_entry_p = xfs_dir2_ftype_data_dotdot_entry_p,
  668. .data_first_entry_p = xfs_dir2_ftype_data_first_entry_p,
  669. .data_entry_p = xfs_dir2_data_entry_p,
  670. .data_unused_p = xfs_dir2_data_unused_p,
  671. .leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr),
  672. .leaf_hdr_to_disk = xfs_dir2_leaf_hdr_to_disk,
  673. .leaf_hdr_from_disk = xfs_dir2_leaf_hdr_from_disk,
  674. .leaf_max_ents = xfs_dir2_max_leaf_ents,
  675. .leaf_ents_p = xfs_dir2_leaf_ents_p,
  676. .node_hdr_size = sizeof(struct xfs_da_node_hdr),
  677. .node_hdr_to_disk = xfs_da2_node_hdr_to_disk,
  678. .node_hdr_from_disk = xfs_da2_node_hdr_from_disk,
  679. .node_tree_p = xfs_da2_node_tree_p,
  680. .free_hdr_size = sizeof(struct xfs_dir2_free_hdr),
  681. .free_hdr_to_disk = xfs_dir2_free_hdr_to_disk,
  682. .free_hdr_from_disk = xfs_dir2_free_hdr_from_disk,
  683. .free_max_bests = xfs_dir2_free_max_bests,
  684. .free_bests_p = xfs_dir2_free_bests_p,
  685. .db_to_fdb = xfs_dir2_db_to_fdb,
  686. .db_to_fdindex = xfs_dir2_db_to_fdindex,
  687. };
  688. static const struct xfs_dir_ops xfs_dir3_ops = {
  689. .sf_entsize = xfs_dir3_sf_entsize,
  690. .sf_nextentry = xfs_dir3_sf_nextentry,
  691. .sf_get_ftype = xfs_dir3_sfe_get_ftype,
  692. .sf_put_ftype = xfs_dir3_sfe_put_ftype,
  693. .sf_get_ino = xfs_dir3_sfe_get_ino,
  694. .sf_put_ino = xfs_dir3_sfe_put_ino,
  695. .sf_get_parent_ino = xfs_dir2_sf_get_parent_ino,
  696. .sf_put_parent_ino = xfs_dir2_sf_put_parent_ino,
  697. .data_entsize = xfs_dir3_data_entsize,
  698. .data_get_ftype = xfs_dir3_data_get_ftype,
  699. .data_put_ftype = xfs_dir3_data_put_ftype,
  700. .data_entry_tag_p = xfs_dir3_data_entry_tag_p,
  701. .data_bestfree_p = xfs_dir3_data_bestfree_p,
  702. .data_dot_offset = sizeof(struct xfs_dir3_data_hdr),
  703. .data_dotdot_offset = sizeof(struct xfs_dir3_data_hdr) +
  704. XFS_DIR3_DATA_ENTSIZE(1),
  705. .data_first_offset = sizeof(struct xfs_dir3_data_hdr) +
  706. XFS_DIR3_DATA_ENTSIZE(1) +
  707. XFS_DIR3_DATA_ENTSIZE(2),
  708. .data_entry_offset = sizeof(struct xfs_dir3_data_hdr),
  709. .data_dot_entry_p = xfs_dir3_data_dot_entry_p,
  710. .data_dotdot_entry_p = xfs_dir3_data_dotdot_entry_p,
  711. .data_first_entry_p = xfs_dir3_data_first_entry_p,
  712. .data_entry_p = xfs_dir3_data_entry_p,
  713. .data_unused_p = xfs_dir3_data_unused_p,
  714. .leaf_hdr_size = sizeof(struct xfs_dir3_leaf_hdr),
  715. .leaf_hdr_to_disk = xfs_dir3_leaf_hdr_to_disk,
  716. .leaf_hdr_from_disk = xfs_dir3_leaf_hdr_from_disk,
  717. .leaf_max_ents = xfs_dir3_max_leaf_ents,
  718. .leaf_ents_p = xfs_dir3_leaf_ents_p,
  719. .node_hdr_size = sizeof(struct xfs_da3_node_hdr),
  720. .node_hdr_to_disk = xfs_da3_node_hdr_to_disk,
  721. .node_hdr_from_disk = xfs_da3_node_hdr_from_disk,
  722. .node_tree_p = xfs_da3_node_tree_p,
  723. .free_hdr_size = sizeof(struct xfs_dir3_free_hdr),
  724. .free_hdr_to_disk = xfs_dir3_free_hdr_to_disk,
  725. .free_hdr_from_disk = xfs_dir3_free_hdr_from_disk,
  726. .free_max_bests = xfs_dir3_free_max_bests,
  727. .free_bests_p = xfs_dir3_free_bests_p,
  728. .db_to_fdb = xfs_dir3_db_to_fdb,
  729. .db_to_fdindex = xfs_dir3_db_to_fdindex,
  730. };
  731. static const struct xfs_dir_ops xfs_dir2_nondir_ops = {
  732. .node_hdr_size = sizeof(struct xfs_da_node_hdr),
  733. .node_hdr_to_disk = xfs_da2_node_hdr_to_disk,
  734. .node_hdr_from_disk = xfs_da2_node_hdr_from_disk,
  735. .node_tree_p = xfs_da2_node_tree_p,
  736. };
  737. static const struct xfs_dir_ops xfs_dir3_nondir_ops = {
  738. .node_hdr_size = sizeof(struct xfs_da3_node_hdr),
  739. .node_hdr_to_disk = xfs_da3_node_hdr_to_disk,
  740. .node_hdr_from_disk = xfs_da3_node_hdr_from_disk,
  741. .node_tree_p = xfs_da3_node_tree_p,
  742. };
  743. /*
  744. * Return the ops structure according to the current config. If we are passed
  745. * an inode, then that overrides the default config we use which is based on
  746. * feature bits.
  747. */
  748. const struct xfs_dir_ops *
  749. xfs_dir_get_ops(
  750. struct xfs_mount *mp,
  751. struct xfs_inode *dp)
  752. {
  753. if (dp)
  754. return dp->d_ops;
  755. if (mp->m_dir_inode_ops)
  756. return mp->m_dir_inode_ops;
  757. if (xfs_sb_version_hascrc(&mp->m_sb))
  758. return &xfs_dir3_ops;
  759. if (xfs_sb_version_hasftype(&mp->m_sb))
  760. return &xfs_dir2_ftype_ops;
  761. return &xfs_dir2_ops;
  762. }
  763. const struct xfs_dir_ops *
  764. xfs_nondir_get_ops(
  765. struct xfs_mount *mp,
  766. struct xfs_inode *dp)
  767. {
  768. if (dp)
  769. return dp->d_ops;
  770. if (mp->m_nondir_inode_ops)
  771. return mp->m_nondir_inode_ops;
  772. if (xfs_sb_version_hascrc(&mp->m_sb))
  773. return &xfs_dir3_nondir_ops;
  774. return &xfs_dir2_nondir_ops;
  775. }