xfs_da_format.c 24 KB

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