Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/xattr.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> |
| 5 | * |
| 6 | * Fix by Harrison Xing <harrison@mountainviewdata.com>. |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 7 | * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 8 | * Extended attributes for symlinks and special files added per |
| 9 | * suggestion of Luka Renko <luka.renko@hermes.si>. |
| 10 | * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>, |
| 11 | * Red Hat Inc. |
| 12 | * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz |
| 13 | * and Andreas Gruenbacher <agruen@suse.de>. |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * Extended attributes are stored directly in inodes (on file systems with |
| 18 | * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl |
| 19 | * field contains the block number if an inode uses an additional block. All |
| 20 | * attributes must fit in the inode and one additional block. Blocks that |
| 21 | * contain the identical set of attributes may be shared among several inodes. |
| 22 | * Identical blocks are detected by keeping a cache of blocks that have |
| 23 | * recently been accessed. |
| 24 | * |
| 25 | * The attributes in inodes and on blocks have a different header; the entries |
| 26 | * are stored in the same format: |
| 27 | * |
| 28 | * +------------------+ |
| 29 | * | header | |
| 30 | * | entry 1 | | |
| 31 | * | entry 2 | | growing downwards |
| 32 | * | entry 3 | v |
| 33 | * | four null bytes | |
| 34 | * | . . . | |
| 35 | * | value 1 | ^ |
| 36 | * | value 3 | | growing upwards |
| 37 | * | value 2 | | |
| 38 | * +------------------+ |
| 39 | * |
| 40 | * The header is followed by multiple entry descriptors. In disk blocks, the |
| 41 | * entry descriptors are kept sorted. In inodes, they are unsorted. The |
| 42 | * attribute values are aligned to the end of the block in no specific order. |
| 43 | * |
| 44 | * Locking strategy |
| 45 | * ---------------- |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 46 | * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 47 | * EA blocks are only changed if they are exclusive to an inode, so |
| 48 | * holding xattr_sem also means that nothing but the EA block's reference |
| 49 | * count can change. Multiple writers to the same block are synchronized |
| 50 | * by the buffer lock. |
| 51 | */ |
| 52 | |
| 53 | #include <linux/init.h> |
| 54 | #include <linux/fs.h> |
| 55 | #include <linux/slab.h> |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame^] | 56 | #include <linux/ext4_jbd2.h> |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 57 | #include <linux/ext4_fs.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 58 | #include <linux/mbcache.h> |
| 59 | #include <linux/quotaops.h> |
| 60 | #include <linux/rwsem.h> |
| 61 | #include "xattr.h" |
| 62 | #include "acl.h" |
| 63 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 64 | #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data)) |
| 65 | #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 66 | #define BFIRST(bh) ENTRY(BHDR(bh)+1) |
| 67 | #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0) |
| 68 | |
| 69 | #define IHDR(inode, raw_inode) \ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 70 | ((struct ext4_xattr_ibody_header *) \ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 71 | ((void *)raw_inode + \ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 72 | EXT4_GOOD_OLD_INODE_SIZE + \ |
| 73 | EXT4_I(inode)->i_extra_isize)) |
| 74 | #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 75 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 76 | #ifdef EXT4_XATTR_DEBUG |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 77 | # define ea_idebug(inode, f...) do { \ |
| 78 | printk(KERN_DEBUG "inode %s:%lu: ", \ |
| 79 | inode->i_sb->s_id, inode->i_ino); \ |
| 80 | printk(f); \ |
| 81 | printk("\n"); \ |
| 82 | } while (0) |
| 83 | # define ea_bdebug(bh, f...) do { \ |
| 84 | char b[BDEVNAME_SIZE]; \ |
| 85 | printk(KERN_DEBUG "block %s:%lu: ", \ |
| 86 | bdevname(bh->b_bdev, b), \ |
| 87 | (unsigned long) bh->b_blocknr); \ |
| 88 | printk(f); \ |
| 89 | printk("\n"); \ |
| 90 | } while (0) |
| 91 | #else |
| 92 | # define ea_idebug(f...) |
| 93 | # define ea_bdebug(f...) |
| 94 | #endif |
| 95 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 96 | static void ext4_xattr_cache_insert(struct buffer_head *); |
| 97 | static struct buffer_head *ext4_xattr_cache_find(struct inode *, |
| 98 | struct ext4_xattr_header *, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 99 | struct mb_cache_entry **); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 100 | static void ext4_xattr_rehash(struct ext4_xattr_header *, |
| 101 | struct ext4_xattr_entry *); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 102 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 103 | static struct mb_cache *ext4_xattr_cache; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 104 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 105 | static struct xattr_handler *ext4_xattr_handler_map[] = { |
| 106 | [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler, |
| 107 | #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL |
| 108 | [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler, |
| 109 | [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 110 | #endif |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 111 | [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler, |
| 112 | #ifdef CONFIG_EXT4DEV_FS_SECURITY |
| 113 | [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 114 | #endif |
| 115 | }; |
| 116 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 117 | struct xattr_handler *ext4_xattr_handlers[] = { |
| 118 | &ext4_xattr_user_handler, |
| 119 | &ext4_xattr_trusted_handler, |
| 120 | #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL |
| 121 | &ext4_xattr_acl_access_handler, |
| 122 | &ext4_xattr_acl_default_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 123 | #endif |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 124 | #ifdef CONFIG_EXT4DEV_FS_SECURITY |
| 125 | &ext4_xattr_security_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 126 | #endif |
| 127 | NULL |
| 128 | }; |
| 129 | |
| 130 | static inline struct xattr_handler * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 131 | ext4_xattr_handler(int name_index) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 132 | { |
| 133 | struct xattr_handler *handler = NULL; |
| 134 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 135 | if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map)) |
| 136 | handler = ext4_xattr_handler_map[name_index]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 137 | return handler; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Inode operation listxattr() |
| 142 | * |
| 143 | * dentry->d_inode->i_mutex: don't care |
| 144 | */ |
| 145 | ssize_t |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 146 | ext4_listxattr(struct dentry *dentry, char *buffer, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 147 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 148 | return ext4_xattr_list(dentry->d_inode, buffer, size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 152 | ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 153 | { |
| 154 | while (!IS_LAST_ENTRY(entry)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 155 | struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 156 | if ((void *)next >= end) |
| 157 | return -EIO; |
| 158 | entry = next; |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static inline int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 164 | ext4_xattr_check_block(struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 165 | { |
| 166 | int error; |
| 167 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 168 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 169 | BHDR(bh)->h_blocks != cpu_to_le32(1)) |
| 170 | return -EIO; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 171 | error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 172 | return error; |
| 173 | } |
| 174 | |
| 175 | static inline int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 176 | ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 177 | { |
| 178 | size_t value_size = le32_to_cpu(entry->e_value_size); |
| 179 | |
| 180 | if (entry->e_value_block != 0 || value_size > size || |
| 181 | le16_to_cpu(entry->e_value_offs) + value_size > size) |
| 182 | return -EIO; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 187 | ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 188 | const char *name, size_t size, int sorted) |
| 189 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 190 | struct ext4_xattr_entry *entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 191 | size_t name_len; |
| 192 | int cmp = 1; |
| 193 | |
| 194 | if (name == NULL) |
| 195 | return -EINVAL; |
| 196 | name_len = strlen(name); |
| 197 | entry = *pentry; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 198 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 199 | cmp = name_index - entry->e_name_index; |
| 200 | if (!cmp) |
| 201 | cmp = name_len - entry->e_name_len; |
| 202 | if (!cmp) |
| 203 | cmp = memcmp(name, entry->e_name, name_len); |
| 204 | if (cmp <= 0 && (sorted || cmp == 0)) |
| 205 | break; |
| 206 | } |
| 207 | *pentry = entry; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 208 | if (!cmp && ext4_xattr_check_entry(entry, size)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 209 | return -EIO; |
| 210 | return cmp ? -ENODATA : 0; |
| 211 | } |
| 212 | |
| 213 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 214 | ext4_xattr_block_get(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 215 | void *buffer, size_t buffer_size) |
| 216 | { |
| 217 | struct buffer_head *bh = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 218 | struct ext4_xattr_entry *entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 219 | size_t size; |
| 220 | int error; |
| 221 | |
| 222 | ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", |
| 223 | name_index, name, buffer, (long)buffer_size); |
| 224 | |
| 225 | error = -ENODATA; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 226 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 227 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 228 | ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl); |
| 229 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 230 | if (!bh) |
| 231 | goto cleanup; |
| 232 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 233 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 234 | if (ext4_xattr_check_block(bh)) { |
| 235 | bad_block: ext4_error(inode->i_sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 236 | "inode %lu: bad block "E3FSBLK, inode->i_ino, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 237 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 238 | error = -EIO; |
| 239 | goto cleanup; |
| 240 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 241 | ext4_xattr_cache_insert(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 242 | entry = BFIRST(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 243 | error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 244 | if (error == -EIO) |
| 245 | goto bad_block; |
| 246 | if (error) |
| 247 | goto cleanup; |
| 248 | size = le32_to_cpu(entry->e_value_size); |
| 249 | if (buffer) { |
| 250 | error = -ERANGE; |
| 251 | if (size > buffer_size) |
| 252 | goto cleanup; |
| 253 | memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs), |
| 254 | size); |
| 255 | } |
| 256 | error = size; |
| 257 | |
| 258 | cleanup: |
| 259 | brelse(bh); |
| 260 | return error; |
| 261 | } |
| 262 | |
| 263 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 264 | ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 265 | void *buffer, size_t buffer_size) |
| 266 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 267 | struct ext4_xattr_ibody_header *header; |
| 268 | struct ext4_xattr_entry *entry; |
| 269 | struct ext4_inode *raw_inode; |
| 270 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 271 | size_t size; |
| 272 | void *end; |
| 273 | int error; |
| 274 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 275 | if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 276 | return -ENODATA; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 277 | error = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 278 | if (error) |
| 279 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 280 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 281 | header = IHDR(inode, raw_inode); |
| 282 | entry = IFIRST(header); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 283 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
| 284 | error = ext4_xattr_check_names(entry, end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 285 | if (error) |
| 286 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 287 | error = ext4_xattr_find_entry(&entry, name_index, name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 288 | end - (void *)entry, 0); |
| 289 | if (error) |
| 290 | goto cleanup; |
| 291 | size = le32_to_cpu(entry->e_value_size); |
| 292 | if (buffer) { |
| 293 | error = -ERANGE; |
| 294 | if (size > buffer_size) |
| 295 | goto cleanup; |
| 296 | memcpy(buffer, (void *)IFIRST(header) + |
| 297 | le16_to_cpu(entry->e_value_offs), size); |
| 298 | } |
| 299 | error = size; |
| 300 | |
| 301 | cleanup: |
| 302 | brelse(iloc.bh); |
| 303 | return error; |
| 304 | } |
| 305 | |
| 306 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 307 | * ext4_xattr_get() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 308 | * |
| 309 | * Copy an extended attribute into the buffer |
| 310 | * provided, or compute the buffer size required. |
| 311 | * Buffer is NULL to compute the size of the buffer required. |
| 312 | * |
| 313 | * Returns a negative error number on failure, or the number of bytes |
| 314 | * used / required on success. |
| 315 | */ |
| 316 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 317 | ext4_xattr_get(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 318 | void *buffer, size_t buffer_size) |
| 319 | { |
| 320 | int error; |
| 321 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 322 | down_read(&EXT4_I(inode)->xattr_sem); |
| 323 | error = ext4_xattr_ibody_get(inode, name_index, name, buffer, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 324 | buffer_size); |
| 325 | if (error == -ENODATA) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 326 | error = ext4_xattr_block_get(inode, name_index, name, buffer, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 327 | buffer_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 328 | up_read(&EXT4_I(inode)->xattr_sem); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 329 | return error; |
| 330 | } |
| 331 | |
| 332 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 333 | ext4_xattr_list_entries(struct inode *inode, struct ext4_xattr_entry *entry, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 334 | char *buffer, size_t buffer_size) |
| 335 | { |
| 336 | size_t rest = buffer_size; |
| 337 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 338 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 339 | struct xattr_handler *handler = |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 340 | ext4_xattr_handler(entry->e_name_index); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 341 | |
| 342 | if (handler) { |
| 343 | size_t size = handler->list(inode, buffer, rest, |
| 344 | entry->e_name, |
| 345 | entry->e_name_len); |
| 346 | if (buffer) { |
| 347 | if (size > rest) |
| 348 | return -ERANGE; |
| 349 | buffer += size; |
| 350 | } |
| 351 | rest -= size; |
| 352 | } |
| 353 | } |
| 354 | return buffer_size - rest; |
| 355 | } |
| 356 | |
| 357 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 358 | ext4_xattr_block_list(struct inode *inode, char *buffer, size_t buffer_size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 359 | { |
| 360 | struct buffer_head *bh = NULL; |
| 361 | int error; |
| 362 | |
| 363 | ea_idebug(inode, "buffer=%p, buffer_size=%ld", |
| 364 | buffer, (long)buffer_size); |
| 365 | |
| 366 | error = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 367 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 368 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 369 | ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl); |
| 370 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 371 | error = -EIO; |
| 372 | if (!bh) |
| 373 | goto cleanup; |
| 374 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 375 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 376 | if (ext4_xattr_check_block(bh)) { |
| 377 | ext4_error(inode->i_sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 378 | "inode %lu: bad block "E3FSBLK, inode->i_ino, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 379 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 380 | error = -EIO; |
| 381 | goto cleanup; |
| 382 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 383 | ext4_xattr_cache_insert(bh); |
| 384 | error = ext4_xattr_list_entries(inode, BFIRST(bh), buffer, buffer_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 385 | |
| 386 | cleanup: |
| 387 | brelse(bh); |
| 388 | |
| 389 | return error; |
| 390 | } |
| 391 | |
| 392 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 393 | ext4_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 394 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 395 | struct ext4_xattr_ibody_header *header; |
| 396 | struct ext4_inode *raw_inode; |
| 397 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 398 | void *end; |
| 399 | int error; |
| 400 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 401 | if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 402 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 403 | error = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 404 | if (error) |
| 405 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 406 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 407 | header = IHDR(inode, raw_inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 408 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
| 409 | error = ext4_xattr_check_names(IFIRST(header), end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 410 | if (error) |
| 411 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 412 | error = ext4_xattr_list_entries(inode, IFIRST(header), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 413 | buffer, buffer_size); |
| 414 | |
| 415 | cleanup: |
| 416 | brelse(iloc.bh); |
| 417 | return error; |
| 418 | } |
| 419 | |
| 420 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 421 | * ext4_xattr_list() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 422 | * |
| 423 | * Copy a list of attribute names into the buffer |
| 424 | * provided, or compute the buffer size required. |
| 425 | * Buffer is NULL to compute the size of the buffer required. |
| 426 | * |
| 427 | * Returns a negative error number on failure, or the number of bytes |
| 428 | * used / required on success. |
| 429 | */ |
| 430 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 431 | ext4_xattr_list(struct inode *inode, char *buffer, size_t buffer_size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 432 | { |
| 433 | int i_error, b_error; |
| 434 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 435 | down_read(&EXT4_I(inode)->xattr_sem); |
| 436 | i_error = ext4_xattr_ibody_list(inode, buffer, buffer_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 437 | if (i_error < 0) { |
| 438 | b_error = 0; |
| 439 | } else { |
| 440 | if (buffer) { |
| 441 | buffer += i_error; |
| 442 | buffer_size -= i_error; |
| 443 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 444 | b_error = ext4_xattr_block_list(inode, buffer, buffer_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 445 | if (b_error < 0) |
| 446 | i_error = 0; |
| 447 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 448 | up_read(&EXT4_I(inode)->xattr_sem); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 449 | return i_error + b_error; |
| 450 | } |
| 451 | |
| 452 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 453 | * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 454 | * not set, set it. |
| 455 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 456 | static void ext4_xattr_update_super_block(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 457 | struct super_block *sb) |
| 458 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 459 | if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 460 | return; |
| 461 | |
| 462 | lock_super(sb); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 463 | if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) { |
| 464 | EXT4_SB(sb)->s_es->s_feature_compat |= |
| 465 | cpu_to_le32(EXT4_FEATURE_COMPAT_EXT_ATTR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 466 | sb->s_dirt = 1; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 467 | ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 468 | } |
| 469 | unlock_super(sb); |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * Release the xattr block BH: If the reference count is > 1, decrement |
| 474 | * it; otherwise free the block. |
| 475 | */ |
| 476 | static void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 477 | ext4_xattr_release_block(handle_t *handle, struct inode *inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 478 | struct buffer_head *bh) |
| 479 | { |
| 480 | struct mb_cache_entry *ce = NULL; |
| 481 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 482 | ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 483 | if (BHDR(bh)->h_refcount == cpu_to_le32(1)) { |
| 484 | ea_bdebug(bh, "refcount now=0; freeing"); |
| 485 | if (ce) |
| 486 | mb_cache_entry_free(ce); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 487 | ext4_free_blocks(handle, inode, bh->b_blocknr, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 488 | get_bh(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 489 | ext4_forget(handle, 1, inode, bh, bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 490 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 491 | if (ext4_journal_get_write_access(handle, bh) == 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 492 | lock_buffer(bh); |
| 493 | BHDR(bh)->h_refcount = cpu_to_le32( |
| 494 | le32_to_cpu(BHDR(bh)->h_refcount) - 1); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 495 | ext4_journal_dirty_metadata(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 496 | if (IS_SYNC(inode)) |
| 497 | handle->h_sync = 1; |
| 498 | DQUOT_FREE_BLOCK(inode, 1); |
| 499 | unlock_buffer(bh); |
| 500 | ea_bdebug(bh, "refcount now=%d; releasing", |
| 501 | le32_to_cpu(BHDR(bh)->h_refcount)); |
| 502 | } |
| 503 | if (ce) |
| 504 | mb_cache_entry_release(ce); |
| 505 | } |
| 506 | } |
| 507 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 508 | struct ext4_xattr_info { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 509 | int name_index; |
| 510 | const char *name; |
| 511 | const void *value; |
| 512 | size_t value_len; |
| 513 | }; |
| 514 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 515 | struct ext4_xattr_search { |
| 516 | struct ext4_xattr_entry *first; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 517 | void *base; |
| 518 | void *end; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 519 | struct ext4_xattr_entry *here; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 520 | int not_found; |
| 521 | }; |
| 522 | |
| 523 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 524 | ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 525 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 526 | struct ext4_xattr_entry *last; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 527 | size_t free, min_offs = s->end - s->base, name_len = strlen(i->name); |
| 528 | |
| 529 | /* Compute min_offs and last. */ |
| 530 | last = s->first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 531 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 532 | if (!last->e_value_block && last->e_value_size) { |
| 533 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 534 | if (offs < min_offs) |
| 535 | min_offs = offs; |
| 536 | } |
| 537 | } |
| 538 | free = min_offs - ((void *)last - s->base) - sizeof(__u32); |
| 539 | if (!s->not_found) { |
| 540 | if (!s->here->e_value_block && s->here->e_value_size) { |
| 541 | size_t size = le32_to_cpu(s->here->e_value_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 542 | free += EXT4_XATTR_SIZE(size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 543 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 544 | free += EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 545 | } |
| 546 | if (i->value) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 547 | if (free < EXT4_XATTR_SIZE(i->value_len) || |
| 548 | free < EXT4_XATTR_LEN(name_len) + |
| 549 | EXT4_XATTR_SIZE(i->value_len)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 550 | return -ENOSPC; |
| 551 | } |
| 552 | |
| 553 | if (i->value && s->not_found) { |
| 554 | /* Insert the new name. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 555 | size_t size = EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 556 | size_t rest = (void *)last - (void *)s->here + sizeof(__u32); |
| 557 | memmove((void *)s->here + size, s->here, rest); |
| 558 | memset(s->here, 0, size); |
| 559 | s->here->e_name_index = i->name_index; |
| 560 | s->here->e_name_len = name_len; |
| 561 | memcpy(s->here->e_name, i->name, name_len); |
| 562 | } else { |
| 563 | if (!s->here->e_value_block && s->here->e_value_size) { |
| 564 | void *first_val = s->base + min_offs; |
| 565 | size_t offs = le16_to_cpu(s->here->e_value_offs); |
| 566 | void *val = s->base + offs; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 567 | size_t size = EXT4_XATTR_SIZE( |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 568 | le32_to_cpu(s->here->e_value_size)); |
| 569 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 570 | if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 571 | /* The old and the new value have the same |
| 572 | size. Just replace. */ |
| 573 | s->here->e_value_size = |
| 574 | cpu_to_le32(i->value_len); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 575 | memset(val + size - EXT4_XATTR_PAD, 0, |
| 576 | EXT4_XATTR_PAD); /* Clear pad bytes. */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 577 | memcpy(val, i->value, i->value_len); |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /* Remove the old value. */ |
| 582 | memmove(first_val + size, first_val, val - first_val); |
| 583 | memset(first_val, 0, size); |
| 584 | s->here->e_value_size = 0; |
| 585 | s->here->e_value_offs = 0; |
| 586 | min_offs += size; |
| 587 | |
| 588 | /* Adjust all value offsets. */ |
| 589 | last = s->first; |
| 590 | while (!IS_LAST_ENTRY(last)) { |
| 591 | size_t o = le16_to_cpu(last->e_value_offs); |
| 592 | if (!last->e_value_block && |
| 593 | last->e_value_size && o < offs) |
| 594 | last->e_value_offs = |
| 595 | cpu_to_le16(o + size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 596 | last = EXT4_XATTR_NEXT(last); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | if (!i->value) { |
| 600 | /* Remove the old name. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 601 | size_t size = EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 602 | last = ENTRY((void *)last - size); |
| 603 | memmove(s->here, (void *)s->here + size, |
| 604 | (void *)last - (void *)s->here + sizeof(__u32)); |
| 605 | memset(last, 0, size); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if (i->value) { |
| 610 | /* Insert the new value. */ |
| 611 | s->here->e_value_size = cpu_to_le32(i->value_len); |
| 612 | if (i->value_len) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 613 | size_t size = EXT4_XATTR_SIZE(i->value_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 614 | void *val = s->base + min_offs - size; |
| 615 | s->here->e_value_offs = cpu_to_le16(min_offs - size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 616 | memset(val + size - EXT4_XATTR_PAD, 0, |
| 617 | EXT4_XATTR_PAD); /* Clear the pad bytes. */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 618 | memcpy(val, i->value, i->value_len); |
| 619 | } |
| 620 | } |
| 621 | return 0; |
| 622 | } |
| 623 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 624 | struct ext4_xattr_block_find { |
| 625 | struct ext4_xattr_search s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 626 | struct buffer_head *bh; |
| 627 | }; |
| 628 | |
| 629 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 630 | ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, |
| 631 | struct ext4_xattr_block_find *bs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 632 | { |
| 633 | struct super_block *sb = inode->i_sb; |
| 634 | int error; |
| 635 | |
| 636 | ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", |
| 637 | i->name_index, i->name, i->value, (long)i->value_len); |
| 638 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 639 | if (EXT4_I(inode)->i_file_acl) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 640 | /* The inode already has an extended attribute block. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 641 | bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 642 | error = -EIO; |
| 643 | if (!bs->bh) |
| 644 | goto cleanup; |
| 645 | ea_bdebug(bs->bh, "b_count=%d, refcount=%d", |
| 646 | atomic_read(&(bs->bh->b_count)), |
| 647 | le32_to_cpu(BHDR(bs->bh)->h_refcount)); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 648 | if (ext4_xattr_check_block(bs->bh)) { |
| 649 | ext4_error(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 650 | "inode %lu: bad block "E3FSBLK, inode->i_ino, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 651 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 652 | error = -EIO; |
| 653 | goto cleanup; |
| 654 | } |
| 655 | /* Find the named attribute. */ |
| 656 | bs->s.base = BHDR(bs->bh); |
| 657 | bs->s.first = BFIRST(bs->bh); |
| 658 | bs->s.end = bs->bh->b_data + bs->bh->b_size; |
| 659 | bs->s.here = bs->s.first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 660 | error = ext4_xattr_find_entry(&bs->s.here, i->name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 661 | i->name, bs->bh->b_size, 1); |
| 662 | if (error && error != -ENODATA) |
| 663 | goto cleanup; |
| 664 | bs->s.not_found = error; |
| 665 | } |
| 666 | error = 0; |
| 667 | |
| 668 | cleanup: |
| 669 | return error; |
| 670 | } |
| 671 | |
| 672 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 673 | ext4_xattr_block_set(handle_t *handle, struct inode *inode, |
| 674 | struct ext4_xattr_info *i, |
| 675 | struct ext4_xattr_block_find *bs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 676 | { |
| 677 | struct super_block *sb = inode->i_sb; |
| 678 | struct buffer_head *new_bh = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 679 | struct ext4_xattr_search *s = &bs->s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 680 | struct mb_cache_entry *ce = NULL; |
| 681 | int error; |
| 682 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 683 | #define header(x) ((struct ext4_xattr_header *)(x)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 684 | |
| 685 | if (i->value && i->value_len > sb->s_blocksize) |
| 686 | return -ENOSPC; |
| 687 | if (s->base) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 688 | ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 689 | bs->bh->b_blocknr); |
| 690 | if (header(s->base)->h_refcount == cpu_to_le32(1)) { |
| 691 | if (ce) { |
| 692 | mb_cache_entry_free(ce); |
| 693 | ce = NULL; |
| 694 | } |
| 695 | ea_bdebug(bs->bh, "modifying in-place"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 696 | error = ext4_journal_get_write_access(handle, bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 697 | if (error) |
| 698 | goto cleanup; |
| 699 | lock_buffer(bs->bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 700 | error = ext4_xattr_set_entry(i, s); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 701 | if (!error) { |
| 702 | if (!IS_LAST_ENTRY(s->first)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 703 | ext4_xattr_rehash(header(s->base), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 704 | s->here); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 705 | ext4_xattr_cache_insert(bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 706 | } |
| 707 | unlock_buffer(bs->bh); |
| 708 | if (error == -EIO) |
| 709 | goto bad_block; |
| 710 | if (!error) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 711 | error = ext4_journal_dirty_metadata(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 712 | bs->bh); |
| 713 | if (error) |
| 714 | goto cleanup; |
| 715 | goto inserted; |
| 716 | } else { |
| 717 | int offset = (char *)s->here - bs->bh->b_data; |
| 718 | |
| 719 | if (ce) { |
| 720 | mb_cache_entry_release(ce); |
| 721 | ce = NULL; |
| 722 | } |
| 723 | ea_bdebug(bs->bh, "cloning"); |
| 724 | s->base = kmalloc(bs->bh->b_size, GFP_KERNEL); |
| 725 | error = -ENOMEM; |
| 726 | if (s->base == NULL) |
| 727 | goto cleanup; |
| 728 | memcpy(s->base, BHDR(bs->bh), bs->bh->b_size); |
| 729 | s->first = ENTRY(header(s->base)+1); |
| 730 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 731 | s->here = ENTRY(s->base + offset); |
| 732 | s->end = s->base + bs->bh->b_size; |
| 733 | } |
| 734 | } else { |
| 735 | /* Allocate a buffer where we construct the new block. */ |
| 736 | s->base = kmalloc(sb->s_blocksize, GFP_KERNEL); |
| 737 | /* assert(header == s->base) */ |
| 738 | error = -ENOMEM; |
| 739 | if (s->base == NULL) |
| 740 | goto cleanup; |
| 741 | memset(s->base, 0, sb->s_blocksize); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 742 | header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 743 | header(s->base)->h_blocks = cpu_to_le32(1); |
| 744 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 745 | s->first = ENTRY(header(s->base)+1); |
| 746 | s->here = ENTRY(header(s->base)+1); |
| 747 | s->end = s->base + sb->s_blocksize; |
| 748 | } |
| 749 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 750 | error = ext4_xattr_set_entry(i, s); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 751 | if (error == -EIO) |
| 752 | goto bad_block; |
| 753 | if (error) |
| 754 | goto cleanup; |
| 755 | if (!IS_LAST_ENTRY(s->first)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 756 | ext4_xattr_rehash(header(s->base), s->here); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 757 | |
| 758 | inserted: |
| 759 | if (!IS_LAST_ENTRY(s->first)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 760 | new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 761 | if (new_bh) { |
| 762 | /* We found an identical block in the cache. */ |
| 763 | if (new_bh == bs->bh) |
| 764 | ea_bdebug(new_bh, "keeping"); |
| 765 | else { |
| 766 | /* The old block is released after updating |
| 767 | the inode. */ |
| 768 | error = -EDQUOT; |
| 769 | if (DQUOT_ALLOC_BLOCK(inode, 1)) |
| 770 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 771 | error = ext4_journal_get_write_access(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 772 | new_bh); |
| 773 | if (error) |
| 774 | goto cleanup_dquot; |
| 775 | lock_buffer(new_bh); |
| 776 | BHDR(new_bh)->h_refcount = cpu_to_le32(1 + |
| 777 | le32_to_cpu(BHDR(new_bh)->h_refcount)); |
| 778 | ea_bdebug(new_bh, "reusing; refcount now=%d", |
| 779 | le32_to_cpu(BHDR(new_bh)->h_refcount)); |
| 780 | unlock_buffer(new_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 781 | error = ext4_journal_dirty_metadata(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 782 | new_bh); |
| 783 | if (error) |
| 784 | goto cleanup_dquot; |
| 785 | } |
| 786 | mb_cache_entry_release(ce); |
| 787 | ce = NULL; |
| 788 | } else if (bs->bh && s->base == bs->bh->b_data) { |
| 789 | /* We were modifying this block in-place. */ |
| 790 | ea_bdebug(bs->bh, "keeping this block"); |
| 791 | new_bh = bs->bh; |
| 792 | get_bh(new_bh); |
| 793 | } else { |
| 794 | /* We need to allocate a new block */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 795 | ext4_fsblk_t goal = le32_to_cpu( |
| 796 | EXT4_SB(sb)->s_es->s_first_data_block) + |
| 797 | (ext4_fsblk_t)EXT4_I(inode)->i_block_group * |
| 798 | EXT4_BLOCKS_PER_GROUP(sb); |
| 799 | ext4_fsblk_t block = ext4_new_block(handle, inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 800 | goal, &error); |
| 801 | if (error) |
| 802 | goto cleanup; |
| 803 | ea_idebug(inode, "creating block %d", block); |
| 804 | |
| 805 | new_bh = sb_getblk(sb, block); |
| 806 | if (!new_bh) { |
| 807 | getblk_failed: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 808 | ext4_free_blocks(handle, inode, block, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 809 | error = -EIO; |
| 810 | goto cleanup; |
| 811 | } |
| 812 | lock_buffer(new_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 813 | error = ext4_journal_get_create_access(handle, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 814 | if (error) { |
| 815 | unlock_buffer(new_bh); |
| 816 | goto getblk_failed; |
| 817 | } |
| 818 | memcpy(new_bh->b_data, s->base, new_bh->b_size); |
| 819 | set_buffer_uptodate(new_bh); |
| 820 | unlock_buffer(new_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 821 | ext4_xattr_cache_insert(new_bh); |
| 822 | error = ext4_journal_dirty_metadata(handle, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 823 | if (error) |
| 824 | goto cleanup; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /* Update the inode. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 829 | EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 830 | |
| 831 | /* Drop the previous xattr block. */ |
| 832 | if (bs->bh && bs->bh != new_bh) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 833 | ext4_xattr_release_block(handle, inode, bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 834 | error = 0; |
| 835 | |
| 836 | cleanup: |
| 837 | if (ce) |
| 838 | mb_cache_entry_release(ce); |
| 839 | brelse(new_bh); |
| 840 | if (!(bs->bh && s->base == bs->bh->b_data)) |
| 841 | kfree(s->base); |
| 842 | |
| 843 | return error; |
| 844 | |
| 845 | cleanup_dquot: |
| 846 | DQUOT_FREE_BLOCK(inode, 1); |
| 847 | goto cleanup; |
| 848 | |
| 849 | bad_block: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 850 | ext4_error(inode->i_sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 851 | "inode %lu: bad block "E3FSBLK, inode->i_ino, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 852 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 853 | goto cleanup; |
| 854 | |
| 855 | #undef header |
| 856 | } |
| 857 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 858 | struct ext4_xattr_ibody_find { |
| 859 | struct ext4_xattr_search s; |
| 860 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 861 | }; |
| 862 | |
| 863 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 864 | ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, |
| 865 | struct ext4_xattr_ibody_find *is) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 866 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 867 | struct ext4_xattr_ibody_header *header; |
| 868 | struct ext4_inode *raw_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 869 | int error; |
| 870 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 871 | if (EXT4_I(inode)->i_extra_isize == 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 872 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 873 | raw_inode = ext4_raw_inode(&is->iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 874 | header = IHDR(inode, raw_inode); |
| 875 | is->s.base = is->s.first = IFIRST(header); |
| 876 | is->s.here = is->s.first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 877 | is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
| 878 | if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) { |
| 879 | error = ext4_xattr_check_names(IFIRST(header), is->s.end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 880 | if (error) |
| 881 | return error; |
| 882 | /* Find the named attribute. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 883 | error = ext4_xattr_find_entry(&is->s.here, i->name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 884 | i->name, is->s.end - |
| 885 | (void *)is->s.base, 0); |
| 886 | if (error && error != -ENODATA) |
| 887 | return error; |
| 888 | is->s.not_found = error; |
| 889 | } |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 894 | ext4_xattr_ibody_set(handle_t *handle, struct inode *inode, |
| 895 | struct ext4_xattr_info *i, |
| 896 | struct ext4_xattr_ibody_find *is) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 897 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 898 | struct ext4_xattr_ibody_header *header; |
| 899 | struct ext4_xattr_search *s = &is->s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 900 | int error; |
| 901 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 902 | if (EXT4_I(inode)->i_extra_isize == 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 903 | return -ENOSPC; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 904 | error = ext4_xattr_set_entry(i, s); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 905 | if (error) |
| 906 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 907 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 908 | if (!IS_LAST_ENTRY(s->first)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 909 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
| 910 | EXT4_I(inode)->i_state |= EXT4_STATE_XATTR; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 911 | } else { |
| 912 | header->h_magic = cpu_to_le32(0); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 913 | EXT4_I(inode)->i_state &= ~EXT4_STATE_XATTR; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 914 | } |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 919 | * ext4_xattr_set_handle() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 920 | * |
| 921 | * Create, replace or remove an extended attribute for this inode. Buffer |
| 922 | * is NULL to remove an existing extended attribute, and non-NULL to |
| 923 | * either replace an existing extended attribute, or create a new extended |
| 924 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE |
| 925 | * specify that an extended attribute must exist and must not exist |
| 926 | * previous to the call, respectively. |
| 927 | * |
| 928 | * Returns 0, or a negative error number on failure. |
| 929 | */ |
| 930 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 931 | ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 932 | const char *name, const void *value, size_t value_len, |
| 933 | int flags) |
| 934 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 935 | struct ext4_xattr_info i = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 936 | .name_index = name_index, |
| 937 | .name = name, |
| 938 | .value = value, |
| 939 | .value_len = value_len, |
| 940 | |
| 941 | }; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 942 | struct ext4_xattr_ibody_find is = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 943 | .s = { .not_found = -ENODATA, }, |
| 944 | }; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 945 | struct ext4_xattr_block_find bs = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 946 | .s = { .not_found = -ENODATA, }, |
| 947 | }; |
| 948 | int error; |
| 949 | |
| 950 | if (!name) |
| 951 | return -EINVAL; |
| 952 | if (strlen(name) > 255) |
| 953 | return -ERANGE; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 954 | down_write(&EXT4_I(inode)->xattr_sem); |
| 955 | error = ext4_get_inode_loc(inode, &is.iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 956 | if (error) |
| 957 | goto cleanup; |
| 958 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 959 | if (EXT4_I(inode)->i_state & EXT4_STATE_NEW) { |
| 960 | struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc); |
| 961 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); |
| 962 | EXT4_I(inode)->i_state &= ~EXT4_STATE_NEW; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 965 | error = ext4_xattr_ibody_find(inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 966 | if (error) |
| 967 | goto cleanup; |
| 968 | if (is.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 969 | error = ext4_xattr_block_find(inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 970 | if (error) |
| 971 | goto cleanup; |
| 972 | if (is.s.not_found && bs.s.not_found) { |
| 973 | error = -ENODATA; |
| 974 | if (flags & XATTR_REPLACE) |
| 975 | goto cleanup; |
| 976 | error = 0; |
| 977 | if (!value) |
| 978 | goto cleanup; |
| 979 | } else { |
| 980 | error = -EEXIST; |
| 981 | if (flags & XATTR_CREATE) |
| 982 | goto cleanup; |
| 983 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 984 | error = ext4_journal_get_write_access(handle, is.iloc.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 985 | if (error) |
| 986 | goto cleanup; |
| 987 | if (!value) { |
| 988 | if (!is.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 989 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 990 | else if (!bs.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 991 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 992 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 993 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 994 | if (!error && !bs.s.not_found) { |
| 995 | i.value = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 996 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 997 | } else if (error == -ENOSPC) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 998 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 999 | if (error) |
| 1000 | goto cleanup; |
| 1001 | if (!is.s.not_found) { |
| 1002 | i.value = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1003 | error = ext4_xattr_ibody_set(handle, inode, &i, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1004 | &is); |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | if (!error) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1009 | ext4_xattr_update_super_block(handle, inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1010 | inode->i_ctime = CURRENT_TIME_SEC; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1011 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1012 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1013 | * The bh is consumed by ext4_mark_iloc_dirty, even with |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1014 | * error != 0. |
| 1015 | */ |
| 1016 | is.iloc.bh = NULL; |
| 1017 | if (IS_SYNC(inode)) |
| 1018 | handle->h_sync = 1; |
| 1019 | } |
| 1020 | |
| 1021 | cleanup: |
| 1022 | brelse(is.iloc.bh); |
| 1023 | brelse(bs.bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1024 | up_write(&EXT4_I(inode)->xattr_sem); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1025 | return error; |
| 1026 | } |
| 1027 | |
| 1028 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1029 | * ext4_xattr_set() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1030 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1031 | * Like ext4_xattr_set_handle, but start from an inode. This extended |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1032 | * attribute modification is a filesystem transaction by itself. |
| 1033 | * |
| 1034 | * Returns 0, or a negative error number on failure. |
| 1035 | */ |
| 1036 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1037 | ext4_xattr_set(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1038 | const void *value, size_t value_len, int flags) |
| 1039 | { |
| 1040 | handle_t *handle; |
| 1041 | int error, retries = 0; |
| 1042 | |
| 1043 | retry: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1044 | handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1045 | if (IS_ERR(handle)) { |
| 1046 | error = PTR_ERR(handle); |
| 1047 | } else { |
| 1048 | int error2; |
| 1049 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1050 | error = ext4_xattr_set_handle(handle, inode, name_index, name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1051 | value, value_len, flags); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1052 | error2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1053 | if (error == -ENOSPC && |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1054 | ext4_should_retry_alloc(inode->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1055 | goto retry; |
| 1056 | if (error == 0) |
| 1057 | error = error2; |
| 1058 | } |
| 1059 | |
| 1060 | return error; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1064 | * ext4_xattr_delete_inode() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1065 | * |
| 1066 | * Free extended attribute resources associated with this inode. This |
| 1067 | * is called immediately before an inode is freed. We have exclusive |
| 1068 | * access to the inode. |
| 1069 | */ |
| 1070 | void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1071 | ext4_xattr_delete_inode(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1072 | { |
| 1073 | struct buffer_head *bh = NULL; |
| 1074 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1075 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1076 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1077 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1078 | if (!bh) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1079 | ext4_error(inode->i_sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1080 | "inode %lu: block "E3FSBLK" read error", inode->i_ino, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1081 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1082 | goto cleanup; |
| 1083 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1084 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1085 | BHDR(bh)->h_blocks != cpu_to_le32(1)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1086 | ext4_error(inode->i_sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1087 | "inode %lu: bad block "E3FSBLK, inode->i_ino, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1088 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1089 | goto cleanup; |
| 1090 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1091 | ext4_xattr_release_block(handle, inode, bh); |
| 1092 | EXT4_I(inode)->i_file_acl = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1093 | |
| 1094 | cleanup: |
| 1095 | brelse(bh); |
| 1096 | } |
| 1097 | |
| 1098 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1099 | * ext4_xattr_put_super() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1100 | * |
| 1101 | * This is called when a file system is unmounted. |
| 1102 | */ |
| 1103 | void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1104 | ext4_xattr_put_super(struct super_block *sb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1105 | { |
| 1106 | mb_cache_shrink(sb->s_bdev); |
| 1107 | } |
| 1108 | |
| 1109 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1110 | * ext4_xattr_cache_insert() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1111 | * |
| 1112 | * Create a new entry in the extended attribute cache, and insert |
| 1113 | * it unless such an entry is already in the cache. |
| 1114 | * |
| 1115 | * Returns 0, or a negative error number on failure. |
| 1116 | */ |
| 1117 | static void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1118 | ext4_xattr_cache_insert(struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1119 | { |
| 1120 | __u32 hash = le32_to_cpu(BHDR(bh)->h_hash); |
| 1121 | struct mb_cache_entry *ce; |
| 1122 | int error; |
| 1123 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1124 | ce = mb_cache_entry_alloc(ext4_xattr_cache); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1125 | if (!ce) { |
| 1126 | ea_bdebug(bh, "out of memory"); |
| 1127 | return; |
| 1128 | } |
| 1129 | error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash); |
| 1130 | if (error) { |
| 1131 | mb_cache_entry_free(ce); |
| 1132 | if (error == -EBUSY) { |
| 1133 | ea_bdebug(bh, "already in cache"); |
| 1134 | error = 0; |
| 1135 | } |
| 1136 | } else { |
| 1137 | ea_bdebug(bh, "inserting [%x]", (int)hash); |
| 1138 | mb_cache_entry_release(ce); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1143 | * ext4_xattr_cmp() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1144 | * |
| 1145 | * Compare two extended attribute blocks for equality. |
| 1146 | * |
| 1147 | * Returns 0 if the blocks are equal, 1 if they differ, and |
| 1148 | * a negative error number on errors. |
| 1149 | */ |
| 1150 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1151 | ext4_xattr_cmp(struct ext4_xattr_header *header1, |
| 1152 | struct ext4_xattr_header *header2) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1153 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1154 | struct ext4_xattr_entry *entry1, *entry2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1155 | |
| 1156 | entry1 = ENTRY(header1+1); |
| 1157 | entry2 = ENTRY(header2+1); |
| 1158 | while (!IS_LAST_ENTRY(entry1)) { |
| 1159 | if (IS_LAST_ENTRY(entry2)) |
| 1160 | return 1; |
| 1161 | if (entry1->e_hash != entry2->e_hash || |
| 1162 | entry1->e_name_index != entry2->e_name_index || |
| 1163 | entry1->e_name_len != entry2->e_name_len || |
| 1164 | entry1->e_value_size != entry2->e_value_size || |
| 1165 | memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) |
| 1166 | return 1; |
| 1167 | if (entry1->e_value_block != 0 || entry2->e_value_block != 0) |
| 1168 | return -EIO; |
| 1169 | if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), |
| 1170 | (char *)header2 + le16_to_cpu(entry2->e_value_offs), |
| 1171 | le32_to_cpu(entry1->e_value_size))) |
| 1172 | return 1; |
| 1173 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1174 | entry1 = EXT4_XATTR_NEXT(entry1); |
| 1175 | entry2 = EXT4_XATTR_NEXT(entry2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1176 | } |
| 1177 | if (!IS_LAST_ENTRY(entry2)) |
| 1178 | return 1; |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1183 | * ext4_xattr_cache_find() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1184 | * |
| 1185 | * Find an identical extended attribute block. |
| 1186 | * |
| 1187 | * Returns a pointer to the block found, or NULL if such a block was |
| 1188 | * not found or an error occurred. |
| 1189 | */ |
| 1190 | static struct buffer_head * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1191 | ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1192 | struct mb_cache_entry **pce) |
| 1193 | { |
| 1194 | __u32 hash = le32_to_cpu(header->h_hash); |
| 1195 | struct mb_cache_entry *ce; |
| 1196 | |
| 1197 | if (!header->h_hash) |
| 1198 | return NULL; /* never share */ |
| 1199 | ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); |
| 1200 | again: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1201 | ce = mb_cache_entry_find_first(ext4_xattr_cache, 0, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1202 | inode->i_sb->s_bdev, hash); |
| 1203 | while (ce) { |
| 1204 | struct buffer_head *bh; |
| 1205 | |
| 1206 | if (IS_ERR(ce)) { |
| 1207 | if (PTR_ERR(ce) == -EAGAIN) |
| 1208 | goto again; |
| 1209 | break; |
| 1210 | } |
| 1211 | bh = sb_bread(inode->i_sb, ce->e_block); |
| 1212 | if (!bh) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1213 | ext4_error(inode->i_sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1214 | "inode %lu: block %lu read error", |
| 1215 | inode->i_ino, (unsigned long) ce->e_block); |
| 1216 | } else if (le32_to_cpu(BHDR(bh)->h_refcount) >= |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1217 | EXT4_XATTR_REFCOUNT_MAX) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1218 | ea_idebug(inode, "block %lu refcount %d>=%d", |
| 1219 | (unsigned long) ce->e_block, |
| 1220 | le32_to_cpu(BHDR(bh)->h_refcount), |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1221 | EXT4_XATTR_REFCOUNT_MAX); |
| 1222 | } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1223 | *pce = ce; |
| 1224 | return bh; |
| 1225 | } |
| 1226 | brelse(bh); |
| 1227 | ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash); |
| 1228 | } |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | |
| 1232 | #define NAME_HASH_SHIFT 5 |
| 1233 | #define VALUE_HASH_SHIFT 16 |
| 1234 | |
| 1235 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1236 | * ext4_xattr_hash_entry() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1237 | * |
| 1238 | * Compute the hash of an extended attribute. |
| 1239 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1240 | static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header, |
| 1241 | struct ext4_xattr_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1242 | { |
| 1243 | __u32 hash = 0; |
| 1244 | char *name = entry->e_name; |
| 1245 | int n; |
| 1246 | |
| 1247 | for (n=0; n < entry->e_name_len; n++) { |
| 1248 | hash = (hash << NAME_HASH_SHIFT) ^ |
| 1249 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ |
| 1250 | *name++; |
| 1251 | } |
| 1252 | |
| 1253 | if (entry->e_value_block == 0 && entry->e_value_size != 0) { |
| 1254 | __le32 *value = (__le32 *)((char *)header + |
| 1255 | le16_to_cpu(entry->e_value_offs)); |
| 1256 | for (n = (le32_to_cpu(entry->e_value_size) + |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1257 | EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1258 | hash = (hash << VALUE_HASH_SHIFT) ^ |
| 1259 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ |
| 1260 | le32_to_cpu(*value++); |
| 1261 | } |
| 1262 | } |
| 1263 | entry->e_hash = cpu_to_le32(hash); |
| 1264 | } |
| 1265 | |
| 1266 | #undef NAME_HASH_SHIFT |
| 1267 | #undef VALUE_HASH_SHIFT |
| 1268 | |
| 1269 | #define BLOCK_HASH_SHIFT 16 |
| 1270 | |
| 1271 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1272 | * ext4_xattr_rehash() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1273 | * |
| 1274 | * Re-compute the extended attribute hash value after an entry has changed. |
| 1275 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1276 | static void ext4_xattr_rehash(struct ext4_xattr_header *header, |
| 1277 | struct ext4_xattr_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1278 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1279 | struct ext4_xattr_entry *here; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1280 | __u32 hash = 0; |
| 1281 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1282 | ext4_xattr_hash_entry(header, entry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1283 | here = ENTRY(header+1); |
| 1284 | while (!IS_LAST_ENTRY(here)) { |
| 1285 | if (!here->e_hash) { |
| 1286 | /* Block is not shared if an entry's hash value == 0 */ |
| 1287 | hash = 0; |
| 1288 | break; |
| 1289 | } |
| 1290 | hash = (hash << BLOCK_HASH_SHIFT) ^ |
| 1291 | (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ |
| 1292 | le32_to_cpu(here->e_hash); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1293 | here = EXT4_XATTR_NEXT(here); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1294 | } |
| 1295 | header->h_hash = cpu_to_le32(hash); |
| 1296 | } |
| 1297 | |
| 1298 | #undef BLOCK_HASH_SHIFT |
| 1299 | |
| 1300 | int __init |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1301 | init_ext4_xattr(void) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1302 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1303 | ext4_xattr_cache = mb_cache_create("ext4_xattr", NULL, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1304 | sizeof(struct mb_cache_entry) + |
| 1305 | sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1306 | if (!ext4_xattr_cache) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1307 | return -ENOMEM; |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1312 | exit_ext4_xattr(void) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1313 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1314 | if (ext4_xattr_cache) |
| 1315 | mb_cache_destroy(ext4_xattr_cache); |
| 1316 | ext4_xattr_cache = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1317 | } |