xfs_dir2_block.c 35 KB

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