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 | |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 75 | static void ext4_xattr_block_cache_insert(struct mb_cache *, |
| 76 | struct buffer_head *); |
| 77 | static struct buffer_head * |
| 78 | ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *, |
| 79 | struct mb_cache_entry **); |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 80 | static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value, |
| 81 | size_t value_count); |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 82 | static void ext4_xattr_rehash(struct ext4_xattr_header *); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 83 | |
Eric Biggers | d600618 | 2017-04-29 23:47:50 -0400 | [diff] [blame] | 84 | static const struct xattr_handler * const ext4_xattr_handler_map[] = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 85 | [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 86 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 87 | [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler, |
| 88 | [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 89 | #endif |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 90 | [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 91 | #ifdef CONFIG_EXT4_FS_SECURITY |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 92 | [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 93 | #endif |
| 94 | }; |
| 95 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 96 | const struct xattr_handler *ext4_xattr_handlers[] = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 97 | &ext4_xattr_user_handler, |
| 98 | &ext4_xattr_trusted_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 99 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 100 | &posix_acl_access_xattr_handler, |
| 101 | &posix_acl_default_xattr_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 102 | #endif |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 103 | #ifdef CONFIG_EXT4_FS_SECURITY |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 104 | &ext4_xattr_security_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 105 | #endif |
| 106 | NULL |
| 107 | }; |
| 108 | |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 109 | #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \ |
| 110 | inode->i_sb->s_fs_info)->s_ea_block_cache) |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 111 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 112 | #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \ |
| 113 | inode->i_sb->s_fs_info)->s_ea_inode_cache) |
| 114 | |
Tahsin Erdogan | 30a7eb9 | 2017-06-22 11:42:09 -0400 | [diff] [blame] | 115 | static int |
| 116 | ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, |
| 117 | struct inode *inode); |
| 118 | |
Tahsin Erdogan | 33d201e | 2017-06-21 21:17:10 -0400 | [diff] [blame] | 119 | #ifdef CONFIG_LOCKDEP |
| 120 | void ext4_xattr_inode_set_class(struct inode *ea_inode) |
| 121 | { |
| 122 | lockdep_set_subclass(&ea_inode->i_rwsem, 1); |
| 123 | } |
| 124 | #endif |
| 125 | |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 126 | static __le32 ext4_xattr_block_csum(struct inode *inode, |
| 127 | sector_t block_nr, |
| 128 | struct ext4_xattr_header *hdr) |
| 129 | { |
| 130 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 131 | __u32 csum; |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 132 | __le64 dsk_block_nr = cpu_to_le64(block_nr); |
Daeho Jeong | b47820e | 2016-07-03 17:51:39 -0400 | [diff] [blame] | 133 | __u32 dummy_csum = 0; |
| 134 | int offset = offsetof(struct ext4_xattr_header, h_checksum); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 135 | |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 136 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr, |
| 137 | sizeof(dsk_block_nr)); |
Daeho Jeong | b47820e | 2016-07-03 17:51:39 -0400 | [diff] [blame] | 138 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset); |
| 139 | csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum)); |
| 140 | offset += sizeof(dummy_csum); |
| 141 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset, |
| 142 | EXT4_BLOCK_SIZE(inode->i_sb) - offset); |
Tao Ma | 41eb70d | 2012-07-09 16:29:27 -0400 | [diff] [blame] | 143 | |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 144 | return cpu_to_le32(csum); |
| 145 | } |
| 146 | |
| 147 | static int ext4_xattr_block_csum_verify(struct inode *inode, |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 148 | struct buffer_head *bh) |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 149 | { |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 150 | struct ext4_xattr_header *hdr = BHDR(bh); |
| 151 | int ret = 1; |
| 152 | |
| 153 | if (ext4_has_metadata_csum(inode->i_sb)) { |
| 154 | lock_buffer(bh); |
| 155 | ret = (hdr->h_checksum == ext4_xattr_block_csum(inode, |
| 156 | bh->b_blocknr, hdr)); |
| 157 | unlock_buffer(bh); |
| 158 | } |
| 159 | return ret; |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void ext4_xattr_block_csum_set(struct inode *inode, |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 163 | struct buffer_head *bh) |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 164 | { |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 165 | if (ext4_has_metadata_csum(inode->i_sb)) |
| 166 | BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode, |
| 167 | bh->b_blocknr, BHDR(bh)); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 168 | } |
| 169 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 170 | static inline const struct xattr_handler * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 171 | ext4_xattr_handler(int name_index) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 172 | { |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 173 | const struct xattr_handler *handler = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 174 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 175 | if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map)) |
| 176 | handler = ext4_xattr_handler_map[name_index]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 177 | return handler; |
| 178 | } |
| 179 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 180 | static int |
Eric Biggers | 2c4f992 | 2017-04-29 23:56:52 -0400 | [diff] [blame] | 181 | ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end, |
| 182 | void *value_start) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 183 | { |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 184 | struct ext4_xattr_entry *e = entry; |
| 185 | |
Eric Biggers | d7614cc | 2016-12-01 14:57:29 -0500 | [diff] [blame] | 186 | /* Find the end of the names list */ |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 187 | while (!IS_LAST_ENTRY(e)) { |
| 188 | struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 189 | if ((void *)next >= end) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 190 | return -EFSCORRUPTED; |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 191 | e = next; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 192 | } |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 193 | |
Eric Biggers | d7614cc | 2016-12-01 14:57:29 -0500 | [diff] [blame] | 194 | /* Check the values */ |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 195 | while (!IS_LAST_ENTRY(entry)) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 196 | if (entry->e_value_size != 0 && |
| 197 | entry->e_value_inum == 0) { |
Eric Biggers | d7614cc | 2016-12-01 14:57:29 -0500 | [diff] [blame] | 198 | u16 offs = le16_to_cpu(entry->e_value_offs); |
| 199 | u32 size = le32_to_cpu(entry->e_value_size); |
| 200 | void *value; |
| 201 | |
| 202 | /* |
| 203 | * The value cannot overlap the names, and the value |
| 204 | * with padding cannot extend beyond 'end'. Check both |
| 205 | * the padded and unpadded sizes, since the size may |
| 206 | * overflow to 0 when adding padding. |
| 207 | */ |
| 208 | if (offs > end - value_start) |
| 209 | return -EFSCORRUPTED; |
| 210 | value = value_start + offs; |
| 211 | if (value < (void *)e + sizeof(u32) || |
| 212 | size > end - value || |
| 213 | EXT4_XATTR_SIZE(size) > end - value) |
| 214 | return -EFSCORRUPTED; |
| 215 | } |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 216 | entry = EXT4_XATTR_NEXT(entry); |
| 217 | } |
| 218 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static inline int |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 223 | ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 224 | { |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 225 | int error; |
| 226 | |
| 227 | if (buffer_verified(bh)) |
| 228 | return 0; |
| 229 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 230 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 231 | BHDR(bh)->h_blocks != cpu_to_le32(1)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 232 | return -EFSCORRUPTED; |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 233 | if (!ext4_xattr_block_csum_verify(inode, bh)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 234 | return -EFSBADCRC; |
Eric Biggers | 2c4f992 | 2017-04-29 23:56:52 -0400 | [diff] [blame] | 235 | error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size, |
| 236 | bh->b_data); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 237 | if (!error) |
| 238 | set_buffer_verified(bh); |
| 239 | return error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 242 | static int |
| 243 | __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header, |
| 244 | void *end, const char *function, unsigned int line) |
| 245 | { |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 246 | int error = -EFSCORRUPTED; |
| 247 | |
Eric Biggers | 290ab23 | 2016-12-01 14:51:58 -0500 | [diff] [blame] | 248 | if (end - (void *)header < sizeof(*header) + sizeof(u32) || |
Eric Biggers | 1996250 | 2016-10-15 09:39:31 -0400 | [diff] [blame] | 249 | (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC))) |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 250 | goto errout; |
Eric Biggers | 2c4f992 | 2017-04-29 23:56:52 -0400 | [diff] [blame] | 251 | error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header)); |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 252 | errout: |
| 253 | if (error) |
| 254 | __ext4_error_inode(inode, function, line, 0, |
| 255 | "corrupted in-inode xattr"); |
| 256 | return error; |
| 257 | } |
| 258 | |
| 259 | #define xattr_check_inode(inode, header, end) \ |
| 260 | __xattr_check_inode((inode), (header), (end), __func__, __LINE__) |
| 261 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 262 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 263 | ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index, |
Eric Biggers | 6ba644b | 2017-04-30 00:01:02 -0400 | [diff] [blame] | 264 | const char *name, int sorted) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 265 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 266 | struct ext4_xattr_entry *entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 267 | size_t name_len; |
| 268 | int cmp = 1; |
| 269 | |
| 270 | if (name == NULL) |
| 271 | return -EINVAL; |
| 272 | name_len = strlen(name); |
| 273 | entry = *pentry; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 274 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 275 | cmp = name_index - entry->e_name_index; |
| 276 | if (!cmp) |
| 277 | cmp = name_len - entry->e_name_len; |
| 278 | if (!cmp) |
| 279 | cmp = memcmp(name, entry->e_name, name_len); |
| 280 | if (cmp <= 0 && (sorted || cmp == 0)) |
| 281 | break; |
| 282 | } |
| 283 | *pentry = entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 284 | return cmp ? -ENODATA : 0; |
| 285 | } |
| 286 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 287 | static u32 |
| 288 | ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size) |
| 289 | { |
| 290 | return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size); |
| 291 | } |
| 292 | |
| 293 | static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode) |
| 294 | { |
| 295 | return ((u64)ea_inode->i_ctime.tv_sec << 32) | |
| 296 | ((u32)ea_inode->i_version); |
| 297 | } |
| 298 | |
| 299 | static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count) |
| 300 | { |
| 301 | ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32); |
| 302 | ea_inode->i_version = (u32)ref_count; |
| 303 | } |
| 304 | |
| 305 | static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode) |
| 306 | { |
| 307 | return (u32)ea_inode->i_atime.tv_sec; |
| 308 | } |
| 309 | |
| 310 | static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash) |
| 311 | { |
| 312 | ea_inode->i_atime.tv_sec = hash; |
| 313 | } |
| 314 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 315 | /* |
| 316 | * Read the EA value from an inode. |
| 317 | */ |
Tahsin Erdogan | 90966693 | 2017-06-21 21:57:36 -0400 | [diff] [blame] | 318 | static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 319 | { |
Tahsin Erdogan | 9699d4f | 2017-08-06 00:07:01 -0400 | [diff] [blame] | 320 | int blocksize = 1 << ea_inode->i_blkbits; |
| 321 | int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits; |
| 322 | int tail_size = (size % blocksize) ?: blocksize; |
| 323 | struct buffer_head *bhs_inline[8]; |
| 324 | struct buffer_head **bhs = bhs_inline; |
| 325 | int i, ret; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 326 | |
Tahsin Erdogan | 9699d4f | 2017-08-06 00:07:01 -0400 | [diff] [blame] | 327 | if (bh_count > ARRAY_SIZE(bhs_inline)) { |
| 328 | bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS); |
| 329 | if (!bhs) |
| 330 | return -ENOMEM; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 331 | } |
Tahsin Erdogan | 9699d4f | 2017-08-06 00:07:01 -0400 | [diff] [blame] | 332 | |
| 333 | ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count, |
| 334 | true /* wait */, bhs); |
| 335 | if (ret) |
| 336 | goto free_bhs; |
| 337 | |
| 338 | for (i = 0; i < bh_count; i++) { |
| 339 | /* There shouldn't be any holes in ea_inode. */ |
| 340 | if (!bhs[i]) { |
| 341 | ret = -EFSCORRUPTED; |
| 342 | goto put_bhs; |
| 343 | } |
| 344 | memcpy((char *)buf + blocksize * i, bhs[i]->b_data, |
| 345 | i < bh_count - 1 ? blocksize : tail_size); |
| 346 | } |
| 347 | ret = 0; |
| 348 | put_bhs: |
| 349 | for (i = 0; i < bh_count; i++) |
| 350 | brelse(bhs[i]); |
| 351 | free_bhs: |
| 352 | if (bhs != bhs_inline) |
| 353 | kfree(bhs); |
| 354 | return ret; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 355 | } |
| 356 | |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 357 | #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec) |
| 358 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 359 | static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 360 | u32 ea_inode_hash, struct inode **ea_inode) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 361 | { |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 362 | struct inode *inode; |
| 363 | int err; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 364 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 365 | inode = ext4_iget(parent->i_sb, ea_ino); |
| 366 | if (IS_ERR(inode)) { |
| 367 | err = PTR_ERR(inode); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 368 | ext4_error(parent->i_sb, |
| 369 | "error while reading EA inode %lu err=%d", ea_ino, |
| 370 | err); |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 371 | return err; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 374 | if (is_bad_inode(inode)) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 375 | ext4_error(parent->i_sb, |
| 376 | "error while reading EA inode %lu is_bad_inode", |
| 377 | ea_ino); |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 378 | err = -EIO; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 379 | goto error; |
| 380 | } |
| 381 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 382 | if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 383 | ext4_error(parent->i_sb, |
| 384 | "EA inode %lu does not have EXT4_EA_INODE_FL flag", |
| 385 | ea_ino); |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 386 | err = -EINVAL; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 387 | goto error; |
| 388 | } |
| 389 | |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 390 | ext4_xattr_inode_set_class(inode); |
| 391 | |
| 392 | /* |
| 393 | * Check whether this is an old Lustre-style xattr inode. Lustre |
| 394 | * implementation does not have hash validation, rather it has a |
| 395 | * backpointer from ea_inode to the parent inode. |
| 396 | */ |
| 397 | if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) && |
| 398 | EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino && |
| 399 | inode->i_generation == parent->i_generation) { |
| 400 | ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE); |
| 401 | ext4_xattr_inode_set_ref(inode, 1); |
| 402 | } else { |
| 403 | inode_lock(inode); |
| 404 | inode->i_flags |= S_NOQUOTA; |
| 405 | inode_unlock(inode); |
| 406 | } |
| 407 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 408 | *ea_inode = inode; |
| 409 | return 0; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 410 | error: |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 411 | iput(inode); |
| 412 | return err; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 413 | } |
| 414 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 415 | static int |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 416 | ext4_xattr_inode_verify_hashes(struct inode *ea_inode, |
| 417 | struct ext4_xattr_entry *entry, void *buffer, |
| 418 | size_t size) |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 419 | { |
| 420 | u32 hash; |
| 421 | |
| 422 | /* Verify stored hash matches calculated hash. */ |
| 423 | hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size); |
| 424 | if (hash != ext4_xattr_inode_get_hash(ea_inode)) |
| 425 | return -EFSCORRUPTED; |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 426 | |
| 427 | if (entry) { |
| 428 | __le32 e_hash, tmp_data; |
| 429 | |
| 430 | /* Verify entry hash. */ |
| 431 | tmp_data = cpu_to_le32(hash); |
| 432 | e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len, |
| 433 | &tmp_data, 1); |
| 434 | if (e_hash != entry->e_hash) |
| 435 | return -EFSCORRUPTED; |
| 436 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 440 | /* |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 441 | * Read xattr value from the EA inode. |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 442 | */ |
| 443 | static int |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 444 | ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry, |
| 445 | void *buffer, size_t size) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 446 | { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 447 | struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode); |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 448 | struct inode *ea_inode; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 449 | int err; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 450 | |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 451 | err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum), |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 452 | le32_to_cpu(entry->e_hash), &ea_inode); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 453 | if (err) { |
| 454 | ea_inode = NULL; |
| 455 | goto out; |
| 456 | } |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 457 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 458 | if (i_size_read(ea_inode) != size) { |
| 459 | ext4_warning_inode(ea_inode, |
| 460 | "ea_inode file size=%llu entry size=%zu", |
| 461 | i_size_read(ea_inode), size); |
| 462 | err = -EFSCORRUPTED; |
| 463 | goto out; |
| 464 | } |
| 465 | |
| 466 | err = ext4_xattr_inode_read(ea_inode, buffer, size); |
| 467 | if (err) |
| 468 | goto out; |
| 469 | |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 470 | if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) { |
| 471 | err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer, |
| 472 | size); |
| 473 | if (err) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 474 | ext4_warning_inode(ea_inode, |
| 475 | "EA inode hash validation failed"); |
| 476 | goto out; |
| 477 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 478 | |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 479 | if (ea_inode_cache) |
| 480 | mb_cache_entry_create(ea_inode_cache, GFP_NOFS, |
| 481 | ext4_xattr_inode_get_hash(ea_inode), |
| 482 | ea_inode->i_ino, true /* reusable */); |
| 483 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 484 | out: |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 485 | iput(ea_inode); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 486 | return err; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 487 | } |
| 488 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 489 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 490 | 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] | 491 | void *buffer, size_t buffer_size) |
| 492 | { |
| 493 | struct buffer_head *bh = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 494 | struct ext4_xattr_entry *entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 495 | size_t size; |
| 496 | int error; |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 497 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 498 | |
| 499 | ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", |
| 500 | name_index, name, buffer, (long)buffer_size); |
| 501 | |
| 502 | error = -ENODATA; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 503 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 504 | goto cleanup; |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 505 | ea_idebug(inode, "reading block %llu", |
| 506 | (unsigned long long)EXT4_I(inode)->i_file_acl); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 507 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 508 | if (!bh) |
| 509 | goto cleanup; |
| 510 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 511 | 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] | 512 | if (ext4_xattr_check_block(inode, bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 513 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 514 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 515 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 516 | goto cleanup; |
| 517 | } |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 518 | ext4_xattr_block_cache_insert(ea_block_cache, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 519 | entry = BFIRST(bh); |
Eric Biggers | 6ba644b | 2017-04-30 00:01:02 -0400 | [diff] [blame] | 520 | error = ext4_xattr_find_entry(&entry, name_index, name, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 521 | if (error) |
| 522 | goto cleanup; |
| 523 | size = le32_to_cpu(entry->e_value_size); |
| 524 | if (buffer) { |
| 525 | error = -ERANGE; |
| 526 | if (size > buffer_size) |
| 527 | goto cleanup; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 528 | if (entry->e_value_inum) { |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 529 | error = ext4_xattr_inode_get(inode, entry, buffer, |
| 530 | size); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 531 | if (error) |
| 532 | goto cleanup; |
| 533 | } else { |
| 534 | memcpy(buffer, bh->b_data + |
| 535 | le16_to_cpu(entry->e_value_offs), size); |
| 536 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 537 | } |
| 538 | error = size; |
| 539 | |
| 540 | cleanup: |
| 541 | brelse(bh); |
| 542 | return error; |
| 543 | } |
| 544 | |
Tao Ma | 879b382 | 2012-12-05 10:28:46 -0500 | [diff] [blame] | 545 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 546 | 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] | 547 | void *buffer, size_t buffer_size) |
| 548 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 549 | struct ext4_xattr_ibody_header *header; |
| 550 | struct ext4_xattr_entry *entry; |
| 551 | struct ext4_inode *raw_inode; |
| 552 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 553 | size_t size; |
| 554 | void *end; |
| 555 | int error; |
| 556 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 557 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 558 | return -ENODATA; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 559 | error = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 560 | if (error) |
| 561 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 562 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 563 | header = IHDR(inode, raw_inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 564 | 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] | 565 | error = xattr_check_inode(inode, header, end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 566 | if (error) |
| 567 | goto cleanup; |
Eric Biggers | 6ba644b | 2017-04-30 00:01:02 -0400 | [diff] [blame] | 568 | entry = IFIRST(header); |
| 569 | error = ext4_xattr_find_entry(&entry, name_index, name, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 570 | if (error) |
| 571 | goto cleanup; |
| 572 | size = le32_to_cpu(entry->e_value_size); |
| 573 | if (buffer) { |
| 574 | error = -ERANGE; |
| 575 | if (size > buffer_size) |
| 576 | goto cleanup; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 577 | if (entry->e_value_inum) { |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 578 | error = ext4_xattr_inode_get(inode, entry, buffer, |
| 579 | size); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 580 | if (error) |
| 581 | goto cleanup; |
| 582 | } else { |
| 583 | memcpy(buffer, (void *)IFIRST(header) + |
| 584 | le16_to_cpu(entry->e_value_offs), size); |
| 585 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 586 | } |
| 587 | error = size; |
| 588 | |
| 589 | cleanup: |
| 590 | brelse(iloc.bh); |
| 591 | return error; |
| 592 | } |
| 593 | |
| 594 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 595 | * ext4_xattr_get() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 596 | * |
| 597 | * Copy an extended attribute into the buffer |
| 598 | * provided, or compute the buffer size required. |
| 599 | * Buffer is NULL to compute the size of the buffer required. |
| 600 | * |
| 601 | * Returns a negative error number on failure, or the number of bytes |
| 602 | * used / required on success. |
| 603 | */ |
| 604 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 605 | ext4_xattr_get(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 606 | void *buffer, size_t buffer_size) |
| 607 | { |
| 608 | int error; |
| 609 | |
Theodore Ts'o | 0db1ff2 | 2017-02-05 01:28:48 -0500 | [diff] [blame] | 610 | if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) |
| 611 | return -EIO; |
| 612 | |
Zhang Zhen | 230b8c1a | 2014-05-12 09:57:59 -0400 | [diff] [blame] | 613 | if (strlen(name) > 255) |
| 614 | return -ERANGE; |
| 615 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 616 | down_read(&EXT4_I(inode)->xattr_sem); |
| 617 | error = ext4_xattr_ibody_get(inode, name_index, name, buffer, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 618 | buffer_size); |
| 619 | if (error == -ENODATA) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 620 | error = ext4_xattr_block_get(inode, name_index, name, buffer, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 621 | buffer_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 622 | up_read(&EXT4_I(inode)->xattr_sem); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 623 | return error; |
| 624 | } |
| 625 | |
| 626 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 627 | ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 628 | char *buffer, size_t buffer_size) |
| 629 | { |
| 630 | size_t rest = buffer_size; |
| 631 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 632 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 633 | const struct xattr_handler *handler = |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 634 | ext4_xattr_handler(entry->e_name_index); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 635 | |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 636 | if (handler && (!handler->list || handler->list(dentry))) { |
| 637 | const char *prefix = handler->prefix ?: handler->name; |
| 638 | size_t prefix_len = strlen(prefix); |
| 639 | size_t size = prefix_len + entry->e_name_len + 1; |
| 640 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 641 | if (buffer) { |
| 642 | if (size > rest) |
| 643 | return -ERANGE; |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 644 | memcpy(buffer, prefix, prefix_len); |
| 645 | buffer += prefix_len; |
| 646 | memcpy(buffer, entry->e_name, entry->e_name_len); |
| 647 | buffer += entry->e_name_len; |
| 648 | *buffer++ = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 649 | } |
| 650 | rest -= size; |
| 651 | } |
| 652 | } |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 653 | return buffer_size - rest; /* total size */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 657 | 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] | 658 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 659 | struct inode *inode = d_inode(dentry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 660 | struct buffer_head *bh = NULL; |
| 661 | int error; |
| 662 | |
| 663 | ea_idebug(inode, "buffer=%p, buffer_size=%ld", |
| 664 | buffer, (long)buffer_size); |
| 665 | |
| 666 | error = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 667 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 668 | goto cleanup; |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 669 | ea_idebug(inode, "reading block %llu", |
| 670 | (unsigned long long)EXT4_I(inode)->i_file_acl); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 671 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 672 | error = -EIO; |
| 673 | if (!bh) |
| 674 | goto cleanup; |
| 675 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 676 | 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] | 677 | if (ext4_xattr_check_block(inode, bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 678 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 679 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 680 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 681 | goto cleanup; |
| 682 | } |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 683 | ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh); |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 684 | error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 685 | |
| 686 | cleanup: |
| 687 | brelse(bh); |
| 688 | |
| 689 | return error; |
| 690 | } |
| 691 | |
| 692 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 693 | 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] | 694 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 695 | struct inode *inode = d_inode(dentry); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 696 | struct ext4_xattr_ibody_header *header; |
| 697 | struct ext4_inode *raw_inode; |
| 698 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 699 | void *end; |
| 700 | int error; |
| 701 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 702 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 703 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 704 | error = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 705 | if (error) |
| 706 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 707 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 708 | header = IHDR(inode, raw_inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 709 | 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] | 710 | error = xattr_check_inode(inode, header, end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 711 | if (error) |
| 712 | goto cleanup; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 713 | error = ext4_xattr_list_entries(dentry, IFIRST(header), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 714 | buffer, buffer_size); |
| 715 | |
| 716 | cleanup: |
| 717 | brelse(iloc.bh); |
| 718 | return error; |
| 719 | } |
| 720 | |
| 721 | /* |
Eric Biggers | ba7ea1d | 2017-04-29 23:53:17 -0400 | [diff] [blame] | 722 | * Inode operation listxattr() |
| 723 | * |
| 724 | * d_inode(dentry)->i_rwsem: don't care |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 725 | * |
| 726 | * Copy a list of attribute names into the buffer |
| 727 | * provided, or compute the buffer size required. |
| 728 | * Buffer is NULL to compute the size of the buffer required. |
| 729 | * |
| 730 | * Returns a negative error number on failure, or the number of bytes |
| 731 | * used / required on success. |
| 732 | */ |
Eric Biggers | ba7ea1d | 2017-04-29 23:53:17 -0400 | [diff] [blame] | 733 | ssize_t |
| 734 | ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 735 | { |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 736 | int ret, ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 737 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 738 | down_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 739 | ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size); |
| 740 | if (ret < 0) |
| 741 | goto errout; |
| 742 | if (buffer) { |
| 743 | buffer += ret; |
| 744 | buffer_size -= ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 745 | } |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 746 | ret = ext4_xattr_block_list(dentry, buffer, buffer_size); |
| 747 | if (ret < 0) |
| 748 | goto errout; |
| 749 | ret += ret2; |
| 750 | errout: |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 751 | up_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 752 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 756 | * 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] | 757 | * not set, set it. |
| 758 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 759 | static void ext4_xattr_update_super_block(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 760 | struct super_block *sb) |
| 761 | { |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 762 | if (ext4_has_feature_xattr(sb)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 763 | return; |
| 764 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 765 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 766 | 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] | 767 | ext4_set_feature_xattr(sb); |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 768 | ext4_handle_dirty_super(handle, sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 769 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Tahsin Erdogan | 7a9ca53 | 2017-06-22 11:46:48 -0400 | [diff] [blame] | 772 | int ext4_get_inode_usage(struct inode *inode, qsize_t *usage) |
| 773 | { |
| 774 | struct ext4_iloc iloc = { .bh = NULL }; |
| 775 | struct buffer_head *bh = NULL; |
| 776 | struct ext4_inode *raw_inode; |
| 777 | struct ext4_xattr_ibody_header *header; |
| 778 | struct ext4_xattr_entry *entry; |
| 779 | qsize_t ea_inode_refs = 0; |
| 780 | void *end; |
| 781 | int ret; |
| 782 | |
| 783 | lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem); |
| 784 | |
| 785 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
| 786 | ret = ext4_get_inode_loc(inode, &iloc); |
| 787 | if (ret) |
| 788 | goto out; |
| 789 | raw_inode = ext4_raw_inode(&iloc); |
| 790 | header = IHDR(inode, raw_inode); |
| 791 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
| 792 | ret = xattr_check_inode(inode, header, end); |
| 793 | if (ret) |
| 794 | goto out; |
| 795 | |
| 796 | for (entry = IFIRST(header); !IS_LAST_ENTRY(entry); |
| 797 | entry = EXT4_XATTR_NEXT(entry)) |
| 798 | if (entry->e_value_inum) |
| 799 | ea_inode_refs++; |
| 800 | } |
| 801 | |
| 802 | if (EXT4_I(inode)->i_file_acl) { |
| 803 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
| 804 | if (!bh) { |
| 805 | ret = -EIO; |
| 806 | goto out; |
| 807 | } |
| 808 | |
| 809 | if (ext4_xattr_check_block(inode, bh)) { |
| 810 | ret = -EFSCORRUPTED; |
| 811 | goto out; |
| 812 | } |
| 813 | |
| 814 | for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry); |
| 815 | entry = EXT4_XATTR_NEXT(entry)) |
| 816 | if (entry->e_value_inum) |
| 817 | ea_inode_refs++; |
| 818 | } |
| 819 | *usage = ea_inode_refs + 1; |
| 820 | ret = 0; |
| 821 | out: |
| 822 | brelse(iloc.bh); |
| 823 | brelse(bh); |
| 824 | return ret; |
| 825 | } |
| 826 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 827 | static inline size_t round_up_cluster(struct inode *inode, size_t length) |
| 828 | { |
| 829 | struct super_block *sb = inode->i_sb; |
| 830 | size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits + |
| 831 | inode->i_blkbits); |
| 832 | size_t mask = ~(cluster_size - 1); |
| 833 | |
| 834 | return (length + cluster_size - 1) & mask; |
| 835 | } |
| 836 | |
| 837 | static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len) |
| 838 | { |
| 839 | int err; |
| 840 | |
| 841 | err = dquot_alloc_inode(inode); |
| 842 | if (err) |
| 843 | return err; |
| 844 | err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len)); |
| 845 | if (err) |
| 846 | dquot_free_inode(inode); |
| 847 | return err; |
| 848 | } |
| 849 | |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 850 | static void ext4_xattr_inode_free_quota(struct inode *parent, |
| 851 | struct inode *ea_inode, |
| 852 | size_t len) |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 853 | { |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 854 | if (ea_inode && |
| 855 | ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) |
| 856 | return; |
| 857 | dquot_free_space_nodirty(parent, round_up_cluster(parent, len)); |
| 858 | dquot_free_inode(parent); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 859 | } |
| 860 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 861 | int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode, |
| 862 | struct buffer_head *block_bh, size_t value_len, |
| 863 | bool is_create) |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 864 | { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 865 | int credits; |
| 866 | int blocks; |
| 867 | |
| 868 | /* |
| 869 | * 1) Owner inode update |
| 870 | * 2) Ref count update on old xattr block |
| 871 | * 3) new xattr block |
| 872 | * 4) block bitmap update for new xattr block |
| 873 | * 5) group descriptor for new xattr block |
| 874 | * 6) block bitmap update for old xattr block |
| 875 | * 7) group descriptor for old block |
| 876 | * |
| 877 | * 6 & 7 can happen if we have two racing threads T_a and T_b |
| 878 | * which are each trying to set an xattr on inodes I_a and I_b |
| 879 | * which were both initially sharing an xattr block. |
| 880 | */ |
| 881 | credits = 7; |
| 882 | |
| 883 | /* Quota updates. */ |
| 884 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb); |
| 885 | |
| 886 | /* |
| 887 | * In case of inline data, we may push out the data to a block, |
| 888 | * so we need to reserve credits for this eventuality |
| 889 | */ |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 890 | if (inode && ext4_has_inline_data(inode)) |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 891 | credits += ext4_writepage_trans_blocks(inode) + 1; |
| 892 | |
| 893 | /* We are done if ea_inode feature is not enabled. */ |
| 894 | if (!ext4_has_feature_ea_inode(sb)) |
| 895 | return credits; |
| 896 | |
| 897 | /* New ea_inode, inode map, block bitmap, group descriptor. */ |
| 898 | credits += 4; |
| 899 | |
| 900 | /* Data blocks. */ |
| 901 | blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits; |
| 902 | |
| 903 | /* Indirection block or one level of extent tree. */ |
| 904 | blocks += 1; |
| 905 | |
| 906 | /* Block bitmap and group descriptor updates for each block. */ |
| 907 | credits += blocks * 2; |
| 908 | |
| 909 | /* Blocks themselves. */ |
| 910 | credits += blocks; |
| 911 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 912 | if (!is_create) { |
| 913 | /* Dereference ea_inode holding old xattr value. |
| 914 | * Old ea_inode, inode map, block bitmap, group descriptor. |
| 915 | */ |
| 916 | credits += 4; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 917 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 918 | /* Data blocks for old ea_inode. */ |
| 919 | blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 920 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 921 | /* Indirection block or one level of extent tree for old |
| 922 | * ea_inode. |
| 923 | */ |
| 924 | blocks += 1; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 925 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 926 | /* Block bitmap and group descriptor updates for each block. */ |
| 927 | credits += blocks * 2; |
| 928 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 929 | |
| 930 | /* We may need to clone the existing xattr block in which case we need |
| 931 | * to increment ref counts for existing ea_inodes referenced by it. |
| 932 | */ |
| 933 | if (block_bh) { |
| 934 | struct ext4_xattr_entry *entry = BFIRST(block_bh); |
| 935 | |
| 936 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) |
| 937 | if (entry->e_value_inum) |
| 938 | /* Ref count update on ea_inode. */ |
| 939 | credits += 1; |
| 940 | } |
| 941 | return credits; |
| 942 | } |
| 943 | |
Tahsin Erdogan | 30a7eb9 | 2017-06-22 11:42:09 -0400 | [diff] [blame] | 944 | static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode, |
| 945 | int credits, struct buffer_head *bh, |
| 946 | bool dirty, bool block_csum) |
| 947 | { |
| 948 | int error; |
| 949 | |
| 950 | if (!ext4_handle_valid(handle)) |
| 951 | return 0; |
| 952 | |
| 953 | if (handle->h_buffer_credits >= credits) |
| 954 | return 0; |
| 955 | |
| 956 | error = ext4_journal_extend(handle, credits - handle->h_buffer_credits); |
| 957 | if (!error) |
| 958 | return 0; |
| 959 | if (error < 0) { |
| 960 | ext4_warning(inode->i_sb, "Extend journal (error %d)", error); |
| 961 | return error; |
| 962 | } |
| 963 | |
| 964 | if (bh && dirty) { |
| 965 | if (block_csum) |
| 966 | ext4_xattr_block_csum_set(inode, bh); |
| 967 | error = ext4_handle_dirty_metadata(handle, NULL, bh); |
| 968 | if (error) { |
| 969 | ext4_warning(inode->i_sb, "Handle metadata (error %d)", |
| 970 | error); |
| 971 | return error; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | error = ext4_journal_restart(handle, credits); |
| 976 | if (error) { |
| 977 | ext4_warning(inode->i_sb, "Restart journal (error %d)", error); |
| 978 | return error; |
| 979 | } |
| 980 | |
| 981 | if (bh) { |
| 982 | error = ext4_journal_get_write_access(handle, bh); |
| 983 | if (error) { |
| 984 | ext4_warning(inode->i_sb, |
| 985 | "Get write access failed (error %d)", |
| 986 | error); |
| 987 | return error; |
| 988 | } |
| 989 | } |
| 990 | return 0; |
| 991 | } |
| 992 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 993 | static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode, |
| 994 | int ref_change) |
| 995 | { |
| 996 | struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode); |
| 997 | struct ext4_iloc iloc; |
| 998 | s64 ref_count; |
| 999 | u32 hash; |
| 1000 | int ret; |
| 1001 | |
| 1002 | inode_lock(ea_inode); |
| 1003 | |
| 1004 | ret = ext4_reserve_inode_write(handle, ea_inode, &iloc); |
| 1005 | if (ret) { |
| 1006 | iloc.bh = NULL; |
| 1007 | goto out; |
| 1008 | } |
| 1009 | |
| 1010 | ref_count = ext4_xattr_inode_get_ref(ea_inode); |
| 1011 | ref_count += ref_change; |
| 1012 | ext4_xattr_inode_set_ref(ea_inode, ref_count); |
| 1013 | |
| 1014 | if (ref_change > 0) { |
| 1015 | WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld", |
| 1016 | ea_inode->i_ino, ref_count); |
| 1017 | |
| 1018 | if (ref_count == 1) { |
| 1019 | WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u", |
| 1020 | ea_inode->i_ino, ea_inode->i_nlink); |
| 1021 | |
| 1022 | set_nlink(ea_inode, 1); |
| 1023 | ext4_orphan_del(handle, ea_inode); |
| 1024 | |
Tahsin Erdogan | cdb7ee4 | 2017-06-22 11:55:14 -0400 | [diff] [blame] | 1025 | if (ea_inode_cache) { |
| 1026 | hash = ext4_xattr_inode_get_hash(ea_inode); |
| 1027 | mb_cache_entry_create(ea_inode_cache, |
| 1028 | GFP_NOFS, hash, |
| 1029 | ea_inode->i_ino, |
| 1030 | true /* reusable */); |
| 1031 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1032 | } |
| 1033 | } else { |
| 1034 | WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld", |
| 1035 | ea_inode->i_ino, ref_count); |
| 1036 | |
| 1037 | if (ref_count == 0) { |
| 1038 | WARN_ONCE(ea_inode->i_nlink != 1, |
| 1039 | "EA inode %lu i_nlink=%u", |
| 1040 | ea_inode->i_ino, ea_inode->i_nlink); |
| 1041 | |
| 1042 | clear_nlink(ea_inode); |
| 1043 | ext4_orphan_add(handle, ea_inode); |
| 1044 | |
Tahsin Erdogan | cdb7ee4 | 2017-06-22 11:55:14 -0400 | [diff] [blame] | 1045 | if (ea_inode_cache) { |
| 1046 | hash = ext4_xattr_inode_get_hash(ea_inode); |
| 1047 | mb_cache_entry_delete(ea_inode_cache, hash, |
| 1048 | ea_inode->i_ino); |
| 1049 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc); |
| 1054 | iloc.bh = NULL; |
| 1055 | if (ret) |
| 1056 | ext4_warning_inode(ea_inode, |
| 1057 | "ext4_mark_iloc_dirty() failed ret=%d", ret); |
| 1058 | out: |
| 1059 | brelse(iloc.bh); |
| 1060 | inode_unlock(ea_inode); |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode) |
| 1065 | { |
| 1066 | return ext4_xattr_inode_update_ref(handle, ea_inode, 1); |
| 1067 | } |
| 1068 | |
| 1069 | static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode) |
| 1070 | { |
| 1071 | return ext4_xattr_inode_update_ref(handle, ea_inode, -1); |
| 1072 | } |
| 1073 | |
| 1074 | static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent, |
| 1075 | struct ext4_xattr_entry *first) |
| 1076 | { |
| 1077 | struct inode *ea_inode; |
| 1078 | struct ext4_xattr_entry *entry; |
| 1079 | struct ext4_xattr_entry *failed_entry; |
| 1080 | unsigned int ea_ino; |
| 1081 | int err, saved_err; |
| 1082 | |
| 1083 | for (entry = first; !IS_LAST_ENTRY(entry); |
| 1084 | entry = EXT4_XATTR_NEXT(entry)) { |
| 1085 | if (!entry->e_value_inum) |
| 1086 | continue; |
| 1087 | ea_ino = le32_to_cpu(entry->e_value_inum); |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1088 | err = ext4_xattr_inode_iget(parent, ea_ino, |
| 1089 | le32_to_cpu(entry->e_hash), |
| 1090 | &ea_inode); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1091 | if (err) |
| 1092 | goto cleanup; |
| 1093 | err = ext4_xattr_inode_inc_ref(handle, ea_inode); |
| 1094 | if (err) { |
| 1095 | ext4_warning_inode(ea_inode, "inc ref error %d", err); |
| 1096 | iput(ea_inode); |
| 1097 | goto cleanup; |
| 1098 | } |
| 1099 | iput(ea_inode); |
| 1100 | } |
| 1101 | return 0; |
| 1102 | |
| 1103 | cleanup: |
| 1104 | saved_err = err; |
| 1105 | failed_entry = entry; |
| 1106 | |
| 1107 | for (entry = first; entry != failed_entry; |
| 1108 | entry = EXT4_XATTR_NEXT(entry)) { |
| 1109 | if (!entry->e_value_inum) |
| 1110 | continue; |
| 1111 | ea_ino = le32_to_cpu(entry->e_value_inum); |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1112 | err = ext4_xattr_inode_iget(parent, ea_ino, |
| 1113 | le32_to_cpu(entry->e_hash), |
| 1114 | &ea_inode); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1115 | if (err) { |
| 1116 | ext4_warning(parent->i_sb, |
| 1117 | "cleanup ea_ino %u iget error %d", ea_ino, |
| 1118 | err); |
| 1119 | continue; |
| 1120 | } |
| 1121 | err = ext4_xattr_inode_dec_ref(handle, ea_inode); |
| 1122 | if (err) |
| 1123 | ext4_warning_inode(ea_inode, "cleanup dec ref error %d", |
| 1124 | err); |
| 1125 | iput(ea_inode); |
| 1126 | } |
| 1127 | return saved_err; |
| 1128 | } |
| 1129 | |
Tahsin Erdogan | 30a7eb9 | 2017-06-22 11:42:09 -0400 | [diff] [blame] | 1130 | static void |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1131 | ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent, |
| 1132 | struct buffer_head *bh, |
| 1133 | struct ext4_xattr_entry *first, bool block_csum, |
| 1134 | struct ext4_xattr_inode_array **ea_inode_array, |
| 1135 | int extra_credits, bool skip_quota) |
Tahsin Erdogan | 30a7eb9 | 2017-06-22 11:42:09 -0400 | [diff] [blame] | 1136 | { |
| 1137 | struct inode *ea_inode; |
| 1138 | struct ext4_xattr_entry *entry; |
| 1139 | bool dirty = false; |
| 1140 | unsigned int ea_ino; |
| 1141 | int err; |
| 1142 | int credits; |
| 1143 | |
| 1144 | /* One credit for dec ref on ea_inode, one for orphan list addition, */ |
| 1145 | credits = 2 + extra_credits; |
| 1146 | |
| 1147 | for (entry = first; !IS_LAST_ENTRY(entry); |
| 1148 | entry = EXT4_XATTR_NEXT(entry)) { |
| 1149 | if (!entry->e_value_inum) |
| 1150 | continue; |
| 1151 | ea_ino = le32_to_cpu(entry->e_value_inum); |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1152 | err = ext4_xattr_inode_iget(parent, ea_ino, |
| 1153 | le32_to_cpu(entry->e_hash), |
| 1154 | &ea_inode); |
Tahsin Erdogan | 30a7eb9 | 2017-06-22 11:42:09 -0400 | [diff] [blame] | 1155 | if (err) |
| 1156 | continue; |
| 1157 | |
| 1158 | err = ext4_expand_inode_array(ea_inode_array, ea_inode); |
| 1159 | if (err) { |
| 1160 | ext4_warning_inode(ea_inode, |
| 1161 | "Expand inode array err=%d", err); |
| 1162 | iput(ea_inode); |
| 1163 | continue; |
| 1164 | } |
| 1165 | |
| 1166 | err = ext4_xattr_ensure_credits(handle, parent, credits, bh, |
| 1167 | dirty, block_csum); |
| 1168 | if (err) { |
| 1169 | ext4_warning_inode(ea_inode, "Ensure credits err=%d", |
| 1170 | err); |
| 1171 | continue; |
| 1172 | } |
| 1173 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1174 | err = ext4_xattr_inode_dec_ref(handle, ea_inode); |
| 1175 | if (err) { |
| 1176 | ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d", |
| 1177 | err); |
| 1178 | continue; |
| 1179 | } |
| 1180 | |
| 1181 | if (!skip_quota) |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1182 | ext4_xattr_inode_free_quota(parent, ea_inode, |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1183 | le32_to_cpu(entry->e_value_size)); |
Tahsin Erdogan | 30a7eb9 | 2017-06-22 11:42:09 -0400 | [diff] [blame] | 1184 | |
| 1185 | /* |
| 1186 | * Forget about ea_inode within the same transaction that |
| 1187 | * decrements the ref count. This avoids duplicate decrements in |
| 1188 | * case the rest of the work spills over to subsequent |
| 1189 | * transactions. |
| 1190 | */ |
| 1191 | entry->e_value_inum = 0; |
| 1192 | entry->e_value_size = 0; |
| 1193 | |
| 1194 | dirty = true; |
| 1195 | } |
| 1196 | |
| 1197 | if (dirty) { |
| 1198 | /* |
| 1199 | * Note that we are deliberately skipping csum calculation for |
| 1200 | * the final update because we do not expect any journal |
| 1201 | * restarts until xattr block is freed. |
| 1202 | */ |
| 1203 | |
| 1204 | err = ext4_handle_dirty_metadata(handle, NULL, bh); |
| 1205 | if (err) |
| 1206 | ext4_warning_inode(parent, |
| 1207 | "handle dirty metadata err=%d", err); |
| 1208 | } |
| 1209 | } |
| 1210 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1211 | /* |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 1212 | * Release the xattr block BH: If the reference count is > 1, decrement it; |
| 1213 | * otherwise free the block. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1214 | */ |
| 1215 | static void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1216 | ext4_xattr_release_block(handle_t *handle, struct inode *inode, |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1217 | struct buffer_head *bh, |
| 1218 | struct ext4_xattr_inode_array **ea_inode_array, |
| 1219 | int extra_credits) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1220 | { |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 1221 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1222 | u32 hash, ref; |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1223 | int error = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1224 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 1225 | BUFFER_TRACE(bh, "get_write_access"); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1226 | error = ext4_journal_get_write_access(handle, bh); |
| 1227 | if (error) |
| 1228 | goto out; |
| 1229 | |
| 1230 | lock_buffer(bh); |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1231 | hash = le32_to_cpu(BHDR(bh)->h_hash); |
| 1232 | ref = le32_to_cpu(BHDR(bh)->h_refcount); |
| 1233 | if (ref == 1) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1234 | ea_bdebug(bh, "refcount now=0; freeing"); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1235 | /* |
| 1236 | * This must happen under buffer lock for |
| 1237 | * ext4_xattr_block_set() to reliably detect freed block |
| 1238 | */ |
Tahsin Erdogan | cdb7ee4 | 2017-06-22 11:55:14 -0400 | [diff] [blame] | 1239 | if (ea_block_cache) |
| 1240 | mb_cache_entry_delete(ea_block_cache, hash, |
| 1241 | bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1242 | get_bh(bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 1243 | unlock_buffer(bh); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1244 | |
| 1245 | if (ext4_has_feature_ea_inode(inode->i_sb)) |
| 1246 | ext4_xattr_inode_dec_ref_all(handle, inode, bh, |
| 1247 | BFIRST(bh), |
| 1248 | true /* block_csum */, |
| 1249 | ea_inode_array, |
| 1250 | extra_credits, |
| 1251 | true /* skip_quota */); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 1252 | ext4_free_blocks(handle, inode, bh, 0, 1, |
| 1253 | EXT4_FREE_BLOCKS_METADATA | |
| 1254 | EXT4_FREE_BLOCKS_FORGET); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1255 | } else { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1256 | ref--; |
| 1257 | BHDR(bh)->h_refcount = cpu_to_le32(ref); |
| 1258 | if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) { |
| 1259 | struct mb_cache_entry *ce; |
| 1260 | |
Tahsin Erdogan | cdb7ee4 | 2017-06-22 11:55:14 -0400 | [diff] [blame] | 1261 | if (ea_block_cache) { |
| 1262 | ce = mb_cache_entry_get(ea_block_cache, hash, |
| 1263 | bh->b_blocknr); |
| 1264 | if (ce) { |
| 1265 | ce->e_reusable = 1; |
| 1266 | mb_cache_entry_put(ea_block_cache, ce); |
| 1267 | } |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1271 | ext4_xattr_block_csum_set(inode, bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 1272 | /* |
| 1273 | * Beware of this ugliness: Releasing of xattr block references |
| 1274 | * from different inodes can race and so we have to protect |
| 1275 | * from a race where someone else frees the block (and releases |
| 1276 | * its journal_head) before we are done dirtying the buffer. In |
| 1277 | * nojournal mode this race is harmless and we actually cannot |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1278 | * call ext4_handle_dirty_metadata() with locked buffer as |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 1279 | * that function can call sync_dirty_buffer() so for that case |
| 1280 | * we handle the dirtying after unlocking the buffer. |
| 1281 | */ |
| 1282 | if (ext4_handle_valid(handle)) |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1283 | error = ext4_handle_dirty_metadata(handle, inode, bh); |
Eric Sandeen | c1bb05a | 2012-02-20 23:06:18 -0500 | [diff] [blame] | 1284 | unlock_buffer(bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 1285 | if (!ext4_handle_valid(handle)) |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1286 | error = ext4_handle_dirty_metadata(handle, inode, bh); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1287 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1288 | ext4_handle_sync(handle); |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 1289 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1)); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1290 | ea_bdebug(bh, "refcount now=%d; releasing", |
| 1291 | le32_to_cpu(BHDR(bh)->h_refcount)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1292 | } |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1293 | out: |
| 1294 | ext4_std_error(inode->i_sb, error); |
| 1295 | return; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1298 | /* |
| 1299 | * Find the available free space for EAs. This also returns the total number of |
| 1300 | * bytes used by EA entries. |
| 1301 | */ |
| 1302 | static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last, |
| 1303 | size_t *min_offs, void *base, int *total) |
| 1304 | { |
| 1305 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1306 | if (!last->e_value_inum && last->e_value_size) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1307 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 1308 | if (offs < *min_offs) |
| 1309 | *min_offs = offs; |
| 1310 | } |
Theodore Ts'o | 7b1b2c1 | 2014-02-19 20:15:21 -0500 | [diff] [blame] | 1311 | if (total) |
| 1312 | *total += EXT4_XATTR_LEN(last->e_name_len); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1313 | } |
| 1314 | return (*min_offs - ((void *)last - base) - sizeof(__u32)); |
| 1315 | } |
| 1316 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1317 | /* |
| 1318 | * Write the value of the EA in an inode. |
| 1319 | */ |
| 1320 | static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode, |
| 1321 | const void *buf, int bufsize) |
| 1322 | { |
| 1323 | struct buffer_head *bh = NULL; |
| 1324 | unsigned long block = 0; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1325 | int blocksize = ea_inode->i_sb->s_blocksize; |
| 1326 | int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1327 | int csize, wsize = 0; |
| 1328 | int ret = 0; |
| 1329 | int retries = 0; |
| 1330 | |
| 1331 | retry: |
| 1332 | while (ret >= 0 && ret < max_blocks) { |
| 1333 | struct ext4_map_blocks map; |
| 1334 | map.m_lblk = block += ret; |
| 1335 | map.m_len = max_blocks -= ret; |
| 1336 | |
| 1337 | ret = ext4_map_blocks(handle, ea_inode, &map, |
| 1338 | EXT4_GET_BLOCKS_CREATE); |
| 1339 | if (ret <= 0) { |
| 1340 | ext4_mark_inode_dirty(handle, ea_inode); |
| 1341 | if (ret == -ENOSPC && |
| 1342 | ext4_should_retry_alloc(ea_inode->i_sb, &retries)) { |
| 1343 | ret = 0; |
| 1344 | goto retry; |
| 1345 | } |
| 1346 | break; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | if (ret < 0) |
| 1351 | return ret; |
| 1352 | |
| 1353 | block = 0; |
| 1354 | while (wsize < bufsize) { |
| 1355 | if (bh != NULL) |
| 1356 | brelse(bh); |
| 1357 | csize = (bufsize - wsize) > blocksize ? blocksize : |
| 1358 | bufsize - wsize; |
| 1359 | bh = ext4_getblk(handle, ea_inode, block, 0); |
| 1360 | if (IS_ERR(bh)) |
| 1361 | return PTR_ERR(bh); |
| 1362 | ret = ext4_journal_get_write_access(handle, bh); |
| 1363 | if (ret) |
| 1364 | goto out; |
| 1365 | |
| 1366 | memcpy(bh->b_data, buf, csize); |
| 1367 | set_buffer_uptodate(bh); |
| 1368 | ext4_handle_dirty_metadata(handle, ea_inode, bh); |
| 1369 | |
| 1370 | buf += csize; |
| 1371 | wsize += csize; |
| 1372 | block += 1; |
| 1373 | } |
| 1374 | |
| 1375 | inode_lock(ea_inode); |
| 1376 | i_size_write(ea_inode, wsize); |
| 1377 | ext4_update_i_disksize(ea_inode, wsize); |
| 1378 | inode_unlock(ea_inode); |
| 1379 | |
| 1380 | ext4_mark_inode_dirty(handle, ea_inode); |
| 1381 | |
| 1382 | out: |
| 1383 | brelse(bh); |
| 1384 | |
| 1385 | return ret; |
| 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * Create an inode to store the value of a large EA. |
| 1390 | */ |
| 1391 | static struct inode *ext4_xattr_inode_create(handle_t *handle, |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1392 | struct inode *inode, u32 hash) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1393 | { |
| 1394 | struct inode *ea_inode = NULL; |
Tahsin Erdogan | 9e1ba00 | 2017-06-21 21:27:00 -0400 | [diff] [blame] | 1395 | uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) }; |
Tahsin Erdogan | bd3b963 | 2017-06-21 21:24:31 -0400 | [diff] [blame] | 1396 | int err; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1397 | |
| 1398 | /* |
| 1399 | * Let the next inode be the goal, so we try and allocate the EA inode |
| 1400 | * in the same group, or nearby one. |
| 1401 | */ |
| 1402 | ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode, |
Tahsin Erdogan | 9e1ba00 | 2017-06-21 21:27:00 -0400 | [diff] [blame] | 1403 | S_IFREG | 0600, NULL, inode->i_ino + 1, owner, |
Tahsin Erdogan | 1b917ed | 2017-06-21 21:21:39 -0400 | [diff] [blame] | 1404 | EXT4_EA_INODE_FL); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1405 | if (!IS_ERR(ea_inode)) { |
| 1406 | ea_inode->i_op = &ext4_file_inode_operations; |
| 1407 | ea_inode->i_fop = &ext4_file_operations; |
| 1408 | ext4_set_aops(ea_inode); |
Tahsin Erdogan | 33d201e | 2017-06-21 21:17:10 -0400 | [diff] [blame] | 1409 | ext4_xattr_inode_set_class(ea_inode); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1410 | unlock_new_inode(ea_inode); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1411 | ext4_xattr_inode_set_ref(ea_inode, 1); |
| 1412 | ext4_xattr_inode_set_hash(ea_inode, hash); |
| 1413 | err = ext4_mark_inode_dirty(handle, ea_inode); |
| 1414 | if (!err) |
| 1415 | err = ext4_inode_attach_jinode(ea_inode); |
Tahsin Erdogan | bd3b963 | 2017-06-21 21:24:31 -0400 | [diff] [blame] | 1416 | if (err) { |
| 1417 | iput(ea_inode); |
| 1418 | return ERR_PTR(err); |
| 1419 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1420 | |
| 1421 | /* |
| 1422 | * Xattr inodes are shared therefore quota charging is performed |
| 1423 | * at a higher level. |
| 1424 | */ |
| 1425 | dquot_free_inode(ea_inode); |
| 1426 | dquot_drop(ea_inode); |
| 1427 | inode_lock(ea_inode); |
| 1428 | ea_inode->i_flags |= S_NOQUOTA; |
| 1429 | inode_unlock(ea_inode); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | return ea_inode; |
| 1433 | } |
| 1434 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1435 | static struct inode * |
| 1436 | ext4_xattr_inode_cache_find(struct inode *inode, const void *value, |
| 1437 | size_t value_len, u32 hash) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1438 | { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1439 | struct inode *ea_inode; |
| 1440 | struct mb_cache_entry *ce; |
| 1441 | struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode); |
| 1442 | void *ea_data; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1443 | |
Tahsin Erdogan | cdb7ee4 | 2017-06-22 11:55:14 -0400 | [diff] [blame] | 1444 | if (!ea_inode_cache) |
| 1445 | return NULL; |
| 1446 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1447 | ce = mb_cache_entry_find_first(ea_inode_cache, hash); |
| 1448 | if (!ce) |
| 1449 | return NULL; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1450 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1451 | ea_data = ext4_kvmalloc(value_len, GFP_NOFS); |
| 1452 | if (!ea_data) { |
| 1453 | mb_cache_entry_put(ea_inode_cache, ce); |
| 1454 | return NULL; |
| 1455 | } |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1456 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1457 | while (ce) { |
| 1458 | ea_inode = ext4_iget(inode->i_sb, ce->e_value); |
| 1459 | if (!IS_ERR(ea_inode) && |
| 1460 | !is_bad_inode(ea_inode) && |
| 1461 | (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) && |
| 1462 | i_size_read(ea_inode) == value_len && |
| 1463 | !ext4_xattr_inode_read(ea_inode, ea_data, value_len) && |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 1464 | !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data, |
| 1465 | value_len) && |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1466 | !memcmp(value, ea_data, value_len)) { |
| 1467 | mb_cache_entry_touch(ea_inode_cache, ce); |
| 1468 | mb_cache_entry_put(ea_inode_cache, ce); |
| 1469 | kvfree(ea_data); |
| 1470 | return ea_inode; |
| 1471 | } |
| 1472 | |
| 1473 | if (!IS_ERR(ea_inode)) |
| 1474 | iput(ea_inode); |
| 1475 | ce = mb_cache_entry_find_next(ea_inode_cache, ce); |
| 1476 | } |
| 1477 | kvfree(ea_data); |
| 1478 | return NULL; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * Add value of the EA in an inode. |
| 1483 | */ |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1484 | static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode, |
| 1485 | const void *value, size_t value_len, |
| 1486 | struct inode **ret_inode) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1487 | { |
| 1488 | struct inode *ea_inode; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1489 | u32 hash; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1490 | int err; |
| 1491 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1492 | hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len); |
| 1493 | ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash); |
| 1494 | if (ea_inode) { |
| 1495 | err = ext4_xattr_inode_inc_ref(handle, ea_inode); |
| 1496 | if (err) { |
| 1497 | iput(ea_inode); |
| 1498 | return err; |
| 1499 | } |
| 1500 | |
| 1501 | *ret_inode = ea_inode; |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1505 | /* Create an inode for the EA value */ |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1506 | ea_inode = ext4_xattr_inode_create(handle, inode, hash); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1507 | if (IS_ERR(ea_inode)) |
| 1508 | return PTR_ERR(ea_inode); |
| 1509 | |
| 1510 | err = ext4_xattr_inode_write(handle, ea_inode, value, value_len); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1511 | if (err) { |
| 1512 | ext4_xattr_inode_dec_ref(handle, ea_inode); |
| 1513 | iput(ea_inode); |
| 1514 | return err; |
| 1515 | } |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1516 | |
Tahsin Erdogan | cdb7ee4 | 2017-06-22 11:55:14 -0400 | [diff] [blame] | 1517 | if (EA_INODE_CACHE(inode)) |
| 1518 | mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash, |
| 1519 | ea_inode->i_ino, true /* reusable */); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1520 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1521 | *ret_inode = ea_inode; |
| 1522 | return 0; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1523 | } |
| 1524 | |
Tahsin Erdogan | 9c6e785 | 2017-06-22 11:48:53 -0400 | [diff] [blame] | 1525 | /* |
| 1526 | * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode |
| 1527 | * feature is enabled. |
| 1528 | */ |
| 1529 | #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U) |
| 1530 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1531 | static int ext4_xattr_set_entry(struct ext4_xattr_info *i, |
| 1532 | struct ext4_xattr_search *s, |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 1533 | handle_t *handle, struct inode *inode, |
| 1534 | bool is_block) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1535 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1536 | struct ext4_xattr_entry *last; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1537 | struct ext4_xattr_entry *here = s->here; |
| 1538 | size_t min_offs = s->end - s->base, name_len = strlen(i->name); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1539 | int in_inode = i->in_inode; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1540 | struct inode *old_ea_inode = NULL; |
| 1541 | struct inode *new_ea_inode = NULL; |
| 1542 | size_t old_size, new_size; |
| 1543 | int ret; |
| 1544 | |
| 1545 | /* Space used by old and new values. */ |
| 1546 | old_size = (!s->not_found && !here->e_value_inum) ? |
| 1547 | EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0; |
| 1548 | new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0; |
| 1549 | |
| 1550 | /* |
| 1551 | * Optimization for the simple case when old and new values have the |
| 1552 | * same padded sizes. Not applicable if external inodes are involved. |
| 1553 | */ |
| 1554 | if (new_size && new_size == old_size) { |
| 1555 | size_t offs = le16_to_cpu(here->e_value_offs); |
| 1556 | void *val = s->base + offs; |
| 1557 | |
| 1558 | here->e_value_size = cpu_to_le32(i->value_len); |
| 1559 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
| 1560 | memset(val, 0, new_size); |
| 1561 | } else { |
| 1562 | memcpy(val, i->value, i->value_len); |
| 1563 | /* Clear padding bytes. */ |
| 1564 | memset(val + i->value_len, 0, new_size - i->value_len); |
| 1565 | } |
Tahsin Erdogan | 32aaf19 | 2017-08-14 08:30:06 -0400 | [diff] [blame] | 1566 | goto update_hash; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1567 | } |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1568 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1569 | /* Compute min_offs and last. */ |
| 1570 | last = s->first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1571 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1572 | if (!last->e_value_inum && last->e_value_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1573 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 1574 | if (offs < min_offs) |
| 1575 | min_offs = offs; |
| 1576 | } |
| 1577 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1578 | |
| 1579 | /* Check whether we have enough space. */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1580 | if (i->value) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1581 | size_t free; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1582 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1583 | free = min_offs - ((void *)last - s->base) - sizeof(__u32); |
| 1584 | if (!s->not_found) |
| 1585 | free += EXT4_XATTR_LEN(name_len) + old_size; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1586 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1587 | if (free < EXT4_XATTR_LEN(name_len) + new_size) { |
| 1588 | ret = -ENOSPC; |
| 1589 | goto out; |
| 1590 | } |
Tahsin Erdogan | 9c6e785 | 2017-06-22 11:48:53 -0400 | [diff] [blame] | 1591 | |
| 1592 | /* |
| 1593 | * If storing the value in an external inode is an option, |
| 1594 | * reserve space for xattr entries/names in the external |
| 1595 | * attribute block so that a long value does not occupy the |
| 1596 | * whole space and prevent futher entries being added. |
| 1597 | */ |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 1598 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
| 1599 | new_size && is_block && |
Tahsin Erdogan | 9c6e785 | 2017-06-22 11:48:53 -0400 | [diff] [blame] | 1600 | (min_offs + old_size - new_size) < |
| 1601 | EXT4_XATTR_BLOCK_RESERVE(inode)) { |
| 1602 | ret = -ENOSPC; |
| 1603 | goto out; |
| 1604 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1607 | /* |
| 1608 | * Getting access to old and new ea inodes is subject to failures. |
| 1609 | * Finish that work before doing any modifications to the xattr data. |
| 1610 | */ |
| 1611 | if (!s->not_found && here->e_value_inum) { |
| 1612 | ret = ext4_xattr_inode_iget(inode, |
| 1613 | le32_to_cpu(here->e_value_inum), |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1614 | le32_to_cpu(here->e_hash), |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1615 | &old_ea_inode); |
| 1616 | if (ret) { |
| 1617 | old_ea_inode = NULL; |
| 1618 | goto out; |
| 1619 | } |
| 1620 | } |
| 1621 | if (i->value && in_inode) { |
| 1622 | WARN_ON_ONCE(!i->value_len); |
| 1623 | |
| 1624 | ret = ext4_xattr_inode_alloc_quota(inode, i->value_len); |
| 1625 | if (ret) |
| 1626 | goto out; |
| 1627 | |
| 1628 | ret = ext4_xattr_inode_lookup_create(handle, inode, i->value, |
| 1629 | i->value_len, |
| 1630 | &new_ea_inode); |
| 1631 | if (ret) { |
| 1632 | new_ea_inode = NULL; |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1633 | ext4_xattr_inode_free_quota(inode, NULL, i->value_len); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1634 | goto out; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | if (old_ea_inode) { |
| 1639 | /* We are ready to release ref count on the old_ea_inode. */ |
| 1640 | ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode); |
| 1641 | if (ret) { |
| 1642 | /* Release newly required ref count on new_ea_inode. */ |
| 1643 | if (new_ea_inode) { |
| 1644 | int err; |
| 1645 | |
| 1646 | err = ext4_xattr_inode_dec_ref(handle, |
| 1647 | new_ea_inode); |
| 1648 | if (err) |
| 1649 | ext4_warning_inode(new_ea_inode, |
| 1650 | "dec ref new_ea_inode err=%d", |
| 1651 | err); |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1652 | ext4_xattr_inode_free_quota(inode, new_ea_inode, |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1653 | i->value_len); |
| 1654 | } |
| 1655 | goto out; |
| 1656 | } |
| 1657 | |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1658 | ext4_xattr_inode_free_quota(inode, old_ea_inode, |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1659 | le32_to_cpu(here->e_value_size)); |
| 1660 | } |
| 1661 | |
| 1662 | /* No failures allowed past this point. */ |
| 1663 | |
| 1664 | if (!s->not_found && here->e_value_offs) { |
| 1665 | /* Remove the old value. */ |
| 1666 | void *first_val = s->base + min_offs; |
| 1667 | size_t offs = le16_to_cpu(here->e_value_offs); |
| 1668 | void *val = s->base + offs; |
| 1669 | |
| 1670 | memmove(first_val + old_size, first_val, val - first_val); |
| 1671 | memset(first_val, 0, old_size); |
| 1672 | min_offs += old_size; |
| 1673 | |
| 1674 | /* Adjust all value offsets. */ |
| 1675 | last = s->first; |
| 1676 | while (!IS_LAST_ENTRY(last)) { |
| 1677 | size_t o = le16_to_cpu(last->e_value_offs); |
| 1678 | |
| 1679 | if (!last->e_value_inum && |
| 1680 | last->e_value_size && o < offs) |
| 1681 | last->e_value_offs = cpu_to_le16(o + old_size); |
| 1682 | last = EXT4_XATTR_NEXT(last); |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | if (!i->value) { |
| 1687 | /* Remove old name. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1688 | size_t size = EXT4_XATTR_LEN(name_len); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1689 | |
| 1690 | last = ENTRY((void *)last - size); |
| 1691 | memmove(here, (void *)here + size, |
| 1692 | (void *)last - (void *)here + sizeof(__u32)); |
| 1693 | memset(last, 0, size); |
| 1694 | } else if (s->not_found) { |
| 1695 | /* Insert new name. */ |
| 1696 | size_t size = EXT4_XATTR_LEN(name_len); |
| 1697 | size_t rest = (void *)last - (void *)here + sizeof(__u32); |
| 1698 | |
| 1699 | memmove((void *)here + size, here, rest); |
| 1700 | memset(here, 0, size); |
| 1701 | here->e_name_index = i->name_index; |
| 1702 | here->e_name_len = name_len; |
| 1703 | memcpy(here->e_name, i->name, name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1704 | } else { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1705 | /* This is an update, reset value info. */ |
| 1706 | here->e_value_inum = 0; |
| 1707 | here->e_value_offs = 0; |
| 1708 | here->e_value_size = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | if (i->value) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1712 | /* Insert new value. */ |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1713 | if (in_inode) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1714 | here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1715 | } else if (i->value_len) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1716 | void *val = s->base + min_offs - new_size; |
| 1717 | |
| 1718 | here->e_value_offs = cpu_to_le16(min_offs - new_size); |
Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 1719 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1720 | memset(val, 0, new_size); |
Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 1721 | } else { |
Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 1722 | memcpy(val, i->value, i->value_len); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1723 | /* Clear padding bytes. */ |
| 1724 | memset(val + i->value_len, 0, |
| 1725 | new_size - i->value_len); |
Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 1726 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1727 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1728 | here->e_value_size = cpu_to_le32(i->value_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1729 | } |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 1730 | |
Tahsin Erdogan | 32aaf19 | 2017-08-14 08:30:06 -0400 | [diff] [blame] | 1731 | update_hash: |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 1732 | if (i->value) { |
| 1733 | __le32 hash = 0; |
| 1734 | |
| 1735 | /* Entry hash calculation. */ |
| 1736 | if (in_inode) { |
| 1737 | __le32 crc32c_hash; |
| 1738 | |
| 1739 | /* |
| 1740 | * Feed crc32c hash instead of the raw value for entry |
| 1741 | * hash calculation. This is to avoid walking |
| 1742 | * potentially long value buffer again. |
| 1743 | */ |
| 1744 | crc32c_hash = cpu_to_le32( |
| 1745 | ext4_xattr_inode_get_hash(new_ea_inode)); |
| 1746 | hash = ext4_xattr_hash_entry(here->e_name, |
| 1747 | here->e_name_len, |
| 1748 | &crc32c_hash, 1); |
| 1749 | } else if (is_block) { |
Tahsin Erdogan | 32aaf19 | 2017-08-14 08:30:06 -0400 | [diff] [blame] | 1750 | __le32 *value = s->base + le16_to_cpu( |
| 1751 | here->e_value_offs); |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 1752 | |
| 1753 | hash = ext4_xattr_hash_entry(here->e_name, |
| 1754 | here->e_name_len, value, |
| 1755 | new_size >> 2); |
| 1756 | } |
| 1757 | here->e_hash = hash; |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 1758 | } |
| 1759 | |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 1760 | if (is_block) |
| 1761 | ext4_xattr_rehash((struct ext4_xattr_header *)s->base); |
| 1762 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1763 | ret = 0; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1764 | out: |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1765 | iput(old_ea_inode); |
| 1766 | iput(new_ea_inode); |
| 1767 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1770 | struct ext4_xattr_block_find { |
| 1771 | struct ext4_xattr_search s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1772 | struct buffer_head *bh; |
| 1773 | }; |
| 1774 | |
| 1775 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1776 | ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, |
| 1777 | struct ext4_xattr_block_find *bs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1778 | { |
| 1779 | struct super_block *sb = inode->i_sb; |
| 1780 | int error; |
| 1781 | |
| 1782 | ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", |
| 1783 | i->name_index, i->name, i->value, (long)i->value_len); |
| 1784 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1785 | if (EXT4_I(inode)->i_file_acl) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1786 | /* The inode already has an extended attribute block. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1787 | bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1788 | error = -EIO; |
| 1789 | if (!bs->bh) |
| 1790 | goto cleanup; |
| 1791 | ea_bdebug(bs->bh, "b_count=%d, refcount=%d", |
| 1792 | atomic_read(&(bs->bh->b_count)), |
| 1793 | le32_to_cpu(BHDR(bs->bh)->h_refcount)); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 1794 | if (ext4_xattr_check_block(inode, bs->bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1795 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 1796 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1797 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1798 | goto cleanup; |
| 1799 | } |
| 1800 | /* Find the named attribute. */ |
| 1801 | bs->s.base = BHDR(bs->bh); |
| 1802 | bs->s.first = BFIRST(bs->bh); |
| 1803 | bs->s.end = bs->bh->b_data + bs->bh->b_size; |
| 1804 | bs->s.here = bs->s.first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1805 | error = ext4_xattr_find_entry(&bs->s.here, i->name_index, |
Eric Biggers | 6ba644b | 2017-04-30 00:01:02 -0400 | [diff] [blame] | 1806 | i->name, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1807 | if (error && error != -ENODATA) |
| 1808 | goto cleanup; |
| 1809 | bs->s.not_found = error; |
| 1810 | } |
| 1811 | error = 0; |
| 1812 | |
| 1813 | cleanup: |
| 1814 | return error; |
| 1815 | } |
| 1816 | |
| 1817 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1818 | ext4_xattr_block_set(handle_t *handle, struct inode *inode, |
| 1819 | struct ext4_xattr_info *i, |
| 1820 | struct ext4_xattr_block_find *bs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1821 | { |
| 1822 | struct super_block *sb = inode->i_sb; |
| 1823 | struct buffer_head *new_bh = NULL; |
Tahsin Erdogan | b347e2b | 2017-06-21 22:20:32 -0400 | [diff] [blame] | 1824 | struct ext4_xattr_search s_copy = bs->s; |
| 1825 | struct ext4_xattr_search *s = &s_copy; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1826 | struct mb_cache_entry *ce = NULL; |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1827 | int error = 0; |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 1828 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1829 | struct inode *ea_inode = NULL, *tmp_inode; |
| 1830 | size_t old_ea_inode_quota = 0; |
| 1831 | unsigned int ea_ino; |
| 1832 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1833 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1834 | #define header(x) ((struct ext4_xattr_header *)(x)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1835 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1836 | if (s->base) { |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 1837 | BUFFER_TRACE(bs->bh, "get_write_access"); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1838 | error = ext4_journal_get_write_access(handle, bs->bh); |
| 1839 | if (error) |
| 1840 | goto cleanup; |
| 1841 | lock_buffer(bs->bh); |
| 1842 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1843 | if (header(s->base)->h_refcount == cpu_to_le32(1)) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1844 | __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash); |
| 1845 | |
| 1846 | /* |
| 1847 | * This must happen under buffer lock for |
| 1848 | * ext4_xattr_block_set() to reliably detect modified |
| 1849 | * block |
| 1850 | */ |
Tahsin Erdogan | cdb7ee4 | 2017-06-22 11:55:14 -0400 | [diff] [blame] | 1851 | if (ea_block_cache) |
| 1852 | mb_cache_entry_delete(ea_block_cache, hash, |
| 1853 | bs->bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1854 | ea_bdebug(bs->bh, "modifying in-place"); |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 1855 | error = ext4_xattr_set_entry(i, s, handle, inode, |
| 1856 | true /* is_block */); |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1857 | ext4_xattr_block_csum_set(inode, bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1858 | unlock_buffer(bs->bh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1859 | if (error == -EFSCORRUPTED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1860 | goto bad_block; |
| 1861 | if (!error) |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1862 | error = ext4_handle_dirty_metadata(handle, |
| 1863 | inode, |
| 1864 | bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1865 | if (error) |
| 1866 | goto cleanup; |
| 1867 | goto inserted; |
| 1868 | } else { |
| 1869 | int offset = (char *)s->here - bs->bh->b_data; |
| 1870 | |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1871 | unlock_buffer(bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1872 | ea_bdebug(bs->bh, "cloning"); |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 1873 | s->base = kmalloc(bs->bh->b_size, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1874 | error = -ENOMEM; |
| 1875 | if (s->base == NULL) |
| 1876 | goto cleanup; |
| 1877 | memcpy(s->base, BHDR(bs->bh), bs->bh->b_size); |
| 1878 | s->first = ENTRY(header(s->base)+1); |
| 1879 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 1880 | s->here = ENTRY(s->base + offset); |
| 1881 | s->end = s->base + bs->bh->b_size; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1882 | |
| 1883 | /* |
| 1884 | * If existing entry points to an xattr inode, we need |
| 1885 | * to prevent ext4_xattr_set_entry() from decrementing |
| 1886 | * ref count on it because the reference belongs to the |
| 1887 | * original block. In this case, make the entry look |
| 1888 | * like it has an empty value. |
| 1889 | */ |
| 1890 | if (!s->not_found && s->here->e_value_inum) { |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1891 | ea_ino = le32_to_cpu(s->here->e_value_inum); |
| 1892 | error = ext4_xattr_inode_iget(inode, ea_ino, |
| 1893 | le32_to_cpu(s->here->e_hash), |
| 1894 | &tmp_inode); |
| 1895 | if (error) |
| 1896 | goto cleanup; |
| 1897 | |
| 1898 | if (!ext4_test_inode_state(tmp_inode, |
| 1899 | EXT4_STATE_LUSTRE_EA_INODE)) { |
| 1900 | /* |
| 1901 | * Defer quota free call for previous |
| 1902 | * inode until success is guaranteed. |
| 1903 | */ |
| 1904 | old_ea_inode_quota = le32_to_cpu( |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1905 | s->here->e_value_size); |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1906 | } |
| 1907 | iput(tmp_inode); |
| 1908 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1909 | s->here->e_value_inum = 0; |
| 1910 | s->here->e_value_size = 0; |
| 1911 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1912 | } |
| 1913 | } else { |
| 1914 | /* Allocate a buffer where we construct the new block. */ |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 1915 | s->base = kzalloc(sb->s_blocksize, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1916 | /* assert(header == s->base) */ |
| 1917 | error = -ENOMEM; |
| 1918 | if (s->base == NULL) |
| 1919 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1920 | header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1921 | header(s->base)->h_blocks = cpu_to_le32(1); |
| 1922 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 1923 | s->first = ENTRY(header(s->base)+1); |
| 1924 | s->here = ENTRY(header(s->base)+1); |
| 1925 | s->end = s->base + sb->s_blocksize; |
| 1926 | } |
| 1927 | |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 1928 | error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1929 | if (error == -EFSCORRUPTED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1930 | goto bad_block; |
| 1931 | if (error) |
| 1932 | goto cleanup; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1933 | |
| 1934 | if (i->value && s->here->e_value_inum) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1935 | /* |
| 1936 | * A ref count on ea_inode has been taken as part of the call to |
| 1937 | * ext4_xattr_set_entry() above. We would like to drop this |
| 1938 | * extra ref but we have to wait until the xattr block is |
| 1939 | * initialized and has its own ref count on the ea_inode. |
| 1940 | */ |
| 1941 | ea_ino = le32_to_cpu(s->here->e_value_inum); |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 1942 | error = ext4_xattr_inode_iget(inode, ea_ino, |
| 1943 | le32_to_cpu(s->here->e_hash), |
| 1944 | &ea_inode); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 1945 | if (error) { |
| 1946 | ea_inode = NULL; |
| 1947 | goto cleanup; |
| 1948 | } |
| 1949 | } |
| 1950 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1951 | inserted: |
| 1952 | if (!IS_LAST_ENTRY(s->first)) { |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 1953 | new_bh = ext4_xattr_block_cache_find(inode, header(s->base), |
| 1954 | &ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1955 | if (new_bh) { |
| 1956 | /* We found an identical block in the cache. */ |
| 1957 | if (new_bh == bs->bh) |
| 1958 | ea_bdebug(new_bh, "keeping"); |
| 1959 | else { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1960 | u32 ref; |
| 1961 | |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 1962 | WARN_ON_ONCE(dquot_initialize_needed(inode)); |
| 1963 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1964 | /* The old block is released after updating |
| 1965 | the inode. */ |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 1966 | error = dquot_alloc_block(inode, |
| 1967 | EXT4_C2B(EXT4_SB(sb), 1)); |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 1968 | if (error) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1969 | goto cleanup; |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 1970 | BUFFER_TRACE(new_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1971 | error = ext4_journal_get_write_access(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1972 | new_bh); |
| 1973 | if (error) |
| 1974 | goto cleanup_dquot; |
| 1975 | lock_buffer(new_bh); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1976 | /* |
| 1977 | * We have to be careful about races with |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1978 | * freeing, rehashing or adding references to |
| 1979 | * xattr block. Once we hold buffer lock xattr |
| 1980 | * block's state is stable so we can check |
| 1981 | * whether the block got freed / rehashed or |
| 1982 | * not. Since we unhash mbcache entry under |
| 1983 | * buffer lock when freeing / rehashing xattr |
| 1984 | * block, checking whether entry is still |
| 1985 | * hashed is reliable. Same rules hold for |
| 1986 | * e_reusable handling. |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1987 | */ |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1988 | if (hlist_bl_unhashed(&ce->e_hash_list) || |
| 1989 | !ce->e_reusable) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1990 | /* |
| 1991 | * Undo everything and check mbcache |
| 1992 | * again. |
| 1993 | */ |
| 1994 | unlock_buffer(new_bh); |
| 1995 | dquot_free_block(inode, |
| 1996 | EXT4_C2B(EXT4_SB(sb), |
| 1997 | 1)); |
| 1998 | brelse(new_bh); |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 1999 | mb_cache_entry_put(ea_block_cache, ce); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 2000 | ce = NULL; |
| 2001 | new_bh = NULL; |
| 2002 | goto inserted; |
| 2003 | } |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 2004 | ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1; |
| 2005 | BHDR(new_bh)->h_refcount = cpu_to_le32(ref); |
| 2006 | if (ref >= EXT4_XATTR_REFCOUNT_MAX) |
| 2007 | ce->e_reusable = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2008 | ea_bdebug(new_bh, "reusing; refcount now=%d", |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 2009 | ref); |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 2010 | ext4_xattr_block_csum_set(inode, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2011 | unlock_buffer(new_bh); |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 2012 | error = ext4_handle_dirty_metadata(handle, |
| 2013 | inode, |
| 2014 | new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2015 | if (error) |
| 2016 | goto cleanup_dquot; |
| 2017 | } |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 2018 | mb_cache_entry_touch(ea_block_cache, ce); |
| 2019 | mb_cache_entry_put(ea_block_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2020 | ce = NULL; |
| 2021 | } else if (bs->bh && s->base == bs->bh->b_data) { |
| 2022 | /* We were modifying this block in-place. */ |
| 2023 | ea_bdebug(bs->bh, "keeping this block"); |
Tahsin Erdogan | ec00022 | 2017-08-05 22:41:42 -0400 | [diff] [blame] | 2024 | ext4_xattr_block_cache_insert(ea_block_cache, bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2025 | new_bh = bs->bh; |
| 2026 | get_bh(new_bh); |
| 2027 | } else { |
| 2028 | /* We need to allocate a new block */ |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 2029 | ext4_fsblk_t goal, block; |
| 2030 | |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 2031 | WARN_ON_ONCE(dquot_initialize_needed(inode)); |
| 2032 | |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 2033 | goal = ext4_group_first_block_no(sb, |
Akinobu Mita | d00a6d7 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2034 | EXT4_I(inode)->i_block_group); |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 2035 | |
| 2036 | /* non-extent files can't have physical blocks past 2^32 */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 2037 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 2038 | goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; |
| 2039 | |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 2040 | block = ext4_new_meta_blocks(handle, inode, goal, 0, |
| 2041 | NULL, &error); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2042 | if (error) |
| 2043 | goto cleanup; |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 2044 | |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 2045 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 2046 | BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS); |
| 2047 | |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 2048 | ea_idebug(inode, "creating block %llu", |
| 2049 | (unsigned long long)block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2050 | |
| 2051 | new_bh = sb_getblk(sb, block); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 2052 | if (unlikely(!new_bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 2053 | error = -ENOMEM; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2054 | getblk_failed: |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 2055 | ext4_free_blocks(handle, inode, NULL, block, 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2056 | EXT4_FREE_BLOCKS_METADATA); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2057 | goto cleanup; |
| 2058 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2059 | error = ext4_xattr_inode_inc_ref_all(handle, inode, |
| 2060 | ENTRY(header(s->base)+1)); |
| 2061 | if (error) |
| 2062 | goto getblk_failed; |
| 2063 | if (ea_inode) { |
| 2064 | /* Drop the extra ref on ea_inode. */ |
| 2065 | error = ext4_xattr_inode_dec_ref(handle, |
| 2066 | ea_inode); |
| 2067 | if (error) |
| 2068 | ext4_warning_inode(ea_inode, |
| 2069 | "dec ref error=%d", |
| 2070 | error); |
| 2071 | iput(ea_inode); |
| 2072 | ea_inode = NULL; |
| 2073 | } |
| 2074 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2075 | lock_buffer(new_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2076 | error = ext4_journal_get_create_access(handle, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2077 | if (error) { |
| 2078 | unlock_buffer(new_bh); |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 2079 | error = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2080 | goto getblk_failed; |
| 2081 | } |
| 2082 | memcpy(new_bh->b_data, s->base, new_bh->b_size); |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 2083 | ext4_xattr_block_csum_set(inode, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2084 | set_buffer_uptodate(new_bh); |
| 2085 | unlock_buffer(new_bh); |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 2086 | ext4_xattr_block_cache_insert(ea_block_cache, new_bh); |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 2087 | error = ext4_handle_dirty_metadata(handle, inode, |
| 2088 | new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2089 | if (error) |
| 2090 | goto cleanup; |
| 2091 | } |
| 2092 | } |
| 2093 | |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 2094 | if (old_ea_inode_quota) |
| 2095 | ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2096 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2097 | /* Update the inode. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2098 | 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] | 2099 | |
| 2100 | /* Drop the previous xattr block. */ |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2101 | if (bs->bh && bs->bh != new_bh) { |
| 2102 | struct ext4_xattr_inode_array *ea_inode_array = NULL; |
| 2103 | |
| 2104 | ext4_xattr_release_block(handle, inode, bs->bh, |
| 2105 | &ea_inode_array, |
| 2106 | 0 /* extra_credits */); |
| 2107 | ext4_xattr_inode_array_free(ea_inode_array); |
| 2108 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2109 | error = 0; |
| 2110 | |
| 2111 | cleanup: |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2112 | if (ea_inode) { |
| 2113 | int error2; |
| 2114 | |
| 2115 | error2 = ext4_xattr_inode_dec_ref(handle, ea_inode); |
| 2116 | if (error2) |
| 2117 | ext4_warning_inode(ea_inode, "dec ref error=%d", |
| 2118 | error2); |
| 2119 | |
| 2120 | /* If there was an error, revert the quota charge. */ |
| 2121 | if (error) |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 2122 | ext4_xattr_inode_free_quota(inode, ea_inode, |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2123 | i_size_read(ea_inode)); |
| 2124 | iput(ea_inode); |
| 2125 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2126 | if (ce) |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 2127 | mb_cache_entry_put(ea_block_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2128 | brelse(new_bh); |
| 2129 | if (!(bs->bh && s->base == bs->bh->b_data)) |
| 2130 | kfree(s->base); |
| 2131 | |
| 2132 | return error; |
| 2133 | |
| 2134 | cleanup_dquot: |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 2135 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2136 | goto cleanup; |
| 2137 | |
| 2138 | bad_block: |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 2139 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 2140 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2141 | goto cleanup; |
| 2142 | |
| 2143 | #undef header |
| 2144 | } |
| 2145 | |
Tao Ma | 879b382 | 2012-12-05 10:28:46 -0500 | [diff] [blame] | 2146 | int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, |
| 2147 | struct ext4_xattr_ibody_find *is) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2148 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2149 | struct ext4_xattr_ibody_header *header; |
| 2150 | struct ext4_inode *raw_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2151 | int error; |
| 2152 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2153 | if (EXT4_I(inode)->i_extra_isize == 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2154 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2155 | raw_inode = ext4_raw_inode(&is->iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2156 | header = IHDR(inode, raw_inode); |
| 2157 | is->s.base = is->s.first = IFIRST(header); |
| 2158 | is->s.here = is->s.first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2159 | 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] | 2160 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 2161 | error = xattr_check_inode(inode, header, is->s.end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2162 | if (error) |
| 2163 | return error; |
| 2164 | /* Find the named attribute. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2165 | error = ext4_xattr_find_entry(&is->s.here, i->name_index, |
Eric Biggers | 6ba644b | 2017-04-30 00:01:02 -0400 | [diff] [blame] | 2166 | i->name, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2167 | if (error && error != -ENODATA) |
| 2168 | return error; |
| 2169 | is->s.not_found = error; |
| 2170 | } |
| 2171 | return 0; |
| 2172 | } |
| 2173 | |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 2174 | int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode, |
| 2175 | struct ext4_xattr_info *i, |
| 2176 | struct ext4_xattr_ibody_find *is) |
| 2177 | { |
| 2178 | struct ext4_xattr_ibody_header *header; |
| 2179 | struct ext4_xattr_search *s = &is->s; |
| 2180 | int error; |
| 2181 | |
| 2182 | if (EXT4_I(inode)->i_extra_isize == 0) |
| 2183 | return -ENOSPC; |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 2184 | error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */); |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 2185 | if (error) { |
| 2186 | if (error == -ENOSPC && |
| 2187 | ext4_has_inline_data(inode)) { |
| 2188 | error = ext4_try_to_evict_inline_data(handle, inode, |
| 2189 | EXT4_XATTR_LEN(strlen(i->name) + |
| 2190 | EXT4_XATTR_SIZE(i->value_len))); |
| 2191 | if (error) |
| 2192 | return error; |
| 2193 | error = ext4_xattr_ibody_find(inode, i, is); |
| 2194 | if (error) |
| 2195 | return error; |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 2196 | error = ext4_xattr_set_entry(i, s, handle, inode, |
| 2197 | false /* is_block */); |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 2198 | } |
| 2199 | if (error) |
| 2200 | return error; |
| 2201 | } |
| 2202 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
| 2203 | if (!IS_LAST_ENTRY(s->first)) { |
| 2204 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
| 2205 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
| 2206 | } else { |
| 2207 | header->h_magic = cpu_to_le32(0); |
| 2208 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
| 2209 | } |
| 2210 | return 0; |
| 2211 | } |
| 2212 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2213 | static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode, |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 2214 | struct ext4_xattr_info *i, |
| 2215 | struct ext4_xattr_ibody_find *is) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2216 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2217 | struct ext4_xattr_ibody_header *header; |
| 2218 | struct ext4_xattr_search *s = &is->s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2219 | int error; |
| 2220 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2221 | if (EXT4_I(inode)->i_extra_isize == 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2222 | return -ENOSPC; |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 2223 | error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2224 | if (error) |
| 2225 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2226 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2227 | if (!IS_LAST_ENTRY(s->first)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2228 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 2229 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2230 | } else { |
| 2231 | header->h_magic = cpu_to_le32(0); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 2232 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2233 | } |
| 2234 | return 0; |
| 2235 | } |
| 2236 | |
Jan Kara | 3fd1646 | 2016-02-22 22:43:04 -0500 | [diff] [blame] | 2237 | static int ext4_xattr_value_same(struct ext4_xattr_search *s, |
| 2238 | struct ext4_xattr_info *i) |
| 2239 | { |
| 2240 | void *value; |
| 2241 | |
Tahsin Erdogan | 0bd454c | 2017-06-21 22:02:06 -0400 | [diff] [blame] | 2242 | /* When e_value_inum is set the value is stored externally. */ |
| 2243 | if (s->here->e_value_inum) |
| 2244 | return 0; |
Jan Kara | 3fd1646 | 2016-02-22 22:43:04 -0500 | [diff] [blame] | 2245 | if (le32_to_cpu(s->here->e_value_size) != i->value_len) |
| 2246 | return 0; |
| 2247 | value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs); |
| 2248 | return !memcmp(value, i->value, i->value_len); |
| 2249 | } |
| 2250 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2251 | static struct buffer_head *ext4_xattr_get_block(struct inode *inode) |
| 2252 | { |
| 2253 | struct buffer_head *bh; |
| 2254 | int error; |
| 2255 | |
| 2256 | if (!EXT4_I(inode)->i_file_acl) |
| 2257 | return NULL; |
| 2258 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
| 2259 | if (!bh) |
| 2260 | return ERR_PTR(-EIO); |
| 2261 | error = ext4_xattr_check_block(inode, bh); |
| 2262 | if (error) |
| 2263 | return ERR_PTR(error); |
| 2264 | return bh; |
| 2265 | } |
| 2266 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2267 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2268 | * ext4_xattr_set_handle() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2269 | * |
Wang Sheng-Hui | 6e9510b | 2011-01-10 12:10:30 -0500 | [diff] [blame] | 2270 | * Create, replace or remove an extended attribute for this inode. Value |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2271 | * is NULL to remove an existing extended attribute, and non-NULL to |
| 2272 | * either replace an existing extended attribute, or create a new extended |
| 2273 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE |
| 2274 | * specify that an extended attribute must exist and must not exist |
| 2275 | * previous to the call, respectively. |
| 2276 | * |
| 2277 | * Returns 0, or a negative error number on failure. |
| 2278 | */ |
| 2279 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2280 | 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] | 2281 | const char *name, const void *value, size_t value_len, |
| 2282 | int flags) |
| 2283 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2284 | struct ext4_xattr_info i = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2285 | .name_index = name_index, |
| 2286 | .name = name, |
| 2287 | .value = value, |
| 2288 | .value_len = value_len, |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2289 | .in_inode = 0, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2290 | }; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2291 | struct ext4_xattr_ibody_find is = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2292 | .s = { .not_found = -ENODATA, }, |
| 2293 | }; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2294 | struct ext4_xattr_block_find bs = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2295 | .s = { .not_found = -ENODATA, }, |
| 2296 | }; |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 2297 | int no_expand; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2298 | int error; |
| 2299 | |
| 2300 | if (!name) |
| 2301 | return -EINVAL; |
| 2302 | if (strlen(name) > 255) |
| 2303 | return -ERANGE; |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 2304 | |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 2305 | ext4_write_lock_xattr(inode, &no_expand); |
Kalpak Shah | 4d20c68 | 2008-10-08 23:21:54 -0400 | [diff] [blame] | 2306 | |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 2307 | /* Check journal credits under write lock. */ |
| 2308 | if (ext4_handle_valid(handle)) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2309 | struct buffer_head *bh; |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 2310 | int credits; |
| 2311 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2312 | bh = ext4_xattr_get_block(inode); |
| 2313 | if (IS_ERR(bh)) { |
| 2314 | error = PTR_ERR(bh); |
| 2315 | goto cleanup; |
| 2316 | } |
| 2317 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 2318 | credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh, |
| 2319 | value_len, |
| 2320 | flags & XATTR_CREATE); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2321 | brelse(bh); |
| 2322 | |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 2323 | if (!ext4_handle_has_enough_credits(handle, credits)) { |
| 2324 | error = -ENOSPC; |
| 2325 | goto cleanup; |
| 2326 | } |
| 2327 | } |
| 2328 | |
Eric Sandeen | 6654361 | 2011-10-26 03:32:07 -0400 | [diff] [blame] | 2329 | error = ext4_reserve_inode_write(handle, inode, &is.iloc); |
Eric Sandeen | 86ebfd0 | 2009-11-15 15:30:52 -0500 | [diff] [blame] | 2330 | if (error) |
| 2331 | goto cleanup; |
| 2332 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 2333 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2334 | struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc); |
| 2335 | 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] | 2336 | ext4_clear_inode_state(inode, EXT4_STATE_NEW); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2337 | } |
| 2338 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2339 | error = ext4_xattr_ibody_find(inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2340 | if (error) |
| 2341 | goto cleanup; |
| 2342 | if (is.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2343 | error = ext4_xattr_block_find(inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2344 | if (error) |
| 2345 | goto cleanup; |
| 2346 | if (is.s.not_found && bs.s.not_found) { |
| 2347 | error = -ENODATA; |
| 2348 | if (flags & XATTR_REPLACE) |
| 2349 | goto cleanup; |
| 2350 | error = 0; |
| 2351 | if (!value) |
| 2352 | goto cleanup; |
| 2353 | } else { |
| 2354 | error = -EEXIST; |
| 2355 | if (flags & XATTR_CREATE) |
| 2356 | goto cleanup; |
| 2357 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2358 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2359 | if (!value) { |
| 2360 | if (!is.s.not_found) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2361 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2362 | else if (!bs.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2363 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2364 | } else { |
Jan Kara | 3fd1646 | 2016-02-22 22:43:04 -0500 | [diff] [blame] | 2365 | error = 0; |
| 2366 | /* Xattr value did not change? Save us some work and bail out */ |
| 2367 | if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i)) |
| 2368 | goto cleanup; |
| 2369 | if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i)) |
| 2370 | goto cleanup; |
| 2371 | |
Tahsin Erdogan | b347e2b | 2017-06-21 22:20:32 -0400 | [diff] [blame] | 2372 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
| 2373 | (EXT4_XATTR_SIZE(i.value_len) > |
| 2374 | EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize))) |
| 2375 | i.in_inode = 1; |
| 2376 | retry_inode: |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2377 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2378 | if (!error && !bs.s.not_found) { |
| 2379 | i.value = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2380 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2381 | } else if (error == -ENOSPC) { |
Tiger Yang | 7e01c8e | 2008-05-14 16:05:47 -0700 | [diff] [blame] | 2382 | if (EXT4_I(inode)->i_file_acl && !bs.s.base) { |
| 2383 | error = ext4_xattr_block_find(inode, &i, &bs); |
| 2384 | if (error) |
| 2385 | goto cleanup; |
| 2386 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2387 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Tahsin Erdogan | b347e2b | 2017-06-21 22:20:32 -0400 | [diff] [blame] | 2388 | if (!error && !is.s.not_found) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2389 | i.value = NULL; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2390 | error = ext4_xattr_ibody_set(handle, inode, &i, |
| 2391 | &is); |
Tahsin Erdogan | b347e2b | 2017-06-21 22:20:32 -0400 | [diff] [blame] | 2392 | } else if (error == -ENOSPC) { |
| 2393 | /* |
| 2394 | * Xattr does not fit in the block, store at |
| 2395 | * external inode if possible. |
| 2396 | */ |
| 2397 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
| 2398 | !i.in_inode) { |
| 2399 | i.in_inode = 1; |
| 2400 | goto retry_inode; |
| 2401 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2402 | } |
| 2403 | } |
| 2404 | } |
| 2405 | if (!error) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2406 | ext4_xattr_update_super_block(handle, inode->i_sb); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 2407 | inode->i_ctime = current_time(inode); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2408 | if (!value) |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 2409 | no_expand = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2410 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2411 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2412 | * The bh is consumed by ext4_mark_iloc_dirty, even with |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2413 | * error != 0. |
| 2414 | */ |
| 2415 | is.iloc.bh = NULL; |
| 2416 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2417 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | cleanup: |
| 2421 | brelse(is.iloc.bh); |
| 2422 | brelse(bs.bh); |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 2423 | ext4_write_unlock_xattr(inode, &no_expand); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2424 | return error; |
| 2425 | } |
| 2426 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 2427 | int ext4_xattr_set_credits(struct inode *inode, size_t value_len, |
| 2428 | bool is_create, int *credits) |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 2429 | { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2430 | struct buffer_head *bh; |
| 2431 | int err; |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 2432 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2433 | *credits = 0; |
| 2434 | |
| 2435 | if (!EXT4_SB(inode->i_sb)->s_journal) |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 2436 | return 0; |
| 2437 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2438 | down_read(&EXT4_I(inode)->xattr_sem); |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 2439 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2440 | bh = ext4_xattr_get_block(inode); |
| 2441 | if (IS_ERR(bh)) { |
| 2442 | err = PTR_ERR(bh); |
| 2443 | } else { |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 2444 | *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh, |
| 2445 | value_len, is_create); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2446 | brelse(bh); |
| 2447 | err = 0; |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 2448 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2449 | |
| 2450 | up_read(&EXT4_I(inode)->xattr_sem); |
| 2451 | return err; |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 2452 | } |
| 2453 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2454 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2455 | * ext4_xattr_set() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2456 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2457 | * Like ext4_xattr_set_handle, but start from an inode. This extended |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2458 | * attribute modification is a filesystem transaction by itself. |
| 2459 | * |
| 2460 | * Returns 0, or a negative error number on failure. |
| 2461 | */ |
| 2462 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2463 | ext4_xattr_set(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2464 | const void *value, size_t value_len, int flags) |
| 2465 | { |
| 2466 | handle_t *handle; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2467 | struct super_block *sb = inode->i_sb; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2468 | int error, retries = 0; |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 2469 | int credits; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2470 | |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 2471 | error = dquot_initialize(inode); |
| 2472 | if (error) |
| 2473 | return error; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2474 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2475 | retry: |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 2476 | error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE, |
| 2477 | &credits); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2478 | if (error) |
| 2479 | return error; |
| 2480 | |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 2481 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2482 | if (IS_ERR(handle)) { |
| 2483 | error = PTR_ERR(handle); |
| 2484 | } else { |
| 2485 | int error2; |
| 2486 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2487 | error = ext4_xattr_set_handle(handle, inode, name_index, name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2488 | value, value_len, flags); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2489 | error2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2490 | if (error == -ENOSPC && |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2491 | ext4_should_retry_alloc(sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2492 | goto retry; |
| 2493 | if (error == 0) |
| 2494 | error = error2; |
| 2495 | } |
| 2496 | |
| 2497 | return error; |
| 2498 | } |
| 2499 | |
| 2500 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2501 | * Shift the EA entries in the inode to create space for the increased |
| 2502 | * i_extra_isize. |
| 2503 | */ |
| 2504 | static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry, |
| 2505 | int value_offs_shift, void *to, |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 2506 | void *from, size_t n) |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2507 | { |
| 2508 | struct ext4_xattr_entry *last = entry; |
| 2509 | int new_offs; |
| 2510 | |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 2511 | /* We always shift xattr headers further thus offsets get lower */ |
| 2512 | BUG_ON(value_offs_shift > 0); |
| 2513 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2514 | /* Adjust the value offsets of the entries */ |
| 2515 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2516 | if (!last->e_value_inum && last->e_value_size) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2517 | new_offs = le16_to_cpu(last->e_value_offs) + |
| 2518 | value_offs_shift; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2519 | last->e_value_offs = cpu_to_le16(new_offs); |
| 2520 | } |
| 2521 | } |
| 2522 | /* Shift the entries by n bytes */ |
| 2523 | memmove(to, from, n); |
| 2524 | } |
| 2525 | |
| 2526 | /* |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 2527 | * Move xattr pointed to by 'entry' from inode into external xattr block |
| 2528 | */ |
| 2529 | static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, |
| 2530 | struct ext4_inode *raw_inode, |
| 2531 | struct ext4_xattr_entry *entry) |
| 2532 | { |
| 2533 | struct ext4_xattr_ibody_find *is = NULL; |
| 2534 | struct ext4_xattr_block_find *bs = NULL; |
| 2535 | char *buffer = NULL, *b_entry_name = NULL; |
Tahsin Erdogan | f610910 | 2017-06-21 22:11:54 -0400 | [diff] [blame] | 2536 | size_t value_size = le32_to_cpu(entry->e_value_size); |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 2537 | struct ext4_xattr_info i = { |
| 2538 | .value = NULL, |
| 2539 | .value_len = 0, |
| 2540 | .name_index = entry->e_name_index, |
Tahsin Erdogan | f610910 | 2017-06-21 22:11:54 -0400 | [diff] [blame] | 2541 | .in_inode = !!entry->e_value_inum, |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 2542 | }; |
| 2543 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); |
| 2544 | int error; |
| 2545 | |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 2546 | is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); |
| 2547 | bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS); |
| 2548 | buffer = kmalloc(value_size, GFP_NOFS); |
| 2549 | b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS); |
| 2550 | if (!is || !bs || !buffer || !b_entry_name) { |
| 2551 | error = -ENOMEM; |
| 2552 | goto out; |
| 2553 | } |
| 2554 | |
| 2555 | is->s.not_found = -ENODATA; |
| 2556 | bs->s.not_found = -ENODATA; |
| 2557 | is->iloc.bh = NULL; |
| 2558 | bs->bh = NULL; |
| 2559 | |
| 2560 | /* Save the entry name and the entry value */ |
Tahsin Erdogan | f610910 | 2017-06-21 22:11:54 -0400 | [diff] [blame] | 2561 | if (entry->e_value_inum) { |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 2562 | error = ext4_xattr_inode_get(inode, entry, buffer, value_size); |
Tahsin Erdogan | f610910 | 2017-06-21 22:11:54 -0400 | [diff] [blame] | 2563 | if (error) |
| 2564 | goto out; |
| 2565 | } else { |
| 2566 | size_t value_offs = le16_to_cpu(entry->e_value_offs); |
| 2567 | memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size); |
| 2568 | } |
| 2569 | |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 2570 | memcpy(b_entry_name, entry->e_name, entry->e_name_len); |
| 2571 | b_entry_name[entry->e_name_len] = '\0'; |
| 2572 | i.name = b_entry_name; |
| 2573 | |
| 2574 | error = ext4_get_inode_loc(inode, &is->iloc); |
| 2575 | if (error) |
| 2576 | goto out; |
| 2577 | |
| 2578 | error = ext4_xattr_ibody_find(inode, &i, is); |
| 2579 | if (error) |
| 2580 | goto out; |
| 2581 | |
| 2582 | /* Remove the chosen entry from the inode */ |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2583 | error = ext4_xattr_ibody_set(handle, inode, &i, is); |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 2584 | if (error) |
| 2585 | goto out; |
| 2586 | |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 2587 | i.value = buffer; |
| 2588 | i.value_len = value_size; |
| 2589 | error = ext4_xattr_block_find(inode, &i, bs); |
| 2590 | if (error) |
| 2591 | goto out; |
| 2592 | |
| 2593 | /* Add entry which was removed from the inode into the block */ |
| 2594 | error = ext4_xattr_block_set(handle, inode, &i, bs); |
| 2595 | if (error) |
| 2596 | goto out; |
| 2597 | error = 0; |
| 2598 | out: |
| 2599 | kfree(b_entry_name); |
| 2600 | kfree(buffer); |
| 2601 | if (is) |
| 2602 | brelse(is->iloc.bh); |
| 2603 | kfree(is); |
| 2604 | kfree(bs); |
| 2605 | |
| 2606 | return error; |
| 2607 | } |
| 2608 | |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 2609 | static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode, |
| 2610 | struct ext4_inode *raw_inode, |
| 2611 | int isize_diff, size_t ifree, |
| 2612 | size_t bfree, int *total_ino) |
| 2613 | { |
| 2614 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); |
| 2615 | struct ext4_xattr_entry *small_entry; |
| 2616 | struct ext4_xattr_entry *entry; |
| 2617 | struct ext4_xattr_entry *last; |
| 2618 | unsigned int entry_size; /* EA entry size */ |
| 2619 | unsigned int total_size; /* EA entry size + value size */ |
| 2620 | unsigned int min_total_size; |
| 2621 | int error; |
| 2622 | |
| 2623 | while (isize_diff > ifree) { |
| 2624 | entry = NULL; |
| 2625 | small_entry = NULL; |
| 2626 | min_total_size = ~0U; |
| 2627 | last = IFIRST(header); |
| 2628 | /* Find the entry best suited to be pushed into EA block */ |
| 2629 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Tahsin Erdogan | 9bb21ce | 2017-06-21 22:05:44 -0400 | [diff] [blame] | 2630 | total_size = EXT4_XATTR_LEN(last->e_name_len); |
| 2631 | if (!last->e_value_inum) |
| 2632 | total_size += EXT4_XATTR_SIZE( |
| 2633 | le32_to_cpu(last->e_value_size)); |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 2634 | if (total_size <= bfree && |
| 2635 | total_size < min_total_size) { |
| 2636 | if (total_size + ifree < isize_diff) { |
| 2637 | small_entry = last; |
| 2638 | } else { |
| 2639 | entry = last; |
| 2640 | min_total_size = total_size; |
| 2641 | } |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | if (entry == NULL) { |
| 2646 | if (small_entry == NULL) |
| 2647 | return -ENOSPC; |
| 2648 | entry = small_entry; |
| 2649 | } |
| 2650 | |
| 2651 | entry_size = EXT4_XATTR_LEN(entry->e_name_len); |
Tahsin Erdogan | 9bb21ce | 2017-06-21 22:05:44 -0400 | [diff] [blame] | 2652 | total_size = entry_size; |
| 2653 | if (!entry->e_value_inum) |
| 2654 | total_size += EXT4_XATTR_SIZE( |
| 2655 | le32_to_cpu(entry->e_value_size)); |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 2656 | error = ext4_xattr_move_to_block(handle, inode, raw_inode, |
| 2657 | entry); |
| 2658 | if (error) |
| 2659 | return error; |
| 2660 | |
| 2661 | *total_ino -= entry_size; |
| 2662 | ifree += total_size; |
| 2663 | bfree -= total_size; |
| 2664 | } |
| 2665 | |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 2669 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2670 | * Expand an inode by new_extra_isize bytes when EAs are present. |
| 2671 | * Returns 0 on success or negative error number on failure. |
| 2672 | */ |
| 2673 | int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize, |
| 2674 | struct ext4_inode *raw_inode, handle_t *handle) |
| 2675 | { |
| 2676 | struct ext4_xattr_ibody_header *header; |
Miao Xie | b640b2c | 2017-08-06 00:55:48 -0400 | [diff] [blame] | 2677 | struct buffer_head *bh; |
Miao Xie | cf0a5e8 | 2017-08-06 00:40:01 -0400 | [diff] [blame] | 2678 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 2679 | static unsigned int mnt_count; |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 2680 | size_t min_offs; |
| 2681 | size_t ifree, bfree; |
Theodore Ts'o | 7b1b2c1 | 2014-02-19 20:15:21 -0500 | [diff] [blame] | 2682 | int total_ino; |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 2683 | void *base, *end; |
Jan Kara | d014119 | 2016-08-11 11:50:30 -0400 | [diff] [blame] | 2684 | int error = 0, tried_min_extra_isize = 0; |
Miao Xie | cf0a5e8 | 2017-08-06 00:40:01 -0400 | [diff] [blame] | 2685 | int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize); |
Jan Kara | d014119 | 2016-08-11 11:50:30 -0400 | [diff] [blame] | 2686 | 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] | 2687 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2688 | retry: |
Jan Kara | d014119 | 2016-08-11 11:50:30 -0400 | [diff] [blame] | 2689 | isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize; |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 2690 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) |
Miao Xie | b640b2c | 2017-08-06 00:55:48 -0400 | [diff] [blame] | 2691 | return 0; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2692 | |
| 2693 | header = IHDR(inode, raw_inode); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2694 | |
| 2695 | /* |
| 2696 | * Check if enough free space is available in the inode to shift the |
| 2697 | * entries ahead by new_extra_isize. |
| 2698 | */ |
| 2699 | |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 2700 | base = IFIRST(header); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2701 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
| 2702 | min_offs = end - base; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2703 | total_ino = sizeof(struct ext4_xattr_ibody_header); |
| 2704 | |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 2705 | error = xattr_check_inode(inode, header, end); |
| 2706 | if (error) |
| 2707 | goto cleanup; |
| 2708 | |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 2709 | ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 2710 | if (ifree >= isize_diff) |
| 2711 | goto shift; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2712 | |
| 2713 | /* |
| 2714 | * Enough free space isn't available in the inode, check if |
| 2715 | * EA block can hold new_extra_isize bytes. |
| 2716 | */ |
| 2717 | if (EXT4_I(inode)->i_file_acl) { |
| 2718 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
| 2719 | error = -EIO; |
| 2720 | if (!bh) |
| 2721 | goto cleanup; |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 2722 | if (ext4_xattr_check_block(inode, bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 2723 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 2724 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2725 | error = -EFSCORRUPTED; |
Miao Xie | b640b2c | 2017-08-06 00:55:48 -0400 | [diff] [blame] | 2726 | brelse(bh); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2727 | goto cleanup; |
| 2728 | } |
| 2729 | base = BHDR(bh); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2730 | end = bh->b_data + bh->b_size; |
| 2731 | min_offs = end - base; |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 2732 | bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base, |
| 2733 | NULL); |
Miao Xie | b640b2c | 2017-08-06 00:55:48 -0400 | [diff] [blame] | 2734 | brelse(bh); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 2735 | if (bfree + ifree < isize_diff) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2736 | if (!tried_min_extra_isize && s_min_extra_isize) { |
| 2737 | tried_min_extra_isize++; |
| 2738 | new_extra_isize = s_min_extra_isize; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2739 | goto retry; |
| 2740 | } |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 2741 | error = -ENOSPC; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2742 | goto cleanup; |
| 2743 | } |
| 2744 | } else { |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 2745 | bfree = inode->i_sb->s_blocksize; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2746 | } |
| 2747 | |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 2748 | error = ext4_xattr_make_inode_space(handle, inode, raw_inode, |
| 2749 | isize_diff, ifree, bfree, |
| 2750 | &total_ino); |
| 2751 | if (error) { |
| 2752 | if (error == -ENOSPC && !tried_min_extra_isize && |
| 2753 | s_min_extra_isize) { |
| 2754 | tried_min_extra_isize++; |
| 2755 | new_extra_isize = s_min_extra_isize; |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 2756 | goto retry; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2757 | } |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 2758 | goto cleanup; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2759 | } |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 2760 | shift: |
| 2761 | /* Adjust the offsets and shift the remaining entries ahead */ |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 2762 | ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 2763 | - new_extra_isize, (void *)raw_inode + |
| 2764 | EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize, |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 2765 | (void *)header, total_ino); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 2766 | EXT4_I(inode)->i_extra_isize = new_extra_isize; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2767 | |
| 2768 | cleanup: |
Miao Xie | b640b2c | 2017-08-06 00:55:48 -0400 | [diff] [blame] | 2769 | if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) { |
Miao Xie | cf0a5e8 | 2017-08-06 00:40:01 -0400 | [diff] [blame] | 2770 | ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.", |
| 2771 | inode->i_ino); |
| 2772 | mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count); |
| 2773 | } |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2774 | return error; |
| 2775 | } |
| 2776 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2777 | #define EIA_INCR 16 /* must be 2^n */ |
| 2778 | #define EIA_MASK (EIA_INCR - 1) |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2779 | |
| 2780 | /* Add the large xattr @inode into @ea_inode_array for deferred iput(). |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2781 | * If @ea_inode_array is new or full it will be grown and the old |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2782 | * contents copied over. |
| 2783 | */ |
| 2784 | static int |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2785 | ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, |
| 2786 | struct inode *inode) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2787 | { |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2788 | if (*ea_inode_array == NULL) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2789 | /* |
| 2790 | * Start with 15 inodes, so it fits into a power-of-two size. |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2791 | * If *ea_inode_array is NULL, this is essentially offsetof() |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2792 | */ |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2793 | (*ea_inode_array) = |
| 2794 | kmalloc(offsetof(struct ext4_xattr_inode_array, |
| 2795 | inodes[EIA_MASK]), |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2796 | GFP_NOFS); |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2797 | if (*ea_inode_array == NULL) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2798 | return -ENOMEM; |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2799 | (*ea_inode_array)->count = 0; |
| 2800 | } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2801 | /* expand the array once all 15 + n * 16 slots are full */ |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2802 | struct ext4_xattr_inode_array *new_array = NULL; |
| 2803 | int count = (*ea_inode_array)->count; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2804 | |
| 2805 | /* if new_array is NULL, this is essentially offsetof() */ |
| 2806 | new_array = kmalloc( |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2807 | offsetof(struct ext4_xattr_inode_array, |
| 2808 | inodes[count + EIA_INCR]), |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2809 | GFP_NOFS); |
| 2810 | if (new_array == NULL) |
| 2811 | return -ENOMEM; |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2812 | memcpy(new_array, *ea_inode_array, |
| 2813 | offsetof(struct ext4_xattr_inode_array, inodes[count])); |
| 2814 | kfree(*ea_inode_array); |
| 2815 | *ea_inode_array = new_array; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2816 | } |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2817 | (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2818 | return 0; |
| 2819 | } |
| 2820 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2821 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2822 | * ext4_xattr_delete_inode() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2823 | * |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2824 | * Free extended attribute resources associated with this inode. Traverse |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2825 | * all entries and decrement reference on any xattr inodes associated with this |
| 2826 | * inode. This is called immediately before an inode is freed. We have exclusive |
| 2827 | * access to the inode. If an orphan inode is deleted it will also release its |
| 2828 | * references on xattr block and xattr inodes. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2829 | */ |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2830 | int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode, |
| 2831 | struct ext4_xattr_inode_array **ea_inode_array, |
| 2832 | int extra_credits) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2833 | { |
| 2834 | struct buffer_head *bh = NULL; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2835 | struct ext4_xattr_ibody_header *header; |
Tahsin Erdogan | 30a7eb9 | 2017-06-22 11:42:09 -0400 | [diff] [blame] | 2836 | struct ext4_iloc iloc = { .bh = NULL }; |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2837 | struct ext4_xattr_entry *entry; |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 2838 | struct inode *ea_inode; |
Tahsin Erdogan | 30a7eb9 | 2017-06-22 11:42:09 -0400 | [diff] [blame] | 2839 | int error; |
| 2840 | |
| 2841 | error = ext4_xattr_ensure_credits(handle, inode, extra_credits, |
| 2842 | NULL /* bh */, |
| 2843 | false /* dirty */, |
| 2844 | false /* block_csum */); |
| 2845 | if (error) { |
| 2846 | EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error); |
| 2847 | goto cleanup; |
| 2848 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2849 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2850 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
| 2851 | ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2852 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2853 | error = ext4_get_inode_loc(inode, &iloc); |
Tahsin Erdogan | 65d3000 | 2017-06-21 22:24:38 -0400 | [diff] [blame] | 2854 | if (error) { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2855 | EXT4_ERROR_INODE(inode, "inode loc (error %d)", error); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2856 | goto cleanup; |
| 2857 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2858 | |
| 2859 | error = ext4_journal_get_write_access(handle, iloc.bh); |
| 2860 | if (error) { |
| 2861 | EXT4_ERROR_INODE(inode, "write access (error %d)", |
| 2862 | error); |
| 2863 | goto cleanup; |
| 2864 | } |
| 2865 | |
| 2866 | header = IHDR(inode, ext4_raw_inode(&iloc)); |
| 2867 | if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC)) |
| 2868 | ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh, |
| 2869 | IFIRST(header), |
| 2870 | false /* block_csum */, |
| 2871 | ea_inode_array, |
| 2872 | extra_credits, |
| 2873 | false /* skip_quota */); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2874 | } |
| 2875 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2876 | if (EXT4_I(inode)->i_file_acl) { |
| 2877 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
| 2878 | if (!bh) { |
| 2879 | EXT4_ERROR_INODE(inode, "block %llu read error", |
| 2880 | EXT4_I(inode)->i_file_acl); |
| 2881 | error = -EIO; |
| 2882 | goto cleanup; |
| 2883 | } |
| 2884 | error = ext4_xattr_check_block(inode, bh); |
| 2885 | if (error) { |
| 2886 | EXT4_ERROR_INODE(inode, "bad block %llu (error %d)", |
| 2887 | EXT4_I(inode)->i_file_acl, error); |
| 2888 | goto cleanup; |
| 2889 | } |
| 2890 | |
| 2891 | if (ext4_has_feature_ea_inode(inode->i_sb)) { |
| 2892 | for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry); |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 2893 | entry = EXT4_XATTR_NEXT(entry)) { |
| 2894 | if (!entry->e_value_inum) |
| 2895 | continue; |
| 2896 | error = ext4_xattr_inode_iget(inode, |
| 2897 | le32_to_cpu(entry->e_value_inum), |
| 2898 | le32_to_cpu(entry->e_hash), |
| 2899 | &ea_inode); |
| 2900 | if (error) |
| 2901 | continue; |
| 2902 | ext4_xattr_inode_free_quota(inode, ea_inode, |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2903 | le32_to_cpu(entry->e_value_size)); |
Tahsin Erdogan | a6d0567 | 2017-08-24 14:25:02 -0400 | [diff] [blame] | 2904 | iput(ea_inode); |
| 2905 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2906 | |
| 2907 | } |
| 2908 | |
| 2909 | ext4_xattr_release_block(handle, inode, bh, ea_inode_array, |
| 2910 | extra_credits); |
| 2911 | /* |
| 2912 | * Update i_file_acl value in the same transaction that releases |
| 2913 | * block. |
| 2914 | */ |
| 2915 | EXT4_I(inode)->i_file_acl = 0; |
| 2916 | error = ext4_mark_inode_dirty(handle, inode); |
| 2917 | if (error) { |
| 2918 | EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)", |
| 2919 | error); |
| 2920 | goto cleanup; |
| 2921 | } |
Tahsin Erdogan | 30a7eb9 | 2017-06-22 11:42:09 -0400 | [diff] [blame] | 2922 | } |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2923 | error = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2924 | cleanup: |
Tahsin Erdogan | 30a7eb9 | 2017-06-22 11:42:09 -0400 | [diff] [blame] | 2925 | brelse(iloc.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2926 | brelse(bh); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2927 | return error; |
| 2928 | } |
| 2929 | |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2930 | void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2931 | { |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2932 | int idx; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2933 | |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2934 | if (ea_inode_array == NULL) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2935 | return; |
| 2936 | |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 2937 | for (idx = 0; idx < ea_inode_array->count; ++idx) |
| 2938 | iput(ea_inode_array->inodes[idx]); |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame] | 2939 | kfree(ea_inode_array); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | /* |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 2943 | * ext4_xattr_block_cache_insert() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2944 | * |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 2945 | * Create a new entry in the extended attribute block cache, and insert |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2946 | * it unless such an entry is already in the cache. |
| 2947 | * |
| 2948 | * Returns 0, or a negative error number on failure. |
| 2949 | */ |
| 2950 | static void |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 2951 | ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache, |
| 2952 | struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2953 | { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 2954 | struct ext4_xattr_header *header = BHDR(bh); |
| 2955 | __u32 hash = le32_to_cpu(header->h_hash); |
| 2956 | int reusable = le32_to_cpu(header->h_refcount) < |
| 2957 | EXT4_XATTR_REFCOUNT_MAX; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2958 | int error; |
| 2959 | |
Tahsin Erdogan | cdb7ee4 | 2017-06-22 11:55:14 -0400 | [diff] [blame] | 2960 | if (!ea_block_cache) |
| 2961 | return; |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 2962 | error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash, |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 2963 | bh->b_blocknr, reusable); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2964 | if (error) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 2965 | if (error == -EBUSY) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2966 | ea_bdebug(bh, "already in cache"); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 2967 | } else |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2968 | ea_bdebug(bh, "inserting [%x]", (int)hash); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2972 | * ext4_xattr_cmp() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2973 | * |
| 2974 | * Compare two extended attribute blocks for equality. |
| 2975 | * |
| 2976 | * Returns 0 if the blocks are equal, 1 if they differ, and |
| 2977 | * a negative error number on errors. |
| 2978 | */ |
| 2979 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2980 | ext4_xattr_cmp(struct ext4_xattr_header *header1, |
| 2981 | struct ext4_xattr_header *header2) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2982 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2983 | struct ext4_xattr_entry *entry1, *entry2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2984 | |
| 2985 | entry1 = ENTRY(header1+1); |
| 2986 | entry2 = ENTRY(header2+1); |
| 2987 | while (!IS_LAST_ENTRY(entry1)) { |
| 2988 | if (IS_LAST_ENTRY(entry2)) |
| 2989 | return 1; |
| 2990 | if (entry1->e_hash != entry2->e_hash || |
| 2991 | entry1->e_name_index != entry2->e_name_index || |
| 2992 | entry1->e_name_len != entry2->e_name_len || |
| 2993 | entry1->e_value_size != entry2->e_value_size || |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2994 | entry1->e_value_inum != entry2->e_value_inum || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2995 | memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) |
| 2996 | return 1; |
Tahsin Erdogan | 7cec191 | 2017-06-21 22:14:30 -0400 | [diff] [blame] | 2997 | if (!entry1->e_value_inum && |
| 2998 | memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2999 | (char *)header2 + le16_to_cpu(entry2->e_value_offs), |
| 3000 | le32_to_cpu(entry1->e_value_size))) |
| 3001 | return 1; |
| 3002 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3003 | entry1 = EXT4_XATTR_NEXT(entry1); |
| 3004 | entry2 = EXT4_XATTR_NEXT(entry2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3005 | } |
| 3006 | if (!IS_LAST_ENTRY(entry2)) |
| 3007 | return 1; |
| 3008 | return 0; |
| 3009 | } |
| 3010 | |
| 3011 | /* |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 3012 | * ext4_xattr_block_cache_find() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3013 | * |
| 3014 | * Find an identical extended attribute block. |
| 3015 | * |
| 3016 | * Returns a pointer to the block found, or NULL if such a block was |
| 3017 | * not found or an error occurred. |
| 3018 | */ |
| 3019 | static struct buffer_head * |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 3020 | ext4_xattr_block_cache_find(struct inode *inode, |
| 3021 | struct ext4_xattr_header *header, |
| 3022 | struct mb_cache_entry **pce) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3023 | { |
| 3024 | __u32 hash = le32_to_cpu(header->h_hash); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 3025 | struct mb_cache_entry *ce; |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 3026 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3027 | |
Tahsin Erdogan | cdb7ee4 | 2017-06-22 11:55:14 -0400 | [diff] [blame] | 3028 | if (!ea_block_cache) |
| 3029 | return NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3030 | if (!header->h_hash) |
| 3031 | return NULL; /* never share */ |
| 3032 | ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 3033 | ce = mb_cache_entry_find_first(ea_block_cache, hash); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3034 | while (ce) { |
| 3035 | struct buffer_head *bh; |
| 3036 | |
Tahsin Erdogan | c07dfcb | 2017-06-22 10:29:53 -0400 | [diff] [blame] | 3037 | bh = sb_bread(inode->i_sb, ce->e_value); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3038 | if (!bh) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 3039 | EXT4_ERROR_INODE(inode, "block %lu read error", |
Tahsin Erdogan | c07dfcb | 2017-06-22 10:29:53 -0400 | [diff] [blame] | 3040 | (unsigned long)ce->e_value); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3041 | } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3042 | *pce = ce; |
| 3043 | return bh; |
| 3044 | } |
| 3045 | brelse(bh); |
Tahsin Erdogan | 4738740 | 2017-06-22 11:28:55 -0400 | [diff] [blame] | 3046 | ce = mb_cache_entry_find_next(ea_block_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3047 | } |
| 3048 | return NULL; |
| 3049 | } |
| 3050 | |
| 3051 | #define NAME_HASH_SHIFT 5 |
| 3052 | #define VALUE_HASH_SHIFT 16 |
| 3053 | |
| 3054 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3055 | * ext4_xattr_hash_entry() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3056 | * |
| 3057 | * Compute the hash of an extended attribute. |
| 3058 | */ |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 3059 | static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value, |
| 3060 | size_t value_count) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3061 | { |
| 3062 | __u32 hash = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3063 | |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 3064 | while (name_len--) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3065 | hash = (hash << NAME_HASH_SHIFT) ^ |
| 3066 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ |
| 3067 | *name++; |
| 3068 | } |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 3069 | while (value_count--) { |
| 3070 | hash = (hash << VALUE_HASH_SHIFT) ^ |
| 3071 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ |
| 3072 | le32_to_cpu(*value++); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3073 | } |
Tahsin Erdogan | b9fc761 | 2017-06-22 11:53:15 -0400 | [diff] [blame] | 3074 | return cpu_to_le32(hash); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | #undef NAME_HASH_SHIFT |
| 3078 | #undef VALUE_HASH_SHIFT |
| 3079 | |
| 3080 | #define BLOCK_HASH_SHIFT 16 |
| 3081 | |
| 3082 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3083 | * ext4_xattr_rehash() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3084 | * |
| 3085 | * Re-compute the extended attribute hash value after an entry has changed. |
| 3086 | */ |
Tahsin Erdogan | daf8328 | 2017-06-22 11:52:03 -0400 | [diff] [blame] | 3087 | static void ext4_xattr_rehash(struct ext4_xattr_header *header) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3088 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3089 | struct ext4_xattr_entry *here; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3090 | __u32 hash = 0; |
| 3091 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3092 | here = ENTRY(header+1); |
| 3093 | while (!IS_LAST_ENTRY(here)) { |
| 3094 | if (!here->e_hash) { |
| 3095 | /* Block is not shared if an entry's hash value == 0 */ |
| 3096 | hash = 0; |
| 3097 | break; |
| 3098 | } |
| 3099 | hash = (hash << BLOCK_HASH_SHIFT) ^ |
| 3100 | (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ |
| 3101 | le32_to_cpu(here->e_hash); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3102 | here = EXT4_XATTR_NEXT(here); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3103 | } |
| 3104 | header->h_hash = cpu_to_le32(hash); |
| 3105 | } |
| 3106 | |
| 3107 | #undef BLOCK_HASH_SHIFT |
| 3108 | |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 3109 | #define HASH_BUCKET_BITS 10 |
| 3110 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 3111 | struct mb_cache * |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 3112 | ext4_xattr_create_cache(void) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3113 | { |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 3114 | return mb_cache_create(HASH_BUCKET_BITS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3115 | } |
| 3116 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 3117 | void ext4_xattr_destroy_cache(struct mb_cache *cache) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3118 | { |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 3119 | if (cache) |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 3120 | mb_cache_destroy(cache); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3121 | } |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 3122 | |