PageRenderTime 5ms CodeModel.GetById 7ms app.highlight 145ms RepoModel.GetById 1ms app.codeStats 0ms

/linux-2.6.21.x/fs/ext4/xattr.c

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