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