xfs_dir2_data.c 25 KB

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