xfs_dir2_data.c 27 KB

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