aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ocfs2
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ocfs2')
-rw-r--r--fs/ocfs2/alloc.c29
-rw-r--r--fs/ocfs2/alloc.h2
-rw-r--r--fs/ocfs2/dir.c18
-rw-r--r--fs/ocfs2/journal.h12
-rw-r--r--fs/ocfs2/localalloc.c6
-rw-r--r--fs/ocfs2/namei.c8
-rw-r--r--fs/ocfs2/ocfs2.h7
-rw-r--r--fs/ocfs2/ocfs2_fs.h136
-rw-r--r--fs/ocfs2/refcounttree.c23
-rw-r--r--fs/ocfs2/resize.c6
-rw-r--r--fs/ocfs2/suballoc.c613
-rw-r--r--fs/ocfs2/suballoc.h19
-rw-r--r--fs/ocfs2/super.c3
-rw-r--r--fs/ocfs2/xattr.c25
14 files changed, 667 insertions, 240 deletions
diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c
index 7e9cb753fba7..215e12ce1d85 100644
--- a/fs/ocfs2/alloc.c
+++ b/fs/ocfs2/alloc.c
@@ -1006,7 +1006,7 @@ static int ocfs2_create_new_meta_bhs(handle_t *handle,
1006 int count, status, i; 1006 int count, status, i;
1007 u16 suballoc_bit_start; 1007 u16 suballoc_bit_start;
1008 u32 num_got; 1008 u32 num_got;
1009 u64 first_blkno; 1009 u64 suballoc_loc, first_blkno;
1010 struct ocfs2_super *osb = 1010 struct ocfs2_super *osb =
1011 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); 1011 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
1012 struct ocfs2_extent_block *eb; 1012 struct ocfs2_extent_block *eb;
@@ -1015,10 +1015,10 @@ static int ocfs2_create_new_meta_bhs(handle_t *handle,
1015 1015
1016 count = 0; 1016 count = 0;
1017 while (count < wanted) { 1017 while (count < wanted) {
1018 status = ocfs2_claim_metadata(osb, 1018 status = ocfs2_claim_metadata(handle,
1019 handle,
1020 meta_ac, 1019 meta_ac,
1021 wanted - count, 1020 wanted - count,
1021 &suballoc_loc,
1022 &suballoc_bit_start, 1022 &suballoc_bit_start,
1023 &num_got, 1023 &num_got,
1024 &first_blkno); 1024 &first_blkno);
@@ -1052,6 +1052,7 @@ static int ocfs2_create_new_meta_bhs(handle_t *handle,
1052 eb->h_fs_generation = cpu_to_le32(osb->fs_generation); 1052 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
1053 eb->h_suballoc_slot = 1053 eb->h_suballoc_slot =
1054 cpu_to_le16(meta_ac->ac_alloc_slot); 1054 cpu_to_le16(meta_ac->ac_alloc_slot);
1055 eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
1055 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); 1056 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1056 eb->h_list.l_count = 1057 eb->h_list.l_count =
1057 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); 1058 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
@@ -4786,7 +4787,7 @@ int ocfs2_add_clusters_in_btree(handle_t *handle,
4786 goto leave; 4787 goto leave;
4787 } 4788 }
4788 4789
4789 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1, 4790 status = __ocfs2_claim_clusters(handle, data_ac, 1,
4790 clusters_to_add, &bit_off, &num_bits); 4791 clusters_to_add, &bit_off, &num_bits);
4791 if (status < 0) { 4792 if (status < 0) {
4792 if (status != -ENOSPC) 4793 if (status != -ENOSPC)
@@ -6295,6 +6296,7 @@ int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6295 */ 6296 */
6296struct ocfs2_cached_block_free { 6297struct ocfs2_cached_block_free {
6297 struct ocfs2_cached_block_free *free_next; 6298 struct ocfs2_cached_block_free *free_next;
6299 u64 free_bg;
6298 u64 free_blk; 6300 u64 free_blk;
6299 unsigned int free_bit; 6301 unsigned int free_bit;
6300}; 6302};
@@ -6341,8 +6343,11 @@ static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6341 } 6343 }
6342 6344
6343 while (head) { 6345 while (head) {
6344 bg_blkno = ocfs2_which_suballoc_group(head->free_blk, 6346 if (head->free_bg)
6345 head->free_bit); 6347 bg_blkno = head->free_bg;
6348 else
6349 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6350 head->free_bit);
6346 mlog(0, "Free bit: (bit %u, blkno %llu)\n", 6351 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6347 head->free_bit, (unsigned long long)head->free_blk); 6352 head->free_bit, (unsigned long long)head->free_blk);
6348 6353
@@ -6390,7 +6395,7 @@ int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6390 int ret = 0; 6395 int ret = 0;
6391 struct ocfs2_cached_block_free *item; 6396 struct ocfs2_cached_block_free *item;
6392 6397
6393 item = kmalloc(sizeof(*item), GFP_NOFS); 6398 item = kzalloc(sizeof(*item), GFP_NOFS);
6394 if (item == NULL) { 6399 if (item == NULL) {
6395 ret = -ENOMEM; 6400 ret = -ENOMEM;
6396 mlog_errno(ret); 6401 mlog_errno(ret);
@@ -6530,8 +6535,8 @@ ocfs2_find_per_slot_free_list(int type,
6530} 6535}
6531 6536
6532int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, 6537int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6533 int type, int slot, u64 blkno, 6538 int type, int slot, u64 suballoc,
6534 unsigned int bit) 6539 u64 blkno, unsigned int bit)
6535{ 6540{
6536 int ret; 6541 int ret;
6537 struct ocfs2_per_slot_free_list *fl; 6542 struct ocfs2_per_slot_free_list *fl;
@@ -6544,7 +6549,7 @@ int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6544 goto out; 6549 goto out;
6545 } 6550 }
6546 6551
6547 item = kmalloc(sizeof(*item), GFP_NOFS); 6552 item = kzalloc(sizeof(*item), GFP_NOFS);
6548 if (item == NULL) { 6553 if (item == NULL) {
6549 ret = -ENOMEM; 6554 ret = -ENOMEM;
6550 mlog_errno(ret); 6555 mlog_errno(ret);
@@ -6554,6 +6559,7 @@ int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6554 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n", 6559 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6555 type, slot, bit, (unsigned long long)blkno); 6560 type, slot, bit, (unsigned long long)blkno);
6556 6561
6562 item->free_bg = suballoc;
6557 item->free_blk = blkno; 6563 item->free_blk = blkno;
6558 item->free_bit = bit; 6564 item->free_bit = bit;
6559 item->free_next = fl->f_first; 6565 item->free_next = fl->f_first;
@@ -6570,6 +6576,7 @@ static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6570{ 6576{
6571 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, 6577 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6572 le16_to_cpu(eb->h_suballoc_slot), 6578 le16_to_cpu(eb->h_suballoc_slot),
6579 le64_to_cpu(eb->h_suballoc_loc),
6573 le64_to_cpu(eb->h_blkno), 6580 le64_to_cpu(eb->h_blkno),
6574 le16_to_cpu(eb->h_suballoc_bit)); 6581 le16_to_cpu(eb->h_suballoc_bit));
6575} 6582}
@@ -6883,7 +6890,7 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6883 6890
6884 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv; 6891 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
6885 6892
6886 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, 6893 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
6887 &num); 6894 &num);
6888 if (ret) { 6895 if (ret) {
6889 mlog_errno(ret); 6896 mlog_errno(ret);
diff --git a/fs/ocfs2/alloc.h b/fs/ocfs2/alloc.h
index a55a27bb96a5..55762b554b99 100644
--- a/fs/ocfs2/alloc.h
+++ b/fs/ocfs2/alloc.h
@@ -210,7 +210,7 @@ static inline void ocfs2_init_dealloc_ctxt(struct ocfs2_cached_dealloc_ctxt *c)
210int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, 210int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
211 u64 blkno, unsigned int bit); 211 u64 blkno, unsigned int bit);
212int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, 212int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
213 int type, int slot, u64 blkno, 213 int type, int slot, u64 suballoc, u64 blkno,
214 unsigned int bit); 214 unsigned int bit);
215static inline int ocfs2_dealloc_has_cluster(struct ocfs2_cached_dealloc_ctxt *c) 215static inline int ocfs2_dealloc_has_cluster(struct ocfs2_cached_dealloc_ctxt *c)
216{ 216{
diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c
index 4a75c2e2f855..f04ebcfffc4a 100644
--- a/fs/ocfs2/dir.c
+++ b/fs/ocfs2/dir.c
@@ -2395,15 +2395,15 @@ static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2395 int ret; 2395 int ret;
2396 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; 2396 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
2397 u16 dr_suballoc_bit; 2397 u16 dr_suballoc_bit;
2398 u64 dr_blkno; 2398 u64 suballoc_loc, dr_blkno;
2399 unsigned int num_bits; 2399 unsigned int num_bits;
2400 struct buffer_head *dx_root_bh = NULL; 2400 struct buffer_head *dx_root_bh = NULL;
2401 struct ocfs2_dx_root_block *dx_root; 2401 struct ocfs2_dx_root_block *dx_root;
2402 struct ocfs2_dir_block_trailer *trailer = 2402 struct ocfs2_dir_block_trailer *trailer =
2403 ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb); 2403 ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
2404 2404
2405 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, &dr_suballoc_bit, 2405 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
2406 &num_bits, &dr_blkno); 2406 &dr_suballoc_bit, &num_bits, &dr_blkno);
2407 if (ret) { 2407 if (ret) {
2408 mlog_errno(ret); 2408 mlog_errno(ret);
2409 goto out; 2409 goto out;
@@ -2431,6 +2431,7 @@ static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2431 memset(dx_root, 0, osb->sb->s_blocksize); 2431 memset(dx_root, 0, osb->sb->s_blocksize);
2432 strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE); 2432 strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
2433 dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 2433 dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2434 dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
2434 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit); 2435 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
2435 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation); 2436 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
2436 dx_root->dr_blkno = cpu_to_le64(dr_blkno); 2437 dx_root->dr_blkno = cpu_to_le64(dr_blkno);
@@ -2544,7 +2545,7 @@ static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
2544 * chance of contiguousness as the directory grows in number 2545 * chance of contiguousness as the directory grows in number
2545 * of entries. 2546 * of entries.
2546 */ 2547 */
2547 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1, 1, &phys, &num); 2548 ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
2548 if (ret) { 2549 if (ret) {
2549 mlog_errno(ret); 2550 mlog_errno(ret);
2550 goto out; 2551 goto out;
@@ -2979,7 +2980,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
2979 */ 2980 */
2980 if (ocfs2_dir_resv_allowed(osb)) 2981 if (ocfs2_dir_resv_allowed(osb))
2981 data_ac->ac_resv = &oi->ip_la_data_resv; 2982 data_ac->ac_resv = &oi->ip_la_data_resv;
2982 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len); 2983 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
2983 if (ret) { 2984 if (ret) {
2984 mlog_errno(ret); 2985 mlog_errno(ret);
2985 goto out_commit; 2986 goto out_commit;
@@ -3118,7 +3119,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
3118 * pass. Claim the 2nd cluster as a separate extent. 3119 * pass. Claim the 2nd cluster as a separate extent.
3119 */ 3120 */
3120 if (alloc > len) { 3121 if (alloc > len) {
3121 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, 3122 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
3122 &len); 3123 &len);
3123 if (ret) { 3124 if (ret) {
3124 mlog_errno(ret); 3125 mlog_errno(ret);
@@ -4465,7 +4466,10 @@ static int ocfs2_dx_dir_remove_index(struct inode *dir,
4465 4466
4466 blk = le64_to_cpu(dx_root->dr_blkno); 4467 blk = le64_to_cpu(dx_root->dr_blkno);
4467 bit = le16_to_cpu(dx_root->dr_suballoc_bit); 4468 bit = le16_to_cpu(dx_root->dr_suballoc_bit);
4468 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 4469 if (dx_root->dr_suballoc_loc)
4470 bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
4471 else
4472 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
4469 ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh, 4473 ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
4470 bit, bg_blkno, 1); 4474 bit, bg_blkno, 1);
4471 if (ret) 4475 if (ret)
diff --git a/fs/ocfs2/journal.h b/fs/ocfs2/journal.h
index 7dc56561c9ae..b5baaa8e710f 100644
--- a/fs/ocfs2/journal.h
+++ b/fs/ocfs2/journal.h
@@ -561,6 +561,18 @@ static inline int ocfs2_calc_group_alloc_credits(struct super_block *sb,
561 return blocks; 561 return blocks;
562} 562}
563 563
564/*
565 * Allocating a discontiguous block group requires the credits from
566 * ocfs2_calc_group_alloc_credits() as well as enough credits to fill
567 * the group descriptor's extent list. The caller already has started
568 * the transaction with ocfs2_calc_group_alloc_credits(). They extend
569 * it with these credits.
570 */
571static inline int ocfs2_calc_bg_discontig_credits(struct super_block *sb)
572{
573 return ocfs2_extent_recs_per_gd(sb);
574}
575
564static inline int ocfs2_calc_tree_trunc_credits(struct super_block *sb, 576static inline int ocfs2_calc_tree_trunc_credits(struct super_block *sb,
565 unsigned int clusters_to_del, 577 unsigned int clusters_to_del,
566 struct ocfs2_dinode *fe, 578 struct ocfs2_dinode *fe,
diff --git a/fs/ocfs2/localalloc.c b/fs/ocfs2/localalloc.c
index 63c41e206792..3d7419682dc0 100644
--- a/fs/ocfs2/localalloc.c
+++ b/fs/ocfs2/localalloc.c
@@ -122,7 +122,7 @@ unsigned int ocfs2_la_default_mb(struct ocfs2_super *osb)
122 struct super_block *sb = osb->sb; 122 struct super_block *sb = osb->sb;
123 123
124 gd_mb = ocfs2_clusters_to_megabytes(osb->sb, 124 gd_mb = ocfs2_clusters_to_megabytes(osb->sb,
125 8 * ocfs2_group_bitmap_size(sb)); 125 8 * ocfs2_group_bitmap_size(sb, 0, osb->s_feature_incompat));
126 126
127 /* 127 /*
128 * This takes care of files systems with very small group 128 * This takes care of files systems with very small group
@@ -1161,7 +1161,7 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
1161 /* we used the generic suballoc reserve function, but we set 1161 /* we used the generic suballoc reserve function, but we set
1162 * everything up nicely, so there's no reason why we can't use 1162 * everything up nicely, so there's no reason why we can't use
1163 * the more specific cluster api to claim bits. */ 1163 * the more specific cluster api to claim bits. */
1164 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits, 1164 status = ocfs2_claim_clusters(handle, ac, osb->local_alloc_bits,
1165 &cluster_off, &cluster_count); 1165 &cluster_off, &cluster_count);
1166 if (status == -ENOSPC) { 1166 if (status == -ENOSPC) {
1167retry_enospc: 1167retry_enospc:
@@ -1175,7 +1175,7 @@ retry_enospc:
1175 goto bail; 1175 goto bail;
1176 1176
1177 ac->ac_bits_wanted = osb->local_alloc_default_bits; 1177 ac->ac_bits_wanted = osb->local_alloc_default_bits;
1178 status = ocfs2_claim_clusters(osb, handle, ac, 1178 status = ocfs2_claim_clusters(handle, ac,
1179 osb->local_alloc_bits, 1179 osb->local_alloc_bits,
1180 &cluster_off, 1180 &cluster_off,
1181 &cluster_count); 1181 &cluster_count);
diff --git a/fs/ocfs2/namei.c b/fs/ocfs2/namei.c
index 607084b349d4..b0fa6b650751 100644
--- a/fs/ocfs2/namei.c
+++ b/fs/ocfs2/namei.c
@@ -480,14 +480,15 @@ static int ocfs2_mknod_locked(struct ocfs2_super *osb,
480 int status = 0; 480 int status = 0;
481 struct ocfs2_dinode *fe = NULL; 481 struct ocfs2_dinode *fe = NULL;
482 struct ocfs2_extent_list *fel; 482 struct ocfs2_extent_list *fel;
483 u64 fe_blkno = 0; 483 u64 suballoc_loc, fe_blkno = 0;
484 u16 suballoc_bit; 484 u16 suballoc_bit;
485 u16 feat; 485 u16 feat;
486 486
487 *new_fe_bh = NULL; 487 *new_fe_bh = NULL;
488 488
489 status = ocfs2_claim_new_inode(osb, handle, dir, parent_fe_bh, 489 status = ocfs2_claim_new_inode(handle, dir, parent_fe_bh,
490 inode_ac, &suballoc_bit, &fe_blkno); 490 inode_ac, &suballoc_loc,
491 &suballoc_bit, &fe_blkno);
491 if (status < 0) { 492 if (status < 0) {
492 mlog_errno(status); 493 mlog_errno(status);
493 goto leave; 494 goto leave;
@@ -524,6 +525,7 @@ static int ocfs2_mknod_locked(struct ocfs2_super *osb,
524 fe->i_generation = cpu_to_le32(inode->i_generation); 525 fe->i_generation = cpu_to_le32(inode->i_generation);
525 fe->i_fs_generation = cpu_to_le32(osb->fs_generation); 526 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
526 fe->i_blkno = cpu_to_le64(fe_blkno); 527 fe->i_blkno = cpu_to_le64(fe_blkno);
528 fe->i_suballoc_loc = cpu_to_le64(suballoc_loc);
527 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit); 529 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
528 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot); 530 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
529 fe->i_uid = cpu_to_le32(inode->i_uid); 531 fe->i_uid = cpu_to_le32(inode->i_uid);
diff --git a/fs/ocfs2/ocfs2.h b/fs/ocfs2/ocfs2.h
index a388528f485c..c67003b6b5a2 100644
--- a/fs/ocfs2/ocfs2.h
+++ b/fs/ocfs2/ocfs2.h
@@ -491,6 +491,13 @@ static inline int ocfs2_supports_indexed_dirs(struct ocfs2_super *osb)
491 return 0; 491 return 0;
492} 492}
493 493
494static inline int ocfs2_supports_discontig_bg(struct ocfs2_super *osb)
495{
496 if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_DISCONTIG_BG)
497 return 1;
498 return 0;
499}
500
494static inline unsigned int ocfs2_link_max(struct ocfs2_super *osb) 501static inline unsigned int ocfs2_link_max(struct ocfs2_super *osb)
495{ 502{
496 if (ocfs2_supports_indexed_dirs(osb)) 503 if (ocfs2_supports_indexed_dirs(osb))
diff --git a/fs/ocfs2/ocfs2_fs.h b/fs/ocfs2/ocfs2_fs.h
index d61a1521b10e..33f1c9a8258d 100644
--- a/fs/ocfs2/ocfs2_fs.h
+++ b/fs/ocfs2/ocfs2_fs.h
@@ -100,7 +100,8 @@
100 | OCFS2_FEATURE_INCOMPAT_XATTR \ 100 | OCFS2_FEATURE_INCOMPAT_XATTR \
101 | OCFS2_FEATURE_INCOMPAT_META_ECC \ 101 | OCFS2_FEATURE_INCOMPAT_META_ECC \
102 | OCFS2_FEATURE_INCOMPAT_INDEXED_DIRS \ 102 | OCFS2_FEATURE_INCOMPAT_INDEXED_DIRS \
103 | OCFS2_FEATURE_INCOMPAT_REFCOUNT_TREE) 103 | OCFS2_FEATURE_INCOMPAT_REFCOUNT_TREE \
104 | OCFS2_FEATURE_INCOMPAT_DISCONTIG_BG)
104#define OCFS2_FEATURE_RO_COMPAT_SUPP (OCFS2_FEATURE_RO_COMPAT_UNWRITTEN \ 105#define OCFS2_FEATURE_RO_COMPAT_SUPP (OCFS2_FEATURE_RO_COMPAT_UNWRITTEN \
105 | OCFS2_FEATURE_RO_COMPAT_USRQUOTA \ 106 | OCFS2_FEATURE_RO_COMPAT_USRQUOTA \
106 | OCFS2_FEATURE_RO_COMPAT_GRPQUOTA) 107 | OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
@@ -165,6 +166,9 @@
165/* Refcount tree support */ 166/* Refcount tree support */
166#define OCFS2_FEATURE_INCOMPAT_REFCOUNT_TREE 0x1000 167#define OCFS2_FEATURE_INCOMPAT_REFCOUNT_TREE 0x1000
167 168
169/* Discontigous block groups */
170#define OCFS2_FEATURE_INCOMPAT_DISCONTIG_BG 0x2000
171
168/* 172/*
169 * backup superblock flag is used to indicate that this volume 173 * backup superblock flag is used to indicate that this volume
170 * has backup superblocks. 174 * has backup superblocks.
@@ -504,7 +508,10 @@ struct ocfs2_extent_block
504 block group */ 508 block group */
505 __le32 h_fs_generation; /* Must match super block */ 509 __le32 h_fs_generation; /* Must match super block */
506 __le64 h_blkno; /* Offset on disk, in blocks */ 510 __le64 h_blkno; /* Offset on disk, in blocks */
507/*20*/ __le64 h_reserved3; 511/*20*/ __le64 h_suballoc_loc; /* Suballocator block group this
512 eb belongs to. Only valid
513 if allocated from a
514 discontiguous block group */
508 __le64 h_next_leaf_blk; /* Offset on disk, in blocks, 515 __le64 h_next_leaf_blk; /* Offset on disk, in blocks,
509 of next leaf header pointing 516 of next leaf header pointing
510 to data */ 517 to data */
@@ -671,7 +678,11 @@ struct ocfs2_dinode {
671/*80*/ struct ocfs2_block_check i_check; /* Error checking */ 678/*80*/ struct ocfs2_block_check i_check; /* Error checking */
672/*88*/ __le64 i_dx_root; /* Pointer to dir index root block */ 679/*88*/ __le64 i_dx_root; /* Pointer to dir index root block */
673/*90*/ __le64 i_refcount_loc; 680/*90*/ __le64 i_refcount_loc;
674 __le64 i_reserved2[4]; 681 __le64 i_suballoc_loc; /* Suballocator block group this
682 inode belongs to. Only valid
683 if allocated from a
684 discontiguous block group */
685/*A0*/ __le64 i_reserved2[3];
675/*B8*/ union { 686/*B8*/ union {
676 __le64 i_pad1; /* Generic way to refer to this 687 __le64 i_pad1; /* Generic way to refer to this
677 64bit union */ 688 64bit union */
@@ -806,7 +817,12 @@ struct ocfs2_dx_root_block {
806 __le32 dr_reserved2; 817 __le32 dr_reserved2;
807 __le64 dr_free_blk; /* Pointer to head of free 818 __le64 dr_free_blk; /* Pointer to head of free
808 * unindexed block list. */ 819 * unindexed block list. */
809 __le64 dr_reserved3[15]; 820 __le64 dr_suballoc_loc; /* Suballocator block group
821 this root belongs to.
822 Only valid if allocated
823 from a discontiguous
824 block group */
825 __le64 dr_reserved3[14];
810 union { 826 union {
811 struct ocfs2_extent_list dr_list; /* Keep this aligned to 128 827 struct ocfs2_extent_list dr_list; /* Keep this aligned to 128
812 * bits for maximum space 828 * bits for maximum space
@@ -832,6 +848,13 @@ struct ocfs2_dx_leaf {
832}; 848};
833 849
834/* 850/*
851 * Largest bitmap for a block (suballocator) group in bytes. This limit
852 * does not affect cluster groups (global allocator). Cluster group
853 * bitmaps run to the end of the block.
854 */
855#define OCFS2_MAX_BG_BITMAP_SIZE 256
856
857/*
835 * On disk allocator group structure for OCFS2 858 * On disk allocator group structure for OCFS2
836 */ 859 */
837struct ocfs2_group_desc 860struct ocfs2_group_desc
@@ -852,7 +875,29 @@ struct ocfs2_group_desc
852 __le64 bg_blkno; /* Offset on disk, in blocks */ 875 __le64 bg_blkno; /* Offset on disk, in blocks */
853/*30*/ struct ocfs2_block_check bg_check; /* Error checking */ 876/*30*/ struct ocfs2_block_check bg_check; /* Error checking */
854 __le64 bg_reserved2; 877 __le64 bg_reserved2;
855/*40*/ __u8 bg_bitmap[0]; 878/*40*/ union {
879 __u8 bg_bitmap[0];
880 struct {
881 /*
882 * Block groups may be discontiguous when
883 * OCFS2_FEATURE_INCOMPAT_DISCONTIG_BG is set.
884 * The extents of a discontigous block group are
885 * stored in bg_list. It is a flat list.
886 * l_tree_depth must always be zero. A
887 * discontiguous group is signified by a non-zero
888 * bg_list->l_next_free_rec. Only block groups
889 * can be discontiguous; Cluster groups cannot.
890 * We've never made a block group with more than
891 * 2048 blocks (256 bytes of bg_bitmap). This
892 * codifies that limit so that we can fit bg_list.
893 * bg_size of a discontiguous block group will
894 * be 256 to match bg_bitmap_filler.
895 */
896 __u8 bg_bitmap_filler[OCFS2_MAX_BG_BITMAP_SIZE];
897/*140*/ struct ocfs2_extent_list bg_list;
898 };
899 };
900/* Actual on-disk size is one block */
856}; 901};
857 902
858struct ocfs2_refcount_rec { 903struct ocfs2_refcount_rec {
@@ -897,7 +942,11 @@ struct ocfs2_refcount_block {
897/*40*/ __le32 rf_generation; /* generation number. all be the same 942/*40*/ __le32 rf_generation; /* generation number. all be the same
898 * for the same refcount tree. */ 943 * for the same refcount tree. */
899 __le32 rf_reserved0; 944 __le32 rf_reserved0;
900 __le64 rf_reserved1[7]; 945 __le64 rf_suballoc_loc; /* Suballocator block group this
946 refcount block belongs to. Only
947 valid if allocated from a
948 discontiguous block group */
949/*50*/ __le64 rf_reserved1[6];
901/*80*/ union { 950/*80*/ union {
902 struct ocfs2_refcount_list rf_records; /* List of refcount 951 struct ocfs2_refcount_list rf_records; /* List of refcount
903 records */ 952 records */
@@ -1009,7 +1058,10 @@ struct ocfs2_xattr_block {
1009 real xattr or a xattr tree. */ 1058 real xattr or a xattr tree. */
1010 __le16 xb_reserved0; 1059 __le16 xb_reserved0;
1011 __le32 xb_reserved1; 1060 __le32 xb_reserved1;
1012 __le64 xb_reserved2; 1061 __le64 xb_suballoc_loc; /* Suballocator block group this
1062 xattr block belongs to. Only
1063 valid if allocated from a
1064 discontiguous block group */
1013/*30*/ union { 1065/*30*/ union {
1014 struct ocfs2_xattr_header xb_header; /* xattr header if this 1066 struct ocfs2_xattr_header xb_header; /* xattr header if this
1015 block contains xattr */ 1067 block contains xattr */
@@ -1246,6 +1298,16 @@ static inline u16 ocfs2_extent_recs_per_eb(struct super_block *sb)
1246 return size / sizeof(struct ocfs2_extent_rec); 1298 return size / sizeof(struct ocfs2_extent_rec);
1247} 1299}
1248 1300
1301static inline u16 ocfs2_extent_recs_per_gd(struct super_block *sb)
1302{
1303 int size;
1304
1305 size = sb->s_blocksize -
1306 offsetof(struct ocfs2_group_desc, bg_list.l_recs);
1307
1308 return size / sizeof(struct ocfs2_extent_rec);
1309}
1310
1249static inline int ocfs2_dx_entries_per_leaf(struct super_block *sb) 1311static inline int ocfs2_dx_entries_per_leaf(struct super_block *sb)
1250{ 1312{
1251 int size; 1313 int size;
@@ -1276,13 +1338,23 @@ static inline u16 ocfs2_local_alloc_size(struct super_block *sb)
1276 return size; 1338 return size;
1277} 1339}
1278 1340
1279static inline int ocfs2_group_bitmap_size(struct super_block *sb) 1341static inline int ocfs2_group_bitmap_size(struct super_block *sb,
1342 int suballocator,
1343 u32 feature_incompat)
1280{ 1344{
1281 int size; 1345 int size = sb->s_blocksize -
1282
1283 size = sb->s_blocksize -
1284 offsetof(struct ocfs2_group_desc, bg_bitmap); 1346 offsetof(struct ocfs2_group_desc, bg_bitmap);
1285 1347
1348 /*
1349 * The cluster allocator uses the entire block. Suballocators have
1350 * never used more than OCFS2_MAX_BG_BITMAP_SIZE. Unfortunately, older
1351 * code expects bg_size set to the maximum. Thus we must keep
1352 * bg_size as-is unless discontig_bg is enabled.
1353 */
1354 if (suballocator &&
1355 (feature_incompat & OCFS2_FEATURE_INCOMPAT_DISCONTIG_BG))
1356 size = OCFS2_MAX_BG_BITMAP_SIZE;
1357
1286 return size; 1358 return size;
1287} 1359}
1288 1360
@@ -1394,23 +1466,43 @@ static inline int ocfs2_extent_recs_per_eb(int blocksize)
1394 return size / sizeof(struct ocfs2_extent_rec); 1466 return size / sizeof(struct ocfs2_extent_rec);
1395} 1467}
1396 1468
1397static inline int ocfs2_local_alloc_size(int blocksize) 1469static inline int ocfs2_extent_recs_per_gd(int blocksize)
1398{ 1470{
1399 int size; 1471 int size;
1400 1472
1401 size = blocksize - 1473 size = blocksize -
1402 offsetof(struct ocfs2_dinode, id2.i_lab.la_bitmap); 1474 offsetof(struct ocfs2_group_desc, bg_list.l_recs);
1403 1475
1404 return size; 1476 return size / sizeof(struct ocfs2_extent_rec);
1405} 1477}
1406 1478
1407static inline int ocfs2_group_bitmap_size(int blocksize) 1479static inline int ocfs2_local_alloc_size(int blocksize)
1408{ 1480{
1409 int size; 1481 int size;
1410 1482
1411 size = blocksize - 1483 size = blocksize -
1484 offsetof(struct ocfs2_dinode, id2.i_lab.la_bitmap);
1485
1486 return size;
1487}
1488
1489static inline int ocfs2_group_bitmap_size(int blocksize,
1490 int suballocator,
1491 uint32_t feature_incompat)
1492{
1493 int size = sb->s_blocksize -
1412 offsetof(struct ocfs2_group_desc, bg_bitmap); 1494 offsetof(struct ocfs2_group_desc, bg_bitmap);
1413 1495
1496 /*
1497 * The cluster allocator uses the entire block. Suballocators have
1498 * never used more than OCFS2_MAX_BG_BITMAP_SIZE. Unfortunately, older
1499 * code expects bg_size set to the maximum. Thus we must keep
1500 * bg_size as-is unless discontig_bg is enabled.
1501 */
1502 if (suballocator &&
1503 (feature_incompat & OCFS2_FEATURE_INCOMPAT_DISCONTIG_BG))
1504 size = OCFS2_MAX_BG_BITMAP_SIZE;
1505
1414 return size; 1506 return size;
1415} 1507}
1416 1508
@@ -1483,5 +1575,19 @@ static inline void ocfs2_set_de_type(struct ocfs2_dir_entry *de,
1483 de->file_type = ocfs2_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; 1575 de->file_type = ocfs2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1484} 1576}
1485 1577
1578static inline int ocfs2_gd_is_discontig(struct ocfs2_group_desc *gd)
1579{
1580 if ((offsetof(struct ocfs2_group_desc, bg_bitmap) +
1581 le16_to_cpu(gd->bg_size)) !=
1582 offsetof(struct ocfs2_group_desc, bg_list))
1583 return 0;
1584 /*
1585 * Only valid to check l_next_free_rec if
1586 * bg_bitmap + bg_size == bg_list.
1587 */
1588 if (!gd->bg_list.l_next_free_rec)
1589 return 0;
1590 return 1;
1591}
1486#endif /* _OCFS2_FS_H */ 1592#endif /* _OCFS2_FS_H */
1487 1593
diff --git a/fs/ocfs2/refcounttree.c b/fs/ocfs2/refcounttree.c
index 6fab28921f3d..52e4f6ee1e23 100644
--- a/fs/ocfs2/refcounttree.c
+++ b/fs/ocfs2/refcounttree.c
@@ -571,7 +571,7 @@ static int ocfs2_create_refcount_tree(struct inode *inode,
571 struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL; 571 struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL;
572 u16 suballoc_bit_start; 572 u16 suballoc_bit_start;
573 u32 num_got; 573 u32 num_got;
574 u64 first_blkno; 574 u64 suballoc_loc, first_blkno;
575 575
576 BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL); 576 BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
577 577
@@ -597,7 +597,7 @@ static int ocfs2_create_refcount_tree(struct inode *inode,
597 goto out_commit; 597 goto out_commit;
598 } 598 }
599 599
600 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, 600 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
601 &suballoc_bit_start, &num_got, 601 &suballoc_bit_start, &num_got,
602 &first_blkno); 602 &first_blkno);
603 if (ret) { 603 if (ret) {
@@ -627,6 +627,7 @@ static int ocfs2_create_refcount_tree(struct inode *inode,
627 memset(rb, 0, inode->i_sb->s_blocksize); 627 memset(rb, 0, inode->i_sb->s_blocksize);
628 strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE); 628 strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
629 rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 629 rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
630 rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
630 rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 631 rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
631 rb->rf_fs_generation = cpu_to_le32(osb->fs_generation); 632 rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
632 rb->rf_blkno = cpu_to_le64(first_blkno); 633 rb->rf_blkno = cpu_to_le64(first_blkno);
@@ -791,7 +792,10 @@ int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh)
791 if (le32_to_cpu(rb->rf_count) == 1) { 792 if (le32_to_cpu(rb->rf_count) == 1) {
792 blk = le64_to_cpu(rb->rf_blkno); 793 blk = le64_to_cpu(rb->rf_blkno);
793 bit = le16_to_cpu(rb->rf_suballoc_bit); 794 bit = le16_to_cpu(rb->rf_suballoc_bit);
794 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 795 if (rb->rf_suballoc_loc)
796 bg_blkno = le64_to_cpu(rb->rf_suballoc_loc);
797 else
798 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
795 799
796 alloc_inode = ocfs2_get_system_file_inode(osb, 800 alloc_inode = ocfs2_get_system_file_inode(osb,
797 EXTENT_ALLOC_SYSTEM_INODE, 801 EXTENT_ALLOC_SYSTEM_INODE,
@@ -1283,7 +1287,7 @@ static int ocfs2_expand_inline_ref_root(handle_t *handle,
1283 int ret; 1287 int ret;
1284 u16 suballoc_bit_start; 1288 u16 suballoc_bit_start;
1285 u32 num_got; 1289 u32 num_got;
1286 u64 blkno; 1290 u64 suballoc_loc, blkno;
1287 struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 1291 struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1288 struct buffer_head *new_bh = NULL; 1292 struct buffer_head *new_bh = NULL;
1289 struct ocfs2_refcount_block *new_rb; 1293 struct ocfs2_refcount_block *new_rb;
@@ -1297,7 +1301,7 @@ static int ocfs2_expand_inline_ref_root(handle_t *handle,
1297 goto out; 1301 goto out;
1298 } 1302 }
1299 1303
1300 ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1, 1304 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
1301 &suballoc_bit_start, &num_got, 1305 &suballoc_bit_start, &num_got,
1302 &blkno); 1306 &blkno);
1303 if (ret) { 1307 if (ret) {
@@ -1329,6 +1333,7 @@ static int ocfs2_expand_inline_ref_root(handle_t *handle,
1329 1333
1330 new_rb = (struct ocfs2_refcount_block *)new_bh->b_data; 1334 new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1331 new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 1335 new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
1336 new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
1332 new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 1337 new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1333 new_rb->rf_blkno = cpu_to_le64(blkno); 1338 new_rb->rf_blkno = cpu_to_le64(blkno);
1334 new_rb->rf_cpos = cpu_to_le32(0); 1339 new_rb->rf_cpos = cpu_to_le32(0);
@@ -1523,7 +1528,7 @@ static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1523 int ret; 1528 int ret;
1524 u16 suballoc_bit_start; 1529 u16 suballoc_bit_start;
1525 u32 num_got, new_cpos; 1530 u32 num_got, new_cpos;
1526 u64 blkno; 1531 u64 suballoc_loc, blkno;
1527 struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 1532 struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1528 struct ocfs2_refcount_block *root_rb = 1533 struct ocfs2_refcount_block *root_rb =
1529 (struct ocfs2_refcount_block *)ref_root_bh->b_data; 1534 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
@@ -1547,7 +1552,7 @@ static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1547 goto out; 1552 goto out;
1548 } 1553 }
1549 1554
1550 ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1, 1555 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
1551 &suballoc_bit_start, &num_got, 1556 &suballoc_bit_start, &num_got,
1552 &blkno); 1557 &blkno);
1553 if (ret) { 1558 if (ret) {
@@ -1575,6 +1580,7 @@ static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1575 memset(new_rb, 0, sb->s_blocksize); 1580 memset(new_rb, 0, sb->s_blocksize);
1576 strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE); 1581 strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
1577 new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 1582 new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
1583 new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
1578 new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 1584 new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1579 new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); 1585 new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1580 new_rb->rf_blkno = cpu_to_le64(blkno); 1586 new_rb->rf_blkno = cpu_to_le64(blkno);
@@ -2105,6 +2111,7 @@ static int ocfs2_remove_refcount_extent(handle_t *handle,
2105 */ 2111 */
2106 ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE, 2112 ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE,
2107 le16_to_cpu(rb->rf_suballoc_slot), 2113 le16_to_cpu(rb->rf_suballoc_slot),
2114 le64_to_cpu(rb->rf_suballoc_loc),
2108 le64_to_cpu(rb->rf_blkno), 2115 le64_to_cpu(rb->rf_blkno),
2109 le16_to_cpu(rb->rf_suballoc_bit)); 2116 le16_to_cpu(rb->rf_suballoc_bit));
2110 if (ret) { 2117 if (ret) {
@@ -3262,7 +3269,7 @@ static int ocfs2_make_clusters_writable(struct super_block *sb,
3262 } else { 3269 } else {
3263 delete = 1; 3270 delete = 1;
3264 3271
3265 ret = __ocfs2_claim_clusters(osb, handle, 3272 ret = __ocfs2_claim_clusters(handle,
3266 context->data_ac, 3273 context->data_ac,
3267 1, set_len, 3274 1, set_len,
3268 &new_bit, &new_len); 3275 &new_bit, &new_len);
diff --git a/fs/ocfs2/resize.c b/fs/ocfs2/resize.c
index a821f667b5c4..dacd553d8617 100644
--- a/fs/ocfs2/resize.c
+++ b/fs/ocfs2/resize.c
@@ -315,7 +315,8 @@ int ocfs2_group_extend(struct inode * inode, int new_clusters)
315 BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 315 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
316 316
317 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) != 317 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
318 ocfs2_group_bitmap_size(osb->sb) * 8) { 318 ocfs2_group_bitmap_size(osb->sb, 0,
319 osb->s_feature_incompat) * 8) {
319 mlog(ML_ERROR, "The disk is too old and small. " 320 mlog(ML_ERROR, "The disk is too old and small. "
320 "Force to do offline resize."); 321 "Force to do offline resize.");
321 ret = -EINVAL; 322 ret = -EINVAL;
@@ -496,7 +497,8 @@ int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input)
496 fe = (struct ocfs2_dinode *)main_bm_bh->b_data; 497 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
497 498
498 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) != 499 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
499 ocfs2_group_bitmap_size(osb->sb) * 8) { 500 ocfs2_group_bitmap_size(osb->sb, 0,
501 osb->s_feature_incompat) * 8) {
500 mlog(ML_ERROR, "The disk is too old and small." 502 mlog(ML_ERROR, "The disk is too old and small."
501 " Force to do offline resize."); 503 " Force to do offline resize.");
502 ret = -EINVAL; 504 ret = -EINVAL;
diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c
index 667d622b3659..a327c80721ee 100644
--- a/fs/ocfs2/suballoc.c
+++ b/fs/ocfs2/suballoc.c
@@ -53,6 +53,15 @@
53 53
54#define OCFS2_MAX_TO_STEAL 1024 54#define OCFS2_MAX_TO_STEAL 1024
55 55
56struct ocfs2_suballoc_result {
57 u64 sr_bg_blkno; /* The bg we allocated from. Set
58 to 0 when a block group is
59 contiguous. */
60 u64 sr_blkno; /* The first allocated block */
61 unsigned int sr_bit_offset; /* The bit in the bg */
62 unsigned int sr_bits; /* How many bits we claimed */
63};
64
56static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg); 65static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
57static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe); 66static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
58static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl); 67static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
@@ -60,6 +69,7 @@ static int ocfs2_block_group_fill(handle_t *handle,
60 struct inode *alloc_inode, 69 struct inode *alloc_inode,
61 struct buffer_head *bg_bh, 70 struct buffer_head *bg_bh,
62 u64 group_blkno, 71 u64 group_blkno,
72 unsigned int group_clusters,
63 u16 my_chain, 73 u16 my_chain,
64 struct ocfs2_chain_list *cl); 74 struct ocfs2_chain_list *cl);
65static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 75static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
@@ -73,20 +83,17 @@ static int ocfs2_cluster_group_search(struct inode *inode,
73 struct buffer_head *group_bh, 83 struct buffer_head *group_bh,
74 u32 bits_wanted, u32 min_bits, 84 u32 bits_wanted, u32 min_bits,
75 u64 max_block, 85 u64 max_block,
76 u16 *bit_off, u16 *bits_found); 86 struct ocfs2_suballoc_result *res);
77static int ocfs2_block_group_search(struct inode *inode, 87static int ocfs2_block_group_search(struct inode *inode,
78 struct buffer_head *group_bh, 88 struct buffer_head *group_bh,
79 u32 bits_wanted, u32 min_bits, 89 u32 bits_wanted, u32 min_bits,
80 u64 max_block, 90 u64 max_block,
81 u16 *bit_off, u16 *bits_found); 91 struct ocfs2_suballoc_result *res);
82static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb, 92static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
83 struct ocfs2_alloc_context *ac,
84 handle_t *handle, 93 handle_t *handle,
85 u32 bits_wanted, 94 u32 bits_wanted,
86 u32 min_bits, 95 u32 min_bits,
87 u16 *bit_off, 96 struct ocfs2_suballoc_result *res);
88 unsigned int *num_bits,
89 u64 *bg_blkno);
90static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, 97static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
91 int nr); 98 int nr);
92static inline int ocfs2_block_group_set_bits(handle_t *handle, 99static inline int ocfs2_block_group_set_bits(handle_t *handle,
@@ -326,14 +333,38 @@ out:
326 return rc; 333 return rc;
327} 334}
328 335
336static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
337 struct ocfs2_group_desc *bg,
338 struct ocfs2_chain_list *cl,
339 u64 p_blkno, u32 clusters)
340{
341 struct ocfs2_extent_list *el = &bg->bg_list;
342 struct ocfs2_extent_rec *rec;
343
344 BUG_ON(!ocfs2_supports_discontig_bg(osb));
345 if (!el->l_next_free_rec)
346 el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
347 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
348 rec->e_blkno = cpu_to_le64(p_blkno);
349 rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
350 le16_to_cpu(cl->cl_bpc));
351 rec->e_leaf_clusters = cpu_to_le32(clusters);
352 le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
353 le16_add_cpu(&bg->bg_free_bits_count,
354 clusters * le16_to_cpu(cl->cl_bpc));
355 le16_add_cpu(&el->l_next_free_rec, 1);
356}
357
329static int ocfs2_block_group_fill(handle_t *handle, 358static int ocfs2_block_group_fill(handle_t *handle,
330 struct inode *alloc_inode, 359 struct inode *alloc_inode,
331 struct buffer_head *bg_bh, 360 struct buffer_head *bg_bh,
332 u64 group_blkno, 361 u64 group_blkno,
362 unsigned int group_clusters,
333 u16 my_chain, 363 u16 my_chain,
334 struct ocfs2_chain_list *cl) 364 struct ocfs2_chain_list *cl)
335{ 365{
336 int status = 0; 366 int status = 0;
367 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
337 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 368 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
338 struct super_block * sb = alloc_inode->i_sb; 369 struct super_block * sb = alloc_inode->i_sb;
339 370
@@ -360,12 +391,18 @@ static int ocfs2_block_group_fill(handle_t *handle,
360 memset(bg, 0, sb->s_blocksize); 391 memset(bg, 0, sb->s_blocksize);
361 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE); 392 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
362 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); 393 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
363 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb)); 394 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
364 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl)); 395 osb->s_feature_incompat));
365 bg->bg_chain = cpu_to_le16(my_chain); 396 bg->bg_chain = cpu_to_le16(my_chain);
366 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno; 397 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
367 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno); 398 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
368 bg->bg_blkno = cpu_to_le64(group_blkno); 399 bg->bg_blkno = cpu_to_le64(group_blkno);
400 if (group_clusters == le16_to_cpu(cl->cl_cpg))
401 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
402 else
403 ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
404 group_clusters);
405
369 /* set the 1st bit in the bitmap to account for the descriptor block */ 406 /* set the 1st bit in the bitmap to account for the descriptor block */
370 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap); 407 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
371 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1); 408 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
@@ -396,6 +433,238 @@ static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
396 return best; 433 return best;
397} 434}
398 435
436static struct buffer_head *
437ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
438 struct inode *alloc_inode,
439 struct ocfs2_alloc_context *ac,
440 struct ocfs2_chain_list *cl)
441{
442 int status;
443 u32 bit_off, num_bits;
444 u64 bg_blkno;
445 struct buffer_head *bg_bh;
446 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
447
448 status = ocfs2_claim_clusters(handle, ac,
449 le16_to_cpu(cl->cl_cpg), &bit_off,
450 &num_bits);
451 if (status < 0) {
452 if (status != -ENOSPC)
453 mlog_errno(status);
454 goto bail;
455 }
456
457 /* setup the group */
458 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
459 mlog(0, "new descriptor, record %u, at block %llu\n",
460 alloc_rec, (unsigned long long)bg_blkno);
461
462 bg_bh = sb_getblk(osb->sb, bg_blkno);
463 if (!bg_bh) {
464 status = -EIO;
465 mlog_errno(status);
466 goto bail;
467 }
468 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
469
470 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
471 bg_blkno, num_bits, alloc_rec, cl);
472 if (status < 0) {
473 brelse(bg_bh);
474 mlog_errno(status);
475 }
476
477bail:
478 return status ? ERR_PTR(status) : bg_bh;
479}
480
481static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
482 handle_t *handle,
483 struct ocfs2_alloc_context *ac,
484 unsigned int min_bits,
485 u32 *bit_off, u32 *num_bits)
486{
487 int status;
488
489 while (min_bits) {
490 status = ocfs2_claim_clusters(handle, ac, min_bits,
491 bit_off, num_bits);
492 if (status != -ENOSPC)
493 break;
494
495 min_bits >>= 1;
496 }
497
498 return status;
499}
500
501static int ocfs2_block_group_grow_discontig(handle_t *handle,
502 struct inode *alloc_inode,
503 struct buffer_head *bg_bh,
504 struct ocfs2_alloc_context *ac,
505 struct ocfs2_chain_list *cl,
506 unsigned int min_bits)
507{
508 int status;
509 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
510 struct ocfs2_group_desc *bg =
511 (struct ocfs2_group_desc *)bg_bh->b_data;
512 unsigned int needed = le16_to_cpu(cl->cl_cpg) -
513 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
514 u32 p_cpos, clusters;
515 u64 p_blkno;
516 struct ocfs2_extent_list *el = &bg->bg_list;
517
518 status = ocfs2_journal_access_gd(handle,
519 INODE_CACHE(alloc_inode),
520 bg_bh,
521 OCFS2_JOURNAL_ACCESS_CREATE);
522 if (status < 0) {
523 mlog_errno(status);
524 goto bail;
525 }
526
527 while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
528 le16_to_cpu(el->l_count))) {
529 if (min_bits > needed)
530 min_bits = needed;
531 status = ocfs2_block_group_claim_bits(osb, handle, ac,
532 min_bits, &p_cpos,
533 &clusters);
534 if (status < 0) {
535 if (status != -ENOSPC)
536 mlog_errno(status);
537 goto bail;
538 }
539 p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
540 ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
541 clusters);
542
543 min_bits = clusters;
544 needed = le16_to_cpu(cl->cl_cpg) -
545 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
546 }
547
548 if (needed > 0) {
549 /*
550 * We have used up all the extent rec but can't fill up
551 * the cpg. So bail out.
552 */
553 status = -ENOSPC;
554 goto bail;
555 }
556
557 ocfs2_journal_dirty(handle, bg_bh);
558
559bail:
560 return status;
561}
562
563static void ocfs2_bg_alloc_cleanup(handle_t *handle,
564 struct ocfs2_alloc_context *cluster_ac,
565 struct inode *alloc_inode,
566 struct buffer_head *bg_bh)
567{
568 int i, ret;
569 struct ocfs2_group_desc *bg;
570 struct ocfs2_extent_list *el;
571 struct ocfs2_extent_rec *rec;
572
573 if (!bg_bh)
574 return;
575
576 bg = (struct ocfs2_group_desc *)bg_bh->b_data;
577 el = &bg->bg_list;
578 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
579 rec = &el->l_recs[i];
580 ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
581 cluster_ac->ac_bh,
582 le64_to_cpu(rec->e_blkno),
583 le32_to_cpu(rec->e_leaf_clusters));
584 if (ret)
585 mlog_errno(ret);
586 /* Try all the clusters to free */
587 }
588
589 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
590 brelse(bg_bh);
591}
592
593static struct buffer_head *
594ocfs2_block_group_alloc_discontig(handle_t *handle,
595 struct inode *alloc_inode,
596 struct ocfs2_alloc_context *ac,
597 struct ocfs2_chain_list *cl)
598{
599 int status;
600 u32 bit_off, num_bits;
601 u64 bg_blkno;
602 unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
603 struct buffer_head *bg_bh = NULL;
604 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
605 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
606
607 if (!ocfs2_supports_discontig_bg(osb)) {
608 status = -ENOSPC;
609 goto bail;
610 }
611
612 status = ocfs2_extend_trans(handle,
613 ocfs2_calc_bg_discontig_credits(osb->sb));
614 if (status) {
615 mlog_errno(status);
616 goto bail;
617 }
618
619 /*
620 * We're going to be grabbing from multiple cluster groups.
621 * We don't have enough credits to relink them all, and the
622 * cluster groups will be staying in cache for the duration of
623 * this operation.
624 */
625 ac->ac_allow_chain_relink = 0;
626
627 /* Claim the first region */
628 status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
629 &bit_off, &num_bits);
630 if (status < 0) {
631 if (status != -ENOSPC)
632 mlog_errno(status);
633 goto bail;
634 }
635 min_bits = num_bits;
636
637 /* setup the group */
638 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
639 mlog(0, "new descriptor, record %u, at block %llu\n",
640 alloc_rec, (unsigned long long)bg_blkno);
641
642 bg_bh = sb_getblk(osb->sb, bg_blkno);
643 if (!bg_bh) {
644 status = -EIO;
645 mlog_errno(status);
646 goto bail;
647 }
648 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
649
650 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
651 bg_blkno, num_bits, alloc_rec, cl);
652 if (status < 0) {
653 mlog_errno(status);
654 goto bail;
655 }
656
657 status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
658 bg_bh, ac, cl, min_bits);
659 if (status)
660 mlog_errno(status);
661
662bail:
663 if (status)
664 ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
665 return status ? ERR_PTR(status) : bg_bh;
666}
667
399/* 668/*
400 * We expect the block group allocator to already be locked. 669 * We expect the block group allocator to already be locked.
401 */ 670 */
@@ -411,9 +680,7 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
411 struct ocfs2_chain_list *cl; 680 struct ocfs2_chain_list *cl;
412 struct ocfs2_alloc_context *ac = NULL; 681 struct ocfs2_alloc_context *ac = NULL;
413 handle_t *handle = NULL; 682 handle_t *handle = NULL;
414 u32 bit_off, num_bits;
415 u16 alloc_rec; 683 u16 alloc_rec;
416 u64 bg_blkno;
417 struct buffer_head *bg_bh = NULL; 684 struct buffer_head *bg_bh = NULL;
418 struct ocfs2_group_desc *bg; 685 struct ocfs2_group_desc *bg;
419 686
@@ -446,44 +713,20 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
446 (unsigned long long)*last_alloc_group); 713 (unsigned long long)*last_alloc_group);
447 ac->ac_last_group = *last_alloc_group; 714 ac->ac_last_group = *last_alloc_group;
448 } 715 }
449 status = ocfs2_claim_clusters(osb, 716
450 handle, 717 bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
451 ac, 718 ac, cl);
452 le16_to_cpu(cl->cl_cpg), 719 if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
453 &bit_off, 720 bg_bh = ocfs2_block_group_alloc_discontig(handle,
454 &num_bits); 721 alloc_inode,
455 if (status < 0) { 722 ac, cl);
723 if (IS_ERR(bg_bh)) {
724 status = PTR_ERR(bg_bh);
725 bg_bh = NULL;
456 if (status != -ENOSPC) 726 if (status != -ENOSPC)
457 mlog_errno(status); 727 mlog_errno(status);
458 goto bail; 728 goto bail;
459 } 729 }
460
461 alloc_rec = ocfs2_find_smallest_chain(cl);
462
463 /* setup the group */
464 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
465 mlog(0, "new descriptor, record %u, at block %llu\n",
466 alloc_rec, (unsigned long long)bg_blkno);
467
468 bg_bh = sb_getblk(osb->sb, bg_blkno);
469 if (!bg_bh) {
470 status = -EIO;
471 mlog_errno(status);
472 goto bail;
473 }
474 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
475
476 status = ocfs2_block_group_fill(handle,
477 alloc_inode,
478 bg_bh,
479 bg_blkno,
480 alloc_rec,
481 cl);
482 if (status < 0) {
483 mlog_errno(status);
484 goto bail;
485 }
486
487 bg = (struct ocfs2_group_desc *) bg_bh->b_data; 730 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
488 731
489 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 732 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
@@ -493,10 +736,12 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
493 goto bail; 736 goto bail;
494 } 737 }
495 738
739 alloc_rec = le16_to_cpu(bg->bg_chain);
496 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free, 740 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
497 le16_to_cpu(bg->bg_free_bits_count)); 741 le16_to_cpu(bg->bg_free_bits_count));
498 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits)); 742 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
499 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno); 743 le16_to_cpu(bg->bg_bits));
744 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg->bg_blkno);
500 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count)) 745 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
501 le16_add_cpu(&cl->cl_next_free_rec, 1); 746 le16_add_cpu(&cl->cl_next_free_rec, 1);
502 747
@@ -1024,8 +1269,7 @@ static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1024 struct buffer_head *bg_bh, 1269 struct buffer_head *bg_bh,
1025 unsigned int bits_wanted, 1270 unsigned int bits_wanted,
1026 unsigned int total_bits, 1271 unsigned int total_bits,
1027 u16 *bit_off, 1272 struct ocfs2_suballoc_result *res)
1028 u16 *bits_found)
1029{ 1273{
1030 void *bitmap; 1274 void *bitmap;
1031 u16 best_offset, best_size; 1275 u16 best_offset, best_size;
@@ -1069,14 +1313,9 @@ static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1069 } 1313 }
1070 } 1314 }
1071 1315
1072 /* XXX: I think the first clause is equivalent to the second 1316 if (best_size) {
1073 * - jlbec */ 1317 res->sr_bit_offset = best_offset;
1074 if (found == bits_wanted) { 1318 res->sr_bits = best_size;
1075 *bit_off = start - found;
1076 *bits_found = found;
1077 } else if (best_size) {
1078 *bit_off = best_offset;
1079 *bits_found = best_size;
1080 } else { 1319 } else {
1081 status = -ENOSPC; 1320 status = -ENOSPC;
1082 /* No error log here -- see the comment above 1321 /* No error log here -- see the comment above
@@ -1232,14 +1471,13 @@ static int ocfs2_cluster_group_search(struct inode *inode,
1232 struct buffer_head *group_bh, 1471 struct buffer_head *group_bh,
1233 u32 bits_wanted, u32 min_bits, 1472 u32 bits_wanted, u32 min_bits,
1234 u64 max_block, 1473 u64 max_block,
1235 u16 *bit_off, u16 *bits_found) 1474 struct ocfs2_suballoc_result *res)
1236{ 1475{
1237 int search = -ENOSPC; 1476 int search = -ENOSPC;
1238 int ret; 1477 int ret;
1239 u64 blkoff; 1478 u64 blkoff;
1240 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data; 1479 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1241 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1480 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1242 u16 tmp_off, tmp_found;
1243 unsigned int max_bits, gd_cluster_off; 1481 unsigned int max_bits, gd_cluster_off;
1244 1482
1245 BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 1483 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
@@ -1266,15 +1504,15 @@ static int ocfs2_cluster_group_search(struct inode *inode,
1266 1504
1267 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1505 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1268 group_bh, bits_wanted, 1506 group_bh, bits_wanted,
1269 max_bits, 1507 max_bits, res);
1270 &tmp_off, &tmp_found);
1271 if (ret) 1508 if (ret)
1272 return ret; 1509 return ret;
1273 1510
1274 if (max_block) { 1511 if (max_block) {
1275 blkoff = ocfs2_clusters_to_blocks(inode->i_sb, 1512 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1276 gd_cluster_off + 1513 gd_cluster_off +
1277 tmp_off + tmp_found); 1514 res->sr_bit_offset +
1515 res->sr_bits);
1278 mlog(0, "Checking %llu against %llu\n", 1516 mlog(0, "Checking %llu against %llu\n",
1279 (unsigned long long)blkoff, 1517 (unsigned long long)blkoff,
1280 (unsigned long long)max_block); 1518 (unsigned long long)max_block);
@@ -1286,16 +1524,14 @@ static int ocfs2_cluster_group_search(struct inode *inode,
1286 * return success, but we still want to return 1524 * return success, but we still want to return
1287 * -ENOSPC unless it found the minimum number 1525 * -ENOSPC unless it found the minimum number
1288 * of bits. */ 1526 * of bits. */
1289 if (min_bits <= tmp_found) { 1527 if (min_bits <= res->sr_bits)
1290 *bit_off = tmp_off;
1291 *bits_found = tmp_found;
1292 search = 0; /* success */ 1528 search = 0; /* success */
1293 } else if (tmp_found) { 1529 else if (res->sr_bits) {
1294 /* 1530 /*
1295 * Don't show bits which we'll be returning 1531 * Don't show bits which we'll be returning
1296 * for allocation to the local alloc bitmap. 1532 * for allocation to the local alloc bitmap.
1297 */ 1533 */
1298 ocfs2_local_alloc_seen_free_bits(osb, tmp_found); 1534 ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
1299 } 1535 }
1300 } 1536 }
1301 1537
@@ -1306,7 +1542,7 @@ static int ocfs2_block_group_search(struct inode *inode,
1306 struct buffer_head *group_bh, 1542 struct buffer_head *group_bh,
1307 u32 bits_wanted, u32 min_bits, 1543 u32 bits_wanted, u32 min_bits,
1308 u64 max_block, 1544 u64 max_block,
1309 u16 *bit_off, u16 *bits_found) 1545 struct ocfs2_suballoc_result *res)
1310{ 1546{
1311 int ret = -ENOSPC; 1547 int ret = -ENOSPC;
1312 u64 blkoff; 1548 u64 blkoff;
@@ -1319,10 +1555,10 @@ static int ocfs2_block_group_search(struct inode *inode,
1319 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1555 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1320 group_bh, bits_wanted, 1556 group_bh, bits_wanted,
1321 le16_to_cpu(bg->bg_bits), 1557 le16_to_cpu(bg->bg_bits),
1322 bit_off, bits_found); 1558 res);
1323 if (!ret && max_block) { 1559 if (!ret && max_block) {
1324 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off + 1560 blkoff = le64_to_cpu(bg->bg_blkno) +
1325 *bits_found; 1561 res->sr_bit_offset + res->sr_bits;
1326 mlog(0, "Checking %llu against %llu\n", 1562 mlog(0, "Checking %llu against %llu\n",
1327 (unsigned long long)blkoff, 1563 (unsigned long long)blkoff,
1328 (unsigned long long)max_block); 1564 (unsigned long long)max_block);
@@ -1361,24 +1597,70 @@ out:
1361 return ret; 1597 return ret;
1362} 1598}
1363 1599
1600static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
1601 struct ocfs2_extent_rec *rec,
1602 struct ocfs2_chain_list *cl)
1603{
1604 unsigned int bpc = le16_to_cpu(cl->cl_bpc);
1605 unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
1606 unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc;
1607
1608 if (res->sr_bit_offset < bitoff)
1609 return 0;
1610 if (res->sr_bit_offset >= (bitoff + bitcount))
1611 return 0;
1612 res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1613 (res->sr_bit_offset - bitoff);
1614 if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
1615 res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
1616 return 1;
1617}
1618
1619static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
1620 struct ocfs2_group_desc *bg,
1621 struct ocfs2_suballoc_result *res)
1622{
1623 int i;
1624 u64 bg_blkno = res->sr_bg_blkno; /* Save off */
1625 struct ocfs2_extent_rec *rec;
1626 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1627 struct ocfs2_chain_list *cl = &di->id2.i_chain;
1628
1629 if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1630 res->sr_blkno = 0;
1631 return;
1632 }
1633
1634 res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
1635 res->sr_bg_blkno = 0; /* Clear it for contig block groups */
1636 if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
1637 !bg->bg_list.l_next_free_rec)
1638 return;
1639
1640 for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
1641 rec = &bg->bg_list.l_recs[i];
1642 if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
1643 res->sr_bg_blkno = bg_blkno; /* Restore */
1644 break;
1645 }
1646 }
1647}
1648
1364static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac, 1649static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1365 handle_t *handle, 1650 handle_t *handle,
1366 u32 bits_wanted, 1651 u32 bits_wanted,
1367 u32 min_bits, 1652 u32 min_bits,
1368 u16 *bit_off, 1653 struct ocfs2_suballoc_result *res,
1369 unsigned int *num_bits,
1370 u64 gd_blkno,
1371 u16 *bits_left) 1654 u16 *bits_left)
1372{ 1655{
1373 int ret; 1656 int ret;
1374 u16 found;
1375 struct buffer_head *group_bh = NULL; 1657 struct buffer_head *group_bh = NULL;
1376 struct ocfs2_group_desc *gd; 1658 struct ocfs2_group_desc *gd;
1377 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; 1659 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1378 struct inode *alloc_inode = ac->ac_inode; 1660 struct inode *alloc_inode = ac->ac_inode;
1379 1661
1380 ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno, 1662 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1381 &group_bh); 1663 res->sr_bg_blkno, &group_bh);
1382 if (ret < 0) { 1664 if (ret < 0) {
1383 mlog_errno(ret); 1665 mlog_errno(ret);
1384 return ret; 1666 return ret;
@@ -1386,17 +1668,18 @@ static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1386 1668
1387 gd = (struct ocfs2_group_desc *) group_bh->b_data; 1669 gd = (struct ocfs2_group_desc *) group_bh->b_data;
1388 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits, 1670 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1389 ac->ac_max_block, bit_off, &found); 1671 ac->ac_max_block, res);
1390 if (ret < 0) { 1672 if (ret < 0) {
1391 if (ret != -ENOSPC) 1673 if (ret != -ENOSPC)
1392 mlog_errno(ret); 1674 mlog_errno(ret);
1393 goto out; 1675 goto out;
1394 } 1676 }
1395 1677
1396 *num_bits = found; 1678 if (!ret)
1679 ocfs2_bg_discontig_fix_result(ac, gd, res);
1397 1680
1398 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh, 1681 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1399 *num_bits, 1682 res->sr_bits,
1400 le16_to_cpu(gd->bg_chain)); 1683 le16_to_cpu(gd->bg_chain));
1401 if (ret < 0) { 1684 if (ret < 0) {
1402 mlog_errno(ret); 1685 mlog_errno(ret);
@@ -1404,7 +1687,7 @@ static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1404 } 1687 }
1405 1688
1406 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh, 1689 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1407 *bit_off, *num_bits); 1690 res->sr_bit_offset, res->sr_bits);
1408 if (ret < 0) 1691 if (ret < 0)
1409 mlog_errno(ret); 1692 mlog_errno(ret);
1410 1693
@@ -1420,13 +1703,11 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1420 handle_t *handle, 1703 handle_t *handle,
1421 u32 bits_wanted, 1704 u32 bits_wanted,
1422 u32 min_bits, 1705 u32 min_bits,
1423 u16 *bit_off, 1706 struct ocfs2_suballoc_result *res,
1424 unsigned int *num_bits,
1425 u64 *bg_blkno,
1426 u16 *bits_left) 1707 u16 *bits_left)
1427{ 1708{
1428 int status; 1709 int status;
1429 u16 chain, tmp_bits; 1710 u16 chain;
1430 u32 tmp_used; 1711 u32 tmp_used;
1431 u64 next_group; 1712 u64 next_group;
1432 struct inode *alloc_inode = ac->ac_inode; 1713 struct inode *alloc_inode = ac->ac_inode;
@@ -1455,8 +1736,8 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1455 * the 1st group with any empty bits. */ 1736 * the 1st group with any empty bits. */
1456 while ((status = ac->ac_group_search(alloc_inode, group_bh, 1737 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1457 bits_wanted, min_bits, 1738 bits_wanted, min_bits,
1458 ac->ac_max_block, bit_off, 1739 ac->ac_max_block,
1459 &tmp_bits)) == -ENOSPC) { 1740 res)) == -ENOSPC) {
1460 if (!bg->bg_next_group) 1741 if (!bg->bg_next_group)
1461 break; 1742 break;
1462 1743
@@ -1481,11 +1762,14 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1481 } 1762 }
1482 1763
1483 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n", 1764 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1484 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno)); 1765 res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1766
1767 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
1485 1768
1486 *num_bits = tmp_bits; 1769 BUG_ON(res->sr_bits == 0);
1770 if (!status)
1771 ocfs2_bg_discontig_fix_result(ac, bg, res);
1487 1772
1488 BUG_ON(*num_bits == 0);
1489 1773
1490 /* 1774 /*
1491 * Keep track of previous block descriptor read. When 1775 * Keep track of previous block descriptor read. When
@@ -1502,7 +1786,7 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1502 */ 1786 */
1503 if (ac->ac_allow_chain_relink && 1787 if (ac->ac_allow_chain_relink &&
1504 (prev_group_bh) && 1788 (prev_group_bh) &&
1505 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) { 1789 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
1506 status = ocfs2_relink_block_group(handle, alloc_inode, 1790 status = ocfs2_relink_block_group(handle, alloc_inode,
1507 ac->ac_bh, group_bh, 1791 ac->ac_bh, group_bh,
1508 prev_group_bh, chain); 1792 prev_group_bh, chain);
@@ -1524,25 +1808,24 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1524 } 1808 }
1525 1809
1526 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used); 1810 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1527 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used); 1811 fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used);
1528 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits)); 1812 le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits);
1529 ocfs2_journal_dirty(handle, ac->ac_bh); 1813 ocfs2_journal_dirty(handle, ac->ac_bh);
1530 1814
1531 status = ocfs2_block_group_set_bits(handle, 1815 status = ocfs2_block_group_set_bits(handle,
1532 alloc_inode, 1816 alloc_inode,
1533 bg, 1817 bg,
1534 group_bh, 1818 group_bh,
1535 *bit_off, 1819 res->sr_bit_offset,
1536 *num_bits); 1820 res->sr_bits);
1537 if (status < 0) { 1821 if (status < 0) {
1538 mlog_errno(status); 1822 mlog_errno(status);
1539 goto bail; 1823 goto bail;
1540 } 1824 }
1541 1825
1542 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits, 1826 mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
1543 (unsigned long long)le64_to_cpu(fe->i_blkno)); 1827 (unsigned long long)le64_to_cpu(fe->i_blkno));
1544 1828
1545 *bg_blkno = le64_to_cpu(bg->bg_blkno);
1546 *bits_left = le16_to_cpu(bg->bg_free_bits_count); 1829 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
1547bail: 1830bail:
1548 brelse(group_bh); 1831 brelse(group_bh);
@@ -1553,19 +1836,15 @@ bail:
1553} 1836}
1554 1837
1555/* will give out up to bits_wanted contiguous bits. */ 1838/* will give out up to bits_wanted contiguous bits. */
1556static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb, 1839static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
1557 struct ocfs2_alloc_context *ac,
1558 handle_t *handle, 1840 handle_t *handle,
1559 u32 bits_wanted, 1841 u32 bits_wanted,
1560 u32 min_bits, 1842 u32 min_bits,
1561 u16 *bit_off, 1843 struct ocfs2_suballoc_result *res)
1562 unsigned int *num_bits,
1563 u64 *bg_blkno)
1564{ 1844{
1565 int status; 1845 int status;
1566 u16 victim, i; 1846 u16 victim, i;
1567 u16 bits_left = 0; 1847 u16 bits_left = 0;
1568 u64 hint_blkno = ac->ac_last_group;
1569 struct ocfs2_chain_list *cl; 1848 struct ocfs2_chain_list *cl;
1570 struct ocfs2_dinode *fe; 1849 struct ocfs2_dinode *fe;
1571 1850
@@ -1583,7 +1862,8 @@ static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1583 1862
1584 if (le32_to_cpu(fe->id1.bitmap1.i_used) >= 1863 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1585 le32_to_cpu(fe->id1.bitmap1.i_total)) { 1864 le32_to_cpu(fe->id1.bitmap1.i_total)) {
1586 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used " 1865 ocfs2_error(ac->ac_inode->i_sb,
1866 "Chain allocator dinode %llu has %u used "
1587 "bits but only %u total.", 1867 "bits but only %u total.",
1588 (unsigned long long)le64_to_cpu(fe->i_blkno), 1868 (unsigned long long)le64_to_cpu(fe->i_blkno),
1589 le32_to_cpu(fe->id1.bitmap1.i_used), 1869 le32_to_cpu(fe->id1.bitmap1.i_used),
@@ -1592,22 +1872,16 @@ static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1592 goto bail; 1872 goto bail;
1593 } 1873 }
1594 1874
1595 if (hint_blkno) { 1875 res->sr_bg_blkno = ac->ac_last_group;
1876 if (res->sr_bg_blkno) {
1596 /* Attempt to short-circuit the usual search mechanism 1877 /* Attempt to short-circuit the usual search mechanism
1597 * by jumping straight to the most recently used 1878 * by jumping straight to the most recently used
1598 * allocation group. This helps us mantain some 1879 * allocation group. This helps us mantain some
1599 * contiguousness across allocations. */ 1880 * contiguousness across allocations. */
1600 status = ocfs2_search_one_group(ac, handle, bits_wanted, 1881 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1601 min_bits, bit_off, num_bits, 1882 min_bits, res, &bits_left);
1602 hint_blkno, &bits_left); 1883 if (!status)
1603 if (!status) {
1604 /* Be careful to update *bg_blkno here as the
1605 * caller is expecting it to be filled in, and
1606 * ocfs2_search_one_group() won't do that for
1607 * us. */
1608 *bg_blkno = hint_blkno;
1609 goto set_hint; 1884 goto set_hint;
1610 }
1611 if (status < 0 && status != -ENOSPC) { 1885 if (status < 0 && status != -ENOSPC) {
1612 mlog_errno(status); 1886 mlog_errno(status);
1613 goto bail; 1887 goto bail;
@@ -1620,8 +1894,8 @@ static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1620 ac->ac_chain = victim; 1894 ac->ac_chain = victim;
1621 ac->ac_allow_chain_relink = 1; 1895 ac->ac_allow_chain_relink = 1;
1622 1896
1623 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off, 1897 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1624 num_bits, bg_blkno, &bits_left); 1898 res, &bits_left);
1625 if (!status) 1899 if (!status)
1626 goto set_hint; 1900 goto set_hint;
1627 if (status < 0 && status != -ENOSPC) { 1901 if (status < 0 && status != -ENOSPC) {
@@ -1645,8 +1919,7 @@ static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1645 1919
1646 ac->ac_chain = i; 1920 ac->ac_chain = i;
1647 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, 1921 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1648 bit_off, num_bits, bg_blkno, 1922 res, &bits_left);
1649 &bits_left);
1650 if (!status) 1923 if (!status)
1651 break; 1924 break;
1652 if (status < 0 && status != -ENOSPC) { 1925 if (status < 0 && status != -ENOSPC) {
@@ -1663,7 +1936,7 @@ set_hint:
1663 if (bits_left < min_bits) 1936 if (bits_left < min_bits)
1664 ac->ac_last_group = 0; 1937 ac->ac_last_group = 0;
1665 else 1938 else
1666 ac->ac_last_group = *bg_blkno; 1939 ac->ac_last_group = res->sr_bg_blkno;
1667 } 1940 }
1668 1941
1669bail: 1942bail:
@@ -1671,37 +1944,37 @@ bail:
1671 return status; 1944 return status;
1672} 1945}
1673 1946
1674int ocfs2_claim_metadata(struct ocfs2_super *osb, 1947int ocfs2_claim_metadata(handle_t *handle,
1675 handle_t *handle,
1676 struct ocfs2_alloc_context *ac, 1948 struct ocfs2_alloc_context *ac,
1677 u32 bits_wanted, 1949 u32 bits_wanted,
1950 u64 *suballoc_loc,
1678 u16 *suballoc_bit_start, 1951 u16 *suballoc_bit_start,
1679 unsigned int *num_bits, 1952 unsigned int *num_bits,
1680 u64 *blkno_start) 1953 u64 *blkno_start)
1681{ 1954{
1682 int status; 1955 int status;
1683 u64 bg_blkno; 1956 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
1684 1957
1685 BUG_ON(!ac); 1958 BUG_ON(!ac);
1686 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted)); 1959 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1687 BUG_ON(ac->ac_which != OCFS2_AC_USE_META); 1960 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1688 1961
1689 status = ocfs2_claim_suballoc_bits(osb, 1962 status = ocfs2_claim_suballoc_bits(ac,
1690 ac,
1691 handle, 1963 handle,
1692 bits_wanted, 1964 bits_wanted,
1693 1, 1965 1,
1694 suballoc_bit_start, 1966 &res);
1695 num_bits,
1696 &bg_blkno);
1697 if (status < 0) { 1967 if (status < 0) {
1698 mlog_errno(status); 1968 mlog_errno(status);
1699 goto bail; 1969 goto bail;
1700 } 1970 }
1701 atomic_inc(&osb->alloc_stats.bg_allocs); 1971 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
1702 1972
1703 *blkno_start = bg_blkno + (u64) *suballoc_bit_start; 1973 *suballoc_loc = res.sr_bg_blkno;
1704 ac->ac_bits_given += (*num_bits); 1974 *suballoc_bit_start = res.sr_bit_offset;
1975 *blkno_start = res.sr_blkno;
1976 ac->ac_bits_given += res.sr_bits;
1977 *num_bits = res.sr_bits;
1705 status = 0; 1978 status = 0;
1706bail: 1979bail:
1707 mlog_exit(status); 1980 mlog_exit(status);
@@ -1709,10 +1982,10 @@ bail:
1709} 1982}
1710 1983
1711static void ocfs2_init_inode_ac_group(struct inode *dir, 1984static void ocfs2_init_inode_ac_group(struct inode *dir,
1712 struct buffer_head *parent_fe_bh, 1985 struct buffer_head *parent_di_bh,
1713 struct ocfs2_alloc_context *ac) 1986 struct ocfs2_alloc_context *ac)
1714{ 1987{
1715 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data; 1988 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
1716 /* 1989 /*
1717 * Try to allocate inodes from some specific group. 1990 * Try to allocate inodes from some specific group.
1718 * 1991 *
@@ -1726,10 +1999,14 @@ static void ocfs2_init_inode_ac_group(struct inode *dir,
1726 if (OCFS2_I(dir)->ip_last_used_group && 1999 if (OCFS2_I(dir)->ip_last_used_group &&
1727 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot) 2000 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1728 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group; 2001 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1729 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot) 2002 else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
1730 ac->ac_last_group = ocfs2_which_suballoc_group( 2003 if (di->i_suballoc_loc)
1731 le64_to_cpu(fe->i_blkno), 2004 ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
1732 le16_to_cpu(fe->i_suballoc_bit)); 2005 else
2006 ac->ac_last_group = ocfs2_which_suballoc_group(
2007 le64_to_cpu(di->i_blkno),
2008 le16_to_cpu(di->i_suballoc_bit));
2009 }
1733} 2010}
1734 2011
1735static inline void ocfs2_save_inode_ac_group(struct inode *dir, 2012static inline void ocfs2_save_inode_ac_group(struct inode *dir,
@@ -1739,17 +2016,16 @@ static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1739 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot; 2016 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1740} 2017}
1741 2018
1742int ocfs2_claim_new_inode(struct ocfs2_super *osb, 2019int ocfs2_claim_new_inode(handle_t *handle,
1743 handle_t *handle,
1744 struct inode *dir, 2020 struct inode *dir,
1745 struct buffer_head *parent_fe_bh, 2021 struct buffer_head *parent_fe_bh,
1746 struct ocfs2_alloc_context *ac, 2022 struct ocfs2_alloc_context *ac,
2023 u64 *suballoc_loc,
1747 u16 *suballoc_bit, 2024 u16 *suballoc_bit,
1748 u64 *fe_blkno) 2025 u64 *fe_blkno)
1749{ 2026{
1750 int status; 2027 int status;
1751 unsigned int num_bits; 2028 struct ocfs2_suballoc_result res;
1752 u64 bg_blkno;
1753 2029
1754 mlog_entry_void(); 2030 mlog_entry_void();
1755 2031
@@ -1760,23 +2036,22 @@ int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1760 2036
1761 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac); 2037 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
1762 2038
1763 status = ocfs2_claim_suballoc_bits(osb, 2039 status = ocfs2_claim_suballoc_bits(ac,
1764 ac,
1765 handle, 2040 handle,
1766 1, 2041 1,
1767 1, 2042 1,
1768 suballoc_bit, 2043 &res);
1769 &num_bits,
1770 &bg_blkno);
1771 if (status < 0) { 2044 if (status < 0) {
1772 mlog_errno(status); 2045 mlog_errno(status);
1773 goto bail; 2046 goto bail;
1774 } 2047 }
1775 atomic_inc(&osb->alloc_stats.bg_allocs); 2048 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
1776 2049
1777 BUG_ON(num_bits != 1); 2050 BUG_ON(res.sr_bits != 1);
1778 2051
1779 *fe_blkno = bg_blkno + (u64) (*suballoc_bit); 2052 *suballoc_loc = res.sr_bg_blkno;
2053 *suballoc_bit = res.sr_bit_offset;
2054 *fe_blkno = res.sr_blkno;
1780 ac->ac_bits_given++; 2055 ac->ac_bits_given++;
1781 ocfs2_save_inode_ac_group(dir, ac); 2056 ocfs2_save_inode_ac_group(dir, ac);
1782 status = 0; 2057 status = 0;
@@ -1846,8 +2121,7 @@ static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1846 * contig. allocation, set to '1' to indicate we can deal with extents 2121 * contig. allocation, set to '1' to indicate we can deal with extents
1847 * of any size. 2122 * of any size.
1848 */ 2123 */
1849int __ocfs2_claim_clusters(struct ocfs2_super *osb, 2124int __ocfs2_claim_clusters(handle_t *handle,
1850 handle_t *handle,
1851 struct ocfs2_alloc_context *ac, 2125 struct ocfs2_alloc_context *ac,
1852 u32 min_clusters, 2126 u32 min_clusters,
1853 u32 max_clusters, 2127 u32 max_clusters,
@@ -1856,8 +2130,8 @@ int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1856{ 2130{
1857 int status; 2131 int status;
1858 unsigned int bits_wanted = max_clusters; 2132 unsigned int bits_wanted = max_clusters;
1859 u64 bg_blkno = 0; 2133 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
1860 u16 bg_bit_off; 2134 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
1861 2135
1862 mlog_entry_void(); 2136 mlog_entry_void();
1863 2137
@@ -1891,20 +2165,19 @@ int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1891 if (bits_wanted > (osb->bitmap_cpg - 1)) 2165 if (bits_wanted > (osb->bitmap_cpg - 1))
1892 bits_wanted = osb->bitmap_cpg - 1; 2166 bits_wanted = osb->bitmap_cpg - 1;
1893 2167
1894 status = ocfs2_claim_suballoc_bits(osb, 2168 status = ocfs2_claim_suballoc_bits(ac,
1895 ac,
1896 handle, 2169 handle,
1897 bits_wanted, 2170 bits_wanted,
1898 min_clusters, 2171 min_clusters,
1899 &bg_bit_off, 2172 &res);
1900 num_clusters,
1901 &bg_blkno);
1902 if (!status) { 2173 if (!status) {
2174 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
1903 *cluster_start = 2175 *cluster_start =
1904 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode, 2176 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1905 bg_blkno, 2177 res.sr_bg_blkno,
1906 bg_bit_off); 2178 res.sr_bit_offset);
1907 atomic_inc(&osb->alloc_stats.bitmap_data); 2179 atomic_inc(&osb->alloc_stats.bitmap_data);
2180 *num_clusters = res.sr_bits;
1908 } 2181 }
1909 } 2182 }
1910 if (status < 0) { 2183 if (status < 0) {
@@ -1920,8 +2193,7 @@ bail:
1920 return status; 2193 return status;
1921} 2194}
1922 2195
1923int ocfs2_claim_clusters(struct ocfs2_super *osb, 2196int ocfs2_claim_clusters(handle_t *handle,
1924 handle_t *handle,
1925 struct ocfs2_alloc_context *ac, 2197 struct ocfs2_alloc_context *ac,
1926 u32 min_clusters, 2198 u32 min_clusters,
1927 u32 *cluster_start, 2199 u32 *cluster_start,
@@ -1929,7 +2201,7 @@ int ocfs2_claim_clusters(struct ocfs2_super *osb,
1929{ 2201{
1930 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given; 2202 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1931 2203
1932 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters, 2204 return __ocfs2_claim_clusters(handle, ac, min_clusters,
1933 bits_wanted, cluster_start, num_clusters); 2205 bits_wanted, cluster_start, num_clusters);
1934} 2206}
1935 2207
@@ -2081,6 +2353,8 @@ int ocfs2_free_dinode(handle_t *handle,
2081 u16 bit = le16_to_cpu(di->i_suballoc_bit); 2353 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2082 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 2354 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2083 2355
2356 if (di->i_suballoc_loc)
2357 bg_blkno = le64_to_cpu(di->i_suballoc_loc);
2084 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode, 2358 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2085 inode_alloc_bh, bit, bg_blkno, 1); 2359 inode_alloc_bh, bit, bg_blkno, 1);
2086} 2360}
@@ -2350,7 +2624,7 @@ static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2350 struct buffer_head *alloc_bh, u64 blkno, 2624 struct buffer_head *alloc_bh, u64 blkno,
2351 u16 bit, int *res) 2625 u16 bit, int *res)
2352{ 2626{
2353 struct ocfs2_dinode *alloc_fe; 2627 struct ocfs2_dinode *alloc_di;
2354 struct ocfs2_group_desc *group; 2628 struct ocfs2_group_desc *group;
2355 struct buffer_head *group_bh = NULL; 2629 struct buffer_head *group_bh = NULL;
2356 u64 bg_blkno; 2630 u64 bg_blkno;
@@ -2359,17 +2633,20 @@ static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2359 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno, 2633 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2360 (unsigned int)bit); 2634 (unsigned int)bit);
2361 2635
2362 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data; 2636 alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
2363 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) { 2637 if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
2364 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n", 2638 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2365 (unsigned int)bit, 2639 (unsigned int)bit,
2366 ocfs2_bits_per_group(&alloc_fe->id2.i_chain)); 2640 ocfs2_bits_per_group(&alloc_di->id2.i_chain));
2367 status = -EINVAL; 2641 status = -EINVAL;
2368 goto bail; 2642 goto bail;
2369 } 2643 }
2370 2644
2371 bg_blkno = ocfs2_which_suballoc_group(blkno, bit); 2645 if (alloc_di->i_suballoc_loc)
2372 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno, 2646 bg_blkno = le64_to_cpu(alloc_di->i_suballoc_loc);
2647 else
2648 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2649 status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
2373 &group_bh); 2650 &group_bh);
2374 if (status < 0) { 2651 if (status < 0) {
2375 mlog(ML_ERROR, "read group %llu failed %d\n", 2652 mlog(ML_ERROR, "read group %llu failed %d\n",
diff --git a/fs/ocfs2/suballoc.h b/fs/ocfs2/suballoc.h
index da2f29a55ec3..a017dd3ee7d9 100644
--- a/fs/ocfs2/suballoc.h
+++ b/fs/ocfs2/suballoc.h
@@ -26,13 +26,14 @@
26#ifndef _CHAINALLOC_H_ 26#ifndef _CHAINALLOC_H_
27#define _CHAINALLOC_H_ 27#define _CHAINALLOC_H_
28 28
29struct ocfs2_suballoc_result;
29typedef int (group_search_t)(struct inode *, 30typedef int (group_search_t)(struct inode *,
30 struct buffer_head *, 31 struct buffer_head *,
31 u32, /* bits_wanted */ 32 u32, /* bits_wanted */
32 u32, /* min_bits */ 33 u32, /* min_bits */
33 u64, /* max_block */ 34 u64, /* max_block */
34 u16 *, /* *bit_off */ 35 struct ocfs2_suballoc_result *);
35 u16 *); /* *bits_found */ 36 /* found bits */
36 37
37struct ocfs2_alloc_context { 38struct ocfs2_alloc_context {
38 struct inode *ac_inode; /* which bitmap are we allocating from? */ 39 struct inode *ac_inode; /* which bitmap are we allocating from? */
@@ -82,22 +83,21 @@ int ocfs2_reserve_clusters(struct ocfs2_super *osb,
82 u32 bits_wanted, 83 u32 bits_wanted,
83 struct ocfs2_alloc_context **ac); 84 struct ocfs2_alloc_context **ac);
84 85
85int ocfs2_claim_metadata(struct ocfs2_super *osb, 86int ocfs2_claim_metadata(handle_t *handle,
86 handle_t *handle,
87 struct ocfs2_alloc_context *ac, 87 struct ocfs2_alloc_context *ac,
88 u32 bits_wanted, 88 u32 bits_wanted,
89 u64 *suballoc_loc,
89 u16 *suballoc_bit_start, 90 u16 *suballoc_bit_start,
90 u32 *num_bits, 91 u32 *num_bits,
91 u64 *blkno_start); 92 u64 *blkno_start);
92int ocfs2_claim_new_inode(struct ocfs2_super *osb, 93int ocfs2_claim_new_inode(handle_t *handle,
93 handle_t *handle,
94 struct inode *dir, 94 struct inode *dir,
95 struct buffer_head *parent_fe_bh, 95 struct buffer_head *parent_fe_bh,
96 struct ocfs2_alloc_context *ac, 96 struct ocfs2_alloc_context *ac,
97 u64 *suballoc_loc,
97 u16 *suballoc_bit, 98 u16 *suballoc_bit,
98 u64 *fe_blkno); 99 u64 *fe_blkno);
99int ocfs2_claim_clusters(struct ocfs2_super *osb, 100int ocfs2_claim_clusters(handle_t *handle,
100 handle_t *handle,
101 struct ocfs2_alloc_context *ac, 101 struct ocfs2_alloc_context *ac,
102 u32 min_clusters, 102 u32 min_clusters,
103 u32 *cluster_start, 103 u32 *cluster_start,
@@ -106,8 +106,7 @@ int ocfs2_claim_clusters(struct ocfs2_super *osb,
106 * Use this variant of ocfs2_claim_clusters to specify a maxiumum 106 * Use this variant of ocfs2_claim_clusters to specify a maxiumum
107 * number of clusters smaller than the allocation reserved. 107 * number of clusters smaller than the allocation reserved.
108 */ 108 */
109int __ocfs2_claim_clusters(struct ocfs2_super *osb, 109int __ocfs2_claim_clusters(handle_t *handle,
110 handle_t *handle,
111 struct ocfs2_alloc_context *ac, 110 struct ocfs2_alloc_context *ac,
112 u32 min_clusters, 111 u32 min_clusters,
113 u32 max_clusters, 112 u32 max_clusters,
diff --git a/fs/ocfs2/super.c b/fs/ocfs2/super.c
index cf6d87b57450..1c2c39f6f0b6 100644
--- a/fs/ocfs2/super.c
+++ b/fs/ocfs2/super.c
@@ -2277,7 +2277,8 @@ static int ocfs2_initialize_super(struct super_block *sb,
2277 osb->osb_clusters_at_boot = OCFS2_I(inode)->ip_clusters; 2277 osb->osb_clusters_at_boot = OCFS2_I(inode)->ip_clusters;
2278 iput(inode); 2278 iput(inode);
2279 2279
2280 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8; 2280 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb, 0,
2281 osb->s_feature_incompat) * 8;
2281 2282
2282 status = ocfs2_init_slot_info(osb); 2283 status = ocfs2_init_slot_info(osb);
2283 if (status < 0) { 2284 if (status < 0) {
diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c
index 38a55ff45b3a..a1cf195935cf 100644
--- a/fs/ocfs2/xattr.c
+++ b/fs/ocfs2/xattr.c
@@ -2466,7 +2466,10 @@ static int ocfs2_xattr_free_block(struct inode *inode,
2466 xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 2466 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2467 blk = le64_to_cpu(xb->xb_blkno); 2467 blk = le64_to_cpu(xb->xb_blkno);
2468 bit = le16_to_cpu(xb->xb_suballoc_bit); 2468 bit = le16_to_cpu(xb->xb_suballoc_bit);
2469 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 2469 if (xb->xb_suballoc_loc)
2470 bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2471 else
2472 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2470 2473
2471 xb_alloc_inode = ocfs2_get_system_file_inode(osb, 2474 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2472 EXTENT_ALLOC_SYSTEM_INODE, 2475 EXTENT_ALLOC_SYSTEM_INODE,
@@ -2829,9 +2832,8 @@ static int ocfs2_create_xattr_block(struct inode *inode,
2829 int ret; 2832 int ret;
2830 u16 suballoc_bit_start; 2833 u16 suballoc_bit_start;
2831 u32 num_got; 2834 u32 num_got;
2832 u64 first_blkno; 2835 u64 suballoc_loc, first_blkno;
2833 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data; 2836 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2834 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2835 struct buffer_head *new_bh = NULL; 2837 struct buffer_head *new_bh = NULL;
2836 struct ocfs2_xattr_block *xblk; 2838 struct ocfs2_xattr_block *xblk;
2837 2839
@@ -2842,9 +2844,9 @@ static int ocfs2_create_xattr_block(struct inode *inode,
2842 goto end; 2844 goto end;
2843 } 2845 }
2844 2846
2845 ret = ocfs2_claim_metadata(osb, ctxt->handle, ctxt->meta_ac, 1, 2847 ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
2846 &suballoc_bit_start, &num_got, 2848 &suballoc_loc, &suballoc_bit_start,
2847 &first_blkno); 2849 &num_got, &first_blkno);
2848 if (ret < 0) { 2850 if (ret < 0) {
2849 mlog_errno(ret); 2851 mlog_errno(ret);
2850 goto end; 2852 goto end;
@@ -2866,8 +2868,10 @@ static int ocfs2_create_xattr_block(struct inode *inode,
2866 memset(xblk, 0, inode->i_sb->s_blocksize); 2868 memset(xblk, 0, inode->i_sb->s_blocksize);
2867 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE); 2869 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2868 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot); 2870 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2871 xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
2869 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start); 2872 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2870 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation); 2873 xblk->xb_fs_generation =
2874 cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
2871 xblk->xb_blkno = cpu_to_le64(first_blkno); 2875 xblk->xb_blkno = cpu_to_le64(first_blkno);
2872 if (indexed) { 2876 if (indexed) {
2873 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root; 2877 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
@@ -4229,7 +4233,6 @@ static int ocfs2_xattr_create_index_block(struct inode *inode,
4229 u32 bit_off, len; 4233 u32 bit_off, len;
4230 u64 blkno; 4234 u64 blkno;
4231 handle_t *handle = ctxt->handle; 4235 handle_t *handle = ctxt->handle;
4232 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4233 struct ocfs2_inode_info *oi = OCFS2_I(inode); 4236 struct ocfs2_inode_info *oi = OCFS2_I(inode);
4234 struct buffer_head *xb_bh = xs->xattr_bh; 4237 struct buffer_head *xb_bh = xs->xattr_bh;
4235 struct ocfs2_xattr_block *xb = 4238 struct ocfs2_xattr_block *xb =
@@ -4257,7 +4260,7 @@ static int ocfs2_xattr_create_index_block(struct inode *inode,
4257 goto out; 4260 goto out;
4258 } 4261 }
4259 4262
4260 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 4263 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
4261 1, 1, &bit_off, &len); 4264 1, 1, &bit_off, &len);
4262 if (ret) { 4265 if (ret) {
4263 mlog_errno(ret); 4266 mlog_errno(ret);
@@ -5078,7 +5081,7 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5078 goto leave; 5081 goto leave;
5079 } 5082 }
5080 5083
5081 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1, 5084 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
5082 clusters_to_add, &bit_off, &num_bits); 5085 clusters_to_add, &bit_off, &num_bits);
5083 if (ret < 0) { 5086 if (ret < 0) {
5084 if (ret != -ENOSPC) 5087 if (ret != -ENOSPC)
@@ -6906,7 +6909,7 @@ static int ocfs2_reflink_xattr_rec(struct inode *inode,
6906 goto out; 6909 goto out;
6907 } 6910 }
6908 6911
6909 ret = ocfs2_claim_clusters(osb, handle, data_ac, 6912 ret = ocfs2_claim_clusters(handle, data_ac,
6910 len, &p_cluster, &num_clusters); 6913 len, &p_cluster, &num_clusters);
6911 if (ret) { 6914 if (ret) {
6912 mlog_errno(ret); 6915 mlog_errno(ret);