~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to fs/ocfs2/alloc.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include <linux/highmem.h>
30
30
#include <linux/swap.h>
31
31
#include <linux/quotaops.h>
 
32
#include <linux/blkdev.h>
32
33
 
33
 
#define MLOG_MASK_PREFIX ML_DISK_ALLOC
34
34
#include <cluster/masklog.h>
35
35
 
36
36
#include "ocfs2.h"
50
50
#include "uptodate.h"
51
51
#include "xattr.h"
52
52
#include "refcounttree.h"
 
53
#include "ocfs2_trace.h"
53
54
 
54
55
#include "buffer_head_io.h"
55
56
 
886
887
        struct ocfs2_extent_block *eb =
887
888
                (struct ocfs2_extent_block *)bh->b_data;
888
889
 
889
 
        mlog(0, "Validating extent block %llu\n",
890
 
             (unsigned long long)bh->b_blocknr);
 
890
        trace_ocfs2_validate_extent_block((unsigned long long)bh->b_blocknr);
891
891
 
892
892
        BUG_ON(!buffer_uptodate(bh));
893
893
 
965
965
        struct buffer_head *eb_bh = NULL;
966
966
        u64 last_eb_blk = 0;
967
967
 
968
 
        mlog_entry_void();
969
 
 
970
968
        el = et->et_root_el;
971
969
        last_eb_blk = ocfs2_et_get_last_eb_blk(et);
972
970
 
987
985
bail:
988
986
        brelse(eb_bh);
989
987
 
990
 
        mlog_exit(retval);
 
988
        trace_ocfs2_num_free_extents(retval);
991
989
        return retval;
992
990
}
993
991
 
1010
1008
                OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
1011
1009
        struct ocfs2_extent_block *eb;
1012
1010
 
1013
 
        mlog_entry_void();
1014
 
 
1015
1011
        count = 0;
1016
1012
        while (count < wanted) {
1017
1013
                status = ocfs2_claim_metadata(handle,
1074
1070
                        brelse(bhs[i]);
1075
1071
                        bhs[i] = NULL;
1076
1072
                }
 
1073
                mlog_errno(status);
1077
1074
        }
1078
 
        mlog_exit(status);
1079
1075
        return status;
1080
1076
}
1081
1077
 
1173
1169
        struct ocfs2_extent_list  *el;
1174
1170
        u32 new_cpos, root_end;
1175
1171
 
1176
 
        mlog_entry_void();
1177
 
 
1178
1172
        BUG_ON(!last_eb_bh || !*last_eb_bh);
1179
1173
 
1180
1174
        if (eb_bh) {
1200
1194
         * from new_cpos).
1201
1195
         */
1202
1196
        if (root_end > new_cpos) {
1203
 
                mlog(0, "adjust the cluster end from %u to %u\n",
1204
 
                     root_end, new_cpos);
 
1197
                trace_ocfs2_adjust_rightmost_branch(
 
1198
                        (unsigned long long)
 
1199
                        ocfs2_metadata_cache_owner(et->et_ci),
 
1200
                        root_end, new_cpos);
 
1201
 
1205
1202
                status = ocfs2_adjust_rightmost_branch(handle, et);
1206
1203
                if (status) {
1207
1204
                        mlog_errno(status);
1332
1329
                kfree(new_eb_bhs);
1333
1330
        }
1334
1331
 
1335
 
        mlog_exit(status);
1336
1332
        return status;
1337
1333
}
1338
1334
 
1353
1349
        struct ocfs2_extent_list  *root_el;
1354
1350
        struct ocfs2_extent_list  *eb_el;
1355
1351
 
1356
 
        mlog_entry_void();
1357
 
 
1358
1352
        status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
1359
1353
                                           &new_eb_bh);
1360
1354
        if (status < 0) {
1415
1409
bail:
1416
1410
        brelse(new_eb_bh);
1417
1411
 
1418
 
        mlog_exit(status);
1419
1412
        return status;
1420
1413
}
1421
1414
 
1446
1439
        struct buffer_head *bh = NULL;
1447
1440
        struct buffer_head *lowest_bh = NULL;
1448
1441
 
1449
 
        mlog_entry_void();
1450
 
 
1451
1442
        *target_bh = NULL;
