xfs_dir2_data.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_dir2.h"
  31. #include "xfs_dir2_priv.h"
  32. #include "xfs_error.h"
  33. #include "xfs_trans.h"
  34. #include "xfs_buf_item.h"
  35. #include "xfs_cksum.h"
  36. /*
  37. * Check the consistency of the data block.
  38. * The input can also be a block-format directory.
  39. * Return 0 is the buffer is good, otherwise an error.
  40. */
  41. int
  42. __xfs_dir3_data_check(
  43. struct xfs_inode *dp, /* incore inode pointer */
  44. struct xfs_buf *bp) /* data block's buffer */
  45. {
  46. xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
  47. xfs_dir2_data_free_t *bf; /* bestfree table */
  48. xfs_dir2_block_tail_t *btp=NULL; /* block tail */
  49. int count; /* count of entries found */
  50. xfs_dir2_data_hdr_t *hdr; /* data block header */
  51. xfs_dir2_data_entry_t *dep; /* data entry */
  52. xfs_dir2_data_free_t *dfp; /* bestfree entry */
  53. xfs_dir2_data_unused_t *dup; /* unused entry */
  54. char *endp; /* end of useful data */
  55. int freeseen; /* mask of bestfrees seen */
  56. xfs_dahash_t hash; /* hash of current name */
  57. int i; /* leaf index */
  58. int lastfree; /* last entry was unused */
  59. xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
  60. xfs_mount_t *mp; /* filesystem mount point */
  61. char *p; /* current data position */
  62. int stale; /* count of stale leaves */
  63. struct xfs_name name;
  64. const struct xfs_dir_ops *ops;
  65. mp = bp->b_target->bt_mount;
  66. /*
  67. * We can be passed a null dp here from a verifier, so we need to go the
  68. * hard way to get them.
  69. */
  70. ops = xfs_dir_get_ops(mp, dp);
  71. hdr = bp->b_addr;
  72. p = (char *)ops->data_entry_p(hdr);
  73. switch (hdr->magic) {
  74. case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
  75. case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
  76. btp = xfs_dir2_block_tail_p(mp, hdr);
  77. lep = xfs_dir2_block_leaf_p(btp);
  78. endp = (char *)lep;
  79. /*
  80. * The number of leaf entries is limited by the size of the
  81. * block and the amount of space used by the data entries.
  82. * We don't know how much space is used by the data entries yet,
  83. * so just ensure that the count falls somewhere inside the
  84. * block right now.
  85. */
  86. XFS_WANT_CORRUPTED_RETURN(be32_to_cpu(btp->count) <
  87. ((char *)btp - p) / sizeof(struct xfs_dir2_leaf_entry));
  88. break;
  89. case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
  90. case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
  91. endp = (char *)hdr + mp->m_dirblksize;
  92. break;
  93. default:
  94. XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
  95. return EFSCORRUPTED;
  96. }
  97. /*
  98. * Account for zero bestfree entries.
  99. */
  100. bf = ops->data_bestfree_p(hdr);
  101. count = lastfree = freeseen = 0;
  102. if (!bf[0].length) {
  103. XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
  104. freeseen |= 1 << 0;
  105. }
  106. if (!bf[1].length) {
  107. XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
  108. freeseen |= 1 << 1;
  109. }
  110. if (!bf[2].length) {
  111. XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
  112. freeseen |= 1 << 2;
  113. }
  114. XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
  115. be16_to_cpu(bf[1].length));
  116. XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
  117. be16_to_cpu(bf[2].length));
  118. /*
  119. * Loop over the data/unused entries.
  120. */
  121. while (p < endp) {
  122. dup = (xfs_dir2_data_unused_t *)p;
  123. /*
  124. * If it's unused, look for the space in the bestfree table.
  125. * If we find it, account for that, else make sure it
  126. * doesn't need to be there.
  127. */
  128. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  129. XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
  130. XFS_WANT_CORRUPTED_RETURN(
  131. be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
  132. (char *)dup - (char *)hdr);
  133. dfp = xfs_dir2_data_freefind(hdr, bf, dup);
  134. if (dfp) {
  135. i = (int)(dfp - bf);
  136. XFS_WANT_CORRUPTED_RETURN(
  137. (freeseen & (1 << i)) == 0);
  138. freeseen |= 1 << i;
  139. } else {
  140. XFS_WANT_CORRUPTED_RETURN(
  141. be16_to_cpu(dup->length) <=
  142. be16_to_cpu(bf[2].length));
  143. }
  144. p += be16_to_cpu(dup->length);
  145. lastfree = 1;
  146. continue;
  147. }
  148. /*
  149. * It's a real entry. Validate the fields.
  150. * If this is a block directory then make sure it's
  151. * in the leaf section of the block.
  152. * The linear search is crude but this is DEBUG code.
  153. */
  154. dep = (xfs_dir2_data_entry_t *)p;
  155. XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
  156. XFS_WANT_CORRUPTED_RETURN(
  157. !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
  158. XFS_WANT_CORRUPTED_RETURN(
  159. be16_to_cpu(*ops->data_entry_tag_p(dep)) ==
  160. (char *)dep - (char *)hdr);
  161. XFS_WANT_CORRUPTED_RETURN(
  162. ops->data_get_ftype(dep) < XFS_DIR3_FT_MAX);
  163. count++;
  164. lastfree = 0;
  165. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  166. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  167. addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  168. (xfs_dir2_data_aoff_t)
  169. ((char *)dep - (char *)hdr));
  170. name.name = dep->name;
  171. name.len = dep->namelen;
  172. hash = mp->m_dirnameops->hashname(&name);
  173. for (i = 0; i < be32_to_cpu(btp->count); i++) {
  174. if (be32_to_cpu(lep[i].address) == addr &&
  175. be32_to_cpu(lep[i].hashval) == hash)
  176. break;
  177. }
  178. XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
  179. }
  180. p += ops->data_entsize(dep->namelen);
  181. }
  182. /*
  183. * Need to have seen all the entries and all the bestfree slots.
  184. */
  185. XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
  186. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  187. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  188. for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
  189. if (lep[i].address ==
  190. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  191. stale++;
  192. if (i > 0)
  193. XFS_WANT_CORRUPTED_RETURN(
  194. be32_to_cpu(lep[i].hashval) >=
  195. be32_to_cpu(lep[i - 1].hashval));
  196. }
  197. XFS_WANT_CORRUPTED_RETURN(count ==
  198. be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
  199. XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
  200. }
  201. return 0;
  202. }
  203. static bool
  204. xfs_dir3_data_verify(
  205. struct xfs_buf *bp)
  206. {
  207. struct xfs_mount *mp = bp->b_target->bt_mount;
  208. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  209. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  210. if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
  211. return false;
  212. if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
  213. return false;
  214. if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
  215. return false;
  216. } else {
  217. if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
  218. return false;
  219. }
  220. if (__xfs_dir3_data_check(NULL, bp))
  221. return false;
  222. return true;
  223. }
  224. /*
  225. * Readahead of the first block of the directory when it is opened is completely
  226. * oblivious to the format of the directory. Hence we can either get a block
  227. * format buffer or a data format buffer on readahead.
  228. */
  229. static void
  230. xfs_dir3_data_reada_verify(
  231. struct xfs_buf *bp)
  232. {
  233. struct xfs_mount *mp = bp->b_target->bt_mount;
  234. struct xfs_dir2_data_hdr *hdr = bp->b_addr;
  235. switch (hdr->magic) {
  236. case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
  237. case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
  238. bp->b_ops = &xfs_dir3_block_buf_ops;
  239. bp->b_ops->verify_read(bp);
  240. return;
  241. case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
  242. case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
  243. xfs_dir3_data_verify(bp);
  244. return;
  245. default:
  246. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
  247. xfs_buf_ioerror(bp, EFSCORRUPTED);
  248. break;
  249. }
  250. }
  251. static void
  252. xfs_dir3_data_read_verify(
  253. struct xfs_buf *bp)
  254. {
  255. struct xfs_mount *mp = bp->b_target->bt_mount;
  256. if ((xfs_sb_version_hascrc(&mp->m_sb) &&
  257. !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
  258. XFS_DIR3_DATA_CRC_OFF)) ||
  259. !xfs_dir3_data_verify(bp)) {
  260. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  261. xfs_buf_ioerror(bp, EFSCORRUPTED);
  262. }
  263. }
  264. static void
  265. xfs_dir3_data_write_verify(
  266. struct xfs_buf *bp)
  267. {
  268. struct xfs_mount *mp = bp->b_target->bt_mount;
  269. struct xfs_buf_log_item *bip = bp->b_fspriv;
  270. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  271. if (!xfs_dir3_data_verify(bp)) {
  272. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  273. xfs_buf_ioerror(bp, EFSCORRUPTED);
  274. return;
  275. }
  276. if (!xfs_sb_version_hascrc(&mp->m_sb))
  277. return;
  278. if (bip)
  279. hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
  280. xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
  281. }
  282. const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
  283. .verify_read = xfs_dir3_data_read_verify,
  284. .verify_write = xfs_dir3_data_write_verify,
  285. };
  286. static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
  287. .verify_read = xfs_dir3_data_reada_verify,
  288. .verify_write = xfs_dir3_data_write_verify,
  289. };
  290. int
  291. xfs_dir3_data_read(
  292. struct xfs_trans *tp,
  293. struct xfs_inode *dp,
  294. xfs_dablk_t bno,
  295. xfs_daddr_t mapped_bno,
  296. struct xfs_buf **bpp)
  297. {
  298. int err;
  299. err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
  300. XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
  301. if (!err && tp)
  302. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
  303. return err;
  304. }
  305. int
  306. xfs_dir3_data_readahead(
  307. struct xfs_trans *tp,
  308. struct xfs_inode *dp,
  309. xfs_dablk_t bno,
  310. xfs_daddr_t mapped_bno)
  311. {
  312. return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
  313. XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
  314. }
  315. /*
  316. * Given a data block and an unused entry from that block,
  317. * return the bestfree entry if any that corresponds to it.
  318. */
  319. xfs_dir2_data_free_t *
  320. xfs_dir2_data_freefind(
  321. struct xfs_dir2_data_hdr *hdr, /* data block header */
  322. struct xfs_dir2_data_free *bf, /* bestfree table pointer */
  323. struct xfs_dir2_data_unused *dup) /* unused space */
  324. {
  325. xfs_dir2_data_free_t *dfp; /* bestfree entry */
  326. xfs_dir2_data_aoff_t off; /* offset value needed */
  327. #ifdef DEBUG
  328. int matched; /* matched the value */
  329. int seenzero; /* saw a 0 bestfree entry */
  330. #endif
  331. off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
  332. #ifdef DEBUG
  333. /*
  334. * Validate some consistency in the bestfree table.
  335. * Check order, non-overlapping entries, and if we find the
  336. * one we're looking for it has to be exact.
  337. */
  338. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  339. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  340. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  341. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  342. for (dfp = &bf[0], seenzero = matched = 0;
  343. dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
  344. dfp++) {
  345. if (!dfp->offset) {
  346. ASSERT(!dfp->length);
  347. seenzero = 1;
  348. continue;
  349. }
  350. ASSERT(seenzero == 0);
  351. if (be16_to_cpu(dfp->offset) == off) {
  352. matched = 1;
  353. ASSERT(dfp->length == dup->length);
  354. } else if (off < be16_to_cpu(dfp->offset))
  355. ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
  356. else
  357. ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
  358. ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
  359. if (dfp > &bf[0])
  360. ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
  361. }
  362. #endif
  363. /*
  364. * If this is smaller than the smallest bestfree entry,
  365. * it can't be there since they're sorted.
  366. */
  367. if (be16_to_cpu(dup->length) <
  368. be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
  369. return NULL;
  370. /*
  371. * Look at the three bestfree entries for our guy.
  372. */
  373. for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
  374. if (!dfp->offset)
  375. return NULL;
  376. if (be16_to_cpu(dfp->offset) == off)
  377. return dfp;
  378. }
  379. /*
  380. * Didn't find it. This only happens if there are duplicate lengths.
  381. */
  382. return NULL;
  383. }
  384. /*
  385. * Insert an unused-space entry into the bestfree table.
  386. */
  387. xfs_dir2_data_free_t * /* entry inserted */
  388. xfs_dir2_data_freeinsert(
  389. struct xfs_dir2_data_hdr *hdr, /* data block pointer */
  390. struct xfs_dir2_data_free *dfp, /* bestfree table pointer */
  391. struct xfs_dir2_data_unused *dup, /* unused space */
  392. int *loghead) /* log the data header (out) */
  393. {
  394. xfs_dir2_data_free_t new; /* new bestfree entry */
  395. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  396. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  397. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  398. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  399. new.length = dup->length;
  400. new.offset = cpu_to_be16((char *)dup - (char *)hdr);
  401. /*
  402. * Insert at position 0, 1, or 2; or not at all.
  403. */
  404. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
  405. dfp[2] = dfp[1];
  406. dfp[1] = dfp[0];
  407. dfp[0] = new;
  408. *loghead = 1;
  409. return &dfp[0];
  410. }
  411. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
  412. dfp[2] = dfp[1];
  413. dfp[1] = new;
  414. *loghead = 1;
  415. return &dfp[1];
  416. }
  417. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
  418. dfp[2] = new;
  419. *loghead = 1;
  420. return &dfp[2];
  421. }
  422. return NULL;
  423. }
  424. /*
  425. * Remove a bestfree entry from the table.
  426. */
  427. STATIC void
  428. xfs_dir2_data_freeremove(
  429. struct xfs_dir2_data_hdr *hdr, /* data block header */
  430. struct xfs_dir2_data_free *bf, /* bestfree table pointer */
  431. struct xfs_dir2_data_free *dfp, /* bestfree entry pointer */
  432. int *loghead) /* out: log data header */
  433. {
  434. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  435. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  436. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  437. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  438. /*
  439. * It's the first entry, slide the next 2 up.
  440. */
  441. if (dfp == &bf[0]) {
  442. bf[0] = bf[1];
  443. bf[1] = bf[2];
  444. }
  445. /*
  446. * It's the second entry, slide the 3rd entry up.
  447. */
  448. else if (dfp == &bf[1])
  449. bf[1] = bf[2];
  450. /*
  451. * Must be the last entry.
  452. */
  453. else
  454. ASSERT(dfp == &bf[2]);
  455. /*
  456. * Clear the 3rd entry, must be zero now.
  457. */
  458. bf[2].length = 0;
  459. bf[2].offset = 0;
  460. *loghead = 1;
  461. }
  462. /*
  463. * Given a data block, reconstruct its bestfree map.
  464. */
  465. void
  466. xfs_dir2_data_freescan(
  467. struct xfs_inode *dp,
  468. struct xfs_dir2_data_hdr *hdr,
  469. int *loghead)
  470. {
  471. xfs_dir2_block_tail_t *btp; /* block tail */
  472. xfs_dir2_data_entry_t *dep; /* active data entry */
  473. xfs_dir2_data_unused_t *dup; /* unused data entry */
  474. struct xfs_dir2_data_free *bf;
  475. char *endp; /* end of block's data */
  476. char *p; /* current entry pointer */
  477. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  478. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  479. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  480. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  481. /*
  482. * Start by clearing the table.
  483. */
  484. bf = dp->d_ops->data_bestfree_p(hdr);
  485. memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
  486. *loghead = 1;
  487. /*
  488. * Set up pointers.
  489. */
  490. p = (char *)dp->d_ops->data_entry_p(hdr);
  491. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  492. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  493. btp = xfs_dir2_block_tail_p(dp->i_mount, hdr);
  494. endp = (char *)xfs_dir2_block_leaf_p(btp);
  495. } else
  496. endp = (char *)hdr + dp->i_mount->m_dirblksize;
  497. /*
  498. * Loop over the block's entries.
  499. */
  500. while (p < endp) {
  501. dup = (xfs_dir2_data_unused_t *)p;
  502. /*
  503. * If it's a free entry, insert it.
  504. */
  505. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  506. ASSERT((char *)dup - (char *)hdr ==
  507. be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  508. xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
  509. p += be16_to_cpu(dup->length);
  510. }
  511. /*
  512. * For active entries, check their tags and skip them.
  513. */
  514. else {
  515. dep = (xfs_dir2_data_entry_t *)p;
  516. ASSERT((char *)dep - (char *)hdr ==
  517. be16_to_cpu(*dp->d_ops->data_entry_tag_p(dep)));
  518. p += dp->d_ops->data_entsize(dep->namelen);
  519. }
  520. }
  521. }
  522. /*
  523. * Initialize a data block at the given block number in the directory.
  524. * Give back the buffer for the created block.
  525. */
  526. int /* error */
  527. xfs_dir3_data_init(
  528. xfs_da_args_t *args, /* directory operation args */
  529. xfs_dir2_db_t blkno, /* logical dir block number */
  530. struct xfs_buf **bpp) /* output block buffer */
  531. {
  532. struct xfs_buf *bp; /* block buffer */
  533. xfs_dir2_data_hdr_t *hdr; /* data block header */
  534. xfs_inode_t *dp; /* incore directory inode */
  535. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  536. struct xfs_dir2_data_free *bf;
  537. int error; /* error return value */
  538. int i; /* bestfree index */
  539. xfs_mount_t *mp; /* filesystem mount point */
  540. xfs_trans_t *tp; /* transaction pointer */
  541. int t; /* temp */
  542. dp = args->dp;
  543. mp = dp->i_mount;
  544. tp = args->trans;
  545. /*
  546. * Get the buffer set up for the block.
  547. */
  548. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
  549. XFS_DATA_FORK);
  550. if (error)
  551. return error;
  552. bp->b_ops = &xfs_dir3_data_buf_ops;
  553. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
  554. /*
  555. * Initialize the header.
  556. */
  557. hdr = bp->b_addr;
  558. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  559. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  560. memset(hdr3, 0, sizeof(*hdr3));
  561. hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
  562. hdr3->blkno = cpu_to_be64(bp->b_bn);
  563. hdr3->owner = cpu_to_be64(dp->i_ino);
  564. uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
  565. } else
  566. hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
  567. bf = dp->d_ops->data_bestfree_p(hdr);
  568. bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset);
  569. for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
  570. bf[i].length = 0;
  571. bf[i].offset = 0;
  572. }
  573. /*
  574. * Set up an unused entry for the block's body.
  575. */
  576. dup = dp->d_ops->data_unused_p(hdr);
  577. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  578. t = mp->m_dirblksize - (uint)dp->d_ops->data_entry_offset;
  579. bf[0].length = cpu_to_be16(t);
  580. dup->length = cpu_to_be16(t);
  581. *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
  582. /*
  583. * Log it and return it.
  584. */
  585. xfs_dir2_data_log_header(tp, dp, bp);
  586. xfs_dir2_data_log_unused(tp, bp, dup);
  587. *bpp = bp;
  588. return 0;
  589. }
  590. /*
  591. * Log an active data entry from the block.
  592. */
  593. void
  594. xfs_dir2_data_log_entry(
  595. struct xfs_trans *tp,
  596. struct xfs_inode *dp,
  597. struct xfs_buf *bp,
  598. xfs_dir2_data_entry_t *dep) /* data entry pointer */
  599. {
  600. struct xfs_dir2_data_hdr *hdr = bp->b_addr;
  601. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  602. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  603. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  604. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  605. xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
  606. (uint)((char *)(dp->d_ops->data_entry_tag_p(dep) + 1) -
  607. (char *)hdr - 1));
  608. }
  609. /*
  610. * Log a data block header.
  611. */
  612. void
  613. xfs_dir2_data_log_header(
  614. struct xfs_trans *tp,
  615. struct xfs_inode *dp,
  616. struct xfs_buf *bp)
  617. {
  618. #ifdef DEBUG
  619. struct xfs_dir2_data_hdr *hdr = bp->b_addr;
  620. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  621. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  622. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  623. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  624. #endif
  625. xfs_trans_log_buf(tp, bp, 0, dp->d_ops->data_entry_offset - 1);
  626. }
  627. /*
  628. * Log a data unused entry.
  629. */
  630. void
  631. xfs_dir2_data_log_unused(
  632. struct xfs_trans *tp,
  633. struct xfs_buf *bp,
  634. xfs_dir2_data_unused_t *dup) /* data unused pointer */
  635. {
  636. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  637. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  638. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  639. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  640. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  641. /*
  642. * Log the first part of the unused entry.
  643. */
  644. xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
  645. (uint)((char *)&dup->length + sizeof(dup->length) -
  646. 1 - (char *)hdr));
  647. /*
  648. * Log the end (tag) of the unused entry.
  649. */
  650. xfs_trans_log_buf(tp, bp,
  651. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
  652. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
  653. sizeof(xfs_dir2_data_off_t) - 1));
  654. }
  655. /*
  656. * Make a byte range in the data block unused.
  657. * Its current contents are unimportant.
  658. */
  659. void
  660. xfs_dir2_data_make_free(
  661. struct xfs_trans *tp,
  662. struct xfs_inode *dp,
  663. struct xfs_buf *bp,
  664. xfs_dir2_data_aoff_t offset, /* starting byte offset */
  665. xfs_dir2_data_aoff_t len, /* length in bytes */
  666. int *needlogp, /* out: log header */
  667. int *needscanp) /* out: regen bestfree */
  668. {
  669. xfs_dir2_data_hdr_t *hdr; /* data block pointer */
  670. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  671. char *endptr; /* end of data area */
  672. xfs_mount_t *mp; /* filesystem mount point */
  673. int needscan; /* need to regen bestfree */
  674. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  675. xfs_dir2_data_unused_t *postdup; /* unused entry after us */
  676. xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
  677. struct xfs_dir2_data_free *bf;
  678. mp = tp->t_mountp;
  679. hdr = bp->b_addr;
  680. /*
  681. * Figure out where the end of the data area is.
  682. */
  683. if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  684. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
  685. endptr = (char *)hdr + mp->m_dirblksize;
  686. else {
  687. xfs_dir2_block_tail_t *btp; /* block tail */
  688. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  689. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  690. btp = xfs_dir2_block_tail_p(mp, hdr);
  691. endptr = (char *)xfs_dir2_block_leaf_p(btp);
  692. }
  693. /*
  694. * If this isn't the start of the block, then back up to
  695. * the previous entry and see if it's free.
  696. */
  697. if (offset > dp->d_ops->data_entry_offset) {
  698. __be16 *tagp; /* tag just before us */
  699. tagp = (__be16 *)((char *)hdr + offset) - 1;
  700. prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  701. if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  702. prevdup = NULL;
  703. } else
  704. prevdup = NULL;
  705. /*
  706. * If this isn't the end of the block, see if the entry after
  707. * us is free.
  708. */
  709. if ((char *)hdr + offset + len < endptr) {
  710. postdup =
  711. (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  712. if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  713. postdup = NULL;
  714. } else
  715. postdup = NULL;
  716. ASSERT(*needscanp == 0);
  717. needscan = 0;
  718. /*
  719. * Previous and following entries are both free,
  720. * merge everything into a single free entry.
  721. */
  722. bf = dp->d_ops->data_bestfree_p(hdr);
  723. if (prevdup && postdup) {
  724. xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
  725. /*
  726. * See if prevdup and/or postdup are in bestfree table.
  727. */
  728. dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
  729. dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
  730. /*
  731. * We need a rescan unless there are exactly 2 free entries
  732. * namely our two. Then we know what's happening, otherwise
  733. * since the third bestfree is there, there might be more
  734. * entries.
  735. */
  736. needscan = (bf[2].length != 0);
  737. /*
  738. * Fix up the new big freespace.
  739. */
  740. be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
  741. *xfs_dir2_data_unused_tag_p(prevdup) =
  742. cpu_to_be16((char *)prevdup - (char *)hdr);
  743. xfs_dir2_data_log_unused(tp, bp, prevdup);
  744. if (!needscan) {
  745. /*
  746. * Has to be the case that entries 0 and 1 are
  747. * dfp and dfp2 (don't know which is which), and
  748. * entry 2 is empty.
  749. * Remove entry 1 first then entry 0.
  750. */
  751. ASSERT(dfp && dfp2);
  752. if (dfp == &bf[1]) {
  753. dfp = &bf[0];
  754. ASSERT(dfp2 == dfp);
  755. dfp2 = &bf[1];
  756. }
  757. xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
  758. xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
  759. /*
  760. * Now insert the new entry.
  761. */
  762. dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
  763. needlogp);
  764. ASSERT(dfp == &bf[0]);
  765. ASSERT(dfp->length == prevdup->length);
  766. ASSERT(!dfp[1].length);
  767. ASSERT(!dfp[2].length);
  768. }
  769. }
  770. /*
  771. * The entry before us is free, merge with it.
  772. */
  773. else if (prevdup) {
  774. dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
  775. be16_add_cpu(&prevdup->length, len);
  776. *xfs_dir2_data_unused_tag_p(prevdup) =
  777. cpu_to_be16((char *)prevdup - (char *)hdr);
  778. xfs_dir2_data_log_unused(tp, bp, prevdup);
  779. /*
  780. * If the previous entry was in the table, the new entry
  781. * is longer, so it will be in the table too. Remove
  782. * the old one and add the new one.
  783. */
  784. if (dfp) {
  785. xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
  786. xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
  787. }
  788. /*
  789. * Otherwise we need a scan if the new entry is big enough.
  790. */
  791. else {
  792. needscan = be16_to_cpu(prevdup->length) >
  793. be16_to_cpu(bf[2].length);
  794. }
  795. }
  796. /*
  797. * The following entry is free, merge with it.
  798. */
  799. else if (postdup) {
  800. dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
  801. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  802. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  803. newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
  804. *xfs_dir2_data_unused_tag_p(newdup) =
  805. cpu_to_be16((char *)newdup - (char *)hdr);
  806. xfs_dir2_data_log_unused(tp, bp, newdup);
  807. /*
  808. * If the following entry was in the table, the new entry
  809. * is longer, so it will be in the table too. Remove
  810. * the old one and add the new one.
  811. */
  812. if (dfp) {
  813. xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
  814. xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
  815. }
  816. /*
  817. * Otherwise we need a scan if the new entry is big enough.
  818. */
  819. else {
  820. needscan = be16_to_cpu(newdup->length) >
  821. be16_to_cpu(bf[2].length);
  822. }
  823. }
  824. /*
  825. * Neither neighbor is free. Make a new entry.
  826. */
  827. else {
  828. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  829. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  830. newdup->length = cpu_to_be16(len);
  831. *xfs_dir2_data_unused_tag_p(newdup) =
  832. cpu_to_be16((char *)newdup - (char *)hdr);
  833. xfs_dir2_data_log_unused(tp, bp, newdup);
  834. xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
  835. }
  836. *needscanp = needscan;
  837. }
  838. /*
  839. * Take a byte range out of an existing unused space and make it un-free.
  840. */
  841. void
  842. xfs_dir2_data_use_free(
  843. struct xfs_trans *tp,
  844. struct xfs_inode *dp,
  845. struct xfs_buf *bp,
  846. xfs_dir2_data_unused_t *dup, /* unused entry */
  847. xfs_dir2_data_aoff_t offset, /* starting offset to use */
  848. xfs_dir2_data_aoff_t len, /* length to use */
  849. int *needlogp, /* out: need to log header */
  850. int *needscanp) /* out: need regen bestfree */
  851. {
  852. xfs_dir2_data_hdr_t *hdr; /* data block header */
  853. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  854. int matchback; /* matches end of freespace */
  855. int matchfront; /* matches start of freespace */
  856. int needscan; /* need to regen bestfree */
  857. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  858. xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
  859. int oldlen; /* old unused entry's length */
  860. struct xfs_dir2_data_free *bf;
  861. hdr = bp->b_addr;
  862. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  863. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  864. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  865. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  866. ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
  867. ASSERT(offset >= (char *)dup - (char *)hdr);
  868. ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
  869. ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  870. /*
  871. * Look up the entry in the bestfree table.
  872. */
  873. oldlen = be16_to_cpu(dup->length);
  874. bf = dp->d_ops->data_bestfree_p(hdr);
  875. dfp = xfs_dir2_data_freefind(hdr, bf, dup);
  876. ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
  877. /*
  878. * Check for alignment with front and back of the entry.
  879. */
  880. matchfront = (char *)dup - (char *)hdr == offset;
  881. matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
  882. ASSERT(*needscanp == 0);
  883. needscan = 0;
  884. /*
  885. * If we matched it exactly we just need to get rid of it from
  886. * the bestfree table.
  887. */
  888. if (matchfront && matchback) {
  889. if (dfp) {
  890. needscan = (bf[2].offset != 0);
  891. if (!needscan)
  892. xfs_dir2_data_freeremove(hdr, bf, dfp,
  893. needlogp);
  894. }
  895. }
  896. /*
  897. * We match the first part of the entry.
  898. * Make a new entry with the remaining freespace.
  899. */
  900. else if (matchfront) {
  901. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  902. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  903. newdup->length = cpu_to_be16(oldlen - len);
  904. *xfs_dir2_data_unused_tag_p(newdup) =
  905. cpu_to_be16((char *)newdup - (char *)hdr);
  906. xfs_dir2_data_log_unused(tp, bp, newdup);
  907. /*
  908. * If it was in the table, remove it and add the new one.
  909. */
  910. if (dfp) {
  911. xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
  912. dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
  913. needlogp);
  914. ASSERT(dfp != NULL);
  915. ASSERT(dfp->length == newdup->length);
  916. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  917. /*
  918. * If we got inserted at the last slot,
  919. * that means we don't know if there was a better
  920. * choice for the last slot, or not. Rescan.
  921. */
  922. needscan = dfp == &bf[2];
  923. }
  924. }
  925. /*
  926. * We match the last part of the entry.
  927. * Trim the allocated space off the tail of the entry.
  928. */
  929. else if (matchback) {
  930. newdup = dup;
  931. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  932. *xfs_dir2_data_unused_tag_p(newdup) =
  933. cpu_to_be16((char *)newdup - (char *)hdr);
  934. xfs_dir2_data_log_unused(tp, bp, newdup);
  935. /*
  936. * If it was in the table, remove it and add the new one.
  937. */
  938. if (dfp) {
  939. xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
  940. dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
  941. needlogp);
  942. ASSERT(dfp != NULL);
  943. ASSERT(dfp->length == newdup->length);
  944. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  945. /*
  946. * If we got inserted at the last slot,
  947. * that means we don't know if there was a better
  948. * choice for the last slot, or not. Rescan.
  949. */
  950. needscan = dfp == &bf[2];
  951. }
  952. }
  953. /*
  954. * Poking out the middle of an entry.
  955. * Make two new entries.
  956. */
  957. else {
  958. newdup = dup;
  959. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  960. *xfs_dir2_data_unused_tag_p(newdup) =
  961. cpu_to_be16((char *)newdup - (char *)hdr);
  962. xfs_dir2_data_log_unused(tp, bp, newdup);
  963. newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  964. newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  965. newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
  966. *xfs_dir2_data_unused_tag_p(newdup2) =
  967. cpu_to_be16((char *)newdup2 - (char *)hdr);
  968. xfs_dir2_data_log_unused(tp, bp, newdup2);
  969. /*
  970. * If the old entry was in the table, we need to scan
  971. * if the 3rd entry was valid, since these entries
  972. * are smaller than the old one.
  973. * If we don't need to scan that means there were 1 or 2
  974. * entries in the table, and removing the old and adding
  975. * the 2 new will work.
  976. */
  977. if (dfp) {
  978. needscan = (bf[2].length != 0);
  979. if (!needscan) {
  980. xfs_dir2_data_freeremove(hdr, bf, dfp,
  981. needlogp);
  982. xfs_dir2_data_freeinsert(hdr, bf, newdup,
  983. needlogp);
  984. xfs_dir2_data_freeinsert(hdr, bf, newdup2,
  985. needlogp);
  986. }
  987. }
  988. }
  989. *needscanp = needscan;
  990. }