xfs_dir2_block.c 35 KB

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