xfs_dir2_data.c 30 KB

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