xfs_dir2_block.c 35 KB

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