xfs_dir2_block.c 37 KB

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