]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - fs/ocfs2/xattr.c
ocfs2: Add the 'inode64' mount option.
[karo-tx-linux.git] / fs / ocfs2 / xattr.c
index 5e8fae948882e825209c09e481c0cb90f22d070d..b2e25a828e382846c33a9503c633735aaf53d2a4 100644 (file)
@@ -131,6 +131,9 @@ static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
                                             struct ocfs2_xattr_info *xi,
                                             struct ocfs2_xattr_search *xs);
 
+static int ocfs2_delete_xattr_index_block(struct inode *inode,
+                                         struct buffer_head *xb_bh);
+
 static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
 {
        struct xattr_handler *handler = NULL;
@@ -203,22 +206,24 @@ static int ocfs2_xattr_extend_allocation(struct inode *inode,
        struct ocfs2_alloc_context *meta_ac = NULL;
        enum ocfs2_alloc_restarted why;
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
-       struct ocfs2_extent_list *root_el = &xv->xr_list;
        u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
+       struct ocfs2_extent_tree et;
 
        mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
 
+       ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
+
 restart_all:
 
-       status = ocfs2_lock_allocators(inode, xattr_bh, root_el,
-                                      clusters_to_add, 0, &data_ac,
-                                      &meta_ac, OCFS2_XATTR_VALUE_EXTENT, xv);
+       status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
+                                      &data_ac, &meta_ac);
        if (status) {
                mlog_errno(status);
                goto leave;
        }
 
-       credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
+       credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
+                                           clusters_to_add);
        handle = ocfs2_start_trans(osb, credits);
        if (IS_ERR(handle)) {
                status = PTR_ERR(handle);
@@ -241,14 +246,11 @@ restarted_transaction:
                                             &logical_start,
                                             clusters_to_add,
                                             0,
-                                            xattr_bh,
-                                            root_el,
+                                            &et,
                                             handle,
                                             data_ac,
                                             meta_ac,
-                                            &why,
-                                            OCFS2_XATTR_VALUE_EXTENT,
-                                            xv);
+                                            &why);
        if ((status < 0) && (status != -EAGAIN)) {
                if (status != -ENOSPC)
                        mlog_errno(status);
@@ -273,7 +275,7 @@ restarted_transaction:
                        mlog(0, "restarting transaction.\n");
                        /* TODO: This can be more intelligent. */
                        credits = ocfs2_calc_extend_credits(osb->sb,
-                                                           root_el,
+                                                           et.et_root_el,
                                                            clusters_to_add);
                        status = ocfs2_extend_trans(handle, credits);
                        if (status < 0) {
@@ -320,10 +322,11 @@ static int __ocfs2_remove_xattr_range(struct inode *inode,
        struct inode *tl_inode = osb->osb_tl_inode;
        handle_t *handle;
        struct ocfs2_alloc_context *meta_ac = NULL;
+       struct ocfs2_extent_tree et;
 
-       ret = ocfs2_lock_allocators(inode, root_bh, &xv->xr_list,
-                                   0, 1, NULL, &meta_ac,
-                                   OCFS2_XATTR_VALUE_EXTENT, xv);
+       ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
+
+       ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
        if (ret) {
                mlog_errno(ret);
                return ret;
@@ -353,8 +356,8 @@ static int __ocfs2_remove_xattr_range(struct inode *inode,
                goto out_commit;
        }
 
-       ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
-                                 dealloc, OCFS2_XATTR_VALUE_EXTENT, xv);
+       ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
+                                 dealloc);
        if (ret) {
                mlog_errno(ret);
                goto out_commit;
@@ -561,6 +564,9 @@ ssize_t ocfs2_listxattr(struct dentry *dentry,
        struct ocfs2_dinode *di = NULL;
        struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
 
+       if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
+               return -EOPNOTSUPP;
+
        if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
                return ret;
 
@@ -840,6 +846,9 @@ int ocfs2_xattr_get(struct inode *inode,
                .not_found = -ENODATA,
        };
 
+       if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
+               return -EOPNOTSUPP;
+
        if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
                ret = -ENODATA;
 
@@ -1327,8 +1336,9 @@ static int ocfs2_xattr_set_entry(struct inode *inode,
        }
 
        if (!(flag & OCFS2_INLINE_XATTR_FL)) {
-               /*set extended attribue in external blcok*/
+               /* set extended attribute in external block. */
                ret = ocfs2_extend_trans(handle,
+                                        OCFS2_INODE_UPDATE_CREDITS +
                                         OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
                if (ret) {
                        mlog_errno(ret);
@@ -1417,51 +1427,6 @@ out:
 
 }
 
-static int ocfs2_xattr_free_block(handle_t *handle,
-                                 struct ocfs2_super *osb,
-                                 struct ocfs2_xattr_block *xb)
-{
-       struct inode *xb_alloc_inode;
-       struct buffer_head *xb_alloc_bh = NULL;
-       u64 blk = le64_to_cpu(xb->xb_blkno);
-       u16 bit = le16_to_cpu(xb->xb_suballoc_bit);
-       u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
-       int ret = 0;
-
-       xb_alloc_inode = ocfs2_get_system_file_inode(osb,
-                               EXTENT_ALLOC_SYSTEM_INODE,
-                               le16_to_cpu(xb->xb_suballoc_slot));
-       if (!xb_alloc_inode) {
-               ret = -ENOMEM;
-               mlog_errno(ret);
-               goto out;
-       }
-       mutex_lock(&xb_alloc_inode->i_mutex);
-
-       ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
-       if (ret < 0) {
-               mlog_errno(ret);
-               goto out_mutex;
-       }
-       ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
-       if (ret < 0) {
-               mlog_errno(ret);
-               goto out_unlock;
-       }
-       ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
-                                      bit, bg_blkno, 1);
-       if (ret < 0)
-               mlog_errno(ret);
-out_unlock:
-       ocfs2_inode_unlock(xb_alloc_inode, 1);
-       brelse(xb_alloc_bh);
-out_mutex:
-       mutex_unlock(&xb_alloc_inode->i_mutex);
-       iput(xb_alloc_inode);
-out:
-       return ret;
-}
-
 static int ocfs2_remove_value_outside(struct inode*inode,
                                      struct buffer_head *bh,
                                      struct ocfs2_xattr_header *header)
@@ -1511,14 +1476,93 @@ static int ocfs2_xattr_block_remove(struct inode *inode,
                                    struct buffer_head *blk_bh)
 {
        struct ocfs2_xattr_block *xb;
-       struct ocfs2_xattr_header *header;
        int ret = 0;
 
        xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
-       header = &(xb->xb_attrs.xb_header);
+       if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
+               struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
+               ret = ocfs2_remove_value_outside(inode, blk_bh, header);
+       } else
+               ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
+
+       return ret;
+}
+
+static int ocfs2_xattr_free_block(struct inode *inode,
+                                 u64 block)
+{
+       struct inode *xb_alloc_inode;
+       struct buffer_head *xb_alloc_bh = NULL;
+       struct buffer_head *blk_bh = NULL;
+       struct ocfs2_xattr_block *xb;
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       handle_t *handle;
+       int ret = 0;
+       u64 blk, bg_blkno;
+       u16 bit;
+
+       ret = ocfs2_read_block(osb, block, &blk_bh,
+                              OCFS2_BH_CACHED, inode);
+       if (ret < 0) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       /*Verify the signature of xattr block*/
+       if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
+                  strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
+               ret = -EFAULT;
+               goto out;
+       }
+
+       ret = ocfs2_xattr_block_remove(inode, blk_bh);
+       if (ret < 0) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
+       blk = le64_to_cpu(xb->xb_blkno);
+       bit = le16_to_cpu(xb->xb_suballoc_bit);
+       bg_blkno = ocfs2_which_suballoc_group(blk, bit);
 
-       ret = ocfs2_remove_value_outside(inode, blk_bh, header);
+       xb_alloc_inode = ocfs2_get_system_file_inode(osb,
+                               EXTENT_ALLOC_SYSTEM_INODE,
+                               le16_to_cpu(xb->xb_suballoc_slot));
+       if (!xb_alloc_inode) {
+               ret = -ENOMEM;
+               mlog_errno(ret);
+               goto out;
+       }
+       mutex_lock(&xb_alloc_inode->i_mutex);
 
+       ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
+       if (ret < 0) {
+               mlog_errno(ret);
+               goto out_mutex;
+       }
+
+       handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
+       if (IS_ERR(handle)) {
+               ret = PTR_ERR(handle);
+               mlog_errno(ret);
+               goto out_unlock;
+       }
+
+       ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
+                                      bit, bg_blkno, 1);
+       if (ret < 0)
+               mlog_errno(ret);
+
+       ocfs2_commit_trans(osb, handle);
+out_unlock:
+       ocfs2_inode_unlock(xb_alloc_inode, 1);
+       brelse(xb_alloc_bh);
+out_mutex:
+       mutex_unlock(&xb_alloc_inode->i_mutex);
+       iput(xb_alloc_inode);
+out:
+       brelse(blk_bh);
        return ret;
 }
 
@@ -1529,14 +1573,14 @@ static int ocfs2_xattr_block_remove(struct inode *inode,
  */
 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
 {
-       struct ocfs2_xattr_block *xb;
-       struct buffer_head *blk_bh = NULL;
-       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
        struct ocfs2_inode_info *oi = OCFS2_I(inode);
        struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
        handle_t *handle;
        int ret;
 
+       if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
+               return 0;
+
        if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
                return 0;
 
@@ -1547,22 +1591,10 @@ int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
                        goto out;
                }
        }
-       if (di->i_xattr_loc) {
-               ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
-                                      le64_to_cpu(di->i_xattr_loc),
-                                      &blk_bh, OCFS2_BH_CACHED, inode);
-               if (ret < 0) {
-                       mlog_errno(ret);
-                       return ret;
-               }
-               /*Verify the signature of xattr block*/
-               if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
-                          strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
-                       ret = -EFAULT;
-                       goto out;
-               }
 
-               ret = ocfs2_xattr_block_remove(inode, blk_bh);
+       if (di->i_xattr_loc) {
+               ret = ocfs2_xattr_free_block(inode,
+                                            le64_to_cpu(di->i_xattr_loc));
                if (ret < 0) {
                        mlog_errno(ret);
                        goto out;
@@ -1583,11 +1615,7 @@ int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
                goto out_commit;
        }
 
-       if (di->i_xattr_loc) {
-               xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
-               ocfs2_xattr_free_block(handle, osb, xb);
-               di->i_xattr_loc = cpu_to_le64(0);
-       }
+       di->i_xattr_loc = 0;
 
        spin_lock(&oi->ip_lock);
        oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
@@ -1600,8 +1628,6 @@ int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
 out_commit:
        ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
 out:
-       brelse(blk_bh);
-
        return ret;
 }
 
@@ -1973,6 +1999,9 @@ int ocfs2_xattr_set(struct inode *inode,
                .not_found = -ENODATA,
        };
 
+       if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
+               return -EOPNOTSUPP;
+
        ret = ocfs2_inode_lock(inode, &di_bh, 1);
        if (ret < 0) {
                mlog_errno(ret);
@@ -3248,7 +3277,8 @@ static int ocfs2_half_xattr_bucket(struct inode *inode,
        xe = &xh->xh_entries[start];
        len = sizeof(struct ocfs2_xattr_entry) * (count - start);
        mlog(0, "mv xattr entry len %d from %d to %d\n", len,
-            (char *)xe - (char *)xh, (char *)xh->xh_entries - (char *)xh);
+            (int)((char *)xe - (char *)xh),
+            (int)((char *)xh->xh_entries - (char *)xh));
        memmove((char *)xh->xh_entries, (char *)xe, len);
        xe = &xh->xh_entries[count - start];
        len = sizeof(struct ocfs2_xattr_entry) * start;
@@ -3605,26 +3635,24 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
        struct ocfs2_alloc_context *data_ac = NULL;
        struct ocfs2_alloc_context *meta_ac = NULL;
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
-       struct ocfs2_xattr_block *xb =
-                       (struct ocfs2_xattr_block *)root_bh->b_data;
-       struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
-       struct ocfs2_extent_list *root_el = &xb_root->xt_list;
-       enum ocfs2_extent_tree_type type = OCFS2_XATTR_TREE_EXTENT;
+       struct ocfs2_extent_tree et;
 
        mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
             "previous xattr blkno = %llu\n",
             (unsigned long long)OCFS2_I(inode)->ip_blkno,
             prev_cpos, prev_blkno);
 
-       ret = ocfs2_lock_allocators(inode, root_bh, root_el,
-                                   clusters_to_add, 0, &data_ac,
-                                   &meta_ac, type, NULL);
+       ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
+
+       ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
+                                   &data_ac, &meta_ac);
        if (ret) {
                mlog_errno(ret);
                goto leave;
        }
 
