xfs_dir2_block.c 35 KB

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