xfs_dir2_block.c 35 KB

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