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