xfs_da_format.c 24 KB

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