xfs_dir2_block.c 35 KB

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