xfs_dir2_data.c 25 KB

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