xfs_dir2_data.c 32 KB

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