xfs_dir2_block.c 35 KB

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