ocfs2: add IO error check in ocfs2_get_sector()
[safe/jmp/linux-2.6] / fs / ocfs2 / xattr.c
index 3e2e92d..4ddd788 100644 (file)
@@ -82,13 +82,14 @@ struct ocfs2_xattr_set_ctxt {
 
 #define OCFS2_XATTR_ROOT_SIZE  (sizeof(struct ocfs2_xattr_def_value_root))
 #define OCFS2_XATTR_INLINE_SIZE        80
+#define OCFS2_XATTR_HEADER_GAP 4
 #define OCFS2_XATTR_FREE_IN_IBODY      (OCFS2_MIN_XATTR_INLINE_SIZE \
                                         - sizeof(struct ocfs2_xattr_header) \
-                                        - sizeof(__u32))
+                                        - OCFS2_XATTR_HEADER_GAP)
 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
                                         - sizeof(struct ocfs2_xattr_block) \
                                         - sizeof(struct ocfs2_xattr_header) \
-                                        - sizeof(__u32))
+                                        - OCFS2_XATTR_HEADER_GAP)
 
 static struct ocfs2_xattr_def_value_root def_xv = {
        .xv.xr_list.l_count = cpu_to_le16(1),
@@ -274,10 +275,12 @@ static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
                               bucket->bu_blocks, bucket->bu_bhs, 0,
                               NULL);
        if (!rc) {
+               spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
                rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
                                                 bucket->bu_bhs,
                                                 bucket->bu_blocks,
                                                 &bucket_xh(bucket)->xh_check);
+               spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
                if (rc)
                        mlog_errno(rc);
        }
@@ -310,9 +313,11 @@ static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
 {
        int i;
 
+       spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
        ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
                                   bucket->bu_bhs, bucket->bu_blocks,
                                   &bucket_xh(bucket)->xh_check);
+       spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
 
        for (i = 0; i < bucket->bu_blocks; i++)
                ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
@@ -490,9 +495,14 @@ int ocfs2_calc_security_init(struct inode *dir,
        }
 
        /* reserve clusters for xattr value which will be set in B tree*/
-       if (si->value_len > OCFS2_XATTR_INLINE_SIZE)
-               *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
-                                                          si->value_len);
+       if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
+               int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
+                                                           si->value_len);
+
+               *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
+                                                          new_clusters);
+               *want_clusters += new_clusters;
+       }
        return ret;
 }
 
@@ -506,9 +516,7 @@ int ocfs2_calc_xattr_init(struct inode *dir,
 {
        int ret = 0;
        struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
-       int s_size = 0;
-       int a_size = 0;
-       int acl_len = 0;
+       int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
 
        if (si->enable)
                s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
@@ -556,16 +564,25 @@ int ocfs2_calc_xattr_init(struct inode *dir,
                *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
        }
 
-       /* reserve clusters for xattr value which will be set in B tree*/
-       if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE)
-               *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
-                                                          si->value_len);
+       /*
+        * reserve credits and clusters for xattrs which has large value
+        * and have to be set outside
+        */
+       if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
+               new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
+                                                       si->value_len);
+               *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
+                                                          new_clusters);
+               *want_clusters += new_clusters;
+       }
        if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
            acl_len > OCFS2_XATTR_INLINE_SIZE) {
-               *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
-               if (S_ISDIR(mode))
-                       *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
-                                                                  acl_len);
+               /* for directory, it has DEFAULT and ACCESS two types of acls */
+               new_clusters = (S_ISDIR(mode) ? 2 : 1) *
+                               ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
+               *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
+                                                          new_clusters);
+               *want_clusters += new_clusters;
        }
 
        return ret;
@@ -1495,7 +1512,7 @@ static int ocfs2_xattr_set_entry(struct inode *inode,
                last += 1;
        }
 
-       free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
+       free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
        if (free < 0)
                return -EIO;
 
@@ -2178,7 +2195,7 @@ static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
                last += 1;
        }
 
-       free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
+       free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
        if (free < 0)
                return 0;
 
@@ -2359,13 +2376,21 @@ meta_guess:
                } else
                        xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
 
+               /*
+                * If there is already an xattr tree, good, we can calculate
+                * like other b-trees. Otherwise we may have the chance of
+                * create a tree, the credit calculation is borrowed from
+                * ocfs2_calc_extend_credits with root_el = NULL. And the
+                * new tree will be cluster based, so no meta is needed.
+                */
                if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
                        struct ocfs2_extent_list *el =
                                 &xb->xb_attrs.xb_root.xt_list;
                        meta_add += ocfs2_extend_meta_needed(el);
                        credits += ocfs2_calc_extend_credits(inode->i_sb,
                                                             el, 1);
