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