xfs_dir2_block.c 37 KB

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