xfs_dir2_data.c 25 KB

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