xfs_dir2_data.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. bp->b_iodone = NULL;
  195. xfs_buf_ioend(bp, 0);
  196. }
  197. int
  198. xfs_dir2_data_read(
  199. struct xfs_trans *tp,
  200. struct xfs_inode *dp,
  201. xfs_dablk_t bno,
  202. xfs_daddr_t mapped_bno,
  203. struct xfs_buf **bpp)
  204. {
  205. return xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
  206. XFS_DATA_FORK, xfs_dir2_data_verify);
  207. }
  208. /*
  209. * Given a data block and an unused entry from that block,
  210. * return the bestfree entry if any that corresponds to it.
  211. */
  212. STATIC xfs_dir2_data_free_t *
  213. xfs_dir2_data_freefind(
  214. xfs_dir2_data_hdr_t *hdr, /* data block */
  215. xfs_dir2_data_unused_t *dup) /* data unused entry */
  216. {
  217. xfs_dir2_data_free_t *dfp; /* bestfree entry */
  218. xfs_dir2_data_aoff_t off; /* offset value needed */
  219. #if defined(DEBUG) && defined(__KERNEL__)
  220. int matched; /* matched the value */
  221. int seenzero; /* saw a 0 bestfree entry */
  222. #endif
  223. off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
  224. #if defined(DEBUG) && defined(__KERNEL__)
  225. /*
  226. * Validate some consistency in the bestfree table.
  227. * Check order, non-overlapping entries, and if we find the
  228. * one we're looking for it has to be exact.
  229. */
  230. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  231. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  232. for (dfp = &hdr->bestfree[0], seenzero = matched = 0;
  233. dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
  234. dfp++) {
  235. if (!dfp->offset) {
  236. ASSERT(!dfp->length);
  237. seenzero = 1;
  238. continue;
  239. }
  240. ASSERT(seenzero == 0);
  241. if (be16_to_cpu(dfp->offset) == off) {
  242. matched = 1;
  243. ASSERT(dfp->length == dup->length);
  244. } else if (off < be16_to_cpu(dfp->offset))
  245. ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
  246. else
  247. ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
  248. ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
  249. if (dfp > &hdr->bestfree[0])
  250. ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
  251. }
  252. #endif
  253. /*
  254. * If this is smaller than the smallest bestfree entry,
  255. * it can't be there since they're sorted.
  256. */
  257. if (be16_to_cpu(dup->length) <
  258. be16_to_cpu(hdr->bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
  259. return NULL;
  260. /*
  261. * Look at the three bestfree entries for our guy.
  262. */
  263. for (dfp = &hdr->bestfree[0];
  264. dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
  265. dfp++) {
  266. if (!dfp->offset)
  267. return NULL;
  268. if (be16_to_cpu(dfp->offset) == off)
  269. return dfp;
  270. }
  271. /*
  272. * Didn't find it. This only happens if there are duplicate lengths.
  273. */
  274. return NULL;
  275. }
  276. /*
  277. * Insert an unused-space entry into the bestfree table.
  278. */
  279. xfs_dir2_data_free_t * /* entry inserted */
  280. xfs_dir2_data_freeinsert(
  281. xfs_dir2_data_hdr_t *hdr, /* data block pointer */
  282. xfs_dir2_data_unused_t *dup, /* unused space */
  283. int *loghead) /* log the data header (out) */
  284. {
  285. xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
  286. xfs_dir2_data_free_t new; /* new bestfree entry */
  287. #ifdef __KERNEL__
  288. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  289. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  290. #endif
  291. dfp = hdr->bestfree;
  292. new.length = dup->length;
  293. new.offset = cpu_to_be16((char *)dup - (char *)hdr);
  294. /*
  295. * Insert at position 0, 1, or 2; or not at all.
  296. */
  297. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
  298. dfp[2] = dfp[1];
  299. dfp[1] = dfp[0];
  300. dfp[0] = new;
  301. *loghead = 1;
  302. return &dfp[0];
  303. }
  304. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
  305. dfp[2] = dfp[1];
  306. dfp[1] = new;
  307. *loghead = 1;
  308. return &dfp[1];
  309. }
  310. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
  311. dfp[2] = new;
  312. *loghead = 1;
  313. return &dfp[2];
  314. }
  315. return NULL;
  316. }
  317. /*
  318. * Remove a bestfree entry from the table.
  319. */
  320. STATIC void
  321. xfs_dir2_data_freeremove(
  322. xfs_dir2_data_hdr_t *hdr, /* data block header */
  323. xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
  324. int *loghead) /* out: log data header */
  325. {
  326. #ifdef __KERNEL__
  327. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  328. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  329. #endif
  330. /*
  331. * It's the first entry, slide the next 2 up.
  332. */
  333. if (dfp == &hdr->bestfree[0]) {
  334. hdr->bestfree[0] = hdr->bestfree[1];
  335. hdr->bestfree[1] = hdr->bestfree[2];
  336. }
  337. /*
  338. * It's the second entry, slide the 3rd entry up.
  339. */
  340. else if (dfp == &hdr->bestfree[1])
  341. hdr->bestfree[1] = hdr->bestfree[2];
  342. /*
  343. * Must be the last entry.
  344. */
  345. else
  346. ASSERT(dfp == &hdr->bestfree[2]);
  347. /*
  348. * Clear the 3rd entry, must be zero now.
  349. */
  350. hdr->bestfree[2].length = 0;
  351. hdr->bestfree[2].offset = 0;
  352. *loghead = 1;
  353. }
  354. /*
  355. * Given a data block, reconstruct its bestfree map.
  356. */
  357. void
  358. xfs_dir2_data_freescan(
  359. xfs_mount_t *mp, /* filesystem mount point */
  360. xfs_dir2_data_hdr_t *hdr, /* data block header */
  361. int *loghead) /* out: log data header */
  362. {
  363. xfs_dir2_block_tail_t *btp; /* block tail */
  364. xfs_dir2_data_entry_t *dep; /* active data entry */
  365. xfs_dir2_data_unused_t *dup; /* unused data entry */
  366. char *endp; /* end of block's data */
  367. char *p; /* current entry pointer */
  368. #ifdef __KERNEL__
  369. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  370. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  371. #endif
  372. /*
  373. * Start by clearing the table.
  374. */
  375. memset(hdr->bestfree, 0, sizeof(hdr->bestfree));
  376. *loghead = 1;
  377. /*
  378. * Set up pointers.
  379. */
  380. p = (char *)(hdr + 1);
  381. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
  382. btp = xfs_dir2_block_tail_p(mp, hdr);
  383. endp = (char *)xfs_dir2_block_leaf_p(btp);
  384. } else
  385. endp = (char *)hdr + mp->m_dirblksize;
  386. /*
  387. * Loop over the block's entries.
  388. */
  389. while (p < endp) {
  390. dup = (xfs_dir2_data_unused_t *)p;
  391. /*
  392. * If it's a free entry, insert it.
  393. */
  394. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  395. ASSERT((char *)dup - (char *)hdr ==
  396. be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  397. xfs_dir2_data_freeinsert(hdr, dup, loghead);
  398. p += be16_to_cpu(dup->length);
  399. }
  400. /*
  401. * For active entries, check their tags and skip them.
  402. */
  403. else {
  404. dep = (xfs_dir2_data_entry_t *)p;
  405. ASSERT((char *)dep - (char *)hdr ==
  406. be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
  407. p += xfs_dir2_data_entsize(dep->namelen);
  408. }
  409. }
  410. }
  411. /*
  412. * Initialize a data block at the given block number in the directory.
  413. * Give back the buffer for the created block.
  414. */
  415. int /* error */
  416. xfs_dir2_data_init(
  417. xfs_da_args_t *args, /* directory operation args */
  418. xfs_dir2_db_t blkno, /* logical dir block number */
  419. struct xfs_buf **bpp) /* output block buffer */
  420. {
  421. struct xfs_buf *bp; /* block buffer */
  422. xfs_dir2_data_hdr_t *hdr; /* data block header */
  423. xfs_inode_t *dp; /* incore directory inode */
  424. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  425. int error; /* error return value */
  426. int i; /* bestfree index */
  427. xfs_mount_t *mp; /* filesystem mount point */
  428. xfs_trans_t *tp; /* transaction pointer */
  429. int t; /* temp */
  430. dp = args->dp;
  431. mp = dp->i_mount;
  432. tp = args->trans;
  433. /*
  434. * Get the buffer set up for the block.
  435. */
  436. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
  437. XFS_DATA_FORK);
  438. if (error) {
  439. return error;
  440. }
  441. ASSERT(bp != NULL);
  442. /*
  443. * Initialize the header.
  444. */
  445. hdr = bp->b_addr;
  446. hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
  447. hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
  448. for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
  449. hdr->bestfree[i].length = 0;
  450. hdr->bestfree[i].offset = 0;
  451. }
  452. /*
  453. * Set up an unused entry for the block's body.
  454. */
  455. dup = (xfs_dir2_data_unused_t *)(hdr + 1);
  456. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  457. t = mp->m_dirblksize - (uint)sizeof(*hdr);
  458. hdr->bestfree[0].length = cpu_to_be16(t);
  459. dup->length = cpu_to_be16(t);
  460. *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
  461. /*
  462. * Log it and return it.
  463. */
  464. xfs_dir2_data_log_header(tp, bp);
  465. xfs_dir2_data_log_unused(tp, bp, dup);
  466. *bpp = bp;
  467. return 0;
  468. }
  469. /*
  470. * Log an active data entry from the block.
  471. */
  472. void
  473. xfs_dir2_data_log_entry(
  474. struct xfs_trans *tp,
  475. struct xfs_buf *bp,
  476. xfs_dir2_data_entry_t *dep) /* data entry pointer */
  477. {
  478. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  479. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  480. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  481. xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
  482. (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
  483. (char *)hdr - 1));
  484. }
  485. /*
  486. * Log a data block header.
  487. */
  488. void
  489. xfs_dir2_data_log_header(
  490. struct xfs_trans *tp,
  491. struct xfs_buf *bp)
  492. {
  493. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  494. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  495. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  496. xfs_trans_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
  497. }
  498. /*
  499. * Log a data unused entry.
  500. */
  501. void
  502. xfs_dir2_data_log_unused(
  503. struct xfs_trans *tp,
  504. struct xfs_buf *bp,
  505. xfs_dir2_data_unused_t *dup) /* data unused pointer */
  506. {
  507. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  508. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  509. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  510. /*
  511. * Log the first part of the unused entry.
  512. */
  513. xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
  514. (uint)((char *)&dup->length + sizeof(dup->length) -
  515. 1 - (char *)hdr));
  516. /*
  517. * Log the end (tag) of the unused entry.
  518. */
  519. xfs_trans_log_buf(tp, bp,
  520. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
  521. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
  522. sizeof(xfs_dir2_data_off_t) - 1));
  523. }
  524. /*
  525. * Make a byte range in the data block unused.
  526. * Its current contents are unimportant.
  527. */
  528. void
  529. xfs_dir2_data_make_free(
  530. struct xfs_trans *tp,
  531. struct xfs_buf *bp,
  532. xfs_dir2_data_aoff_t offset, /* starting byte offset */
  533. xfs_dir2_data_aoff_t len, /* length in bytes */
  534. int *needlogp, /* out: log header */
  535. int *needscanp) /* out: regen bestfree */
  536. {
  537. xfs_dir2_data_hdr_t *hdr; /* data block pointer */
  538. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  539. char *endptr; /* end of data area */
  540. xfs_mount_t *mp; /* filesystem mount point */
  541. int needscan; /* need to regen bestfree */
  542. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  543. xfs_dir2_data_unused_t *postdup; /* unused entry after us */
  544. xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
  545. mp = tp->t_mountp;
  546. hdr = bp->b_addr;
  547. /*
  548. * Figure out where the end of the data area is.
  549. */
  550. if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC))
  551. endptr = (char *)hdr + mp->m_dirblksize;
  552. else {
  553. xfs_dir2_block_tail_t *btp; /* block tail */
  554. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  555. btp = xfs_dir2_block_tail_p(mp, hdr);
  556. endptr = (char *)xfs_dir2_block_leaf_p(btp);
  557. }
  558. /*
  559. * If this isn't the start of the block, then back up to
  560. * the previous entry and see if it's free.
  561. */
  562. if (offset > sizeof(*hdr)) {
  563. __be16 *tagp; /* tag just before us */
  564. tagp = (__be16 *)((char *)hdr + offset) - 1;
  565. prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  566. if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  567. prevdup = NULL;
  568. } else
  569. prevdup = NULL;
  570. /*
  571. * If this isn't the end of the block, see if the entry after
  572. * us is free.
  573. */
  574. if ((char *)hdr + offset + len < endptr) {
  575. postdup =
  576. (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  577. if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  578. postdup = NULL;
  579. } else
  580. postdup = NULL;
  581. ASSERT(*needscanp == 0);
  582. needscan = 0;
  583. /*
  584. * Previous and following entries are both free,
  585. * merge everything into a single free entry.
  586. */
  587. if (prevdup && postdup) {
  588. xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
  589. /*
  590. * See if prevdup and/or postdup are in bestfree table.
  591. */
  592. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  593. dfp2 = xfs_dir2_data_freefind(hdr, postdup);
  594. /*
  595. * We need a rescan unless there are exactly 2 free entries
  596. * namely our two. Then we know what's happening, otherwise
  597. * since the third bestfree is there, there might be more
  598. * entries.
  599. */
  600. needscan = (hdr->bestfree[2].length != 0);
  601. /*
  602. * Fix up the new big freespace.
  603. */
  604. be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
  605. *xfs_dir2_data_unused_tag_p(prevdup) =
  606. cpu_to_be16((char *)prevdup - (char *)hdr);
  607. xfs_dir2_data_log_unused(tp, bp, prevdup);
  608. if (!needscan) {
  609. /*
  610. * Has to be the case that entries 0 and 1 are
  611. * dfp and dfp2 (don't know which is which), and
  612. * entry 2 is empty.
  613. * Remove entry 1 first then entry 0.
  614. */
  615. ASSERT(dfp && dfp2);
  616. if (dfp == &hdr->bestfree[1]) {
  617. dfp = &hdr->bestfree[0];
  618. ASSERT(dfp2 == dfp);
  619. dfp2 = &hdr->bestfree[1];
  620. }
  621. xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
  622. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  623. /*
  624. * Now insert the new entry.
  625. */
  626. dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  627. ASSERT(dfp == &hdr->bestfree[0]);
  628. ASSERT(dfp->length == prevdup->length);
  629. ASSERT(!dfp[1].length);
  630. ASSERT(!dfp[2].length);
  631. }
  632. }
  633. /*
  634. * The entry before us is free, merge with it.
  635. */
  636. else if (prevdup) {
  637. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  638. be16_add_cpu(&prevdup->length, len);
  639. *xfs_dir2_data_unused_tag_p(prevdup) =
  640. cpu_to_be16((char *)prevdup - (char *)hdr);
  641. xfs_dir2_data_log_unused(tp, bp, prevdup);
  642. /*
  643. * If the previous entry was in the table, the new entry
  644. * is longer, so it will be in the table too. Remove
  645. * the old one and add the new one.
  646. */
  647. if (dfp) {
  648. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  649. xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  650. }
  651. /*
  652. * Otherwise we need a scan if the new entry is big enough.
  653. */
  654. else {
  655. needscan = be16_to_cpu(prevdup->length) >
  656. be16_to_cpu(hdr->bestfree[2].length);
  657. }
  658. }
  659. /*
  660. * The following entry is free, merge with it.
  661. */
  662. else if (postdup) {
  663. dfp = xfs_dir2_data_freefind(hdr, postdup);
  664. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  665. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  666. newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
  667. *xfs_dir2_data_unused_tag_p(newdup) =
  668. cpu_to_be16((char *)newdup - (char *)hdr);
  669. xfs_dir2_data_log_unused(tp, bp, newdup);
  670. /*
  671. * If the following entry was in the table, the new entry
  672. * is longer, so it will be in the table too. Remove
  673. * the old one and add the new one.
  674. */
  675. if (dfp) {
  676. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  677. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  678. }
  679. /*
  680. * Otherwise we need a scan if the new entry is big enough.
  681. */
  682. else {
  683. needscan = be16_to_cpu(newdup->length) >
  684. be16_to_cpu(hdr->bestfree[2].length);
  685. }
  686. }
  687. /*
  688. * Neither neighbor is free. Make a new entry.
  689. */
  690. else {
  691. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  692. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  693. newdup->length = cpu_to_be16(len);
  694. *xfs_dir2_data_unused_tag_p(newdup) =
  695. cpu_to_be16((char *)newdup - (char *)hdr);
  696. xfs_dir2_data_log_unused(tp, bp, newdup);
  697. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  698. }
  699. *needscanp = needscan;
  700. }
  701. /*
  702. * Take a byte range out of an existing unused space and make it un-free.
  703. */
  704. void
  705. xfs_dir2_data_use_free(
  706. struct xfs_trans *tp,
  707. struct xfs_buf *bp,
  708. xfs_dir2_data_unused_t *dup, /* unused entry */
  709. xfs_dir2_data_aoff_t offset, /* starting offset to use */
  710. xfs_dir2_data_aoff_t len, /* length to use */
  711. int *needlogp, /* out: need to log header */
  712. int *needscanp) /* out: need regen bestfree */
  713. {
  714. xfs_dir2_data_hdr_t *hdr; /* data block header */
  715. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  716. int matchback; /* matches end of freespace */
  717. int matchfront; /* matches start of freespace */
  718. int needscan; /* need to regen bestfree */
  719. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  720. xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
  721. int oldlen; /* old unused entry's length */
  722. hdr = bp->b_addr;
  723. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  724. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  725. ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
  726. ASSERT(offset >= (char *)dup - (char *)hdr);
  727. ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
  728. ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  729. /*
  730. * Look up the entry in the bestfree table.
  731. */
  732. dfp = xfs_dir2_data_freefind(hdr, dup);
  733. oldlen = be16_to_cpu(dup->length);
  734. ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
  735. /*
  736. * Check for alignment with front and back of the entry.
  737. */
  738. matchfront = (char *)dup - (char *)hdr == offset;
  739. matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
  740. ASSERT(*needscanp == 0);
  741. needscan = 0;
  742. /*
  743. * If we matched it exactly we just need to get rid of it from
  744. * the bestfree table.
  745. */
  746. if (matchfront && matchback) {
  747. if (dfp) {
  748. needscan = (hdr->bestfree[2].offset != 0);
  749. if (!needscan)
  750. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  751. }
  752. }
  753. /*
  754. * We match the first part of the entry.
  755. * Make a new entry with the remaining freespace.
  756. */
  757. else if (matchfront) {
  758. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  759. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  760. newdup->length = cpu_to_be16(oldlen - len);
  761. *xfs_dir2_data_unused_tag_p(newdup) =
  762. cpu_to_be16((char *)newdup - (char *)hdr);
  763. xfs_dir2_data_log_unused(tp, bp, newdup);
  764. /*
  765. * If it was in the table, remove it and add the new one.
  766. */
  767. if (dfp) {
  768. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  769. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  770. ASSERT(dfp != NULL);
  771. ASSERT(dfp->length == newdup->length);
  772. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  773. /*
  774. * If we got inserted at the last slot,
  775. * that means we don't know if there was a better
  776. * choice for the last slot, or not. Rescan.
  777. */
  778. needscan = dfp == &hdr->bestfree[2];
  779. }
  780. }
  781. /*
  782. * We match the last part of the entry.
  783. * Trim the allocated space off the tail of the entry.
  784. */
  785. else if (matchback) {
  786. newdup = dup;
  787. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  788. *xfs_dir2_data_unused_tag_p(newdup) =
  789. cpu_to_be16((char *)newdup - (char *)hdr);
  790. xfs_dir2_data_log_unused(tp, bp, newdup);
  791. /*
  792. * If it was in the table, remove it and add the new one.
  793. */
  794. if (dfp) {
  795. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  796. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  797. ASSERT(dfp != NULL);
  798. ASSERT(dfp->length == newdup->length);
  799. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  800. /*
  801. * If we got inserted at the last slot,
  802. * that means we don't know if there was a better
  803. * choice for the last slot, or not. Rescan.
  804. */
  805. needscan = dfp == &hdr->bestfree[2];
  806. }
  807. }
  808. /*
  809. * Poking out the middle of an entry.
  810. * Make two new entries.
  811. */
  812. else {
  813. newdup = dup;
  814. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  815. *xfs_dir2_data_unused_tag_p(newdup) =
  816. cpu_to_be16((char *)newdup - (char *)hdr);
  817. xfs_dir2_data_log_unused(tp, bp, newdup);
  818. newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  819. newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  820. newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
  821. *xfs_dir2_data_unused_tag_p(newdup2) =
  822. cpu_to_be16((char *)newdup2 - (char *)hdr);
  823. xfs_dir2_data_log_unused(tp, bp, newdup2);
  824. /*
  825. * If the old entry was in the table, we need to scan
  826. * if the 3rd entry was valid, since these entries
  827. * are smaller than the old one.
  828. * If we don't need to scan that means there were 1 or 2
  829. * entries in the table, and removing the old and adding
  830. * the 2 new will work.
  831. */
  832. if (dfp) {
  833. needscan = (hdr->bestfree[2].length != 0);
  834. if (!needscan) {
  835. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  836. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  837. xfs_dir2_data_freeinsert(hdr, newdup2,
  838. needlogp);
  839. }
  840. }
  841. }
  842. *needscanp = needscan;
  843. }