aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ext4/xattr.c')
-rw-r--r--fs/ext4/xattr.c168
1 files changed, 95 insertions, 73 deletions
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index 263002f0389d..b310ed81c10e 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -53,7 +53,7 @@
53#include <linux/init.h> 53#include <linux/init.h>
54#include <linux/fs.h> 54#include <linux/fs.h>
55#include <linux/slab.h> 55#include <linux/slab.h>
56#include <linux/mbcache.h> 56#include <linux/mbcache2.h>
57#include <linux/quotaops.h> 57#include <linux/quotaops.h>
58#include "ext4_jbd2.h" 58#include "ext4_jbd2.h"
59#include "ext4.h" 59#include "ext4.h"
@@ -80,10 +80,10 @@
80# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__) 80# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
81#endif 81#endif
82 82
83static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *); 83static void ext4_xattr_cache_insert(struct mb2_cache *, struct buffer_head *);
84static struct buffer_head *ext4_xattr_cache_find(struct inode *, 84static struct buffer_head *ext4_xattr_cache_find(struct inode *,
85 struct ext4_xattr_header *, 85 struct ext4_xattr_header *,
86 struct mb_cache_entry **); 86 struct mb2_cache_entry **);
87static void ext4_xattr_rehash(struct ext4_xattr_header *, 87static void ext4_xattr_rehash(struct ext4_xattr_header *,
88 struct ext4_xattr_entry *); 88 struct ext4_xattr_entry *);
89static int ext4_xattr_list(struct dentry *dentry, char *buffer, 89static int ext4_xattr_list(struct dentry *dentry, char *buffer,
@@ -233,6 +233,27 @@ ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
233 return error; 233 return error;
234} 234}
235 235
236static int
237__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
238 void *end, const char *function, unsigned int line)
239{
240 struct ext4_xattr_entry *entry = IFIRST(header);
241 int error = -EFSCORRUPTED;
242
243 if (((void *) header >= end) ||
244 (header->h_magic != le32_to_cpu(EXT4_XATTR_MAGIC)))
245 goto errout;
246 error = ext4_xattr_check_names(entry, end, entry);
247errout:
248 if (error)
249 __ext4_error_inode(inode, function, line, 0,
250 "corrupted in-inode xattr");
251 return error;
252}
253
254#define xattr_check_inode(inode, header, end) \
255 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
256
236static inline int 257static inline int
237ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size) 258ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
238{ 259{
@@ -279,7 +300,7 @@ ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
279 struct ext4_xattr_entry *entry; 300 struct ext4_xattr_entry *entry;
280 size_t size; 301 size_t size;
281 int error; 302 int error;
282 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); 303 struct mb2_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
283 304
284 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", 305 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
285 name_index, name, buffer, (long)buffer_size); 306 name_index, name, buffer, (long)buffer_size);
@@ -344,7 +365,7 @@ ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
344 header = IHDR(inode, raw_inode); 365 header = IHDR(inode, raw_inode);
345 entry = IFIRST(header); 366 entry = IFIRST(header);
346 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; 367 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
347 error = ext4_xattr_check_names(entry, end, entry); 368 error = xattr_check_inode(inode, header, end);
348 if (error) 369 if (error)
349 goto cleanup; 370 goto cleanup;
350 error = ext4_xattr_find_entry(&entry, name_index, name, 371 error = ext4_xattr_find_entry(&entry, name_index, name,
@@ -426,7 +447,7 @@ ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
426 struct inode *inode = d_inode(dentry); 447 struct inode *inode = d_inode(dentry);
427 struct buffer_head *bh = NULL; 448 struct buffer_head *bh = NULL;
428 int error; 449 int error;
429 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); 450 struct mb2_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
430 451
431 ea_idebug(inode, "buffer=%p, buffer_size=%ld", 452 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
432 buffer, (long)buffer_size); 453 buffer, (long)buffer_size);
@@ -475,7 +496,7 @@ ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
475 raw_inode = ext4_raw_inode(&iloc); 496 raw_inode = ext4_raw_inode(&iloc);
476 header = IHDR(inode, raw_inode); 497 header = IHDR(inode, raw_inode);
477 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; 498 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
478 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header)); 499 error = xattr_check_inode(inode, header, end);
479 if (error) 500 if (error)
480 goto cleanup; 501 goto cleanup;
481 error = ext4_xattr_list_entries(dentry, IFIRST(header), 502 error = ext4_xattr_list_entries(dentry, IFIRST(header),
@@ -543,11 +564,8 @@ static void
543ext4_xattr_release_block(handle_t *handle, struct inode *inode, 564ext4_xattr_release_block(handle_t *handle, struct inode *inode,
544 struct buffer_head *bh) 565 struct buffer_head *bh)
545{ 566{
546 struct mb_cache_entry *ce = NULL;
547 int error = 0; 567 int error = 0;
548 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
549 568
550 ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
551 BUFFER_TRACE(bh, "get_write_access"); 569 BUFFER_TRACE(bh, "get_write_access");
552 error = ext4_journal_get_write_access(handle, bh); 570 error = ext4_journal_get_write_access(handle, bh);
553 if (error) 571 if (error)
@@ -555,9 +573,15 @@ ext4_xattr_release_block(handle_t *handle, struct inode *inode,
555 573
556 lock_buffer(bh); 574 lock_buffer(bh);
557 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) { 575 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
576 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
577
558 ea_bdebug(bh, "refcount now=0; freeing"); 578 ea_bdebug(bh, "refcount now=0; freeing");
559 if (ce) 579 /*
560 mb_cache_entry_free(ce); 580 * This must happen under buffer lock for
581 * ext4_xattr_block_set() to reliably detect freed block
582 */
583 mb2_cache_entry_delete_block(EXT4_GET_MB_CACHE(inode), hash,
584 bh->b_blocknr);
561 get_bh(bh); 585 get_bh(bh);
562 unlock_buffer(bh); 586 unlock_buffer(bh);
563 ext4_free_blocks(handle, inode, bh, 0, 1, 587 ext4_free_blocks(handle, inode, bh, 0, 1,
@@ -565,8 +589,6 @@ ext4_xattr_release_block(handle_t *handle, struct inode *inode,
565 EXT4_FREE_BLOCKS_FORGET); 589 EXT4_FREE_BLOCKS_FORGET);
566 } else { 590 } else {
567 le32_add_cpu(&BHDR(bh)->h_refcount, -1); 591 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
568 if (ce)
569 mb_cache_entry_release(ce);
570 /* 592 /*
571 * Beware of this ugliness: Releasing of xattr block references 593 * Beware of this ugliness: Releasing of xattr block references
572 * from different inodes can race and so we have to protect 594 * from different inodes can race and so we have to protect
@@ -779,17 +801,15 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
779 struct super_block *sb = inode->i_sb; 801 struct super_block *sb = inode->i_sb;
780 struct buffer_head *new_bh = NULL; 802 struct buffer_head *new_bh = NULL;
781 struct ext4_xattr_search *s = &bs->s; 803 struct ext4_xattr_search *s = &bs->s;
782 struct mb_cache_entry *ce = NULL; 804 struct mb2_cache_entry *ce = NULL;
783 int error = 0; 805 int error = 0;
784 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); 806 struct mb2_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
785 807
786#define header(x) ((struct ext4_xattr_header *)(x)) 808#define header(x) ((struct ext4_xattr_header *)(x))
787 809
788 if (i->value && i->value_len > sb->s_blocksize) 810 if (i->value && i->value_len > sb->s_blocksize)
789 return -ENOSPC; 811 return -ENOSPC;
790 if (s->base) { 812 if (s->base) {
791 ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
792 bs->bh->b_blocknr);
793 BUFFER_TRACE(bs->bh, "get_write_access"); 813 BUFFER_TRACE(bs->bh, "get_write_access");
794 error = ext4_journal_get_write_access(handle, bs->bh); 814 error = ext4_journal_get_write_access(handle, bs->bh);
795 if (error) 815 if (error)
@@ -797,10 +817,15 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
797 lock_buffer(bs->bh); 817 lock_buffer(bs->bh);
798 818
799 if (header(s->base)->h_refcount == cpu_to_le32(1)) { 819 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
800 if (ce) { 820 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
801 mb_cache_entry_free(ce); 821
802 ce = NULL; 822 /*
803 } 823 * This must happen under buffer lock for
824 * ext4_xattr_block_set() to reliably detect modified
825 * block
826 */
827 mb2_cache_entry_delete_block(ext4_mb_cache, hash,
828 bs->bh->b_blocknr);
804 ea_bdebug(bs->bh, "modifying in-place"); 829 ea_bdebug(bs->bh, "modifying in-place");
805 error = ext4_xattr_set_entry(i, s); 830 error = ext4_xattr_set_entry(i, s);
806 if (!error) { 831 if (!error) {
@@ -824,10 +849,6 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
824 int offset = (char *)s->here - bs->bh->b_data; 849 int offset = (char *)s->here - bs->bh->b_data;
825 850
826 unlock_buffer(bs->bh); 851 unlock_buffer(bs->bh);
827 if (ce) {
828 mb_cache_entry_release(ce);
829 ce = NULL;
830 }
831 ea_bdebug(bs->bh, "cloning"); 852 ea_bdebug(bs->bh, "cloning");
832 s->base = kmalloc(bs->bh->b_size, GFP_NOFS); 853 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
833 error = -ENOMEM; 854 error = -ENOMEM;
@@ -882,6 +903,31 @@ inserted:
882 if (error) 903 if (error)
883 goto cleanup_dquot; 904 goto cleanup_dquot;
884 lock_buffer(new_bh); 905 lock_buffer(new_bh);
906 /*
907 * We have to be careful about races with
908 * freeing or rehashing of xattr block. Once we
909 * hold buffer lock xattr block's state is
910 * stable so we can check whether the block got
911 * freed / rehashed or not. Since we unhash
912 * mbcache entry under buffer lock when freeing
913 * / rehashing xattr block, checking whether
914 * entry is still hashed is reliable.
915 */
916 if (hlist_bl_unhashed(&ce->e_hash_list)) {
917 /*
918 * Undo everything and check mbcache
919 * again.
920 */
921 unlock_buffer(new_bh);
922 dquot_free_block(inode,
923 EXT4_C2B(EXT4_SB(sb),
924 1));
925 brelse(new_bh);
926 mb2_cache_entry_put(ext4_mb_cache, ce);
927 ce = NULL;
928 new_bh = NULL;
929 goto inserted;
930 }
885 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1); 931 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
886 ea_bdebug(new_bh, "reusing; refcount now=%d", 932 ea_bdebug(new_bh, "reusing; refcount now=%d",
887 le32_to_cpu(BHDR(new_bh)->h_refcount)); 933 le32_to_cpu(BHDR(new_bh)->h_refcount));
@@ -892,7 +938,8 @@ inserted:
892 if (error) 938 if (error)
893 goto cleanup_dquot; 939 goto cleanup_dquot;
894 } 940 }
895 mb_cache_entry_release(ce); 941 mb2_cache_entry_touch(ext4_mb_cache, ce);
942 mb2_cache_entry_put(ext4_mb_cache, ce);
896 ce = NULL; 943 ce = NULL;
897 } else if (bs->bh && s->base == bs->bh->b_data) { 944 } else if (bs->bh && s->base == bs->bh->b_data) {
898 /* We were modifying this block in-place. */ 945 /* We were modifying this block in-place. */
@@ -957,7 +1004,7 @@ getblk_failed:
957 1004
958cleanup: 1005cleanup:
959 if (ce) 1006 if (ce)
960 mb_cache_entry_release(ce); 1007 mb2_cache_entry_put(ext4_mb_cache, ce);
961 brelse(new_bh); 1008 brelse(new_bh);
962 if (!(bs->bh && s->base == bs->bh->b_data)) 1009 if (!(bs->bh && s->base == bs->bh->b_data))
963 kfree(s->base); 1010 kfree(s->base);
@@ -991,8 +1038,7 @@ int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
991 is->s.here = is->s.first; 1038 is->s.here = is->s.first;
992 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; 1039 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
993 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 1040 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
994 error = ext4_xattr_check_names(IFIRST(header), is->s.end, 1041 error = xattr_check_inode(inode, header, is->s.end);
995 IFIRST(header));
996 if (error) 1042 if (error)
997 return error; 1043 return error;
998 /* Find the named attribute. */ 1044 /* Find the named attribute. */
@@ -1293,6 +1339,10 @@ retry:
1293 last = entry; 1339 last = entry;
1294 total_ino = sizeof(struct ext4_xattr_ibody_header); 1340 total_ino = sizeof(struct ext4_xattr_ibody_header);
1295 1341
1342 error = xattr_check_inode(inode, header, end);
1343 if (error)
1344 goto cleanup;
1345
1296 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino); 1346 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1297 if (free >= isize_diff) { 1347 if (free >= isize_diff) {
1298 entry = IFIRST(header); 1348 entry = IFIRST(header);
@@ -1519,17 +1569,6 @@ cleanup:
1519} 1569}
1520 1570
1521/* 1571/*
1522 * ext4_xattr_put_super()
1523 *
1524 * This is called when a file system is unmounted.
1525 */
1526void
1527ext4_xattr_put_super(struct super_block *sb)
1528{
1529 mb_cache_shrink(sb->s_bdev);
1530}
1531
1532/*
1533 * ext4_xattr_cache_insert() 1572 * ext4_xattr_cache_insert()
1534 * 1573 *
1535 * Create a new entry in the extended attribute cache, and insert 1574 * Create a new entry in the extended attribute cache, and insert
@@ -1538,28 +1577,18 @@ ext4_xattr_put_super(struct super_block *sb)
1538 * Returns 0, or a negative error number on failure. 1577 * Returns 0, or a negative error number on failure.
1539 */ 1578 */
1540static void 1579static void
1541ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh) 1580ext4_xattr_cache_insert(struct mb2_cache *ext4_mb_cache, struct buffer_head *bh)
1542{ 1581{
1543 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash); 1582 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1544 struct mb_cache_entry *ce;
1545 int error; 1583 int error;
1546 1584
1547 ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS); 1585 error = mb2_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
1548 if (!ce) { 1586 bh->b_blocknr);
1549 ea_bdebug(bh, "out of memory");
1550 return;
1551 }
1552 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1553 if (error) { 1587 if (error) {
1554 mb_cache_entry_free(ce); 1588 if (error == -EBUSY)
1555 if (error == -EBUSY) {
1556 ea_bdebug(bh, "already in cache"); 1589 ea_bdebug(bh, "already in cache");
1557 error = 0; 1590 } else
1558 }
1559 } else {
1560 ea_bdebug(bh, "inserting [%x]", (int)hash); 1591 ea_bdebug(bh, "inserting [%x]", (int)hash);
1561 mb_cache_entry_release(ce);
1562 }
1563} 1592}
1564 1593
1565/* 1594/*
@@ -1612,26 +1641,19 @@ ext4_xattr_cmp(struct ext4_xattr_header *header1,
1612 */ 1641 */
1613static struct buffer_head * 1642static struct buffer_head *
1614ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header, 1643ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1615 struct mb_cache_entry **pce) 1644 struct mb2_cache_entry **pce)
1616{ 1645{
1617 __u32 hash = le32_to_cpu(header->h_hash); 1646 __u32 hash = le32_to_cpu(header->h_hash);
1618 struct mb_cache_entry *ce; 1647 struct mb2_cache_entry *ce;
1619 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); 1648 struct mb2_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
1620 1649
1621 if (!header->h_hash) 1650 if (!header->h_hash)
1622 return NULL; /* never share */ 1651 return NULL; /* never share */
1623 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); 1652 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1624again: 1653 ce = mb2_cache_entry_find_first(ext4_mb_cache, hash);
1625 ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
1626 hash);
1627 while (ce) { 1654 while (ce) {
1628 struct buffer_head *bh; 1655 struct buffer_head *bh;
1629 1656
1630 if (IS_ERR(ce)) {
1631 if (PTR_ERR(ce) == -EAGAIN)
1632 goto again;
1633 break;
1634 }
1635 bh = sb_bread(inode->i_sb, ce->e_block); 1657 bh = sb_bread(inode->i_sb, ce->e_block);
1636 if (!bh) { 1658 if (!bh) {
1637 EXT4_ERROR_INODE(inode, "block %lu read error", 1659 EXT4_ERROR_INODE(inode, "block %lu read error",
@@ -1647,7 +1669,7 @@ again:
1647 return bh; 1669 return bh;
1648 } 1670 }
1649 brelse(bh); 1671 brelse(bh);
1650 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash); 1672 ce = mb2_cache_entry_find_next(ext4_mb_cache, ce);
1651 } 1673 }
1652 return NULL; 1674 return NULL;
1653} 1675}
@@ -1722,15 +1744,15 @@ static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1722 1744
1723#define HASH_BUCKET_BITS 10 1745#define HASH_BUCKET_BITS 10
1724 1746
1725struct mb_cache * 1747struct mb2_cache *
1726ext4_xattr_create_cache(char *name) 1748ext4_xattr_create_cache(void)
1727{ 1749{
1728 return mb_cache_create(name, HASH_BUCKET_BITS); 1750 return mb2_cache_create(HASH_BUCKET_BITS);
1729} 1751}
1730 1752
1731void ext4_xattr_destroy_cache(struct mb_cache *cache) 1753void ext4_xattr_destroy_cache(struct mb2_cache *cache)
1732{ 1754{
1733 if (cache) 1755 if (cache)
1734 mb_cache_destroy(cache); 1756 mb2_cache_destroy(cache);
1735} 1757}
1736 1758