xfs_ialloc_btree.c 7.8 KB

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