xfs_dir2_block.c 36 KB

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