xfs_dir2_block.c 35 KB

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