xfs_ialloc_btree.c 8.6 KB

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