PageRenderTime 93ms CodeModel.GetById 2ms app.highlight 78ms RepoModel.GetById 1ms app.codeStats 1ms

/fs/ext4/xattr.c

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