xfs_dir2_block.c 36 KB

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