xfs_dir2_data.c 25 KB

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