xfs_dir2_block.c 34 KB

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