Merge to kernel-2.6.20-1.2949.fc6.vs2.2.0.1
[linux-2.6.git] / fs / ext2 / xattr.c
index 8c79e16..b7b83fc 100644 (file)
@@ -24,7 +24,7 @@
  *
  *   +------------------+
  *   | header           |
- *   ¦ entry 1          | |
+ *   | entry 1          | |
  *   | entry 2          | | growing downwards
  *   | entry 3          | v
  *   | four null bytes  |
@@ -101,7 +101,7 @@ static void ext2_xattr_rehash(struct ext2_xattr_header *,
 
 static struct mb_cache *ext2_xattr_cache;
 
-static struct xattr_handler *ext2_xattr_handler_map[EXT2_XATTR_INDEX_MAX] = {
+static struct xattr_handler *ext2_xattr_handler_map[] = {
        [EXT2_XATTR_INDEX_USER]              = &ext2_xattr_user_handler,
 #ifdef CONFIG_EXT2_FS_POSIX_ACL
        [EXT2_XATTR_INDEX_POSIX_ACL_ACCESS]  = &ext2_xattr_acl_access_handler,
@@ -131,22 +131,11 @@ ext2_xattr_handler(int name_index)
 {
        struct xattr_handler *handler = NULL;
 
-       if (name_index > 0 && name_index <= EXT2_XATTR_INDEX_MAX)
+       if (name_index > 0 && name_index < ARRAY_SIZE(ext2_xattr_handler_map))
                handler = ext2_xattr_handler_map[name_index];
        return handler;
 }
 
-/*
- * Inode operation listxattr()
- *
- * dentry->d_inode->i_sem: don't care
- */
-ssize_t
-ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
-{
-       return ext2_xattr_list(dentry->d_inode, buffer, size);
-}
-
 /*
  * ext2_xattr_get()
  *
@@ -260,7 +249,7 @@ cleanup:
  * Returns a negative error number on failure, or the number of bytes
  * used / required on success.
  */
-int
+static int
 ext2_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
 {
        struct buffer_head *bh = NULL;
@@ -334,6 +323,17 @@ cleanup:
        return error;
 }
 
+/*
+ * Inode operation listxattr()
+ *
+ * dentry->d_inode->i_mutex: don't care
+ */
+ssize_t
+ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
+{
+       return ext2_xattr_list(dentry->d_inode, buffer, size);
+}
+
 /*
  * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  * not set, set it.
@@ -343,12 +343,9 @@ static void ext2_xattr_update_super_block(struct super_block *sb)
        if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
                return;
 
-       lock_super(sb);
-       EXT2_SB(sb)->s_es->s_feature_compat |=
-               cpu_to_le32(EXT2_FEATURE_COMPAT_EXT_ATTR);
+       EXT2_SET_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR);
        sb->s_dirt = 1;
        mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
-       unlock_super(sb);
 }
 
 /*
@@ -390,10 +387,6 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name,
        ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
                  name_index, name, value, (long)value_len);
 
-       if (IS_RDONLY(inode))
-               return -EROFS;
-       if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
-               return -EPERM;
        if (value == NULL)
                value_len = 0;
        if (name == NULL)
@@ -495,6 +488,7 @@ bad_block:          ext2_error(sb, "ext2_xattr_set",
 
        if (header) {
                struct mb_cache_entry *ce;
+
                /* assert(header == HDR(bh)); */
                ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev,
                                        bh->b_blocknr);
@@ -525,11 +519,10 @@ bad_block:                ext2_error(sb, "ext2_xattr_set",
                }
        } else {
                /* Allocate a buffer where we construct the new block. */
-               header = kmalloc(sb->s_blocksize, GFP_KERNEL);
+               header = kzalloc(sb->s_blocksize, GFP_KERNEL);
                error = -ENOMEM;
                if (header == NULL)
                        goto cleanup;
-               memset(header, 0, sb->s_blocksize);
                end = (char *)header + sb->s_blocksize;
                header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
                header->h_blocks = header->h_refcount = cpu_to_le32(1);
@@ -650,11 +643,11 @@ ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
                                ea_bdebug(new_bh, "reusing block");
 
                                error = -ENOSPC;
-                               if (DLIMIT_ALLOC_BLOCK(sb, inode->i_xid, 1))
+                               if (DLIMIT_ALLOC_BLOCK(inode, 1))
                                        goto cleanup;
                                error = -EDQUOT;
                                if (DQUOT_ALLOC_BLOCK(inode, 1)) {
-                                       DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
+                                       DLIMIT_FREE_BLOCK(inode, 1);
                                        unlock_buffer(new_bh);
                                        goto cleanup;
                                }
@@ -707,17 +700,24 @@ ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
 
        /* Update the inode. */
        EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
-       inode->i_ctime = CURRENT_TIME;
+       inode->i_ctime = CURRENT_TIME_SEC;
        if (IS_SYNC(inode)) {
                error = ext2_sync_inode (inode);
-               if (error)
+               /* In case sync failed due to ENOSPC the inode was actually
+                * written (only some dirty data were not) so we just proceed
+                * as if nothing happened and cleanup the unused block */
+               if (error && error != -ENOSPC) {
+                       if (new_bh && new_bh != old_bh)
+                               DQUOT_FREE_BLOCK(inode, 1);
                        goto cleanup;
+               }
        } else
                mark_inode_dirty(inode);
 
        error = 0;
        if (old_bh && old_bh != new_bh) {
                struct mb_cache_entry *ce;
+
                /*
                 * If there was an old block and we are no longer using it,
                 * release the old block.
@@ -737,11 +737,11 @@ ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
                        bforget(old_bh);
                } else {
                        /* Decrement the refcount only. */
-                       if (ce)
-                               mb_cache_entry_release(ce);
                        HDR(old_bh)->h_refcount = cpu_to_le32(
                                le32_to_cpu(HDR(old_bh)->h_refcount) - 1);
-                       DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
+                       if (ce)
+                               mb_cache_entry_release(ce);
+                       DLIMIT_FREE_BLOCK(inode, 1);
                        DQUOT_FREE_BLOCK(inode, 1);
                        mark_buffer_dirty(old_bh);
                        ea_bdebug(old_bh, "refcount now=%d",
@@ -786,8 +786,7 @@ ext2_xattr_delete_inode(struct inode *inode)
                        EXT2_I(inode)->i_file_acl);
                goto cleanup;
        }
-       ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev,
-                               bh->b_blocknr);
+       ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev, bh->b_blocknr);
        lock_buffer(bh);
        if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
                if (ce)
