xfs_ialloc_btree.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_btree.h"
  34. #include "xfs_btree_trace.h"
  35. #include "xfs_ialloc.h"
  36. #include "xfs_alloc.h"
  37. #include "xfs_error.h"
  38. STATIC int
  39. xfs_inobt_get_minrecs(
  40. struct xfs_btree_cur *cur,
  41. int level)
  42. {
  43. return cur->bc_mp->m_inobt_mnr[level != 0];
  44. }
  45. STATIC struct xfs_btree_cur *
  46. xfs_inobt_dup_cursor(
  47. struct xfs_btree_cur *cur)
  48. {
  49. return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
  50. cur->bc_private.a.agbp, cur->bc_private.a.agno);
  51. }
  52. STATIC void
  53. xfs_inobt_set_root(
  54. struct xfs_btree_cur *cur,
  55. union xfs_btree_ptr *nptr,
  56. int inc) /* level change */
  57. {
  58. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  59. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  60. agi->agi_root = nptr->s;
  61. be32_add_cpu(&agi->agi_level, inc);
  62. xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
  63. }
  64. STATIC int
  65. xfs_inobt_alloc_block(
  66. struct xfs_btree_cur *cur,
  67. union xfs_btree_ptr *start,
  68. union xfs_btree_ptr *new,
  69. int length,
  70. int *stat)
  71. {
  72. xfs_alloc_arg_t args; /* block allocation args */
  73. int error; /* error return value */
  74. xfs_agblock_t sbno = be32_to_cpu(start->s);
  75. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  76. memset(&args, 0, sizeof(args));
  77. args.tp = cur->bc_tp;
  78. args.mp = cur->bc_mp;
  79. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
  80. args.minlen = 1;
  81. args.maxlen = 1;
  82. args.prod = 1;
  83. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  84. error = xfs_alloc_vextent(&args);
  85. if (error) {
  86. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  87. return error;
  88. }
  89. if (args.fsbno == NULLFSBLOCK) {
  90. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  91. *stat = 0;
  92. return 0;
  93. }
  94. ASSERT(args.len == 1);
  95. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  96. new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
  97. *stat = 1;
  98. return 0;
  99. }
  100. STATIC int
  101. xfs_inobt_free_block(
  102. struct xfs_btree_cur *cur,
  103. struct xfs_buf *bp)
  104. {
  105. xfs_fsblock_t fsbno;
  106. int error;
  107. fsbno = XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(bp));
  108. error = xfs_free_extent(cur->bc_tp, fsbno, 1);
  109. if (error)
  110. return error;
  111. xfs_trans_binval(cur->bc_tp, bp);
  112. return error;
  113. }
  114. STATIC int
  115. xfs_inobt_get_maxrecs(
  116. struct xfs_btree_cur *cur,
  117. int level)
  118. {
  119. return cur->bc_mp->m_inobt_mxr[level != 0];
  120. }
  121. STATIC void
  122. xfs_inobt_init_key_from_rec(
  123. union xfs_btree_key *key,
  124. union xfs_btree_rec *rec)
  125. {
  126. key->inobt.ir_startino = rec->inobt.ir_startino;
  127. }
  128. STATIC void
  129. xfs_inobt_init_rec_from_key(
  130. union xfs_btree_key *key,
  131. union xfs_btree_rec *rec)
  132. {
  133. rec->inobt.ir_startino = key->inobt.ir_startino;
  134. }
  135. STATIC void
  136. xfs_inobt_init_rec_from_cur(
  137. struct xfs_btree_cur *cur,
  138. union xfs_btree_rec *rec)
  139. {
  140. rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
  141. rec->inobt.ir_freecount = cpu_to_be32(cur->bc_rec.i.ir_freecount);
  142. rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
  143. }
  144. /*
  145. * initial value of ptr for lookup
  146. */
  147. STATIC void
  148. xfs_inobt_init_ptr_from_cur(
  149. struct xfs_btree_cur *cur,
  150. union xfs_btree_ptr *ptr)
  151. {
  152. struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  153. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
  154. ptr->s = agi->agi_root;
  155. }
  156. STATIC __int64_t
  157. xfs_inobt_key_diff(
  158. struct xfs_btree_cur *cur,
  159. union xfs_btree_key *key)
  160. {
  161. return (__int64_t)be32_to_cpu(key->inobt.ir_startino) -
  162. cur->bc_rec.i.ir_startino;
  163. }
  164. STATIC int
  165. xfs_inobt_kill_root(
  166. struct xfs_btree_cur *cur,
  167. struct xfs_buf *bp,
  168. int level,
  169. union xfs_btree_ptr *newroot)
  170. {
  171. int error;
  172. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  173. XFS_BTREE_STATS_INC(cur, killroot);
  174. /*
  175. * Update the root pointer, decreasing the level by 1 and then
  176. * free the old root.
  177. */
  178. xfs_inobt_set_root(cur, newroot, -1);
  179. error = xfs_inobt_free_block(cur, bp);
  180. if (error) {
  181. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  182. return error;
  183. }
  184. XFS_BTREE_STATS_INC(cur, free);
  185. cur->bc_bufs[level] = NULL;
  186. cur->bc_nlevels--;
  187. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  188. return 0;
  189. }
  190. #ifdef DEBUG
  191. STATIC int
  192. xfs_inobt_keys_inorder(
  193. struct xfs_btree_cur *cur,
  194. union xfs_btree_key *k1,
  195. union xfs_btree_key *k2)
  196. {
  197. return be32_to_cpu(k1->inobt.ir_startino) <
  198. be32_to_cpu(k2->inobt.ir_startino);
  199. }
  200. STATIC int
  201. xfs_inobt_recs_inorder(
  202. struct xfs_btree_cur *cur,
  203. union xfs_btree_rec *r1,
  204. union xfs_btree_rec *r2)
  205. {
  206. return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
  207. be32_to_cpu(r2->inobt.ir_startino);
  208. }
  209. #endif /* DEBUG */
  210. #ifdef XFS_BTREE_TRACE
  211. ktrace_t *xfs_inobt_trace_buf;
  212. STATIC void
  213. xfs_inobt_trace_enter(
  214. struct xfs_btree_cur *cur,
  215. const char *func,
  216. char *s,
  217. int type,
  218. int line,
  219. __psunsigned_t a0,
  220. __psunsigned_t a1,
  221. __psunsigned_t a2,
  222. __psunsigned_t a3,
  223. __psunsigned_t a4,
  224. __psunsigned_t a5,
  225. __psunsigned_t a6,
  226. __psunsigned_t a7,
  227. __psunsigned_t a8,
  228. __psunsigned_t a9,
  229. __psunsigned_t a10)
  230. {
  231. ktrace_enter(xfs_inobt_trace_buf, (void *)(__psint_t)type,
  232. (void *)func, (void *)s, NULL, (void *)cur,
  233. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  234. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  235. (void *)a8, (void *)a9, (void *)a10);
  236. }
  237. STATIC void
  238. xfs_inobt_trace_cursor(
  239. struct xfs_btree_cur *cur,
  240. __uint32_t *s0,
  241. __uint64_t *l0,
  242. __uint64_t *l1)
  243. {
  244. *s0 = cur->bc_private.a.agno;
  245. *l0 = cur->bc_rec.i.ir_startino;
  246. *l1 = cur->bc_rec.i.ir_free;
  247. }
  248. STATIC void
  249. xfs_inobt_trace_key(
  250. struct xfs_btree_cur *cur,
  251. union xfs_btree_key *key,
  252. __uint64_t *l0,
  253. __uint64_t *l1)
  254. {
  255. *l0 = be32_to_cpu(key->inobt.ir_startino);
  256. *l1 = 0;
  257. }
  258. STATIC void
  259. xfs_inobt_trace_record(
  260. struct xfs_btree_cur *cur,
  261. union xfs_btree_rec *rec,
  262. __uint64_t *l0,
  263. __uint64_t *l1,
  264. __uint64_t *l2)
  265. {
  266. *l0 = be32_to_cpu(rec->inobt.ir_startino);
  267. *l1 = be32_to_cpu(rec->inobt.ir_freecount);
  268. *l2 = be64_to_cpu(rec->inobt.ir_free);
  269. }
  270. #endif /* XFS_BTREE_TRACE */
  271. static const struct xfs_btree_ops xfs_inobt_ops = {
  272. .rec_len = sizeof(xfs_inobt_rec_t),
  273. .key_len = sizeof(xfs_inobt_key_t),
  274. .dup_cursor = xfs_inobt_dup_cursor,
  275. .set_root = xfs_inobt_set_root,
  276. .kill_root = xfs_inobt_kill_root,
  277. .alloc_block = xfs_inobt_alloc_block,
  278. .free_block = xfs_inobt_free_block,
  279. .get_minrecs = xfs_inobt_get_minrecs,
  280. .get_maxrecs = xfs_inobt_get_maxrecs,
  281. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  282. .init_rec_from_key = xfs_inobt_init_rec_from_key,
  283. .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
  284. .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
  285. .key_diff = xfs_inobt_key_diff,
  286. #ifdef DEBUG
  287. .keys_inorder = xfs_inobt_keys_inorder,
  288. .recs_inorder = xfs_inobt_recs_inorder,
  289. #endif
  290. #ifdef XFS_BTREE_TRACE
  291. .trace_enter = xfs_inobt_trace_enter,
  292. .trace_cursor = xfs_inobt_trace_cursor,
  293. .trace_key = xfs_inobt_trace_key,
  294. .trace_record = xfs_inobt_trace_record,
  295. #endif
  296. };
  297. /*
  298. * Allocate a new inode btree cursor.
  299. */
  300. struct xfs_btree_cur * /* new inode btree cursor */
  301. xfs_inobt_init_cursor(
  302. struct xfs_mount *mp, /* file system mount point */
  303. struct xfs_trans *tp, /* transaction pointer */
  304. struct xfs_buf *agbp, /* buffer for agi structure */
  305. xfs_agnumber_t agno) /* allocation group number */
  306. {
  307. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  308. struct xfs_btree_cur *cur;
  309. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  310. cur->bc_tp = tp;
  311. cur->bc_mp = mp;
  312. cur->bc_nlevels = be32_to_cpu(agi->agi_level);
  313. cur->bc_btnum = XFS_BTNUM_INO;
  314. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  315. cur->bc_ops = &xfs_inobt_ops;
  316. cur->bc_private.a.agbp = agbp;
  317. cur->bc_private.a.agno = agno;
  318. return cur;
  319. }
  320. /*
  321. * Calculate number of records in an inobt btree block.
  322. */
  323. int
  324. xfs_inobt_maxrecs(
  325. struct xfs_mount *mp,
  326. int blocklen,
  327. int leaf)
  328. {
  329. blocklen -= XFS_INOBT_BLOCK_LEN(mp);
  330. if (leaf)
  331. return blocklen / sizeof(xfs_inobt_rec_t);
  332. return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
  333. }