xfs_dir2_block.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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. void *dirent,
  529. xfs_off_t *offset,
  530. filldir_t filldir)
  531. {
  532. xfs_dir2_data_hdr_t *hdr; /* block header */
  533. struct xfs_buf *bp; /* buffer for block */
  534. xfs_dir2_block_tail_t *btp; /* block tail */
  535. xfs_dir2_data_entry_t *dep; /* block data entry */
  536. xfs_dir2_data_unused_t *dup; /* block unused entry */
  537. char *endptr; /* end of the data entries */
  538. int error; /* error return value */
  539. xfs_mount_t *mp; /* filesystem mount point */
  540. char *ptr; /* current data entry */
  541. int wantoff; /* starting block offset */
  542. xfs_off_t cook;
  543. mp = dp->i_mount;
  544. /*
  545. * If the block number in the offset is out of range, we're done.
  546. */
  547. if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
  548. return 0;
  549. error = xfs_dir3_block_read(NULL, dp, &bp);
  550. if (error)
  551. return error;
  552. /*
  553. * Extract the byte offset we start at from the seek pointer.
  554. * We'll skip entries before this.
  555. */
  556. wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
  557. hdr = bp->b_addr;
  558. xfs_dir3_data_check(dp, bp);
  559. /*
  560. * Set up values for the loop.
  561. */
  562. btp = xfs_dir2_block_tail_p(mp, hdr);
  563. ptr = (char *)xfs_dir3_data_entry_p(hdr);
  564. endptr = (char *)xfs_dir2_block_leaf_p(btp);
  565. /*
  566. * Loop over the data portion of the block.
  567. * Each object is a real entry (dep) or an unused one (dup).
  568. */
  569. while (ptr < endptr) {
  570. dup = (xfs_dir2_data_unused_t *)ptr;
  571. /*
  572. * Unused, skip it.
  573. */
  574. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  575. ptr += be16_to_cpu(dup->length);
  576. continue;
  577. }
  578. dep = (xfs_dir2_data_entry_t *)ptr;
  579. /*
  580. * Bump pointer for the next iteration.
  581. */
  582. ptr += xfs_dir2_data_entsize(dep->namelen);
  583. /*
  584. * The entry is before the desired starting point, skip it.
  585. */
  586. if ((char *)dep - (char *)hdr < wantoff)
  587. continue;
  588. cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  589. (char *)dep - (char *)hdr);
  590. /*
  591. * If it didn't fit, set the final offset to here & return.
  592. */
  593. if (filldir(dirent, (char *)dep->name, dep->namelen,
  594. cook & 0x7fffffff, be64_to_cpu(dep->inumber),
  595. DT_UNKNOWN)) {
  596. *offset = cook & 0x7fffffff;
  597. xfs_trans_brelse(NULL, bp);
  598. return 0;
  599. }
  600. }
  601. /*
  602. * Reached the end of the block.
  603. * Set the offset to a non-existent block 1 and return.
  604. */
  605. *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
  606. 0x7fffffff;
  607. xfs_trans_brelse(NULL, bp);
  608. return 0;
  609. }
  610. /*
  611. * Log leaf entries from the block.
  612. */
  613. static void
  614. xfs_dir2_block_log_leaf(
  615. xfs_trans_t *tp, /* transaction structure */
  616. struct xfs_buf *bp, /* block buffer */
  617. int first, /* index of first logged leaf */
  618. int last) /* index of last logged leaf */
  619. {
  620. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  621. xfs_dir2_leaf_entry_t *blp;
  622. xfs_dir2_block_tail_t *btp;
  623. btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
  624. blp = xfs_dir2_block_leaf_p(btp);
  625. xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
  626. (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
  627. }
  628. /*
  629. * Log the block tail.
  630. */
  631. static void
  632. xfs_dir2_block_log_tail(
  633. xfs_trans_t *tp, /* transaction structure */
  634. struct xfs_buf *bp) /* block buffer */
  635. {
  636. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  637. xfs_dir2_block_tail_t *btp;
  638. btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
  639. xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
  640. (uint)((char *)(btp + 1) - (char *)hdr - 1));
  641. }
  642. /*
  643. * Look up an entry in the block. This is the external routine,
  644. * xfs_dir2_block_lookup_int does the real work.
  645. */
  646. int /* error */
  647. xfs_dir2_block_lookup(
  648. xfs_da_args_t *args) /* dir lookup arguments */
  649. {
  650. xfs_dir2_data_hdr_t *hdr; /* block header */
  651. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  652. struct xfs_buf *bp; /* block buffer */
  653. xfs_dir2_block_tail_t *btp; /* block tail */
  654. xfs_dir2_data_entry_t *dep; /* block data entry */
  655. xfs_inode_t *dp; /* incore inode */
  656. int ent; /* entry index */
  657. int error; /* error return value */
  658. xfs_mount_t *mp; /* filesystem mount point */
  659. trace_xfs_dir2_block_lookup(args);
  660. /*
  661. * Get the buffer, look up the entry.
  662. * If not found (ENOENT) then return, have no buffer.
  663. */
  664. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
  665. return error;
  666. dp = args->dp;
  667. mp = dp->i_mount;
  668. hdr = bp->b_addr;
  669. xfs_dir3_data_check(dp, bp);
  670. btp = xfs_dir2_block_tail_p(mp, hdr);
  671. blp = xfs_dir2_block_leaf_p(btp);
  672. /*
  673. * Get the offset from the leaf entry, to point to the data.
  674. */
  675. dep = (xfs_dir2_data_entry_t *)((char *)hdr +
  676. xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
  677. /*
  678. * Fill in inode number, CI name if appropriate, release the block.
  679. */
  680. args->inumber = be64_to_cpu(dep->inumber);
  681. error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  682. xfs_trans_brelse(args->trans, bp);
  683. return XFS_ERROR(error);
  684. }
  685. /*
  686. * Internal block lookup routine.
  687. */
  688. static int /* error */
  689. xfs_dir2_block_lookup_int(
  690. xfs_da_args_t *args, /* dir lookup arguments */
  691. struct xfs_buf **bpp, /* returned block buffer */
  692. int *entno) /* returned entry number */
  693. {
  694. xfs_dir2_dataptr_t addr; /* data entry address */
  695. xfs_dir2_data_hdr_t *hdr; /* block header */
  696. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  697. struct xfs_buf *bp; /* block buffer */
  698. xfs_dir2_block_tail_t *btp; /* block tail */
  699. xfs_dir2_data_entry_t *dep; /* block data entry */
  700. xfs_inode_t *dp; /* incore inode */
  701. int error; /* error return value */
  702. xfs_dahash_t hash; /* found hash value */
  703. int high; /* binary search high index */
  704. int low; /* binary search low index */
  705. int mid; /* binary search current idx */
  706. xfs_mount_t *mp; /* filesystem mount point */
  707. xfs_trans_t *tp; /* transaction pointer */
  708. enum xfs_dacmp cmp; /* comparison result */
  709. dp = args->dp;
  710. tp = args->trans;
  711. mp = dp->i_mount;
  712. error = xfs_dir3_block_read(tp, dp, &bp);
  713. if (error)
  714. return error;
  715. hdr = bp->b_addr;
  716. xfs_dir3_data_check(dp, bp);
  717. btp = xfs_dir2_block_tail_p(mp, hdr);
  718. blp = xfs_dir2_block_leaf_p(btp);
  719. /*
  720. * Loop doing a binary search for our hash value.
  721. * Find our entry, ENOENT if it's not there.
  722. */
  723. for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
  724. ASSERT(low <= high);
  725. mid = (low + high) >> 1;
  726. if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
  727. break;
  728. if (hash < args->hashval)
  729. low = mid + 1;
  730. else
  731. high = mid - 1;
  732. if (low > high) {
  733. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  734. xfs_trans_brelse(tp, bp);
  735. return XFS_ERROR(ENOENT);
  736. }
  737. }
  738. /*
  739. * Back up to the first one with the right hash value.
  740. */
  741. while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
  742. mid--;
  743. }
  744. /*
  745. * Now loop forward through all the entries with the
  746. * right hash value looking for our name.
  747. */
  748. do {
  749. if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
  750. continue;
  751. /*
  752. * Get pointer to the entry from the leaf.
  753. */
  754. dep = (xfs_dir2_data_entry_t *)
  755. ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
  756. /*
  757. * Compare name and if it's an exact match, return the index
  758. * and buffer. If it's the first case-insensitive match, store
  759. * the index and buffer and continue looking for an exact match.
  760. */
  761. cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
  762. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  763. args->cmpresult = cmp;
  764. *bpp = bp;
  765. *entno = mid;
  766. if (cmp == XFS_CMP_EXACT)
  767. return 0;
  768. }
  769. } while (++mid < be32_to_cpu(btp->count) &&
  770. be32_to_cpu(blp[mid].hashval) == hash);
  771. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  772. /*
  773. * Here, we can only be doing a lookup (not a rename or replace).
  774. * If a case-insensitive match was found earlier, return success.
  775. */
  776. if (args->cmpresult == XFS_CMP_CASE)
  777. return 0;
  778. /*
  779. * No match, release the buffer and return ENOENT.
  780. */
  781. xfs_trans_brelse(tp, bp);
  782. return XFS_ERROR(ENOENT);
  783. }
  784. /*
  785. * Remove an entry from a block format directory.
  786. * If that makes the block small enough to fit in shortform, transform it.
  787. */
  788. int /* error */
  789. xfs_dir2_block_removename(
  790. xfs_da_args_t *args) /* directory operation args */
  791. {
  792. xfs_dir2_data_hdr_t *hdr; /* block header */
  793. xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
  794. struct xfs_buf *bp; /* block buffer */
  795. xfs_dir2_block_tail_t *btp; /* block tail */
  796. xfs_dir2_data_entry_t *dep; /* block data entry */
  797. xfs_inode_t *dp; /* incore inode */
  798. int ent; /* block leaf entry index */
  799. int error; /* error return value */
  800. xfs_mount_t *mp; /* filesystem mount point */
  801. int needlog; /* need to log block header */
  802. int needscan; /* need to fixup bestfree */
  803. xfs_dir2_sf_hdr_t sfh; /* shortform header */
  804. int size; /* shortform size */
  805. xfs_trans_t *tp; /* transaction pointer */
  806. trace_xfs_dir2_block_removename(args);
  807. /*
  808. * Look up the entry in the block. Gets the buffer and entry index.
  809. * It will always be there, the vnodeops level does a lookup first.
  810. */
  811. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
  812. return error;
  813. }
  814. dp = args->dp;
  815. tp = args->trans;
  816. mp = dp->i_mount;
  817. hdr = bp->b_addr;
  818. btp = xfs_dir2_block_tail_p(mp, hdr);
  819. blp = xfs_dir2_block_leaf_p(btp);
  820. /*
  821. * Point to the data entry using the leaf entry.
  822. */
  823. dep = (xfs_dir2_data_entry_t *)
  824. ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
  825. /*
  826. * Mark the data entry's space free.
  827. */
  828. needlog = needscan = 0;
  829. xfs_dir2_data_make_free(tp, bp,
  830. (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
  831. xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
  832. /*
  833. * Fix up the block tail.
  834. */
  835. be32_add_cpu(&btp->stale, 1);
  836. xfs_dir2_block_log_tail(tp, bp);
  837. /*
  838. * Remove the leaf entry by marking it stale.
  839. */
  840. blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  841. xfs_dir2_block_log_leaf(tp, bp, ent, ent);
  842. /*
  843. * Fix up bestfree, log the header if necessary.
  844. */
  845. if (needscan)
  846. xfs_dir2_data_freescan(mp, hdr, &needlog);
  847. if (needlog)
  848. xfs_dir2_data_log_header(tp, bp);
  849. xfs_dir3_data_check(dp, bp);
  850. /*
  851. * See if the size as a shortform is good enough.
  852. */
  853. size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
  854. if (size > XFS_IFORK_DSIZE(dp))
  855. return 0;
  856. /*
  857. * If it works, do the conversion.
  858. */
  859. return xfs_dir2_block_to_sf(args, bp, size, &sfh);
  860. }
  861. /*
  862. * Replace an entry in a V2 block directory.
  863. * Change the inode number to the new value.
  864. */
  865. int /* error */
  866. xfs_dir2_block_replace(
  867. xfs_da_args_t *args) /* directory operation args */
  868. {
  869. xfs_dir2_data_hdr_t *hdr; /* block header */
  870. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  871. struct xfs_buf *bp; /* block buffer */
  872. xfs_dir2_block_tail_t *btp; /* block tail */
  873. xfs_dir2_data_entry_t *dep; /* block data entry */
  874. xfs_inode_t *dp; /* incore inode */
  875. int ent; /* leaf entry index */
  876. int error; /* error return value */
  877. xfs_mount_t *mp; /* filesystem mount point */
  878. trace_xfs_dir2_block_replace(args);
  879. /*
  880. * Lookup the entry in the directory. Get buffer and entry index.
  881. * This will always succeed since the caller has already done a lookup.
  882. */
  883. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
  884. return error;
  885. }
  886. dp = args->dp;
  887. mp = dp->i_mount;
  888. hdr = bp->b_addr;
  889. btp = xfs_dir2_block_tail_p(mp, hdr);
  890. blp = xfs_dir2_block_leaf_p(btp);
  891. /*
  892. * Point to the data entry we need to change.
  893. */
  894. dep = (xfs_dir2_data_entry_t *)
  895. ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
  896. ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
  897. /*
  898. * Change the inode number to the new value.
  899. */
  900. dep->inumber = cpu_to_be64(args->inumber);
  901. xfs_dir2_data_log_entry(args->trans, bp, dep);
  902. xfs_dir3_data_check(dp, bp);
  903. return 0;
  904. }
  905. /*
  906. * Qsort comparison routine for the block leaf entries.
  907. */
  908. static int /* sort order */
  909. xfs_dir2_block_sort(
  910. const void *a, /* first leaf entry */
  911. const void *b) /* second leaf entry */
  912. {
  913. const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
  914. const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
  915. la = a;
  916. lb = b;
  917. return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
  918. (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
  919. }
  920. /*
  921. * Convert a V2 leaf directory to a V2 block directory if possible.
  922. */
  923. int /* error */
  924. xfs_dir2_leaf_to_block(
  925. xfs_da_args_t *args, /* operation arguments */
  926. struct xfs_buf *lbp, /* leaf buffer */
  927. struct xfs_buf *dbp) /* data buffer */
  928. {
  929. __be16 *bestsp; /* leaf bests table */
  930. xfs_dir2_data_hdr_t *hdr; /* block header */
  931. xfs_dir2_block_tail_t *btp; /* block tail */
  932. xfs_inode_t *dp; /* incore directory inode */
  933. xfs_dir2_data_unused_t *dup; /* unused data entry */
  934. int error; /* error return value */
  935. int from; /* leaf from index */
  936. xfs_dir2_leaf_t *leaf; /* leaf structure */
  937. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  938. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  939. xfs_mount_t *mp; /* file system mount point */
  940. int needlog; /* need to log data header */
  941. int needscan; /* need to scan for bestfree */
  942. xfs_dir2_sf_hdr_t sfh; /* shortform header */
  943. int size; /* bytes used */
  944. __be16 *tagp; /* end of entry (tag) */
  945. int to; /* block/leaf to index */
  946. xfs_trans_t *tp; /* transaction pointer */
  947. struct xfs_dir2_leaf_entry *ents;
  948. struct xfs_dir3_icleaf_hdr leafhdr;
  949. trace_xfs_dir2_leaf_to_block(args);
  950. dp = args->dp;
  951. tp = args->trans;
  952. mp = dp->i_mount;
  953. leaf = lbp->b_addr;
  954. xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
  955. ents = xfs_dir3_leaf_ents_p(leaf);
  956. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  957. ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
  958. leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
  959. /*
  960. * If there are data blocks other than the first one, take this
  961. * opportunity to remove trailing empty data blocks that may have
  962. * been left behind during no-space-reservation operations.
  963. * These will show up in the leaf bests table.
  964. */
  965. while (dp->i_d.di_size > mp->m_dirblksize) {
  966. int hdrsz;
  967. hdrsz = xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb));
  968. bestsp = xfs_dir2_leaf_bests_p(ltp);
  969. if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
  970. mp->m_dirblksize - hdrsz) {
  971. if ((error =
  972. xfs_dir2_leaf_trim_data(args, lbp,
  973. (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
  974. return error;
  975. } else
  976. return 0;
  977. }
  978. /*
  979. * Read the data block if we don't already have it, give up if it fails.
  980. */
  981. if (!dbp) {
  982. error = xfs_dir3_data_read(tp, dp, mp->m_dirdatablk, -1, &dbp);
  983. if (error)
  984. return error;
  985. }
  986. hdr = dbp->b_addr;
  987. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  988. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
  989. /*
  990. * Size of the "leaf" area in the block.
  991. */
  992. size = (uint)sizeof(xfs_dir2_block_tail_t) +
  993. (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
  994. /*
  995. * Look at the last data entry.
  996. */
  997. tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
  998. dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  999. /*
  1000. * If it's not free or is too short we can't do it.
  1001. */
  1002. if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
  1003. be16_to_cpu(dup->length) < size)
  1004. return 0;
  1005. /*
  1006. * Start converting it to block form.
  1007. */
  1008. xfs_dir3_block_init(mp, tp, dbp, dp);
  1009. needlog = 1;
  1010. needscan = 0;
  1011. /*
  1012. * Use up the space at the end of the block (blp/btp).
  1013. */
  1014. xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
  1015. &needlog, &needscan);
  1016. /*
  1017. * Initialize the block tail.
  1018. */
  1019. btp = xfs_dir2_block_tail_p(mp, hdr);
  1020. btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
  1021. btp->stale = 0;
  1022. xfs_dir2_block_log_tail(tp, dbp);
  1023. /*
  1024. * Initialize the block leaf area. We compact out stale entries.
  1025. */
  1026. lep = xfs_dir2_block_leaf_p(btp);
  1027. for (from = to = 0; from < leafhdr.count; from++) {
  1028. if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  1029. continue;
  1030. lep[to++] = ents[from];
  1031. }
  1032. ASSERT(to == be32_to_cpu(btp->count));
  1033. xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
  1034. /*
  1035. * Scan the bestfree if we need it and log the data block header.
  1036. */
  1037. if (needscan)
  1038. xfs_dir2_data_freescan(mp, hdr, &needlog);
  1039. if (needlog)
  1040. xfs_dir2_data_log_header(tp, dbp);
  1041. /*
  1042. * Pitch the old leaf block.
  1043. */
  1044. error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
  1045. if (error)
  1046. return error;
  1047. /*
  1048. * Now see if the resulting block can be shrunken to shortform.
  1049. */
  1050. size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
  1051. if (size > XFS_IFORK_DSIZE(dp))
  1052. return 0;
  1053. return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
  1054. }
  1055. /*
  1056. * Convert the shortform directory to block form.
  1057. */
  1058. int /* error */
  1059. xfs_dir2_sf_to_block(
  1060. xfs_da_args_t *args) /* operation arguments */
  1061. {
  1062. xfs_dir2_db_t blkno; /* dir-relative block # (0) */
  1063. xfs_dir2_data_hdr_t *hdr; /* block header */
  1064. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  1065. struct xfs_buf *bp; /* block buffer */
  1066. xfs_dir2_block_tail_t *btp; /* block tail pointer */
  1067. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  1068. xfs_inode_t *dp; /* incore directory inode */
  1069. int dummy; /* trash */
  1070. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  1071. int endoffset; /* end of data objects */
  1072. int error; /* error return value */
  1073. int i; /* index */
  1074. xfs_mount_t *mp; /* filesystem mount point */
  1075. int needlog; /* need to log block header */
  1076. int needscan; /* need to scan block freespc */
  1077. int newoffset; /* offset from current entry */
  1078. int offset; /* target block offset */
  1079. xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
  1080. xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
  1081. xfs_dir2_sf_hdr_t *sfp; /* shortform header */
  1082. __be16 *tagp; /* end of data entry */
  1083. xfs_trans_t *tp; /* transaction pointer */
  1084. struct xfs_name name;
  1085. trace_xfs_dir2_sf_to_block(args);
  1086. dp = args->dp;
  1087. tp = args->trans;
  1088. mp = dp->i_mount;
  1089. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  1090. /*
  1091. * Bomb out if the shortform directory is way too short.
  1092. */
  1093. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  1094. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1095. return XFS_ERROR(EIO);
  1096. }
  1097. oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  1098. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  1099. ASSERT(dp->i_df.if_u1.if_data != NULL);
  1100. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
  1101. /*
  1102. * Copy the directory into a temporary buffer.
  1103. * Then pitch the incore inode data so we can make extents.
  1104. */
  1105. sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
  1106. memcpy(sfp, oldsfp, dp->i_df.if_bytes);
  1107. xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
  1108. dp->i_d.di_size = 0;
  1109. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1110. /*
  1111. * Add block 0 to the inode.
  1112. */
  1113. error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
  1114. if (error) {
  1115. kmem_free(sfp);
  1116. return error;
  1117. }
  1118. /*
  1119. * Initialize the data block, then convert it to block format.
  1120. */
  1121. error = xfs_dir3_data_init(args, blkno, &bp);
  1122. if (error) {
  1123. kmem_free(sfp);
  1124. return error;
  1125. }
  1126. xfs_dir3_block_init(mp, tp, bp, dp);
  1127. hdr = bp->b_addr;
  1128. /*
  1129. * Compute size of block "tail" area.
  1130. */
  1131. i = (uint)sizeof(*btp) +
  1132. (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
  1133. /*
  1134. * The whole thing is initialized to free by the init routine.
  1135. * Say we're using the leaf and tail area.
  1136. */
  1137. dup = xfs_dir3_data_unused_p(hdr);
  1138. needlog = needscan = 0;
  1139. xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
  1140. &needscan);
  1141. ASSERT(needscan == 0);
  1142. /*
  1143. * Fill in the tail.
  1144. */
  1145. btp = xfs_dir2_block_tail_p(mp, hdr);
  1146. btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
  1147. btp->stale = 0;
  1148. blp = xfs_dir2_block_leaf_p(btp);
  1149. endoffset = (uint)((char *)blp - (char *)hdr);
  1150. /*
  1151. * Remove the freespace, we'll manage it.
  1152. */
  1153. xfs_dir2_data_use_free(tp, bp, dup,
  1154. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
  1155. be16_to_cpu(dup->length), &needlog, &needscan);
  1156. /*
  1157. * Create entry for .
  1158. */
  1159. dep = xfs_dir3_data_dot_entry_p(hdr);
  1160. dep->inumber = cpu_to_be64(dp->i_ino);
  1161. dep->namelen = 1;
  1162. dep->name[0] = '.';
  1163. tagp = xfs_dir2_data_entry_tag_p(dep);
  1164. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1165. xfs_dir2_data_log_entry(tp, bp, dep);
  1166. blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
  1167. blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
  1168. (char *)dep - (char *)hdr));
  1169. /*
  1170. * Create entry for ..
  1171. */
  1172. dep = xfs_dir3_data_dotdot_entry_p(hdr);
  1173. dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
  1174. dep->namelen = 2;
  1175. dep->name[0] = dep->name[1] = '.';
  1176. tagp = xfs_dir2_data_entry_tag_p(dep);
  1177. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1178. xfs_dir2_data_log_entry(tp, bp, dep);
  1179. blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
  1180. blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
  1181. (char *)dep - (char *)hdr));
  1182. offset = xfs_dir3_data_first_offset(hdr);
  1183. /*
  1184. * Loop over existing entries, stuff them in.
  1185. */
  1186. i = 0;
  1187. if (!sfp->count)
  1188. sfep = NULL;
  1189. else
  1190. sfep = xfs_dir2_sf_firstentry(sfp);
  1191. /*
  1192. * Need to preserve the existing offset values in the sf directory.
  1193. * Insert holes (unused entries) where necessary.
  1194. */
  1195. while (offset < endoffset) {
  1196. /*
  1197. * sfep is null when we reach the end of the list.
  1198. */
  1199. if (sfep == NULL)
  1200. newoffset = endoffset;
  1201. else
  1202. newoffset = xfs_dir2_sf_get_offset(sfep);
  1203. /*
  1204. * There should be a hole here, make one.
  1205. */
  1206. if (offset < newoffset) {
  1207. dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  1208. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  1209. dup->length = cpu_to_be16(newoffset - offset);
  1210. *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
  1211. ((char *)dup - (char *)hdr));
  1212. xfs_dir2_data_log_unused(tp, bp, dup);
  1213. xfs_dir2_data_freeinsert(hdr, dup, &dummy);
  1214. offset += be16_to_cpu(dup->length);
  1215. continue;
  1216. }
  1217. /*
  1218. * Copy a real entry.
  1219. */
  1220. dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
  1221. dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
  1222. dep->namelen = sfep->namelen;
  1223. memcpy(dep->name, sfep->name, dep->namelen);
  1224. tagp = xfs_dir2_data_entry_tag_p(dep);
  1225. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1226. xfs_dir2_data_log_entry(tp, bp, dep);
  1227. name.name = sfep->name;
  1228. name.len = sfep->namelen;
  1229. blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
  1230. hashname(&name));
  1231. blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
  1232. (char *)dep - (char *)hdr));
  1233. offset = (int)((char *)(tagp + 1) - (char *)hdr);
  1234. if (++i == sfp->count)
  1235. sfep = NULL;
  1236. else
  1237. sfep = xfs_dir2_sf_nextentry(sfp, sfep);
  1238. }
  1239. /* Done with the temporary buffer */
  1240. kmem_free(sfp);
  1241. /*
  1242. * Sort the leaf entries by hash value.
  1243. */
  1244. xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
  1245. /*
  1246. * Log the leaf entry area and tail.
  1247. * Already logged the header in data_init, ignore needlog.
  1248. */
  1249. ASSERT(needscan == 0);
  1250. xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
  1251. xfs_dir2_block_log_tail(tp, bp);
  1252. xfs_dir3_data_check(dp, bp);
  1253. return 0;
  1254. }