xfs_dir2_block.c 37 KB

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