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