xfs_dir2_block.c 35 KB

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