| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * linux/fs/ext4/xattr.c |
| 4 | * |
| 5 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> |
| 6 | * |
| 7 | * Fix by Harrison Xing <harrison@mountainviewdata.com>. |
| 8 | * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>. |
| 9 | * Extended attributes for symlinks and special files added per |
| 10 | * suggestion of Luka Renko <luka.renko@hermes.si>. |
| 11 | * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>, |
| 12 | * Red Hat Inc. |
| 13 | * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz |
| 14 | * and Andreas Gruenbacher <agruen@suse.de>. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Extended attributes are stored directly in inodes (on file systems with |
| 19 | * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl |
| 20 | * field contains the block number if an inode uses an additional block. All |
| 21 | * attributes must fit in the inode and one additional block. Blocks that |
| 22 | * contain the identical set of attributes may be shared among several inodes. |
| 23 | * Identical blocks are detected by keeping a cache of blocks that have |
| 24 | * recently been accessed. |
| 25 | * |
| 26 | * The attributes in inodes and on blocks have a different header; the entries |
| 27 | * are stored in the same format: |
| 28 | * |
| 29 | * +------------------+ |
| 30 | * | header | |
| 31 | * | entry 1 | | |
| 32 | * | entry 2 | | growing downwards |
| 33 | * | entry 3 | v |
| 34 | * | four null bytes | |
| 35 | * | . . . | |
| 36 | * | value 1 | ^ |
| 37 | * | value 3 | | growing upwards |
| 38 | * | value 2 | | |
| 39 | * +------------------+ |
| 40 | * |
| 41 | * The header is followed by multiple entry descriptors. In disk blocks, the |
| 42 | * entry descriptors are kept sorted. In inodes, they are unsorted. The |
| 43 | * attribute values are aligned to the end of the block in no specific order. |
| 44 | * |
| 45 | * Locking strategy |
| 46 | * ---------------- |
| 47 | * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem. |
| 48 | * EA blocks are only changed if they are exclusive to an inode, so |
| 49 | * holding xattr_sem also means that nothing but the EA block's reference |
| 50 | * count can change. Multiple writers to the same block are synchronized |
| 51 | * by the buffer lock. |
| 52 | */ |
| 53 | |
| 54 | #include <linux/init.h> |
| 55 | #include <linux/fs.h> |
| 56 | #include <linux/slab.h> |
| 57 | #include <linux/mbcache.h> |
| 58 | #include <linux/quotaops.h> |
| 59 | #include <linux/iversion.h> |
| 60 | #include "ext4_jbd2.h" |
| 61 | #include "ext4.h" |
| 62 | #include "xattr.h" |
| 63 | #include "acl.h" |
| 64 | |
| 65 | #ifdef EXT4_XATTR_DEBUG |
| 66 | # define ea_idebug(inode, fmt, ...) \ |
| 67 | printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \ |
| 68 | inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__) |
| 69 | # define ea_bdebug(bh, fmt, ...) \ |
| 70 | printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \ |
| 71 | bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__) |
| 72 | #else |
| 73 | # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__) |
| 74 | # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__) |
| 75 | #endif |
| 76 | |
| 77 | static void ext4_xattr_block_cache_insert(struct mb_cache *, |
| 78 | struct buffer_head *); |
| 79 | static struct buffer_head * |
| 80 | ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *, |
| 81 | struct mb_cache_entry **); |
| 82 | static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value, |
| 83 | size_t value_count); |
| 84 | static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value, |
| 85 | size_t value_count); |
| 86 | static void ext4_xattr_rehash(struct ext4_xattr_header *); |
| 87 | |
| 88 | static const struct xattr_handler * const ext4_xattr_handler_map[] = { |
| 89 | [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler, |
| 90 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
| 91 | [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &nop_posix_acl_access, |
| 92 | [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default, |
| 93 | #endif |
| 94 | [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler, |
| 95 | #ifdef CONFIG_EXT4_FS_SECURITY |
| 96 | [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler, |
| 97 | #endif |
| 98 | [EXT4_XATTR_INDEX_HURD] = &ext4_xattr_hurd_handler, |
| 99 | }; |
| 100 | |
| 101 | const struct xattr_handler * const ext4_xattr_handlers[] = { |
| 102 | &ext4_xattr_user_handler, |
| 103 | &ext4_xattr_trusted_handler, |
| 104 | #ifdef CONFIG_EXT4_FS_SECURITY |
| 105 | &ext4_xattr_security_handler, |
| 106 | #endif |
| 107 | &ext4_xattr_hurd_handler, |
| 108 | NULL |
| 109 | }; |
| 110 | |
| 111 | #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \ |
| 112 | inode->i_sb->s_fs_info)->s_ea_block_cache) |
| 113 | |
| 114 | #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \ |
| 115 | inode->i_sb->s_fs_info)->s_ea_inode_cache) |
| 116 | |
| 117 | static int |
| 118 | ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, |
| 119 | struct inode *inode); |
| 120 | |
| 121 | #ifdef CONFIG_LOCKDEP |
| 122 | void ext4_xattr_inode_set_class(struct inode *ea_inode) |
| 123 | { |
| 124 | struct ext4_inode_info *ei = EXT4_I(ea_inode); |
| 125 | |
| 126 | lockdep_set_subclass(&ea_inode->i_rwsem, 1); |
| 127 | (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */ |
| 128 | lockdep_set_subclass(&ei->i_data_sem, I_DATA_SEM_EA); |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | static __le32 ext4_xattr_block_csum(struct inode *inode, |
| 133 | sector_t block_nr, |
| 134 | struct ext4_xattr_header *hdr) |
| 135 | { |
| 136 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 137 | __u32 csum; |
| 138 | __le64 dsk_block_nr = cpu_to_le64(block_nr); |
| 139 | __u32 dummy_csum = 0; |
| 140 | int offset = offsetof(struct ext4_xattr_header, h_checksum); |
| 141 | |
| 142 | csum = ext4_chksum(sbi->s_csum_seed, (__u8 *)&dsk_block_nr, |
| 143 | sizeof(dsk_block_nr)); |
| 144 | csum = ext4_chksum(csum, (__u8 *)hdr, offset); |
| 145 | csum = ext4_chksum(csum, (__u8 *)&dummy_csum, sizeof(dummy_csum)); |
| 146 | offset += sizeof(dummy_csum); |
| 147 | csum = ext4_chksum(csum, (__u8 *)hdr + offset, |
| 148 | EXT4_BLOCK_SIZE(inode->i_sb) - offset); |
| 149 | |
| 150 | return cpu_to_le32(csum); |
| 151 | } |
| 152 | |
| 153 | static int ext4_xattr_block_csum_verify(struct inode *inode, |
| 154 | struct buffer_head *bh) |
| 155 | { |
| 156 | struct ext4_xattr_header *hdr = BHDR(bh); |
| 157 | int ret = 1; |
| 158 | |
| 159 | if (ext4_has_feature_metadata_csum(inode->i_sb)) { |
| 160 | lock_buffer(bh); |
| 161 | ret = (hdr->h_checksum == ext4_xattr_block_csum(inode, |
| 162 | bh->b_blocknr, hdr)); |
| 163 | unlock_buffer(bh); |
| 164 | } |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | static void ext4_xattr_block_csum_set(struct inode *inode, |
| 169 | struct buffer_head *bh) |
| 170 | { |
| 171 | if (ext4_has_feature_metadata_csum(inode->i_sb)) |
| 172 | BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode, |
| 173 | bh->b_blocknr, BHDR(bh)); |
| 174 | } |
| 175 | |
| 176 | static inline const char *ext4_xattr_prefix(int name_index, |
| 177 | struct dentry *dentry) |
| 178 | { |
| 179 | const struct xattr_handler *handler = NULL; |
| 180 | |
| 181 | if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map)) |
| 182 | handler = ext4_xattr_handler_map[name_index]; |
| 183 | |
| 184 | if (!xattr_handler_can_list(handler, dentry)) |
| 185 | return NULL; |
| 186 | |
| 187 | return xattr_prefix(handler); |
| 188 | } |
| 189 | |
| 190 | static int |
| 191 | check_xattrs(struct inode *inode, struct buffer_head *bh, |
| 192 | struct ext4_xattr_entry *entry, void *end, void *value_start, |
| 193 | const char *function, unsigned int line) |
| 194 | { |
| 195 | struct ext4_xattr_entry *e = entry; |
| 196 | int err = -EFSCORRUPTED; |
| 197 | char *err_str; |
| 198 | |
| 199 | if (bh) { |
| 200 | if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || |
| 201 | BHDR(bh)->h_blocks != cpu_to_le32(1)) { |
| 202 | err_str = "invalid header"; |
| 203 | goto errout; |
| 204 | } |
| 205 | if (buffer_verified(bh)) |
| 206 | return 0; |
| 207 | if (!ext4_xattr_block_csum_verify(inode, bh)) { |
| 208 | err = -EFSBADCRC; |
| 209 | err_str = "invalid checksum"; |
| 210 | goto errout; |
| 211 | } |
| 212 | } else { |
| 213 | struct ext4_xattr_ibody_header *header = value_start; |
| 214 | |
| 215 | header -= 1; |
| 216 | if (end - (void *)header < sizeof(*header) + sizeof(u32)) { |
| 217 | err_str = "in-inode xattr block too small"; |
| 218 | goto errout; |
| 219 | } |
| 220 | if (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { |
| 221 | err_str = "bad magic number in in-inode xattr"; |
| 222 | goto errout; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* Find the end of the names list */ |
| 227 | while (!IS_LAST_ENTRY(e)) { |
| 228 | struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e); |
| 229 | if ((void *)next >= end) { |
| 230 | err_str = "e_name out of bounds"; |
| 231 | goto errout; |
| 232 | } |
| 233 | if (strnlen(e->e_name, e->e_name_len) != e->e_name_len) { |
| 234 | err_str = "bad e_name length"; |
| 235 | goto errout; |
| 236 | } |
| 237 | e = next; |
| 238 | } |
| 239 | |
| 240 | /* Check the values */ |
| 241 | while (!IS_LAST_ENTRY(entry)) { |
| 242 | u32 size = le32_to_cpu(entry->e_value_size); |
| 243 | unsigned long ea_ino = le32_to_cpu(entry->e_value_inum); |
| 244 | |
| 245 | if (!ext4_has_feature_ea_inode(inode->i_sb) && ea_ino) { |
| 246 | err_str = "ea_inode specified without ea_inode feature enabled"; |
| 247 | goto errout; |
| 248 | } |
| 249 | if (ea_ino && ((ea_ino == EXT4_ROOT_INO) || |
| 250 | !ext4_valid_inum(inode->i_sb, ea_ino))) { |
| 251 | err_str = "invalid ea_ino"; |
| 252 | goto errout; |
| 253 | } |
| 254 | if (size > EXT4_XATTR_SIZE_MAX) { |
| 255 | err_str = "e_value size too large"; |
| 256 | goto errout; |
| 257 | } |
| 258 | |
| 259 | if (size != 0 && entry->e_value_inum == 0) { |
| 260 | u16 offs = le16_to_cpu(entry->e_value_offs); |
| 261 | void *value; |
| 262 | |
| 263 | /* |
| 264 | * The value cannot overlap the names, and the value |
| 265 | * with padding cannot extend beyond 'end'. Check both |
| 266 | * the padded and unpadded sizes, since the size may |
| 267 | * overflow to 0 when adding padding. |
| 268 | */ |
| 269 | if (offs > end - value_start) { |
| 270 | err_str = "e_value out of bounds"; |
| 271 | goto errout; |
| 272 | } |
| 273 | value = value_start + offs; |
| 274 | if (value < (void *)e + sizeof(u32) || |
| 275 | size > end - value || |
| 276 | EXT4_XATTR_SIZE(size) > end - value) { |
| 277 | err_str = "overlapping e_value "; |
| 278 | goto errout; |
| 279 | } |
| 280 | } |
| 281 | entry = EXT4_XATTR_NEXT(entry); |
| 282 | } |
| 283 | if (bh) |
| 284 | set_buffer_verified(bh); |
| 285 | return 0; |
| 286 | |
| 287 | errout: |
| 288 | if (bh) |
| 289 | __ext4_error_inode(inode, function, line, 0, -err, |
| 290 | "corrupted xattr block %llu: %s", |
| 291 | (unsigned long long) bh->b_blocknr, |
| 292 | err_str); |
| 293 | else |
| 294 | __ext4_error_inode(inode, function, line, 0, -err, |
| 295 | "corrupted in-inode xattr: %s", err_str); |
| 296 | return err; |
| 297 | } |
| 298 | |
| 299 | static inline int |
| 300 | __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh, |
| 301 | const char *function, unsigned int line) |
| 302 | { |
| 303 | return check_xattrs(inode, bh, BFIRST(bh), bh->b_data + bh->b_size, |
| 304 | bh->b_data, function, line); |
| 305 | } |
| 306 | |
| 307 | #define ext4_xattr_check_block(inode, bh) \ |
| 308 | __ext4_xattr_check_block((inode), (bh), __func__, __LINE__) |
| 309 | |
| 310 | |
| 311 | int |
| 312 | __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header, |
| 313 | void *end, const char *function, unsigned int line) |
| 314 | { |
| 315 | return check_xattrs(inode, NULL, IFIRST(header), end, IFIRST(header), |
| 316 | function, line); |
| 317 | } |
| 318 | |
| 319 | static int |
| 320 | xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry, |
| 321 | void *end, int name_index, const char *name, int sorted) |
| 322 | { |
| 323 | struct ext4_xattr_entry *entry, *next; |
| 324 | size_t name_len; |
| 325 | int cmp = 1; |
| 326 | |
| 327 | if (name == NULL) |
| 328 | return -EINVAL; |
| 329 | name_len = strlen(name); |
| 330 | for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) { |
| 331 | next = EXT4_XATTR_NEXT(entry); |
| 332 | if ((void *) next >= end) { |
| 333 | EXT4_ERROR_INODE(inode, "corrupted xattr entries"); |
| 334 | return -EFSCORRUPTED; |
| 335 | } |
| 336 | cmp = name_index - entry->e_name_index; |
| 337 | if (!cmp) |
| 338 | cmp = name_len - entry->e_name_len; |
| 339 | if (!cmp) |
| 340 | cmp = memcmp(name, entry->e_name, name_len); |
| 341 | if (cmp <= 0 && (sorted || cmp == 0)) |
| 342 | break; |
| 343 | } |
| 344 | *pentry = entry; |
| 345 | return cmp ? -ENODATA : 0; |
| 346 | } |
| 347 | |
| 348 | static u32 |
| 349 | ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size) |
| 350 | { |
| 351 | return ext4_chksum(sbi->s_csum_seed, buffer, size); |
| 352 | } |
| 353 | |
| 354 | static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode) |
| 355 | { |
| 356 | return ((u64) inode_get_ctime_sec(ea_inode) << 32) | |
| 357 | (u32) inode_peek_iversion_raw(ea_inode); |
| 358 | } |
| 359 | |
| 360 | static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count) |
| 361 | { |
| 362 | inode_set_ctime(ea_inode, (u32)(ref_count >> 32), 0); |
| 363 | inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff); |
| 364 | } |
| 365 | |
| 366 | static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode) |
| 367 | { |
| 368 | return (u32) inode_get_atime_sec(ea_inode); |
| 369 | } |
| 370 | |
| 371 | static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash) |
| 372 | { |
| 373 | inode_set_atime(ea_inode, hash, 0); |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Read the EA value from an inode. |
| 378 | */ |
| 379 | static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size) |
| 380 | { |
| 381 | int blocksize = 1 << ea_inode->i_blkbits; |
| 382 | int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits; |
| 383 | int tail_size = (size % blocksize) ?: blocksize; |
| 384 | struct buffer_head *bhs_inline[8]; |
| 385 | struct buffer_head **bhs = bhs_inline; |
| 386 | int i, ret; |
| 387 | |
| 388 | if (bh_count > ARRAY_SIZE(bhs_inline)) { |
| 389 | bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS); |
| 390 | if (!bhs) |
| 391 | return -ENOMEM; |
| 392 | } |
| 393 | |
| 394 | ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count, |
| 395 | true /* wait */, bhs); |
| 396 | if (ret) |
| 397 | goto free_bhs; |
| 398 | |
| 399 | for (i = 0; i < bh_count; i++) { |
| 400 | /* There shouldn't be any holes in ea_inode. */ |
| 401 | if (!bhs[i]) { |
| 402 | ret = -EFSCORRUPTED; |
| 403 | goto put_bhs; |
| 404 | } |
| 405 | memcpy((char *)buf + blocksize * i, bhs[i]->b_data, |
| 406 | i < bh_count - 1 ? blocksize : tail_size); |
| 407 | } |
| 408 | ret = 0; |
| 409 | put_bhs: |
| 410 | for (i = 0; i < bh_count; i++) |
| 411 | brelse(bhs[i]); |
| 412 | free_bhs: |
| 413 | if (bhs != bhs_inline) |
| 414 | kfree(bhs); |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode_get_mtime_sec(inode))) |
| 419 | |
| 420 | static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, |
| 421 | u32 ea_inode_hash, struct inode **ea_inode) |
| 422 | { |
| 423 | struct inode *inode; |
| 424 | int err; |
| 425 | |
| 426 | /* |
| 427 | * We have to check for this corruption early as otherwise |
| 428 | * iget_locked() could wait indefinitely for the state of our |
| 429 | * parent inode. |
| 430 | */ |
| 431 | if (parent->i_ino == ea_ino) { |
| 432 | ext4_error(parent->i_sb, |
| 433 | "Parent and EA inode have the same ino %lu", ea_ino); |
| 434 | return -EFSCORRUPTED; |
| 435 | } |
| 436 | |
| 437 | inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_EA_INODE); |
| 438 | if (IS_ERR(inode)) { |
| 439 | err = PTR_ERR(inode); |
| 440 | ext4_error(parent->i_sb, |
| 441 | "error while reading EA inode %lu err=%d", ea_ino, |
| 442 | err); |
| 443 | return err; |
| 444 | } |
| 445 | ext4_xattr_inode_set_class(inode); |
| 446 | |
| 447 | /* |
| 448 | * Check whether this is an old Lustre-style xattr inode. Lustre |
| 449 | * implementation does not have hash validation, rather it has a |
| 450 | * backpointer from ea_inode to the parent inode. |
| 451 | */ |
| 452 | if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) && |
| 453 | EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino && |
| 454 | inode->i_generation == parent->i_generation) { |
| 455 | ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE); |
| 456 | ext4_xattr_inode_set_ref(inode, 1); |
| 457 | } else { |
| 458 | inode_lock_nested(inode, I_MUTEX_XATTR); |
| 459 | inode->i_flags |= S_NOQUOTA; |
| 460 | inode_unlock(inode); |
| 461 | } |
| 462 | |
| 463 | *ea_inode = inode; |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | /* Remove entry from mbcache when EA inode is getting evicted */ |
| 468 | void ext4_evict_ea_inode(struct inode *inode) |
| 469 | { |
| 470 | struct mb_cache_entry *oe; |
| 471 | |
| 472 | if (!EA_INODE_CACHE(inode)) |
| 473 | return; |
| 474 | /* Wait for entry to get unused so that we can remove it */ |
| 475 | while ((oe = mb_cache_entry_delete_or_get(EA_INODE_CACHE(inode), |
| 476 | ext4_xattr_inode_get_hash(inode), inode->i_ino))) { |
| 477 | mb_cache_entry_wait_unused(oe); |
| 478 | mb_cache_entry_put(EA_INODE_CACHE(inode), oe); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | static int |
| 483 | ext4_xattr_inode_verify_hashes(struct inode *ea_inode, |
| 484 | struct ext4_xattr_entry *entry, void *buffer, |
| 485 | size_t size) |
| 486 | { |
| 487 | u32 hash; |
| 488 | |
| 489 | /* Verify stored hash matches calculated hash. */ |
| 490 | hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size); |
| 491 | if (hash != ext4_xattr_inode_get_hash(ea_inode)) |
| 492 | return -EFSCORRUPTED; |
| 493 | |
| 494 | if (entry) { |
| 495 | __le32 e_hash, tmp_data; |
| 496 | |
| 497 | /* Verify entry hash. */ |
| 498 | tmp_data = cpu_to_le32(hash); |
| 499 | e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len, |
| 500 | &tmp_data, 1); |
| 501 | /* All good? */ |
| 502 | if (e_hash == entry->e_hash) |
| 503 | return 0; |
| 504 | |
| 505 | /* |
| 506 | * Not good. Maybe the entry hash was calculated |
| 507 | * using the buggy signed char version? |
| 508 | */ |
| 509 | e_hash = ext4_xattr_hash_entry_signed(entry->e_name, entry->e_name_len, |
| 510 | &tmp_data, 1); |
| 511 | /* Still no match - bad */ |
| 512 | if (e_hash != entry->e_hash) |
| 513 | return -EFSCORRUPTED; |
| 514 | |
| 515 | /* Let people know about old hash */ |
| 516 | pr_warn_once("ext4: filesystem with signed xattr name hash"); |
| 517 | } |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Read xattr value from the EA inode. |
| 523 | */ |
| 524 | static int |
| 525 | ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry, |
| 526 | void *buffer, size_t size) |
| 527 | { |
| 528 | struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode); |
| 529 | struct inode *ea_inode; |
| 530 | int err; |
| 531 | |
| 532 | err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum), |
| 533 | le32_to_cpu(entry->e_hash), &ea_inode); |
| 534 | if (err) { |
| 535 | ea_inode = NULL; |
| 536 | goto out; |
| 537 | } |
| 538 | |
| 539 | if (i_size_read(ea_inode) != size) { |
| 540 | ext4_warning_inode(ea_inode, |
| 541 | "ea_inode file size=%llu entry size=%zu", |
| 542 | i_size_read(ea_inode), size); |
| 543 | err = -EFSCORRUPTED; |
| 544 | goto out; |
| 545 | } |
| 546 | |
| 547 | err = ext4_xattr_inode_read(ea_inode, buffer, size); |
| 548 | if (err) |
| 549 | goto out; |
| 550 | |
| 551 | if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) { |
| 552 | err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer, |
| 553 | size); |
| 554 | if (err) { |
| 555 | ext4_warning_inode(ea_inode, |
| 556 | "EA inode hash validation failed"); |
| 557 | goto out; |
| 558 | } |
| 559 | |
| 560 | if (ea_inode_cache) |
| 561 | mb_cache_entry_create(ea_inode_cache, GFP_NOFS, |
| 562 | ext4_xattr_inode_get_hash(ea_inode), |
| 563 | ea_inode->i_ino, true /* reusable */); |
| 564 | } |
| 565 | out: |
| 566 | iput(ea_inode); |
| 567 | return err; |
| 568 | } |
| 569 | |
| 570 | static int |
| 571 | ext4_xattr_block_get(struct inode *inode, int name_index, const char *name, |
| 572 | void *buffer, size_t buffer_size) |
| 573 | { |
| 574 | struct buffer_head *bh = NULL; |
| 575 | struct ext4_xattr_entry *entry; |
| 576 | size_t size; |
| 577 | void *end; |
| 578 | int error; |
| 579 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
| 580 | |
| 581 | ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", |
| 582 | name_index, name, buffer, (long)buffer_size); |
| 583 | |
| 584 | if (!EXT4_I(inode)->i_file_acl) |
| 585 | return -ENODATA; |
| 586 | ea_idebug(inode, "reading block %llu", |
| 587 | (unsigned long long)EXT4_I(inode)->i_file_acl); |
| 588 | bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO); |
| 589 | if (IS_ERR(bh)) |
| 590 | return PTR_ERR(bh); |
| 591 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 592 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); |
| 593 | error = ext4_xattr_check_block(inode, bh); |
| 594 | if (error) |
| 595 | goto cleanup; |
| 596 | ext4_xattr_block_cache_insert(ea_block_cache, bh); |
| 597 | entry = BFIRST(bh); |
| 598 | end = bh->b_data + bh->b_size; |
| 599 | error = xattr_find_entry(inode, &entry, end, name_index, name, 1); |
| 600 | if (error) |
| 601 | goto cleanup; |
| 602 | size = le32_to_cpu(entry->e_value_size); |
| 603 | error = -ERANGE; |
| 604 | if (unlikely(size > EXT4_XATTR_SIZE_MAX)) |
| 605 | goto cleanup; |
| 606 | if (buffer) { |
| 607 | if (size > buffer_size) |
| 608 | goto cleanup; |
| 609 | if (entry->e_value_inum) { |
| 610 | error = ext4_xattr_inode_get(inode, entry, buffer, |
| 611 | size); |
| 612 | if (error) |
| 613 | goto cleanup; |
| 614 | } else { |
| 615 | u16 offset = le16_to_cpu(entry->e_value_offs); |
| 616 | void *p = bh->b_data + offset; |
| 617 | |
| 618 | if (unlikely(p + size > end)) |
| 619 | goto cleanup; |
| 620 | memcpy(buffer, p, size); |
| 621 | } |
| 622 | } |
| 623 | error = size; |
| 624 | |
| 625 | cleanup: |
| 626 | brelse(bh); |
| 627 | return error; |
| 628 | } |
| 629 | |
| 630 | int |
| 631 | ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name, |
| 632 | void *buffer, size_t buffer_size) |
| 633 | { |
| 634 | struct ext4_xattr_ibody_header *header; |
| 635 | struct ext4_xattr_entry *entry; |
| 636 | struct ext4_inode *raw_inode; |
| 637 | struct ext4_iloc iloc; |
| 638 | size_t size; |
| 639 | void *end; |
| 640 | int error; |
| 641 | |
| 642 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
| 643 | return -ENODATA; |
| 644 | error = ext4_get_inode_loc(inode, &iloc); |
| 645 | if (error) |
| 646 | return error; |
| 647 | raw_inode = ext4_raw_inode(&iloc); |
| 648 | header = IHDR(inode, raw_inode); |
| 649 | end = ITAIL(inode, raw_inode); |
| 650 | entry = IFIRST(header); |
| 651 | error = xattr_find_entry(inode, &entry, end, name_index, name, 0); |
| 652 | if (error) |
| 653 | goto cleanup; |
| 654 | size = le32_to_cpu(entry->e_value_size); |
| 655 | error = -ERANGE; |
| 656 | if (unlikely(size > EXT4_XATTR_SIZE_MAX)) |
| 657 | goto cleanup; |
| 658 | if (buffer) { |
| 659 | if (size > buffer_size) |
| 660 | goto cleanup; |
| 661 | if (entry->e_value_inum) { |
| 662 | error = ext4_xattr_inode_get(inode, entry, buffer, |
| 663 | size); |
| 664 | if (error) |
| 665 | goto cleanup; |
| 666 | } else { |
| 667 | u16 offset = le16_to_cpu(entry->e_value_offs); |
| 668 | void *p = (void *)IFIRST(header) + offset; |
| 669 | |
| 670 | if (unlikely(p + size > end)) |
| 671 | goto cleanup; |
| 672 | memcpy(buffer, p, size); |
| 673 | } |
| 674 | } |
| 675 | error = size; |
| 676 | |
| 677 | cleanup: |
| 678 | brelse(iloc.bh); |
| 679 | return error; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * ext4_xattr_get() |
| 684 | * |
| 685 | * Copy an extended attribute into the buffer |
| 686 | * provided, or compute the buffer size required. |
| 687 | * Buffer is NULL to compute the size of the buffer required. |
| 688 | * |
| 689 | * Returns a negative error number on failure, or the number of bytes |
| 690 | * used / required on success. |
| 691 | */ |
| 692 | int |
| 693 | ext4_xattr_get(struct inode *inode, int name_index, const char *name, |
| 694 | void *buffer, size_t buffer_size) |
| 695 | { |
| 696 | int error; |
| 697 | |
| 698 | if (unlikely(ext4_forced_shutdown(inode->i_sb))) |
| 699 | return -EIO; |
| 700 | |
| 701 | if (strlen(name) > 255) |
| 702 | return -ERANGE; |
| 703 | |
| 704 | down_read(&EXT4_I(inode)->xattr_sem); |
| 705 | error = ext4_xattr_ibody_get(inode, name_index, name, buffer, |
| 706 | buffer_size); |
| 707 | if (error == -ENODATA) |
| 708 | error = ext4_xattr_block_get(inode, name_index, name, buffer, |
| 709 | buffer_size); |
| 710 | up_read(&EXT4_I(inode)->xattr_sem); |
| 711 | return error; |
| 712 | } |
| 713 | |
| 714 | static int |
| 715 | ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry, |
| 716 | char *buffer, size_t buffer_size) |
| 717 | { |
| 718 | size_t rest = buffer_size; |
| 719 | |
| 720 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
| 721 | const char *prefix; |
| 722 | |
| 723 | prefix = ext4_xattr_prefix(entry->e_name_index, dentry); |
| 724 | if (prefix) { |
| 725 | size_t prefix_len = strlen(prefix); |
| 726 | size_t size = prefix_len + entry->e_name_len + 1; |
| 727 | |
| 728 | if (buffer) { |
| 729 | if (size > rest) |
| 730 | return -ERANGE; |
| 731 | memcpy(buffer, prefix, prefix_len); |
| 732 | buffer += prefix_len; |
| 733 | memcpy(buffer, entry->e_name, entry->e_name_len); |
| 734 | buffer += entry->e_name_len; |
| 735 | *buffer++ = 0; |
| 736 | } |
| 737 | rest -= size; |
| 738 | } |
| 739 | } |
| 740 | return buffer_size - rest; /* total size */ |
| 741 | } |
| 742 | |
| 743 | static int |
| 744 | ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 745 | { |
| 746 | struct inode *inode = d_inode(dentry); |
| 747 | struct buffer_head *bh = NULL; |
| 748 | int error; |
| 749 | |
| 750 | ea_idebug(inode, "buffer=%p, buffer_size=%ld", |
| 751 | buffer, (long)buffer_size); |
| 752 | |
| 753 | if (!EXT4_I(inode)->i_file_acl) |
| 754 | return 0; |
| 755 | ea_idebug(inode, "reading block %llu", |
| 756 | (unsigned long long)EXT4_I(inode)->i_file_acl); |
| 757 | bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO); |
| 758 | if (IS_ERR(bh)) |
| 759 | return PTR_ERR(bh); |
| 760 | ea_bdebug(bh, "b_count=%d, refcount=%d", |
| 761 | atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); |
| 762 | error = ext4_xattr_check_block(inode, bh); |
| 763 | if (error) |
| 764 | goto cleanup; |
| 765 | ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh); |
| 766 | error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, |
| 767 | buffer_size); |
| 768 | cleanup: |
| 769 | brelse(bh); |
| 770 | return error; |
| 771 | } |
| 772 | |
| 773 | static int |
| 774 | ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 775 | { |
| 776 | struct inode *inode = d_inode(dentry); |
| 777 | struct ext4_xattr_ibody_header *header; |
| 778 | struct ext4_inode *raw_inode; |
| 779 | struct ext4_iloc iloc; |
| 780 | int error; |
| 781 | |
| 782 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
| 783 | return 0; |
| 784 | error = ext4_get_inode_loc(inode, &iloc); |
| 785 | if (error) |
| 786 | return error; |
| 787 | raw_inode = ext4_raw_inode(&iloc); |
| 788 | header = IHDR(inode, raw_inode); |
| 789 | error = ext4_xattr_list_entries(dentry, IFIRST(header), |
| 790 | buffer, buffer_size); |
| 791 | |
| 792 | brelse(iloc.bh); |
| 793 | return error; |
| 794 | } |
| 795 | |
| 796 | /* |
| 797 | * Inode operation listxattr() |
| 798 | * |
| 799 | * d_inode(dentry)->i_rwsem: don't care |
| 800 | * |
| 801 | * Copy a list of attribute names into the buffer |
| 802 | * provided, or compute the buffer size required. |
| 803 | * Buffer is NULL to compute the size of the buffer required. |
| 804 | * |
| 805 | * Returns a negative error number on failure, or the number of bytes |
| 806 | * used / required on success. |
| 807 | */ |
| 808 | ssize_t |
| 809 | ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 810 | { |
| 811 | int ret, ret2; |
| 812 | |
| 813 | down_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
| 814 | ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size); |
| 815 | if (ret < 0) |
| 816 | goto errout; |
| 817 | if (buffer) { |
| 818 | buffer += ret; |
| 819 | buffer_size -= ret; |
| 820 | } |
| 821 | ret = ext4_xattr_block_list(dentry, buffer, buffer_size); |
| 822 | if (ret < 0) |
| 823 | goto errout; |
| 824 | ret += ret2; |
| 825 | errout: |
| 826 | up_read(&EXT4_I(d_inode(dentry))->xattr_sem); |
| 827 | return ret; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is |
| 832 | * not set, set it. |
| 833 | */ |
| 834 | static void ext4_xattr_update_super_block(handle_t *handle, |
| 835 | struct super_block *sb) |
| 836 | { |
| 837 | if (ext4_has_feature_xattr(sb)) |
| 838 | return; |
| 839 | |
| 840 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); |
| 841 | if (ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh, |
| 842 | EXT4_JTR_NONE) == 0) { |
| 843 | lock_buffer(EXT4_SB(sb)->s_sbh); |
| 844 | ext4_set_feature_xattr(sb); |
| 845 | ext4_superblock_csum_set(sb); |
| 846 | unlock_buffer(EXT4_SB(sb)->s_sbh); |
| 847 | ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | int ext4_get_inode_usage(struct inode *inode, qsize_t *usage) |
| 852 | { |
| 853 | struct ext4_iloc iloc = { .bh = NULL }; |
| 854 | struct buffer_head *bh = NULL; |
| 855 | struct ext4_inode *raw_inode; |
| 856 | struct ext4_xattr_ibody_header *header; |
| 857 | struct ext4_xattr_entry *entry; |
| 858 | qsize_t ea_inode_refs = 0; |
| 859 | int ret; |
| 860 | |
| 861 | lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem); |
| 862 | |
| 863 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
| 864 | ret = ext4_get_inode_loc(inode, &iloc); |
| 865 | if (ret) |
| 866 | goto out; |
| 867 | raw_inode = ext4_raw_inode(&iloc); |
| 868 | header = IHDR(inode, raw_inode); |
| 869 | |
| 870 | for (entry = IFIRST(header); !IS_LAST_ENTRY(entry); |
| 871 | entry = EXT4_XATTR_NEXT(entry)) |
| 872 | if (entry->e_value_inum) |
| 873 | ea_inode_refs++; |
| 874 | } |
| 875 | |
| 876 | if (EXT4_I(inode)->i_file_acl) { |
| 877 | bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO); |
| 878 | if (IS_ERR(bh)) { |
| 879 | ret = PTR_ERR(bh); |
| 880 | bh = NULL; |
| 881 | goto out; |
| 882 | } |
| 883 | |
| 884 | ret = ext4_xattr_check_block(inode, bh); |
| 885 | if (ret) |
| 886 | goto out; |
| 887 | |
| 888 | for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry); |
| 889 | entry = EXT4_XATTR_NEXT(entry)) |
| 890 | if (entry->e_value_inum) |
| 891 | ea_inode_refs++; |
| 892 | } |
| 893 | *usage = ea_inode_refs + 1; |
| 894 | ret = 0; |
| 895 | out: |
| 896 | brelse(iloc.bh); |
| 897 | brelse(bh); |
| 898 | return ret; |
| 899 | } |
| 900 | |
| 901 | static inline size_t round_up_cluster(struct inode *inode, size_t length) |
| 902 | { |
| 903 | struct super_block *sb = inode->i_sb; |
| 904 | size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits + |
| 905 | inode->i_blkbits); |
| 906 | size_t mask = ~(cluster_size - 1); |
| 907 | |
| 908 | return (length + cluster_size - 1) & mask; |
| 909 | } |
| 910 | |
| 911 | static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len) |
| 912 | { |
| 913 | int err; |
| 914 | |
| 915 | err = dquot_alloc_inode(inode); |
| 916 | if (err) |
| 917 | return err; |
| 918 | err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len)); |
| 919 | if (err) |
| 920 | dquot_free_inode(inode); |
| 921 | return err; |
| 922 | } |
| 923 | |
| 924 | static void ext4_xattr_inode_free_quota(struct inode *parent, |
| 925 | struct inode *ea_inode, |
| 926 | size_t len) |
| 927 | { |
| 928 | if (ea_inode && |
| 929 | ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) |
| 930 | return; |
| 931 | dquot_free_space_nodirty(parent, round_up_cluster(parent, len)); |
| 932 | dquot_free_inode(parent); |
| 933 | } |
| 934 | |
| 935 | int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode, |
| 936 | struct buffer_head *block_bh, size_t value_len, |
| 937 | bool is_create) |
| 938 | { |
| 939 | int credits; |
| 940 | int blocks; |
| 941 | |
| 942 | /* |
| 943 | * 1) Owner inode update |
| 944 | * 2) Ref count update on old xattr block |
| 945 | * 3) new xattr block |
| 946 | * 4) block bitmap update for new xattr block |
| 947 | * 5) group descriptor for new xattr block |
| 948 | * 6) block bitmap update for old xattr block |
| 949 | * 7) group descriptor for old block |
| 950 | * |
| 951 | * 6 & 7 can happen if we have two racing threads T_a and T_b |
| 952 | * which are each trying to set an xattr on inodes I_a and I_b |
| 953 | * which were both initially sharing an xattr block. |
| 954 | */ |
| 955 | credits = 7; |
| 956 | |
| 957 | /* Quota updates. */ |
| 958 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb); |
| 959 | |
| 960 | /* |
| 961 | * In case of inline data, we may push out the data to a block, |
| 962 | * so we need to reserve credits for this eventuality |
| 963 | */ |
| 964 | if (inode && ext4_has_inline_data(inode)) |
| 965 | credits += ext4_writepage_trans_blocks(inode) + 1; |
| 966 | |
| 967 | /* We are done if ea_inode feature is not enabled. */ |
| 968 | if (!ext4_has_feature_ea_inode(sb)) |
| 969 | return credits; |
| 970 | |
| 971 | /* New ea_inode, inode map, block bitmap, group descriptor. */ |
| 972 | credits += 4; |
| 973 | |
| 974 | /* Data blocks. */ |
| 975 | blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits; |
| 976 | |
| 977 | /* Indirection block or one level of extent tree. */ |
| 978 | blocks += 1; |
| 979 | |
| 980 | /* Block bitmap and group descriptor updates for each block. */ |
| 981 | credits += blocks * 2; |
| 982 | |
| 983 | /* Blocks themselves. */ |
| 984 | credits += blocks; |
| 985 | |
| 986 | if (!is_create) { |
| 987 | /* Dereference ea_inode holding old xattr value. |
| 988 | * Old ea_inode, inode map, block bitmap, group descriptor. |
| 989 | */ |
| 990 | credits += 4; |
| 991 | |
| 992 | /* Data blocks for old ea_inode. */ |
| 993 | blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits; |
| 994 | |
| 995 | /* Indirection block or one level of extent tree for old |
| 996 | * ea_inode. |
| 997 | */ |
| 998 | blocks += 1; |
| 999 | |
| 1000 | /* Block bitmap and group descriptor updates for each block. */ |
| 1001 | credits += blocks * 2; |
| 1002 | } |
| 1003 | |
| 1004 | /* We may need to clone the existing xattr block in which case we need |
| 1005 | * to increment ref counts for existing ea_inodes referenced by it. |
| 1006 | */ |
| 1007 | if (block_bh) { |
| 1008 | struct ext4_xattr_entry *entry = BFIRST(block_bh); |
| 1009 | |
| 1010 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) |
| 1011 | if (entry->e_value_inum) |
| 1012 | /* Ref count update on ea_inode. */ |
| 1013 | credits += 1; |
| 1014 | } |
| 1015 | return credits; |
| 1016 | } |
| 1017 | |
| 1018 | static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode, |
| 1019 | int ref_change) |
| 1020 | { |
| 1021 | struct ext4_iloc iloc; |
| 1022 | s64 ref_count; |
| 1023 | int ret; |
| 1024 | |
| 1025 | inode_lock_nested(ea_inode, I_MUTEX_XATTR); |
| 1026 | |
| 1027 | ret = ext4_reserve_inode_write(handle, ea_inode, &iloc); |
| 1028 | if (ret) |
| 1029 | goto out; |
| 1030 | |
| 1031 | ref_count = ext4_xattr_inode_get_ref(ea_inode); |
| 1032 | ref_count += ref_change; |
| 1033 | ext4_xattr_inode_set_ref(ea_inode, ref_count); |
| 1034 | |
| 1035 | if (ref_change > 0) { |
| 1036 | WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld", |
| 1037 | ea_inode->i_ino, ref_count); |
| 1038 | |
| 1039 | if (ref_count == 1) { |
| 1040 | WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u", |
| 1041 | ea_inode->i_ino, ea_inode->i_nlink); |
| 1042 | |
| 1043 | set_nlink(ea_inode, 1); |
| 1044 | ext4_orphan_del(handle, ea_inode); |
| 1045 | } |
| 1046 | } else { |
| 1047 | WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld", |
| 1048 | ea_inode->i_ino, ref_count); |
| 1049 | |
| 1050 | if (ref_count == 0) { |
| 1051 | WARN_ONCE(ea_inode->i_nlink != 1, |
| 1052 | "EA inode %lu i_nlink=%u", |
| 1053 | ea_inode->i_ino, ea_inode->i_nlink); |
| 1054 | |
| 1055 | clear_nlink(ea_inode); |
| 1056 | ext4_orphan_add(handle, ea_inode); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc); |
| 1061 | if (ret) |
| 1062 | ext4_warning_inode(ea_inode, |
| 1063 | "ext4_mark_iloc_dirty() failed ret=%d", ret); |
| 1064 | out: |
| 1065 | inode_unlock(ea_inode); |
| 1066 | return ret; |
| 1067 | } |
| 1068 | |
| 1069 | static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode) |
| 1070 | { |
| 1071 | return ext4_xattr_inode_update_ref(handle, ea_inode, 1); |
| 1072 | } |
| 1073 | |
| 1074 | static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode) |
| 1075 | { |
| 1076 | return ext4_xattr_inode_update_ref(handle, ea_inode, -1); |
| 1077 | } |
| 1078 | |
| 1079 | static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent, |
| 1080 | struct ext4_xattr_entry *first) |
| 1081 | { |
| 1082 | struct inode *ea_inode; |
| 1083 | struct ext4_xattr_entry *entry; |
| 1084 | struct ext4_xattr_entry *failed_entry; |
| 1085 | unsigned int ea_ino; |
| 1086 | int err, saved_err; |
| 1087 | |
| 1088 | for (entry = first; !IS_LAST_ENTRY(entry); |
| 1089 | entry = EXT4_XATTR_NEXT(entry)) { |
| 1090 | if (!entry->e_value_inum) |
| 1091 | continue; |
| 1092 | ea_ino = le32_to_cpu(entry->e_value_inum); |
| 1093 | err = ext4_xattr_inode_iget(parent, ea_ino, |
| 1094 | le32_to_cpu(entry->e_hash), |
| 1095 | &ea_inode); |
| 1096 | if (err) |
| 1097 | goto cleanup; |
| 1098 | err = ext4_xattr_inode_inc_ref(handle, ea_inode); |
| 1099 | if (err) { |
| 1100 | ext4_warning_inode(ea_inode, "inc ref error %d", err); |
| 1101 | iput(ea_inode); |
| 1102 | goto cleanup; |
| 1103 | } |
| 1104 | iput(ea_inode); |
| 1105 | } |
| 1106 | return 0; |
| 1107 | |
| 1108 | cleanup: |
| 1109 | saved_err = err; |
| 1110 | failed_entry = entry; |
| 1111 | |
| 1112 | for (entry = first; entry != failed_entry; |
| 1113 | entry = EXT4_XATTR_NEXT(entry)) { |
| 1114 | if (!entry->e_value_inum) |
| 1115 | continue; |
| 1116 | ea_ino = le32_to_cpu(entry->e_value_inum); |
| 1117 | err = ext4_xattr_inode_iget(parent, ea_ino, |
| 1118 | le32_to_cpu(entry->e_hash), |
| 1119 | &ea_inode); |
| 1120 | if (err) { |
| 1121 | ext4_warning(parent->i_sb, |
| 1122 | "cleanup ea_ino %u iget error %d", ea_ino, |
| 1123 | err); |
| 1124 | continue; |
| 1125 | } |
| 1126 | err = ext4_xattr_inode_dec_ref(handle, ea_inode); |
| 1127 | if (err) |
| 1128 | ext4_warning_inode(ea_inode, "cleanup dec ref error %d", |
| 1129 | err); |
| 1130 | iput(ea_inode); |
| 1131 | } |
| 1132 | return saved_err; |
| 1133 | } |
| 1134 | |
| 1135 | static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode, |
| 1136 | struct buffer_head *bh, bool block_csum, bool dirty) |
| 1137 | { |
| 1138 | int error; |
| 1139 | |
| 1140 | if (bh && dirty) { |
| 1141 | if (block_csum) |
| 1142 | ext4_xattr_block_csum_set(inode, bh); |
| 1143 | error = ext4_handle_dirty_metadata(handle, NULL, bh); |
| 1144 | if (error) { |
| 1145 | ext4_warning(inode->i_sb, "Handle metadata (error %d)", |
| 1146 | error); |
| 1147 | return error; |
| 1148 | } |
| 1149 | } |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | static void |
| 1154 | ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent, |
| 1155 | struct buffer_head *bh, |
| 1156 | struct ext4_xattr_entry *first, bool block_csum, |
| 1157 | struct ext4_xattr_inode_array **ea_inode_array, |
| 1158 | int extra_credits, bool skip_quota) |
| 1159 | { |
| 1160 | struct inode *ea_inode; |
| 1161 | struct ext4_xattr_entry *entry; |
| 1162 | struct ext4_iloc iloc; |
| 1163 | bool dirty = false; |
| 1164 | unsigned int ea_ino; |
| 1165 | int err; |
| 1166 | int credits; |
| 1167 | void *end; |
| 1168 | |
| 1169 | if (block_csum) |
| 1170 | end = (void *)bh->b_data + bh->b_size; |
| 1171 | else { |
| 1172 | ext4_get_inode_loc(parent, &iloc); |
| 1173 | end = (void *)ext4_raw_inode(&iloc) + EXT4_SB(parent->i_sb)->s_inode_size; |
| 1174 | } |
| 1175 | |
| 1176 | /* One credit for dec ref on ea_inode, one for orphan list addition, */ |
| 1177 | credits = 2 + extra_credits; |
| 1178 | |
| 1179 | for (entry = first; (void *)entry < end && !IS_LAST_ENTRY(entry); |
| 1180 | entry = EXT4_XATTR_NEXT(entry)) { |
| 1181 | if (!entry->e_value_inum) |
| 1182 | continue; |
| 1183 | ea_ino = le32_to_cpu(entry->e_value_inum); |
| 1184 | err = ext4_xattr_inode_iget(parent, ea_ino, |
| 1185 | le32_to_cpu(entry->e_hash), |
| 1186 | &ea_inode); |
| 1187 | if (err) |
| 1188 | continue; |
| 1189 | |
| 1190 | err = ext4_expand_inode_array(ea_inode_array, ea_inode); |
| 1191 | if (err) { |
| 1192 | ext4_warning_inode(ea_inode, |
| 1193 | "Expand inode array err=%d", err); |
| 1194 | iput(ea_inode); |
| 1195 | continue; |
| 1196 | } |
| 1197 | |
| 1198 | err = ext4_journal_ensure_credits_fn(handle, credits, credits, |
| 1199 | ext4_free_metadata_revoke_credits(parent->i_sb, 1), |
| 1200 | ext4_xattr_restart_fn(handle, parent, bh, block_csum, |
| 1201 | dirty)); |
| 1202 | if (err < 0) { |
| 1203 | ext4_warning_inode(ea_inode, "Ensure credits err=%d", |
| 1204 | err); |
| 1205 | continue; |
| 1206 | } |
| 1207 | if (err > 0) { |
| 1208 | err = ext4_journal_get_write_access(handle, |
| 1209 | parent->i_sb, bh, EXT4_JTR_NONE); |
| 1210 | if (err) { |
| 1211 | ext4_warning_inode(ea_inode, |
| 1212 | "Re-get write access err=%d", |
| 1213 | err); |
| 1214 | continue; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | err = ext4_xattr_inode_dec_ref(handle, ea_inode); |
| 1219 | if (err) { |
| 1220 | ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d", |
| 1221 | err); |
| 1222 | continue; |
| 1223 | } |
| 1224 | |
| 1225 | if (!skip_quota) |
| 1226 | ext4_xattr_inode_free_quota(parent, ea_inode, |
| 1227 | le32_to_cpu(entry->e_value_size)); |
| 1228 | |
| 1229 | /* |
| 1230 | * Forget about ea_inode within the same transaction that |
| 1231 | * decrements the ref count. This avoids duplicate decrements in |
| 1232 | * case the rest of the work spills over to subsequent |
| 1233 | * transactions. |
| 1234 | */ |
| 1235 | entry->e_value_inum = 0; |
| 1236 | entry->e_value_size = 0; |
| 1237 | |
| 1238 | dirty = true; |
| 1239 | } |
| 1240 | |
| 1241 | if (dirty) { |
| 1242 | /* |
| 1243 | * Note that we are deliberately skipping csum calculation for |
| 1244 | * the final update because we do not expect any journal |
| 1245 | * restarts until xattr block is freed. |
| 1246 | */ |
| 1247 | |
| 1248 | err = ext4_handle_dirty_metadata(handle, NULL, bh); |
| 1249 | if (err) |
| 1250 | ext4_warning_inode(parent, |
| 1251 | "handle dirty metadata err=%d", err); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Release the xattr block BH: If the reference count is > 1, decrement it; |
| 1257 | * otherwise free the block. |
| 1258 | */ |
| 1259 | static void |
| 1260 | ext4_xattr_release_block(handle_t *handle, struct inode *inode, |
| 1261 | struct buffer_head *bh, |
| 1262 | struct ext4_xattr_inode_array **ea_inode_array, |
| 1263 | int extra_credits) |
| 1264 | { |
| 1265 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
| 1266 | u32 hash, ref; |
| 1267 | int error = 0; |
| 1268 | |
| 1269 | BUFFER_TRACE(bh, "get_write_access"); |
| 1270 | error = ext4_journal_get_write_access(handle, inode->i_sb, bh, |
| 1271 | EXT4_JTR_NONE); |
| 1272 | if (error) |
| 1273 | goto out; |
| 1274 | |
| 1275 | retry_ref: |
| 1276 | lock_buffer(bh); |
| 1277 | hash = le32_to_cpu(BHDR(bh)->h_hash); |
| 1278 | ref = le32_to_cpu(BHDR(bh)->h_refcount); |
| 1279 | if (ref == 1) { |
| 1280 | ea_bdebug(bh, "refcount now=0; freeing"); |
| 1281 | /* |
| 1282 | * This must happen under buffer lock for |
| 1283 | * ext4_xattr_block_set() to reliably detect freed block |
| 1284 | */ |
| 1285 | if (ea_block_cache) { |
| 1286 | struct mb_cache_entry *oe; |
| 1287 | |
| 1288 | oe = mb_cache_entry_delete_or_get(ea_block_cache, hash, |
| 1289 | bh->b_blocknr); |
| 1290 | if (oe) { |
| 1291 | unlock_buffer(bh); |
| 1292 | mb_cache_entry_wait_unused(oe); |
| 1293 | mb_cache_entry_put(ea_block_cache, oe); |
| 1294 | goto retry_ref; |
| 1295 | } |
| 1296 | } |
| 1297 | get_bh(bh); |
| 1298 | unlock_buffer(bh); |
| 1299 | |
| 1300 | if (ext4_has_feature_ea_inode(inode->i_sb)) |
| 1301 | ext4_xattr_inode_dec_ref_all(handle, inode, bh, |
| 1302 | BFIRST(bh), |
| 1303 | true /* block_csum */, |
| 1304 | ea_inode_array, |
| 1305 | extra_credits, |
| 1306 | true /* skip_quota */); |
| 1307 | ext4_free_blocks(handle, inode, bh, 0, 1, |
| 1308 | EXT4_FREE_BLOCKS_METADATA | |
| 1309 | EXT4_FREE_BLOCKS_FORGET); |
| 1310 | } else { |
| 1311 | ref--; |
| 1312 | BHDR(bh)->h_refcount = cpu_to_le32(ref); |
| 1313 | if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) { |
| 1314 | struct mb_cache_entry *ce; |
| 1315 | |
| 1316 | if (ea_block_cache) { |
| 1317 | ce = mb_cache_entry_get(ea_block_cache, hash, |
| 1318 | bh->b_blocknr); |
| 1319 | if (ce) { |
| 1320 | set_bit(MBE_REUSABLE_B, &ce->e_flags); |
| 1321 | mb_cache_entry_put(ea_block_cache, ce); |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | ext4_xattr_block_csum_set(inode, bh); |
| 1327 | /* |
| 1328 | * Beware of this ugliness: Releasing of xattr block references |
| 1329 | * from different inodes can race and so we have to protect |
| 1330 | * from a race where someone else frees the block (and releases |
| 1331 | * its journal_head) before we are done dirtying the buffer. In |
| 1332 | * nojournal mode this race is harmless and we actually cannot |
| 1333 | * call ext4_handle_dirty_metadata() with locked buffer as |
| 1334 | * that function can call sync_dirty_buffer() so for that case |
| 1335 | * we handle the dirtying after unlocking the buffer. |
| 1336 | */ |
| 1337 | if (ext4_handle_valid(handle)) |
| 1338 | error = ext4_handle_dirty_metadata(handle, inode, bh); |
| 1339 | unlock_buffer(bh); |
| 1340 | if (!ext4_handle_valid(handle)) |
| 1341 | error = ext4_handle_dirty_metadata(handle, inode, bh); |
| 1342 | if (IS_SYNC(inode)) |
| 1343 | ext4_handle_sync(handle); |
| 1344 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1)); |
| 1345 | ea_bdebug(bh, "refcount now=%d; releasing", |
| 1346 | le32_to_cpu(BHDR(bh)->h_refcount)); |
| 1347 | } |
| 1348 | out: |
| 1349 | ext4_std_error(inode->i_sb, error); |
| 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | /* |
| 1354 | * Find the available free space for EAs. This also returns the total number of |
| 1355 | * bytes used by EA entries. |
| 1356 | */ |
| 1357 | static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last, |
| 1358 | size_t *min_offs, void *base, int *total) |
| 1359 | { |
| 1360 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
| 1361 | if (!last->e_value_inum && last->e_value_size) { |
| 1362 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 1363 | if (offs < *min_offs) |
| 1364 | *min_offs = offs; |
| 1365 | } |
| 1366 | if (total) |
| 1367 | *total += EXT4_XATTR_LEN(last->e_name_len); |
| 1368 | } |
| 1369 | return (*min_offs - ((void *)last - base) - sizeof(__u32)); |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * Write the value of the EA in an inode. |
| 1374 | */ |
| 1375 | static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode, |
| 1376 | const void *buf, int bufsize) |
| 1377 | { |
| 1378 | struct buffer_head *bh = NULL; |
| 1379 | unsigned long block = 0; |
| 1380 | int blocksize = ea_inode->i_sb->s_blocksize; |
| 1381 | int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits; |
| 1382 | int csize, wsize = 0; |
| 1383 | int ret = 0, ret2 = 0; |
| 1384 | int retries = 0; |
| 1385 | |
| 1386 | retry: |
| 1387 | while (ret >= 0 && ret < max_blocks) { |
| 1388 | struct ext4_map_blocks map; |
| 1389 | map.m_lblk = block += ret; |
| 1390 | map.m_len = max_blocks -= ret; |
| 1391 | |
| 1392 | ret = ext4_map_blocks(handle, ea_inode, &map, |
| 1393 | EXT4_GET_BLOCKS_CREATE); |
| 1394 | if (ret <= 0) { |
| 1395 | ext4_mark_inode_dirty(handle, ea_inode); |
| 1396 | if (ret == -ENOSPC && |
| 1397 | ext4_should_retry_alloc(ea_inode->i_sb, &retries)) { |
| 1398 | ret = 0; |
| 1399 | goto retry; |
| 1400 | } |
| 1401 | break; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | if (ret < 0) |
| 1406 | return ret; |
| 1407 | |
| 1408 | block = 0; |
| 1409 | while (wsize < bufsize) { |
| 1410 | brelse(bh); |
| 1411 | csize = (bufsize - wsize) > blocksize ? blocksize : |
| 1412 | bufsize - wsize; |
| 1413 | bh = ext4_getblk(handle, ea_inode, block, 0); |
| 1414 | if (IS_ERR(bh)) |
| 1415 | return PTR_ERR(bh); |
| 1416 | if (!bh) { |
| 1417 | WARN_ON_ONCE(1); |
| 1418 | EXT4_ERROR_INODE(ea_inode, |
| 1419 | "ext4_getblk() return bh = NULL"); |
| 1420 | return -EFSCORRUPTED; |
| 1421 | } |
| 1422 | ret = ext4_journal_get_write_access(handle, ea_inode->i_sb, bh, |
| 1423 | EXT4_JTR_NONE); |
| 1424 | if (ret) |
| 1425 | goto out; |
| 1426 | |
| 1427 | memcpy(bh->b_data, buf, csize); |
| 1428 | /* |
| 1429 | * Zero out block tail to avoid writing uninitialized memory |
| 1430 | * to disk. |
| 1431 | */ |
| 1432 | if (csize < blocksize) |
| 1433 | memset(bh->b_data + csize, 0, blocksize - csize); |
| 1434 | set_buffer_uptodate(bh); |
| 1435 | ext4_handle_dirty_metadata(handle, ea_inode, bh); |
| 1436 | |
| 1437 | buf += csize; |
| 1438 | wsize += csize; |
| 1439 | block += 1; |
| 1440 | } |
| 1441 | |
| 1442 | inode_lock(ea_inode); |
| 1443 | i_size_write(ea_inode, wsize); |
| 1444 | ext4_update_i_disksize(ea_inode, wsize); |
| 1445 | inode_unlock(ea_inode); |
| 1446 | |
| 1447 | ret2 = ext4_mark_inode_dirty(handle, ea_inode); |
| 1448 | if (unlikely(ret2 && !ret)) |
| 1449 | ret = ret2; |
| 1450 | |
| 1451 | out: |
| 1452 | brelse(bh); |
| 1453 | |
| 1454 | return ret; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * Create an inode to store the value of a large EA. |
| 1459 | */ |
| 1460 | static struct inode *ext4_xattr_inode_create(handle_t *handle, |
| 1461 | struct inode *inode, u32 hash) |
| 1462 | { |
| 1463 | struct inode *ea_inode = NULL; |
| 1464 | uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) }; |
| 1465 | int err; |
| 1466 | |
| 1467 | if (inode->i_sb->s_root == NULL) { |
| 1468 | ext4_warning(inode->i_sb, |
| 1469 | "refuse to create EA inode when umounting"); |
| 1470 | WARN_ON(1); |
| 1471 | return ERR_PTR(-EINVAL); |
| 1472 | } |
| 1473 | |
| 1474 | /* |
| 1475 | * Let the next inode be the goal, so we try and allocate the EA inode |
| 1476 | * in the same group, or nearby one. |
| 1477 | */ |
| 1478 | ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode, |
| 1479 | S_IFREG | 0600, NULL, inode->i_ino + 1, owner, |
| 1480 | EXT4_EA_INODE_FL); |
| 1481 | if (!IS_ERR(ea_inode)) { |
| 1482 | ea_inode->i_op = &ext4_file_inode_operations; |
| 1483 | ea_inode->i_fop = &ext4_file_operations; |
| 1484 | ext4_set_aops(ea_inode); |
| 1485 | ext4_xattr_inode_set_class(ea_inode); |
| 1486 | unlock_new_inode(ea_inode); |
| 1487 | ext4_xattr_inode_set_ref(ea_inode, 1); |
| 1488 | ext4_xattr_inode_set_hash(ea_inode, hash); |
| 1489 | err = ext4_mark_inode_dirty(handle, ea_inode); |
| 1490 | if (!err) |
| 1491 | err = ext4_inode_attach_jinode(ea_inode); |
| 1492 | if (err) { |
| 1493 | if (ext4_xattr_inode_dec_ref(handle, ea_inode)) |
| 1494 | ext4_warning_inode(ea_inode, |
| 1495 | "cleanup dec ref error %d", err); |
| 1496 | iput(ea_inode); |
| 1497 | return ERR_PTR(err); |
| 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * Xattr inodes are shared therefore quota charging is performed |
| 1502 | * at a higher level. |
| 1503 | */ |
| 1504 | dquot_free_inode(ea_inode); |
| 1505 | dquot_drop(ea_inode); |
| 1506 | inode_lock(ea_inode); |
| 1507 | ea_inode->i_flags |= S_NOQUOTA; |
| 1508 | inode_unlock(ea_inode); |
| 1509 | } |
| 1510 | |
| 1511 | return ea_inode; |
| 1512 | } |
| 1513 | |
| 1514 | static struct inode * |
| 1515 | ext4_xattr_inode_cache_find(struct inode *inode, const void *value, |
| 1516 | size_t value_len, u32 hash) |
| 1517 | { |
| 1518 | struct inode *ea_inode; |
| 1519 | struct mb_cache_entry *ce; |
| 1520 | struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode); |
| 1521 | void *ea_data; |
| 1522 | |
| 1523 | if (!ea_inode_cache) |
| 1524 | return NULL; |
| 1525 | |
| 1526 | ce = mb_cache_entry_find_first(ea_inode_cache, hash); |
| 1527 | if (!ce) |
| 1528 | return NULL; |
| 1529 | |
| 1530 | WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) && |
| 1531 | !(current->flags & PF_MEMALLOC_NOFS)); |
| 1532 | |
| 1533 | ea_data = kvmalloc(value_len, GFP_KERNEL); |
| 1534 | if (!ea_data) { |
| 1535 | mb_cache_entry_put(ea_inode_cache, ce); |
| 1536 | return NULL; |
| 1537 | } |
| 1538 | |
| 1539 | while (ce) { |
| 1540 | ea_inode = ext4_iget(inode->i_sb, ce->e_value, |
| 1541 | EXT4_IGET_EA_INODE); |
| 1542 | if (IS_ERR(ea_inode)) |
| 1543 | goto next_entry; |
| 1544 | ext4_xattr_inode_set_class(ea_inode); |
| 1545 | if (i_size_read(ea_inode) == value_len && |
| 1546 | !ext4_xattr_inode_read(ea_inode, ea_data, value_len) && |
| 1547 | !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data, |
| 1548 | value_len) && |
| 1549 | !memcmp(value, ea_data, value_len)) { |
| 1550 | mb_cache_entry_touch(ea_inode_cache, ce); |
| 1551 | mb_cache_entry_put(ea_inode_cache, ce); |
| 1552 | kvfree(ea_data); |
| 1553 | return ea_inode; |
| 1554 | } |
| 1555 | iput(ea_inode); |
| 1556 | next_entry: |
| 1557 | ce = mb_cache_entry_find_next(ea_inode_cache, ce); |
| 1558 | } |
| 1559 | kvfree(ea_data); |
| 1560 | return NULL; |
| 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | * Add value of the EA in an inode. |
| 1565 | */ |
| 1566 | static struct inode *ext4_xattr_inode_lookup_create(handle_t *handle, |
| 1567 | struct inode *inode, const void *value, size_t value_len) |
| 1568 | { |
| 1569 | struct inode *ea_inode; |
| 1570 | u32 hash; |
| 1571 | int err; |
| 1572 | |
| 1573 | /* Account inode & space to quota even if sharing... */ |
| 1574 | err = ext4_xattr_inode_alloc_quota(inode, value_len); |
| 1575 | if (err) |
| 1576 | return ERR_PTR(err); |
| 1577 | |
| 1578 | hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len); |
| 1579 | ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash); |
| 1580 | if (ea_inode) { |
| 1581 | err = ext4_xattr_inode_inc_ref(handle, ea_inode); |
| 1582 | if (err) |
| 1583 | goto out_err; |
| 1584 | return ea_inode; |
| 1585 | } |
| 1586 | |
| 1587 | /* Create an inode for the EA value */ |
| 1588 | ea_inode = ext4_xattr_inode_create(handle, inode, hash); |
| 1589 | if (IS_ERR(ea_inode)) { |
| 1590 | ext4_xattr_inode_free_quota(inode, NULL, value_len); |
| 1591 | return ea_inode; |
| 1592 | } |
| 1593 | |
| 1594 | err = ext4_xattr_inode_write(handle, ea_inode, value, value_len); |
| 1595 | if (err) { |
| 1596 | if (ext4_xattr_inode_dec_ref(handle, ea_inode)) |
| 1597 | ext4_warning_inode(ea_inode, "cleanup dec ref error %d", err); |
| 1598 | goto out_err; |
| 1599 | } |
| 1600 | |
| 1601 | if (EA_INODE_CACHE(inode)) |
| 1602 | mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash, |
| 1603 | ea_inode->i_ino, true /* reusable */); |
| 1604 | return ea_inode; |
| 1605 | out_err: |
| 1606 | iput(ea_inode); |
| 1607 | ext4_xattr_inode_free_quota(inode, NULL, value_len); |
| 1608 | return ERR_PTR(err); |
| 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode |
| 1613 | * feature is enabled. |
| 1614 | */ |
| 1615 | #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U) |
| 1616 | |
| 1617 | static int ext4_xattr_set_entry(struct ext4_xattr_info *i, |
| 1618 | struct ext4_xattr_search *s, |
| 1619 | handle_t *handle, struct inode *inode, |
| 1620 | struct inode *new_ea_inode, |
| 1621 | bool is_block) |
| 1622 | { |
| 1623 | struct ext4_xattr_entry *last, *next; |
| 1624 | struct ext4_xattr_entry *here = s->here; |
| 1625 | size_t min_offs = s->end - s->base, name_len = strlen(i->name); |
| 1626 | int in_inode = i->in_inode; |
| 1627 | struct inode *old_ea_inode = NULL; |
| 1628 | size_t old_size, new_size; |
| 1629 | int ret; |
| 1630 | |
| 1631 | /* Space used by old and new values. */ |
| 1632 | old_size = (!s->not_found && !here->e_value_inum) ? |
| 1633 | EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0; |
| 1634 | new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0; |
| 1635 | |
| 1636 | /* |
| 1637 | * Optimization for the simple case when old and new values have the |
| 1638 | * same padded sizes. Not applicable if external inodes are involved. |
| 1639 | */ |
| 1640 | if (new_size && new_size == old_size) { |
| 1641 | size_t offs = le16_to_cpu(here->e_value_offs); |
| 1642 | void *val = s->base + offs; |
| 1643 | |
| 1644 | here->e_value_size = cpu_to_le32(i->value_len); |
| 1645 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
| 1646 | memset(val, 0, new_size); |
| 1647 | } else { |
| 1648 | memcpy(val, i->value, i->value_len); |
| 1649 | /* Clear padding bytes. */ |
| 1650 | memset(val + i->value_len, 0, new_size - i->value_len); |
| 1651 | } |
| 1652 | goto update_hash; |
| 1653 | } |
| 1654 | |
| 1655 | /* Compute min_offs and last. */ |
| 1656 | last = s->first; |
| 1657 | for (; !IS_LAST_ENTRY(last); last = next) { |
| 1658 | next = EXT4_XATTR_NEXT(last); |
| 1659 | if ((void *)next >= s->end) { |
| 1660 | EXT4_ERROR_INODE(inode, "corrupted xattr entries"); |
| 1661 | ret = -EFSCORRUPTED; |
| 1662 | goto out; |
| 1663 | } |
| 1664 | if (!last->e_value_inum && last->e_value_size) { |
| 1665 | size_t offs = le16_to_cpu(last->e_value_offs); |
| 1666 | if (offs < min_offs) |
| 1667 | min_offs = offs; |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | /* Check whether we have enough space. */ |
| 1672 | if (i->value) { |
| 1673 | size_t free; |
| 1674 | |
| 1675 | free = min_offs - ((void *)last - s->base) - sizeof(__u32); |
| 1676 | if (!s->not_found) |
| 1677 | free += EXT4_XATTR_LEN(name_len) + old_size; |
| 1678 | |
| 1679 | if (free < EXT4_XATTR_LEN(name_len) + new_size) { |
| 1680 | ret = -ENOSPC; |
| 1681 | goto out; |
| 1682 | } |
| 1683 | |
| 1684 | /* |
| 1685 | * If storing the value in an external inode is an option, |
| 1686 | * reserve space for xattr entries/names in the external |
| 1687 | * attribute block so that a long value does not occupy the |
| 1688 | * whole space and prevent further entries being added. |
| 1689 | */ |
| 1690 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
| 1691 | new_size && is_block && |
| 1692 | (min_offs + old_size - new_size) < |
| 1693 | EXT4_XATTR_BLOCK_RESERVE(inode)) { |
| 1694 | ret = -ENOSPC; |
| 1695 | goto out; |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | /* |
| 1700 | * Getting access to old and new ea inodes is subject to failures. |
| 1701 | * Finish that work before doing any modifications to the xattr data. |
| 1702 | */ |
| 1703 | if (!s->not_found && here->e_value_inum) { |
| 1704 | ret = ext4_xattr_inode_iget(inode, |
| 1705 | le32_to_cpu(here->e_value_inum), |
| 1706 | le32_to_cpu(here->e_hash), |
| 1707 | &old_ea_inode); |
| 1708 | if (ret) { |
| 1709 | old_ea_inode = NULL; |
| 1710 | goto out; |
| 1711 | } |
| 1712 | |
| 1713 | /* We are ready to release ref count on the old_ea_inode. */ |
| 1714 | ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode); |
| 1715 | if (ret) |
| 1716 | goto out; |
| 1717 | |
| 1718 | ext4_xattr_inode_free_quota(inode, old_ea_inode, |
| 1719 | le32_to_cpu(here->e_value_size)); |
| 1720 | } |
| 1721 | |
| 1722 | /* No failures allowed past this point. */ |
| 1723 | |
| 1724 | if (!s->not_found && here->e_value_size && !here->e_value_inum) { |
| 1725 | /* Remove the old value. */ |
| 1726 | void *first_val = s->base + min_offs; |
| 1727 | size_t offs = le16_to_cpu(here->e_value_offs); |
| 1728 | void *val = s->base + offs; |
| 1729 | |
| 1730 | memmove(first_val + old_size, first_val, val - first_val); |
| 1731 | memset(first_val, 0, old_size); |
| 1732 | min_offs += old_size; |
| 1733 | |
| 1734 | /* Adjust all value offsets. */ |
| 1735 | last = s->first; |
| 1736 | while (!IS_LAST_ENTRY(last)) { |
| 1737 | size_t o = le16_to_cpu(last->e_value_offs); |
| 1738 | |
| 1739 | if (!last->e_value_inum && |
| 1740 | last->e_value_size && o < offs) |
| 1741 | last->e_value_offs = cpu_to_le16(o + old_size); |
| 1742 | last = EXT4_XATTR_NEXT(last); |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | if (!i->value) { |
| 1747 | /* Remove old name. */ |
| 1748 | size_t size = EXT4_XATTR_LEN(name_len); |
| 1749 | |
| 1750 | last = ENTRY((void *)last - size); |
| 1751 | memmove(here, (void *)here + size, |
| 1752 | (void *)last - (void *)here + sizeof(__u32)); |
| 1753 | memset(last, 0, size); |
| 1754 | |
| 1755 | /* |
| 1756 | * Update i_inline_off - moved ibody region might contain |
| 1757 | * system.data attribute. Handling a failure here won't |
| 1758 | * cause other complications for setting an xattr. |
| 1759 | */ |
| 1760 | if (!is_block && ext4_has_inline_data(inode)) { |
| 1761 | ret = ext4_find_inline_data_nolock(inode); |
| 1762 | if (ret) { |
| 1763 | ext4_warning_inode(inode, |
| 1764 | "unable to update i_inline_off"); |
| 1765 | goto out; |
| 1766 | } |
| 1767 | } |
| 1768 | } else if (s->not_found) { |
| 1769 | /* Insert new name. */ |
| 1770 | size_t size = EXT4_XATTR_LEN(name_len); |
| 1771 | size_t rest = (void *)last - (void *)here + sizeof(__u32); |
| 1772 | |
| 1773 | memmove((void *)here + size, here, rest); |
| 1774 | memset(here, 0, size); |
| 1775 | here->e_name_index = i->name_index; |
| 1776 | here->e_name_len = name_len; |
| 1777 | memcpy(here->e_name, i->name, name_len); |
| 1778 | } else { |
| 1779 | /* This is an update, reset value info. */ |
| 1780 | here->e_value_inum = 0; |
| 1781 | here->e_value_offs = 0; |
| 1782 | here->e_value_size = 0; |
| 1783 | } |
| 1784 | |
| 1785 | if (i->value) { |
| 1786 | /* Insert new value. */ |
| 1787 | if (in_inode) { |
| 1788 | here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino); |
| 1789 | } else if (i->value_len) { |
| 1790 | void *val = s->base + min_offs - new_size; |
| 1791 | |
| 1792 | here->e_value_offs = cpu_to_le16(min_offs - new_size); |
| 1793 | if (i->value == EXT4_ZERO_XATTR_VALUE) { |
| 1794 | memset(val, 0, new_size); |
| 1795 | } else { |
| 1796 | memcpy(val, i->value, i->value_len); |
| 1797 | /* Clear padding bytes. */ |
| 1798 | memset(val + i->value_len, 0, |
| 1799 | new_size - i->value_len); |
| 1800 | } |
| 1801 | } |
| 1802 | here->e_value_size = cpu_to_le32(i->value_len); |
| 1803 | } |
| 1804 | |
| 1805 | update_hash: |
| 1806 | if (i->value) { |
| 1807 | __le32 hash = 0; |
| 1808 | |
| 1809 | /* Entry hash calculation. */ |
| 1810 | if (in_inode) { |
| 1811 | __le32 crc32c_hash; |
| 1812 | |
| 1813 | /* |
| 1814 | * Feed crc32c hash instead of the raw value for entry |
| 1815 | * hash calculation. This is to avoid walking |
| 1816 | * potentially long value buffer again. |
| 1817 | */ |
| 1818 | crc32c_hash = cpu_to_le32( |
| 1819 | ext4_xattr_inode_get_hash(new_ea_inode)); |
| 1820 | hash = ext4_xattr_hash_entry(here->e_name, |
| 1821 | here->e_name_len, |
| 1822 | &crc32c_hash, 1); |
| 1823 | } else if (is_block) { |
| 1824 | __le32 *value = s->base + le16_to_cpu( |
| 1825 | here->e_value_offs); |
| 1826 | |
| 1827 | hash = ext4_xattr_hash_entry(here->e_name, |
| 1828 | here->e_name_len, value, |
| 1829 | new_size >> 2); |
| 1830 | } |
| 1831 | here->e_hash = hash; |
| 1832 | } |
| 1833 | |
| 1834 | if (is_block) |
| 1835 | ext4_xattr_rehash((struct ext4_xattr_header *)s->base); |
| 1836 | |
| 1837 | ret = 0; |
| 1838 | out: |
| 1839 | iput(old_ea_inode); |
| 1840 | return ret; |
| 1841 | } |
| 1842 | |
| 1843 | struct ext4_xattr_block_find { |
| 1844 | struct ext4_xattr_search s; |
| 1845 | struct buffer_head *bh; |
| 1846 | }; |
| 1847 | |
| 1848 | static int |
| 1849 | ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, |
| 1850 | struct ext4_xattr_block_find *bs) |
| 1851 | { |
| 1852 | struct super_block *sb = inode->i_sb; |
| 1853 | int error; |
| 1854 | |
| 1855 | ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", |
| 1856 | i->name_index, i->name, i->value, (long)i->value_len); |
| 1857 | |
| 1858 | if (EXT4_I(inode)->i_file_acl) { |
| 1859 | /* The inode already has an extended attribute block. */ |
| 1860 | bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO); |
| 1861 | if (IS_ERR(bs->bh)) { |
| 1862 | error = PTR_ERR(bs->bh); |
| 1863 | bs->bh = NULL; |
| 1864 | return error; |
| 1865 | } |
| 1866 | ea_bdebug(bs->bh, "b_count=%d, refcount=%d", |
| 1867 | atomic_read(&(bs->bh->b_count)), |
| 1868 | le32_to_cpu(BHDR(bs->bh)->h_refcount)); |
| 1869 | error = ext4_xattr_check_block(inode, bs->bh); |
| 1870 | if (error) |
| 1871 | return error; |
| 1872 | /* Find the named attribute. */ |
| 1873 | bs->s.base = BHDR(bs->bh); |
| 1874 | bs->s.first = BFIRST(bs->bh); |
| 1875 | bs->s.end = bs->bh->b_data + bs->bh->b_size; |
| 1876 | bs->s.here = bs->s.first; |
| 1877 | error = xattr_find_entry(inode, &bs->s.here, bs->s.end, |
| 1878 | i->name_index, i->name, 1); |
| 1879 | if (error && error != -ENODATA) |
| 1880 | return error; |
| 1881 | bs->s.not_found = error; |
| 1882 | } |
| 1883 | return 0; |
| 1884 | } |
| 1885 | |
| 1886 | static int |
| 1887 | ext4_xattr_block_set(handle_t *handle, struct inode *inode, |
| 1888 | struct ext4_xattr_info *i, |
| 1889 | struct ext4_xattr_block_find *bs) |
| 1890 | { |
| 1891 | struct super_block *sb = inode->i_sb; |
| 1892 | struct buffer_head *new_bh = NULL; |
| 1893 | struct ext4_xattr_search s_copy = bs->s; |
| 1894 | struct ext4_xattr_search *s = &s_copy; |
| 1895 | struct mb_cache_entry *ce = NULL; |
| 1896 | int error = 0; |
| 1897 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
| 1898 | struct inode *ea_inode = NULL, *tmp_inode; |
| 1899 | size_t old_ea_inode_quota = 0; |
| 1900 | unsigned int ea_ino; |
| 1901 | |
| 1902 | #define header(x) ((struct ext4_xattr_header *)(x)) |
| 1903 | |
| 1904 | /* If we need EA inode, prepare it before locking the buffer */ |
| 1905 | if (i->value && i->in_inode) { |
| 1906 | WARN_ON_ONCE(!i->value_len); |
| 1907 | |
| 1908 | ea_inode = ext4_xattr_inode_lookup_create(handle, inode, |
| 1909 | i->value, i->value_len); |
| 1910 | if (IS_ERR(ea_inode)) { |
| 1911 | error = PTR_ERR(ea_inode); |
| 1912 | ea_inode = NULL; |
| 1913 | goto cleanup; |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | if (s->base) { |
| 1918 | int offset = (char *)s->here - bs->bh->b_data; |
| 1919 | |
| 1920 | BUFFER_TRACE(bs->bh, "get_write_access"); |
| 1921 | error = ext4_journal_get_write_access(handle, sb, bs->bh, |
| 1922 | EXT4_JTR_NONE); |
| 1923 | if (error) |
| 1924 | goto cleanup; |
| 1925 | |
| 1926 | lock_buffer(bs->bh); |
| 1927 | |
| 1928 | if (header(s->base)->h_refcount == cpu_to_le32(1)) { |
| 1929 | __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash); |
| 1930 | |
| 1931 | /* |
| 1932 | * This must happen under buffer lock for |
| 1933 | * ext4_xattr_block_set() to reliably detect modified |
| 1934 | * block |
| 1935 | */ |
| 1936 | if (ea_block_cache) { |
| 1937 | struct mb_cache_entry *oe; |
| 1938 | |
| 1939 | oe = mb_cache_entry_delete_or_get(ea_block_cache, |
| 1940 | hash, bs->bh->b_blocknr); |
| 1941 | if (oe) { |
| 1942 | /* |
| 1943 | * Xattr block is getting reused. Leave |
| 1944 | * it alone. |
| 1945 | */ |
| 1946 | mb_cache_entry_put(ea_block_cache, oe); |
| 1947 | goto clone_block; |
| 1948 | } |
| 1949 | } |
| 1950 | ea_bdebug(bs->bh, "modifying in-place"); |
| 1951 | error = ext4_xattr_set_entry(i, s, handle, inode, |
| 1952 | ea_inode, true /* is_block */); |
| 1953 | ext4_xattr_block_csum_set(inode, bs->bh); |
| 1954 | unlock_buffer(bs->bh); |
| 1955 | if (error == -EFSCORRUPTED) |
| 1956 | goto bad_block; |
| 1957 | if (!error) |
| 1958 | error = ext4_handle_dirty_metadata(handle, |
| 1959 | inode, |
| 1960 | bs->bh); |
| 1961 | if (error) |
| 1962 | goto cleanup; |
| 1963 | goto inserted; |
| 1964 | } |
| 1965 | clone_block: |
| 1966 | unlock_buffer(bs->bh); |
| 1967 | ea_bdebug(bs->bh, "cloning"); |
| 1968 | s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS); |
| 1969 | error = -ENOMEM; |
| 1970 | if (s->base == NULL) |
| 1971 | goto cleanup; |
| 1972 | s->first = ENTRY(header(s->base)+1); |
| 1973 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 1974 | s->here = ENTRY(s->base + offset); |
| 1975 | s->end = s->base + bs->bh->b_size; |
| 1976 | |
| 1977 | /* |
| 1978 | * If existing entry points to an xattr inode, we need |
| 1979 | * to prevent ext4_xattr_set_entry() from decrementing |
| 1980 | * ref count on it because the reference belongs to the |
| 1981 | * original block. In this case, make the entry look |
| 1982 | * like it has an empty value. |
| 1983 | */ |
| 1984 | if (!s->not_found && s->here->e_value_inum) { |
| 1985 | ea_ino = le32_to_cpu(s->here->e_value_inum); |
| 1986 | error = ext4_xattr_inode_iget(inode, ea_ino, |
| 1987 | le32_to_cpu(s->here->e_hash), |
| 1988 | &tmp_inode); |
| 1989 | if (error) |
| 1990 | goto cleanup; |
| 1991 | |
| 1992 | if (!ext4_test_inode_state(tmp_inode, |
| 1993 | EXT4_STATE_LUSTRE_EA_INODE)) { |
| 1994 | /* |
| 1995 | * Defer quota free call for previous |
| 1996 | * inode until success is guaranteed. |
| 1997 | */ |
| 1998 | old_ea_inode_quota = le32_to_cpu( |
| 1999 | s->here->e_value_size); |
| 2000 | } |
| 2001 | iput(tmp_inode); |
| 2002 | |
| 2003 | s->here->e_value_inum = 0; |
| 2004 | s->here->e_value_size = 0; |
| 2005 | } |
| 2006 | } else { |
| 2007 | /* Allocate a buffer where we construct the new block. */ |
| 2008 | s->base = kzalloc(sb->s_blocksize, GFP_NOFS); |
| 2009 | error = -ENOMEM; |
| 2010 | if (s->base == NULL) |
| 2011 | goto cleanup; |
| 2012 | header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
| 2013 | header(s->base)->h_blocks = cpu_to_le32(1); |
| 2014 | header(s->base)->h_refcount = cpu_to_le32(1); |
| 2015 | s->first = ENTRY(header(s->base)+1); |
| 2016 | s->here = ENTRY(header(s->base)+1); |
| 2017 | s->end = s->base + sb->s_blocksize; |
| 2018 | } |
| 2019 | |
| 2020 | error = ext4_xattr_set_entry(i, s, handle, inode, ea_inode, |
| 2021 | true /* is_block */); |
| 2022 | if (error == -EFSCORRUPTED) |
| 2023 | goto bad_block; |
| 2024 | if (error) |
| 2025 | goto cleanup; |
| 2026 | |
| 2027 | inserted: |
| 2028 | if (!IS_LAST_ENTRY(s->first)) { |
| 2029 | new_bh = ext4_xattr_block_cache_find(inode, header(s->base), &ce); |
| 2030 | if (IS_ERR(new_bh)) { |
| 2031 | error = PTR_ERR(new_bh); |
| 2032 | new_bh = NULL; |
| 2033 | goto cleanup; |
| 2034 | } |
| 2035 | |
| 2036 | if (new_bh) { |
| 2037 | /* We found an identical block in the cache. */ |
| 2038 | if (new_bh == bs->bh) |
| 2039 | ea_bdebug(new_bh, "keeping"); |
| 2040 | else { |
| 2041 | u32 ref; |
| 2042 | |
| 2043 | #ifdef EXT4_XATTR_DEBUG |
| 2044 | WARN_ON_ONCE(dquot_initialize_needed(inode)); |
| 2045 | #endif |
| 2046 | /* The old block is released after updating |
| 2047 | the inode. */ |
| 2048 | error = dquot_alloc_block(inode, |
| 2049 | EXT4_C2B(EXT4_SB(sb), 1)); |
| 2050 | if (error) |
| 2051 | goto cleanup; |
| 2052 | BUFFER_TRACE(new_bh, "get_write_access"); |
| 2053 | error = ext4_journal_get_write_access( |
| 2054 | handle, sb, new_bh, |
| 2055 | EXT4_JTR_NONE); |
| 2056 | if (error) |
| 2057 | goto cleanup_dquot; |
| 2058 | lock_buffer(new_bh); |
| 2059 | /* |
| 2060 | * We have to be careful about races with |
| 2061 | * adding references to xattr block. Once we |
| 2062 | * hold buffer lock xattr block's state is |
| 2063 | * stable so we can check the additional |
| 2064 | * reference fits. |
| 2065 | */ |
| 2066 | ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1; |
| 2067 | if (ref > EXT4_XATTR_REFCOUNT_MAX) { |
| 2068 | /* |
| 2069 | * Undo everything and check mbcache |
| 2070 | * again. |
| 2071 | */ |
| 2072 | unlock_buffer(new_bh); |
| 2073 | dquot_free_block(inode, |
| 2074 | EXT4_C2B(EXT4_SB(sb), |
| 2075 | 1)); |
| 2076 | brelse(new_bh); |
| 2077 | mb_cache_entry_put(ea_block_cache, ce); |
| 2078 | ce = NULL; |
| 2079 | new_bh = NULL; |
| 2080 | goto inserted; |
| 2081 | } |
| 2082 | BHDR(new_bh)->h_refcount = cpu_to_le32(ref); |
| 2083 | if (ref == EXT4_XATTR_REFCOUNT_MAX) |
| 2084 | clear_bit(MBE_REUSABLE_B, &ce->e_flags); |
| 2085 | ea_bdebug(new_bh, "reusing; refcount now=%d", |
| 2086 | ref); |
| 2087 | ext4_xattr_block_csum_set(inode, new_bh); |
| 2088 | unlock_buffer(new_bh); |
| 2089 | error = ext4_handle_dirty_metadata(handle, |
| 2090 | inode, |
| 2091 | new_bh); |
| 2092 | if (error) |
| 2093 | goto cleanup_dquot; |
| 2094 | } |
| 2095 | mb_cache_entry_touch(ea_block_cache, ce); |
| 2096 | mb_cache_entry_put(ea_block_cache, ce); |
| 2097 | ce = NULL; |
| 2098 | } else if (bs->bh && s->base == bs->bh->b_data) { |
| 2099 | /* We were modifying this block in-place. */ |
| 2100 | ea_bdebug(bs->bh, "keeping this block"); |
| 2101 | ext4_xattr_block_cache_insert(ea_block_cache, bs->bh); |
| 2102 | new_bh = bs->bh; |
| 2103 | get_bh(new_bh); |
| 2104 | } else { |
| 2105 | /* We need to allocate a new block */ |
| 2106 | ext4_fsblk_t goal, block; |
| 2107 | |
| 2108 | #ifdef EXT4_XATTR_DEBUG |
| 2109 | WARN_ON_ONCE(dquot_initialize_needed(inode)); |
| 2110 | #endif |
| 2111 | goal = ext4_group_first_block_no(sb, |
| 2112 | EXT4_I(inode)->i_block_group); |
| 2113 | block = ext4_new_meta_blocks(handle, inode, goal, 0, |
| 2114 | NULL, &error); |
| 2115 | if (error) |
| 2116 | goto cleanup; |
| 2117 | |
| 2118 | ea_idebug(inode, "creating block %llu", |
| 2119 | (unsigned long long)block); |
| 2120 | |
| 2121 | new_bh = sb_getblk(sb, block); |
| 2122 | if (unlikely(!new_bh)) { |
| 2123 | error = -ENOMEM; |
| 2124 | getblk_failed: |
| 2125 | ext4_free_blocks(handle, inode, NULL, block, 1, |
| 2126 | EXT4_FREE_BLOCKS_METADATA); |
| 2127 | goto cleanup; |
| 2128 | } |
| 2129 | error = ext4_xattr_inode_inc_ref_all(handle, inode, |
| 2130 | ENTRY(header(s->base)+1)); |
| 2131 | if (error) |
| 2132 | goto getblk_failed; |
| 2133 | if (ea_inode) { |
| 2134 | /* Drop the extra ref on ea_inode. */ |
| 2135 | error = ext4_xattr_inode_dec_ref(handle, |
| 2136 | ea_inode); |
| 2137 | if (error) |
| 2138 | ext4_warning_inode(ea_inode, |
| 2139 | "dec ref error=%d", |
| 2140 | error); |
| 2141 | iput(ea_inode); |
| 2142 | ea_inode = NULL; |
| 2143 | } |
| 2144 | |
| 2145 | lock_buffer(new_bh); |
| 2146 | error = ext4_journal_get_create_access(handle, sb, |
| 2147 | new_bh, EXT4_JTR_NONE); |
| 2148 | if (error) { |
| 2149 | unlock_buffer(new_bh); |
| 2150 | error = -EIO; |
| 2151 | goto getblk_failed; |
| 2152 | } |
| 2153 | memcpy(new_bh->b_data, s->base, new_bh->b_size); |
| 2154 | ext4_xattr_block_csum_set(inode, new_bh); |
| 2155 | set_buffer_uptodate(new_bh); |
| 2156 | unlock_buffer(new_bh); |
| 2157 | ext4_xattr_block_cache_insert(ea_block_cache, new_bh); |
| 2158 | error = ext4_handle_dirty_metadata(handle, inode, |
| 2159 | new_bh); |
| 2160 | if (error) |
| 2161 | goto cleanup; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | if (old_ea_inode_quota) |
| 2166 | ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota); |
| 2167 | |
| 2168 | /* Update the inode. */ |
| 2169 | EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0; |
| 2170 | |
| 2171 | /* Drop the previous xattr block. */ |
| 2172 | if (bs->bh && bs->bh != new_bh) { |
| 2173 | struct ext4_xattr_inode_array *ea_inode_array = NULL; |
| 2174 | |
| 2175 | ext4_xattr_release_block(handle, inode, bs->bh, |
| 2176 | &ea_inode_array, |
| 2177 | 0 /* extra_credits */); |
| 2178 | ext4_xattr_inode_array_free(ea_inode_array); |
| 2179 | } |
| 2180 | error = 0; |
| 2181 | |
| 2182 | cleanup: |
| 2183 | if (ea_inode) { |
| 2184 | if (error) { |
| 2185 | int error2; |
| 2186 | |
| 2187 | error2 = ext4_xattr_inode_dec_ref(handle, ea_inode); |
| 2188 | if (error2) |
| 2189 | ext4_warning_inode(ea_inode, "dec ref error=%d", |
| 2190 | error2); |
| 2191 | ext4_xattr_inode_free_quota(inode, ea_inode, |
| 2192 | i_size_read(ea_inode)); |
| 2193 | } |
| 2194 | iput(ea_inode); |
| 2195 | } |
| 2196 | if (ce) |
| 2197 | mb_cache_entry_put(ea_block_cache, ce); |
| 2198 | brelse(new_bh); |
| 2199 | if (!(bs->bh && s->base == bs->bh->b_data)) |
| 2200 | kfree(s->base); |
| 2201 | |
| 2202 | return error; |
| 2203 | |
| 2204 | cleanup_dquot: |
| 2205 | dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1)); |
| 2206 | goto cleanup; |
| 2207 | |
| 2208 | bad_block: |
| 2209 | EXT4_ERROR_INODE(inode, "bad block %llu", |
| 2210 | EXT4_I(inode)->i_file_acl); |
| 2211 | goto cleanup; |
| 2212 | |
| 2213 | #undef header |
| 2214 | } |
| 2215 | |
| 2216 | int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, |
| 2217 | struct ext4_xattr_ibody_find *is) |
| 2218 | { |
| 2219 | struct ext4_xattr_ibody_header *header; |
| 2220 | struct ext4_inode *raw_inode; |
| 2221 | int error; |
| 2222 | |
| 2223 | if (!EXT4_INODE_HAS_XATTR_SPACE(inode)) |
| 2224 | return 0; |
| 2225 | |
| 2226 | raw_inode = ext4_raw_inode(&is->iloc); |
| 2227 | header = IHDR(inode, raw_inode); |
| 2228 | is->s.base = is->s.first = IFIRST(header); |
| 2229 | is->s.here = is->s.first; |
| 2230 | is->s.end = ITAIL(inode, raw_inode); |
| 2231 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
| 2232 | /* Find the named attribute. */ |
| 2233 | error = xattr_find_entry(inode, &is->s.here, is->s.end, |
| 2234 | i->name_index, i->name, 0); |
| 2235 | if (error && error != -ENODATA) |
| 2236 | return error; |
| 2237 | is->s.not_found = error; |
| 2238 | } |
| 2239 | return 0; |
| 2240 | } |
| 2241 | |
| 2242 | int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode, |
| 2243 | struct ext4_xattr_info *i, |
| 2244 | struct ext4_xattr_ibody_find *is) |
| 2245 | { |
| 2246 | struct ext4_xattr_ibody_header *header; |
| 2247 | struct ext4_xattr_search *s = &is->s; |
| 2248 | struct inode *ea_inode = NULL; |
| 2249 | int error; |
| 2250 | |
| 2251 | if (!EXT4_INODE_HAS_XATTR_SPACE(inode)) |
| 2252 | return -ENOSPC; |
| 2253 | |
| 2254 | /* If we need EA inode, prepare it before locking the buffer */ |
| 2255 | if (i->value && i->in_inode) { |
| 2256 | WARN_ON_ONCE(!i->value_len); |
| 2257 | |
| 2258 | ea_inode = ext4_xattr_inode_lookup_create(handle, inode, |
| 2259 | i->value, i->value_len); |
| 2260 | if (IS_ERR(ea_inode)) |
| 2261 | return PTR_ERR(ea_inode); |
| 2262 | } |
| 2263 | error = ext4_xattr_set_entry(i, s, handle, inode, ea_inode, |
| 2264 | false /* is_block */); |
| 2265 | if (error) { |
| 2266 | if (ea_inode) { |
| 2267 | int error2; |
| 2268 | |
| 2269 | error2 = ext4_xattr_inode_dec_ref(handle, ea_inode); |
| 2270 | if (error2) |
| 2271 | ext4_warning_inode(ea_inode, "dec ref error=%d", |
| 2272 | error2); |
| 2273 | |
| 2274 | ext4_xattr_inode_free_quota(inode, ea_inode, |
| 2275 | i_size_read(ea_inode)); |
| 2276 | iput(ea_inode); |
| 2277 | } |
| 2278 | return error; |
| 2279 | } |
| 2280 | header = IHDR(inode, ext4_raw_inode(&is->iloc)); |
| 2281 | if (!IS_LAST_ENTRY(s->first)) { |
| 2282 | header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); |
| 2283 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
| 2284 | } else { |
| 2285 | header->h_magic = cpu_to_le32(0); |
| 2286 | ext4_clear_inode_state(inode, EXT4_STATE_XATTR); |
| 2287 | } |
| 2288 | iput(ea_inode); |
| 2289 | return 0; |
| 2290 | } |
| 2291 | |
| 2292 | static int ext4_xattr_value_same(struct ext4_xattr_search *s, |
| 2293 | struct ext4_xattr_info *i) |
| 2294 | { |
| 2295 | void *value; |
| 2296 | |
| 2297 | /* When e_value_inum is set the value is stored externally. */ |
| 2298 | if (s->here->e_value_inum) |
| 2299 | return 0; |
| 2300 | if (le32_to_cpu(s->here->e_value_size) != i->value_len) |
| 2301 | return 0; |
| 2302 | value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs); |
| 2303 | return !memcmp(value, i->value, i->value_len); |
| 2304 | } |
| 2305 | |
| 2306 | static struct buffer_head *ext4_xattr_get_block(struct inode *inode) |
| 2307 | { |
| 2308 | struct buffer_head *bh; |
| 2309 | int error; |
| 2310 | |
| 2311 | if (!EXT4_I(inode)->i_file_acl) |
| 2312 | return NULL; |
| 2313 | bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO); |
| 2314 | if (IS_ERR(bh)) |
| 2315 | return bh; |
| 2316 | error = ext4_xattr_check_block(inode, bh); |
| 2317 | if (error) { |
| 2318 | brelse(bh); |
| 2319 | return ERR_PTR(error); |
| 2320 | } |
| 2321 | return bh; |
| 2322 | } |
| 2323 | |
| 2324 | /* |
| 2325 | * ext4_xattr_set_handle() |
| 2326 | * |
| 2327 | * Create, replace or remove an extended attribute for this inode. Value |
| 2328 | * is NULL to remove an existing extended attribute, and non-NULL to |
| 2329 | * either replace an existing extended attribute, or create a new extended |
| 2330 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE |
| 2331 | * specify that an extended attribute must exist and must not exist |
| 2332 | * previous to the call, respectively. |
| 2333 | * |
| 2334 | * Returns 0, or a negative error number on failure. |
| 2335 | */ |
| 2336 | int |
| 2337 | ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index, |
| 2338 | const char *name, const void *value, size_t value_len, |
| 2339 | int flags) |
| 2340 | { |
| 2341 | struct ext4_xattr_info i = { |
| 2342 | .name_index = name_index, |
| 2343 | .name = name, |
| 2344 | .value = value, |
| 2345 | .value_len = value_len, |
| 2346 | .in_inode = 0, |
| 2347 | }; |
| 2348 | struct ext4_xattr_ibody_find is = { |
| 2349 | .s = { .not_found = -ENODATA, }, |
| 2350 | }; |
| 2351 | struct ext4_xattr_block_find bs = { |
| 2352 | .s = { .not_found = -ENODATA, }, |
| 2353 | }; |
| 2354 | int no_expand; |
| 2355 | int error; |
| 2356 | |
| 2357 | if (!name) |
| 2358 | return -EINVAL; |
| 2359 | if (strlen(name) > 255) |
| 2360 | return -ERANGE; |
| 2361 | |
| 2362 | ext4_write_lock_xattr(inode, &no_expand); |
| 2363 | |
| 2364 | /* Check journal credits under write lock. */ |
| 2365 | if (ext4_handle_valid(handle)) { |
| 2366 | struct buffer_head *bh; |
| 2367 | int credits; |
| 2368 | |
| 2369 | bh = ext4_xattr_get_block(inode); |
| 2370 | if (IS_ERR(bh)) { |
| 2371 | error = PTR_ERR(bh); |
| 2372 | goto cleanup; |
| 2373 | } |
| 2374 | |
| 2375 | credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh, |
| 2376 | value_len, |
| 2377 | flags & XATTR_CREATE); |
| 2378 | brelse(bh); |
| 2379 | |
| 2380 | if (jbd2_handle_buffer_credits(handle) < credits) { |
| 2381 | error = -ENOSPC; |
| 2382 | goto cleanup; |
| 2383 | } |
| 2384 | WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS)); |
| 2385 | } |
| 2386 | |
| 2387 | error = ext4_reserve_inode_write(handle, inode, &is.iloc); |
| 2388 | if (error) |
| 2389 | goto cleanup; |
| 2390 | |
| 2391 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) { |
| 2392 | struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc); |
| 2393 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); |
| 2394 | ext4_clear_inode_state(inode, EXT4_STATE_NEW); |
| 2395 | } |
| 2396 | |
| 2397 | error = ext4_xattr_ibody_find(inode, &i, &is); |
| 2398 | if (error) |
| 2399 | goto cleanup; |
| 2400 | if (is.s.not_found) |
| 2401 | error = ext4_xattr_block_find(inode, &i, &bs); |
| 2402 | if (error) |
| 2403 | goto cleanup; |
| 2404 | if (is.s.not_found && bs.s.not_found) { |
| 2405 | error = -ENODATA; |
| 2406 | if (flags & XATTR_REPLACE) |
| 2407 | goto cleanup; |
| 2408 | error = 0; |
| 2409 | if (!value) |
| 2410 | goto cleanup; |
| 2411 | } else { |
| 2412 | error = -EEXIST; |
| 2413 | if (flags & XATTR_CREATE) |
| 2414 | goto cleanup; |
| 2415 | } |
| 2416 | |
| 2417 | if (!value) { |
| 2418 | if (!is.s.not_found) |
| 2419 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
| 2420 | else if (!bs.s.not_found) |
| 2421 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
| 2422 | } else { |
| 2423 | error = 0; |
| 2424 | /* Xattr value did not change? Save us some work and bail out */ |
| 2425 | if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i)) |
| 2426 | goto cleanup; |
| 2427 | if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i)) |
| 2428 | goto cleanup; |
| 2429 | |
| 2430 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
| 2431 | (EXT4_XATTR_SIZE(i.value_len) > |
| 2432 | EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize))) |
| 2433 | i.in_inode = 1; |
| 2434 | retry_inode: |
| 2435 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
| 2436 | if (!error && !bs.s.not_found) { |
| 2437 | i.value = NULL; |
| 2438 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
| 2439 | } else if (error == -ENOSPC) { |
| 2440 | if (EXT4_I(inode)->i_file_acl && !bs.s.base) { |
| 2441 | brelse(bs.bh); |
| 2442 | bs.bh = NULL; |
| 2443 | error = ext4_xattr_block_find(inode, &i, &bs); |
| 2444 | if (error) |
| 2445 | goto cleanup; |
| 2446 | } |
| 2447 | error = ext4_xattr_block_set(handle, inode, &i, &bs); |
| 2448 | if (!error && !is.s.not_found) { |
| 2449 | i.value = NULL; |
| 2450 | error = ext4_xattr_ibody_set(handle, inode, &i, |
| 2451 | &is); |
| 2452 | } else if (error == -ENOSPC) { |
| 2453 | /* |
| 2454 | * Xattr does not fit in the block, store at |
| 2455 | * external inode if possible. |
| 2456 | */ |
| 2457 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
| 2458 | i.value_len && !i.in_inode) { |
| 2459 | i.in_inode = 1; |
| 2460 | goto retry_inode; |
| 2461 | } |
| 2462 | } |
| 2463 | } |
| 2464 | } |
| 2465 | if (!error) { |
| 2466 | ext4_xattr_update_super_block(handle, inode->i_sb); |
| 2467 | inode_set_ctime_current(inode); |
| 2468 | inode_inc_iversion(inode); |
| 2469 | if (!value) |
| 2470 | no_expand = 0; |
| 2471 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
| 2472 | /* |
| 2473 | * The bh is consumed by ext4_mark_iloc_dirty, even with |
| 2474 | * error != 0. |
| 2475 | */ |
| 2476 | is.iloc.bh = NULL; |
| 2477 | if (IS_SYNC(inode)) |
| 2478 | ext4_handle_sync(handle); |
| 2479 | } |
| 2480 | ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle); |
| 2481 | |
| 2482 | cleanup: |
| 2483 | brelse(is.iloc.bh); |
| 2484 | brelse(bs.bh); |
| 2485 | ext4_write_unlock_xattr(inode, &no_expand); |
| 2486 | return error; |
| 2487 | } |
| 2488 | |
| 2489 | int ext4_xattr_set_credits(struct inode *inode, size_t value_len, |
| 2490 | bool is_create, int *credits) |
| 2491 | { |
| 2492 | struct buffer_head *bh; |
| 2493 | int err; |
| 2494 | |
| 2495 | *credits = 0; |
| 2496 | |
| 2497 | if (!EXT4_SB(inode->i_sb)->s_journal) |
| 2498 | return 0; |
| 2499 | |
| 2500 | down_read(&EXT4_I(inode)->xattr_sem); |
| 2501 | |
| 2502 | bh = ext4_xattr_get_block(inode); |
| 2503 | if (IS_ERR(bh)) { |
| 2504 | err = PTR_ERR(bh); |
| 2505 | } else { |
| 2506 | *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh, |
| 2507 | value_len, is_create); |
| 2508 | brelse(bh); |
| 2509 | err = 0; |
| 2510 | } |
| 2511 | |
| 2512 | up_read(&EXT4_I(inode)->xattr_sem); |
| 2513 | return err; |
| 2514 | } |
| 2515 | |
| 2516 | /* |
| 2517 | * ext4_xattr_set() |
| 2518 | * |
| 2519 | * Like ext4_xattr_set_handle, but start from an inode. This extended |
| 2520 | * attribute modification is a filesystem transaction by itself. |
| 2521 | * |
| 2522 | * Returns 0, or a negative error number on failure. |
| 2523 | */ |
| 2524 | int |
| 2525 | ext4_xattr_set(struct inode *inode, int name_index, const char *name, |
| 2526 | const void *value, size_t value_len, int flags) |
| 2527 | { |
| 2528 | handle_t *handle; |
| 2529 | struct super_block *sb = inode->i_sb; |
| 2530 | int error, retries = 0; |
| 2531 | int credits; |
| 2532 | |
| 2533 | error = dquot_initialize(inode); |
| 2534 | if (error) |
| 2535 | return error; |
| 2536 | |
| 2537 | retry: |
| 2538 | error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE, |
| 2539 | &credits); |
| 2540 | if (error) |
| 2541 | return error; |
| 2542 | |
| 2543 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
| 2544 | if (IS_ERR(handle)) { |
| 2545 | error = PTR_ERR(handle); |
| 2546 | } else { |
| 2547 | int error2; |
| 2548 | |
| 2549 | error = ext4_xattr_set_handle(handle, inode, name_index, name, |
| 2550 | value, value_len, flags); |
| 2551 | ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, |
| 2552 | handle); |
| 2553 | error2 = ext4_journal_stop(handle); |
| 2554 | if (error == -ENOSPC && |
| 2555 | ext4_should_retry_alloc(sb, &retries)) |
| 2556 | goto retry; |
| 2557 | if (error == 0) |
| 2558 | error = error2; |
| 2559 | } |
| 2560 | |
| 2561 | return error; |
| 2562 | } |
| 2563 | |
| 2564 | /* |
| 2565 | * Shift the EA entries in the inode to create space for the increased |
| 2566 | * i_extra_isize. |
| 2567 | */ |
| 2568 | static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry, |
| 2569 | int value_offs_shift, void *to, |
| 2570 | void *from, size_t n) |
| 2571 | { |
| 2572 | struct ext4_xattr_entry *last = entry; |
| 2573 | int new_offs; |
| 2574 | |
| 2575 | /* We always shift xattr headers further thus offsets get lower */ |
| 2576 | BUG_ON(value_offs_shift > 0); |
| 2577 | |
| 2578 | /* Adjust the value offsets of the entries */ |
| 2579 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
| 2580 | if (!last->e_value_inum && last->e_value_size) { |
| 2581 | new_offs = le16_to_cpu(last->e_value_offs) + |
| 2582 | value_offs_shift; |
| 2583 | last->e_value_offs = cpu_to_le16(new_offs); |
| 2584 | } |
| 2585 | } |
| 2586 | /* Shift the entries by n bytes */ |
| 2587 | memmove(to, from, n); |
| 2588 | } |
| 2589 | |
| 2590 | /* |
| 2591 | * Move xattr pointed to by 'entry' from inode into external xattr block |
| 2592 | */ |
| 2593 | static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, |
| 2594 | struct ext4_inode *raw_inode, |
| 2595 | struct ext4_xattr_entry *entry) |
| 2596 | { |
| 2597 | struct ext4_xattr_ibody_find *is = NULL; |
| 2598 | struct ext4_xattr_block_find *bs = NULL; |
| 2599 | char *buffer = NULL, *b_entry_name = NULL; |
| 2600 | size_t value_size = le32_to_cpu(entry->e_value_size); |
| 2601 | struct ext4_xattr_info i = { |
| 2602 | .value = NULL, |
| 2603 | .value_len = 0, |
| 2604 | .name_index = entry->e_name_index, |
| 2605 | .in_inode = !!entry->e_value_inum, |
| 2606 | }; |
| 2607 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); |
| 2608 | int needs_kvfree = 0; |
| 2609 | int error; |
| 2610 | |
| 2611 | is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); |
| 2612 | bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS); |
| 2613 | b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS); |
| 2614 | if (!is || !bs || !b_entry_name) { |
| 2615 | error = -ENOMEM; |
| 2616 | goto out; |
| 2617 | } |
| 2618 | |
| 2619 | is->s.not_found = -ENODATA; |
| 2620 | bs->s.not_found = -ENODATA; |
| 2621 | is->iloc.bh = NULL; |
| 2622 | bs->bh = NULL; |
| 2623 | |
| 2624 | /* Save the entry name and the entry value */ |
| 2625 | if (entry->e_value_inum) { |
| 2626 | buffer = kvmalloc(value_size, GFP_NOFS); |
| 2627 | if (!buffer) { |
| 2628 | error = -ENOMEM; |
| 2629 | goto out; |
| 2630 | } |
| 2631 | needs_kvfree = 1; |
| 2632 | error = ext4_xattr_inode_get(inode, entry, buffer, value_size); |
| 2633 | if (error) |
| 2634 | goto out; |
| 2635 | } else { |
| 2636 | size_t value_offs = le16_to_cpu(entry->e_value_offs); |
| 2637 | buffer = (void *)IFIRST(header) + value_offs; |
| 2638 | } |
| 2639 | |
| 2640 | memcpy(b_entry_name, entry->e_name, entry->e_name_len); |
| 2641 | b_entry_name[entry->e_name_len] = '\0'; |
| 2642 | i.name = b_entry_name; |
| 2643 | |
| 2644 | error = ext4_get_inode_loc(inode, &is->iloc); |
| 2645 | if (error) |
| 2646 | goto out; |
| 2647 | |
| 2648 | error = ext4_xattr_ibody_find(inode, &i, is); |
| 2649 | if (error) |
| 2650 | goto out; |
| 2651 | |
| 2652 | i.value = buffer; |
| 2653 | i.value_len = value_size; |
| 2654 | error = ext4_xattr_block_find(inode, &i, bs); |
| 2655 | if (error) |
| 2656 | goto out; |
| 2657 | |
| 2658 | /* Move ea entry from the inode into the block */ |
| 2659 | error = ext4_xattr_block_set(handle, inode, &i, bs); |
| 2660 | if (error) |
| 2661 | goto out; |
| 2662 | |
| 2663 | /* Remove the chosen entry from the inode */ |
| 2664 | i.value = NULL; |
| 2665 | i.value_len = 0; |
| 2666 | error = ext4_xattr_ibody_set(handle, inode, &i, is); |
| 2667 | |
| 2668 | out: |
| 2669 | kfree(b_entry_name); |
| 2670 | if (needs_kvfree && buffer) |
| 2671 | kvfree(buffer); |
| 2672 | if (is) |
| 2673 | brelse(is->iloc.bh); |
| 2674 | if (bs) |
| 2675 | brelse(bs->bh); |
| 2676 | kfree(is); |
| 2677 | kfree(bs); |
| 2678 | |
| 2679 | return error; |
| 2680 | } |
| 2681 | |
| 2682 | static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode, |
| 2683 | struct ext4_inode *raw_inode, |
| 2684 | int isize_diff, size_t ifree, |
| 2685 | size_t bfree, int *total_ino) |
| 2686 | { |
| 2687 | struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); |
| 2688 | struct ext4_xattr_entry *small_entry; |
| 2689 | struct ext4_xattr_entry *entry; |
| 2690 | struct ext4_xattr_entry *last; |
| 2691 | unsigned int entry_size; /* EA entry size */ |
| 2692 | unsigned int total_size; /* EA entry size + value size */ |
| 2693 | unsigned int min_total_size; |
| 2694 | int error; |
| 2695 | |
| 2696 | while (isize_diff > ifree) { |
| 2697 | entry = NULL; |
| 2698 | small_entry = NULL; |
| 2699 | min_total_size = ~0U; |
| 2700 | last = IFIRST(header); |
| 2701 | /* Find the entry best suited to be pushed into EA block */ |
| 2702 | for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { |
| 2703 | /* never move system.data out of the inode */ |
| 2704 | if ((last->e_name_len == 4) && |
| 2705 | (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) && |
| 2706 | !memcmp(last->e_name, "data", 4)) |
| 2707 | continue; |
| 2708 | total_size = EXT4_XATTR_LEN(last->e_name_len); |
| 2709 | if (!last->e_value_inum) |
| 2710 | total_size += EXT4_XATTR_SIZE( |
| 2711 | le32_to_cpu(last->e_value_size)); |
| 2712 | if (total_size <= bfree && |
| 2713 | total_size < min_total_size) { |
| 2714 | if (total_size + ifree < isize_diff) { |
| 2715 | small_entry = last; |
| 2716 | } else { |
| 2717 | entry = last; |
| 2718 | min_total_size = total_size; |
| 2719 | } |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | if (entry == NULL) { |
| 2724 | if (small_entry == NULL) |
| 2725 | return -ENOSPC; |
| 2726 | entry = small_entry; |
| 2727 | } |
| 2728 | |
| 2729 | entry_size = EXT4_XATTR_LEN(entry->e_name_len); |
| 2730 | total_size = entry_size; |
| 2731 | if (!entry->e_value_inum) |
| 2732 | total_size += EXT4_XATTR_SIZE( |
| 2733 | le32_to_cpu(entry->e_value_size)); |
| 2734 | error = ext4_xattr_move_to_block(handle, inode, raw_inode, |
| 2735 | entry); |
| 2736 | if (error) |
| 2737 | return error; |
| 2738 | |
| 2739 | *total_ino -= entry_size; |
| 2740 | ifree += total_size; |
| 2741 | bfree -= total_size; |
| 2742 | } |
| 2743 | |
| 2744 | return 0; |
| 2745 | } |
| 2746 | |
| 2747 | /* |
| 2748 | * Expand an inode by new_extra_isize bytes when EAs are present. |
| 2749 | * Returns 0 on success or negative error number on failure. |
| 2750 | */ |
| 2751 | int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize, |
| 2752 | struct ext4_inode *raw_inode, handle_t *handle) |
| 2753 | { |
| 2754 | struct ext4_xattr_ibody_header *header; |
| 2755 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 2756 | static unsigned int mnt_count; |
| 2757 | size_t min_offs; |
| 2758 | size_t ifree, bfree; |
| 2759 | int total_ino; |
| 2760 | void *base, *end; |
| 2761 | int error = 0, tried_min_extra_isize = 0; |
| 2762 | int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize); |
| 2763 | int isize_diff; /* How much do we need to grow i_extra_isize */ |
| 2764 | |
| 2765 | retry: |
| 2766 | isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize; |
| 2767 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) |
| 2768 | return 0; |
| 2769 | |
| 2770 | header = IHDR(inode, raw_inode); |
| 2771 | |
| 2772 | /* |
| 2773 | * Check if enough free space is available in the inode to shift the |
| 2774 | * entries ahead by new_extra_isize. |
| 2775 | */ |
| 2776 | |
| 2777 | base = IFIRST(header); |
| 2778 | end = ITAIL(inode, raw_inode); |
| 2779 | min_offs = end - base; |
| 2780 | total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32); |
| 2781 | |
| 2782 | ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino); |
| 2783 | if (ifree >= isize_diff) |
| 2784 | goto shift; |
| 2785 | |
| 2786 | /* |
| 2787 | * Enough free space isn't available in the inode, check if |
| 2788 | * EA block can hold new_extra_isize bytes. |
| 2789 | */ |
| 2790 | if (EXT4_I(inode)->i_file_acl) { |
| 2791 | struct buffer_head *bh; |
| 2792 | |
| 2793 | bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO); |
| 2794 | if (IS_ERR(bh)) { |
| 2795 | error = PTR_ERR(bh); |
| 2796 | goto cleanup; |
| 2797 | } |
| 2798 | error = ext4_xattr_check_block(inode, bh); |
| 2799 | if (error) { |
| 2800 | brelse(bh); |
| 2801 | goto cleanup; |
| 2802 | } |
| 2803 | base = BHDR(bh); |
| 2804 | end = bh->b_data + bh->b_size; |
| 2805 | min_offs = end - base; |
| 2806 | bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base, |
| 2807 | NULL); |
| 2808 | brelse(bh); |
| 2809 | if (bfree + ifree < isize_diff) { |
| 2810 | if (!tried_min_extra_isize && s_min_extra_isize) { |
| 2811 | tried_min_extra_isize++; |
| 2812 | new_extra_isize = s_min_extra_isize; |
| 2813 | goto retry; |
| 2814 | } |
| 2815 | error = -ENOSPC; |
| 2816 | goto cleanup; |
| 2817 | } |
| 2818 | } else { |
| 2819 | bfree = inode->i_sb->s_blocksize; |
| 2820 | } |
| 2821 | |
| 2822 | error = ext4_xattr_make_inode_space(handle, inode, raw_inode, |
| 2823 | isize_diff, ifree, bfree, |
| 2824 | &total_ino); |
| 2825 | if (error) { |
| 2826 | if (error == -ENOSPC && !tried_min_extra_isize && |
| 2827 | s_min_extra_isize) { |
| 2828 | tried_min_extra_isize++; |
| 2829 | new_extra_isize = s_min_extra_isize; |
| 2830 | goto retry; |
| 2831 | } |
| 2832 | goto cleanup; |
| 2833 | } |
| 2834 | shift: |
| 2835 | /* Adjust the offsets and shift the remaining entries ahead */ |
| 2836 | ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize |
| 2837 | - new_extra_isize, (void *)raw_inode + |
| 2838 | EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize, |
| 2839 | (void *)header, total_ino); |
| 2840 | EXT4_I(inode)->i_extra_isize = new_extra_isize; |
| 2841 | |
| 2842 | if (ext4_has_inline_data(inode)) |
| 2843 | error = ext4_find_inline_data_nolock(inode); |
| 2844 | |
| 2845 | cleanup: |
| 2846 | if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) { |
| 2847 | ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.", |
| 2848 | inode->i_ino); |
| 2849 | mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count); |
| 2850 | } |
| 2851 | return error; |
| 2852 | } |
| 2853 | |
| 2854 | #define EIA_INCR 16 /* must be 2^n */ |
| 2855 | #define EIA_MASK (EIA_INCR - 1) |
| 2856 | |
| 2857 | /* Add the large xattr @inode into @ea_inode_array for deferred iput(). |
| 2858 | * If @ea_inode_array is new or full it will be grown and the old |
| 2859 | * contents copied over. |
| 2860 | */ |
| 2861 | static int |
| 2862 | ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, |
| 2863 | struct inode *inode) |
| 2864 | { |
| 2865 | if (*ea_inode_array == NULL) { |
| 2866 | /* |
| 2867 | * Start with 15 inodes, so it fits into a power-of-two size. |
| 2868 | */ |
| 2869 | (*ea_inode_array) = kmalloc( |
| 2870 | struct_size(*ea_inode_array, inodes, EIA_MASK), |
| 2871 | GFP_NOFS); |
| 2872 | if (*ea_inode_array == NULL) |
| 2873 | return -ENOMEM; |
| 2874 | (*ea_inode_array)->count = 0; |
| 2875 | } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) { |
| 2876 | /* expand the array once all 15 + n * 16 slots are full */ |
| 2877 | struct ext4_xattr_inode_array *new_array = NULL; |
| 2878 | |
| 2879 | new_array = kmalloc( |
| 2880 | struct_size(*ea_inode_array, inodes, |
| 2881 | (*ea_inode_array)->count + EIA_INCR), |
| 2882 | GFP_NOFS); |
| 2883 | if (new_array == NULL) |
| 2884 | return -ENOMEM; |
| 2885 | memcpy(new_array, *ea_inode_array, |
| 2886 | struct_size(*ea_inode_array, inodes, |
| 2887 | (*ea_inode_array)->count)); |
| 2888 | kfree(*ea_inode_array); |
| 2889 | *ea_inode_array = new_array; |
| 2890 | } |
| 2891 | (*ea_inode_array)->count++; |
| 2892 | (*ea_inode_array)->inodes[(*ea_inode_array)->count - 1] = inode; |
| 2893 | return 0; |
| 2894 | } |
| 2895 | |
| 2896 | /* |
| 2897 | * ext4_xattr_delete_inode() |
| 2898 | * |
| 2899 | * Free extended attribute resources associated with this inode. Traverse |
| 2900 | * all entries and decrement reference on any xattr inodes associated with this |
| 2901 | * inode. This is called immediately before an inode is freed. We have exclusive |
| 2902 | * access to the inode. If an orphan inode is deleted it will also release its |
| 2903 | * references on xattr block and xattr inodes. |
| 2904 | */ |
| 2905 | int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode, |
| 2906 | struct ext4_xattr_inode_array **ea_inode_array, |
| 2907 | int extra_credits) |
| 2908 | { |
| 2909 | struct buffer_head *bh = NULL; |
| 2910 | struct ext4_xattr_ibody_header *header; |
| 2911 | struct ext4_iloc iloc = { .bh = NULL }; |
| 2912 | struct ext4_xattr_entry *entry; |
| 2913 | struct inode *ea_inode; |
| 2914 | int error; |
| 2915 | |
| 2916 | error = ext4_journal_ensure_credits(handle, extra_credits, |
| 2917 | ext4_free_metadata_revoke_credits(inode->i_sb, 1)); |
| 2918 | if (error < 0) { |
| 2919 | EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error); |
| 2920 | goto cleanup; |
| 2921 | } |
| 2922 | |
| 2923 | if (ext4_has_feature_ea_inode(inode->i_sb) && |
| 2924 | ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
| 2925 | |
| 2926 | error = ext4_get_inode_loc(inode, &iloc); |
| 2927 | if (error) { |
| 2928 | EXT4_ERROR_INODE(inode, "inode loc (error %d)", error); |
| 2929 | goto cleanup; |
| 2930 | } |
| 2931 | |
| 2932 | error = ext4_journal_get_write_access(handle, inode->i_sb, |
| 2933 | iloc.bh, EXT4_JTR_NONE); |
| 2934 | if (error) { |
| 2935 | EXT4_ERROR_INODE(inode, "write access (error %d)", |
| 2936 | error); |
| 2937 | goto cleanup; |
| 2938 | } |
| 2939 | |
| 2940 | header = IHDR(inode, ext4_raw_inode(&iloc)); |
| 2941 | if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC)) |
| 2942 | ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh, |
| 2943 | IFIRST(header), |
| 2944 | false /* block_csum */, |
| 2945 | ea_inode_array, |
| 2946 | extra_credits, |
| 2947 | false /* skip_quota */); |
| 2948 | } |
| 2949 | |
| 2950 | if (EXT4_I(inode)->i_file_acl) { |
| 2951 | bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO); |
| 2952 | if (IS_ERR(bh)) { |
| 2953 | error = PTR_ERR(bh); |
| 2954 | if (error == -EIO) { |
| 2955 | EXT4_ERROR_INODE_ERR(inode, EIO, |
| 2956 | "block %llu read error", |
| 2957 | EXT4_I(inode)->i_file_acl); |
| 2958 | } |
| 2959 | bh = NULL; |
| 2960 | goto cleanup; |
| 2961 | } |
| 2962 | error = ext4_xattr_check_block(inode, bh); |
| 2963 | if (error) |
| 2964 | goto cleanup; |
| 2965 | |
| 2966 | if (ext4_has_feature_ea_inode(inode->i_sb)) { |
| 2967 | for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry); |
| 2968 | entry = EXT4_XATTR_NEXT(entry)) { |
| 2969 | if (!entry->e_value_inum) |
| 2970 | continue; |
| 2971 | error = ext4_xattr_inode_iget(inode, |
| 2972 | le32_to_cpu(entry->e_value_inum), |
| 2973 | le32_to_cpu(entry->e_hash), |
| 2974 | &ea_inode); |
| 2975 | if (error) |
| 2976 | continue; |
| 2977 | ext4_xattr_inode_free_quota(inode, ea_inode, |
| 2978 | le32_to_cpu(entry->e_value_size)); |
| 2979 | iput(ea_inode); |
| 2980 | } |
| 2981 | |
| 2982 | } |
| 2983 | |
| 2984 | ext4_xattr_release_block(handle, inode, bh, ea_inode_array, |
| 2985 | extra_credits); |
| 2986 | /* |
| 2987 | * Update i_file_acl value in the same transaction that releases |
| 2988 | * block. |
| 2989 | */ |
| 2990 | EXT4_I(inode)->i_file_acl = 0; |
| 2991 | error = ext4_mark_inode_dirty(handle, inode); |
| 2992 | if (error) { |
| 2993 | EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)", |
| 2994 | error); |
| 2995 | goto cleanup; |
| 2996 | } |
| 2997 | ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle); |
| 2998 | } |
| 2999 | error = 0; |
| 3000 | cleanup: |
| 3001 | brelse(iloc.bh); |
| 3002 | brelse(bh); |
| 3003 | return error; |
| 3004 | } |
| 3005 | |
| 3006 | void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array) |
| 3007 | { |
| 3008 | int idx; |
| 3009 | |
| 3010 | if (ea_inode_array == NULL) |
| 3011 | return; |
| 3012 | |
| 3013 | for (idx = 0; idx < ea_inode_array->count; ++idx) |
| 3014 | iput(ea_inode_array->inodes[idx]); |
| 3015 | kfree(ea_inode_array); |
| 3016 | } |
| 3017 | |
| 3018 | /* |
| 3019 | * ext4_xattr_block_cache_insert() |
| 3020 | * |
| 3021 | * Create a new entry in the extended attribute block cache, and insert |
| 3022 | * it unless such an entry is already in the cache. |
| 3023 | */ |
| 3024 | static void |
| 3025 | ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache, |
| 3026 | struct buffer_head *bh) |
| 3027 | { |
| 3028 | struct ext4_xattr_header *header = BHDR(bh); |
| 3029 | __u32 hash = le32_to_cpu(header->h_hash); |
| 3030 | int reusable = le32_to_cpu(header->h_refcount) < |
| 3031 | EXT4_XATTR_REFCOUNT_MAX; |
| 3032 | int error; |
| 3033 | |
| 3034 | if (!ea_block_cache) |
| 3035 | return; |
| 3036 | error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash, |
| 3037 | bh->b_blocknr, reusable); |
| 3038 | if (error) { |
| 3039 | if (error == -EBUSY) |
| 3040 | ea_bdebug(bh, "already in cache"); |
| 3041 | } else |
| 3042 | ea_bdebug(bh, "inserting [%x]", (int)hash); |
| 3043 | } |
| 3044 | |
| 3045 | /* |
| 3046 | * ext4_xattr_cmp() |
| 3047 | * |
| 3048 | * Compare two extended attribute blocks for equality. |
| 3049 | * |
| 3050 | * Returns 0 if the blocks are equal, 1 if they differ. |
| 3051 | */ |
| 3052 | static int |
| 3053 | ext4_xattr_cmp(struct ext4_xattr_header *header1, |
| 3054 | struct ext4_xattr_header *header2) |
| 3055 | { |
| 3056 | struct ext4_xattr_entry *entry1, *entry2; |
| 3057 | |
| 3058 | entry1 = ENTRY(header1+1); |
| 3059 | entry2 = ENTRY(header2+1); |
| 3060 | while (!IS_LAST_ENTRY(entry1)) { |
| 3061 | if (IS_LAST_ENTRY(entry2)) |
| 3062 | return 1; |
| 3063 | if (entry1->e_hash != entry2->e_hash || |
| 3064 | entry1->e_name_index != entry2->e_name_index || |
| 3065 | entry1->e_name_len != entry2->e_name_len || |
| 3066 | entry1->e_value_size != entry2->e_value_size || |
| 3067 | entry1->e_value_inum != entry2->e_value_inum || |
| 3068 | memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) |
| 3069 | return 1; |
| 3070 | if (!entry1->e_value_inum && |
| 3071 | memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), |
| 3072 | (char *)header2 + le16_to_cpu(entry2->e_value_offs), |
| 3073 | le32_to_cpu(entry1->e_value_size))) |
| 3074 | return 1; |
| 3075 | |
| 3076 | entry1 = EXT4_XATTR_NEXT(entry1); |
| 3077 | entry2 = EXT4_XATTR_NEXT(entry2); |
| 3078 | } |
| 3079 | if (!IS_LAST_ENTRY(entry2)) |
| 3080 | return 1; |
| 3081 | return 0; |
| 3082 | } |
| 3083 | |
| 3084 | /* |
| 3085 | * ext4_xattr_block_cache_find() |
| 3086 | * |
| 3087 | * Find an identical extended attribute block. |
| 3088 | * |
| 3089 | * Returns a pointer to the block found, or NULL if such a block was not |
| 3090 | * found, or an error pointer if an error occurred while reading ea block. |
| 3091 | */ |
| 3092 | static struct buffer_head * |
| 3093 | ext4_xattr_block_cache_find(struct inode *inode, |
| 3094 | struct ext4_xattr_header *header, |
| 3095 | struct mb_cache_entry **pce) |
| 3096 | { |
| 3097 | __u32 hash = le32_to_cpu(header->h_hash); |
| 3098 | struct mb_cache_entry *ce; |
| 3099 | struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); |
| 3100 | |
| 3101 | if (!ea_block_cache) |
| 3102 | return NULL; |
| 3103 | if (!header->h_hash) |
| 3104 | return NULL; /* never share */ |
| 3105 | ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); |
| 3106 | ce = mb_cache_entry_find_first(ea_block_cache, hash); |
| 3107 | while (ce) { |
| 3108 | struct buffer_head *bh; |
| 3109 | |
| 3110 | bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO); |
| 3111 | if (IS_ERR(bh)) { |
| 3112 | if (PTR_ERR(bh) != -ENOMEM) |
| 3113 | EXT4_ERROR_INODE(inode, "block %lu read error", |
| 3114 | (unsigned long)ce->e_value); |
| 3115 | mb_cache_entry_put(ea_block_cache, ce); |
| 3116 | return bh; |
| 3117 | } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { |
| 3118 | *pce = ce; |
| 3119 | return bh; |
| 3120 | } |
| 3121 | brelse(bh); |
| 3122 | ce = mb_cache_entry_find_next(ea_block_cache, ce); |
| 3123 | } |
| 3124 | return NULL; |
| 3125 | } |
| 3126 | |
| 3127 | #define NAME_HASH_SHIFT 5 |
| 3128 | #define VALUE_HASH_SHIFT 16 |
| 3129 | |
| 3130 | /* |
| 3131 | * ext4_xattr_hash_entry() |
| 3132 | * |
| 3133 | * Compute the hash of an extended attribute. |
| 3134 | */ |
| 3135 | static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value, |
| 3136 | size_t value_count) |
| 3137 | { |
| 3138 | __u32 hash = 0; |
| 3139 | |
| 3140 | while (name_len--) { |
| 3141 | hash = (hash << NAME_HASH_SHIFT) ^ |
| 3142 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ |
| 3143 | (unsigned char)*name++; |
| 3144 | } |
| 3145 | while (value_count--) { |
| 3146 | hash = (hash << VALUE_HASH_SHIFT) ^ |
| 3147 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ |
| 3148 | le32_to_cpu(*value++); |
| 3149 | } |
| 3150 | return cpu_to_le32(hash); |
| 3151 | } |
| 3152 | |
| 3153 | /* |
| 3154 | * ext4_xattr_hash_entry_signed() |
| 3155 | * |
| 3156 | * Compute the hash of an extended attribute incorrectly. |
| 3157 | */ |
| 3158 | static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value, size_t value_count) |
| 3159 | { |
| 3160 | __u32 hash = 0; |
| 3161 | |
| 3162 | while (name_len--) { |
| 3163 | hash = (hash << NAME_HASH_SHIFT) ^ |
| 3164 | (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ |
| 3165 | (signed char)*name++; |
| 3166 | } |
| 3167 | while (value_count--) { |
| 3168 | hash = (hash << VALUE_HASH_SHIFT) ^ |
| 3169 | (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ |
| 3170 | le32_to_cpu(*value++); |
| 3171 | } |
| 3172 | return cpu_to_le32(hash); |
| 3173 | } |
| 3174 | |
| 3175 | #undef NAME_HASH_SHIFT |
| 3176 | #undef VALUE_HASH_SHIFT |
| 3177 | |
| 3178 | #define BLOCK_HASH_SHIFT 16 |
| 3179 | |
| 3180 | /* |
| 3181 | * ext4_xattr_rehash() |
| 3182 | * |
| 3183 | * Re-compute the extended attribute hash value after an entry has changed. |
| 3184 | */ |
| 3185 | static void ext4_xattr_rehash(struct ext4_xattr_header *header) |
| 3186 | { |
| 3187 | struct ext4_xattr_entry *here; |
| 3188 | __u32 hash = 0; |
| 3189 | |
| 3190 | here = ENTRY(header+1); |
| 3191 | while (!IS_LAST_ENTRY(here)) { |
| 3192 | if (!here->e_hash) { |
| 3193 | /* Block is not shared if an entry's hash value == 0 */ |
| 3194 | hash = 0; |
| 3195 | break; |
| 3196 | } |
| 3197 | hash = (hash << BLOCK_HASH_SHIFT) ^ |
| 3198 | (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ |
| 3199 | le32_to_cpu(here->e_hash); |
| 3200 | here = EXT4_XATTR_NEXT(here); |
| 3201 | } |
| 3202 | header->h_hash = cpu_to_le32(hash); |
| 3203 | } |
| 3204 | |
| 3205 | #undef BLOCK_HASH_SHIFT |
| 3206 | |
| 3207 | #define HASH_BUCKET_BITS 10 |
| 3208 | |
| 3209 | struct mb_cache * |
| 3210 | ext4_xattr_create_cache(void) |
| 3211 | { |
| 3212 | return mb_cache_create(HASH_BUCKET_BITS); |
| 3213 | } |
| 3214 | |
| 3215 | void ext4_xattr_destroy_cache(struct mb_cache *cache) |
| 3216 | { |
| 3217 | if (cache) |
| 3218 | mb_cache_destroy(cache); |
| 3219 | } |
| 3220 | |