xfs_dir2_block.c 35 KB

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