xfs_dir2_block.c 37 KB

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