xfs_dir2_data.c 33 KB

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