xfs_dir2_block.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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_data_hdr_t *hdr; /* block header */
  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. hdr = bp->data;
  103. /*
  104. * Check the magic number, corrupted if wrong.
  105. */
  106. if (unlikely(hdr->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))) {
  107. XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
  108. XFS_ERRLEVEL_LOW, mp, hdr);
  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 = hdr->bestfree;
  117. btp = xfs_dir2_block_tail_p(mp, hdr);
  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 *)hdr + 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 *)hdr + 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 *)hdr +
  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 *)hdr + 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 *)hdr + 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 (blp[fromidx].address ==
  252. cpu_to_be32(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_cpu(&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 *)hdr),
  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, hdr, &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 *)hdr + 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_cpu(&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, hdr, &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. blp[lowstale].address !=
  350. cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  351. lowstale--)
  352. continue;
  353. for (highstale = mid + 1;
  354. highstale < be32_to_cpu(btp->count) &&
  355. blp[highstale].address !=
  356. cpu_to_be32(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 *)hdr));
  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 *)hdr),
  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 *)hdr);
  411. /*
  412. * Clean up the bestfree array and log the header, tail, and entry.
  413. */
  414. if (needscan)
  415. xfs_dir2_data_freescan(mp, hdr, &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_data_hdr_t *hdr; /* block header */
  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. hdr = 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, hdr);
  471. ptr = (char *)(hdr + 1);
  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 *)hdr < wantoff)
  495. continue;
  496. cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  497. (char *)dep - (char *)hdr);
  498. /*
  499. * If it didn't fit, set the final offset to here & return.
  500. */
  501. if (filldir(dirent, (char *)dep->name, dep->namelen,
  502. cook & 0x7fffffff, be64_to_cpu(dep->inumber),
  503. DT_UNKNOWN)) {
  504. *offset = cook & 0x7fffffff;
  505. xfs_da_brelse(NULL, bp);
  506. return 0;
  507. }
  508. }
  509. /*
  510. * Reached the end of the block.
  511. * Set the offset to a non-existent block 1 and return.
  512. */
  513. *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
  514. 0x7fffffff;
  515. xfs_da_brelse(NULL, bp);
  516. return 0;
  517. }
  518. /*
  519. * Log leaf entries from the block.
  520. */
  521. static void
  522. xfs_dir2_block_log_leaf(
  523. xfs_trans_t *tp, /* transaction structure */
  524. xfs_dabuf_t *bp, /* block buffer */
  525. int first, /* index of first logged leaf */
  526. int last) /* index of last logged leaf */
  527. {
  528. xfs_dir2_data_hdr_t *hdr = bp->data;
  529. xfs_dir2_leaf_entry_t *blp;
  530. xfs_dir2_block_tail_t *btp;
  531. btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
  532. blp = xfs_dir2_block_leaf_p(btp);
  533. xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
  534. (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
  535. }
  536. /*
  537. * Log the block tail.
  538. */
  539. static void
  540. xfs_dir2_block_log_tail(
  541. xfs_trans_t *tp, /* transaction structure */
  542. xfs_dabuf_t *bp) /* block buffer */
  543. {
  544. xfs_dir2_data_hdr_t *hdr = bp->data;
  545. xfs_dir2_block_tail_t *btp;
  546. btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
  547. xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
  548. (uint)((char *)(btp + 1) - (char *)hdr - 1));
  549. }
  550. /*
  551. * Look up an entry in the block. This is the external routine,
  552. * xfs_dir2_block_lookup_int does the real work.
  553. */
  554. int /* error */
  555. xfs_dir2_block_lookup(
  556. xfs_da_args_t *args) /* dir lookup arguments */
  557. {
  558. xfs_dir2_data_hdr_t *hdr; /* block header */
  559. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  560. xfs_dabuf_t *bp; /* block buffer */
  561. xfs_dir2_block_tail_t *btp; /* block tail */
  562. xfs_dir2_data_entry_t *dep; /* block data entry */
  563. xfs_inode_t *dp; /* incore inode */
  564. int ent; /* entry index */
  565. int error; /* error return value */
  566. xfs_mount_t *mp; /* filesystem mount point */
  567. trace_xfs_dir2_block_lookup(args);
  568. /*
  569. * Get the buffer, look up the entry.
  570. * If not found (ENOENT) then return, have no buffer.
  571. */
  572. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
  573. return error;
  574. dp = args->dp;
  575. mp = dp->i_mount;
  576. hdr = bp->data;
  577. xfs_dir2_data_check(dp, bp);
  578. btp = xfs_dir2_block_tail_p(mp, hdr);
  579. blp = xfs_dir2_block_leaf_p(btp);
  580. /*
  581. * Get the offset from the leaf entry, to point to the data.
  582. */
  583. dep = (xfs_dir2_data_entry_t *)((char *)hdr +
  584. xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
  585. /*
  586. * Fill in inode number, CI name if appropriate, release the block.
  587. */
  588. args->inumber = be64_to_cpu(dep->inumber);
  589. error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  590. xfs_da_brelse(args->trans, bp);
  591. return XFS_ERROR(error);
  592. }
  593. /*
  594. * Internal block lookup routine.
  595. */
  596. static int /* error */
  597. xfs_dir2_block_lookup_int(
  598. xfs_da_args_t *args, /* dir lookup arguments */
  599. xfs_dabuf_t **bpp, /* returned block buffer */
  600. int *entno) /* returned entry number */
  601. {
  602. xfs_dir2_dataptr_t addr; /* data entry address */
  603. xfs_dir2_data_hdr_t *hdr; /* block header */
  604. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  605. xfs_dabuf_t *bp; /* block buffer */
  606. xfs_dir2_block_tail_t *btp; /* block tail */
  607. xfs_dir2_data_entry_t *dep; /* block data entry */
  608. xfs_inode_t *dp; /* incore inode */
  609. int error; /* error return value */
  610. xfs_dahash_t hash; /* found hash value */
  611. int high; /* binary search high index */
  612. int low; /* binary search low index */
  613. int mid; /* binary search current idx */
  614. xfs_mount_t *mp; /* filesystem mount point */
  615. xfs_trans_t *tp; /* transaction pointer */
  616. enum xfs_dacmp cmp; /* comparison result */
  617. dp = args->dp;
  618. tp = args->trans;
  619. mp = dp->i_mount;
  620. /*
  621. * Read the buffer, return error if we can't get it.
  622. */
  623. if ((error =
  624. xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
  625. return error;
  626. }
  627. ASSERT(bp != NULL);
  628. hdr = bp->data;
  629. xfs_dir2_data_check(dp, bp);
  630. btp = xfs_dir2_block_tail_p(mp, hdr);
  631. blp = xfs_dir2_block_leaf_p(btp);
  632. /*
  633. * Loop doing a binary search for our hash value.
  634. * Find our entry, ENOENT if it's not there.
  635. */
  636. for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
  637. ASSERT(low <= high);
  638. mid = (low + high) >> 1;
  639. if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
  640. break;
  641. if (hash < args->hashval)
  642. low = mid + 1;
  643. else
  644. high = mid - 1;
  645. if (low > high) {
  646. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  647. xfs_da_brelse(tp, bp);
  648. return XFS_ERROR(ENOENT);
  649. }
  650. }
  651. /*
  652. * Back up to the first one with the right hash value.
  653. */
  654. while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
  655. mid--;
  656. }
  657. /*
  658. * Now loop forward through all the entries with the
  659. * right hash value looking for our name.
  660. */
  661. do {
  662. if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
  663. continue;
  664. /*
  665. * Get pointer to the entry from the leaf.
  666. */
  667. dep = (xfs_dir2_data_entry_t *)
  668. ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
  669. /*
  670. * Compare name and if it's an exact match, return the index
  671. * and buffer. If it's the first case-insensitive match, store
  672. * the index and buffer and continue looking for an exact match.
  673. */
  674. cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
  675. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  676. args->cmpresult = cmp;
  677. *bpp = bp;
  678. *entno = mid;
  679. if (cmp == XFS_CMP_EXACT)
  680. return 0;
  681. }
  682. } while (++mid < be32_to_cpu(btp->count) &&
  683. be32_to_cpu(blp[mid].hashval) == hash);
  684. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  685. /*
  686. * Here, we can only be doing a lookup (not a rename or replace).
  687. * If a case-insensitive match was found earlier, return success.
  688. */
  689. if (args->cmpresult == XFS_CMP_CASE)
  690. return 0;
  691. /*
  692. * No match, release the buffer and return ENOENT.
  693. */
  694. xfs_da_brelse(tp, bp);
  695. return XFS_ERROR(ENOENT);
  696. }
  697. /*
  698. * Remove an entry from a block format directory.
  699. * If that makes the block small enough to fit in shortform, transform it.
  700. */
  701. int /* error */
  702. xfs_dir2_block_removename(
  703. xfs_da_args_t *args) /* directory operation args */
  704. {
  705. xfs_dir2_data_hdr_t *hdr; /* block header */
  706. xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
  707. xfs_dabuf_t *bp; /* block buffer */
  708. xfs_dir2_block_tail_t *btp; /* block tail */
  709. xfs_dir2_data_entry_t *dep; /* block data entry */
  710. xfs_inode_t *dp; /* incore inode */
  711. int ent; /* block leaf entry index */
  712. int error; /* error return value */
  713. xfs_mount_t *mp; /* filesystem mount point */
  714. int needlog; /* need to log block header */
  715. int needscan; /* need to fixup bestfree */
  716. xfs_dir2_sf_hdr_t sfh; /* shortform header */
  717. int size; /* shortform size */
  718. xfs_trans_t *tp; /* transaction pointer */
  719. trace_xfs_dir2_block_removename(args);
  720. /*
  721. * Look up the entry in the block. Gets the buffer and entry index.
  722. * It will always be there, the vnodeops level does a lookup first.
  723. */
  724. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
  725. return error;
  726. }
  727. dp = args->dp;
  728. tp = args->trans;
  729. mp = dp->i_mount;
  730. hdr = bp->data;
  731. btp = xfs_dir2_block_tail_p(mp, hdr);
  732. blp = xfs_dir2_block_leaf_p(btp);
  733. /*
  734. * Point to the data entry using the leaf entry.
  735. */
  736. dep = (xfs_dir2_data_entry_t *)
  737. ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
  738. /*
  739. * Mark the data entry's space free.
  740. */
  741. needlog = needscan = 0;
  742. xfs_dir2_data_make_free(tp, bp,
  743. (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
  744. xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
  745. /*
  746. * Fix up the block tail.
  747. */
  748. be32_add_cpu(&btp->stale, 1);
  749. xfs_dir2_block_log_tail(tp, bp);
  750. /*
  751. * Remove the leaf entry by marking it stale.
  752. */
  753. blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  754. xfs_dir2_block_log_leaf(tp, bp, ent, ent);
  755. /*
  756. * Fix up bestfree, log the header if necessary.
  757. */
  758. if (needscan)
  759. xfs_dir2_data_freescan(mp, hdr, &needlog);
  760. if (needlog)
  761. xfs_dir2_data_log_header(tp, bp);
  762. xfs_dir2_data_check(dp, bp);
  763. /*
  764. * See if the size as a shortform is good enough.
  765. */
  766. size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
  767. if (size > XFS_IFORK_DSIZE(dp)) {
  768. xfs_da_buf_done(bp);
  769. return 0;
  770. }
  771. /*
  772. * If it works, do the conversion.
  773. */
  774. return xfs_dir2_block_to_sf(args, bp, size, &sfh);
  775. }
  776. /*
  777. * Replace an entry in a V2 block directory.
  778. * Change the inode number to the new value.
  779. */
  780. int /* error */
  781. xfs_dir2_block_replace(
  782. xfs_da_args_t *args) /* directory operation args */
  783. {
  784. xfs_dir2_data_hdr_t *hdr; /* block header */
  785. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  786. xfs_dabuf_t *bp; /* block buffer */
  787. xfs_dir2_block_tail_t *btp; /* block tail */
  788. xfs_dir2_data_entry_t *dep; /* block data entry */
  789. xfs_inode_t *dp; /* incore inode */
  790. int ent; /* leaf entry index */
  791. int error; /* error return value */
  792. xfs_mount_t *mp; /* filesystem mount point */
  793. trace_xfs_dir2_block_replace(args);
  794. /*
  795. * Lookup the entry in the directory. Get buffer and entry index.
  796. * This will always succeed since the caller has already done a lookup.
  797. */
  798. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
  799. return error;
  800. }
  801. dp = args->dp;
  802. mp = dp->i_mount;
  803. hdr = bp->data;
  804. btp = xfs_dir2_block_tail_p(mp, hdr);
  805. blp = xfs_dir2_block_leaf_p(btp);
  806. /*
  807. * Point to the data entry we need to change.
  808. */
  809. dep = (xfs_dir2_data_entry_t *)
  810. ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
  811. ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
  812. /*
  813. * Change the inode number to the new value.
  814. */
  815. dep->inumber = cpu_to_be64(args->inumber);
  816. xfs_dir2_data_log_entry(args->trans, bp, dep);
  817. xfs_dir2_data_check(dp, bp);
  818. xfs_da_buf_done(bp);
  819. return 0;
  820. }
  821. /*
  822. * Qsort comparison routine for the block leaf entries.
  823. */
  824. static int /* sort order */
  825. xfs_dir2_block_sort(
  826. const void *a, /* first leaf entry */
  827. const void *b) /* second leaf entry */
  828. {
  829. const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
  830. const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
  831. la = a;
  832. lb = b;
  833. return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
  834. (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
  835. }
  836. /*
  837. * Convert a V2 leaf directory to a V2 block directory if possible.
  838. */
  839. int /* error */
  840. xfs_dir2_leaf_to_block(
  841. xfs_da_args_t *args, /* operation arguments */
  842. xfs_dabuf_t *lbp, /* leaf buffer */
  843. xfs_dabuf_t *dbp) /* data buffer */
  844. {
  845. __be16 *bestsp; /* leaf bests table */
  846. xfs_dir2_data_hdr_t *hdr; /* block header */
  847. xfs_dir2_block_tail_t *btp; /* block tail */
  848. xfs_inode_t *dp; /* incore directory inode */
  849. xfs_dir2_data_unused_t *dup; /* unused data entry */
  850. int error; /* error return value */
  851. int from; /* leaf from index */
  852. xfs_dir2_leaf_t *leaf; /* leaf structure */
  853. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  854. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  855. xfs_mount_t *mp; /* file system mount point */
  856. int needlog; /* need to log data header */
  857. int needscan; /* need to scan for bestfree */
  858. xfs_dir2_sf_hdr_t sfh; /* shortform header */
  859. int size; /* bytes used */
  860. __be16 *tagp; /* end of entry (tag) */
  861. int to; /* block/leaf to index */
  862. xfs_trans_t *tp; /* transaction pointer */
  863. trace_xfs_dir2_leaf_to_block(args);
  864. dp = args->dp;
  865. tp = args->trans;
  866. mp = dp->i_mount;
  867. leaf = lbp->data;
  868. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
  869. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  870. /*
  871. * If there are data blocks other than the first one, take this
  872. * opportunity to remove trailing empty data blocks that may have
  873. * been left behind during no-space-reservation operations.
  874. * These will show up in the leaf bests table.
  875. */
  876. while (dp->i_d.di_size > mp->m_dirblksize) {
  877. bestsp = xfs_dir2_leaf_bests_p(ltp);
  878. if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
  879. mp->m_dirblksize - (uint)sizeof(*hdr)) {
  880. if ((error =
  881. xfs_dir2_leaf_trim_data(args, lbp,
  882. (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
  883. goto out;
  884. } else {
  885. error = 0;
  886. goto out;
  887. }
  888. }
  889. /*
  890. * Read the data block if we don't already have it, give up if it fails.
  891. */
  892. if (dbp == NULL &&
  893. (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
  894. XFS_DATA_FORK))) {
  895. goto out;
  896. }
  897. hdr = dbp->data;
  898. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
  899. /*
  900. * Size of the "leaf" area in the block.
  901. */
  902. size = (uint)sizeof(xfs_dir2_block_tail_t) +
  903. (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
  904. /*
  905. * Look at the last data entry.
  906. */
  907. tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
  908. dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  909. /*
  910. * If it's not free or is too short we can't do it.
  911. */
  912. if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
  913. be16_to_cpu(dup->length) < size) {
  914. error = 0;
  915. goto out;
  916. }
  917. /*
  918. * Start converting it to block form.
  919. */
  920. hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
  921. needlog = 1;
  922. needscan = 0;
  923. /*
  924. * Use up the space at the end of the block (blp/btp).
  925. */
  926. xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
  927. &needlog, &needscan);
  928. /*
  929. * Initialize the block tail.
  930. */
  931. btp = xfs_dir2_block_tail_p(mp, hdr);
  932. btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
  933. btp->stale = 0;
  934. xfs_dir2_block_log_tail(tp, dbp);
  935. /*
  936. * Initialize the block leaf area. We compact out stale entries.
  937. */
  938. lep = xfs_dir2_block_leaf_p(btp);
  939. for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
  940. if (leaf->ents[from].address ==
  941. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  942. continue;
  943. lep[to++] = leaf->ents[from];
  944. }
  945. ASSERT(to == be32_to_cpu(btp->count));
  946. xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
  947. /*
  948. * Scan the bestfree if we need it and log the data block header.
  949. */
  950. if (needscan)
  951. xfs_dir2_data_freescan(mp, hdr, &needlog);
  952. if (needlog)
  953. xfs_dir2_data_log_header(tp, dbp);
  954. /*
  955. * Pitch the old leaf block.
  956. */
  957. error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
  958. lbp = NULL;
  959. if (error) {
  960. goto out;
  961. }
  962. /*
  963. * Now see if the resulting block can be shrunken to shortform.
  964. */
  965. size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
  966. if (size > XFS_IFORK_DSIZE(dp)) {
  967. error = 0;
  968. goto out;
  969. }
  970. return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
  971. out:
  972. if (lbp)
  973. xfs_da_buf_done(lbp);
  974. if (dbp)
  975. xfs_da_buf_done(dbp);
  976. return error;
  977. }
  978. /*
  979. * Convert the shortform directory to block form.
  980. */
  981. int /* error */
  982. xfs_dir2_sf_to_block(
  983. xfs_da_args_t *args) /* operation arguments */
  984. {
  985. xfs_dir2_db_t blkno; /* dir-relative block # (0) */
  986. xfs_dir2_data_hdr_t *hdr; /* block header */
  987. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  988. xfs_dabuf_t *bp; /* block buffer */
  989. xfs_dir2_block_tail_t *btp; /* block tail pointer */
  990. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  991. xfs_inode_t *dp; /* incore directory inode */
  992. int dummy; /* trash */
  993. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  994. int endoffset; /* end of data objects */
  995. int error; /* error return value */
  996. int i; /* index */
  997. xfs_mount_t *mp; /* filesystem mount point */
  998. int needlog; /* need to log block header */
  999. int needscan; /* need to scan block freespc */
  1000. int newoffset; /* offset from current entry */
  1001. int offset; /* target block offset */
  1002. xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
  1003. xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
  1004. xfs_dir2_sf_hdr_t *sfp; /* shortform header */
  1005. __be16 *tagp; /* end of data entry */
  1006. xfs_trans_t *tp; /* transaction pointer */
  1007. struct xfs_name name;
  1008. trace_xfs_dir2_sf_to_block(args);
  1009. dp = args->dp;
  1010. tp = args->trans;
  1011. mp = dp->i_mount;
  1012. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  1013. /*
  1014. * Bomb out if the shortform directory is way too short.
  1015. */
  1016. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  1017. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1018. return XFS_ERROR(EIO);
  1019. }
  1020. oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  1021. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  1022. ASSERT(dp->i_df.if_u1.if_data != NULL);
  1023. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
  1024. /*
  1025. * Copy the directory into a temporary buffer.
  1026. * Then pitch the incore inode data so we can make extents.
  1027. */
  1028. sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
  1029. memcpy(sfp, oldsfp, dp->i_df.if_bytes);
  1030. xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
  1031. dp->i_d.di_size = 0;
  1032. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1033. /*
  1034. * Add block 0 to the inode.
  1035. */
  1036. error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
  1037. if (error) {
  1038. kmem_free(sfp);
  1039. return error;
  1040. }
  1041. /*
  1042. * Initialize the data block.
  1043. */
  1044. error = xfs_dir2_data_init(args, blkno, &bp);
  1045. if (error) {
  1046. kmem_free(sfp);
  1047. return error;
  1048. }
  1049. hdr = bp->data;
  1050. hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
  1051. /*
  1052. * Compute size of block "tail" area.
  1053. */
  1054. i = (uint)sizeof(*btp) +
  1055. (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
  1056. /*
  1057. * The whole thing is initialized to free by the init routine.
  1058. * Say we're using the leaf and tail area.
  1059. */
  1060. dup = (xfs_dir2_data_unused_t *)(hdr + 1);
  1061. needlog = needscan = 0;
  1062. xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
  1063. &needscan);
  1064. ASSERT(needscan == 0);
  1065. /*
  1066. * Fill in the tail.
  1067. */
  1068. btp = xfs_dir2_block_tail_p(mp, hdr);
  1069. btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
  1070. btp->stale = 0;
  1071. blp = xfs_dir2_block_leaf_p(btp);
  1072. endoffset = (uint)((char *)blp - (char *)hdr);
  1073. /*
  1074. * Remove the freespace, we'll manage it.
  1075. */
  1076. xfs_dir2_data_use_free(tp, bp, dup,
  1077. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
  1078. be16_to_cpu(dup->length), &needlog, &needscan);
  1079. /*
  1080. * Create entry for .
  1081. */
  1082. dep = (xfs_dir2_data_entry_t *)
  1083. ((char *)hdr + XFS_DIR2_DATA_DOT_OFFSET);
  1084. dep->inumber = cpu_to_be64(dp->i_ino);
  1085. dep->namelen = 1;
  1086. dep->name[0] = '.';
  1087. tagp = xfs_dir2_data_entry_tag_p(dep);
  1088. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1089. xfs_dir2_data_log_entry(tp, bp, dep);
  1090. blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
  1091. blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
  1092. (char *)dep - (char *)hdr));
  1093. /*
  1094. * Create entry for ..
  1095. */
  1096. dep = (xfs_dir2_data_entry_t *)
  1097. ((char *)hdr + XFS_DIR2_DATA_DOTDOT_OFFSET);
  1098. dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
  1099. dep->namelen = 2;
  1100. dep->name[0] = dep->name[1] = '.';
  1101. tagp = xfs_dir2_data_entry_tag_p(dep);
  1102. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1103. xfs_dir2_data_log_entry(tp, bp, dep);
  1104. blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
  1105. blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
  1106. (char *)dep - (char *)hdr));
  1107. offset = XFS_DIR2_DATA_FIRST_OFFSET;
  1108. /*
  1109. * Loop over existing entries, stuff them in.
  1110. */
  1111. i = 0;
  1112. if (!sfp->count)
  1113. sfep = NULL;
  1114. else
  1115. sfep = xfs_dir2_sf_firstentry(sfp);
  1116. /*
  1117. * Need to preserve the existing offset values in the sf directory.
  1118. * Insert holes (unused entries) where necessary.
  1119. */
  1120. while (offset < endoffset) {
  1121. /*
  1122. * sfep is null when we reach the end of the list.
  1123. */
  1124. if (sfep == NULL)
  1125. newoffset = endoffset;
  1126. else
  1127. newoffset = xfs_dir2_sf_get_offset(sfep);
  1128. /*
  1129. * There should be a hole here, make one.
  1130. */
  1131. if (offset < newoffset) {
  1132. dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  1133. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  1134. dup->length = cpu_to_be16(newoffset - offset);
  1135. *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
  1136. ((char *)dup - (char *)hdr));
  1137. xfs_dir2_data_log_unused(tp, bp, dup);
  1138. xfs_dir2_data_freeinsert(hdr, dup, &dummy);
  1139. offset += be16_to_cpu(dup->length);
  1140. continue;
  1141. }
  1142. /*
  1143. * Copy a real entry.
  1144. */
  1145. dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
  1146. dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
  1147. dep->namelen = sfep->namelen;
  1148. memcpy(dep->name, sfep->name, dep->namelen);
  1149. tagp = xfs_dir2_data_entry_tag_p(dep);
  1150. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1151. xfs_dir2_data_log_entry(tp, bp, dep);
  1152. name.name = sfep->name;
  1153. name.len = sfep->namelen;
  1154. blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
  1155. hashname(&name));
  1156. blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
  1157. (char *)dep - (char *)hdr));
  1158. offset = (int)((char *)(tagp + 1) - (char *)hdr);
  1159. if (++i == sfp->count)
  1160. sfep = NULL;
  1161. else
  1162. sfep = xfs_dir2_sf_nextentry(sfp, sfep);
  1163. }
  1164. /* Done with the temporary buffer */
  1165. kmem_free(sfp);
  1166. /*
  1167. * Sort the leaf entries by hash value.
  1168. */
  1169. xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
  1170. /*
  1171. * Log the leaf entry area and tail.
  1172. * Already logged the header in data_init, ignore needlog.
  1173. */
  1174. ASSERT(needscan == 0);
  1175. xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
  1176. xfs_dir2_block_log_tail(tp, bp);
  1177. xfs_dir2_data_check(dp, bp);
  1178. xfs_da_buf_done(bp);
  1179. return 0;
  1180. }