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