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