Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/xattr.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> |
| 5 | * |
| 6 | * Fix by Harrison Xing <harrison@mountainviewdata.com>. |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 7 | * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 8 | * Extended attributes for symlinks and special files added per |
| 9 | * suggestion of Luka Renko <luka.renko@hermes.si>. |
| 10 | * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>, |
| 11 | * Red Hat Inc. |
| 12 | * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz |
| 13 | * and Andreas Gruenbacher <agruen@suse.de>. |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * Extended attributes are stored directly in inodes (on file systems with |
| 18 | * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl |
| 19 | * field contains the block number if an inode uses an additional block. All |
| 20 | * attributes must fit in the inode and one additional block. Blocks that |
| 21 | * contain the identical set of attributes may be shared among several inodes. |
| 22 | * Identical blocks are detected by keeping a cache of blocks that have |
| 23 | * recently been accessed. |
| 24 | * |
| 25 | * The attributes in inodes and on blocks have a different header; the entries |
| 26 | * are stored in the same format: |
| 27 | * |
| 28 | * +------------------+ |
| 29 | * | header | |
| 30 | * | entry 1 | | |
| 31 | * | entry 2 | | growing downwards |
| 32 | * | entry 3 | v |
| 33 | * | four null bytes | |
| 34 | * | . . . | |
| 35 | * | value 1 | ^ |
| 36 | * | value 3 | | growing upwards |
| 37 | * | value 2 | | |
| 38 | * +------------------+ |
| 39 | * |
| 40 | * The header is followed by multiple entry descriptors. In disk blocks, the |
| 41 | * entry descriptors are kept sorted. In inodes, they are unsorted. The |
| 42 | * attribute values are aligned to the end of the block in no specific order. |
| 43 | * |
| 44 | * Locking strategy |
| 45 | * ---------------- |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 46 | * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 47 | * EA blocks are only changed if they are exclusive to an inode, so |
| 48 | * holding xattr_sem also means that nothing but the EA block's reference |
| 49 | * count can change. Multiple writers to the same block are synchronized |
| 50 | * by the buffer lock. |
| 51 | */ |
| 52 | |
| 53 | #include <linux/init.h> |
| 54 | #include <linux/fs.h> |
| 55 | #include <linux/slab.h> |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 56 | #include <linux/mbcache.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 57 | #include <linux/quotaops.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 58 | #include "ext4_jbd2.h" |
| 59 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 60 | #include "xattr.h" |
| 61 | #include "acl.h" |
| 62 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 63 | #ifdef EXT4_XATTR_DEBUG |
Joe Perches | d74f3d2 | 2016-10-15 09:57:31 -0400 | [diff] [blame] | 64 | # define ea_idebug(inode, fmt, ...) \ |
| 65 | printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \ |
| 66 | inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__) |
| 67 | # define ea_bdebug(bh, fmt, ...) \ |
| 68 | printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \ |
| 69 | bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 70 | #else |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 71 | # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__) |
| 72 | # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 73 | #endif |
| 74 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 75 | static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 76 | static struct buffer_head *ext4_xattr_cache_find(struct inode *, |
| 77 | struct ext4_xattr_header *, |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 78 | struct mb_cache_entry **); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 79 | static void ext4_xattr_rehash(struct ext4_xattr_header *, |
| 80 | struct ext4_xattr_entry *); |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 81 | static int ext4_xattr_list(struct dentry *dentry, char *buffer, |
Mingming Cao | d3a95d4 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 82 | size_t buffer_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 83 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 84 | static const struct xattr_handler *ext4_xattr_handler_map[] = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 85 | [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 86 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 87 | [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler, |
| 88 | [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 89 | #endif |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 90 | [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 91 | #ifdef CONFIG_EXT4_FS_SECURITY |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 92 | [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 93 | #endif |
| 94 | }; |
| 95 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 96 | const struct xattr_handler *ext4_xattr_handlers[] = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 97 | &ext4_xattr_user_handler, |
| 98 | &ext4_xattr_trusted_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 99 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 100 | &posix_acl_access_xattr_handler, |
| 101 | &posix_acl_default_xattr_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 102 | #endif |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 103 | #ifdef CONFIG_EXT4_FS_SECURITY |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 104 | &ext4_xattr_security_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 105 | #endif |
| 106 | NULL |
| 107 | }; |
| 108 | |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 109 | #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \ |
| 110 | inode->i_sb->s_fs_info)->s_mb_cache) |
| 111 | |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 112 | static __le32 ext4_xattr_block_csum(struct inode *inode, |
| 113 | sector_t block_nr, |
| 114 | struct ext4_xattr_header *hdr) |
| 115 | { |
| 116 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 117 | __u32 csum; |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 118 | __le64 dsk_block_nr = cpu_to_le64(block_nr); |
Daeho Jeong | b47820e | 2016-07-03 17:51:39 -0400 | [diff] [blame] | 119 | __u32 dummy_csum = 0; |
| 120 | int offset = offsetof(struct ext4_xattr_header, h_checksum); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 121 | |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 122 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr, |
| 123 | sizeof(dsk_block_nr)); |
Daeho Jeong | b47820e | 2016-07-03 17:51:39 -0400 | [diff] [blame] | 124 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset); |
| 125 | csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum)); |
| 126 | offset += sizeof(dummy_csum); |
| 127 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset, |
| 128 | EXT4_BLOCK_SIZE(inode->i_sb) - offset); |
Tao Ma | 41eb70d | 2012-07-09 16:29:27 -0400 | [diff] [blame] | 129 | |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 130 | return cpu_to_le32(csum); |
| 131 | } |
| 132 | |
| 133 | static int ext4_xattr_block_csum_verify(struct inode *inode, |
| 134 | sector_t block_nr, |
| 135 | struct ext4_xattr_header *hdr) |
| 136 | { |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 137 | if (ext4_has_metadata_csum(inode->i_sb) && |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 138 | (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr))) |
| 139 | return 0; |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | static void ext4_xattr_block_csum_set(struct inode *inode, |
| 144 | sector_t block_nr, |
| 145 | struct ext4_xattr_header *hdr) |
| 146 | { |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 147 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 148 | return; |
| 149 | |
| 150 | hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr); |
| 151 | } |
| 152 | |
| 153 | static inline int ext4_handle_dirty_xattr_block(handle_t *handle, |
| 154 | struct inode *inode, |
| 155 | struct buffer_head *bh) |
| 156 | { |
| 157 | ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh)); |
| 158 | return ext4_handle_dirty_metadata(handle, inode, bh); |
| 159 | } |
| 160 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 161 | static inline const struct xattr_handler * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 162 | ext4_xattr_handler(int name_index) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 163 | { |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 164 | const struct xattr_handler *handler = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 165 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 166 | if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map)) |
| 167 | handler = ext4_xattr_handler_map[name_index]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 168 | return handler; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Inode operation listxattr() |
| 173 | * |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 174 | * d_inode(dentry)->i_mutex: don't care |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 175 | */ |
| 176 | ssize_t |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 177 | ext4_listxattr(struct dentry *dentry, char *buffer, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 178 | { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 179 | return ext4_xattr_list(dentry, buffer, size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 183 | ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end, |
| 184 | void *value_start) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 185 | { |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 186 | struct ext4_xattr_entry *e = entry; |
| 187 | |
Eric Biggers | d7614cc | 2016-12-01 14:57:29 -0500 | [diff] [blame] | 188 | /* Find the end of the names list */ |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 189 | while (!IS_LAST_ENTRY(e)) { |
| 190 | struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 191 | if ((void *)next >= end) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 192 | return -EFSCORRUPTED; |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 193 | e = next; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 194 | } |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 195 | |
Eric Biggers | d7614cc | 2016-12-01 14:57:29 -0500 | [diff] [blame] | 196 | /* Check the values */ |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 197 | while (!IS_LAST_ENTRY(entry)) { |
Jan Kara | 2de58f1 | 2016-08-29 15:39:11 -0400 | [diff] [blame] | 198 | if (entry->e_value_block != 0) |
| 199 | return -EFSCORRUPTED; |
Eric Biggers | d7614cc | 2016-12-01 14:57:29 -0500 | [diff] [blame] | 200 | if (entry->e_value_size != 0) { |
| 201 | u16 offs = le16_to_cpu(entry->e_value_offs); |
| 202 | u32 size = le32_to_cpu(entry->e_value_size); |
| 203 | void *value; |
| 204 | |
| 205 | /* |
| 206 | * The value cannot overlap the names, and the value |
| 207 | * with padding cannot extend beyond 'end'. Check both |
| 208 | * the padded and unpadded sizes, since the size may |
| 209 | * overflow to 0 when adding padding. |
| 210 | */ |
| 211 | if (offs > end - value_start) |
| 212 | return -EFSCORRUPTED; |
| 213 | value = value_start + offs; |
| 214 | if (value < (void *)e + sizeof(u32) || |
| 215 | size > end - value || |
| 216 | EXT4_XATTR_SIZE(size) > end - value) |
| 217 | return -EFSCORRUPTED; |
| 218 | } |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 219 | entry = EXT4_XATTR_NEXT(entry); |
| 220 | } |
| 221 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static inline int |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 226 | ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 227 | { |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 228 | int error; |
| 229 | |
| 230 | if (buffer_verified(bh)) |
| 231 | return 0; |
| 232 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 233 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 234 | BHDR(bh)->h_blocks != cpu_to_le32(1)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 235 | return -EFSCORRUPTED; |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 236 | if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh))) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 237 | return -EFSBADCRC; |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 238 | error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size, |
| 239 | bh->b_data); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 240 | if (!error) |
| 241 | set_buffer_verified(bh); |
| 242 | return error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 245 | static int |
| 246 | __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header, |
| 247 | void *end, const char *function, unsigned int line) |
| 248 | { |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 249 | int error = -EFSCORRUPTED; |
| 250 | |
Eric Biggers | 290ab23 | 2016-12-01 14:51:58 -0500 | [diff] [blame] | 251 | if (end - (void *)header < sizeof(*header) + sizeof(u32) || |
Eric Biggers | 1996250 | 2016-10-15 09:39:31 -0400 | [diff] [blame] | 252 | (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC))) |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 253 | goto errout; |
Eric Biggers | 290ab23 | 2016-12-01 14:51:58 -0500 | [diff] [blame] | 254 | error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header)); |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 255 | errout: |
| 256 | if (error) |
| 257 | __ext4_error_inode(inode, function, line, 0, |
| 258 | "corrupted in-inode xattr"); |
| 259 | return error; |
| 260 | } |
| 261 | |
| 262 | #define xattr_check_inode(inode, header, end) \ |
| 263 | __xattr_check_inode((inode), (header), (end), __func__, __LINE__) |
| 264 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 265 | static inline int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 266 | ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 267 | { |
| 268 | size_t value_size = le32_to_cpu(entry->e_value_size); |
| 269 | |
| 270 | if (entry->e_value_block != 0 || value_size > size || |
| 271 | le16_to_cpu(entry->e_value_offs) + value_size > size) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 272 | return -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 277 | ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 278 | const char *name, size_t size, int sorted) |
| 279 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 280 | struct ext4_xattr_entry *entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 281 | size_t name_len; |
| 282 | int cmp = 1; |
| 283 | |
| 284 | if (name == NULL) |
| 285 | return -EINVAL; |
| 286 | name_len = strlen(name); |
| 287 | entry = *pentry; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 288 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 289 | cmp = name_index - entry->e_name_index; |
| 290 | if (!cmp) |
| 291 | cmp = name_len - entry->e_name_len; |
| 292 | if (!cmp) |
| 293 | cmp = memcmp(name, entry->e_name, name_len); |
| 294 | if (cmp <= 0 && (sorted || cmp == 0)) |
| 295 | break; |
| 296 | } |
| 297 | *pentry = entry; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 298 | if (!cmp && ext4_xattr_check_entry(entry, size)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 299 | return -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 300 | return cmp ? -ENODATA : 0; |
| 301 | } |
| 302 | |
| 303 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 304 | ext4_xattr_block_get(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 305 | void *buffer, size_t buffer_size) |
| 306 | { |
| 307 | struct buffer_head *bh = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 308 | struct ext4_xattr_entry *entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 309 | size_t size; |
| 310 | int error; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 311 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 312 | |
| 313 | ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", |
| 314 | name_index, name, buffer, (long)buffer_size); |
| 315 | |
| 316 | error = -ENODATA; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 317 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 318 | goto cleanup; |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 319 | ea_idebug(inode, "reading block %llu", |
| 320 | (unsigned long long)EXT4_I(inode)->i_file_acl); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 321 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 322 | if (!bh) |
| 323 | goto cleanup; |
| 324 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 325 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 326 | if (ext4_xattr_check_block(inode, bh)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 327 | bad_block: |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 328 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 329 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 330 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 331 | goto cleanup; |
| 332 | } |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 333 | ext4_xattr_cache_insert(ext4_mb_cache, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 334 | entry = BFIRST(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 335 | error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 336 | if (error == -EFSCORRUPTED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 337 | goto bad_block; |
| 338 | if (error) |
| 339 | goto cleanup; |
| 340 | size = le32_to_cpu(entry->e_value_size); |
| 341 | if (buffer) { |
| 342 | error = -ERANGE; |
| 343 | if (size > buffer_size) |
| 344 | goto cleanup; |
| 345 | memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs), |
| 346 | size); |
| 347 | } |
| 348 | error = size; |
| 349 | |
| 350 | cleanup: |
| 351 | brelse(bh); |
| 352 | return error; |
| 353 | } |
| 354 | |
Tao Ma | 879b382 | 2012-12-05 10:28:46 -0500 | [diff] [blame] | 355 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 356 | ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 357 | void *buffer, size_t buffer_size) |
| 358 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 359 | struct ext4_xattr_ibody_header *header; |
| 360 | struct ext4_xattr_entry *entry; |
| 361 | struct ext4_inode *raw_inode; |
| 362 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 363 | size_t size; |
| 364 | void *end; |
| 365 | int error; |
| 366 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 367 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 368 | return -ENODATA; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 369 | error = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 370 | if (error) |
| 371 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 372 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 373 | header = IHDR(inode, raw_inode); |
| 374 | entry = IFIRST(header); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 375 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 376 | error = xattr_check_inode(inode, header, end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 377 | if (error) |
| 378 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 379 | error = ext4_xattr_find_entry(&entry, name_index, name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 380 | end - (void *)entry, 0); |
| 381 | if (error) |
| 382 | goto cleanup; |
| 383 | size = le32_to_cpu(entry->e_value_size); |
| 384 | if (buffer) { |
| 385 | error = -ERANGE; |
| 386 | if (size > buffer_size) |
| 387 | goto cleanup; |
| 388 | memcpy(buffer, (void *)IFIRST(header) + |
| 389 | le16_to_cpu(entry->e_value_offs), size); |
| 390 | } |
| 391 | error = size; |
| 392 | |
| 393 | cleanup: |
| 394 | brelse(iloc.bh); |
| 395 | return error; |
| 396 | } |
| 397 | |
| 398 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 399 | * ext4_xattr_get() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 400 | * |
| 401 | * Copy an extended attribute into the buffer |
| 402 | * provided, or compute the buffer size required. |
| 403 | * Buffer is NULL to compute the size of the buffer required. |
| 404 | * |
| 405 | * Returns a negative error number on failure, or the number of bytes |
| 406 | * used / required on success. |
| 407 | */ |
| 408 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 409 | ext4_xattr_get(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 410 | void *buffer, size_t buffer_size) |
| 411 | { |
| 412 | int error; |
| 413 | |
Theodore Ts'o | 0db1ff2 | 2017-02-05 01:28:48 -0500 | [diff] [blame] | 414 | if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) |
| 415 | return -EIO; |
| 416 | |
Zhang Zhen | 230b8c1a | 2014-05-12 09:57:59 -0400 | [diff] [blame] | 417 | if (strlen(name) > 255) |
| 418 | return -ERANGE; |
| 419 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 420 | down_read(&EXT4_I(inode)->xattr_sem); |
| 421 | error = ext4_xattr_ibody_get(inode, name_index, name, buffer, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 422 | buffer_size); |
| 423 | if (error == -ENODATA) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 424 | error = ext4_xattr_block_get(inode, name_index, name, buffer, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 425 | buffer_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 426 | up_read(&EXT4_I(inode)->xattr_sem); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 427 | return error; |
| 428 | } |
| 429 | |
| 430 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 431 | ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 432 | char *buffer, size_t buffer_size) |
| 433 | { |
| 434 | size_t rest = buffer_size; |
| 435 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 436 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 437 | const struct xattr_handler *handler = |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 438 | ext4_xattr_handler(entry->e_name_index); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 439 | |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 440 | if (handler && (!handler->list || handler->list(dentry))) { |
| 441 | const char *prefix = handler->prefix ?: handler->name; |
| 442 | size_t prefix_len = strlen(prefix); |
| 443 | size_t size = prefix_len + entry->e_name_len + 1; |
| 444 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 445 | if (buffer) { |
| 446 | if (size > rest) |
| 447 | return -ERANGE; |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 448 | memcpy(buffer, prefix, prefix_len); |
| 449 | buffer += prefix_len; |
| 450 | memcpy(buffer, entry->e_name, entry->e_name_len); |
| 451 | buffer += entry->e_name_len; |
| 452 | *buffer++ = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 453 | } |
| 454 | rest -= size; |
| 455 | } |
| 456 | } |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 457 | return buffer_size - rest; /* total size */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 461 | ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 462 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 463 | struct inode *inode = d_inode(dentry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 464 | struct buffer_head *bh = NULL; |
| 465 | int error; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 466 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 467 | |
| 468 | ea_idebug(inode, "buffer=%p, buffer_size=%ld", |
| 469 | buffer, (long)buffer_size); |
| 470 | |
| 471 | error = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 472 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 473 | goto cleanup; |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 474 | ea_idebug(inode, "reading block %llu", |
| 475 | (unsigned long long)EXT4_I(inode)->i_file_acl); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 476 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 477 | error = -EIO; |
| 478 | if (!bh) |
| 479 | goto cleanup; |
| 480 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 481 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 482 | if (ext4_xattr_check_block(inode, bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 483 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 484 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 485 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 486 | goto cleanup; |
| 487 | } |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 488 | ext4_xattr_cache_insert(ext4_mb_cache, bh); |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 489 | error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 490 | |
| 491 | cleanup: |
| 492 | brelse(bh); |
| 493 | |
| 494 | return error; |
| 495 | } |
| 496 | |
| 497 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 498 | ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 499 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 500 | struct inode *inode = d_inode(dentry); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 501 | struct ext4_xattr_ibody_header *header; |
| 502 | struct ext4_inode *raw_inode; |
| 503 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 504 | void *end; |
| 505 | int error; |
| 506 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 507 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 508 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 509 | error = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 510 | if (error) |
| 511 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 512 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 513 | header = IHDR(inode, raw_inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 514 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 515 | error = xattr_check_inode(inode, header, end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 516 | if (error) |
| 517 | goto cleanup; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 518 | error = ext4_xattr_list_entries(dentry, IFIRST(header), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 519 | buffer, buffer_size); |
| 520 | |
| 521 | cleanup: |
| 522 | brelse(iloc.bh); |
| 523 | return error; |
| 524 | } |
| 525 | |
| 526 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 527 | * ext4_xattr_list() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 528 | * |
| 529 | * Copy a list of attribute names into the buffer |
| 530 | * provided, or compute the buffer size required. |
| 531 | * Buffer is NULL to compute the size of the buffer required. |
| 532 | * |
| 533 | * Returns a negative error number on failure, or the number of bytes |
| 534 | * used / required on success. |
| 535 | */ |
Mingming Cao | d3a95d4 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 536 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 537 | ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 538 | { |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 539 | int ret, ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 540 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 541 | down_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 542 | ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size); |
| 543 | if (ret < 0) |
| 544 | goto errout; |
| 545 | if (buffer) { |
| 546 | buffer += ret; |
| 547 | buffer_size -= ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 548 | } |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 549 | ret = ext4_xattr_block_list(dentry, buffer, buffer_size); |
| 550 | if (ret < 0) |
| 551 | goto errout; |
| 552 | ret += ret2; |
| 553 | errout: |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 554 | up_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 555 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 559 | * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 560 | * not set, set it. |
| 561 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 562 | static void ext4_xattr_update_super_block(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 563 | struct super_block *sb) |
| 564 | { |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 565 | if (ext4_has_feature_xattr(sb)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 566 | return; |
| 567 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 568 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 569 | if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) { |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 570 | ext4_set_feature_xattr(sb); |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 571 | ext4_handle_dirty_super(handle, sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 572 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 576 | * Release the xattr block BH: If the reference count is > 1, decrement it; |
| 577 | * otherwise free the block. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 578 | */ |
| 579 | static void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 580 | ext4_xattr_release_block(handle_t *handle, struct inode *inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 581 | struct buffer_head *bh) |
| 582 | { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 583 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
| 584 | u32 hash, ref; |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 585 | int error = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 586 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 587 | BUFFER_TRACE(bh, "get_write_access"); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 588 | error = ext4_journal_get_write_access(handle, bh); |
| 589 | if (error) |
| 590 | goto out; |
| 591 | |
| 592 | lock_buffer(bh); |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 593 | hash = le32_to_cpu(BHDR(bh)->h_hash); |
| 594 | ref = le32_to_cpu(BHDR(bh)->h_refcount); |
| 595 | if (ref == 1) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 596 | ea_bdebug(bh, "refcount now=0; freeing"); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 597 | /* |
| 598 | * This must happen under buffer lock for |
| 599 | * ext4_xattr_block_set() to reliably detect freed block |
| 600 | */ |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 601 | mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 602 | get_bh(bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 603 | unlock_buffer(bh); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 604 | ext4_free_blocks(handle, inode, bh, 0, 1, |
| 605 | EXT4_FREE_BLOCKS_METADATA | |
| 606 | EXT4_FREE_BLOCKS_FORGET); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 607 | } else { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 608 | ref--; |
| 609 | BHDR(bh)->h_refcount = cpu_to_le32(ref); |
| 610 | if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) { |
| 611 | struct mb_cache_entry *ce; |
| 612 | |
| 613 | ce = mb_cache_entry_get(ext4_mb_cache, hash, |
| 614 | bh->b_blocknr); |
| 615 | if (ce) { |
| 616 | ce->e_reusable = 1; |
| 617 | mb_cache_entry_put(ext4_mb_cache, ce); |
| 618 | } |
| 619 | } |
| 620 | |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 621 | /* |
| 622 | * Beware of this ugliness: Releasing of xattr block references |
| 623 | * from different inodes can race and so we have to protect |
| 624 | * from a race where someone else frees the block (and releases |
| 625 | * its journal_head) before we are done dirtying the buffer. In |
| 626 | * nojournal mode this race is harmless and we actually cannot |
| 627 | * call ext4_handle_dirty_xattr_block() with locked buffer as |
| 628 | * that function can call sync_dirty_buffer() so for that case |
| 629 | * we handle the dirtying after unlocking the buffer. |
| 630 | */ |
| 631 | if (ext4_handle_valid(handle)) |
| 632 | error = ext4_handle_dirty_xattr_block(handle, inode, |
| 633 | bh); |
Eric Sandeen | c1bb05a | 2012-02-20 23:06:18 -0500 | [diff] [blame] | 634 | unlock_buffer(bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 635 | if (!ext4_handle_valid(handle)) |
| 636 | error = ext4_handle_dirty_xattr_block(handle, inode, |
| 637 | bh); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 638 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 639 | ext4_handle_sync(handle); |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 640 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1)); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 641 | ea_bdebug(bh, "refcount now=%d; releasing", |
| 642 | le32_to_cpu(BHDR(bh)->h_refcount)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 643 | } |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 644 | out: |
| 645 | ext4_std_error(inode->i_sb, error); |
| 646 | return; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 649 | /* |
| 650 | * Find the available free space for EAs. This also returns the total number of |
| 651 | * bytes used by EA entries. |
| 652 | */ |
| 653 | static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last, |
| 654 | size_t *min_offs, void *base, int *total) |
| 655 | { |
| 656 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 657 | if (last->e_value_size) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 658 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 659 | if (offs < *min_offs) |
| 660 | *min_offs = offs; |
| 661 | } |
Theodore Ts'o | 7b1b2c1 | 2014-02-19 20:15:21 -0500 | [diff] [blame] | 662 | if (total) |
| 663 | *total += EXT4_XATTR_LEN(last->e_name_len); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 664 | } |
| 665 | return (*min_offs - ((void *)last - base) - sizeof(__u32)); |
| 666 | } |
| 667 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 668 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 669 | ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 670 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 671 | struct ext4_xattr_entry *last; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 672 | size_t free, min_offs = s->end - s->base, name_len = strlen(i->name); |
| 673 | |
| 674 | /* Compute min_offs and last. */ |
| 675 | last = s->first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 676 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 677 | if (last->e_value_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 678 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 679 | if (offs < min_offs) |
| 680 | min_offs = offs; |
| 681 | } |
| 682 | } |
| 683 | free = min_offs - ((void *)last - s->base) - sizeof(__u32); |
| 684 | if (!s->not_found) { |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 685 | if (s->here->e_value_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 686 | size_t size = le32_to_cpu(s->here->e_value_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 687 | free += EXT4_XATTR_SIZE(size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 688 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 689 | free += EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 690 | } |
| 691 | if (i->value) { |
Wei Yuan | 5f80f62 | 2015-04-02 23:50:48 -0400 | [diff] [blame] | 692 | if (free < EXT4_XATTR_LEN(name_len) + |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 693 | EXT4_XATTR_SIZE(i->value_len)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 694 | return -ENOSPC; |
| 695 | } |
| 696 | |
| 697 | if (i->value && s->not_found) { |
| 698 | /* Insert the new name. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 699 | size_t size = EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 700 | size_t rest = (void *)last - (void *)s->here + sizeof(__u32); |
| 701 | memmove((void *)s->here + size, s->here, rest); |
| 702 | memset(s->here, 0, size); |
| 703 | s->here->e_name_index = i->name_index; |
| 704 | s->here->e_name_len = name_len; |
| 705 | memcpy(s->here->e_name, i->name, name_len); |
| 706 | } else { |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 707 | if (s->here->e_value_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 708 | void *first_val = s->base + min_offs; |
| 709 | size_t offs = le16_to_cpu(s->here->e_value_offs); |
| 710 | void *val = s->base + offs; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 711 | size_t size = EXT4_XATTR_SIZE( |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 712 | le32_to_cpu(s->here->e_value_size)); |
| 713 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 714 | if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 715 | /* The old and the new value have the same |
| 716 | size. Just replace. */ |
| 717 | s->here->e_value_size = |
| 718 | cpu_to_le32(i->value_len); |
Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 719 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
| 720 | memset(val, 0, size); |
| 721 | } else { |
| 722 | /* Clear pad bytes first. */ |
| 723 | memset(val + size - EXT4_XATTR_PAD, 0, |
| 724 | EXT4_XATTR_PAD); |
| 725 | memcpy(val, i->value, i->value_len); |
| 726 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | /* Remove the old value. */ |
| 731 | memmove(first_val + size, first_val, val - first_val); |
| 732 | memset(first_val, 0, size); |
| 733 | s->here->e_value_size = 0; |
| 734 | s->here->e_value_offs = 0; |
| 735 | min_offs += size; |
| 736 | |
| 737 | /* Adjust all value offsets. */ |
| 738 | last = s->first; |
| 739 | while (!IS_LAST_ENTRY(last)) { |
| 740 | size_t o = le16_to_cpu(last->e_value_offs); |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 741 | if (last->e_value_size && o < offs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 742 | last->e_value_offs = |
| 743 | cpu_to_le16(o + size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 744 | last = EXT4_XATTR_NEXT(last); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | if (!i->value) { |
| 748 | /* Remove the old name. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 749 | size_t size = EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 750 | last = ENTRY((void *)last - size); |
| 751 | memmove(s->here, (void *)s->here + size, |
| 752 | (void *)last - (void *)s->here + sizeof(__u32)); |
| 753 | memset(last, 0, size); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (i->value) { |
| 758 | /* Insert the new value. */ |
| 759 | s->here->e_value_size = cpu_to_le32(i->value_len); |
| 760 | if (i->value_len) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 761 | size_t size = EXT4_XATTR_SIZE(i->value_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 762 | void *val = s->base + min_offs - size; |
| 763 | s->here->e_value_offs = cpu_to_le16(min_offs - size); |
Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 764 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
| 765 | memset(val, 0, size); |
| 766 | } else { |
| 767 | /* Clear the pad bytes first. */ |
| 768 | memset(val + size - EXT4_XATTR_PAD, 0, |
| 769 | EXT4_XATTR_PAD); |
| 770 | memcpy(val, i->value, i->value_len); |
| 771 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | return 0; |
| 775 | } |
| 776 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 777 | struct ext4_xattr_block_find { |
| 778 | struct ext4_xattr_search s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 779 | struct buffer_head *bh; |
| 780 | }; |
| 781 | |
| 782 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 783 | ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, |
| 784 | struct ext4_xattr_block_find *bs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 785 | { |
| 786 | struct super_block *sb = inode->i_sb; |
| 787 | int error; |
| 788 | |
| 789 | ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", |
| 790 | i->name_index, i->name, i->value, (long)i->value_len); |
| 791 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 792 | if (EXT4_I(inode)->i_file_acl) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 793 | /* The inode already has an extended attribute block. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 794 | bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 795 | error = -EIO; |
| 796 | if (!bs->bh) |
| 797 | goto cleanup; |
| 798 | ea_bdebug(bs->bh, "b_count=%d, refcount=%d", |
| 799 | atomic_read(&(bs->bh->b_count)), |
| 800 | le32_to_cpu(BHDR(bs->bh)->h_refcount)); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 801 | if (ext4_xattr_check_block(inode, bs->bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 802 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 803 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 804 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 805 | goto cleanup; |
| 806 | } |
| 807 | /* Find the named attribute. */ |
| 808 | bs->s.base = BHDR(bs->bh); |
| 809 | bs->s.first = BFIRST(bs->bh); |
| 810 | bs->s.end = bs->bh->b_data + bs->bh->b_size; |
| 811 | bs->s.here = bs->s.first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 812 | error = ext4_xattr_find_entry(&bs->s.here, i->name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 813 | i->name, bs->bh->b_size, 1); |
| 814 | if (error && error != -ENODATA) |
| 815 | goto cleanup; |
| 816 | bs->s.not_found = error; |
| 817 | } |
| 818 | error = 0; |
| 819 | |
| 820 | cleanup: |
| 821 | return error; |
| 822 | } |
| 823 | |
| 824 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 825 | ext4_xattr_block_set(handle_t *handle, struct inode *inode, |
| 826 | struct ext4_xattr_info *i, |
| 827 | struct ext4_xattr_block_find *bs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 828 | { |
| 829 | struct super_block *sb = inode->i_sb; |
| 830 | struct buffer_head *new_bh = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 831 | struct ext4_xattr_search *s = &bs->s; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 832 | struct mb_cache_entry *ce = NULL; |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 833 | int error = 0; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 834 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 835 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 836 | #define header(x) ((struct ext4_xattr_header *)(x)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 837 | |
| 838 | if (i->value && i->value_len > sb->s_blocksize) |
| 839 | return -ENOSPC; |
| 840 | if (s->base) { |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 841 | BUFFER_TRACE(bs->bh, "get_write_access"); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 842 | error = ext4_journal_get_write_access(handle, bs->bh); |
| 843 | if (error) |
| 844 | goto cleanup; |
| 845 | lock_buffer(bs->bh); |
| 846 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 847 | if (header(s->base)->h_refcount == cpu_to_le32(1)) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 848 | __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash); |
| 849 | |
| 850 | /* |
| 851 | * This must happen under buffer lock for |
| 852 | * ext4_xattr_block_set() to reliably detect modified |
| 853 | * block |
| 854 | */ |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 855 | mb_cache_entry_delete_block(ext4_mb_cache, hash, |
| 856 | bs->bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 857 | ea_bdebug(bs->bh, "modifying in-place"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 858 | error = ext4_xattr_set_entry(i, s); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 859 | if (!error) { |
| 860 | if (!IS_LAST_ENTRY(s->first)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 861 | ext4_xattr_rehash(header(s->base), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 862 | s->here); |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 863 | ext4_xattr_cache_insert(ext4_mb_cache, |
| 864 | bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 865 | } |
| 866 | unlock_buffer(bs->bh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 867 | if (error == -EFSCORRUPTED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 868 | goto bad_block; |
| 869 | if (!error) |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 870 | error = ext4_handle_dirty_xattr_block(handle, |
| 871 | inode, |
| 872 | bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 873 | if (error) |
| 874 | goto cleanup; |
| 875 | goto inserted; |
| 876 | } else { |
| 877 | int offset = (char *)s->here - bs->bh->b_data; |
| 878 | |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 879 | unlock_buffer(bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 880 | ea_bdebug(bs->bh, "cloning"); |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 881 | s->base = kmalloc(bs->bh->b_size, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 882 | error = -ENOMEM; |
| 883 | if (s->base == NULL) |
| 884 | goto cleanup; |
| 885 | memcpy(s->base, BHDR(bs->bh), bs->bh->b_size); |
| 886 | s->first = ENTRY(header(s->base)+1); |
| 887 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 888 | s->here = ENTRY(s->base + offset); |
| 889 | s->end = s->base + bs->bh->b_size; |
| 890 | } |
| 891 | } else { |
| 892 | /* Allocate a buffer where we construct the new block. */ |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 893 | s->base = kzalloc(sb->s_blocksize, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 894 | /* assert(header == s->base) */ |
| 895 | error = -ENOMEM; |
| 896 | if (s->base == NULL) |
| 897 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 898 | header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 899 | header(s->base)->h_blocks = cpu_to_le32(1); |
| 900 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 901 | s->first = ENTRY(header(s->base)+1); |
| 902 | s->here = ENTRY(header(s->base)+1); |
| 903 | s->end = s->base + sb->s_blocksize; |
| 904 | } |
| 905 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 906 | error = ext4_xattr_set_entry(i, s); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 907 | if (error == -EFSCORRUPTED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 908 | goto bad_block; |
| 909 | if (error) |
| 910 | goto cleanup; |
| 911 | if (!IS_LAST_ENTRY(s->first)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 912 | ext4_xattr_rehash(header(s->base), s->here); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 913 | |
| 914 | inserted: |
| 915 | if (!IS_LAST_ENTRY(s->first)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 916 | new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 917 | if (new_bh) { |
| 918 | /* We found an identical block in the cache. */ |
| 919 | if (new_bh == bs->bh) |
| 920 | ea_bdebug(new_bh, "keeping"); |
| 921 | else { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 922 | u32 ref; |
| 923 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 924 | /* The old block is released after updating |
| 925 | the inode. */ |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 926 | error = dquot_alloc_block(inode, |
| 927 | EXT4_C2B(EXT4_SB(sb), 1)); |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 928 | if (error) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 929 | goto cleanup; |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 930 | BUFFER_TRACE(new_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 931 | error = ext4_journal_get_write_access(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 932 | new_bh); |
| 933 | if (error) |
| 934 | goto cleanup_dquot; |
| 935 | lock_buffer(new_bh); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 936 | /* |
| 937 | * We have to be careful about races with |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 938 | * freeing, rehashing or adding references to |
| 939 | * xattr block. Once we hold buffer lock xattr |
| 940 | * block's state is stable so we can check |
| 941 | * whether the block got freed / rehashed or |
| 942 | * not. Since we unhash mbcache entry under |
| 943 | * buffer lock when freeing / rehashing xattr |
| 944 | * block, checking whether entry is still |
| 945 | * hashed is reliable. Same rules hold for |
| 946 | * e_reusable handling. |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 947 | */ |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 948 | if (hlist_bl_unhashed(&ce->e_hash_list) || |
| 949 | !ce->e_reusable) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 950 | /* |
| 951 | * Undo everything and check mbcache |
| 952 | * again. |
| 953 | */ |
| 954 | unlock_buffer(new_bh); |
| 955 | dquot_free_block(inode, |
| 956 | EXT4_C2B(EXT4_SB(sb), |
| 957 | 1)); |
| 958 | brelse(new_bh); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 959 | mb_cache_entry_put(ext4_mb_cache, ce); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 960 | ce = NULL; |
| 961 | new_bh = NULL; |
| 962 | goto inserted; |
| 963 | } |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 964 | ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1; |
| 965 | BHDR(new_bh)->h_refcount = cpu_to_le32(ref); |
| 966 | if (ref >= EXT4_XATTR_REFCOUNT_MAX) |
| 967 | ce->e_reusable = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 968 | ea_bdebug(new_bh, "reusing; refcount now=%d", |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 969 | ref); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 970 | unlock_buffer(new_bh); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 971 | error = ext4_handle_dirty_xattr_block(handle, |
| 972 | inode, |
| 973 | new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 974 | if (error) |
| 975 | goto cleanup_dquot; |
| 976 | } |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 977 | mb_cache_entry_touch(ext4_mb_cache, ce); |
| 978 | mb_cache_entry_put(ext4_mb_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 979 | ce = NULL; |
| 980 | } else if (bs->bh && s->base == bs->bh->b_data) { |
| 981 | /* We were modifying this block in-place. */ |
| 982 | ea_bdebug(bs->bh, "keeping this block"); |
| 983 | new_bh = bs->bh; |
| 984 | get_bh(new_bh); |
| 985 | } else { |
| 986 | /* We need to allocate a new block */ |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 987 | ext4_fsblk_t goal, block; |
| 988 | |
| 989 | goal = ext4_group_first_block_no(sb, |
Akinobu Mita | d00a6d7 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 990 | EXT4_I(inode)->i_block_group); |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 991 | |
| 992 | /* non-extent files can't have physical blocks past 2^32 */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 993 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 994 | goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; |
| 995 | |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 996 | block = ext4_new_meta_blocks(handle, inode, goal, 0, |
| 997 | NULL, &error); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 998 | if (error) |
| 999 | goto cleanup; |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 1000 | |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 1001 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 1002 | BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS); |
| 1003 | |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 1004 | ea_idebug(inode, "creating block %llu", |
| 1005 | (unsigned long long)block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1006 | |
| 1007 | new_bh = sb_getblk(sb, block); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1008 | if (unlikely(!new_bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1009 | error = -ENOMEM; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1010 | getblk_failed: |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 1011 | ext4_free_blocks(handle, inode, NULL, block, 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 1012 | EXT4_FREE_BLOCKS_METADATA); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1013 | goto cleanup; |
| 1014 | } |
| 1015 | lock_buffer(new_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1016 | error = ext4_journal_get_create_access(handle, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1017 | if (error) { |
| 1018 | unlock_buffer(new_bh); |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1019 | error = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1020 | goto getblk_failed; |
| 1021 | } |
| 1022 | memcpy(new_bh->b_data, s->base, new_bh->b_size); |
| 1023 | set_buffer_uptodate(new_bh); |
| 1024 | unlock_buffer(new_bh); |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 1025 | ext4_xattr_cache_insert(ext4_mb_cache, new_bh); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 1026 | error = ext4_handle_dirty_xattr_block(handle, |
| 1027 | inode, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1028 | if (error) |
| 1029 | goto cleanup; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | /* Update the inode. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1034 | EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1035 | |
| 1036 | /* Drop the previous xattr block. */ |
| 1037 | if (bs->bh && bs->bh != new_bh) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1038 | ext4_xattr_release_block(handle, inode, bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1039 | error = 0; |
| 1040 | |
| 1041 | cleanup: |
| 1042 | if (ce) |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1043 | mb_cache_entry_put(ext4_mb_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1044 | brelse(new_bh); |
| 1045 | if (!(bs->bh && s->base == bs->bh->b_data)) |
| 1046 | kfree(s->base); |
| 1047 | |
| 1048 | return error; |
| 1049 | |
| 1050 | cleanup_dquot: |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 1051 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1052 | goto cleanup; |
| 1053 | |
| 1054 | bad_block: |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1055 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 1056 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1057 | goto cleanup; |
| 1058 | |
| 1059 | #undef header |
| 1060 | } |
| 1061 | |
Tao Ma | 879b382 | 2012-12-05 10:28:46 -0500 | [diff] [blame] | 1062 | int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, |
| 1063 | struct ext4_xattr_ibody_find *is) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1064 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1065 | struct ext4_xattr_ibody_header *header; |
| 1066 | struct ext4_inode *raw_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1067 | int error; |
| 1068 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1069 | if (EXT4_I(inode)->i_extra_isize == 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1070 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1071 | raw_inode = ext4_raw_inode(&is->iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1072 | header = IHDR(inode, raw_inode); |
| 1073 | is->s.base = is->s.first = IFIRST(header); |
| 1074 | is->s.here = is->s.first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1075 | is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1076 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 1077 | error = xattr_check_inode(inode, header, is->s.end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1078 | if (error) |
| 1079 | return error; |
| 1080 | /* Find the named attribute. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1081 | error = ext4_xattr_find_entry(&is->s.here, i->name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1082 | i->name, is->s.end - |
| 1083 | (void *)is->s.base, 0); |
| 1084 | if (error && error != -ENODATA) |
| 1085 | return error; |
| 1086 | is->s.not_found = error; |
| 1087 | } |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 1091 | int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode, |
| 1092 | struct ext4_xattr_info *i, |
| 1093 | struct ext4_xattr_ibody_find *is) |
| 1094 | { |
| 1095 | struct ext4_xattr_ibody_header *header; |
| 1096 | struct ext4_xattr_search *s = &is->s; |
| 1097 | int error; |
| 1098 | |
| 1099 | if (EXT4_I(inode)->i_extra_isize == 0) |
| 1100 | return -ENOSPC; |
| 1101 | error = ext4_xattr_set_entry(i, s); |
| 1102 | if (error) { |
| 1103 | if (error == -ENOSPC && |
| 1104 | ext4_has_inline_data(inode)) { |
| 1105 | error = ext4_try_to_evict_inline_data(handle, inode, |
| 1106 | EXT4_XATTR_LEN(strlen(i->name) + |
| 1107 | EXT4_XATTR_SIZE(i->value_len))); |
| 1108 | if (error) |
| 1109 | return error; |
| 1110 | error = ext4_xattr_ibody_find(inode, i, is); |
| 1111 | if (error) |
| 1112 | return error; |
| 1113 | error = ext4_xattr_set_entry(i, s); |
| 1114 | } |
| 1115 | if (error) |
| 1116 | return error; |
| 1117 | } |
| 1118 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
| 1119 | if (!IS_LAST_ENTRY(s->first)) { |
| 1120 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
| 1121 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
| 1122 | } else { |
| 1123 | header->h_magic = cpu_to_le32(0); |
| 1124 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
| 1125 | } |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Eric Whitney | d5c8dab | 2016-11-14 21:56:48 -0500 | [diff] [blame] | 1129 | static int ext4_xattr_ibody_set(struct inode *inode, |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 1130 | struct ext4_xattr_info *i, |
| 1131 | struct ext4_xattr_ibody_find *is) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1132 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1133 | struct ext4_xattr_ibody_header *header; |
| 1134 | struct ext4_xattr_search *s = &is->s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1135 | int error; |
| 1136 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1137 | if (EXT4_I(inode)->i_extra_isize == 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1138 | return -ENOSPC; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1139 | error = ext4_xattr_set_entry(i, s); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1140 | if (error) |
| 1141 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1142 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1143 | if (!IS_LAST_ENTRY(s->first)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1144 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1145 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1146 | } else { |
| 1147 | header->h_magic = cpu_to_le32(0); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1148 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1149 | } |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
Jan Kara | 3fd1646 | 2016-02-22 22:43:04 -0500 | [diff] [blame] | 1153 | static int ext4_xattr_value_same(struct ext4_xattr_search *s, |
| 1154 | struct ext4_xattr_info *i) |
| 1155 | { |
| 1156 | void *value; |
| 1157 | |
| 1158 | if (le32_to_cpu(s->here->e_value_size) != i->value_len) |
| 1159 | return 0; |
| 1160 | value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs); |
| 1161 | return !memcmp(value, i->value, i->value_len); |
| 1162 | } |
| 1163 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1164 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1165 | * ext4_xattr_set_handle() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1166 | * |
Wang Sheng-Hui | 6e9510b | 2011-01-10 12:10:30 -0500 | [diff] [blame] | 1167 | * Create, replace or remove an extended attribute for this inode. Value |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1168 | * is NULL to remove an existing extended attribute, and non-NULL to |
| 1169 | * either replace an existing extended attribute, or create a new extended |
| 1170 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE |
| 1171 | * specify that an extended attribute must exist and must not exist |
| 1172 | * previous to the call, respectively. |
| 1173 | * |
| 1174 | * Returns 0, or a negative error number on failure. |
| 1175 | */ |
| 1176 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1177 | ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1178 | const char *name, const void *value, size_t value_len, |
| 1179 | int flags) |
| 1180 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1181 | struct ext4_xattr_info i = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1182 | .name_index = name_index, |
| 1183 | .name = name, |
| 1184 | .value = value, |
| 1185 | .value_len = value_len, |
| 1186 | |
| 1187 | }; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1188 | struct ext4_xattr_ibody_find is = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1189 | .s = { .not_found = -ENODATA, }, |
| 1190 | }; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1191 | struct ext4_xattr_block_find bs = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1192 | .s = { .not_found = -ENODATA, }, |
| 1193 | }; |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1194 | int no_expand; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1195 | int error; |
| 1196 | |
| 1197 | if (!name) |
| 1198 | return -EINVAL; |
| 1199 | if (strlen(name) > 255) |
| 1200 | return -ERANGE; |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1201 | ext4_write_lock_xattr(inode, &no_expand); |
Kalpak Shah | 4d20c68 | 2008-10-08 23:21:54 -0400 | [diff] [blame] | 1202 | |
Eric Sandeen | 6654361 | 2011-10-26 03:32:07 -0400 | [diff] [blame] | 1203 | error = ext4_reserve_inode_write(handle, inode, &is.iloc); |
Eric Sandeen | 86ebfd0 | 2009-11-15 15:30:52 -0500 | [diff] [blame] | 1204 | if (error) |
| 1205 | goto cleanup; |
| 1206 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1207 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1208 | struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc); |
| 1209 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1210 | ext4_clear_inode_state(inode, EXT4_STATE_NEW); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1213 | error = ext4_xattr_ibody_find(inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1214 | if (error) |
| 1215 | goto cleanup; |
| 1216 | if (is.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1217 | error = ext4_xattr_block_find(inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1218 | if (error) |
| 1219 | goto cleanup; |
| 1220 | if (is.s.not_found && bs.s.not_found) { |
| 1221 | error = -ENODATA; |
| 1222 | if (flags & XATTR_REPLACE) |
| 1223 | goto cleanup; |
| 1224 | error = 0; |
| 1225 | if (!value) |
| 1226 | goto cleanup; |
| 1227 | } else { |
| 1228 | error = -EEXIST; |
| 1229 | if (flags & XATTR_CREATE) |
| 1230 | goto cleanup; |
| 1231 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1232 | if (!value) { |
| 1233 | if (!is.s.not_found) |
Eric Whitney | d5c8dab | 2016-11-14 21:56:48 -0500 | [diff] [blame] | 1234 | error = ext4_xattr_ibody_set(inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1235 | else if (!bs.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1236 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1237 | } else { |
Jan Kara | 3fd1646 | 2016-02-22 22:43:04 -0500 | [diff] [blame] | 1238 | error = 0; |
| 1239 | /* Xattr value did not change? Save us some work and bail out */ |
| 1240 | if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i)) |
| 1241 | goto cleanup; |
| 1242 | if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i)) |
| 1243 | goto cleanup; |
| 1244 | |
Eric Whitney | d5c8dab | 2016-11-14 21:56:48 -0500 | [diff] [blame] | 1245 | error = ext4_xattr_ibody_set(inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1246 | if (!error && !bs.s.not_found) { |
| 1247 | i.value = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1248 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1249 | } else if (error == -ENOSPC) { |
Tiger Yang | 7e01c8e | 2008-05-14 16:05:47 -0700 | [diff] [blame] | 1250 | if (EXT4_I(inode)->i_file_acl && !bs.s.base) { |
| 1251 | error = ext4_xattr_block_find(inode, &i, &bs); |
| 1252 | if (error) |
| 1253 | goto cleanup; |
| 1254 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1255 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1256 | if (error) |
| 1257 | goto cleanup; |
| 1258 | if (!is.s.not_found) { |
| 1259 | i.value = NULL; |
Eric Whitney | d5c8dab | 2016-11-14 21:56:48 -0500 | [diff] [blame] | 1260 | error = ext4_xattr_ibody_set(inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | if (!error) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1265 | ext4_xattr_update_super_block(handle, inode->i_sb); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 1266 | inode->i_ctime = current_time(inode); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1267 | if (!value) |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1268 | no_expand = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1269 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1270 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1271 | * The bh is consumed by ext4_mark_iloc_dirty, even with |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1272 | * error != 0. |
| 1273 | */ |
| 1274 | is.iloc.bh = NULL; |
| 1275 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1276 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | cleanup: |
| 1280 | brelse(is.iloc.bh); |
| 1281 | brelse(bs.bh); |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1282 | ext4_write_unlock_xattr(inode, &no_expand); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1283 | return error; |
| 1284 | } |
| 1285 | |
| 1286 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1287 | * ext4_xattr_set() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1288 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1289 | * Like ext4_xattr_set_handle, but start from an inode. This extended |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1290 | * attribute modification is a filesystem transaction by itself. |
| 1291 | * |
| 1292 | * Returns 0, or a negative error number on failure. |
| 1293 | */ |
| 1294 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1295 | ext4_xattr_set(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1296 | const void *value, size_t value_len, int flags) |
| 1297 | { |
| 1298 | handle_t *handle; |
| 1299 | int error, retries = 0; |
Theodore Ts'o | 95eaefb | 2013-02-09 15:23:03 -0500 | [diff] [blame] | 1300 | int credits = ext4_jbd2_credits_xattr(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1301 | |
| 1302 | retry: |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 1303 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1304 | if (IS_ERR(handle)) { |
| 1305 | error = PTR_ERR(handle); |
| 1306 | } else { |
| 1307 | int error2; |
| 1308 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1309 | error = ext4_xattr_set_handle(handle, inode, name_index, name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1310 | value, value_len, flags); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1311 | error2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1312 | if (error == -ENOSPC && |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1313 | ext4_should_retry_alloc(inode->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1314 | goto retry; |
| 1315 | if (error == 0) |
| 1316 | error = error2; |
| 1317 | } |
| 1318 | |
| 1319 | return error; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1323 | * Shift the EA entries in the inode to create space for the increased |
| 1324 | * i_extra_isize. |
| 1325 | */ |
| 1326 | static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry, |
| 1327 | int value_offs_shift, void *to, |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 1328 | void *from, size_t n) |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1329 | { |
| 1330 | struct ext4_xattr_entry *last = entry; |
| 1331 | int new_offs; |
| 1332 | |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 1333 | /* We always shift xattr headers further thus offsets get lower */ |
| 1334 | BUG_ON(value_offs_shift > 0); |
| 1335 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1336 | /* Adjust the value offsets of the entries */ |
| 1337 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 1338 | if (last->e_value_size) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1339 | new_offs = le16_to_cpu(last->e_value_offs) + |
| 1340 | value_offs_shift; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1341 | last->e_value_offs = cpu_to_le16(new_offs); |
| 1342 | } |
| 1343 | } |
| 1344 | /* Shift the entries by n bytes */ |
| 1345 | memmove(to, from, n); |
| 1346 | } |
| 1347 | |
| 1348 | /* |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1349 | * Move xattr pointed to by 'entry' from inode into external xattr block |
| 1350 | */ |
| 1351 | static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, |
| 1352 | struct ext4_inode *raw_inode, |
| 1353 | struct ext4_xattr_entry *entry) |
| 1354 | { |
| 1355 | struct ext4_xattr_ibody_find *is = NULL; |
| 1356 | struct ext4_xattr_block_find *bs = NULL; |
| 1357 | char *buffer = NULL, *b_entry_name = NULL; |
| 1358 | size_t value_offs, value_size; |
| 1359 | struct ext4_xattr_info i = { |
| 1360 | .value = NULL, |
| 1361 | .value_len = 0, |
| 1362 | .name_index = entry->e_name_index, |
| 1363 | }; |
| 1364 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); |
| 1365 | int error; |
| 1366 | |
| 1367 | value_offs = le16_to_cpu(entry->e_value_offs); |
| 1368 | value_size = le32_to_cpu(entry->e_value_size); |
| 1369 | |
| 1370 | is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); |
| 1371 | bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS); |
| 1372 | buffer = kmalloc(value_size, GFP_NOFS); |
| 1373 | b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS); |
| 1374 | if (!is || !bs || !buffer || !b_entry_name) { |
| 1375 | error = -ENOMEM; |
| 1376 | goto out; |
| 1377 | } |
| 1378 | |
| 1379 | is->s.not_found = -ENODATA; |
| 1380 | bs->s.not_found = -ENODATA; |
| 1381 | is->iloc.bh = NULL; |
| 1382 | bs->bh = NULL; |
| 1383 | |
| 1384 | /* Save the entry name and the entry value */ |
| 1385 | memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size); |
| 1386 | memcpy(b_entry_name, entry->e_name, entry->e_name_len); |
| 1387 | b_entry_name[entry->e_name_len] = '\0'; |
| 1388 | i.name = b_entry_name; |
| 1389 | |
| 1390 | error = ext4_get_inode_loc(inode, &is->iloc); |
| 1391 | if (error) |
| 1392 | goto out; |
| 1393 | |
| 1394 | error = ext4_xattr_ibody_find(inode, &i, is); |
| 1395 | if (error) |
| 1396 | goto out; |
| 1397 | |
| 1398 | /* Remove the chosen entry from the inode */ |
Eric Whitney | d5c8dab | 2016-11-14 21:56:48 -0500 | [diff] [blame] | 1399 | error = ext4_xattr_ibody_set(inode, &i, is); |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1400 | if (error) |
| 1401 | goto out; |
| 1402 | |
| 1403 | i.name = b_entry_name; |
| 1404 | i.value = buffer; |
| 1405 | i.value_len = value_size; |
| 1406 | error = ext4_xattr_block_find(inode, &i, bs); |
| 1407 | if (error) |
| 1408 | goto out; |
| 1409 | |
| 1410 | /* Add entry which was removed from the inode into the block */ |
| 1411 | error = ext4_xattr_block_set(handle, inode, &i, bs); |
| 1412 | if (error) |
| 1413 | goto out; |
| 1414 | error = 0; |
| 1415 | out: |
| 1416 | kfree(b_entry_name); |
| 1417 | kfree(buffer); |
| 1418 | if (is) |
| 1419 | brelse(is->iloc.bh); |
| 1420 | kfree(is); |
| 1421 | kfree(bs); |
| 1422 | |
| 1423 | return error; |
| 1424 | } |
| 1425 | |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1426 | static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode, |
| 1427 | struct ext4_inode *raw_inode, |
| 1428 | int isize_diff, size_t ifree, |
| 1429 | size_t bfree, int *total_ino) |
| 1430 | { |
| 1431 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); |
| 1432 | struct ext4_xattr_entry *small_entry; |
| 1433 | struct ext4_xattr_entry *entry; |
| 1434 | struct ext4_xattr_entry *last; |
| 1435 | unsigned int entry_size; /* EA entry size */ |
| 1436 | unsigned int total_size; /* EA entry size + value size */ |
| 1437 | unsigned int min_total_size; |
| 1438 | int error; |
| 1439 | |
| 1440 | while (isize_diff > ifree) { |
| 1441 | entry = NULL; |
| 1442 | small_entry = NULL; |
| 1443 | min_total_size = ~0U; |
| 1444 | last = IFIRST(header); |
| 1445 | /* Find the entry best suited to be pushed into EA block */ |
| 1446 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
| 1447 | total_size = |
| 1448 | EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) + |
| 1449 | EXT4_XATTR_LEN(last->e_name_len); |
| 1450 | if (total_size <= bfree && |
| 1451 | total_size < min_total_size) { |
| 1452 | if (total_size + ifree < isize_diff) { |
| 1453 | small_entry = last; |
| 1454 | } else { |
| 1455 | entry = last; |
| 1456 | min_total_size = total_size; |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | if (entry == NULL) { |
| 1462 | if (small_entry == NULL) |
| 1463 | return -ENOSPC; |
| 1464 | entry = small_entry; |
| 1465 | } |
| 1466 | |
| 1467 | entry_size = EXT4_XATTR_LEN(entry->e_name_len); |
| 1468 | total_size = entry_size + |
| 1469 | EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)); |
| 1470 | error = ext4_xattr_move_to_block(handle, inode, raw_inode, |
| 1471 | entry); |
| 1472 | if (error) |
| 1473 | return error; |
| 1474 | |
| 1475 | *total_ino -= entry_size; |
| 1476 | ifree += total_size; |
| 1477 | bfree -= total_size; |
| 1478 | } |
| 1479 | |
| 1480 | return 0; |
| 1481 | } |
| 1482 | |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1483 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1484 | * Expand an inode by new_extra_isize bytes when EAs are present. |
| 1485 | * Returns 0 on success or negative error number on failure. |
| 1486 | */ |
| 1487 | int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize, |
| 1488 | struct ext4_inode *raw_inode, handle_t *handle) |
| 1489 | { |
| 1490 | struct ext4_xattr_ibody_header *header; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1491 | struct buffer_head *bh = NULL; |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1492 | size_t min_offs; |
| 1493 | size_t ifree, bfree; |
Theodore Ts'o | 7b1b2c1 | 2014-02-19 20:15:21 -0500 | [diff] [blame] | 1494 | int total_ino; |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1495 | void *base, *end; |
Jan Kara | d014119 | 2016-08-11 11:50:30 -0400 | [diff] [blame] | 1496 | int error = 0, tried_min_extra_isize = 0; |
Aneesh Kumar K.V | ac39849 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1497 | int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize); |
Jan Kara | d014119 | 2016-08-11 11:50:30 -0400 | [diff] [blame] | 1498 | int isize_diff; /* How much do we need to grow i_extra_isize */ |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1499 | int no_expand; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1500 | |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1501 | if (ext4_write_trylock_xattr(inode, &no_expand) == 0) |
| 1502 | return 0; |
| 1503 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1504 | retry: |
Jan Kara | d014119 | 2016-08-11 11:50:30 -0400 | [diff] [blame] | 1505 | isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize; |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1506 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) |
| 1507 | goto out; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1508 | |
| 1509 | header = IHDR(inode, raw_inode); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1510 | |
| 1511 | /* |
| 1512 | * Check if enough free space is available in the inode to shift the |
| 1513 | * entries ahead by new_extra_isize. |
| 1514 | */ |
| 1515 | |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1516 | base = IFIRST(header); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1517 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
| 1518 | min_offs = end - base; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1519 | total_ino = sizeof(struct ext4_xattr_ibody_header); |
| 1520 | |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 1521 | error = xattr_check_inode(inode, header, end); |
| 1522 | if (error) |
| 1523 | goto cleanup; |
| 1524 | |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1525 | ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1526 | if (ifree >= isize_diff) |
| 1527 | goto shift; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1528 | |
| 1529 | /* |
| 1530 | * Enough free space isn't available in the inode, check if |
| 1531 | * EA block can hold new_extra_isize bytes. |
| 1532 | */ |
| 1533 | if (EXT4_I(inode)->i_file_acl) { |
| 1534 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
| 1535 | error = -EIO; |
| 1536 | if (!bh) |
| 1537 | goto cleanup; |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 1538 | if (ext4_xattr_check_block(inode, bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1539 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 1540 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1541 | error = -EFSCORRUPTED; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1542 | goto cleanup; |
| 1543 | } |
| 1544 | base = BHDR(bh); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1545 | end = bh->b_data + bh->b_size; |
| 1546 | min_offs = end - base; |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1547 | bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base, |
| 1548 | NULL); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1549 | if (bfree + ifree < isize_diff) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1550 | if (!tried_min_extra_isize && s_min_extra_isize) { |
| 1551 | tried_min_extra_isize++; |
| 1552 | new_extra_isize = s_min_extra_isize; |
| 1553 | brelse(bh); |
| 1554 | goto retry; |
| 1555 | } |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1556 | error = -ENOSPC; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1557 | goto cleanup; |
| 1558 | } |
| 1559 | } else { |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1560 | bfree = inode->i_sb->s_blocksize; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1561 | } |
| 1562 | |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1563 | error = ext4_xattr_make_inode_space(handle, inode, raw_inode, |
| 1564 | isize_diff, ifree, bfree, |
| 1565 | &total_ino); |
| 1566 | if (error) { |
| 1567 | if (error == -ENOSPC && !tried_min_extra_isize && |
| 1568 | s_min_extra_isize) { |
| 1569 | tried_min_extra_isize++; |
| 1570 | new_extra_isize = s_min_extra_isize; |
| 1571 | brelse(bh); |
| 1572 | goto retry; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1573 | } |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1574 | goto cleanup; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1575 | } |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1576 | shift: |
| 1577 | /* Adjust the offsets and shift the remaining entries ahead */ |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1578 | ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1579 | - new_extra_isize, (void *)raw_inode + |
| 1580 | EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize, |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 1581 | (void *)header, total_ino); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1582 | EXT4_I(inode)->i_extra_isize = new_extra_isize; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1583 | brelse(bh); |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1584 | out: |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1585 | ext4_write_unlock_xattr(inode, &no_expand); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1586 | return 0; |
| 1587 | |
| 1588 | cleanup: |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1589 | brelse(bh); |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1590 | /* |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1591 | * Inode size expansion failed; don't try again |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1592 | */ |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1593 | no_expand = 1; |
| 1594 | ext4_write_unlock_xattr(inode, &no_expand); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1595 | return error; |
| 1596 | } |
| 1597 | |
| 1598 | |
| 1599 | |
| 1600 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1601 | * ext4_xattr_delete_inode() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1602 | * |
| 1603 | * Free extended attribute resources associated with this inode. This |
| 1604 | * is called immediately before an inode is freed. We have exclusive |
| 1605 | * access to the inode. |
| 1606 | */ |
| 1607 | void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1608 | ext4_xattr_delete_inode(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1609 | { |
| 1610 | struct buffer_head *bh = NULL; |
| 1611 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1612 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1613 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1614 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1615 | if (!bh) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1616 | EXT4_ERROR_INODE(inode, "block %llu read error", |
| 1617 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1618 | goto cleanup; |
| 1619 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1620 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1621 | BHDR(bh)->h_blocks != cpu_to_le32(1)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1622 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 1623 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1624 | goto cleanup; |
| 1625 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1626 | ext4_xattr_release_block(handle, inode, bh); |
| 1627 | EXT4_I(inode)->i_file_acl = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1628 | |
| 1629 | cleanup: |
| 1630 | brelse(bh); |
| 1631 | } |
| 1632 | |
| 1633 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1634 | * ext4_xattr_cache_insert() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1635 | * |
| 1636 | * Create a new entry in the extended attribute cache, and insert |
| 1637 | * it unless such an entry is already in the cache. |
| 1638 | * |
| 1639 | * Returns 0, or a negative error number on failure. |
| 1640 | */ |
| 1641 | static void |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1642 | ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1643 | { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1644 | struct ext4_xattr_header *header = BHDR(bh); |
| 1645 | __u32 hash = le32_to_cpu(header->h_hash); |
| 1646 | int reusable = le32_to_cpu(header->h_refcount) < |
| 1647 | EXT4_XATTR_REFCOUNT_MAX; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1648 | int error; |
| 1649 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1650 | error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash, |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1651 | bh->b_blocknr, reusable); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1652 | if (error) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1653 | if (error == -EBUSY) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1654 | ea_bdebug(bh, "already in cache"); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1655 | } else |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1656 | ea_bdebug(bh, "inserting [%x]", (int)hash); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1660 | * ext4_xattr_cmp() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1661 | * |
| 1662 | * Compare two extended attribute blocks for equality. |
| 1663 | * |
| 1664 | * Returns 0 if the blocks are equal, 1 if they differ, and |
| 1665 | * a negative error number on errors. |
| 1666 | */ |
| 1667 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1668 | ext4_xattr_cmp(struct ext4_xattr_header *header1, |
| 1669 | struct ext4_xattr_header *header2) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1670 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1671 | struct ext4_xattr_entry *entry1, *entry2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1672 | |
| 1673 | entry1 = ENTRY(header1+1); |
| 1674 | entry2 = ENTRY(header2+1); |
| 1675 | while (!IS_LAST_ENTRY(entry1)) { |
| 1676 | if (IS_LAST_ENTRY(entry2)) |
| 1677 | return 1; |
| 1678 | if (entry1->e_hash != entry2->e_hash || |
| 1679 | entry1->e_name_index != entry2->e_name_index || |
| 1680 | entry1->e_name_len != entry2->e_name_len || |
| 1681 | entry1->e_value_size != entry2->e_value_size || |
| 1682 | memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) |
| 1683 | return 1; |
| 1684 | if (entry1->e_value_block != 0 || entry2->e_value_block != 0) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1685 | return -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1686 | if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), |
| 1687 | (char *)header2 + le16_to_cpu(entry2->e_value_offs), |
| 1688 | le32_to_cpu(entry1->e_value_size))) |
| 1689 | return 1; |
| 1690 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1691 | entry1 = EXT4_XATTR_NEXT(entry1); |
| 1692 | entry2 = EXT4_XATTR_NEXT(entry2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1693 | } |
| 1694 | if (!IS_LAST_ENTRY(entry2)) |
| 1695 | return 1; |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1700 | * ext4_xattr_cache_find() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1701 | * |
| 1702 | * Find an identical extended attribute block. |
| 1703 | * |
| 1704 | * Returns a pointer to the block found, or NULL if such a block was |
| 1705 | * not found or an error occurred. |
| 1706 | */ |
| 1707 | static struct buffer_head * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1708 | ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header, |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1709 | struct mb_cache_entry **pce) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1710 | { |
| 1711 | __u32 hash = le32_to_cpu(header->h_hash); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1712 | struct mb_cache_entry *ce; |
| 1713 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1714 | |
| 1715 | if (!header->h_hash) |
| 1716 | return NULL; /* never share */ |
| 1717 | ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1718 | ce = mb_cache_entry_find_first(ext4_mb_cache, hash); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1719 | while (ce) { |
| 1720 | struct buffer_head *bh; |
| 1721 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1722 | bh = sb_bread(inode->i_sb, ce->e_block); |
| 1723 | if (!bh) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1724 | EXT4_ERROR_INODE(inode, "block %lu read error", |
| 1725 | (unsigned long) ce->e_block); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1726 | } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1727 | *pce = ce; |
| 1728 | return bh; |
| 1729 | } |
| 1730 | brelse(bh); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1731 | ce = mb_cache_entry_find_next(ext4_mb_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1732 | } |
| 1733 | return NULL; |
| 1734 | } |
| 1735 | |
| 1736 | #define NAME_HASH_SHIFT 5 |
| 1737 | #define VALUE_HASH_SHIFT 16 |
| 1738 | |
| 1739 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1740 | * ext4_xattr_hash_entry() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1741 | * |
| 1742 | * Compute the hash of an extended attribute. |
| 1743 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1744 | static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header, |
| 1745 | struct ext4_xattr_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1746 | { |
| 1747 | __u32 hash = 0; |
| 1748 | char *name = entry->e_name; |
| 1749 | int n; |
| 1750 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1751 | for (n = 0; n < entry->e_name_len; n++) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1752 | hash = (hash << NAME_HASH_SHIFT) ^ |
| 1753 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ |
| 1754 | *name++; |
| 1755 | } |
| 1756 | |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 1757 | if (entry->e_value_size != 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1758 | __le32 *value = (__le32 *)((char *)header + |
| 1759 | le16_to_cpu(entry->e_value_offs)); |
| 1760 | for (n = (le32_to_cpu(entry->e_value_size) + |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1761 | EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1762 | hash = (hash << VALUE_HASH_SHIFT) ^ |
| 1763 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ |
| 1764 | le32_to_cpu(*value++); |
| 1765 | } |
| 1766 | } |
| 1767 | entry->e_hash = cpu_to_le32(hash); |
| 1768 | } |
| 1769 | |
| 1770 | #undef NAME_HASH_SHIFT |
| 1771 | #undef VALUE_HASH_SHIFT |
| 1772 | |
| 1773 | #define BLOCK_HASH_SHIFT 16 |
| 1774 | |
| 1775 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1776 | * ext4_xattr_rehash() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1777 | * |
| 1778 | * Re-compute the extended attribute hash value after an entry has changed. |
| 1779 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1780 | static void ext4_xattr_rehash(struct ext4_xattr_header *header, |
| 1781 | struct ext4_xattr_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1782 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1783 | struct ext4_xattr_entry *here; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1784 | __u32 hash = 0; |
| 1785 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1786 | ext4_xattr_hash_entry(header, entry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1787 | here = ENTRY(header+1); |
| 1788 | while (!IS_LAST_ENTRY(here)) { |
| 1789 | if (!here->e_hash) { |
| 1790 | /* Block is not shared if an entry's hash value == 0 */ |
| 1791 | hash = 0; |
| 1792 | break; |
| 1793 | } |
| 1794 | hash = (hash << BLOCK_HASH_SHIFT) ^ |
| 1795 | (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ |
| 1796 | le32_to_cpu(here->e_hash); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1797 | here = EXT4_XATTR_NEXT(here); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1798 | } |
| 1799 | header->h_hash = cpu_to_le32(hash); |
| 1800 | } |
| 1801 | |
| 1802 | #undef BLOCK_HASH_SHIFT |
| 1803 | |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 1804 | #define HASH_BUCKET_BITS 10 |
| 1805 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1806 | struct mb_cache * |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1807 | ext4_xattr_create_cache(void) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1808 | { |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1809 | return mb_cache_create(HASH_BUCKET_BITS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1812 | void ext4_xattr_destroy_cache(struct mb_cache *cache) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1813 | { |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 1814 | if (cache) |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1815 | mb_cache_destroy(cache); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1816 | } |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 1817 | |