xfs_da_format.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. ASSERT(ftype < XFS_DIR3_FT_MAX);
  243. if (ftype >= XFS_DIR3_FT_MAX)
  244. return XFS_DIR3_FT_UNKNOWN;
  245. return ftype;
  246. }
  247. static void
  248. xfs_dir3_data_put_ftype(
  249. struct xfs_dir2_data_entry *dep,
  250. __uint8_t type)
  251. {
  252. ASSERT(type < XFS_DIR3_FT_MAX);
  253. ASSERT(dep->namelen != 0);
  254. dep->name[dep->namelen] = type;
  255. }
  256. /*
  257. * Pointer to an entry's tag word.
  258. */
  259. static __be16 *
  260. xfs_dir2_data_entry_tag_p(
  261. struct xfs_dir2_data_entry *dep)
  262. {
  263. return (__be16 *)((char *)dep +
  264. xfs_dir2_data_entsize(dep->namelen) - sizeof(__be16));
  265. }
  266. static __be16 *
  267. xfs_dir3_data_entry_tag_p(
  268. struct xfs_dir2_data_entry *dep)
  269. {
  270. return (__be16 *)((char *)dep +
  271. xfs_dir3_data_entsize(dep->namelen) - sizeof(__be16));
  272. }
  273. /*
  274. * location of . and .. in data space (always block 0)
  275. */
  276. static struct xfs_dir2_data_entry *
  277. xfs_dir2_data_dot_entry_p(
  278. struct xfs_dir2_data_hdr *hdr)
  279. {
  280. return (struct xfs_dir2_data_entry *)
  281. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr));
  282. }
  283. static struct xfs_dir2_data_entry *
  284. xfs_dir2_data_dotdot_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. }
  291. static struct xfs_dir2_data_entry *
  292. xfs_dir2_data_first_entry_p(
  293. struct xfs_dir2_data_hdr *hdr)
  294. {
  295. return (struct xfs_dir2_data_entry *)
  296. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr) +
  297. XFS_DIR2_DATA_ENTSIZE(1) +
  298. XFS_DIR2_DATA_ENTSIZE(2));
  299. }
  300. static struct xfs_dir2_data_entry *
  301. xfs_dir2_ftype_data_dotdot_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. }
  308. static struct xfs_dir2_data_entry *
  309. xfs_dir2_ftype_data_first_entry_p(
  310. struct xfs_dir2_data_hdr *hdr)
  311. {
  312. return (struct xfs_dir2_data_entry *)
  313. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr) +
  314. XFS_DIR3_DATA_ENTSIZE(1) +
  315. XFS_DIR3_DATA_ENTSIZE(2));
  316. }
  317. static struct xfs_dir2_data_entry *
  318. xfs_dir3_data_dot_entry_p(
  319. struct xfs_dir2_data_hdr *hdr)
  320. {
  321. return (struct xfs_dir2_data_entry *)
  322. ((char *)hdr + sizeof(struct xfs_dir3_data_hdr));
  323. }
  324. static struct xfs_dir2_data_entry *
  325. xfs_dir3_data_dotdot_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. }
  332. static struct xfs_dir2_data_entry *
  333. xfs_dir3_data_first_entry_p(
  334. struct xfs_dir2_data_hdr *hdr)
  335. {
  336. return (struct xfs_dir2_data_entry *)
  337. ((char *)hdr + sizeof(struct xfs_dir3_data_hdr) +
  338. XFS_DIR3_DATA_ENTSIZE(1) +
  339. XFS_DIR3_DATA_ENTSIZE(2));
  340. }
  341. static struct xfs_dir2_data_free *
  342. xfs_dir2_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
  343. {
  344. return hdr->bestfree;
  345. }
  346. static struct xfs_dir2_data_free *
  347. xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
  348. {
  349. return ((struct xfs_dir3_data_hdr *)hdr)->best_free;
  350. }
  351. static struct xfs_dir2_data_entry *
  352. xfs_dir2_data_entry_p(struct xfs_dir2_data_hdr *hdr)
  353. {
  354. return (struct xfs_dir2_data_entry *)
  355. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr));
  356. }
  357. static struct xfs_dir2_data_unused *
  358. xfs_dir2_data_unused_p(struct xfs_dir2_data_hdr *hdr)
  359. {
  360. return (struct xfs_dir2_data_unused *)
  361. ((char *)hdr + sizeof(struct xfs_dir2_data_hdr));
  362. }
  363. static struct xfs_dir2_data_entry *
  364. xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
  365. {
  366. return (struct xfs_dir2_data_entry *)
  367. ((char *)hdr + sizeof(struct xfs_dir3_data_hdr));
  368. }
  369. static struct xfs_dir2_data_unused *
  370. xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
  371. {
  372. return (struct xfs_dir2_data_unused *)
  373. ((char *)hdr + sizeof(struct xfs_dir3_data_hdr));
  374. }
  375. /*
  376. * Directory Leaf block operations
  377. */
  378. static int
  379. xfs_dir2_max_leaf_ents(struct xfs_da_geometry *geo)
  380. {
  381. return (geo->blksize - sizeof(struct xfs_dir2_leaf_hdr)) /
  382. (uint)sizeof(struct xfs_dir2_leaf_entry);
  383. }
  384. static struct xfs_dir2_leaf_entry *
  385. xfs_dir2_leaf_ents_p(struct xfs_dir2_leaf *lp)
  386. {
  387. return lp->__ents;
  388. }
  389. static int
  390. xfs_dir3_max_leaf_ents(struct xfs_da_geometry *geo)
  391. {
  392. return (geo->blksize - sizeof(struct xfs_dir3_leaf_hdr)) /
  393. (uint)sizeof(struct xfs_dir2_leaf_entry);
  394. }
  395. static struct xfs_dir2_leaf_entry *
  396. xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
  397. {
  398. return ((struct xfs_dir3_leaf *)lp)->__ents;
  399. }
  400. static void
  401. xfs_dir2_leaf_hdr_from_disk(
  402. struct xfs_dir3_icleaf_hdr *to,
  403. struct xfs_dir2_leaf *from)
  404. {
  405. to->forw = be32_to_cpu(from->hdr.info.forw);
  406. to->back = be32_to_cpu(from->hdr.info.back);
  407. to->magic = be16_to_cpu(from->hdr.info.magic);
  408. to->count = be16_to_cpu(from->hdr.count);
  409. to->stale = be16_to_cpu(from->hdr.stale);
  410. ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
  411. to->magic == XFS_DIR2_LEAFN_MAGIC);
  412. }
  413. static void
  414. xfs_dir2_leaf_hdr_to_disk(
  415. struct xfs_dir2_leaf *to,
  416. struct xfs_dir3_icleaf_hdr *from)
  417. {
  418. ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
  419. from->magic == XFS_DIR2_LEAFN_MAGIC);
  420. to->hdr.info.forw = cpu_to_be32(from->forw);
  421. to->hdr.info.back = cpu_to_be32(from->back);
  422. to->hdr.info.magic = cpu_to_be16(from->magic);
  423. to->hdr.count = cpu_to_be16(from->count);
  424. to->hdr.stale = cpu_to_be16(from->stale);
  425. }
  426. static void
  427. xfs_dir3_leaf_hdr_from_disk(
  428. struct xfs_dir3_icleaf_hdr *to,
  429. struct xfs_dir2_leaf *from)
  430. {
  431. struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)from;
  432. to->forw = be32_to_cpu(hdr3->info.hdr.forw);
  433. to->back = be32_to_cpu(hdr3->info.hdr.back);
  434. to->magic = be16_to_cpu(hdr3->info.hdr.magic);
  435. to->count = be16_to_cpu(hdr3->count);
  436. to->stale = be16_to_cpu(hdr3->stale);
  437. ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC ||
  438. to->magic == XFS_DIR3_LEAFN_MAGIC);
  439. }
  440. static void
  441. xfs_dir3_leaf_hdr_to_disk(
  442. struct xfs_dir2_leaf *to,
  443. struct xfs_dir3_icleaf_hdr *from)
  444. {
  445. struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)to;
  446. ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
  447. from->magic == XFS_DIR3_LEAFN_MAGIC);
  448. hdr3->info.hdr.forw = cpu_to_be32(from->forw);
  449. hdr3->info.hdr.back = cpu_to_be32(from->back);
  450. hdr3->info.hdr.magic = cpu_to_be16(from->magic);
  451. hdr3->count = cpu_to_be16(from->count);
  452. hdr3->stale = cpu_to_be16(from->stale);
  453. }
  454. /*
  455. * Directory/Attribute Node block operations
  456. */
  457. static struct xfs_da_node_entry *
  458. xfs_da2_node_tree_p(struct xfs_da_intnode *dap)
  459. {
  460. return dap->__btree;
  461. }
  462. static struct xfs_da_node_entry *
  463. xfs_da3_node_tree_p(struct xfs_da_intnode *dap)
  464. {
  465. return ((struct xfs_da3_intnode *)dap)->__btree;
  466. }
  467. static void
  468. xfs_da2_node_hdr_from_disk(
  469. struct xfs_da3_icnode_hdr *to,
  470. struct xfs_da_intnode *from)
  471. {
  472. ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
  473. to->forw = be32_to_cpu(from->hdr.info.forw);
  474. to->back = be32_to_cpu(from->hdr.info.back);
  475. to->magic = be16_to_cpu(from->hdr.info.magic);
  476. to->count = be16_to_cpu(from->hdr.__count);
  477. to->level = be16_to_cpu(from->hdr.__level);
  478. }
  479. static void
  480. xfs_da2_node_hdr_to_disk(
  481. struct xfs_da_intnode *to,
  482. struct xfs_da3_icnode_hdr *from)
  483. {
  484. ASSERT(from->magic == XFS_DA_NODE_MAGIC);
  485. to->hdr.info.forw = cpu_to_be32(from->forw);
  486. to->hdr.info.back = cpu_to_be32(from->back);
  487. to->hdr.info.magic = cpu_to_be16(from->magic);
  488. to->hdr.__count = cpu_to_be16(from->count);
  489. to->hdr.__level = cpu_to_be16(from->level);
  490. }
  491. static void
  492. xfs_da3_node_hdr_from_disk(
  493. struct xfs_da3_icnode_hdr *to,
  494. struct xfs_da_intnode *from)
  495. {
  496. struct xfs_da3_node_hdr *hdr3 = (struct xfs_da3_node_hdr *)from;
  497. ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
  498. to->forw = be32_to_cpu(hdr3->info.hdr.forw);
  499. to->back = be32_to_cpu(hdr3->info.hdr.back);
  500. to->magic = be16_to_cpu(hdr3->info.hdr.magic);
  501. to->count = be16_to_cpu(hdr3->__count);
  502. to->level = be16_to_cpu(hdr3->__level);
  503. }
  504. static void
  505. xfs_da3_node_hdr_to_disk(
  506. struct xfs_da_intnode *to,
  507. struct xfs_da3_icnode_hdr *from)
  508. {
  509. struct xfs_da3_node_hdr *hdr3 = (struct xfs_da3_node_hdr *)to;
  510. ASSERT(from->magic == XFS_DA3_NODE_MAGIC);
  511. hdr3->info.hdr.forw = cpu_to_be32(from->forw);
  512. hdr3->info.hdr.back = cpu_to_be32(from->back);
  513. hdr3->info.hdr.magic = cpu_to_be16(from->magic);
  514. hdr3->__count = cpu_to_be16(from->count);
  515. hdr3->__level = cpu_to_be16(from->level);
  516. }
  517. /*
  518. * Directory free space block operations
  519. */
  520. static int
  521. xfs_dir2_free_max_bests(struct xfs_da_geometry *geo)
  522. {
  523. return (geo->blksize - sizeof(struct xfs_dir2_free_hdr)) /
  524. sizeof(xfs_dir2_data_off_t);
  525. }
  526. static __be16 *
  527. xfs_dir2_free_bests_p(struct xfs_dir2_free *free)
  528. {
  529. return (__be16 *)((char *)free + sizeof(struct xfs_dir2_free_hdr));
  530. }
  531. /*
  532. * Convert data space db to the corresponding free db.
  533. */
  534. static xfs_dir2_db_t
  535. xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  536. {
  537. return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
  538. (db / xfs_dir2_free_max_bests(geo));
  539. }
  540. /*
  541. * Convert data space db to the corresponding index in a free db.
  542. */
  543. static int
  544. xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  545. {
  546. return db % xfs_dir2_free_max_bests(geo);
  547. }
  548. static int
  549. xfs_dir3_free_max_bests(struct xfs_da_geometry *geo)
  550. {
  551. return (geo->blksize - sizeof(struct xfs_dir3_free_hdr)) /
  552. sizeof(xfs_dir2_data_off_t);
  553. }
  554. static __be16 *
  555. xfs_dir3_free_bests_p(struct xfs_dir2_free *free)
  556. {
  557. return (__be16 *)((char *)free + sizeof(struct xfs_dir3_free_hdr));
  558. }
  559. /*
  560. * Convert data space db to the corresponding free db.
  561. */
  562. static xfs_dir2_db_t
  563. xfs_dir3_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  564. {
  565. return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
  566. (db / xfs_dir3_free_max_bests(geo));
  567. }
  568. /*
  569. * Convert data space db to the corresponding index in a free db.
  570. */
  571. static int
  572. xfs_dir3_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  573. {
  574. return db % xfs_dir3_free_max_bests(geo);
  575. }
  576. static void
  577. xfs_dir2_free_hdr_from_disk(
  578. struct xfs_dir3_icfree_hdr *to,
  579. struct xfs_dir2_free *from)
  580. {
  581. to->magic = be32_to_cpu(from->hdr.magic);
  582. to->firstdb = be32_to_cpu(from->hdr.firstdb);
  583. to->nvalid = be32_to_cpu(from->hdr.nvalid);
  584. to->nused = be32_to_cpu(from->hdr.nused);
  585. ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
  586. }
  587. static void
  588. xfs_dir2_free_hdr_to_disk(
  589. struct xfs_dir2_free *to,
  590. struct xfs_dir3_icfree_hdr *from)
  591. {
  592. ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
  593. to->hdr.magic = cpu_to_be32(from->magic);
  594. to->hdr.firstdb = cpu_to_be32(from->firstdb);
  595. to->hdr.nvalid = cpu_to_be32(from->nvalid);
  596. to->hdr.nused = cpu_to_be32(from->nused);
  597. }
  598. static void
  599. xfs_dir3_free_hdr_from_disk(
  600. struct xfs_dir3_icfree_hdr *to,
  601. struct xfs_dir2_free *from)
  602. {
  603. struct xfs_dir3_free_hdr *hdr3 = (struct xfs_dir3_free_hdr *)from;
  604. to->magic = be32_to_cpu(hdr3->hdr.magic);
  605. to->firstdb = be32_to_cpu(hdr3->firstdb);
  606. to->nvalid = be32_to_cpu(hdr3->nvalid);
  607. to->nused = be32_to_cpu(hdr3->nused);
  608. ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
  609. }
  610. static void
  611. xfs_dir3_free_hdr_to_disk(
  612. struct xfs_dir2_free *to,
  613. struct xfs_dir3_icfree_hdr *from)
  614. {
  615. struct xfs_dir3_free_hdr *hdr3 = (struct xfs_dir3_free_hdr *)to;
  616. ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
  617. hdr3->hdr.magic = cpu_to_be32(from->magic);
  618. hdr3->firstdb = cpu_to_be32(from->firstdb);
  619. hdr3->nvalid = cpu_to_be32(from->nvalid);
  620. hdr3->nused = cpu_to_be32(from->nused);
  621. }
  622. static const struct xfs_dir_ops xfs_dir2_ops = {
  623. .sf_entsize = xfs_dir2_sf_entsize,
  624. .sf_nextentry = xfs_dir2_sf_nextentry,
  625. .sf_get_ftype = xfs_dir2_sfe_get_ftype,
  626. .sf_put_ftype = xfs_dir2_sfe_put_ftype,
  627. .sf_get_ino = xfs_dir2_sfe_get_ino,
  628. .sf_put_ino = xfs_dir2_sfe_put_ino,
  629. .sf_get_parent_ino = xfs_dir2_sf_get_parent_ino,
  630. .sf_put_parent_ino = xfs_dir2_sf_put_parent_ino,
  631. .data_entsize = xfs_dir2_data_entsize,
  632. .data_get_ftype = xfs_dir2_data_get_ftype,
  633. .data_put_ftype = xfs_dir2_data_put_ftype,
  634. .data_entry_tag_p = xfs_dir2_data_entry_tag_p,
  635. .data_bestfree_p = xfs_dir2_data_bestfree_p,
  636. .data_dot_offset = sizeof(struct xfs_dir2_data_hdr),
  637. .data_dotdot_offset = sizeof(struct xfs_dir2_data_hdr) +
  638. XFS_DIR2_DATA_ENTSIZE(1),
  639. .data_first_offset = sizeof(struct xfs_dir2_data_hdr) +
  640. XFS_DIR2_DATA_ENTSIZE(1) +
  641. XFS_DIR2_DATA_ENTSIZE(2),
  642. .data_entry_offset = sizeof(struct xfs_dir2_data_hdr),
  643. .data_dot_entry_p = xfs_dir2_data_dot_entry_p,
  644. .data_dotdot_entry_p = xfs_dir2_data_dotdot_entry_p,
  645. .data_first_entry_p = xfs_dir2_data_first_entry_p,
  646. .data_entry_p = xfs_dir2_data_entry_p,
  647. .data_unused_p = xfs_dir2_data_unused_p,
  648. .leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr),
  649. .leaf_hdr_to_disk = xfs_dir2_leaf_hdr_to_disk,
  650. .leaf_hdr_from_disk = xfs_dir2_leaf_hdr_from_disk,
  651. .leaf_max_ents = xfs_dir2_max_leaf_ents,
  652. .leaf_ents_p = xfs_dir2_leaf_ents_p,
  653. .node_hdr_size = sizeof(struct xfs_da_node_hdr),
  654. .node_hdr_to_disk = xfs_da2_node_hdr_to_disk,
  655. .node_hdr_from_disk = xfs_da2_node_hdr_from_disk,
  656. .node_tree_p = xfs_da2_node_tree_p,
  657. .free_hdr_size = sizeof(struct xfs_dir2_free_hdr),
  658. .free_hdr_to_disk = xfs_dir2_free_hdr_to_disk,
  659. .free_hdr_from_disk = xfs_dir2_free_hdr_from_disk,
  660. .free_max_bests = xfs_dir2_free_max_bests,
  661. .free_bests_p = xfs_dir2_free_bests_p,
  662. .db_to_fdb = xfs_dir2_db_to_fdb,
  663. .db_to_fdindex = xfs_dir2_db_to_fdindex,
  664. };
  665. static const struct xfs_dir_ops xfs_dir2_ftype_ops = {
  666. .sf_entsize = xfs_dir3_sf_entsize,
  667. .sf_nextentry = xfs_dir3_sf_nextentry,
  668. .sf_get_ftype = xfs_dir3_sfe_get_ftype,
  669. .sf_put_ftype = xfs_dir3_sfe_put_ftype,
  670. .sf_get_ino = xfs_dir3_sfe_get_ino,
  671. .sf_put_ino = xfs_dir3_sfe_put_ino,
  672. .sf_get_parent_ino = xfs_dir2_sf_get_parent_ino,
  673. .sf_put_parent_ino = xfs_dir2_sf_put_parent_ino,
  674. .data_entsize = xfs_dir3_data_entsize,
  675. .data_get_ftype = xfs_dir3_data_get_ftype,
  676. .data_put_ftype = xfs_dir3_data_put_ftype,
  677. .data_entry_tag_p = xfs_dir3_data_entry_tag_p,
  678. .data_bestfree_p = xfs_dir2_data_bestfree_p,
  679. .data_dot_offset = sizeof(struct xfs_dir2_data_hdr),
  680. .data_dotdot_offset = sizeof(struct xfs_dir2_data_hdr) +
  681. XFS_DIR3_DATA_ENTSIZE(1),
  682. .data_first_offset = sizeof(struct xfs_dir2_data_hdr) +
  683. XFS_DIR3_DATA_ENTSIZE(1) +
  684. XFS_DIR3_DATA_ENTSIZE(2),
  685. .data_entry_offset = sizeof(struct xfs_dir2_data_hdr),
  686. .data_dot_entry_p = xfs_dir2_data_dot_entry_p,
  687. .data_dotdot_entry_p = xfs_dir2_ftype_data_dotdot_entry_p,
  688. .data_first_entry_p = xfs_dir2_ftype_data_first_entry_p,
  689. .data_entry_p = xfs_dir2_data_entry_p,
  690. .data_unused_p = xfs_dir2_data_unused_p,
  691. .leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr),
  692. .leaf_hdr_to_disk = xfs_dir2_leaf_hdr_to_disk,
  693. .leaf_hdr_from_disk = xfs_dir2_leaf_hdr_from_disk,
  694. .leaf_max_ents = xfs_dir2_max_leaf_ents,
  695. .leaf_ents_p = xfs_dir2_leaf_ents_p,
  696. .node_hdr_size = sizeof(struct xfs_da_node_hdr),
  697. .node_hdr_to_disk = xfs_da2_node_hdr_to_disk,
  698. .node_hdr_from_disk = xfs_da2_node_hdr_from_disk,
  699. .node_tree_p = xfs_da2_node_tree_p,
  700. .free_hdr_size = sizeof(struct xfs_dir2_free_hdr),
  701. .free_hdr_to_disk = xfs_dir2_free_hdr_to_disk,
  702. .free_hdr_from_disk = xfs_dir2_free_hdr_from_disk,
  703. .free_max_bests = xfs_dir2_free_max_bests,
  704. .free_bests_p = xfs_dir2_free_bests_p,
  705. .db_to_fdb = xfs_dir2_db_to_fdb,
  706. .db_to_fdindex = xfs_dir2_db_to_fdindex,
  707. };
  708. static const struct xfs_dir_ops xfs_dir3_ops = {
  709. .sf_entsize = xfs_dir3_sf_entsize,
  710. .sf_nextentry = xfs_dir3_sf_nextentry,
  711. .sf_get_ftype = xfs_dir3_sfe_get_ftype,
  712. .sf_put_ftype = xfs_dir3_sfe_put_ftype,
  713. .sf_get_ino = xfs_dir3_sfe_get_ino,
  714. .sf_put_ino = xfs_dir3_sfe_put_ino,
  715. .sf_get_parent_ino = xfs_dir2_sf_get_parent_ino,
  716. .sf_put_parent_ino = xfs_dir2_sf_put_parent_ino,
  717. .data_entsize = xfs_dir3_data_entsize,
  718. .data_get_ftype = xfs_dir3_data_get_ftype,
  719. .data_put_ftype = xfs_dir3_data_put_ftype,
  720. .data_entry_tag_p = xfs_dir3_data_entry_tag_p,
  721. .data_bestfree_p = xfs_dir3_data_bestfree_p,
  722. .data_dot_offset = sizeof(struct xfs_dir3_data_hdr),
  723. .data_dotdot_offset = sizeof(struct xfs_dir3_data_hdr) +
  724. XFS_DIR3_DATA_ENTSIZE(1),
  725. .data_first_offset = sizeof(struct xfs_dir3_data_hdr) +
  726. XFS_DIR3_DATA_ENTSIZE(1) +
  727. XFS_DIR3_DATA_ENTSIZE(2),
  728. .data_entry_offset = sizeof(struct xfs_dir3_data_hdr),
  729. .data_dot_entry_p = xfs_dir3_data_dot_entry_p,
  730. .data_dotdot_entry_p = xfs_dir3_data_dotdot_entry_p,
  731. .data_first_entry_p = xfs_dir3_data_first_entry_p,
  732. .data_entry_p = xfs_dir3_data_entry_p,
  733. .data_unused_p = xfs_dir3_data_unused_p,
  734. .leaf_hdr_size = sizeof(struct xfs_dir3_leaf_hdr),
  735. .leaf_hdr_to_disk = xfs_dir3_leaf_hdr_to_disk,
  736. .leaf_hdr_from_disk = xfs_dir3_leaf_hdr_from_disk,
  737. .leaf_max_ents = xfs_dir3_max_leaf_ents,
  738. .leaf_ents_p = xfs_dir3_leaf_ents_p,
  739. .node_hdr_size = sizeof(struct xfs_da3_node_hdr),
  740. .node_hdr_to_disk = xfs_da3_node_hdr_to_disk,
  741. .node_hdr_from_disk = xfs_da3_node_hdr_from_disk,
  742. .node_tree_p = xfs_da3_node_tree_p,
  743. .free_hdr_size = sizeof(struct xfs_dir3_free_hdr),
  744. .free_hdr_to_disk = xfs_dir3_free_hdr_to_disk,
  745. .free_hdr_from_disk = xfs_dir3_free_hdr_from_disk,
  746. .free_max_bests = xfs_dir3_free_max_bests,
  747. .free_bests_p = xfs_dir3_free_bests_p,
  748. .db_to_fdb = xfs_dir3_db_to_fdb,
  749. .db_to_fdindex = xfs_dir3_db_to_fdindex,
  750. };
  751. static const struct xfs_dir_ops xfs_dir2_nondir_ops = {
  752. .node_hdr_size = sizeof(struct xfs_da_node_hdr),
  753. .node_hdr_to_disk = xfs_da2_node_hdr_to_disk,
  754. .node_hdr_from_disk = xfs_da2_node_hdr_from_disk,
  755. .node_tree_p = xfs_da2_node_tree_p,
  756. };
  757. static const struct xfs_dir_ops xfs_dir3_nondir_ops = {
  758. .node_hdr_size = sizeof(struct xfs_da3_node_hdr),
  759. .node_hdr_to_disk = xfs_da3_node_hdr_to_disk,
  760. .node_hdr_from_disk = xfs_da3_node_hdr_from_disk,
  761. .node_tree_p = xfs_da3_node_tree_p,
  762. };
  763. /*
  764. * Return the ops structure according to the current config. If we are passed
  765. * an inode, then that overrides the default config we use which is based on
  766. * feature bits.
  767. */
  768. const struct xfs_dir_ops *
  769. xfs_dir_get_ops(
  770. struct xfs_mount *mp,
  771. struct xfs_inode *dp)
  772. {
  773. if (dp)
  774. return dp->d_ops;
  775. if (mp->m_dir_inode_ops)
  776. return mp->m_dir_inode_ops;
  777. if (xfs_sb_version_hascrc(&mp->m_sb))
  778. return &xfs_dir3_ops;
  779. if (xfs_sb_version_hasftype(&mp->m_sb))
  780. return &xfs_dir2_ftype_ops;
  781. return &xfs_dir2_ops;
  782. }
  783. const struct xfs_dir_ops *
  784. xfs_nondir_get_ops(
  785. struct xfs_mount *mp,
  786. struct xfs_inode *dp)
  787. {
  788. if (dp)
  789. return dp->d_ops;
  790. if (mp->m_nondir_inode_ops)
  791. return mp->m_nondir_inode_ops;
  792. if (xfs_sb_version_hascrc(&mp->m_sb))
  793. return &xfs_dir3_nondir_ops;
  794. return &xfs_dir2_nondir_ops;
  795. }