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