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