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