27 #include <linux/types.h>
28 #include <linux/slab.h>
30 #include <linux/bitops.h>
49 #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
53 static int ocfs2_local_alloc_find_clear_bits(
struct ocfs2_super *osb,
58 static void ocfs2_clear_local_alloc(
struct ocfs2_dinode *alloc);
60 static int ocfs2_sync_local_to_main(
struct ocfs2_super *osb,
63 struct inode *main_bm_inode,
64 struct buffer_head *main_bm_bh);
66 static int ocfs2_local_alloc_reserve_for_window(
struct ocfs2_super *osb,
68 struct inode **bitmap_inode,
69 struct buffer_head **bitmap_bh);
71 static int ocfs2_local_alloc_new_window(
struct ocfs2_super *osb,
75 static int ocfs2_local_alloc_slide_window(
struct ocfs2_super *osb,
76 struct inode *local_alloc_inode);
115 #define OCFS2_LA_MAX_DEFAULT_MB 256
116 #define OCFS2_LA_OLD_DEFAULT 8
121 unsigned int la_max_mb;
122 unsigned int megs_per_slot;
125 gd_mb = ocfs2_clusters_to_megabytes(osb->
sb,
171 unsigned int gd_mult = gd_mb;
173 while (gd_mult > 256)
174 gd_mult = gd_mult >> 1;
181 megs_per_slot = ocfs2_clusters_to_megabytes(osb->
sb, megs_per_slot);
183 if (megs_per_slot < la_mb)
184 la_mb = megs_per_slot;
187 la_max_mb = ocfs2_clusters_to_megabytes(osb->
sb,
188 ocfs2_local_alloc_size(sb) * 8);
189 if (la_mb > la_max_mb)
199 unsigned int la_max_mb;
201 la_max_mb = ocfs2_clusters_to_megabytes(sb,
202 ocfs2_local_alloc_size(sb) * 8);
204 trace_ocfs2_la_set_sizes(requested_mb, la_max_mb, la_default_mb);
206 if (requested_mb == -1) {
209 ocfs2_megabytes_to_clusters(sb, la_default_mb);
210 }
else if (requested_mb > la_max_mb) {
213 ocfs2_megabytes_to_clusters(sb, la_max_mb);
216 ocfs2_megabytes_to_clusters(sb, requested_mb);
222 static inline int ocfs2_la_state_enabled(
struct ocfs2_super *osb)
229 unsigned int num_clusters)
266 if (!ocfs2_la_state_enabled(osb))
273 if (bits > (la_bits / 2))
278 trace_ocfs2_alloc_should_use_local(
288 struct buffer_head *alloc_bh =
NULL;
298 "than max possible %u. Using defaults.\n",
301 ocfs2_megabytes_to_clusters(osb->
sb,
327 (
unsigned long long)OCFS2_I(inode)->ip_blkno);
334 mlog(
ML_ERROR,
"Local alloc size is invalid (la_size = %u)\n",
341 num_used = ocfs2_local_alloc_count_bits(alloc);
350 "found = %u, set = %u, taken = %u, off = %u\n",
383 struct buffer_head *bh =
NULL;
384 struct buffer_head *main_bm_bh =
NULL;
399 if (!local_alloc_inode) {
412 if (!main_bm_inode) {
428 if (IS_ERR(handle)) {
442 memcpy(alloc_copy, alloc, bh->b_size);
451 ocfs2_clear_local_alloc(alloc);
458 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
459 main_bm_inode, main_bm_bh);
476 if (local_alloc_inode)
477 iput(local_alloc_inode);
495 struct buffer_head *alloc_bh =
NULL;
499 trace_ocfs2_begin_local_alloc_recovery(slot_num);
522 if (!(*alloc_copy)) {
526 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
529 ocfs2_clear_local_alloc(alloc);
537 if ((status < 0) && (*alloc_copy)) {
565 struct buffer_head *main_bm_bh =
NULL;
566 struct inode *main_bm_inode;
571 if (!main_bm_inode) {
586 if (IS_ERR(handle)) {
587 status = PTR_ERR(handle);
596 status = ocfs2_sync_local_to_main(osb, handle, alloc,
597 main_bm_inode, main_bm_bh);
634 struct inode *local_alloc_inode;
635 unsigned int free_bits;
643 if (!local_alloc_inode) {
656 if (!ocfs2_la_state_enabled(osb) ||
666 #ifdef CONFIG_OCFS2_DEBUG_FS
668 ocfs2_local_alloc_count_bits(alloc)) {
670 "%u free bits, but a count shows %u",
673 ocfs2_local_alloc_count_bits(alloc));
681 if (bits_wanted > free_bits) {
684 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
698 if (!ocfs2_la_state_enabled(osb))
703 if (bits_wanted > free_bits)
715 if (status < 0 && local_alloc_inode) {
717 iput(local_alloc_inode);
720 trace_ocfs2_reserve_local_alloc_bits(
722 bits_wanted, osb->
slot_num, status);
737 struct inode *local_alloc_inode;
748 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
759 *num_bits = bits_wanted;
762 INODE_CACHE(local_alloc_inode),
776 le32_add_cpu(&alloc->
id1.
bitmap1.i_used, *num_bits);
792 trace_ocfs2_local_alloc_count_bits(count);
796 static int ocfs2_local_alloc_find_clear_bits(
struct ocfs2_super *osb,
801 int numfound, bitoff,
left, startoff, lastzero;
821 if (numfound < *numbits)
838 numfound = bitoff = startoff = 0;
842 if (bitoff == left) {
851 if (bitoff == startoff) {
861 if (numfound == *numbits) {
867 trace_ocfs2_local_alloc_find_clear_bits_search_bitmap(bitoff, numfound);
869 if (numfound == *numbits)
870 bitoff = startoff - numfound;
878 trace_ocfs2_local_alloc_find_clear_bits(*numbits,
885 static void ocfs2_clear_local_alloc(
struct ocfs2_dinode *alloc)
899 static void ocfs2_verify_zero_bits(
unsigned long *bitmap,
906 printk(
"ocfs2_verify_zero_bits: start = %u, count = "
907 "%u\n", start, count);
908 printk(
"ocfs2_verify_zero_bits: bit %u is set!",
922 static int ocfs2_sync_local_to_main(
struct ocfs2_super *osb,
925 struct inode *main_bm_inode,
926 struct buffer_head *main_bm_bh)
935 trace_ocfs2_sync_local_to_main(
948 la_start_blk = ocfs2_clusters_to_blocks(osb->
sb,
951 start = count = bit_off = 0;
956 if ((bit_off < left) && (bit_off == start)) {
962 blkno = la_start_blk +
963 ocfs2_clusters_to_blocks(osb->
sb,
966 trace_ocfs2_sync_local_to_main_free(
967 count, start - count,
968 (
unsigned long long)la_start_blk,
969 (
unsigned long long)blkno);
1003 #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
1014 static int ocfs2_recalc_la_window(
struct ocfs2_super *osb,
1037 if (bits > ocfs2_megabytes_to_clusters(osb->
sb, 1)) {
1071 static int ocfs2_local_alloc_reserve_for_window(
struct ocfs2_super *osb,
1073 struct inode **bitmap_inode,
1074 struct buffer_head **bitmap_bh)
1102 *bitmap_inode = (*ac)->ac_inode;
1103 igrab(*bitmap_inode);
1104 *bitmap_bh = (*ac)->ac_bh;
1108 if ((status < 0) && *ac) {
1121 static int ocfs2_local_alloc_new_window(
struct ocfs2_super *osb,
1126 u32 cluster_off, cluster_count;
1133 trace_ocfs2_local_alloc_new_window(
1146 &cluster_off, &cluster_count);
1197 trace_ocfs2_local_alloc_new_window_result(
1209 static int ocfs2_local_alloc_slide_window(
struct ocfs2_super *osb,
1210 struct inode *local_alloc_inode)
1213 struct buffer_head *main_bm_bh =
NULL;
1215 handle_t *handle =
NULL;
1223 status = ocfs2_local_alloc_reserve_for_window(osb,
1234 if (IS_ERR(handle)) {
1235 status = PTR_ERR(handle);
1257 INODE_CACHE(local_alloc_inode),
1265 ocfs2_clear_local_alloc(alloc);
1268 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
1269 main_bm_inode, main_bm_bh);
1275 status = ocfs2_local_alloc_new_window(osb, handle, ac);
1291 iput(main_bm_inode);