xfs_dir2_block.c 35 KB

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