xfs_dir2_block.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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, &needlog);
  280. needscan = 0;
  281. }
  282. }
  283. /*
  284. * Set leaf logging boundaries to impossible state.
  285. * For the no-stale case they're set explicitly.
  286. */
  287. else if (btp->stale) {
  288. lfloglow = be32_to_cpu(btp->count);
  289. lfloghigh = -1;
  290. }
  291. /*
  292. * Find the slot that's first lower than our hash value, -1 if none.
  293. */
  294. for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
  295. mid = (low + high) >> 1;
  296. if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
  297. break;
  298. if (hash < args->hashval)
  299. low = mid + 1;
  300. else
  301. high = mid - 1;
  302. }
  303. while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
  304. mid--;
  305. }
  306. /*
  307. * No stale entries, will use enddup space to hold new leaf.
  308. */
  309. if (!btp->stale) {
  310. /*
  311. * Mark the space needed for the new leaf entry, now in use.
  312. */
  313. xfs_dir2_data_use_free(tp, bp, enddup,
  314. (xfs_dir2_data_aoff_t)
  315. ((char *)enddup - (char *)block + be16_to_cpu(enddup->length) -
  316. sizeof(*blp)),
  317. (xfs_dir2_data_aoff_t)sizeof(*blp),
  318. &needlog, &needscan);
  319. /*
  320. * Update the tail (entry count).
  321. */
  322. be32_add(&btp->count, 1);
  323. /*
  324. * If we now need to rebuild the bestfree map, do so.
  325. * This needs to happen before the next call to use_free.
  326. */
  327. if (needscan) {
  328. xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
  329. &needlog);
  330. needscan = 0;
  331. }
  332. /*
  333. * Adjust pointer to the first leaf entry, we're about to move
  334. * the table up one to open up space for the new leaf entry.
  335. * Then adjust our index to match.
  336. */
  337. blp--;
  338. mid++;
  339. if (mid)
  340. memmove(blp, &blp[1], mid * sizeof(*blp));
  341. lfloglow = 0;
  342. lfloghigh = mid;
  343. }
  344. /*
  345. * Use a stale leaf for our new entry.
  346. */
  347. else {
  348. for (lowstale = mid;
  349. lowstale >= 0 &&
  350. be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
  351. lowstale--)
  352. continue;
  353. for (highstale = mid + 1;
  354. highstale < be32_to_cpu(btp->count) &&
  355. be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
  356. (lowstale < 0 || mid - lowstale > highstale - mid);
  357. highstale++)
  358. continue;
  359. /*
  360. * Move entries toward the low-numbered stale entry.
  361. */
  362. if (lowstale >= 0 &&
  363. (highstale == be32_to_cpu(btp->count) ||
  364. mid - lowstale <= highstale - mid)) {
  365. if (mid - lowstale)
  366. memmove(&blp[lowstale], &blp[lowstale + 1],
  367. (mid - lowstale) * sizeof(*blp));
  368. lfloglow = MIN(lowstale, lfloglow);
  369. lfloghigh = MAX(mid, lfloghigh);
  370. }
  371. /*
  372. * Move entries toward the high-numbered stale entry.
  373. */
  374. else {
  375. ASSERT(highstale < be32_to_cpu(btp->count));
  376. mid++;
  377. if (highstale - mid)
  378. memmove(&blp[mid + 1], &blp[mid],
  379. (highstale - mid) * sizeof(*blp));
  380. lfloglow = MIN(mid, lfloglow);
  381. lfloghigh = MAX(highstale, lfloghigh);
  382. }
  383. be32_add(&btp->stale, -1);
  384. }
  385. /*
  386. * Point to the new data entry.
  387. */
  388. dep = (xfs_dir2_data_entry_t *)dup;
  389. /*
  390. * Fill in the leaf entry.
  391. */
  392. blp[mid].hashval = cpu_to_be32(args->hashval);
  393. blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
  394. (char *)dep - (char *)block));
  395. xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
  396. /*
  397. * Mark space for the data entry used.
  398. */
  399. xfs_dir2_data_use_free(tp, bp, dup,
  400. (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
  401. (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
  402. /*
  403. * Create the new data entry.
  404. */
  405. dep->inumber = cpu_to_be64(args->inumber);
  406. dep->namelen = args->namelen;
  407. memcpy(dep->name, args->name, args->namelen);
  408. tagp = xfs_dir2_data_entry_tag_p(dep);
  409. *tagp = cpu_to_be16((char *)dep - (char *)block);
  410. /*
  411. * Clean up the bestfree array and log the header, tail, and entry.
  412. */
  413. if (needscan)
  414. xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
  415. if (needlog)
  416. xfs_dir2_data_log_header(tp, bp);
  417. xfs_dir2_block_log_tail(tp, bp);
  418. xfs_dir2_data_log_entry(tp, bp, dep);
  419. xfs_dir2_data_check(dp, bp);
  420. xfs_da_buf_done(bp);
  421. return 0;
  422. }
  423. /*
  424. * Readdir for block directories.
  425. */
  426. int /* error */
  427. xfs_dir2_block_getdents(
  428. xfs_inode_t *dp, /* incore inode */
  429. void *dirent,
  430. xfs_off_t *offset,
  431. filldir_t filldir)
  432. {
  433. xfs_dir2_block_t *block; /* directory block structure */
  434. xfs_dabuf_t *bp; /* buffer for block */
  435. xfs_dir2_block_tail_t *btp; /* block tail */
  436. xfs_dir2_data_entry_t *dep; /* block data entry */
  437. xfs_dir2_data_unused_t *dup; /* block unused entry */
  438. char *endptr; /* end of the data entries */
  439. int error; /* error return value */
  440. xfs_mount_t *mp; /* filesystem mount point */
  441. char *ptr; /* current data entry */
  442. int wantoff; /* starting block offset */
  443. xfs_ino_t ino;
  444. xfs_off_t cook;
  445. mp = dp->i_mount;
  446. /*
  447. * If the block number in the offset is out of range, we're done.
  448. */
  449. if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) {
  450. return 0;
  451. }
  452. /*
  453. * Can't read the block, give up, else get dabuf in bp.
  454. */
  455. error = xfs_da_read_buf(NULL, dp, mp->m_dirdatablk, -1,
  456. &bp, XFS_DATA_FORK);
  457. if (error)
  458. return error;
  459. ASSERT(bp != NULL);
  460. /*
  461. * Extract the byte offset we start at from the seek pointer.
  462. * We'll skip entries before this.
  463. */
  464. wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
  465. block = bp->data;
  466. xfs_dir2_data_check(dp, bp);
  467. /*
  468. * Set up values for the loop.
  469. */
  470. btp = xfs_dir2_block_tail_p(mp, block);
  471. ptr = (char *)block->u;
  472. endptr = (char *)xfs_dir2_block_leaf_p(btp);
  473. /*
  474. * Loop over the data portion of the block.
  475. * Each object is a real entry (dep) or an unused one (dup).
  476. */
  477. while (ptr < endptr) {
  478. dup = (xfs_dir2_data_unused_t *)ptr;
  479. /*
  480. * Unused, skip it.
  481. */
  482. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  483. ptr += be16_to_cpu(dup->length);
  484. continue;
  485. }
  486. dep = (xfs_dir2_data_entry_t *)ptr;
  487. /*
  488. * Bump pointer for the next iteration.
  489. */
  490. ptr += xfs_dir2_data_entsize(dep->namelen);
  491. /*
  492. * The entry is before the desired starting point, skip it.
  493. */
  494. if ((char *)dep - (char *)block < wantoff)
  495. continue;
  496. cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  497. ptr - (char *)block);
  498. ino = be64_to_cpu(dep->inumber);
  499. #if XFS_BIG_INUMS
  500. ino += mp->m_inoadd;
  501. #endif
  502. /*
  503. * If it didn't fit, set the final offset to here & return.
  504. */
  505. if (filldir(dirent, dep->name, dep->namelen, cook,
  506. ino, DT_UNKNOWN)) {
  507. *offset = xfs_dir2_db_off_to_dataptr(mp,
  508. mp->m_dirdatablk,
  509. (char *)dep - (char *)block);
  510. xfs_da_brelse(NULL, bp);
  511. return 0;
  512. }
  513. }
  514. /*
  515. * Reached the end of the block.
  516. * Set the offset to a non-existent block 1 and return.
  517. */
  518. *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0);
  519. xfs_da_brelse(NULL, bp);
  520. return 0;
  521. }
  522. /*
  523. * Log leaf entries from the block.
  524. */
  525. static void
  526. xfs_dir2_block_log_leaf(
  527. xfs_trans_t *tp, /* transaction structure */
  528. xfs_dabuf_t *bp, /* block buffer */
  529. int first, /* index of first logged leaf */
  530. int last) /* index of last logged leaf */
  531. {
  532. xfs_dir2_block_t *block; /* directory block structure */
  533. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  534. xfs_dir2_block_tail_t *btp; /* block tail */
  535. xfs_mount_t *mp; /* filesystem mount point */
  536. mp = tp->t_mountp;
  537. block = bp->data;
  538. btp = xfs_dir2_block_tail_p(mp, block);
  539. blp = xfs_dir2_block_leaf_p(btp);
  540. xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block),
  541. (uint)((char *)&blp[last + 1] - (char *)block - 1));
  542. }
  543. /*
  544. * Log the block tail.
  545. */
  546. static void
  547. xfs_dir2_block_log_tail(
  548. xfs_trans_t *tp, /* transaction structure */
  549. xfs_dabuf_t *bp) /* block buffer */
  550. {
  551. xfs_dir2_block_t *block; /* directory block structure */
  552. xfs_dir2_block_tail_t *btp; /* block tail */
  553. xfs_mount_t *mp; /* filesystem mount point */
  554. mp = tp->t_mountp;
  555. block = bp->data;
  556. btp = xfs_dir2_block_tail_p(mp, block);
  557. xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block),
  558. (uint)((char *)(btp + 1) - (char *)block - 1));
  559. }
  560. /*
  561. * Look up an entry in the block. This is the external routine,
  562. * xfs_dir2_block_lookup_int does the real work.
  563. */
  564. int /* error */
  565. xfs_dir2_block_lookup(
  566. xfs_da_args_t *args) /* dir lookup arguments */
  567. {
  568. xfs_dir2_block_t *block; /* block structure */
  569. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  570. xfs_dabuf_t *bp; /* block buffer */
  571. xfs_dir2_block_tail_t *btp; /* block tail */
  572. xfs_dir2_data_entry_t *dep; /* block data entry */
  573. xfs_inode_t *dp; /* incore inode */
  574. int ent; /* entry index */
  575. int error; /* error return value */
  576. xfs_mount_t *mp; /* filesystem mount point */
  577. xfs_dir2_trace_args("block_lookup", args);
  578. /*
  579. * Get the buffer, look up the entry.
  580. * If not found (ENOENT) then return, have no buffer.
  581. */
  582. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
  583. return error;
  584. dp = args->dp;
  585. mp = dp->i_mount;
  586. block = bp->data;
  587. xfs_dir2_data_check(dp, bp);
  588. btp = xfs_dir2_block_tail_p(mp, block);
  589. blp = xfs_dir2_block_leaf_p(btp);
  590. /*
  591. * Get the offset from the leaf entry, to point to the data.
  592. */
  593. dep = (xfs_dir2_data_entry_t *)
  594. ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
  595. /*
  596. * Fill in inode number, release the block.
  597. */
  598. args->inumber = be64_to_cpu(dep->inumber);
  599. xfs_da_brelse(args->trans, bp);
  600. return XFS_ERROR(EEXIST);
  601. }
  602. /*
  603. * Internal block lookup routine.
  604. */
  605. static int /* error */
  606. xfs_dir2_block_lookup_int(
  607. xfs_da_args_t *args, /* dir lookup arguments */
  608. xfs_dabuf_t **bpp, /* returned block buffer */
  609. int *entno) /* returned entry number */
  610. {
  611. xfs_dir2_dataptr_t addr; /* data entry address */
  612. xfs_dir2_block_t *block; /* block structure */
  613. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  614. xfs_dabuf_t *bp; /* block buffer */
  615. xfs_dir2_block_tail_t *btp; /* block tail */
  616. xfs_dir2_data_entry_t *dep; /* block data entry */
  617. xfs_inode_t *dp; /* incore inode */
  618. int error; /* error return value */
  619. xfs_dahash_t hash; /* found hash value */
  620. int high; /* binary search high index */
  621. int low; /* binary search low index */
  622. int mid; /* binary search current idx */
  623. xfs_mount_t *mp; /* filesystem mount point */
  624. xfs_trans_t *tp; /* transaction pointer */
  625. dp = args->dp;
  626. tp = args->trans;
  627. mp = dp->i_mount;
  628. /*
  629. * Read the buffer, return error if we can't get it.
  630. */
  631. if ((error =
  632. xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
  633. return error;
  634. }
  635. ASSERT(bp != NULL);
  636. block = bp->data;
  637. xfs_dir2_data_check(dp, bp);
  638. btp = xfs_dir2_block_tail_p(mp, block);
  639. blp = xfs_dir2_block_leaf_p(btp);
  640. /*
  641. * Loop doing a binary search for our hash value.
  642. * Find our entry, ENOENT if it's not there.
  643. */
  644. for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
  645. ASSERT(low <= high);
  646. mid = (low + high) >> 1;
  647. if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
  648. break;
  649. if (hash < args->hashval)
  650. low = mid + 1;
  651. else
  652. high = mid - 1;
  653. if (low > high) {
  654. ASSERT(args->oknoent);
  655. xfs_da_brelse(tp, bp);
  656. return XFS_ERROR(ENOENT);
  657. }
  658. }
  659. /*
  660. * Back up to the first one with the right hash value.
  661. */
  662. while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
  663. mid--;
  664. }
  665. /*
  666. * Now loop forward through all the entries with the
  667. * right hash value looking for our name.
  668. */
  669. do {
  670. if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
  671. continue;
  672. /*
  673. * Get pointer to the entry from the leaf.
  674. */
  675. dep = (xfs_dir2_data_entry_t *)
  676. ((char *)block + xfs_dir2_dataptr_to_off(mp, addr));
  677. /*
  678. * Compare, if it's right give back buffer & entry number.
  679. */
  680. if (dep->namelen == args->namelen &&
  681. dep->name[0] == args->name[0] &&
  682. memcmp(dep->name, args->name, args->namelen) == 0) {
  683. *bpp = bp;
  684. *entno = mid;
  685. return 0;
  686. }
  687. } while (++mid < be32_to_cpu(btp->count) && be32_to_cpu(blp[mid].hashval) == hash);
  688. /*
  689. * No match, release the buffer and return ENOENT.
  690. */
  691. ASSERT(args->oknoent);
  692. xfs_da_brelse(tp, bp);
  693. return XFS_ERROR(ENOENT);
  694. }
  695. /*
  696. * Remove an entry from a block format directory.
  697. * If that makes the block small enough to fit in shortform, transform it.
  698. */
  699. int /* error */
  700. xfs_dir2_block_removename(
  701. xfs_da_args_t *args) /* directory operation args */
  702. {
  703. xfs_dir2_block_t *block; /* block structure */
  704. xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
  705. xfs_dabuf_t *bp; /* block buffer */
  706. xfs_dir2_block_tail_t *btp; /* block tail */
  707. xfs_dir2_data_entry_t *dep; /* block data entry */
  708. xfs_inode_t *dp; /* incore inode */
  709. int ent; /* block leaf entry index */
  710. int error; /* error return value */
  711. xfs_mount_t *mp; /* filesystem mount point */
  712. int needlog; /* need to log block header */
  713. int needscan; /* need to fixup bestfree */
  714. xfs_dir2_sf_hdr_t sfh; /* shortform header */
  715. int size; /* shortform size */
  716. xfs_trans_t *tp; /* transaction pointer */
  717. xfs_dir2_trace_args("block_removename", args);
  718. /*
  719. * Look up the entry in the block. Gets the buffer and entry index.
  720. * It will always be there, the vnodeops level does a lookup first.
  721. */
  722. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
  723. return error;
  724. }
  725. dp = args->dp;
  726. tp = args->trans;
  727. mp = dp->i_mount;
  728. block = bp->data;
  729. btp = xfs_dir2_block_tail_p(mp, block);
  730. blp = xfs_dir2_block_leaf_p(btp);
  731. /*
  732. * Point to the data entry using the leaf entry.
  733. */
  734. dep = (xfs_dir2_data_entry_t *)
  735. ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
  736. /*
  737. * Mark the data entry's space free.
  738. */
  739. needlog = needscan = 0;
  740. xfs_dir2_data_make_free(tp, bp,
  741. (xfs_dir2_data_aoff_t)((char *)dep - (char *)block),
  742. xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
  743. /*
  744. * Fix up the block tail.
  745. */
  746. be32_add(&btp->stale, 1);
  747. xfs_dir2_block_log_tail(tp, bp);
  748. /*
  749. * Remove the leaf entry by marking it stale.
  750. */
  751. blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  752. xfs_dir2_block_log_leaf(tp, bp, ent, ent);
  753. /*
  754. * Fix up bestfree, log the header if necessary.
  755. */
  756. if (needscan)
  757. xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
  758. if (needlog)
  759. xfs_dir2_data_log_header(tp, bp);
  760. xfs_dir2_data_check(dp, bp);
  761. /*
  762. * See if the size as a shortform is good enough.
  763. */
  764. if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
  765. XFS_IFORK_DSIZE(dp)) {
  766. xfs_da_buf_done(bp);
  767. return 0;
  768. }
  769. /*
  770. * If it works, do the conversion.
  771. */
  772. return xfs_dir2_block_to_sf(args, bp, size, &sfh);
  773. }
  774. /*
  775. * Replace an entry in a V2 block directory.
  776. * Change the inode number to the new value.
  777. */
  778. int /* error */
  779. xfs_dir2_block_replace(
  780. xfs_da_args_t *args) /* directory operation args */
  781. {
  782. xfs_dir2_block_t *block; /* block structure */
  783. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  784. xfs_dabuf_t *bp; /* block buffer */
  785. xfs_dir2_block_tail_t *btp; /* block tail */
  786. xfs_dir2_data_entry_t *dep; /* block data entry */
  787. xfs_inode_t *dp; /* incore inode */
  788. int ent; /* leaf entry index */
  789. int error; /* error return value */
  790. xfs_mount_t *mp; /* filesystem mount point */
  791. xfs_dir2_trace_args("block_replace", args);
  792. /*
  793. * Lookup the entry in the directory. Get buffer and entry index.
  794. * This will always succeed since the caller has already done a lookup.
  795. */
  796. if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
  797. return error;
  798. }
  799. dp = args->dp;
  800. mp = dp->i_mount;
  801. block = bp->data;
  802. btp = xfs_dir2_block_tail_p(mp, block);
  803. blp = xfs_dir2_block_leaf_p(btp);
  804. /*
  805. * Point to the data entry we need to change.
  806. */
  807. dep = (xfs_dir2_data_entry_t *)
  808. ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
  809. ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
  810. /*
  811. * Change the inode number to the new value.
  812. */
  813. dep->inumber = cpu_to_be64(args->inumber);
  814. xfs_dir2_data_log_entry(args->trans, bp, dep);
  815. xfs_dir2_data_check(dp, bp);
  816. xfs_da_buf_done(bp);
  817. return 0;
  818. }
  819. /*
  820. * Qsort comparison routine for the block leaf entries.
  821. */
  822. static int /* sort order */
  823. xfs_dir2_block_sort(
  824. const void *a, /* first leaf entry */
  825. const void *b) /* second leaf entry */
  826. {
  827. const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
  828. const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
  829. la = a;
  830. lb = b;
  831. return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
  832. (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
  833. }
  834. /*
  835. * Convert a V2 leaf directory to a V2 block directory if possible.
  836. */
  837. int /* error */
  838. xfs_dir2_leaf_to_block(
  839. xfs_da_args_t *args, /* operation arguments */
  840. xfs_dabuf_t *lbp, /* leaf buffer */
  841. xfs_dabuf_t *dbp) /* data buffer */
  842. {
  843. __be16 *bestsp; /* leaf bests table */
  844. xfs_dir2_block_t *block; /* block structure */
  845. xfs_dir2_block_tail_t *btp; /* block tail */
  846. xfs_inode_t *dp; /* incore directory inode */
  847. xfs_dir2_data_unused_t *dup; /* unused data entry */
  848. int error; /* error return value */
  849. int from; /* leaf from index */
  850. xfs_dir2_leaf_t *leaf; /* leaf structure */
  851. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  852. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  853. xfs_mount_t *mp; /* file system mount point */
  854. int needlog; /* need to log data header */
  855. int needscan; /* need to scan for bestfree */
  856. xfs_dir2_sf_hdr_t sfh; /* shortform header */
  857. int size; /* bytes used */
  858. __be16 *tagp; /* end of entry (tag) */
  859. int to; /* block/leaf to index */
  860. xfs_trans_t *tp; /* transaction pointer */
  861. xfs_dir2_trace_args_bb("leaf_to_block", args, lbp, dbp);
  862. dp = args->dp;
  863. tp = args->trans;
  864. mp = dp->i_mount;
  865. leaf = lbp->data;
  866. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
  867. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  868. /*
  869. * If there are data blocks other than the first one, take this
  870. * opportunity to remove trailing empty data blocks that may have
  871. * been left behind during no-space-reservation operations.
  872. * These will show up in the leaf bests table.
  873. */
  874. while (dp->i_d.di_size > mp->m_dirblksize) {
  875. bestsp = xfs_dir2_leaf_bests_p(ltp);
  876. if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
  877. mp->m_dirblksize - (uint)sizeof(block->hdr)) {
  878. if ((error =
  879. xfs_dir2_leaf_trim_data(args, lbp,
  880. (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
  881. goto out;
  882. } else {
  883. error = 0;
  884. goto out;
  885. }
  886. }
  887. /*
  888. * Read the data block if we don't already have it, give up if it fails.
  889. */
  890. if (dbp == NULL &&
  891. (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
  892. XFS_DATA_FORK))) {
  893. goto out;
  894. }
  895. block = dbp->data;
  896. ASSERT(be32_to_cpu(block->hdr.magic) == XFS_DIR2_DATA_MAGIC);
  897. /*
  898. * Size of the "leaf" area in the block.
  899. */
  900. size = (uint)sizeof(block->tail) +
  901. (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
  902. /*
  903. * Look at the last data entry.
  904. */
  905. tagp = (__be16 *)((char *)block + mp->m_dirblksize) - 1;
  906. dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
  907. /*
  908. * If it's not free or is too short we can't do it.
  909. */
  910. if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
  911. be16_to_cpu(dup->length) < size) {
  912. error = 0;
  913. goto out;
  914. }
  915. /*
  916. * Start converting it to block form.
  917. */
  918. block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
  919. needlog = 1;
  920. needscan = 0;
  921. /*
  922. * Use up the space at the end of the block (blp/btp).
  923. */
  924. xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
  925. &needlog, &needscan);
  926. /*
  927. * Initialize the block tail.
  928. */
  929. btp = xfs_dir2_block_tail_p(mp, block);
  930. btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
  931. btp->stale = 0;
  932. xfs_dir2_block_log_tail(tp, dbp);
  933. /*
  934. * Initialize the block leaf area. We compact out stale entries.
  935. */
  936. lep = xfs_dir2_block_leaf_p(btp);
  937. for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
  938. if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
  939. continue;
  940. lep[to++] = leaf->ents[from];
  941. }
  942. ASSERT(to == be32_to_cpu(btp->count));
  943. xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
  944. /*
  945. * Scan the bestfree if we need it and log the data block header.
  946. */
  947. if (needscan)
  948. xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
  949. if (needlog)
  950. xfs_dir2_data_log_header(tp, dbp);
  951. /*
  952. * Pitch the old leaf block.
  953. */
  954. error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
  955. lbp = NULL;
  956. if (error) {
  957. goto out;
  958. }
  959. /*
  960. * Now see if the resulting block can be shrunken to shortform.
  961. */
  962. if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
  963. XFS_IFORK_DSIZE(dp)) {
  964. error = 0;
  965. goto out;
  966. }
  967. return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
  968. out:
  969. if (lbp)
  970. xfs_da_buf_done(lbp);
  971. if (dbp)
  972. xfs_da_buf_done(dbp);
  973. return error;
  974. }
  975. /*
  976. * Convert the shortform directory to block form.
  977. */
  978. int /* error */
  979. xfs_dir2_sf_to_block(
  980. xfs_da_args_t *args) /* operation arguments */
  981. {
  982. xfs_dir2_db_t blkno; /* dir-relative block # (0) */
  983. xfs_dir2_block_t *block; /* block structure */
  984. xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
  985. xfs_dabuf_t *bp; /* block buffer */
  986. xfs_dir2_block_tail_t *btp; /* block tail pointer */
  987. char *buf; /* sf buffer */
  988. int buf_len;
  989. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  990. xfs_inode_t *dp; /* incore directory inode */
  991. int dummy; /* trash */
  992. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  993. int endoffset; /* end of data objects */
  994. int error; /* error return value */
  995. int i; /* index */
  996. xfs_mount_t *mp; /* filesystem mount point */
  997. int needlog; /* need to log block header */
  998. int needscan; /* need to scan block freespc */
  999. int newoffset; /* offset from current entry */
  1000. int offset; /* target block offset */
  1001. xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
  1002. xfs_dir2_sf_t *sfp; /* shortform structure */
  1003. __be16 *tagp; /* end of data entry */
  1004. xfs_trans_t *tp; /* transaction pointer */
  1005. xfs_dir2_trace_args("sf_to_block", args);
  1006. dp = args->dp;
  1007. tp = args->trans;
  1008. mp = dp->i_mount;
  1009. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  1010. /*
  1011. * Bomb out if the shortform directory is way too short.
  1012. */
  1013. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  1014. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1015. return XFS_ERROR(EIO);
  1016. }
  1017. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  1018. ASSERT(dp->i_df.if_u1.if_data != NULL);
  1019. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  1020. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
  1021. /*
  1022. * Copy the directory into the stack buffer.
  1023. * Then pitch the incore inode data so we can make extents.
  1024. */
  1025. buf_len = dp->i_df.if_bytes;
  1026. buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
  1027. memcpy(buf, sfp, dp->i_df.if_bytes);
  1028. xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
  1029. dp->i_d.di_size = 0;
  1030. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1031. /*
  1032. * Reset pointer - old sfp is gone.
  1033. */
  1034. sfp = (xfs_dir2_sf_t *)buf;
  1035. /*
  1036. * Add block 0 to the inode.
  1037. */
  1038. error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
  1039. if (error) {
  1040. kmem_free(buf, buf_len);
  1041. return error;
  1042. }
  1043. /*
  1044. * Initialize the data block.
  1045. */
  1046. error = xfs_dir2_data_init(args, blkno, &bp);
  1047. if (error) {
  1048. kmem_free(buf, buf_len);
  1049. return error;
  1050. }
  1051. block = bp->data;
  1052. block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
  1053. /*
  1054. * Compute size of block "tail" area.
  1055. */
  1056. i = (uint)sizeof(*btp) +
  1057. (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
  1058. /*
  1059. * The whole thing is initialized to free by the init routine.
  1060. * Say we're using the leaf and tail area.
  1061. */
  1062. dup = (xfs_dir2_data_unused_t *)block->u;
  1063. needlog = needscan = 0;
  1064. xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
  1065. &needscan);
  1066. ASSERT(needscan == 0);
  1067. /*
  1068. * Fill in the tail.
  1069. */
  1070. btp = xfs_dir2_block_tail_p(mp, block);
  1071. btp->count = cpu_to_be32(sfp->hdr.count + 2); /* ., .. */
  1072. btp->stale = 0;
  1073. blp = xfs_dir2_block_leaf_p(btp);
  1074. endoffset = (uint)((char *)blp - (char *)block);
  1075. /*
  1076. * Remove the freespace, we'll manage it.
  1077. */
  1078. xfs_dir2_data_use_free(tp, bp, dup,
  1079. (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
  1080. be16_to_cpu(dup->length), &needlog, &needscan);
  1081. /*
  1082. * Create entry for .
  1083. */
  1084. dep = (xfs_dir2_data_entry_t *)
  1085. ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
  1086. dep->inumber = cpu_to_be64(dp->i_ino);
  1087. dep->namelen = 1;
  1088. dep->name[0] = '.';
  1089. tagp = xfs_dir2_data_entry_tag_p(dep);
  1090. *tagp = cpu_to_be16((char *)dep - (char *)block);
  1091. xfs_dir2_data_log_entry(tp, bp, dep);
  1092. blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
  1093. blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
  1094. (char *)dep - (char *)block));
  1095. /*
  1096. * Create entry for ..
  1097. */
  1098. dep = (xfs_dir2_data_entry_t *)
  1099. ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
  1100. dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent));
  1101. dep->namelen = 2;
  1102. dep->name[0] = dep->name[1] = '.';
  1103. tagp = xfs_dir2_data_entry_tag_p(dep);
  1104. *tagp = cpu_to_be16((char *)dep - (char *)block);
  1105. xfs_dir2_data_log_entry(tp, bp, dep);
  1106. blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
  1107. blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
  1108. (char *)dep - (char *)block));
  1109. offset = XFS_DIR2_DATA_FIRST_OFFSET;
  1110. /*
  1111. * Loop over existing entries, stuff them in.
  1112. */
  1113. if ((i = 0) == sfp->hdr.count)
  1114. sfep = NULL;
  1115. else
  1116. sfep = xfs_dir2_sf_firstentry(sfp);
  1117. /*
  1118. * Need to preserve the existing offset values in the sf directory.
  1119. * Insert holes (unused entries) where necessary.
  1120. */
  1121. while (offset < endoffset) {
  1122. /*
  1123. * sfep is null when we reach the end of the list.
  1124. */
  1125. if (sfep == NULL)
  1126. newoffset = endoffset;
  1127. else
  1128. newoffset = xfs_dir2_sf_get_offset(sfep);
  1129. /*
  1130. * There should be a hole here, make one.
  1131. */
  1132. if (offset < newoffset) {
  1133. dup = (xfs_dir2_data_unused_t *)
  1134. ((char *)block + offset);
  1135. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  1136. dup->length = cpu_to_be16(newoffset - offset);
  1137. *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
  1138. ((char *)dup - (char *)block));
  1139. xfs_dir2_data_log_unused(tp, bp, dup);
  1140. (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
  1141. dup, &dummy);
  1142. offset += be16_to_cpu(dup->length);
  1143. continue;
  1144. }
  1145. /*
  1146. * Copy a real entry.
  1147. */
  1148. dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
  1149. dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp,
  1150. xfs_dir2_sf_inumberp(sfep)));
  1151. dep->namelen = sfep->namelen;
  1152. memcpy(dep->name, sfep->name, dep->namelen);
  1153. tagp = xfs_dir2_data_entry_tag_p(dep);
  1154. *tagp = cpu_to_be16((char *)dep - (char *)block);
  1155. xfs_dir2_data_log_entry(tp, bp, dep);
  1156. blp[2 + i].hashval = cpu_to_be32(xfs_da_hashname(
  1157. (char *)sfep->name, sfep->namelen));
  1158. blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
  1159. (char *)dep - (char *)block));
  1160. offset = (int)((char *)(tagp + 1) - (char *)block);
  1161. if (++i == sfp->hdr.count)
  1162. sfep = NULL;
  1163. else
  1164. sfep = xfs_dir2_sf_nextentry(sfp, sfep);
  1165. }
  1166. /* Done with the temporary buffer */
  1167. kmem_free(buf, buf_len);
  1168. /*
  1169. * Sort the leaf entries by hash value.
  1170. */
  1171. xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
  1172. /*
  1173. * Log the leaf entry area and tail.
  1174. * Already logged the header in data_init, ignore needlog.
  1175. */
  1176. ASSERT(needscan == 0);
  1177. xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
  1178. xfs_dir2_block_log_tail(tp, bp);
  1179. xfs_dir2_data_check(dp, bp);
  1180. xfs_da_buf_done(bp);
  1181. return 0;
  1182. }