xfs_ialloc_btree.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #ifdef DEBUG
  165. STATIC int
  166. xfs_inobt_keys_inorder(
  167. struct xfs_btree_cur *cur,
  168. union xfs_btree_key *k1,
  169. union xfs_btree_key *k2)
  170. {
  171. return be32_to_cpu(k1->inobt.ir_startino) <
  172. be32_to_cpu(k2->inobt.ir_startino);
  173. }
  174. STATIC int
  175. xfs_inobt_recs_inorder(
  176. struct xfs_btree_cur *cur,
  177. union xfs_btree_rec *r1,
  178. union xfs_btree_rec *r2)
  179. {
  180. return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
  181. be32_to_cpu(r2->inobt.ir_startino);
  182. }
  183. #endif /* DEBUG */
  184. #ifdef XFS_BTREE_TRACE
  185. ktrace_t *xfs_inobt_trace_buf;
  186. STATIC void
  187. xfs_inobt_trace_enter(
  188. struct xfs_btree_cur *cur,
  189. const char *func,
  190. char *s,
  191. int type,
  192. int line,
  193. __psunsigned_t a0,
  194. __psunsigned_t a1,
  195. __psunsigned_t a2,
  196. __psunsigned_t a3,
  197. __psunsigned_t a4,
  198. __psunsigned_t a5,
  199. __psunsigned_t a6,
  200. __psunsigned_t a7,
  201. __psunsigned_t a8,
  202. __psunsigned_t a9,
  203. __psunsigned_t a10)
  204. {
  205. ktrace_enter(xfs_inobt_trace_buf, (void *)(__psint_t)type,
  206. (void *)func, (void *)s, NULL, (void *)cur,
  207. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  208. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  209. (void *)a8, (void *)a9, (void *)a10);
  210. }
  211. STATIC void
  212. xfs_inobt_trace_cursor(
  213. struct xfs_btree_cur *cur,
  214. __uint32_t *s0,
  215. __uint64_t *l0,
  216. __uint64_t *l1)
  217. {
  218. *s0 = cur->bc_private.a.agno;
  219. *l0 = cur->bc_rec.i.ir_startino;
  220. *l1 = cur->bc_rec.i.ir_free;
  221. }
  222. STATIC void
  223. xfs_inobt_trace_key(
  224. struct xfs_btree_cur *cur,
  225. union xfs_btree_key *key,
  226. __uint64_t *l0,
  227. __uint64_t *l1)
  228. {
  229. *l0 = be32_to_cpu(key->inobt.ir_startino);
  230. *l1 = 0;
  231. }
  232. STATIC void
  233. xfs_inobt_trace_record(
  234. struct xfs_btree_cur *cur,
  235. union xfs_btree_rec *rec,
  236. __uint64_t *l0,
  237. __uint64_t *l1,
  238. __uint64_t *l2)
  239. {
  240. *l0 = be32_to_cpu(rec->inobt.ir_startino);
  241. *l1 = be32_to_cpu(rec->inobt.ir_freecount);
  242. *l2 = be64_to_cpu(rec->inobt.ir_free);
  243. }
  244. #endif /* XFS_BTREE_TRACE */
  245. static const struct xfs_btree_ops xfs_inobt_ops = {
  246. .rec_len = sizeof(xfs_inobt_rec_t),
  247. .key_len = sizeof(xfs_inobt_key_t),
  248. .dup_cursor = xfs_inobt_dup_cursor,
  249. .set_root = xfs_inobt_set_root,
  250. .alloc_block = xfs_inobt_alloc_block,
  251. .free_block = xfs_inobt_free_block,
  252. .get_minrecs = xfs_inobt_get_minrecs,
  253. .get_maxrecs = xfs_inobt_get_maxrecs,
  254. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  255. .init_rec_from_key = xfs_inobt_init_rec_from_key,
  256. .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
  257. .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
  258. .key_diff = xfs_inobt_key_diff,
  259. #ifdef DEBUG
  260. .keys_inorder = xfs_inobt_keys_inorder,
  261. .recs_inorder = xfs_inobt_recs_inorder,
  262. #endif
  263. #ifdef XFS_BTREE_TRACE
  264. .trace_enter = xfs_inobt_trace_enter,
  265. .trace_cursor = xfs_inobt_trace_cursor,
  266. .trace_key = xfs_inobt_trace_key,
  267. .trace_record = xfs_inobt_trace_record,
  268. #endif
  269. };
  270. /*
  271. * Allocate a new inode btree cursor.
  272. */
  273. struct xfs_btree_cur * /* new inode btree cursor */
  274. xfs_inobt_init_cursor(
  275. struct xfs_mount *mp, /* file system mount point */
  276. struct xfs_trans *tp, /* transaction pointer */
  277. struct xfs_buf *agbp, /* buffer for agi structure */
  278. xfs_agnumber_t agno) /* allocation group number */
  279. {
  280. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  281. struct xfs_btree_cur *cur;
  282. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  283. cur->bc_tp = tp;
  284. cur->bc_mp = mp;
  285. cur->bc_nlevels = be32_to_cpu(agi->agi_level);
  286. cur->bc_btnum = XFS_BTNUM_INO;
  287. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  288. cur->bc_ops = &xfs_inobt_ops;
  289. cur->bc_private.a.agbp = agbp;
  290. cur->bc_private.a.agno = agno;
  291. return cur;
  292. }
  293. /*
  294. * Calculate number of records in an inobt btree block.
  295. */
  296. int
  297. xfs_inobt_maxrecs(
  298. struct xfs_mount *mp,
  299. int blocklen,
  300. int leaf)
  301. {
  302. blocklen -= XFS_INOBT_BLOCK_LEN(mp);
  303. if (leaf)
  304. return blocklen / sizeof(xfs_inobt_rec_t);
  305. return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
  306. }