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 *); |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 81 | static int ext4_xattr_list(struct dentry *dentry, char *buffer, |
Mingming Cao | d3a95d4 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 82 | size_t buffer_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 83 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 84 | static const struct xattr_handler *ext4_xattr_handler_map[] = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 85 | [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 86 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 87 | [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler, |
| 88 | [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 89 | #endif |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 90 | [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 91 | #ifdef CONFIG_EXT4_FS_SECURITY |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 92 | [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 93 | #endif |
| 94 | }; |
| 95 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 96 | const struct xattr_handler *ext4_xattr_handlers[] = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 97 | &ext4_xattr_user_handler, |
| 98 | &ext4_xattr_trusted_handler, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 99 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 100 | &posix_acl_access_xattr_handler, |
| 101 | &posix_acl_default_xattr_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 102 | #endif |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 103 | #ifdef CONFIG_EXT4_FS_SECURITY |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 104 | &ext4_xattr_security_handler, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 105 | #endif |
| 106 | NULL |
| 107 | }; |
| 108 | |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 109 | #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \ |
| 110 | inode->i_sb->s_fs_info)->s_mb_cache) |
| 111 | |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 112 | static __le32 ext4_xattr_block_csum(struct inode *inode, |
| 113 | sector_t block_nr, |
| 114 | struct ext4_xattr_header *hdr) |
| 115 | { |
| 116 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 117 | __u32 csum; |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 118 | __le64 dsk_block_nr = cpu_to_le64(block_nr); |
Daeho Jeong | b47820e | 2016-07-03 17:51:39 -0400 | [diff] [blame] | 119 | __u32 dummy_csum = 0; |
| 120 | int offset = offsetof(struct ext4_xattr_header, h_checksum); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 121 | |
Theodore Ts'o | d6a7710 | 2013-04-09 23:59:55 -0400 | [diff] [blame] | 122 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr, |
| 123 | sizeof(dsk_block_nr)); |
Daeho Jeong | b47820e | 2016-07-03 17:51:39 -0400 | [diff] [blame] | 124 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset); |
| 125 | csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum)); |
| 126 | offset += sizeof(dummy_csum); |
| 127 | csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset, |
| 128 | EXT4_BLOCK_SIZE(inode->i_sb) - offset); |
Tao Ma | 41eb70d | 2012-07-09 16:29:27 -0400 | [diff] [blame] | 129 | |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 130 | return cpu_to_le32(csum); |
| 131 | } |
| 132 | |
| 133 | static int ext4_xattr_block_csum_verify(struct inode *inode, |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 134 | struct buffer_head *bh) |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 135 | { |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 136 | struct ext4_xattr_header *hdr = BHDR(bh); |
| 137 | int ret = 1; |
| 138 | |
| 139 | if (ext4_has_metadata_csum(inode->i_sb)) { |
| 140 | lock_buffer(bh); |
| 141 | ret = (hdr->h_checksum == ext4_xattr_block_csum(inode, |
| 142 | bh->b_blocknr, hdr)); |
| 143 | unlock_buffer(bh); |
| 144 | } |
| 145 | return ret; |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static void ext4_xattr_block_csum_set(struct inode *inode, |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 149 | struct buffer_head *bh) |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 150 | { |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 151 | if (ext4_has_metadata_csum(inode->i_sb)) |
| 152 | BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode, |
| 153 | bh->b_blocknr, BHDR(bh)); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 156 | static inline const struct xattr_handler * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 157 | ext4_xattr_handler(int name_index) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 158 | { |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 159 | const struct xattr_handler *handler = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 160 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 161 | if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map)) |
| 162 | handler = ext4_xattr_handler_map[name_index]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 163 | return handler; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Inode operation listxattr() |
| 168 | * |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 169 | * d_inode(dentry)->i_mutex: don't care |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 170 | */ |
| 171 | ssize_t |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 172 | ext4_listxattr(struct dentry *dentry, char *buffer, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 173 | { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 174 | return ext4_xattr_list(dentry, buffer, size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static int |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 178 | ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end, |
| 179 | void *value_start) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 180 | { |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 181 | struct ext4_xattr_entry *e = entry; |
| 182 | |
| 183 | while (!IS_LAST_ENTRY(e)) { |
| 184 | struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 185 | if ((void *)next >= end) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 186 | return -EFSCORRUPTED; |
Theodore Ts'o | 9c2860f | 2018-08-01 12:36:52 -0400 | [diff] [blame] | 187 | if (strnlen(e->e_name, e->e_name_len) != e->e_name_len) |
| 188 | return -EFSCORRUPTED; |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 189 | e = next; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 190 | } |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 191 | |
| 192 | while (!IS_LAST_ENTRY(entry)) { |
Jan Kara | 2de58f1 | 2016-08-29 15:39:11 -0400 | [diff] [blame] | 193 | if (entry->e_value_block != 0) |
| 194 | return -EFSCORRUPTED; |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 195 | if (entry->e_value_size != 0 && |
| 196 | (value_start + le16_to_cpu(entry->e_value_offs) < |
| 197 | (void *)e + sizeof(__u32) || |
| 198 | value_start + le16_to_cpu(entry->e_value_offs) + |
| 199 | le32_to_cpu(entry->e_value_size) > end)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 200 | return -EFSCORRUPTED; |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 201 | entry = EXT4_XATTR_NEXT(entry); |
| 202 | } |
| 203 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static inline int |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 208 | ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 209 | { |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 210 | int error; |
| 211 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 212 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 213 | BHDR(bh)->h_blocks != cpu_to_le32(1)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 214 | return -EFSCORRUPTED; |
Theodore Ts'o | 0050338 | 2018-06-13 00:51:28 -0400 | [diff] [blame] | 215 | if (buffer_verified(bh)) |
| 216 | return 0; |
| 217 | |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 218 | if (!ext4_xattr_block_csum_verify(inode, bh)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 219 | return -EFSBADCRC; |
Darrick J. Wong | a0626e75 | 2014-09-16 14:34:59 -0400 | [diff] [blame] | 220 | error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size, |
| 221 | bh->b_data); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 222 | if (!error) |
| 223 | set_buffer_verified(bh); |
| 224 | return error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 227 | static int |
| 228 | __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header, |
| 229 | void *end, const char *function, unsigned int line) |
| 230 | { |
| 231 | struct ext4_xattr_entry *entry = IFIRST(header); |
| 232 | int error = -EFSCORRUPTED; |
| 233 | |
| 234 | if (((void *) header >= end) || |
Eric Biggers | 1996250 | 2016-10-15 09:39:31 -0400 | [diff] [blame] | 235 | (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC))) |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 236 | goto errout; |
| 237 | error = ext4_xattr_check_names(entry, end, entry); |
| 238 | errout: |
| 239 | if (error) |
| 240 | __ext4_error_inode(inode, function, line, 0, |
| 241 | "corrupted in-inode xattr"); |
| 242 | return error; |
| 243 | } |
| 244 | |
| 245 | #define xattr_check_inode(inode, header, end) \ |
| 246 | __xattr_check_inode((inode), (header), (end), __func__, __LINE__) |
| 247 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 248 | static inline int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 249 | ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 250 | { |
| 251 | size_t value_size = le32_to_cpu(entry->e_value_size); |
| 252 | |
| 253 | if (entry->e_value_block != 0 || value_size > size || |
| 254 | le16_to_cpu(entry->e_value_offs) + value_size > size) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 255 | return -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 260 | ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 261 | const char *name, size_t size, int sorted) |
| 262 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 263 | struct ext4_xattr_entry *entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 264 | size_t name_len; |
| 265 | int cmp = 1; |
| 266 | |
| 267 | if (name == NULL) |
| 268 | return -EINVAL; |
| 269 | name_len = strlen(name); |
| 270 | entry = *pentry; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 271 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 272 | cmp = name_index - entry->e_name_index; |
| 273 | if (!cmp) |
| 274 | cmp = name_len - entry->e_name_len; |
| 275 | if (!cmp) |
| 276 | cmp = memcmp(name, entry->e_name, name_len); |
| 277 | if (cmp <= 0 && (sorted || cmp == 0)) |
| 278 | break; |
| 279 | } |
| 280 | *pentry = entry; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 281 | if (!cmp && ext4_xattr_check_entry(entry, size)) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 282 | return -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 283 | return cmp ? -ENODATA : 0; |
| 284 | } |
| 285 | |
| 286 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 287 | 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] | 288 | void *buffer, size_t buffer_size) |
| 289 | { |
| 290 | struct buffer_head *bh = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 291 | struct ext4_xattr_entry *entry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 292 | size_t size; |
| 293 | int error; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 294 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 295 | |
| 296 | ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", |
| 297 | name_index, name, buffer, (long)buffer_size); |
| 298 | |
| 299 | error = -ENODATA; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 300 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 301 | goto cleanup; |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 302 | ea_idebug(inode, "reading block %llu", |
| 303 | (unsigned long long)EXT4_I(inode)->i_file_acl); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 304 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 305 | if (!bh) |
| 306 | goto cleanup; |
| 307 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 308 | 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] | 309 | if (ext4_xattr_check_block(inode, bh)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 310 | bad_block: |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 311 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 312 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 313 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 314 | goto cleanup; |
| 315 | } |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 316 | ext4_xattr_cache_insert(ext4_mb_cache, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 317 | entry = BFIRST(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 318 | error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 319 | if (error == -EFSCORRUPTED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 320 | goto bad_block; |
| 321 | if (error) |
| 322 | goto cleanup; |
| 323 | size = le32_to_cpu(entry->e_value_size); |
| 324 | if (buffer) { |
| 325 | error = -ERANGE; |
| 326 | if (size > buffer_size) |
| 327 | goto cleanup; |
| 328 | memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs), |
| 329 | size); |
| 330 | } |
| 331 | error = size; |
| 332 | |
| 333 | cleanup: |
| 334 | brelse(bh); |
| 335 | return error; |
| 336 | } |
| 337 | |
Tao Ma | 879b382 | 2012-12-05 10:28:46 -0500 | [diff] [blame] | 338 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 339 | 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] | 340 | void *buffer, size_t buffer_size) |
| 341 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 342 | struct ext4_xattr_ibody_header *header; |
| 343 | struct ext4_xattr_entry *entry; |
| 344 | struct ext4_inode *raw_inode; |
| 345 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 346 | size_t size; |
| 347 | void *end; |
| 348 | int error; |
| 349 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 350 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 351 | return -ENODATA; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 352 | error = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 353 | if (error) |
| 354 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 355 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 356 | header = IHDR(inode, raw_inode); |
| 357 | entry = IFIRST(header); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 358 | 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] | 359 | error = xattr_check_inode(inode, header, end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 360 | if (error) |
| 361 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 362 | error = ext4_xattr_find_entry(&entry, name_index, name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 363 | end - (void *)entry, 0); |
| 364 | if (error) |
| 365 | goto cleanup; |
| 366 | size = le32_to_cpu(entry->e_value_size); |
| 367 | if (buffer) { |
| 368 | error = -ERANGE; |
| 369 | if (size > buffer_size) |
| 370 | goto cleanup; |
| 371 | memcpy(buffer, (void *)IFIRST(header) + |
| 372 | le16_to_cpu(entry->e_value_offs), size); |
| 373 | } |
| 374 | error = size; |
| 375 | |
| 376 | cleanup: |
| 377 | brelse(iloc.bh); |
| 378 | return error; |
| 379 | } |
| 380 | |
| 381 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 382 | * ext4_xattr_get() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 383 | * |
| 384 | * Copy an extended attribute into the buffer |
| 385 | * provided, or compute the buffer size required. |
| 386 | * Buffer is NULL to compute the size of the buffer required. |
| 387 | * |
| 388 | * Returns a negative error number on failure, or the number of bytes |
| 389 | * used / required on success. |
| 390 | */ |
| 391 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 392 | ext4_xattr_get(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 393 | void *buffer, size_t buffer_size) |
| 394 | { |
| 395 | int error; |
| 396 | |
Zhang Zhen | 230b8c1a | 2014-05-12 09:57:59 -0400 | [diff] [blame] | 397 | if (strlen(name) > 255) |
| 398 | return -ERANGE; |
| 399 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 400 | down_read(&EXT4_I(inode)->xattr_sem); |
| 401 | error = ext4_xattr_ibody_get(inode, name_index, name, buffer, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 402 | buffer_size); |
| 403 | if (error == -ENODATA) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 404 | error = ext4_xattr_block_get(inode, name_index, name, buffer, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 405 | buffer_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 406 | up_read(&EXT4_I(inode)->xattr_sem); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 407 | return error; |
| 408 | } |
| 409 | |
| 410 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 411 | ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 412 | char *buffer, size_t buffer_size) |
| 413 | { |
| 414 | size_t rest = buffer_size; |
| 415 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 416 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 417 | const struct xattr_handler *handler = |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 418 | ext4_xattr_handler(entry->e_name_index); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 419 | |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 420 | if (handler && (!handler->list || handler->list(dentry))) { |
| 421 | const char *prefix = handler->prefix ?: handler->name; |
| 422 | size_t prefix_len = strlen(prefix); |
| 423 | size_t size = prefix_len + entry->e_name_len + 1; |
| 424 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 425 | if (buffer) { |
| 426 | if (size > rest) |
| 427 | return -ERANGE; |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 428 | memcpy(buffer, prefix, prefix_len); |
| 429 | buffer += prefix_len; |
| 430 | memcpy(buffer, entry->e_name, entry->e_name_len); |
| 431 | buffer += entry->e_name_len; |
| 432 | *buffer++ = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 433 | } |
| 434 | rest -= size; |
| 435 | } |
| 436 | } |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 437 | return buffer_size - rest; /* total size */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 441 | 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] | 442 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 443 | struct inode *inode = d_inode(dentry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 444 | struct buffer_head *bh = NULL; |
| 445 | int error; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 446 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 447 | |
| 448 | ea_idebug(inode, "buffer=%p, buffer_size=%ld", |
| 449 | buffer, (long)buffer_size); |
| 450 | |
| 451 | error = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 452 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 453 | goto cleanup; |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 454 | ea_idebug(inode, "reading block %llu", |
| 455 | (unsigned long long)EXT4_I(inode)->i_file_acl); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 456 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 457 | error = -EIO; |
| 458 | if (!bh) |
| 459 | goto cleanup; |
| 460 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 461 | 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] | 462 | if (ext4_xattr_check_block(inode, bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 463 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 464 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 465 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 466 | goto cleanup; |
| 467 | } |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 468 | ext4_xattr_cache_insert(ext4_mb_cache, bh); |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 469 | error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 470 | |
| 471 | cleanup: |
| 472 | brelse(bh); |
| 473 | |
| 474 | return error; |
| 475 | } |
| 476 | |
| 477 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 478 | 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] | 479 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 480 | struct inode *inode = d_inode(dentry); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 481 | struct ext4_xattr_ibody_header *header; |
| 482 | struct ext4_inode *raw_inode; |
| 483 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 484 | void *end; |
| 485 | int error; |
| 486 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 487 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 488 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 489 | error = ext4_get_inode_loc(inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 490 | if (error) |
| 491 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 492 | raw_inode = ext4_raw_inode(&iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 493 | header = IHDR(inode, raw_inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 494 | 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] | 495 | error = xattr_check_inode(inode, header, end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 496 | if (error) |
| 497 | goto cleanup; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 498 | error = ext4_xattr_list_entries(dentry, IFIRST(header), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 499 | buffer, buffer_size); |
| 500 | |
| 501 | cleanup: |
| 502 | brelse(iloc.bh); |
| 503 | return error; |
| 504 | } |
| 505 | |
| 506 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 507 | * ext4_xattr_list() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 508 | * |
| 509 | * Copy a list of attribute names into the buffer |
| 510 | * provided, or compute the buffer size required. |
| 511 | * Buffer is NULL to compute the size of the buffer required. |
| 512 | * |
| 513 | * Returns a negative error number on failure, or the number of bytes |
| 514 | * used / required on success. |
| 515 | */ |
Mingming Cao | d3a95d4 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 516 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 517 | ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 518 | { |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 519 | int ret, ret2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 520 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 521 | down_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 522 | ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size); |
| 523 | if (ret < 0) |
| 524 | goto errout; |
| 525 | if (buffer) { |
| 526 | buffer += ret; |
| 527 | buffer_size -= ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 528 | } |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 529 | ret = ext4_xattr_block_list(dentry, buffer, buffer_size); |
| 530 | if (ret < 0) |
| 531 | goto errout; |
| 532 | ret += ret2; |
| 533 | errout: |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 534 | up_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
Theodore Ts'o | eaeef86 | 2011-01-10 12:10:07 -0500 | [diff] [blame] | 535 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 539 | * 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] | 540 | * not set, set it. |
| 541 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 542 | static void ext4_xattr_update_super_block(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 543 | struct super_block *sb) |
| 544 | { |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 545 | if (ext4_has_feature_xattr(sb)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 546 | return; |
| 547 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 548 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 549 | 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] | 550 | ext4_set_feature_xattr(sb); |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 551 | ext4_handle_dirty_super(handle, sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 552 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /* |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 556 | * Release the xattr block BH: If the reference count is > 1, decrement it; |
| 557 | * otherwise free the block. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 558 | */ |
| 559 | static void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 560 | ext4_xattr_release_block(handle_t *handle, struct inode *inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 561 | struct buffer_head *bh) |
| 562 | { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 563 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
| 564 | u32 hash, ref; |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 565 | int error = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 566 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 567 | BUFFER_TRACE(bh, "get_write_access"); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 568 | error = ext4_journal_get_write_access(handle, bh); |
| 569 | if (error) |
| 570 | goto out; |
| 571 | |
| 572 | lock_buffer(bh); |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 573 | hash = le32_to_cpu(BHDR(bh)->h_hash); |
| 574 | ref = le32_to_cpu(BHDR(bh)->h_refcount); |
| 575 | if (ref == 1) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 576 | ea_bdebug(bh, "refcount now=0; freeing"); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 577 | /* |
| 578 | * This must happen under buffer lock for |
| 579 | * ext4_xattr_block_set() to reliably detect freed block |
| 580 | */ |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 581 | mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 582 | get_bh(bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 583 | unlock_buffer(bh); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 584 | ext4_free_blocks(handle, inode, bh, 0, 1, |
| 585 | EXT4_FREE_BLOCKS_METADATA | |
| 586 | EXT4_FREE_BLOCKS_FORGET); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 587 | } else { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 588 | ref--; |
| 589 | BHDR(bh)->h_refcount = cpu_to_le32(ref); |
| 590 | if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) { |
| 591 | struct mb_cache_entry *ce; |
| 592 | |
| 593 | ce = mb_cache_entry_get(ext4_mb_cache, hash, |
| 594 | bh->b_blocknr); |
| 595 | if (ce) { |
| 596 | ce->e_reusable = 1; |
| 597 | mb_cache_entry_put(ext4_mb_cache, ce); |
| 598 | } |
| 599 | } |
| 600 | |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 601 | ext4_xattr_block_csum_set(inode, bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 602 | /* |
| 603 | * Beware of this ugliness: Releasing of xattr block references |
| 604 | * from different inodes can race and so we have to protect |
| 605 | * from a race where someone else frees the block (and releases |
| 606 | * its journal_head) before we are done dirtying the buffer. In |
| 607 | * nojournal mode this race is harmless and we actually cannot |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 608 | * call ext4_handle_dirty_metadata() with locked buffer as |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 609 | * that function can call sync_dirty_buffer() so for that case |
| 610 | * we handle the dirtying after unlocking the buffer. |
| 611 | */ |
| 612 | if (ext4_handle_valid(handle)) |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 613 | error = ext4_handle_dirty_metadata(handle, inode, bh); |
Eric Sandeen | c1bb05a | 2012-02-20 23:06:18 -0500 | [diff] [blame] | 614 | unlock_buffer(bh); |
Jan Kara | ec4cb1a | 2014-04-07 10:54:21 -0400 | [diff] [blame] | 615 | if (!ext4_handle_valid(handle)) |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 616 | error = ext4_handle_dirty_metadata(handle, inode, bh); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 617 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 618 | ext4_handle_sync(handle); |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 619 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1)); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 620 | ea_bdebug(bh, "refcount now=%d; releasing", |
| 621 | le32_to_cpu(BHDR(bh)->h_refcount)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 622 | } |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 623 | out: |
| 624 | ext4_std_error(inode->i_sb, error); |
| 625 | return; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 628 | /* |
| 629 | * Find the available free space for EAs. This also returns the total number of |
| 630 | * bytes used by EA entries. |
| 631 | */ |
| 632 | static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last, |
| 633 | size_t *min_offs, void *base, int *total) |
| 634 | { |
| 635 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 636 | if (last->e_value_size) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 637 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 638 | if (offs < *min_offs) |
| 639 | *min_offs = offs; |
| 640 | } |
Theodore Ts'o | 7b1b2c1 | 2014-02-19 20:15:21 -0500 | [diff] [blame] | 641 | if (total) |
| 642 | *total += EXT4_XATTR_LEN(last->e_name_len); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 643 | } |
| 644 | return (*min_offs - ((void *)last - base) - sizeof(__u32)); |
| 645 | } |
| 646 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 647 | static int |
Theodore Ts'o | b469713 | 2018-06-13 00:23:11 -0400 | [diff] [blame] | 648 | ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s, |
| 649 | struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 650 | { |
Theodore Ts'o | b469713 | 2018-06-13 00:23:11 -0400 | [diff] [blame] | 651 | struct ext4_xattr_entry *last, *next; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 652 | size_t free, min_offs = s->end - s->base, name_len = strlen(i->name); |
| 653 | |
| 654 | /* Compute min_offs and last. */ |
| 655 | last = s->first; |
Theodore Ts'o | b469713 | 2018-06-13 00:23:11 -0400 | [diff] [blame] | 656 | for (; !IS_LAST_ENTRY(last); last = next) { |
| 657 | next = EXT4_XATTR_NEXT(last); |
| 658 | if ((void *)next >= s->end) { |
| 659 | EXT4_ERROR_INODE(inode, "corrupted xattr entries"); |
Daniel Rosenberg | a295ff4 | 2018-10-15 15:10:52 -0700 | [diff] [blame] | 660 | return -EFSCORRUPTED; |
Theodore Ts'o | b469713 | 2018-06-13 00:23:11 -0400 | [diff] [blame] | 661 | } |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 662 | if (last->e_value_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 663 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 664 | if (offs < min_offs) |
| 665 | min_offs = offs; |
| 666 | } |
| 667 | } |
| 668 | free = min_offs - ((void *)last - s->base) - sizeof(__u32); |
| 669 | if (!s->not_found) { |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 670 | if (s->here->e_value_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 671 | size_t size = le32_to_cpu(s->here->e_value_size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 672 | free += EXT4_XATTR_SIZE(size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 673 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 674 | free += EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 675 | } |
| 676 | if (i->value) { |
Wei Yuan | 5f80f62 | 2015-04-02 23:50:48 -0400 | [diff] [blame] | 677 | if (free < EXT4_XATTR_LEN(name_len) + |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 678 | EXT4_XATTR_SIZE(i->value_len)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 679 | return -ENOSPC; |
| 680 | } |
| 681 | |
| 682 | if (i->value && s->not_found) { |
| 683 | /* Insert the new name. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 684 | size_t size = EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 685 | size_t rest = (void *)last - (void *)s->here + sizeof(__u32); |
| 686 | memmove((void *)s->here + size, s->here, rest); |
| 687 | memset(s->here, 0, size); |
| 688 | s->here->e_name_index = i->name_index; |
| 689 | s->here->e_name_len = name_len; |
| 690 | memcpy(s->here->e_name, i->name, name_len); |
| 691 | } else { |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 692 | if (s->here->e_value_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 693 | void *first_val = s->base + min_offs; |
| 694 | size_t offs = le16_to_cpu(s->here->e_value_offs); |
| 695 | void *val = s->base + offs; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 696 | size_t size = EXT4_XATTR_SIZE( |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 697 | le32_to_cpu(s->here->e_value_size)); |
| 698 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 699 | if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 700 | /* The old and the new value have the same |
| 701 | size. Just replace. */ |
| 702 | s->here->e_value_size = |
| 703 | cpu_to_le32(i->value_len); |
Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 704 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
| 705 | memset(val, 0, size); |
| 706 | } else { |
| 707 | /* Clear pad bytes first. */ |
| 708 | memset(val + size - EXT4_XATTR_PAD, 0, |
| 709 | EXT4_XATTR_PAD); |
| 710 | memcpy(val, i->value, i->value_len); |
| 711 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | /* Remove the old value. */ |
| 716 | memmove(first_val + size, first_val, val - first_val); |
| 717 | memset(first_val, 0, size); |
| 718 | s->here->e_value_size = 0; |
| 719 | s->here->e_value_offs = 0; |
| 720 | min_offs += size; |
| 721 | |
| 722 | /* Adjust all value offsets. */ |
| 723 | last = s->first; |
| 724 | while (!IS_LAST_ENTRY(last)) { |
| 725 | size_t o = le16_to_cpu(last->e_value_offs); |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 726 | if (last->e_value_size && o < offs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 727 | last->e_value_offs = |
| 728 | cpu_to_le16(o + size); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 729 | last = EXT4_XATTR_NEXT(last); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | if (!i->value) { |
| 733 | /* Remove the old name. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 734 | size_t size = EXT4_XATTR_LEN(name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 735 | last = ENTRY((void *)last - size); |
| 736 | memmove(s->here, (void *)s->here + size, |
| 737 | (void *)last - (void *)s->here + sizeof(__u32)); |
| 738 | memset(last, 0, size); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | if (i->value) { |
| 743 | /* Insert the new value. */ |
| 744 | s->here->e_value_size = cpu_to_le32(i->value_len); |
| 745 | if (i->value_len) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 746 | size_t size = EXT4_XATTR_SIZE(i->value_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 747 | void *val = s->base + min_offs - size; |
| 748 | s->here->e_value_offs = cpu_to_le16(min_offs - size); |
Theodore Ts'o | bd9926e | 2012-12-11 03:31:49 -0500 | [diff] [blame] | 749 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
| 750 | memset(val, 0, size); |
| 751 | } else { |
| 752 | /* Clear the pad bytes first. */ |
| 753 | memset(val + size - EXT4_XATTR_PAD, 0, |
| 754 | EXT4_XATTR_PAD); |
| 755 | memcpy(val, i->value, i->value_len); |
| 756 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | return 0; |
| 760 | } |
| 761 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 762 | struct ext4_xattr_block_find { |
| 763 | struct ext4_xattr_search s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 764 | struct buffer_head *bh; |
| 765 | }; |
| 766 | |
| 767 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 768 | ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, |
| 769 | struct ext4_xattr_block_find *bs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 770 | { |
| 771 | struct super_block *sb = inode->i_sb; |
| 772 | int error; |
| 773 | |
| 774 | ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", |
| 775 | i->name_index, i->name, i->value, (long)i->value_len); |
| 776 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 777 | if (EXT4_I(inode)->i_file_acl) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 778 | /* The inode already has an extended attribute block. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 779 | bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 780 | error = -EIO; |
| 781 | if (!bs->bh) |
| 782 | goto cleanup; |
| 783 | ea_bdebug(bs->bh, "b_count=%d, refcount=%d", |
| 784 | atomic_read(&(bs->bh->b_count)), |
| 785 | le32_to_cpu(BHDR(bs->bh)->h_refcount)); |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 786 | if (ext4_xattr_check_block(inode, bs->bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 787 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 788 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 789 | error = -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 790 | goto cleanup; |
| 791 | } |
| 792 | /* Find the named attribute. */ |
| 793 | bs->s.base = BHDR(bs->bh); |
| 794 | bs->s.first = BFIRST(bs->bh); |
| 795 | bs->s.end = bs->bh->b_data + bs->bh->b_size; |
| 796 | bs->s.here = bs->s.first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 797 | error = ext4_xattr_find_entry(&bs->s.here, i->name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 798 | i->name, bs->bh->b_size, 1); |
| 799 | if (error && error != -ENODATA) |
| 800 | goto cleanup; |
| 801 | bs->s.not_found = error; |
| 802 | } |
| 803 | error = 0; |
| 804 | |
| 805 | cleanup: |
| 806 | return error; |
| 807 | } |
| 808 | |
| 809 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 810 | ext4_xattr_block_set(handle_t *handle, struct inode *inode, |
| 811 | struct ext4_xattr_info *i, |
| 812 | struct ext4_xattr_block_find *bs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 813 | { |
| 814 | struct super_block *sb = inode->i_sb; |
| 815 | struct buffer_head *new_bh = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 816 | struct ext4_xattr_search *s = &bs->s; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 817 | struct mb_cache_entry *ce = NULL; |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 818 | int error = 0; |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 819 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 820 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 821 | #define header(x) ((struct ext4_xattr_header *)(x)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 822 | |
| 823 | if (i->value && i->value_len > sb->s_blocksize) |
| 824 | return -ENOSPC; |
| 825 | if (s->base) { |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 826 | BUFFER_TRACE(bs->bh, "get_write_access"); |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 827 | error = ext4_journal_get_write_access(handle, bs->bh); |
| 828 | if (error) |
| 829 | goto cleanup; |
| 830 | lock_buffer(bs->bh); |
| 831 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 832 | if (header(s->base)->h_refcount == cpu_to_le32(1)) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 833 | __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash); |
| 834 | |
| 835 | /* |
| 836 | * This must happen under buffer lock for |
| 837 | * ext4_xattr_block_set() to reliably detect modified |
| 838 | * block |
| 839 | */ |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 840 | mb_cache_entry_delete_block(ext4_mb_cache, hash, |
| 841 | bs->bh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 842 | ea_bdebug(bs->bh, "modifying in-place"); |
Theodore Ts'o | b469713 | 2018-06-13 00:23:11 -0400 | [diff] [blame] | 843 | error = ext4_xattr_set_entry(i, s, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 844 | if (!error) { |
| 845 | if (!IS_LAST_ENTRY(s->first)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 846 | ext4_xattr_rehash(header(s->base), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 847 | s->here); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 848 | } |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 849 | ext4_xattr_block_csum_set(inode, bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 850 | unlock_buffer(bs->bh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 851 | if (error == -EFSCORRUPTED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 852 | goto bad_block; |
| 853 | if (!error) |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 854 | error = ext4_handle_dirty_metadata(handle, |
| 855 | inode, |
| 856 | bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 857 | if (error) |
| 858 | goto cleanup; |
| 859 | goto inserted; |
| 860 | } else { |
| 861 | int offset = (char *)s->here - bs->bh->b_data; |
| 862 | |
Mingming Cao | 8a2bfdc | 2007-02-28 20:13:35 -0800 | [diff] [blame] | 863 | unlock_buffer(bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 864 | ea_bdebug(bs->bh, "cloning"); |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 865 | s->base = kmalloc(bs->bh->b_size, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 866 | error = -ENOMEM; |
| 867 | if (s->base == NULL) |
| 868 | goto cleanup; |
| 869 | memcpy(s->base, BHDR(bs->bh), bs->bh->b_size); |
| 870 | s->first = ENTRY(header(s->base)+1); |
| 871 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 872 | s->here = ENTRY(s->base + offset); |
| 873 | s->end = s->base + bs->bh->b_size; |
| 874 | } |
| 875 | } else { |
| 876 | /* Allocate a buffer where we construct the new block. */ |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 877 | s->base = kzalloc(sb->s_blocksize, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 878 | /* assert(header == s->base) */ |
| 879 | error = -ENOMEM; |
| 880 | if (s->base == NULL) |
| 881 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 882 | header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 883 | header(s->base)->h_blocks = cpu_to_le32(1); |
| 884 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 885 | s->first = ENTRY(header(s->base)+1); |
| 886 | s->here = ENTRY(header(s->base)+1); |
| 887 | s->end = s->base + sb->s_blocksize; |
| 888 | } |
| 889 | |
Theodore Ts'o | b469713 | 2018-06-13 00:23:11 -0400 | [diff] [blame] | 890 | error = ext4_xattr_set_entry(i, s, inode); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 891 | if (error == -EFSCORRUPTED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 892 | goto bad_block; |
| 893 | if (error) |
| 894 | goto cleanup; |
| 895 | if (!IS_LAST_ENTRY(s->first)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 896 | ext4_xattr_rehash(header(s->base), s->here); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 897 | |
| 898 | inserted: |
| 899 | if (!IS_LAST_ENTRY(s->first)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 900 | new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 901 | if (new_bh) { |
| 902 | /* We found an identical block in the cache. */ |
| 903 | if (new_bh == bs->bh) |
| 904 | ea_bdebug(new_bh, "keeping"); |
| 905 | else { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 906 | u32 ref; |
| 907 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 908 | /* The old block is released after updating |
| 909 | the inode. */ |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 910 | error = dquot_alloc_block(inode, |
| 911 | EXT4_C2B(EXT4_SB(sb), 1)); |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 912 | if (error) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 913 | goto cleanup; |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 914 | BUFFER_TRACE(new_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 915 | error = ext4_journal_get_write_access(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 916 | new_bh); |
| 917 | if (error) |
| 918 | goto cleanup_dquot; |
| 919 | lock_buffer(new_bh); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 920 | /* |
| 921 | * We have to be careful about races with |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 922 | * freeing, rehashing or adding references to |
| 923 | * xattr block. Once we hold buffer lock xattr |
| 924 | * block's state is stable so we can check |
| 925 | * whether the block got freed / rehashed or |
| 926 | * not. Since we unhash mbcache entry under |
| 927 | * buffer lock when freeing / rehashing xattr |
| 928 | * block, checking whether entry is still |
| 929 | * hashed is reliable. Same rules hold for |
| 930 | * e_reusable handling. |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 931 | */ |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 932 | if (hlist_bl_unhashed(&ce->e_hash_list) || |
| 933 | !ce->e_reusable) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 934 | /* |
| 935 | * Undo everything and check mbcache |
| 936 | * again. |
| 937 | */ |
| 938 | unlock_buffer(new_bh); |
| 939 | dquot_free_block(inode, |
| 940 | EXT4_C2B(EXT4_SB(sb), |
| 941 | 1)); |
| 942 | brelse(new_bh); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 943 | mb_cache_entry_put(ext4_mb_cache, ce); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 944 | ce = NULL; |
| 945 | new_bh = NULL; |
| 946 | goto inserted; |
| 947 | } |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 948 | ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1; |
| 949 | BHDR(new_bh)->h_refcount = cpu_to_le32(ref); |
| 950 | if (ref >= EXT4_XATTR_REFCOUNT_MAX) |
| 951 | ce->e_reusable = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 952 | ea_bdebug(new_bh, "reusing; refcount now=%d", |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 953 | ref); |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 954 | ext4_xattr_block_csum_set(inode, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 955 | unlock_buffer(new_bh); |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 956 | error = ext4_handle_dirty_metadata(handle, |
| 957 | inode, |
| 958 | new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 959 | if (error) |
| 960 | goto cleanup_dquot; |
| 961 | } |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 962 | mb_cache_entry_touch(ext4_mb_cache, ce); |
| 963 | mb_cache_entry_put(ext4_mb_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 964 | ce = NULL; |
| 965 | } else if (bs->bh && s->base == bs->bh->b_data) { |
| 966 | /* We were modifying this block in-place. */ |
| 967 | ea_bdebug(bs->bh, "keeping this block"); |
Tahsin Erdogan | b496b24 | 2017-08-05 22:41:42 -0400 | [diff] [blame] | 968 | ext4_xattr_cache_insert(ext4_mb_cache, bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 969 | new_bh = bs->bh; |
| 970 | get_bh(new_bh); |
| 971 | } else { |
| 972 | /* We need to allocate a new block */ |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 973 | ext4_fsblk_t goal, block; |
| 974 | |
| 975 | goal = ext4_group_first_block_no(sb, |
Akinobu Mita | d00a6d7 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 976 | EXT4_I(inode)->i_block_group); |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 977 | |
| 978 | /* non-extent files can't have physical blocks past 2^32 */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 979 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 980 | goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; |
| 981 | |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 982 | block = ext4_new_meta_blocks(handle, inode, goal, 0, |
| 983 | NULL, &error); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 984 | if (error) |
| 985 | goto cleanup; |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 986 | |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 987 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 988 | BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS); |
| 989 | |
Joe Perches | ace36ad | 2012-03-19 23:11:43 -0400 | [diff] [blame] | 990 | ea_idebug(inode, "creating block %llu", |
| 991 | (unsigned long long)block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 992 | |
| 993 | new_bh = sb_getblk(sb, block); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 994 | if (unlikely(!new_bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 995 | error = -ENOMEM; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 996 | getblk_failed: |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 997 | ext4_free_blocks(handle, inode, NULL, block, 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 998 | EXT4_FREE_BLOCKS_METADATA); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 999 | goto cleanup; |
| 1000 | } |
| 1001 | lock_buffer(new_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1002 | error = ext4_journal_get_create_access(handle, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1003 | if (error) { |
| 1004 | unlock_buffer(new_bh); |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1005 | error = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1006 | goto getblk_failed; |
| 1007 | } |
| 1008 | memcpy(new_bh->b_data, s->base, new_bh->b_size); |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1009 | ext4_xattr_block_csum_set(inode, new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1010 | set_buffer_uptodate(new_bh); |
| 1011 | unlock_buffer(new_bh); |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 1012 | ext4_xattr_cache_insert(ext4_mb_cache, new_bh); |
Theodore Ts'o | 9a9dc3ec | 2017-03-25 17:22:47 -0400 | [diff] [blame] | 1013 | error = ext4_handle_dirty_metadata(handle, inode, |
| 1014 | new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1015 | if (error) |
| 1016 | goto cleanup; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | /* Update the inode. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1021 | 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] | 1022 | |
| 1023 | /* Drop the previous xattr block. */ |
| 1024 | if (bs->bh && bs->bh != new_bh) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1025 | ext4_xattr_release_block(handle, inode, bs->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1026 | error = 0; |
| 1027 | |
| 1028 | cleanup: |
| 1029 | if (ce) |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1030 | mb_cache_entry_put(ext4_mb_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1031 | brelse(new_bh); |
| 1032 | if (!(bs->bh && s->base == bs->bh->b_data)) |
| 1033 | kfree(s->base); |
| 1034 | |
| 1035 | return error; |
| 1036 | |
| 1037 | cleanup_dquot: |
Lukas Czerner | 1231b3a | 2013-02-18 12:12:07 -0500 | [diff] [blame] | 1038 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1039 | goto cleanup; |
| 1040 | |
| 1041 | bad_block: |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1042 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 1043 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1044 | goto cleanup; |
| 1045 | |
| 1046 | #undef header |
| 1047 | } |
| 1048 | |
Tao Ma | 879b382 | 2012-12-05 10:28:46 -0500 | [diff] [blame] | 1049 | int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, |
| 1050 | struct ext4_xattr_ibody_find *is) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1051 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1052 | struct ext4_xattr_ibody_header *header; |
| 1053 | struct ext4_inode *raw_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1054 | int error; |
| 1055 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1056 | if (EXT4_I(inode)->i_extra_isize == 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1057 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1058 | raw_inode = ext4_raw_inode(&is->iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1059 | header = IHDR(inode, raw_inode); |
| 1060 | is->s.base = is->s.first = IFIRST(header); |
| 1061 | is->s.here = is->s.first; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1062 | 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] | 1063 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 1064 | error = xattr_check_inode(inode, header, is->s.end); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1065 | if (error) |
| 1066 | return error; |
| 1067 | /* Find the named attribute. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1068 | error = ext4_xattr_find_entry(&is->s.here, i->name_index, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1069 | i->name, is->s.end - |
| 1070 | (void *)is->s.base, 0); |
| 1071 | if (error && error != -ENODATA) |
| 1072 | return error; |
| 1073 | is->s.not_found = error; |
| 1074 | } |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 1078 | int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode, |
| 1079 | struct ext4_xattr_info *i, |
| 1080 | struct ext4_xattr_ibody_find *is) |
| 1081 | { |
| 1082 | struct ext4_xattr_ibody_header *header; |
| 1083 | struct ext4_xattr_search *s = &is->s; |
| 1084 | int error; |
| 1085 | |
| 1086 | if (EXT4_I(inode)->i_extra_isize == 0) |
| 1087 | return -ENOSPC; |
Theodore Ts'o | b469713 | 2018-06-13 00:23:11 -0400 | [diff] [blame] | 1088 | error = ext4_xattr_set_entry(i, s, inode); |
Theodore Ts'o | 7dd5589 | 2018-06-16 23:41:59 -0400 | [diff] [blame] | 1089 | if (error) |
| 1090 | return error; |
Tao Ma | 0d812f7 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 1091 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
| 1092 | if (!IS_LAST_ENTRY(s->first)) { |
| 1093 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
| 1094 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
| 1095 | } else { |
| 1096 | header->h_magic = cpu_to_le32(0); |
| 1097 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
| 1098 | } |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode, |
| 1103 | struct ext4_xattr_info *i, |
| 1104 | struct ext4_xattr_ibody_find *is) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1105 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1106 | struct ext4_xattr_ibody_header *header; |
| 1107 | struct ext4_xattr_search *s = &is->s; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1108 | int error; |
| 1109 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1110 | if (EXT4_I(inode)->i_extra_isize == 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1111 | return -ENOSPC; |
Theodore Ts'o | b469713 | 2018-06-13 00:23:11 -0400 | [diff] [blame] | 1112 | error = ext4_xattr_set_entry(i, s, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1113 | if (error) |
| 1114 | return error; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1115 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1116 | if (!IS_LAST_ENTRY(s->first)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1117 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1118 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1119 | } else { |
| 1120 | header->h_magic = cpu_to_le32(0); |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1121 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1122 | } |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
Jan Kara | 3fd1646 | 2016-02-22 22:43:04 -0500 | [diff] [blame] | 1126 | static int ext4_xattr_value_same(struct ext4_xattr_search *s, |
| 1127 | struct ext4_xattr_info *i) |
| 1128 | { |
| 1129 | void *value; |
| 1130 | |
| 1131 | if (le32_to_cpu(s->here->e_value_size) != i->value_len) |
| 1132 | return 0; |
| 1133 | value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs); |
| 1134 | return !memcmp(value, i->value, i->value_len); |
| 1135 | } |
| 1136 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1137 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1138 | * ext4_xattr_set_handle() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1139 | * |
Wang Sheng-Hui | 6e9510b | 2011-01-10 12:10:30 -0500 | [diff] [blame] | 1140 | * Create, replace or remove an extended attribute for this inode. Value |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1141 | * is NULL to remove an existing extended attribute, and non-NULL to |
| 1142 | * either replace an existing extended attribute, or create a new extended |
| 1143 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE |
| 1144 | * specify that an extended attribute must exist and must not exist |
| 1145 | * previous to the call, respectively. |
| 1146 | * |
| 1147 | * Returns 0, or a negative error number on failure. |
| 1148 | */ |
| 1149 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1150 | 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] | 1151 | const char *name, const void *value, size_t value_len, |
| 1152 | int flags) |
| 1153 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1154 | struct ext4_xattr_info i = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1155 | .name_index = name_index, |
| 1156 | .name = name, |
| 1157 | .value = value, |
| 1158 | .value_len = value_len, |
| 1159 | |
| 1160 | }; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1161 | struct ext4_xattr_ibody_find is = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1162 | .s = { .not_found = -ENODATA, }, |
| 1163 | }; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1164 | struct ext4_xattr_block_find bs = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1165 | .s = { .not_found = -ENODATA, }, |
| 1166 | }; |
Theodore Ts'o | da1e402 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1167 | int no_expand; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1168 | int error; |
| 1169 | |
| 1170 | if (!name) |
| 1171 | return -EINVAL; |
| 1172 | if (strlen(name) > 255) |
| 1173 | return -ERANGE; |
Theodore Ts'o | da1e402 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1174 | ext4_write_lock_xattr(inode, &no_expand); |
Kalpak Shah | 4d20c68 | 2008-10-08 23:21:54 -0400 | [diff] [blame] | 1175 | |
Eric Sandeen | 6654361 | 2011-10-26 03:32:07 -0400 | [diff] [blame] | 1176 | error = ext4_reserve_inode_write(handle, inode, &is.iloc); |
Eric Sandeen | 86ebfd0 | 2009-11-15 15:30:52 -0500 | [diff] [blame] | 1177 | if (error) |
| 1178 | goto cleanup; |
| 1179 | |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 1180 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1181 | struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc); |
| 1182 | 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] | 1183 | ext4_clear_inode_state(inode, EXT4_STATE_NEW); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1186 | error = ext4_xattr_ibody_find(inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1187 | if (error) |
| 1188 | goto cleanup; |
| 1189 | if (is.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1190 | error = ext4_xattr_block_find(inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1191 | if (error) |
| 1192 | goto cleanup; |
| 1193 | if (is.s.not_found && bs.s.not_found) { |
| 1194 | error = -ENODATA; |
| 1195 | if (flags & XATTR_REPLACE) |
| 1196 | goto cleanup; |
| 1197 | error = 0; |
| 1198 | if (!value) |
| 1199 | goto cleanup; |
| 1200 | } else { |
| 1201 | error = -EEXIST; |
| 1202 | if (flags & XATTR_CREATE) |
| 1203 | goto cleanup; |
| 1204 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1205 | if (!value) { |
| 1206 | if (!is.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1207 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1208 | else if (!bs.s.not_found) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1209 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1210 | } else { |
Jan Kara | 3fd1646 | 2016-02-22 22:43:04 -0500 | [diff] [blame] | 1211 | error = 0; |
| 1212 | /* Xattr value did not change? Save us some work and bail out */ |
| 1213 | if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i)) |
| 1214 | goto cleanup; |
| 1215 | if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i)) |
| 1216 | goto cleanup; |
| 1217 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1218 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1219 | if (!error && !bs.s.not_found) { |
| 1220 | i.value = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1221 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1222 | } else if (error == -ENOSPC) { |
Tiger Yang | 7e01c8e | 2008-05-14 16:05:47 -0700 | [diff] [blame] | 1223 | if (EXT4_I(inode)->i_file_acl && !bs.s.base) { |
Vasily Averin | b66102a | 2018-11-07 11:07:01 -0500 | [diff] [blame] | 1224 | brelse(bs.bh); |
| 1225 | bs.bh = NULL; |
Tiger Yang | 7e01c8e | 2008-05-14 16:05:47 -0700 | [diff] [blame] | 1226 | error = ext4_xattr_block_find(inode, &i, &bs); |
| 1227 | if (error) |
| 1228 | goto cleanup; |
| 1229 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1230 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1231 | if (error) |
| 1232 | goto cleanup; |
| 1233 | if (!is.s.not_found) { |
| 1234 | i.value = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1235 | error = ext4_xattr_ibody_set(handle, inode, &i, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1236 | &is); |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | if (!error) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1241 | ext4_xattr_update_super_block(handle, inode->i_sb); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 1242 | inode->i_ctime = ext4_current_time(inode); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1243 | if (!value) |
Theodore Ts'o | da1e402 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1244 | no_expand = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1245 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1246 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1247 | * The bh is consumed by ext4_mark_iloc_dirty, even with |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1248 | * error != 0. |
| 1249 | */ |
| 1250 | is.iloc.bh = NULL; |
| 1251 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1252 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | cleanup: |
| 1256 | brelse(is.iloc.bh); |
| 1257 | brelse(bs.bh); |
Theodore Ts'o | da1e402 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1258 | ext4_write_unlock_xattr(inode, &no_expand); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1259 | return error; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1263 | * ext4_xattr_set() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1264 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1265 | * Like ext4_xattr_set_handle, but start from an inode. This extended |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1266 | * attribute modification is a filesystem transaction by itself. |
| 1267 | * |
| 1268 | * Returns 0, or a negative error number on failure. |
| 1269 | */ |
| 1270 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1271 | ext4_xattr_set(struct inode *inode, int name_index, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1272 | const void *value, size_t value_len, int flags) |
| 1273 | { |
| 1274 | handle_t *handle; |
| 1275 | int error, retries = 0; |
Theodore Ts'o | 95eaefb | 2013-02-09 15:23:03 -0500 | [diff] [blame] | 1276 | int credits = ext4_jbd2_credits_xattr(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1277 | |
| 1278 | retry: |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 1279 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1280 | if (IS_ERR(handle)) { |
| 1281 | error = PTR_ERR(handle); |
| 1282 | } else { |
| 1283 | int error2; |
| 1284 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1285 | error = ext4_xattr_set_handle(handle, inode, name_index, name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1286 | value, value_len, flags); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1287 | error2 = ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1288 | if (error == -ENOSPC && |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1289 | ext4_should_retry_alloc(inode->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1290 | goto retry; |
| 1291 | if (error == 0) |
| 1292 | error = error2; |
| 1293 | } |
| 1294 | |
| 1295 | return error; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1299 | * Shift the EA entries in the inode to create space for the increased |
| 1300 | * i_extra_isize. |
| 1301 | */ |
| 1302 | static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry, |
| 1303 | int value_offs_shift, void *to, |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 1304 | void *from, size_t n) |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1305 | { |
| 1306 | struct ext4_xattr_entry *last = entry; |
| 1307 | int new_offs; |
| 1308 | |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 1309 | /* We always shift xattr headers further thus offsets get lower */ |
| 1310 | BUG_ON(value_offs_shift > 0); |
| 1311 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1312 | /* Adjust the value offsets of the entries */ |
| 1313 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 1314 | if (last->e_value_size) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1315 | new_offs = le16_to_cpu(last->e_value_offs) + |
| 1316 | value_offs_shift; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1317 | last->e_value_offs = cpu_to_le16(new_offs); |
| 1318 | } |
| 1319 | } |
| 1320 | /* Shift the entries by n bytes */ |
| 1321 | memmove(to, from, n); |
| 1322 | } |
| 1323 | |
| 1324 | /* |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1325 | * Move xattr pointed to by 'entry' from inode into external xattr block |
| 1326 | */ |
| 1327 | static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, |
| 1328 | struct ext4_inode *raw_inode, |
| 1329 | struct ext4_xattr_entry *entry) |
| 1330 | { |
| 1331 | struct ext4_xattr_ibody_find *is = NULL; |
| 1332 | struct ext4_xattr_block_find *bs = NULL; |
| 1333 | char *buffer = NULL, *b_entry_name = NULL; |
| 1334 | size_t value_offs, value_size; |
| 1335 | struct ext4_xattr_info i = { |
| 1336 | .value = NULL, |
| 1337 | .value_len = 0, |
| 1338 | .name_index = entry->e_name_index, |
| 1339 | }; |
| 1340 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); |
| 1341 | int error; |
| 1342 | |
| 1343 | value_offs = le16_to_cpu(entry->e_value_offs); |
| 1344 | value_size = le32_to_cpu(entry->e_value_size); |
| 1345 | |
| 1346 | is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); |
| 1347 | bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS); |
| 1348 | buffer = kmalloc(value_size, GFP_NOFS); |
| 1349 | b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS); |
| 1350 | if (!is || !bs || !buffer || !b_entry_name) { |
| 1351 | error = -ENOMEM; |
| 1352 | goto out; |
| 1353 | } |
| 1354 | |
| 1355 | is->s.not_found = -ENODATA; |
| 1356 | bs->s.not_found = -ENODATA; |
| 1357 | is->iloc.bh = NULL; |
| 1358 | bs->bh = NULL; |
| 1359 | |
| 1360 | /* Save the entry name and the entry value */ |
| 1361 | memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size); |
| 1362 | memcpy(b_entry_name, entry->e_name, entry->e_name_len); |
| 1363 | b_entry_name[entry->e_name_len] = '\0'; |
| 1364 | i.name = b_entry_name; |
| 1365 | |
| 1366 | error = ext4_get_inode_loc(inode, &is->iloc); |
| 1367 | if (error) |
| 1368 | goto out; |
| 1369 | |
| 1370 | error = ext4_xattr_ibody_find(inode, &i, is); |
| 1371 | if (error) |
| 1372 | goto out; |
| 1373 | |
| 1374 | /* Remove the chosen entry from the inode */ |
| 1375 | error = ext4_xattr_ibody_set(handle, inode, &i, is); |
| 1376 | if (error) |
| 1377 | goto out; |
| 1378 | |
| 1379 | i.name = b_entry_name; |
| 1380 | i.value = buffer; |
| 1381 | i.value_len = value_size; |
| 1382 | error = ext4_xattr_block_find(inode, &i, bs); |
| 1383 | if (error) |
| 1384 | goto out; |
| 1385 | |
| 1386 | /* Add entry which was removed from the inode into the block */ |
| 1387 | error = ext4_xattr_block_set(handle, inode, &i, bs); |
| 1388 | if (error) |
| 1389 | goto out; |
| 1390 | error = 0; |
| 1391 | out: |
| 1392 | kfree(b_entry_name); |
| 1393 | kfree(buffer); |
| 1394 | if (is) |
| 1395 | brelse(is->iloc.bh); |
Vasily Averin | 82dfeb8 | 2018-11-07 11:10:21 -0500 | [diff] [blame] | 1396 | if (bs) |
| 1397 | brelse(bs->bh); |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1398 | kfree(is); |
| 1399 | kfree(bs); |
| 1400 | |
| 1401 | return error; |
| 1402 | } |
| 1403 | |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1404 | static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode, |
| 1405 | struct ext4_inode *raw_inode, |
| 1406 | int isize_diff, size_t ifree, |
| 1407 | size_t bfree, int *total_ino) |
| 1408 | { |
| 1409 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); |
| 1410 | struct ext4_xattr_entry *small_entry; |
| 1411 | struct ext4_xattr_entry *entry; |
| 1412 | struct ext4_xattr_entry *last; |
| 1413 | unsigned int entry_size; /* EA entry size */ |
| 1414 | unsigned int total_size; /* EA entry size + value size */ |
| 1415 | unsigned int min_total_size; |
| 1416 | int error; |
| 1417 | |
| 1418 | while (isize_diff > ifree) { |
| 1419 | entry = NULL; |
| 1420 | small_entry = NULL; |
| 1421 | min_total_size = ~0U; |
| 1422 | last = IFIRST(header); |
| 1423 | /* Find the entry best suited to be pushed into EA block */ |
| 1424 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
Theodore Ts'o | 3a28247 | 2018-06-16 15:40:48 -0400 | [diff] [blame] | 1425 | /* never move system.data out of the inode */ |
| 1426 | if ((last->e_name_len == 4) && |
| 1427 | (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) && |
| 1428 | !memcmp(last->e_name, "data", 4)) |
| 1429 | continue; |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1430 | total_size = |
| 1431 | EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) + |
| 1432 | EXT4_XATTR_LEN(last->e_name_len); |
| 1433 | if (total_size <= bfree && |
| 1434 | total_size < min_total_size) { |
| 1435 | if (total_size + ifree < isize_diff) { |
| 1436 | small_entry = last; |
| 1437 | } else { |
| 1438 | entry = last; |
| 1439 | min_total_size = total_size; |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | if (entry == NULL) { |
| 1445 | if (small_entry == NULL) |
| 1446 | return -ENOSPC; |
| 1447 | entry = small_entry; |
| 1448 | } |
| 1449 | |
| 1450 | entry_size = EXT4_XATTR_LEN(entry->e_name_len); |
| 1451 | total_size = entry_size + |
| 1452 | EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)); |
| 1453 | error = ext4_xattr_move_to_block(handle, inode, raw_inode, |
| 1454 | entry); |
| 1455 | if (error) |
| 1456 | return error; |
| 1457 | |
| 1458 | *total_ino -= entry_size; |
| 1459 | ifree += total_size; |
| 1460 | bfree -= total_size; |
| 1461 | } |
| 1462 | |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
Jan Kara | 3f2571c | 2016-08-29 15:42:11 -0400 | [diff] [blame] | 1466 | /* |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1467 | * Expand an inode by new_extra_isize bytes when EAs are present. |
| 1468 | * Returns 0 on success or negative error number on failure. |
| 1469 | */ |
| 1470 | int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize, |
| 1471 | struct ext4_inode *raw_inode, handle_t *handle) |
| 1472 | { |
| 1473 | struct ext4_xattr_ibody_header *header; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1474 | struct buffer_head *bh = NULL; |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1475 | size_t min_offs; |
| 1476 | size_t ifree, bfree; |
Theodore Ts'o | 7b1b2c1 | 2014-02-19 20:15:21 -0500 | [diff] [blame] | 1477 | int total_ino; |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1478 | void *base, *end; |
Jan Kara | d014119 | 2016-08-11 11:50:30 -0400 | [diff] [blame] | 1479 | int error = 0, tried_min_extra_isize = 0; |
Aneesh Kumar K.V | ac39849 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1480 | 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] | 1481 | int isize_diff; /* How much do we need to grow i_extra_isize */ |
Theodore Ts'o | da1e402 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1482 | int no_expand; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1483 | |
Theodore Ts'o | da1e402 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1484 | if (ext4_write_trylock_xattr(inode, &no_expand) == 0) |
| 1485 | return 0; |
| 1486 | |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1487 | retry: |
Jan Kara | d014119 | 2016-08-11 11:50:30 -0400 | [diff] [blame] | 1488 | isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize; |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1489 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) |
| 1490 | goto out; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1491 | |
| 1492 | header = IHDR(inode, raw_inode); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1493 | |
| 1494 | /* |
| 1495 | * Check if enough free space is available in the inode to shift the |
| 1496 | * entries ahead by new_extra_isize. |
| 1497 | */ |
| 1498 | |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1499 | base = IFIRST(header); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1500 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
| 1501 | min_offs = end - base; |
Theodore Ts'o | 2eb64177 | 2018-12-19 12:28:13 -0500 | [diff] [blame] | 1502 | total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1503 | |
Theodore Ts'o | 9e92f48 | 2016-03-22 16:13:15 -0400 | [diff] [blame] | 1504 | error = xattr_check_inode(inode, header, end); |
| 1505 | if (error) |
| 1506 | goto cleanup; |
| 1507 | |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1508 | ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1509 | if (ifree >= isize_diff) |
| 1510 | goto shift; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1511 | |
| 1512 | /* |
| 1513 | * Enough free space isn't available in the inode, check if |
| 1514 | * EA block can hold new_extra_isize bytes. |
| 1515 | */ |
| 1516 | if (EXT4_I(inode)->i_file_acl) { |
| 1517 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
| 1518 | error = -EIO; |
| 1519 | if (!bh) |
| 1520 | goto cleanup; |
Darrick J. Wong | cc8e94f | 2012-04-29 18:43:10 -0400 | [diff] [blame] | 1521 | if (ext4_xattr_check_block(inode, bh)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1522 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 1523 | EXT4_I(inode)->i_file_acl); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1524 | error = -EFSCORRUPTED; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1525 | goto cleanup; |
| 1526 | } |
| 1527 | base = BHDR(bh); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1528 | end = bh->b_data + bh->b_size; |
| 1529 | min_offs = end - base; |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1530 | bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base, |
| 1531 | NULL); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1532 | if (bfree + ifree < isize_diff) { |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1533 | if (!tried_min_extra_isize && s_min_extra_isize) { |
| 1534 | tried_min_extra_isize++; |
| 1535 | new_extra_isize = s_min_extra_isize; |
| 1536 | brelse(bh); |
| 1537 | goto retry; |
| 1538 | } |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1539 | error = -ENOSPC; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1540 | goto cleanup; |
| 1541 | } |
| 1542 | } else { |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1543 | bfree = inode->i_sb->s_blocksize; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1544 | } |
| 1545 | |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1546 | error = ext4_xattr_make_inode_space(handle, inode, raw_inode, |
| 1547 | isize_diff, ifree, bfree, |
| 1548 | &total_ino); |
| 1549 | if (error) { |
| 1550 | if (error == -ENOSPC && !tried_min_extra_isize && |
| 1551 | s_min_extra_isize) { |
| 1552 | tried_min_extra_isize++; |
| 1553 | new_extra_isize = s_min_extra_isize; |
| 1554 | brelse(bh); |
| 1555 | goto retry; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1556 | } |
Jan Kara | dfa2064 | 2016-08-29 15:44:11 -0400 | [diff] [blame] | 1557 | goto cleanup; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1558 | } |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1559 | shift: |
| 1560 | /* Adjust the offsets and shift the remaining entries ahead */ |
Jan Kara | 6e0cd08 | 2016-08-29 15:43:11 -0400 | [diff] [blame] | 1561 | ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1562 | - new_extra_isize, (void *)raw_inode + |
| 1563 | EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize, |
Jan Kara | 9440571 | 2016-08-29 15:41:11 -0400 | [diff] [blame] | 1564 | (void *)header, total_ino); |
Jan Kara | e3014d1 | 2016-08-29 15:38:11 -0400 | [diff] [blame] | 1565 | EXT4_I(inode)->i_extra_isize = new_extra_isize; |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1566 | brelse(bh); |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1567 | out: |
Theodore Ts'o | da1e402 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1568 | ext4_write_unlock_xattr(inode, &no_expand); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1569 | return 0; |
| 1570 | |
| 1571 | cleanup: |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1572 | brelse(bh); |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1573 | /* |
Theodore Ts'o | da1e402 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1574 | * Inode size expansion failed; don't try again |
Jan Kara | 2e81a4e | 2016-08-11 12:38:55 -0400 | [diff] [blame] | 1575 | */ |
Theodore Ts'o | da1e402 | 2017-01-11 21:50:46 -0500 | [diff] [blame] | 1576 | no_expand = 1; |
| 1577 | ext4_write_unlock_xattr(inode, &no_expand); |
Kalpak Shah | 6dd4ee7 | 2007-07-18 09:19:57 -0400 | [diff] [blame] | 1578 | return error; |
| 1579 | } |
| 1580 | |
| 1581 | |
| 1582 | |
| 1583 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1584 | * ext4_xattr_delete_inode() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1585 | * |
| 1586 | * Free extended attribute resources associated with this inode. This |
| 1587 | * is called immediately before an inode is freed. We have exclusive |
| 1588 | * access to the inode. |
| 1589 | */ |
| 1590 | void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1591 | ext4_xattr_delete_inode(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1592 | { |
| 1593 | struct buffer_head *bh = NULL; |
| 1594 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1595 | if (!EXT4_I(inode)->i_file_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1596 | goto cleanup; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1597 | bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1598 | if (!bh) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1599 | EXT4_ERROR_INODE(inode, "block %llu read error", |
| 1600 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1601 | goto cleanup; |
| 1602 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1603 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1604 | BHDR(bh)->h_blocks != cpu_to_le32(1)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1605 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 1606 | EXT4_I(inode)->i_file_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1607 | goto cleanup; |
| 1608 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1609 | ext4_xattr_release_block(handle, inode, bh); |
| 1610 | EXT4_I(inode)->i_file_acl = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1611 | |
| 1612 | cleanup: |
| 1613 | brelse(bh); |
| 1614 | } |
| 1615 | |
| 1616 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1617 | * ext4_xattr_cache_insert() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1618 | * |
| 1619 | * Create a new entry in the extended attribute cache, and insert |
| 1620 | * it unless such an entry is already in the cache. |
| 1621 | * |
| 1622 | * Returns 0, or a negative error number on failure. |
| 1623 | */ |
| 1624 | static void |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1625 | 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] | 1626 | { |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1627 | struct ext4_xattr_header *header = BHDR(bh); |
| 1628 | __u32 hash = le32_to_cpu(header->h_hash); |
| 1629 | int reusable = le32_to_cpu(header->h_refcount) < |
| 1630 | EXT4_XATTR_REFCOUNT_MAX; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1631 | int error; |
| 1632 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1633 | error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash, |
Andreas Gruenbacher | 6048c64 | 2016-02-22 22:44:04 -0500 | [diff] [blame] | 1634 | bh->b_blocknr, reusable); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1635 | if (error) { |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1636 | if (error == -EBUSY) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1637 | ea_bdebug(bh, "already in cache"); |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1638 | } else |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1639 | ea_bdebug(bh, "inserting [%x]", (int)hash); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1643 | * ext4_xattr_cmp() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1644 | * |
| 1645 | * Compare two extended attribute blocks for equality. |
| 1646 | * |
| 1647 | * Returns 0 if the blocks are equal, 1 if they differ, and |
| 1648 | * a negative error number on errors. |
| 1649 | */ |
| 1650 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1651 | ext4_xattr_cmp(struct ext4_xattr_header *header1, |
| 1652 | struct ext4_xattr_header *header2) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1653 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1654 | struct ext4_xattr_entry *entry1, *entry2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1655 | |
| 1656 | entry1 = ENTRY(header1+1); |
| 1657 | entry2 = ENTRY(header2+1); |
| 1658 | while (!IS_LAST_ENTRY(entry1)) { |
| 1659 | if (IS_LAST_ENTRY(entry2)) |
| 1660 | return 1; |
| 1661 | if (entry1->e_hash != entry2->e_hash || |
| 1662 | entry1->e_name_index != entry2->e_name_index || |
| 1663 | entry1->e_name_len != entry2->e_name_len || |
| 1664 | entry1->e_value_size != entry2->e_value_size || |
| 1665 | memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) |
| 1666 | return 1; |
| 1667 | if (entry1->e_value_block != 0 || entry2->e_value_block != 0) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1668 | return -EFSCORRUPTED; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1669 | if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), |
| 1670 | (char *)header2 + le16_to_cpu(entry2->e_value_offs), |
| 1671 | le32_to_cpu(entry1->e_value_size))) |
| 1672 | return 1; |
| 1673 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1674 | entry1 = EXT4_XATTR_NEXT(entry1); |
| 1675 | entry2 = EXT4_XATTR_NEXT(entry2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1676 | } |
| 1677 | if (!IS_LAST_ENTRY(entry2)) |
| 1678 | return 1; |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1683 | * ext4_xattr_cache_find() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1684 | * |
| 1685 | * Find an identical extended attribute block. |
| 1686 | * |
| 1687 | * Returns a pointer to the block found, or NULL if such a block was |
| 1688 | * not found or an error occurred. |
| 1689 | */ |
| 1690 | static struct buffer_head * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1691 | ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header, |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1692 | struct mb_cache_entry **pce) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1693 | { |
| 1694 | __u32 hash = le32_to_cpu(header->h_hash); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1695 | struct mb_cache_entry *ce; |
| 1696 | struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1697 | |
| 1698 | if (!header->h_hash) |
| 1699 | return NULL; /* never share */ |
| 1700 | ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1701 | ce = mb_cache_entry_find_first(ext4_mb_cache, hash); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1702 | while (ce) { |
| 1703 | struct buffer_head *bh; |
| 1704 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1705 | bh = sb_bread(inode->i_sb, ce->e_block); |
| 1706 | if (!bh) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1707 | EXT4_ERROR_INODE(inode, "block %lu read error", |
| 1708 | (unsigned long) ce->e_block); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1709 | } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1710 | *pce = ce; |
| 1711 | return bh; |
| 1712 | } |
| 1713 | brelse(bh); |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1714 | ce = mb_cache_entry_find_next(ext4_mb_cache, ce); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1715 | } |
| 1716 | return NULL; |
| 1717 | } |
| 1718 | |
| 1719 | #define NAME_HASH_SHIFT 5 |
| 1720 | #define VALUE_HASH_SHIFT 16 |
| 1721 | |
| 1722 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1723 | * ext4_xattr_hash_entry() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1724 | * |
| 1725 | * Compute the hash of an extended attribute. |
| 1726 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1727 | static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header, |
| 1728 | struct ext4_xattr_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1729 | { |
| 1730 | __u32 hash = 0; |
| 1731 | char *name = entry->e_name; |
| 1732 | int n; |
| 1733 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1734 | for (n = 0; n < entry->e_name_len; n++) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1735 | hash = (hash << NAME_HASH_SHIFT) ^ |
| 1736 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ |
| 1737 | *name++; |
| 1738 | } |
| 1739 | |
Jan Kara | 1cba423 | 2016-08-29 15:40:11 -0400 | [diff] [blame] | 1740 | if (entry->e_value_size != 0) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1741 | __le32 *value = (__le32 *)((char *)header + |
| 1742 | le16_to_cpu(entry->e_value_offs)); |
| 1743 | for (n = (le32_to_cpu(entry->e_value_size) + |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1744 | EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1745 | hash = (hash << VALUE_HASH_SHIFT) ^ |
| 1746 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ |
| 1747 | le32_to_cpu(*value++); |
| 1748 | } |
| 1749 | } |
| 1750 | entry->e_hash = cpu_to_le32(hash); |
| 1751 | } |
| 1752 | |
| 1753 | #undef NAME_HASH_SHIFT |
| 1754 | #undef VALUE_HASH_SHIFT |
| 1755 | |
| 1756 | #define BLOCK_HASH_SHIFT 16 |
| 1757 | |
| 1758 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1759 | * ext4_xattr_rehash() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1760 | * |
| 1761 | * Re-compute the extended attribute hash value after an entry has changed. |
| 1762 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1763 | static void ext4_xattr_rehash(struct ext4_xattr_header *header, |
| 1764 | struct ext4_xattr_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1765 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1766 | struct ext4_xattr_entry *here; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1767 | __u32 hash = 0; |
| 1768 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1769 | ext4_xattr_hash_entry(header, entry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1770 | here = ENTRY(header+1); |
| 1771 | while (!IS_LAST_ENTRY(here)) { |
| 1772 | if (!here->e_hash) { |
| 1773 | /* Block is not shared if an entry's hash value == 0 */ |
| 1774 | hash = 0; |
| 1775 | break; |
| 1776 | } |
| 1777 | hash = (hash << BLOCK_HASH_SHIFT) ^ |
| 1778 | (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ |
| 1779 | le32_to_cpu(here->e_hash); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1780 | here = EXT4_XATTR_NEXT(here); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1781 | } |
| 1782 | header->h_hash = cpu_to_le32(hash); |
| 1783 | } |
| 1784 | |
| 1785 | #undef BLOCK_HASH_SHIFT |
| 1786 | |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 1787 | #define HASH_BUCKET_BITS 10 |
| 1788 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1789 | struct mb_cache * |
Jan Kara | 82939d7 | 2016-02-22 11:50:13 -0500 | [diff] [blame] | 1790 | ext4_xattr_create_cache(void) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1791 | { |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1792 | return mb_cache_create(HASH_BUCKET_BITS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1795 | void ext4_xattr_destroy_cache(struct mb_cache *cache) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1796 | { |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 1797 | if (cache) |
Jan Kara | 7a2508e | 2016-02-22 22:35:22 -0500 | [diff] [blame] | 1798 | mb_cache_destroy(cache); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1799 | } |
T Makphaibulchoke | 9c191f7 | 2014-03-18 19:24:49 -0400 | [diff] [blame] | 1800 | |