xfs_da_format.c 24 KB

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