xfs_dir2_block.c 35 KB

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