@@ -795,19 +794,21 @@ ext2_xattr_delete_inode(struct inode *inode)
                ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
                get_bh(bh);
                bforget(bh);
+               unlock_buffer(bh);
        } else {
-               if (ce)
-                       mb_cache_entry_release(ce);
                HDR(bh)->h_refcount = cpu_to_le32(
                        le32_to_cpu(HDR(bh)->h_refcount) - 1);
+               if (ce)
+                       mb_cache_entry_release(ce);
+               ea_bdebug(bh, "refcount now=%d",
+                       le32_to_cpu(HDR(bh)->h_refcount));
+               unlock_buffer(bh);
                mark_buffer_dirty(bh);
                if (IS_SYNC(inode))
                        sync_dirty_buffer(bh);
-               DLIMIT_FREE_BLOCK(inode->i_sb, inode->i_xid, 1);
+               DLIMIT_FREE_BLOCK(inode, 1);
                DQUOT_FREE_BLOCK(inode, 1);
        }
-       ea_bdebug(bh, "refcount now=%d", le32_to_cpu(HDR(bh)->h_refcount) - 1);
-       unlock_buffer(bh);
        EXT2_I(inode)->i_file_acl = 0;
 
 cleanup:
@@ -823,7 +824,7 @@ cleanup:
 void
 ext2_xattr_put_super(struct super_block *sb)
 {
-       mb_cache_shrink(ext2_xattr_cache, sb->s_bdev);
+       mb_cache_shrink(sb->s_bdev);
 }
 
 
@@ -881,6 +882,7 @@ ext2_xattr_cmp(struct ext2_xattr_header *header1,
                if (IS_LAST_ENTRY(entry2))
                        return 1;
                if (entry1->e_hash != entry2->e_hash ||
+                   entry1->e_name_index != entry2->e_name_index ||
                    entry1->e_name_len != entry2->e_name_len ||
                    entry1->e_value_size != entry2->e_value_size ||
                    memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
@@ -930,7 +932,6 @@ again:
                }
 
                bh = sb_bread(inode->i_sb, ce->e_block);
-
                if (!bh) {
                        ext2_error(inode->i_sb, "ext2_xattr_cache_find",
                                "inode %ld: block %ld read error",
@@ -1030,7 +1031,7 @@ init_ext2_xattr(void)
 {
        ext2_xattr_cache = mb_cache_create("ext2_xattr", NULL,
                sizeof(struct mb_cache_entry) +
-               sizeof(struct mb_cache_entry_index), 1, 6);
+               sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
        if (!ext2_xattr_cache)
                return -ENOMEM;
        return 0;