xfs_dir2_data.c 25 KB

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