1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * metadata alloc and free
7 * Inspired by ext3 block groups.
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
38 #include "blockcheck.h"
42 #include "localalloc.h"
48 #include "buffer_head_io.h"
50 #define NOT_ALLOC_NEW_GROUP 0
51 #define ALLOC_NEW_GROUP 0x1
52 #define ALLOC_GROUPS_FROM_GLOBAL 0x2
54 #define OCFS2_MAX_TO_STEAL 1024
56 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 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
,
113 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super
*osb
,
114 u32 bits_wanted
, u64 max_block
,
116 struct ocfs2_alloc_context
**ac
);
118 void ocfs2_free_ac_resource(struct ocfs2_alloc_context
*ac
)
120 struct inode
*inode
= ac
->ac_inode
;
123 if (ac
->ac_which
!= OCFS2_AC_USE_LOCAL
)
124 ocfs2_inode_unlock(inode
, 1);
126 mutex_unlock(&inode
->i_mutex
);
135 void ocfs2_free_alloc_context(struct ocfs2_alloc_context
*ac
)
137 ocfs2_free_ac_resource(ac
);
141 static u32
ocfs2_bits_per_group(struct ocfs2_chain_list
*cl
)
143 return (u32
)le16_to_cpu(cl
->cl_cpg
) * (u32
)le16_to_cpu(cl
->cl_bpc
);
146 #define do_error(fmt, ...) \
149 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
151 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
154 static int ocfs2_validate_gd_self(struct super_block
*sb
,
155 struct buffer_head
*bh
,
158 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
160 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
161 do_error("Group descriptor #%llu has bad signature %.*s",
162 (unsigned long long)bh
->b_blocknr
, 7,
167 if (le64_to_cpu(gd
->bg_blkno
) != bh
->b_blocknr
) {
168 do_error("Group descriptor #%llu has an invalid bg_blkno "
170 (unsigned long long)bh
->b_blocknr
,
171 (unsigned long long)le64_to_cpu(gd
->bg_blkno
));
175 if (le32_to_cpu(gd
->bg_generation
) != OCFS2_SB(sb
)->fs_generation
) {
176 do_error("Group descriptor #%llu has an invalid "
177 "fs_generation of #%u",
178 (unsigned long long)bh
->b_blocknr
,
179 le32_to_cpu(gd
->bg_generation
));
183 if (le16_to_cpu(gd
->bg_free_bits_count
) > le16_to_cpu(gd
->bg_bits
)) {
184 do_error("Group descriptor #%llu has bit count %u but "
185 "claims that %u are free",
186 (unsigned long long)bh
->b_blocknr
,
187 le16_to_cpu(gd
->bg_bits
),
188 le16_to_cpu(gd
->bg_free_bits_count
));
192 if (le16_to_cpu(gd
->bg_bits
) > (8 * le16_to_cpu(gd
->bg_size
))) {
193 do_error("Group descriptor #%llu has bit count %u but "
194 "max bitmap bits of %u",
195 (unsigned long long)bh
->b_blocknr
,
196 le16_to_cpu(gd
->bg_bits
),
197 8 * le16_to_cpu(gd
->bg_size
));
204 static int ocfs2_validate_gd_parent(struct super_block
*sb
,
205 struct ocfs2_dinode
*di
,
206 struct buffer_head
*bh
,
209 unsigned int max_bits
;
210 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
212 if (di
->i_blkno
!= gd
->bg_parent_dinode
) {
213 do_error("Group descriptor #%llu has bad parent "
214 "pointer (%llu, expected %llu)",
215 (unsigned long long)bh
->b_blocknr
,
216 (unsigned long long)le64_to_cpu(gd
->bg_parent_dinode
),
217 (unsigned long long)le64_to_cpu(di
->i_blkno
));
221 max_bits
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
) * le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
222 if (le16_to_cpu(gd
->bg_bits
) > max_bits
) {
223 do_error("Group descriptor #%llu has bit count of %u",
224 (unsigned long long)bh
->b_blocknr
,
225 le16_to_cpu(gd
->bg_bits
));
229 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
230 if ((le16_to_cpu(gd
->bg_chain
) >
231 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) ||
232 ((le16_to_cpu(gd
->bg_chain
) ==
233 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) && !resize
)) {
234 do_error("Group descriptor #%llu has bad chain %u",
235 (unsigned long long)bh
->b_blocknr
,
236 le16_to_cpu(gd
->bg_chain
));
246 * This version only prints errors. It does not fail the filesystem, and
247 * exists only for resize.
249 int ocfs2_check_group_descriptor(struct super_block
*sb
,
250 struct ocfs2_dinode
*di
,
251 struct buffer_head
*bh
)
254 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
256 BUG_ON(!buffer_uptodate(bh
));
259 * If the ecc fails, we return the error but otherwise
260 * leave the filesystem running. We know any error is
261 * local to this block.
263 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &gd
->bg_check
);
266 "Checksum failed for group descriptor %llu\n",
267 (unsigned long long)bh
->b_blocknr
);
269 rc
= ocfs2_validate_gd_self(sb
, bh
, 1);
271 rc
= ocfs2_validate_gd_parent(sb
, di
, bh
, 1);
276 static int ocfs2_validate_group_descriptor(struct super_block
*sb
,
277 struct buffer_head
*bh
)
280 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
282 mlog(0, "Validating group descriptor %llu\n",
283 (unsigned long long)bh
->b_blocknr
);
285 BUG_ON(!buffer_uptodate(bh
));
288 * If the ecc fails, we return the error but otherwise
289 * leave the filesystem running. We know any error is
290 * local to this block.
292 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &gd
->bg_check
);
297 * Errors after here are fatal.
300 return ocfs2_validate_gd_self(sb
, bh
, 0);
303 int ocfs2_read_group_descriptor(struct inode
*inode
, struct ocfs2_dinode
*di
,
304 u64 gd_blkno
, struct buffer_head
**bh
)
307 struct buffer_head
*tmp
= *bh
;
309 rc
= ocfs2_read_block(INODE_CACHE(inode
), gd_blkno
, &tmp
,
310 ocfs2_validate_group_descriptor
);
314 rc
= ocfs2_validate_gd_parent(inode
->i_sb
, di
, tmp
, 0);
320 /* If ocfs2_read_block() got us a new bh, pass it up. */
328 static int ocfs2_block_group_fill(handle_t
*handle
,
329 struct inode
*alloc_inode
,
330 struct buffer_head
*bg_bh
,
333 struct ocfs2_chain_list
*cl
)
336 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
337 struct super_block
* sb
= alloc_inode
->i_sb
;
341 if (((unsigned long long) bg_bh
->b_blocknr
) != group_blkno
) {
342 ocfs2_error(alloc_inode
->i_sb
, "group block (%llu) != "
344 (unsigned long long)group_blkno
,
345 (unsigned long long) bg_bh
->b_blocknr
);
350 status
= ocfs2_journal_access_gd(handle
,
351 INODE_CACHE(alloc_inode
),
353 OCFS2_JOURNAL_ACCESS_CREATE
);
359 memset(bg
, 0, sb
->s_blocksize
);
360 strcpy(bg
->bg_signature
, OCFS2_GROUP_DESC_SIGNATURE
);
361 bg
->bg_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
362 bg
->bg_size
= cpu_to_le16(ocfs2_group_bitmap_size(sb
));
363 bg
->bg_bits
= cpu_to_le16(ocfs2_bits_per_group(cl
));
364 bg
->bg_chain
= cpu_to_le16(my_chain
);
365 bg
->bg_next_group
= cl
->cl_recs
[my_chain
].c_blkno
;
366 bg
->bg_parent_dinode
= cpu_to_le64(OCFS2_I(alloc_inode
)->ip_blkno
);
367 bg
->bg_blkno
= cpu_to_le64(group_blkno
);
368 /* set the 1st bit in the bitmap to account for the descriptor block */
369 ocfs2_set_bit(0, (unsigned long *)bg
->bg_bitmap
);
370 bg
->bg_free_bits_count
= cpu_to_le16(le16_to_cpu(bg
->bg_bits
) - 1);
372 status
= ocfs2_journal_dirty(handle
, bg_bh
);
376 /* There is no need to zero out or otherwise initialize the
377 * other blocks in a group - All valid FS metadata in a block
378 * group stores the superblock fs_generation value at
379 * allocation time. */
386 static inline u16
ocfs2_find_smallest_chain(struct ocfs2_chain_list
*cl
)
391 while (curr
< le16_to_cpu(cl
->cl_count
)) {
392 if (le32_to_cpu(cl
->cl_recs
[best
].c_total
) >
393 le32_to_cpu(cl
->cl_recs
[curr
].c_total
))
401 * We expect the block group allocator to already be locked.
403 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
404 struct inode
*alloc_inode
,
405 struct buffer_head
*bh
,
407 u64
*last_alloc_group
,
411 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) bh
->b_data
;
412 struct ocfs2_chain_list
*cl
;
413 struct ocfs2_alloc_context
*ac
= NULL
;
414 handle_t
*handle
= NULL
;
415 u32 bit_off
, num_bits
;
418 struct buffer_head
*bg_bh
= NULL
;
419 struct ocfs2_group_desc
*bg
;
421 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode
));
425 cl
= &fe
->id2
.i_chain
;
426 status
= ocfs2_reserve_clusters_with_limit(osb
,
427 le16_to_cpu(cl
->cl_cpg
),
428 max_block
, flags
, &ac
);
430 if (status
!= -ENOSPC
)
435 credits
= ocfs2_calc_group_alloc_credits(osb
->sb
,
436 le16_to_cpu(cl
->cl_cpg
));
437 handle
= ocfs2_start_trans(osb
, credits
);
438 if (IS_ERR(handle
)) {
439 status
= PTR_ERR(handle
);
445 if (last_alloc_group
&& *last_alloc_group
!= 0) {
446 mlog(0, "use old allocation group %llu for block group alloc\n",
447 (unsigned long long)*last_alloc_group
);
448 ac
->ac_last_group
= *last_alloc_group
;
450 status
= ocfs2_claim_clusters(osb
,
453 le16_to_cpu(cl
->cl_cpg
),
457 if (status
!= -ENOSPC
)
462 alloc_rec
= ocfs2_find_smallest_chain(cl
);
464 /* setup the group */
465 bg_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
466 mlog(0, "new descriptor, record %u, at block %llu\n",
467 alloc_rec
, (unsigned long long)bg_blkno
);
469 bg_bh
= sb_getblk(osb
->sb
, bg_blkno
);
475 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode
), bg_bh
);
477 status
= ocfs2_block_group_fill(handle
,
488 bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
490 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(alloc_inode
),
491 bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
497 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_free
,
498 le16_to_cpu(bg
->bg_free_bits_count
));
499 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_total
, le16_to_cpu(bg
->bg_bits
));
500 cl
->cl_recs
[alloc_rec
].c_blkno
= cpu_to_le64(bg_blkno
);
501 if (le16_to_cpu(cl
->cl_next_free_rec
) < le16_to_cpu(cl
->cl_count
))
502 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
504 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, le16_to_cpu(bg
->bg_bits
) -
505 le16_to_cpu(bg
->bg_free_bits_count
));
506 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, le16_to_cpu(bg
->bg_bits
));
507 le32_add_cpu(&fe
->i_clusters
, le16_to_cpu(cl
->cl_cpg
));
509 status
= ocfs2_journal_dirty(handle
, bh
);
515 spin_lock(&OCFS2_I(alloc_inode
)->ip_lock
);
516 OCFS2_I(alloc_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
517 fe
->i_size
= cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode
->i_sb
,
518 le32_to_cpu(fe
->i_clusters
)));
519 spin_unlock(&OCFS2_I(alloc_inode
)->ip_lock
);
520 i_size_write(alloc_inode
, le64_to_cpu(fe
->i_size
));
521 alloc_inode
->i_blocks
= ocfs2_inode_sector_count(alloc_inode
);
525 /* save the new last alloc group so that the caller can cache it. */
526 if (last_alloc_group
)
527 *last_alloc_group
= ac
->ac_last_group
;
531 ocfs2_commit_trans(osb
, handle
);
534 ocfs2_free_alloc_context(ac
);
542 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super
*osb
,
543 struct ocfs2_alloc_context
*ac
,
546 u64
*last_alloc_group
,
550 u32 bits_wanted
= ac
->ac_bits_wanted
;
551 struct inode
*alloc_inode
;
552 struct buffer_head
*bh
= NULL
;
553 struct ocfs2_dinode
*fe
;
558 alloc_inode
= ocfs2_get_system_file_inode(osb
, type
, slot
);
564 mutex_lock(&alloc_inode
->i_mutex
);
566 status
= ocfs2_inode_lock(alloc_inode
, &bh
, 1);
568 mutex_unlock(&alloc_inode
->i_mutex
);
575 ac
->ac_inode
= alloc_inode
;
576 ac
->ac_alloc_slot
= slot
;
578 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
580 /* The bh was validated by the inode read inside
581 * ocfs2_inode_lock(). Any corruption is a code bug. */
582 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
584 if (!(fe
->i_flags
& cpu_to_le32(OCFS2_CHAIN_FL
))) {
585 ocfs2_error(alloc_inode
->i_sb
, "Invalid chain allocator %llu",
586 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
591 free_bits
= le32_to_cpu(fe
->id1
.bitmap1
.i_total
) -
592 le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
594 if (bits_wanted
> free_bits
) {
595 /* cluster bitmap never grows */
596 if (ocfs2_is_cluster_bitmap(alloc_inode
)) {
597 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
598 bits_wanted
, free_bits
);
603 if (!(flags
& ALLOC_NEW_GROUP
)) {
604 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
605 "and we don't alloc a new group for it.\n",
606 slot
, bits_wanted
, free_bits
);
611 status
= ocfs2_block_group_alloc(osb
, alloc_inode
, bh
,
613 last_alloc_group
, flags
);
615 if (status
!= -ENOSPC
)
619 atomic_inc(&osb
->alloc_stats
.bg_extends
);
621 /* You should never ask for this much metadata */
623 (le32_to_cpu(fe
->id1
.bitmap1
.i_total
)
624 - le32_to_cpu(fe
->id1
.bitmap1
.i_used
)));
636 static void ocfs2_init_inode_steal_slot(struct ocfs2_super
*osb
)
638 spin_lock(&osb
->osb_lock
);
639 osb
->s_inode_steal_slot
= OCFS2_INVALID_SLOT
;
640 spin_unlock(&osb
->osb_lock
);
641 atomic_set(&osb
->s_num_inodes_stolen
, 0);
644 static void ocfs2_init_meta_steal_slot(struct ocfs2_super
*osb
)
646 spin_lock(&osb
->osb_lock
);
647 osb
->s_meta_steal_slot
= OCFS2_INVALID_SLOT
;
648 spin_unlock(&osb
->osb_lock
);
649 atomic_set(&osb
->s_num_meta_stolen
, 0);
652 void ocfs2_init_steal_slots(struct ocfs2_super
*osb
)
654 ocfs2_init_inode_steal_slot(osb
);
655 ocfs2_init_meta_steal_slot(osb
);
658 static void __ocfs2_set_steal_slot(struct ocfs2_super
*osb
, int slot
, int type
)
660 spin_lock(&osb
->osb_lock
);
661 if (type
== INODE_ALLOC_SYSTEM_INODE
)
662 osb
->s_inode_steal_slot
= slot
;
663 else if (type
== EXTENT_ALLOC_SYSTEM_INODE
)
664 osb
->s_meta_steal_slot
= slot
;
665 spin_unlock(&osb
->osb_lock
);
668 static int __ocfs2_get_steal_slot(struct ocfs2_super
*osb
, int type
)
670 int slot
= OCFS2_INVALID_SLOT
;
672 spin_lock(&osb
->osb_lock
);
673 if (type
== INODE_ALLOC_SYSTEM_INODE
)
674 slot
= osb
->s_inode_steal_slot
;
675 else if (type
== EXTENT_ALLOC_SYSTEM_INODE
)
676 slot
= osb
->s_meta_steal_slot
;
677 spin_unlock(&osb
->osb_lock
);
682 static int ocfs2_get_inode_steal_slot(struct ocfs2_super
*osb
)
684 return __ocfs2_get_steal_slot(osb
, INODE_ALLOC_SYSTEM_INODE
);
687 static int ocfs2_get_meta_steal_slot(struct ocfs2_super
*osb
)
689 return __ocfs2_get_steal_slot(osb
, EXTENT_ALLOC_SYSTEM_INODE
);
692 static int ocfs2_steal_resource(struct ocfs2_super
*osb
,
693 struct ocfs2_alloc_context
*ac
,
696 int i
, status
= -ENOSPC
;
697 int slot
= __ocfs2_get_steal_slot(osb
, type
);
699 /* Start to steal resource from the first slot after ours. */
700 if (slot
== OCFS2_INVALID_SLOT
)
701 slot
= osb
->slot_num
+ 1;
703 for (i
= 0; i
< osb
->max_slots
; i
++, slot
++) {
704 if (slot
== osb
->max_slots
)
707 if (slot
== osb
->slot_num
)
710 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
713 NOT_ALLOC_NEW_GROUP
);
715 __ocfs2_set_steal_slot(osb
, slot
, type
);
719 ocfs2_free_ac_resource(ac
);
725 static int ocfs2_steal_inode(struct ocfs2_super
*osb
,
726 struct ocfs2_alloc_context
*ac
)
728 return ocfs2_steal_resource(osb
, ac
, INODE_ALLOC_SYSTEM_INODE
);
731 static int ocfs2_steal_meta(struct ocfs2_super
*osb
,
732 struct ocfs2_alloc_context
*ac
)
734 return ocfs2_steal_resource(osb
, ac
, EXTENT_ALLOC_SYSTEM_INODE
);
737 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super
*osb
,
739 struct ocfs2_alloc_context
**ac
)
742 int slot
= ocfs2_get_meta_steal_slot(osb
);
744 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
751 (*ac
)->ac_bits_wanted
= blocks
;
752 (*ac
)->ac_which
= OCFS2_AC_USE_META
;
753 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
755 if (slot
!= OCFS2_INVALID_SLOT
&&
756 atomic_read(&osb
->s_num_meta_stolen
) < OCFS2_MAX_TO_STEAL
)
759 atomic_set(&osb
->s_num_meta_stolen
, 0);
760 status
= ocfs2_reserve_suballoc_bits(osb
, (*ac
),
761 EXTENT_ALLOC_SYSTEM_INODE
,
762 (u32
)osb
->slot_num
, NULL
,
768 if (slot
!= OCFS2_INVALID_SLOT
)
769 ocfs2_init_meta_steal_slot(osb
);
771 } else if (status
< 0 && status
!= -ENOSPC
) {
776 ocfs2_free_ac_resource(*ac
);
779 status
= ocfs2_steal_meta(osb
, *ac
);
780 atomic_inc(&osb
->s_num_meta_stolen
);
782 if (status
!= -ENOSPC
)
789 if ((status
< 0) && *ac
) {
790 ocfs2_free_alloc_context(*ac
);
798 int ocfs2_reserve_new_metadata(struct ocfs2_super
*osb
,
799 struct ocfs2_extent_list
*root_el
,
800 struct ocfs2_alloc_context
**ac
)
802 return ocfs2_reserve_new_metadata_blocks(osb
,
803 ocfs2_extend_meta_needed(root_el
),
807 int ocfs2_reserve_new_inode(struct ocfs2_super
*osb
,
808 struct ocfs2_alloc_context
**ac
)
811 int slot
= ocfs2_get_inode_steal_slot(osb
);
814 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
821 (*ac
)->ac_bits_wanted
= 1;
822 (*ac
)->ac_which
= OCFS2_AC_USE_INODE
;
824 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
827 * stat(2) can't handle i_ino > 32bits, so we tell the
828 * lower levels not to allocate us a block group past that
829 * limit. The 'inode64' mount option avoids this behavior.
831 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_INODE64
))
832 (*ac
)->ac_max_block
= (u32
)~0U;
835 * slot is set when we successfully steal inode from other nodes.
836 * It is reset in 3 places:
837 * 1. when we flush the truncate log
838 * 2. when we complete local alloc recovery.
839 * 3. when we successfully allocate from our own slot.
840 * After it is set, we will go on stealing inodes until we find the
841 * need to check our slots to see whether there is some space for us.
843 if (slot
!= OCFS2_INVALID_SLOT
&&
844 atomic_read(&osb
->s_num_inodes_stolen
) < OCFS2_MAX_TO_STEAL
)
847 atomic_set(&osb
->s_num_inodes_stolen
, 0);
848 alloc_group
= osb
->osb_inode_alloc_group
;
849 status
= ocfs2_reserve_suballoc_bits(osb
, *ac
,
850 INODE_ALLOC_SYSTEM_INODE
,
854 ALLOC_GROUPS_FROM_GLOBAL
);
858 spin_lock(&osb
->osb_lock
);
859 osb
->osb_inode_alloc_group
= alloc_group
;
860 spin_unlock(&osb
->osb_lock
);
861 mlog(0, "after reservation, new allocation group is "
862 "%llu\n", (unsigned long long)alloc_group
);
865 * Some inodes must be freed by us, so try to allocate
866 * from our own next time.
868 if (slot
!= OCFS2_INVALID_SLOT
)
869 ocfs2_init_inode_steal_slot(osb
);
871 } else if (status
< 0 && status
!= -ENOSPC
) {
876 ocfs2_free_ac_resource(*ac
);
879 status
= ocfs2_steal_inode(osb
, *ac
);
880 atomic_inc(&osb
->s_num_inodes_stolen
);
882 if (status
!= -ENOSPC
)
889 if ((status
< 0) && *ac
) {
890 ocfs2_free_alloc_context(*ac
);
898 /* local alloc code has to do the same thing, so rather than do this
900 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super
*osb
,
901 struct ocfs2_alloc_context
*ac
)
905 ac
->ac_which
= OCFS2_AC_USE_MAIN
;
906 ac
->ac_group_search
= ocfs2_cluster_group_search
;
908 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
909 GLOBAL_BITMAP_SYSTEM_INODE
,
910 OCFS2_INVALID_SLOT
, NULL
,
912 if (status
< 0 && status
!= -ENOSPC
) {
921 /* Callers don't need to care which bitmap (local alloc or main) to
922 * use so we figure it out for them, but unfortunately this clutters
924 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super
*osb
,
925 u32 bits_wanted
, u64 max_block
,
927 struct ocfs2_alloc_context
**ac
)
933 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
940 (*ac
)->ac_bits_wanted
= bits_wanted
;
941 (*ac
)->ac_max_block
= max_block
;
944 if (!(flags
& ALLOC_GROUPS_FROM_GLOBAL
) &&
945 ocfs2_alloc_should_use_local(osb
, bits_wanted
)) {
946 status
= ocfs2_reserve_local_alloc_bits(osb
,
949 if (status
== -EFBIG
) {
950 /* The local alloc window is outside ac_max_block.
951 * use the main bitmap. */
953 } else if ((status
< 0) && (status
!= -ENOSPC
)) {
959 if (status
== -ENOSPC
) {
960 status
= ocfs2_reserve_cluster_bitmap_bits(osb
, *ac
);
962 if (status
!= -ENOSPC
)
970 if ((status
< 0) && *ac
) {
971 ocfs2_free_alloc_context(*ac
);
979 int ocfs2_reserve_clusters(struct ocfs2_super
*osb
,
981 struct ocfs2_alloc_context
**ac
)
983 return ocfs2_reserve_clusters_with_limit(osb
, bits_wanted
, 0,
984 ALLOC_NEW_GROUP
, ac
);
988 * More or less lifted from ext3. I'll leave their description below:
990 * "For ext3 allocations, we must not reuse any blocks which are
991 * allocated in the bitmap buffer's "last committed data" copy. This
992 * prevents deletes from freeing up the page for reuse until we have
993 * committed the delete transaction.
995 * If we didn't do this, then deleting something and reallocating it as
996 * data would allow the old block to be overwritten before the
997 * transaction committed (because we force data to disk before commit).
998 * This would lead to corruption if we crashed between overwriting the
999 * data and committing the delete.
1001 * @@@ We may want to make this allocation behaviour conditional on
1002 * data-writes at some point, and disable it for metadata allocations or
1003 * sync-data inodes."
1005 * Note: OCFS2 already does this differently for metadata vs data
1006 * allocations, as those bitmaps are separate and undo access is never
1007 * called on a metadata group descriptor.
1009 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
1012 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
1015 if (ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
))
1018 if (!buffer_jbd(bg_bh
))
1021 jbd_lock_bh_state(bg_bh
);
1022 bg
= (struct ocfs2_group_desc
*) bh2jh(bg_bh
)->b_committed_data
;
1024 ret
= !ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
);
1027 jbd_unlock_bh_state(bg_bh
);
1032 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super
*osb
,
1033 struct buffer_head
*bg_bh
,
1034 unsigned int bits_wanted
,
1035 unsigned int total_bits
,
1040 u16 best_offset
, best_size
;
1041 int offset
, start
, found
, status
= 0;
1042 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
1044 /* Callers got this descriptor from
1045 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1046 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1048 found
= start
= best_offset
= best_size
= 0;
1049 bitmap
= bg
->bg_bitmap
;
1051 while((offset
= ocfs2_find_next_zero_bit(bitmap
, total_bits
, start
)) != -1) {
1052 if (offset
== total_bits
)
1055 if (!ocfs2_test_bg_bit_allocatable(bg_bh
, offset
)) {
1056 /* We found a zero, but we can't use it as it
1057 * hasn't been put to disk yet! */
1060 } else if (offset
== start
) {
1061 /* we found a zero */
1063 /* move start to the next bit to test */
1066 /* got a zero after some ones */
1070 if (found
> best_size
) {
1072 best_offset
= start
- found
;
1074 /* we got everything we needed */
1075 if (found
== bits_wanted
) {
1076 /* mlog(0, "Found it all!\n"); */
1081 /* XXX: I think the first clause is equivalent to the second
1083 if (found
== bits_wanted
) {
1084 *bit_off
= start
- found
;
1085 *bits_found
= found
;
1086 } else if (best_size
) {
1087 *bit_off
= best_offset
;
1088 *bits_found
= best_size
;
1091 /* No error log here -- see the comment above
1092 * ocfs2_test_bg_bit_allocatable */
1098 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
1099 struct inode
*alloc_inode
,
1100 struct ocfs2_group_desc
*bg
,
1101 struct buffer_head
*group_bh
,
1102 unsigned int bit_off
,
1103 unsigned int num_bits
)
1106 void *bitmap
= bg
->bg_bitmap
;
1107 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1111 /* All callers get the descriptor via
1112 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1113 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1114 BUG_ON(le16_to_cpu(bg
->bg_free_bits_count
) < num_bits
);
1116 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off
,
1119 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1120 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1122 status
= ocfs2_journal_access_gd(handle
,
1123 INODE_CACHE(alloc_inode
),
1131 le16_add_cpu(&bg
->bg_free_bits_count
, -num_bits
);
1134 ocfs2_set_bit(bit_off
++, bitmap
);
1136 status
= ocfs2_journal_dirty(handle
,
1148 /* find the one with the most empty bits */
1149 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
)
1153 BUG_ON(!cl
->cl_next_free_rec
);
1156 while (curr
< le16_to_cpu(cl
->cl_next_free_rec
)) {
1157 if (le32_to_cpu(cl
->cl_recs
[curr
].c_free
) >
1158 le32_to_cpu(cl
->cl_recs
[best
].c_free
))
1163 BUG_ON(best
>= le16_to_cpu(cl
->cl_next_free_rec
));
1167 static int ocfs2_relink_block_group(handle_t
*handle
,
1168 struct inode
*alloc_inode
,
1169 struct buffer_head
*fe_bh
,
1170 struct buffer_head
*bg_bh
,
1171 struct buffer_head
*prev_bg_bh
,
1175 /* there is a really tiny chance the journal calls could fail,
1176 * but we wouldn't want inconsistent blocks in *any* case. */
1177 u64 fe_ptr
, bg_ptr
, prev_bg_ptr
;
1178 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
1179 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
1180 struct ocfs2_group_desc
*prev_bg
= (struct ocfs2_group_desc
*) prev_bg_bh
->b_data
;
1182 /* The caller got these descriptors from
1183 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1184 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1185 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg
));
1187 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
1188 (unsigned long long)le64_to_cpu(fe
->i_blkno
), chain
,
1189 (unsigned long long)le64_to_cpu(bg
->bg_blkno
),
1190 (unsigned long long)le64_to_cpu(prev_bg
->bg_blkno
));
1192 fe_ptr
= le64_to_cpu(fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
);
1193 bg_ptr
= le64_to_cpu(bg
->bg_next_group
);
1194 prev_bg_ptr
= le64_to_cpu(prev_bg
->bg_next_group
);
1196 status
= ocfs2_journal_access_gd(handle
, INODE_CACHE(alloc_inode
),
1198 OCFS2_JOURNAL_ACCESS_WRITE
);
1204 prev_bg
->bg_next_group
= bg
->bg_next_group
;
1206 status
= ocfs2_journal_dirty(handle
, prev_bg_bh
);
1212 status
= ocfs2_journal_access_gd(handle
, INODE_CACHE(alloc_inode
),
1213 bg_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
1219 bg
->bg_next_group
= fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
;
1221 status
= ocfs2_journal_dirty(handle
, bg_bh
);
1227 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(alloc_inode
),
1228 fe_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
1234 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= bg
->bg_blkno
;
1236 status
= ocfs2_journal_dirty(handle
, fe_bh
);
1245 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= cpu_to_le64(fe_ptr
);
1246 bg
->bg_next_group
= cpu_to_le64(bg_ptr
);
1247 prev_bg
->bg_next_group
= cpu_to_le64(prev_bg_ptr
);
1254 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
1257 return le16_to_cpu(bg
->bg_free_bits_count
) > wanted
;
1260 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1261 * value on error. */
1262 static int ocfs2_cluster_group_search(struct inode
*inode
,
1263 struct buffer_head
*group_bh
,
1264 u32 bits_wanted
, u32 min_bits
,
1266 u16
*bit_off
, u16
*bits_found
)
1268 int search
= -ENOSPC
;
1271 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1272 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1273 u16 tmp_off
, tmp_found
;
1274 unsigned int max_bits
, gd_cluster_off
;
1276 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1278 if (gd
->bg_free_bits_count
) {
1279 max_bits
= le16_to_cpu(gd
->bg_bits
);
1281 /* Tail groups in cluster bitmaps which aren't cpg
1282 * aligned are prone to partial extention by a failed
1283 * fs resize. If the file system resize never got to
1284 * update the dinode cluster count, then we don't want
1285 * to trust any clusters past it, regardless of what
1286 * the group descriptor says. */
1287 gd_cluster_off
= ocfs2_blocks_to_clusters(inode
->i_sb
,
1288 le64_to_cpu(gd
->bg_blkno
));
1289 if ((gd_cluster_off
+ max_bits
) >
1290 OCFS2_I(inode
)->ip_clusters
) {
1291 max_bits
= OCFS2_I(inode
)->ip_clusters
- gd_cluster_off
;
1292 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1293 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
1294 le16_to_cpu(gd
->bg_bits
),
1295 OCFS2_I(inode
)->ip_clusters
, max_bits
);
1298 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1299 group_bh
, bits_wanted
,
1301 &tmp_off
, &tmp_found
);
1306 blkoff
= ocfs2_clusters_to_blocks(inode
->i_sb
,
1308 tmp_off
+ tmp_found
);
1309 mlog(0, "Checking %llu against %llu\n",
1310 (unsigned long long)blkoff
,
1311 (unsigned long long)max_block
);
1312 if (blkoff
> max_block
)
1316 /* ocfs2_block_group_find_clear_bits() might
1317 * return success, but we still want to return
1318 * -ENOSPC unless it found the minimum number
1320 if (min_bits
<= tmp_found
) {
1322 *bits_found
= tmp_found
;
1323 search
= 0; /* success */
1324 } else if (tmp_found
) {
1326 * Don't show bits which we'll be returning
1327 * for allocation to the local alloc bitmap.
1329 ocfs2_local_alloc_seen_free_bits(osb
, tmp_found
);
1336 static int ocfs2_block_group_search(struct inode
*inode
,
1337 struct buffer_head
*group_bh
,
1338 u32 bits_wanted
, u32 min_bits
,
1340 u16
*bit_off
, u16
*bits_found
)
1344 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1346 BUG_ON(min_bits
!= 1);
1347 BUG_ON(ocfs2_is_cluster_bitmap(inode
));
1349 if (bg
->bg_free_bits_count
) {
1350 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1351 group_bh
, bits_wanted
,
1352 le16_to_cpu(bg
->bg_bits
),
1353 bit_off
, bits_found
);
1354 if (!ret
&& max_block
) {
1355 blkoff
= le64_to_cpu(bg
->bg_blkno
) + *bit_off
+
1357 mlog(0, "Checking %llu against %llu\n",
1358 (unsigned long long)blkoff
,
1359 (unsigned long long)max_block
);
1360 if (blkoff
> max_block
)
1368 static int ocfs2_alloc_dinode_update_counts(struct inode
*inode
,
1370 struct buffer_head
*di_bh
,
1376 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
1377 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &di
->id2
.i_chain
;
1379 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
1380 OCFS2_JOURNAL_ACCESS_WRITE
);
1386 tmp_used
= le32_to_cpu(di
->id1
.bitmap1
.i_used
);
1387 di
->id1
.bitmap1
.i_used
= cpu_to_le32(num_bits
+ tmp_used
);
1388 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -num_bits
);
1390 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1398 static int ocfs2_search_one_group(struct ocfs2_alloc_context
*ac
,
1403 unsigned int *num_bits
,
1409 struct buffer_head
*group_bh
= NULL
;
1410 struct ocfs2_group_desc
*gd
;
1411 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)ac
->ac_bh
->b_data
;
1412 struct inode
*alloc_inode
= ac
->ac_inode
;
1414 ret
= ocfs2_read_group_descriptor(alloc_inode
, di
, gd_blkno
,
1421 gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1422 ret
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
, min_bits
,
1423 ac
->ac_max_block
, bit_off
, &found
);
1432 ret
= ocfs2_alloc_dinode_update_counts(alloc_inode
, handle
, ac
->ac_bh
,
1434 le16_to_cpu(gd
->bg_chain
));
1440 ret
= ocfs2_block_group_set_bits(handle
, alloc_inode
, gd
, group_bh
,
1441 *bit_off
, *num_bits
);
1445 *bits_left
= le16_to_cpu(gd
->bg_free_bits_count
);
1453 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
1458 unsigned int *num_bits
,
1463 u16 chain
, tmp_bits
;
1466 struct inode
*alloc_inode
= ac
->ac_inode
;
1467 struct buffer_head
*group_bh
= NULL
;
1468 struct buffer_head
*prev_group_bh
= NULL
;
1469 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1470 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1471 struct ocfs2_group_desc
*bg
;
1473 chain
= ac
->ac_chain
;
1474 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1476 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
);
1478 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
,
1479 le64_to_cpu(cl
->cl_recs
[chain
].c_blkno
),
1485 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1488 /* for now, the chain search is a bit simplistic. We just use
1489 * the 1st group with any empty bits. */
1490 while ((status
= ac
->ac_group_search(alloc_inode
, group_bh
,
1491 bits_wanted
, min_bits
,
1492 ac
->ac_max_block
, bit_off
,
1493 &tmp_bits
)) == -ENOSPC
) {
1494 if (!bg
->bg_next_group
)
1497 brelse(prev_group_bh
);
1498 prev_group_bh
= NULL
;
1500 next_group
= le64_to_cpu(bg
->bg_next_group
);
1501 prev_group_bh
= group_bh
;
1503 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
,
1504 next_group
, &group_bh
);
1509 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1512 if (status
!= -ENOSPC
)
1517 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1518 tmp_bits
, (unsigned long long)le64_to_cpu(bg
->bg_blkno
));
1520 *num_bits
= tmp_bits
;
1522 BUG_ON(*num_bits
== 0);
1525 * Keep track of previous block descriptor read. When
1526 * we find a target, if we have read more than X
1527 * number of descriptors, and the target is reasonably
1528 * empty, relink him to top of his chain.
1530 * We've read 0 extra blocks and only send one more to
1531 * the transaction, yet the next guy to search has a
1534 * Do this *after* figuring out how many bits we're taking out
1535 * of our target group.
1537 if (ac
->ac_allow_chain_relink
&&
1539 (ocfs2_block_group_reasonably_empty(bg
, *num_bits
))) {
1540 status
= ocfs2_relink_block_group(handle
, alloc_inode
,
1541 ac
->ac_bh
, group_bh
,
1542 prev_group_bh
, chain
);
1549 /* Ok, claim our bits now: set the info on dinode, chainlist
1550 * and then the group */
1551 status
= ocfs2_journal_access_di(handle
,
1552 INODE_CACHE(alloc_inode
),
1554 OCFS2_JOURNAL_ACCESS_WRITE
);
1560 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1561 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(*num_bits
+ tmp_used
);
1562 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -(*num_bits
));
1564 status
= ocfs2_journal_dirty(handle
,
1571 status
= ocfs2_block_group_set_bits(handle
,
1582 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits
,
1583 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
1585 *bg_blkno
= le64_to_cpu(bg
->bg_blkno
);
1586 *bits_left
= le16_to_cpu(bg
->bg_free_bits_count
);
1589 brelse(prev_group_bh
);
1595 /* will give out up to bits_wanted contiguous bits. */
1596 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
1597 struct ocfs2_alloc_context
*ac
,
1602 unsigned int *num_bits
,
1608 u64 hint_blkno
= ac
->ac_last_group
;
1609 struct ocfs2_chain_list
*cl
;
1610 struct ocfs2_dinode
*fe
;
1614 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1615 BUG_ON(bits_wanted
> (ac
->ac_bits_wanted
- ac
->ac_bits_given
));
1618 fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1620 /* The bh was validated by the inode read during
1621 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1622 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
1624 if (le32_to_cpu(fe
->id1
.bitmap1
.i_used
) >=
1625 le32_to_cpu(fe
->id1
.bitmap1
.i_total
)) {
1626 ocfs2_error(osb
->sb
, "Chain allocator dinode %llu has %u used "
1627 "bits but only %u total.",
1628 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
1629 le32_to_cpu(fe
->id1
.bitmap1
.i_used
),
1630 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1636 /* Attempt to short-circuit the usual search mechanism
1637 * by jumping straight to the most recently used
1638 * allocation group. This helps us mantain some
1639 * contiguousness across allocations. */
1640 status
= ocfs2_search_one_group(ac
, handle
, bits_wanted
,
1641 min_bits
, bit_off
, num_bits
,
1642 hint_blkno
, &bits_left
);
1644 /* Be careful to update *bg_blkno here as the
1645 * caller is expecting it to be filled in, and
1646 * ocfs2_search_one_group() won't do that for
1648 *bg_blkno
= hint_blkno
;
1651 if (status
< 0 && status
!= -ENOSPC
) {
1657 cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1659 victim
= ocfs2_find_victim_chain(cl
);
1660 ac
->ac_chain
= victim
;
1661 ac
->ac_allow_chain_relink
= 1;
1663 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
, bit_off
,
1664 num_bits
, bg_blkno
, &bits_left
);
1667 if (status
< 0 && status
!= -ENOSPC
) {
1672 mlog(0, "Search of victim chain %u came up with nothing, "
1673 "trying all chains now.\n", victim
);
1675 /* If we didn't pick a good victim, then just default to
1676 * searching each chain in order. Don't allow chain relinking
1677 * because we only calculate enough journal credits for one
1678 * relink per alloc. */
1679 ac
->ac_allow_chain_relink
= 0;
1680 for (i
= 0; i
< le16_to_cpu(cl
->cl_next_free_rec
); i
++) {
1683 if (!cl
->cl_recs
[i
].c_free
)
1687 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
,
1688 bit_off
, num_bits
, bg_blkno
,
1692 if (status
< 0 && status
!= -ENOSPC
) {
1699 if (status
!= -ENOSPC
) {
1700 /* If the next search of this group is not likely to
1701 * yield a suitable extent, then we reset the last
1702 * group hint so as to not waste a disk read */
1703 if (bits_left
< min_bits
)
1704 ac
->ac_last_group
= 0;
1706 ac
->ac_last_group
= *bg_blkno
;
1714 int ocfs2_claim_metadata(struct ocfs2_super
*osb
,
1716 struct ocfs2_alloc_context
*ac
,
1718 u16
*suballoc_bit_start
,
1719 unsigned int *num_bits
,
1726 BUG_ON(ac
->ac_bits_wanted
< (ac
->ac_bits_given
+ bits_wanted
));
1727 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_META
);
1729 status
= ocfs2_claim_suballoc_bits(osb
,
1741 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1743 *blkno_start
= bg_blkno
+ (u64
) *suballoc_bit_start
;
1744 ac
->ac_bits_given
+= (*num_bits
);
1751 static void ocfs2_init_inode_ac_group(struct inode
*dir
,
1752 struct buffer_head
*parent_fe_bh
,
1753 struct ocfs2_alloc_context
*ac
)
1755 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*)parent_fe_bh
->b_data
;
1757 * Try to allocate inodes from some specific group.
1759 * If the parent dir has recorded the last group used in allocation,
1760 * cool, use it. Otherwise if we try to allocate new inode from the
1761 * same slot the parent dir belongs to, use the same chunk.
1763 * We are very careful here to avoid the mistake of setting
1764 * ac_last_group to a group descriptor from a different (unlocked) slot.
1766 if (OCFS2_I(dir
)->ip_last_used_group
&&
1767 OCFS2_I(dir
)->ip_last_used_slot
== ac
->ac_alloc_slot
)
1768 ac
->ac_last_group
= OCFS2_I(dir
)->ip_last_used_group
;
1769 else if (le16_to_cpu(fe
->i_suballoc_slot
) == ac
->ac_alloc_slot
)
1770 ac
->ac_last_group
= ocfs2_which_suballoc_group(
1771 le64_to_cpu(fe
->i_blkno
),
1772 le16_to_cpu(fe
->i_suballoc_bit
));
1775 static inline void ocfs2_save_inode_ac_group(struct inode
*dir
,
1776 struct ocfs2_alloc_context
*ac
)
1778 OCFS2_I(dir
)->ip_last_used_group
= ac
->ac_last_group
;
1779 OCFS2_I(dir
)->ip_last_used_slot
= ac
->ac_alloc_slot
;
1782 int ocfs2_claim_new_inode(struct ocfs2_super
*osb
,
1785 struct buffer_head
*parent_fe_bh
,
1786 struct ocfs2_alloc_context
*ac
,
1791 unsigned int num_bits
;
1797 BUG_ON(ac
->ac_bits_given
!= 0);
1798 BUG_ON(ac
->ac_bits_wanted
!= 1);
1799 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_INODE
);
1801 ocfs2_init_inode_ac_group(dir
, parent_fe_bh
, ac
);
1803 status
= ocfs2_claim_suballoc_bits(osb
,
1815 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1817 BUG_ON(num_bits
!= 1);
1819 *fe_blkno
= bg_blkno
+ (u64
) (*suballoc_bit
);
1820 ac
->ac_bits_given
++;
1821 ocfs2_save_inode_ac_group(dir
, ac
);
1828 /* translate a group desc. blkno and it's bitmap offset into
1829 * disk cluster offset. */
1830 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
1834 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1837 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1839 if (bg_blkno
!= osb
->first_cluster_group_blkno
)
1840 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, bg_blkno
);
1841 cluster
+= (u32
) bg_bit_off
;
1845 /* given a cluster offset, calculate which block group it belongs to
1846 * and return that block offset. */
1847 u64
ocfs2_which_cluster_group(struct inode
*inode
, u32 cluster
)
1849 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1852 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1854 group_no
= cluster
/ osb
->bitmap_cpg
;
1856 return osb
->first_cluster_group_blkno
;
1857 return ocfs2_clusters_to_blocks(inode
->i_sb
,
1858 group_no
* osb
->bitmap_cpg
);
1861 /* given the block number of a cluster start, calculate which cluster
1862 * group and descriptor bitmap offset that corresponds to. */
1863 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
1868 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1869 u32 data_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, data_blkno
);
1871 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1873 *bg_blkno
= ocfs2_which_cluster_group(inode
,
1876 if (*bg_blkno
== osb
->first_cluster_group_blkno
)
1877 *bg_bit_off
= (u16
) data_cluster
;
1879 *bg_bit_off
= (u16
) ocfs2_blocks_to_clusters(osb
->sb
,
1880 data_blkno
- *bg_blkno
);
1884 * min_bits - minimum contiguous chunk from this total allocation we
1885 * can handle. set to what we asked for originally for a full
1886 * contig. allocation, set to '1' to indicate we can deal with extents
1889 int __ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1891 struct ocfs2_alloc_context
*ac
,
1898 unsigned int bits_wanted
= max_clusters
;
1904 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1906 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_LOCAL
1907 && ac
->ac_which
!= OCFS2_AC_USE_MAIN
);
1909 if (ac
->ac_which
== OCFS2_AC_USE_LOCAL
) {
1910 status
= ocfs2_claim_local_alloc_bits(osb
,
1917 atomic_inc(&osb
->alloc_stats
.local_data
);
1919 if (min_clusters
> (osb
->bitmap_cpg
- 1)) {
1920 /* The only paths asking for contiguousness
1921 * should know about this already. */
1922 mlog(ML_ERROR
, "minimum allocation requested %u exceeds "
1923 "group bitmap size %u!\n", min_clusters
,
1928 /* clamp the current request down to a realistic size. */
1929 if (bits_wanted
> (osb
->bitmap_cpg
- 1))
1930 bits_wanted
= osb
->bitmap_cpg
- 1;
1932 status
= ocfs2_claim_suballoc_bits(osb
,
1942 ocfs2_desc_bitmap_to_cluster_off(ac
->ac_inode
,
1945 atomic_inc(&osb
->alloc_stats
.bitmap_data
);
1949 if (status
!= -ENOSPC
)
1954 ac
->ac_bits_given
+= *num_clusters
;
1961 int ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1963 struct ocfs2_alloc_context
*ac
,
1968 unsigned int bits_wanted
= ac
->ac_bits_wanted
- ac
->ac_bits_given
;
1970 return __ocfs2_claim_clusters(osb
, handle
, ac
, min_clusters
,
1971 bits_wanted
, cluster_start
, num_clusters
);
1974 static int ocfs2_block_group_clear_bits(handle_t
*handle
,
1975 struct inode
*alloc_inode
,
1976 struct ocfs2_group_desc
*bg
,
1977 struct buffer_head
*group_bh
,
1978 unsigned int bit_off
,
1979 unsigned int num_bits
,
1980 void (*undo_fn
)(unsigned int bit
,
1981 unsigned long *bmap
))
1985 struct ocfs2_group_desc
*undo_bg
= NULL
;
1989 /* The caller got this descriptor from
1990 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1991 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1993 mlog(0, "off = %u, num = %u\n", bit_off
, num_bits
);
1995 BUG_ON(undo_fn
&& !ocfs2_is_cluster_bitmap(alloc_inode
));
1996 status
= ocfs2_journal_access_gd(handle
, INODE_CACHE(alloc_inode
),
1999 OCFS2_JOURNAL_ACCESS_UNDO
:
2000 OCFS2_JOURNAL_ACCESS_WRITE
);
2007 jbd_lock_bh_state(group_bh
);
2008 undo_bg
= (struct ocfs2_group_desc
*)
2009 bh2jh(group_bh
)->b_committed_data
;
2015 ocfs2_clear_bit((bit_off
+ tmp
),
2016 (unsigned long *) bg
->bg_bitmap
);
2018 undo_fn(bit_off
+ tmp
,
2019 (unsigned long *) undo_bg
->bg_bitmap
);
2021 le16_add_cpu(&bg
->bg_free_bits_count
, num_bits
);
2024 jbd_unlock_bh_state(group_bh
);
2026 status
= ocfs2_journal_dirty(handle
, group_bh
);
2034 * expects the suballoc inode to already be locked.
2036 static int _ocfs2_free_suballoc_bits(handle_t
*handle
,
2037 struct inode
*alloc_inode
,
2038 struct buffer_head
*alloc_bh
,
2039 unsigned int start_bit
,
2042 void (*undo_fn
)(unsigned int bit
,
2043 unsigned long *bitmap
))
2047 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) alloc_bh
->b_data
;
2048 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
2049 struct buffer_head
*group_bh
= NULL
;
2050 struct ocfs2_group_desc
*group
;
2054 /* The alloc_bh comes from ocfs2_free_dinode() or
2055 * ocfs2_free_clusters(). The callers have all locked the
2056 * allocator and gotten alloc_bh from the lock call. This
2057 * validates the dinode buffer. Any corruption that has happended
2059 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
2060 BUG_ON((count
+ start_bit
) > ocfs2_bits_per_group(cl
));
2062 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2063 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
, count
,
2064 (unsigned long long)bg_blkno
, start_bit
);
2066 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
, bg_blkno
,
2072 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
2074 BUG_ON((count
+ start_bit
) > le16_to_cpu(group
->bg_bits
));
2076 status
= ocfs2_block_group_clear_bits(handle
, alloc_inode
,
2078 start_bit
, count
, undo_fn
);
2084 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(alloc_inode
),
2085 alloc_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
2091 le32_add_cpu(&cl
->cl_recs
[le16_to_cpu(group
->bg_chain
)].c_free
,
2093 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
2094 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(tmp_used
- count
);
2096 status
= ocfs2_journal_dirty(handle
, alloc_bh
);
2109 int ocfs2_free_suballoc_bits(handle_t
*handle
,
2110 struct inode
*alloc_inode
,
2111 struct buffer_head
*alloc_bh
,
2112 unsigned int start_bit
,
2116 return _ocfs2_free_suballoc_bits(handle
, alloc_inode
, alloc_bh
,
2117 start_bit
, bg_blkno
, count
, NULL
);
2120 int ocfs2_free_dinode(handle_t
*handle
,
2121 struct inode
*inode_alloc_inode
,
2122 struct buffer_head
*inode_alloc_bh
,
2123 struct ocfs2_dinode
*di
)
2125 u64 blk
= le64_to_cpu(di
->i_blkno
);
2126 u16 bit
= le16_to_cpu(di
->i_suballoc_bit
);
2127 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
2129 return ocfs2_free_suballoc_bits(handle
, inode_alloc_inode
,
2130 inode_alloc_bh
, bit
, bg_blkno
, 1);
2133 static int _ocfs2_free_clusters(handle_t
*handle
,
2134 struct inode
*bitmap_inode
,
2135 struct buffer_head
*bitmap_bh
,
2137 unsigned int num_clusters
,
2138 void (*undo_fn
)(unsigned int bit
,
2139 unsigned long *bitmap
))
2144 struct ocfs2_dinode
*fe
;
2146 /* You can't ever have a contiguous set of clusters
2147 * bigger than a block group bitmap so we never have to worry
2148 * about looping on them. */
2152 /* This is expensive. We can safely remove once this stuff has
2153 * gotten tested really well. */
2154 BUG_ON(start_blk
!= ocfs2_clusters_to_blocks(bitmap_inode
->i_sb
, ocfs2_blocks_to_clusters(bitmap_inode
->i_sb
, start_blk
)));
2156 fe
= (struct ocfs2_dinode
*) bitmap_bh
->b_data
;
2158 ocfs2_block_to_cluster_group(bitmap_inode
, start_blk
, &bg_blkno
,
2161 mlog(0, "want to free %u clusters starting at block %llu\n",
2162 num_clusters
, (unsigned long long)start_blk
);
2163 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2164 (unsigned long long)bg_blkno
, bg_start_bit
);
2166 status
= _ocfs2_free_suballoc_bits(handle
, bitmap_inode
, bitmap_bh
,
2167 bg_start_bit
, bg_blkno
,
2168 num_clusters
, undo_fn
);
2174 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode
->i_sb
),
2182 int ocfs2_free_clusters(handle_t
*handle
,
2183 struct inode
*bitmap_inode
,
2184 struct buffer_head
*bitmap_bh
,
2186 unsigned int num_clusters
)
2188 return _ocfs2_free_clusters(handle
, bitmap_inode
, bitmap_bh
,
2189 start_blk
, num_clusters
,
2194 * Give never-used clusters back to the global bitmap. We don't need
2195 * to protect these bits in the undo buffer.
2197 int ocfs2_release_clusters(handle_t
*handle
,
2198 struct inode
*bitmap_inode
,
2199 struct buffer_head
*bitmap_bh
,
2201 unsigned int num_clusters
)
2203 return _ocfs2_free_clusters(handle
, bitmap_inode
, bitmap_bh
,
2204 start_blk
, num_clusters
,
2208 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
)
2210 printk("Block Group:\n");
2211 printk("bg_signature: %s\n", bg
->bg_signature
);
2212 printk("bg_size: %u\n", bg
->bg_size
);
2213 printk("bg_bits: %u\n", bg
->bg_bits
);
2214 printk("bg_free_bits_count: %u\n", bg
->bg_free_bits_count
);
2215 printk("bg_chain: %u\n", bg
->bg_chain
);
2216 printk("bg_generation: %u\n", le32_to_cpu(bg
->bg_generation
));
2217 printk("bg_next_group: %llu\n",
2218 (unsigned long long)bg
->bg_next_group
);
2219 printk("bg_parent_dinode: %llu\n",
2220 (unsigned long long)bg
->bg_parent_dinode
);
2221 printk("bg_blkno: %llu\n",
2222 (unsigned long long)bg
->bg_blkno
);
2225 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
)
2229 printk("Suballoc Inode %llu:\n", (unsigned long long)fe
->i_blkno
);
2230 printk("i_signature: %s\n", fe
->i_signature
);
2231 printk("i_size: %llu\n",
2232 (unsigned long long)fe
->i_size
);
2233 printk("i_clusters: %u\n", fe
->i_clusters
);
2234 printk("i_generation: %u\n",
2235 le32_to_cpu(fe
->i_generation
));
2236 printk("id1.bitmap1.i_used: %u\n",
2237 le32_to_cpu(fe
->id1
.bitmap1
.i_used
));
2238 printk("id1.bitmap1.i_total: %u\n",
2239 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
2240 printk("id2.i_chain.cl_cpg: %u\n", fe
->id2
.i_chain
.cl_cpg
);
2241 printk("id2.i_chain.cl_bpc: %u\n", fe
->id2
.i_chain
.cl_bpc
);
2242 printk("id2.i_chain.cl_count: %u\n", fe
->id2
.i_chain
.cl_count
);
2243 printk("id2.i_chain.cl_next_free_rec: %u\n",
2244 fe
->id2
.i_chain
.cl_next_free_rec
);
2245 for(i
= 0; i
< fe
->id2
.i_chain
.cl_next_free_rec
; i
++) {
2246 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i
,
2247 fe
->id2
.i_chain
.cl_recs
[i
].c_free
);
2248 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i
,
2249 fe
->id2
.i_chain
.cl_recs
[i
].c_total
);
2250 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i
,
2251 (unsigned long long)fe
->id2
.i_chain
.cl_recs
[i
].c_blkno
);
2256 * For a given allocation, determine which allocators will need to be
2257 * accessed, and lock them, reserving the appropriate number of bits.
2259 * Sparse file systems call this from ocfs2_write_begin_nolock()
2260 * and ocfs2_allocate_unwritten_extents().
2262 * File systems which don't support holes call this from
2263 * ocfs2_extend_allocation().
2265 int ocfs2_lock_allocators(struct inode
*inode
,
2266 struct ocfs2_extent_tree
*et
,
2267 u32 clusters_to_add
, u32 extents_to_split
,
2268 struct ocfs2_alloc_context
**data_ac
,
2269 struct ocfs2_alloc_context
**meta_ac
)
2271 int ret
= 0, num_free_extents
;
2272 unsigned int max_recs_needed
= clusters_to_add
+ 2 * extents_to_split
;
2273 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2279 BUG_ON(clusters_to_add
!= 0 && data_ac
== NULL
);
2281 num_free_extents
= ocfs2_num_free_extents(osb
, et
);
2282 if (num_free_extents
< 0) {
2283 ret
= num_free_extents
;
2289 * Sparse allocation file systems need to be more conservative
2290 * with reserving room for expansion - the actual allocation
2291 * happens while we've got a journal handle open so re-taking
2292 * a cluster lock (because we ran out of room for another
2293 * extent) will violate ordering rules.
2295 * Most of the time we'll only be seeing this 1 cluster at a time
2298 * Always lock for any unwritten extents - we might want to
2299 * add blocks during a split.
2301 if (!num_free_extents
||
2302 (ocfs2_sparse_alloc(osb
) && num_free_extents
< max_recs_needed
)) {
2303 ret
= ocfs2_reserve_new_metadata(osb
, et
->et_root_el
, meta_ac
);
2311 if (clusters_to_add
== 0)
2314 ret
= ocfs2_reserve_clusters(osb
, clusters_to_add
, data_ac
);
2324 ocfs2_free_alloc_context(*meta_ac
);
2329 * We cannot have an error and a non null *data_ac.
2337 * Read the inode specified by blkno to get suballoc_slot and
2340 static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super
*osb
, u64 blkno
,
2341 u16
*suballoc_slot
, u16
*suballoc_bit
)
2344 struct buffer_head
*inode_bh
= NULL
;
2345 struct ocfs2_dinode
*inode_fe
;
2347 mlog_entry("blkno: %llu\n", (unsigned long long)blkno
);
2349 /* dirty read disk */
2350 status
= ocfs2_read_blocks_sync(osb
, blkno
, 1, &inode_bh
);
2352 mlog(ML_ERROR
, "read block %llu failed %d\n",
2353 (unsigned long long)blkno
, status
);
2357 inode_fe
= (struct ocfs2_dinode
*) inode_bh
->b_data
;
2358 if (!OCFS2_IS_VALID_DINODE(inode_fe
)) {
2359 mlog(ML_ERROR
, "invalid inode %llu requested\n",
2360 (unsigned long long)blkno
);
2365 if (le16_to_cpu(inode_fe
->i_suballoc_slot
) != (u16
)OCFS2_INVALID_SLOT
&&
2366 (u32
)le16_to_cpu(inode_fe
->i_suballoc_slot
) > osb
->max_slots
- 1) {
2367 mlog(ML_ERROR
, "inode %llu has invalid suballoc slot %u\n",
2368 (unsigned long long)blkno
,
2369 (u32
)le16_to_cpu(inode_fe
->i_suballoc_slot
));
2375 *suballoc_slot
= le16_to_cpu(inode_fe
->i_suballoc_slot
);
2377 *suballoc_bit
= le16_to_cpu(inode_fe
->i_suballoc_bit
);
2387 * test whether bit is SET in allocator bitmap or not. on success, 0
2388 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2389 * is returned and *res is meaningless. Call this after you have
2390 * cluster locked against suballoc, or you may get a result based on
2391 * non-up2date contents
2393 static int ocfs2_test_suballoc_bit(struct ocfs2_super
*osb
,
2394 struct inode
*suballoc
,
2395 struct buffer_head
*alloc_bh
, u64 blkno
,
2398 struct ocfs2_dinode
*alloc_fe
;
2399 struct ocfs2_group_desc
*group
;
2400 struct buffer_head
*group_bh
= NULL
;
2404 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno
,
2407 alloc_fe
= (struct ocfs2_dinode
*)alloc_bh
->b_data
;
2408 if ((bit
+ 1) > ocfs2_bits_per_group(&alloc_fe
->id2
.i_chain
)) {
2409 mlog(ML_ERROR
, "suballoc bit %u out of range of %u\n",
2411 ocfs2_bits_per_group(&alloc_fe
->id2
.i_chain
));
2416 bg_blkno
= ocfs2_which_suballoc_group(blkno
, bit
);
2417 status
= ocfs2_read_group_descriptor(suballoc
, alloc_fe
, bg_blkno
,
2420 mlog(ML_ERROR
, "read group %llu failed %d\n",
2421 (unsigned long long)bg_blkno
, status
);
2425 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
2426 *res
= ocfs2_test_bit(bit
, (unsigned long *)group
->bg_bitmap
);
2436 * Test if the bit representing this inode (blkno) is set in the
2439 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2441 * In the event of failure, a negative value is returned and *res is
2444 * Callers must make sure to hold nfs_sync_lock to prevent
2445 * ocfs2_delete_inode() on another node from accessing the same
2446 * suballocator concurrently.
2448 int ocfs2_test_inode_bit(struct ocfs2_super
*osb
, u64 blkno
, int *res
)
2451 u16 suballoc_bit
= 0, suballoc_slot
= 0;
2452 struct inode
*inode_alloc_inode
;
2453 struct buffer_head
*alloc_bh
= NULL
;
2455 mlog_entry("blkno: %llu", (unsigned long long)blkno
);
2457 status
= ocfs2_get_suballoc_slot_bit(osb
, blkno
, &suballoc_slot
,
2460 mlog(ML_ERROR
, "get alloc slot and bit failed %d\n", status
);
2465 ocfs2_get_system_file_inode(osb
, INODE_ALLOC_SYSTEM_INODE
,
2467 if (!inode_alloc_inode
) {
2468 /* the error code could be inaccurate, but we are not able to
2469 * get the correct one. */
2471 mlog(ML_ERROR
, "unable to get alloc inode in slot %u\n",
2472 (u32
)suballoc_slot
);
2476 mutex_lock(&inode_alloc_inode
->i_mutex
);
2477 status
= ocfs2_inode_lock(inode_alloc_inode
, &alloc_bh
, 0);
2479 mutex_unlock(&inode_alloc_inode
->i_mutex
);
2480 mlog(ML_ERROR
, "lock on alloc inode on slot %u failed %d\n",
2481 (u32
)suballoc_slot
, status
);
2485 status
= ocfs2_test_suballoc_bit(osb
, inode_alloc_inode
, alloc_bh
,
2486 blkno
, suballoc_bit
, res
);
2488 mlog(ML_ERROR
, "test suballoc bit failed %d\n", status
);
2490 ocfs2_inode_unlock(inode_alloc_inode
, 0);
2491 mutex_unlock(&inode_alloc_inode
->i_mutex
);
2493 iput(inode_alloc_inode
);