xfs_dir2_data.c 25 KB

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