tree-wide: fix assorted typos all over the place
[safe/jmp/linux-2.6] / fs / ocfs2 / alloc.c
index a629656..7c7198a 100644 (file)
@@ -49,6 +49,7 @@
 #include "super.h"
 #include "uptodate.h"
 #include "xattr.h"
+#include "refcounttree.h"
 
 #include "buffer_head_io.h"
 
@@ -567,36 +568,6 @@ static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
                                         struct ocfs2_extent_block *eb);
-
-/*
- * Structures which describe a path through a btree, and functions to
- * manipulate them.
- *
- * The idea here is to be as generic as possible with the tree
- * manipulation code.
- */
-struct ocfs2_path_item {
-       struct buffer_head              *bh;
-       struct ocfs2_extent_list        *el;
-};
-
-#define OCFS2_MAX_PATH_DEPTH   5
-
-struct ocfs2_path {
-       int                             p_tree_depth;
-       ocfs2_journal_access_func       p_root_access;
-       struct ocfs2_path_item          p_node[OCFS2_MAX_PATH_DEPTH];
-};
-
-#define path_root_bh(_path) ((_path)->p_node[0].bh)
-#define path_root_el(_path) ((_path)->p_node[0].el)
-#define path_root_access(_path)((_path)->p_root_access)
-#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
-#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
-#define path_num_items(_path) ((_path)->p_tree_depth + 1)
-
-static int ocfs2_find_path(struct ocfs2_caching_info *ci,
-                          struct ocfs2_path *path, u32 cpos);
 static void ocfs2_adjust_rightmost_records(handle_t *handle,
                                           struct ocfs2_extent_tree *et,
                                           struct ocfs2_path *path,
@@ -606,7 +577,7 @@ static void ocfs2_adjust_rightmost_records(handle_t *handle,
  * to build another path. Generally, this involves freeing the buffer
  * heads.
  */
-static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
+void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
 {
        int i, start = 0, depth = 0;
        struct ocfs2_path_item *node;
@@ -635,7 +606,7 @@ static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
        path->p_tree_depth = depth;
 }
 
-static void ocfs2_free_path(struct ocfs2_path *path)
+void ocfs2_free_path(struct ocfs2_path *path)
 {
        if (path) {
                ocfs2_reinit_path(path, 0);
@@ -733,13 +704,13 @@ static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
        return path;
 }
 
-static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
+struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
 {
        return ocfs2_new_path(path_root_bh(path), path_root_el(path),
                              path_root_access(path));
 }
 
-static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
+struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
 {
        return ocfs2_new_path(et->et_root_bh, et->et_root_el,
                              et->et_root_journal_access);
@@ -752,10 +723,10 @@ static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
  * I don't like the way this function's name looks next to
  * ocfs2_journal_access_path(), but I don't have a better one.
  */
-static int ocfs2_path_bh_journal_access(handle_t *handle,
-                                       struct ocfs2_caching_info *ci,
-                                       struct ocfs2_path *path,
-                                       int idx)
+int ocfs2_path_bh_journal_access(handle_t *handle,
+                                struct ocfs2_caching_info *ci,
+                                struct ocfs2_path *path,
+                                int idx)
 {
        ocfs2_journal_access_func access = path_root_access(path);
 
@@ -772,9 +743,9 @@ static int ocfs2_path_bh_journal_access(handle_t *handle,
 /*
  * Convenience function to journal all components in a path.
  */
-static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
-                                    handle_t *handle,
-                                    struct ocfs2_path *path)
+int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
+                             handle_t *handle,
+                             struct ocfs2_path *path)
 {
        int i, ret = 0;
 
@@ -1942,8 +1913,8 @@ static void find_path_ins(void *data, struct buffer_head *bh)
        ocfs2_path_insert_eb(fp->path, fp->index, bh);
        fp->index++;
 }
-static int ocfs2_find_path(struct ocfs2_caching_info *ci,
-                          struct ocfs2_path *path, u32 cpos)
+int ocfs2_find_path(struct ocfs2_caching_info *ci,
+                   struct ocfs2_path *path, u32 cpos)
 {
        struct find_path_data data;
 
@@ -2355,10 +2326,18 @@ static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
                                           int op_credits,
                                           struct ocfs2_path *path)
 {
+       int ret;
        int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
 
-       if (handle->h_buffer_credits < credits)
-               return ocfs2_extend_trans(handle, credits);
+       if (handle->h_buffer_credits < credits) {
+               ret = ocfs2_extend_trans(handle,
+                                        credits - handle->h_buffer_credits);
+               if (ret)
+                       return ret;
+
+               if (unlikely(handle->h_buffer_credits < credits))
+                       return ocfs2_extend_trans(handle, credits);
+       }
 
        return 0;
 }
@@ -2419,7 +2398,7 @@ static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
  *
  * The array is assumed to be large enough to hold an entire path (tree depth).
  *
- * Upon succesful return from this function:
+ * Upon successful return from this function:
  *
  * - The 'right_path' array will contain a path to the leaf block
  *   whose range contains e_cpos.
@@ -5104,13 +5083,13 @@ out:
  * have been brought into cache (and pinned via the journal), so the
  * extra overhead is not expressed in terms of disk reads.
  */
-static int __ocfs2_split_extent(handle_t *handle,
-                               struct ocfs2_extent_tree *et,
-                               struct ocfs2_path *path,
-                               int split_index,
-                               struct ocfs2_extent_rec *split_rec,
-                               struct ocfs2_alloc_context *meta_ac,
-                               struct ocfs2_cached_dealloc_ctxt *dealloc)
+int ocfs2_split_extent(handle_t *handle,
+                      struct ocfs2_extent_tree *et,
+                      struct ocfs2_path *path,
+                      int split_index,
+                      struct ocfs2_extent_rec *split_rec,
+                      struct ocfs2_alloc_context *meta_ac,
+                      struct ocfs2_cached_dealloc_ctxt *dealloc)
 {
        int ret = 0;
        struct ocfs2_extent_list *el = path_leaf_el(path);
@@ -5199,12 +5178,12 @@ out:
  *
  * The caller is responsible for passing down meta_ac if we'll need it.
  */
-static int ocfs2_change_extent_flag(handle_t *handle,
-                                   struct ocfs2_extent_tree *et,
-                                   u32 cpos, u32 len, u32 phys,
-                                   struct ocfs2_alloc_context *meta_ac,
-                                   struct ocfs2_cached_dealloc_ctxt *dealloc,
-                                   int new_flags, int clear_flags)
+int ocfs2_change_extent_flag(handle_t *handle,
+                            struct ocfs2_extent_tree *et,
+                            u32 cpos, u32 len, u32 phys,
+                            struct ocfs2_alloc_context *meta_ac,
+                            struct ocfs2_cached_dealloc_ctxt *dealloc,
+                            int new_flags, int clear_flags)
 {
        int ret, index;
        struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
@@ -5267,9 +5246,9 @@ static int ocfs2_change_extent_flag(handle_t *handle,
        if (clear_flags)
                split_rec.e_flags &= ~clear_flags;
 
-       ret = __ocfs2_split_extent(handle, et, left_path,
-                                 index, &split_rec, meta_ac,
-                                 dealloc);
+       ret = ocfs2_split_extent(handle, et, left_path,
+                                index, &split_rec, meta_ac,
+                                dealloc);
        if (ret)
                mlog_errno(ret);
 
@@ -6552,9 +6531,9 @@ ocfs2_find_per_slot_free_list(int type,
        return fl;
 }
 
-static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
-                                    int type, int slot, u64 blkno,
-                                    unsigned int bit)
+int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
+                             int type, int slot, u64 blkno,
+                             unsigned int bit)
 {
        int ret;
        struct ocfs2_per_slot_free_list *fl;
@@ -6703,7 +6682,7 @@ out:
  */
 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
                           handle_t *handle, struct ocfs2_truncate_context *tc,
-                          u32 clusters_to_del, u64 *delete_start)
+                          u32 clusters_to_del, u64 *delete_start, u8 *flags)
 {
        int ret, i, index = path->p_tree_depth;
        u32 new_edge = 0;
@@ -6713,6 +6692,7 @@ static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
        struct ocfs2_extent_rec *rec;
 
        *delete_start = 0;
+       *flags = 0;
 
        while (index >= 0) {
                bh = path->p_node[index].bh;
@@ -6800,6 +6780,7 @@ find_tail_record:
                        *delete_start = le64_to_cpu(rec->e_blkno)
                                + ocfs2_clusters_to_blocks(inode->i_sb,
                                        le16_to_cpu(rec->e_leaf_clusters));
+                       *flags = rec->e_flags;
 
                        /*
                         * If it's now empty, remove this record.
@@ -6899,7 +6880,8 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
                             struct buffer_head *fe_bh,
                             handle_t *handle,
                             struct ocfs2_truncate_context *tc,
-                            struct ocfs2_path *path)
+                            struct ocfs2_path *path,
+                            struct ocfs2_alloc_context *meta_ac)
 {
        int status;
        struct ocfs2_dinode *fe;
@@ -6907,6 +6889,7 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
        struct ocfs2_extent_list *el;
        struct buffer_head *last_eb_bh = NULL;
        u64 delete_blk = 0;
+       u8 rec_flags;
 
        fe = (struct ocfs2_dinode *) fe_bh->b_data;
 
@@ -6962,7 +6945,7 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
        inode->i_blocks = ocfs2_inode_sector_count(inode);
 
        status = ocfs2_trim_tree(inode, path, handle, tc,
-                                clusters_to_del, &delete_blk);
+                                clusters_to_del, &delete_blk, &rec_flags);
        if (status) {
                mlog_errno(status);
                goto bail;
@@ -6994,8 +6977,16 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
        }
 
        if (delete_blk) {
-               status = ocfs2_truncate_log_append(osb, handle, delete_blk,
-                                                  clusters_to_del);
+               if (rec_flags & OCFS2_EXT_REFCOUNTED)
+                       status = ocfs2_decrease_refcount(inode, handle,
+                                       ocfs2_blocks_to_clusters(osb->sb,
+                                                                delete_blk),
+                                       clusters_to_del, meta_ac,
+                                       &tc->tc_dealloc, 1);
+               else
+                       status = ocfs2_truncate_log_append(osb, handle,
+                                                          delete_blk,
+                                                          clusters_to_del);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
@@ -7015,9 +7006,9 @@ static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
        return 0;
 }
 
-static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
-                                    unsigned int from, unsigned int to,
-                                    struct page *page, int zero, u64 *phys)
+void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
+                             unsigned int from, unsigned int to,
+                             struct page *page, int zero, u64 *phys)
 {
        int ret, partial = 0;
 
@@ -7085,20 +7076,16 @@ out:
                ocfs2_unlock_and_free_pages(pages, numpages);
 }
 
-static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
-                               struct page **pages, int *num)
+int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
+                    struct page **pages, int *num)
 {
        int numpages, ret = 0;
-       struct super_block *sb = inode->i_sb;
        struct address_space *mapping = inode->i_mapping;
        unsigned long index;
        loff_t last_page_bytes;
 
        BUG_ON(start > end);
 
-       BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
-              (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
-
        numpages = 0;
        last_page_bytes = PAGE_ALIGN(end);
        index = start >> PAGE_CACHE_SHIFT;
@@ -7126,6 +7113,17 @@ out:
        return ret;
 }
 
+static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
+                               struct page **pages, int *num)
+{
+       struct super_block *sb = inode->i_sb;
+
+       BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
+              (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
+
+       return ocfs2_grab_pages(inode, start, end, pages, num);
+}
+
 /*
  * Zero the area past i_size but still within an allocated
  * cluster. This avoids exposing nonzero data on subsequent file
@@ -7413,11 +7411,14 @@ int ocfs2_commit_truncate(struct ocfs2_super *osb,
 {
        int status, i, credits, tl_sem = 0;
        u32 clusters_to_del, new_highest_cpos, range;
+       u64 blkno = 0;
        struct ocfs2_extent_list *el;
        handle_t *handle = NULL;
        struct inode *tl_inode = osb->osb_tl_inode;
        struct ocfs2_path *path = NULL;
        struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
+       struct ocfs2_alloc_context *meta_ac = NULL;
+       struct ocfs2_refcount_tree *ref_tree = NULL;
 
        mlog_entry_void();
 
@@ -7443,6 +7444,8 @@ start:
                goto bail;
        }
 
+       credits = 0;
+
        /*
         * Truncate always works against the rightmost tree branch.
         */
@@ -7483,10 +7486,15 @@ start:
                clusters_to_del = 0;
        } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
                clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
+               blkno = le64_to_cpu(el->l_recs[i].e_blkno);
        } else if (range > new_highest_cpos) {
                clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
                                   le32_to_cpu(el->l_recs[i].e_cpos)) -
                                  new_highest_cpos;
+               blkno = le64_to_cpu(el->l_recs[i].e_blkno) +
+                       ocfs2_clusters_to_blocks(inode->i_sb,
+                               ocfs2_rec_clusters(el, &el->l_recs[i]) -
+                               clusters_to_del);
        } else {
                status = 0;
                goto bail;
@@ -7495,6 +7503,29 @@ start:
        mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
             clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
 
+       if (el->l_recs[i].e_flags & OCFS2_EXT_REFCOUNTED && clusters_to_del) {
+               BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
+                        OCFS2_HAS_REFCOUNT_FL));
+
+               status = ocfs2_lock_refcount_tree(osb,
+                                               le64_to_cpu(di->i_refcount_loc),
+                                               1, &ref_tree, NULL);
+               if (status) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               status = ocfs2_prepare_refcount_change_for_del(inode, fe_bh,
+                                                              blkno,
+                                                              clusters_to_del,
+                                                              &credits,
+                                                              &meta_ac);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+       }
+
        mutex_lock(&tl_inode->i_mutex);
        tl_sem = 1;
        /* ocfs2_truncate_log_needs_flush guarantees us at least one
@@ -7508,7 +7539,7 @@ start:
                }
        }
 
-       credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
+       credits += ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
                                                (struct ocfs2_dinode *)fe_bh->b_data,
                                                el);
        handle = ocfs2_start_trans(osb, credits);
@@ -7520,7 +7551,7 @@ start:
        }
 
        status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
-                                  tc, path);
+                                  tc, path, meta_ac);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -7534,6 +7565,16 @@ start:
 
        ocfs2_reinit_path(path, 1);
 
+       if (meta_ac) {
+               ocfs2_free_alloc_context(meta_ac);
+               meta_ac = NULL;
+       }
+
+       if (ref_tree) {
+               ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
+               ref_tree = NULL;
+       }
+
        /*
         * The check above will catch the case where we've truncated
         * away all allocation.
@@ -7550,6 +7591,12 @@ bail:
        if (handle)
                ocfs2_commit_trans(osb, handle);
 
+       if (meta_ac)
+               ocfs2_free_alloc_context(meta_ac);
+
+       if (ref_tree)
+               ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
+
        ocfs2_run_deallocs(osb, &tc->tc_dealloc);
 
        ocfs2_free_path(path);