xfs_dir2_data.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. return xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
  284. XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
  285. }
  286. int
  287. xfs_dir3_data_readahead(
  288. struct xfs_trans *tp,
  289. struct xfs_inode *dp,
  290. xfs_dablk_t bno,
  291. xfs_daddr_t mapped_bno)
  292. {
  293. return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
  294. XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
  295. }
  296. /*
  297. * Given a data block and an unused entry from that block,
  298. * return the bestfree entry if any that corresponds to it.
  299. */
  300. STATIC xfs_dir2_data_free_t *
  301. xfs_dir2_data_freefind(
  302. xfs_dir2_data_hdr_t *hdr, /* data block */
  303. xfs_dir2_data_unused_t *dup) /* data unused entry */
  304. {
  305. xfs_dir2_data_free_t *dfp; /* bestfree entry */
  306. xfs_dir2_data_aoff_t off; /* offset value needed */
  307. struct xfs_dir2_data_free *bf;
  308. #if defined(DEBUG) && defined(__KERNEL__)
  309. int matched; /* matched the value */
  310. int seenzero; /* saw a 0 bestfree entry */
  311. #endif
  312. off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
  313. bf = xfs_dir3_data_bestfree_p(hdr);
  314. #if defined(DEBUG) && defined(__KERNEL__)
  315. /*
  316. * Validate some consistency in the bestfree table.
  317. * Check order, non-overlapping entries, and if we find the
  318. * one we're looking for it has to be exact.
  319. */
  320. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  321. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  322. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  323. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  324. for (dfp = &bf[0], seenzero = matched = 0;
  325. dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
  326. dfp++) {
  327. if (!dfp->offset) {
  328. ASSERT(!dfp->length);
  329. seenzero = 1;
  330. continue;
  331. }
  332. ASSERT(seenzero == 0);
  333. if (be16_to_cpu(dfp->offset) == off) {
  334. matched = 1;
  335. ASSERT(dfp->length == dup->length);
  336. } else if (off < be16_to_cpu(dfp->offset))
  337. ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
  338. else
  339. ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
  340. ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
  341. if (dfp > &bf[0])
  342. ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
  343. }
  344. #endif
  345. /*
  346. * If this is smaller than the smallest bestfree entry,
  347. * it can't be there since they're sorted.
  348. */
  349. if (be16_to_cpu(dup->length) <
  350. be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
  351. return NULL;
  352. /*
  353. * Look at the three bestfree entries for our guy.
  354. */
  355. for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
  356. if (!dfp->offset)
  357. return NULL;
  358. if (be16_to_cpu(dfp->offset) == off)
  359. return dfp;
  360. }
  361. /*
  362. * Didn't find it. This only happens if there are duplicate lengths.
  363. */
  364. return NULL;
  365. }
  366. /*
  367. * Insert an unused-space entry into the bestfree table.
  368. */
  369. xfs_dir2_data_free_t * /* entry inserted */
  370. xfs_dir2_data_freeinsert(
  371. xfs_dir2_data_hdr_t *hdr, /* data block pointer */
  372. xfs_dir2_data_unused_t *dup, /* unused space */
  373. int *loghead) /* log the data header (out) */
  374. {
  375. xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
  376. xfs_dir2_data_free_t new; /* new bestfree entry */
  377. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  378. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  379. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  380. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  381. dfp = xfs_dir3_data_bestfree_p(hdr);
  382. new.length = dup->length;
  383. new.offset = cpu_to_be16((char *)dup - (char *)hdr);
  384. /*
  385. * Insert at position 0, 1, or 2; or not at all.
  386. */
  387. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
  388. dfp[2] = dfp[1];
  389. dfp[1] = dfp[0];
  390. dfp[0] = new;
  391. *loghead = 1;
  392. return &dfp[0];
  393. }
  394. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
  395. dfp[2] = dfp[1];
  396. dfp[1] = new;
  397. *loghead = 1;
  398. return &dfp[1];
  399. }
  400. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
  401. dfp[2] = new;
  402. *loghead = 1;
  403. return &dfp[2];
  404. }
  405. return NULL;
  406. }
  407. /*
  408. * Remove a bestfree entry from the table.
  409. */
  410. STATIC void
  411. xfs_dir2_data_freeremove(
  412. xfs_dir2_data_hdr_t *hdr, /* data block header */
  413. xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
  414. int *loghead) /* out: log data header */
  415. {
  416. struct xfs_dir2_data_free *bf;
  417. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  418. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  419. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  420. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  421. /*
  422. * It's the first entry, slide the next 2 up.
  423. */
  424. bf = xfs_dir3_data_bestfree_p(hdr);
  425. if (dfp == &bf[0]) {
  426. bf[0] = bf[1];
  427. bf[1] = bf[2];
  428. }
  429. /*
  430. * It's the second entry, slide the 3rd entry up.
  431. */
  432. else if (dfp == &bf[1])
  433. bf[1] = bf[2];
  434. /*
  435. * Must be the last entry.
  436. */
  437. else
  438. ASSERT(dfp == &bf[2]);
  439. /*
  440. * Clear the 3rd entry, must be zero now.
  441. */
  442. bf[2].length = 0;
  443. bf[2].offset = 0;
  444. *loghead = 1;
  445. }
  446. /*
  447. * Given a data block, reconstruct its bestfree map.
  448. */
  449. void
  450. xfs_dir2_data_freescan(
  451. xfs_mount_t *mp, /* filesystem mount point */
  452. xfs_dir2_data_hdr_t *hdr, /* data block header */
  453. int *loghead) /* out: log data header */
  454. {
  455. xfs_dir2_block_tail_t *btp; /* block tail */
  456. xfs_dir2_data_entry_t *dep; /* active data entry */
  457. xfs_dir2_data_unused_t *dup; /* unused data entry */
  458. struct xfs_dir2_data_free *bf;
  459. char *endp; /* end of block's data */
  460. char *p; /* current entry pointer */
  461. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  462. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  463. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  464. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  465. /*
  466. * Start by clearing the table.
  467. */
  468. bf = xfs_dir3_data_bestfree_p(hdr);
  469. memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
  470. *loghead = 1;
  471. /*
  472. * Set up pointers.
  473. */
  474. p = (char *)xfs_dir3_data_entry_p(hdr);
  475. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  476. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  477. btp = xfs_dir2_block_tail_p(mp, hdr);
  478. endp = (char *)xfs_dir2_block_leaf_p(btp);
  479. } else
  480. endp = (char *)hdr + mp->m_dirblksize;
  481. /*
  482. * Loop over the block's entries.
  483. */
  484. while (p < endp) {
  485. dup = (xfs_dir2_data_unused_t *)p;
  486. /*
  487. * If it's a free entry, insert it.
  488. */
  489. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  490. ASSERT((char *)dup - (char *)hdr ==
  491. be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  492. xfs_dir2_data_freeinsert(hdr, dup, loghead);
  493. p += be16_to_cpu(dup->length);
  494. }
  495. /*
  496. * For active entries, check their tags and skip them.
  497. */
  498. else {
  499. dep = (xfs_dir2_data_entry_t *)p;
  500. ASSERT((char *)dep - (char *)hdr ==
  501. be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
  502. p += xfs_dir2_data_entsize(dep->namelen);
  503. }
  504. }
  505. }
  506. /*
  507. * Initialize a data block at the given block number in the directory.
  508. * Give back the buffer for the created block.
  509. */
  510. int /* error */
  511. xfs_dir3_data_init(
  512. xfs_da_args_t *args, /* directory operation args */
  513. xfs_dir2_db_t blkno, /* logical dir block number */
  514. struct xfs_buf **bpp) /* output block buffer */
  515. {
  516. struct xfs_buf *bp; /* block buffer */
  517. xfs_dir2_data_hdr_t *hdr; /* data block header */
  518. xfs_inode_t *dp; /* incore directory inode */
  519. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  520. struct xfs_dir2_data_free *bf;
  521. int error; /* error return value */
  522. int i; /* bestfree index */
  523. xfs_mount_t *mp; /* filesystem mount point */
  524. xfs_trans_t *tp; /* transaction pointer */
  525. int t; /* temp */
  526. dp = args->dp;
  527. mp = dp->i_mount;
  528. tp = args->trans;
  529. /*
  530. * Get the buffer set up for the block.
  531. */
  532. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
  533. XFS_DATA_FORK);
  534. if (error)
  535. return error;
  536. bp->b_ops = &xfs_dir3_data_buf_ops;
  537. /*
  538. * Initialize the header.
  539. */
  540. hdr = bp->b_addr;
  541. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  542. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  543. memset(hdr3, 0, sizeof(*hdr3));
  544. hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
  545. hdr3->blkno = cpu_to_be64(bp->b_bn);
  546. hdr3->owner = cpu_to_be64(dp->i_ino);
  547. uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
  548. } else
  549. hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
  550. bf = xfs_dir3_data_bestfree_p(hdr);
  551. bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
  552. for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
  553. bf[i].length = 0;
  554. bf[i].offset = 0;
  555. }
  556. /*
  557. * Set up an unused entry for the block's body.
  558. */
  559. dup = xfs_dir3_data_unused_p(hdr);
  560. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  561. t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
  562. bf[0].length = cpu_to_be16(t);
  563. dup->length = cpu_to_be16(t);
  564. *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
  565. /*
  566. * Log it and return it.
  567. */
  568. xfs_dir2_data_log_header(tp, bp);
  569. xfs_dir2_data_log_unused(tp, bp, dup);
  570. *bpp = bp;
  571. return 0;
  572. }
  573. /*
  574. * Log an active data entry from the block.
  575. */
  576. void
  577. xfs_dir2_data_log_entry(
  578. struct xfs_trans *tp,
  579. struct xfs_buf *bp,
  580. xfs_dir2_data_entry_t *dep) /* data entry pointer */
  581. {
  582. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  583. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  584. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  585. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  586. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  587. xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
  588. (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
  589. (char *)hdr - 1));
  590. }
  591. /*
  592. * Log a data block header.
  593. */
  594. void
  595. xfs_dir2_data_log_header(
  596. struct xfs_trans *tp,
  597. struct xfs_buf *bp)
  598. {
  599. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  600. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  601. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  602. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  603. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  604. xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
  605. }
  606. /*
  607. * Log a data unused entry.
  608. */
  609. void
  610. xfs_dir2_data_log_unused(
  611. struct xfs_trans *tp,
  612. struct xfs_buf *bp,
  613. xfs_dir2_data_unused_t *dup) /* data unused pointer */
  614. {
  615. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  616. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  617. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  618. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  619. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  620. /*
  621. * Log the first part of the unused entry.
  622. */
  623. xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
  624. (uint)((char *)&dup->length + sizeof(dup->length) -
  625. 1 - (char *)hdr));
  626. /*
  627. * Log the end (tag) of the unused entry.
  628. */
  629. xfs_trans_log_buf(tp, bp,
  630. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
  631. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
  632. sizeof(xfs_dir2_data_off_t) - 1));
  633. }
  634. /*
  635. * Make a byte range in the data block unused.
  636. * Its current contents are unimportant.
  637. */
  638. void
  639. xfs_dir2_data_make_free(
  640. struct xfs_trans *tp,
  641. struct xfs_buf *bp,
  642. xfs_dir2_data_aoff_t offset, /* starting byte offset */
  643. xfs_dir2_data_aoff_t len, /* length in bytes */
  644. int *needlogp, /* out: log header */
  645. int *needscanp) /* out: regen bestfree */
  646. {
  647. xfs_dir2_data_hdr_t *hdr; /* data block pointer */
  648. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  649. char *endptr; /* end of data area */
  650. xfs_mount_t *mp; /* filesystem mount point */
  651. int needscan; /* need to regen bestfree */
  652. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  653. xfs_dir2_data_unused_t *postdup; /* unused entry after us */
  654. xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
  655. struct xfs_dir2_data_free *bf;
  656. mp = tp->t_mountp;
  657. hdr = bp->b_addr;
  658. /*
  659. * Figure out where the end of the data area is.
  660. */
  661. if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  662. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
  663. endptr = (char *)hdr + mp->m_dirblksize;
  664. else {
  665. xfs_dir2_block_tail_t *btp; /* block tail */
  666. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  667. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  668. btp = xfs_dir2_block_tail_p(mp, hdr);
  669. endptr = (char *)xfs_dir2_block_leaf_p(btp);
  670. }
  671. /*
  672. * If this isn't the start of the block, then back up to
  673. * the previous entry and see if it's free.
  674. */
  675. if (offset > xfs_dir3_data_entry_offset(hdr)) {
  676. __be16 *tagp; /* tag just before us */
  677. tagp = (__be16 *)((char *)hdr + offset) - 1;
  678. prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  679. if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  680. prevdup = NULL;
  681. } else
  682. prevdup = NULL;
  683. /*
  684. * If this isn't the end of the block, see if the entry after
  685. * us is free.
  686. */
  687. if ((char *)hdr + offset + len < endptr) {
  688. postdup =
  689. (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  690. if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  691. postdup = NULL;
  692. } else
  693. postdup = NULL;
  694. ASSERT(*needscanp == 0);
  695. needscan = 0;
  696. /*
  697. * Previous and following entries are both free,
  698. * merge everything into a single free entry.
  699. */
  700. bf = xfs_dir3_data_bestfree_p(hdr);
  701. if (prevdup && postdup) {
  702. xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
  703. /*
  704. * See if prevdup and/or postdup are in bestfree table.
  705. */
  706. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  707. dfp2 = xfs_dir2_data_freefind(hdr, postdup);
  708. /*
  709. * We need a rescan unless there are exactly 2 free entries
  710. * namely our two. Then we know what's happening, otherwise
  711. * since the third bestfree is there, there might be more
  712. * entries.
  713. */
  714. needscan = (bf[2].length != 0);
  715. /*
  716. * Fix up the new big freespace.
  717. */
  718. be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
  719. *xfs_dir2_data_unused_tag_p(prevdup) =
  720. cpu_to_be16((char *)prevdup - (char *)hdr);
  721. xfs_dir2_data_log_unused(tp, bp, prevdup);
  722. if (!needscan) {
  723. /*
  724. * Has to be the case that entries 0 and 1 are
  725. * dfp and dfp2 (don't know which is which), and
  726. * entry 2 is empty.
  727. * Remove entry 1 first then entry 0.
  728. */
  729. ASSERT(dfp && dfp2);
  730. if (dfp == &bf[1]) {
  731. dfp = &bf[0];
  732. ASSERT(dfp2 == dfp);
  733. dfp2 = &bf[1];
  734. }
  735. xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
  736. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  737. /*
  738. * Now insert the new entry.
  739. */
  740. dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  741. ASSERT(dfp == &bf[0]);
  742. ASSERT(dfp->length == prevdup->length);
  743. ASSERT(!dfp[1].length);
  744. ASSERT(!dfp[2].length);
  745. }
  746. }
  747. /*
  748. * The entry before us is free, merge with it.
  749. */
  750. else if (prevdup) {
  751. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  752. be16_add_cpu(&prevdup->length, len);
  753. *xfs_dir2_data_unused_tag_p(prevdup) =
  754. cpu_to_be16((char *)prevdup - (char *)hdr);
  755. xfs_dir2_data_log_unused(tp, bp, prevdup);
  756. /*
  757. * If the previous entry was in the table, the new entry
  758. * is longer, so it will be in the table too. Remove
  759. * the old one and add the new one.
  760. */
  761. if (dfp) {
  762. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  763. xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  764. }
  765. /*
  766. * Otherwise we need a scan if the new entry is big enough.
  767. */
  768. else {
  769. needscan = be16_to_cpu(prevdup->length) >
  770. be16_to_cpu(bf[2].length);
  771. }
  772. }
  773. /*
  774. * The following entry is free, merge with it.
  775. */
  776. else if (postdup) {
  777. dfp = xfs_dir2_data_freefind(hdr, postdup);
  778. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  779. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  780. newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
  781. *xfs_dir2_data_unused_tag_p(newdup) =
  782. cpu_to_be16((char *)newdup - (char *)hdr);
  783. xfs_dir2_data_log_unused(tp, bp, newdup);
  784. /*
  785. * If the following entry was in the table, the new entry
  786. * is longer, so it will be in the table too. Remove
  787. * the old one and add the new one.
  788. */
  789. if (dfp) {
  790. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  791. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  792. }
  793. /*
  794. * Otherwise we need a scan if the new entry is big enough.
  795. */
  796. else {
  797. needscan = be16_to_cpu(newdup->length) >
  798. be16_to_cpu(bf[2].length);
  799. }
  800. }
  801. /*
  802. * Neither neighbor is free. Make a new entry.
  803. */
  804. else {
  805. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  806. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  807. newdup->length = cpu_to_be16(len);
  808. *xfs_dir2_data_unused_tag_p(newdup) =
  809. cpu_to_be16((char *)newdup - (char *)hdr);
  810. xfs_dir2_data_log_unused(tp, bp, newdup);
  811. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  812. }
  813. *needscanp = needscan;
  814. }
  815. /*
  816. * Take a byte range out of an existing unused space and make it un-free.
  817. */
  818. void
  819. xfs_dir2_data_use_free(
  820. struct xfs_trans *tp,
  821. struct xfs_buf *bp,
  822. xfs_dir2_data_unused_t *dup, /* unused entry */
  823. xfs_dir2_data_aoff_t offset, /* starting offset to use */
  824. xfs_dir2_data_aoff_t len, /* length to use */
  825. int *needlogp, /* out: need to log header */
  826. int *needscanp) /* out: need regen bestfree */
  827. {
  828. xfs_dir2_data_hdr_t *hdr; /* data block header */
  829. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  830. int matchback; /* matches end of freespace */
  831. int matchfront; /* matches start of freespace */
  832. int needscan; /* need to regen bestfree */
  833. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  834. xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
  835. int oldlen; /* old unused entry's length */
  836. struct xfs_dir2_data_free *bf;
  837. hdr = bp->b_addr;
  838. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  839. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  840. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  841. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  842. ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
  843. ASSERT(offset >= (char *)dup - (char *)hdr);
  844. ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
  845. ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  846. /*
  847. * Look up the entry in the bestfree table.
  848. */
  849. dfp = xfs_dir2_data_freefind(hdr, dup);
  850. oldlen = be16_to_cpu(dup->length);
  851. bf = xfs_dir3_data_bestfree_p(hdr);
  852. ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
  853. /*
  854. * Check for alignment with front and back of the entry.
  855. */
  856. matchfront = (char *)dup - (char *)hdr == offset;
  857. matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
  858. ASSERT(*needscanp == 0);
  859. needscan = 0;
  860. /*
  861. * If we matched it exactly we just need to get rid of it from
  862. * the bestfree table.
  863. */
  864. if (matchfront && matchback) {
  865. if (dfp) {
  866. needscan = (bf[2].offset != 0);
  867. if (!needscan)
  868. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  869. }
  870. }
  871. /*
  872. * We match the first part of the entry.
  873. * Make a new entry with the remaining freespace.
  874. */
  875. else if (matchfront) {
  876. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  877. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  878. newdup->length = cpu_to_be16(oldlen - len);
  879. *xfs_dir2_data_unused_tag_p(newdup) =
  880. cpu_to_be16((char *)newdup - (char *)hdr);
  881. xfs_dir2_data_log_unused(tp, bp, newdup);
  882. /*
  883. * If it was in the table, remove it and add the new one.
  884. */
  885. if (dfp) {
  886. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  887. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  888. ASSERT(dfp != NULL);
  889. ASSERT(dfp->length == newdup->length);
  890. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  891. /*
  892. * If we got inserted at the last slot,
  893. * that means we don't know if there was a better
  894. * choice for the last slot, or not. Rescan.
  895. */
  896. needscan = dfp == &bf[2];
  897. }
  898. }
  899. /*
  900. * We match the last part of the entry.
  901. * Trim the allocated space off the tail of the entry.
  902. */
  903. else if (matchback) {
  904. newdup = dup;
  905. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  906. *xfs_dir2_data_unused_tag_p(newdup) =
  907. cpu_to_be16((char *)newdup - (char *)hdr);
  908. xfs_dir2_data_log_unused(tp, bp, newdup);
  909. /*
  910. * If it was in the table, remove it and add the new one.
  911. */
  912. if (dfp) {
  913. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  914. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  915. ASSERT(dfp != NULL);
  916. ASSERT(dfp->length == newdup->length);
  917. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  918. /*
  919. * If we got inserted at the last slot,
  920. * that means we don't know if there was a better
  921. * choice for the last slot, or not. Rescan.
  922. */
  923. needscan = dfp == &bf[2];
  924. }
  925. }
  926. /*
  927. * Poking out the middle of an entry.
  928. * Make two new entries.
  929. */
  930. else {
  931. newdup = dup;
  932. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  933. *xfs_dir2_data_unused_tag_p(newdup) =
  934. cpu_to_be16((char *)newdup - (char *)hdr);
  935. xfs_dir2_data_log_unused(tp, bp, newdup);
  936. newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  937. newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  938. newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
  939. *xfs_dir2_data_unused_tag_p(newdup2) =
  940. cpu_to_be16((char *)newdup2 - (char *)hdr);
  941. xfs_dir2_data_log_unused(tp, bp, newdup2);
  942. /*
  943. * If the old entry was in the table, we need to scan
  944. * if the 3rd entry was valid, since these entries
  945. * are smaller than the old one.
  946. * If we don't need to scan that means there were 1 or 2
  947. * entries in the table, and removing the old and adding
  948. * the 2 new will work.
  949. */
  950. if (dfp) {
  951. needscan = (bf[2].length != 0);
  952. if (!needscan) {
  953. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  954. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  955. xfs_dir2_data_freeinsert(hdr, newdup2,
  956. needlogp);
  957. }
  958. }
  959. }
  960. *needscanp = needscan;
  961. }