xfs_dir2_block.c 37 KB

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