]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/ocfs2/xattr.c
ocfs2: Move buckets up into ocfs2_add_new_xattr_cluster().
[linux-2.6-omap-h63xx.git] / fs / ocfs2 / xattr.c
index 97340940cee2f37140eb8ec4e5e936f4dab2b3fe..4b247047b7aa78a381f871c09aa2f06f3ead186c 100644 (file)
@@ -170,11 +170,10 @@ static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
 
 static int ocfs2_delete_xattr_index_block(struct inode *inode,
                                          struct buffer_head *xb_bh);
-static int ocfs2_cp_xattr_bucket(struct inode *inode,
-                                handle_t *handle,
-                                u64 s_blkno,
-                                u64 t_blkno,
-                                int t_is_new);
+static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
+                                 u64 src_blk, u64 last_blk, u64 to_blk,
+                                 unsigned int start_bucket,
+                                 u32 *first_hash);
 
 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
 {
@@ -3549,140 +3548,66 @@ out:
  */
 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
                                               handle_t *handle,
-                                              struct buffer_head **first_bh,
-                                              struct buffer_head **header_bh,
+                                              struct ocfs2_xattr_bucket *first,
+                                              struct ocfs2_xattr_bucket *target,
                                               u64 new_blkno,
-                                              u64 prev_blkno,
                                               u32 num_clusters,
                                               u32 *first_hash)
 {
-       int i, ret, credits;
-       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
-       int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
-       int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
-       int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
+       int ret;
+       struct super_block *sb = inode->i_sb;
+       int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
+       int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
        int to_move = num_buckets / 2;
-       u64 last_cluster_blkno, src_blkno;
-       struct ocfs2_xattr_header *xh =
-                       (struct ocfs2_xattr_header *)((*first_bh)->b_data);
-       struct ocfs2_xattr_bucket *old_first, *new_first;
+       u64 src_blkno;
+       u64 last_cluster_blkno = bucket_blkno(first) +
+               ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
 
-       BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
-       BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
-
-       last_cluster_blkno = prev_blkno + ((num_clusters - 1) * bpc);
-       src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
+       BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
+       BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
 
        mlog(0, "move half of xattrs in cluster %llu to %llu\n",
-            (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
-
-       /* The first bucket of the original extent */
-       old_first = ocfs2_xattr_bucket_new(inode);
-       /* The first bucket of the new extent */
-       new_first = ocfs2_xattr_bucket_new(inode);
-       if (!old_first || !new_first) {
-               ret = -ENOMEM;
-               mlog_errno(ret);
-               goto out;
-       }
+            (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
 
-       ret = ocfs2_read_xattr_bucket(old_first, prev_blkno);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-
-       /*
-        * We need to update the 1st half of the new extent, and we
-        * need to update the first bucket of the old extent.
-        */
-       credits = ((to_move + 1) * blks_per_bucket) + handle->h_buffer_credits;
-       ret = ocfs2_extend_trans(handle, credits);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-
-       ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
-                                               OCFS2_JOURNAL_ACCESS_WRITE);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-
-       for (i = 0; i < to_move; i++) {
-               ret = ocfs2_cp_xattr_bucket(inode, handle,
-                                           src_blkno + (i * blks_per_bucket),
-                                           new_blkno + (i * blks_per_bucket),
-                                           1);
-               if (ret) {
-                       mlog_errno(ret);
-                       goto out;
-               }
-       }
-
-       /*
-        * Get the new bucket ready before we dirty anything
-        * (This actually shouldn't fail, because we already dirtied
-        * it once in ocfs2_cp_xattr_bucket()).
-        */
-       ret = ocfs2_read_xattr_bucket(new_first, new_blkno);
+       ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
+                                    last_cluster_blkno, new_blkno,
+                                    to_move, first_hash);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
-       ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
-                                               OCFS2_JOURNAL_ACCESS_WRITE);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-
-       /* Now update the headers */
-       le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -to_move);
-       ocfs2_xattr_bucket_journal_dirty(handle, old_first);
 
-       bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(to_move);
-       ocfs2_xattr_bucket_journal_dirty(handle, new_first);
-
-       if (first_hash)
-               *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
+       /* This is the first bucket that got moved */
+       src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
 
        /*
-        * If the target bucket is anywhere past src_blkno, we moved
-        * it to the new extent.  We need to update first_bh and header_bh.
+        * If the target bucket was part of the moved buckets, we need to
+        * update first and target.
         */
