xfs_dir2_block.c 37 KB

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