xfs_dir2_block.c 35 KB

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