xfs_dir2_data.c 27 KB

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