xfs_dir2_data.c 34 KB

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