xfs_dir2_data.c 25 KB

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