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