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