xfs_dir2_block.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_trans.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_priv.h"
  36. #include "xfs_error.h"
  37. #include "xfs_trace.h"
  38. #include "xfs_cksum.h"
  39. #include "xfs_dinode.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_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
  87. xfs_buf_ioerror(bp, EFSBADCRC);
  88. else if (!xfs_dir3_block_verify(bp))
  89. xfs_buf_ioerror(bp, EFSCORRUPTED);
  90. if (bp->b_error)
  91. xfs_verifier_error(bp);
  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_buf_ioerror(bp, EFSCORRUPTED);
  102. xfs_verifier_error(bp);
  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_buf_update_cksum(bp, 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_dir_geo->datablk, -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_inode *dp,
  152. struct xfs_dir2_data_hdr *hdr,
  153. struct xfs_dir2_block_tail *btp,
  154. struct xfs_dir2_leaf_entry *blp,
  155. __be16 **tagpp,
  156. struct xfs_dir2_data_unused **dupp,
  157. struct xfs_dir2_data_unused **enddupp,
  158. int *compact,
  159. int len)
  160. {
  161. struct xfs_dir2_data_free *bf;
  162. __be16 *tagp = NULL;
  163. struct xfs_dir2_data_unused *dup = NULL;
  164. struct xfs_dir2_data_unused *enddup = NULL;
  165. *compact = 0;
  166. bf = dp->d_ops->data_bestfree_p(hdr);
  167. /*
  168. * If there are stale entries we'll use one for the leaf.
  169. */
  170. if (btp->stale) {
  171. if (be16_to_cpu(bf[0].length) >= len) {
  172. /*
  173. * The biggest entry enough to avoid compaction.
  174. */
  175. dup = (xfs_dir2_data_unused_t *)
  176. ((char *)hdr + be16_to_cpu(bf[0].offset));
  177. goto out;
  178. }
  179. /*
  180. * Will need to compact to make this work.
  181. * Tag just before the first leaf entry.
  182. */
  183. *compact = 1;
  184. tagp = (__be16 *)blp - 1;
  185. /* Data object just before the first leaf entry. */
  186. dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  187. /*
  188. * If it's not free then the data will go where the
  189. * leaf data starts now, if it works at all.
  190. */
  191. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  192. if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
  193. (uint)sizeof(*blp) < len)
  194. dup = NULL;
  195. } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
  196. dup = NULL;
  197. else
  198. dup = (xfs_dir2_data_unused_t *)blp;
  199. goto out;
  200. }
  201. /*
  202. * no stale entries, so just use free space.
  203. * Tag just before the first leaf entry.
  204. */
  205. tagp = (__be16 *)blp - 1;
  206. /* Data object just before the first leaf entry. */
  207. enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  208. /*
  209. * If it's not free then can't do this add without cleaning up:
  210. * the space before the first leaf entry needs to be free so it
  211. * can be expanded to hold the pointer to the new entry.
  212. */
  213. if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  214. /*
  215. * Check out the biggest freespace and see if it's the same one.
  216. */
  217. dup = (xfs_dir2_data_unused_t *)
  218. ((char *)hdr + be16_to_cpu(bf[0].offset));
  219. if (dup != enddup) {
  220. /*
  221. * Not the same free entry, just check its length.
  222. */
  223. if (be16_to_cpu(dup->length) < len)
  224. dup = NULL;
  225. goto out;
  226. }
  227. /*
  228. * It is the biggest freespace, can it hold the leaf too?
  229. */
  230. if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
  231. /*
  232. * Yes, use the second-largest entry instead if it works.
  233. */
  234. if (be16_to_cpu(bf[1].length) >= len)
  235. dup = (xfs_dir2_data_unused_t *)
  236. ((char *)hdr + be16_to_cpu(bf[1].offset));
  237. else
  238. dup = NULL;
  239. }
  240. }
  241. out:
  242. *tagpp = tagp;
  243. *dupp = dup;
  244. *enddupp = enddup;
  245. }
  246. /*
  247. * compact the leaf entries.
  248. * Leave the highest-numbered stale entry stale.
  249. * XXX should be the one closest to mid but mid is not yet computed.
  250. */
  251. static void
  252. xfs_dir2_block_compact(
  253. struct xfs_da_args *args,
  254. struct xfs_buf *bp,
  255. struct xfs_dir2_data_hdr *hdr,
  256. struct xfs_dir2_block_tail *btp,
  257. struct xfs_dir2_leaf_entry *blp,
  258. int *needlog,
  259. int *lfloghigh,
  260. int *lfloglow)
  261. {
  262. int fromidx; /* source leaf index */
  263. int toidx; /* target leaf index */
  264. int needscan = 0;
  265. int highstale; /* high stale index */
  266. fromidx = toidx = be32_to_cpu(btp->count) - 1;
  267. highstale = *lfloghigh = -1;
  268. for (; fromidx >= 0; fromidx--) {
  269. if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
  270. if (highstale == -1)
  271. highstale = toidx;
  272. else {
  273. if (*lfloghigh == -1)
  274. *lfloghigh = toidx;
  275. continue;
  276. }
  277. }
  278. if (fromidx < toidx)
  279. blp[toidx] = blp[fromidx];
  280. toidx--;
  281. }
  282. *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
  283. *lfloghigh -= be32_to_cpu(btp->stale) - 1;
  284. be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
  285. xfs_dir2_data_make_free(args, bp,
  286. (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
  287. (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
  288. needlog, &needscan);
  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(args->dp, 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 = dp->d_ops->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(args->geo, 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(dp, 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(args, 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(args, 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(dp, 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(
  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(args, 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. dp->d_ops->data_put_ftype(dep, args->filetype);
  510. tagp = dp->d_ops->data_entry_tag_p(dep);
  511. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  512. /*
  513. * Clean up the bestfree array and log the header, tail, and entry.
  514. */
  515. if (needscan)
  516. xfs_dir2_data_freescan(dp, hdr, &needlog);
  517. if (needlog)
  518. xfs_dir2_data_log_header(args, bp);
  519. xfs_dir2_block_log_tail(tp, bp);
  520. xfs_dir2_data_log_entry(args, bp, dep);
  521. xfs_dir3_data_check(dp, bp);
  522. return 0;
  523. }
  524. /*
  525. * Log leaf entries from the block.
  526. */
  527. static void
  528. xfs_dir2_block_log_leaf(
  529. xfs_trans_t *tp, /* transaction structure */
  530. struct xfs_buf *bp, /* block buffer */
  531. int first, /* index of first logged leaf */
  532. int last) /* index of last logged leaf */
  533. {
  534. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  535. xfs_dir2_leaf_entry_t *blp;
  536. xfs_dir2_block_tail_t *btp;
  537. btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
  538. blp = xfs_dir2_block_leaf_p(btp);
  539. xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
  540. (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
  541. }
  542. /*
  543. * Log the block tail.
  544. */
  545. static void
  546. xfs_dir2_block_log_tail(
  547. xfs_trans_t *tp, /* transaction structure */
  548. struct xfs_buf *bp) /* block buffer */
  549. {
  550. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  551. xfs_dir2_block_tail_t *btp;
  552. btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
  553. xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
  554. (uint)((char *)(btp + 1) - (char *)hdr - 1));
  555. }
  556. /*
  557. * Look up an entry in the block. This is the external routine,
  558. * xfs_dir2_block_lookup_int does the real work.
  559. */
  560. int /* error */
  561. xfs_dir2_block_lookup(
  562. xfs_da_args_t *args) /* dir lookup arguments */
  563. {
  564. xfs_dir2_data_hdr_t *hdr; /* block header */
  565. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  566. struct xfs_buf *bp; /* block buffer */
  567. xfs_dir2_block_tail_t *btp; /* block tail */
  568. xfs_dir2_data_entry_t *dep; /* block data entry */
  569. xfs_inode_t *dp; /* incore inode */
  570. int ent; /* entry index */
  571. int error; /* error return value */
  572. xfs_mount_t *mp; /* filesystem mount point */
  573. trace_xfs_dir2_block_lookup(args);
  574. /*
  575. * Get the buffer, look up the entry.
  576. * If not found (ENOENT) then return, have no buffer.
  577. */
  578. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
  579. return error;
  580. dp = args->dp;
  581. mp = dp->i_mount;
  582. hdr = bp->b_addr;
  583. xfs_dir3_data_check(dp, bp);
  584. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  585. blp = xfs_dir2_block_leaf_p(btp);
  586. /*
  587. * Get the offset from the leaf entry, to point to the data.
  588. */
  589. dep = (xfs_dir2_data_entry_t *)((char *)hdr +
  590. xfs_dir2_dataptr_to_off(args->geo,
  591. be32_to_cpu(blp[ent].address)));
  592. /*
  593. * Fill in inode number, CI name if appropriate, release the block.
  594. */
  595. args->inumber = be64_to_cpu(dep->inumber);
  596. args->filetype = dp->d_ops->data_get_ftype(dep);
  597. error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  598. xfs_trans_brelse(args->trans, bp);
  599. return XFS_ERROR(error);
  600. }
  601. /*
  602. * Internal block lookup routine.
  603. */
  604. static int /* error */
  605. xfs_dir2_block_lookup_int(
  606. xfs_da_args_t *args, /* dir lookup arguments */
  607. struct xfs_buf **bpp, /* returned block buffer */
  608. int *entno) /* returned entry number */
  609. {
  610. xfs_dir2_dataptr_t addr; /* data entry address */
  611. xfs_dir2_data_hdr_t *hdr; /* block header */
  612. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  613. struct xfs_buf *bp; /* block buffer */
  614. xfs_dir2_block_tail_t *btp; /* block tail */
  615. xfs_dir2_data_entry_t *dep; /* block data entry */
  616. xfs_inode_t *dp; /* incore inode */
  617. int error; /* error return value */
  618. xfs_dahash_t hash; /* found hash value */
  619. int high; /* binary search high index */
  620. int low; /* binary search low index */
  621. int mid; /* binary search current idx */
  622. xfs_mount_t *mp; /* filesystem mount point */
  623. xfs_trans_t *tp; /* transaction pointer */
  624. enum xfs_dacmp cmp; /* comparison result */
  625. dp = args->dp;
  626. tp = args->trans;
  627. mp = dp->i_mount;
  628. error = xfs_dir3_block_read(tp, dp, &bp);
  629. if (error)
  630. return error;
  631. hdr = bp->b_addr;
  632. xfs_dir3_data_check(dp, bp);
  633. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  634. blp = xfs_dir2_block_leaf_p(btp);
  635. /*
  636. * Loop doing a binary search for our hash value.
  637. * Find our entry, ENOENT if it's not there.
  638. */
  639. for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
  640. ASSERT(low <= high);
  641. mid = (low + high) >> 1;
  642. if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
  643. break;
  644. if (hash < args->hashval)
  645. low = mid + 1;
  646. else
  647. high = mid - 1;
  648. if (low > high) {
  649. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  650. xfs_trans_brelse(tp, bp);
  651. return XFS_ERROR(ENOENT);
  652. }
  653. }
  654. /*
  655. * Back up to the first one with the right hash value.
  656. */
  657. while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
  658. mid--;
  659. }
  660. /*
  661. * Now loop forward through all the entries with the
  662. * right hash value looking for our name.
  663. */
  664. do {
  665. if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
  666. continue;
  667. /*
  668. * Get pointer to the entry from the leaf.
  669. */
  670. dep = (xfs_dir2_data_entry_t *)
  671. ((char *)hdr + xfs_dir2_dataptr_to_off(args->geo, addr));
  672. /*
  673. * Compare name and if it's an exact match, return the index
  674. * and buffer. If it's the first case-insensitive match, store
  675. * the index and buffer and continue looking for an exact match.
  676. */
  677. cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
  678. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  679. args->cmpresult = cmp;
  680. *bpp = bp;
  681. *entno = mid;
  682. if (cmp == XFS_CMP_EXACT)
  683. return 0;
  684. }
  685. } while (++mid < be32_to_cpu(btp->count) &&
  686. be32_to_cpu(blp[mid].hashval) == hash);
  687. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  688. /*
  689. * Here, we can only be doing a lookup (not a rename or replace).
  690. * If a case-insensitive match was found earlier, return success.
  691. */
  692. if (args->cmpresult == XFS_CMP_CASE)
  693. return 0;
  694. /*
  695. * No match, release the buffer and return ENOENT.
  696. */
  697. xfs_trans_brelse(tp, bp);
  698. return XFS_ERROR(ENOENT);
  699. }
  700. /*
  701. * Remove an entry from a block format directory.
  702. * If that makes the block small enough to fit in shortform, transform it.
  703. */
  704. int /* error */
  705. xfs_dir2_block_removename(
  706. xfs_da_args_t *args) /* directory operation args */
  707. {
  708. xfs_dir2_data_hdr_t *hdr; /* block header */
  709. xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
  710. struct xfs_buf *bp; /* block buffer */
  711. xfs_dir2_block_tail_t *btp; /* block tail */
  712. xfs_dir2_data_entry_t *dep; /* block data entry */
  713. xfs_inode_t *dp; /* incore inode */
  714. int ent; /* block leaf entry index */
  715. int error; /* error return value */
  716. xfs_mount_t *mp; /* filesystem mount point */
  717. int needlog; /* need to log block header */
  718. int needscan; /* need to fixup bestfree */
  719. xfs_dir2_sf_hdr_t sfh; /* shortform header */
  720. int size; /* shortform size */
  721. xfs_trans_t *tp; /* transaction pointer */
  722. trace_xfs_dir2_block_removename(args);
  723. /*
  724. * Look up the entry in the block. Gets the buffer and entry index.
  725. * It will always be there, the vnodeops level does a lookup first.
  726. */
  727. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
  728. return error;
  729. }
  730. dp = args->dp;
  731. tp = args->trans;
  732. mp = dp->i_mount;
  733. hdr = bp->b_addr;
  734. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  735. blp = xfs_dir2_block_leaf_p(btp);
  736. /*
  737. * Point to the data entry using the leaf entry.
  738. */
  739. dep = (xfs_dir2_data_entry_t *)((char *)hdr +
  740. xfs_dir2_dataptr_to_off(args->geo,
  741. be32_to_cpu(blp[ent].address)));
  742. /*
  743. * Mark the data entry's space free.
  744. */
  745. needlog = needscan = 0;
  746. xfs_dir2_data_make_free(args, bp,
  747. (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
  748. dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
  749. /*
  750. * Fix up the block tail.
  751. */
  752. be32_add_cpu(&btp->stale, 1);
  753. xfs_dir2_block_log_tail(tp, bp);
  754. /*
  755. * Remove the leaf entry by marking it stale.
  756. */
  757. blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  758. xfs_dir2_block_log_leaf(tp, bp, ent, ent);
  759. /*
  760. * Fix up bestfree, log the header if necessary.
  761. */
  762. if (needscan)
  763. xfs_dir2_data_freescan(dp, hdr, &needlog);
  764. if (needlog)
  765. xfs_dir2_data_log_header(args, bp);
  766. xfs_dir3_data_check(dp, bp);
  767. /*
  768. * See if the size as a shortform is good enough.
  769. */
  770. size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
  771. if (size > XFS_IFORK_DSIZE(dp))
  772. return 0;
  773. /*
  774. * If it works, do the conversion.
  775. */
  776. return xfs_dir2_block_to_sf(args, bp, size, &sfh);
  777. }
  778. /*
  779. * Replace an entry in a V2 block directory.
  780. * Change the inode number to the new value.
  781. */
  782. int /* error */
  783. xfs_dir2_block_replace(
  784. xfs_da_args_t *args) /* directory operation args */
  785. {
  786. xfs_dir2_data_hdr_t *hdr; /* block header */
  787. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  788. struct xfs_buf *bp; /* block buffer */
  789. xfs_dir2_block_tail_t *btp; /* block tail */
  790. xfs_dir2_data_entry_t *dep; /* block data entry */
  791. xfs_inode_t *dp; /* incore inode */
  792. int ent; /* leaf entry index */
  793. int error; /* error return value */
  794. xfs_mount_t *mp; /* filesystem mount point */
  795. trace_xfs_dir2_block_replace(args);
  796. /*
  797. * Lookup the entry in the directory. Get buffer and entry index.
  798. * This will always succeed since the caller has already done a lookup.
  799. */
  800. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
  801. return error;
  802. }
  803. dp = args->dp;
  804. mp = dp->i_mount;
  805. hdr = bp->b_addr;
  806. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  807. blp = xfs_dir2_block_leaf_p(btp);
  808. /*
  809. * Point to the data entry we need to change.
  810. */
  811. dep = (xfs_dir2_data_entry_t *)((char *)hdr +
  812. xfs_dir2_dataptr_to_off(args->geo,
  813. be32_to_cpu(blp[ent].address)));
  814. ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
  815. /*
  816. * Change the inode number to the new value.
  817. */
  818. dep->inumber = cpu_to_be64(args->inumber);
  819. dp->d_ops->data_put_ftype(dep, args->filetype);
  820. xfs_dir2_data_log_entry(args, bp, dep);
  821. xfs_dir3_data_check(dp, bp);
  822. return 0;
  823. }
  824. /*
  825. * Qsort comparison routine for the block leaf entries.
  826. */
  827. static int /* sort order */
  828. xfs_dir2_block_sort(
  829. const void *a, /* first leaf entry */
  830. const void *b) /* second leaf entry */
  831. {
  832. const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
  833. const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
  834. la = a;
  835. lb = b;
  836. return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
  837. (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
  838. }
  839. /*
  840. * Convert a V2 leaf directory to a V2 block directory if possible.
  841. */
  842. int /* error */
  843. xfs_dir2_leaf_to_block(
  844. xfs_da_args_t *args, /* operation arguments */
  845. struct xfs_buf *lbp, /* leaf buffer */
  846. struct xfs_buf *dbp) /* data buffer */
  847. {
  848. __be16 *bestsp; /* leaf bests table */
  849. xfs_dir2_data_hdr_t *hdr; /* block header */
  850. xfs_dir2_block_tail_t *btp; /* block tail */
  851. xfs_inode_t *dp; /* incore directory inode */
  852. xfs_dir2_data_unused_t *dup; /* unused data entry */
  853. int error; /* error return value */
  854. int from; /* leaf from index */
  855. xfs_dir2_leaf_t *leaf; /* leaf structure */
  856. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  857. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  858. xfs_mount_t *mp; /* file system mount point */
  859. int needlog; /* need to log data header */
  860. int needscan; /* need to scan for bestfree */
  861. xfs_dir2_sf_hdr_t sfh; /* shortform header */
  862. int size; /* bytes used */
  863. __be16 *tagp; /* end of entry (tag) */
  864. int to; /* block/leaf to index */
  865. xfs_trans_t *tp; /* transaction pointer */
  866. struct xfs_dir2_leaf_entry *ents;
  867. struct xfs_dir3_icleaf_hdr leafhdr;
  868. trace_xfs_dir2_leaf_to_block(args);
  869. dp = args->dp;
  870. tp = args->trans;
  871. mp = dp->i_mount;
  872. leaf = lbp->b_addr;
  873. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  874. ents = dp->d_ops->leaf_ents_p(leaf);
  875. ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
  876. ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
  877. leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
  878. /*
  879. * If there are data blocks other than the first one, take this
  880. * opportunity to remove trailing empty data blocks that may have
  881. * been left behind during no-space-reservation operations.
  882. * These will show up in the leaf bests table.
  883. */
  884. while (dp->i_d.di_size > args->geo->blksize) {
  885. int hdrsz;
  886. hdrsz = dp->d_ops->data_entry_offset;
  887. bestsp = xfs_dir2_leaf_bests_p(ltp);
  888. if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
  889. args->geo->blksize - hdrsz) {
  890. if ((error =
  891. xfs_dir2_leaf_trim_data(args, lbp,
  892. (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
  893. return error;
  894. } else
  895. return 0;
  896. }
  897. /*
  898. * Read the data block if we don't already have it, give up if it fails.
  899. */
  900. if (!dbp) {
  901. error = xfs_dir3_data_read(tp, dp, args->geo->datablk, -1, &dbp);
  902. if (error)
  903. return error;
  904. }
  905. hdr = dbp->b_addr;
  906. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  907. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
  908. /*
  909. * Size of the "leaf" area in the block.
  910. */
  911. size = (uint)sizeof(xfs_dir2_block_tail_t) +
  912. (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
  913. /*
  914. * Look at the last data entry.
  915. */
  916. tagp = (__be16 *)((char *)hdr + args->geo->blksize) - 1;
  917. dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  918. /*
  919. * If it's not free or is too short we can't do it.
  920. */
  921. if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
  922. be16_to_cpu(dup->length) < size)
  923. return 0;
  924. /*
  925. * Start converting it to block form.
  926. */
  927. xfs_dir3_block_init(mp, tp, dbp, dp);
  928. needlog = 1;
  929. needscan = 0;
  930. /*
  931. * Use up the space at the end of the block (blp/btp).
  932. */
  933. xfs_dir2_data_use_free(args, dbp, dup, args->geo->blksize - size, size,
  934. &needlog, &needscan);
  935. /*
  936. * Initialize the block tail.
  937. */
  938. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  939. btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
  940. btp->stale = 0;
  941. xfs_dir2_block_log_tail(tp, dbp);
  942. /*
  943. * Initialize the block leaf area. We compact out stale entries.
  944. */
  945. lep = xfs_dir2_block_leaf_p(btp);
  946. for (from = to = 0; from < leafhdr.count; from++) {
  947. if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  948. continue;
  949. lep[to++] = ents[from];
  950. }
  951. ASSERT(to == be32_to_cpu(btp->count));
  952. xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
  953. /*
  954. * Scan the bestfree if we need it and log the data block header.
  955. */
  956. if (needscan)
  957. xfs_dir2_data_freescan(dp, hdr, &needlog);
  958. if (needlog)
  959. xfs_dir2_data_log_header(args, dbp);
  960. /*
  961. * Pitch the old leaf block.
  962. */
  963. error = xfs_da_shrink_inode(args, args->geo->leafblk, lbp);
  964. if (error)
  965. return error;
  966. /*
  967. * Now see if the resulting block can be shrunken to shortform.
  968. */
  969. size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
  970. if (size > XFS_IFORK_DSIZE(dp))
  971. return 0;
  972. return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
  973. }
  974. /*
  975. * Convert the shortform directory to block form.
  976. */
  977. int /* error */
  978. xfs_dir2_sf_to_block(
  979. xfs_da_args_t *args) /* operation arguments */
  980. {
  981. xfs_dir2_db_t blkno; /* dir-relative block # (0) */
  982. xfs_dir2_data_hdr_t *hdr; /* block header */
  983. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  984. struct xfs_buf *bp; /* block buffer */
  985. xfs_dir2_block_tail_t *btp; /* block tail pointer */
  986. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  987. xfs_inode_t *dp; /* incore directory inode */
  988. int dummy; /* trash */
  989. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  990. int endoffset; /* end of data objects */
  991. int error; /* error return value */
  992. int i; /* index */
  993. xfs_mount_t *mp; /* filesystem mount point */
  994. int needlog; /* need to log block header */
  995. int needscan; /* need to scan block freespc */
  996. int newoffset; /* offset from current entry */
  997. int offset; /* target block offset */
  998. xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
  999. xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
  1000. xfs_dir2_sf_hdr_t *sfp; /* shortform header */
  1001. __be16 *tagp; /* end of data entry */
  1002. xfs_trans_t *tp; /* transaction pointer */
  1003. struct xfs_name name;
  1004. struct xfs_ifork *ifp;
  1005. trace_xfs_dir2_sf_to_block(args);
  1006. dp = args->dp;
  1007. tp = args->trans;
  1008. mp = dp->i_mount;
  1009. ifp = XFS_IFORK_PTR(dp, XFS_DATA_FORK);
  1010. ASSERT(ifp->if_flags & XFS_IFINLINE);
  1011. /*
  1012. * Bomb out if the shortform directory is way too short.
  1013. */
  1014. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  1015. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1016. return XFS_ERROR(EIO);
  1017. }
  1018. oldsfp = (xfs_dir2_sf_hdr_t *)ifp->if_u1.if_data;
  1019. ASSERT(ifp->if_bytes == dp->i_d.di_size);
  1020. ASSERT(ifp->if_u1.if_data != NULL);
  1021. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
  1022. ASSERT(dp->i_d.di_nextents == 0);
  1023. /*
  1024. * Copy the directory into a temporary buffer.
  1025. * Then pitch the incore inode data so we can make extents.
  1026. */
  1027. sfp = kmem_alloc(ifp->if_bytes, KM_SLEEP);
  1028. memcpy(sfp, oldsfp, ifp->if_bytes);
  1029. xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
  1030. xfs_bmap_local_to_extents_empty(dp, XFS_DATA_FORK);
  1031. dp->i_d.di_size = 0;
  1032. /*
  1033. * Add block 0 to the inode.
  1034. */
  1035. error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
  1036. if (error) {
  1037. kmem_free(sfp);
  1038. return error;
  1039. }
  1040. /*
  1041. * Initialize the data block, then convert it to block format.
  1042. */
  1043. error = xfs_dir3_data_init(args, blkno, &bp);
  1044. if (error) {
  1045. kmem_free(sfp);
  1046. return error;
  1047. }
  1048. xfs_dir3_block_init(mp, tp, bp, dp);
  1049. hdr = bp->b_addr;
  1050. /*
  1051. * Compute size of block "tail" area.
  1052. */
  1053. i = (uint)sizeof(*btp) +
  1054. (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
  1055. /*
  1056. * The whole thing is initialized to free by the init routine.
  1057. * Say we're using the leaf and tail area.
  1058. */
  1059. dup = dp->d_ops->data_unused_p(hdr);
  1060. needlog = needscan = 0;
  1061. xfs_dir2_data_use_free(args, bp, dup, args->geo->blksize - i,
  1062. i, &needlog, &needscan);
  1063. ASSERT(needscan == 0);
  1064. /*
  1065. * Fill in the tail.
  1066. */
  1067. btp = xfs_dir2_block_tail_p(args->geo, hdr);
  1068. btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
  1069. btp->stale = 0;
  1070. blp = xfs_dir2_block_leaf_p(btp);
  1071. endoffset = (uint)((char *)blp - (char *)hdr);
  1072. /*
  1073. * Remove the freespace, we'll manage it.
  1074. */
  1075. xfs_dir2_data_use_free(args, bp, dup,
  1076. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
  1077. be16_to_cpu(dup->length), &needlog, &needscan);
  1078. /*
  1079. * Create entry for .
  1080. */
  1081. dep = dp->d_ops->data_dot_entry_p(hdr);
  1082. dep->inumber = cpu_to_be64(dp->i_ino);
  1083. dep->namelen = 1;
  1084. dep->name[0] = '.';
  1085. dp->d_ops->data_put_ftype(dep, XFS_DIR3_FT_DIR);
  1086. tagp = dp->d_ops->data_entry_tag_p(dep);
  1087. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1088. xfs_dir2_data_log_entry(args, bp, dep);
  1089. blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
  1090. blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
  1091. (char *)dep - (char *)hdr));
  1092. /*
  1093. * Create entry for ..
  1094. */
  1095. dep = dp->d_ops->data_dotdot_entry_p(hdr);
  1096. dep->inumber = cpu_to_be64(dp->d_ops->sf_get_parent_ino(sfp));
  1097. dep->namelen = 2;
  1098. dep->name[0] = dep->name[1] = '.';
  1099. dp->d_ops->data_put_ftype(dep, XFS_DIR3_FT_DIR);
  1100. tagp = dp->d_ops->data_entry_tag_p(dep);
  1101. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1102. xfs_dir2_data_log_entry(args, bp, dep);
  1103. blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
  1104. blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
  1105. (char *)dep - (char *)hdr));
  1106. offset = dp->d_ops->data_first_offset;
  1107. /*
  1108. * Loop over existing entries, stuff them in.
  1109. */
  1110. i = 0;
  1111. if (!sfp->count)
  1112. sfep = NULL;
  1113. else
  1114. sfep = xfs_dir2_sf_firstentry(sfp);
  1115. /*
  1116. * Need to preserve the existing offset values in the sf directory.
  1117. * Insert holes (unused entries) where necessary.
  1118. */
  1119. while (offset < endoffset) {
  1120. /*
  1121. * sfep is null when we reach the end of the list.
  1122. */
  1123. if (sfep == NULL)
  1124. newoffset = endoffset;
  1125. else
  1126. newoffset = xfs_dir2_sf_get_offset(sfep);
  1127. /*
  1128. * There should be a hole here, make one.
  1129. */
  1130. if (offset < newoffset) {
  1131. dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  1132. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  1133. dup->length = cpu_to_be16(newoffset - offset);
  1134. *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
  1135. ((char *)dup - (char *)hdr));
  1136. xfs_dir2_data_log_unused(args, bp, dup);
  1137. xfs_dir2_data_freeinsert(hdr,
  1138. dp->d_ops->data_bestfree_p(hdr),
  1139. dup, &dummy);
  1140. offset += be16_to_cpu(dup->length);
  1141. continue;
  1142. }
  1143. /*
  1144. * Copy a real entry.
  1145. */
  1146. dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
  1147. dep->inumber = cpu_to_be64(dp->d_ops->sf_get_ino(sfp, sfep));
  1148. dep->namelen = sfep->namelen;
  1149. dp->d_ops->data_put_ftype(dep, dp->d_ops->sf_get_ftype(sfep));
  1150. memcpy(dep->name, sfep->name, dep->namelen);
  1151. tagp = dp->d_ops->data_entry_tag_p(dep);
  1152. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1153. xfs_dir2_data_log_entry(args, bp, dep);
  1154. name.name = sfep->name;
  1155. name.len = sfep->namelen;
  1156. blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
  1157. hashname(&name));
  1158. blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
  1159. (char *)dep - (char *)hdr));
  1160. offset = (int)((char *)(tagp + 1) - (char *)hdr);
  1161. if (++i == sfp->count)
  1162. sfep = NULL;
  1163. else
  1164. sfep = dp->d_ops->sf_nextentry(sfp, sfep);
  1165. }
  1166. /* Done with the temporary buffer */
  1167. kmem_free(sfp);
  1168. /*
  1169. * Sort the leaf entries by hash value.
  1170. */
  1171. xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
  1172. /*
  1173. * Log the leaf entry area and tail.
  1174. * Already logged the header in data_init, ignore needlog.
  1175. */
  1176. ASSERT(needscan == 0);
  1177. xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
  1178. xfs_dir2_block_log_tail(tp, bp);
  1179. xfs_dir3_data_check(dp, bp);
  1180. return 0;
  1181. }