-               }
+               } else
+                       credits += OCFS2_SUBALLOC_ALLOC + 1;
 
                /*
                 * This cluster will be used either for new bucket or for
@@ -2572,8 +2597,9 @@ static int __ocfs2_xattr_set_handle(struct inode *inode,
 
        if (!ret) {
                /* Update inode ctime. */
-               ret = ocfs2_journal_access(ctxt->handle, inode, xis->inode_bh,
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_journal_access_di(ctxt->handle, inode,
+                                             xis->inode_bh,
+                                             OCFS2_JOURNAL_ACCESS_WRITE);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -2591,9 +2617,7 @@ out:
 /*
  * This function only called duing creating inode
  * for init security/acl xattrs of the new inode.
- * The xattrs could be put into ibody or extent block,
- * xattr bucket would not be use in this case.
- * transanction credits also be reserved in here.
+ * All transanction credits have been reserved in mknod.
  */
 int ocfs2_xattr_set_handle(handle_t *handle,
                           struct inode *inode,
@@ -2633,6 +2657,19 @@ int ocfs2_xattr_set_handle(handle_t *handle,
        if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
                return -EOPNOTSUPP;
 
+       /*
+        * In extreme situation, may need xattr bucket when
+        * block size is too small. And we have already reserved
+        * the credits for bucket in mknod.
+        */
+       if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
+               xbs.bucket = ocfs2_xattr_bucket_new(inode);
+               if (!xbs.bucket) {
+                       mlog_errno(-ENOMEM);
+                       return -ENOMEM;
+               }
+       }
+
        xis.inode_bh = xbs.inode_bh = di_bh;
        di = (struct ocfs2_dinode *)di_bh->b_data;
 
@@ -2652,6 +2689,7 @@ int ocfs2_xattr_set_handle(handle_t *handle,
 cleanup:
        up_write(&OCFS2_I(inode)->ip_xattr_sem);
        brelse(xbs.xattr_bh);
+       ocfs2_xattr_bucket_free(xbs.bucket);
 
        return ret;
 }
@@ -4697,13 +4735,6 @@ static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
        vb.vb_xv = (struct ocfs2_xattr_value_root *)
                (vb.vb_bh->b_data + offset % blocksize);
 
-       ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
-                                               OCFS2_JOURNAL_ACCESS_WRITE);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-
        /*
         * From here on out we have to dirty the bucket.  The generic
         * value calls only modify one of the bucket's bhs, but we need
@@ -4716,12 +4747,18 @@ static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
        ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
        if (ret) {
                mlog_errno(ret);
-               goto out_dirty;
+               goto out;
+       }
+
+       ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
+                                               OCFS2_JOURNAL_ACCESS_WRITE);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
        }
 
        xe->xe_value_size = cpu_to_le64(len);
 
-out_dirty:
        ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
 
 out:
@@ -5029,8 +5066,8 @@ try_again:
        xh_free_start = le16_to_cpu(xh->xh_free_start);
        header_size = sizeof(struct ocfs2_xattr_header) +
                        count * sizeof(struct ocfs2_xattr_entry);
-       max_free = OCFS2_XATTR_BUCKET_SIZE -
-               le16_to_cpu(xh->xh_name_value_len) - header_size;
+       max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
+               le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
 
        mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
                        "of %u which exceed block size\n",
@@ -5063,7 +5100,7 @@ try_again:
                        need = 0;
        }
 
-       free = xh_free_start - header_size;
+       free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
        /*
         * We need to make sure the new name/value pair
         * can exist in the same block.
@@ -5096,7 +5133,8 @@ try_again:
                        }
 
                        xh_free_start = le16_to_cpu(xh->xh_free_start);
-                       free = xh_free_start - header_size;
+                       free = xh_free_start - header_size
+                               - OCFS2_XATTR_HEADER_GAP;
                        if (xh_free_start % blocksize < need)
                                free -= xh_free_start % blocksize;
 
@@ -5292,6 +5330,9 @@ int ocfs2_init_security_get(struct inode *inode,
                            struct inode *dir,
                            struct ocfs2_security_xattr_info *si)
 {
+       /* check whether ocfs2 support feature xattr */
+       if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
+               return -EOPNOTSUPP;
        return security_inode_init_security(inode, dir, &si->name, &si->value,
                                            &si->value_len);
 }