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