xfs_dir2_data.c 25 KB

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