1452
1443
 
1453
1444
        el = et->et_root_el;
1503
1494
bail:
1504
1495
        brelse(bh);
1505
1496
 
1506
 
        mlog_exit(status);
1507
1497
        return status;
1508
1498
}
1509
1499
 
1540
1530
         * another tree level */
1541
1531
        if (shift) {
1542
1532
                BUG_ON(bh);
1543
 
                mlog(0, "need to shift tree depth (current = %d)\n", depth);
 
1533
                trace_ocfs2_grow_tree(
 
1534
                        (unsigned long long)
 
1535
                        ocfs2_metadata_cache_owner(et->et_ci),
 
1536
                        depth);
1544
1537
 
1545
1538
                /* ocfs2_shift_tree_depth will return us a buffer with
1546
1539
                 * the new extent block (so we can pass that to
1570
1563
 
1571
1564
        /* call ocfs2_add_branch to add the final part of the tree with
1572
1565
         * the new data. */
1573
 
        mlog(0, "add branch. bh = %p\n", bh);
1574
1566
        ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
1575
1567
                               meta_ac);
1576
1568
        if (ret < 0) {
1645
1637
        }
1646
1638
        insert_index = i;
1647
1639
 
1648
 
        mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1649
 
             insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
 
1640
        trace_ocfs2_rotate_leaf(insert_cpos, insert_index,
 
1641
                                has_empty, next_free,
 
1642
                                le16_to_cpu(el->l_count));
1650
1643
 
1651
1644
        BUG_ON(insert_index < 0);
1652
1645
        BUG_ON(insert_index >= le16_to_cpu(el->l_count));
2059
2052
        left_el = path_leaf_el(left_path);
2060
2053
        right_el = path_leaf_el(right_path);
2061
2054
        for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2062
 
                mlog(0, "Adjust records at index %u\n", i);
 
2055
                trace_ocfs2_complete_edge_insert(i);
2063
2056
 
2064
2057
                /*
2065
2058
                 * One nice property of knowing that all of these
2389
2382
                goto out;
2390
2383
        }
2391
2384
 
2392
 
        mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
 
2385
        trace_ocfs2_rotate_tree_right(
 
2386
                (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
 
2387
                insert_cpos, cpos);
2393
2388
 
2394
2389
        /*
2395
2390
         * What we want to do here is:
2418
2413
         * rotating subtrees.
2419
2414
         */
2420
2415
        while (cpos && insert_cpos <= cpos) {
2421
 
                mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2422
 
                     insert_cpos, cpos);
 
2416
                trace_ocfs2_rotate_tree_right(
 
2417
                        (unsigned long long)
 
2418
                        ocfs2_metadata_cache_owner(et->et_ci),
 
2419
                        insert_cpos, cpos);
2423
2420
 
2424
2421
                ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2425
2422
                if (ret) {
2461
2458
 
2462
2459
                start = ocfs2_find_subtree_root(et, left_path, right_path);
2463
2460
 
2464
 
                mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2465
 
                     start,
2466
 
                     (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2467
 
                     right_path->p_tree_depth);
 
2461
                trace_ocfs2_rotate_subtree(start,
 
2462
                        (unsigned long long)
 
2463
                        right_path->p_node[start].bh->b_blocknr,
 
2464
                        right_path->p_tree_depth);
2468
2465
 
2469
2466
                ret = ocfs2_extend_rotate_transaction(handle, start,
2470
2467
                                                      orig_credits, right_path);
2964
2961
                subtree_root = ocfs2_find_subtree_root(et, left_path,
2965
2962
                                                       right_path);
2966
2963
 
2967
 
                mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2968
 
                     subtree_root,
 
2964
                trace_ocfs2_rotate_subtree(subtree_root,
2969
2965
                     (unsigned long long)
2970
2966
                     right_path->p_node[subtree_root].bh->b_blocknr,
2971
2967
                     right_path->p_tree_depth);
3989
3985
                        goto out;
3990
3986
                }
3991
3987
 
3992
 
                mlog(0, "Append may need a left path update. cpos: %u, "
3993
 
                     "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3994
 
                     left_cpos);
 
