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