xfs_alloc_btree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (c) 2000-2001,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_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_btree_trace.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_alloc.h"
  41. #include "xfs_error.h"
  42. STATIC struct xfs_btree_cur *
  43. xfs_allocbt_dup_cursor(
  44. struct xfs_btree_cur *cur)
  45. {
  46. return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
  47. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  48. cur->bc_btnum);
  49. }
  50. STATIC void
  51. xfs_allocbt_set_root(
  52. struct xfs_btree_cur *cur,
  53. union xfs_btree_ptr *ptr,
  54. int inc)
  55. {
  56. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  57. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  58. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  59. int btnum = cur->bc_btnum;
  60. ASSERT(ptr->s != 0);
  61. agf->agf_roots[btnum] = ptr->s;
  62. be32_add_cpu(&agf->agf_levels[btnum], inc);
  63. cur->bc_mp->m_perag[seqno].pagf_levels[btnum] += inc;
  64. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  65. }
  66. STATIC int
  67. xfs_allocbt_alloc_block(
  68. struct xfs_btree_cur *cur,
  69. union xfs_btree_ptr *start,
  70. union xfs_btree_ptr *new,
  71. int length,
  72. int *stat)
  73. {
  74. int error;
  75. xfs_agblock_t bno;
  76. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  77. /* Allocate the new block from the freelist. If we can't, give up. */
  78. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  79. &bno, 1);
  80. if (error) {
  81. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  82. return error;
  83. }
  84. if (bno == NULLAGBLOCK) {
  85. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  86. *stat = 0;
  87. return 0;
  88. }
  89. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  90. new->s = cpu_to_be32(bno);
  91. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  92. *stat = 1;
  93. return 0;
  94. }
  95. STATIC int
  96. xfs_allocbt_free_block(
  97. struct xfs_btree_cur *cur,
  98. struct xfs_buf *bp)
  99. {
  100. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  101. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  102. xfs_agblock_t bno;
  103. int error;
  104. bno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(bp));
  105. error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
  106. if (error)
  107. return error;
  108. /*
  109. * Since blocks move to the free list without the coordination used in
  110. * xfs_bmap_finish, we can't allow block to be available for
  111. * reallocation and non-transaction writing (user data) until we know
  112. * that the transaction that moved it to the free list is permanently
  113. * on disk. We track the blocks by declaring these blocks as "busy";
  114. * the busy list is maintained on a per-ag basis and each transaction
  115. * records which entries should be removed when the iclog commits to
  116. * disk. If a busy block is allocated, the iclog is pushed up to the
  117. * LSN that freed the block.
  118. */
  119. xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
  120. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  121. return 0;
  122. }
  123. /*
  124. * Update the longest extent in the AGF
  125. */
  126. STATIC void
  127. xfs_allocbt_update_lastrec(
  128. struct xfs_btree_cur *cur,
  129. struct xfs_btree_block *block,
  130. union xfs_btree_rec *rec,
  131. int ptr,
  132. int reason)
  133. {
  134. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  135. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  136. __be32 len;
  137. int numrecs;
  138. ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
  139. switch (reason) {
  140. case LASTREC_UPDATE:
  141. /*
  142. * If this is the last leaf block and it's the last record,
  143. * then update the size of the longest extent in the AG.
  144. */
  145. if (ptr != xfs_btree_get_numrecs(block))
  146. return;
  147. len = rec->alloc.ar_blockcount;
  148. break;
  149. case LASTREC_INSREC:
  150. if (be32_to_cpu(rec->alloc.ar_blockcount) <=
  151. be32_to_cpu(agf->agf_longest))
  152. return;
  153. len = rec->alloc.ar_blockcount;
  154. break;
  155. case LASTREC_DELREC:
  156. numrecs = xfs_btree_get_numrecs(block);
  157. if (ptr <= numrecs)
  158. return;
  159. ASSERT(ptr == numrecs + 1);
  160. if (numrecs) {
  161. xfs_alloc_rec_t *rrp;
  162. rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
  163. len = rrp->ar_blockcount;
  164. } else {
  165. len = 0;
  166. }
  167. break;
  168. default:
  169. ASSERT(0);
  170. return;
  171. }
  172. agf->agf_longest = len;
  173. cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
  174. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
  175. }
  176. STATIC int
  177. xfs_allocbt_get_minrecs(
  178. struct xfs_btree_cur *cur,
  179. int level)
  180. {
  181. return cur->bc_mp->m_alloc_mnr[level != 0];
  182. }
  183. STATIC int
  184. xfs_allocbt_get_maxrecs(
  185. struct xfs_btree_cur *cur,
  186. int level)
  187. {
  188. return cur->bc_mp->m_alloc_mxr[level != 0];
  189. }
  190. STATIC void
  191. xfs_allocbt_init_key_from_rec(
  192. union xfs_btree_key *key,
  193. union xfs_btree_rec *rec)
  194. {
  195. ASSERT(rec->alloc.ar_startblock != 0);
  196. key->alloc.ar_startblock = rec->alloc.ar_startblock;
  197. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  198. }
  199. STATIC void
  200. xfs_allocbt_init_rec_from_key(
  201. union xfs_btree_key *key,
  202. union xfs_btree_rec *rec)
  203. {
  204. ASSERT(key->alloc.ar_startblock != 0);
  205. rec->alloc.ar_startblock = key->alloc.ar_startblock;
  206. rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
  207. }
  208. STATIC void
  209. xfs_allocbt_init_rec_from_cur(
  210. struct xfs_btree_cur *cur,
  211. union xfs_btree_rec *rec)
  212. {
  213. ASSERT(cur->bc_rec.a.ar_startblock != 0);
  214. rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
  215. rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
  216. }
  217. STATIC void
  218. xfs_allocbt_init_ptr_from_cur(
  219. struct xfs_btree_cur *cur,
  220. union xfs_btree_ptr *ptr)
  221. {
  222. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  223. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  224. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  225. ptr->s = agf->agf_roots[cur->bc_btnum];
  226. }
  227. STATIC __int64_t
  228. xfs_allocbt_key_diff(
  229. struct xfs_btree_cur *cur,
  230. union xfs_btree_key *key)
  231. {
  232. xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
  233. xfs_alloc_key_t *kp = &key->alloc;
  234. __int64_t diff;
  235. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  236. return (__int64_t)be32_to_cpu(kp->ar_startblock) -
  237. rec->ar_startblock;
  238. }
  239. diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
  240. if (diff)
  241. return diff;
  242. return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  243. }
  244. STATIC int
  245. xfs_allocbt_kill_root(
  246. struct xfs_btree_cur *cur,
  247. struct xfs_buf *bp,
  248. int level,
  249. union xfs_btree_ptr *newroot)
  250. {
  251. int error;
  252. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  253. XFS_BTREE_STATS_INC(cur, killroot);
  254. /*
  255. * Update the root pointer, decreasing the level by 1 and then
  256. * free the old root.
  257. */
  258. xfs_allocbt_set_root(cur, newroot, -1);
  259. error = xfs_allocbt_free_block(cur, bp);
  260. if (error) {
  261. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  262. return error;
  263. }
  264. XFS_BTREE_STATS_INC(cur, free);
  265. xfs_btree_setbuf(cur, level, NULL);
  266. cur->bc_nlevels--;
  267. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  268. return 0;
  269. }
  270. #ifdef XFS_BTREE_TRACE
  271. ktrace_t *xfs_allocbt_trace_buf;
  272. STATIC void
  273. xfs_allocbt_trace_enter(
  274. struct xfs_btree_cur *cur,
  275. const char *func,
  276. char *s,
  277. int type,
  278. int line,
  279. __psunsigned_t a0,
  280. __psunsigned_t a1,
  281. __psunsigned_t a2,
  282. __psunsigned_t a3,
  283. __psunsigned_t a4,
  284. __psunsigned_t a5,
  285. __psunsigned_t a6,
  286. __psunsigned_t a7,
  287. __psunsigned_t a8,
  288. __psunsigned_t a9,
  289. __psunsigned_t a10)
  290. {
  291. ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
  292. (void *)func, (void *)s, NULL, (void *)cur,
  293. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  294. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  295. (void *)a8, (void *)a9, (void *)a10);
  296. }
  297. STATIC void
  298. xfs_allocbt_trace_cursor(
  299. struct xfs_btree_cur *cur,
  300. __uint32_t *s0,
  301. __uint64_t *l0,
  302. __uint64_t *l1)
  303. {
  304. *s0 = cur->bc_private.a.agno;
  305. *l0 = cur->bc_rec.a.ar_startblock;
  306. *l1 = cur->bc_rec.a.ar_blockcount;
  307. }
  308. STATIC void
  309. xfs_allocbt_trace_key(
  310. struct xfs_btree_cur *cur,
  311. union xfs_btree_key *key,
  312. __uint64_t *l0,
  313. __uint64_t *l1)
  314. {
  315. *l0 = be32_to_cpu(key->alloc.ar_startblock);
  316. *l1 = be32_to_cpu(key->alloc.ar_blockcount);
  317. }
  318. STATIC void
  319. xfs_allocbt_trace_record(
  320. struct xfs_btree_cur *cur,
  321. union xfs_btree_rec *rec,
  322. __uint64_t *l0,
  323. __uint64_t *l1,
  324. __uint64_t *l2)
  325. {
  326. *l0 = be32_to_cpu(rec->alloc.ar_startblock);
  327. *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
  328. *l2 = 0;
  329. }
  330. #endif /* XFS_BTREE_TRACE */
  331. static const struct xfs_btree_ops xfs_allocbt_ops = {
  332. .rec_len = sizeof(xfs_alloc_rec_t),
  333. .key_len = sizeof(xfs_alloc_key_t),
  334. .dup_cursor = xfs_allocbt_dup_cursor,
  335. .set_root = xfs_allocbt_set_root,
  336. .kill_root = xfs_allocbt_kill_root,
  337. .alloc_block = xfs_allocbt_alloc_block,
  338. .free_block = xfs_allocbt_free_block,
  339. .update_lastrec = xfs_allocbt_update_lastrec,
  340. .get_minrecs = xfs_allocbt_get_minrecs,
  341. .get_maxrecs = xfs_allocbt_get_maxrecs,
  342. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  343. .init_rec_from_key = xfs_allocbt_init_rec_from_key,
  344. .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
  345. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  346. .key_diff = xfs_allocbt_key_diff,
  347. #ifdef XFS_BTREE_TRACE
  348. .trace_enter = xfs_allocbt_trace_enter,
  349. .trace_cursor = xfs_allocbt_trace_cursor,
  350. .trace_key = xfs_allocbt_trace_key,
  351. .trace_record = xfs_allocbt_trace_record,
  352. #endif
  353. };
  354. /*
  355. * Allocate a new allocation btree cursor.
  356. */
  357. struct xfs_btree_cur * /* new alloc btree cursor */
  358. xfs_allocbt_init_cursor(
  359. struct xfs_mount *mp, /* file system mount point */
  360. struct xfs_trans *tp, /* transaction pointer */
  361. struct xfs_buf *agbp, /* buffer for agf structure */
  362. xfs_agnumber_t agno, /* allocation group number */
  363. xfs_btnum_t btnum) /* btree identifier */
  364. {
  365. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  366. struct xfs_btree_cur *cur;
  367. ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
  368. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  369. cur->bc_tp = tp;
  370. cur->bc_mp = mp;
  371. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
  372. cur->bc_btnum = btnum;
  373. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  374. cur->bc_ops = &xfs_allocbt_ops;
  375. if (btnum == XFS_BTNUM_CNT)
  376. cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
  377. cur->bc_private.a.agbp = agbp;
  378. cur->bc_private.a.agno = agno;
  379. return cur;
  380. }