xfs_dir2_block.c 35 KB

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