xfs_dir2_data.c 30 KB

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