1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * metadata alloc and free
7 * Inspired by ext3 block groups.
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
38 #include "blockcheck.h"
42 #include "localalloc.h"
48 #include "buffer_head_io.h"
50 #define NOT_ALLOC_NEW_GROUP 0
51 #define ALLOC_NEW_GROUP 0x1
52 #define ALLOC_GROUPS_FROM_GLOBAL 0x2
54 #define OCFS2_MAX_TO_STEAL 1024
56 struct ocfs2_suballoc_result
{
57 u64 sr_bg_blkno
; /* The bg we allocated from. Set
58 to 0 when a block group is
60 u64 sr_blkno
; /* The first allocated block */
61 unsigned int sr_bit_offset
; /* The bit in the bg */
62 unsigned int sr_bits
; /* How many bits we claimed */
65 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
);
66 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
);
67 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
);
68 static int ocfs2_block_group_fill(handle_t
*handle
,
69 struct inode
*alloc_inode
,
70 struct buffer_head
*bg_bh
,
72 unsigned int group_clusters
,
74 struct ocfs2_chain_list
*cl
);
75 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
76 struct inode
*alloc_inode
,
77 struct buffer_head
*bh
,
79 u64
*last_alloc_group
,
82 static int ocfs2_cluster_group_search(struct inode
*inode
,
83 struct buffer_head
*group_bh
,
84 u32 bits_wanted
, u32 min_bits
,
86 struct ocfs2_suballoc_result
*res
);
87 static int ocfs2_block_group_search(struct inode
*inode
,
88 struct buffer_head
*group_bh
,
89 u32 bits_wanted
, u32 min_bits
,
91 struct ocfs2_suballoc_result
*res
);
92 static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context
*ac
,
96 struct ocfs2_suballoc_result
*res
);
97 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
99 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
100 struct inode
*alloc_inode
,
101 struct ocfs2_group_desc
*bg
,
102 struct buffer_head
*group_bh
,
103 unsigned int bit_off
,
104 unsigned int num_bits
);
105 static int ocfs2_relink_block_group(handle_t
*handle
,
106 struct inode
*alloc_inode
,
107 struct buffer_head
*fe_bh
,
108 struct buffer_head
*bg_bh
,
109 struct buffer_head
*prev_bg_bh
,
111 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
113 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
116 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
120 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super
*osb
,
121 u32 bits_wanted
, u64 max_block
,
123 struct ocfs2_alloc_context
**ac
);
125 void ocfs2_free_ac_resource(struct ocfs2_alloc_context
*ac
)
127 struct inode
*inode
= ac
->ac_inode
;
130 if (ac
->ac_which
!= OCFS2_AC_USE_LOCAL
)
131 ocfs2_inode_unlock(inode
, 1);
133 mutex_unlock(&inode
->i_mutex
);
143 void ocfs2_free_alloc_context(struct ocfs2_alloc_context
*ac
)
145 ocfs2_free_ac_resource(ac
);
149 static u32
ocfs2_bits_per_group(struct ocfs2_chain_list
*cl
)
151 return (u32
)le16_to_cpu(cl
->cl_cpg
) * (u32
)le16_to_cpu(cl
->cl_bpc
);
154 #define do_error(fmt, ...) \
157 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
159 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
162 static int ocfs2_validate_gd_self(struct super_block
*sb
,
163 struct buffer_head
*bh
,
166 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
168 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
169 do_error("Group descriptor #%llu has bad signature %.*s",
170 (unsigned long long)bh
->b_blocknr
, 7,
175 if (le64_to_cpu(gd
->bg_blkno
) != bh
->b_blocknr
) {
176 do_error("Group descriptor #%llu has an invalid bg_blkno "
178 (unsigned long long)bh
->b_blocknr
,
179 (unsigned long long)le64_to_cpu(gd
->bg_blkno
));
183 if (le32_to_cpu(gd
->bg_generation
) != OCFS2_SB(sb
)->fs_generation
) {
184 do_error("Group descriptor #%llu has an invalid "
185 "fs_generation of #%u",
186 (unsigned long long)bh
->b_blocknr
,
187 le32_to_cpu(gd
->bg_generation
));
191 if (le16_to_cpu(gd
->bg_free_bits_count
) > le16_to_cpu(gd
->bg_bits
)) {
192 do_error("Group descriptor #%llu has bit count %u but "
193 "claims that %u are free",
194 (unsigned long long)bh
->b_blocknr
,
195 le16_to_cpu(gd
->bg_bits
),
196 le16_to_cpu(gd
->bg_free_bits_count
));
200 if (le16_to_cpu(gd
->bg_bits
) > (8 * le16_to_cpu(gd
->bg_size
))) {
201 do_error("Group descriptor #%llu has bit count %u but "
202 "max bitmap bits of %u",
203 (unsigned long long)bh
->b_blocknr
,
204 le16_to_cpu(gd
->bg_bits
),
205 8 * le16_to_cpu(gd
->bg_size
));
212 static int ocfs2_validate_gd_parent(struct super_block
*sb
,
213 struct ocfs2_dinode
*di
,
214 struct buffer_head
*bh
,
217 unsigned int max_bits
;
218 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
220 if (di
->i_blkno
!= gd
->bg_parent_dinode
) {
221 do_error("Group descriptor #%llu has bad parent "
222 "pointer (%llu, expected %llu)",
223 (unsigned long long)bh
->b_blocknr
,
224 (unsigned long long)le64_to_cpu(gd
->bg_parent_dinode
),
225 (unsigned long long)le64_to_cpu(di
->i_blkno
));
229 max_bits
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
) * le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
230 if (le16_to_cpu(gd
->bg_bits
) > max_bits
) {
231 do_error("Group descriptor #%llu has bit count of %u",
232 (unsigned long long)bh
->b_blocknr
,
233 le16_to_cpu(gd
->bg_bits
));
237 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
238 if ((le16_to_cpu(gd
->bg_chain
) >
239 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) ||
240 ((le16_to_cpu(gd
->bg_chain
) ==
241 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) && !resize
)) {
242 do_error("Group descriptor #%llu has bad chain %u",
243 (unsigned long long)bh
->b_blocknr
,
244 le16_to_cpu(gd
->bg_chain
));
254 * This version only prints errors. It does not fail the filesystem, and
255 * exists only for resize.
257 int ocfs2_check_group_descriptor(struct super_block
*sb
,
258 struct ocfs2_dinode
*di
,
259 struct buffer_head
*bh
)
262 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
264 BUG_ON(!buffer_uptodate(bh
));
267 * If the ecc fails, we return the error but otherwise
268 * leave the filesystem running. We know any error is
269 * local to this block.
271 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &gd
->bg_check
);
274 "Checksum failed for group descriptor %llu\n",
275 (unsigned long long)bh
->b_blocknr
);
277 rc
= ocfs2_validate_gd_self(sb
, bh
, 1);
279 rc
= ocfs2_validate_gd_parent(sb
, di
, bh
, 1);
284 static int ocfs2_validate_group_descriptor(struct super_block
*sb
,
285 struct buffer_head
*bh
)
288 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
290 mlog(0, "Validating group descriptor %llu\n",
291 (unsigned long long)bh
->b_blocknr
);
293 BUG_ON(!buffer_uptodate(bh
));
296 * If the ecc fails, we return the error but otherwise
297 * leave the filesystem running. We know any error is
298 * local to this block.
300 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &gd
->bg_check
);
305 * Errors after here are fatal.
308 return ocfs2_validate_gd_self(sb
, bh
, 0);
311 int ocfs2_read_group_descriptor(struct inode
*inode
, struct ocfs2_dinode
*di
,
312 u64 gd_blkno
, struct buffer_head
**bh
)
315 struct buffer_head
*tmp
= *bh
;
317 rc
= ocfs2_read_block(INODE_CACHE(inode
), gd_blkno
, &tmp
,
318 ocfs2_validate_group_descriptor
);
322 rc
= ocfs2_validate_gd_parent(inode
->i_sb
, di
, tmp
, 0);
328 /* If ocfs2_read_block() got us a new bh, pass it up. */
336 static void ocfs2_bg_discontig_add_extent(struct ocfs2_super
*osb
,
337 struct ocfs2_group_desc
*bg
,
338 struct ocfs2_chain_list
*cl
,
339 u64 p_blkno
, u32 clusters
)
341 struct ocfs2_extent_list
*el
= &bg
->bg_list
;
342 struct ocfs2_extent_rec
*rec
;
344 BUG_ON(!ocfs2_supports_discontig_bg(osb
));
345 if (!el
->l_next_free_rec
)
346 el
->l_count
= cpu_to_le16(ocfs2_extent_recs_per_gd(osb
->sb
));
347 rec
= &el
->l_recs
[le16_to_cpu(el
->l_next_free_rec
)];
348 rec
->e_blkno
= cpu_to_le64(p_blkno
);
349 rec
->e_cpos
= cpu_to_le32(le16_to_cpu(bg
->bg_bits
) /
350 le16_to_cpu(cl
->cl_bpc
));
351 rec
->e_leaf_clusters
= cpu_to_le32(clusters
);
352 le16_add_cpu(&bg
->bg_bits
, clusters
* le16_to_cpu(cl
->cl_bpc
));
353 le16_add_cpu(&bg
->bg_free_bits_count
,
354 clusters
* le16_to_cpu(cl
->cl_bpc
));
355 le16_add_cpu(&el
->l_next_free_rec
, 1);
358 static int ocfs2_block_group_fill(handle_t
*handle
,
359 struct inode
*alloc_inode
,
360 struct buffer_head
*bg_bh
,
362 unsigned int group_clusters
,
364 struct ocfs2_chain_list
*cl
)
367 struct ocfs2_super
*osb
= OCFS2_SB(alloc_inode
->i_sb
);
368 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
369 struct super_block
* sb
= alloc_inode
->i_sb
;
373 if (((unsigned long long) bg_bh
->b_blocknr
) != group_blkno
) {
374 ocfs2_error(alloc_inode
->i_sb
, "group block (%llu) != "
376 (unsigned long long)group_blkno
,
377 (unsigned long long) bg_bh
->b_blocknr
);
382 status
= ocfs2_journal_access_gd(handle
,
383 INODE_CACHE(alloc_inode
),
385 OCFS2_JOURNAL_ACCESS_CREATE
);
391 memset(bg
, 0, sb
->s_blocksize
);
392 strcpy(bg
->bg_signature
, OCFS2_GROUP_DESC_SIGNATURE
);
393 bg
->bg_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
394 bg
->bg_size
= cpu_to_le16(ocfs2_group_bitmap_size(sb
, 1,
395 osb
->s_feature_incompat
));
396 bg
->bg_chain
= cpu_to_le16(my_chain
);
397 bg
->bg_next_group
= cl
->cl_recs
[my_chain
].c_blkno
;
398 bg
->bg_parent_dinode
= cpu_to_le64(OCFS2_I(alloc_inode
)->ip_blkno
);
399 bg
->bg_blkno
= cpu_to_le64(group_blkno
);
400 if (group_clusters
== le16_to_cpu(cl
->cl_cpg
))
401 bg
->bg_bits
= cpu_to_le16(ocfs2_bits_per_group(cl
));
403 ocfs2_bg_discontig_add_extent(osb
, bg
, cl
, group_blkno
,
406 /* set the 1st bit in the bitmap to account for the descriptor block */
407 ocfs2_set_bit(0, (unsigned long *)bg
->bg_bitmap
);
408 bg
->bg_free_bits_count
= cpu_to_le16(le16_to_cpu(bg
->bg_bits
) - 1);
410 ocfs2_journal_dirty(handle
, bg_bh
);
412 /* There is no need to zero out or otherwise initialize the
413 * other blocks in a group - All valid FS metadata in a block
414 * group stores the superblock fs_generation value at
415 * allocation time. */
422 static inline u16
ocfs2_find_smallest_chain(struct ocfs2_chain_list
*cl
)
427 while (curr
< le16_to_cpu(cl
->cl_count
)) {
428 if (le32_to_cpu(cl
->cl_recs
[best
].c_total
) >
429 le32_to_cpu(cl
->cl_recs
[curr
].c_total
))
436 static struct buffer_head
*
437 ocfs2_block_group_alloc_contig(struct ocfs2_super
*osb
, handle_t
*handle
,
438 struct inode
*alloc_inode
,
439 struct ocfs2_alloc_context
*ac
,
440 struct ocfs2_chain_list
*cl
)
443 u32 bit_off
, num_bits
;
445 struct buffer_head
*bg_bh
;
446 unsigned int alloc_rec
= ocfs2_find_smallest_chain(cl
);
448 status
= ocfs2_claim_clusters(handle
, ac
,
449 le16_to_cpu(cl
->cl_cpg
), &bit_off
,
452 if (status
!= -ENOSPC
)
457 /* setup the group */
458 bg_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
459 mlog(0, "new descriptor, record %u, at block %llu\n",
460 alloc_rec
, (unsigned long long)bg_blkno
);
462 bg_bh
= sb_getblk(osb
->sb
, bg_blkno
);
468 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode
), bg_bh
);
470 status
= ocfs2_block_group_fill(handle
, alloc_inode
, bg_bh
,
471 bg_blkno
, num_bits
, alloc_rec
, cl
);
478 return status
? ERR_PTR(status
) : bg_bh
;
481 static int ocfs2_block_group_claim_bits(struct ocfs2_super
*osb
,
483 struct ocfs2_alloc_context
*ac
,
484 unsigned int min_bits
,
485 u32
*bit_off
, u32
*num_bits
)
490 status
= ocfs2_claim_clusters(handle
, ac
, min_bits
,
492 if (status
!= -ENOSPC
)
501 static int ocfs2_block_group_grow_discontig(handle_t
*handle
,
502 struct inode
*alloc_inode
,
503 struct buffer_head
*bg_bh
,
504 struct ocfs2_alloc_context
*ac
,
505 struct ocfs2_chain_list
*cl
,
506 unsigned int min_bits
)
509 struct ocfs2_super
*osb
= OCFS2_SB(alloc_inode
->i_sb
);
510 struct ocfs2_group_desc
*bg
=
511 (struct ocfs2_group_desc
*)bg_bh
->b_data
;
512 unsigned int needed
= le16_to_cpu(cl
->cl_cpg
) -
513 le16_to_cpu(bg
->bg_bits
) / le16_to_cpu(cl
->cl_bpc
);
514 u32 p_cpos
, clusters
;
516 struct ocfs2_extent_list
*el
= &bg
->bg_list
;
518 status
= ocfs2_journal_access_gd(handle
,
519 INODE_CACHE(alloc_inode
),
521 OCFS2_JOURNAL_ACCESS_CREATE
);
527 while ((needed
> 0) && (le16_to_cpu(el
->l_next_free_rec
) <
528 le16_to_cpu(el
->l_count
))) {
529 if (min_bits
> needed
)
531 status
= ocfs2_block_group_claim_bits(osb
, handle
, ac
,
535 if (status
!= -ENOSPC
)
539 p_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, p_cpos
);
540 ocfs2_bg_discontig_add_extent(osb
, bg
, cl
, p_blkno
,
544 needed
= le16_to_cpu(cl
->cl_cpg
) -
545 le16_to_cpu(bg
->bg_bits
) / le16_to_cpu(cl
->cl_bpc
);
550 * We have used up all the extent rec but can't fill up
551 * the cpg. So bail out.
557 ocfs2_journal_dirty(handle
, bg_bh
);
563 static void ocfs2_bg_alloc_cleanup(handle_t
*handle
,
564 struct ocfs2_alloc_context
*cluster_ac
,
565 struct inode
*alloc_inode
,
566 struct buffer_head
*bg_bh
)
569 struct ocfs2_group_desc
*bg
;
570 struct ocfs2_extent_list
*el
;
571 struct ocfs2_extent_rec
*rec
;
576 bg
= (struct ocfs2_group_desc
*)bg_bh
->b_data
;
578 for (i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
579 rec
= &el
->l_recs
[i
];
580 ret
= ocfs2_free_clusters(handle
, cluster_ac
->ac_inode
,
582 le64_to_cpu(rec
->e_blkno
),
583 le32_to_cpu(rec
->e_leaf_clusters
));
586 /* Try all the clusters to free */
589 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode
), bg_bh
);
593 static struct buffer_head
*
594 ocfs2_block_group_alloc_discontig(handle_t
*handle
,
595 struct inode
*alloc_inode
,
596 struct ocfs2_alloc_context
*ac
,
597 struct ocfs2_chain_list
*cl
)
600 u32 bit_off
, num_bits
;
602 unsigned int min_bits
= le16_to_cpu(cl
->cl_cpg
) >> 1;
603 struct buffer_head
*bg_bh
= NULL
;
604 unsigned int alloc_rec
= ocfs2_find_smallest_chain(cl
);
605 struct ocfs2_super
*osb
= OCFS2_SB(alloc_inode
->i_sb
);
607 if (!ocfs2_supports_discontig_bg(osb
)) {
612 status
= ocfs2_extend_trans(handle
,
613 ocfs2_calc_bg_discontig_credits(osb
->sb
));
620 * We're going to be grabbing from multiple cluster groups.
621 * We don't have enough credits to relink them all, and the
622 * cluster groups will be staying in cache for the duration of
625 ac
->ac_allow_chain_relink
= 0;
627 /* Claim the first region */
628 status
= ocfs2_block_group_claim_bits(osb
, handle
, ac
, min_bits
,
629 &bit_off
, &num_bits
);
631 if (status
!= -ENOSPC
)
637 /* setup the group */
638 bg_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
639 mlog(0, "new descriptor, record %u, at block %llu\n",
640 alloc_rec
, (unsigned long long)bg_blkno
);
642 bg_bh
= sb_getblk(osb
->sb
, bg_blkno
);
648 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode
), bg_bh
);
650 status
= ocfs2_block_group_fill(handle
, alloc_inode
, bg_bh
,
651 bg_blkno
, num_bits
, alloc_rec
, cl
);
657 status
= ocfs2_block_group_grow_discontig(handle
, alloc_inode
,
658 bg_bh
, ac
, cl
, min_bits
);
664 ocfs2_bg_alloc_cleanup(handle
, ac
, alloc_inode
, bg_bh
);
665 return status
? ERR_PTR(status
) : bg_bh
;
669 * We expect the block group allocator to already be locked.
671 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
672 struct inode
*alloc_inode
,
673 struct buffer_head
*bh
,
675 u64
*last_alloc_group
,
679 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) bh
->b_data
;
680 struct ocfs2_chain_list
*cl
;
681 struct ocfs2_alloc_context
*ac
= NULL
;
682 handle_t
*handle
= NULL
;
684 struct buffer_head
*bg_bh
= NULL
;
685 struct ocfs2_group_desc
*bg
;
687 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode
));
691 cl
= &fe
->id2
.i_chain
;
692 status
= ocfs2_reserve_clusters_with_limit(osb
,
693 le16_to_cpu(cl
->cl_cpg
),
694 max_block
, flags
, &ac
);
696 if (status
!= -ENOSPC
)
701 credits
= ocfs2_calc_group_alloc_credits(osb
->sb
,
702 le16_to_cpu(cl
->cl_cpg
));
703 handle
= ocfs2_start_trans(osb
, credits
);
704 if (IS_ERR(handle
)) {
705 status
= PTR_ERR(handle
);
711 if (last_alloc_group
&& *last_alloc_group
!= 0) {
712 mlog(0, "use old allocation group %llu for block group alloc\n",
713 (unsigned long long)*last_alloc_group
);
714 ac
->ac_last_group
= *last_alloc_group
;
717 bg_bh
= ocfs2_block_group_alloc_contig(osb
, handle
, alloc_inode
,
719 if (IS_ERR(bg_bh
) && (PTR_ERR(bg_bh
) == -ENOSPC
))
720 bg_bh
= ocfs2_block_group_alloc_discontig(handle
,
724 status
= PTR_ERR(bg_bh
);
726 if (status
!= -ENOSPC
)
730 bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
732 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(alloc_inode
),
733 bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
739 alloc_rec
= le16_to_cpu(bg
->bg_chain
);
740 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_free
,
741 le16_to_cpu(bg
->bg_free_bits_count
));
742 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_total
,
743 le16_to_cpu(bg
->bg_bits
));
744 cl
->cl_recs
[alloc_rec
].c_blkno
= bg
->bg_blkno
;
745 if (le16_to_cpu(cl
->cl_next_free_rec
) < le16_to_cpu(cl
->cl_count
))
746 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
748 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, le16_to_cpu(bg
->bg_bits
) -
749 le16_to_cpu(bg
->bg_free_bits_count
));
750 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, le16_to_cpu(bg
->bg_bits
));
751 le32_add_cpu(&fe
->i_clusters
, le16_to_cpu(cl
->cl_cpg
));
753 ocfs2_journal_dirty(handle
, bh
);
755 spin_lock(&OCFS2_I(alloc_inode
)->ip_lock
);
756 OCFS2_I(alloc_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
757 fe
->i_size
= cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode
->i_sb
,
758 le32_to_cpu(fe
->i_clusters
)));
759 spin_unlock(&OCFS2_I(alloc_inode
)->ip_lock
);
760 i_size_write(alloc_inode
, le64_to_cpu(fe
->i_size
));
761 alloc_inode
->i_blocks
= ocfs2_inode_sector_count(alloc_inode
);
765 /* save the new last alloc group so that the caller can cache it. */
766 if (last_alloc_group
)
767 *last_alloc_group
= ac
->ac_last_group
;
771 ocfs2_commit_trans(osb
, handle
);
774 ocfs2_free_alloc_context(ac
);
782 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super
*osb
,
783 struct ocfs2_alloc_context
*ac
,
786 u64
*last_alloc_group
,
790 u32 bits_wanted
= ac
->ac_bits_wanted
;
791 struct inode
*alloc_inode
;
792 struct buffer_head
*bh
= NULL
;
793 struct ocfs2_dinode
*fe
;
798 alloc_inode
= ocfs2_get_system_file_inode(osb
, type
, slot
);
804 mutex_lock(&alloc_inode
->i_mutex
);
806 status
= ocfs2_inode_lock(alloc_inode
, &bh
, 1);
808 mutex_unlock(&alloc_inode
->i_mutex
);
815 ac
->ac_inode
= alloc_inode
;
816 ac
->ac_alloc_slot
= slot
;
818 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
820 /* The bh was validated by the inode read inside
821 * ocfs2_inode_lock(). Any corruption is a code bug. */
822 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
824 if (!(fe
->i_flags
& cpu_to_le32(OCFS2_CHAIN_FL
))) {
825 ocfs2_error(alloc_inode
->i_sb
, "Invalid chain allocator %llu",
826 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
831 free_bits
= le32_to_cpu(fe
->id1
.bitmap1
.i_total
) -
832 le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
834 if (bits_wanted
> free_bits
) {
835 /* cluster bitmap never grows */
836 if (ocfs2_is_cluster_bitmap(alloc_inode
)) {
837 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
838 bits_wanted
, free_bits
);
843 if (!(flags
& ALLOC_NEW_GROUP
)) {
844 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
845 "and we don't alloc a new group for it.\n",
846 slot
, bits_wanted
, free_bits
);
851 status
= ocfs2_block_group_alloc(osb
, alloc_inode
, bh
,
853 last_alloc_group
, flags
);
855 if (status
!= -ENOSPC
)
859 atomic_inc(&osb
->alloc_stats
.bg_extends
);
861 /* You should never ask for this much metadata */
863 (le32_to_cpu(fe
->id1
.bitmap1
.i_total
)
864 - le32_to_cpu(fe
->id1
.bitmap1
.i_used
)));
876 static void ocfs2_init_inode_steal_slot(struct ocfs2_super
*osb
)
878 spin_lock(&osb
->osb_lock
);
879 osb
->s_inode_steal_slot
= OCFS2_INVALID_SLOT
;
880 spin_unlock(&osb
->osb_lock
);
881 atomic_set(&osb
->s_num_inodes_stolen
, 0);
884 static void ocfs2_init_meta_steal_slot(struct ocfs2_super
*osb
)
886 spin_lock(&osb
->osb_lock
);
887 osb
->s_meta_steal_slot
= OCFS2_INVALID_SLOT
;
888 spin_unlock(&osb
->osb_lock
);
889 atomic_set(&osb
->s_num_meta_stolen
, 0);
892 void ocfs2_init_steal_slots(struct ocfs2_super
*osb
)
894 ocfs2_init_inode_steal_slot(osb
);
895 ocfs2_init_meta_steal_slot(osb
);
898 static void __ocfs2_set_steal_slot(struct ocfs2_super
*osb
, int slot
, int type
)
900 spin_lock(&osb
->osb_lock
);
901 if (type
== INODE_ALLOC_SYSTEM_INODE
)
902 osb
->s_inode_steal_slot
= slot
;
903 else if (type
== EXTENT_ALLOC_SYSTEM_INODE
)
904 osb
->s_meta_steal_slot
= slot
;
905 spin_unlock(&osb
->osb_lock
);
908 static int __ocfs2_get_steal_slot(struct ocfs2_super
*osb
, int type
)
910 int slot
= OCFS2_INVALID_SLOT
;
912 spin_lock(&osb
->osb_lock
);
913 if (type
== INODE_ALLOC_SYSTEM_INODE
)
914 slot
= osb
->s_inode_steal_slot
;
915 else if (type
== EXTENT_ALLOC_SYSTEM_INODE
)
916 slot
= osb
->s_meta_steal_slot
;
917 spin_unlock(&osb
->osb_lock
);
922 static int ocfs2_get_inode_steal_slot(struct ocfs2_super
*osb
)
924 return __ocfs2_get_steal_slot(osb
, INODE_ALLOC_SYSTEM_INODE
);
927 static int ocfs2_get_meta_steal_slot(struct ocfs2_super
*osb
)
929 return __ocfs2_get_steal_slot(osb
, EXTENT_ALLOC_SYSTEM_INODE
);
932 static int ocfs2_steal_resource(struct ocfs2_super
*osb
,
933 struct ocfs2_alloc_context
*ac
,
936 int i
, status
= -ENOSPC
;
937 int slot
= __ocfs2_get_steal_slot(osb
, type
);
939 /* Start to steal resource from the first slot after ours. */
940 if (slot
== OCFS2_INVALID_SLOT
)
941 slot
= osb
->slot_num
+ 1;
943 for (i
= 0; i
< osb
->max_slots
; i
++, slot
++) {
944 if (slot
== osb
->max_slots
)
947 if (slot
== osb
->slot_num
)
950 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
953 NOT_ALLOC_NEW_GROUP
);
955 __ocfs2_set_steal_slot(osb
, slot
, type
);
959 ocfs2_free_ac_resource(ac
);
965 static int ocfs2_steal_inode(struct ocfs2_super
*osb
,
966 struct ocfs2_alloc_context
*ac
)
968 return ocfs2_steal_resource(osb
, ac
, INODE_ALLOC_SYSTEM_INODE
);
971 static int ocfs2_steal_meta(struct ocfs2_super
*osb
,
972 struct ocfs2_alloc_context
*ac
)
974 return ocfs2_steal_resource(osb
, ac
, EXTENT_ALLOC_SYSTEM_INODE
);
977 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super
*osb
,
979 struct ocfs2_alloc_context
**ac
)
982 int slot
= ocfs2_get_meta_steal_slot(osb
);
984 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
991 (*ac
)->ac_bits_wanted
= blocks
;
992 (*ac
)->ac_which
= OCFS2_AC_USE_META
;
993 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
995 if (slot
!= OCFS2_INVALID_SLOT
&&
996 atomic_read(&osb
->s_num_meta_stolen
) < OCFS2_MAX_TO_STEAL
)
999 atomic_set(&osb
->s_num_meta_stolen
, 0);
1000 status
= ocfs2_reserve_suballoc_bits(osb
, (*ac
),
1001 EXTENT_ALLOC_SYSTEM_INODE
,
1002 (u32
)osb
->slot_num
, NULL
,
1003 ALLOC_GROUPS_FROM_GLOBAL
|ALLOC_NEW_GROUP
);
1008 if (slot
!= OCFS2_INVALID_SLOT
)
1009 ocfs2_init_meta_steal_slot(osb
);
1011 } else if (status
< 0 && status
!= -ENOSPC
) {
1016 ocfs2_free_ac_resource(*ac
);
1019 status
= ocfs2_steal_meta(osb
, *ac
);
1020 atomic_inc(&osb
->s_num_meta_stolen
);
1022 if (status
!= -ENOSPC
)
1029 if ((status
< 0) && *ac
) {
1030 ocfs2_free_alloc_context(*ac
);
1038 int ocfs2_reserve_new_metadata(struct ocfs2_super
*osb
,
1039 struct ocfs2_extent_list
*root_el
,
1040 struct ocfs2_alloc_context
**ac
)
1042 return ocfs2_reserve_new_metadata_blocks(osb
,
1043 ocfs2_extend_meta_needed(root_el
),
1047 int ocfs2_reserve_new_inode(struct ocfs2_super
*osb
,
1048 struct ocfs2_alloc_context
**ac
)
1051 int slot
= ocfs2_get_inode_steal_slot(osb
);
1054 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
1061 (*ac
)->ac_bits_wanted
= 1;
1062 (*ac
)->ac_which
= OCFS2_AC_USE_INODE
;
1064 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
1067 * stat(2) can't handle i_ino > 32bits, so we tell the
1068 * lower levels not to allocate us a block group past that
1069 * limit. The 'inode64' mount option avoids this behavior.
1071 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_INODE64
))
1072 (*ac
)->ac_max_block
= (u32
)~0U;
1075 * slot is set when we successfully steal inode from other nodes.
1076 * It is reset in 3 places:
1077 * 1. when we flush the truncate log
1078 * 2. when we complete local alloc recovery.
1079 * 3. when we successfully allocate from our own slot.
1080 * After it is set, we will go on stealing inodes until we find the
1081 * need to check our slots to see whether there is some space for us.
1083 if (slot
!= OCFS2_INVALID_SLOT
&&
1084 atomic_read(&osb
->s_num_inodes_stolen
) < OCFS2_MAX_TO_STEAL
)
1087 atomic_set(&osb
->s_num_inodes_stolen
, 0);
1088 alloc_group
= osb
->osb_inode_alloc_group
;
1089 status
= ocfs2_reserve_suballoc_bits(osb
, *ac
,
1090 INODE_ALLOC_SYSTEM_INODE
,
1094 ALLOC_GROUPS_FROM_GLOBAL
);
1098 spin_lock(&osb
->osb_lock
);
1099 osb
->osb_inode_alloc_group
= alloc_group
;
1100 spin_unlock(&osb
->osb_lock
);
1101 mlog(0, "after reservation, new allocation group is "
1102 "%llu\n", (unsigned long long)alloc_group
);
1105 * Some inodes must be freed by us, so try to allocate
1106 * from our own next time.
1108 if (slot
!= OCFS2_INVALID_SLOT
)
1109 ocfs2_init_inode_steal_slot(osb
);
1111 } else if (status
< 0 && status
!= -ENOSPC
) {
1116 ocfs2_free_ac_resource(*ac
);
1119 status
= ocfs2_steal_inode(osb
, *ac
);
1120 atomic_inc(&osb
->s_num_inodes_stolen
);
1122 if (status
!= -ENOSPC
)
1129 if ((status
< 0) && *ac
) {
1130 ocfs2_free_alloc_context(*ac
);
1138 /* local alloc code has to do the same thing, so rather than do this
1140 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super
*osb
,
1141 struct ocfs2_alloc_context
*ac
)
1145 ac
->ac_which
= OCFS2_AC_USE_MAIN
;
1146 ac
->ac_group_search
= ocfs2_cluster_group_search
;
1148 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
1149 GLOBAL_BITMAP_SYSTEM_INODE
,
1150 OCFS2_INVALID_SLOT
, NULL
,
1152 if (status
< 0 && status
!= -ENOSPC
) {
1161 /* Callers don't need to care which bitmap (local alloc or main) to
1162 * use so we figure it out for them, but unfortunately this clutters
1164 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super
*osb
,
1165 u32 bits_wanted
, u64 max_block
,
1167 struct ocfs2_alloc_context
**ac
)
1173 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
1180 (*ac
)->ac_bits_wanted
= bits_wanted
;
1181 (*ac
)->ac_max_block
= max_block
;
1184 if (!(flags
& ALLOC_GROUPS_FROM_GLOBAL
) &&
1185 ocfs2_alloc_should_use_local(osb
, bits_wanted
)) {
1186 status
= ocfs2_reserve_local_alloc_bits(osb
,
1189 if ((status
< 0) && (status
!= -ENOSPC
)) {
1195 if (status
== -ENOSPC
) {
1196 status
= ocfs2_reserve_cluster_bitmap_bits(osb
, *ac
);
1198 if (status
!= -ENOSPC
)
1206 if ((status
< 0) && *ac
) {
1207 ocfs2_free_alloc_context(*ac
);
1215 int ocfs2_reserve_clusters(struct ocfs2_super
*osb
,
1217 struct ocfs2_alloc_context
**ac
)
1219 return ocfs2_reserve_clusters_with_limit(osb
, bits_wanted
, 0,
1220 ALLOC_NEW_GROUP
, ac
);
1224 * More or less lifted from ext3. I'll leave their description below:
1226 * "For ext3 allocations, we must not reuse any blocks which are
1227 * allocated in the bitmap buffer's "last committed data" copy. This
1228 * prevents deletes from freeing up the page for reuse until we have
1229 * committed the delete transaction.
1231 * If we didn't do this, then deleting something and reallocating it as
1232 * data would allow the old block to be overwritten before the
1233 * transaction committed (because we force data to disk before commit).
1234 * This would lead to corruption if we crashed between overwriting the
1235 * data and committing the delete.
1237 * @@@ We may want to make this allocation behaviour conditional on
1238 * data-writes at some point, and disable it for metadata allocations or
1239 * sync-data inodes."
1241 * Note: OCFS2 already does this differently for metadata vs data
1242 * allocations, as those bitmaps are separate and undo access is never
1243 * called on a metadata group descriptor.
1245 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
1248 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
1251 if (ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
))
1254 if (!buffer_jbd(bg_bh
))
1257 jbd_lock_bh_state(bg_bh
);
1258 bg
= (struct ocfs2_group_desc
*) bh2jh(bg_bh
)->b_committed_data
;
1260 ret
= !ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
);
1263 jbd_unlock_bh_state(bg_bh
);
1268 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super
*osb
,
1269 struct buffer_head
*bg_bh
,
1270 unsigned int bits_wanted
,
1271 unsigned int total_bits
,
1272 struct ocfs2_suballoc_result
*res
)
1275 u16 best_offset
, best_size
;
1276 int offset
, start
, found
, status
= 0;
1277 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
1279 /* Callers got this descriptor from
1280 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1281 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1283 found
= start
= best_offset
= best_size
= 0;
1284 bitmap
= bg
->bg_bitmap
;
1286 while((offset
= ocfs2_find_next_zero_bit(bitmap
, total_bits
, start
)) != -1) {
1287 if (offset
== total_bits
)
1290 if (!ocfs2_test_bg_bit_allocatable(bg_bh
, offset
)) {
1291 /* We found a zero, but we can't use it as it
1292 * hasn't been put to disk yet! */
1295 } else if (offset
== start
) {
1296 /* we found a zero */
1298 /* move start to the next bit to test */
1301 /* got a zero after some ones */
1305 if (found
> best_size
) {
1307 best_offset
= start
- found
;
1309 /* we got everything we needed */
1310 if (found
== bits_wanted
) {
1311 /* mlog(0, "Found it all!\n"); */
1317 res
->sr_bit_offset
= best_offset
;
1318 res
->sr_bits
= best_size
;
1321 /* No error log here -- see the comment above
1322 * ocfs2_test_bg_bit_allocatable */
1328 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
1329 struct inode
*alloc_inode
,
1330 struct ocfs2_group_desc
*bg
,
1331 struct buffer_head
*group_bh
,
1332 unsigned int bit_off
,
1333 unsigned int num_bits
)
1336 void *bitmap
= bg
->bg_bitmap
;
1337 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1341 /* All callers get the descriptor via
1342 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1343 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1344 BUG_ON(le16_to_cpu(bg
->bg_free_bits_count
) < num_bits
);
1346 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off
,
1349 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1350 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1352 status
= ocfs2_journal_access_gd(handle
,
1353 INODE_CACHE(alloc_inode
),
1361 le16_add_cpu(&bg
->bg_free_bits_count
, -num_bits
);
1363 ocfs2_set_bit(bit_off
++, bitmap
);
1365 ocfs2_journal_dirty(handle
, group_bh
);
1372 /* find the one with the most empty bits */
1373 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
)
1377 BUG_ON(!cl
->cl_next_free_rec
);
1380 while (curr
< le16_to_cpu(cl
->cl_next_free_rec
)) {
1381 if (le32_to_cpu(cl
->cl_recs
[curr
].c_free
) >
1382 le32_to_cpu(cl
->cl_recs
[best
].c_free
))
1387 BUG_ON(best
>= le16_to_cpu(cl
->cl_next_free_rec
));
1391 static int ocfs2_relink_block_group(handle_t
*handle
,
1392 struct inode
*alloc_inode
,
1393 struct buffer_head
*fe_bh
,
1394 struct buffer_head
*bg_bh
,
1395 struct buffer_head
*prev_bg_bh
,
1399 /* there is a really tiny chance the journal calls could fail,
1400 * but we wouldn't want inconsistent blocks in *any* case. */
1401 u64 fe_ptr
, bg_ptr
, prev_bg_ptr
;
1402 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
1403 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
1404 struct ocfs2_group_desc
*prev_bg
= (struct ocfs2_group_desc
*) prev_bg_bh
->b_data
;
1406 /* The caller got these descriptors from
1407 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1408 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1409 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg
));
1411 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
1412 (unsigned long long)le64_to_cpu(fe
->i_blkno
), chain
,
1413 (unsigned long long)le64_to_cpu(bg
->bg_blkno
),
1414 (unsigned long long)le64_to_cpu(prev_bg
->bg_blkno
));
1416 fe_ptr
= le64_to_cpu(fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
);
1417 bg_ptr
= le64_to_cpu(bg
->bg_next_group
);
1418 prev_bg_ptr
= le64_to_cpu(prev_bg
->bg_next_group
);
1420 status
= ocfs2_journal_access_gd(handle
, INODE_CACHE(alloc_inode
),
1422 OCFS2_JOURNAL_ACCESS_WRITE
);
1428 prev_bg
->bg_next_group
= bg
->bg_next_group
;
1429 ocfs2_journal_dirty(handle
, prev_bg_bh
);
1431 status
= ocfs2_journal_access_gd(handle
, INODE_CACHE(alloc_inode
),
1432 bg_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
1438 bg
->bg_next_group
= fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
;
1439 ocfs2_journal_dirty(handle
, bg_bh
);
1441 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(alloc_inode
),
1442 fe_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
1448 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= bg
->bg_blkno
;
1449 ocfs2_journal_dirty(handle
, fe_bh
);
1453 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= cpu_to_le64(fe_ptr
);
1454 bg
->bg_next_group
= cpu_to_le64(bg_ptr
);
1455 prev_bg
->bg_next_group
= cpu_to_le64(prev_bg_ptr
);
1462 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
1465 return le16_to_cpu(bg
->bg_free_bits_count
) > wanted
;
1468 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1469 * value on error. */
1470 static int ocfs2_cluster_group_search(struct inode
*inode
,
1471 struct buffer_head
*group_bh
,
1472 u32 bits_wanted
, u32 min_bits
,
1474 struct ocfs2_suballoc_result
*res
)
1476 int search
= -ENOSPC
;
1479 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1480 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1481 unsigned int max_bits
, gd_cluster_off
;
1483 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1485 if (gd
->bg_free_bits_count
) {
1486 max_bits
= le16_to_cpu(gd
->bg_bits
);
1488 /* Tail groups in cluster bitmaps which aren't cpg
1489 * aligned are prone to partial extention by a failed
1490 * fs resize. If the file system resize never got to
1491 * update the dinode cluster count, then we don't want
1492 * to trust any clusters past it, regardless of what
1493 * the group descriptor says. */
1494 gd_cluster_off
= ocfs2_blocks_to_clusters(inode
->i_sb
,
1495 le64_to_cpu(gd
->bg_blkno
));
1496 if ((gd_cluster_off
+ max_bits
) >
1497 OCFS2_I(inode
)->ip_clusters
) {
1498 max_bits
= OCFS2_I(inode
)->ip_clusters
- gd_cluster_off
;
1499 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1500 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
1501 le16_to_cpu(gd
->bg_bits
),
1502 OCFS2_I(inode
)->ip_clusters
, max_bits
);
1505 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1506 group_bh
, bits_wanted
,
1512 blkoff
= ocfs2_clusters_to_blocks(inode
->i_sb
,
1514 res
->sr_bit_offset
+
1516 mlog(0, "Checking %llu against %llu\n",
1517 (unsigned long long)blkoff
,
1518 (unsigned long long)max_block
);
1519 if (blkoff
> max_block
)
1523 /* ocfs2_block_group_find_clear_bits() might
1524 * return success, but we still want to return
1525 * -ENOSPC unless it found the minimum number
1527 if (min_bits
<= res
->sr_bits
)
1528 search
= 0; /* success */
1529 else if (res
->sr_bits
) {
1531 * Don't show bits which we'll be returning
1532 * for allocation to the local alloc bitmap.
1534 ocfs2_local_alloc_seen_free_bits(osb
, res
->sr_bits
);
1541 static int ocfs2_block_group_search(struct inode
*inode
,
1542 struct buffer_head
*group_bh
,
1543 u32 bits_wanted
, u32 min_bits
,
1545 struct ocfs2_suballoc_result
*res
)
1549 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1551 BUG_ON(min_bits
!= 1);
1552 BUG_ON(ocfs2_is_cluster_bitmap(inode
));
1554 if (bg
->bg_free_bits_count
) {
1555 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1556 group_bh
, bits_wanted
,
1557 le16_to_cpu(bg
->bg_bits
),
1559 if (!ret
&& max_block
) {
1560 blkoff
= le64_to_cpu(bg
->bg_blkno
) +
1561 res
->sr_bit_offset
+ res
->sr_bits
;
1562 mlog(0, "Checking %llu against %llu\n",
1563 (unsigned long long)blkoff
,
1564 (unsigned long long)max_block
);
1565 if (blkoff
> max_block
)
1573 static int ocfs2_alloc_dinode_update_counts(struct inode
*inode
,
1575 struct buffer_head
*di_bh
,
1581 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
1582 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &di
->id2
.i_chain
;
1584 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
1585 OCFS2_JOURNAL_ACCESS_WRITE
);
1591 tmp_used
= le32_to_cpu(di
->id1
.bitmap1
.i_used
);
1592 di
->id1
.bitmap1
.i_used
= cpu_to_le32(num_bits
+ tmp_used
);
1593 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -num_bits
);
1594 ocfs2_journal_dirty(handle
, di_bh
);
1600 static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result
*res
,
1601 struct ocfs2_extent_rec
*rec
,
1602 struct ocfs2_chain_list
*cl
)
1604 unsigned int bpc
= le16_to_cpu(cl
->cl_bpc
);
1605 unsigned int bitoff
= le32_to_cpu(rec
->e_cpos
) * bpc
;
1606 unsigned int bitcount
= le32_to_cpu(rec
->e_leaf_clusters
) * bpc
;
1608 if (res
->sr_bit_offset
< bitoff
)
1610 if (res
->sr_bit_offset
>= (bitoff
+ bitcount
))
1612 res
->sr_blkno
= le64_to_cpu(rec
->e_blkno
) +
1613 (res
->sr_bit_offset
- bitoff
);
1614 if ((res
->sr_bit_offset
+ res
->sr_bits
) > (bitoff
+ bitcount
))
1615 res
->sr_bits
= (bitoff
+ bitcount
) - res
->sr_bit_offset
;
1619 static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context
*ac
,
1620 struct ocfs2_group_desc
*bg
,
1621 struct ocfs2_suballoc_result
*res
)
1624 u64 bg_blkno
= res
->sr_bg_blkno
; /* Save off */
1625 struct ocfs2_extent_rec
*rec
;
1626 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)ac
->ac_bh
->b_data
;
1627 struct ocfs2_chain_list
*cl
= &di
->id2
.i_chain
;
1629 if (ocfs2_is_cluster_bitmap(ac
->ac_inode
)) {
1634 res
->sr_blkno
= res
->sr_bg_blkno
+ res
->sr_bit_offset
;
1635 res
->sr_bg_blkno
= 0; /* Clear it for contig block groups */
1636 if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac
->ac_inode
->i_sb
)) ||
1637 !bg
->bg_list
.l_next_free_rec
)
1640 for (i
= 0; i
< le16_to_cpu(bg
->bg_list
.l_next_free_rec
); i
++) {
1641 rec
= &bg
->bg_list
.l_recs
[i
];
1642 if (ocfs2_bg_discontig_fix_by_rec(res
, rec
, cl
)) {
1643 res
->sr_bg_blkno
= bg_blkno
; /* Restore */
1649 static int ocfs2_search_one_group(struct ocfs2_alloc_context
*ac
,
1653 struct ocfs2_suballoc_result
*res
,
1657 struct buffer_head
*group_bh
= NULL
;
1658 struct ocfs2_group_desc
*gd
;
1659 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)ac
->ac_bh
->b_data
;
1660 struct inode
*alloc_inode
= ac
->ac_inode
;
1662 ret
= ocfs2_read_group_descriptor(alloc_inode
, di
,
1663 res
->sr_bg_blkno
, &group_bh
);
1669 gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1670 ret
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
, min_bits
,
1671 ac
->ac_max_block
, res
);
1679 ocfs2_bg_discontig_fix_result(ac
, gd
, res
);
1681 ret
= ocfs2_alloc_dinode_update_counts(alloc_inode
, handle
, ac
->ac_bh
,
1683 le16_to_cpu(gd
->bg_chain
));
1689 ret
= ocfs2_block_group_set_bits(handle
, alloc_inode
, gd
, group_bh
,
1690 res
->sr_bit_offset
, res
->sr_bits
);
1694 *bits_left
= le16_to_cpu(gd
->bg_free_bits_count
);
1702 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
1706 struct ocfs2_suballoc_result
*res
,
1713 struct inode
*alloc_inode
= ac
->ac_inode
;
1714 struct buffer_head
*group_bh
= NULL
;
1715 struct buffer_head
*prev_group_bh
= NULL
;
1716 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1717 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1718 struct ocfs2_group_desc
*bg
;
1720 chain
= ac
->ac_chain
;
1721 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1723 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
);
1725 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
,
1726 le64_to_cpu(cl
->cl_recs
[chain
].c_blkno
),
1732 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1735 /* for now, the chain search is a bit simplistic. We just use
1736 * the 1st group with any empty bits. */
1737 while ((status
= ac
->ac_group_search(alloc_inode
, group_bh
,
1738 bits_wanted
, min_bits
,
1741 if (!bg
->bg_next_group
)
1744 brelse(prev_group_bh
);
1745 prev_group_bh
= NULL
;
1747 next_group
= le64_to_cpu(bg
->bg_next_group
);
1748 prev_group_bh
= group_bh
;
1750 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
,
1751 next_group
, &group_bh
);
1756 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1759 if (status
!= -ENOSPC
)
1764 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1765 res
->sr_bits
, (unsigned long long)le64_to_cpu(bg
->bg_blkno
));
1767 res
->sr_bg_blkno
= le64_to_cpu(bg
->bg_blkno
);
1769 BUG_ON(res
->sr_bits
== 0);
1771 ocfs2_bg_discontig_fix_result(ac
, bg
, res
);
1775 * Keep track of previous block descriptor read. When
1776 * we find a target, if we have read more than X
1777 * number of descriptors, and the target is reasonably
1778 * empty, relink him to top of his chain.
1780 * We've read 0 extra blocks and only send one more to
1781 * the transaction, yet the next guy to search has a
1784 * Do this *after* figuring out how many bits we're taking out
1785 * of our target group.
1787 if (ac
->ac_allow_chain_relink
&&
1789 (ocfs2_block_group_reasonably_empty(bg
, res
->sr_bits
))) {
1790 status
= ocfs2_relink_block_group(handle
, alloc_inode
,
1791 ac
->ac_bh
, group_bh
,
1792 prev_group_bh
, chain
);
1799 /* Ok, claim our bits now: set the info on dinode, chainlist
1800 * and then the group */
1801 status
= ocfs2_journal_access_di(handle
,
1802 INODE_CACHE(alloc_inode
),
1804 OCFS2_JOURNAL_ACCESS_WRITE
);
1810 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1811 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(res
->sr_bits
+ tmp_used
);
1812 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -res
->sr_bits
);
1813 ocfs2_journal_dirty(handle
, ac
->ac_bh
);
1815 status
= ocfs2_block_group_set_bits(handle
,
1826 mlog(0, "Allocated %u bits from suballocator %llu\n", res
->sr_bits
,
1827 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
1829 *bits_left
= le16_to_cpu(bg
->bg_free_bits_count
);
1832 brelse(prev_group_bh
);
1838 /* will give out up to bits_wanted contiguous bits. */
1839 static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context
*ac
,
1843 struct ocfs2_suballoc_result
*res
)
1848 struct ocfs2_chain_list
*cl
;
1849 struct ocfs2_dinode
*fe
;
1853 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1854 BUG_ON(bits_wanted
> (ac
->ac_bits_wanted
- ac
->ac_bits_given
));
1857 fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1859 /* The bh was validated by the inode read during
1860 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1861 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
1863 if (le32_to_cpu(fe
->id1
.bitmap1
.i_used
) >=
1864 le32_to_cpu(fe
->id1
.bitmap1
.i_total
)) {
1865 ocfs2_error(ac
->ac_inode
->i_sb
,
1866 "Chain allocator dinode %llu has %u used "
1867 "bits but only %u total.",
1868 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
1869 le32_to_cpu(fe
->id1
.bitmap1
.i_used
),
1870 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1875 res
->sr_bg_blkno
= ac
->ac_last_group
;
1876 if (res
->sr_bg_blkno
) {
1877 /* Attempt to short-circuit the usual search mechanism
1878 * by jumping straight to the most recently used
1879 * allocation group. This helps us mantain some
1880 * contiguousness across allocations. */
1881 status
= ocfs2_search_one_group(ac
, handle
, bits_wanted
,
1882 min_bits
, res
, &bits_left
);
1885 if (status
< 0 && status
!= -ENOSPC
) {
1891 cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1893 victim
= ocfs2_find_victim_chain(cl
);
1894 ac
->ac_chain
= victim
;
1895 ac
->ac_allow_chain_relink
= 1;
1897 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
,
1901 if (status
< 0 && status
!= -ENOSPC
) {
1906 mlog(0, "Search of victim chain %u came up with nothing, "
1907 "trying all chains now.\n", victim
);
1909 /* If we didn't pick a good victim, then just default to
1910 * searching each chain in order. Don't allow chain relinking
1911 * because we only calculate enough journal credits for one
1912 * relink per alloc. */
1913 ac
->ac_allow_chain_relink
= 0;
1914 for (i
= 0; i
< le16_to_cpu(cl
->cl_next_free_rec
); i
++) {
1917 if (!cl
->cl_recs
[i
].c_free
)
1921 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
,
1925 if (status
< 0 && status
!= -ENOSPC
) {
1932 if (status
!= -ENOSPC
) {
1933 /* If the next search of this group is not likely to
1934 * yield a suitable extent, then we reset the last
1935 * group hint so as to not waste a disk read */
1936 if (bits_left
< min_bits
)
1937 ac
->ac_last_group
= 0;
1939 ac
->ac_last_group
= res
->sr_bg_blkno
;
1947 int ocfs2_claim_metadata(handle_t
*handle
,
1948 struct ocfs2_alloc_context
*ac
,
1951 u16
*suballoc_bit_start
,
1952 unsigned int *num_bits
,
1956 struct ocfs2_suballoc_result res
= { .sr_blkno
= 0, };
1959 BUG_ON(ac
->ac_bits_wanted
< (ac
->ac_bits_given
+ bits_wanted
));
1960 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_META
);
1962 status
= ocfs2_claim_suballoc_bits(ac
,
1971 atomic_inc(&OCFS2_SB(ac
->ac_inode
->i_sb
)->alloc_stats
.bg_allocs
);
1973 *suballoc_loc
= res
.sr_bg_blkno
;
1974 *suballoc_bit_start
= res
.sr_bit_offset
;
1975 *blkno_start
= res
.sr_blkno
;
1976 ac
->ac_bits_given
+= res
.sr_bits
;
1977 *num_bits
= res
.sr_bits
;
1984 static void ocfs2_init_inode_ac_group(struct inode
*dir
,
1985 struct buffer_head
*parent_di_bh
,
1986 struct ocfs2_alloc_context
*ac
)
1988 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)parent_di_bh
->b_data
;
1990 * Try to allocate inodes from some specific group.
1992 * If the parent dir has recorded the last group used in allocation,
1993 * cool, use it. Otherwise if we try to allocate new inode from the
1994 * same slot the parent dir belongs to, use the same chunk.
1996 * We are very careful here to avoid the mistake of setting
1997 * ac_last_group to a group descriptor from a different (unlocked) slot.
1999 if (OCFS2_I(dir
)->ip_last_used_group
&&
2000 OCFS2_I(dir
)->ip_last_used_slot
== ac
->ac_alloc_slot
)
2001 ac
->ac_last_group
= OCFS2_I(dir
)->ip_last_used_group
;
2002 else if (le16_to_cpu(di
->i_suballoc_slot
) == ac
->ac_alloc_slot
) {
2003 if (di
->i_suballoc_loc
)
2004 ac
->ac_last_group
= le64_to_cpu(di
->i_suballoc_loc
);
2006 ac
->ac_last_group
= ocfs2_which_suballoc_group(
2007 le64_to_cpu(di
->i_blkno
),
2008 le16_to_cpu(di
->i_suballoc_bit
));
2012 static inline void ocfs2_save_inode_ac_group(struct inode
*dir
,
2013 struct ocfs2_alloc_context
*ac
)
2015 OCFS2_I(dir
)->ip_last_used_group
= ac
->ac_last_group
;
2016 OCFS2_I(dir
)->ip_last_used_slot
= ac
->ac_alloc_slot
;
2019 int ocfs2_claim_new_inode(handle_t
*handle
,
2021 struct buffer_head
*parent_fe_bh
,
2022 struct ocfs2_alloc_context
*ac
,
2028 struct ocfs2_suballoc_result res
;
2033 BUG_ON(ac
->ac_bits_given
!= 0);
2034 BUG_ON(ac
->ac_bits_wanted
!= 1);
2035 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_INODE
);
2037 ocfs2_init_inode_ac_group(dir
, parent_fe_bh
, ac
);
2039 status
= ocfs2_claim_suballoc_bits(ac
,
2048 atomic_inc(&OCFS2_SB(ac
->ac_inode
->i_sb
)->alloc_stats
.bg_allocs
);
2050 BUG_ON(res
.sr_bits
!= 1);
2052 *suballoc_loc
= res
.sr_bg_blkno
;
2053 *suballoc_bit
= res
.sr_bit_offset
;
2054 *fe_blkno
= res
.sr_blkno
;
2055 ac
->ac_bits_given
++;
2056 ocfs2_save_inode_ac_group(dir
, ac
);
2063 /* translate a group desc. blkno and it's bitmap offset into
2064 * disk cluster offset. */
2065 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
2069 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2072 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
2074 if (bg_blkno
!= osb
->first_cluster_group_blkno
)
2075 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, bg_blkno
);
2076 cluster
+= (u32
) bg_bit_off
;
2080 /* given a cluster offset, calculate which block group it belongs to
2081 * and return that block offset. */
2082 u64
ocfs2_which_cluster_group(struct inode
*inode
, u32 cluster
)
2084 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2087 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
2089 group_no
= cluster
/ osb
->bitmap_cpg
;
2091 return osb
->first_cluster_group_blkno
;
2092 return ocfs2_clusters_to_blocks(inode
->i_sb
,
2093 group_no
* osb
->bitmap_cpg
);
2096 /* given the block number of a cluster start, calculate which cluster
2097 * group and descriptor bitmap offset that corresponds to. */
2098 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
2103 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2104 u32 data_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, data_blkno
);
2106 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
2108 *bg_blkno
= ocfs2_which_cluster_group(inode
,
2111 if (*bg_blkno
== osb
->first_cluster_group_blkno
)
2112 *bg_bit_off
= (u16
) data_cluster
;
2114 *bg_bit_off
= (u16
) ocfs2_blocks_to_clusters(osb
->sb
,
2115 data_blkno
- *bg_blkno
);
2119 * min_bits - minimum contiguous chunk from this total allocation we
2120 * can handle. set to what we asked for originally for a full
2121 * contig. allocation, set to '1' to indicate we can deal with extents
2124 int __ocfs2_claim_clusters(handle_t
*handle
,
2125 struct ocfs2_alloc_context
*ac
,
2132 unsigned int bits_wanted
= max_clusters
;
2133 struct ocfs2_suballoc_result res
= { .sr_blkno
= 0, };
2134 struct ocfs2_super
*osb
= OCFS2_SB(ac
->ac_inode
->i_sb
);
2138 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
2140 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_LOCAL
2141 && ac
->ac_which
!= OCFS2_AC_USE_MAIN
);
2143 if (ac
->ac_which
== OCFS2_AC_USE_LOCAL
) {
2144 WARN_ON(min_clusters
> 1);
2146 status
= ocfs2_claim_local_alloc_bits(osb
,
2153 atomic_inc(&osb
->alloc_stats
.local_data
);
2155 if (min_clusters
> (osb
->bitmap_cpg
- 1)) {
2156 /* The only paths asking for contiguousness
2157 * should know about this already. */
2158 mlog(ML_ERROR
, "minimum allocation requested %u exceeds "
2159 "group bitmap size %u!\n", min_clusters
,
2164 /* clamp the current request down to a realistic size. */
2165 if (bits_wanted
> (osb
->bitmap_cpg
- 1))
2166 bits_wanted
= osb
->bitmap_cpg
- 1;
2168 status
= ocfs2_claim_suballoc_bits(ac
,
2174 BUG_ON(res
.sr_blkno
); /* cluster alloc can't set */
2176 ocfs2_desc_bitmap_to_cluster_off(ac
->ac_inode
,
2179 atomic_inc(&osb
->alloc_stats
.bitmap_data
);
2180 *num_clusters
= res
.sr_bits
;
2184 if (status
!= -ENOSPC
)
2189 ac
->ac_bits_given
+= *num_clusters
;
2196 int ocfs2_claim_clusters(handle_t
*handle
,
2197 struct ocfs2_alloc_context
*ac
,
2202 unsigned int bits_wanted
= ac
->ac_bits_wanted
- ac
->ac_bits_given
;
2204 return __ocfs2_claim_clusters(handle
, ac
, min_clusters
,
2205 bits_wanted
, cluster_start
, num_clusters
);
2208 static int ocfs2_block_group_clear_bits(handle_t
*handle
,
2209 struct inode
*alloc_inode
,
2210 struct ocfs2_group_desc
*bg
,
2211 struct buffer_head
*group_bh
,
2212 unsigned int bit_off
,
2213 unsigned int num_bits
,
2214 void (*undo_fn
)(unsigned int bit
,
2215 unsigned long *bmap
))
2219 struct ocfs2_group_desc
*undo_bg
= NULL
;
2223 /* The caller got this descriptor from
2224 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2225 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
2227 mlog(0, "off = %u, num = %u\n", bit_off
, num_bits
);
2229 BUG_ON(undo_fn
&& !ocfs2_is_cluster_bitmap(alloc_inode
));
2230 status
= ocfs2_journal_access_gd(handle
, INODE_CACHE(alloc_inode
),
2233 OCFS2_JOURNAL_ACCESS_UNDO
:
2234 OCFS2_JOURNAL_ACCESS_WRITE
);
2241 jbd_lock_bh_state(group_bh
);
2242 undo_bg
= (struct ocfs2_group_desc
*)
2243 bh2jh(group_bh
)->b_committed_data
;
2249 ocfs2_clear_bit((bit_off
+ tmp
),
2250 (unsigned long *) bg
->bg_bitmap
);
2252 undo_fn(bit_off
+ tmp
,
2253 (unsigned long *) undo_bg
->bg_bitmap
);
2255 le16_add_cpu(&bg
->bg_free_bits_count
, num_bits
);
2258 jbd_unlock_bh_state(group_bh
);
2260 ocfs2_journal_dirty(handle
, group_bh
);
2266 * expects the suballoc inode to already be locked.
2268 static int _ocfs2_free_suballoc_bits(handle_t
*handle
,
2269 struct inode
*alloc_inode
,
2270 struct buffer_head
*alloc_bh
,
2271 unsigned int start_bit
,
2274 void (*undo_fn
)(unsigned int bit
,
2275 unsigned long *bitmap
))
2279 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) alloc_bh
->b_data
;
2280 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
2281 struct buffer_head
*group_bh
= NULL
;
2282 struct ocfs2_group_desc
*group
;
2286 /* The alloc_bh comes from ocfs2_free_dinode() or
2287 * ocfs2_free_clusters(). The callers have all locked the
2288 * allocator and gotten alloc_bh from the lock call. This
2289 * validates the dinode buffer. Any corruption that has happended
2291 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
2292 BUG_ON((count
+ start_bit
) > ocfs2_bits_per_group(cl
));
2294 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2295 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
, count
,
2296 (unsigned long long)bg_blkno
, start_bit
);
2298 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
, bg_blkno
,
2304 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
2306 BUG_ON((count
+ start_bit
) > le16_to_cpu(group
->bg_bits
));
2308 status
= ocfs2_block_group_clear_bits(handle
, alloc_inode
,
2310 start_bit
, count
, undo_fn
);
2316 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(alloc_inode
),
2317 alloc_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
2323 le32_add_cpu(&cl
->cl_recs
[le16_to_cpu(group
->bg_chain
)].c_free
,
2325 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
2326 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(tmp_used
- count
);
2327 ocfs2_journal_dirty(handle
, alloc_bh
);
2336 int ocfs2_free_suballoc_bits(handle_t
*handle
,
2337 struct inode
*alloc_inode
,
2338 struct buffer_head
*alloc_bh
,
2339 unsigned int start_bit
,
2343 return _ocfs2_free_suballoc_bits(handle
, alloc_inode
, alloc_bh
,
2344 start_bit
, bg_blkno
, count
, NULL
);
2347 int ocfs2_free_dinode(handle_t
*handle
,
2348 struct inode
*inode_alloc_inode
,
2349 struct buffer_head
*inode_alloc_bh
,
2350 struct ocfs2_dinode
*di
)
2352 u64 blk
= le64_to_cpu(di
->i_blkno
);
2353 u16 bit
= le16_to_cpu(di
->i_suballoc_bit
);
2354 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
2356 if (di
->i_suballoc_loc
)
2357 bg_blkno
= le64_to_cpu(di
->i_suballoc_loc
);
2358 return ocfs2_free_suballoc_bits(handle
, inode_alloc_inode
,
2359 inode_alloc_bh
, bit
, bg_blkno
, 1);
2362 static int _ocfs2_free_clusters(handle_t
*handle
,
2363 struct inode
*bitmap_inode
,
2364 struct buffer_head
*bitmap_bh
,
2366 unsigned int num_clusters
,
2367 void (*undo_fn
)(unsigned int bit
,
2368 unsigned long *bitmap
))
2373 struct ocfs2_dinode
*fe
;
2375 /* You can't ever have a contiguous set of clusters
2376 * bigger than a block group bitmap so we never have to worry
2377 * about looping on them. */
2381 /* This is expensive. We can safely remove once this stuff has
2382 * gotten tested really well. */
2383 BUG_ON(start_blk
!= ocfs2_clusters_to_blocks(bitmap_inode
->i_sb
, ocfs2_blocks_to_clusters(bitmap_inode
->i_sb
, start_blk
)));
2385 fe
= (struct ocfs2_dinode
*) bitmap_bh
->b_data
;
2387 ocfs2_block_to_cluster_group(bitmap_inode
, start_blk
, &bg_blkno
,
2390 mlog(0, "want to free %u clusters starting at block %llu\n",
2391 num_clusters
, (unsigned long long)start_blk
);
2392 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2393 (unsigned long long)bg_blkno
, bg_start_bit
);
2395 status
= _ocfs2_free_suballoc_bits(handle
, bitmap_inode
, bitmap_bh
,
2396 bg_start_bit
, bg_blkno
,
2397 num_clusters
, undo_fn
);
2403 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode
->i_sb
),
2411 int ocfs2_free_clusters(handle_t
*handle
,
2412 struct inode
*bitmap_inode
,
2413 struct buffer_head
*bitmap_bh
,
2415 unsigned int num_clusters
)
2417 return _ocfs2_free_clusters(handle
, bitmap_inode
, bitmap_bh
,
2418 start_blk
, num_clusters
,
2423 * Give never-used clusters back to the global bitmap. We don't need
2424 * to protect these bits in the undo buffer.
2426 int ocfs2_release_clusters(handle_t
*handle
,
2427 struct inode
*bitmap_inode
,
2428 struct buffer_head
*bitmap_bh
,
2430 unsigned int num_clusters
)
2432 return _ocfs2_free_clusters(handle
, bitmap_inode
, bitmap_bh
,
2433 start_blk
, num_clusters
,
2437 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
)
2439 printk("Block Group:\n");
2440 printk("bg_signature: %s\n", bg
->bg_signature
);
2441 printk("bg_size: %u\n", bg
->bg_size
);
2442 printk("bg_bits: %u\n", bg
->bg_bits
);
2443 printk("bg_free_bits_count: %u\n", bg
->bg_free_bits_count
);
2444 printk("bg_chain: %u\n", bg
->bg_chain
);
2445 printk("bg_generation: %u\n", le32_to_cpu(bg
->bg_generation
));
2446 printk("bg_next_group: %llu\n",
2447 (unsigned long long)bg
->bg_next_group
);
2448 printk("bg_parent_dinode: %llu\n",
2449 (unsigned long long)bg
->bg_parent_dinode
);
2450 printk("bg_blkno: %llu\n",
2451 (unsigned long long)bg
->bg_blkno
);
2454 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
)
2458 printk("Suballoc Inode %llu:\n", (unsigned long long)fe
->i_blkno
);
2459 printk("i_signature: %s\n", fe
->i_signature
);
2460 printk("i_size: %llu\n",
2461 (unsigned long long)fe
->i_size
);
2462 printk("i_clusters: %u\n", fe
->i_clusters
);
2463 printk("i_generation: %u\n",
2464 le32_to_cpu(fe
->i_generation
));
2465 printk("id1.bitmap1.i_used: %u\n",
2466 le32_to_cpu(fe
->id1
.bitmap1
.i_used
));
2467 printk("id1.bitmap1.i_total: %u\n",
2468 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
2469 printk("id2.i_chain.cl_cpg: %u\n", fe
->id2
.i_chain
.cl_cpg
);
2470 printk("id2.i_chain.cl_bpc: %u\n", fe
->id2
.i_chain
.cl_bpc
);
2471 printk("id2.i_chain.cl_count: %u\n", fe
->id2
.i_chain
.cl_count
);
2472 printk("id2.i_chain.cl_next_free_rec: %u\n",
2473 fe
->id2
.i_chain
.cl_next_free_rec
);
2474 for(i
= 0; i
< fe
->id2
.i_chain
.cl_next_free_rec
; i
++) {
2475 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i
,
2476 fe
->id2
.i_chain
.cl_recs
[i
].c_free
);
2477 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i
,
2478 fe
->id2
.i_chain
.cl_recs
[i
].c_total
);
2479 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i
,
2480 (unsigned long long)fe
->id2
.i_chain
.cl_recs
[i
].c_blkno
);
2485 * For a given allocation, determine which allocators will need to be
2486 * accessed, and lock them, reserving the appropriate number of bits.
2488 * Sparse file systems call this from ocfs2_write_begin_nolock()
2489 * and ocfs2_allocate_unwritten_extents().
2491 * File systems which don't support holes call this from
2492 * ocfs2_extend_allocation().
2494 int ocfs2_lock_allocators(struct inode
*inode
,
2495 struct ocfs2_extent_tree
*et
,
2496 u32 clusters_to_add
, u32 extents_to_split
,
2497 struct ocfs2_alloc_context
**data_ac
,
2498 struct ocfs2_alloc_context
**meta_ac
)
2500 int ret
= 0, num_free_extents
;
2501 unsigned int max_recs_needed
= clusters_to_add
+ 2 * extents_to_split
;
2502 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2508 BUG_ON(clusters_to_add
!= 0 && data_ac
== NULL
);
2510 num_free_extents
= ocfs2_num_free_extents(osb
, et
);
2511 if (num_free_extents
< 0) {
2512 ret
= num_free_extents
;
2518 * Sparse allocation file systems need to be more conservative
2519 * with reserving room for expansion - the actual allocation
2520 * happens while we've got a journal handle open so re-taking
2521 * a cluster lock (because we ran out of room for another
2522 * extent) will violate ordering rules.
2524 * Most of the time we'll only be seeing this 1 cluster at a time
2527 * Always lock for any unwritten extents - we might want to
2528 * add blocks during a split.
2530 if (!num_free_extents
||
2531 (ocfs2_sparse_alloc(osb
) && num_free_extents
< max_recs_needed
)) {
2532 ret
= ocfs2_reserve_new_metadata(osb
, et
->et_root_el
, meta_ac
);
2540 if (clusters_to_add
== 0)
2543 ret
= ocfs2_reserve_clusters(osb
, clusters_to_add
, data_ac
);
2553 ocfs2_free_alloc_context(*meta_ac
);
2558 * We cannot have an error and a non null *data_ac.
2566 * Read the inode specified by blkno to get suballoc_slot and
2569 static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super
*osb
, u64 blkno
,
2570 u16
*suballoc_slot
, u16
*suballoc_bit
)
2573 struct buffer_head
*inode_bh
= NULL
;
2574 struct ocfs2_dinode
*inode_fe
;
2576 mlog_entry("blkno: %llu\n", (unsigned long long)blkno
);
2578 /* dirty read disk */
2579 status
= ocfs2_read_blocks_sync(osb
, blkno
, 1, &inode_bh
);
2581 mlog(ML_ERROR
, "read block %llu failed %d\n",
2582 (unsigned long long)blkno
, status
);
2586 inode_fe
= (struct ocfs2_dinode
*) inode_bh
->b_data
;
2587 if (!OCFS2_IS_VALID_DINODE(inode_fe
)) {
2588 mlog(ML_ERROR
, "invalid inode %llu requested\n",
2589 (unsigned long long)blkno
);
2594 if (le16_to_cpu(inode_fe
->i_suballoc_slot
) != (u16
)OCFS2_INVALID_SLOT
&&
2595 (u32
)le16_to_cpu(inode_fe
->i_suballoc_slot
) > osb
->max_slots
- 1) {
2596 mlog(ML_ERROR
, "inode %llu has invalid suballoc slot %u\n",
2597 (unsigned long long)blkno
,
2598 (u32
)le16_to_cpu(inode_fe
->i_suballoc_slot
));
2604 *suballoc_slot
= le16_to_cpu(inode_fe
->i_suballoc_slot
);
2606 *suballoc_bit
= le16_to_cpu(inode_fe
->i_suballoc_bit
);
2616 * test whether bit is SET in allocator bitmap or not. on success, 0
2617 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2618 * is returned and *res is meaningless. Call this after you have
2619 * cluster locked against suballoc, or you may get a result based on
2620 * non-up2date contents
2622 static int ocfs2_test_suballoc_bit(struct ocfs2_super
*osb
,
2623 struct inode
*suballoc
,
2624 struct buffer_head
*alloc_bh
, u64 blkno
,
2627 struct ocfs2_dinode
*alloc_di
;
2628 struct ocfs2_group_desc
*group
;
2629 struct buffer_head
*group_bh
= NULL
;
2633 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno
,
2636 alloc_di
= (struct ocfs2_dinode
*)alloc_bh
->b_data
;
2637 if ((bit
+ 1) > ocfs2_bits_per_group(&alloc_di
->id2
.i_chain
)) {
2638 mlog(ML_ERROR
, "suballoc bit %u out of range of %u\n",
2640 ocfs2_bits_per_group(&alloc_di
->id2
.i_chain
));
2645 if (alloc_di
->i_suballoc_loc
)
2646 bg_blkno
= le64_to_cpu(alloc_di
->i_suballoc_loc
);
2648 bg_blkno
= ocfs2_which_suballoc_group(blkno
, bit
);
2649 status
= ocfs2_read_group_descriptor(suballoc
, alloc_di
, bg_blkno
,
2652 mlog(ML_ERROR
, "read group %llu failed %d\n",
2653 (unsigned long long)bg_blkno
, status
);
2657 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
2658 *res
= ocfs2_test_bit(bit
, (unsigned long *)group
->bg_bitmap
);
2668 * Test if the bit representing this inode (blkno) is set in the
2671 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2673 * In the event of failure, a negative value is returned and *res is
2676 * Callers must make sure to hold nfs_sync_lock to prevent
2677 * ocfs2_delete_inode() on another node from accessing the same
2678 * suballocator concurrently.
2680 int ocfs2_test_inode_bit(struct ocfs2_super
*osb
, u64 blkno
, int *res
)
2683 u16 suballoc_bit
= 0, suballoc_slot
= 0;
2684 struct inode
*inode_alloc_inode
;
2685 struct buffer_head
*alloc_bh
= NULL
;
2687 mlog_entry("blkno: %llu", (unsigned long long)blkno
);
2689 status
= ocfs2_get_suballoc_slot_bit(osb
, blkno
, &suballoc_slot
,
2692 mlog(ML_ERROR
, "get alloc slot and bit failed %d\n", status
);
2697 ocfs2_get_system_file_inode(osb
, INODE_ALLOC_SYSTEM_INODE
,
2699 if (!inode_alloc_inode
) {
2700 /* the error code could be inaccurate, but we are not able to
2701 * get the correct one. */
2703 mlog(ML_ERROR
, "unable to get alloc inode in slot %u\n",
2704 (u32
)suballoc_slot
);
2708 mutex_lock(&inode_alloc_inode
->i_mutex
);
2709 status
= ocfs2_inode_lock(inode_alloc_inode
, &alloc_bh
, 0);
2711 mutex_unlock(&inode_alloc_inode
->i_mutex
);
2712 mlog(ML_ERROR
, "lock on alloc inode on slot %u failed %d\n",
2713 (u32
)suballoc_slot
, status
);
2717 status
= ocfs2_test_suballoc_bit(osb
, inode_alloc_inode
, alloc_bh
,
2718 blkno
, suballoc_bit
, res
);
2720 mlog(ML_ERROR
, "test suballoc bit failed %d\n", status
);
2722 ocfs2_inode_unlock(inode_alloc_inode
, 0);
2723 mutex_unlock(&inode_alloc_inode
->i_mutex
);
2725 iput(inode_alloc_inode
);