xfs_dir2_data.c 26 KB

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