xfs_dir2_data.c 31 KB

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