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>
41 #include "localalloc.h"
47 #include "buffer_head_io.h"
49 #define NOT_ALLOC_NEW_GROUP 0
50 #define ALLOC_NEW_GROUP 1
52 #define OCFS2_MAX_INODES_TO_STEAL 1024
54 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
);
55 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
);
56 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
);
57 static int ocfs2_block_group_fill(handle_t
*handle
,
58 struct inode
*alloc_inode
,
59 struct buffer_head
*bg_bh
,
62 struct ocfs2_chain_list
*cl
);
63 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
64 struct inode
*alloc_inode
,
65 struct buffer_head
*bh
);
67 static int ocfs2_cluster_group_search(struct inode
*inode
,
68 struct buffer_head
*group_bh
,
69 u32 bits_wanted
, u32 min_bits
,
70 u16
*bit_off
, u16
*bits_found
);
71 static int ocfs2_block_group_search(struct inode
*inode
,
72 struct buffer_head
*group_bh
,
73 u32 bits_wanted
, u32 min_bits
,
74 u16
*bit_off
, u16
*bits_found
);
75 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
76 struct ocfs2_alloc_context
*ac
,
81 unsigned int *num_bits
,
83 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
85 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
86 struct inode
*alloc_inode
,
87 struct ocfs2_group_desc
*bg
,
88 struct buffer_head
*group_bh
,
90 unsigned int num_bits
);
91 static inline int ocfs2_block_group_clear_bits(handle_t
*handle
,
92 struct inode
*alloc_inode
,
93 struct ocfs2_group_desc
*bg
,
94 struct buffer_head
*group_bh
,
96 unsigned int num_bits
);
98 static int ocfs2_relink_block_group(handle_t
*handle
,
99 struct inode
*alloc_inode
,
100 struct buffer_head
*fe_bh
,
101 struct buffer_head
*bg_bh
,
102 struct buffer_head
*prev_bg_bh
,
104 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
106 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
109 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
114 void ocfs2_free_ac_resource(struct ocfs2_alloc_context
*ac
)
116 struct inode
*inode
= ac
->ac_inode
;
119 if (ac
->ac_which
!= OCFS2_AC_USE_LOCAL
)
120 ocfs2_inode_unlock(inode
, 1);
122 mutex_unlock(&inode
->i_mutex
);
133 void ocfs2_free_alloc_context(struct ocfs2_alloc_context
*ac
)
135 ocfs2_free_ac_resource(ac
);
139 static u32
ocfs2_bits_per_group(struct ocfs2_chain_list
*cl
)
141 return (u32
)le16_to_cpu(cl
->cl_cpg
) * (u32
)le16_to_cpu(cl
->cl_bpc
);
144 /* somewhat more expensive than our other checks, so use sparingly. */
145 int ocfs2_check_group_descriptor(struct super_block
*sb
,
146 struct ocfs2_dinode
*di
,
147 struct ocfs2_group_desc
*gd
)
149 unsigned int max_bits
;
151 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
152 OCFS2_RO_ON_INVALID_GROUP_DESC(sb
, gd
);
156 if (di
->i_blkno
!= gd
->bg_parent_dinode
) {
157 ocfs2_error(sb
, "Group descriptor # %llu has bad parent "
158 "pointer (%llu, expected %llu)",
159 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
160 (unsigned long long)le64_to_cpu(gd
->bg_parent_dinode
),
161 (unsigned long long)le64_to_cpu(di
->i_blkno
));
165 max_bits
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
) * le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
166 if (le16_to_cpu(gd
->bg_bits
) > max_bits
) {
167 ocfs2_error(sb
, "Group descriptor # %llu has bit count of %u",
168 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
169 le16_to_cpu(gd
->bg_bits
));
173 if (le16_to_cpu(gd
->bg_chain
) >=
174 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) {
175 ocfs2_error(sb
, "Group descriptor # %llu has bad chain %u",
176 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
177 le16_to_cpu(gd
->bg_chain
));
181 if (le16_to_cpu(gd
->bg_free_bits_count
) > le16_to_cpu(gd
->bg_bits
)) {
182 ocfs2_error(sb
, "Group descriptor # %llu has bit count %u but "
183 "claims that %u are free",
184 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
185 le16_to_cpu(gd
->bg_bits
),
186 le16_to_cpu(gd
->bg_free_bits_count
));
190 if (le16_to_cpu(gd
->bg_bits
) > (8 * le16_to_cpu(gd
->bg_size
))) {
191 ocfs2_error(sb
, "Group descriptor # %llu has bit count %u but "
192 "max bitmap bits of %u",
193 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
194 le16_to_cpu(gd
->bg_bits
),
195 8 * le16_to_cpu(gd
->bg_size
));
202 static int ocfs2_block_group_fill(handle_t
*handle
,
203 struct inode
*alloc_inode
,
204 struct buffer_head
*bg_bh
,
207 struct ocfs2_chain_list
*cl
)
210 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
211 struct super_block
* sb
= alloc_inode
->i_sb
;
215 if (((unsigned long long) bg_bh
->b_blocknr
) != group_blkno
) {
216 ocfs2_error(alloc_inode
->i_sb
, "group block (%llu) != "
218 (unsigned long long)group_blkno
,
219 (unsigned long long) bg_bh
->b_blocknr
);
224 status
= ocfs2_journal_access(handle
,
227 OCFS2_JOURNAL_ACCESS_CREATE
);
233 memset(bg
, 0, sb
->s_blocksize
);
234 strcpy(bg
->bg_signature
, OCFS2_GROUP_DESC_SIGNATURE
);
235 bg
->bg_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
236 bg
->bg_size
= cpu_to_le16(ocfs2_group_bitmap_size(sb
));
237 bg
->bg_bits
= cpu_to_le16(ocfs2_bits_per_group(cl
));
238 bg
->bg_chain
= cpu_to_le16(my_chain
);
239 bg
->bg_next_group
= cl
->cl_recs
[my_chain
].c_blkno
;
240 bg
->bg_parent_dinode
= cpu_to_le64(OCFS2_I(alloc_inode
)->ip_blkno
);
241 bg
->bg_blkno
= cpu_to_le64(group_blkno
);
242 /* set the 1st bit in the bitmap to account for the descriptor block */
243 ocfs2_set_bit(0, (unsigned long *)bg
->bg_bitmap
);
244 bg
->bg_free_bits_count
= cpu_to_le16(le16_to_cpu(bg
->bg_bits
) - 1);
246 status
= ocfs2_journal_dirty(handle
, bg_bh
);
250 /* There is no need to zero out or otherwise initialize the
251 * other blocks in a group - All valid FS metadata in a block
252 * group stores the superblock fs_generation value at
253 * allocation time. */
260 static inline u16
ocfs2_find_smallest_chain(struct ocfs2_chain_list
*cl
)
265 while (curr
< le16_to_cpu(cl
->cl_count
)) {
266 if (le32_to_cpu(cl
->cl_recs
[best
].c_total
) >
267 le32_to_cpu(cl
->cl_recs
[curr
].c_total
))
275 * We expect the block group allocator to already be locked.
277 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
278 struct inode
*alloc_inode
,
279 struct buffer_head
*bh
)
282 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) bh
->b_data
;
283 struct ocfs2_chain_list
*cl
;
284 struct ocfs2_alloc_context
*ac
= NULL
;
285 handle_t
*handle
= NULL
;
286 u32 bit_off
, num_bits
;
289 struct buffer_head
*bg_bh
= NULL
;
290 struct ocfs2_group_desc
*bg
;
292 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode
));
296 cl
= &fe
->id2
.i_chain
;
297 status
= ocfs2_reserve_clusters(osb
,
298 le16_to_cpu(cl
->cl_cpg
),
301 if (status
!= -ENOSPC
)
306 credits
= ocfs2_calc_group_alloc_credits(osb
->sb
,
307 le16_to_cpu(cl
->cl_cpg
));
308 handle
= ocfs2_start_trans(osb
, credits
);
309 if (IS_ERR(handle
)) {
310 status
= PTR_ERR(handle
);
316 status
= ocfs2_claim_clusters(osb
,
319 le16_to_cpu(cl
->cl_cpg
),
323 if (status
!= -ENOSPC
)
328 alloc_rec
= ocfs2_find_smallest_chain(cl
);
330 /* setup the group */
331 bg_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
332 mlog(0, "new descriptor, record %u, at block %llu\n",
333 alloc_rec
, (unsigned long long)bg_blkno
);
335 bg_bh
= sb_getblk(osb
->sb
, bg_blkno
);
341 ocfs2_set_new_buffer_uptodate(alloc_inode
, bg_bh
);
343 status
= ocfs2_block_group_fill(handle
,
354 bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
356 status
= ocfs2_journal_access(handle
, alloc_inode
,
357 bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
363 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_free
,
364 le16_to_cpu(bg
->bg_free_bits_count
));
365 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_total
, le16_to_cpu(bg
->bg_bits
));
366 cl
->cl_recs
[alloc_rec
].c_blkno
= cpu_to_le64(bg_blkno
);
367 if (le16_to_cpu(cl
->cl_next_free_rec
) < le16_to_cpu(cl
->cl_count
))
368 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
370 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, le16_to_cpu(bg
->bg_bits
) -
371 le16_to_cpu(bg
->bg_free_bits_count
));
372 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, le16_to_cpu(bg
->bg_bits
));
373 le32_add_cpu(&fe
->i_clusters
, le16_to_cpu(cl
->cl_cpg
));
375 status
= ocfs2_journal_dirty(handle
, bh
);
381 spin_lock(&OCFS2_I(alloc_inode
)->ip_lock
);
382 OCFS2_I(alloc_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
383 fe
->i_size
= cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode
->i_sb
,
384 le32_to_cpu(fe
->i_clusters
)));
385 spin_unlock(&OCFS2_I(alloc_inode
)->ip_lock
);
386 i_size_write(alloc_inode
, le64_to_cpu(fe
->i_size
));
387 alloc_inode
->i_blocks
= ocfs2_inode_sector_count(alloc_inode
);
392 ocfs2_commit_trans(osb
, handle
);
395 ocfs2_free_alloc_context(ac
);
404 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super
*osb
,
405 struct ocfs2_alloc_context
*ac
,
411 u32 bits_wanted
= ac
->ac_bits_wanted
;
412 struct inode
*alloc_inode
;
413 struct buffer_head
*bh
= NULL
;
414 struct ocfs2_dinode
*fe
;
419 alloc_inode
= ocfs2_get_system_file_inode(osb
, type
, slot
);
425 mutex_lock(&alloc_inode
->i_mutex
);
427 status
= ocfs2_inode_lock(alloc_inode
, &bh
, 1);
429 mutex_unlock(&alloc_inode
->i_mutex
);
436 ac
->ac_inode
= alloc_inode
;
437 ac
->ac_alloc_slot
= slot
;
439 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
440 if (!OCFS2_IS_VALID_DINODE(fe
)) {
441 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
445 if (!(fe
->i_flags
& cpu_to_le32(OCFS2_CHAIN_FL
))) {
446 ocfs2_error(alloc_inode
->i_sb
, "Invalid chain allocator %llu",
447 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
452 free_bits
= le32_to_cpu(fe
->id1
.bitmap1
.i_total
) -
453 le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
455 if (bits_wanted
> free_bits
) {
456 /* cluster bitmap never grows */
457 if (ocfs2_is_cluster_bitmap(alloc_inode
)) {
458 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
459 bits_wanted
, free_bits
);
464 if (alloc_new_group
!= ALLOC_NEW_GROUP
) {
465 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
466 "and we don't alloc a new group for it.\n",
467 slot
, bits_wanted
, free_bits
);
472 status
= ocfs2_block_group_alloc(osb
, alloc_inode
, bh
);
474 if (status
!= -ENOSPC
)
478 atomic_inc(&osb
->alloc_stats
.bg_extends
);
480 /* You should never ask for this much metadata */
482 (le32_to_cpu(fe
->id1
.bitmap1
.i_total
)
483 - le32_to_cpu(fe
->id1
.bitmap1
.i_used
)));
496 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super
*osb
,
498 struct ocfs2_alloc_context
**ac
)
503 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
510 (*ac
)->ac_bits_wanted
= blocks
;
511 (*ac
)->ac_which
= OCFS2_AC_USE_META
;
512 slot
= osb
->slot_num
;
513 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
515 status
= ocfs2_reserve_suballoc_bits(osb
, (*ac
),
516 EXTENT_ALLOC_SYSTEM_INODE
,
517 slot
, ALLOC_NEW_GROUP
);
519 if (status
!= -ENOSPC
)
526 if ((status
< 0) && *ac
) {
527 ocfs2_free_alloc_context(*ac
);
535 int ocfs2_reserve_new_metadata(struct ocfs2_super
*osb
,
536 struct ocfs2_extent_list
*root_el
,
537 struct ocfs2_alloc_context
**ac
)
539 return ocfs2_reserve_new_metadata_blocks(osb
,
540 ocfs2_extend_meta_needed(root_el
),
544 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super
*osb
,
545 struct ocfs2_alloc_context
*ac
)
547 int i
, status
= -ENOSPC
;
548 s16 slot
= ocfs2_get_inode_steal_slot(osb
);
550 /* Start to steal inodes from the first slot after ours. */
551 if (slot
== OCFS2_INVALID_SLOT
)
552 slot
= osb
->slot_num
+ 1;
554 for (i
= 0; i
< osb
->max_slots
; i
++, slot
++) {
555 if (slot
== osb
->max_slots
)
558 if (slot
== osb
->slot_num
)
561 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
562 INODE_ALLOC_SYSTEM_INODE
,
563 slot
, NOT_ALLOC_NEW_GROUP
);
565 ocfs2_set_inode_steal_slot(osb
, slot
);
569 ocfs2_free_ac_resource(ac
);
575 int ocfs2_reserve_new_inode(struct ocfs2_super
*osb
,
576 struct ocfs2_alloc_context
**ac
)
579 s16 slot
= ocfs2_get_inode_steal_slot(osb
);
581 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
588 (*ac
)->ac_bits_wanted
= 1;
589 (*ac
)->ac_which
= OCFS2_AC_USE_INODE
;
591 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
594 * slot is set when we successfully steal inode from other nodes.
595 * It is reset in 3 places:
596 * 1. when we flush the truncate log
597 * 2. when we complete local alloc recovery.
598 * 3. when we successfully allocate from our own slot.
599 * After it is set, we will go on stealing inodes until we find the
600 * need to check our slots to see whether there is some space for us.
602 if (slot
!= OCFS2_INVALID_SLOT
&&
603 atomic_read(&osb
->s_num_inodes_stolen
) < OCFS2_MAX_INODES_TO_STEAL
)
606 atomic_set(&osb
->s_num_inodes_stolen
, 0);
607 status
= ocfs2_reserve_suballoc_bits(osb
, *ac
,
608 INODE_ALLOC_SYSTEM_INODE
,
609 osb
->slot_num
, ALLOC_NEW_GROUP
);
614 * Some inodes must be freed by us, so try to allocate
615 * from our own next time.
617 if (slot
!= OCFS2_INVALID_SLOT
)
618 ocfs2_init_inode_steal_slot(osb
);
620 } else if (status
< 0 && status
!= -ENOSPC
) {
625 ocfs2_free_ac_resource(*ac
);
628 status
= ocfs2_steal_inode_from_other_nodes(osb
, *ac
);
629 atomic_inc(&osb
->s_num_inodes_stolen
);
631 if (status
!= -ENOSPC
)
638 if ((status
< 0) && *ac
) {
639 ocfs2_free_alloc_context(*ac
);
647 /* local alloc code has to do the same thing, so rather than do this
649 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super
*osb
,
650 struct ocfs2_alloc_context
*ac
)
654 ac
->ac_which
= OCFS2_AC_USE_MAIN
;
655 ac
->ac_group_search
= ocfs2_cluster_group_search
;
657 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
658 GLOBAL_BITMAP_SYSTEM_INODE
,
661 if (status
< 0 && status
!= -ENOSPC
) {
670 /* Callers don't need to care which bitmap (local alloc or main) to
671 * use so we figure it out for them, but unfortunately this clutters
673 int ocfs2_reserve_clusters(struct ocfs2_super
*osb
,
675 struct ocfs2_alloc_context
**ac
)
681 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
688 (*ac
)->ac_bits_wanted
= bits_wanted
;
691 if (ocfs2_alloc_should_use_local(osb
, bits_wanted
)) {
692 status
= ocfs2_reserve_local_alloc_bits(osb
,
695 if ((status
< 0) && (status
!= -ENOSPC
)) {
701 if (status
== -ENOSPC
) {
702 status
= ocfs2_reserve_cluster_bitmap_bits(osb
, *ac
);
704 if (status
!= -ENOSPC
)
712 if ((status
< 0) && *ac
) {
713 ocfs2_free_alloc_context(*ac
);
722 * More or less lifted from ext3. I'll leave their description below:
724 * "For ext3 allocations, we must not reuse any blocks which are
725 * allocated in the bitmap buffer's "last committed data" copy. This
726 * prevents deletes from freeing up the page for reuse until we have
727 * committed the delete transaction.
729 * If we didn't do this, then deleting something and reallocating it as
730 * data would allow the old block to be overwritten before the
731 * transaction committed (because we force data to disk before commit).
732 * This would lead to corruption if we crashed between overwriting the
733 * data and committing the delete.
735 * @@@ We may want to make this allocation behaviour conditional on
736 * data-writes at some point, and disable it for metadata allocations or
739 * Note: OCFS2 already does this differently for metadata vs data
740 * allocations, as those bitmaps are separate and undo access is never
741 * called on a metadata group descriptor.
743 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
746 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
748 if (ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
))
750 if (!buffer_jbd(bg_bh
) || !bh2jh(bg_bh
)->b_committed_data
)
753 bg
= (struct ocfs2_group_desc
*) bh2jh(bg_bh
)->b_committed_data
;
754 return !ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
);
757 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super
*osb
,
758 struct buffer_head
*bg_bh
,
759 unsigned int bits_wanted
,
760 unsigned int total_bits
,
765 u16 best_offset
, best_size
;
766 int offset
, start
, found
, status
= 0;
767 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
769 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
770 OCFS2_RO_ON_INVALID_GROUP_DESC(osb
->sb
, bg
);
774 found
= start
= best_offset
= best_size
= 0;
775 bitmap
= bg
->bg_bitmap
;
777 while((offset
= ocfs2_find_next_zero_bit(bitmap
, total_bits
, start
)) != -1) {
778 if (offset
== total_bits
)
781 if (!ocfs2_test_bg_bit_allocatable(bg_bh
, offset
)) {
782 /* We found a zero, but we can't use it as it
783 * hasn't been put to disk yet! */
786 } else if (offset
== start
) {
787 /* we found a zero */
789 /* move start to the next bit to test */
792 /* got a zero after some ones */
796 if (found
> best_size
) {
798 best_offset
= start
- found
;
800 /* we got everything we needed */
801 if (found
== bits_wanted
) {
802 /* mlog(0, "Found it all!\n"); */
807 /* XXX: I think the first clause is equivalent to the second
809 if (found
== bits_wanted
) {
810 *bit_off
= start
- found
;
812 } else if (best_size
) {
813 *bit_off
= best_offset
;
814 *bits_found
= best_size
;
817 /* No error log here -- see the comment above
818 * ocfs2_test_bg_bit_allocatable */
824 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
825 struct inode
*alloc_inode
,
826 struct ocfs2_group_desc
*bg
,
827 struct buffer_head
*group_bh
,
828 unsigned int bit_off
,
829 unsigned int num_bits
)
832 void *bitmap
= bg
->bg_bitmap
;
833 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
837 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
838 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
842 BUG_ON(le16_to_cpu(bg
->bg_free_bits_count
) < num_bits
);
844 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off
,
847 if (ocfs2_is_cluster_bitmap(alloc_inode
))
848 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
850 status
= ocfs2_journal_access(handle
,
859 le16_add_cpu(&bg
->bg_free_bits_count
, -num_bits
);
862 ocfs2_set_bit(bit_off
++, bitmap
);
864 status
= ocfs2_journal_dirty(handle
,
876 /* find the one with the most empty bits */
877 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
)
881 BUG_ON(!cl
->cl_next_free_rec
);
884 while (curr
< le16_to_cpu(cl
->cl_next_free_rec
)) {
885 if (le32_to_cpu(cl
->cl_recs
[curr
].c_free
) >
886 le32_to_cpu(cl
->cl_recs
[best
].c_free
))
891 BUG_ON(best
>= le16_to_cpu(cl
->cl_next_free_rec
));
895 static int ocfs2_relink_block_group(handle_t
*handle
,
896 struct inode
*alloc_inode
,
897 struct buffer_head
*fe_bh
,
898 struct buffer_head
*bg_bh
,
899 struct buffer_head
*prev_bg_bh
,
903 /* there is a really tiny chance the journal calls could fail,
904 * but we wouldn't want inconsistent blocks in *any* case. */
905 u64 fe_ptr
, bg_ptr
, prev_bg_ptr
;
906 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
907 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
908 struct ocfs2_group_desc
*prev_bg
= (struct ocfs2_group_desc
*) prev_bg_bh
->b_data
;
910 if (!OCFS2_IS_VALID_DINODE(fe
)) {
911 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
915 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
916 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
920 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg
)) {
921 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, prev_bg
);
926 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
927 (unsigned long long)le64_to_cpu(fe
->i_blkno
), chain
,
928 (unsigned long long)le64_to_cpu(bg
->bg_blkno
),
929 (unsigned long long)le64_to_cpu(prev_bg
->bg_blkno
));
931 fe_ptr
= le64_to_cpu(fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
);
932 bg_ptr
= le64_to_cpu(bg
->bg_next_group
);
933 prev_bg_ptr
= le64_to_cpu(prev_bg
->bg_next_group
);
935 status
= ocfs2_journal_access(handle
, alloc_inode
, prev_bg_bh
,
936 OCFS2_JOURNAL_ACCESS_WRITE
);
942 prev_bg
->bg_next_group
= bg
->bg_next_group
;
944 status
= ocfs2_journal_dirty(handle
, prev_bg_bh
);
950 status
= ocfs2_journal_access(handle
, alloc_inode
, bg_bh
,
951 OCFS2_JOURNAL_ACCESS_WRITE
);
957 bg
->bg_next_group
= fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
;
959 status
= ocfs2_journal_dirty(handle
, bg_bh
);
965 status
= ocfs2_journal_access(handle
, alloc_inode
, fe_bh
,
966 OCFS2_JOURNAL_ACCESS_WRITE
);
972 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= bg
->bg_blkno
;
974 status
= ocfs2_journal_dirty(handle
, fe_bh
);
983 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= cpu_to_le64(fe_ptr
);
984 bg
->bg_next_group
= cpu_to_le64(bg_ptr
);
985 prev_bg
->bg_next_group
= cpu_to_le64(prev_bg_ptr
);
992 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
995 return le16_to_cpu(bg
->bg_free_bits_count
) > wanted
;
998 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1000 static int ocfs2_cluster_group_search(struct inode
*inode
,
1001 struct buffer_head
*group_bh
,
1002 u32 bits_wanted
, u32 min_bits
,
1003 u16
*bit_off
, u16
*bits_found
)
1005 int search
= -ENOSPC
;
1007 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1008 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1009 u16 tmp_off
, tmp_found
;
1010 unsigned int max_bits
, gd_cluster_off
;
1012 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1014 if (gd
->bg_free_bits_count
) {
1015 max_bits
= le16_to_cpu(gd
->bg_bits
);
1017 /* Tail groups in cluster bitmaps which aren't cpg
1018 * aligned are prone to partial extention by a failed
1019 * fs resize. If the file system resize never got to
1020 * update the dinode cluster count, then we don't want
1021 * to trust any clusters past it, regardless of what
1022 * the group descriptor says. */
1023 gd_cluster_off
= ocfs2_blocks_to_clusters(inode
->i_sb
,
1024 le64_to_cpu(gd
->bg_blkno
));
1025 if ((gd_cluster_off
+ max_bits
) >
1026 OCFS2_I(inode
)->ip_clusters
) {
1027 max_bits
= OCFS2_I(inode
)->ip_clusters
- gd_cluster_off
;
1028 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1029 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
1030 le16_to_cpu(gd
->bg_bits
),
1031 OCFS2_I(inode
)->ip_clusters
, max_bits
);
1034 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1035 group_bh
, bits_wanted
,
1037 &tmp_off
, &tmp_found
);
1041 /* ocfs2_block_group_find_clear_bits() might
1042 * return success, but we still want to return
1043 * -ENOSPC unless it found the minimum number
1045 if (min_bits
<= tmp_found
) {
1047 *bits_found
= tmp_found
;
1048 search
= 0; /* success */
1049 } else if (tmp_found
) {
1051 * Don't show bits which we'll be returning
1052 * for allocation to the local alloc bitmap.
1054 ocfs2_local_alloc_seen_free_bits(osb
, tmp_found
);
1061 static int ocfs2_block_group_search(struct inode
*inode
,
1062 struct buffer_head
*group_bh
,
1063 u32 bits_wanted
, u32 min_bits
,
1064 u16
*bit_off
, u16
*bits_found
)
1067 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1069 BUG_ON(min_bits
!= 1);
1070 BUG_ON(ocfs2_is_cluster_bitmap(inode
));
1072 if (bg
->bg_free_bits_count
)
1073 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1074 group_bh
, bits_wanted
,
1075 le16_to_cpu(bg
->bg_bits
),
1076 bit_off
, bits_found
);
1081 static int ocfs2_alloc_dinode_update_counts(struct inode
*inode
,
1083 struct buffer_head
*di_bh
,
1089 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
1090 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &di
->id2
.i_chain
;
1092 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1093 OCFS2_JOURNAL_ACCESS_WRITE
);
1099 tmp_used
= le32_to_cpu(di
->id1
.bitmap1
.i_used
);
1100 di
->id1
.bitmap1
.i_used
= cpu_to_le32(num_bits
+ tmp_used
);
1101 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -num_bits
);
1103 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1111 static int ocfs2_search_one_group(struct ocfs2_alloc_context
*ac
,
1116 unsigned int *num_bits
,
1122 struct buffer_head
*group_bh
= NULL
;
1123 struct ocfs2_group_desc
*gd
;
1124 struct inode
*alloc_inode
= ac
->ac_inode
;
1126 ret
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
), gd_blkno
,
1127 &group_bh
, OCFS2_BH_CACHED
, alloc_inode
);
1133 gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1134 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
1135 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, gd
);
1140 ret
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
, min_bits
,
1150 ret
= ocfs2_alloc_dinode_update_counts(alloc_inode
, handle
, ac
->ac_bh
,
1152 le16_to_cpu(gd
->bg_chain
));
1158 ret
= ocfs2_block_group_set_bits(handle
, alloc_inode
, gd
, group_bh
,
1159 *bit_off
, *num_bits
);
1163 *bits_left
= le16_to_cpu(gd
->bg_free_bits_count
);
1171 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
1176 unsigned int *num_bits
,
1181 u16 chain
, tmp_bits
;
1184 struct inode
*alloc_inode
= ac
->ac_inode
;
1185 struct buffer_head
*group_bh
= NULL
;
1186 struct buffer_head
*prev_group_bh
= NULL
;
1187 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1188 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1189 struct ocfs2_group_desc
*bg
;
1191 chain
= ac
->ac_chain
;
1192 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1194 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
);
1196 status
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
),
1197 le64_to_cpu(cl
->cl_recs
[chain
].c_blkno
),
1198 &group_bh
, OCFS2_BH_CACHED
, alloc_inode
);
1203 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1204 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1211 /* for now, the chain search is a bit simplistic. We just use
1212 * the 1st group with any empty bits. */
1213 while ((status
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
,
1214 min_bits
, bit_off
, &tmp_bits
)) == -ENOSPC
) {
1215 if (!bg
->bg_next_group
)
1218 if (prev_group_bh
) {
1219 brelse(prev_group_bh
);
1220 prev_group_bh
= NULL
;
1222 next_group
= le64_to_cpu(bg
->bg_next_group
);
1223 prev_group_bh
= group_bh
;
1225 status
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
),
1226 next_group
, &group_bh
,
1227 OCFS2_BH_CACHED
, alloc_inode
);
1232 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1233 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1240 if (status
!= -ENOSPC
)
1245 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1246 tmp_bits
, (unsigned long long)le64_to_cpu(bg
->bg_blkno
));
1248 *num_bits
= tmp_bits
;
1250 BUG_ON(*num_bits
== 0);
1253 * Keep track of previous block descriptor read. When
1254 * we find a target, if we have read more than X
1255 * number of descriptors, and the target is reasonably
1256 * empty, relink him to top of his chain.
1258 * We've read 0 extra blocks and only send one more to
1259 * the transaction, yet the next guy to search has a
1262 * Do this *after* figuring out how many bits we're taking out
1263 * of our target group.
1265 if (ac
->ac_allow_chain_relink
&&
1267 (ocfs2_block_group_reasonably_empty(bg
, *num_bits
))) {
1268 status
= ocfs2_relink_block_group(handle
, alloc_inode
,
1269 ac
->ac_bh
, group_bh
,
1270 prev_group_bh
, chain
);
1277 /* Ok, claim our bits now: set the info on dinode, chainlist
1278 * and then the group */
1279 status
= ocfs2_journal_access(handle
,
1282 OCFS2_JOURNAL_ACCESS_WRITE
);
1288 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1289 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(*num_bits
+ tmp_used
);
1290 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -(*num_bits
));
1292 status
= ocfs2_journal_dirty(handle
,
1299 status
= ocfs2_block_group_set_bits(handle
,
1310 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits
,
1311 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
1313 *bg_blkno
= le64_to_cpu(bg
->bg_blkno
);
1314 *bits_left
= le16_to_cpu(bg
->bg_free_bits_count
);
1319 brelse(prev_group_bh
);
1325 /* will give out up to bits_wanted contiguous bits. */
1326 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
1327 struct ocfs2_alloc_context
*ac
,
1332 unsigned int *num_bits
,
1338 u64 hint_blkno
= ac
->ac_last_group
;
1339 struct ocfs2_chain_list
*cl
;
1340 struct ocfs2_dinode
*fe
;
1344 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1345 BUG_ON(bits_wanted
> (ac
->ac_bits_wanted
- ac
->ac_bits_given
));
1348 fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1349 if (!OCFS2_IS_VALID_DINODE(fe
)) {
1350 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, fe
);
1354 if (le32_to_cpu(fe
->id1
.bitmap1
.i_used
) >=
1355 le32_to_cpu(fe
->id1
.bitmap1
.i_total
)) {
1356 ocfs2_error(osb
->sb
, "Chain allocator dinode %llu has %u used "
1357 "bits but only %u total.",
1358 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
1359 le32_to_cpu(fe
->id1
.bitmap1
.i_used
),
1360 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1366 /* Attempt to short-circuit the usual search mechanism
1367 * by jumping straight to the most recently used
1368 * allocation group. This helps us mantain some
1369 * contiguousness across allocations. */
1370 status
= ocfs2_search_one_group(ac
, handle
, bits_wanted
,
1371 min_bits
, bit_off
, num_bits
,
1372 hint_blkno
, &bits_left
);
1374 /* Be careful to update *bg_blkno here as the
1375 * caller is expecting it to be filled in, and
1376 * ocfs2_search_one_group() won't do that for
1378 *bg_blkno
= hint_blkno
;
1381 if (status
< 0 && status
!= -ENOSPC
) {
1387 cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1389 victim
= ocfs2_find_victim_chain(cl
);
1390 ac
->ac_chain
= victim
;
1391 ac
->ac_allow_chain_relink
= 1;
1393 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
, bit_off
,
1394 num_bits
, bg_blkno
, &bits_left
);
1397 if (status
< 0 && status
!= -ENOSPC
) {
1402 mlog(0, "Search of victim chain %u came up with nothing, "
1403 "trying all chains now.\n", victim
);
1405 /* If we didn't pick a good victim, then just default to
1406 * searching each chain in order. Don't allow chain relinking
1407 * because we only calculate enough journal credits for one
1408 * relink per alloc. */
1409 ac
->ac_allow_chain_relink
= 0;
1410 for (i
= 0; i
< le16_to_cpu(cl
->cl_next_free_rec
); i
++) {
1413 if (!cl
->cl_recs
[i
].c_free
)
1417 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
,
1418 bit_off
, num_bits
, bg_blkno
,
1422 if (status
< 0 && status
!= -ENOSPC
) {
1429 if (status
!= -ENOSPC
) {
1430 /* If the next search of this group is not likely to
1431 * yield a suitable extent, then we reset the last
1432 * group hint so as to not waste a disk read */
1433 if (bits_left
< min_bits
)
1434 ac
->ac_last_group
= 0;
1436 ac
->ac_last_group
= *bg_blkno
;
1444 int ocfs2_claim_metadata(struct ocfs2_super
*osb
,
1446 struct ocfs2_alloc_context
*ac
,
1448 u16
*suballoc_bit_start
,
1449 unsigned int *num_bits
,
1456 BUG_ON(ac
->ac_bits_wanted
< (ac
->ac_bits_given
+ bits_wanted
));
1457 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_META
);
1459 status
= ocfs2_claim_suballoc_bits(osb
,
1471 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1473 *blkno_start
= bg_blkno
+ (u64
) *suballoc_bit_start
;
1474 ac
->ac_bits_given
+= (*num_bits
);
1481 int ocfs2_claim_new_inode(struct ocfs2_super
*osb
,
1483 struct ocfs2_alloc_context
*ac
,
1488 unsigned int num_bits
;
1494 BUG_ON(ac
->ac_bits_given
!= 0);
1495 BUG_ON(ac
->ac_bits_wanted
!= 1);
1496 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_INODE
);
1498 status
= ocfs2_claim_suballoc_bits(osb
,
1510 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1512 BUG_ON(num_bits
!= 1);
1514 *fe_blkno
= bg_blkno
+ (u64
) (*suballoc_bit
);
1515 ac
->ac_bits_given
++;
1522 /* translate a group desc. blkno and it's bitmap offset into
1523 * disk cluster offset. */
1524 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
1528 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1531 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1533 if (bg_blkno
!= osb
->first_cluster_group_blkno
)
1534 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, bg_blkno
);
1535 cluster
+= (u32
) bg_bit_off
;
1539 /* given a cluster offset, calculate which block group it belongs to
1540 * and return that block offset. */
1541 u64
ocfs2_which_cluster_group(struct inode
*inode
, u32 cluster
)
1543 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1546 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1548 group_no
= cluster
/ osb
->bitmap_cpg
;
1550 return osb
->first_cluster_group_blkno
;
1551 return ocfs2_clusters_to_blocks(inode
->i_sb
,
1552 group_no
* osb
->bitmap_cpg
);
1555 /* given the block number of a cluster start, calculate which cluster
1556 * group and descriptor bitmap offset that corresponds to. */
1557 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
1562 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1563 u32 data_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, data_blkno
);
1565 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1567 *bg_blkno
= ocfs2_which_cluster_group(inode
,
1570 if (*bg_blkno
== osb
->first_cluster_group_blkno
)
1571 *bg_bit_off
= (u16
) data_cluster
;
1573 *bg_bit_off
= (u16
) ocfs2_blocks_to_clusters(osb
->sb
,
1574 data_blkno
- *bg_blkno
);
1578 * min_bits - minimum contiguous chunk from this total allocation we
1579 * can handle. set to what we asked for originally for a full
1580 * contig. allocation, set to '1' to indicate we can deal with extents
1583 int __ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1585 struct ocfs2_alloc_context
*ac
,
1592 unsigned int bits_wanted
= max_clusters
;
1598 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1600 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_LOCAL
1601 && ac
->ac_which
!= OCFS2_AC_USE_MAIN
);
1603 if (ac
->ac_which
== OCFS2_AC_USE_LOCAL
) {
1604 status
= ocfs2_claim_local_alloc_bits(osb
,
1611 atomic_inc(&osb
->alloc_stats
.local_data
);
1613 if (min_clusters
> (osb
->bitmap_cpg
- 1)) {
1614 /* The only paths asking for contiguousness
1615 * should know about this already. */
1616 mlog(ML_ERROR
, "minimum allocation requested %u exceeds "
1617 "group bitmap size %u!\n", min_clusters
,
1622 /* clamp the current request down to a realistic size. */
1623 if (bits_wanted
> (osb
->bitmap_cpg
- 1))
1624 bits_wanted
= osb
->bitmap_cpg
- 1;
1626 status
= ocfs2_claim_suballoc_bits(osb
,
1636 ocfs2_desc_bitmap_to_cluster_off(ac
->ac_inode
,
1639 atomic_inc(&osb
->alloc_stats
.bitmap_data
);
1643 if (status
!= -ENOSPC
)
1648 ac
->ac_bits_given
+= *num_clusters
;
1655 int ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1657 struct ocfs2_alloc_context
*ac
,
1662 unsigned int bits_wanted
= ac
->ac_bits_wanted
- ac
->ac_bits_given
;
1664 return __ocfs2_claim_clusters(osb
, handle
, ac
, min_clusters
,
1665 bits_wanted
, cluster_start
, num_clusters
);
1668 static inline int ocfs2_block_group_clear_bits(handle_t
*handle
,
1669 struct inode
*alloc_inode
,
1670 struct ocfs2_group_desc
*bg
,
1671 struct buffer_head
*group_bh
,
1672 unsigned int bit_off
,
1673 unsigned int num_bits
)
1677 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1678 struct ocfs2_group_desc
*undo_bg
= NULL
;
1682 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
1683 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
1688 mlog(0, "off = %u, num = %u\n", bit_off
, num_bits
);
1690 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1691 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1693 status
= ocfs2_journal_access(handle
, alloc_inode
, group_bh
,
1700 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1701 undo_bg
= (struct ocfs2_group_desc
*) bh2jh(group_bh
)->b_committed_data
;
1705 ocfs2_clear_bit((bit_off
+ tmp
),
1706 (unsigned long *) bg
->bg_bitmap
);
1707 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1708 ocfs2_set_bit(bit_off
+ tmp
,
1709 (unsigned long *) undo_bg
->bg_bitmap
);
1711 le16_add_cpu(&bg
->bg_free_bits_count
, num_bits
);
1713 status
= ocfs2_journal_dirty(handle
, group_bh
);
1721 * expects the suballoc inode to already be locked.
1723 int ocfs2_free_suballoc_bits(handle_t
*handle
,
1724 struct inode
*alloc_inode
,
1725 struct buffer_head
*alloc_bh
,
1726 unsigned int start_bit
,
1732 struct ocfs2_super
*osb
= OCFS2_SB(alloc_inode
->i_sb
);
1733 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) alloc_bh
->b_data
;
1734 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
1735 struct buffer_head
*group_bh
= NULL
;
1736 struct ocfs2_group_desc
*group
;
1740 if (!OCFS2_IS_VALID_DINODE(fe
)) {
1741 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
1745 BUG_ON((count
+ start_bit
) > ocfs2_bits_per_group(cl
));
1747 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1748 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
, count
,
1749 (unsigned long long)bg_blkno
, start_bit
);
1751 status
= ocfs2_read_block(osb
, bg_blkno
, &group_bh
, OCFS2_BH_CACHED
,
1758 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1759 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, group
);
1764 BUG_ON((count
+ start_bit
) > le16_to_cpu(group
->bg_bits
));
1766 status
= ocfs2_block_group_clear_bits(handle
, alloc_inode
,
1774 status
= ocfs2_journal_access(handle
, alloc_inode
, alloc_bh
,
1775 OCFS2_JOURNAL_ACCESS_WRITE
);
1781 le32_add_cpu(&cl
->cl_recs
[le16_to_cpu(group
->bg_chain
)].c_free
,
1783 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1784 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(tmp_used
- count
);
1786 status
= ocfs2_journal_dirty(handle
, alloc_bh
);
1800 int ocfs2_free_dinode(handle_t
*handle
,
1801 struct inode
*inode_alloc_inode
,
1802 struct buffer_head
*inode_alloc_bh
,
1803 struct ocfs2_dinode
*di
)
1805 u64 blk
= le64_to_cpu(di
->i_blkno
);
1806 u16 bit
= le16_to_cpu(di
->i_suballoc_bit
);
1807 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1809 return ocfs2_free_suballoc_bits(handle
, inode_alloc_inode
,
1810 inode_alloc_bh
, bit
, bg_blkno
, 1);
1813 int ocfs2_free_clusters(handle_t
*handle
,
1814 struct inode
*bitmap_inode
,
1815 struct buffer_head
*bitmap_bh
,
1817 unsigned int num_clusters
)
1822 struct ocfs2_dinode
*fe
;
1824 /* You can't ever have a contiguous set of clusters
1825 * bigger than a block group bitmap so we never have to worry
1826 * about looping on them. */
1830 /* This is expensive. We can safely remove once this stuff has
1831 * gotten tested really well. */
1832 BUG_ON(start_blk
!= ocfs2_clusters_to_blocks(bitmap_inode
->i_sb
, ocfs2_blocks_to_clusters(bitmap_inode
->i_sb
, start_blk
)));
1834 fe
= (struct ocfs2_dinode
*) bitmap_bh
->b_data
;
1836 ocfs2_block_to_cluster_group(bitmap_inode
, start_blk
, &bg_blkno
,
1839 mlog(0, "want to free %u clusters starting at block %llu\n",
1840 num_clusters
, (unsigned long long)start_blk
);
1841 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1842 (unsigned long long)bg_blkno
, bg_start_bit
);
1844 status
= ocfs2_free_suballoc_bits(handle
, bitmap_inode
, bitmap_bh
,
1845 bg_start_bit
, bg_blkno
,
1852 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode
->i_sb
),
1860 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
)
1862 printk("Block Group:\n");
1863 printk("bg_signature: %s\n", bg
->bg_signature
);
1864 printk("bg_size: %u\n", bg
->bg_size
);
1865 printk("bg_bits: %u\n", bg
->bg_bits
);
1866 printk("bg_free_bits_count: %u\n", bg
->bg_free_bits_count
);
1867 printk("bg_chain: %u\n", bg
->bg_chain
);
1868 printk("bg_generation: %u\n", le32_to_cpu(bg
->bg_generation
));
1869 printk("bg_next_group: %llu\n",
1870 (unsigned long long)bg
->bg_next_group
);
1871 printk("bg_parent_dinode: %llu\n",
1872 (unsigned long long)bg
->bg_parent_dinode
);
1873 printk("bg_blkno: %llu\n",
1874 (unsigned long long)bg
->bg_blkno
);
1877 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
)
1881 printk("Suballoc Inode %llu:\n", (unsigned long long)fe
->i_blkno
);
1882 printk("i_signature: %s\n", fe
->i_signature
);
1883 printk("i_size: %llu\n",
1884 (unsigned long long)fe
->i_size
);
1885 printk("i_clusters: %u\n", fe
->i_clusters
);
1886 printk("i_generation: %u\n",
1887 le32_to_cpu(fe
->i_generation
));
1888 printk("id1.bitmap1.i_used: %u\n",
1889 le32_to_cpu(fe
->id1
.bitmap1
.i_used
));
1890 printk("id1.bitmap1.i_total: %u\n",
1891 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1892 printk("id2.i_chain.cl_cpg: %u\n", fe
->id2
.i_chain
.cl_cpg
);
1893 printk("id2.i_chain.cl_bpc: %u\n", fe
->id2
.i_chain
.cl_bpc
);
1894 printk("id2.i_chain.cl_count: %u\n", fe
->id2
.i_chain
.cl_count
);
1895 printk("id2.i_chain.cl_next_free_rec: %u\n",
1896 fe
->id2
.i_chain
.cl_next_free_rec
);
1897 for(i
= 0; i
< fe
->id2
.i_chain
.cl_next_free_rec
; i
++) {
1898 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i
,
1899 fe
->id2
.i_chain
.cl_recs
[i
].c_free
);
1900 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i
,
1901 fe
->id2
.i_chain
.cl_recs
[i
].c_total
);
1902 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i
,
1903 (unsigned long long)fe
->id2
.i_chain
.cl_recs
[i
].c_blkno
);
1908 * For a given allocation, determine which allocators will need to be
1909 * accessed, and lock them, reserving the appropriate number of bits.
1911 * Sparse file systems call this from ocfs2_write_begin_nolock()
1912 * and ocfs2_allocate_unwritten_extents().
1914 * File systems which don't support holes call this from
1915 * ocfs2_extend_allocation().
1917 int ocfs2_lock_allocators(struct inode
*inode
, struct buffer_head
*root_bh
,
1918 struct ocfs2_extent_list
*root_el
,
1919 u32 clusters_to_add
, u32 extents_to_split
,
1920 struct ocfs2_alloc_context
**data_ac
,
1921 struct ocfs2_alloc_context
**meta_ac
,
1922 enum ocfs2_extent_tree_type type
, void *private)
1924 int ret
= 0, num_free_extents
;
1925 unsigned int max_recs_needed
= clusters_to_add
+ 2 * extents_to_split
;
1926 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1932 BUG_ON(clusters_to_add
!= 0 && data_ac
== NULL
);
1934 num_free_extents
= ocfs2_num_free_extents(osb
, inode
, root_bh
,
1936 if (num_free_extents
< 0) {
1937 ret
= num_free_extents
;
1943 * Sparse allocation file systems need to be more conservative
1944 * with reserving room for expansion - the actual allocation
1945 * happens while we've got a journal handle open so re-taking
1946 * a cluster lock (because we ran out of room for another
1947 * extent) will violate ordering rules.
1949 * Most of the time we'll only be seeing this 1 cluster at a time
1952 * Always lock for any unwritten extents - we might want to
1953 * add blocks during a split.
1955 if (!num_free_extents
||
1956 (ocfs2_sparse_alloc(osb
) && num_free_extents
< max_recs_needed
)) {
1957 ret
= ocfs2_reserve_new_metadata(osb
, root_el
, meta_ac
);
1965 if (clusters_to_add
== 0)
1968 ret
= ocfs2_reserve_clusters(osb
, clusters_to_add
, data_ac
);
1978 ocfs2_free_alloc_context(*meta_ac
);
1983 * We cannot have an error and a non null *data_ac.