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