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