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