xfs_dir2_block.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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. xfs_dir2_data_off_t *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(INT_GET(block->hdr.magic, ARCH_CONVERT)
  101. != XFS_DIR2_BLOCK_MAGIC)) {
  102. XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
  103. XFS_ERRLEVEL_LOW, mp, block);
  104. xfs_da_brelse(tp, bp);
  105. return XFS_ERROR(EFSCORRUPTED);
  106. }
  107. len = XFS_DIR2_DATA_ENTSIZE(args->namelen);
  108. /*
  109. * Set up pointers to parts of the block.
  110. */
  111. bf = block->hdr.bestfree;
  112. btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
  113. blp = XFS_DIR2_BLOCK_LEAF_P(btp);
  114. /*
  115. * No stale entries? Need space for entry and new leaf.
  116. */
  117. if (!btp->stale) {
  118. /*
  119. * Tag just before the first leaf entry.
  120. */
  121. tagp = (xfs_dir2_data_off_t *)blp - 1;
  122. /*
  123. * Data object just before the first leaf entry.
  124. */
  125. enddup = (xfs_dir2_data_unused_t *)((char *)block + INT_GET(*tagp, ARCH_CONVERT));
  126. /*
  127. * If it's not free then can't do this add without cleaning up:
  128. * the space before the first leaf entry needs to be free so it
  129. * can be expanded to hold the pointer to the new entry.
  130. */
  131. if (INT_GET(enddup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG)
  132. dup = enddup = NULL;
  133. /*
  134. * Check out the biggest freespace and see if it's the same one.
  135. */
  136. else {
  137. dup = (xfs_dir2_data_unused_t *)
  138. ((char *)block + INT_GET(bf[0].offset, ARCH_CONVERT));
  139. if (dup == enddup) {
  140. /*
  141. * It is the biggest freespace, is it too small
  142. * to hold the new leaf too?
  143. */
  144. if (INT_GET(dup->length, ARCH_CONVERT) < len + (uint)sizeof(*blp)) {
  145. /*
  146. * Yes, we use the second-largest
  147. * entry instead if it works.
  148. */
  149. if (INT_GET(bf[1].length, ARCH_CONVERT) >= len)
  150. dup = (xfs_dir2_data_unused_t *)
  151. ((char *)block +
  152. INT_GET(bf[1].offset, ARCH_CONVERT));
  153. else
  154. dup = NULL;
  155. }
  156. } else {
  157. /*
  158. * Not the same free entry,
  159. * just check its length.
  160. */
  161. if (INT_GET(dup->length, ARCH_CONVERT) < len) {
  162. dup = NULL;
  163. }
  164. }
  165. }
  166. compact = 0;
  167. }
  168. /*
  169. * If there are stale entries we'll use one for the leaf.
  170. * Is the biggest entry enough to avoid compaction?
  171. */
  172. else if (INT_GET(bf[0].length, ARCH_CONVERT) >= len) {
  173. dup = (xfs_dir2_data_unused_t *)
  174. ((char *)block + INT_GET(bf[0].offset, ARCH_CONVERT));
  175. compact = 0;
  176. }
  177. /*
  178. * Will need to compact to make this work.
  179. */
  180. else {
  181. /*
  182. * Tag just before the first leaf entry.
  183. */
  184. tagp = (xfs_dir2_data_off_t *)blp - 1;
  185. /*
  186. * Data object just before the first leaf entry.
  187. */
  188. dup = (xfs_dir2_data_unused_t *)((char *)block + INT_GET(*tagp, ARCH_CONVERT));
  189. /*
  190. * If it's not free then the data will go where the
  191. * leaf data starts now, if it works at all.
  192. */
  193. if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
  194. if (INT_GET(dup->length, ARCH_CONVERT) + (INT_GET(btp->stale, ARCH_CONVERT) - 1) *
  195. (uint)sizeof(*blp) < len)
  196. dup = NULL;
  197. } else if ((INT_GET(btp->stale, ARCH_CONVERT) - 1) * (uint)sizeof(*blp) < len)
  198. dup = NULL;
  199. else
  200. dup = (xfs_dir2_data_unused_t *)blp;
  201. compact = 1;
  202. }
  203. /*
  204. * If this isn't a real add, we're done with the buffer.
  205. */
  206. if (args->justcheck)
  207. xfs_da_brelse(tp, bp);
  208. /*
  209. * If we don't have space for the new entry & leaf ...
  210. */
  211. if (!dup) {
  212. /*
  213. * Not trying to actually do anything, or don't have
  214. * a space reservation: return no-space.
  215. */
  216. if (args->justcheck || args->total == 0)
  217. return XFS_ERROR(ENOSPC);
  218. /*
  219. * Convert to the next larger format.
  220. * Then add the new entry in that format.
  221. */
  222. error = xfs_dir2_block_to_leaf(args, bp);
  223. xfs_da_buf_done(bp);
  224. if (error)
  225. return error;
  226. return xfs_dir2_leaf_addname(args);
  227. }
  228. /*
  229. * Just checking, and it would work, so say so.
  230. */
  231. if (args->justcheck)
  232. return 0;
  233. needlog = needscan = 0;
  234. /*
  235. * If need to compact the leaf entries, do it now.
  236. * Leave the highest-numbered stale entry stale.
  237. * XXX should be the one closest to mid but mid is not yet computed.
  238. */
  239. if (compact) {
  240. int fromidx; /* source leaf index */
  241. int toidx; /* target leaf index */
  242. for (fromidx = toidx = INT_GET(btp->count, ARCH_CONVERT) - 1,
  243. highstale = lfloghigh = -1;
  244. fromidx >= 0;
  245. fromidx--) {
  246. if (INT_GET(blp[fromidx].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR) {
  247. if (highstale == -1)
  248. highstale = toidx;
  249. else {
  250. if (lfloghigh == -1)
  251. lfloghigh = toidx;
  252. continue;
  253. }
  254. }
  255. if (fromidx < toidx)
  256. blp[toidx] = blp[fromidx];
  257. toidx--;
  258. }
  259. lfloglow = toidx + 1 - (INT_GET(btp->stale, ARCH_CONVERT) - 1);
  260. lfloghigh -= INT_GET(btp->stale, ARCH_CONVERT) - 1;
  261. INT_MOD(btp->count, ARCH_CONVERT, -(INT_GET(btp->stale, ARCH_CONVERT) - 1));
  262. xfs_dir2_data_make_free(tp, bp,
  263. (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
  264. (xfs_dir2_data_aoff_t)((INT_GET(btp->stale, ARCH_CONVERT) - 1) * sizeof(*blp)),
  265. &needlog, &needscan);
  266. blp += INT_GET(btp->stale, ARCH_CONVERT) - 1;
  267. INT_SET(btp->stale, ARCH_CONVERT, 1);
  268. /*
  269. * If we now need to rebuild the bestfree map, do so.
  270. * This needs to happen before the next call to use_free.
  271. */
  272. if (needscan) {
  273. xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
  274. &needlog, NULL);
  275. needscan = 0;
  276. }
  277. }
  278. /*
  279. * Set leaf logging boundaries to impossible state.
  280. * For the no-stale case they're set explicitly.
  281. */
  282. else if (INT_GET(btp->stale, ARCH_CONVERT)) {
  283. lfloglow = INT_GET(btp->count, ARCH_CONVERT);
  284. lfloghigh = -1;
  285. }
  286. /*
  287. * Find the slot that's first lower than our hash value, -1 if none.
  288. */
  289. for (low = 0, high = INT_GET(btp->count, ARCH_CONVERT) - 1; low <= high; ) {
  290. mid = (low + high) >> 1;
  291. if ((hash = INT_GET(blp[mid].hashval, ARCH_CONVERT)) == args->hashval)
  292. break;
  293. if (hash < args->hashval)
  294. low = mid + 1;
  295. else
  296. high = mid - 1;
  297. }
  298. while (mid >= 0 && INT_GET(blp[mid].hashval, ARCH_CONVERT) >= args->hashval) {
  299. mid--;
  300. }
  301. /*
  302. * No stale entries, will use enddup space to hold new leaf.
  303. */
  304. if (!btp->stale) {
  305. /*
  306. * Mark the space needed for the new leaf entry, now in use.
  307. */
  308. xfs_dir2_data_use_free(tp, bp, enddup,
  309. (xfs_dir2_data_aoff_t)
  310. ((char *)enddup - (char *)block + INT_GET(enddup->length, ARCH_CONVERT) -
  311. sizeof(*blp)),
  312. (xfs_dir2_data_aoff_t)sizeof(*blp),
  313. &needlog, &needscan);
  314. /*
  315. * Update the tail (entry count).
  316. */
  317. INT_MOD(btp->count, ARCH_CONVERT, +1);
  318. /*
  319. * If we now need to rebuild the bestfree map, do so.
  320. * This needs to happen before the next call to use_free.
  321. */
  322. if (needscan) {
  323. xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
  324. &needlog, NULL);
  325. needscan = 0;
  326. }
  327. /*
  328. * Adjust pointer to the first leaf entry, we're about to move
  329. * the table up one to open up space for the new leaf entry.
  330. * Then adjust our index to match.
  331. */
  332. blp--;
  333. mid++;
  334. if (mid)
  335. memmove(blp, &blp[1], mid * sizeof(*blp));
  336. lfloglow = 0;
  337. lfloghigh = mid;
  338. }
  339. /*
  340. * Use a stale leaf for our new entry.
  341. */
  342. else {
  343. for (lowstale = mid;
  344. lowstale >= 0 &&
  345. INT_GET(blp[lowstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR;
  346. lowstale--)
  347. continue;
  348. for (highstale = mid + 1;
  349. highstale < INT_GET(btp->count, ARCH_CONVERT) &&
  350. INT_GET(blp[highstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR &&
  351. (lowstale < 0 || mid - lowstale > highstale - mid);
  352. highstale++)
  353. continue;
  354. /*
  355. * Move entries toward the low-numbered stale entry.
  356. */
  357. if (lowstale >= 0 &&
  358. (highstale == INT_GET(btp->count, ARCH_CONVERT) ||
  359. mid - lowstale <= highstale - mid)) {
  360. if (mid - lowstale)
  361. memmove(&blp[lowstale], &blp[lowstale + 1],
  362. (mid - lowstale) * sizeof(*blp));
  363. lfloglow = MIN(lowstale, lfloglow);
  364. lfloghigh = MAX(mid, lfloghigh);
  365. }
  366. /*
  367. * Move entries toward the high-numbered stale entry.
  368. */
  369. else {
  370. ASSERT(highstale < INT_GET(btp->count, ARCH_CONVERT));
  371. mid++;
  372. if (highstale - mid)
  373. memmove(&blp[mid + 1], &blp[mid],
  374. (highstale - mid) * sizeof(*blp));
  375. lfloglow = MIN(mid, lfloglow);
  376. lfloghigh = MAX(highstale, lfloghigh);
  377. }
  378. INT_MOD(btp->stale, ARCH_CONVERT, -1);
  379. }
  380. /*
  381. * Point to the new data entry.
  382. */
  383. dep = (xfs_dir2_data_entry_t *)dup;
  384. /*
  385. * Fill in the leaf entry.
  386. */
  387. INT_SET(blp[mid].hashval, ARCH_CONVERT, args->hashval);
  388. INT_SET(blp[mid].address, ARCH_CONVERT, XFS_DIR2_BYTE_TO_DATAPTR(mp, (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. INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((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 (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
  483. ptr += INT_GET(dup->length, ARCH_CONVERT);
  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 nonexistent 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, INT_GET(blp[ent].address, ARCH_CONVERT)));
  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 = INT_GET(btp->count, ARCH_CONVERT) - 1; ; ) {
  655. ASSERT(low <= high);
  656. mid = (low + high) >> 1;
  657. if ((hash = INT_GET(blp[mid].hashval, ARCH_CONVERT)) == 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 && INT_GET(blp[mid - 1].hashval, ARCH_CONVERT) == 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 = INT_GET(blp[mid].address, ARCH_CONVERT)) == 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 < INT_GET(btp->count, ARCH_CONVERT) && INT_GET(blp[mid].hashval, ARCH_CONVERT) == 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, INT_GET(blp[ent].address, ARCH_CONVERT)));
  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. INT_MOD(btp->stale, ARCH_CONVERT, +1);
  757. xfs_dir2_block_log_tail(tp, bp);
  758. /*
  759. * Remove the leaf entry by marking it stale.
  760. */
  761. INT_SET(blp[ent].address, ARCH_CONVERT, 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, INT_GET(blp[ent].address, ARCH_CONVERT)));
  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 INT_GET(la->hashval, ARCH_CONVERT) < INT_GET(lb->hashval, ARCH_CONVERT) ? -1 :
  843. (INT_GET(la->hashval, ARCH_CONVERT) > INT_GET(lb->hashval, ARCH_CONVERT) ? 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. xfs_dir2_data_off_t *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. xfs_dir2_data_off_t *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(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == 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 (INT_GET(bestsp[INT_GET(ltp->bestcount, ARCH_CONVERT) - 1], ARCH_CONVERT) ==
  888. mp->m_dirblksize - (uint)sizeof(block->hdr)) {
  889. if ((error =
  890. xfs_dir2_leaf_trim_data(args, lbp,
  891. (xfs_dir2_db_t)(INT_GET(ltp->bestcount, ARCH_CONVERT) - 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(INT_GET(block->hdr.magic, ARCH_CONVERT) == 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) * (INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT));
  913. /*
  914. * Look at the last data entry.
  915. */
  916. tagp = (xfs_dir2_data_off_t *)((char *)block + mp->m_dirblksize) - 1;
  917. dup = (xfs_dir2_data_unused_t *)((char *)block + INT_GET(*tagp, ARCH_CONVERT));
  918. /*
  919. * If it's not free or is too short we can't do it.
  920. */
  921. if (INT_GET(dup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG || INT_GET(dup->length, ARCH_CONVERT) < size) {
  922. error = 0;
  923. goto out;
  924. }
  925. /*
  926. * Start converting it to block form.
  927. */
  928. INT_SET(block->hdr.magic, ARCH_CONVERT, XFS_DIR2_BLOCK_MAGIC);
  929. needlog = 1;
  930. needscan = 0;
  931. /*
  932. * Use up the space at the end of the block (blp/btp).
  933. */
  934. xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
  935. &needlog, &needscan);
  936. /*
  937. * Initialize the block tail.
  938. */
  939. btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
  940. INT_SET(btp->count, ARCH_CONVERT, INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT));
  941. btp->stale = 0;
  942. xfs_dir2_block_log_tail(tp, dbp);
  943. /*
  944. * Initialize the block leaf area. We compact out stale entries.
  945. */
  946. lep = XFS_DIR2_BLOCK_LEAF_P(btp);
  947. for (from = to = 0; from < INT_GET(leaf->hdr.count, ARCH_CONVERT); from++) {
  948. if (INT_GET(leaf->ents[from].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
  949. continue;
  950. lep[to++] = leaf->ents[from];
  951. }
  952. ASSERT(to == INT_GET(btp->count, ARCH_CONVERT));
  953. xfs_dir2_block_log_leaf(tp, dbp, 0, INT_GET(btp->count, ARCH_CONVERT) - 1);
  954. /*
  955. * Scan the bestfree if we need it and log the data block header.
  956. */
  957. if (needscan)
  958. xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
  959. NULL);
  960. if (needlog)
  961. xfs_dir2_data_log_header(tp, dbp);
  962. /*
  963. * Pitch the old leaf block.
  964. */
  965. error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
  966. lbp = NULL;
  967. if (error) {
  968. goto out;
  969. }
  970. /*
  971. * Now see if the resulting block can be shrunken to shortform.
  972. */
  973. if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
  974. XFS_IFORK_DSIZE(dp)) {
  975. error = 0;
  976. goto out;
  977. }
  978. return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
  979. out:
  980. if (lbp)
  981. xfs_da_buf_done(lbp);
  982. if (dbp)
  983. xfs_da_buf_done(dbp);
  984. return error;
  985. }
  986. /*
  987. * Convert the shortform directory to block form.
  988. */
  989. int /* error */
  990. xfs_dir2_sf_to_block(
  991. xfs_da_args_t *args) /* operation arguments */
  992. {
  993. xfs_dir2_db_t blkno; /* dir-relative block # (0) */
  994. xfs_dir2_block_t *block; /* block structure */
  995. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  996. xfs_dabuf_t *bp; /* block buffer */
  997. xfs_dir2_block_tail_t *btp; /* block tail pointer */
  998. char *buf; /* sf buffer */
  999. int buf_len;
  1000. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  1001. xfs_inode_t *dp; /* incore directory inode */
  1002. int dummy; /* trash */
  1003. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  1004. int endoffset; /* end of data objects */
  1005. int error; /* error return value */
  1006. int i; /* index */
  1007. xfs_mount_t *mp; /* filesystem mount point */
  1008. int needlog; /* need to log block header */
  1009. int needscan; /* need to scan block freespc */
  1010. int newoffset; /* offset from current entry */
  1011. int offset; /* target block offset */
  1012. xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
  1013. xfs_dir2_sf_t *sfp; /* shortform structure */
  1014. xfs_dir2_data_off_t *tagp; /* end of data entry */
  1015. xfs_trans_t *tp; /* transaction pointer */
  1016. xfs_dir2_trace_args("sf_to_block", args);
  1017. dp = args->dp;
  1018. tp = args->trans;
  1019. mp = dp->i_mount;
  1020. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  1021. /*
  1022. * Bomb out if the shortform directory is way too short.
  1023. */
  1024. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  1025. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1026. return XFS_ERROR(EIO);
  1027. }
  1028. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  1029. ASSERT(dp->i_df.if_u1.if_data != NULL);
  1030. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  1031. ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
  1032. /*
  1033. * Copy the directory into the stack buffer.
  1034. * Then pitch the incore inode data so we can make extents.
  1035. */
  1036. buf_len = dp->i_df.if_bytes;
  1037. buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
  1038. memcpy(buf, sfp, dp->i_df.if_bytes);
  1039. xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
  1040. dp->i_d.di_size = 0;
  1041. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1042. /*
  1043. * Reset pointer - old sfp is gone.
  1044. */
  1045. sfp = (xfs_dir2_sf_t *)buf;
  1046. /*
  1047. * Add block 0 to the inode.
  1048. */
  1049. error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
  1050. if (error) {
  1051. kmem_free(buf, buf_len);
  1052. return error;
  1053. }
  1054. /*
  1055. * Initialize the data block.
  1056. */
  1057. error = xfs_dir2_data_init(args, blkno, &bp);
  1058. if (error) {
  1059. kmem_free(buf, buf_len);
  1060. return error;
  1061. }
  1062. block = bp->data;
  1063. INT_SET(block->hdr.magic, ARCH_CONVERT, XFS_DIR2_BLOCK_MAGIC);
  1064. /*
  1065. * Compute size of block "tail" area.
  1066. */
  1067. i = (uint)sizeof(*btp) +
  1068. (INT_GET(sfp->hdr.count, ARCH_CONVERT) + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
  1069. /*
  1070. * The whole thing is initialized to free by the init routine.
  1071. * Say we're using the leaf and tail area.
  1072. */
  1073. dup = (xfs_dir2_data_unused_t *)block->u;
  1074. needlog = needscan = 0;
  1075. xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
  1076. &needscan);
  1077. ASSERT(needscan == 0);
  1078. /*
  1079. * Fill in the tail.
  1080. */
  1081. btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
  1082. INT_SET(btp->count, ARCH_CONVERT, INT_GET(sfp->hdr.count, ARCH_CONVERT) + 2); /* ., .. */
  1083. btp->stale = 0;
  1084. blp = XFS_DIR2_BLOCK_LEAF_P(btp);
  1085. endoffset = (uint)((char *)blp - (char *)block);
  1086. /*
  1087. * Remove the freespace, we'll manage it.
  1088. */
  1089. xfs_dir2_data_use_free(tp, bp, dup,
  1090. (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
  1091. INT_GET(dup->length, ARCH_CONVERT), &needlog, &needscan);
  1092. /*
  1093. * Create entry for .
  1094. */
  1095. dep = (xfs_dir2_data_entry_t *)
  1096. ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
  1097. INT_SET(dep->inumber, ARCH_CONVERT, dp->i_ino);
  1098. dep->namelen = 1;
  1099. dep->name[0] = '.';
  1100. tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
  1101. INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)block));
  1102. xfs_dir2_data_log_entry(tp, bp, dep);
  1103. INT_SET(blp[0].hashval, ARCH_CONVERT, xfs_dir_hash_dot);
  1104. INT_SET(blp[0].address, ARCH_CONVERT, XFS_DIR2_BYTE_TO_DATAPTR(mp, (char *)dep - (char *)block));
  1105. /*
  1106. * Create entry for ..
  1107. */
  1108. dep = (xfs_dir2_data_entry_t *)
  1109. ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
  1110. INT_SET(dep->inumber, ARCH_CONVERT, XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent));
  1111. dep->namelen = 2;
  1112. dep->name[0] = dep->name[1] = '.';
  1113. tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
  1114. INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)block));
  1115. xfs_dir2_data_log_entry(tp, bp, dep);
  1116. INT_SET(blp[1].hashval, ARCH_CONVERT, xfs_dir_hash_dotdot);
  1117. INT_SET(blp[1].address, ARCH_CONVERT, XFS_DIR2_BYTE_TO_DATAPTR(mp, (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) == INT_GET(sfp->hdr.count, ARCH_CONVERT))
  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. INT_SET(dup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
  1145. INT_SET(dup->length, ARCH_CONVERT, newoffset - offset);
  1146. INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT,
  1147. (xfs_dir2_data_off_t)
  1148. ((char *)dup - (char *)block));
  1149. xfs_dir2_data_log_unused(tp, bp, dup);
  1150. (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
  1151. dup, &dummy);
  1152. offset += INT_GET(dup->length, ARCH_CONVERT);
  1153. continue;
  1154. }
  1155. /*
  1156. * Copy a real entry.
  1157. */
  1158. dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
  1159. INT_SET(dep->inumber, ARCH_CONVERT, XFS_DIR2_SF_GET_INUMBER(sfp,
  1160. XFS_DIR2_SF_INUMBERP(sfep)));
  1161. dep->namelen = sfep->namelen;
  1162. memcpy(dep->name, sfep->name, dep->namelen);
  1163. tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
  1164. INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)block));
  1165. xfs_dir2_data_log_entry(tp, bp, dep);
  1166. INT_SET(blp[2 + i].hashval, ARCH_CONVERT, xfs_da_hashname((char *)sfep->name, sfep->namelen));
  1167. INT_SET(blp[2 + i].address, ARCH_CONVERT, XFS_DIR2_BYTE_TO_DATAPTR(mp,
  1168. (char *)dep - (char *)block));
  1169. offset = (int)((char *)(tagp + 1) - (char *)block);
  1170. if (++i == INT_GET(sfp->hdr.count, ARCH_CONVERT))
  1171. sfep = NULL;
  1172. else
  1173. sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
  1174. }
  1175. /* Done with the temporary buffer */
  1176. kmem_free(buf, buf_len);
  1177. /*
  1178. * Sort the leaf entries by hash value.
  1179. */
  1180. xfs_sort(blp, INT_GET(btp->count, ARCH_CONVERT), sizeof(*blp), xfs_dir2_block_sort);
  1181. /*
  1182. * Log the leaf entry area and tail.
  1183. * Already logged the header in data_init, ignore needlog.
  1184. */
  1185. ASSERT(needscan == 0);
  1186. xfs_dir2_block_log_leaf(tp, bp, 0, INT_GET(btp->count, ARCH_CONVERT) - 1);
  1187. xfs_dir2_block_log_tail(tp, bp);
  1188. xfs_dir2_data_check(dp, bp);
  1189. xfs_da_buf_done(bp);
  1190. return 0;
  1191. }