xfs_dir2_block.c 35 KB

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