xfs_dir2_data.c 25 KB

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