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