xfs_dir2_block.c 37 KB

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