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