xfs_dir2_block.c 37 KB

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