xfs_dir2_data.c 27 KB

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