PageRenderTime 105ms CodeModel.GetById 3ms app.highlight 89ms RepoModel.GetById 2ms app.codeStats 0ms

/fs/ext4/xattr.c

https://bitbucket.org/digetx/picasso-kernel
C | 1715 lines | 1343 code | 150 blank | 222 comment | 299 complexity | 1e2ff725b643583baec61b1c91bb4d8f 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 it;
 520 * 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		unlock_buffer(bh);
 541		ext4_free_blocks(handle, inode, bh, 0, 1,
 542				 EXT4_FREE_BLOCKS_METADATA |
 543				 EXT4_FREE_BLOCKS_FORGET);
 544	} else {
 545		le32_add_cpu(&BHDR(bh)->h_refcount, -1);
 546		if (ce)
 547			mb_cache_entry_release(ce);
 548		/*
 549		 * Beware of this ugliness: Releasing of xattr block references
 550		 * from different inodes can race and so we have to protect
 551		 * from a race where someone else frees the block (and releases
 552		 * its journal_head) before we are done dirtying the buffer. In
 553		 * nojournal mode this race is harmless and we actually cannot
 554		 * call ext4_handle_dirty_xattr_block() with locked buffer as
 555		 * that function can call sync_dirty_buffer() so for that case
 556		 * we handle the dirtying after unlocking the buffer.
 557		 */
 558		if (ext4_handle_valid(handle))
 559			error = ext4_handle_dirty_xattr_block(handle, inode,
 560							      bh);
 561		unlock_buffer(bh);
 562		if (!ext4_handle_valid(handle))
 563			error = ext4_handle_dirty_xattr_block(handle, inode,
 564							      bh);
 565		if (IS_SYNC(inode))
 566			ext4_handle_sync(handle);
 567		dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
 568		ea_bdebug(bh, "refcount now=%d; releasing",
 569			  le32_to_cpu(BHDR(bh)->h_refcount));
 570	}
 571out:
 572	ext4_std_error(inode->i_sb, error);
 573	return;
 574}
 575
 576/*
 577 * Find the available free space for EAs. This also returns the total number of
 578 * bytes used by EA entries.
 579 */
 580static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
 581				    size_t *min_offs, void *base, int *total)
 582{
 583	for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
 584		*total += EXT4_XATTR_LEN(last->e_name_len);
 585		if (!last->e_value_block && last->e_value_size) {
 586			size_t offs = le16_to_cpu(last->e_value_offs);
 587			if (offs < *min_offs)
 588				*min_offs = offs;
 589		}
 590	}
 591	return (*min_offs - ((void *)last - base) - sizeof(__u32));
 592}
 593
 594static int
 595ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
 596{
 597	struct ext4_xattr_entry *last;
 598	size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
 599
 600	/* Compute min_offs and last. */
 601	last = s->first;
 602	for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
 603		if (!last->e_value_block && last->e_value_size) {
 604			size_t offs = le16_to_cpu(last->e_value_offs);
 605			if (offs < min_offs)
 606				min_offs = offs;
 607		}
 608	}
 609	free = min_offs - ((void *)last - s->base) - sizeof(__u32);
 610	if (!s->not_found) {
 611		if (!s->here->e_value_block && s->here->e_value_size) {
 612			size_t size = le32_to_cpu(s->here->e_value_size);
 613			free += EXT4_XATTR_SIZE(size);
 614		}
 615		free += EXT4_XATTR_LEN(name_len);
 616	}
 617	if (i->value) {
 618		if (free < EXT4_XATTR_SIZE(i->value_len) ||
 619		    free < EXT4_XATTR_LEN(name_len) +
 620			   EXT4_XATTR_SIZE(i->value_len))
 621			return -ENOSPC;
 622	}
 623
 624	if (i->value && s->not_found) {
 625		/* Insert the new name. */
 626		size_t size = EXT4_XATTR_LEN(name_len);
 627		size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
 628		memmove((void *)s->here + size, s->here, rest);
 629		memset(s->here, 0, size);
 630		s->here->e_name_index = i->name_index;
 631		s->here->e_name_len = name_len;
 632		memcpy(s->here->e_name, i->name, name_len);
 633	} else {
 634		if (!s->here->e_value_block && s->here->e_value_size) {
 635			void *first_val = s->base + min_offs;
 636			size_t offs = le16_to_cpu(s->here->e_value_offs);
 637			void *val = s->base + offs;
 638			size_t size = EXT4_XATTR_SIZE(
 639				le32_to_cpu(s->here->e_value_size));
 640
 641			if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
 642				/* The old and the new value have the same
 643				   size. Just replace. */
 644				s->here->e_value_size =
 645					cpu_to_le32(i->value_len);
 646				if (i->value == EXT4_ZERO_XATTR_VALUE) {
 647					memset(val, 0, size);
 648				} else {
 649					/* Clear pad bytes first. */
 650					memset(val + size - EXT4_XATTR_PAD, 0,
 651					       EXT4_XATTR_PAD);
 652					memcpy(val, i->value, i->value_len);
 653				}
 654				return 0;
 655			}
 656
 657			/* Remove the old value. */
 658			memmove(first_val + size, first_val, val - first_val);
 659			memset(first_val, 0, size);
 660			s->here->e_value_size = 0;
 661			s->here->e_value_offs = 0;
 662			min_offs += size;
 663
 664			/* Adjust all value offsets. */
 665			last = s->first;
 666			while (!IS_LAST_ENTRY(last)) {
 667				size_t o = le16_to_cpu(last->e_value_offs);
 668				if (!last->e_value_block &&
 669				    last->e_value_size && o < offs)
 670					last->e_value_offs =
 671						cpu_to_le16(o + size);
 672				last = EXT4_XATTR_NEXT(last);
 673			}
 674		}
 675		if (!i->value) {
 676			/* Remove the old name. */
 677			size_t size = EXT4_XATTR_LEN(name_len);
 678			last = ENTRY((void *)last - size);
 679			memmove(s->here, (void *)s->here + size,
 680				(void *)last - (void *)s->here + sizeof(__u32));
 681			memset(last, 0, size);
 682		}
 683	}
 684
 685	if (i->value) {
 686		/* Insert the new value. */
 687		s->here->e_value_size = cpu_to_le32(i->value_len);
 688		if (i->value_len) {
 689			size_t size = EXT4_XATTR_SIZE(i->value_len);
 690			void *val = s->base + min_offs - size;
 691			s->here->e_value_offs = cpu_to_le16(min_offs - size);
 692			if (i->value == EXT4_ZERO_XATTR_VALUE) {
 693				memset(val, 0, size);
 694			} else {
 695				/* Clear the pad bytes first. */
 696				memset(val + size - EXT4_XATTR_PAD, 0,
 697				       EXT4_XATTR_PAD);
 698				memcpy(val, i->value, i->value_len);
 699			}
 700		}
 701	}
 702	return 0;
 703}
 704
 705struct ext4_xattr_block_find {
 706	struct ext4_xattr_search s;
 707	struct buffer_head *bh;
 708};
 709
 710static int
 711ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
 712		      struct ext4_xattr_block_find *bs)
 713{
 714	struct super_block *sb = inode->i_sb;
 715	int error;
 716
 717	ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
 718		  i->name_index, i->name, i->value, (long)i->value_len);
 719
 720	if (EXT4_I(inode)->i_file_acl) {
 721		/* The inode already has an extended attribute block. */
 722		bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
 723		error = -EIO;
 724		if (!bs->bh)
 725			goto cleanup;
 726		ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
 727			atomic_read(&(bs->bh->b_count)),
 728			le32_to_cpu(BHDR(bs->bh)->h_refcount));
 729		if (ext4_xattr_check_block(inode, bs->bh)) {
 730			EXT4_ERROR_INODE(inode, "bad block %llu",
 731					 EXT4_I(inode)->i_file_acl);
 732			error = -EIO;
 733			goto cleanup;
 734		}
 735		/* Find the named attribute. */
 736		bs->s.base = BHDR(bs->bh);
 737		bs->s.first = BFIRST(bs->bh);
 738		bs->s.end = bs->bh->b_data + bs->bh->b_size;
 739		bs->s.here = bs->s.first;
 740		error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
 741					      i->name, bs->bh->b_size, 1);
 742		if (error && error != -ENODATA)
 743			goto cleanup;
 744		bs->s.not_found = error;
 745	}
 746	error = 0;
 747
 748cleanup:
 749	return error;
 750}
 751
 752static int
 753ext4_xattr_block_set(handle_t *handle, struct inode *inode,
 754		     struct ext4_xattr_info *i,
 755		     struct ext4_xattr_block_find *bs)
 756{
 757	struct super_block *sb = inode->i_sb;
 758	struct buffer_head *new_bh = NULL;
 759	struct ext4_xattr_search *s = &bs->s;
 760	struct mb_cache_entry *ce = NULL;
 761	int error = 0;
 762
 763#define header(x) ((struct ext4_xattr_header *)(x))
 764
 765	if (i->value && i->value_len > sb->s_blocksize)
 766		return -ENOSPC;
 767	if (s->base) {
 768		ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
 769					bs->bh->b_blocknr);
 770		error = ext4_journal_get_write_access(handle, bs->bh);
 771		if (error)
 772			goto cleanup;
 773		lock_buffer(bs->bh);
 774
 775		if (header(s->base)->h_refcount == cpu_to_le32(1)) {
 776			if (ce) {
 777				mb_cache_entry_free(ce);
 778				ce = NULL;
 779			}
 780			ea_bdebug(bs->bh, "modifying in-place");
 781			error = ext4_xattr_set_entry(i, s);
 782			if (!error) {
 783				if (!IS_LAST_ENTRY(s->first))
 784					ext4_xattr_rehash(header(s->base),
 785							  s->here);
 786				ext4_xattr_cache_insert(bs->bh);
 787			}
 788			unlock_buffer(bs->bh);
 789			if (error == -EIO)
 790				goto bad_block;
 791			if (!error)
 792				error = ext4_handle_dirty_xattr_block(handle,
 793								      inode,
 794								      bs->bh);
 795			if (error)
 796				goto cleanup;
 797			goto inserted;
 798		} else {
 799			int offset = (char *)s->here - bs->bh->b_data;
 800
 801			unlock_buffer(bs->bh);
 802			if (ce) {
 803				mb_cache_entry_release(ce);
 804				ce = NULL;
 805			}
 806			ea_bdebug(bs->bh, "cloning");
 807			s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
 808			error = -ENOMEM;
 809			if (s->base == NULL)
 810				goto cleanup;
 811			memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
 812			s->first = ENTRY(header(s->base)+1);
 813			header(s->base)->h_refcount = cpu_to_le32(1);
 814			s->here = ENTRY(s->base + offset);
 815			s->end = s->base + bs->bh->b_size;
 816		}
 817	} else {
 818		/* Allocate a buffer where we construct the new block. */
 819		s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
 820		/* assert(header == s->base) */
 821		error = -ENOMEM;
 822		if (s->base == NULL)
 823			goto cleanup;
 824		header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
 825		header(s->base)->h_blocks = cpu_to_le32(1);
 826		header(s->base)->h_refcount = cpu_to_le32(1);
 827		s->first = ENTRY(header(s->base)+1);
 828		s->here = ENTRY(header(s->base)+1);
 829		s->end = s->base + sb->s_blocksize;
 830	}
 831
 832	error = ext4_xattr_set_entry(i, s);
 833	if (error == -EIO)
 834		goto bad_block;
 835	if (error)
 836		goto cleanup;
 837	if (!IS_LAST_ENTRY(s->first))
 838		ext4_xattr_rehash(header(s->base), s->here);
 839
 840inserted:
 841	if (!IS_LAST_ENTRY(s->first)) {
 842		new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
 843		if (new_bh) {
 844			/* We found an identical block in the cache. */
 845			if (new_bh == bs->bh)
 846				ea_bdebug(new_bh, "keeping");
 847			else {
 848				/* The old block is released after updating
 849				   the inode. */
 850				error = dquot_alloc_block(inode,
 851						EXT4_C2B(EXT4_SB(sb), 1));
 852				if (error)
 853					goto cleanup;
 854				error = ext4_journal_get_write_access(handle,
 855								      new_bh);
 856				if (error)
 857					goto cleanup_dquot;
 858				lock_buffer(new_bh);
 859				le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
 860				ea_bdebug(new_bh, "reusing; refcount now=%d",
 861					le32_to_cpu(BHDR(new_bh)->h_refcount));
 862				unlock_buffer(new_bh);
 863				error = ext4_handle_dirty_xattr_block(handle,
 864								      inode,
 865								      new_bh);
 866				if (error)
 867					goto cleanup_dquot;
 868			}
 869			mb_cache_entry_release(ce);
 870			ce = NULL;
 871		} else if (bs->bh && s->base == bs->bh->b_data) {
 872			/* We were modifying this block in-place. */
 873			ea_bdebug(bs->bh, "keeping this block");
 874			new_bh = bs->bh;
 875			get_bh(new_bh);
 876		} else {
 877			/* We need to allocate a new block */
 878			ext4_fsblk_t goal, block;
 879
 880			goal = ext4_group_first_block_no(sb,
 881						EXT4_I(inode)->i_block_group);
 882
 883			/* non-extent files can't have physical blocks past 2^32 */
 884			if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
 885				goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
 886
 887			/*
 888			 * take i_data_sem because we will test
 889			 * i_delalloc_reserved_flag in ext4_mb_new_blocks
 890			 */
 891			down_read((&EXT4_I(inode)->i_data_sem));
 892			block = ext4_new_meta_blocks(handle, inode, goal, 0,
 893						     NULL, &error);
 894			up_read((&EXT4_I(inode)->i_data_sem));
 895			if (error)
 896				goto cleanup;
 897
 898			if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
 899				BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
 900
 901			ea_idebug(inode, "creating block %llu",
 902				  (unsigned long long)block);
 903
 904			new_bh = sb_getblk(sb, block);
 905			if (!new_bh) {
 906				error = -ENOMEM;
 907getblk_failed:
 908				ext4_free_blocks(handle, inode, NULL, block, 1,
 909						 EXT4_FREE_BLOCKS_METADATA);
 910				goto cleanup;
 911			}
 912			lock_buffer(new_bh);
 913			error = ext4_journal_get_create_access(handle, new_bh);
 914			if (error) {
 915				unlock_buffer(new_bh);
 916				error = -EIO;
 917				goto getblk_failed;
 918			}
 919			memcpy(new_bh->b_data, s->base, new_bh->b_size);
 920			set_buffer_uptodate(new_bh);
 921			unlock_buffer(new_bh);
 922			ext4_xattr_cache_insert(new_bh);
 923			error = ext4_handle_dirty_xattr_block(handle,
 924							      inode, new_bh);
 925			if (error)
 926				goto cleanup;
 927		}
 928	}
 929
 930	/* Update the inode. */
 931	EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
 932
 933	/* Drop the previous xattr block. */
 934	if (bs->bh && bs->bh != new_bh)
 935		ext4_xattr_release_block(handle, inode, bs->bh);
 936	error = 0;
 937
 938cleanup:
 939	if (ce)
 940		mb_cache_entry_release(ce);
 941	brelse(new_bh);
 942	if (!(bs->bh && s->base == bs->bh->b_data))
 943		kfree(s->base);
 944
 945	return error;
 946
 947cleanup_dquot:
 948	dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
 949	goto cleanup;
 950
 951bad_block:
 952	EXT4_ERROR_INODE(inode, "bad block %llu",
 953			 EXT4_I(inode)->i_file_acl);
 954	goto cleanup;
 955
 956#undef header
 957}
 958
 959int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
 960			  struct ext4_xattr_ibody_find *is)
 961{
 962	struct ext4_xattr_ibody_header *header;
 963	struct ext4_inode *raw_inode;
 964	int error;
 965
 966	if (EXT4_I(inode)->i_extra_isize == 0)
 967		return 0;
 968	raw_inode = ext4_raw_inode(&is->iloc);
 969	header = IHDR(inode, raw_inode);
 970	is->s.base = is->s.first = IFIRST(header);
 971	is->s.here = is->s.first;
 972	is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
 973	if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
 974		error = ext4_xattr_check_names(IFIRST(header), is->s.end);
 975		if (error)
 976			return error;
 977		/* Find the named attribute. */
 978		error = ext4_xattr_find_entry(&is->s.here, i->name_index,
 979					      i->name, is->s.end -
 980					      (void *)is->s.base, 0);
 981		if (error && error != -ENODATA)
 982			return error;
 983		is->s.not_found = error;
 984	}
 985	return 0;
 986}
 987
 988int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
 989				struct ext4_xattr_info *i,
 990				struct ext4_xattr_ibody_find *is)
 991{
 992	struct ext4_xattr_ibody_header *header;
 993	struct ext4_xattr_search *s = &is->s;
 994	int error;
 995
 996	if (EXT4_I(inode)->i_extra_isize == 0)
 997		return -ENOSPC;
 998	error = ext4_xattr_set_entry(i, s);
 999	if (error) {
1000		if (error == -ENOSPC &&
1001		    ext4_has_inline_data(inode)) {
1002			error = ext4_try_to_evict_inline_data(handle, inode,
1003					EXT4_XATTR_LEN(strlen(i->name) +
1004					EXT4_XATTR_SIZE(i->value_len)));
1005			if (error)
1006				return error;
1007			error = ext4_xattr_ibody_find(inode, i, is);
1008			if (error)
1009				return error;
1010			error = ext4_xattr_set_entry(i, s);
1011		}
1012		if (error)
1013			return error;
1014	}
1015	header = IHDR(inode, ext4_raw_inode(&is->iloc));
1016	if (!IS_LAST_ENTRY(s->first)) {
1017		header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1018		ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1019	} else {
1020		header->h_magic = cpu_to_le32(0);
1021		ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1022	}
1023	return 0;
1024}
1025
1026static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1027				struct ext4_xattr_info *i,
1028				struct ext4_xattr_ibody_find *is)
1029{
1030	struct ext4_xattr_ibody_header *header;
1031	struct ext4_xattr_search *s = &is->s;
1032	int error;
1033
1034	if (EXT4_I(inode)->i_extra_isize == 0)
1035		return -ENOSPC;
1036	error = ext4_xattr_set_entry(i, s);
1037	if (error)
1038		return error;
1039	header = IHDR(inode, ext4_raw_inode(&is->iloc));
1040	if (!IS_LAST_ENTRY(s->first)) {
1041		header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1042		ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1043	} else {
1044		header->h_magic = cpu_to_le32(0);
1045		ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1046	}
1047	return 0;
1048}
1049
1050/*
1051 * ext4_xattr_set_handle()
1052 *
1053 * Create, replace or remove an extended attribute for this inode.  Value
1054 * is NULL to remove an existing extended attribute, and non-NULL to
1055 * either replace an existing extended attribute, or create a new extended
1056 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1057 * specify that an extended attribute must exist and must not exist
1058 * previous to the call, respectively.
1059 *
1060 * Returns 0, or a negative error number on failure.
1061 */
1062int
1063ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
1064		      const char *name, const void *value, size_t value_len,
1065		      int flags)
1066{
1067	struct ext4_xattr_info i = {
1068		.name_index = name_index,
1069		.name = name,
1070		.value = value,
1071		.value_len = value_len,
1072
1073	};
1074	struct ext4_xattr_ibody_find is = {
1075		.s = { .not_found = -ENODATA, },
1076	};
1077	struct ext4_xattr_block_find bs = {
1078		.s = { .not_found = -ENODATA, },
1079	};
1080	unsigned long no_expand;
1081	int error;
1082
1083	if (!name)
1084		return -EINVAL;
1085	if (strlen(name) > 255)
1086		return -ERANGE;
1087	down_write(&EXT4_I(inode)->xattr_sem);
1088	no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1089	ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1090
1091	error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1092	if (error)
1093		goto cleanup;
1094
1095	if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1096		struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1097		memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1098		ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1099	}
1100
1101	error = ext4_xattr_ibody_find(inode, &i, &is);
1102	if (error)
1103		goto cleanup;
1104	if (is.s.not_found)
1105		error = ext4_xattr_block_find(inode, &i, &bs);
1106	if (error)
1107		goto cleanup;
1108	if (is.s.not_found && bs.s.not_found) {
1109		error = -ENODATA;
1110		if (flags & XATTR_REPLACE)
1111			goto cleanup;
1112		error = 0;
1113		if (!value)
1114			goto cleanup;
1115	} else {
1116		error = -EEXIST;
1117		if (flags & XATTR_CREATE)
1118			goto cleanup;
1119	}
1120	if (!value) {
1121		if (!is.s.not_found)
1122			error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1123		else if (!bs.s.not_found)
1124			error = ext4_xattr_block_set(handle, inode, &i, &bs);
1125	} else {
1126		error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1127		if (!error && !bs.s.not_found) {
1128			i.value = NULL;
1129			error = ext4_xattr_block_set(handle, inode, &i, &bs);
1130		} else if (error == -ENOSPC) {
1131			if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1132				error = ext4_xattr_block_find(inode, &i, &bs);
1133				if (error)
1134					goto cleanup;
1135			}
1136			error = ext4_xattr_block_set(handle, inode, &i, &bs);
1137			if (error)
1138				goto cleanup;
1139			if (!is.s.not_found) {
1140				i.value = NULL;
1141				error = ext4_xattr_ibody_set(handle, inode, &i,
1142							     &is);
1143			}
1144		}
1145	}
1146	if (!error) {
1147		ext4_xattr_update_super_block(handle, inode->i_sb);
1148		inode->i_ctime = ext4_current_time(inode);
1149		if (!value)
1150			ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1151		error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1152		/*
1153		 * The bh is consumed by ext4_mark_iloc_dirty, even with
1154		 * error != 0.
1155		 */
1156		is.iloc.bh = NULL;
1157		if (IS_SYNC(inode))
1158			ext4_handle_sync(handle);
1159	}
1160
1161cleanup:
1162	brelse(is.iloc.bh);
1163	brelse(bs.bh);
1164	if (no_expand == 0)
1165		ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1166	up_write(&EXT4_I(inode)->xattr_sem);
1167	return error;
1168}
1169
1170/*
1171 * ext4_xattr_set()
1172 *
1173 * Like ext4_xattr_set_handle, but start from an inode. This extended
1174 * attribute modification is a filesystem transaction by itself.
1175 *
1176 * Returns 0, or a negative error number on failure.
1177 */
1178int
1179ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1180	       const void *value, size_t value_len, int flags)
1181{
1182	handle_t *handle;
1183	int error, retries = 0;
1184	int credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb);
1185
1186retry:
1187	/*
1188	 * In case of inline data, we may push out the data to a block,
1189	 * So reserve the journal space first.
1190	 */
1191	if (ext4_has_inline_data(inode))
1192		credits += ext4_writepage_trans_blocks(inode) + 1;
1193
1194	handle = ext4_journal_start(inode, credits);
1195	if (IS_ERR(handle)) {
1196		error = PTR_ERR(handle);
1197	} else {
1198		int error2;
1199
1200		error = ext4_xattr_set_handle(handle, inode, name_index, name,
1201					      value, value_len, flags);
1202		error2 = ext4_journal_stop(handle);
1203		if (error == -ENOSPC &&
1204		    ext4_should_retry_alloc(inode->i_sb, &retries))
1205			goto retry;
1206		if (error == 0)
1207			error = error2;
1208	}
1209
1210	return error;
1211}
1212
1213/*
1214 * Shift the EA entries in the inode to create space for the increased
1215 * i_extra_isize.
1216 */
1217static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1218				     int value_offs_shift, void *to,
1219				     void *from, size_t n, int blocksize)
1220{
1221	struct ext4_xattr_entry *last = entry;
1222	int new_offs;
1223
1224	/* Adjust the value offsets of the entries */
1225	for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1226		if (!last->e_value_block && last->e_value_size) {
1227			new_offs = le16_to_cpu(last->e_value_offs) +
1228							value_offs_shift;
1229			BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1230				 > blocksize);
1231			last->e_value_offs = cpu_to_le16(new_offs);
1232		}
1233	}
1234	/* Shift the entries by n bytes */
1235	memmove(to, from, n);
1236}
1237
1238/*
1239 * Expand an inode by new_extra_isize bytes when EAs are present.
1240 * Returns 0 on success or negative error number on failure.
1241 */
1242int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1243			       struct ext4_inode *raw_inode, handle_t *handle)
1244{
1245	struct ext4_xattr_ibody_header *header;
1246	struct ext4_xattr_entry *entry, *last, *first;
1247	struct buffer_head *bh = NULL;
1248	struct ext4_xattr_ibody_find *is = NULL;
1249	struct ext4_xattr_block_find *bs = NULL;
1250	char *buffer = NULL, *b_entry_name = NULL;
1251	size_t min_offs, free;
1252	int total_ino, total_blk;
1253	void *base, *start, *end;
1254	int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
1255	int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1256
1257	down_write(&EXT4_I(inode)->xattr_sem);
1258retry:
1259	if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1260		up_write(&EXT4_I(inode)->xattr_sem);
1261		return 0;
1262	}
1263
1264	header = IHDR(inode, raw_inode);
1265	entry = IFIRST(header);
1266
1267	/*
1268	 * Check if enough free space is available in the inode to shift the
1269	 * entries ahead by new_extra_isize.
1270	 */
1271
1272	base = start = entry;
1273	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1274	min_offs = end - base;
1275	last = entry;
1276	total_ino = sizeof(struct ext4_xattr_ibody_header);
1277
1278	free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1279	if (free >= new_extra_isize) {
1280		entry = IFIRST(header);
1281		ext4_xattr_shift_entries(entry,	EXT4_I(inode)->i_extra_isize
1282				- new_extra_isize, (void *)raw_inode +
1283				EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1284				(void *)header, total_ino,
1285				inode->i_sb->s_blocksize);
1286		EXT4_I(inode)->i_extra_isize = new_extra_isize;
1287		error = 0;
1288		goto cleanup;
1289	}
1290
1291	/*
1292	 * Enough free space isn't available in the inode, check if
1293	 * EA block can hold new_extra_isize bytes.
1294	 */
1295	if (EXT4_I(inode)->i_file_acl) {
1296		bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1297		error = -EIO;
1298		if (!bh)
1299			goto cleanup;
1300		if (ext4_xattr_check_block(inode, bh)) {
1301			EXT4_ERROR_INODE(inode, "bad block %llu",
1302					 EXT4_I(inode)->i_file_acl);
1303			error = -EIO;
1304			goto cleanup;
1305		}
1306		base = BHDR(bh);
1307		first = BFIRST(bh);
1308		end = bh->b_data + bh->b_size;
1309		min_offs = end - base;
1310		free = ext4_xattr_free_space(first, &min_offs, base,
1311					     &total_blk);
1312		if (free < new_extra_isize) {
1313			if (!tried_min_extra_isize && s_min_extra_isize) {
1314				tried_min_extra_isize++;
1315				new_extra_isize = s_min_extra_isize;
1316				brelse(bh);
1317				goto retry;
1318			}
1319			error = -1;
1320			goto cleanup;
1321		}
1322	} else {
1323		free = inode->i_sb->s_blocksize;
1324	}
1325
1326	while (new_extra_isize > 0) {
1327		size_t offs, size, entry_size;
1328		struct ext4_xattr_entry *small_entry = NULL;
1329		struct ext4_xattr_info i = {
1330			.value = NULL,
1331			.value_len = 0,
1332		};
1333		unsigned int total_size;  /* EA entry size + value size */
1334		unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1335		unsigned int min_total_size = ~0U;
1336
1337		is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1338		bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1339		if (!is || !bs) {
1340			error = -ENOMEM;
1341			goto cleanup;
1342		}
1343
1344		is->s.not_found = -ENODATA;
1345		bs->s.not_found = -ENODATA;
1346		is->iloc.bh = NULL;
1347		bs->bh = NULL;
1348
1349		last = IFIRST(header);
1350		/* Find the entry best suited to be pushed into EA block */
1351		entry = NULL;
1352		for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1353			total_size =
1354			EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1355					EXT4_XATTR_LEN(last->e_name_len);
1356			if (total_size <= free && total_size < min_total_size) {
1357				if (total_size < new_extra_isize) {
1358					small_entry = last;
1359				} else {
1360					entry = last;
1361					min_total_size = total_size;
1362				}
1363			}
1364		}
1365
1366		if (entry == NULL) {
1367			if (small_entry) {
1368				entry = small_entry;
1369			} else {
1370				if (!tried_min_extra_isize &&
1371				    s_min_extra_isize) {
1372					tried_min_extra_isize++;
1373					new_extra_isize = s_min_extra_isize;
1374					kfree(is); is = NULL;
1375					kfree(bs); bs = NULL;
1376					brelse(bh);
1377					goto retry;
1378				}
1379				error = -1;
1380				goto cleanup;
1381			}
1382		}
1383		offs = le16_to_cpu(entry->e_value_offs);
1384		size = le32_to_cpu(entry->e_value_size);
1385		entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1386		i.name_index = entry->e_name_index,
1387		buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1388		b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1389		if (!buffer || !b_entry_name) {
1390			error = -ENOMEM;
1391			goto cleanup;
1392		}
1393		/* Save the entry name and the entry value */
1394		memcpy(buffer, (void *)IFIRST(header) + offs,
1395		       EXT4_XATTR_SIZE(size));
1396		memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1397		b_entry_name[entry->e_name_len] = '\0';
1398		i.name = b_entry_name;
1399
1400		error = ext4_get_inode_loc(inode, &is->iloc);
1401		if (error)
1402			goto cleanup;
1403
1404		error = ext4_xattr_ibody_find(inode, &i, is);
1405		if (error)
1406			goto cleanup;
1407
1408		/* Remove the chosen entry from the inode */
1409		error = ext4_xattr_ibody_set(handle, inode, &i, is);
1410		if (error)
1411			goto cleanup;
1412
1413		entry = IFIRST(header);
1414		if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1415			shift_bytes = new_extra_isize;
1416		else
1417			shift_bytes = entry_size + size;
1418		/* Adjust the offsets and shift the remaining entries ahead */
1419		ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1420			shift_bytes, (void *)raw_inode +
1421			EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1422			(void *)header, total_ino - entry_size,
1423			inode->i_sb->s_blocksize);
1424
1425		extra_isize += shift_bytes;
1426		new_extra_isize -= shift_bytes;
1427		EXT4_I(inode)->i_extra_isize = extra_isize;
1428
1429		i.name = b_entry_name;
1430		i.value = buffer;
1431		i.value_len = size;
1432		error = ext4_xattr_block_find(inode, &i, bs);
1433		if (error)
1434			goto cleanup;
1435
1436		/* Add entry which was removed from the inode into the block */
1437		error = ext4_xattr_block_set(handle, inode, &i, bs);
1438		if (error)
1439			goto cleanup;
1440		kfree(b_entry_name);
1441		kfree(buffer);
1442		b_entry_name = NULL;
1443		buffer = NULL;
1444		brelse(is->iloc.bh);
1445		kfree(is);
1446		kfree(bs);
1447	}
1448	brelse(bh);
1449	up_write(&EXT4_I(inode)->xattr_sem);
1450	return 0;
1451
1452cleanup:
1453	kfree(b_entry_name);
1454	kfree(buffer);
1455	if (is)
1456		brelse(is->iloc.bh);
1457	kfree(is);
1458	kfree(bs);
1459	brelse(bh);
1460	up_write(&EXT4_I(inode)->xattr_sem);
1461	return error;
1462}
1463
1464
1465
1466/*
1467 * ext4_xattr_delete_inode()
1468 *
1469 * Free extended attribute resources associated with this inode. This
1470 * is called immediately before an inode is freed. We have exclusive
1471 * access to the inode.
1472 */
1473void
1474ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1475{
1476	struct buffer_head *bh = NULL;
1477
1478	if (!EXT4_I(inode)->i_file_acl)
1479		goto cleanup;
1480	bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1481	if (!bh) {
1482		EXT4_ERROR_INODE(inode, "block %llu read error",
1483				 EXT4_I(inode)->i_file_acl);
1484		goto cleanup;
1485	}
1486	if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1487	    BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1488		EXT4_ERROR_INODE(inode, "bad block %llu",
1489				 EXT4_I(inode)->i_file_acl);
1490		goto cleanup;
1491	}
1492	ext4_xattr_release_block(handle, inode, bh);
1493	EXT4_I(inode)->i_file_acl = 0;
1494
1495cleanup:
1496	brelse(bh);
1497}
1498
1499/*
1500 * ext4_xattr_put_super()
1501 *
1502 * This is called when a file system is unmounted.
1503 */
1504void
1505ext4_xattr_put_super(struct super_block *sb)
1506{
1507	mb_cache_shrink(sb->s_bdev);
1508}
1509
1510/*
1511 * ext4_xattr_cache_insert()
1512 *
1513 * Create a new entry in the extended attribute cache, and insert
1514 * it unless such an entry is already in the cache.
1515 *
1516 * Returns 0, or a negative error number on failure.
1517 */
1518static void
1519ext4_xattr_cache_insert(struct buffer_head *bh)
1520{
1521	__u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1522	struct mb_cache_entry *ce;
1523	int error;
1524
1525	ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
1526	if (!ce) {
1527		ea_bdebug(bh, "out of memory");
1528		return;
1529	}
1530	error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1531	if (error) {
1532		mb_cache_entry_free(ce);
1533		if (error == -EBUSY) {
1534			ea_bdebug(bh, "already in cache");
1535			error = 0;
1536		}
1537	} else {
1538		ea_bdebug(bh, "inserting [%x]", (int)hash);
1539		mb_cache_entry_release(ce);
1540	}
1541}
1542
1543/*
1544 * ext4_xattr_cmp()
1545 *
1546 * Compare two extended attribute blocks for equality.
1547 *
1548 * Returns 0 if the blocks are equal, 1 if they differ, and
1549 * a negative error number on errors.
1550 */
1551static int
1552ext4_xattr_cmp(struct ext4_xattr_header *header1,
1553	       struct ext4_xattr_header *header2)
1554{
1555	struct ext4_xattr_entry *entry1, *entry2;
1556
1557	entry1 = ENTRY(header1+1);
1558	entry2 = ENTRY(header2+1);
1559	while (!IS_LAST_ENTRY(entry1)) {
1560		if (IS_LAST_ENTRY(entry2))
1561			return 1;
1562		if (entry1->e_hash != entry2->e_hash ||
1563		    entry1->e_name_index != entry2->e_name_index ||
1564		    entry1->e_name_len != entry2->e_name_len ||
1565		    entry1->e_value_size != entry2->e_value_size ||
1566		    memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1567			return 1;
1568		if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1569			return -EIO;
1570		if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1571			   (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1572			   le32_to_cpu(entry1->e_value_size)))
1573			return 1;
1574
1575		entry1 = EXT4_XATTR_NEXT(entry1);
1576		entry2 = EXT4_XATTR_NEXT(entry2);
1577	}
1578	if (!IS_LAST_ENTRY(entry2))
1579		return 1;
1580	return 0;
1581}
1582
1583/*
1584 * ext4_xattr_cache_find()
1585 *
1586 * Find an identical extended attribute block.
1587 *
1588 * Returns a pointer to the block found, or NULL if such a block was
1589 * not found or an error occurred.
1590 */
1591static struct buffer_head *
1592ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1593		      struct mb_cache_entry **pce)
1594{
1595	__u32 hash = le32_to_cpu(header->h_hash);
1596	struct mb_cache_entry *ce;
1597
1598	if (!header->h_hash)
1599		return NULL;  /* never share */
1600	ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1601again:
1602	ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
1603				       hash);
1604	while (ce) {
1605		struct buffer_head *bh;
1606
1607		if (IS_ERR(ce)) {
1608			if (PTR_ERR(ce) == -EAGAIN)
1609				goto again;
1610			break;
1611		}
1612		bh = sb_bread(inode->i_sb, ce->e_block);
1613		if (!bh) {
1614			EXT4_ERROR_INODE(inode, "block %lu read error",
1615					 (unsigned long) ce->e_block);
1616		} else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1617				EXT4_XATTR_REFCOUNT_MAX) {
1618			ea_idebug(inode, "block %lu refcount %d>=%d",
1619				  (unsigned long) ce->e_block,
1620				  le32_to_cpu(BHDR(bh)->h_refcount),
1621					  EXT4_XATTR_REFCOUNT_MAX);
1622		} else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1623			*pce = ce;
1624			return bh;
1625		}
1626		brelse(bh);
1627		ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1628	}
1629	return NULL;
1630}
1631
1632#define NAME_HASH_SHIFT 5
1633#define VALUE_HASH_SHIFT 16
1634
1635/*
1636 * ext4_xattr_hash_entry()
1637 *
1638 * Compute the hash of an extended attribute.
1639 */
1640static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1641					 struct ext4_xattr_entry *entry)
1642{
1643	__u32 hash = 0;
1644	char *name = entry->e_name;
1645	int n;
1646
1647	for (n = 0; n < entry->e_name_len; n++) {
1648		hash = (hash << NAME_HASH_SHIFT) ^
1649		       (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1650		       *name++;
1651	}
1652
1653	if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1654		__le32 *value = (__le32 *)((char *)header +
1655			le16_to_cpu(entry->e_value_offs));
1656		for (n = (le32_to_cpu(entry->e_value_size) +
1657		     EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1658			hash = (hash << VALUE_HASH_SHIFT) ^
1659			       (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1660			       le32_to_cpu(*value++);
1661		}
1662	}
1663	entry->e_hash = cpu_to_le32(hash);
1664}
1665
1666#undef NAME_HASH_SHIFT
1667#undef VALUE_HASH_SHIFT
1668
1669#define BLOCK_HASH_SHIFT 16
1670
1671/*
1672 * ext4_xattr_rehash()
1673 *
1674 * Re-compute the extended attribute hash value after an entry has changed.
1675 */
1676static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1677			      struct ext4_xattr_entry *entry)
1678{
1679	struct ext4_xattr_entry *here;
1680	__u32 hash = 0;
1681
1682	ext4_xattr_hash_entry(header, entry);
1683	here = ENTRY(header+1);
1684	while (!IS_LAST_ENTRY(here)) {
1685		if (!here->e_hash) {
1686			/* Block is not shared if an entry's hash value == 0 */
1687			hash = 0;
1688			break;
1689		}
1690		hash = (hash << BLOCK_HASH_SHIFT) ^
1691		       (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1692		       le32_to_cpu(here->e_hash);
1693		here = EXT4_XATTR_NEXT(here);
1694	}
1695	header->h_hash = cpu_to_le32(hash);
1696}
1697
1698#undef BLOCK_HASH_SHIFT
1699
1700int __init
1701ext4_init_xattr(void)
1702{
1703	ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
1704	if (!ext4_xattr_cache)
1705		return -ENOMEM;
1706	return 0;
1707}
1708
1709void
1710ext4_exit_xattr(void)
1711{
1712	if (ext4_xattr_cache)
1713		mb_cache_destroy(ext4_xattr_cache);
1714	ext4_xattr_cache = NULL;
1715}