-       if ((*header_bh)->b_blocknr >= src_blkno) {
-               /* We're done with old_first, so we can re-use it. */
-               ocfs2_xattr_bucket_relse(old_first);
-
+       if (bucket_blkno(target) >= src_blkno) {
                /* Find the block for the new target bucket */
                src_blkno = new_blkno +
-                       ((*header_bh)->b_blocknr - src_blkno);
+                       (bucket_blkno(target) - src_blkno);
+
+               ocfs2_xattr_bucket_relse(first);
+               ocfs2_xattr_bucket_relse(target);
 
                /*
-                * This shouldn't fail - the buffers are in the
+                * These shouldn't fail - the buffers are in the
                 * journal from ocfs2_cp_xattr_bucket().
                 */
-               ret = ocfs2_read_xattr_bucket(old_first, src_blkno);
+               ret = ocfs2_read_xattr_bucket(first, new_blkno);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
+               ret = ocfs2_read_xattr_bucket(target, src_blkno);
+               if (ret)
+                       mlog_errno(ret);
 
-               brelse(*first_bh);
-               *first_bh = new_first->bu_bhs[0];
-               get_bh(*first_bh);
-
-               brelse(*header_bh);
-               *header_bh = old_first->bu_bhs[0];
-               get_bh(*header_bh);
        }
 
 out:
-       ocfs2_xattr_bucket_free(new_first);
-       ocfs2_xattr_bucket_free(old_first);
-
        return ret;
 }
 
@@ -4155,44 +4080,49 @@ static int ocfs2_divide_xattr_cluster(struct inode *inode,
  */
 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
                                            handle_t *handle,
-                                           struct buffer_head **first_bh,
-                                           struct buffer_head **header_bh,
+                                           struct ocfs2_xattr_bucket *first,
+                                           struct ocfs2_xattr_bucket *target,
                                            u64 new_blk,
-                                           u64 prev_blk,
                                            u32 prev_clusters,
                                            u32 *v_start,
                                            int *extend)
 {
-       int ret = 0;
-       int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
+       int ret;
 
        mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
-            (unsigned long long)prev_blk, prev_clusters,
+            (unsigned long long)bucket_blkno(first), prev_clusters,
             (unsigned long long)new_blk);
 
-       if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
+       if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
                ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
                                                          handle,
-                                                         first_bh,
-                                                         header_bh,
+                                                         first, target,
                                                          new_blk,
-                                                         prev_blk,
                                                          prev_clusters,
                                                          v_start);
-       else {
-               u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
+               if (ret)
+                       mlog_errno(ret);
+       } else {
+               /* The start of the last cluster in the first extent */
+               u64 last_blk = bucket_blkno(first) +
+                       ((prev_clusters - 1) *
+                        ocfs2_clusters_to_blocks(inode->i_sb, 1));
 
-               if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
+               if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
                        ret = ocfs2_mv_xattr_buckets(inode, handle,
-                                                    (*first_bh)->b_blocknr,
+                                                    bucket_blkno(first),
                                                     last_blk, new_blk, 0,
                                                     v_start);
-               else {
+                       if (ret)
+                               mlog_errno(ret);
+               } else {
                        ret = ocfs2_divide_xattr_cluster(inode, handle,
                                                         last_blk, new_blk,
                                                         v_start);
+                       if (ret)
+                               mlog_errno(ret);
 
-                       if ((*header_bh)->b_blocknr == last_blk && extend)
+                       if ((bucket_blkno(target) == last_blk) && extend)
                                *extend = 0;
                }
        }
@@ -4234,6 +4164,7 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
        handle_t *handle = ctxt->handle;
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
        struct ocfs2_extent_tree et;
+       struct ocfs2_xattr_bucket *first, *target;
 
        mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
             "previous xattr blkno = %llu\n",
@@ -4242,6 +4173,29 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
 
        ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
 
+       /* The first bucket of the original extent */
+       first = ocfs2_xattr_bucket_new(inode);
+       /* The target bucket for insert */
+       target = ocfs2_xattr_bucket_new(inode);
+       if (!first || !target) {
+               ret = -ENOMEM;
+               mlog_errno(ret);
+               goto leave;
+       }
+
+       BUG_ON(prev_blkno != (*first_bh)->b_blocknr);
+       ret = ocfs2_read_xattr_bucket(first, prev_blkno);
+       if (ret) {
+               mlog_errno(ret);
+               goto leave;
+       }
+
+       ret = ocfs2_read_xattr_bucket(target, (*header_bh)->b_blocknr);
+       if (ret) {
+               mlog_errno(ret);
+               goto leave;
+       }
+
        ret = ocfs2_journal_access(handle, inode, root_bh,
                                   OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret < 0) {
@@ -4282,10 +4236,9 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
        } else {
                ret = ocfs2_adjust_xattr_cross_cluster(inode,
                                                       handle,
-                                                      first_bh,
-                                                      header_bh,
+                                                      first,
+                                                      target,
                                                       block,
-                                                      prev_blkno,
                                                       prev_clusters,
                                                       &v_start,
                                                       extend);
@@ -4293,6 +4246,17 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
                        mlog_errno(ret);
                        goto leave;
                }
+
+               /* Did first+target get moved? */
+               if (prev_blkno != bucket_blkno(first)) {
+                       brelse(*first_bh);
+                       *first_bh = first->bu_bhs[0];
+                       get_bh(*first_bh);
+
+                       brelse(*header_bh);
+                       *header_bh = target->bu_bhs[0];
+                       get_bh(*header_bh);
+               }
        }
 
        mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
@@ -4309,6 +4273,8 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
                mlog_errno(ret);
 
 leave:
+       ocfs2_xattr_bucket_free(first);
+       ocfs2_xattr_bucket_free(target);
        return ret;
 }