xfs_dir2_block.c 35 KB

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