xfs_dir2_block.c 36 KB

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