File Index
Files and directories
Sign in
FILE

xattr.c

File preview with syntax highlighting.

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