xfs_dir2_block.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_bmap.h"
  17. #include "xfs_buf_item.h"
  18. #include "xfs_dir2.h"
  19. #include "xfs_dir2_priv.h"
  20. #include "xfs_error.h"
  21. #include "xfs_trace.h"
  22. #include "xfs_log.h"
  23. /*
  24. * Local function prototypes.
  25. */
  26. static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp,
  27. int first, int last);
  28. static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp);
  29. static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp,
  30. int *entno);
  31. static int xfs_dir2_block_sort(const void *a, const void *b);
  32. static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
  33. /*
  34. * One-time startup routine called from xfs_init().
  35. */
  36. void
  37. xfs_dir_startup(void)
  38. {
  39. xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
  40. xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
  41. }
  42. static xfs_failaddr_t
  43. xfs_dir3_block_verify(
  44. struct xfs_buf *bp)
  45. {
  46. struct xfs_mount *mp = bp->b_mount;
  47. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  48. if (!xfs_verify_magic(bp, hdr3->magic))
  49. return __this_address;
  50. if (xfs_has_crc(mp)) {
  51. if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
  52. return __this_address;
  53. if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
  54. return __this_address;
  55. if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
  56. return __this_address;
  57. }
  58. return __xfs_dir3_data_check(NULL, bp);
  59. }
  60. static void
  61. xfs_dir3_block_read_verify(
  62. struct xfs_buf *bp)
  63. {
  64. struct xfs_mount *mp = bp->b_mount;
  65. xfs_failaddr_t fa;
  66. if (xfs_has_crc(mp) &&
  67. !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
  68. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  69. else {
  70. fa = xfs_dir3_block_verify(bp);
  71. if (fa)
  72. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  73. }
  74. }
  75. static void
  76. xfs_dir3_block_write_verify(
  77. struct xfs_buf *bp)
  78. {
  79. struct xfs_mount *mp = bp->b_mount;
  80. struct xfs_buf_log_item *bip = bp->b_log_item;
  81. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  82. xfs_failaddr_t fa;
  83. fa = xfs_dir3_block_verify(bp);
  84. if (fa) {
  85. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  86. return;
  87. }
  88. if (!xfs_has_crc(mp))
  89. return;
  90. if (bip)
  91. hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
  92. xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
  93. }
  94. const struct xfs_buf_ops xfs_dir3_block_buf_ops = {
  95. .name = "xfs_dir3_block",
  96. .magic = { cpu_to_be32(XFS_DIR2_BLOCK_MAGIC),
  97. cpu_to_be32(XFS_DIR3_BLOCK_MAGIC) },
  98. .verify_read = xfs_dir3_block_read_verify,
  99. .verify_write = xfs_dir3_block_write_verify,
  100. .verify_struct = xfs_dir3_block_verify,
  101. };
  102. static xfs_failaddr_t
  103. xfs_dir3_block_header_check(
  104. struct xfs_inode *dp,
  105. struct xfs_buf *bp)
  106. {
  107. struct xfs_mount *mp = dp->i_mount;
  108. if (xfs_has_crc(mp)) {
  109. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  110. if (be64_to_cpu(hdr3->owner) != dp->i_ino)
  111. return __this_address;
  112. }
  113. return NULL;
  114. }
  115. int
  116. xfs_dir3_block_read(
  117. struct xfs_trans *tp,
  118. struct xfs_inode *dp,
  119. struct xfs_buf **bpp)
  120. {
  121. struct xfs_mount *mp = dp->i_mount;
  122. xfs_failaddr_t fa;
  123. int err;
  124. err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, 0, bpp,
  125. XFS_DATA_FORK, &xfs_dir3_block_buf_ops);
  126. if (err || !*bpp)
  127. return err;
  128. /* Check things that we can't do in the verifier. */
  129. fa = xfs_dir3_block_header_check(dp, *bpp);
  130. if (fa) {
  131. __xfs_buf_mark_corrupt(*bpp, fa);
  132. xfs_trans_brelse(tp, *bpp);
  133. *bpp = NULL;
  134. return -EFSCORRUPTED;
  135. }
  136. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF);
  137. return err;
  138. }
  139. static void
  140. xfs_dir3_block_init(
  141. struct xfs_mount *mp,
  142. struct xfs_trans *tp,
  143. struct xfs_buf *bp,
  144. struct xfs_inode *dp)
  145. {
  146. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  147. bp->b_ops = &xfs_dir3_block_buf_ops;
  148. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF);
  149. if (xfs_has_crc(mp)) {
  150. memset(hdr3, 0, sizeof(*hdr3));
  151. hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
  152. hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp));
  153. hdr3->owner = cpu_to_be64(dp->i_ino);
  154. uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
  155. return;
  156. }
  157. hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
  158. }
  159. static void
  160. xfs_dir2_block_need_space(
  161. struct xfs_inode *dp,
  162. struct xfs_dir2_data_hdr *hdr,
  163. struct xfs_dir2_block_tail *btp,
  164. struct xfs_dir2_leaf_entry *blp,
  165. __be16 **tagpp,
  166. struct xfs_dir2_data_unused **dupp,
  167. struct xfs_dir2_data_unused **enddupp,
  168. int *compact,
  169. int len)
  170. {
  171. struct xfs_dir2_data_free *bf;
  172. __be16 *tagp = NULL;
  173. struct xfs_dir2_data_unused *dup = NULL;
  174. struct xfs_dir2_data_unused *enddup = NULL;
  175. *compact = 0;
  176. bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
  177. /*
  178. * If there are stale entries we'll use one for the leaf.
  179. */
  180. if (btp->stale) {
  181. if (be16_to_cpu(bf[0].length) >= len) {
  182. /*
  183. * The biggest entry enough to avoid compaction.
  184. */
  185. dup = (xfs_dir2_data_unused_t *)
  186. ((char *)hdr + be16_to_cpu(bf[0].offset));
  187. goto out;
  188. }
  189. /*
  190. * Will need to compact to make this work.
  191. * Tag just before the first leaf entry.
  192. */
  193. *compact = 1;
  194. tagp = (__be16 *)blp - 1;
  195. /* Data object just before the first leaf entry. */
  196. dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  197. /*
  198. * If it's not free then the data will go where the
  199. * leaf data starts now, if it works at all.
  200. */
  201. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  202. if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
  203. (uint)sizeof(*blp) < len)
  204. dup = NULL;
  205. } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
  206. dup = NULL;
  207. else
  208. dup = (xfs_dir2_data_unused_t *)blp;
  209. goto out;
  210. }
  211. /*
  212. * no stale entries, so just use free space.
  213. * Tag just before the first leaf entry.
  214. */
  215. tagp = (__be16 *)blp - 1;
  216. /* Data object just before the first leaf entry. */
  217. enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  218. /*
  219. * If it's not free then can't do this add without cleaning up:
  220. * the space before the first leaf entry needs to be free so it
  221. * can be expanded to hold the pointer to the new entry.
  222. */
  223. if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  224. /*
  225. * Check out the biggest freespace and see if it's the same one.
  226. */
  227. dup = (xfs_dir2_data_unused_t *)
  228. ((char *)hdr + be16_to_cpu(bf[0].offset));
  229. if (dup != enddup) {
  230. /*
  231. * Not the same free entry, just check its length.
  232. */
  233. if (be16_to_cpu(dup->length) < len)
  234. dup = NULL;
  235. goto out;
  236. }
  237. /*
  238. * It is the biggest freespace, can it hold the leaf too?
  239. */
  240. if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
  241. /*
  242. * Yes, use the second-largest entry instead if it works.
  243. */
  244. if (be16_to_cpu(bf[1].length) >= len)
  245. dup = (xfs_dir2_data_unused_t *)
  246. ((char *)hdr + be16_to_cpu(bf[1].offset));
  247. else
  248. dup = NULL;
  249. }
  250. }
  251. out:
  252. *tagpp = tagp;
  253. *dupp = dup;
  254. *enddupp = enddup;
  255. }
  256. /*
  257. * compact the leaf entries.
  258. * Leave the highest-numbered stale entry stale.
  259. * XXX should be the one closest to mid but mid is not yet computed.
  260. */
  261. static void
  262. xfs_dir2_block_compact(
  263. struct xfs_da_args *args,
  264. struct xfs_buf *bp,
  265. struct xfs_dir2_data_hdr *hdr,
  266. struct xfs_dir2_block_tail *btp,
  267. struct xfs_dir2_leaf_entry *blp,
  268. int *needlog,
  269. int *lfloghigh,
  270. int *lfloglow)
  271. {
  272. int fromidx; /* source leaf index */
  273. int toidx; /* target leaf index */
  274. int needscan = 0;
  275. int highstale; /* high stale index */
  276. fromidx = toidx = be32_to_cpu(btp->count) - 1;
  277. highstale = *lfloghigh = -1;
  278. for (; fromidx >= 0; fromidx--) {
  279. if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
  280. if (highstale == -1)
  281. highstale = toidx;
  282. else {
  283. if (*lfloghigh == -1)
  284. *lfloghigh = toidx;
  285. continue;
  286. }
  287. }
  288. if (fromidx < toidx)
  289. blp[toidx] = blp[fromidx];
  290. toidx--;
  291. }
  292. *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
  293. *lfloghigh -= be32_to_cpu(btp->stale) - 1;
  294. be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
  295. xfs_dir2_data_make_free(args, bp,
  296. (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
  297. (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
  298. needlog, &needscan);
  299. btp->stale = cpu_to_be32(1);
  300. /*
  301. * If we now need to rebuild the bestfree map, do so.
  302. * This needs to happen before the next call to use_free.
  303. */
  304. if (needscan)
  305. xfs_dir2_data_freescan(args->dp->i_mount, hdr, needlog);
  306. }
  307. /*
  308. * Add an entry to a block directory.
  309. */
  310. int /* error */
  311. xfs_dir2_block_addname(
  312. xfs_da_args_t *args) /* directory op arguments */
  313. {
  314. xfs_dir2_data_hdr_t *hdr; /* block header */
  315. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  316. struct xfs_buf *bp; /* buffer for block */
  317. xfs_dir2_block_tail_t *btp; /* block tail */
  318. int compact; /* need to compact leaf ents */
  319. xfs_dir2_data_entry_t *dep; /* block data entry */
  320. xfs_inode_t *dp; /* directory inode */
  321. xfs_dir2_data_unused_t *dup; /* block unused entry */
  322. int error; /* error return value */
  323. xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
  324. xfs_dahash_t hash; /* hash value of found entry */
  325. int high; /* high index for binary srch */
  326. int highstale; /* high stale index */
  327. int lfloghigh=0; /* last final leaf to log */
  328. int lfloglow=0; /* first final leaf to log */
  329. int len; /* length of the new entry */
  330. int low; /* low index for binary srch */
  331. int lowstale; /* low stale index */
  332. int mid=0; /* midpoint for binary srch */
  333. int needlog; /* need to log header */
  334. int needscan; /* need to rescan freespace */
  335. __be16 *tagp; /* pointer to tag value */
  336. xfs_trans_t *tp; /* transaction structure */
  337. trace_xfs_dir2_block_addname(args);
  338. dp = args->dp;
  339. tp = args->trans;
  340. /* Read the (one and only) directory block into bp. */
  341. error = xfs_dir3_block_read(tp, dp, &bp);
  342. if (error)
  343. return error;
  344. len = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
  345. /*
  346. * Set up pointers to parts of the block.
  347. */
  348. hdr = bp->b_addr;
  349. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  350. blp = xfs_dir2_block_leaf_p(btp);
  351. /*
  352. * Find out if we can reuse stale entries or whether we need extra
  353. * space for entry and new leaf.
  354. */
  355. xfs_dir2_block_need_space(dp, hdr, btp, blp, &tagp, &dup,
  356. &enddup, &compact, len);
  357. /*
  358. * Done everything we need for a space check now.
  359. */
  360. if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
  361. xfs_trans_brelse(tp, bp);
  362. if (!dup)
  363. return -ENOSPC;
  364. return 0;
  365. }
  366. /*
  367. * If we don't have space for the new entry & leaf ...
  368. */
  369. if (!dup) {
  370. /* Don't have a space reservation: return no-space. */
  371. if (args->total == 0)
  372. return -ENOSPC;
  373. /*
  374. * Convert to the next larger format.
  375. * Then add the new entry in that format.
  376. */
  377. error = xfs_dir2_block_to_leaf(args, bp);
  378. if (error)
  379. return error;
  380. return xfs_dir2_leaf_addname(args);
  381. }
  382. needlog = needscan = 0;
  383. /*
  384. * If need to compact the leaf entries, do it now.
  385. */
  386. if (compact) {
  387. xfs_dir2_block_compact(args, bp, hdr, btp, blp, &needlog,
  388. &lfloghigh, &lfloglow);
  389. /* recalculate blp post-compaction */
  390. blp = xfs_dir2_block_leaf_p(btp);
  391. } else if (btp->stale) {
  392. /*
  393. * Set leaf logging boundaries to impossible state.
  394. * For the no-stale case they're set explicitly.
  395. */
  396. lfloglow = be32_to_cpu(btp->count);
  397. lfloghigh = -1;
  398. }
  399. /*
  400. * Find the slot that's first lower than our hash value, -1 if none.
  401. */
  402. for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
  403. mid = (low + high) >> 1;
  404. if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
  405. break;
  406. if (hash < args->hashval)
  407. low = mid + 1;
  408. else
  409. high = mid - 1;
  410. }
  411. while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
  412. mid--;
  413. }
  414. /*
  415. * No stale entries, will use enddup space to hold new leaf.
  416. */
  417. if (!btp->stale) {
  418. xfs_dir2_data_aoff_t aoff;
  419. /*
  420. * Mark the space needed for the new leaf entry, now in use.
  421. */
  422. aoff = (xfs_dir2_data_aoff_t)((char *)enddup - (char *)hdr +
  423. be16_to_cpu(enddup->length) - sizeof(*blp));
  424. error = xfs_dir2_data_use_free(args, bp, enddup, aoff,
  425. (xfs_dir2_data_aoff_t)sizeof(*blp), &needlog,
  426. &needscan);
  427. if (error)
  428. return error;
  429. /*
  430. * Update the tail (entry count).
  431. */
  432. be32_add_cpu(&btp->count, 1);
  433. /*
  434. * If we now need to rebuild the bestfree map, do so.
  435. * This needs to happen before the next call to use_free.
  436. */
  437. if (needscan) {
  438. xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
  439. needscan = 0;
  440. }
  441. /*
  442. * Adjust pointer to the first leaf entry, we're about to move
  443. * the table up one to open up space for the new leaf entry.
  444. * Then adjust our index to match.
  445. */
  446. blp--;
  447. mid++;
  448. if (mid)
  449. memmove(blp, &blp[1], mid * sizeof(*blp));
  450. lfloglow = 0;
  451. lfloghigh = mid;
  452. }
  453. /*
  454. * Use a stale leaf for our new entry.
  455. */
  456. else {
  457. for (lowstale = mid;
  458. lowstale >= 0 &&
  459. blp[lowstale].address !=
  460. cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  461. lowstale--)
  462. continue;
  463. for (highstale = mid + 1;
  464. highstale < be32_to_cpu(btp->count) &&
  465. blp[highstale].address !=
  466. cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
  467. (lowstale < 0 || mid - lowstale > highstale - mid);
  468. highstale++)
  469. continue;
  470. /*
  471. * Move entries toward the low-numbered stale entry.
  472. */
  473. if (lowstale >= 0 &&
  474. (highstale == be32_to_cpu(btp->count) ||
  475. mid - lowstale <= highstale - mid)) {
  476. if (mid - lowstale)
  477. memmove(&blp[lowstale], &blp[lowstale + 1],
  478. (mid - lowstale) * sizeof(*blp));
  479. lfloglow = min(lowstale, lfloglow);
  480. lfloghigh = max(mid, lfloghigh);
  481. }
  482. /*
  483. * Move entries toward the high-numbered stale entry.
  484. */
  485. else {
  486. ASSERT(highstale < be32_to_cpu(btp->count));
  487. mid++;
  488. if (highstale - mid)
  489. memmove(&blp[mid + 1], &blp[mid],
  490. (highstale - mid) * sizeof(*blp));
  491. lfloglow = min(mid, lfloglow);
  492. lfloghigh = max(highstale, lfloghigh);
  493. }
  494. be32_add_cpu(&btp->stale, -1);
  495. }
  496. /*
  497. * Point to the new data entry.
  498. */
  499. dep = (xfs_dir2_data_entry_t *)dup;
  500. /*
  501. * Fill in the leaf entry.
  502. */
  503. blp[mid].hashval = cpu_to_be32(args->hashval);
  504. blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
  505. (char *)dep - (char *)hdr));
  506. xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
  507. /*
  508. * Mark space for the data entry used.
  509. */
  510. error = xfs_dir2_data_use_free(args, bp, dup,
  511. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
  512. (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
  513. if (error)
  514. return error;
  515. /*
  516. * Create the new data entry.
  517. */
  518. dep->inumber = cpu_to_be64(args->inumber);
  519. dep->namelen = args->namelen;
  520. memcpy(dep->name, args->name, args->namelen);
  521. xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
  522. tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
  523. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  524. /*
  525. * Clean up the bestfree array and log the header, tail, and entry.
  526. */
  527. if (needscan)
  528. xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
  529. if (needlog)
  530. xfs_dir2_data_log_header(args, bp);
  531. xfs_dir2_block_log_tail(tp, bp);
  532. xfs_dir2_data_log_entry(args, bp, dep);
  533. xfs_dir3_data_check(dp, bp);
  534. return 0;
  535. }
  536. /*
  537. * Log leaf entries from the block.
  538. */
  539. static void
  540. xfs_dir2_block_log_leaf(
  541. xfs_trans_t *tp, /* transaction structure */
  542. struct xfs_buf *bp, /* block buffer */
  543. int first, /* index of first logged leaf */
  544. int last) /* index of last logged leaf */
  545. {
  546. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  547. xfs_dir2_leaf_entry_t *blp;
  548. xfs_dir2_block_tail_t *btp;
  549. btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
  550. blp = xfs_dir2_block_leaf_p(btp);
  551. xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
  552. (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
  553. }
  554. /*
  555. * Log the block tail.
  556. */
  557. static void
  558. xfs_dir2_block_log_tail(
  559. xfs_trans_t *tp, /* transaction structure */
  560. struct xfs_buf *bp) /* block buffer */
  561. {
  562. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  563. xfs_dir2_block_tail_t *btp;
  564. btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
  565. xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
  566. (uint)((char *)(btp + 1) - (char *)hdr - 1));
  567. }
  568. /*
  569. * Look up an entry in the block. This is the external routine,
  570. * xfs_dir2_block_lookup_int does the real work.
  571. */
  572. int /* error */
  573. xfs_dir2_block_lookup(
  574. xfs_da_args_t *args) /* dir lookup arguments */
  575. {
  576. xfs_dir2_data_hdr_t *hdr; /* block header */
  577. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  578. struct xfs_buf *bp; /* block buffer */
  579. xfs_dir2_block_tail_t *btp; /* block tail */
  580. xfs_dir2_data_entry_t *dep; /* block data entry */
  581. xfs_inode_t *dp; /* incore inode */
  582. int ent; /* entry index */
  583. int error; /* error return value */
  584. trace_xfs_dir2_block_lookup(args);
  585. /*
  586. * Get the buffer, look up the entry.
  587. * If not found (ENOENT) then return, have no buffer.
  588. */
  589. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
  590. return error;
  591. dp = args->dp;
  592. hdr = bp->b_addr;
  593. xfs_dir3_data_check(dp, bp);
  594. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  595. blp = xfs_dir2_block_leaf_p(btp);
  596. /*
  597. * Get the offset from the leaf entry, to point to the data.
  598. */
  599. dep = (xfs_dir2_data_entry_t *)((char *)hdr +
  600. xfs_dir2_dataptr_to_off(args->geo,
  601. be32_to_cpu(blp[ent].address)));
  602. /*
  603. * Fill in inode number, CI name if appropriate, release the block.
  604. */
  605. args->inumber = be64_to_cpu(dep->inumber);
  606. args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
  607. error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  608. xfs_trans_brelse(args->trans, bp);
  609. return error;
  610. }
  611. /*
  612. * Internal block lookup routine.
  613. */
  614. static int /* error */
  615. xfs_dir2_block_lookup_int(
  616. xfs_da_args_t *args, /* dir lookup arguments */
  617. struct xfs_buf **bpp, /* returned block buffer */
  618. int *entno) /* returned entry number */
  619. {
  620. xfs_dir2_dataptr_t addr; /* data entry address */
  621. xfs_dir2_data_hdr_t *hdr; /* block header */
  622. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  623. struct xfs_buf *bp; /* block buffer */
  624. xfs_dir2_block_tail_t *btp; /* block tail */
  625. xfs_dir2_data_entry_t *dep; /* block data entry */
  626. xfs_inode_t *dp; /* incore inode */
  627. int error; /* error return value */
  628. xfs_dahash_t hash; /* found hash value */
  629. int high; /* binary search high index */
  630. int low; /* binary search low index */
  631. int mid; /* binary search current idx */
  632. xfs_trans_t *tp; /* transaction pointer */
  633. enum xfs_dacmp cmp; /* comparison result */
  634. dp = args->dp;
  635. tp = args->trans;
  636. error = xfs_dir3_block_read(tp, dp, &bp);
  637. if (error)
  638. return error;
  639. hdr = bp->b_addr;
  640. xfs_dir3_data_check(dp, bp);
  641. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  642. blp = xfs_dir2_block_leaf_p(btp);
  643. /*
  644. * Loop doing a binary search for our hash value.
  645. * Find our entry, ENOENT if it's not there.
  646. */
  647. for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
  648. ASSERT(low <= high);
  649. mid = (low + high) >> 1;
  650. if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
  651. break;
  652. if (hash < args->hashval)
  653. low = mid + 1;
  654. else
  655. high = mid - 1;
  656. if (low > high) {
  657. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  658. xfs_trans_brelse(tp, bp);
  659. return -ENOENT;
  660. }
  661. }
  662. /*
  663. * Back up to the first one with the right hash value.
  664. */
  665. while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
  666. mid--;
  667. }
  668. /*
  669. * Now loop forward through all the entries with the
  670. * right hash value looking for our name.
  671. */
  672. do {
  673. if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
  674. continue;
  675. /*
  676. * Get pointer to the entry from the leaf.
  677. */
  678. dep = (xfs_dir2_data_entry_t *)
  679. ((char *)hdr + xfs_dir2_dataptr_to_off(args->geo, addr));
  680. /*
  681. * Compare name and if it's an exact match, return the index
  682. * and buffer. If it's the first case-insensitive match, store
  683. * the index and buffer and continue looking for an exact match.
  684. */
  685. cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
  686. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  687. args->cmpresult = cmp;
  688. *bpp = bp;
  689. *entno = mid;
  690. if (cmp == XFS_CMP_EXACT)
  691. return 0;
  692. }
  693. } while (++mid < be32_to_cpu(btp->count) &&
  694. be32_to_cpu(blp[mid].hashval) == hash);
  695. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  696. /*
  697. * Here, we can only be doing a lookup (not a rename or replace).
  698. * If a case-insensitive match was found earlier, return success.
  699. */
  700. if (args->cmpresult == XFS_CMP_CASE)
  701. return 0;
  702. /*
  703. * No match, release the buffer and return ENOENT.
  704. */
  705. xfs_trans_brelse(tp, bp);
  706. return -ENOENT;
  707. }
  708. /*
  709. * Remove an entry from a block format directory.
  710. * If that makes the block small enough to fit in shortform, transform it.
  711. */
  712. int /* error */
  713. xfs_dir2_block_removename(
  714. xfs_da_args_t *args) /* directory operation args */
  715. {
  716. xfs_dir2_data_hdr_t *hdr; /* block header */
  717. xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
  718. struct xfs_buf *bp; /* block buffer */
  719. xfs_dir2_block_tail_t *btp; /* block tail */
  720. xfs_dir2_data_entry_t *dep; /* block data entry */
  721. xfs_inode_t *dp; /* incore inode */
  722. int ent; /* block leaf entry index */
  723. int error; /* error return value */
  724. int needlog; /* need to log block header */
  725. int needscan; /* need to fixup bestfree */
  726. xfs_dir2_sf_hdr_t sfh; /* shortform header */
  727. int size; /* shortform size */
  728. xfs_trans_t *tp; /* transaction pointer */
  729. trace_xfs_dir2_block_removename(args);
  730. /*
  731. * Look up the entry in the block. Gets the buffer and entry index.
  732. * It will always be there, the vnodeops level does a lookup first.
  733. */
  734. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
  735. return error;
  736. }
  737. dp = args->dp;
  738. tp = args->trans;
  739. hdr = bp->b_addr;
  740. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  741. blp = xfs_dir2_block_leaf_p(btp);
  742. /*
  743. * Point to the data entry using the leaf entry.
  744. */
  745. dep = (xfs_dir2_data_entry_t *)((char *)hdr +
  746. xfs_dir2_dataptr_to_off(args->geo,
  747. be32_to_cpu(blp[ent].address)));
  748. /*
  749. * Mark the data entry's space free.
  750. */
  751. needlog = needscan = 0;
  752. xfs_dir2_data_make_free(args, bp,
  753. (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
  754. xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
  755. &needscan);
  756. /*
  757. * Fix up the block tail.
  758. */
  759. be32_add_cpu(&btp->stale, 1);
  760. xfs_dir2_block_log_tail(tp, bp);
  761. /*
  762. * Remove the leaf entry by marking it stale.
  763. */
  764. blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  765. xfs_dir2_block_log_leaf(tp, bp, ent, ent);
  766. /*
  767. * Fix up bestfree, log the header if necessary.
  768. */
  769. if (needscan)
  770. xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
  771. if (needlog)
  772. xfs_dir2_data_log_header(args, bp);
  773. xfs_dir3_data_check(dp, bp);
  774. /*
  775. * See if the size as a shortform is good enough.
  776. */
  777. size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
  778. if (size > xfs_inode_data_fork_size(dp))
  779. return 0;
  780. /*
  781. * If it works, do the conversion.
  782. */
  783. return xfs_dir2_block_to_sf(args, bp, size, &sfh);
  784. }
  785. /*
  786. * Replace an entry in a V2 block directory.
  787. * Change the inode number to the new value.
  788. */
  789. int /* error */
  790. xfs_dir2_block_replace(
  791. xfs_da_args_t *args) /* directory operation args */
  792. {
  793. xfs_dir2_data_hdr_t *hdr; /* block header */
  794. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  795. struct xfs_buf *bp; /* block buffer */
  796. xfs_dir2_block_tail_t *btp; /* block tail */
  797. xfs_dir2_data_entry_t *dep; /* block data entry */
  798. xfs_inode_t *dp; /* incore inode */
  799. int ent; /* leaf entry index */
  800. int error; /* error return value */
  801. trace_xfs_dir2_block_replace(args);
  802. /*
  803. * Lookup the entry in the directory. Get buffer and entry index.
  804. * This will always succeed since the caller has already done a lookup.
  805. */
  806. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
  807. return error;
  808. }
  809. dp = args->dp;
  810. hdr = bp->b_addr;
  811. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  812. blp = xfs_dir2_block_leaf_p(btp);
  813. /*
  814. * Point to the data entry we need to change.
  815. */
  816. dep = (xfs_dir2_data_entry_t *)((char *)hdr +
  817. xfs_dir2_dataptr_to_off(args->geo,
  818. be32_to_cpu(blp[ent].address)));
  819. ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
  820. /*
  821. * Change the inode number to the new value.
  822. */
  823. dep->inumber = cpu_to_be64(args->inumber);
  824. xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
  825. xfs_dir2_data_log_entry(args, bp, dep);
  826. xfs_dir3_data_check(dp, bp);
  827. return 0;
  828. }
  829. /*
  830. * Qsort comparison routine for the block leaf entries.
  831. */
  832. static int /* sort order */
  833. xfs_dir2_block_sort(
  834. const void *a, /* first leaf entry */
  835. const void *b) /* second leaf entry */
  836. {
  837. const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
  838. const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
  839. la = a;
  840. lb = b;
  841. return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
  842. (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
  843. }
  844. /*
  845. * Convert a V2 leaf directory to a V2 block directory if possible.
  846. */
  847. int /* error */
  848. xfs_dir2_leaf_to_block(
  849. xfs_da_args_t *args, /* operation arguments */
  850. struct xfs_buf *lbp, /* leaf buffer */
  851. struct xfs_buf *dbp) /* data buffer */
  852. {
  853. __be16 *bestsp; /* leaf bests table */
  854. xfs_dir2_data_hdr_t *hdr; /* block header */
  855. xfs_dir2_block_tail_t *btp; /* block tail */
  856. xfs_inode_t *dp; /* incore directory inode */
  857. xfs_dir2_data_unused_t *dup; /* unused data entry */
  858. int error; /* error return value */
  859. int from; /* leaf from index */
  860. xfs_dir2_leaf_t *leaf; /* leaf structure */
  861. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  862. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  863. xfs_mount_t *mp; /* file system mount point */
  864. int needlog; /* need to log data header */
  865. int needscan; /* need to scan for bestfree */
  866. xfs_dir2_sf_hdr_t sfh; /* shortform header */
  867. int size; /* bytes used */
  868. __be16 *tagp; /* end of entry (tag) */
  869. int to; /* block/leaf to index */
  870. xfs_trans_t *tp; /* transaction pointer */
  871. struct xfs_dir3_icleaf_hdr leafhdr;
  872. trace_xfs_dir2_leaf_to_block(args);
  873. dp = args->dp;
  874. tp = args->trans;
  875. mp = dp->i_mount;
  876. leaf = lbp->b_addr;
  877. xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
  878. ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
  879. ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
  880. leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
  881. /*
  882. * If there are data blocks other than the first one, take this
  883. * opportunity to remove trailing empty data blocks that may have
  884. * been left behind during no-space-reservation operations.
  885. * These will show up in the leaf bests table.
  886. */
  887. while (dp->i_disk_size > args->geo->blksize) {
  888. int hdrsz;
  889. hdrsz = args->geo->data_entry_offset;
  890. bestsp = xfs_dir2_leaf_bests_p(ltp);
  891. if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
  892. args->geo->blksize - hdrsz) {
  893. if ((error =
  894. xfs_dir2_leaf_trim_data(args, lbp,
  895. (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
  896. return error;
  897. } else
  898. return 0;
  899. }
  900. /*
  901. * Read the data block if we don't already have it, give up if it fails.
  902. */
  903. if (!dbp) {
  904. error = xfs_dir3_data_read(tp, dp, args->geo->datablk, 0, &dbp);
  905. if (error)
  906. return error;
  907. }
  908. hdr = dbp->b_addr;
  909. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  910. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
  911. /*
  912. * Size of the "leaf" area in the block.
  913. */
  914. size = (uint)sizeof(xfs_dir2_block_tail_t) +
  915. (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
  916. /*
  917. * Look at the last data entry.
  918. */
  919. tagp = (__be16 *)((char *)hdr + args->geo->blksize) - 1;
  920. dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  921. /*
  922. * If it's not free or is too short we can't do it.
  923. */
  924. if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
  925. be16_to_cpu(dup->length) < size)
  926. return 0;
  927. /*
  928. * Start converting it to block form.
  929. */
  930. xfs_dir3_block_init(mp, tp, dbp, dp);
  931. needlog = 1;
  932. needscan = 0;
  933. /*
  934. * Use up the space at the end of the block (blp/btp).
  935. */
  936. error = xfs_dir2_data_use_free(args, dbp, dup,
  937. args->geo->blksize - size, size, &needlog, &needscan);
  938. if (error)
  939. return error;
  940. /*
  941. * Initialize the block tail.
  942. */
  943. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  944. btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
  945. btp->stale = 0;
  946. xfs_dir2_block_log_tail(tp, dbp);
  947. /*
  948. * Initialize the block leaf area. We compact out stale entries.
  949. */
  950. lep = xfs_dir2_block_leaf_p(btp);
  951. for (from = to = 0; from < leafhdr.count; from++) {
  952. if (leafhdr.ents[from].address ==
  953. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  954. continue;
  955. lep[to++] = leafhdr.ents[from];
  956. }
  957. ASSERT(to == be32_to_cpu(btp->count));
  958. xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
  959. /*
  960. * Scan the bestfree if we need it and log the data block header.
  961. */
  962. if (needscan)
  963. xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
  964. if (needlog)
  965. xfs_dir2_data_log_header(args, dbp);
  966. /*
  967. * Pitch the old leaf block.
  968. */
  969. error = xfs_da_shrink_inode(args, args->geo->leafblk, lbp);
  970. if (error)
  971. return error;
  972. /*
  973. * Now see if the resulting block can be shrunken to shortform.
  974. */
  975. size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
  976. if (size > xfs_inode_data_fork_size(dp))
  977. return 0;
  978. return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
  979. }
  980. /*
  981. * Convert the shortform directory to block form.
  982. */
  983. int /* error */
  984. xfs_dir2_sf_to_block(
  985. struct xfs_da_args *args)
  986. {
  987. struct xfs_trans *tp = args->trans;
  988. struct xfs_inode *dp = args->dp;
  989. struct xfs_mount *mp = dp->i_mount;
  990. struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
  991. struct xfs_da_geometry *geo = args->geo;
  992. xfs_dir2_db_t blkno; /* dir-relative block # (0) */
  993. xfs_dir2_data_hdr_t *hdr; /* block header */
  994. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  995. struct xfs_buf *bp; /* block buffer */
  996. xfs_dir2_block_tail_t *btp; /* block tail pointer */
  997. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  998. int dummy; /* trash */
  999. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  1000. int endoffset; /* end of data objects */
  1001. int error; /* error return value */
  1002. int i; /* index */
  1003. int needlog; /* need to log block header */
  1004. int needscan; /* need to scan block freespc */
  1005. int newoffset; /* offset from current entry */
  1006. unsigned int offset = geo->data_entry_offset;
  1007. xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
  1008. xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
  1009. xfs_dir2_sf_hdr_t *sfp; /* shortform header */
  1010. __be16 *tagp; /* end of data entry */
  1011. struct xfs_name name;
  1012. trace_xfs_dir2_sf_to_block(args);
  1013. ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
  1014. ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
  1015. oldsfp = (xfs_dir2_sf_hdr_t *)ifp->if_u1.if_data;
  1016. ASSERT(ifp->if_bytes == dp->i_disk_size);
  1017. ASSERT(ifp->if_u1.if_data != NULL);
  1018. ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
  1019. ASSERT(dp->i_df.if_nextents == 0);
  1020. /*
  1021. * Copy the directory into a temporary buffer.
  1022. * Then pitch the incore inode data so we can make extents.
  1023. */
  1024. sfp = kmem_alloc(ifp->if_bytes, 0);
  1025. memcpy(sfp, oldsfp, ifp->if_bytes);
  1026. xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
  1027. xfs_bmap_local_to_extents_empty(tp, dp, XFS_DATA_FORK);
  1028. dp->i_disk_size = 0;
  1029. /*
  1030. * Add block 0 to the inode.
  1031. */
  1032. error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
  1033. if (error)
  1034. goto out_free;
  1035. /*
  1036. * Initialize the data block, then convert it to block format.
  1037. */
  1038. error = xfs_dir3_data_init(args, blkno, &bp);
  1039. if (error)
  1040. goto out_free;
  1041. xfs_dir3_block_init(mp, tp, bp, dp);
  1042. hdr = bp->b_addr;
  1043. /*
  1044. * Compute size of block "tail" area.
  1045. */
  1046. i = (uint)sizeof(*btp) +
  1047. (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
  1048. /*
  1049. * The whole thing is initialized to free by the init routine.
  1050. * Say we're using the leaf and tail area.
  1051. */
  1052. dup = bp->b_addr + offset;
  1053. needlog = needscan = 0;
  1054. error = xfs_dir2_data_use_free(args, bp, dup, args->geo->blksize - i,
  1055. i, &needlog, &needscan);
  1056. if (error)
  1057. goto out_free;
  1058. ASSERT(needscan == 0);
  1059. /*
  1060. * Fill in the tail.
  1061. */
  1062. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  1063. btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
  1064. btp->stale = 0;
  1065. blp = xfs_dir2_block_leaf_p(btp);
  1066. endoffset = (uint)((char *)blp - (char *)hdr);
  1067. /*
  1068. * Remove the freespace, we'll manage it.
  1069. */
  1070. error = xfs_dir2_data_use_free(args, bp, dup,
  1071. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
  1072. be16_to_cpu(dup->length), &needlog, &needscan);
  1073. if (error)
  1074. goto out_free;
  1075. /*
  1076. * Create entry for .
  1077. */
  1078. dep = bp->b_addr + offset;
  1079. dep->inumber = cpu_to_be64(dp->i_ino);
  1080. dep->namelen = 1;
  1081. dep->name[0] = '.';
  1082. xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
  1083. tagp = xfs_dir2_data_entry_tag_p(mp, dep);
  1084. *tagp = cpu_to_be16(offset);
  1085. xfs_dir2_data_log_entry(args, bp, dep);
  1086. blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
  1087. blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset));
  1088. offset += xfs_dir2_data_entsize(mp, dep->namelen);
  1089. /*
  1090. * Create entry for ..
  1091. */
  1092. dep = bp->b_addr + offset;
  1093. dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
  1094. dep->namelen = 2;
  1095. dep->name[0] = dep->name[1] = '.';
  1096. xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
  1097. tagp = xfs_dir2_data_entry_tag_p(mp, dep);
  1098. *tagp = cpu_to_be16(offset);
  1099. xfs_dir2_data_log_entry(args, bp, dep);
  1100. blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
  1101. blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset));
  1102. offset += xfs_dir2_data_entsize(mp, dep->namelen);
  1103. /*
  1104. * Loop over existing entries, stuff them in.
  1105. */
  1106. i = 0;
  1107. if (!sfp->count)
  1108. sfep = NULL;
  1109. else
  1110. sfep = xfs_dir2_sf_firstentry(sfp);
  1111. /*
  1112. * Need to preserve the existing offset values in the sf directory.
  1113. * Insert holes (unused entries) where necessary.
  1114. */
  1115. while (offset < endoffset) {
  1116. /*
  1117. * sfep is null when we reach the end of the list.
  1118. */
  1119. if (sfep == NULL)
  1120. newoffset = endoffset;
  1121. else
  1122. newoffset = xfs_dir2_sf_get_offset(sfep);
  1123. /*
  1124. * There should be a hole here, make one.
  1125. */
  1126. if (offset < newoffset) {
  1127. dup = bp->b_addr + offset;
  1128. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  1129. dup->length = cpu_to_be16(newoffset - offset);
  1130. *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(offset);
  1131. xfs_dir2_data_log_unused(args, bp, dup);
  1132. xfs_dir2_data_freeinsert(hdr,
  1133. xfs_dir2_data_bestfree_p(mp, hdr),
  1134. dup, &dummy);
  1135. offset += be16_to_cpu(dup->length);
  1136. continue;
  1137. }
  1138. /*
  1139. * Copy a real entry.
  1140. */
  1141. dep = bp->b_addr + newoffset;
  1142. dep->inumber = cpu_to_be64(xfs_dir2_sf_get_ino(mp, sfp, sfep));
  1143. dep->namelen = sfep->namelen;
  1144. xfs_dir2_data_put_ftype(mp, dep,
  1145. xfs_dir2_sf_get_ftype(mp, sfep));
  1146. memcpy(dep->name, sfep->name, dep->namelen);
  1147. tagp = xfs_dir2_data_entry_tag_p(mp, dep);
  1148. *tagp = cpu_to_be16(newoffset);
  1149. xfs_dir2_data_log_entry(args, bp, dep);
  1150. name.name = sfep->name;
  1151. name.len = sfep->namelen;
  1152. blp[2 + i].hashval = cpu_to_be32(xfs_dir2_hashname(mp, &name));
  1153. blp[2 + i].address =
  1154. cpu_to_be32(xfs_dir2_byte_to_dataptr(newoffset));
  1155. offset = (int)((char *)(tagp + 1) - (char *)hdr);
  1156. if (++i == sfp->count)
  1157. sfep = NULL;
  1158. else
  1159. sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
  1160. }
  1161. /* Done with the temporary buffer */
  1162. kmem_free(sfp);
  1163. /*
  1164. * Sort the leaf entries by hash value.
  1165. */
  1166. xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
  1167. /*
  1168. * Log the leaf entry area and tail.
  1169. * Already logged the header in data_init, ignore needlog.
  1170. */
  1171. ASSERT(needscan == 0);
  1172. xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
  1173. xfs_dir2_block_log_tail(tp, bp);
  1174. xfs_dir3_data_check(dp, bp);
  1175. return 0;
  1176. out_free:
  1177. kmem_free(sfp);
  1178. return error;
  1179. }