3988
                trace_ocfs2_append_rec_to_path(
 
3989
                        (unsigned long long)
 
3990
                        ocfs2_metadata_cache_owner(et->et_ci),
 
3991
                        le32_to_cpu(insert_rec->e_cpos),
 
3992
                        left_cpos);
3995
3993
 
3996
3994
                /*
3997
3995
                 * No need to worry if the append is already in the
4522
4520
}
4523
4521
 
4524
4522
/*
4525
 
 * Helper function called at the begining of an insert.
 
4523
 * Helper function called at the beginning of an insert.
4526
4524
 *
4527
4525
 * This computes a few things that are commonly used in the process of
4528
4526
 * inserting into the btree:
4562
4560
                                              ocfs2_et_get_last_eb_blk(et),
4563
4561
                                              &bh);
4564
4562
                if (ret) {
4565
 
                        mlog_exit(ret);
 
4563
                        mlog_errno(ret);
4566
4564
                        goto out;
4567
4565
                }
4568
4566
                eb = (struct ocfs2_extent_block *) bh->b_data;
4678
4676
        struct ocfs2_insert_type insert = {0, };
4679
4677
        struct ocfs2_extent_rec rec;
4680
4678
 
4681
 
        mlog(0, "add %u clusters at position %u to owner %llu\n",
4682
 
             new_clusters, cpos,
4683
 
             (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
 
4679
        trace_ocfs2_insert_extent_start(
 
4680
                (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
 
4681
                cpos, new_clusters);
4684
4682
 
4685
4683
        memset(&rec, 0, sizeof(rec));
4686
4684
        rec.e_cpos = cpu_to_le32(cpos);
4700
4698
                goto bail;
4701
4699
        }
4702
4700
 
4703
 
        mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4704
 
             "Insert.contig_index: %d, Insert.free_records: %d, "
4705
 
             "Insert.tree_depth: %d\n",
4706
 
             insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4707
 
             free_records, insert.ins_tree_depth);
 
4701
        trace_ocfs2_insert_extent(insert.ins_appending, insert.ins_contig,
 
4702
                                  insert.ins_contig_index, free_records,
 
4703
                                  insert.ins_tree_depth);
4708
4704
 
4709
4705
        if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4710
4706
                status = ocfs2_grow_tree(handle, et,
4726
4722
bail:
4727
4723
        brelse(last_eb_bh);
4728
4724
 
4729
 
        mlog_exit(status);
4730
4725
        return status;
4731
4726
}
4732
4727
 
4746
4741
                                struct ocfs2_alloc_context *meta_ac,
4747
4742
                                enum ocfs2_alloc_restarted *reason_ret)
4748
4743
{
4749
 
        int status = 0;
 
4744
        int status = 0, err = 0;
4750
4745
        int free_extents;
4751
4746
        enum ocfs2_alloc_restarted reason = RESTART_NONE;
4752
4747
        u32 bit_off, num_bits;
4773
4768
         * 2) we are so fragmented, we've needed to add metadata too
4774
4769
         *    many times. */
4775
4770
        if (!free_extents && !meta_ac) {
4776
 
                mlog(0, "we haven't reserved any metadata!\n");
 
4771
                err = -1;
4777
4772
                status = -EAGAIN;
4778
4773
                reason = RESTART_META;
4779
4774
                goto leave;
4780
4775
        } else if ((!free_extents)
4781
4776
                   && (ocfs2_alloc_context_bits_left(meta_ac)
4782
4777
                       < ocfs2_extend_meta_needed(et->et_root_el))) {
4783
 
                mlog(0, "filesystem is really fragmented...\n");
 
4778
                err = -2;
4784
4779
                status = -EAGAIN;
4785
4780
                reason = RESTART_META;
4786
4781
                goto leave;
4805
4800
        }
4806
4801
 
4807
4802
        block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4808
 
        mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4809
 
             num_bits, bit_off,
