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