xfs_dir2_block.c 35 KB

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