xfs_dir2_data.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_log.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_da_btree.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_dinode.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_dir2_format.h"
  31. #include "xfs_dir2_priv.h"
  32. #include "xfs_error.h"
  33. STATIC xfs_dir2_data_free_t *
  34. xfs_dir2_data_freefind(xfs_dir2_data_hdr_t *hdr, xfs_dir2_data_unused_t *dup);
  35. #ifdef DEBUG
  36. /*
  37. * Check the consistency of the data block.
  38. * The input can also be a block-format directory.
  39. * Pop an assert if we find anything bad.
  40. */
  41. void
  42. xfs_dir2_data_check(
  43. struct xfs_inode *dp, /* incore inode pointer */
  44. struct xfs_buf *bp) /* data block's buffer */
  45. {
  46. xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
  47. xfs_dir2_data_free_t *bf; /* bestfree table */
  48. xfs_dir2_block_tail_t *btp=NULL; /* block tail */
  49. int count; /* count of entries found */
  50. xfs_dir2_data_hdr_t *hdr; /* data block header */
  51. xfs_dir2_data_entry_t *dep; /* data entry */
  52. xfs_dir2_data_free_t *dfp; /* bestfree entry */
  53. xfs_dir2_data_unused_t *dup; /* unused entry */
  54. char *endp; /* end of useful data */
  55. int freeseen; /* mask of bestfrees seen */
  56. xfs_dahash_t hash; /* hash of current name */
  57. int i; /* leaf index */
  58. int lastfree; /* last entry was unused */
  59. xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
  60. xfs_mount_t *mp; /* filesystem mount point */
  61. char *p; /* current data position */
  62. int stale; /* count of stale leaves */
  63. struct xfs_name name;
  64. mp = dp->i_mount;
  65. hdr = bp->b_addr;
  66. bf = hdr->bestfree;
  67. p = (char *)(hdr + 1);
  68. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
  69. btp = xfs_dir2_block_tail_p(mp, hdr);
  70. lep = xfs_dir2_block_leaf_p(btp);
  71. endp = (char *)lep;
  72. } else {
  73. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
  74. endp = (char *)hdr + mp->m_dirblksize;
  75. }
  76. count = lastfree = freeseen = 0;
  77. /*
  78. * Account for zero bestfree entries.
  79. */
  80. if (!bf[0].length) {
  81. ASSERT(!bf[0].offset);
  82. freeseen |= 1 << 0;
  83. }
  84. if (!bf[1].length) {
  85. ASSERT(!bf[1].offset);
  86. freeseen |= 1 << 1;
  87. }
  88. if (!bf[2].length) {
  89. ASSERT(!bf[2].offset);
  90. freeseen |= 1 << 2;
  91. }
  92. ASSERT(be16_to_cpu(bf[0].length) >= be16_to_cpu(bf[1].length));
  93. ASSERT(be16_to_cpu(bf[1].length) >= be16_to_cpu(bf[2].length));
  94. /*
  95. * Loop over the data/unused entries.
  96. */
  97. while (p < endp) {
  98. dup = (xfs_dir2_data_unused_t *)p;
  99. /*
  100. * If it's unused, look for the space in the bestfree table.
  101. * If we find it, account for that, else make sure it
  102. * doesn't need to be there.
  103. */
  104. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  105. ASSERT(lastfree == 0);
  106. ASSERT(be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
  107. (char *)dup - (char *)hdr);
  108. dfp = xfs_dir2_data_freefind(hdr, dup);
  109. if (dfp) {
  110. i = (int)(dfp - bf);
  111. ASSERT((freeseen & (1 << i)) == 0);
  112. freeseen |= 1 << i;
  113. } else {
  114. ASSERT(be16_to_cpu(dup->length) <=
  115. be16_to_cpu(bf[2].length));
  116. }
  117. p += be16_to_cpu(dup->length);
  118. lastfree = 1;
  119. continue;
  120. }
  121. /*
  122. * It's a real entry. Validate the fields.
  123. * If this is a block directory then make sure it's
  124. * in the leaf section of the block.
  125. * The linear search is crude but this is DEBUG code.
  126. */
  127. dep = (xfs_dir2_data_entry_t *)p;
  128. ASSERT(dep->namelen != 0);
  129. ASSERT(xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)) == 0);
  130. ASSERT(be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
  131. (char *)dep - (char *)hdr);
  132. count++;
  133. lastfree = 0;
  134. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
  135. addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  136. (xfs_dir2_data_aoff_t)
  137. ((char *)dep - (char *)hdr));
  138. name.name = dep->name;
  139. name.len = dep->namelen;
  140. hash = mp->m_dirnameops->hashname(&name);
  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 (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
  155. for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
  156. if (lep[i].address ==
  157. cpu_to_be32(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. STATIC xfs_dir2_data_free_t *
  172. xfs_dir2_data_freefind(
  173. xfs_dir2_data_hdr_t *hdr, /* 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 *)hdr);
  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(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  190. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  191. for (dfp = &hdr->bestfree[0], seenzero = matched = 0;
  192. dfp < &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 > &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(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 = &hdr->bestfree[0];
  223. dfp < &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_hdr_t *hdr, /* 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(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  248. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  249. #endif
  250. dfp = hdr->bestfree;
  251. new.length = dup->length;
  252. new.offset = cpu_to_be16((char *)dup - (char *)hdr);
  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_hdr_t *hdr, /* data block header */
  282. xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
  283. int *loghead) /* out: log data header */
  284. {
  285. #ifdef __KERNEL__
  286. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  287. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  288. #endif
  289. /*
  290. * It's the first entry, slide the next 2 up.
  291. */
  292. if (dfp == &hdr->bestfree[0]) {
  293. hdr->bestfree[0] = hdr->bestfree[1];
  294. hdr->bestfree[1] = hdr->bestfree[2];
  295. }
  296. /*
  297. * It's the second entry, slide the 3rd entry up.
  298. */
  299. else if (dfp == &hdr->bestfree[1])
  300. hdr->bestfree[1] = hdr->bestfree[2];
  301. /*
  302. * Must be the last entry.
  303. */
  304. else
  305. ASSERT(dfp == &hdr->bestfree[2]);
  306. /*
  307. * Clear the 3rd entry, must be zero now.
  308. */
  309. hdr->bestfree[2].length = 0;
  310. 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_hdr_t *hdr, /* data block header */
  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(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  329. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  330. #endif
  331. /*
  332. * Start by clearing the table.
  333. */
  334. memset(hdr->bestfree, 0, sizeof(hdr->bestfree));
  335. *loghead = 1;
  336. /*
  337. * Set up pointers.
  338. */
  339. p = (char *)(hdr + 1);
  340. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
  341. btp = xfs_dir2_block_tail_p(mp, hdr);
  342. endp = (char *)xfs_dir2_block_leaf_p(btp);
  343. } else
  344. endp = (char *)hdr + 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 *)hdr ==
  355. be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  356. xfs_dir2_data_freeinsert(hdr, 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 *)hdr ==
  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. struct xfs_buf **bpp) /* output block buffer */
  379. {
  380. struct xfs_buf *bp; /* block buffer */
  381. xfs_dir2_data_hdr_t *hdr; /* data block header */
  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. hdr = bp->b_addr;
  405. hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
  406. hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
  407. for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
  408. hdr->bestfree[i].length = 0;
  409. hdr->bestfree[i].offset = 0;
  410. }
  411. /*
  412. * Set up an unused entry for the block's body.
  413. */
  414. dup = (xfs_dir2_data_unused_t *)(hdr + 1);
  415. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  416. t = mp->m_dirblksize - (uint)sizeof(*hdr);
  417. 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 *)hdr);
  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. struct xfs_trans *tp,
  434. struct xfs_buf *bp,
  435. xfs_dir2_data_entry_t *dep) /* data entry pointer */
  436. {
  437. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  438. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  439. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  440. xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
  441. (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
  442. (char *)hdr - 1));
  443. }
  444. /*
  445. * Log a data block header.
  446. */
  447. void
  448. xfs_dir2_data_log_header(
  449. struct xfs_trans *tp,
  450. struct xfs_buf *bp)
  451. {
  452. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  453. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  454. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  455. xfs_trans_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
  456. }
  457. /*
  458. * Log a data unused entry.
  459. */
  460. void
  461. xfs_dir2_data_log_unused(
  462. struct xfs_trans *tp,
  463. struct xfs_buf *bp,
  464. xfs_dir2_data_unused_t *dup) /* data unused pointer */
  465. {
  466. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  467. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  468. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  469. /*
  470. * Log the first part of the unused entry.
  471. */
  472. xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
  473. (uint)((char *)&dup->length + sizeof(dup->length) -
  474. 1 - (char *)hdr));
  475. /*
  476. * Log the end (tag) of the unused entry.
  477. */
  478. xfs_trans_log_buf(tp, bp,
  479. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
  480. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
  481. sizeof(xfs_dir2_data_off_t) - 1));
  482. }
  483. /*
  484. * Make a byte range in the data block unused.
  485. * Its current contents are unimportant.
  486. */
  487. void
  488. xfs_dir2_data_make_free(
  489. struct xfs_trans *tp,
  490. struct xfs_buf *bp,
  491. xfs_dir2_data_aoff_t offset, /* starting byte offset */
  492. xfs_dir2_data_aoff_t len, /* length in bytes */
  493. int *needlogp, /* out: log header */
  494. int *needscanp) /* out: regen bestfree */
  495. {
  496. xfs_dir2_data_hdr_t *hdr; /* data block pointer */
  497. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  498. char *endptr; /* end of data area */
  499. xfs_mount_t *mp; /* filesystem mount point */
  500. int needscan; /* need to regen bestfree */
  501. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  502. xfs_dir2_data_unused_t *postdup; /* unused entry after us */
  503. xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
  504. mp = tp->t_mountp;
  505. hdr = bp->b_addr;
  506. /*
  507. * Figure out where the end of the data area is.
  508. */
  509. if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC))
  510. endptr = (char *)hdr + mp->m_dirblksize;
  511. else {
  512. xfs_dir2_block_tail_t *btp; /* block tail */
  513. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  514. btp = xfs_dir2_block_tail_p(mp, hdr);
  515. endptr = (char *)xfs_dir2_block_leaf_p(btp);
  516. }
  517. /*
  518. * If this isn't the start of the block, then back up to
  519. * the previous entry and see if it's free.
  520. */
  521. if (offset > sizeof(*hdr)) {
  522. __be16 *tagp; /* tag just before us */
  523. tagp = (__be16 *)((char *)hdr + offset) - 1;
  524. prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  525. if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  526. prevdup = NULL;
  527. } else
  528. prevdup = NULL;
  529. /*
  530. * If this isn't the end of the block, see if the entry after
  531. * us is free.
  532. */
  533. if ((char *)hdr + offset + len < endptr) {
  534. postdup =
  535. (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  536. if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  537. postdup = NULL;
  538. } else
  539. postdup = NULL;
  540. ASSERT(*needscanp == 0);
  541. needscan = 0;
  542. /*
  543. * Previous and following entries are both free,
  544. * merge everything into a single free entry.
  545. */
  546. if (prevdup && postdup) {
  547. xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
  548. /*
  549. * See if prevdup and/or postdup are in bestfree table.
  550. */
  551. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  552. dfp2 = xfs_dir2_data_freefind(hdr, postdup);
  553. /*
  554. * We need a rescan unless there are exactly 2 free entries
  555. * namely our two. Then we know what's happening, otherwise
  556. * since the third bestfree is there, there might be more
  557. * entries.
  558. */
  559. needscan = (hdr->bestfree[2].length != 0);
  560. /*
  561. * Fix up the new big freespace.
  562. */
  563. be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
  564. *xfs_dir2_data_unused_tag_p(prevdup) =
  565. cpu_to_be16((char *)prevdup - (char *)hdr);
  566. xfs_dir2_data_log_unused(tp, bp, prevdup);
  567. if (!needscan) {
  568. /*
  569. * Has to be the case that entries 0 and 1 are
  570. * dfp and dfp2 (don't know which is which), and
  571. * entry 2 is empty.
  572. * Remove entry 1 first then entry 0.
  573. */
  574. ASSERT(dfp && dfp2);
  575. if (dfp == &hdr->bestfree[1]) {
  576. dfp = &hdr->bestfree[0];
  577. ASSERT(dfp2 == dfp);
  578. dfp2 = &hdr->bestfree[1];
  579. }
  580. xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
  581. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  582. /*
  583. * Now insert the new entry.
  584. */
  585. dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  586. ASSERT(dfp == &hdr->bestfree[0]);
  587. ASSERT(dfp->length == prevdup->length);
  588. ASSERT(!dfp[1].length);
  589. ASSERT(!dfp[2].length);
  590. }
  591. }
  592. /*
  593. * The entry before us is free, merge with it.
  594. */
  595. else if (prevdup) {
  596. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  597. be16_add_cpu(&prevdup->length, len);
  598. *xfs_dir2_data_unused_tag_p(prevdup) =
  599. cpu_to_be16((char *)prevdup - (char *)hdr);
  600. xfs_dir2_data_log_unused(tp, bp, prevdup);
  601. /*
  602. * If the previous entry was in the table, the new entry
  603. * is longer, so it will be in the table too. Remove
  604. * the old one and add the new one.
  605. */
  606. if (dfp) {
  607. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  608. xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  609. }
  610. /*
  611. * Otherwise we need a scan if the new entry is big enough.
  612. */
  613. else {
  614. needscan = be16_to_cpu(prevdup->length) >
  615. be16_to_cpu(hdr->bestfree[2].length);
  616. }
  617. }
  618. /*
  619. * The following entry is free, merge with it.
  620. */
  621. else if (postdup) {
  622. dfp = xfs_dir2_data_freefind(hdr, postdup);
  623. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  624. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  625. newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
  626. *xfs_dir2_data_unused_tag_p(newdup) =
  627. cpu_to_be16((char *)newdup - (char *)hdr);
  628. xfs_dir2_data_log_unused(tp, bp, newdup);
  629. /*
  630. * If the following entry was in the table, the new entry
  631. * is longer, so it will be in the table too. Remove
  632. * the old one and add the new one.
  633. */
  634. if (dfp) {
  635. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  636. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  637. }
  638. /*
  639. * Otherwise we need a scan if the new entry is big enough.
  640. */
  641. else {
  642. needscan = be16_to_cpu(newdup->length) >
  643. be16_to_cpu(hdr->bestfree[2].length);
  644. }
  645. }
  646. /*
  647. * Neither neighbor is free. Make a new entry.
  648. */
  649. else {
  650. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  651. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  652. newdup->length = cpu_to_be16(len);
  653. *xfs_dir2_data_unused_tag_p(newdup) =
  654. cpu_to_be16((char *)newdup - (char *)hdr);
  655. xfs_dir2_data_log_unused(tp, bp, newdup);
  656. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  657. }
  658. *needscanp = needscan;
  659. }
  660. /*
  661. * Take a byte range out of an existing unused space and make it un-free.
  662. */
  663. void
  664. xfs_dir2_data_use_free(
  665. struct xfs_trans *tp,
  666. struct xfs_buf *bp,
  667. xfs_dir2_data_unused_t *dup, /* unused entry */
  668. xfs_dir2_data_aoff_t offset, /* starting offset to use */
  669. xfs_dir2_data_aoff_t len, /* length to use */
  670. int *needlogp, /* out: need to log header */
  671. int *needscanp) /* out: need regen bestfree */
  672. {
  673. xfs_dir2_data_hdr_t *hdr; /* data block header */
  674. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  675. int matchback; /* matches end of freespace */
  676. int matchfront; /* matches start of freespace */
  677. int needscan; /* need to regen bestfree */
  678. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  679. xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
  680. int oldlen; /* old unused entry's length */
  681. hdr = bp->b_addr;
  682. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  683. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
  684. ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
  685. ASSERT(offset >= (char *)dup - (char *)hdr);
  686. ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
  687. ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  688. /*
  689. * Look up the entry in the bestfree table.
  690. */
  691. dfp = xfs_dir2_data_freefind(hdr, dup);
  692. oldlen = be16_to_cpu(dup->length);
  693. ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
  694. /*
  695. * Check for alignment with front and back of the entry.
  696. */
  697. matchfront = (char *)dup - (char *)hdr == offset;
  698. matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
  699. ASSERT(*needscanp == 0);
  700. needscan = 0;
  701. /*
  702. * If we matched it exactly we just need to get rid of it from
  703. * the bestfree table.
  704. */
  705. if (matchfront && matchback) {
  706. if (dfp) {
  707. needscan = (hdr->bestfree[2].offset != 0);
  708. if (!needscan)
  709. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  710. }
  711. }
  712. /*
  713. * We match the first part of the entry.
  714. * Make a new entry with the remaining freespace.
  715. */
  716. else if (matchfront) {
  717. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  718. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  719. newdup->length = cpu_to_be16(oldlen - len);
  720. *xfs_dir2_data_unused_tag_p(newdup) =
  721. cpu_to_be16((char *)newdup - (char *)hdr);
  722. xfs_dir2_data_log_unused(tp, bp, newdup);
  723. /*
  724. * If it was in the table, remove it and add the new one.
  725. */
  726. if (dfp) {
  727. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  728. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  729. ASSERT(dfp != NULL);
  730. ASSERT(dfp->length == newdup->length);
  731. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  732. /*
  733. * If we got inserted at the last slot,
  734. * that means we don't know if there was a better
  735. * choice for the last slot, or not. Rescan.
  736. */
  737. needscan = dfp == &hdr->bestfree[2];
  738. }
  739. }
  740. /*
  741. * We match the last part of the entry.
  742. * Trim the allocated space off the tail of the entry.
  743. */
  744. else if (matchback) {
  745. newdup = dup;
  746. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  747. *xfs_dir2_data_unused_tag_p(newdup) =
  748. cpu_to_be16((char *)newdup - (char *)hdr);
  749. xfs_dir2_data_log_unused(tp, bp, newdup);
  750. /*
  751. * If it was in the table, remove it and add the new one.
  752. */
  753. if (dfp) {
  754. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  755. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  756. ASSERT(dfp != NULL);
  757. ASSERT(dfp->length == newdup->length);
  758. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  759. /*
  760. * If we got inserted at the last slot,
  761. * that means we don't know if there was a better
  762. * choice for the last slot, or not. Rescan.
  763. */
  764. needscan = dfp == &hdr->bestfree[2];
  765. }
  766. }
  767. /*
  768. * Poking out the middle of an entry.
  769. * Make two new entries.
  770. */
  771. else {
  772. newdup = dup;
  773. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  774. *xfs_dir2_data_unused_tag_p(newdup) =
  775. cpu_to_be16((char *)newdup - (char *)hdr);
  776. xfs_dir2_data_log_unused(tp, bp, newdup);
  777. newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  778. newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  779. newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
  780. *xfs_dir2_data_unused_tag_p(newdup2) =
  781. cpu_to_be16((char *)newdup2 - (char *)hdr);
  782. xfs_dir2_data_log_unused(tp, bp, newdup2);
  783. /*
  784. * If the old entry was in the table, we need to scan
  785. * if the 3rd entry was valid, since these entries
  786. * are smaller than the old one.
  787. * If we don't need to scan that means there were 1 or 2
  788. * entries in the table, and removing the old and adding
  789. * the 2 new will work.
  790. */
  791. if (dfp) {
  792. needscan = (hdr->bestfree[2].length != 0);
  793. if (!needscan) {
  794. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  795. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  796. xfs_dir2_data_freeinsert(hdr, newdup2,
  797. needlogp);
  798. }
  799. }
  800. }
  801. *needscanp = needscan;
  802. }