xfs_dir2_data.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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_dir.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_dir_sf.h"
  32. #include "xfs_dir2_sf.h"
  33. #include "xfs_attr_sf.h"
  34. #include "xfs_dinode.h"
  35. #include "xfs_inode.h"
  36. #include "xfs_dir_leaf.h"
  37. #include "xfs_dir2_data.h"
  38. #include "xfs_dir2_leaf.h"
  39. #include "xfs_dir2_block.h"
  40. #include "xfs_error.h"
  41. #ifdef DEBUG
  42. /*
  43. * Check the consistency of the data block.
  44. * The input can also be a block-format directory.
  45. * Pop an assert if we find anything bad.
  46. */
  47. void
  48. xfs_dir2_data_check(
  49. xfs_inode_t *dp, /* incore inode pointer */
  50. xfs_dabuf_t *bp) /* data block's buffer */
  51. {
  52. xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
  53. xfs_dir2_data_free_t *bf; /* bestfree table */
  54. xfs_dir2_block_tail_t *btp=NULL; /* block tail */
  55. int count; /* count of entries found */
  56. xfs_dir2_data_t *d; /* data block pointer */
  57. xfs_dir2_data_entry_t *dep; /* data entry */
  58. xfs_dir2_data_free_t *dfp; /* bestfree entry */
  59. xfs_dir2_data_unused_t *dup; /* unused entry */
  60. char *endp; /* end of useful data */
  61. int freeseen; /* mask of bestfrees seen */
  62. xfs_dahash_t hash; /* hash of current name */
  63. int i; /* leaf index */
  64. int lastfree; /* last entry was unused */
  65. xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
  66. xfs_mount_t *mp; /* filesystem mount point */
  67. char *p; /* current data position */
  68. int stale; /* count of stale leaves */
  69. mp = dp->i_mount;
  70. d = bp->data;
  71. ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
  72. INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
  73. bf = d->hdr.bestfree;
  74. p = (char *)d->u;
  75. if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
  76. btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
  77. lep = XFS_DIR2_BLOCK_LEAF_P(btp);
  78. endp = (char *)lep;
  79. } else
  80. endp = (char *)d + mp->m_dirblksize;
  81. count = lastfree = freeseen = 0;
  82. /*
  83. * Account for zero bestfree entries.
  84. */
  85. if (!bf[0].length) {
  86. ASSERT(!bf[0].offset);
  87. freeseen |= 1 << 0;
  88. }
  89. if (!bf[1].length) {
  90. ASSERT(!bf[1].offset);
  91. freeseen |= 1 << 1;
  92. }
  93. if (!bf[2].length) {
  94. ASSERT(!bf[2].offset);
  95. freeseen |= 1 << 2;
  96. }
  97. ASSERT(INT_GET(bf[0].length, ARCH_CONVERT) >= INT_GET(bf[1].length, ARCH_CONVERT));
  98. ASSERT(INT_GET(bf[1].length, ARCH_CONVERT) >= INT_GET(bf[2].length, ARCH_CONVERT));
  99. /*
  100. * Loop over the data/unused entries.
  101. */
  102. while (p < endp) {
  103. dup = (xfs_dir2_data_unused_t *)p;
  104. /*
  105. * If it's unused, look for the space in the bestfree table.
  106. * If we find it, account for that, else make sure it
  107. * doesn't need to be there.
  108. */
  109. if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
  110. ASSERT(lastfree == 0);
  111. ASSERT(INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT) ==
  112. (char *)dup - (char *)d);
  113. dfp = xfs_dir2_data_freefind(d, dup);
  114. if (dfp) {
  115. i = (int)(dfp - bf);
  116. ASSERT((freeseen & (1 << i)) == 0);
  117. freeseen |= 1 << i;
  118. } else
  119. ASSERT(INT_GET(dup->length, ARCH_CONVERT) <= INT_GET(bf[2].length, ARCH_CONVERT));
  120. p += INT_GET(dup->length, ARCH_CONVERT);
  121. lastfree = 1;
  122. continue;
  123. }
  124. /*
  125. * It's a real entry. Validate the fields.
  126. * If this is a block directory then make sure it's
  127. * in the leaf section of the block.
  128. * The linear search is crude but this is DEBUG code.
  129. */
  130. dep = (xfs_dir2_data_entry_t *)p;
  131. ASSERT(dep->namelen != 0);
  132. ASSERT(xfs_dir_ino_validate(mp, INT_GET(dep->inumber, ARCH_CONVERT)) == 0);
  133. ASSERT(INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep), ARCH_CONVERT) ==
  134. (char *)dep - (char *)d);
  135. count++;
  136. lastfree = 0;
  137. if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
  138. addr = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
  139. (xfs_dir2_data_aoff_t)
  140. ((char *)dep - (char *)d));
  141. hash = xfs_da_hashname((char *)dep->name, dep->namelen);
  142. for (i = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
  143. if (INT_GET(lep[i].address, ARCH_CONVERT) == addr &&
  144. INT_GET(lep[i].hashval, ARCH_CONVERT) == hash)
  145. break;
  146. }
  147. ASSERT(i < INT_GET(btp->count, ARCH_CONVERT));
  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 (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
  156. for (i = stale = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
  157. if (INT_GET(lep[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
  158. stale++;
  159. if (i > 0)
  160. ASSERT(INT_GET(lep[i].hashval, ARCH_CONVERT) >= INT_GET(lep[i - 1].hashval, ARCH_CONVERT));
  161. }
  162. ASSERT(count == INT_GET(btp->count, ARCH_CONVERT) - INT_GET(btp->stale, ARCH_CONVERT));
  163. ASSERT(stale == INT_GET(btp->stale, ARCH_CONVERT));
  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. xfs_dir2_data_free_t *
  172. xfs_dir2_data_freefind(
  173. xfs_dir2_data_t *d, /* 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 *)d);
  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(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
  190. INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
  191. for (dfp = &d->hdr.bestfree[0], seenzero = matched = 0;
  192. dfp < &d->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 (INT_GET(dfp->offset, ARCH_CONVERT) == off) {
  201. matched = 1;
  202. ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(dup->length, ARCH_CONVERT));
  203. } else if (off < INT_GET(dfp->offset, ARCH_CONVERT))
  204. ASSERT(off + INT_GET(dup->length, ARCH_CONVERT) <= INT_GET(dfp->offset, ARCH_CONVERT));
  205. else
  206. ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) + INT_GET(dfp->length, ARCH_CONVERT) <= off);
  207. ASSERT(matched || INT_GET(dfp->length, ARCH_CONVERT) >= INT_GET(dup->length, ARCH_CONVERT));
  208. if (dfp > &d->hdr.bestfree[0])
  209. ASSERT(INT_GET(dfp[-1].length, ARCH_CONVERT) >= INT_GET(dfp[0].length, ARCH_CONVERT));
  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 (INT_GET(dup->length, ARCH_CONVERT) < INT_GET(d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length, ARCH_CONVERT))
  217. return NULL;
  218. /*
  219. * Look at the three bestfree entries for our guy.
  220. */
  221. for (dfp = &d->hdr.bestfree[0];
  222. dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
  223. dfp++) {
  224. if (!dfp->offset)
  225. return NULL;
  226. if (INT_GET(dfp->offset, ARCH_CONVERT) == off)
  227. return dfp;
  228. }
  229. /*
  230. * Didn't find it. This only happens if there are duplicate lengths.
  231. */
  232. return NULL;
  233. }
  234. /*
  235. * Insert an unused-space entry into the bestfree table.
  236. */
  237. xfs_dir2_data_free_t * /* entry inserted */
  238. xfs_dir2_data_freeinsert(
  239. xfs_dir2_data_t *d, /* data block pointer */
  240. xfs_dir2_data_unused_t *dup, /* unused space */
  241. int *loghead) /* log the data header (out) */
  242. {
  243. xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
  244. xfs_dir2_data_free_t new; /* new bestfree entry */
  245. #ifdef __KERNEL__
  246. ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
  247. INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
  248. #endif
  249. dfp = d->hdr.bestfree;
  250. INT_COPY(new.length, dup->length, ARCH_CONVERT);
  251. INT_SET(new.offset, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dup - (char *)d));
  252. /*
  253. * Insert at position 0, 1, or 2; or not at all.
  254. */
  255. if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[0].length, ARCH_CONVERT)) {
  256. dfp[2] = dfp[1];
  257. dfp[1] = dfp[0];
  258. dfp[0] = new;
  259. *loghead = 1;
  260. return &dfp[0];
  261. }
  262. if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[1].length, ARCH_CONVERT)) {
  263. dfp[2] = dfp[1];
  264. dfp[1] = new;
  265. *loghead = 1;
  266. return &dfp[1];
  267. }
  268. if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[2].length, ARCH_CONVERT)) {
  269. dfp[2] = new;
  270. *loghead = 1;
  271. return &dfp[2];
  272. }
  273. return NULL;
  274. }
  275. /*
  276. * Remove a bestfree entry from the table.
  277. */
  278. STATIC void
  279. xfs_dir2_data_freeremove(
  280. xfs_dir2_data_t *d, /* data block pointer */
  281. xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
  282. int *loghead) /* out: log data header */
  283. {
  284. #ifdef __KERNEL__
  285. ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
  286. INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
  287. #endif
  288. /*
  289. * It's the first entry, slide the next 2 up.
  290. */
  291. if (dfp == &d->hdr.bestfree[0]) {
  292. d->hdr.bestfree[0] = d->hdr.bestfree[1];
  293. d->hdr.bestfree[1] = d->hdr.bestfree[2];
  294. }
  295. /*
  296. * It's the second entry, slide the 3rd entry up.
  297. */
  298. else if (dfp == &d->hdr.bestfree[1])
  299. d->hdr.bestfree[1] = d->hdr.bestfree[2];
  300. /*
  301. * Must be the last entry.
  302. */
  303. else
  304. ASSERT(dfp == &d->hdr.bestfree[2]);
  305. /*
  306. * Clear the 3rd entry, must be zero now.
  307. */
  308. d->hdr.bestfree[2].length = 0;
  309. d->hdr.bestfree[2].offset = 0;
  310. *loghead = 1;
  311. }
  312. /*
  313. * Given a data block, reconstruct its bestfree map.
  314. */
  315. void
  316. xfs_dir2_data_freescan(
  317. xfs_mount_t *mp, /* filesystem mount point */
  318. xfs_dir2_data_t *d, /* data block pointer */
  319. int *loghead, /* out: log data header */
  320. char *aendp) /* in: caller's endp */
  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(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
  329. INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
  330. #endif
  331. /*
  332. * Start by clearing the table.
  333. */
  334. memset(d->hdr.bestfree, 0, sizeof(d->hdr.bestfree));
  335. *loghead = 1;
  336. /*
  337. * Set up pointers.
  338. */
  339. p = (char *)d->u;
  340. if (aendp)
  341. endp = aendp;
  342. else if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
  343. btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
  344. endp = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
  345. } else
  346. endp = (char *)d + mp->m_dirblksize;
  347. /*
  348. * Loop over the block's entries.
  349. */
  350. while (p < endp) {
  351. dup = (xfs_dir2_data_unused_t *)p;
  352. /*
  353. * If it's a free entry, insert it.
  354. */
  355. if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
  356. ASSERT((char *)dup - (char *)d ==
  357. INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT));
  358. xfs_dir2_data_freeinsert(d, dup, loghead);
  359. p += INT_GET(dup->length, ARCH_CONVERT);
  360. }
  361. /*
  362. * For active entries, check their tags and skip them.
  363. */
  364. else {
  365. dep = (xfs_dir2_data_entry_t *)p;
  366. ASSERT((char *)dep - (char *)d ==
  367. INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep), ARCH_CONVERT));
  368. p += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
  369. }
  370. }
  371. }
  372. /*
  373. * Initialize a data block at the given block number in the directory.
  374. * Give back the buffer for the created block.
  375. */
  376. int /* error */
  377. xfs_dir2_data_init(
  378. xfs_da_args_t *args, /* directory operation args */
  379. xfs_dir2_db_t blkno, /* logical dir block number */
  380. xfs_dabuf_t **bpp) /* output block buffer */
  381. {
  382. xfs_dabuf_t *bp; /* block buffer */
  383. xfs_dir2_data_t *d; /* pointer to block */
  384. xfs_inode_t *dp; /* incore directory inode */
  385. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  386. int error; /* error return value */
  387. int i; /* bestfree index */
  388. xfs_mount_t *mp; /* filesystem mount point */
  389. xfs_trans_t *tp; /* transaction pointer */
  390. int t; /* temp */
  391. dp = args->dp;
  392. mp = dp->i_mount;
  393. tp = args->trans;
  394. /*
  395. * Get the buffer set up for the block.
  396. */
  397. error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, blkno), -1, &bp,
  398. XFS_DATA_FORK);
  399. if (error) {
  400. return error;
  401. }
  402. ASSERT(bp != NULL);
  403. /*
  404. * Initialize the header.
  405. */
  406. d = bp->data;
  407. INT_SET(d->hdr.magic, ARCH_CONVERT, XFS_DIR2_DATA_MAGIC);
  408. INT_SET(d->hdr.bestfree[0].offset, ARCH_CONVERT, (xfs_dir2_data_off_t)sizeof(d->hdr));
  409. for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
  410. d->hdr.bestfree[i].length = 0;
  411. d->hdr.bestfree[i].offset = 0;
  412. }
  413. /*
  414. * Set up an unused entry for the block's body.
  415. */
  416. dup = &d->u[0].unused;
  417. INT_SET(dup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
  418. t=mp->m_dirblksize - (uint)sizeof(d->hdr);
  419. INT_SET(d->hdr.bestfree[0].length, ARCH_CONVERT, t);
  420. INT_SET(dup->length, ARCH_CONVERT, t);
  421. INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT,
  422. (xfs_dir2_data_off_t)((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(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
  443. INT_GET(d->hdr.magic, ARCH_CONVERT) == 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(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
  459. INT_GET(d->hdr.magic, ARCH_CONVERT) == 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(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
  475. INT_GET(d->hdr.magic, ARCH_CONVERT) == 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 (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC)
  517. endptr = (char *)d + mp->m_dirblksize;
  518. else {
  519. xfs_dir2_block_tail_t *btp; /* block tail */
  520. ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == 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. xfs_dir2_data_off_t *tagp; /* tag just before us */
  530. tagp = (xfs_dir2_data_off_t *)((char *)d + offset) - 1;
  531. prevdup = (xfs_dir2_data_unused_t *)((char *)d + INT_GET(*tagp, ARCH_CONVERT));
  532. if (INT_GET(prevdup->freetag, ARCH_CONVERT) != 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 (INT_GET(postdup->freetag, ARCH_CONVERT) != 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;
  567. /*
  568. * Fix up the new big freespace.
  569. */
  570. INT_MOD(prevdup->length, ARCH_CONVERT, len + INT_GET(postdup->length, ARCH_CONVERT));
  571. INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup), ARCH_CONVERT,
  572. (xfs_dir2_data_off_t)((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(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(prevdup->length, ARCH_CONVERT));
  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. INT_MOD(prevdup->length, ARCH_CONVERT, len);
  605. INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup), ARCH_CONVERT,
  606. (xfs_dir2_data_off_t)((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 = INT_GET(prevdup->length, ARCH_CONVERT) > INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT);
  622. }
  623. /*
  624. * The following entry is free, merge with it.
  625. */
  626. else if (postdup) {
  627. dfp = xfs_dir2_data_freefind(d, postdup);
  628. newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
  629. INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
  630. INT_SET(newdup->length, ARCH_CONVERT, len + INT_GET(postdup->length, ARCH_CONVERT));
  631. INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
  632. (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
  633. xfs_dir2_data_log_unused(tp, bp, newdup);
  634. /*
  635. * If the following entry was in the table, the new entry
  636. * is longer, so it will be in the table too. Remove
  637. * the old one and add the new one.
  638. */
  639. if (dfp) {
  640. xfs_dir2_data_freeremove(d, dfp, needlogp);
  641. (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
  642. }
  643. /*
  644. * Otherwise we need a scan if the new entry is big enough.
  645. */
  646. else
  647. needscan = INT_GET(newdup->length, ARCH_CONVERT) > INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT);
  648. }
  649. /*
  650. * Neither neighbor is free. Make a new entry.
  651. */
  652. else {
  653. newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
  654. INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
  655. INT_SET(newdup->length, ARCH_CONVERT, len);
  656. INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
  657. (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
  658. xfs_dir2_data_log_unused(tp, bp, newdup);
  659. (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
  660. }
  661. *needscanp = needscan;
  662. }
  663. /*
  664. * Take a byte range out of an existing unused space and make it un-free.
  665. */
  666. void
  667. xfs_dir2_data_use_free(
  668. xfs_trans_t *tp, /* transaction pointer */
  669. xfs_dabuf_t *bp, /* data block buffer */
  670. xfs_dir2_data_unused_t *dup, /* unused entry */
  671. xfs_dir2_data_aoff_t offset, /* starting offset to use */
  672. xfs_dir2_data_aoff_t len, /* length to use */
  673. int *needlogp, /* out: need to log header */
  674. int *needscanp) /* out: need regen bestfree */
  675. {
  676. xfs_dir2_data_t *d; /* data block */
  677. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  678. int matchback; /* matches end of freespace */
  679. int matchfront; /* matches start of freespace */
  680. int needscan; /* need to regen bestfree */
  681. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  682. xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
  683. int oldlen; /* old unused entry's length */
  684. d = bp->data;
  685. ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
  686. INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
  687. ASSERT(INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG);
  688. ASSERT(offset >= (char *)dup - (char *)d);
  689. ASSERT(offset + len <= (char *)dup + INT_GET(dup->length, ARCH_CONVERT) - (char *)d);
  690. ASSERT((char *)dup - (char *)d == INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT));
  691. /*
  692. * Look up the entry in the bestfree table.
  693. */
  694. dfp = xfs_dir2_data_freefind(d, dup);
  695. oldlen = INT_GET(dup->length, ARCH_CONVERT);
  696. ASSERT(dfp || oldlen <= INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT));
  697. /*
  698. * Check for alignment with front and back of the entry.
  699. */
  700. matchfront = (char *)dup - (char *)d == offset;
  701. matchback = (char *)dup + oldlen - (char *)d == offset + len;
  702. ASSERT(*needscanp == 0);
  703. needscan = 0;
  704. /*
  705. * If we matched it exactly we just need to get rid of it from
  706. * the bestfree table.
  707. */
  708. if (matchfront && matchback) {
  709. if (dfp) {
  710. needscan = d->hdr.bestfree[2].offset;
  711. if (!needscan)
  712. xfs_dir2_data_freeremove(d, dfp, needlogp);
  713. }
  714. }
  715. /*
  716. * We match the first part of the entry.
  717. * Make a new entry with the remaining freespace.
  718. */
  719. else if (matchfront) {
  720. newdup = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
  721. INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
  722. INT_SET(newdup->length, ARCH_CONVERT, oldlen - len);
  723. INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
  724. (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
  725. xfs_dir2_data_log_unused(tp, bp, newdup);
  726. /*
  727. * If it was in the table, remove it and add the new one.
  728. */
  729. if (dfp) {
  730. xfs_dir2_data_freeremove(d, dfp, needlogp);
  731. dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
  732. ASSERT(dfp != NULL);
  733. ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(newdup->length, ARCH_CONVERT));
  734. ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) == (char *)newdup - (char *)d);
  735. /*
  736. * If we got inserted at the last slot,
  737. * that means we don't know if there was a better
  738. * choice for the last slot, or not. Rescan.
  739. */
  740. needscan = dfp == &d->hdr.bestfree[2];
  741. }
  742. }
  743. /*
  744. * We match the last part of the entry.
  745. * Trim the allocated space off the tail of the entry.
  746. */
  747. else if (matchback) {
  748. newdup = dup;
  749. INT_SET(newdup->length, ARCH_CONVERT, (xfs_dir2_data_off_t)
  750. (((char *)d + offset) - (char *)newdup));
  751. INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
  752. (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
  753. xfs_dir2_data_log_unused(tp, bp, newdup);
  754. /*
  755. * If it was in the table, remove it and add the new one.
  756. */
  757. if (dfp) {
  758. xfs_dir2_data_freeremove(d, dfp, needlogp);
  759. dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
  760. ASSERT(dfp != NULL);
  761. ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(newdup->length, ARCH_CONVERT));
  762. ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) == (char *)newdup - (char *)d);
  763. /*
  764. * If we got inserted at the last slot,
  765. * that means we don't know if there was a better
  766. * choice for the last slot, or not. Rescan.
  767. */
  768. needscan = dfp == &d->hdr.bestfree[2];
  769. }
  770. }
  771. /*
  772. * Poking out the middle of an entry.
  773. * Make two new entries.
  774. */
  775. else {
  776. newdup = dup;
  777. INT_SET(newdup->length, ARCH_CONVERT, (xfs_dir2_data_off_t)
  778. (((char *)d + offset) - (char *)newdup));
  779. INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
  780. (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
  781. xfs_dir2_data_log_unused(tp, bp, newdup);
  782. newdup2 = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
  783. INT_SET(newdup2->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
  784. INT_SET(newdup2->length, ARCH_CONVERT, oldlen - len - INT_GET(newdup->length, ARCH_CONVERT));
  785. INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup2), ARCH_CONVERT,
  786. (xfs_dir2_data_off_t)((char *)newdup2 - (char *)d));
  787. xfs_dir2_data_log_unused(tp, bp, newdup2);
  788. /*
  789. * If the old entry was in the table, we need to scan
  790. * if the 3rd entry was valid, since these entries
  791. * are smaller than the old one.
  792. * If we don't need to scan that means there were 1 or 2
  793. * entries in the table, and removing the old and adding
  794. * the 2 new will work.
  795. */
  796. if (dfp) {
  797. needscan = d->hdr.bestfree[2].length;
  798. if (!needscan) {
  799. xfs_dir2_data_freeremove(d, dfp, needlogp);
  800. (void)xfs_dir2_data_freeinsert(d, newdup,
  801. needlogp);
  802. (void)xfs_dir2_data_freeinsert(d, newdup2,
  803. needlogp);
  804. }
  805. }
  806. }
  807. *needscanp = needscan;
  808. }