xfs_dir2_block.c 35 KB

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