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 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 75 | static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 76 | static struct buffer_head *ext4_xattr_cache_find(struct inode *, |
| 77 | struct ext4_xattr_header *, |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 78 | struct mb_cache_entry **); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 79 | static void ext4_xattr_rehash(struct ext4_xattr_header *, |
| 80 | struct ext4_xattr_entry *); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 81 | |
Eric Biggers | d600618 | 2017-04-29 23:47:50 -0400 | [diff] [blame] | 82 | static const struct xattr_handler * const ext4_xattr_handler_map[] = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 83 | [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 84 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 85 | [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler, |
| 86 | [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 87 | #endif |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 88 | [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 89 | #ifdef CONFIG_EXT4_FS_SECURITY |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 90 | [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 91 | #endif |
| 92 | }; |
| 93 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 94 | const struct xattr_handler *ext4_xattr_handlers[] = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 95 | &ext4_xattr_user_handler, |
| 96 | &ext4_xattr_trusted_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 97 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 98 | &posix_acl_access_xattr_handler, |
| 99 | &posix_acl_default_xattr_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 100 | #endif |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 101 | #ifdef CONFIG_EXT4_FS_SECURITY |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 102 | &ext4_xattr_security_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 103 | #endif |
| 104 | NULL |
| 105 | }; |
| 106 | |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 107 | #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \ |
| 108 | inode->i_sb->s_fs_info)->s_mb_cache) |
| 109 | |
Tahsin Erdogan | 33d201e | 2017-06-21 21:17:10 -0400 | [diff] [blame] | 110 | #ifdef CONFIG_LOCKDEP |
| 111 | void ext4_xattr_inode_set_class(struct inode *ea_inode) |
| 112 | { |
| 113 | lockdep_set_subclass(&ea_inode->i_rwsem, 1); |
| 114 | } |
| 115 | #endif |
| 116 | |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 117 | static __le32 ext4_xattr_block_csum(struct inode *inode, |
| 118 | sector_t block_nr, |
| 119 | struct ext4_xattr_header *hdr) |
| 120 | { |
| 121 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 122 | __u32 csum; |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 123 | __le64 dsk_block_nr = cpu_to_le64(block_nr); |
Daeho Jeong | b47820e | 2016-07-03 17:51:39 -0400 | [diff] [blame] | 124 | __u32 dummy_csum = 0; |
| 125 | int offset = offsetof(struct ext4_xattr_header, h_checksum); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 126 | |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 127 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr, |
| 128 | sizeof(dsk_block_nr)); |
Daeho Jeong | b47820e | 2016-07-03 17:51:39 -0400 | [diff] [blame] | 129 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset); |
| 130 | csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum)); |
| 131 | offset += sizeof(dummy_csum); |
| 132 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset, |
| 133 | EXT4_BLOCK_SIZE(inode->i_sb) - offset); |
Tao Ma | 41eb70d | 2012-07-09 16:29:27 -0400 | [diff] [blame] | 134 | |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 135 | return cpu_to_le32(csum); |
| 136 | } |
| 137 | |
| 138 | static int ext4_xattr_block_csum_verify(struct inode *inode, |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 139 | struct buffer_head *bh) |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 140 | { |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 141 | struct ext4_xattr_header *hdr = BHDR(bh); |
| 142 | int ret = 1; |
| 143 | |
| 144 | if (ext4_has_metadata_csum(inode->i_sb)) { |
| 145 | lock_buffer(bh); |
| 146 | ret = (hdr->h_checksum == ext4_xattr_block_csum(inode, |
| 147 | bh->b_blocknr, hdr)); |
| 148 | unlock_buffer(bh); |
| 149 | } |
| 150 | return ret; |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void ext4_xattr_block_csum_set(struct inode *inode, |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 154 | struct buffer_head *bh) |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 155 | { |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 156 | if (ext4_has_metadata_csum(inode->i_sb)) |
| 157 | BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode, |
| 158 | bh->b_blocknr, BHDR(bh)); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 161 | static inline const struct xattr_handler * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 162 | ext4_xattr_handler(int name_index) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 163 | { |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 164 | const struct xattr_handler *handler = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 165 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 166 | if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map)) |
| 167 | handler = ext4_xattr_handler_map[name_index]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 168 | return handler; |
| 169 | } |
| 170 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 171 | static int |
Eric Biggers | 2c4f992 | 2017-04-29 23:56:52 -0400 | [diff] [blame] | 172 | ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end, |
| 173 | void *value_start) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 174 | { |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 175 | struct ext4_xattr_entry *e = entry; |
| 176 | |
Eric Biggers | d7614cc | 2016-12-01 14:57:29 -0500 | [diff] [blame] | 177 | /* Find the end of the names list */ |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 178 | while (!IS_LAST_ENTRY(e)) { |
| 179 | struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 180 | if ((void *)next >= end) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 181 | return -EFSCORRUPTED; |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 182 | e = next; |
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 | |
Eric Biggers | d7614cc | 2016-12-01 14:57:29 -0500 | [diff] [blame] | 185 | /* Check the values */ |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 186 | while (!IS_LAST_ENTRY(entry)) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 187 | if (entry->e_value_size != 0 && |
| 188 | entry->e_value_inum == 0) { |
Eric Biggers | d7614cc | 2016-12-01 14:57:29 -0500 | [diff] [blame] | 189 | u16 offs = le16_to_cpu(entry->e_value_offs); |
| 190 | u32 size = le32_to_cpu(entry->e_value_size); |
| 191 | void *value; |
| 192 | |
| 193 | /* |
| 194 | * The value cannot overlap the names, and the value |
| 195 | * with padding cannot extend beyond 'end'. Check both |
| 196 | * the padded and unpadded sizes, since the size may |
| 197 | * overflow to 0 when adding padding. |
| 198 | */ |
| 199 | if (offs > end - value_start) |
| 200 | return -EFSCORRUPTED; |
| 201 | value = value_start + offs; |
| 202 | if (value < (void *)e + sizeof(u32) || |
| 203 | size > end - value || |
| 204 | EXT4_XATTR_SIZE(size) > end - value) |
| 205 | return -EFSCORRUPTED; |
| 206 | } |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 207 | entry = EXT4_XATTR_NEXT(entry); |
| 208 | } |
| 209 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static inline int |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 214 | ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 215 | { |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 216 | int error; |
| 217 | |
| 218 | if (buffer_verified(bh)) |
| 219 | return 0; |
| 220 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 221 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 222 | BHDR(bh)->h_blocks != cpu_to_le32(1)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 223 | return -EFSCORRUPTED; |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 224 | if (!ext4_xattr_block_csum_verify(inode, bh)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 225 | return -EFSBADCRC; |
Eric Biggers | 2c4f992 | 2017-04-29 23:56:52 -0400 | [diff] [blame] | 226 | error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size, |
| 227 | bh->b_data); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 228 | if (!error) |
| 229 | set_buffer_verified(bh); |
| 230 | return error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 233 | static int |
| 234 | __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header, |
| 235 | void *end, const char *function, unsigned int line) |
| 236 | { |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 237 | int error = -EFSCORRUPTED; |
| 238 | |
Eric Biggers | 290ab23 | 2016-12-01 14:51:58 -0500 | [diff] [blame] | 239 | if (end - (void *)header < sizeof(*header) + sizeof(u32) || |
Eric Biggers | 1996250 | 2016-10-15 09:39:31 -0400 | [diff] [blame] | 240 | (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC))) |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 241 | goto errout; |
Eric Biggers | 2c4f992 | 2017-04-29 23:56:52 -0400 | [diff] [blame] | 242 | error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header)); |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 243 | errout: |
| 244 | if (error) |
| 245 | __ext4_error_inode(inode, function, line, 0, |
| 246 | "corrupted in-inode xattr"); |
| 247 | return error; |
| 248 | } |
| 249 | |
| 250 | #define xattr_check_inode(inode, header, end) \ |
| 251 | __xattr_check_inode((inode), (header), (end), __func__, __LINE__) |
| 252 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 253 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 254 | ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index, |
Eric Biggers | 6ba644b | 2017-04-30 00:01:02 -0400 | [diff] [blame] | 255 | const char *name, int sorted) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 256 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 257 | struct ext4_xattr_entry *entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 258 | size_t name_len; |
| 259 | int cmp = 1; |
| 260 | |
| 261 | if (name == NULL) |
| 262 | return -EINVAL; |
| 263 | name_len = strlen(name); |
| 264 | entry = *pentry; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 265 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 266 | cmp = name_index - entry->e_name_index; |
| 267 | if (!cmp) |
| 268 | cmp = name_len - entry->e_name_len; |
| 269 | if (!cmp) |
| 270 | cmp = memcmp(name, entry->e_name, name_len); |
| 271 | if (cmp <= 0 && (sorted || cmp == 0)) |
| 272 | break; |
| 273 | } |
| 274 | *pentry = entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 275 | return cmp ? -ENODATA : 0; |
| 276 | } |
| 277 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 278 | /* |
| 279 | * Read the EA value from an inode. |
| 280 | */ |
Tahsin Erdogan | 90966693 | 2017-06-21 21:57:36 -0400 | [diff] [blame] | 281 | 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] | 282 | { |
| 283 | unsigned long block = 0; |
| 284 | struct buffer_head *bh = NULL; |
Tahsin Erdogan | 90966693 | 2017-06-21 21:57:36 -0400 | [diff] [blame] | 285 | int blocksize = ea_inode->i_sb->s_blocksize; |
| 286 | size_t csize, copied = 0; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 287 | |
Tahsin Erdogan | 90966693 | 2017-06-21 21:57:36 -0400 | [diff] [blame] | 288 | while (copied < size) { |
| 289 | csize = (size - copied) > blocksize ? blocksize : size - copied; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 290 | bh = ext4_bread(NULL, ea_inode, block, 0); |
Tahsin Erdogan | 90966693 | 2017-06-21 21:57:36 -0400 | [diff] [blame] | 291 | if (IS_ERR(bh)) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 292 | return PTR_ERR(bh); |
Tahsin Erdogan | 90966693 | 2017-06-21 21:57:36 -0400 | [diff] [blame] | 293 | if (!bh) |
| 294 | return -EFSCORRUPTED; |
| 295 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 296 | memcpy(buf, bh->b_data, csize); |
| 297 | brelse(bh); |
| 298 | |
| 299 | buf += csize; |
| 300 | block += 1; |
Tahsin Erdogan | 90966693 | 2017-06-21 21:57:36 -0400 | [diff] [blame] | 301 | copied += csize; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 302 | } |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 306 | static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, |
| 307 | struct inode **ea_inode) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 308 | { |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 309 | struct inode *inode; |
| 310 | int err; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 311 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 312 | inode = ext4_iget(parent->i_sb, ea_ino); |
| 313 | if (IS_ERR(inode)) { |
| 314 | err = PTR_ERR(inode); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 315 | ext4_error(parent->i_sb, "error while reading EA inode %lu " |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 316 | "err=%d", ea_ino, err); |
| 317 | return err; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 320 | if (is_bad_inode(inode)) { |
| 321 | ext4_error(parent->i_sb, "error while reading EA inode %lu " |
| 322 | "is_bad_inode", ea_ino); |
| 323 | err = -EIO; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 324 | goto error; |
| 325 | } |
| 326 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 327 | if (EXT4_XATTR_INODE_GET_PARENT(inode) != parent->i_ino || |
| 328 | inode->i_generation != parent->i_generation) { |
| 329 | ext4_error(parent->i_sb, "Backpointer from EA inode %lu " |
| 330 | "to parent is invalid.", ea_ino); |
| 331 | err = -EINVAL; |
| 332 | goto error; |
| 333 | } |
| 334 | |
| 335 | if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 336 | ext4_error(parent->i_sb, "EA inode %lu does not have " |
| 337 | "EXT4_EA_INODE_FL flag set.\n", ea_ino); |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 338 | err = -EINVAL; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 339 | goto error; |
| 340 | } |
| 341 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 342 | *ea_inode = inode; |
| 343 | return 0; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 344 | error: |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 345 | iput(inode); |
| 346 | return err; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Read the value from the EA inode. |
| 351 | */ |
| 352 | static int |
| 353 | ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer, |
Tahsin Erdogan | 90966693 | 2017-06-21 21:57:36 -0400 | [diff] [blame] | 354 | size_t size) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 355 | { |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 356 | struct inode *ea_inode; |
| 357 | int ret; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 358 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 359 | ret = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode); |
| 360 | if (ret) |
| 361 | return ret; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 362 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 363 | ret = ext4_xattr_inode_read(ea_inode, buffer, size); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 364 | iput(ea_inode); |
| 365 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 366 | return ret; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 369 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 370 | 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] | 371 | void *buffer, size_t buffer_size) |
| 372 | { |
| 373 | struct buffer_head *bh = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 374 | struct ext4_xattr_entry *entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 375 | size_t size; |
| 376 | int error; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 377 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 378 | |
| 379 | ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", |
| 380 | name_index, name, buffer, (long)buffer_size); |
| 381 | |
| 382 | error = -ENODATA; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 383 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 384 | goto cleanup; |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 385 | ea_idebug(inode, "reading block %llu", |
| 386 | (unsigned long long)EXT4_I(inode)->i_file_acl); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 387 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 388 | if (!bh) |
| 389 | goto cleanup; |
| 390 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 391 | 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] | 392 | if (ext4_xattr_check_block(inode, bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 393 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 394 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 395 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 396 | goto cleanup; |
| 397 | } |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 398 | ext4_xattr_cache_insert(ext4_mb_cache, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 399 | entry = BFIRST(bh); |
Eric Biggers | 6ba644b | 2017-04-30 00:01:02 -0400 | [diff] [blame] | 400 | error = ext4_xattr_find_entry(&entry, name_index, name, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 401 | if (error) |
| 402 | goto cleanup; |
| 403 | size = le32_to_cpu(entry->e_value_size); |
| 404 | if (buffer) { |
| 405 | error = -ERANGE; |
| 406 | if (size > buffer_size) |
| 407 | goto cleanup; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 408 | if (entry->e_value_inum) { |
| 409 | error = ext4_xattr_inode_get(inode, |
| 410 | le32_to_cpu(entry->e_value_inum), |
Tahsin Erdogan | 90966693 | 2017-06-21 21:57:36 -0400 | [diff] [blame] | 411 | buffer, size); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 412 | if (error) |
| 413 | goto cleanup; |
| 414 | } else { |
| 415 | memcpy(buffer, bh->b_data + |
| 416 | le16_to_cpu(entry->e_value_offs), size); |
| 417 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 418 | } |
| 419 | error = size; |
| 420 | |
| 421 | cleanup: |
| 422 | brelse(bh); |
| 423 | return error; |
| 424 | } |
| 425 | |
Tao Ma | 879b382 | 2012-12-05 10:28:46 -0500 | [diff] [blame] | 426 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 427 | 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] | 428 | void *buffer, size_t buffer_size) |
| 429 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 430 | struct ext4_xattr_ibody_header *header; |
| 431 | struct ext4_xattr_entry *entry; |
| 432 | struct ext4_inode *raw_inode; |
| 433 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 434 | size_t size; |
| 435 | void *end; |
| 436 | int error; |
| 437 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 438 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 439 | return -ENODATA; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 440 | error = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 441 | if (error) |
| 442 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 443 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 444 | header = IHDR(inode, raw_inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 445 | 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] | 446 | error = xattr_check_inode(inode, header, end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 447 | if (error) |
| 448 | goto cleanup; |
Eric Biggers | 6ba644b | 2017-04-30 00:01:02 -0400 | [diff] [blame] | 449 | entry = IFIRST(header); |
| 450 | error = ext4_xattr_find_entry(&entry, name_index, name, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 451 | if (error) |
| 452 | goto cleanup; |
| 453 | size = le32_to_cpu(entry->e_value_size); |
| 454 | if (buffer) { |
| 455 | error = -ERANGE; |
| 456 | if (size > buffer_size) |
| 457 | goto cleanup; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 458 | if (entry->e_value_inum) { |
| 459 | error = ext4_xattr_inode_get(inode, |
| 460 | le32_to_cpu(entry->e_value_inum), |
Tahsin Erdogan | 90966693 | 2017-06-21 21:57:36 -0400 | [diff] [blame] | 461 | buffer, size); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 462 | if (error) |
| 463 | goto cleanup; |
| 464 | } else { |
| 465 | memcpy(buffer, (void *)IFIRST(header) + |
| 466 | le16_to_cpu(entry->e_value_offs), size); |
| 467 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 468 | } |
| 469 | error = size; |
| 470 | |
| 471 | cleanup: |
| 472 | brelse(iloc.bh); |
| 473 | return error; |
| 474 | } |
| 475 | |
| 476 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 477 | * ext4_xattr_get() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 478 | * |
| 479 | * Copy an extended attribute into the buffer |
| 480 | * provided, or compute the buffer size required. |
| 481 | * Buffer is NULL to compute the size of the buffer required. |
| 482 | * |
| 483 | * Returns a negative error number on failure, or the number of bytes |
| 484 | * used / required on success. |
| 485 | */ |
| 486 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 487 | ext4_xattr_get(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 488 | void *buffer, size_t buffer_size) |
| 489 | { |
| 490 | int error; |
| 491 | |
Theodore Ts'o | 0db1ff2 | 2017-02-05 01:28:48 -0500 | [diff] [blame] | 492 | if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) |
| 493 | return -EIO; |
| 494 | |
Zhang Zhen | 230b8c1a | 2014-05-12 09:57:59 -0400 | [diff] [blame] | 495 | if (strlen(name) > 255) |
| 496 | return -ERANGE; |
| 497 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 498 | down_read(&EXT4_I(inode)->xattr_sem); |
| 499 | error = ext4_xattr_ibody_get(inode, name_index, name, buffer, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 500 | buffer_size); |
| 501 | if (error == -ENODATA) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 502 | error = ext4_xattr_block_get(inode, name_index, name, buffer, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 503 | buffer_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 504 | up_read(&EXT4_I(inode)->xattr_sem); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 505 | return error; |
| 506 | } |
| 507 | |
| 508 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 509 | ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 510 | char *buffer, size_t buffer_size) |
| 511 | { |
| 512 | size_t rest = buffer_size; |
| 513 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 514 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 515 | const struct xattr_handler *handler = |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 516 | ext4_xattr_handler(entry->e_name_index); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 517 | |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 518 | if (handler && (!handler->list || handler->list(dentry))) { |
| 519 | const char *prefix = handler->prefix ?: handler->name; |
| 520 | size_t prefix_len = strlen(prefix); |
| 521 | size_t size = prefix_len + entry->e_name_len + 1; |
| 522 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 523 | if (buffer) { |
| 524 | if (size > rest) |
| 525 | return -ERANGE; |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 526 | memcpy(buffer, prefix, prefix_len); |
| 527 | buffer += prefix_len; |
| 528 | memcpy(buffer, entry->e_name, entry->e_name_len); |
| 529 | buffer += entry->e_name_len; |
| 530 | *buffer++ = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 531 | } |
| 532 | rest -= size; |
| 533 | } |
| 534 | } |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 535 | return buffer_size - rest; /* total size */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 539 | 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] | 540 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 541 | struct inode *inode = d_inode(dentry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 542 | struct buffer_head *bh = NULL; |
| 543 | int error; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 544 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 545 | |
| 546 | ea_idebug(inode, "buffer=%p, buffer_size=%ld", |
| 547 | buffer, (long)buffer_size); |
| 548 | |
| 549 | error = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 550 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 551 | goto cleanup; |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 552 | ea_idebug(inode, "reading block %llu", |
| 553 | (unsigned long long)EXT4_I(inode)->i_file_acl); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 554 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 555 | error = -EIO; |
| 556 | if (!bh) |
| 557 | goto cleanup; |
| 558 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 559 | 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] | 560 | if (ext4_xattr_check_block(inode, bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 561 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 562 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 563 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 564 | goto cleanup; |
| 565 | } |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 566 | ext4_xattr_cache_insert(ext4_mb_cache, bh); |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 567 | error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 568 | |
| 569 | cleanup: |
| 570 | brelse(bh); |
| 571 | |
| 572 | return error; |
| 573 | } |
| 574 | |
| 575 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 576 | 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] | 577 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 578 | struct inode *inode = d_inode(dentry); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 579 | struct ext4_xattr_ibody_header *header; |
| 580 | struct ext4_inode *raw_inode; |
| 581 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 582 | void *end; |
| 583 | int error; |
| 584 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 585 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 586 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 587 | error = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 588 | if (error) |
| 589 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 590 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 591 | header = IHDR(inode, raw_inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 592 | 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] | 593 | error = xattr_check_inode(inode, header, end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 594 | if (error) |
| 595 | goto cleanup; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 596 | error = ext4_xattr_list_entries(dentry, IFIRST(header), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 597 | buffer, buffer_size); |
| 598 | |
| 599 | cleanup: |
| 600 | brelse(iloc.bh); |
| 601 | return error; |
| 602 | } |
| 603 | |
| 604 | /* |
Eric Biggers | ba7ea1d | 2017-04-29 23:53:17 -0400 | [diff] [blame] | 605 | * Inode operation listxattr() |
| 606 | * |
| 607 | * d_inode(dentry)->i_rwsem: don't care |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 608 | * |
| 609 | * Copy a list of attribute names into the buffer |
| 610 | * provided, or compute the buffer size required. |
| 611 | * Buffer is NULL to compute the size of the buffer required. |
| 612 | * |
| 613 | * Returns a negative error number on failure, or the number of bytes |
| 614 | * used / required on success. |
| 615 | */ |
Eric Biggers | ba7ea1d | 2017-04-29 23:53:17 -0400 | [diff] [blame] | 616 | ssize_t |
| 617 | ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 618 | { |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 619 | int ret, ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 620 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 621 | down_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 622 | ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size); |
| 623 | if (ret < 0) |
| 624 | goto errout; |
| 625 | if (buffer) { |
| 626 | buffer += ret; |
| 627 | buffer_size -= ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 628 | } |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 629 | ret = ext4_xattr_block_list(dentry, buffer, buffer_size); |
| 630 | if (ret < 0) |
| 631 | goto errout; |
| 632 | ret += ret2; |
| 633 | errout: |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 634 | up_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 635 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 639 | * 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] | 640 | * not set, set it. |
| 641 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 642 | static void ext4_xattr_update_super_block(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 643 | struct super_block *sb) |
| 644 | { |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 645 | if (ext4_has_feature_xattr(sb)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 646 | return; |
| 647 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 648 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 649 | 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] | 650 | ext4_set_feature_xattr(sb); |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 651 | ext4_handle_dirty_super(handle, sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 652 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /* |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 656 | * Release the xattr block BH: If the reference count is > 1, decrement it; |
| 657 | * otherwise free the block. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 658 | */ |
| 659 | static void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 660 | ext4_xattr_release_block(handle_t *handle, struct inode *inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 661 | struct buffer_head *bh) |
| 662 | { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 663 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
| 664 | u32 hash, ref; |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 665 | int error = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 666 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 667 | BUFFER_TRACE(bh, "get_write_access"); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 668 | error = ext4_journal_get_write_access(handle, bh); |
| 669 | if (error) |
| 670 | goto out; |
| 671 | |
| 672 | lock_buffer(bh); |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 673 | hash = le32_to_cpu(BHDR(bh)->h_hash); |
| 674 | ref = le32_to_cpu(BHDR(bh)->h_refcount); |
| 675 | if (ref == 1) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 676 | ea_bdebug(bh, "refcount now=0; freeing"); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 677 | /* |
| 678 | * This must happen under buffer lock for |
| 679 | * ext4_xattr_block_set() to reliably detect freed block |
| 680 | */ |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 681 | mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 682 | get_bh(bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 683 | unlock_buffer(bh); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 684 | ext4_free_blocks(handle, inode, bh, 0, 1, |
| 685 | EXT4_FREE_BLOCKS_METADATA | |
| 686 | EXT4_FREE_BLOCKS_FORGET); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 687 | } else { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 688 | ref--; |
| 689 | BHDR(bh)->h_refcount = cpu_to_le32(ref); |
| 690 | if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) { |
| 691 | struct mb_cache_entry *ce; |
| 692 | |
| 693 | ce = mb_cache_entry_get(ext4_mb_cache, hash, |
| 694 | bh->b_blocknr); |
| 695 | if (ce) { |
| 696 | ce->e_reusable = 1; |
| 697 | mb_cache_entry_put(ext4_mb_cache, ce); |
| 698 | } |
| 699 | } |
| 700 | |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 701 | ext4_xattr_block_csum_set(inode, bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 702 | /* |
| 703 | * Beware of this ugliness: Releasing of xattr block references |
| 704 | * from different inodes can race and so we have to protect |
| 705 | * from a race where someone else frees the block (and releases |
| 706 | * its journal_head) before we are done dirtying the buffer. In |
| 707 | * nojournal mode this race is harmless and we actually cannot |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 708 | * call ext4_handle_dirty_metadata() with locked buffer as |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 709 | * that function can call sync_dirty_buffer() so for that case |
| 710 | * we handle the dirtying after unlocking the buffer. |
| 711 | */ |
| 712 | if (ext4_handle_valid(handle)) |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 713 | error = ext4_handle_dirty_metadata(handle, inode, bh); |
Eric Sandeen | c1bb05a | 2012-02-20 23:06:18 -0500 | [diff] [blame] | 714 | unlock_buffer(bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 715 | if (!ext4_handle_valid(handle)) |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 716 | error = ext4_handle_dirty_metadata(handle, inode, bh); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 717 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 718 | ext4_handle_sync(handle); |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 719 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1)); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 720 | ea_bdebug(bh, "refcount now=%d; releasing", |
| 721 | le32_to_cpu(BHDR(bh)->h_refcount)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 722 | } |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 723 | out: |
| 724 | ext4_std_error(inode->i_sb, error); |
| 725 | return; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 728 | /* |
| 729 | * Find the available free space for EAs. This also returns the total number of |
| 730 | * bytes used by EA entries. |
| 731 | */ |
| 732 | static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last, |
| 733 | size_t *min_offs, void *base, int *total) |
| 734 | { |
| 735 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 736 | if (!last->e_value_inum && last->e_value_size) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 737 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 738 | if (offs < *min_offs) |
| 739 | *min_offs = offs; |
| 740 | } |
Theodore Ts'o | 7b1b2c1 | 2014-02-19 20:15:21 -0500 | [diff] [blame] | 741 | if (total) |
| 742 | *total += EXT4_XATTR_LEN(last->e_name_len); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 743 | } |
| 744 | return (*min_offs - ((void *)last - base) - sizeof(__u32)); |
| 745 | } |
| 746 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 747 | /* |
| 748 | * Write the value of the EA in an inode. |
| 749 | */ |
| 750 | static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode, |
| 751 | const void *buf, int bufsize) |
| 752 | { |
| 753 | struct buffer_head *bh = NULL; |
| 754 | unsigned long block = 0; |
| 755 | unsigned blocksize = ea_inode->i_sb->s_blocksize; |
| 756 | unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits; |
| 757 | int csize, wsize = 0; |
| 758 | int ret = 0; |
| 759 | int retries = 0; |
| 760 | |
| 761 | retry: |
| 762 | while (ret >= 0 && ret < max_blocks) { |
| 763 | struct ext4_map_blocks map; |
| 764 | map.m_lblk = block += ret; |
| 765 | map.m_len = max_blocks -= ret; |
| 766 | |
| 767 | ret = ext4_map_blocks(handle, ea_inode, &map, |
| 768 | EXT4_GET_BLOCKS_CREATE); |
| 769 | if (ret <= 0) { |
| 770 | ext4_mark_inode_dirty(handle, ea_inode); |
| 771 | if (ret == -ENOSPC && |
| 772 | ext4_should_retry_alloc(ea_inode->i_sb, &retries)) { |
| 773 | ret = 0; |
| 774 | goto retry; |
| 775 | } |
| 776 | break; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | if (ret < 0) |
| 781 | return ret; |
| 782 | |
| 783 | block = 0; |
| 784 | while (wsize < bufsize) { |
| 785 | if (bh != NULL) |
| 786 | brelse(bh); |
| 787 | csize = (bufsize - wsize) > blocksize ? blocksize : |
| 788 | bufsize - wsize; |
| 789 | bh = ext4_getblk(handle, ea_inode, block, 0); |
| 790 | if (IS_ERR(bh)) |
| 791 | return PTR_ERR(bh); |
| 792 | ret = ext4_journal_get_write_access(handle, bh); |
| 793 | if (ret) |
| 794 | goto out; |
| 795 | |
| 796 | memcpy(bh->b_data, buf, csize); |
| 797 | set_buffer_uptodate(bh); |
| 798 | ext4_handle_dirty_metadata(handle, ea_inode, bh); |
| 799 | |
| 800 | buf += csize; |
| 801 | wsize += csize; |
| 802 | block += 1; |
| 803 | } |
| 804 | |
| 805 | inode_lock(ea_inode); |
| 806 | i_size_write(ea_inode, wsize); |
| 807 | ext4_update_i_disksize(ea_inode, wsize); |
| 808 | inode_unlock(ea_inode); |
| 809 | |
| 810 | ext4_mark_inode_dirty(handle, ea_inode); |
| 811 | |
| 812 | out: |
| 813 | brelse(bh); |
| 814 | |
| 815 | return ret; |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * Create an inode to store the value of a large EA. |
| 820 | */ |
| 821 | static struct inode *ext4_xattr_inode_create(handle_t *handle, |
| 822 | struct inode *inode) |
| 823 | { |
| 824 | struct inode *ea_inode = NULL; |
Tahsin Erdogan | 9e1ba00 | 2017-06-21 21:27:00 -0400 | [diff] [blame] | 825 | uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) }; |
Tahsin Erdogan | bd3b963 | 2017-06-21 21:24:31 -0400 | [diff] [blame] | 826 | int err; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 827 | |
| 828 | /* |
| 829 | * Let the next inode be the goal, so we try and allocate the EA inode |
| 830 | * in the same group, or nearby one. |
| 831 | */ |
| 832 | 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] | 833 | S_IFREG | 0600, NULL, inode->i_ino + 1, owner, |
Tahsin Erdogan | 1b917ed | 2017-06-21 21:21:39 -0400 | [diff] [blame] | 834 | EXT4_EA_INODE_FL); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 835 | if (!IS_ERR(ea_inode)) { |
| 836 | ea_inode->i_op = &ext4_file_inode_operations; |
| 837 | ea_inode->i_fop = &ext4_file_operations; |
| 838 | ext4_set_aops(ea_inode); |
Tahsin Erdogan | 33d201e | 2017-06-21 21:17:10 -0400 | [diff] [blame] | 839 | ext4_xattr_inode_set_class(ea_inode); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 840 | ea_inode->i_generation = inode->i_generation; |
| 841 | EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL; |
| 842 | |
| 843 | /* |
| 844 | * A back-pointer from EA inode to parent inode will be useful |
| 845 | * for e2fsck. |
| 846 | */ |
| 847 | EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino); |
| 848 | unlock_new_inode(ea_inode); |
Tahsin Erdogan | bd3b963 | 2017-06-21 21:24:31 -0400 | [diff] [blame] | 849 | err = ext4_inode_attach_jinode(ea_inode); |
| 850 | if (err) { |
| 851 | iput(ea_inode); |
| 852 | return ERR_PTR(err); |
| 853 | } |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | return ea_inode; |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Unlink the inode storing the value of the EA. |
| 861 | */ |
| 862 | int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino) |
| 863 | { |
| 864 | struct inode *ea_inode = NULL; |
| 865 | int err; |
| 866 | |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 867 | err = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 868 | if (err) |
| 869 | return err; |
| 870 | |
| 871 | clear_nlink(ea_inode); |
| 872 | iput(ea_inode); |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * Add value of the EA in an inode. |
| 879 | */ |
| 880 | static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode, |
| 881 | unsigned long *ea_ino, const void *value, |
| 882 | size_t value_len) |
| 883 | { |
| 884 | struct inode *ea_inode; |
| 885 | int err; |
| 886 | |
| 887 | /* Create an inode for the EA value */ |
| 888 | ea_inode = ext4_xattr_inode_create(handle, inode); |
| 889 | if (IS_ERR(ea_inode)) |
| 890 | return PTR_ERR(ea_inode); |
| 891 | |
| 892 | err = ext4_xattr_inode_write(handle, ea_inode, value, value_len); |
| 893 | if (err) |
| 894 | clear_nlink(ea_inode); |
| 895 | else |
| 896 | *ea_ino = ea_inode->i_ino; |
| 897 | |
| 898 | iput(ea_inode); |
| 899 | |
| 900 | return err; |
| 901 | } |
| 902 | |
| 903 | static int ext4_xattr_set_entry(struct ext4_xattr_info *i, |
| 904 | struct ext4_xattr_search *s, |
| 905 | handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 906 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 907 | struct ext4_xattr_entry *last; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 908 | size_t free, min_offs = s->end - s->base, name_len = strlen(i->name); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 909 | int in_inode = i->in_inode; |
| 910 | int rc; |
| 911 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 912 | /* Compute min_offs and last. */ |
| 913 | last = s->first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 914 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 915 | if (!last->e_value_inum && last->e_value_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 916 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 917 | if (offs < min_offs) |
| 918 | min_offs = offs; |
| 919 | } |
| 920 | } |
| 921 | free = min_offs - ((void *)last - s->base) - sizeof(__u32); |
| 922 | if (!s->not_found) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 923 | if (!in_inode && |
| 924 | !s->here->e_value_inum && s->here->e_value_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 925 | size_t size = le32_to_cpu(s->here->e_value_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 926 | free += EXT4_XATTR_SIZE(size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 927 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 928 | free += EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 929 | } |
| 930 | if (i->value) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 931 | size_t value_len = EXT4_XATTR_SIZE(i->value_len); |
| 932 | |
| 933 | if (in_inode) |
| 934 | value_len = 0; |
| 935 | |
| 936 | if (free < EXT4_XATTR_LEN(name_len) + value_len) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 937 | return -ENOSPC; |
| 938 | } |
| 939 | |
| 940 | if (i->value && s->not_found) { |
| 941 | /* Insert the new name. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 942 | size_t size = EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 943 | size_t rest = (void *)last - (void *)s->here + sizeof(__u32); |
| 944 | memmove((void *)s->here + size, s->here, rest); |
| 945 | memset(s->here, 0, size); |
| 946 | s->here->e_name_index = i->name_index; |
| 947 | s->here->e_name_len = name_len; |
| 948 | memcpy(s->here->e_name, i->name, name_len); |
| 949 | } else { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 950 | if (!s->here->e_value_inum && s->here->e_value_size && |
| 951 | s->here->e_value_offs > 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 952 | void *first_val = s->base + min_offs; |
| 953 | size_t offs = le16_to_cpu(s->here->e_value_offs); |
| 954 | void *val = s->base + offs; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 955 | size_t size = EXT4_XATTR_SIZE( |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 956 | le32_to_cpu(s->here->e_value_size)); |
| 957 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 958 | if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 959 | /* The old and the new value have the same |
| 960 | size. Just replace. */ |
| 961 | s->here->e_value_size = |
| 962 | cpu_to_le32(i->value_len); |
Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 963 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
| 964 | memset(val, 0, size); |
| 965 | } else { |
| 966 | /* Clear pad bytes first. */ |
| 967 | memset(val + size - EXT4_XATTR_PAD, 0, |
| 968 | EXT4_XATTR_PAD); |
| 969 | memcpy(val, i->value, i->value_len); |
| 970 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | /* Remove the old value. */ |
| 975 | memmove(first_val + size, first_val, val - first_val); |
| 976 | memset(first_val, 0, size); |
| 977 | s->here->e_value_size = 0; |
| 978 | s->here->e_value_offs = 0; |
| 979 | min_offs += size; |
| 980 | |
| 981 | /* Adjust all value offsets. */ |
| 982 | last = s->first; |
| 983 | while (!IS_LAST_ENTRY(last)) { |
| 984 | size_t o = le16_to_cpu(last->e_value_offs); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 985 | if (!last->e_value_inum && |
| 986 | last->e_value_size && o < offs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 987 | last->e_value_offs = |
| 988 | cpu_to_le16(o + size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 989 | last = EXT4_XATTR_NEXT(last); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 990 | } |
| 991 | } |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 992 | if (s->here->e_value_inum) { |
| 993 | ext4_xattr_inode_unlink(inode, |
| 994 | le32_to_cpu(s->here->e_value_inum)); |
| 995 | s->here->e_value_inum = 0; |
| 996 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 997 | if (!i->value) { |
| 998 | /* Remove the old name. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 999 | size_t size = EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1000 | last = ENTRY((void *)last - size); |
| 1001 | memmove(s->here, (void *)s->here + size, |
| 1002 | (void *)last - (void *)s->here + sizeof(__u32)); |
| 1003 | memset(last, 0, size); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | if (i->value) { |
| 1008 | /* Insert the new value. */ |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1009 | if (in_inode) { |
| 1010 | unsigned long ea_ino = |
| 1011 | le32_to_cpu(s->here->e_value_inum); |
| 1012 | rc = ext4_xattr_inode_set(handle, inode, &ea_ino, |
| 1013 | i->value, i->value_len); |
| 1014 | if (rc) |
| 1015 | goto out; |
| 1016 | s->here->e_value_inum = cpu_to_le32(ea_ino); |
| 1017 | s->here->e_value_offs = 0; |
| 1018 | } else if (i->value_len) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1019 | size_t size = EXT4_XATTR_SIZE(i->value_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1020 | void *val = s->base + min_offs - size; |
| 1021 | s->here->e_value_offs = cpu_to_le16(min_offs - size); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1022 | s->here->e_value_inum = 0; |
Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 1023 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
| 1024 | memset(val, 0, size); |
| 1025 | } else { |
| 1026 | /* Clear the pad bytes first. */ |
| 1027 | memset(val + size - EXT4_XATTR_PAD, 0, |
| 1028 | EXT4_XATTR_PAD); |
| 1029 | memcpy(val, i->value, i->value_len); |
| 1030 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1031 | } |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1032 | s->here->e_value_size = cpu_to_le32(i->value_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1033 | } |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1034 | |
| 1035 | out: |
| 1036 | return rc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1039 | struct ext4_xattr_block_find { |
| 1040 | struct ext4_xattr_search s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1041 | struct buffer_head *bh; |
| 1042 | }; |
| 1043 | |
| 1044 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1045 | ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, |
| 1046 | struct ext4_xattr_block_find *bs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1047 | { |
| 1048 | struct super_block *sb = inode->i_sb; |
| 1049 | int error; |
| 1050 | |
| 1051 | ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", |
| 1052 | i->name_index, i->name, i->value, (long)i->value_len); |
| 1053 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1054 | if (EXT4_I(inode)->i_file_acl) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1055 | /* The inode already has an extended attribute block. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1056 | bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1057 | error = -EIO; |
| 1058 | if (!bs->bh) |
| 1059 | goto cleanup; |
| 1060 | ea_bdebug(bs->bh, "b_count=%d, refcount=%d", |
| 1061 | atomic_read(&(bs->bh->b_count)), |
| 1062 | le32_to_cpu(BHDR(bs->bh)->h_refcount)); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 1063 | if (ext4_xattr_check_block(inode, bs->bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1064 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 1065 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1066 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1067 | goto cleanup; |
| 1068 | } |
| 1069 | /* Find the named attribute. */ |
| 1070 | bs->s.base = BHDR(bs->bh); |
| 1071 | bs->s.first = BFIRST(bs->bh); |
| 1072 | bs->s.end = bs->bh->b_data + bs->bh->b_size; |
| 1073 | bs->s.here = bs->s.first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1074 | error = ext4_xattr_find_entry(&bs->s.here, i->name_index, |
Eric Biggers | 6ba644b | 2017-04-30 00:01:02 -0400 | [diff] [blame] | 1075 | i->name, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1076 | if (error && error != -ENODATA) |
| 1077 | goto cleanup; |
| 1078 | bs->s.not_found = error; |
| 1079 | } |
| 1080 | error = 0; |
| 1081 | |
| 1082 | cleanup: |
| 1083 | return error; |
| 1084 | } |
| 1085 | |
| 1086 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1087 | ext4_xattr_block_set(handle_t *handle, struct inode *inode, |
| 1088 | struct ext4_xattr_info *i, |
| 1089 | struct ext4_xattr_block_find *bs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1090 | { |
| 1091 | struct super_block *sb = inode->i_sb; |
| 1092 | struct buffer_head *new_bh = NULL; |
Tahsin Erdogan | b347e2b | 2017-06-21 22:20:32 -0400 | [diff] [blame] | 1093 | struct ext4_xattr_search s_copy = bs->s; |
| 1094 | struct ext4_xattr_search *s = &s_copy; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1095 | struct mb_cache_entry *ce = NULL; |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1096 | int error = 0; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1097 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1098 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1099 | #define header(x) ((struct ext4_xattr_header *)(x)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1100 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1101 | if (s->base) { |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 1102 | BUFFER_TRACE(bs->bh, "get_write_access"); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1103 | error = ext4_journal_get_write_access(handle, bs->bh); |
| 1104 | if (error) |
| 1105 | goto cleanup; |
| 1106 | lock_buffer(bs->bh); |
| 1107 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1108 | if (header(s->base)->h_refcount == cpu_to_le32(1)) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1109 | __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash); |
| 1110 | |
| 1111 | /* |
| 1112 | * This must happen under buffer lock for |
| 1113 | * ext4_xattr_block_set() to reliably detect modified |
| 1114 | * block |
| 1115 | */ |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1116 | mb_cache_entry_delete_block(ext4_mb_cache, hash, |
| 1117 | bs->bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1118 | ea_bdebug(bs->bh, "modifying in-place"); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1119 | error = ext4_xattr_set_entry(i, s, handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1120 | if (!error) { |
| 1121 | if (!IS_LAST_ENTRY(s->first)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1122 | ext4_xattr_rehash(header(s->base), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1123 | s->here); |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 1124 | ext4_xattr_cache_insert(ext4_mb_cache, |
| 1125 | bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1126 | } |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1127 | ext4_xattr_block_csum_set(inode, bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1128 | unlock_buffer(bs->bh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1129 | if (error == -EFSCORRUPTED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1130 | goto bad_block; |
| 1131 | if (!error) |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1132 | error = ext4_handle_dirty_metadata(handle, |
| 1133 | inode, |
| 1134 | bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1135 | if (error) |
| 1136 | goto cleanup; |
| 1137 | goto inserted; |
| 1138 | } else { |
| 1139 | int offset = (char *)s->here - bs->bh->b_data; |
| 1140 | |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 1141 | unlock_buffer(bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1142 | ea_bdebug(bs->bh, "cloning"); |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 1143 | s->base = kmalloc(bs->bh->b_size, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1144 | error = -ENOMEM; |
| 1145 | if (s->base == NULL) |
| 1146 | goto cleanup; |
| 1147 | memcpy(s->base, BHDR(bs->bh), bs->bh->b_size); |
| 1148 | s->first = ENTRY(header(s->base)+1); |
| 1149 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 1150 | s->here = ENTRY(s->base + offset); |
| 1151 | s->end = s->base + bs->bh->b_size; |
| 1152 | } |
| 1153 | } else { |
| 1154 | /* Allocate a buffer where we construct the new block. */ |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 1155 | s->base = kzalloc(sb->s_blocksize, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1156 | /* assert(header == s->base) */ |
| 1157 | error = -ENOMEM; |
| 1158 | if (s->base == NULL) |
| 1159 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1160 | header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1161 | header(s->base)->h_blocks = cpu_to_le32(1); |
| 1162 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 1163 | s->first = ENTRY(header(s->base)+1); |
| 1164 | s->here = ENTRY(header(s->base)+1); |
| 1165 | s->end = s->base + sb->s_blocksize; |
| 1166 | } |
| 1167 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1168 | error = ext4_xattr_set_entry(i, s, handle, inode); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1169 | if (error == -EFSCORRUPTED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1170 | goto bad_block; |
| 1171 | if (error) |
| 1172 | goto cleanup; |
| 1173 | if (!IS_LAST_ENTRY(s->first)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1174 | ext4_xattr_rehash(header(s->base), s->here); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1175 | |
| 1176 | inserted: |
| 1177 | if (!IS_LAST_ENTRY(s->first)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1178 | new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1179 | if (new_bh) { |
| 1180 | /* We found an identical block in the cache. */ |
| 1181 | if (new_bh == bs->bh) |
| 1182 | ea_bdebug(new_bh, "keeping"); |
| 1183 | else { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1184 | u32 ref; |
| 1185 | |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 1186 | WARN_ON_ONCE(dquot_initialize_needed(inode)); |
| 1187 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1188 | /* The old block is released after updating |
| 1189 | the inode. */ |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 1190 | error = dquot_alloc_block(inode, |
| 1191 | EXT4_C2B(EXT4_SB(sb), 1)); |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 1192 | if (error) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1193 | goto cleanup; |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 1194 | BUFFER_TRACE(new_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1195 | error = ext4_journal_get_write_access(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1196 | new_bh); |
| 1197 | if (error) |
| 1198 | goto cleanup_dquot; |
| 1199 | lock_buffer(new_bh); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1200 | /* |
| 1201 | * We have to be careful about races with |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1202 | * freeing, rehashing or adding references to |
| 1203 | * xattr block. Once we hold buffer lock xattr |
| 1204 | * block's state is stable so we can check |
| 1205 | * whether the block got freed / rehashed or |
| 1206 | * not. Since we unhash mbcache entry under |
| 1207 | * buffer lock when freeing / rehashing xattr |
| 1208 | * block, checking whether entry is still |
| 1209 | * hashed is reliable. Same rules hold for |
| 1210 | * e_reusable handling. |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1211 | */ |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1212 | if (hlist_bl_unhashed(&ce->e_hash_list) || |
| 1213 | !ce->e_reusable) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1214 | /* |
| 1215 | * Undo everything and check mbcache |
| 1216 | * again. |
| 1217 | */ |
| 1218 | unlock_buffer(new_bh); |
| 1219 | dquot_free_block(inode, |
| 1220 | EXT4_C2B(EXT4_SB(sb), |
| 1221 | 1)); |
| 1222 | brelse(new_bh); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1223 | mb_cache_entry_put(ext4_mb_cache, ce); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1224 | ce = NULL; |
| 1225 | new_bh = NULL; |
| 1226 | goto inserted; |
| 1227 | } |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1228 | ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1; |
| 1229 | BHDR(new_bh)->h_refcount = cpu_to_le32(ref); |
| 1230 | if (ref >= EXT4_XATTR_REFCOUNT_MAX) |
| 1231 | ce->e_reusable = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1232 | ea_bdebug(new_bh, "reusing; refcount now=%d", |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1233 | ref); |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1234 | ext4_xattr_block_csum_set(inode, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1235 | unlock_buffer(new_bh); |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1236 | error = ext4_handle_dirty_metadata(handle, |
| 1237 | inode, |
| 1238 | new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1239 | if (error) |
| 1240 | goto cleanup_dquot; |
| 1241 | } |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1242 | mb_cache_entry_touch(ext4_mb_cache, ce); |
| 1243 | mb_cache_entry_put(ext4_mb_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1244 | ce = NULL; |
| 1245 | } else if (bs->bh && s->base == bs->bh->b_data) { |
| 1246 | /* We were modifying this block in-place. */ |
| 1247 | ea_bdebug(bs->bh, "keeping this block"); |
| 1248 | new_bh = bs->bh; |
| 1249 | get_bh(new_bh); |
| 1250 | } else { |
| 1251 | /* We need to allocate a new block */ |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 1252 | ext4_fsblk_t goal, block; |
| 1253 | |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 1254 | WARN_ON_ONCE(dquot_initialize_needed(inode)); |
| 1255 | |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 1256 | goal = ext4_group_first_block_no(sb, |
Akinobu Mita | d00a6d7 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1257 | EXT4_I(inode)->i_block_group); |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 1258 | |
| 1259 | /* non-extent files can't have physical blocks past 2^32 */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 1260 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 1261 | goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; |
| 1262 | |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1263 | block = ext4_new_meta_blocks(handle, inode, goal, 0, |
| 1264 | NULL, &error); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1265 | if (error) |
| 1266 | goto cleanup; |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 1267 | |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 1268 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 1269 | BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS); |
| 1270 | |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 1271 | ea_idebug(inode, "creating block %llu", |
| 1272 | (unsigned long long)block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1273 | |
| 1274 | new_bh = sb_getblk(sb, block); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1275 | if (unlikely(!new_bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1276 | error = -ENOMEM; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1277 | getblk_failed: |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 1278 | ext4_free_blocks(handle, inode, NULL, block, 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 1279 | EXT4_FREE_BLOCKS_METADATA); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1280 | goto cleanup; |
| 1281 | } |
| 1282 | lock_buffer(new_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1283 | error = ext4_journal_get_create_access(handle, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1284 | if (error) { |
| 1285 | unlock_buffer(new_bh); |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1286 | error = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1287 | goto getblk_failed; |
| 1288 | } |
| 1289 | memcpy(new_bh->b_data, s->base, new_bh->b_size); |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1290 | ext4_xattr_block_csum_set(inode, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1291 | set_buffer_uptodate(new_bh); |
| 1292 | unlock_buffer(new_bh); |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 1293 | ext4_xattr_cache_insert(ext4_mb_cache, new_bh); |
Theodore Ts'o | dac7a4b | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1294 | error = ext4_handle_dirty_metadata(handle, inode, |
| 1295 | new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1296 | if (error) |
| 1297 | goto cleanup; |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | /* Update the inode. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1302 | 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] | 1303 | |
| 1304 | /* Drop the previous xattr block. */ |
| 1305 | if (bs->bh && bs->bh != new_bh) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1306 | ext4_xattr_release_block(handle, inode, bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1307 | error = 0; |
| 1308 | |
| 1309 | cleanup: |
| 1310 | if (ce) |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1311 | mb_cache_entry_put(ext4_mb_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1312 | brelse(new_bh); |
| 1313 | if (!(bs->bh && s->base == bs->bh->b_data)) |
| 1314 | kfree(s->base); |
| 1315 | |
| 1316 | return error; |
| 1317 | |
| 1318 | cleanup_dquot: |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 1319 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1320 | goto cleanup; |
| 1321 | |
| 1322 | bad_block: |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1323 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 1324 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1325 | goto cleanup; |
| 1326 | |
| 1327 | #undef header |
| 1328 | } |
| 1329 | |
Tao Ma | 879b382 | 2012-12-05 10:28:46 -0500 | [diff] [blame] | 1330 | int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, |
| 1331 | struct ext4_xattr_ibody_find *is) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1332 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1333 | struct ext4_xattr_ibody_header *header; |
| 1334 | struct ext4_inode *raw_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1335 | int error; |
| 1336 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1337 | if (EXT4_I(inode)->i_extra_isize == 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1338 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1339 | raw_inode = ext4_raw_inode(&is->iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1340 | header = IHDR(inode, raw_inode); |
| 1341 | is->s.base = is->s.first = IFIRST(header); |
| 1342 | is->s.here = is->s.first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1343 | 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] | 1344 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 1345 | error = xattr_check_inode(inode, header, is->s.end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1346 | if (error) |
| 1347 | return error; |
| 1348 | /* Find the named attribute. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1349 | error = ext4_xattr_find_entry(&is->s.here, i->name_index, |
Eric Biggers | 6ba644b | 2017-04-30 00:01:02 -0400 | [diff] [blame] | 1350 | i->name, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1351 | if (error && error != -ENODATA) |
| 1352 | return error; |
| 1353 | is->s.not_found = error; |
| 1354 | } |
| 1355 | return 0; |
| 1356 | } |
| 1357 | |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 1358 | int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode, |
| 1359 | struct ext4_xattr_info *i, |
| 1360 | struct ext4_xattr_ibody_find *is) |
| 1361 | { |
| 1362 | struct ext4_xattr_ibody_header *header; |
| 1363 | struct ext4_xattr_search *s = &is->s; |
| 1364 | int error; |
| 1365 | |
| 1366 | if (EXT4_I(inode)->i_extra_isize == 0) |
| 1367 | return -ENOSPC; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1368 | error = ext4_xattr_set_entry(i, s, handle, inode); |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 1369 | if (error) { |
| 1370 | if (error == -ENOSPC && |
| 1371 | ext4_has_inline_data(inode)) { |
| 1372 | error = ext4_try_to_evict_inline_data(handle, inode, |
| 1373 | EXT4_XATTR_LEN(strlen(i->name) + |
| 1374 | EXT4_XATTR_SIZE(i->value_len))); |
| 1375 | if (error) |
| 1376 | return error; |
| 1377 | error = ext4_xattr_ibody_find(inode, i, is); |
| 1378 | if (error) |
| 1379 | return error; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1380 | error = ext4_xattr_set_entry(i, s, handle, inode); |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 1381 | } |
| 1382 | if (error) |
| 1383 | return error; |
| 1384 | } |
| 1385 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
| 1386 | if (!IS_LAST_ENTRY(s->first)) { |
| 1387 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
| 1388 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
| 1389 | } else { |
| 1390 | header->h_magic = cpu_to_le32(0); |
| 1391 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
| 1392 | } |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1396 | static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode, |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 1397 | struct ext4_xattr_info *i, |
| 1398 | struct ext4_xattr_ibody_find *is) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1399 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1400 | struct ext4_xattr_ibody_header *header; |
| 1401 | struct ext4_xattr_search *s = &is->s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1402 | int error; |
| 1403 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1404 | if (EXT4_I(inode)->i_extra_isize == 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1405 | return -ENOSPC; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1406 | error = ext4_xattr_set_entry(i, s, handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1407 | if (error) |
| 1408 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1409 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1410 | if (!IS_LAST_ENTRY(s->first)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1411 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1412 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1413 | } else { |
| 1414 | header->h_magic = cpu_to_le32(0); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1415 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1416 | } |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
Jan Kara | 3fd1646 | 2016-02-22 22:43:04 -0500 | [diff] [blame] | 1420 | static int ext4_xattr_value_same(struct ext4_xattr_search *s, |
| 1421 | struct ext4_xattr_info *i) |
| 1422 | { |
| 1423 | void *value; |
| 1424 | |
Tahsin Erdogan | 0bd454c | 2017-06-21 22:02:06 -0400 | [diff] [blame] | 1425 | /* When e_value_inum is set the value is stored externally. */ |
| 1426 | if (s->here->e_value_inum) |
| 1427 | return 0; |
Jan Kara | 3fd1646 | 2016-02-22 22:43:04 -0500 | [diff] [blame] | 1428 | if (le32_to_cpu(s->here->e_value_size) != i->value_len) |
| 1429 | return 0; |
| 1430 | value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs); |
| 1431 | return !memcmp(value, i->value, i->value_len); |
| 1432 | } |
| 1433 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1434 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1435 | * ext4_xattr_set_handle() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1436 | * |
Wang Sheng-Hui | 6e9510b | 2011-01-10 12:10:30 -0500 | [diff] [blame] | 1437 | * Create, replace or remove an extended attribute for this inode. Value |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1438 | * is NULL to remove an existing extended attribute, and non-NULL to |
| 1439 | * either replace an existing extended attribute, or create a new extended |
| 1440 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE |
| 1441 | * specify that an extended attribute must exist and must not exist |
| 1442 | * previous to the call, respectively. |
| 1443 | * |
| 1444 | * Returns 0, or a negative error number on failure. |
| 1445 | */ |
| 1446 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1447 | 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] | 1448 | const char *name, const void *value, size_t value_len, |
| 1449 | int flags) |
| 1450 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1451 | struct ext4_xattr_info i = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1452 | .name_index = name_index, |
| 1453 | .name = name, |
| 1454 | .value = value, |
| 1455 | .value_len = value_len, |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1456 | .in_inode = 0, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1457 | }; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1458 | struct ext4_xattr_ibody_find is = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1459 | .s = { .not_found = -ENODATA, }, |
| 1460 | }; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1461 | struct ext4_xattr_block_find bs = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1462 | .s = { .not_found = -ENODATA, }, |
| 1463 | }; |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1464 | int no_expand; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1465 | int error; |
| 1466 | |
| 1467 | if (!name) |
| 1468 | return -EINVAL; |
| 1469 | if (strlen(name) > 255) |
| 1470 | return -ERANGE; |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 1471 | |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1472 | ext4_write_lock_xattr(inode, &no_expand); |
Kalpak Shah | 4d20c68 | 2008-10-08 23:21:54 -0400 | [diff] [blame] | 1473 | |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 1474 | /* Check journal credits under write lock. */ |
| 1475 | if (ext4_handle_valid(handle)) { |
| 1476 | int credits; |
| 1477 | |
| 1478 | credits = ext4_xattr_set_credits(inode, value_len); |
| 1479 | if (!ext4_handle_has_enough_credits(handle, credits)) { |
| 1480 | error = -ENOSPC; |
| 1481 | goto cleanup; |
| 1482 | } |
| 1483 | } |
| 1484 | |
Eric Sandeen | 6654361 | 2011-10-26 03:32:07 -0400 | [diff] [blame] | 1485 | error = ext4_reserve_inode_write(handle, inode, &is.iloc); |
Eric Sandeen | 86ebfd0 | 2009-11-15 15:30:52 -0500 | [diff] [blame] | 1486 | if (error) |
| 1487 | goto cleanup; |
| 1488 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1489 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1490 | struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc); |
| 1491 | 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] | 1492 | ext4_clear_inode_state(inode, EXT4_STATE_NEW); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1495 | error = ext4_xattr_ibody_find(inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1496 | if (error) |
| 1497 | goto cleanup; |
| 1498 | if (is.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1499 | error = ext4_xattr_block_find(inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1500 | if (error) |
| 1501 | goto cleanup; |
| 1502 | if (is.s.not_found && bs.s.not_found) { |
| 1503 | error = -ENODATA; |
| 1504 | if (flags & XATTR_REPLACE) |
| 1505 | goto cleanup; |
| 1506 | error = 0; |
| 1507 | if (!value) |
| 1508 | goto cleanup; |
| 1509 | } else { |
| 1510 | error = -EEXIST; |
| 1511 | if (flags & XATTR_CREATE) |
| 1512 | goto cleanup; |
| 1513 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1514 | if (!value) { |
| 1515 | if (!is.s.not_found) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1516 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1517 | else if (!bs.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1518 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1519 | } else { |
Jan Kara | 3fd1646 | 2016-02-22 22:43:04 -0500 | [diff] [blame] | 1520 | error = 0; |
| 1521 | /* Xattr value did not change? Save us some work and bail out */ |
| 1522 | if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i)) |
| 1523 | goto cleanup; |
| 1524 | if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i)) |
| 1525 | goto cleanup; |
| 1526 | |
Tahsin Erdogan | b347e2b | 2017-06-21 22:20:32 -0400 | [diff] [blame] | 1527 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
| 1528 | (EXT4_XATTR_SIZE(i.value_len) > |
| 1529 | EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize))) |
| 1530 | i.in_inode = 1; |
| 1531 | retry_inode: |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1532 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1533 | if (!error && !bs.s.not_found) { |
| 1534 | i.value = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1535 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1536 | } else if (error == -ENOSPC) { |
Tiger Yang | 7e01c8e | 2008-05-14 16:05:47 -0700 | [diff] [blame] | 1537 | if (EXT4_I(inode)->i_file_acl && !bs.s.base) { |
| 1538 | error = ext4_xattr_block_find(inode, &i, &bs); |
| 1539 | if (error) |
| 1540 | goto cleanup; |
| 1541 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1542 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Tahsin Erdogan | b347e2b | 2017-06-21 22:20:32 -0400 | [diff] [blame] | 1543 | if (!error && !is.s.not_found) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1544 | i.value = NULL; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1545 | error = ext4_xattr_ibody_set(handle, inode, &i, |
| 1546 | &is); |
Tahsin Erdogan | b347e2b | 2017-06-21 22:20:32 -0400 | [diff] [blame] | 1547 | } else if (error == -ENOSPC) { |
| 1548 | /* |
| 1549 | * Xattr does not fit in the block, store at |
| 1550 | * external inode if possible. |
| 1551 | */ |
| 1552 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
| 1553 | !i.in_inode) { |
| 1554 | i.in_inode = 1; |
| 1555 | goto retry_inode; |
| 1556 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | if (!error) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1561 | ext4_xattr_update_super_block(handle, inode->i_sb); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 1562 | inode->i_ctime = current_time(inode); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1563 | if (!value) |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1564 | no_expand = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1565 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1566 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1567 | * The bh is consumed by ext4_mark_iloc_dirty, even with |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1568 | * error != 0. |
| 1569 | */ |
| 1570 | is.iloc.bh = NULL; |
| 1571 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1572 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | cleanup: |
| 1576 | brelse(is.iloc.bh); |
| 1577 | brelse(bs.bh); |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1578 | ext4_write_unlock_xattr(inode, &no_expand); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1579 | return error; |
| 1580 | } |
| 1581 | |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 1582 | int ext4_xattr_set_credits(struct inode *inode, size_t value_len) |
| 1583 | { |
| 1584 | struct super_block *sb = inode->i_sb; |
| 1585 | int credits; |
| 1586 | |
| 1587 | if (!EXT4_SB(sb)->s_journal) |
| 1588 | return 0; |
| 1589 | |
| 1590 | credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb); |
| 1591 | |
| 1592 | /* |
| 1593 | * In case of inline data, we may push out the data to a block, |
| 1594 | * so we need to reserve credits for this eventuality |
| 1595 | */ |
| 1596 | if (ext4_has_inline_data(inode)) |
| 1597 | credits += ext4_writepage_trans_blocks(inode) + 1; |
| 1598 | |
| 1599 | if (ext4_has_feature_ea_inode(sb)) { |
| 1600 | int nrblocks = (value_len + sb->s_blocksize - 1) >> |
| 1601 | sb->s_blocksize_bits; |
| 1602 | |
| 1603 | /* For new inode */ |
| 1604 | credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3; |
| 1605 | |
| 1606 | /* For data blocks of EA inode */ |
| 1607 | credits += ext4_meta_trans_blocks(inode, nrblocks, 0); |
| 1608 | } |
| 1609 | return credits; |
| 1610 | } |
| 1611 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1612 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1613 | * ext4_xattr_set() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1614 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1615 | * Like ext4_xattr_set_handle, but start from an inode. This extended |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1616 | * attribute modification is a filesystem transaction by itself. |
| 1617 | * |
| 1618 | * Returns 0, or a negative error number on failure. |
| 1619 | */ |
| 1620 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1621 | ext4_xattr_set(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1622 | const void *value, size_t value_len, int flags) |
| 1623 | { |
| 1624 | handle_t *handle; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1625 | struct super_block *sb = inode->i_sb; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1626 | int error, retries = 0; |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 1627 | int credits; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1628 | |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 1629 | error = dquot_initialize(inode); |
| 1630 | if (error) |
| 1631 | return error; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1632 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1633 | retry: |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 1634 | credits = ext4_xattr_set_credits(inode, value_len); |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 1635 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1636 | if (IS_ERR(handle)) { |
| 1637 | error = PTR_ERR(handle); |
| 1638 | } else { |
| 1639 | int error2; |
| 1640 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1641 | error = ext4_xattr_set_handle(handle, inode, name_index, name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1642 | value, value_len, flags); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1643 | error2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1644 | if (error == -ENOSPC && |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1645 | ext4_should_retry_alloc(sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1646 | goto retry; |
| 1647 | if (error == 0) |
| 1648 | error = error2; |
| 1649 | } |
| 1650 | |
| 1651 | return error; |
| 1652 | } |
| 1653 | |
| 1654 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1655 | * Shift the EA entries in the inode to create space for the increased |
| 1656 | * i_extra_isize. |
| 1657 | */ |
| 1658 | static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry, |
| 1659 | int value_offs_shift, void *to, |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 1660 | void *from, size_t n) |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1661 | { |
| 1662 | struct ext4_xattr_entry *last = entry; |
| 1663 | int new_offs; |
| 1664 | |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 1665 | /* We always shift xattr headers further thus offsets get lower */ |
| 1666 | BUG_ON(value_offs_shift > 0); |
| 1667 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1668 | /* Adjust the value offsets of the entries */ |
| 1669 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1670 | if (!last->e_value_inum && last->e_value_size) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1671 | new_offs = le16_to_cpu(last->e_value_offs) + |
| 1672 | value_offs_shift; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1673 | last->e_value_offs = cpu_to_le16(new_offs); |
| 1674 | } |
| 1675 | } |
| 1676 | /* Shift the entries by n bytes */ |
| 1677 | memmove(to, from, n); |
| 1678 | } |
| 1679 | |
| 1680 | /* |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1681 | * Move xattr pointed to by 'entry' from inode into external xattr block |
| 1682 | */ |
| 1683 | static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, |
| 1684 | struct ext4_inode *raw_inode, |
| 1685 | struct ext4_xattr_entry *entry) |
| 1686 | { |
| 1687 | struct ext4_xattr_ibody_find *is = NULL; |
| 1688 | struct ext4_xattr_block_find *bs = NULL; |
| 1689 | char *buffer = NULL, *b_entry_name = NULL; |
Tahsin Erdogan | f610910 | 2017-06-21 22:11:54 -0400 | [diff] [blame] | 1690 | size_t value_size = le32_to_cpu(entry->e_value_size); |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1691 | struct ext4_xattr_info i = { |
| 1692 | .value = NULL, |
| 1693 | .value_len = 0, |
| 1694 | .name_index = entry->e_name_index, |
Tahsin Erdogan | f610910 | 2017-06-21 22:11:54 -0400 | [diff] [blame] | 1695 | .in_inode = !!entry->e_value_inum, |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1696 | }; |
| 1697 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); |
| 1698 | int error; |
| 1699 | |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1700 | is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); |
| 1701 | bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS); |
| 1702 | buffer = kmalloc(value_size, GFP_NOFS); |
| 1703 | b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS); |
| 1704 | if (!is || !bs || !buffer || !b_entry_name) { |
| 1705 | error = -ENOMEM; |
| 1706 | goto out; |
| 1707 | } |
| 1708 | |
| 1709 | is->s.not_found = -ENODATA; |
| 1710 | bs->s.not_found = -ENODATA; |
| 1711 | is->iloc.bh = NULL; |
| 1712 | bs->bh = NULL; |
| 1713 | |
| 1714 | /* Save the entry name and the entry value */ |
Tahsin Erdogan | f610910 | 2017-06-21 22:11:54 -0400 | [diff] [blame] | 1715 | if (entry->e_value_inum) { |
| 1716 | error = ext4_xattr_inode_get(inode, |
| 1717 | le32_to_cpu(entry->e_value_inum), |
| 1718 | buffer, value_size); |
| 1719 | if (error) |
| 1720 | goto out; |
| 1721 | } else { |
| 1722 | size_t value_offs = le16_to_cpu(entry->e_value_offs); |
| 1723 | memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size); |
| 1724 | } |
| 1725 | |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1726 | memcpy(b_entry_name, entry->e_name, entry->e_name_len); |
| 1727 | b_entry_name[entry->e_name_len] = '\0'; |
| 1728 | i.name = b_entry_name; |
| 1729 | |
| 1730 | error = ext4_get_inode_loc(inode, &is->iloc); |
| 1731 | if (error) |
| 1732 | goto out; |
| 1733 | |
| 1734 | error = ext4_xattr_ibody_find(inode, &i, is); |
| 1735 | if (error) |
| 1736 | goto out; |
| 1737 | |
| 1738 | /* Remove the chosen entry from the inode */ |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1739 | error = ext4_xattr_ibody_set(handle, inode, &i, is); |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1740 | if (error) |
| 1741 | goto out; |
| 1742 | |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1743 | i.value = buffer; |
| 1744 | i.value_len = value_size; |
| 1745 | error = ext4_xattr_block_find(inode, &i, bs); |
| 1746 | if (error) |
| 1747 | goto out; |
| 1748 | |
| 1749 | /* Add entry which was removed from the inode into the block */ |
| 1750 | error = ext4_xattr_block_set(handle, inode, &i, bs); |
| 1751 | if (error) |
| 1752 | goto out; |
| 1753 | error = 0; |
| 1754 | out: |
| 1755 | kfree(b_entry_name); |
| 1756 | kfree(buffer); |
| 1757 | if (is) |
| 1758 | brelse(is->iloc.bh); |
| 1759 | kfree(is); |
| 1760 | kfree(bs); |
| 1761 | |
| 1762 | return error; |
| 1763 | } |
| 1764 | |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1765 | static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode, |
| 1766 | struct ext4_inode *raw_inode, |
| 1767 | int isize_diff, size_t ifree, |
| 1768 | size_t bfree, int *total_ino) |
| 1769 | { |
| 1770 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); |
| 1771 | struct ext4_xattr_entry *small_entry; |
| 1772 | struct ext4_xattr_entry *entry; |
| 1773 | struct ext4_xattr_entry *last; |
| 1774 | unsigned int entry_size; /* EA entry size */ |
| 1775 | unsigned int total_size; /* EA entry size + value size */ |
| 1776 | unsigned int min_total_size; |
| 1777 | int error; |
| 1778 | |
| 1779 | while (isize_diff > ifree) { |
| 1780 | entry = NULL; |
| 1781 | small_entry = NULL; |
| 1782 | min_total_size = ~0U; |
| 1783 | last = IFIRST(header); |
| 1784 | /* Find the entry best suited to be pushed into EA block */ |
| 1785 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Tahsin Erdogan | 9bb21ce | 2017-06-21 22:05:44 -0400 | [diff] [blame] | 1786 | total_size = EXT4_XATTR_LEN(last->e_name_len); |
| 1787 | if (!last->e_value_inum) |
| 1788 | total_size += EXT4_XATTR_SIZE( |
| 1789 | le32_to_cpu(last->e_value_size)); |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1790 | if (total_size <= bfree && |
| 1791 | total_size < min_total_size) { |
| 1792 | if (total_size + ifree < isize_diff) { |
| 1793 | small_entry = last; |
| 1794 | } else { |
| 1795 | entry = last; |
| 1796 | min_total_size = total_size; |
| 1797 | } |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | if (entry == NULL) { |
| 1802 | if (small_entry == NULL) |
| 1803 | return -ENOSPC; |
| 1804 | entry = small_entry; |
| 1805 | } |
| 1806 | |
| 1807 | entry_size = EXT4_XATTR_LEN(entry->e_name_len); |
Tahsin Erdogan | 9bb21ce | 2017-06-21 22:05:44 -0400 | [diff] [blame] | 1808 | total_size = entry_size; |
| 1809 | if (!entry->e_value_inum) |
| 1810 | total_size += EXT4_XATTR_SIZE( |
| 1811 | le32_to_cpu(entry->e_value_size)); |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1812 | error = ext4_xattr_move_to_block(handle, inode, raw_inode, |
| 1813 | entry); |
| 1814 | if (error) |
| 1815 | return error; |
| 1816 | |
| 1817 | *total_ino -= entry_size; |
| 1818 | ifree += total_size; |
| 1819 | bfree -= total_size; |
| 1820 | } |
| 1821 | |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1825 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1826 | * Expand an inode by new_extra_isize bytes when EAs are present. |
| 1827 | * Returns 0 on success or negative error number on failure. |
| 1828 | */ |
| 1829 | int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize, |
| 1830 | struct ext4_inode *raw_inode, handle_t *handle) |
| 1831 | { |
| 1832 | struct ext4_xattr_ibody_header *header; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1833 | struct buffer_head *bh = NULL; |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1834 | size_t min_offs; |
| 1835 | size_t ifree, bfree; |
Theodore Ts'o | 7b1b2c1 | 2014-02-19 20:15:21 -0500 | [diff] [blame] | 1836 | int total_ino; |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1837 | void *base, *end; |
Jan Kara | d014119 | 2016-08-11 11:50:30 -0400 | [diff] [blame] | 1838 | int error = 0, tried_min_extra_isize = 0; |
Aneesh Kumar K.V | ac39849 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1839 | 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] | 1840 | 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] | 1841 | int no_expand; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1842 | |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1843 | if (ext4_write_trylock_xattr(inode, &no_expand) == 0) |
| 1844 | return 0; |
| 1845 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1846 | retry: |
Jan Kara | d014119 | 2016-08-11 11:50:30 -0400 | [diff] [blame] | 1847 | isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize; |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1848 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) |
| 1849 | goto out; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1850 | |
| 1851 | header = IHDR(inode, raw_inode); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1852 | |
| 1853 | /* |
| 1854 | * Check if enough free space is available in the inode to shift the |
| 1855 | * entries ahead by new_extra_isize. |
| 1856 | */ |
| 1857 | |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1858 | base = IFIRST(header); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1859 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
| 1860 | min_offs = end - base; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1861 | total_ino = sizeof(struct ext4_xattr_ibody_header); |
| 1862 | |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 1863 | error = xattr_check_inode(inode, header, end); |
| 1864 | if (error) |
| 1865 | goto cleanup; |
| 1866 | |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1867 | ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1868 | if (ifree >= isize_diff) |
| 1869 | goto shift; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1870 | |
| 1871 | /* |
| 1872 | * Enough free space isn't available in the inode, check if |
| 1873 | * EA block can hold new_extra_isize bytes. |
| 1874 | */ |
| 1875 | if (EXT4_I(inode)->i_file_acl) { |
| 1876 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
| 1877 | error = -EIO; |
| 1878 | if (!bh) |
| 1879 | goto cleanup; |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 1880 | if (ext4_xattr_check_block(inode, bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1881 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 1882 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1883 | error = -EFSCORRUPTED; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1884 | goto cleanup; |
| 1885 | } |
| 1886 | base = BHDR(bh); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1887 | end = bh->b_data + bh->b_size; |
| 1888 | min_offs = end - base; |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1889 | bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base, |
| 1890 | NULL); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1891 | if (bfree + ifree < isize_diff) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1892 | if (!tried_min_extra_isize && s_min_extra_isize) { |
| 1893 | tried_min_extra_isize++; |
| 1894 | new_extra_isize = s_min_extra_isize; |
| 1895 | brelse(bh); |
| 1896 | goto retry; |
| 1897 | } |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1898 | error = -ENOSPC; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1899 | goto cleanup; |
| 1900 | } |
| 1901 | } else { |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1902 | bfree = inode->i_sb->s_blocksize; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1903 | } |
| 1904 | |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1905 | error = ext4_xattr_make_inode_space(handle, inode, raw_inode, |
| 1906 | isize_diff, ifree, bfree, |
| 1907 | &total_ino); |
| 1908 | if (error) { |
| 1909 | if (error == -ENOSPC && !tried_min_extra_isize && |
| 1910 | s_min_extra_isize) { |
| 1911 | tried_min_extra_isize++; |
| 1912 | new_extra_isize = s_min_extra_isize; |
| 1913 | brelse(bh); |
| 1914 | goto retry; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1915 | } |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1916 | goto cleanup; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1917 | } |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1918 | shift: |
| 1919 | /* Adjust the offsets and shift the remaining entries ahead */ |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1920 | ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1921 | - new_extra_isize, (void *)raw_inode + |
| 1922 | EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize, |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 1923 | (void *)header, total_ino); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1924 | EXT4_I(inode)->i_extra_isize = new_extra_isize; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1925 | brelse(bh); |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1926 | out: |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1927 | ext4_write_unlock_xattr(inode, &no_expand); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1928 | return 0; |
| 1929 | |
| 1930 | cleanup: |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1931 | brelse(bh); |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1932 | /* |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1933 | * Inode size expansion failed; don't try again |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1934 | */ |
Theodore Ts'o | c755e25 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1935 | no_expand = 1; |
| 1936 | ext4_write_unlock_xattr(inode, &no_expand); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1937 | return error; |
| 1938 | } |
| 1939 | |
| 1940 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1941 | #define EIA_INCR 16 /* must be 2^n */ |
| 1942 | #define EIA_MASK (EIA_INCR - 1) |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1943 | /* Add the large xattr @inode into @ea_inode_array for later deletion. |
| 1944 | * 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] | 1945 | * contents copied over. |
| 1946 | */ |
| 1947 | static int |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1948 | ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, |
| 1949 | struct inode *inode) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1950 | { |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1951 | if (*ea_inode_array == NULL) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1952 | /* |
| 1953 | * 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^] | 1954 | * If *ea_inode_array is NULL, this is essentially offsetof() |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1955 | */ |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1956 | (*ea_inode_array) = |
| 1957 | kmalloc(offsetof(struct ext4_xattr_inode_array, |
| 1958 | inodes[EIA_MASK]), |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1959 | GFP_NOFS); |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1960 | if (*ea_inode_array == NULL) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1961 | return -ENOMEM; |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1962 | (*ea_inode_array)->count = 0; |
| 1963 | } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1964 | /* expand the array once all 15 + n * 16 slots are full */ |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1965 | struct ext4_xattr_inode_array *new_array = NULL; |
| 1966 | int count = (*ea_inode_array)->count; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1967 | |
| 1968 | /* if new_array is NULL, this is essentially offsetof() */ |
| 1969 | new_array = kmalloc( |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1970 | offsetof(struct ext4_xattr_inode_array, |
| 1971 | inodes[count + EIA_INCR]), |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1972 | GFP_NOFS); |
| 1973 | if (new_array == NULL) |
| 1974 | return -ENOMEM; |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1975 | memcpy(new_array, *ea_inode_array, |
| 1976 | offsetof(struct ext4_xattr_inode_array, inodes[count])); |
| 1977 | kfree(*ea_inode_array); |
| 1978 | *ea_inode_array = new_array; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1979 | } |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1980 | (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1981 | return 0; |
| 1982 | } |
| 1983 | |
| 1984 | /** |
| 1985 | * Add xattr inode to orphan list |
| 1986 | */ |
| 1987 | static int |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1988 | ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode, int credits, |
| 1989 | struct ext4_xattr_inode_array *ea_inode_array) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1990 | { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1991 | int idx = 0, error = 0; |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1992 | struct inode *ea_inode; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1993 | |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1994 | if (ea_inode_array == NULL) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1995 | return 0; |
| 1996 | |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 1997 | for (; idx < ea_inode_array->count; ++idx) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 1998 | if (!ext4_handle_has_enough_credits(handle, credits)) { |
| 1999 | error = ext4_journal_extend(handle, credits); |
| 2000 | if (error > 0) |
| 2001 | error = ext4_journal_restart(handle, credits); |
| 2002 | |
| 2003 | if (error != 0) { |
| 2004 | ext4_warning(inode->i_sb, |
| 2005 | "couldn't extend journal " |
| 2006 | "(err %d)", error); |
| 2007 | return error; |
| 2008 | } |
| 2009 | } |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2010 | ea_inode = ea_inode_array->inodes[idx]; |
Tahsin Erdogan | 0de5983 | 2017-06-21 21:19:16 -0400 | [diff] [blame] | 2011 | inode_lock(ea_inode); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2012 | ext4_orphan_add(handle, ea_inode); |
Tahsin Erdogan | 0de5983 | 2017-06-21 21:19:16 -0400 | [diff] [blame] | 2013 | inode_unlock(ea_inode); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2014 | /* the inode's i_count will be released by caller */ |
| 2015 | } |
| 2016 | |
| 2017 | return 0; |
| 2018 | } |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 2019 | |
| 2020 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2021 | * ext4_xattr_delete_inode() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2022 | * |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2023 | * Free extended attribute resources associated with this inode. Traverse |
| 2024 | * all entries and unlink any xattr inodes associated with this inode. This |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2025 | * is called immediately before an inode is freed. We have exclusive |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2026 | * access to the inode. If an orphan inode is deleted it will also delete any |
| 2027 | * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget() |
| 2028 | * to ensure they belong to the parent inode and were not deleted already. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2029 | */ |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2030 | int |
| 2031 | ext4_xattr_delete_inode(handle_t *handle, struct inode *inode, |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2032 | struct ext4_xattr_inode_array **ea_inode_array) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2033 | { |
| 2034 | struct buffer_head *bh = NULL; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2035 | struct ext4_xattr_ibody_header *header; |
| 2036 | struct ext4_inode *raw_inode; |
| 2037 | struct ext4_iloc iloc; |
| 2038 | struct ext4_xattr_entry *entry; |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2039 | struct inode *ea_inode; |
Tahsin Erdogan | 990461d | 2017-06-21 21:59:30 -0400 | [diff] [blame] | 2040 | unsigned int ea_ino; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2041 | int credits = 3, error = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2042 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2043 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
| 2044 | goto delete_external_ea; |
| 2045 | |
| 2046 | error = ext4_get_inode_loc(inode, &iloc); |
| 2047 | if (error) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2048 | goto cleanup; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2049 | raw_inode = ext4_raw_inode(&iloc); |
| 2050 | header = IHDR(inode, raw_inode); |
| 2051 | for (entry = IFIRST(header); !IS_LAST_ENTRY(entry); |
| 2052 | entry = EXT4_XATTR_NEXT(entry)) { |
| 2053 | if (!entry->e_value_inum) |
| 2054 | continue; |
Tahsin Erdogan | 990461d | 2017-06-21 21:59:30 -0400 | [diff] [blame] | 2055 | ea_ino = le32_to_cpu(entry->e_value_inum); |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2056 | error = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode); |
| 2057 | if (error) |
| 2058 | continue; |
| 2059 | error = ext4_expand_inode_array(ea_inode_array, ea_inode); |
Tahsin Erdogan | 65d3000 | 2017-06-21 22:24:38 -0400 | [diff] [blame] | 2060 | if (error) { |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2061 | iput(ea_inode); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2062 | brelse(iloc.bh); |
| 2063 | goto cleanup; |
| 2064 | } |
| 2065 | entry->e_value_inum = 0; |
| 2066 | } |
| 2067 | brelse(iloc.bh); |
| 2068 | |
| 2069 | delete_external_ea: |
| 2070 | if (!EXT4_I(inode)->i_file_acl) { |
| 2071 | /* add xattr inode to orphan list */ |
Tahsin Erdogan | 65d3000 | 2017-06-21 22:24:38 -0400 | [diff] [blame] | 2072 | error = ext4_xattr_inode_orphan_add(handle, inode, credits, |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2073 | *ea_inode_array); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2074 | goto cleanup; |
| 2075 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2076 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2077 | if (!bh) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 2078 | EXT4_ERROR_INODE(inode, "block %llu read error", |
| 2079 | EXT4_I(inode)->i_file_acl); |
Tahsin Erdogan | 65d3000 | 2017-06-21 22:24:38 -0400 | [diff] [blame] | 2080 | error = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2081 | goto cleanup; |
| 2082 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2083 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2084 | BHDR(bh)->h_blocks != cpu_to_le32(1)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 2085 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 2086 | EXT4_I(inode)->i_file_acl); |
Tahsin Erdogan | 65d3000 | 2017-06-21 22:24:38 -0400 | [diff] [blame] | 2087 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2088 | goto cleanup; |
| 2089 | } |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2090 | |
| 2091 | for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry); |
| 2092 | entry = EXT4_XATTR_NEXT(entry)) { |
| 2093 | if (!entry->e_value_inum) |
| 2094 | continue; |
Tahsin Erdogan | 990461d | 2017-06-21 21:59:30 -0400 | [diff] [blame] | 2095 | ea_ino = le32_to_cpu(entry->e_value_inum); |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2096 | error = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode); |
| 2097 | if (error) |
| 2098 | continue; |
| 2099 | error = ext4_expand_inode_array(ea_inode_array, ea_inode); |
Tahsin Erdogan | 65d3000 | 2017-06-21 22:24:38 -0400 | [diff] [blame] | 2100 | if (error) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2101 | goto cleanup; |
| 2102 | entry->e_value_inum = 0; |
| 2103 | } |
| 2104 | |
| 2105 | /* add xattr inode to orphan list */ |
| 2106 | error = ext4_xattr_inode_orphan_add(handle, inode, credits, |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2107 | *ea_inode_array); |
Tahsin Erdogan | 65d3000 | 2017-06-21 22:24:38 -0400 | [diff] [blame] | 2108 | if (error) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2109 | goto cleanup; |
| 2110 | |
| 2111 | if (!IS_NOQUOTA(inode)) |
| 2112 | credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb); |
| 2113 | |
| 2114 | if (!ext4_handle_has_enough_credits(handle, credits)) { |
| 2115 | error = ext4_journal_extend(handle, credits); |
| 2116 | if (error > 0) |
| 2117 | error = ext4_journal_restart(handle, credits); |
Tahsin Erdogan | 65d3000 | 2017-06-21 22:24:38 -0400 | [diff] [blame] | 2118 | if (error) { |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2119 | ext4_warning(inode->i_sb, |
| 2120 | "couldn't extend journal (err %d)", error); |
| 2121 | goto cleanup; |
| 2122 | } |
| 2123 | } |
| 2124 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2125 | ext4_xattr_release_block(handle, inode, bh); |
| 2126 | EXT4_I(inode)->i_file_acl = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2127 | |
| 2128 | cleanup: |
| 2129 | brelse(bh); |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2130 | |
| 2131 | return error; |
| 2132 | } |
| 2133 | |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2134 | 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] | 2135 | { |
Tahsin Erdogan | bab79b0 | 2017-06-21 21:49:53 -0400 | [diff] [blame] | 2136 | struct inode *ea_inode; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2137 | int idx = 0; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2138 | |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2139 | if (ea_inode_array == NULL) |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2140 | return; |
| 2141 | |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2142 | for (; idx < ea_inode_array->count; ++idx) { |
| 2143 | ea_inode = ea_inode_array->inodes[idx]; |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2144 | clear_nlink(ea_inode); |
| 2145 | iput(ea_inode); |
| 2146 | } |
Tahsin Erdogan | 0421a18 | 2017-06-22 10:26:31 -0400 | [diff] [blame^] | 2147 | kfree(ea_inode_array); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2151 | * ext4_xattr_cache_insert() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2152 | * |
| 2153 | * Create a new entry in the extended attribute cache, and insert |
| 2154 | * it unless such an entry is already in the cache. |
| 2155 | * |
| 2156 | * Returns 0, or a negative error number on failure. |
| 2157 | */ |
| 2158 | static void |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 2159 | ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2160 | { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 2161 | struct ext4_xattr_header *header = BHDR(bh); |
| 2162 | __u32 hash = le32_to_cpu(header->h_hash); |
| 2163 | int reusable = le32_to_cpu(header->h_refcount) < |
| 2164 | EXT4_XATTR_REFCOUNT_MAX; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2165 | int error; |
| 2166 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 2167 | error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash, |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 2168 | bh->b_blocknr, reusable); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2169 | if (error) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 2170 | if (error == -EBUSY) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2171 | ea_bdebug(bh, "already in cache"); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 2172 | } else |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2173 | ea_bdebug(bh, "inserting [%x]", (int)hash); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2177 | * ext4_xattr_cmp() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2178 | * |
| 2179 | * Compare two extended attribute blocks for equality. |
| 2180 | * |
| 2181 | * Returns 0 if the blocks are equal, 1 if they differ, and |
| 2182 | * a negative error number on errors. |
| 2183 | */ |
| 2184 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2185 | ext4_xattr_cmp(struct ext4_xattr_header *header1, |
| 2186 | struct ext4_xattr_header *header2) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2187 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2188 | struct ext4_xattr_entry *entry1, *entry2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2189 | |
| 2190 | entry1 = ENTRY(header1+1); |
| 2191 | entry2 = ENTRY(header2+1); |
| 2192 | while (!IS_LAST_ENTRY(entry1)) { |
| 2193 | if (IS_LAST_ENTRY(entry2)) |
| 2194 | return 1; |
| 2195 | if (entry1->e_hash != entry2->e_hash || |
| 2196 | entry1->e_name_index != entry2->e_name_index || |
| 2197 | entry1->e_name_len != entry2->e_name_len || |
| 2198 | entry1->e_value_size != entry2->e_value_size || |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2199 | entry1->e_value_inum != entry2->e_value_inum || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2200 | memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) |
| 2201 | return 1; |
Tahsin Erdogan | 7cec191 | 2017-06-21 22:14:30 -0400 | [diff] [blame] | 2202 | if (!entry1->e_value_inum && |
| 2203 | memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2204 | (char *)header2 + le16_to_cpu(entry2->e_value_offs), |
| 2205 | le32_to_cpu(entry1->e_value_size))) |
| 2206 | return 1; |
| 2207 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2208 | entry1 = EXT4_XATTR_NEXT(entry1); |
| 2209 | entry2 = EXT4_XATTR_NEXT(entry2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2210 | } |
| 2211 | if (!IS_LAST_ENTRY(entry2)) |
| 2212 | return 1; |
| 2213 | return 0; |
| 2214 | } |
| 2215 | |
| 2216 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2217 | * ext4_xattr_cache_find() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2218 | * |
| 2219 | * Find an identical extended attribute block. |
| 2220 | * |
| 2221 | * Returns a pointer to the block found, or NULL if such a block was |
| 2222 | * not found or an error occurred. |
| 2223 | */ |
| 2224 | static struct buffer_head * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2225 | ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header, |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 2226 | struct mb_cache_entry **pce) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2227 | { |
| 2228 | __u32 hash = le32_to_cpu(header->h_hash); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 2229 | struct mb_cache_entry *ce; |
| 2230 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2231 | |
| 2232 | if (!header->h_hash) |
| 2233 | return NULL; /* never share */ |
| 2234 | ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 2235 | ce = mb_cache_entry_find_first(ext4_mb_cache, hash); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2236 | while (ce) { |
| 2237 | struct buffer_head *bh; |
| 2238 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2239 | bh = sb_bread(inode->i_sb, ce->e_block); |
| 2240 | if (!bh) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 2241 | EXT4_ERROR_INODE(inode, "block %lu read error", |
| 2242 | (unsigned long) ce->e_block); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2243 | } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2244 | *pce = ce; |
| 2245 | return bh; |
| 2246 | } |
| 2247 | brelse(bh); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 2248 | ce = mb_cache_entry_find_next(ext4_mb_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2249 | } |
| 2250 | return NULL; |
| 2251 | } |
| 2252 | |
| 2253 | #define NAME_HASH_SHIFT 5 |
| 2254 | #define VALUE_HASH_SHIFT 16 |
| 2255 | |
| 2256 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2257 | * ext4_xattr_hash_entry() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2258 | * |
| 2259 | * Compute the hash of an extended attribute. |
| 2260 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2261 | static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header, |
| 2262 | struct ext4_xattr_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2263 | { |
| 2264 | __u32 hash = 0; |
| 2265 | char *name = entry->e_name; |
| 2266 | int n; |
| 2267 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 2268 | for (n = 0; n < entry->e_name_len; n++) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2269 | hash = (hash << NAME_HASH_SHIFT) ^ |
| 2270 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ |
| 2271 | *name++; |
| 2272 | } |
| 2273 | |
Andreas Dilger | e50e512 | 2017-06-21 21:10:32 -0400 | [diff] [blame] | 2274 | if (!entry->e_value_inum && entry->e_value_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2275 | __le32 *value = (__le32 *)((char *)header + |
| 2276 | le16_to_cpu(entry->e_value_offs)); |
| 2277 | for (n = (le32_to_cpu(entry->e_value_size) + |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2278 | EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2279 | hash = (hash << VALUE_HASH_SHIFT) ^ |
| 2280 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ |
| 2281 | le32_to_cpu(*value++); |
| 2282 | } |
| 2283 | } |
| 2284 | entry->e_hash = cpu_to_le32(hash); |
| 2285 | } |
| 2286 | |
| 2287 | #undef NAME_HASH_SHIFT |
| 2288 | #undef VALUE_HASH_SHIFT |
| 2289 | |
| 2290 | #define BLOCK_HASH_SHIFT 16 |
| 2291 | |
| 2292 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2293 | * ext4_xattr_rehash() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2294 | * |
| 2295 | * Re-compute the extended attribute hash value after an entry has changed. |
| 2296 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2297 | static void ext4_xattr_rehash(struct ext4_xattr_header *header, |
| 2298 | struct ext4_xattr_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2299 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2300 | struct ext4_xattr_entry *here; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2301 | __u32 hash = 0; |
| 2302 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2303 | ext4_xattr_hash_entry(header, entry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2304 | here = ENTRY(header+1); |
| 2305 | while (!IS_LAST_ENTRY(here)) { |
| 2306 | if (!here->e_hash) { |
| 2307 | /* Block is not shared if an entry's hash value == 0 */ |
| 2308 | hash = 0; |
| 2309 | break; |
| 2310 | } |
| 2311 | hash = (hash << BLOCK_HASH_SHIFT) ^ |
| 2312 | (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ |
| 2313 | le32_to_cpu(here->e_hash); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2314 | here = EXT4_XATTR_NEXT(here); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2315 | } |
| 2316 | header->h_hash = cpu_to_le32(hash); |
| 2317 | } |
| 2318 | |
| 2319 | #undef BLOCK_HASH_SHIFT |
| 2320 | |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 2321 | #define HASH_BUCKET_BITS 10 |
| 2322 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 2323 | struct mb_cache * |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 2324 | ext4_xattr_create_cache(void) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2325 | { |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 2326 | return mb_cache_create(HASH_BUCKET_BITS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2327 | } |
| 2328 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 2329 | void ext4_xattr_destroy_cache(struct mb_cache *cache) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2330 | { |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 2331 | if (cache) |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 2332 | mb_cache_destroy(cache); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2333 | } |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 2334 | |