xfs_dir2_data.c 30 KB

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