xfs_dir2_block.c 35 KB

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