xfs_dir2_data.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. 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. static void
  196. xfs_dir2_data_write_verify(
  197. struct xfs_buf *bp)
  198. {
  199. xfs_dir2_data_verify(bp);
  200. }
  201. 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. }
  464. ASSERT(bp != NULL);
  465. /*
  466. * Initialize the header.
  467. */
  468. hdr = bp->b_addr;
  469. hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
  470. hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
  471. for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
  472. hdr->bestfree[i].length = 0;
  473. hdr->bestfree[i].offset = 0;
  474. }
  475. /*
  476. * Set up an unused entry for the block's body.
  477. */
  478. dup = (xfs_dir2_data_unused_t *)(hdr + 1);
  479. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  480. t = mp->m_dirblksize - (uint)sizeof(*hdr);
  481. hdr->bestfree[0].length = cpu_to_be16(t);
  482. dup->length = cpu_to_be16(t);
  483. *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
  484. /*
  485. * Log it and return it.
  486. */
  487. xfs_dir2_data_log_header(tp, bp);
  488. xfs_dir2_data_log_unused(tp, bp, dup);
  489. *bpp = bp;
  490. return 0;
  491. }
  492. /*
  493. * Log an active data entry from the block.
  494. */
  495. void
  496. xfs_dir2_data_log_entry(
  497. struct xfs_trans *tp,
  498. struct xfs_buf *bp,
  499. xfs_dir2_data_entry_t *dep) /* data entry pointer */
  500. {
  501. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  502. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  503. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  504. xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
  505. (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
  506. (char *)hdr - 1));
  507. }
  508. /*
  509. * Log a data block header.
  510. */
  511. void
  512. xfs_dir2_data_log_header(
  513. struct xfs_trans *tp,
  514. struct xfs_buf *bp)
  515. {
  516. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  517. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  518. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  519. xfs_trans_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
  520. }
  521. /*
  522. * Log a data unused entry.
  523. */
  524. void
  525. xfs_dir2_data_log_unused(
  526. struct xfs_trans *tp,
  527. struct xfs_buf *bp,
  528. xfs_dir2_data_unused_t *dup) /* data unused pointer */
  529. {
  530. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  531. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  532. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  533. /*
  534. * Log the first part of the unused entry.
  535. */
  536. xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
  537. (uint)((char *)&dup->length + sizeof(dup->length) -
  538. 1 - (char *)hdr));
  539. /*
  540. * Log the end (tag) of the unused entry.
  541. */
  542. xfs_trans_log_buf(tp, bp,
  543. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
  544. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
  545. sizeof(xfs_dir2_data_off_t) - 1));
  546. }
  547. /*
  548. * Make a byte range in the data block unused.
  549. * Its current contents are unimportant.
  550. */
  551. void
  552. xfs_dir2_data_make_free(
  553. struct xfs_trans *tp,
  554. struct xfs_buf *bp,
  555. xfs_dir2_data_aoff_t offset, /* starting byte offset */
  556. xfs_dir2_data_aoff_t len, /* length in bytes */
  557. int *needlogp, /* out: log header */
  558. int *needscanp) /* out: regen bestfree */
  559. {
  560. xfs_dir2_data_hdr_t *hdr; /* data block pointer */
  561. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  562. char *endptr; /* end of data area */
  563. xfs_mount_t *mp; /* filesystem mount point */
  564. int needscan; /* need to regen bestfree */
  565. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  566. xfs_dir2_data_unused_t *postdup; /* unused entry after us */
  567. xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
  568. mp = tp->t_mountp;
  569. hdr = bp->b_addr;
  570. /*
  571. * Figure out where the end of the data area is.
  572. */
  573. if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC))
  574. endptr = (char *)hdr + mp->m_dirblksize;
  575. else {
  576. xfs_dir2_block_tail_t *btp; /* block tail */
  577. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  578. btp = xfs_dir2_block_tail_p(mp, hdr);
  579. endptr = (char *)xfs_dir2_block_leaf_p(btp);
  580. }
  581. /*
  582. * If this isn't the start of the block, then back up to
  583. * the previous entry and see if it's free.
  584. */
  585. if (offset > sizeof(*hdr)) {
  586. __be16 *tagp; /* tag just before us */
  587. tagp = (__be16 *)((char *)hdr + offset) - 1;
  588. prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  589. if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  590. prevdup = NULL;
  591. } else
  592. prevdup = NULL;
  593. /*
  594. * If this isn't the end of the block, see if the entry after
  595. * us is free.
  596. */
  597. if ((char *)hdr + offset + len < endptr) {
  598. postdup =
  599. (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  600. if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  601. postdup = NULL;
  602. } else
  603. postdup = NULL;
  604. ASSERT(*needscanp == 0);
  605. needscan = 0;
  606. /*
  607. * Previous and following entries are both free,
  608. * merge everything into a single free entry.
  609. */
  610. if (prevdup && postdup) {
  611. xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
  612. /*
  613. * See if prevdup and/or postdup are in bestfree table.
  614. */
  615. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  616. dfp2 = xfs_dir2_data_freefind(hdr, postdup);
  617. /*
  618. * We need a rescan unless there are exactly 2 free entries
  619. * namely our two. Then we know what's happening, otherwise
  620. * since the third bestfree is there, there might be more
  621. * entries.
  622. */
  623. needscan = (hdr->bestfree[2].length != 0);
  624. /*
  625. * Fix up the new big freespace.
  626. */
  627. be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
  628. *xfs_dir2_data_unused_tag_p(prevdup) =
  629. cpu_to_be16((char *)prevdup - (char *)hdr);
  630. xfs_dir2_data_log_unused(tp, bp, prevdup);
  631. if (!needscan) {
  632. /*
  633. * Has to be the case that entries 0 and 1 are
  634. * dfp and dfp2 (don't know which is which), and
  635. * entry 2 is empty.
  636. * Remove entry 1 first then entry 0.
  637. */
  638. ASSERT(dfp && dfp2);
  639. if (dfp == &hdr->bestfree[1]) {
  640. dfp = &hdr->bestfree[0];
  641. ASSERT(dfp2 == dfp);
  642. dfp2 = &hdr->bestfree[1];
  643. }
  644. xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
  645. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  646. /*
  647. * Now insert the new entry.
  648. */
  649. dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  650. ASSERT(dfp == &hdr->bestfree[0]);
  651. ASSERT(dfp->length == prevdup->length);
  652. ASSERT(!dfp[1].length);
  653. ASSERT(!dfp[2].length);
  654. }
  655. }
  656. /*
  657. * The entry before us is free, merge with it.
  658. */
  659. else if (prevdup) {
  660. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  661. be16_add_cpu(&prevdup->length, len);
  662. *xfs_dir2_data_unused_tag_p(prevdup) =
  663. cpu_to_be16((char *)prevdup - (char *)hdr);
  664. xfs_dir2_data_log_unused(tp, bp, prevdup);
  665. /*
  666. * If the previous entry was in the table, the new entry
  667. * is longer, so it will be in the table too. Remove
  668. * the old one and add the new one.
  669. */
  670. if (dfp) {
  671. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  672. xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  673. }
  674. /*
  675. * Otherwise we need a scan if the new entry is big enough.
  676. */
  677. else {
  678. needscan = be16_to_cpu(prevdup->length) >
  679. be16_to_cpu(hdr->bestfree[2].length);
  680. }
  681. }
  682. /*
  683. * The following entry is free, merge with it.
  684. */
  685. else if (postdup) {
  686. dfp = xfs_dir2_data_freefind(hdr, postdup);
  687. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  688. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  689. newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
  690. *xfs_dir2_data_unused_tag_p(newdup) =
  691. cpu_to_be16((char *)newdup - (char *)hdr);
  692. xfs_dir2_data_log_unused(tp, bp, newdup);
  693. /*
  694. * If the following entry was in the table, the new entry
  695. * is longer, so it will be in the table too. Remove
  696. * the old one and add the new one.
  697. */
  698. if (dfp) {
  699. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  700. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  701. }
  702. /*
  703. * Otherwise we need a scan if the new entry is big enough.
  704. */
  705. else {
  706. needscan = be16_to_cpu(newdup->length) >
  707. be16_to_cpu(hdr->bestfree[2].length);
  708. }
  709. }
  710. /*
  711. * Neither neighbor is free. Make a new entry.
  712. */
  713. else {
  714. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  715. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  716. newdup->length = cpu_to_be16(len);
  717. *xfs_dir2_data_unused_tag_p(newdup) =
  718. cpu_to_be16((char *)newdup - (char *)hdr);
  719. xfs_dir2_data_log_unused(tp, bp, newdup);
  720. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  721. }
  722. *needscanp = needscan;
  723. }
  724. /*
  725. * Take a byte range out of an existing unused space and make it un-free.
  726. */
  727. void
  728. xfs_dir2_data_use_free(
  729. struct xfs_trans *tp,
  730. struct xfs_buf *bp,
  731. xfs_dir2_data_unused_t *dup, /* unused entry */
  732. xfs_dir2_data_aoff_t offset, /* starting offset to use */
  733. xfs_dir2_data_aoff_t len, /* length to use */
  734. int *needlogp, /* out: need to log header */
  735. int *needscanp) /* out: need regen bestfree */
  736. {
  737. xfs_dir2_data_hdr_t *hdr; /* data block header */
  738. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  739. int matchback; /* matches end of freespace */
  740. int matchfront; /* matches start of freespace */
  741. int needscan; /* need to regen bestfree */
  742. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  743. xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
  744. int oldlen; /* old unused entry's length */
  745. hdr = bp->b_addr;
  746. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  747. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  748. ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
  749. ASSERT(offset >= (char *)dup - (char *)hdr);
  750. ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
  751. ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  752. /*
  753. * Look up the entry in the bestfree table.
  754. */
  755. dfp = xfs_dir2_data_freefind(hdr, dup);
  756. oldlen = be16_to_cpu(dup->length);
  757. ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
  758. /*
  759. * Check for alignment with front and back of the entry.
  760. */
  761. matchfront = (char *)dup - (char *)hdr == offset;
  762. matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
  763. ASSERT(*needscanp == 0);
  764. needscan = 0;
  765. /*
  766. * If we matched it exactly we just need to get rid of it from
  767. * the bestfree table.
  768. */
  769. if (matchfront && matchback) {
  770. if (dfp) {
  771. needscan = (hdr->bestfree[2].offset != 0);
  772. if (!needscan)
  773. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  774. }
  775. }
  776. /*
  777. * We match the first part of the entry.
  778. * Make a new entry with the remaining freespace.
  779. */
  780. else if (matchfront) {
  781. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  782. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  783. newdup->length = cpu_to_be16(oldlen - len);
  784. *xfs_dir2_data_unused_tag_p(newdup) =
  785. cpu_to_be16((char *)newdup - (char *)hdr);
  786. xfs_dir2_data_log_unused(tp, bp, newdup);
  787. /*
  788. * If it was in the table, remove it and add the new one.
  789. */
  790. if (dfp) {
  791. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  792. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  793. ASSERT(dfp != NULL);
  794. ASSERT(dfp->length == newdup->length);
  795. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  796. /*
  797. * If we got inserted at the last slot,
  798. * that means we don't know if there was a better
  799. * choice for the last slot, or not. Rescan.
  800. */
  801. needscan = dfp == &hdr->bestfree[2];
  802. }
  803. }
  804. /*
  805. * We match the last part of the entry.
  806. * Trim the allocated space off the tail of the entry.
  807. */
  808. else if (matchback) {
  809. newdup = dup;
  810. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  811. *xfs_dir2_data_unused_tag_p(newdup) =
  812. cpu_to_be16((char *)newdup - (char *)hdr);
  813. xfs_dir2_data_log_unused(tp, bp, newdup);
  814. /*
  815. * If it was in the table, remove it and add the new one.
  816. */
  817. if (dfp) {
  818. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  819. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  820. ASSERT(dfp != NULL);
  821. ASSERT(dfp->length == newdup->length);
  822. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  823. /*
  824. * If we got inserted at the last slot,
  825. * that means we don't know if there was a better
  826. * choice for the last slot, or not. Rescan.
  827. */
  828. needscan = dfp == &hdr->bestfree[2];
  829. }
  830. }
  831. /*
  832. * Poking out the middle of an entry.
  833. * Make two new entries.
  834. */
  835. else {
  836. newdup = dup;
  837. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  838. *xfs_dir2_data_unused_tag_p(newdup) =
  839. cpu_to_be16((char *)newdup - (char *)hdr);
  840. xfs_dir2_data_log_unused(tp, bp, newdup);
  841. newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  842. newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  843. newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
  844. *xfs_dir2_data_unused_tag_p(newdup2) =
  845. cpu_to_be16((char *)newdup2 - (char *)hdr);
  846. xfs_dir2_data_log_unused(tp, bp, newdup2);
  847. /*
  848. * If the old entry was in the table, we need to scan
  849. * if the 3rd entry was valid, since these entries
  850. * are smaller than the old one.
  851. * If we don't need to scan that means there were 1 or 2
  852. * entries in the table, and removing the old and adding
  853. * the 2 new will work.
  854. */
  855. if (dfp) {
  856. needscan = (hdr->bestfree[2].length != 0);
  857. if (!needscan) {
  858. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  859. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  860. xfs_dir2_data_freeinsert(hdr, newdup2,
  861. needlogp);
  862. }
  863. }
  864. }
  865. *needscanp = needscan;
  866. }