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