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