xfs_dir2_data.c 32 KB

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