-       credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
+       credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
+                                           clusters_to_add);
        handle = ocfs2_start_trans(osb, credits);
        if (IS_ERR(handle)) {
                ret = PTR_ERR(handle);
@@ -3686,11 +3714,22 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
                }
        }
 
+       if (handle->h_buffer_credits < credits) {
+               /*
+                * The journal has been restarted before, and don't
+                * have enough space for the insertion, so extend it
+                * here.
+                */
+               ret = ocfs2_extend_trans(handle, credits);
+               if (ret) {
+                       mlog_errno(ret);
+                       goto leave;
+               }
+       }
        mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
             num_bits, block, v_start);
-       ret = ocfs2_xattr_tree_insert_extent(osb, handle, inode, root_bh,
-                                            v_start, block, num_bits,
-                                            0, meta_ac);
+       ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
+                                 num_bits, 0, meta_ac);
        if (ret < 0) {
                mlog_errno(ret);
                goto leave;
@@ -4057,8 +4096,8 @@ static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
        u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
 
-       mlog(0, "Set xattr entry len = %d index = %d in bucket %llu\n",
-            xi->value_len, xi->name_index,
+       mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
+            (unsigned long)xi->value_len, xi->name_index,
             (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
 
        if (!xs->bucket.bhs[1]) {
@@ -4314,9 +4353,11 @@ static int ocfs2_rm_xattr_cluster(struct inode *inode,
        handle_t *handle;
        struct ocfs2_xattr_block *xb =
                        (struct ocfs2_xattr_block *)root_bh->b_data;
-       struct ocfs2_extent_list *root_el = &xb->xb_attrs.xb_root.xt_list;
        struct ocfs2_alloc_context *meta_ac = NULL;
        struct ocfs2_cached_dealloc_ctxt dealloc;
+       struct ocfs2_extent_tree et;
+
+       ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
 
        ocfs2_init_dealloc_ctxt(&dealloc);
 
@@ -4325,9 +4366,7 @@ static int ocfs2_rm_xattr_cluster(struct inode *inode,
 
        ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
 
-       ret = ocfs2_lock_allocators(inode, root_bh, root_el,
-                                   0, 1, NULL, &meta_ac,
-                                   OCFS2_XATTR_TREE_EXTENT, NULL);
+       ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
        if (ret) {
                mlog_errno(ret);
                return ret;
@@ -4357,8 +4396,8 @@ static int ocfs2_rm_xattr_cluster(struct inode *inode,
                goto out_commit;
        }
 
-       ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
-                                 &dealloc, OCFS2_XATTR_TREE_EXTENT, NULL);
+       ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
+                                 &dealloc);
        if (ret) {
                mlog_errno(ret);
                goto out_commit;
@@ -4738,3 +4777,75 @@ out:
        mlog_exit(ret);
        return ret;
 }
+
+static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
+                                       struct ocfs2_xattr_bucket *bucket,
+                                       void *para)
+{
+       int ret = 0;
+       struct ocfs2_xattr_header *xh = bucket->xh;
+       u16 i;
+       struct ocfs2_xattr_entry *xe;
+
+       for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
+               xe = &xh->xh_entries[i];
+               if (ocfs2_xattr_is_local(xe))
+                       continue;
+
+               ret = ocfs2_xattr_bucket_value_truncate(inode,
+                                                       bucket->bhs[0],
+                                                       i, 0);
+               if (ret) {
+                       mlog_errno(ret);
+                       break;
+               }
+       }
+
+       return ret;
+}
+
+static int ocfs2_delete_xattr_index_block(struct inode *inode,
+                                         struct buffer_head *xb_bh)
+{
+       struct ocfs2_xattr_block *xb =
+                       (struct ocfs2_xattr_block *)xb_bh->b_data;
+       struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
+       int ret = 0;
+       u32 name_hash = UINT_MAX, e_cpos, num_clusters;
+       u64 p_blkno;
+
+       if (le16_to_cpu(el->l_next_free_rec) == 0)
+               return 0;
+
+       while (name_hash > 0) {
+               ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
+                                         &e_cpos, &num_clusters, el);
+               if (ret) {
+                       mlog_errno(ret);
+                       goto out;
+               }
+
+               ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
+                                                 ocfs2_delete_xattr_in_bucket,
+                                                 NULL);
+               if (ret) {
+                       mlog_errno(ret);
+                       goto out;
+               }
+
+               ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
+                                            p_blkno, e_cpos, num_clusters);
+               if (ret) {
+                       mlog_errno(ret);
+                       break;
+               }
+
+               if (e_cpos == 0)
+                       break;
+
+               name_hash = e_cpos - 1;
+       }
+
+out:
+       return ret;
+}