xfs_dir2_block.c 35 KB

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