xfs_dir2_data.c 33 KB

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