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_INODES_TO_STEAL 1024
56 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
);
57 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
);
58 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
);
59 static int ocfs2_block_group_fill(handle_t
*handle
,
60 struct inode
*alloc_inode
,
61 struct buffer_head
*bg_bh
,
64 struct ocfs2_chain_list
*cl
);
65 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
66 struct inode
*alloc_inode
,
67 struct buffer_head
*bh
,
69 u64
*last_alloc_group
,
72 static int ocfs2_cluster_group_search(struct inode
*inode
,
73 struct buffer_head
*group_bh
,
74 u32 bits_wanted
, u32 min_bits
,
76 u16
*bit_off
, u16
*bits_found
);
77 static int ocfs2_block_group_search(struct inode
*inode
,
78 struct buffer_head
*group_bh
,
79 u32 bits_wanted
, u32 min_bits
,
81 u16
*bit_off
, u16
*bits_found
);
82 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
83 struct ocfs2_alloc_context
*ac
,
88 unsigned int *num_bits
,
90 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
92 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
93 struct inode
*alloc_inode
,
94 struct ocfs2_group_desc
*bg
,
95 struct buffer_head
*group_bh
,
97 unsigned int num_bits
);
98 static inline int ocfs2_block_group_clear_bits(handle_t
*handle
,
99 struct inode
*alloc_inode
,
100 struct ocfs2_group_desc
*bg
,
101 struct buffer_head
*group_bh
,
102 unsigned int bit_off
,
103 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
);
142 void ocfs2_free_alloc_context(struct ocfs2_alloc_context
*ac
)
144 ocfs2_free_ac_resource(ac
);
148 static u32
ocfs2_bits_per_group(struct ocfs2_chain_list
*cl
)
150 return (u32
)le16_to_cpu(cl
->cl_cpg
) * (u32
)le16_to_cpu(cl
->cl_bpc
);
153 #define do_error(fmt, ...) \
156 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
158 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
161 static int ocfs2_validate_gd_self(struct super_block
*sb
,
162 struct buffer_head
*bh
,
165 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
167 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
168 do_error("Group descriptor #%llu has bad signature %.*s",
169 (unsigned long long)bh
->b_blocknr
, 7,
174 if (le64_to_cpu(gd
->bg_blkno
) != bh
->b_blocknr
) {
175 do_error("Group descriptor #%llu has an invalid bg_blkno "
177 (unsigned long long)bh
->b_blocknr
,
178 (unsigned long long)le64_to_cpu(gd
->bg_blkno
));
182 if (le32_to_cpu(gd
->bg_generation
) != OCFS2_SB(sb
)->fs_generation
) {
183 do_error("Group descriptor #%llu has an invalid "
184 "fs_generation of #%u",
185 (unsigned long long)bh
->b_blocknr
,
186 le32_to_cpu(gd
->bg_generation
));
190 if (le16_to_cpu(gd
->bg_free_bits_count
) > le16_to_cpu(gd
->bg_bits
)) {
191 do_error("Group descriptor #%llu has bit count %u but "
192 "claims that %u are free",
193 (unsigned long long)bh
->b_blocknr
,
194 le16_to_cpu(gd
->bg_bits
),
195 le16_to_cpu(gd
->bg_free_bits_count
));
199 if (le16_to_cpu(gd
->bg_bits
) > (8 * le16_to_cpu(gd
->bg_size
))) {
200 do_error("Group descriptor #%llu has bit count %u but "
201 "max bitmap bits of %u",
202 (unsigned long long)bh
->b_blocknr
,
203 le16_to_cpu(gd
->bg_bits
),
204 8 * le16_to_cpu(gd
->bg_size
));
211 static int ocfs2_validate_gd_parent(struct super_block
*sb
,
212 struct ocfs2_dinode
*di
,
213 struct buffer_head
*bh
,
216 unsigned int max_bits
;
217 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
219 if (di
->i_blkno
!= gd
->bg_parent_dinode
) {
220 do_error("Group descriptor #%llu has bad parent "
221 "pointer (%llu, expected %llu)",
222 (unsigned long long)bh
->b_blocknr
,
223 (unsigned long long)le64_to_cpu(gd
->bg_parent_dinode
),
224 (unsigned long long)le64_to_cpu(di
->i_blkno
));
228 max_bits
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
) * le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
229 if (le16_to_cpu(gd
->bg_bits
) > max_bits
) {
230 do_error("Group descriptor #%llu has bit count of %u",
231 (unsigned long long)bh
->b_blocknr
,
232 le16_to_cpu(gd
->bg_bits
));
236 if (le16_to_cpu(gd
->bg_chain
) >=
237 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) {
238 do_error("Group descriptor #%llu has bad chain %u",
239 (unsigned long long)bh
->b_blocknr
,
240 le16_to_cpu(gd
->bg_chain
));
250 * This version only prints errors. It does not fail the filesystem, and
251 * exists only for resize.
253 int ocfs2_check_group_descriptor(struct super_block
*sb
,
254 struct ocfs2_dinode
*di
,
255 struct buffer_head
*bh
)
258 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
260 BUG_ON(!buffer_uptodate(bh
));
263 * If the ecc fails, we return the error but otherwise
264 * leave the filesystem running. We know any error is
265 * local to this block.
267 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &gd
->bg_check
);
270 "Checksum failed for group descriptor %llu\n",
271 (unsigned long long)bh
->b_blocknr
);
273 rc
= ocfs2_validate_gd_self(sb
, bh
, 1);
275 rc
= ocfs2_validate_gd_parent(sb
, di
, bh
, 1);
280 static int ocfs2_validate_group_descriptor(struct super_block
*sb
,
281 struct buffer_head
*bh
)
284 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
286 mlog(0, "Validating group descriptor %llu\n",
287 (unsigned long long)bh
->b_blocknr
);
289 BUG_ON(!buffer_uptodate(bh
));
292 * If the ecc fails, we return the error but otherwise
293 * leave the filesystem running. We know any error is
294 * local to this block.
296 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &gd
->bg_check
);
301 * Errors after here are fatal.
304 return ocfs2_validate_gd_self(sb
, bh
, 0);
307 int ocfs2_read_group_descriptor(struct inode
*inode
, struct ocfs2_dinode
*di
,
308 u64 gd_blkno
, struct buffer_head
**bh
)
311 struct buffer_head
*tmp
= *bh
;
313 rc
= ocfs2_read_block(INODE_CACHE(inode
), gd_blkno
, &tmp
,
314 ocfs2_validate_group_descriptor
);
318 rc
= ocfs2_validate_gd_parent(inode
->i_sb
, di
, tmp
, 0);
324 /* If ocfs2_read_block() got us a new bh, pass it up. */
332 static int ocfs2_block_group_fill(handle_t
*handle
,
333 struct inode
*alloc_inode
,
334 struct buffer_head
*bg_bh
,
337 struct ocfs2_chain_list
*cl
)
340 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
341 struct super_block
* sb
= alloc_inode
->i_sb
;
345 if (((unsigned long long) bg_bh
->b_blocknr
) != group_blkno
) {
346 ocfs2_error(alloc_inode
->i_sb
, "group block (%llu) != "
348 (unsigned long long)group_blkno
,
349 (unsigned long long) bg_bh
->b_blocknr
);
354 status
= ocfs2_journal_access_gd(handle
,
355 INODE_CACHE(alloc_inode
),
357 OCFS2_JOURNAL_ACCESS_CREATE
);
363 memset(bg
, 0, sb
->s_blocksize
);
364 strcpy(bg
->bg_signature
, OCFS2_GROUP_DESC_SIGNATURE
);
365 bg
->bg_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
366 bg
->bg_size
= cpu_to_le16(ocfs2_group_bitmap_size(sb
));
367 bg
->bg_bits
= cpu_to_le16(ocfs2_bits_per_group(cl
));
368 bg
->bg_chain
= cpu_to_le16(my_chain
);
369 bg
->bg_next_group
= cl
->cl_recs
[my_chain
].c_blkno
;
370 bg
->bg_parent_dinode
= cpu_to_le64(OCFS2_I(alloc_inode
)->ip_blkno
);
371 bg
->bg_blkno
= cpu_to_le64(group_blkno
);
372 /* set the 1st bit in the bitmap to account for the descriptor block */
373 ocfs2_set_bit(0, (unsigned long *)bg
->bg_bitmap
);
374 bg
->bg_free_bits_count
= cpu_to_le16(le16_to_cpu(bg
->bg_bits
) - 1);
376 status
= ocfs2_journal_dirty(handle
, bg_bh
);
380 /* There is no need to zero out or otherwise initialize the
381 * other blocks in a group - All valid FS metadata in a block
382 * group stores the superblock fs_generation value at
383 * allocation time. */
390 static inline u16
ocfs2_find_smallest_chain(struct ocfs2_chain_list
*cl
)
395 while (curr
< le16_to_cpu(cl
->cl_count
)) {
396 if (le32_to_cpu(cl
->cl_recs
[best
].c_total
) >
397 le32_to_cpu(cl
->cl_recs
[curr
].c_total
))
405 * We expect the block group allocator to already be locked.
407 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
408 struct inode
*alloc_inode
,
409 struct buffer_head
*bh
,
411 u64
*last_alloc_group
,
415 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) bh
->b_data
;
416 struct ocfs2_chain_list
*cl
;
417 struct ocfs2_alloc_context
*ac
= NULL
;
418 handle_t
*handle
= NULL
;
419 u32 bit_off
, num_bits
;
422 struct buffer_head
*bg_bh
= NULL
;
423 struct ocfs2_group_desc
*bg
;
425 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode
));
429 cl
= &fe
->id2
.i_chain
;
430 status
= ocfs2_reserve_clusters_with_limit(osb
,
431 le16_to_cpu(cl
->cl_cpg
),
432 max_block
, flags
, &ac
);
434 if (status
!= -ENOSPC
)
439 credits
= ocfs2_calc_group_alloc_credits(osb
->sb
,
440 le16_to_cpu(cl
->cl_cpg
));
441 handle
= ocfs2_start_trans(osb
, credits
);
442 if (IS_ERR(handle
)) {
443 status
= PTR_ERR(handle
);
449 if (last_alloc_group
&& *last_alloc_group
!= 0) {
450 mlog(0, "use old allocation group %llu for block group alloc\n",
451 (unsigned long long)*last_alloc_group
);
452 ac
->ac_last_group
= *last_alloc_group
;
454 status
= ocfs2_claim_clusters(osb
,
457 le16_to_cpu(cl
->cl_cpg
),
461 if (status
!= -ENOSPC
)
466 alloc_rec
= ocfs2_find_smallest_chain(cl
);
468 /* setup the group */
469 bg_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
470 mlog(0, "new descriptor, record %u, at block %llu\n",
471 alloc_rec
, (unsigned long long)bg_blkno
);
473 bg_bh
= sb_getblk(osb
->sb
, bg_blkno
);
479 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode
), bg_bh
);
481 status
= ocfs2_block_group_fill(handle
,
492 bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
494 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(alloc_inode
),
495 bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
501 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_free
,
502 le16_to_cpu(bg
->bg_free_bits_count
));
503 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_total
, le16_to_cpu(bg
->bg_bits
));
504 cl
->cl_recs
[alloc_rec
].c_blkno
= cpu_to_le64(bg_blkno
);
505 if (le16_to_cpu(cl
->cl_next_free_rec
) < le16_to_cpu(cl
->cl_count
))
506 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
508 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, le16_to_cpu(bg
->bg_bits
) -
509 le16_to_cpu(bg
->bg_free_bits_count
));
510 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, le16_to_cpu(bg
->bg_bits
));
511 le32_add_cpu(&fe
->i_clusters
, le16_to_cpu(cl
->cl_cpg
));
513 status
= ocfs2_journal_dirty(handle
, bh
);
519 spin_lock(&OCFS2_I(alloc_inode
)->ip_lock
);
520 OCFS2_I(alloc_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
521 fe
->i_size
= cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode
->i_sb
,
522 le32_to_cpu(fe
->i_clusters
)));
523 spin_unlock(&OCFS2_I(alloc_inode
)->ip_lock
);
524 i_size_write(alloc_inode
, le64_to_cpu(fe
->i_size
));
525 alloc_inode
->i_blocks
= ocfs2_inode_sector_count(alloc_inode
);
529 /* save the new last alloc group so that the caller can cache it. */
530 if (last_alloc_group
)
531 *last_alloc_group
= ac
->ac_last_group
;
535 ocfs2_commit_trans(osb
, handle
);
538 ocfs2_free_alloc_context(ac
);
546 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super
*osb
,
547 struct ocfs2_alloc_context
*ac
,
550 u64
*last_alloc_group
,
554 u32 bits_wanted
= ac
->ac_bits_wanted
;
555 struct inode
*alloc_inode
;
556 struct buffer_head
*bh
= NULL
;
557 struct ocfs2_dinode
*fe
;
562 alloc_inode
= ocfs2_get_system_file_inode(osb
, type
, slot
);
568 mutex_lock(&alloc_inode
->i_mutex
);
570 status
= ocfs2_inode_lock(alloc_inode
, &bh
, 1);
572 mutex_unlock(&alloc_inode
->i_mutex
);
579 ac
->ac_inode
= alloc_inode
;
580 ac
->ac_alloc_slot
= slot
;
582 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
584 /* The bh was validated by the inode read inside
585 * ocfs2_inode_lock(). Any corruption is a code bug. */
586 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
588 if (!(fe
->i_flags
& cpu_to_le32(OCFS2_CHAIN_FL
))) {
589 ocfs2_error(alloc_inode
->i_sb
, "Invalid chain allocator %llu",
590 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
595 free_bits
= le32_to_cpu(fe
->id1
.bitmap1
.i_total
) -
596 le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
598 if (bits_wanted
> free_bits
) {
599 /* cluster bitmap never grows */
600 if (ocfs2_is_cluster_bitmap(alloc_inode
)) {
601 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
602 bits_wanted
, free_bits
);
607 if (!(flags
& ALLOC_NEW_GROUP
)) {
608 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
609 "and we don't alloc a new group for it.\n",
610 slot
, bits_wanted
, free_bits
);
615 status
= ocfs2_block_group_alloc(osb
, alloc_inode
, bh
,
617 last_alloc_group
, flags
);
619 if (status
!= -ENOSPC
)
623 atomic_inc(&osb
->alloc_stats
.bg_extends
);
625 /* You should never ask for this much metadata */
627 (le32_to_cpu(fe
->id1
.bitmap1
.i_total
)
628 - le32_to_cpu(fe
->id1
.bitmap1
.i_used
)));
640 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super
*osb
,
642 struct ocfs2_alloc_context
**ac
)
647 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
654 (*ac
)->ac_bits_wanted
= blocks
;
655 (*ac
)->ac_which
= OCFS2_AC_USE_META
;
656 slot
= osb
->slot_num
;
657 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
659 status
= ocfs2_reserve_suballoc_bits(osb
, (*ac
),
660 EXTENT_ALLOC_SYSTEM_INODE
,
661 slot
, NULL
, ALLOC_NEW_GROUP
);
663 if (status
!= -ENOSPC
)
670 if ((status
< 0) && *ac
) {
671 ocfs2_free_alloc_context(*ac
);
679 int ocfs2_reserve_new_metadata(struct ocfs2_super
*osb
,
680 struct ocfs2_extent_list
*root_el
,
681 struct ocfs2_alloc_context
**ac
)
683 return ocfs2_reserve_new_metadata_blocks(osb
,
684 ocfs2_extend_meta_needed(root_el
),
688 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super
*osb
,
689 struct ocfs2_alloc_context
*ac
)
691 int i
, status
= -ENOSPC
;
692 s16 slot
= ocfs2_get_inode_steal_slot(osb
);
694 /* Start to steal inodes from the first slot after ours. */
695 if (slot
== OCFS2_INVALID_SLOT
)
696 slot
= osb
->slot_num
+ 1;
698 for (i
= 0; i
< osb
->max_slots
; i
++, slot
++) {
699 if (slot
== osb
->max_slots
)
702 if (slot
== osb
->slot_num
)
705 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
706 INODE_ALLOC_SYSTEM_INODE
,
708 NOT_ALLOC_NEW_GROUP
);
710 ocfs2_set_inode_steal_slot(osb
, slot
);
714 ocfs2_free_ac_resource(ac
);
720 int ocfs2_reserve_new_inode(struct ocfs2_super
*osb
,
721 struct ocfs2_alloc_context
**ac
)
724 s16 slot
= ocfs2_get_inode_steal_slot(osb
);
727 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
734 (*ac
)->ac_bits_wanted
= 1;
735 (*ac
)->ac_which
= OCFS2_AC_USE_INODE
;
737 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
740 * stat(2) can't handle i_ino > 32bits, so we tell the
741 * lower levels not to allocate us a block group past that
742 * limit. The 'inode64' mount option avoids this behavior.
744 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_INODE64
))
745 (*ac
)->ac_max_block
= (u32
)~0U;
748 * slot is set when we successfully steal inode from other nodes.
749 * It is reset in 3 places:
750 * 1. when we flush the truncate log
751 * 2. when we complete local alloc recovery.
752 * 3. when we successfully allocate from our own slot.
753 * After it is set, we will go on stealing inodes until we find the
754 * need to check our slots to see whether there is some space for us.
756 if (slot
!= OCFS2_INVALID_SLOT
&&
757 atomic_read(&osb
->s_num_inodes_stolen
) < OCFS2_MAX_INODES_TO_STEAL
)
760 atomic_set(&osb
->s_num_inodes_stolen
, 0);
761 alloc_group
= osb
->osb_inode_alloc_group
;
762 status
= ocfs2_reserve_suballoc_bits(osb
, *ac
,
763 INODE_ALLOC_SYSTEM_INODE
,
767 ALLOC_GROUPS_FROM_GLOBAL
);
771 spin_lock(&osb
->osb_lock
);
772 osb
->osb_inode_alloc_group
= alloc_group
;
773 spin_unlock(&osb
->osb_lock
);
774 mlog(0, "after reservation, new allocation group is "
775 "%llu\n", (unsigned long long)alloc_group
);
778 * Some inodes must be freed by us, so try to allocate
779 * from our own next time.
781 if (slot
!= OCFS2_INVALID_SLOT
)
782 ocfs2_init_inode_steal_slot(osb
);
784 } else if (status
< 0 && status
!= -ENOSPC
) {
789 ocfs2_free_ac_resource(*ac
);
792 status
= ocfs2_steal_inode_from_other_nodes(osb
, *ac
);
793 atomic_inc(&osb
->s_num_inodes_stolen
);
795 if (status
!= -ENOSPC
)
802 if ((status
< 0) && *ac
) {
803 ocfs2_free_alloc_context(*ac
);
811 /* local alloc code has to do the same thing, so rather than do this
813 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super
*osb
,
814 struct ocfs2_alloc_context
*ac
)
818 ac
->ac_which
= OCFS2_AC_USE_MAIN
;
819 ac
->ac_group_search
= ocfs2_cluster_group_search
;
821 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
822 GLOBAL_BITMAP_SYSTEM_INODE
,
823 OCFS2_INVALID_SLOT
, NULL
,
825 if (status
< 0 && status
!= -ENOSPC
) {
834 /* Callers don't need to care which bitmap (local alloc or main) to
835 * use so we figure it out for them, but unfortunately this clutters
837 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super
*osb
,
838 u32 bits_wanted
, u64 max_block
,
840 struct ocfs2_alloc_context
**ac
)
846 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
853 (*ac
)->ac_bits_wanted
= bits_wanted
;
854 (*ac
)->ac_max_block
= max_block
;
857 if (!(flags
& ALLOC_GROUPS_FROM_GLOBAL
) &&
858 ocfs2_alloc_should_use_local(osb
, bits_wanted
)) {
859 status
= ocfs2_reserve_local_alloc_bits(osb
,
862 if (status
== -EFBIG
) {
863 /* The local alloc window is outside ac_max_block.
864 * use the main bitmap. */
866 } else if ((status
< 0) && (status
!= -ENOSPC
)) {
872 if (status
== -ENOSPC
) {
873 status
= ocfs2_reserve_cluster_bitmap_bits(osb
, *ac
);
875 if (status
!= -ENOSPC
)
883 if ((status
< 0) && *ac
) {
884 ocfs2_free_alloc_context(*ac
);
892 int ocfs2_reserve_clusters(struct ocfs2_super
*osb
,
894 struct ocfs2_alloc_context
**ac
)
896 return ocfs2_reserve_clusters_with_limit(osb
, bits_wanted
, 0,
897 ALLOC_NEW_GROUP
, ac
);
901 * More or less lifted from ext3. I'll leave their description below:
903 * "For ext3 allocations, we must not reuse any blocks which are
904 * allocated in the bitmap buffer's "last committed data" copy. This
905 * prevents deletes from freeing up the page for reuse until we have
906 * committed the delete transaction.
908 * If we didn't do this, then deleting something and reallocating it as
909 * data would allow the old block to be overwritten before the
910 * transaction committed (because we force data to disk before commit).
911 * This would lead to corruption if we crashed between overwriting the
912 * data and committing the delete.
914 * @@@ We may want to make this allocation behaviour conditional on
915 * data-writes at some point, and disable it for metadata allocations or
918 * Note: OCFS2 already does this differently for metadata vs data
919 * allocations, as those bitmaps are separate and undo access is never
920 * called on a metadata group descriptor.
922 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
925 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
928 if (ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
))
931 if (!buffer_jbd(bg_bh
))
934 jbd_lock_bh_state(bg_bh
);
935 bg
= (struct ocfs2_group_desc
*) bh2jh(bg_bh
)->b_committed_data
;
937 ret
= !ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
);
940 jbd_unlock_bh_state(bg_bh
);
945 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super
*osb
,
946 struct buffer_head
*bg_bh
,
947 unsigned int bits_wanted
,
948 unsigned int total_bits
,
953 u16 best_offset
, best_size
;
954 int offset
, start
, found
, status
= 0;
955 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
957 /* Callers got this descriptor from
958 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
959 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
961 found
= start
= best_offset
= best_size
= 0;
962 bitmap
= bg
->bg_bitmap
;
964 while((offset
= ocfs2_find_next_zero_bit(bitmap
, total_bits
, start
)) != -1) {
965 if (offset
== total_bits
)
968 if (!ocfs2_test_bg_bit_allocatable(bg_bh
, offset
)) {
969 /* We found a zero, but we can't use it as it
970 * hasn't been put to disk yet! */
973 } else if (offset
== start
) {
974 /* we found a zero */
976 /* move start to the next bit to test */
979 /* got a zero after some ones */
983 if (found
> best_size
) {
985 best_offset
= start
- found
;
987 /* we got everything we needed */
988 if (found
== bits_wanted
) {
989 /* mlog(0, "Found it all!\n"); */
994 /* XXX: I think the first clause is equivalent to the second
996 if (found
== bits_wanted
) {
997 *bit_off
= start
- found
;
999 } else if (best_size
) {
1000 *bit_off
= best_offset
;
1001 *bits_found
= best_size
;
1004 /* No error log here -- see the comment above
1005 * ocfs2_test_bg_bit_allocatable */
1011 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
1012 struct inode
*alloc_inode
,
1013 struct ocfs2_group_desc
*bg
,
1014 struct buffer_head
*group_bh
,
1015 unsigned int bit_off
,
1016 unsigned int num_bits
)
1019 void *bitmap
= bg
->bg_bitmap
;
1020 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1024 /* All callers get the descriptor via
1025 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1026 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1027 BUG_ON(le16_to_cpu(bg
->bg_free_bits_count
) < num_bits
);
1029 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off
,
1032 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1033 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1035 status
= ocfs2_journal_access_gd(handle
,
1036 INODE_CACHE(alloc_inode
),
1044 le16_add_cpu(&bg
->bg_free_bits_count
, -num_bits
);
1047 ocfs2_set_bit(bit_off
++, bitmap
);
1049 status
= ocfs2_journal_dirty(handle
,
1061 /* find the one with the most empty bits */
1062 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
)
1066 BUG_ON(!cl
->cl_next_free_rec
);
1069 while (curr
< le16_to_cpu(cl
->cl_next_free_rec
)) {
1070 if (le32_to_cpu(cl
->cl_recs
[curr
].c_free
) >
1071 le32_to_cpu(cl
->cl_recs
[best
].c_free
))
1076 BUG_ON(best
>= le16_to_cpu(cl
->cl_next_free_rec
));
1080 static int ocfs2_relink_block_group(handle_t
*handle
,
1081 struct inode
*alloc_inode
,
1082 struct buffer_head
*fe_bh
,
1083 struct buffer_head
*bg_bh
,
1084 struct buffer_head
*prev_bg_bh
,
1088 /* there is a really tiny chance the journal calls could fail,
1089 * but we wouldn't want inconsistent blocks in *any* case. */
1090 u64 fe_ptr
, bg_ptr
, prev_bg_ptr
;
1091 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
1092 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
1093 struct ocfs2_group_desc
*prev_bg
= (struct ocfs2_group_desc
*) prev_bg_bh
->b_data
;
1095 /* The caller got these descriptors from
1096 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1097 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1098 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg
));
1100 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
1101 (unsigned long long)le64_to_cpu(fe
->i_blkno
), chain
,
1102 (unsigned long long)le64_to_cpu(bg
->bg_blkno
),
1103 (unsigned long long)le64_to_cpu(prev_bg
->bg_blkno
));
1105 fe_ptr
= le64_to_cpu(fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
);
1106 bg_ptr
= le64_to_cpu(bg
->bg_next_group
);
1107 prev_bg_ptr
= le64_to_cpu(prev_bg
->bg_next_group
);
1109 status
= ocfs2_journal_access_gd(handle
, INODE_CACHE(alloc_inode
),
1111 OCFS2_JOURNAL_ACCESS_WRITE
);
1117 prev_bg
->bg_next_group
= bg
->bg_next_group
;
1119 status
= ocfs2_journal_dirty(handle
, prev_bg_bh
);
1125 status
= ocfs2_journal_access_gd(handle
, INODE_CACHE(alloc_inode
),
1126 bg_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
1132 bg
->bg_next_group
= fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
;
1134 status
= ocfs2_journal_dirty(handle
, bg_bh
);
1140 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(alloc_inode
),
1141 fe_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
1147 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= bg
->bg_blkno
;
1149 status
= ocfs2_journal_dirty(handle
, fe_bh
);
1158 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= cpu_to_le64(fe_ptr
);
1159 bg
->bg_next_group
= cpu_to_le64(bg_ptr
);
1160 prev_bg
->bg_next_group
= cpu_to_le64(prev_bg_ptr
);
1167 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
1170 return le16_to_cpu(bg
->bg_free_bits_count
) > wanted
;
1173 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1174 * value on error. */
1175 static int ocfs2_cluster_group_search(struct inode
*inode
,
1176 struct buffer_head
*group_bh
,
1177 u32 bits_wanted
, u32 min_bits
,
1179 u16
*bit_off
, u16
*bits_found
)
1181 int search
= -ENOSPC
;
1184 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1185 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1186 u16 tmp_off
, tmp_found
;
1187 unsigned int max_bits
, gd_cluster_off
;
1189 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1191 if (gd
->bg_free_bits_count
) {
1192 max_bits
= le16_to_cpu(gd
->bg_bits
);
1194 /* Tail groups in cluster bitmaps which aren't cpg
1195 * aligned are prone to partial extention by a failed
1196 * fs resize. If the file system resize never got to
1197 * update the dinode cluster count, then we don't want
1198 * to trust any clusters past it, regardless of what
1199 * the group descriptor says. */
1200 gd_cluster_off
= ocfs2_blocks_to_clusters(inode
->i_sb
,
1201 le64_to_cpu(gd
->bg_blkno
));
1202 if ((gd_cluster_off
+ max_bits
) >
1203 OCFS2_I(inode
)->ip_clusters
) {
1204 max_bits
= OCFS2_I(inode
)->ip_clusters
- gd_cluster_off
;
1205 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1206 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
1207 le16_to_cpu(gd
->bg_bits
),
1208 OCFS2_I(inode
)->ip_clusters
, max_bits
);
1211 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1212 group_bh
, bits_wanted
,
1214 &tmp_off
, &tmp_found
);
1219 blkoff
= ocfs2_clusters_to_blocks(inode
->i_sb
,
1221 tmp_off
+ tmp_found
);
1222 mlog(0, "Checking %llu against %llu\n",
1223 (unsigned long long)blkoff
,
1224 (unsigned long long)max_block
);
1225 if (blkoff
> max_block
)
1229 /* ocfs2_block_group_find_clear_bits() might
1230 * return success, but we still want to return
1231 * -ENOSPC unless it found the minimum number
1233 if (min_bits
<= tmp_found
) {
1235 *bits_found
= tmp_found
;
1236 search
= 0; /* success */
1237 } else if (tmp_found
) {
1239 * Don't show bits which we'll be returning
1240 * for allocation to the local alloc bitmap.
1242 ocfs2_local_alloc_seen_free_bits(osb
, tmp_found
);
1249 static int ocfs2_block_group_search(struct inode
*inode
,
1250 struct buffer_head
*group_bh
,
1251 u32 bits_wanted
, u32 min_bits
,
1253 u16
*bit_off
, u16
*bits_found
)
1257 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1259 BUG_ON(min_bits
!= 1);
1260 BUG_ON(ocfs2_is_cluster_bitmap(inode
));
1262 if (bg
->bg_free_bits_count
) {
1263 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1264 group_bh
, bits_wanted
,
1265 le16_to_cpu(bg
->bg_bits
),
1266 bit_off
, bits_found
);
1267 if (!ret
&& max_block
) {
1268 blkoff
= le64_to_cpu(bg
->bg_blkno
) + *bit_off
+
1270 mlog(0, "Checking %llu against %llu\n",
1271 (unsigned long long)blkoff
,
1272 (unsigned long long)max_block
);
1273 if (blkoff
> max_block
)
1281 static int ocfs2_alloc_dinode_update_counts(struct inode
*inode
,
1283 struct buffer_head
*di_bh
,
1289 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
1290 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &di
->id2
.i_chain
;
1292 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
1293 OCFS2_JOURNAL_ACCESS_WRITE
);
1299 tmp_used
= le32_to_cpu(di
->id1
.bitmap1
.i_used
);
1300 di
->id1
.bitmap1
.i_used
= cpu_to_le32(num_bits
+ tmp_used
);
1301 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -num_bits
);
1303 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1311 static int ocfs2_search_one_group(struct ocfs2_alloc_context
*ac
,
1316 unsigned int *num_bits
,
1322 struct buffer_head
*group_bh
= NULL
;
1323 struct ocfs2_group_desc
*gd
;
1324 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)ac
->ac_bh
->b_data
;
1325 struct inode
*alloc_inode
= ac
->ac_inode
;
1327 ret
= ocfs2_read_group_descriptor(alloc_inode
, di
, gd_blkno
,
1334 gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1335 ret
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
, min_bits
,
1336 ac
->ac_max_block
, bit_off
, &found
);
1345 ret
= ocfs2_alloc_dinode_update_counts(alloc_inode
, handle
, ac
->ac_bh
,
1347 le16_to_cpu(gd
->bg_chain
));
1353 ret
= ocfs2_block_group_set_bits(handle
, alloc_inode
, gd
, group_bh
,
1354 *bit_off
, *num_bits
);
1358 *bits_left
= le16_to_cpu(gd
->bg_free_bits_count
);
1366 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
1371 unsigned int *num_bits
,
1376 u16 chain
, tmp_bits
;
1379 struct inode
*alloc_inode
= ac
->ac_inode
;
1380 struct buffer_head
*group_bh
= NULL
;
1381 struct buffer_head
*prev_group_bh
= NULL
;
1382 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1383 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1384 struct ocfs2_group_desc
*bg
;
1386 chain
= ac
->ac_chain
;
1387 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1389 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
);
1391 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
,
1392 le64_to_cpu(cl
->cl_recs
[chain
].c_blkno
),
1398 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1401 /* for now, the chain search is a bit simplistic. We just use
1402 * the 1st group with any empty bits. */
1403 while ((status
= ac
->ac_group_search(alloc_inode
, group_bh
,
1404 bits_wanted
, min_bits
,
1405 ac
->ac_max_block
, bit_off
,
1406 &tmp_bits
)) == -ENOSPC
) {
1407 if (!bg
->bg_next_group
)
1410 brelse(prev_group_bh
);
1411 prev_group_bh
= NULL
;
1413 next_group
= le64_to_cpu(bg
->bg_next_group
);
1414 prev_group_bh
= group_bh
;
1416 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
,
1417 next_group
, &group_bh
);
1422 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1425 if (status
!= -ENOSPC
)
1430 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1431 tmp_bits
, (unsigned long long)le64_to_cpu(bg
->bg_blkno
));
1433 *num_bits
= tmp_bits
;
1435 BUG_ON(*num_bits
== 0);
1438 * Keep track of previous block descriptor read. When
1439 * we find a target, if we have read more than X
1440 * number of descriptors, and the target is reasonably
1441 * empty, relink him to top of his chain.
1443 * We've read 0 extra blocks and only send one more to
1444 * the transaction, yet the next guy to search has a
1447 * Do this *after* figuring out how many bits we're taking out
1448 * of our target group.
1450 if (ac
->ac_allow_chain_relink
&&
1452 (ocfs2_block_group_reasonably_empty(bg
, *num_bits
))) {
1453 status
= ocfs2_relink_block_group(handle
, alloc_inode
,
1454 ac
->ac_bh
, group_bh
,
1455 prev_group_bh
, chain
);
1462 /* Ok, claim our bits now: set the info on dinode, chainlist
1463 * and then the group */
1464 status
= ocfs2_journal_access_di(handle
,
1465 INODE_CACHE(alloc_inode
),
1467 OCFS2_JOURNAL_ACCESS_WRITE
);
1473 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1474 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(*num_bits
+ tmp_used
);
1475 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -(*num_bits
));
1477 status
= ocfs2_journal_dirty(handle
,
1484 status
= ocfs2_block_group_set_bits(handle
,
1495 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits
,
1496 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
1498 *bg_blkno
= le64_to_cpu(bg
->bg_blkno
);
1499 *bits_left
= le16_to_cpu(bg
->bg_free_bits_count
);
1502 brelse(prev_group_bh
);
1508 /* will give out up to bits_wanted contiguous bits. */
1509 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
1510 struct ocfs2_alloc_context
*ac
,
1515 unsigned int *num_bits
,
1521 u64 hint_blkno
= ac
->ac_last_group
;
1522 struct ocfs2_chain_list
*cl
;
1523 struct ocfs2_dinode
*fe
;
1527 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1528 BUG_ON(bits_wanted
> (ac
->ac_bits_wanted
- ac
->ac_bits_given
));
1531 fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1533 /* The bh was validated by the inode read during
1534 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1535 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
1537 if (le32_to_cpu(fe
->id1
.bitmap1
.i_used
) >=
1538 le32_to_cpu(fe
->id1
.bitmap1
.i_total
)) {
1539 ocfs2_error(osb
->sb
, "Chain allocator dinode %llu has %u used "
1540 "bits but only %u total.",
1541 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
1542 le32_to_cpu(fe
->id1
.bitmap1
.i_used
),
1543 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1549 /* Attempt to short-circuit the usual search mechanism
1550 * by jumping straight to the most recently used
1551 * allocation group. This helps us mantain some
1552 * contiguousness across allocations. */
1553 status
= ocfs2_search_one_group(ac
, handle
, bits_wanted
,
1554 min_bits
, bit_off
, num_bits
,
1555 hint_blkno
, &bits_left
);
1557 /* Be careful to update *bg_blkno here as the
1558 * caller is expecting it to be filled in, and
1559 * ocfs2_search_one_group() won't do that for
1561 *bg_blkno
= hint_blkno
;
1564 if (status
< 0 && status
!= -ENOSPC
) {
1570 cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1572 victim
= ocfs2_find_victim_chain(cl
);
1573 ac
->ac_chain
= victim
;
1574 ac
->ac_allow_chain_relink
= 1;
1576 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
, bit_off
,
1577 num_bits
, bg_blkno
, &bits_left
);
1580 if (status
< 0 && status
!= -ENOSPC
) {
1585 mlog(0, "Search of victim chain %u came up with nothing, "
1586 "trying all chains now.\n", victim
);
1588 /* If we didn't pick a good victim, then just default to
1589 * searching each chain in order. Don't allow chain relinking
1590 * because we only calculate enough journal credits for one
1591 * relink per alloc. */
1592 ac
->ac_allow_chain_relink
= 0;
1593 for (i
= 0; i
< le16_to_cpu(cl
->cl_next_free_rec
); i
++) {
1596 if (!cl
->cl_recs
[i
].c_free
)
1600 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
,
1601 bit_off
, num_bits
, bg_blkno
,
1605 if (status
< 0 && status
!= -ENOSPC
) {
1612 if (status
!= -ENOSPC
) {
1613 /* If the next search of this group is not likely to
1614 * yield a suitable extent, then we reset the last
1615 * group hint so as to not waste a disk read */
1616 if (bits_left
< min_bits
)
1617 ac
->ac_last_group
= 0;
1619 ac
->ac_last_group
= *bg_blkno
;
1627 int ocfs2_claim_metadata(struct ocfs2_super
*osb
,
1629 struct ocfs2_alloc_context
*ac
,
1631 u16
*suballoc_bit_start
,
1632 unsigned int *num_bits
,
1639 BUG_ON(ac
->ac_bits_wanted
< (ac
->ac_bits_given
+ bits_wanted
));
1640 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_META
);
1642 status
= ocfs2_claim_suballoc_bits(osb
,
1654 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1656 *blkno_start
= bg_blkno
+ (u64
) *suballoc_bit_start
;
1657 ac
->ac_bits_given
+= (*num_bits
);
1664 static void ocfs2_init_inode_ac_group(struct inode
*dir
,
1665 struct buffer_head
*parent_fe_bh
,
1666 struct ocfs2_alloc_context
*ac
)
1668 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*)parent_fe_bh
->b_data
;
1670 * Try to allocate inodes from some specific group.
1672 * If the parent dir has recorded the last group used in allocation,
1673 * cool, use it. Otherwise if we try to allocate new inode from the
1674 * same slot the parent dir belongs to, use the same chunk.
1676 * We are very careful here to avoid the mistake of setting
1677 * ac_last_group to a group descriptor from a different (unlocked) slot.
1679 if (OCFS2_I(dir
)->ip_last_used_group
&&
1680 OCFS2_I(dir
)->ip_last_used_slot
== ac
->ac_alloc_slot
)
1681 ac
->ac_last_group
= OCFS2_I(dir
)->ip_last_used_group
;
1682 else if (le16_to_cpu(fe
->i_suballoc_slot
) == ac
->ac_alloc_slot
)
1683 ac
->ac_last_group
= ocfs2_which_suballoc_group(
1684 le64_to_cpu(fe
->i_blkno
),
1685 le16_to_cpu(fe
->i_suballoc_bit
));
1688 static inline void ocfs2_save_inode_ac_group(struct inode
*dir
,
1689 struct ocfs2_alloc_context
*ac
)
1691 OCFS2_I(dir
)->ip_last_used_group
= ac
->ac_last_group
;
1692 OCFS2_I(dir
)->ip_last_used_slot
= ac
->ac_alloc_slot
;
1695 int ocfs2_claim_new_inode(struct ocfs2_super
*osb
,
1698 struct buffer_head
*parent_fe_bh
,
1699 struct ocfs2_alloc_context
*ac
,
1704 unsigned int num_bits
;
1710 BUG_ON(ac
->ac_bits_given
!= 0);
1711 BUG_ON(ac
->ac_bits_wanted
!= 1);
1712 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_INODE
);
1714 ocfs2_init_inode_ac_group(dir
, parent_fe_bh
, ac
);
1716 status
= ocfs2_claim_suballoc_bits(osb
,
1728 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1730 BUG_ON(num_bits
!= 1);
1732 *fe_blkno
= bg_blkno
+ (u64
) (*suballoc_bit
);
1733 ac
->ac_bits_given
++;
1734 ocfs2_save_inode_ac_group(dir
, ac
);
1741 /* translate a group desc. blkno and it's bitmap offset into
1742 * disk cluster offset. */
1743 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
1747 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1750 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1752 if (bg_blkno
!= osb
->first_cluster_group_blkno
)
1753 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, bg_blkno
);
1754 cluster
+= (u32
) bg_bit_off
;
1758 /* given a cluster offset, calculate which block group it belongs to
1759 * and return that block offset. */
1760 u64
ocfs2_which_cluster_group(struct inode
*inode
, u32 cluster
)
1762 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1765 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1767 group_no
= cluster
/ osb
->bitmap_cpg
;
1769 return osb
->first_cluster_group_blkno
;
1770 return ocfs2_clusters_to_blocks(inode
->i_sb
,
1771 group_no
* osb
->bitmap_cpg
);
1774 /* given the block number of a cluster start, calculate which cluster
1775 * group and descriptor bitmap offset that corresponds to. */
1776 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
1781 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1782 u32 data_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, data_blkno
);
1784 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1786 *bg_blkno
= ocfs2_which_cluster_group(inode
,
1789 if (*bg_blkno
== osb
->first_cluster_group_blkno
)
1790 *bg_bit_off
= (u16
) data_cluster
;
1792 *bg_bit_off
= (u16
) ocfs2_blocks_to_clusters(osb
->sb
,
1793 data_blkno
- *bg_blkno
);
1797 * min_bits - minimum contiguous chunk from this total allocation we
1798 * can handle. set to what we asked for originally for a full
1799 * contig. allocation, set to '1' to indicate we can deal with extents
1802 int __ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1804 struct ocfs2_alloc_context
*ac
,
1811 unsigned int bits_wanted
= max_clusters
;
1817 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1819 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_LOCAL
1820 && ac
->ac_which
!= OCFS2_AC_USE_MAIN
);
1822 if (ac
->ac_which
== OCFS2_AC_USE_LOCAL
) {
1823 status
= ocfs2_claim_local_alloc_bits(osb
,
1830 atomic_inc(&osb
->alloc_stats
.local_data
);
1832 if (min_clusters
> (osb
->bitmap_cpg
- 1)) {
1833 /* The only paths asking for contiguousness
1834 * should know about this already. */
1835 mlog(ML_ERROR
, "minimum allocation requested %u exceeds "
1836 "group bitmap size %u!\n", min_clusters
,
1841 /* clamp the current request down to a realistic size. */
1842 if (bits_wanted
> (osb
->bitmap_cpg
- 1))
1843 bits_wanted
= osb
->bitmap_cpg
- 1;
1845 status
= ocfs2_claim_suballoc_bits(osb
,
1855 ocfs2_desc_bitmap_to_cluster_off(ac
->ac_inode
,
1858 atomic_inc(&osb
->alloc_stats
.bitmap_data
);
1862 if (status
!= -ENOSPC
)
1867 ac
->ac_bits_given
+= *num_clusters
;
1874 int ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1876 struct ocfs2_alloc_context
*ac
,
1881 unsigned int bits_wanted
= ac
->ac_bits_wanted
- ac
->ac_bits_given
;
1883 return __ocfs2_claim_clusters(osb
, handle
, ac
, min_clusters
,
1884 bits_wanted
, cluster_start
, num_clusters
);
1887 static inline int ocfs2_block_group_clear_bits(handle_t
*handle
,
1888 struct inode
*alloc_inode
,
1889 struct ocfs2_group_desc
*bg
,
1890 struct buffer_head
*group_bh
,
1891 unsigned int bit_off
,
1892 unsigned int num_bits
)
1896 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1897 struct ocfs2_group_desc
*undo_bg
= NULL
;
1898 int cluster_bitmap
= 0;
1902 /* The caller got this descriptor from
1903 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1904 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1906 mlog(0, "off = %u, num = %u\n", bit_off
, num_bits
);
1908 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1909 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1911 status
= ocfs2_journal_access_gd(handle
, INODE_CACHE(alloc_inode
),
1912 group_bh
, journal_type
);
1918 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1921 if (cluster_bitmap
) {
1922 jbd_lock_bh_state(group_bh
);
1923 undo_bg
= (struct ocfs2_group_desc
*)
1924 bh2jh(group_bh
)->b_committed_data
;
1930 ocfs2_clear_bit((bit_off
+ tmp
),
1931 (unsigned long *) bg
->bg_bitmap
);
1933 ocfs2_set_bit(bit_off
+ tmp
,
1934 (unsigned long *) undo_bg
->bg_bitmap
);
1936 le16_add_cpu(&bg
->bg_free_bits_count
, num_bits
);
1939 jbd_unlock_bh_state(group_bh
);
1941 status
= ocfs2_journal_dirty(handle
, group_bh
);
1949 * expects the suballoc inode to already be locked.
1951 int ocfs2_free_suballoc_bits(handle_t
*handle
,
1952 struct inode
*alloc_inode
,
1953 struct buffer_head
*alloc_bh
,
1954 unsigned int start_bit
,
1960 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) alloc_bh
->b_data
;
1961 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
1962 struct buffer_head
*group_bh
= NULL
;
1963 struct ocfs2_group_desc
*group
;
1967 /* The alloc_bh comes from ocfs2_free_dinode() or
1968 * ocfs2_free_clusters(). The callers have all locked the
1969 * allocator and gotten alloc_bh from the lock call. This
1970 * validates the dinode buffer. Any corruption that has happended
1972 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
1973 BUG_ON((count
+ start_bit
) > ocfs2_bits_per_group(cl
));
1975 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1976 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
, count
,
1977 (unsigned long long)bg_blkno
, start_bit
);
1979 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
, bg_blkno
,
1985 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1987 BUG_ON((count
+ start_bit
) > le16_to_cpu(group
->bg_bits
));
1989 status
= ocfs2_block_group_clear_bits(handle
, alloc_inode
,
1997 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(alloc_inode
),
1998 alloc_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
2004 le32_add_cpu(&cl
->cl_recs
[le16_to_cpu(group
->bg_chain
)].c_free
,
2006 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
2007 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(tmp_used
- count
);
2009 status
= ocfs2_journal_dirty(handle
, alloc_bh
);
2022 int ocfs2_free_dinode(handle_t
*handle
,
2023 struct inode
*inode_alloc_inode
,
2024 struct buffer_head
*inode_alloc_bh
,
2025 struct ocfs2_dinode
*di
)
2027 u64 blk
= le64_to_cpu(di
->i_blkno
);
2028 u16 bit
= le16_to_cpu(di
->i_suballoc_bit
);
2029 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
2031 return ocfs2_free_suballoc_bits(handle
, inode_alloc_inode
,
2032 inode_alloc_bh
, bit
, bg_blkno
, 1);
2035 int ocfs2_free_clusters(handle_t
*handle
,
2036 struct inode
*bitmap_inode
,
2037 struct buffer_head
*bitmap_bh
,
2039 unsigned int num_clusters
)
2044 struct ocfs2_dinode
*fe
;
2046 /* You can't ever have a contiguous set of clusters
2047 * bigger than a block group bitmap so we never have to worry
2048 * about looping on them. */
2052 /* This is expensive. We can safely remove once this stuff has
2053 * gotten tested really well. */
2054 BUG_ON(start_blk
!= ocfs2_clusters_to_blocks(bitmap_inode
->i_sb
, ocfs2_blocks_to_clusters(bitmap_inode
->i_sb
, start_blk
)));
2056 fe
= (struct ocfs2_dinode
*) bitmap_bh
->b_data
;
2058 ocfs2_block_to_cluster_group(bitmap_inode
, start_blk
, &bg_blkno
,
2061 mlog(0, "want to free %u clusters starting at block %llu\n",
2062 num_clusters
, (unsigned long long)start_blk
);
2063 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2064 (unsigned long long)bg_blkno
, bg_start_bit
);
2066 status
= ocfs2_free_suballoc_bits(handle
, bitmap_inode
, bitmap_bh
,
2067 bg_start_bit
, bg_blkno
,
2074 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode
->i_sb
),
2082 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
)
2084 printk("Block Group:\n");
2085 printk("bg_signature: %s\n", bg
->bg_signature
);
2086 printk("bg_size: %u\n", bg
->bg_size
);
2087 printk("bg_bits: %u\n", bg
->bg_bits
);
2088 printk("bg_free_bits_count: %u\n", bg
->bg_free_bits_count
);
2089 printk("bg_chain: %u\n", bg
->bg_chain
);
2090 printk("bg_generation: %u\n", le32_to_cpu(bg
->bg_generation
));
2091 printk("bg_next_group: %llu\n",
2092 (unsigned long long)bg
->bg_next_group
);
2093 printk("bg_parent_dinode: %llu\n",
2094 (unsigned long long)bg
->bg_parent_dinode
);
2095 printk("bg_blkno: %llu\n",
2096 (unsigned long long)bg
->bg_blkno
);
2099 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
)
2103 printk("Suballoc Inode %llu:\n", (unsigned long long)fe
->i_blkno
);
2104 printk("i_signature: %s\n", fe
->i_signature
);
2105 printk("i_size: %llu\n",
2106 (unsigned long long)fe
->i_size
);
2107 printk("i_clusters: %u\n", fe
->i_clusters
);
2108 printk("i_generation: %u\n",
2109 le32_to_cpu(fe
->i_generation
));
2110 printk("id1.bitmap1.i_used: %u\n",
2111 le32_to_cpu(fe
->id1
.bitmap1
.i_used
));
2112 printk("id1.bitmap1.i_total: %u\n",
2113 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
2114 printk("id2.i_chain.cl_cpg: %u\n", fe
->id2
.i_chain
.cl_cpg
);
2115 printk("id2.i_chain.cl_bpc: %u\n", fe
->id2
.i_chain
.cl_bpc
);
2116 printk("id2.i_chain.cl_count: %u\n", fe
->id2
.i_chain
.cl_count
);
2117 printk("id2.i_chain.cl_next_free_rec: %u\n",
2118 fe
->id2
.i_chain
.cl_next_free_rec
);
2119 for(i
= 0; i
< fe
->id2
.i_chain
.cl_next_free_rec
; i
++) {
2120 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i
,
2121 fe
->id2
.i_chain
.cl_recs
[i
].c_free
);
2122 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i
,
2123 fe
->id2
.i_chain
.cl_recs
[i
].c_total
);
2124 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i
,
2125 (unsigned long long)fe
->id2
.i_chain
.cl_recs
[i
].c_blkno
);
2130 * For a given allocation, determine which allocators will need to be
2131 * accessed, and lock them, reserving the appropriate number of bits.
2133 * Sparse file systems call this from ocfs2_write_begin_nolock()
2134 * and ocfs2_allocate_unwritten_extents().
2136 * File systems which don't support holes call this from
2137 * ocfs2_extend_allocation().
2139 int ocfs2_lock_allocators(struct inode
*inode
,
2140 struct ocfs2_extent_tree
*et
,
2141 u32 clusters_to_add
, u32 extents_to_split
,
2142 struct ocfs2_alloc_context
**data_ac
,
2143 struct ocfs2_alloc_context
**meta_ac
)
2145 int ret
= 0, num_free_extents
;
2146 unsigned int max_recs_needed
= clusters_to_add
+ 2 * extents_to_split
;
2147 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2153 BUG_ON(clusters_to_add
!= 0 && data_ac
== NULL
);
2155 num_free_extents
= ocfs2_num_free_extents(osb
, et
);
2156 if (num_free_extents
< 0) {
2157 ret
= num_free_extents
;
2163 * Sparse allocation file systems need to be more conservative
2164 * with reserving room for expansion - the actual allocation
2165 * happens while we've got a journal handle open so re-taking
2166 * a cluster lock (because we ran out of room for another
2167 * extent) will violate ordering rules.
2169 * Most of the time we'll only be seeing this 1 cluster at a time
2172 * Always lock for any unwritten extents - we might want to
2173 * add blocks during a split.
2175 if (!num_free_extents
||
2176 (ocfs2_sparse_alloc(osb
) && num_free_extents
< max_recs_needed
)) {
2177 ret
= ocfs2_reserve_new_metadata(osb
, et
->et_root_el
, meta_ac
);
2185 if (clusters_to_add
== 0)
2188 ret
= ocfs2_reserve_clusters(osb
, clusters_to_add
, data_ac
);
2198 ocfs2_free_alloc_context(*meta_ac
);
2203 * We cannot have an error and a non null *data_ac.
2211 * Read the inode specified by blkno to get suballoc_slot and
2214 static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super
*osb
, u64 blkno
,
2215 u16
*suballoc_slot
, u16
*suballoc_bit
)
2218 struct buffer_head
*inode_bh
= NULL
;
2219 struct ocfs2_dinode
*inode_fe
;
2221 mlog_entry("blkno: %llu\n", (unsigned long long)blkno
);
2223 /* dirty read disk */
2224 status
= ocfs2_read_blocks_sync(osb
, blkno
, 1, &inode_bh
);
2226 mlog(ML_ERROR
, "read block %llu failed %d\n",
2227 (unsigned long long)blkno
, status
);
2231 inode_fe
= (struct ocfs2_dinode
*) inode_bh
->b_data
;
2232 if (!OCFS2_IS_VALID_DINODE(inode_fe
)) {
2233 mlog(ML_ERROR
, "invalid inode %llu requested\n",
2234 (unsigned long long)blkno
);
2239 if (le16_to_cpu(inode_fe
->i_suballoc_slot
) != (u16
)OCFS2_INVALID_SLOT
&&
2240 (u32
)le16_to_cpu(inode_fe
->i_suballoc_slot
) > osb
->max_slots
- 1) {
2241 mlog(ML_ERROR
, "inode %llu has invalid suballoc slot %u\n",
2242 (unsigned long long)blkno
,
2243 (u32
)le16_to_cpu(inode_fe
->i_suballoc_slot
));
2249 *suballoc_slot
= le16_to_cpu(inode_fe
->i_suballoc_slot
);
2251 *suballoc_bit
= le16_to_cpu(inode_fe
->i_suballoc_bit
);
2261 * test whether bit is SET in allocator bitmap or not. on success, 0
2262 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2263 * is returned and *res is meaningless. Call this after you have
2264 * cluster locked against suballoc, or you may get a result based on
2265 * non-up2date contents
2267 static int ocfs2_test_suballoc_bit(struct ocfs2_super
*osb
,
2268 struct inode
*suballoc
,
2269 struct buffer_head
*alloc_bh
, u64 blkno
,
2272 struct ocfs2_dinode
*alloc_fe
;
2273 struct ocfs2_group_desc
*group
;
2274 struct buffer_head
*group_bh
= NULL
;
2278 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno
,
2281 alloc_fe
= (struct ocfs2_dinode
*)alloc_bh
->b_data
;
2282 if ((bit
+ 1) > ocfs2_bits_per_group(&alloc_fe
->id2
.i_chain
)) {
2283 mlog(ML_ERROR
, "suballoc bit %u out of range of %u\n",
2285 ocfs2_bits_per_group(&alloc_fe
->id2
.i_chain
));
2290 bg_blkno
= ocfs2_which_suballoc_group(blkno
, bit
);
2291 status
= ocfs2_read_group_descriptor(suballoc
, alloc_fe
, bg_blkno
,
2294 mlog(ML_ERROR
, "read group %llu failed %d\n",
2295 (unsigned long long)bg_blkno
, status
);
2299 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
2300 *res
= ocfs2_test_bit(bit
, (unsigned long *)group
->bg_bitmap
);
2310 * Test if the bit representing this inode (blkno) is set in the
2313 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2315 * In the event of failure, a negative value is returned and *res is
2318 * Callers must make sure to hold nfs_sync_lock to prevent
2319 * ocfs2_delete_inode() on another node from accessing the same
2320 * suballocator concurrently.
2322 int ocfs2_test_inode_bit(struct ocfs2_super
*osb
, u64 blkno
, int *res
)
2325 u16 suballoc_bit
= 0, suballoc_slot
= 0;
2326 struct inode
*inode_alloc_inode
;
2327 struct buffer_head
*alloc_bh
= NULL
;
2329 mlog_entry("blkno: %llu", (unsigned long long)blkno
);
2331 status
= ocfs2_get_suballoc_slot_bit(osb
, blkno
, &suballoc_slot
,
2334 mlog(ML_ERROR
, "get alloc slot and bit failed %d\n", status
);
2339 ocfs2_get_system_file_inode(osb
, INODE_ALLOC_SYSTEM_INODE
,
2341 if (!inode_alloc_inode
) {
2342 /* the error code could be inaccurate, but we are not able to
2343 * get the correct one. */
2345 mlog(ML_ERROR
, "unable to get alloc inode in slot %u\n",
2346 (u32
)suballoc_slot
);
2350 mutex_lock(&inode_alloc_inode
->i_mutex
);
2351 status
= ocfs2_inode_lock(inode_alloc_inode
, &alloc_bh
, 0);
2353 mutex_unlock(&inode_alloc_inode
->i_mutex
);
2354 mlog(ML_ERROR
, "lock on alloc inode on slot %u failed %d\n",
2355 (u32
)suballoc_slot
, status
);
2359 status
= ocfs2_test_suballoc_bit(osb
, inode_alloc_inode
, alloc_bh
,
2360 blkno
, suballoc_bit
, res
);
2362 mlog(ML_ERROR
, "test suballoc bit failed %d\n", status
);
2364 ocfs2_inode_unlock(inode_alloc_inode
, 0);
2365 mutex_unlock(&inode_alloc_inode
->i_mutex
);
2367 iput(inode_alloc_inode
);