aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ocfs2/suballoc.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ocfs2/suballoc.c')
-rw-r--r--fs/ocfs2/suballoc.c613
1 files changed, 445 insertions, 168 deletions
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",