]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
da23017d NS |
2 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
5000d01d | 4 | * |
da23017d NS |
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 | |
2bd0ea18 | 7 | * published by the Free Software Foundation. |
5000d01d | 8 | * |
da23017d NS |
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. | |
5000d01d | 13 | * |
da23017d NS |
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 | |
2bd0ea18 | 17 | */ |
9c799827 | 18 | #include "libxfs_priv.h" |
b626fb59 DC |
19 | #include "xfs_fs.h" |
20 | #include "xfs_shared.h" | |
21 | #include "xfs_format.h" | |
22 | #include "xfs_log_format.h" | |
23 | #include "xfs_trans_resv.h" | |
24 | #include "xfs_bit.h" | |
25 | #include "xfs_mount.h" | |
26 | #include "xfs_inode.h" | |
27 | #include "xfs_btree.h" | |
28 | #include "xfs_ialloc.h" | |
29 | #include "xfs_ialloc_btree.h" | |
30 | #include "xfs_alloc.h" | |
31 | #include "xfs_trace.h" | |
32 | #include "xfs_cksum.h" | |
33 | #include "xfs_trans.h" | |
85aec44f | 34 | #include "xfs_rmap.h" |
b626fb59 | 35 | |
2bd0ea18 | 36 | |
b194c7d8 BN |
37 | STATIC int |
38 | xfs_inobt_get_minrecs( | |
39 | struct xfs_btree_cur *cur, | |
40 | int level) | |
2bd0ea18 | 41 | { |
b194c7d8 | 42 | return cur->bc_mp->m_inobt_mnr[level != 0]; |
2bd0ea18 NS |
43 | } |
44 | ||
b194c7d8 BN |
45 | STATIC struct xfs_btree_cur * |
46 | xfs_inobt_dup_cursor( | |
47 | struct xfs_btree_cur *cur) | |
2bd0ea18 | 48 | { |
b194c7d8 | 49 | return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp, |
70eb7337 BF |
50 | cur->bc_private.a.agbp, cur->bc_private.a.agno, |
51 | cur->bc_btnum); | |
2bd0ea18 NS |
52 | } |
53 | ||
2bd0ea18 | 54 | STATIC void |
b194c7d8 BN |
55 | xfs_inobt_set_root( |
56 | struct xfs_btree_cur *cur, | |
57 | union xfs_btree_ptr *nptr, | |
58 | int inc) /* level change */ | |
2bd0ea18 | 59 | { |
b194c7d8 BN |
60 | struct xfs_buf *agbp = cur->bc_private.a.agbp; |
61 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); | |
2bd0ea18 | 62 | |
b194c7d8 BN |
63 | agi->agi_root = nptr->s; |
64 | be32_add_cpu(&agi->agi_level, inc); | |
65 | xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL); | |
2bd0ea18 NS |
66 | } |
67 | ||
c0a4c227 BF |
68 | STATIC void |
69 | xfs_finobt_set_root( | |
70 | struct xfs_btree_cur *cur, | |
71 | union xfs_btree_ptr *nptr, | |
72 | int inc) /* level change */ | |
73 | { | |
74 | struct xfs_buf *agbp = cur->bc_private.a.agbp; | |
75 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); | |
76 | ||
77 | agi->agi_free_root = nptr->s; | |
78 | be32_add_cpu(&agi->agi_free_level, inc); | |
79 | xfs_ialloc_log_agi(cur->bc_tp, agbp, | |
80 | XFS_AGI_FREE_ROOT | XFS_AGI_FREE_LEVEL); | |
81 | } | |
82 | ||
b194c7d8 | 83 | STATIC int |
74e502ac | 84 | __xfs_inobt_alloc_block( |
b194c7d8 BN |
85 | struct xfs_btree_cur *cur, |
86 | union xfs_btree_ptr *start, | |
87 | union xfs_btree_ptr *new, | |
74e502ac CH |
88 | int *stat, |
89 | enum xfs_ag_resv_type resv) | |
2bd0ea18 | 90 | { |
b194c7d8 BN |
91 | xfs_alloc_arg_t args; /* block allocation args */ |
92 | int error; /* error return value */ | |
93 | xfs_agblock_t sbno = be32_to_cpu(start->s); | |
2bd0ea18 | 94 | |
b194c7d8 | 95 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
2bd0ea18 | 96 | |
b194c7d8 | 97 | memset(&args, 0, sizeof(args)); |
2bd0ea18 NS |
98 | args.tp = cur->bc_tp; |
99 | args.mp = cur->bc_mp; | |
85aec44f | 100 | xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_INOBT); |
b194c7d8 BN |
101 | args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno); |
102 | args.minlen = 1; | |
103 | args.maxlen = 1; | |
104 | args.prod = 1; | |
2bd0ea18 | 105 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
74e502ac | 106 | args.resv = resv; |
b194c7d8 BN |
107 | |
108 | error = xfs_alloc_vextent(&args); | |
109 | if (error) { | |
110 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
2bd0ea18 | 111 | return error; |
b194c7d8 | 112 | } |
2bd0ea18 | 113 | if (args.fsbno == NULLFSBLOCK) { |
b194c7d8 | 114 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
2bd0ea18 NS |
115 | *stat = 0; |
116 | return 0; | |
117 | } | |
118 | ASSERT(args.len == 1); | |
b194c7d8 BN |
119 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
120 | ||
121 | new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno)); | |
2bd0ea18 NS |
122 | *stat = 1; |
123 | return 0; | |
124 | } | |
125 | ||
74e502ac CH |
126 | STATIC int |
127 | xfs_inobt_alloc_block( | |
128 | struct xfs_btree_cur *cur, | |
129 | union xfs_btree_ptr *start, | |
130 | union xfs_btree_ptr *new, | |
131 | int *stat) | |
132 | { | |
133 | return __xfs_inobt_alloc_block(cur, start, new, stat, XFS_AG_RESV_NONE); | |
134 | } | |
135 | ||
136 | STATIC int | |
137 | xfs_finobt_alloc_block( | |
138 | struct xfs_btree_cur *cur, | |
139 | union xfs_btree_ptr *start, | |
140 | union xfs_btree_ptr *new, | |
141 | int *stat) | |
142 | { | |
8504509d BF |
143 | if (cur->bc_mp->m_inotbt_nores) |
144 | return xfs_inobt_alloc_block(cur, start, new, stat); | |
74e502ac CH |
145 | return __xfs_inobt_alloc_block(cur, start, new, stat, |
146 | XFS_AG_RESV_METADATA); | |
147 | } | |
148 | ||
b194c7d8 | 149 | STATIC int |
8504509d | 150 | __xfs_inobt_free_block( |
b194c7d8 | 151 | struct xfs_btree_cur *cur, |
8504509d BF |
152 | struct xfs_buf *bp, |
153 | enum xfs_ag_resv_type resv) | |
2bd0ea18 | 154 | { |
85aec44f DW |
155 | struct xfs_owner_info oinfo; |
156 | ||
157 | xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INOBT); | |
08caf14f | 158 | return xfs_free_extent(cur->bc_tp, |
85aec44f | 159 | XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(bp)), 1, |
8504509d BF |
160 | &oinfo, resv); |
161 | } | |
162 | ||
163 | STATIC int | |
164 | xfs_inobt_free_block( | |
165 | struct xfs_btree_cur *cur, | |
166 | struct xfs_buf *bp) | |
167 | { | |
168 | return __xfs_inobt_free_block(cur, bp, XFS_AG_RESV_NONE); | |
169 | } | |
170 | ||
171 | STATIC int | |
172 | xfs_finobt_free_block( | |
173 | struct xfs_btree_cur *cur, | |
174 | struct xfs_buf *bp) | |
175 | { | |
176 | if (cur->bc_mp->m_inotbt_nores) | |
177 | return xfs_inobt_free_block(cur, bp); | |
178 | return __xfs_inobt_free_block(cur, bp, XFS_AG_RESV_METADATA); | |
2bd0ea18 NS |
179 | } |
180 | ||
b194c7d8 BN |
181 | STATIC int |
182 | xfs_inobt_get_maxrecs( | |
183 | struct xfs_btree_cur *cur, | |
184 | int level) | |
2bd0ea18 | 185 | { |
b194c7d8 BN |
186 | return cur->bc_mp->m_inobt_mxr[level != 0]; |
187 | } | |
2bd0ea18 | 188 | |
b194c7d8 BN |
189 | STATIC void |
190 | xfs_inobt_init_key_from_rec( | |
191 | union xfs_btree_key *key, | |
192 | union xfs_btree_rec *rec) | |
193 | { | |
194 | key->inobt.ir_startino = rec->inobt.ir_startino; | |
195 | } | |
2bd0ea18 | 196 | |
4e388bc9 DW |
197 | STATIC void |
198 | xfs_inobt_init_high_key_from_rec( | |
199 | union xfs_btree_key *key, | |
200 | union xfs_btree_rec *rec) | |
201 | { | |
202 | __u32 x; | |
203 | ||
204 | x = be32_to_cpu(rec->inobt.ir_startino); | |
205 | x += XFS_INODES_PER_CHUNK - 1; | |
206 | key->inobt.ir_startino = cpu_to_be32(x); | |
207 | } | |
208 | ||
b194c7d8 BN |
209 | STATIC void |
210 | xfs_inobt_init_rec_from_cur( | |
211 | struct xfs_btree_cur *cur, | |
212 | union xfs_btree_rec *rec) | |
213 | { | |
214 | rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino); | |
11640e30 BF |
215 | if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) { |
216 | rec->inobt.ir_u.sp.ir_holemask = | |
217 | cpu_to_be16(cur->bc_rec.i.ir_holemask); | |
218 | rec->inobt.ir_u.sp.ir_count = cur->bc_rec.i.ir_count; | |
219 | rec->inobt.ir_u.sp.ir_freecount = cur->bc_rec.i.ir_freecount; | |
220 | } else { | |
221 | /* ir_holemask/ir_count not supported on-disk */ | |
222 | rec->inobt.ir_u.f.ir_freecount = | |
223 | cpu_to_be32(cur->bc_rec.i.ir_freecount); | |
224 | } | |
b194c7d8 | 225 | rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free); |
2bd0ea18 NS |
226 | } |
227 | ||
228 | /* | |
56b2de80 | 229 | * initial value of ptr for lookup |
2bd0ea18 | 230 | */ |
b194c7d8 BN |
231 | STATIC void |
232 | xfs_inobt_init_ptr_from_cur( | |
233 | struct xfs_btree_cur *cur, | |
234 | union xfs_btree_ptr *ptr) | |
2bd0ea18 | 235 | { |
b194c7d8 | 236 | struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp); |
2bd0ea18 | 237 | |
b194c7d8 | 238 | ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno)); |
2bd0ea18 | 239 | |
b194c7d8 | 240 | ptr->s = agi->agi_root; |
2bd0ea18 NS |
241 | } |
242 | ||
c0a4c227 BF |
243 | STATIC void |
244 | xfs_finobt_init_ptr_from_cur( | |
245 | struct xfs_btree_cur *cur, | |
246 | union xfs_btree_ptr *ptr) | |
247 | { | |
248 | struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp); | |
249 | ||
250 | ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno)); | |
251 | ptr->s = agi->agi_free_root; | |
252 | } | |
253 | ||
4a492e72 | 254 | STATIC int64_t |
b194c7d8 BN |
255 | xfs_inobt_key_diff( |
256 | struct xfs_btree_cur *cur, | |
257 | union xfs_btree_key *key) | |
258 | { | |
4a492e72 | 259 | return (int64_t)be32_to_cpu(key->inobt.ir_startino) - |
b194c7d8 BN |
260 | cur->bc_rec.i.ir_startino; |
261 | } | |
2bd0ea18 | 262 | |
4e388bc9 DW |
263 | STATIC int64_t |
264 | xfs_inobt_diff_two_keys( | |
265 | struct xfs_btree_cur *cur, | |
266 | union xfs_btree_key *k1, | |
267 | union xfs_btree_key *k2) | |
268 | { | |
269 | return (int64_t)be32_to_cpu(k1->inobt.ir_startino) - | |
270 | be32_to_cpu(k2->inobt.ir_startino); | |
271 | } | |
272 | ||
bc01119d | 273 | static xfs_failaddr_t |
a2ceac1f DC |
274 | xfs_inobt_verify( |
275 | struct xfs_buf *bp) | |
276 | { | |
277 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
278 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); | |
bc01119d | 279 | xfs_failaddr_t fa; |
a2ceac1f | 280 | unsigned int level; |
a2ceac1f | 281 | |
5dfa5cd2 DC |
282 | /* |
283 | * During growfs operations, we can't verify the exact owner as the | |
284 | * perag is not fully initialised and hence not attached to the buffer. | |
c0adfb03 DC |
285 | * |
286 | * Similarly, during log recovery we will have a perag structure | |
287 | * attached, but the agi information will not yet have been initialised | |
288 | * from the on disk AGI. We don't currently use any of this information, | |
289 | * but beware of the landmine (i.e. need to check pag->pagi_init) if we | |
290 | * ever do. | |
5dfa5cd2 | 291 | */ |
c0adfb03 DC |
292 | switch (block->bb_magic) { |
293 | case cpu_to_be32(XFS_IBT_CRC_MAGIC): | |
c0a4c227 | 294 | case cpu_to_be32(XFS_FIBT_CRC_MAGIC): |
bc01119d DW |
295 | fa = xfs_btree_sblock_v5hdr_verify(bp); |
296 | if (fa) | |
297 | return fa; | |
5dfa5cd2 | 298 | /* fall through */ |
c0adfb03 | 299 | case cpu_to_be32(XFS_IBT_MAGIC): |
c0a4c227 | 300 | case cpu_to_be32(XFS_FIBT_MAGIC): |
5dfa5cd2 DC |
301 | break; |
302 | default: | |
bc01119d | 303 | return NULL; |
5dfa5cd2 | 304 | } |
a2ceac1f | 305 | |
dbca0167 | 306 | /* level verification */ |
5dfa5cd2 DC |
307 | level = be16_to_cpu(block->bb_level); |
308 | if (level >= mp->m_in_maxlevels) | |
bc01119d | 309 | return __this_address; |
5dfa5cd2 | 310 | |
dbca0167 | 311 | return xfs_btree_sblock_verify(bp, mp->m_inobt_mxr[level != 0]); |
a2ceac1f DC |
312 | } |
313 | ||
314 | static void | |
315 | xfs_inobt_read_verify( | |
316 | struct xfs_buf *bp) | |
317 | { | |
1e697959 DW |
318 | xfs_failaddr_t fa; |
319 | ||
45922933 | 320 | if (!xfs_btree_sblock_verify_crc(bp)) |
1e697959 DW |
321 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
322 | else { | |
323 | fa = xfs_inobt_verify(bp); | |
324 | if (fa) | |
325 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
326 | } | |
45922933 | 327 | |
7e6c95f1 | 328 | if (bp->b_error) |
45922933 | 329 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
a2ceac1f DC |
330 | } |
331 | ||
332 | static void | |
333 | xfs_inobt_write_verify( | |
334 | struct xfs_buf *bp) | |
335 | { | |
1e697959 DW |
336 | xfs_failaddr_t fa; |
337 | ||
338 | fa = xfs_inobt_verify(bp); | |
339 | if (fa) { | |
5dfa5cd2 | 340 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
1e697959 | 341 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
dbf564d1 | 342 | return; |
5dfa5cd2 DC |
343 | } |
344 | xfs_btree_sblock_calc_crc(bp); | |
345 | ||
a2ceac1f DC |
346 | } |
347 | ||
348 | const struct xfs_buf_ops xfs_inobt_buf_ops = { | |
a3fac935 | 349 | .name = "xfs_inobt", |
a2ceac1f DC |
350 | .verify_read = xfs_inobt_read_verify, |
351 | .verify_write = xfs_inobt_write_verify, | |
95d9582b | 352 | .verify_struct = xfs_inobt_verify, |
a2ceac1f DC |
353 | }; |
354 | ||
b194c7d8 BN |
355 | STATIC int |
356 | xfs_inobt_keys_inorder( | |
357 | struct xfs_btree_cur *cur, | |
358 | union xfs_btree_key *k1, | |
359 | union xfs_btree_key *k2) | |
360 | { | |
361 | return be32_to_cpu(k1->inobt.ir_startino) < | |
362 | be32_to_cpu(k2->inobt.ir_startino); | |
2bd0ea18 NS |
363 | } |
364 | ||
b194c7d8 BN |
365 | STATIC int |
366 | xfs_inobt_recs_inorder( | |
367 | struct xfs_btree_cur *cur, | |
368 | union xfs_btree_rec *r1, | |
369 | union xfs_btree_rec *r2) | |
2bd0ea18 | 370 | { |
b194c7d8 BN |
371 | return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <= |
372 | be32_to_cpu(r2->inobt.ir_startino); | |
373 | } | |
2bd0ea18 | 374 | |
b194c7d8 BN |
375 | static const struct xfs_btree_ops xfs_inobt_ops = { |
376 | .rec_len = sizeof(xfs_inobt_rec_t), | |
377 | .key_len = sizeof(xfs_inobt_key_t), | |
378 | ||
379 | .dup_cursor = xfs_inobt_dup_cursor, | |
380 | .set_root = xfs_inobt_set_root, | |
b194c7d8 BN |
381 | .alloc_block = xfs_inobt_alloc_block, |
382 | .free_block = xfs_inobt_free_block, | |
383 | .get_minrecs = xfs_inobt_get_minrecs, | |
384 | .get_maxrecs = xfs_inobt_get_maxrecs, | |
385 | .init_key_from_rec = xfs_inobt_init_key_from_rec, | |
4e388bc9 | 386 | .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec, |
b194c7d8 BN |
387 | .init_rec_from_cur = xfs_inobt_init_rec_from_cur, |
388 | .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur, | |
389 | .key_diff = xfs_inobt_key_diff, | |
a2ceac1f | 390 | .buf_ops = &xfs_inobt_buf_ops, |
4e388bc9 | 391 | .diff_two_keys = xfs_inobt_diff_two_keys, |
b194c7d8 BN |
392 | .keys_inorder = xfs_inobt_keys_inorder, |
393 | .recs_inorder = xfs_inobt_recs_inorder, | |
b194c7d8 | 394 | }; |
2bd0ea18 | 395 | |
c0a4c227 BF |
396 | static const struct xfs_btree_ops xfs_finobt_ops = { |
397 | .rec_len = sizeof(xfs_inobt_rec_t), | |
398 | .key_len = sizeof(xfs_inobt_key_t), | |
399 | ||
400 | .dup_cursor = xfs_inobt_dup_cursor, | |
401 | .set_root = xfs_finobt_set_root, | |
74e502ac | 402 | .alloc_block = xfs_finobt_alloc_block, |
8504509d | 403 | .free_block = xfs_finobt_free_block, |
c0a4c227 BF |
404 | .get_minrecs = xfs_inobt_get_minrecs, |
405 | .get_maxrecs = xfs_inobt_get_maxrecs, | |
406 | .init_key_from_rec = xfs_inobt_init_key_from_rec, | |
4e388bc9 | 407 | .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec, |
c0a4c227 BF |
408 | .init_rec_from_cur = xfs_inobt_init_rec_from_cur, |
409 | .init_ptr_from_cur = xfs_finobt_init_ptr_from_cur, | |
410 | .key_diff = xfs_inobt_key_diff, | |
411 | .buf_ops = &xfs_inobt_buf_ops, | |
4e388bc9 | 412 | .diff_two_keys = xfs_inobt_diff_two_keys, |
c0a4c227 BF |
413 | .keys_inorder = xfs_inobt_keys_inorder, |
414 | .recs_inorder = xfs_inobt_recs_inorder, | |
c0a4c227 BF |
415 | }; |
416 | ||
2bd0ea18 | 417 | /* |
b194c7d8 | 418 | * Allocate a new inode btree cursor. |
2bd0ea18 | 419 | */ |
b194c7d8 BN |
420 | struct xfs_btree_cur * /* new inode btree cursor */ |
421 | xfs_inobt_init_cursor( | |
422 | struct xfs_mount *mp, /* file system mount point */ | |
423 | struct xfs_trans *tp, /* transaction pointer */ | |
424 | struct xfs_buf *agbp, /* buffer for agi structure */ | |
70eb7337 BF |
425 | xfs_agnumber_t agno, /* allocation group number */ |
426 | xfs_btnum_t btnum) /* ialloc or free ino btree */ | |
2bd0ea18 | 427 | { |
b194c7d8 BN |
428 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
429 | struct xfs_btree_cur *cur; | |
2bd0ea18 | 430 | |
762989ef | 431 | cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS); |
2bd0ea18 | 432 | |
b194c7d8 BN |
433 | cur->bc_tp = tp; |
434 | cur->bc_mp = mp; | |
70eb7337 | 435 | cur->bc_btnum = btnum; |
c0a4c227 BF |
436 | if (btnum == XFS_BTNUM_INO) { |
437 | cur->bc_nlevels = be32_to_cpu(agi->agi_level); | |
438 | cur->bc_ops = &xfs_inobt_ops; | |
5d8acc46 | 439 | cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_ibt_2); |
c0a4c227 BF |
440 | } else { |
441 | cur->bc_nlevels = be32_to_cpu(agi->agi_free_level); | |
442 | cur->bc_ops = &xfs_finobt_ops; | |
5d8acc46 | 443 | cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_fibt_2); |
c0a4c227 BF |
444 | } |
445 | ||
b194c7d8 BN |
446 | cur->bc_blocklog = mp->m_sb.sb_blocklog; |
447 | ||
5dfa5cd2 DC |
448 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
449 | cur->bc_flags |= XFS_BTREE_CRC_BLOCKS; | |
b194c7d8 BN |
450 | |
451 | cur->bc_private.a.agbp = agbp; | |
452 | cur->bc_private.a.agno = agno; | |
453 | ||
454 | return cur; | |
2bd0ea18 | 455 | } |
b3563c19 BN |
456 | |
457 | /* | |
458 | * Calculate number of records in an inobt btree block. | |
459 | */ | |
460 | int | |
461 | xfs_inobt_maxrecs( | |
462 | struct xfs_mount *mp, | |
463 | int blocklen, | |
464 | int leaf) | |
465 | { | |
466 | blocklen -= XFS_INOBT_BLOCK_LEN(mp); | |
467 | ||
468 | if (leaf) | |
469 | return blocklen / sizeof(xfs_inobt_rec_t); | |
470 | return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t)); | |
471 | } | |
2cf87710 BF |
472 | |
473 | /* | |
474 | * Convert the inode record holemask to an inode allocation bitmap. The inode | |
475 | * allocation bitmap is inode granularity and specifies whether an inode is | |
476 | * physically allocated on disk (not whether the inode is considered allocated | |
477 | * or free by the fs). | |
478 | * | |
479 | * A bit value of 1 means the inode is allocated, a value of 0 means it is free. | |
480 | */ | |
481 | uint64_t | |
482 | xfs_inobt_irec_to_allocmask( | |
483 | struct xfs_inobt_rec_incore *rec) | |
484 | { | |
485 | uint64_t bitmap = 0; | |
486 | uint64_t inodespbit; | |
487 | int nextbit; | |
488 | uint allocbitmap; | |
489 | ||
490 | /* | |
491 | * The holemask has 16-bits for a 64 inode record. Therefore each | |
492 | * holemask bit represents multiple inodes. Create a mask of bits to set | |
493 | * in the allocmask for each holemask bit. | |
494 | */ | |
495 | inodespbit = (1 << XFS_INODES_PER_HOLEMASK_BIT) - 1; | |
496 | ||
497 | /* | |
498 | * Allocated inodes are represented by 0 bits in holemask. Invert the 0 | |
499 | * bits to 1 and convert to a uint so we can use xfs_next_bit(). Mask | |
500 | * anything beyond the 16 holemask bits since this casts to a larger | |
501 | * type. | |
502 | */ | |
503 | allocbitmap = ~rec->ir_holemask & ((1 << XFS_INOBT_HOLEMASK_BITS) - 1); | |
504 | ||
505 | /* | |
506 | * allocbitmap is the inverted holemask so every set bit represents | |
507 | * allocated inodes. To expand from 16-bit holemask granularity to | |
508 | * 64-bit (e.g., bit-per-inode), set inodespbit bits in the target | |
509 | * bitmap for every holemask bit. | |
510 | */ | |
511 | nextbit = xfs_next_bit(&allocbitmap, 1, 0); | |
512 | while (nextbit != -1) { | |
513 | ASSERT(nextbit < (sizeof(rec->ir_holemask) * NBBY)); | |
514 | ||
515 | bitmap |= (inodespbit << | |
516 | (nextbit * XFS_INODES_PER_HOLEMASK_BIT)); | |
517 | ||
518 | nextbit = xfs_next_bit(&allocbitmap, 1, nextbit + 1); | |
519 | } | |
520 | ||
521 | return bitmap; | |
522 | } | |
6f4c54a4 BF |
523 | |
524 | #if defined(DEBUG) || defined(XFS_WARN) | |
525 | /* | |
526 | * Verify that an in-core inode record has a valid inode count. | |
527 | */ | |
528 | int | |
529 | xfs_inobt_rec_check_count( | |
530 | struct xfs_mount *mp, | |
531 | struct xfs_inobt_rec_incore *rec) | |
532 | { | |
533 | int inocount = 0; | |
534 | int nextbit = 0; | |
535 | uint64_t allocbmap; | |
536 | int wordsz; | |
537 | ||
538 | wordsz = sizeof(allocbmap) / sizeof(unsigned int); | |
539 | allocbmap = xfs_inobt_irec_to_allocmask(rec); | |
540 | ||
541 | nextbit = xfs_next_bit((uint *) &allocbmap, wordsz, nextbit); | |
542 | while (nextbit != -1) { | |
543 | inocount++; | |
544 | nextbit = xfs_next_bit((uint *) &allocbmap, wordsz, | |
545 | nextbit + 1); | |
546 | } | |
547 | ||
548 | if (inocount != rec->ir_count) | |
549 | return -EFSCORRUPTED; | |
550 | ||
551 | return 0; | |
552 | } | |
553 | #endif /* DEBUG */ | |
74e502ac CH |
554 | |
555 | static xfs_extlen_t | |
556 | xfs_inobt_max_size( | |
557 | struct xfs_mount *mp) | |
558 | { | |
559 | /* Bail out if we're uninitialized, which can happen in mkfs. */ | |
560 | if (mp->m_inobt_mxr[0] == 0) | |
561 | return 0; | |
562 | ||
563 | return xfs_btree_calc_size(mp, mp->m_inobt_mnr, | |
564 | (uint64_t)mp->m_sb.sb_agblocks * mp->m_sb.sb_inopblock / | |
565 | XFS_INODES_PER_CHUNK); | |
566 | } | |
567 | ||
568 | static int | |
569 | xfs_inobt_count_blocks( | |
570 | struct xfs_mount *mp, | |
571 | xfs_agnumber_t agno, | |
572 | xfs_btnum_t btnum, | |
573 | xfs_extlen_t *tree_blocks) | |
574 | { | |
575 | struct xfs_buf *agbp; | |
576 | struct xfs_btree_cur *cur; | |
577 | int error; | |
578 | ||
579 | error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp); | |
580 | if (error) | |
581 | return error; | |
582 | ||
583 | cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno, btnum); | |
584 | error = xfs_btree_count_blocks(cur, tree_blocks); | |
585 | xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | |
586 | xfs_buf_relse(agbp); | |
587 | ||
588 | return error; | |
589 | } | |
590 | ||
591 | /* | |
592 | * Figure out how many blocks to reserve and how many are used by this btree. | |
593 | */ | |
594 | int | |
595 | xfs_finobt_calc_reserves( | |
596 | struct xfs_mount *mp, | |
597 | xfs_agnumber_t agno, | |
598 | xfs_extlen_t *ask, | |
599 | xfs_extlen_t *used) | |
600 | { | |
601 | xfs_extlen_t tree_len = 0; | |
602 | int error; | |
603 | ||
604 | if (!xfs_sb_version_hasfinobt(&mp->m_sb)) | |
605 | return 0; | |
606 | ||
607 | error = xfs_inobt_count_blocks(mp, agno, XFS_BTNUM_FINO, &tree_len); | |
608 | if (error) | |
609 | return error; | |
610 | ||
611 | *ask += xfs_inobt_max_size(mp); | |
612 | *used += tree_len; | |
613 | return 0; | |
614 | } |