4810
 
             (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
 
4803
        trace_ocfs2_add_clusters_in_btree(
 
4804
             (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
 
4805
             bit_off, num_bits);
4811
4806
        status = ocfs2_insert_extent(handle, et, *logical_offset, block,
4812
4807
                                     num_bits, flags, meta_ac);
4813
4808
        if (status < 0) {
4821
4816
        *logical_offset += num_bits;
4822
4817
 
4823
4818
        if (clusters_to_add) {
4824
 
                mlog(0, "need to alloc once more, wanted = %u\n",
4825
 
                     clusters_to_add);
 
4819
                err = clusters_to_add;
4826
4820
                status = -EAGAIN;
4827
4821
                reason = RESTART_TRANS;
4828
4822
        }
4829
4823
 
4830
4824
leave:
4831
 
        mlog_exit(status);
4832
4825
        if (reason_ret)
4833
4826
                *reason_ret = reason;
 
4827
        trace_ocfs2_add_clusters_in_btree_ret(status, reason, err);
4834
4828
        return status;
4835
4829
}
4836
4830
 
5039
5033
                                              ocfs2_et_get_last_eb_blk(et),
5040
5034
                                              &last_eb_bh);
5041
5035
                if (ret) {
5042
 
                        mlog_exit(ret);
 
5036
                        mlog_errno(ret);
5043
5037
                        goto out;
5044
5038
                }
5045
5039
 
5056
5050
 
5057
5051
        ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5058
5052
 
5059
 
        mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5060
 
             split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5061
 
             ctxt.c_split_covers_rec);
 
5053
        trace_ocfs2_split_extent(split_index, ctxt.c_contig_type,
 
5054
                                 ctxt.c_has_empty_extent,
 
5055
                                 ctxt.c_split_covers_rec);
5062
5056
 
