0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 #include <linux/buffer_head.h>
0058 #include <linux/init.h>
0059 #include <linux/printk.h>
0060 #include <linux/slab.h>
0061 #include <linux/mbcache.h>
0062 #include <linux/quotaops.h>
0063 #include <linux/rwsem.h>
0064 #include <linux/security.h>
0065 #include "ext2.h"
0066 #include "xattr.h"
0067 #include "acl.h"
0068
0069 #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
0070 #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
0071 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
0072 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
0073
0074 #ifdef EXT2_XATTR_DEBUG
0075 # define ea_idebug(inode, f...) do { \
0076 printk(KERN_DEBUG "inode %s:%ld: ", \
0077 inode->i_sb->s_id, inode->i_ino); \
0078 printk(f); \
0079 printk("\n"); \
0080 } while (0)
0081 # define ea_bdebug(bh, f...) do { \
0082 printk(KERN_DEBUG "block %pg:%lu: ", \
0083 bh->b_bdev, (unsigned long) bh->b_blocknr); \
0084 printk(f); \
0085 printk("\n"); \
0086 } while (0)
0087 #else
0088 # define ea_idebug(inode, f...) no_printk(f)
0089 # define ea_bdebug(bh, f...) no_printk(f)
0090 #endif
0091
0092 static int ext2_xattr_set2(struct inode *, struct buffer_head *,
0093 struct ext2_xattr_header *);
0094
0095 static int ext2_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
0096 static struct buffer_head *ext2_xattr_cache_find(struct inode *,
0097 struct ext2_xattr_header *);
0098 static void ext2_xattr_rehash(struct ext2_xattr_header *,
0099 struct ext2_xattr_entry *);
0100
0101 static const struct xattr_handler *ext2_xattr_handler_map[] = {
0102 [EXT2_XATTR_INDEX_USER] = &ext2_xattr_user_handler,
0103 #ifdef CONFIG_EXT2_FS_POSIX_ACL
0104 [EXT2_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
0105 [EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
0106 #endif
0107 [EXT2_XATTR_INDEX_TRUSTED] = &ext2_xattr_trusted_handler,
0108 #ifdef CONFIG_EXT2_FS_SECURITY
0109 [EXT2_XATTR_INDEX_SECURITY] = &ext2_xattr_security_handler,
0110 #endif
0111 };
0112
0113 const struct xattr_handler *ext2_xattr_handlers[] = {
0114 &ext2_xattr_user_handler,
0115 &ext2_xattr_trusted_handler,
0116 #ifdef CONFIG_EXT2_FS_POSIX_ACL
0117 &posix_acl_access_xattr_handler,
0118 &posix_acl_default_xattr_handler,
0119 #endif
0120 #ifdef CONFIG_EXT2_FS_SECURITY
0121 &ext2_xattr_security_handler,
0122 #endif
0123 NULL
0124 };
0125
0126 #define EA_BLOCK_CACHE(inode) (EXT2_SB(inode->i_sb)->s_ea_block_cache)
0127
0128 static inline const struct xattr_handler *
0129 ext2_xattr_handler(int name_index)
0130 {
0131 const struct xattr_handler *handler = NULL;
0132
0133 if (name_index > 0 && name_index < ARRAY_SIZE(ext2_xattr_handler_map))
0134 handler = ext2_xattr_handler_map[name_index];
0135 return handler;
0136 }
0137
0138 static bool
0139 ext2_xattr_header_valid(struct ext2_xattr_header *header)
0140 {
0141 if (header->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
0142 header->h_blocks != cpu_to_le32(1))
0143 return false;
0144
0145 return true;
0146 }
0147
0148 static bool
0149 ext2_xattr_entry_valid(struct ext2_xattr_entry *entry,
0150 char *end, size_t end_offs)
0151 {
0152 struct ext2_xattr_entry *next;
0153 size_t size;
0154
0155 next = EXT2_XATTR_NEXT(entry);
0156 if ((char *)next >= end)
0157 return false;
0158
0159 if (entry->e_value_block != 0)
0160 return false;
0161
0162 size = le32_to_cpu(entry->e_value_size);
0163 if (size > end_offs ||
0164 le16_to_cpu(entry->e_value_offs) + size > end_offs)
0165 return false;
0166
0167 return true;
0168 }
0169
0170 static int
0171 ext2_xattr_cmp_entry(int name_index, size_t name_len, const char *name,
0172 struct ext2_xattr_entry *entry)
0173 {
0174 int cmp;
0175
0176 cmp = name_index - entry->e_name_index;
0177 if (!cmp)
0178 cmp = name_len - entry->e_name_len;
0179 if (!cmp)
0180 cmp = memcmp(name, entry->e_name, name_len);
0181
0182 return cmp;
0183 }
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 int
0196 ext2_xattr_get(struct inode *inode, int name_index, const char *name,
0197 void *buffer, size_t buffer_size)
0198 {
0199 struct buffer_head *bh = NULL;
0200 struct ext2_xattr_entry *entry;
0201 size_t name_len, size;
0202 char *end;
0203 int error, not_found;
0204 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
0205
0206 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
0207 name_index, name, buffer, (long)buffer_size);
0208
0209 if (name == NULL)
0210 return -EINVAL;
0211 name_len = strlen(name);
0212 if (name_len > 255)
0213 return -ERANGE;
0214
0215 down_read(&EXT2_I(inode)->xattr_sem);
0216 error = -ENODATA;
0217 if (!EXT2_I(inode)->i_file_acl)
0218 goto cleanup;
0219 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
0220 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
0221 error = -EIO;
0222 if (!bh)
0223 goto cleanup;
0224 ea_bdebug(bh, "b_count=%d, refcount=%d",
0225 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
0226 end = bh->b_data + bh->b_size;
0227 if (!ext2_xattr_header_valid(HDR(bh))) {
0228 bad_block:
0229 ext2_error(inode->i_sb, "ext2_xattr_get",
0230 "inode %ld: bad block %d", inode->i_ino,
0231 EXT2_I(inode)->i_file_acl);
0232 error = -EIO;
0233 goto cleanup;
0234 }
0235
0236
0237 entry = FIRST_ENTRY(bh);
0238 while (!IS_LAST_ENTRY(entry)) {
0239 if (!ext2_xattr_entry_valid(entry, end,
0240 inode->i_sb->s_blocksize))
0241 goto bad_block;
0242
0243 not_found = ext2_xattr_cmp_entry(name_index, name_len, name,
0244 entry);
0245 if (!not_found)
0246 goto found;
0247 if (not_found < 0)
0248 break;
0249
0250 entry = EXT2_XATTR_NEXT(entry);
0251 }
0252 if (ext2_xattr_cache_insert(ea_block_cache, bh))
0253 ea_idebug(inode, "cache insert failed");
0254 error = -ENODATA;
0255 goto cleanup;
0256 found:
0257 size = le32_to_cpu(entry->e_value_size);
0258 if (ext2_xattr_cache_insert(ea_block_cache, bh))
0259 ea_idebug(inode, "cache insert failed");
0260 if (buffer) {
0261 error = -ERANGE;
0262 if (size > buffer_size)
0263 goto cleanup;
0264
0265 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
0266 size);
0267 }
0268 error = size;
0269
0270 cleanup:
0271 brelse(bh);
0272 up_read(&EXT2_I(inode)->xattr_sem);
0273
0274 return error;
0275 }
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287 static int
0288 ext2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
0289 {
0290 struct inode *inode = d_inode(dentry);
0291 struct buffer_head *bh = NULL;
0292 struct ext2_xattr_entry *entry;
0293 char *end;
0294 size_t rest = buffer_size;
0295 int error;
0296 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
0297
0298 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
0299 buffer, (long)buffer_size);
0300
0301 down_read(&EXT2_I(inode)->xattr_sem);
0302 error = 0;
0303 if (!EXT2_I(inode)->i_file_acl)
0304 goto cleanup;
0305 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
0306 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
0307 error = -EIO;
0308 if (!bh)
0309 goto cleanup;
0310 ea_bdebug(bh, "b_count=%d, refcount=%d",
0311 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
0312 end = bh->b_data + bh->b_size;
0313 if (!ext2_xattr_header_valid(HDR(bh))) {
0314 bad_block:
0315 ext2_error(inode->i_sb, "ext2_xattr_list",
0316 "inode %ld: bad block %d", inode->i_ino,
0317 EXT2_I(inode)->i_file_acl);
0318 error = -EIO;
0319 goto cleanup;
0320 }
0321
0322
0323 entry = FIRST_ENTRY(bh);
0324 while (!IS_LAST_ENTRY(entry)) {
0325 if (!ext2_xattr_entry_valid(entry, end,
0326 inode->i_sb->s_blocksize))
0327 goto bad_block;
0328 entry = EXT2_XATTR_NEXT(entry);
0329 }
0330 if (ext2_xattr_cache_insert(ea_block_cache, bh))
0331 ea_idebug(inode, "cache insert failed");
0332
0333
0334 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
0335 entry = EXT2_XATTR_NEXT(entry)) {
0336 const struct xattr_handler *handler =
0337 ext2_xattr_handler(entry->e_name_index);
0338
0339 if (handler && (!handler->list || handler->list(dentry))) {
0340 const char *prefix = handler->prefix ?: handler->name;
0341 size_t prefix_len = strlen(prefix);
0342 size_t size = prefix_len + entry->e_name_len + 1;
0343
0344 if (buffer) {
0345 if (size > rest) {
0346 error = -ERANGE;
0347 goto cleanup;
0348 }
0349 memcpy(buffer, prefix, prefix_len);
0350 buffer += prefix_len;
0351 memcpy(buffer, entry->e_name, entry->e_name_len);
0352 buffer += entry->e_name_len;
0353 *buffer++ = 0;
0354 }
0355 rest -= size;
0356 }
0357 }
0358 error = buffer_size - rest;
0359
0360 cleanup:
0361 brelse(bh);
0362 up_read(&EXT2_I(inode)->xattr_sem);
0363
0364 return error;
0365 }
0366
0367
0368
0369
0370
0371
0372 ssize_t
0373 ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
0374 {
0375 return ext2_xattr_list(dentry, buffer, size);
0376 }
0377
0378
0379
0380
0381
0382 static void ext2_xattr_update_super_block(struct super_block *sb)
0383 {
0384 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
0385 return;
0386
0387 spin_lock(&EXT2_SB(sb)->s_lock);
0388 ext2_update_dynamic_rev(sb);
0389 EXT2_SET_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR);
0390 spin_unlock(&EXT2_SB(sb)->s_lock);
0391 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
0392 }
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406 int
0407 ext2_xattr_set(struct inode *inode, int name_index, const char *name,
0408 const void *value, size_t value_len, int flags)
0409 {
0410 struct super_block *sb = inode->i_sb;
0411 struct buffer_head *bh = NULL;
0412 struct ext2_xattr_header *header = NULL;
0413 struct ext2_xattr_entry *here = NULL, *last = NULL;
0414 size_t name_len, free, min_offs = sb->s_blocksize;
0415 int not_found = 1, error;
0416 char *end;
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
0431 name_index, name, value, (long)value_len);
0432
0433 if (value == NULL)
0434 value_len = 0;
0435 if (name == NULL)
0436 return -EINVAL;
0437 name_len = strlen(name);
0438 if (name_len > 255 || value_len > sb->s_blocksize)
0439 return -ERANGE;
0440 error = dquot_initialize(inode);
0441 if (error)
0442 return error;
0443 down_write(&EXT2_I(inode)->xattr_sem);
0444 if (EXT2_I(inode)->i_file_acl) {
0445
0446 bh = sb_bread(sb, EXT2_I(inode)->i_file_acl);
0447 error = -EIO;
0448 if (!bh)
0449 goto cleanup;
0450 ea_bdebug(bh, "b_count=%d, refcount=%d",
0451 atomic_read(&(bh->b_count)),
0452 le32_to_cpu(HDR(bh)->h_refcount));
0453 header = HDR(bh);
0454 end = bh->b_data + bh->b_size;
0455 if (!ext2_xattr_header_valid(header)) {
0456 bad_block:
0457 ext2_error(sb, "ext2_xattr_set",
0458 "inode %ld: bad block %d", inode->i_ino,
0459 EXT2_I(inode)->i_file_acl);
0460 error = -EIO;
0461 goto cleanup;
0462 }
0463
0464
0465
0466
0467
0468 last = FIRST_ENTRY(bh);
0469 while (!IS_LAST_ENTRY(last)) {
0470 if (!ext2_xattr_entry_valid(last, end, sb->s_blocksize))
0471 goto bad_block;
0472 if (last->e_value_size) {
0473 size_t offs = le16_to_cpu(last->e_value_offs);
0474 if (offs < min_offs)
0475 min_offs = offs;
0476 }
0477 if (not_found > 0) {
0478 not_found = ext2_xattr_cmp_entry(name_index,
0479 name_len,
0480 name, last);
0481 if (not_found <= 0)
0482 here = last;
0483 }
0484 last = EXT2_XATTR_NEXT(last);
0485 }
0486 if (not_found > 0)
0487 here = last;
0488
0489
0490 free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
0491 } else {
0492
0493 free = sb->s_blocksize -
0494 sizeof(struct ext2_xattr_header) - sizeof(__u32);
0495 }
0496
0497 if (not_found) {
0498
0499 error = -ENODATA;
0500 if (flags & XATTR_REPLACE)
0501 goto cleanup;
0502 error = 0;
0503 if (value == NULL)
0504 goto cleanup;
0505 } else {
0506
0507 error = -EEXIST;
0508 if (flags & XATTR_CREATE)
0509 goto cleanup;
0510 free += EXT2_XATTR_SIZE(le32_to_cpu(here->e_value_size));
0511 free += EXT2_XATTR_LEN(name_len);
0512 }
0513 error = -ENOSPC;
0514 if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
0515 goto cleanup;
0516
0517
0518
0519 if (header) {
0520 int offset;
0521
0522 lock_buffer(bh);
0523 if (header->h_refcount == cpu_to_le32(1)) {
0524 __u32 hash = le32_to_cpu(header->h_hash);
0525 struct mb_cache_entry *oe;
0526
0527 oe = mb_cache_entry_delete_or_get(EA_BLOCK_CACHE(inode),
0528 hash, bh->b_blocknr);
0529 if (!oe) {
0530 ea_bdebug(bh, "modifying in-place");
0531 goto update_block;
0532 }
0533
0534
0535
0536 mb_cache_entry_put(EA_BLOCK_CACHE(inode), oe);
0537 }
0538 unlock_buffer(bh);
0539 ea_bdebug(bh, "cloning");
0540 header = kmemdup(HDR(bh), bh->b_size, GFP_KERNEL);
0541 error = -ENOMEM;
0542 if (header == NULL)
0543 goto cleanup;
0544 header->h_refcount = cpu_to_le32(1);
0545
0546 offset = (char *)here - bh->b_data;
0547 here = ENTRY((char *)header + offset);
0548 offset = (char *)last - bh->b_data;
0549 last = ENTRY((char *)header + offset);
0550 } else {
0551
0552 header = kzalloc(sb->s_blocksize, GFP_KERNEL);
0553 error = -ENOMEM;
0554 if (header == NULL)
0555 goto cleanup;
0556 end = (char *)header + sb->s_blocksize;
0557 header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
0558 header->h_blocks = header->h_refcount = cpu_to_le32(1);
0559 last = here = ENTRY(header+1);
0560 }
0561
0562 update_block:
0563
0564
0565 if (not_found) {
0566
0567 size_t size = EXT2_XATTR_LEN(name_len);
0568 size_t rest = (char *)last - (char *)here;
0569 memmove((char *)here + size, here, rest);
0570 memset(here, 0, size);
0571 here->e_name_index = name_index;
0572 here->e_name_len = name_len;
0573 memcpy(here->e_name, name, name_len);
0574 } else {
0575 if (here->e_value_size) {
0576 char *first_val = (char *)header + min_offs;
0577 size_t offs = le16_to_cpu(here->e_value_offs);
0578 char *val = (char *)header + offs;
0579 size_t size = EXT2_XATTR_SIZE(
0580 le32_to_cpu(here->e_value_size));
0581
0582 if (size == EXT2_XATTR_SIZE(value_len)) {
0583
0584
0585 here->e_value_size = cpu_to_le32(value_len);
0586 memset(val + size - EXT2_XATTR_PAD, 0,
0587 EXT2_XATTR_PAD);
0588 memcpy(val, value, value_len);
0589 goto skip_replace;
0590 }
0591
0592
0593 memmove(first_val + size, first_val, val - first_val);
0594 memset(first_val, 0, size);
0595 min_offs += size;
0596
0597
0598 last = ENTRY(header+1);
0599 while (!IS_LAST_ENTRY(last)) {
0600 size_t o = le16_to_cpu(last->e_value_offs);
0601 if (o < offs)
0602 last->e_value_offs =
0603 cpu_to_le16(o + size);
0604 last = EXT2_XATTR_NEXT(last);
0605 }
0606
0607 here->e_value_offs = 0;
0608 }
0609 if (value == NULL) {
0610
0611 size_t size = EXT2_XATTR_LEN(name_len);
0612 last = ENTRY((char *)last - size);
0613 memmove(here, (char*)here + size,
0614 (char*)last - (char*)here);
0615 memset(last, 0, size);
0616 }
0617 }
0618
0619 if (value != NULL) {
0620
0621 here->e_value_size = cpu_to_le32(value_len);
0622 if (value_len) {
0623 size_t size = EXT2_XATTR_SIZE(value_len);
0624 char *val = (char *)header + min_offs - size;
0625 here->e_value_offs =
0626 cpu_to_le16((char *)val - (char *)header);
0627 memset(val + size - EXT2_XATTR_PAD, 0,
0628 EXT2_XATTR_PAD);
0629 memcpy(val, value, value_len);
0630 }
0631 }
0632
0633 skip_replace:
0634 if (IS_LAST_ENTRY(ENTRY(header+1))) {
0635
0636 if (bh && header == HDR(bh))
0637 unlock_buffer(bh);
0638 error = ext2_xattr_set2(inode, bh, NULL);
0639 } else {
0640 ext2_xattr_rehash(header, here);
0641 if (bh && header == HDR(bh))
0642 unlock_buffer(bh);
0643 error = ext2_xattr_set2(inode, bh, header);
0644 }
0645
0646 cleanup:
0647 if (!(bh && header == HDR(bh)))
0648 kfree(header);
0649 brelse(bh);
0650 up_write(&EXT2_I(inode)->xattr_sem);
0651
0652 return error;
0653 }
0654
0655 static void ext2_xattr_release_block(struct inode *inode,
0656 struct buffer_head *bh)
0657 {
0658 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
0659
0660 retry_ref:
0661 lock_buffer(bh);
0662 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
0663 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
0664 struct mb_cache_entry *oe;
0665
0666
0667
0668
0669
0670 oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
0671 bh->b_blocknr);
0672 if (oe) {
0673
0674
0675
0676
0677 unlock_buffer(bh);
0678 mb_cache_entry_wait_unused(oe);
0679 mb_cache_entry_put(ea_block_cache, oe);
0680 goto retry_ref;
0681 }
0682
0683
0684 ea_bdebug(bh, "freeing");
0685 ext2_free_blocks(inode, bh->b_blocknr, 1);
0686
0687
0688 get_bh(bh);
0689 bforget(bh);
0690 unlock_buffer(bh);
0691 } else {
0692
0693 le32_add_cpu(&HDR(bh)->h_refcount, -1);
0694 dquot_free_block(inode, 1);
0695 mark_buffer_dirty(bh);
0696 unlock_buffer(bh);
0697 ea_bdebug(bh, "refcount now=%d",
0698 le32_to_cpu(HDR(bh)->h_refcount));
0699 if (IS_SYNC(inode))
0700 sync_dirty_buffer(bh);
0701 }
0702 }
0703
0704
0705
0706
0707 static int
0708 ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
0709 struct ext2_xattr_header *header)
0710 {
0711 struct super_block *sb = inode->i_sb;
0712 struct buffer_head *new_bh = NULL;
0713 int error;
0714 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
0715
0716 if (header) {
0717 new_bh = ext2_xattr_cache_find(inode, header);
0718 if (new_bh) {
0719
0720 if (new_bh == old_bh) {
0721 ea_bdebug(new_bh, "keeping this block");
0722 } else {
0723
0724
0725 ea_bdebug(new_bh, "reusing block");
0726
0727 error = dquot_alloc_block(inode, 1);
0728 if (error) {
0729 unlock_buffer(new_bh);
0730 goto cleanup;
0731 }
0732 le32_add_cpu(&HDR(new_bh)->h_refcount, 1);
0733 ea_bdebug(new_bh, "refcount now=%d",
0734 le32_to_cpu(HDR(new_bh)->h_refcount));
0735 }
0736 unlock_buffer(new_bh);
0737 } else if (old_bh && header == HDR(old_bh)) {
0738
0739
0740 new_bh = old_bh;
0741 get_bh(new_bh);
0742 ext2_xattr_cache_insert(ea_block_cache, new_bh);
0743 } else {
0744
0745 ext2_fsblk_t goal = ext2_group_first_block_no(sb,
0746 EXT2_I(inode)->i_block_group);
0747 int block = ext2_new_block(inode, goal, &error);
0748 if (error)
0749 goto cleanup;
0750 ea_idebug(inode, "creating block %d", block);
0751
0752 new_bh = sb_getblk(sb, block);
0753 if (unlikely(!new_bh)) {
0754 ext2_free_blocks(inode, block, 1);
0755 mark_inode_dirty(inode);
0756 error = -ENOMEM;
0757 goto cleanup;
0758 }
0759 lock_buffer(new_bh);
0760 memcpy(new_bh->b_data, header, new_bh->b_size);
0761 set_buffer_uptodate(new_bh);
0762 unlock_buffer(new_bh);
0763 ext2_xattr_cache_insert(ea_block_cache, new_bh);
0764
0765 ext2_xattr_update_super_block(sb);
0766 }
0767 mark_buffer_dirty(new_bh);
0768 if (IS_SYNC(inode)) {
0769 sync_dirty_buffer(new_bh);
0770 error = -EIO;
0771 if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
0772 goto cleanup;
0773 }
0774 }
0775
0776
0777 EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
0778 inode->i_ctime = current_time(inode);
0779 if (IS_SYNC(inode)) {
0780 error = sync_inode_metadata(inode, 1);
0781
0782
0783
0784 if (error && error != -ENOSPC) {
0785 if (new_bh && new_bh != old_bh) {
0786 dquot_free_block_nodirty(inode, 1);
0787 mark_inode_dirty(inode);
0788 }
0789 goto cleanup;
0790 }
0791 } else
0792 mark_inode_dirty(inode);
0793
0794 error = 0;
0795 if (old_bh && old_bh != new_bh) {
0796
0797
0798
0799
0800 ext2_xattr_release_block(inode, old_bh);
0801 }
0802
0803 cleanup:
0804 brelse(new_bh);
0805
0806 return error;
0807 }
0808
0809
0810
0811
0812
0813
0814
0815 void
0816 ext2_xattr_delete_inode(struct inode *inode)
0817 {
0818 struct buffer_head *bh = NULL;
0819 struct ext2_sb_info *sbi = EXT2_SB(inode->i_sb);
0820
0821
0822
0823
0824
0825
0826
0827
0828 if (WARN_ON_ONCE(!down_write_trylock(&EXT2_I(inode)->xattr_sem)))
0829 return;
0830 if (!EXT2_I(inode)->i_file_acl)
0831 goto cleanup;
0832
0833 if (!ext2_data_block_valid(sbi, EXT2_I(inode)->i_file_acl, 1)) {
0834 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
0835 "inode %ld: xattr block %d is out of data blocks range",
0836 inode->i_ino, EXT2_I(inode)->i_file_acl);
0837 goto cleanup;
0838 }
0839
0840 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
0841 if (!bh) {
0842 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
0843 "inode %ld: block %d read error", inode->i_ino,
0844 EXT2_I(inode)->i_file_acl);
0845 goto cleanup;
0846 }
0847 ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
0848 if (!ext2_xattr_header_valid(HDR(bh))) {
0849 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
0850 "inode %ld: bad block %d", inode->i_ino,
0851 EXT2_I(inode)->i_file_acl);
0852 goto cleanup;
0853 }
0854 ext2_xattr_release_block(inode, bh);
0855 EXT2_I(inode)->i_file_acl = 0;
0856
0857 cleanup:
0858 brelse(bh);
0859 up_write(&EXT2_I(inode)->xattr_sem);
0860 }
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870 static int
0871 ext2_xattr_cache_insert(struct mb_cache *cache, struct buffer_head *bh)
0872 {
0873 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
0874 int error;
0875
0876 error = mb_cache_entry_create(cache, GFP_NOFS, hash, bh->b_blocknr,
0877 true);
0878 if (error) {
0879 if (error == -EBUSY) {
0880 ea_bdebug(bh, "already in cache");
0881 error = 0;
0882 }
0883 } else
0884 ea_bdebug(bh, "inserting [%x]", (int)hash);
0885 return error;
0886 }
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896 static int
0897 ext2_xattr_cmp(struct ext2_xattr_header *header1,
0898 struct ext2_xattr_header *header2)
0899 {
0900 struct ext2_xattr_entry *entry1, *entry2;
0901
0902 entry1 = ENTRY(header1+1);
0903 entry2 = ENTRY(header2+1);
0904 while (!IS_LAST_ENTRY(entry1)) {
0905 if (IS_LAST_ENTRY(entry2))
0906 return 1;
0907 if (entry1->e_hash != entry2->e_hash ||
0908 entry1->e_name_index != entry2->e_name_index ||
0909 entry1->e_name_len != entry2->e_name_len ||
0910 entry1->e_value_size != entry2->e_value_size ||
0911 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
0912 return 1;
0913 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
0914 return -EIO;
0915 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
0916 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
0917 le32_to_cpu(entry1->e_value_size)))
0918 return 1;
0919
0920 entry1 = EXT2_XATTR_NEXT(entry1);
0921 entry2 = EXT2_XATTR_NEXT(entry2);
0922 }
0923 if (!IS_LAST_ENTRY(entry2))
0924 return 1;
0925 return 0;
0926 }
0927
0928
0929
0930
0931
0932
0933
0934
0935
0936 static struct buffer_head *
0937 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
0938 {
0939 __u32 hash = le32_to_cpu(header->h_hash);
0940 struct mb_cache_entry *ce;
0941 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
0942
0943 if (!header->h_hash)
0944 return NULL;
0945 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
0946
0947 ce = mb_cache_entry_find_first(ea_block_cache, hash);
0948 while (ce) {
0949 struct buffer_head *bh;
0950
0951 bh = sb_bread(inode->i_sb, ce->e_value);
0952 if (!bh) {
0953 ext2_error(inode->i_sb, "ext2_xattr_cache_find",
0954 "inode %ld: block %ld read error",
0955 inode->i_ino, (unsigned long) ce->e_value);
0956 } else {
0957 lock_buffer(bh);
0958 if (le32_to_cpu(HDR(bh)->h_refcount) >
0959 EXT2_XATTR_REFCOUNT_MAX) {
0960 ea_idebug(inode, "block %ld refcount %d>%d",
0961 (unsigned long) ce->e_value,
0962 le32_to_cpu(HDR(bh)->h_refcount),
0963 EXT2_XATTR_REFCOUNT_MAX);
0964 } else if (!ext2_xattr_cmp(header, HDR(bh))) {
0965 ea_bdebug(bh, "b_count=%d",
0966 atomic_read(&(bh->b_count)));
0967 mb_cache_entry_touch(ea_block_cache, ce);
0968 mb_cache_entry_put(ea_block_cache, ce);
0969 return bh;
0970 }
0971 unlock_buffer(bh);
0972 brelse(bh);
0973 }
0974 ce = mb_cache_entry_find_next(ea_block_cache, ce);
0975 }
0976 return NULL;
0977 }
0978
0979 #define NAME_HASH_SHIFT 5
0980 #define VALUE_HASH_SHIFT 16
0981
0982
0983
0984
0985
0986
0987 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
0988 struct ext2_xattr_entry *entry)
0989 {
0990 __u32 hash = 0;
0991 char *name = entry->e_name;
0992 int n;
0993
0994 for (n=0; n < entry->e_name_len; n++) {
0995 hash = (hash << NAME_HASH_SHIFT) ^
0996 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
0997 *name++;
0998 }
0999
1000 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1001 __le32 *value = (__le32 *)((char *)header +
1002 le16_to_cpu(entry->e_value_offs));
1003 for (n = (le32_to_cpu(entry->e_value_size) +
1004 EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
1005 hash = (hash << VALUE_HASH_SHIFT) ^
1006 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1007 le32_to_cpu(*value++);
1008 }
1009 }
1010 entry->e_hash = cpu_to_le32(hash);
1011 }
1012
1013 #undef NAME_HASH_SHIFT
1014 #undef VALUE_HASH_SHIFT
1015
1016 #define BLOCK_HASH_SHIFT 16
1017
1018
1019
1020
1021
1022
1023 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
1024 struct ext2_xattr_entry *entry)
1025 {
1026 struct ext2_xattr_entry *here;
1027 __u32 hash = 0;
1028
1029 ext2_xattr_hash_entry(header, entry);
1030 here = ENTRY(header+1);
1031 while (!IS_LAST_ENTRY(here)) {
1032 if (!here->e_hash) {
1033
1034 hash = 0;
1035 break;
1036 }
1037 hash = (hash << BLOCK_HASH_SHIFT) ^
1038 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1039 le32_to_cpu(here->e_hash);
1040 here = EXT2_XATTR_NEXT(here);
1041 }
1042 header->h_hash = cpu_to_le32(hash);
1043 }
1044
1045 #undef BLOCK_HASH_SHIFT
1046
1047 #define HASH_BUCKET_BITS 10
1048
1049 struct mb_cache *ext2_xattr_create_cache(void)
1050 {
1051 return mb_cache_create(HASH_BUCKET_BITS);
1052 }
1053
1054 void ext2_xattr_destroy_cache(struct mb_cache *cache)
1055 {
1056 if (cache)
1057 mb_cache_destroy(cache);
1058 }