xfs_ialloc_btree.c 8.9 KB

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