5063
5057
        if (ctxt.c_contig_type == CONTIG_NONE) {
5064
5058
                if (ctxt.c_split_covers_rec)
5192
5186
{
5193
5187
        int ret;
5194
5188
 
5195
 
        mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5196
 
             inode->i_ino, cpos, len, phys);
 
5189
        trace_ocfs2_mark_extent_written(
 
5190
                (unsigned long long)OCFS2_I(inode)->ip_blkno,
 
5191
                cpos, len, phys);
5197
5192
 
5198
5193
        if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5199
5194
                ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5512
5507
 
5513
5508
        BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5514
5509
 
5515
 
        mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
5516
 
             "(cpos %u, len %u)\n",
5517
 
             (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5518
 
             cpos, len, index,
5519
 
             le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
 
5510
        trace_ocfs2_remove_extent(
 
5511
                (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
 
5512
                cpos, len, index, le32_to_cpu(rec->e_cpos),
 
5513
                ocfs2_rec_clusters(el, rec));
5520
5514
 
5521
5515
        if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5522
5516
                ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5795
5789
        struct ocfs2_dinode *di;
5796
5790
        struct ocfs2_truncate_log *tl;
5797
5791
 
5798
 
        mlog_entry("start_blk = %llu, num_clusters = %u\n",
5799
 
                   (unsigned long long)start_blk, num_clusters);
5800
 
 
5801
5792
        BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5802
5793
 
5803
5794
        start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5834
5825
                goto bail;
5835
5826
        }
5836
5827
 
5837
 
        mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5838
 
             "%llu (index = %d)\n", num_clusters, start_cluster,
5839
 
             (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5840
 
 
 
5828
        trace_ocfs2_truncate_log_append(
 
5829
                (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index,
 
5830
                start_cluster, num_clusters);
5841
5831
        if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5842
5832
                /*
5843
5833
                 * Move index back to the record we are coalescing with.
5846
5836
                index--;
5847
5837
 
5848
5838
                num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5849
 
                mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5850
 
                     index, le32_to_cpu(tl->tl_recs[index].t_start),
5851
 
                     num_clusters);
 
5839
                trace_ocfs2_truncate_log_append(
 
5840
                        (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
 
5841
                        index, le32_to_cpu(tl->tl_recs[index].t_start),
 
5842
                        num_clusters);
5852
5843
        } else {
5853
5844
                tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5854
5845
                tl->tl_used = cpu_to_le16(index + 1);
5859
5850
 
5860
5851
        osb->truncated_clusters += num_clusters;
5861
5852
bail:
5862
 
        mlog_exit(status);
5863
5853
        return status;
5864
5854
}
5865
5855
 
5878
5868
        struct inode *tl_inode = osb->osb_tl_inode;
5879
5869
        struct buffer_head *tl_bh = osb->osb_tl_bh;
5880
5870
 
5881
 
        mlog_entry_void();
5882
 
 
5883
5871
        di = (struct ocfs2_dinode *) tl_bh->b_data;
5884
5872
        tl = &di->id2.i_dealloc;
5885
5873
        i = le16_to_cpu(tl->tl_used) - 1;
5915
5903
                /* if start_blk is not set, we ignore the record as
5916
5904
                 * invalid. */
5917
5905
                if (start_blk) {
5918
 
                        mlog(0, "free record %d, start = %u, clusters = %u\n",
5919
 
                             i, le32_to_cpu(rec.t_start), num_clusters);
 
5906
                        trace_ocfs2_replay_truncate_records(
 
5907
                                (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
 
5908
                                i, le32_to_cpu(rec.t_start), num_clusters);
5920
5909
 
5921
5910
                        status = ocfs2_free_clusters(handle, data_alloc_inode,
5922
5911
                                                     data_alloc_bh, start_blk,
5932
5921
        osb->truncated_clusters = 0;
5933
5922
 
5934
5923
bail:
5935
 
        mlog_exit(status);
5936
5924
        return status;
5937
5925
}
5938
5926
 
5949
5937
        struct ocfs2_dinode *di;
5950
5938
        struct ocfs2_truncate_log *tl;
5951
5939
 
5952
 
        mlog_entry_void();
5953
 
 
5954
5940
        BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5955
5941
 
5956
5942
        di = (struct ocfs2_dinode *) tl_bh->b_data;
5962
5948
 
5963
5949
        tl = &di->id2.i_dealloc;
5964
5950
        num_to_flush = le16_to_cpu(tl->tl_used);
5965
 
        mlog(0, "Flush %u records from truncate log #%llu\n",
5966
 
             num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
 
5951
        trace_ocfs2_flush_truncate_log(
 
5952
                (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
 
5953
                num_to_flush);
5967
5954
        if (!num_to_flush) {
5968
5955
                status = 0;
5969
5956
                goto out;
6009
5996
        iput(data_alloc_inode);
6010
5997
 
6011
5998
out:
6012
 
        mlog_exit(status);
6013
5999
        return status;
6014
6000
}
6015
6001
 
6032
6018
                container_of(work, struct ocfs2_super,
6033
6019
                             osb_truncate_log_wq.work);
6034
6020
 
6035
 
        mlog_entry_void();
6036
 
 
6037
6021
        status = ocfs2_flush_truncate_log(osb);
6038
6022
        if (status < 0)
6039
6023
                mlog_errno(status);
6040
6024
        else
6041
6025
                ocfs2_init_steal_slots(osb);
6042
 
 
6043
 
        mlog_exit(status);
6044
6026
}
6045
6027
 
6046
6028
#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6086
6068
        *tl_inode = inode;
6087
6069
        *tl_bh    = bh;
6088
6070
bail:
6089
 
        mlog_exit(status);
6090
6071
        return status;
6091
6072
}
6092
6073
 
6106
6087
 
6107
6088
        *tl_copy = NULL;
6108
6089
 
6109
 
        mlog(0, "recover truncate log from slot %d\n", slot_num);
 
6090
        trace_ocfs2_begin_truncate_log_recovery(slot_num);
6110
6091
 
6111
6092
        status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6112
6093
        if (status < 0) {
6123
6104
 
6124
6105
        tl = &di->id2.i_dealloc;
6125
6106
        if (le16_to_cpu(tl->tl_used)) {
6126
 
                mlog(0, "We'll have %u logs to recover\n",
6127
 
                     le16_to_cpu(tl->tl_used));
 
6107
                trace_ocfs2_truncate_log_recovery_num(le16_to_cpu(tl->tl_used));
6128
6108
 
6129
6109
                *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6130
6110
                if (!(*tl_copy)) {
6157
6137
        if (status < 0 && (*tl_copy)) {
6158
6138
                kfree(*tl_copy);
6159
6139
                *tl_copy = NULL;
 
6140
                mlog_errno(status);
6160
6141
        }
6161
6142
 
6162
 
        mlog_exit(status);
6163
6143
        return status;
6164
6144
}
6165
6145
 
6174
6154
        struct inode *tl_inode = osb->osb_tl_inode;
6175
6155
        struct ocfs2_truncate_log *tl;
6176
6156
 
6177
 
        mlog_entry_void();
6178
 
 
6179
6157
        if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6180
6158
                mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6181
6159
                return -EINVAL;
6183
6161
 
6184
6162
        tl = &tl_copy->id2.i_dealloc;
6185
6163
        num_recs = le16_to_cpu(tl->tl_used);
6186
 
        mlog(0, "cleanup %u records from %llu\n", num_recs,
6187
 
             (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
 
6164
        trace_ocfs2_complete_truncate_log_recovery(
 
6165
                (unsigned long long)le64_to_cpu(tl_copy->i_blkno),
 
6166
                num_recs);
6188
6167
 
6189
6168
        mutex_lock(&tl_inode->i_mutex);
6190
6169
        for(i = 0; i < num_recs; i++) {
6219
6198
bail_up:
6220
6199
        mutex_unlock(&tl_inode->i_mutex);
6221
6200
 
6222
 
        mlog_exit(status);
6223
6201
        return status;
6224
6202
}
6225
6203
 
6228
6206
        int status;
6229
6207
        struct inode *tl_inode = osb->osb_tl_inode;
6230
6208
 
6231
 
        mlog_entry_void();
6232
 
 
6233
6209
        if (tl_inode) {
6234
6210
                cancel_delayed_work(&osb->osb_truncate_log_wq);
6235
6211
                flush_workqueue(ocfs2_wq);
6241
6217
                brelse(osb->osb_tl_bh);
6242
6218
                iput(osb->osb_tl_inode);
6243
6219
        }
6244
 
 
6245
 
        mlog_exit_void();
6246
6220
}
6247
6221
 
6248
6222
int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6251
6225
        struct inode *tl_inode = NULL;
6252
6226
        struct buffer_head *tl_bh = NULL;
6253
6227
 
6254
 
        mlog_entry_void();
6255
 
 
6256
6228
        status = ocfs2_get_truncate_log_info(osb,
6257
6229
                                             osb->slot_num,
6258
6230
                                             &tl_inode,
6268
6240
        osb->osb_tl_bh    = tl_bh;
6269
6241
        osb->osb_tl_inode = tl_inode;
6270
6242
 
6271
 
        mlog_exit(status);
6272
6243
        return status;
6273
6244
}
6274
6245
 
6350
6321
                else
6351
6322
                        bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6352
6323
                                                              head->free_bit);
6353
 
                mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6354
 
                     head->free_bit, (unsigned long long)head->free_blk);
 
6324
                trace_ocfs2_free_cached_blocks(
 
6325
                     (unsigned long long)head->free_blk, head->free_bit);
6355
6326
 
6356
6327
                ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6357
6328
                                               head->free_bit, bg_blkno, 1);
6404
6375
                return ret;
6405
6376
        }
6406
6377
 
6407
 
        mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6408
 
             bit, (unsigned long long)blkno);
 
6378
        trace_ocfs2_cache_cluster_dealloc((unsigned long long)blkno, bit);
6409
6379
 
6410
6380
        item->free_blk = blkno;
6411
6381
        item->free_bit = bit;
6480
6450
                fl = ctxt->c_first_suballocator;
6481
6451
 
6482
6452
                if (fl->f_first) {
6483
 
                        mlog(0, "Free items: (type %u, slot %d)\n",
6484
 
                             fl->f_inode_type, fl->f_slot);
 
6453
                        trace_ocfs2_run_deallocs(fl->f_inode_type,
 
6454
                                                 fl->f_slot);
6485
6455
                        ret2 = ocfs2_free_cached_blocks(osb,
6486
6456
                                                        fl->f_inode_type,
6487
6457
                                                        fl->f_slot,
6558
6528
                goto out;
6559
6529
        }
6560
6530
 
6561
 
        mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6562
 
             type, slot, bit, (unsigned long long)blkno);
 
6531
        trace_ocfs2_cache_block_dealloc(type, slot,
 
6532
                                        (unsigned long long)suballoc,
 
6533
                                        (unsigned long long)blkno, bit);
6563
6534
 
6564
6535
        item->free_bg = suballoc;
6565
6536
        item->free_blk = blkno;
7005
6976
        struct ocfs2_extent_tree et;
7006
6977
        struct ocfs2_cached_dealloc_ctxt dealloc;
7007
6978
 
7008
 
        mlog_entry_void();
7009
 
 
7010
6979
        ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
7011
6980
        ocfs2_init_dealloc_ctxt(&dealloc);
7012
6981
 
7041
7010
                goto bail;
7042
7011
        }
7043
7012
 
7044
 
        mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7045
 
             OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
 
7013
        trace_ocfs2_commit_truncate(
 
7014
                (unsigned long long)OCFS2_I(inode)->ip_blkno,
 
7015
                new_highest_cpos,
 
7016
                OCFS2_I(inode)->ip_clusters,
 
7017
                path->p_tree_depth);
7046
7018
 
7047
7019
        /*
7048
7020
         * By now, el will point to the extent list on the bottom most
7136
7108
 
7137
7109
        ocfs2_free_path(path);
7138
7110
 
7139
 
        mlog_exit(status);
7140
7111
        return status;
7141
7112
}
7142
7113
 
7214
7185
out:
7215
7186
        return ret;
7216
7187
}
 
7188
 
 
7189
static int ocfs2_trim_extent(struct super_block *sb,
 
7190
                             struct ocfs2_group_desc *gd,
 
7191
                             u32 start, u32 count)
 
7192
{
 
7193
        u64 discard, bcount;
 
7194
 
 
7195
        bcount = ocfs2_clusters_to_blocks(sb, count);
 
7196
        discard = le64_to_cpu(gd->bg_blkno) +
 
7197
                        ocfs2_clusters_to_blocks(sb, start);
 
7198
 
 
7199
        trace_ocfs2_trim_extent(sb, (unsigned long long)discard, bcount);
 
7200
 
 
7201
        return sb_issue_discard(sb, discard, bcount, GFP_NOFS, 0);
 
7202
}
 
7203
 
 
7204
static int ocfs2_trim_group(struct super_block *sb,
 
7205
                            struct ocfs2_group_desc *gd,
 
7206
                            u32 start, u32 max, u32 minbits)
 
7207
{
 
7208
        int ret = 0, count = 0, next;
 
7209
        void *bitmap = gd->bg_bitmap;
 
7210
 
 
7211
        if (le16_to_cpu(gd->bg_free_bits_count) < minbits)
 
7212
                return 0;
 
7213
 
 
7214
        trace_ocfs2_trim_group((unsigned long long)le64_to_cpu(gd->bg_blkno),
 
7215
                               start, max, minbits);
 
7216
 
 
7217
        while (start < max) {
 
7218
                start = ocfs2_find_next_zero_bit(bitmap, max, start);
 
7219
                if (start >= max)
 
7220
                        break;
 
7221
                next = ocfs2_find_next_bit(bitmap, max, start);
 
7222
 
 
7223
                if ((next - start) >= minbits) {
 
7224
                        ret = ocfs2_trim_extent(sb, gd,
 
7225
                                                start, next - start);
 
7226
                        if (ret < 0) {
 
7227
                                mlog_errno(ret);
 
7228
                                break;
 
7229
                        }
 
7230
                        count += next - start;
 
7231
                }
 
7232
                start = next + 1;
 
7233
 
 
7234
                if (fatal_signal_pending(current)) {
 
7235
                        count = -ERESTARTSYS;
 
7236
                        break;
 
7237
                }
 
7238
 
 
7239
                if ((le16_to_cpu(gd->bg_free_bits_count) - count) < minbits)
 
7240
                        break;
 
7241
        }
 
7242
 
 
7243
        if (ret < 0)
 
7244
                count = ret;
 
7245
 
 
7246
        return count;
 
7247
}
 
7248
 
 
7249
int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range)
 
7250
{
 
7251
        struct ocfs2_super *osb = OCFS2_SB(sb);
 
7252
        u64 start, len, trimmed, first_group, last_group, group;
 
7253
        int ret, cnt;
 
7254
        u32 first_bit, last_bit, minlen;
 
7255
        struct buffer_head *main_bm_bh = NULL;
 
7256
        struct inode *main_bm_inode = NULL;
 
7257
        struct buffer_head *gd_bh = NULL;
 
7258
        struct ocfs2_dinode *main_bm;
 
7259
        struct ocfs2_group_desc *gd = NULL;
 
7260
 
 
7261
        start = range->start >> osb->s_clustersize_bits;
 
7262
        len = range->len >> osb->s_clustersize_bits;
 
7263
        minlen = range->minlen >> osb->s_clustersize_bits;
 
7264
        trimmed = 0;
 
7265
 
 
7266
        if (!len) {
 
7267
                range->len = 0;
 
7268
                return 0;
 
7269
        }
 
7270
 
 
7271
        if (minlen >= osb->bitmap_cpg)
 
7272
                return -EINVAL;
 
7273
 
 
7274
        main_bm_inode = ocfs2_get_system_file_inode(osb,
 
7275
                                                    GLOBAL_BITMAP_SYSTEM_INODE,
 
7276
                                                    OCFS2_INVALID_SLOT);
 
7277
        if (!main_bm_inode) {
 
7278
                ret = -EIO;
 
7279
                mlog_errno(ret);
 
7280
                goto out;
 
7281
        }
 
7282
 
 
7283
        mutex_lock(&main_bm_inode->i_mutex);
 
7284
 
 
7285
        ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 0);
 
7286
        if (ret < 0) {
 
7287
                mlog_errno(ret);
 
7288
                goto out_mutex;
 
7289
        }
 
7290
        main_bm = (struct ocfs2_dinode *)main_bm_bh->b_data;
 
7291
 
 
7292
        if (start >= le32_to_cpu(main_bm->i_clusters)) {
 
7293
                ret = -EINVAL;
 
7294
                goto out_unlock;
 
7295
        }
 
7296
 
 
7297
        if (start + len > le32_to_cpu(main_bm->i_clusters))
 
7298
                len = le32_to_cpu(main_bm->i_clusters) - start;
 
7299
 
 
7300
        trace_ocfs2_trim_fs(start, len, minlen);
 
7301
 
 
7302
        /* Determine first and last group to examine based on start and len */
 
7303
        first_group = ocfs2_which_cluster_group(main_bm_inode, start);
 
7304
        if (first_group == osb->first_cluster_group_blkno)
 
7305
                first_bit = start;
 
7306
        else
 
7307
                first_bit = start - ocfs2_blocks_to_clusters(sb, first_group);
 
7308
        last_group = ocfs2_which_cluster_group(main_bm_inode, start + len - 1);
 
7309
        last_bit = osb->bitmap_cpg;
 
7310
 
 
7311
        for (group = first_group; group <= last_group;) {
 
7312
                if (first_bit + len >= osb->bitmap_cpg)
 
7313
                        last_bit = osb->bitmap_cpg;
 
7314
                else
 
7315
                        last_bit = first_bit + len;
 
7316
 
 
7317
                ret = ocfs2_read_group_descriptor(main_bm_inode,
 
7318
                                                  main_bm, group,
 
7319
                                                  &gd_bh);
 
7320
                if (ret < 0) {
 
7321
                        mlog_errno(ret);
 
7322
                        break;
 
7323
                }
 
7324
 
 
7325
                gd = (struct ocfs2_group_desc *)gd_bh->b_data;
 
7326
                cnt = ocfs2_trim_group(sb, gd, first_bit, last_bit, minlen);
 
7327
                brelse(gd_bh);
 
7328
                gd_bh = NULL;
 
7329
                if (cnt < 0) {
 
7330
                        ret = cnt;
 
7331
                        mlog_errno(ret);
 
7332
                        break;
 
7333
                }
 
7334
 
 
7335
                trimmed += cnt;
 
7336
                len -= osb->bitmap_cpg - first_bit;
 
7337
                first_bit = 0;
 
7338
                if (group == osb->first_cluster_group_blkno)
 
7339
                        group = ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
 
7340
                else
 
7341
                        group += ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
 
7342
        }
 
7343
        range->len = trimmed * sb->s_blocksize;
 
7344
out_unlock:
 
7345
        ocfs2_inode_unlock(main_bm_inode, 0);
 
7346
        brelse(main_bm_bh);
 
7347
out_mutex:
 
7348
        mutex_unlock(&main_bm_inode->i_mutex);
 
7349
        iput(main_bm_inode);
 
7350
out:
 
7351
        return ret;
 
7352
}