Linux Audio

Check our new training course

Loading...
v3.5.6
   1/*
   2 * linux/fs/ext3/xattr.c
   3 *
   4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
   5 *
   6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
   7 * Ext3 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 * EXT3_I(inode)->i_file_acl is protected by EXT3_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 "ext3.h"
 
 
 
 
  54#include <linux/mbcache.h>
  55#include <linux/quotaops.h>
 
  56#include "xattr.h"
  57#include "acl.h"
  58
  59#define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
  60#define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
  61#define BFIRST(bh) ENTRY(BHDR(bh)+1)
  62#define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
  63
  64#define IHDR(inode, raw_inode) \
  65	((struct ext3_xattr_ibody_header *) \
  66		((void *)raw_inode + \
  67		 EXT3_GOOD_OLD_INODE_SIZE + \
  68		 EXT3_I(inode)->i_extra_isize))
  69#define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
  70
  71#ifdef EXT3_XATTR_DEBUG
  72# define ea_idebug(inode, f...) do { \
  73		printk(KERN_DEBUG "inode %s:%lu: ", \
  74			inode->i_sb->s_id, inode->i_ino); \
  75		printk(f); \
  76		printk("\n"); \
  77	} while (0)
  78# define ea_bdebug(bh, f...) do { \
  79		char b[BDEVNAME_SIZE]; \
  80		printk(KERN_DEBUG "block %s:%lu: ", \
  81			bdevname(bh->b_bdev, b), \
  82			(unsigned long) bh->b_blocknr); \
  83		printk(f); \
  84		printk("\n"); \
  85	} while (0)
  86#else
  87# define ea_idebug(f...)
  88# define ea_bdebug(f...)
  89#endif
  90
  91static void ext3_xattr_cache_insert(struct buffer_head *);
  92static struct buffer_head *ext3_xattr_cache_find(struct inode *,
  93						 struct ext3_xattr_header *,
  94						 struct mb_cache_entry **);
  95static void ext3_xattr_rehash(struct ext3_xattr_header *,
  96			      struct ext3_xattr_entry *);
  97static int ext3_xattr_list(struct dentry *dentry, char *buffer,
  98			   size_t buffer_size);
  99
 100static struct mb_cache *ext3_xattr_cache;
 101
 102static const struct xattr_handler *ext3_xattr_handler_map[] = {
 103	[EXT3_XATTR_INDEX_USER]		     = &ext3_xattr_user_handler,
 104#ifdef CONFIG_EXT3_FS_POSIX_ACL
 105	[EXT3_XATTR_INDEX_POSIX_ACL_ACCESS]  = &ext3_xattr_acl_access_handler,
 106	[EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_handler,
 107#endif
 108	[EXT3_XATTR_INDEX_TRUSTED]	     = &ext3_xattr_trusted_handler,
 109#ifdef CONFIG_EXT3_FS_SECURITY
 110	[EXT3_XATTR_INDEX_SECURITY]	     = &ext3_xattr_security_handler,
 111#endif
 112};
 113
 114const struct xattr_handler *ext3_xattr_handlers[] = {
 115	&ext3_xattr_user_handler,
 116	&ext3_xattr_trusted_handler,
 117#ifdef CONFIG_EXT3_FS_POSIX_ACL
 118	&ext3_xattr_acl_access_handler,
 119	&ext3_xattr_acl_default_handler,
 120#endif
 121#ifdef CONFIG_EXT3_FS_SECURITY
 122	&ext3_xattr_security_handler,
 123#endif
 124	NULL
 125};
 126
 127static inline const struct xattr_handler *
 128ext3_xattr_handler(int name_index)
 129{
 130	const struct xattr_handler *handler = NULL;
 131
 132	if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
 133		handler = ext3_xattr_handler_map[name_index];
 134	return handler;
 135}
 136
 137/*
 138 * Inode operation listxattr()
 139 *
 140 * dentry->d_inode->i_mutex: don't care
 141 */
 142ssize_t
 143ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
 144{
 145	return ext3_xattr_list(dentry, buffer, size);
 146}
 147
 148static int
 149ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
 150{
 151	while (!IS_LAST_ENTRY(entry)) {
 152		struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
 153		if ((void *)next >= end)
 154			return -EIO;
 155		entry = next;
 156	}
 157	return 0;
 158}
 159
 160static inline int
 161ext3_xattr_check_block(struct buffer_head *bh)
 162{
 163	int error;
 164
 165	if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
 166	    BHDR(bh)->h_blocks != cpu_to_le32(1))
 167		return -EIO;
 168	error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
 169	return error;
 170}
 171
 172static inline int
 173ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
 174{
 175	size_t value_size = le32_to_cpu(entry->e_value_size);
 176
 177	if (entry->e_value_block != 0 || value_size > size ||
 178	    le16_to_cpu(entry->e_value_offs) + value_size > size)
 179		return -EIO;
 180	return 0;
 181}
 182
 183static int
 184ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
 185		      const char *name, size_t size, int sorted)
 186{
 187	struct ext3_xattr_entry *entry;
 188	size_t name_len;
 189	int cmp = 1;
 190
 191	if (name == NULL)
 192		return -EINVAL;
 193	name_len = strlen(name);
 194	entry = *pentry;
 195	for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
 196		cmp = name_index - entry->e_name_index;
 197		if (!cmp)
 198			cmp = name_len - entry->e_name_len;
 199		if (!cmp)
 200			cmp = memcmp(name, entry->e_name, name_len);
 201		if (cmp <= 0 && (sorted || cmp == 0))
 202			break;
 203	}
 204	*pentry = entry;
 205	if (!cmp && ext3_xattr_check_entry(entry, size))
 206			return -EIO;
 207	return cmp ? -ENODATA : 0;
 208}
 209
 210static int
 211ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
 212		     void *buffer, size_t buffer_size)
 213{
 214	struct buffer_head *bh = NULL;
 215	struct ext3_xattr_entry *entry;
 216	size_t size;
 217	int error;
 218
 219	ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
 220		  name_index, name, buffer, (long)buffer_size);
 221
 222	error = -ENODATA;
 223	if (!EXT3_I(inode)->i_file_acl)
 224		goto cleanup;
 225	ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
 226	bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
 227	if (!bh)
 228		goto cleanup;
 229	ea_bdebug(bh, "b_count=%d, refcount=%d",
 230		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
 231	if (ext3_xattr_check_block(bh)) {
 232bad_block:	ext3_error(inode->i_sb, __func__,
 233			   "inode %lu: bad block "E3FSBLK, inode->i_ino,
 234			   EXT3_I(inode)->i_file_acl);
 235		error = -EIO;
 236		goto cleanup;
 237	}
 238	ext3_xattr_cache_insert(bh);
 239	entry = BFIRST(bh);
 240	error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
 241	if (error == -EIO)
 242		goto bad_block;
 243	if (error)
 244		goto cleanup;
 245	size = le32_to_cpu(entry->e_value_size);
 246	if (buffer) {
 247		error = -ERANGE;
 248		if (size > buffer_size)
 249			goto cleanup;
 250		memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
 251		       size);
 252	}
 253	error = size;
 254
 255cleanup:
 256	brelse(bh);
 257	return error;
 258}
 259
 260static int
 261ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
 262		     void *buffer, size_t buffer_size)
 263{
 264	struct ext3_xattr_ibody_header *header;
 265	struct ext3_xattr_entry *entry;
 266	struct ext3_inode *raw_inode;
 267	struct ext3_iloc iloc;
 268	size_t size;
 269	void *end;
 270	int error;
 271
 272	if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
 273		return -ENODATA;
 274	error = ext3_get_inode_loc(inode, &iloc);
 275	if (error)
 276		return error;
 277	raw_inode = ext3_raw_inode(&iloc);
 278	header = IHDR(inode, raw_inode);
 279	entry = IFIRST(header);
 280	end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
 281	error = ext3_xattr_check_names(entry, end);
 282	if (error)
 283		goto cleanup;
 284	error = ext3_xattr_find_entry(&entry, name_index, name,
 285				      end - (void *)entry, 0);
 286	if (error)
 287		goto cleanup;
 288	size = le32_to_cpu(entry->e_value_size);
 289	if (buffer) {
 290		error = -ERANGE;
 291		if (size > buffer_size)
 292			goto cleanup;
 293		memcpy(buffer, (void *)IFIRST(header) +
 294		       le16_to_cpu(entry->e_value_offs), size);
 295	}
 296	error = size;
 297
 298cleanup:
 299	brelse(iloc.bh);
 300	return error;
 301}
 302
 303/*
 304 * ext3_xattr_get()
 305 *
 306 * Copy an extended attribute into the buffer
 307 * provided, or compute the buffer size required.
 308 * Buffer is NULL to compute the size of the buffer required.
 309 *
 310 * Returns a negative error number on failure, or the number of bytes
 311 * used / required on success.
 312 */
 313int
 314ext3_xattr_get(struct inode *inode, int name_index, const char *name,
 315	       void *buffer, size_t buffer_size)
 316{
 317	int error;
 318
 319	down_read(&EXT3_I(inode)->xattr_sem);
 320	error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
 321				     buffer_size);
 322	if (error == -ENODATA)
 323		error = ext3_xattr_block_get(inode, name_index, name, buffer,
 324					     buffer_size);
 325	up_read(&EXT3_I(inode)->xattr_sem);
 326	return error;
 327}
 328
 329static int
 330ext3_xattr_list_entries(struct dentry *dentry, struct ext3_xattr_entry *entry,
 331			char *buffer, size_t buffer_size)
 332{
 333	size_t rest = buffer_size;
 334
 335	for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
 336		const struct xattr_handler *handler =
 337			ext3_xattr_handler(entry->e_name_index);
 338
 339		if (handler) {
 340			size_t size = handler->list(dentry, buffer, rest,
 341						    entry->e_name,
 342						    entry->e_name_len,
 343						    handler->flags);
 344			if (buffer) {
 345				if (size > rest)
 346					return -ERANGE;
 347				buffer += size;
 348			}
 349			rest -= size;
 350		}
 351	}
 352	return buffer_size - rest;
 353}
 354
 355static int
 356ext3_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
 357{
 358	struct inode *inode = dentry->d_inode;
 359	struct buffer_head *bh = NULL;
 360	int error;
 361
 362	ea_idebug(inode, "buffer=%p, buffer_size=%ld",
 363		  buffer, (long)buffer_size);
 364
 365	error = 0;
 366	if (!EXT3_I(inode)->i_file_acl)
 367		goto cleanup;
 368	ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
 369	bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
 370	error = -EIO;
 371	if (!bh)
 372		goto cleanup;
 373	ea_bdebug(bh, "b_count=%d, refcount=%d",
 374		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
 375	if (ext3_xattr_check_block(bh)) {
 376		ext3_error(inode->i_sb, __func__,
 377			   "inode %lu: bad block "E3FSBLK, inode->i_ino,
 378			   EXT3_I(inode)->i_file_acl);
 379		error = -EIO;
 380		goto cleanup;
 381	}
 382	ext3_xattr_cache_insert(bh);
 383	error = ext3_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
 384
 385cleanup:
 386	brelse(bh);
 387
 388	return error;
 389}
 390
 391static int
 392ext3_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
 393{
 394	struct inode *inode = dentry->d_inode;
 395	struct ext3_xattr_ibody_header *header;
 396	struct ext3_inode *raw_inode;
 397	struct ext3_iloc iloc;
 398	void *end;
 399	int error;
 400
 401	if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
 402		return 0;
 403	error = ext3_get_inode_loc(inode, &iloc);
 404	if (error)
 405		return error;
 406	raw_inode = ext3_raw_inode(&iloc);
 407	header = IHDR(inode, raw_inode);
 408	end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
 409	error = ext3_xattr_check_names(IFIRST(header), end);
 410	if (error)
 411		goto cleanup;
 412	error = ext3_xattr_list_entries(dentry, IFIRST(header),
 413					buffer, buffer_size);
 414
 415cleanup:
 416	brelse(iloc.bh);
 417	return error;
 418}
 419
 420/*
 421 * ext3_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 */
 430static int
 431ext3_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
 432{
 433	int i_error, b_error;
 434
 435	down_read(&EXT3_I(dentry->d_inode)->xattr_sem);
 436	i_error = ext3_xattr_ibody_list(dentry, 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 = ext3_xattr_block_list(dentry, buffer, buffer_size);
 445		if (b_error < 0)
 446			i_error = 0;
 447	}
 448	up_read(&EXT3_I(dentry->d_inode)->xattr_sem);
 449	return i_error + b_error;
 450}
 451
 452/*
 453 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
 454 * not set, set it.
 455 */
 456static void ext3_xattr_update_super_block(handle_t *handle,
 457					  struct super_block *sb)
 458{
 459	if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
 460		return;
 461
 462	if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
 463		EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR);
 464		ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
 465	}
 466}
 467
 468/*
 469 * Release the xattr block BH: If the reference count is > 1, decrement
 470 * it; otherwise free the block.
 471 */
 472static void
 473ext3_xattr_release_block(handle_t *handle, struct inode *inode,
 474			 struct buffer_head *bh)
 475{
 476	struct mb_cache_entry *ce = NULL;
 477	int error = 0;
 478
 479	ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
 480	error = ext3_journal_get_write_access(handle, bh);
 481	if (error)
 482		 goto out;
 483
 484	lock_buffer(bh);
 485
 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		ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
 491		get_bh(bh);
 492		ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
 493	} else {
 494		le32_add_cpu(&BHDR(bh)->h_refcount, -1);
 495		error = ext3_journal_dirty_metadata(handle, bh);
 496		if (IS_SYNC(inode))
 497			handle->h_sync = 1;
 498		dquot_free_block(inode, 1);
 499		ea_bdebug(bh, "refcount now=%d; releasing",
 500			  le32_to_cpu(BHDR(bh)->h_refcount));
 501		if (ce)
 502			mb_cache_entry_release(ce);
 503	}
 504	unlock_buffer(bh);
 505out:
 506	ext3_std_error(inode->i_sb, error);
 507	return;
 508}
 509
 510struct ext3_xattr_info {
 511	int name_index;
 512	const char *name;
 513	const void *value;
 514	size_t value_len;
 515};
 516
 517struct ext3_xattr_search {
 518	struct ext3_xattr_entry *first;
 519	void *base;
 520	void *end;
 521	struct ext3_xattr_entry *here;
 522	int not_found;
 523};
 524
 525static int
 526ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
 527{
 528	struct ext3_xattr_entry *last;
 529	size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
 530
 531	/* Compute min_offs and last. */
 532	last = s->first;
 533	for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
 534		if (!last->e_value_block && last->e_value_size) {
 535			size_t offs = le16_to_cpu(last->e_value_offs);
 536			if (offs < min_offs)
 537				min_offs = offs;
 538		}
 539	}
 540	free = min_offs - ((void *)last - s->base) - sizeof(__u32);
 541	if (!s->not_found) {
 542		if (!s->here->e_value_block && s->here->e_value_size) {
 543			size_t size = le32_to_cpu(s->here->e_value_size);
 544			free += EXT3_XATTR_SIZE(size);
 545		}
 546		free += EXT3_XATTR_LEN(name_len);
 547	}
 548	if (i->value) {
 549		if (free < EXT3_XATTR_SIZE(i->value_len) ||
 550		    free < EXT3_XATTR_LEN(name_len) +
 551			   EXT3_XATTR_SIZE(i->value_len))
 552			return -ENOSPC;
 553	}
 554
 555	if (i->value && s->not_found) {
 556		/* Insert the new name. */
 557		size_t size = EXT3_XATTR_LEN(name_len);
 558		size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
 559		memmove((void *)s->here + size, s->here, rest);
 560		memset(s->here, 0, size);
 561		s->here->e_name_index = i->name_index;
 562		s->here->e_name_len = name_len;
 563		memcpy(s->here->e_name, i->name, name_len);
 564	} else {
 565		if (!s->here->e_value_block && s->here->e_value_size) {
 566			void *first_val = s->base + min_offs;
 567			size_t offs = le16_to_cpu(s->here->e_value_offs);
 568			void *val = s->base + offs;
 569			size_t size = EXT3_XATTR_SIZE(
 570				le32_to_cpu(s->here->e_value_size));
 571
 572			if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
 573				/* The old and the new value have the same
 574				   size. Just replace. */
 575				s->here->e_value_size =
 576					cpu_to_le32(i->value_len);
 577				memset(val + size - EXT3_XATTR_PAD, 0,
 578				       EXT3_XATTR_PAD); /* Clear pad bytes. */
 579				memcpy(val, i->value, i->value_len);
 580				return 0;
 581			}
 582
 583			/* Remove the old value. */
 584			memmove(first_val + size, first_val, val - first_val);
 585			memset(first_val, 0, size);
 586			s->here->e_value_size = 0;
 587			s->here->e_value_offs = 0;
 588			min_offs += size;
 589
 590			/* Adjust all value offsets. */
 591			last = s->first;
 592			while (!IS_LAST_ENTRY(last)) {
 593				size_t o = le16_to_cpu(last->e_value_offs);
 594				if (!last->e_value_block &&
 595				    last->e_value_size && o < offs)
 596					last->e_value_offs =
 597						cpu_to_le16(o + size);
 598				last = EXT3_XATTR_NEXT(last);
 599			}
 600		}
 601		if (!i->value) {
 602			/* Remove the old name. */
 603			size_t size = EXT3_XATTR_LEN(name_len);
 604			last = ENTRY((void *)last - size);
 605			memmove(s->here, (void *)s->here + size,
 606				(void *)last - (void *)s->here + sizeof(__u32));
 607			memset(last, 0, size);
 608		}
 609	}
 610
 611	if (i->value) {
 612		/* Insert the new value. */
 613		s->here->e_value_size = cpu_to_le32(i->value_len);
 614		if (i->value_len) {
 615			size_t size = EXT3_XATTR_SIZE(i->value_len);
 616			void *val = s->base + min_offs - size;
 617			s->here->e_value_offs = cpu_to_le16(min_offs - size);
 618			memset(val + size - EXT3_XATTR_PAD, 0,
 619			       EXT3_XATTR_PAD); /* Clear the pad bytes. */
 620			memcpy(val, i->value, i->value_len);
 621		}
 622	}
 623	return 0;
 624}
 625
 626struct ext3_xattr_block_find {
 627	struct ext3_xattr_search s;
 628	struct buffer_head *bh;
 629};
 630
 631static int
 632ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
 633		      struct ext3_xattr_block_find *bs)
 634{
 635	struct super_block *sb = inode->i_sb;
 636	int error;
 637
 638	ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
 639		  i->name_index, i->name, i->value, (long)i->value_len);
 640
 641	if (EXT3_I(inode)->i_file_acl) {
 642		/* The inode already has an extended attribute block. */
 643		bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
 644		error = -EIO;
 645		if (!bs->bh)
 646			goto cleanup;
 647		ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
 648			atomic_read(&(bs->bh->b_count)),
 649			le32_to_cpu(BHDR(bs->bh)->h_refcount));
 650		if (ext3_xattr_check_block(bs->bh)) {
 651			ext3_error(sb, __func__,
 652				"inode %lu: bad block "E3FSBLK, inode->i_ino,
 653				EXT3_I(inode)->i_file_acl);
 654			error = -EIO;
 655			goto cleanup;
 656		}
 657		/* Find the named attribute. */
 658		bs->s.base = BHDR(bs->bh);
 659		bs->s.first = BFIRST(bs->bh);
 660		bs->s.end = bs->bh->b_data + bs->bh->b_size;
 661		bs->s.here = bs->s.first;
 662		error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
 663					      i->name, bs->bh->b_size, 1);
 664		if (error && error != -ENODATA)
 665			goto cleanup;
 666		bs->s.not_found = error;
 667	}
 668	error = 0;
 669
 670cleanup:
 671	return error;
 672}
 673
 674static int
 675ext3_xattr_block_set(handle_t *handle, struct inode *inode,
 676		     struct ext3_xattr_info *i,
 677		     struct ext3_xattr_block_find *bs)
 678{
 679	struct super_block *sb = inode->i_sb;
 680	struct buffer_head *new_bh = NULL;
 681	struct ext3_xattr_search *s = &bs->s;
 682	struct mb_cache_entry *ce = NULL;
 683	int error = 0;
 684
 685#define header(x) ((struct ext3_xattr_header *)(x))
 686
 687	if (i->value && i->value_len > sb->s_blocksize)
 688		return -ENOSPC;
 689	if (s->base) {
 690		ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
 691					bs->bh->b_blocknr);
 692		error = ext3_journal_get_write_access(handle, bs->bh);
 693		if (error)
 694			goto cleanup;
 695		lock_buffer(bs->bh);
 696
 697		if (header(s->base)->h_refcount == cpu_to_le32(1)) {
 698			if (ce) {
 699				mb_cache_entry_free(ce);
 700				ce = NULL;
 701			}
 702			ea_bdebug(bs->bh, "modifying in-place");
 703			error = ext3_xattr_set_entry(i, s);
 704			if (!error) {
 705				if (!IS_LAST_ENTRY(s->first))
 706					ext3_xattr_rehash(header(s->base),
 707							  s->here);
 708				ext3_xattr_cache_insert(bs->bh);
 709			}
 710			unlock_buffer(bs->bh);
 711			if (error == -EIO)
 712				goto bad_block;
 713			if (!error)
 714				error = ext3_journal_dirty_metadata(handle,
 715								    bs->bh);
 716			if (error)
 717				goto cleanup;
 718			goto inserted;
 719		} else {
 720			int offset = (char *)s->here - bs->bh->b_data;
 721
 722			unlock_buffer(bs->bh);
 723			journal_release_buffer(handle, bs->bh);
 724
 725			if (ce) {
 726				mb_cache_entry_release(ce);
 727				ce = NULL;
 728			}
 729			ea_bdebug(bs->bh, "cloning");
 730			s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
 731			error = -ENOMEM;
 732			if (s->base == NULL)
 733				goto cleanup;
 734			memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
 735			s->first = ENTRY(header(s->base)+1);
 736			header(s->base)->h_refcount = cpu_to_le32(1);
 737			s->here = ENTRY(s->base + offset);
 738			s->end = s->base + bs->bh->b_size;
 739		}
 740	} else {
 741		/* Allocate a buffer where we construct the new block. */
 742		s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
 743		/* assert(header == s->base) */
 744		error = -ENOMEM;
 745		if (s->base == NULL)
 746			goto cleanup;
 747		header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
 748		header(s->base)->h_blocks = cpu_to_le32(1);
 749		header(s->base)->h_refcount = cpu_to_le32(1);
 750		s->first = ENTRY(header(s->base)+1);
 751		s->here = ENTRY(header(s->base)+1);
 752		s->end = s->base + sb->s_blocksize;
 753	}
 754
 755	error = ext3_xattr_set_entry(i, s);
 756	if (error == -EIO)
 757		goto bad_block;
 758	if (error)
 759		goto cleanup;
 760	if (!IS_LAST_ENTRY(s->first))
 761		ext3_xattr_rehash(header(s->base), s->here);
 762
 763inserted:
 764	if (!IS_LAST_ENTRY(s->first)) {
 765		new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
 766		if (new_bh) {
 767			/* We found an identical block in the cache. */
 768			if (new_bh == bs->bh)
 769				ea_bdebug(new_bh, "keeping");
 770			else {
 771				/* The old block is released after updating
 772				   the inode. */
 773				error = dquot_alloc_block(inode, 1);
 774				if (error)
 775					goto cleanup;
 776				error = ext3_journal_get_write_access(handle,
 777								      new_bh);
 778				if (error)
 779					goto cleanup_dquot;
 780				lock_buffer(new_bh);
 781				le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
 782				ea_bdebug(new_bh, "reusing; refcount now=%d",
 783					le32_to_cpu(BHDR(new_bh)->h_refcount));
 784				unlock_buffer(new_bh);
 785				error = ext3_journal_dirty_metadata(handle,
 786								    new_bh);
 787				if (error)
 788					goto cleanup_dquot;
 789			}
 790			mb_cache_entry_release(ce);
 791			ce = NULL;
 792		} else if (bs->bh && s->base == bs->bh->b_data) {
 793			/* We were modifying this block in-place. */
 794			ea_bdebug(bs->bh, "keeping this block");
 795			new_bh = bs->bh;
 796			get_bh(new_bh);
 797		} else {
 798			/* We need to allocate a new block */
 799			ext3_fsblk_t goal = ext3_group_first_block_no(sb,
 800						EXT3_I(inode)->i_block_group);
 801			ext3_fsblk_t block;
 802
 803			/*
 804			 * Protect us agaist concurrent allocations to the
 805			 * same inode from ext3_..._writepage(). Reservation
 806			 * code does not expect racing allocations.
 807			 */
 808			mutex_lock(&EXT3_I(inode)->truncate_mutex);
 809			block = ext3_new_block(handle, inode, goal, &error);
 810			mutex_unlock(&EXT3_I(inode)->truncate_mutex);
 811			if (error)
 812				goto cleanup;
 813			ea_idebug(inode, "creating block %d", block);
 814
 815			new_bh = sb_getblk(sb, block);
 816			if (!new_bh) {
 817getblk_failed:
 818				ext3_free_blocks(handle, inode, block, 1);
 819				error = -EIO;
 820				goto cleanup;
 821			}
 822			lock_buffer(new_bh);
 823			error = ext3_journal_get_create_access(handle, new_bh);
 824			if (error) {
 825				unlock_buffer(new_bh);
 826				goto getblk_failed;
 827			}
 828			memcpy(new_bh->b_data, s->base, new_bh->b_size);
 829			set_buffer_uptodate(new_bh);
 830			unlock_buffer(new_bh);
 831			ext3_xattr_cache_insert(new_bh);
 832			error = ext3_journal_dirty_metadata(handle, new_bh);
 833			if (error)
 834				goto cleanup;
 835		}
 836	}
 837
 838	/* Update the inode. */
 839	EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
 840
 841	/* Drop the previous xattr block. */
 842	if (bs->bh && bs->bh != new_bh)
 843		ext3_xattr_release_block(handle, inode, bs->bh);
 844	error = 0;
 845
 846cleanup:
 847	if (ce)
 848		mb_cache_entry_release(ce);
 849	brelse(new_bh);
 850	if (!(bs->bh && s->base == bs->bh->b_data))
 851		kfree(s->base);
 852
 853	return error;
 854
 855cleanup_dquot:
 856	dquot_free_block(inode, 1);
 857	goto cleanup;
 858
 859bad_block:
 860	ext3_error(inode->i_sb, __func__,
 861		   "inode %lu: bad block "E3FSBLK, inode->i_ino,
 862		   EXT3_I(inode)->i_file_acl);
 863	goto cleanup;
 864
 865#undef header
 866}
 867
 868struct ext3_xattr_ibody_find {
 869	struct ext3_xattr_search s;
 870	struct ext3_iloc iloc;
 871};
 872
 873static int
 874ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
 875		      struct ext3_xattr_ibody_find *is)
 876{
 877	struct ext3_xattr_ibody_header *header;
 878	struct ext3_inode *raw_inode;
 879	int error;
 880
 881	if (EXT3_I(inode)->i_extra_isize == 0)
 882		return 0;
 883	raw_inode = ext3_raw_inode(&is->iloc);
 884	header = IHDR(inode, raw_inode);
 885	is->s.base = is->s.first = IFIRST(header);
 886	is->s.here = is->s.first;
 887	is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
 888	if (ext3_test_inode_state(inode, EXT3_STATE_XATTR)) {
 889		error = ext3_xattr_check_names(IFIRST(header), is->s.end);
 890		if (error)
 891			return error;
 892		/* Find the named attribute. */
 893		error = ext3_xattr_find_entry(&is->s.here, i->name_index,
 894					      i->name, is->s.end -
 895					      (void *)is->s.base, 0);
 896		if (error && error != -ENODATA)
 897			return error;
 898		is->s.not_found = error;
 899	}
 900	return 0;
 901}
 902
 903static int
 904ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
 905		     struct ext3_xattr_info *i,
 906		     struct ext3_xattr_ibody_find *is)
 907{
 908	struct ext3_xattr_ibody_header *header;
 909	struct ext3_xattr_search *s = &is->s;
 910	int error;
 911
 912	if (EXT3_I(inode)->i_extra_isize == 0)
 913		return -ENOSPC;
 914	error = ext3_xattr_set_entry(i, s);
 915	if (error)
 916		return error;
 917	header = IHDR(inode, ext3_raw_inode(&is->iloc));
 918	if (!IS_LAST_ENTRY(s->first)) {
 919		header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
 920		ext3_set_inode_state(inode, EXT3_STATE_XATTR);
 921	} else {
 922		header->h_magic = cpu_to_le32(0);
 923		ext3_clear_inode_state(inode, EXT3_STATE_XATTR);
 924	}
 925	return 0;
 926}
 927
 928/*
 929 * ext3_xattr_set_handle()
 930 *
 931 * Create, replace or remove an extended attribute for this inode.  Value
 932 * is NULL to remove an existing extended attribute, and non-NULL to
 933 * either replace an existing extended attribute, or create a new extended
 934 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
 935 * specify that an extended attribute must exist and must not exist
 936 * previous to the call, respectively.
 937 *
 938 * Returns 0, or a negative error number on failure.
 939 */
 940int
 941ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
 942		      const char *name, const void *value, size_t value_len,
 943		      int flags)
 944{
 945	struct ext3_xattr_info i = {
 946		.name_index = name_index,
 947		.name = name,
 948		.value = value,
 949		.value_len = value_len,
 950
 951	};
 952	struct ext3_xattr_ibody_find is = {
 953		.s = { .not_found = -ENODATA, },
 954	};
 955	struct ext3_xattr_block_find bs = {
 956		.s = { .not_found = -ENODATA, },
 957	};
 958	int error;
 959
 960	if (!name)
 961		return -EINVAL;
 962	if (strlen(name) > 255)
 963		return -ERANGE;
 964	down_write(&EXT3_I(inode)->xattr_sem);
 965	error = ext3_get_inode_loc(inode, &is.iloc);
 966	if (error)
 967		goto cleanup;
 968
 969	error = ext3_journal_get_write_access(handle, is.iloc.bh);
 970	if (error)
 971		goto cleanup;
 972
 973	if (ext3_test_inode_state(inode, EXT3_STATE_NEW)) {
 974		struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
 975		memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
 976		ext3_clear_inode_state(inode, EXT3_STATE_NEW);
 977	}
 978
 979	error = ext3_xattr_ibody_find(inode, &i, &is);
 980	if (error)
 981		goto cleanup;
 982	if (is.s.not_found)
 983		error = ext3_xattr_block_find(inode, &i, &bs);
 984	if (error)
 985		goto cleanup;
 986	if (is.s.not_found && bs.s.not_found) {
 987		error = -ENODATA;
 988		if (flags & XATTR_REPLACE)
 989			goto cleanup;
 990		error = 0;
 991		if (!value)
 992			goto cleanup;
 993	} else {
 994		error = -EEXIST;
 995		if (flags & XATTR_CREATE)
 996			goto cleanup;
 997	}
 998	if (!value) {
 999		if (!is.s.not_found)
1000			error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1001		else if (!bs.s.not_found)
1002			error = ext3_xattr_block_set(handle, inode, &i, &bs);
1003	} else {
1004		error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1005		if (!error && !bs.s.not_found) {
1006			i.value = NULL;
1007			error = ext3_xattr_block_set(handle, inode, &i, &bs);
1008		} else if (error == -ENOSPC) {
1009			if (EXT3_I(inode)->i_file_acl && !bs.s.base) {
1010				error = ext3_xattr_block_find(inode, &i, &bs);
1011				if (error)
1012					goto cleanup;
1013			}
1014			error = ext3_xattr_block_set(handle, inode, &i, &bs);
1015			if (error)
1016				goto cleanup;
1017			if (!is.s.not_found) {
1018				i.value = NULL;
1019				error = ext3_xattr_ibody_set(handle, inode, &i,
1020							     &is);
1021			}
1022		}
1023	}
1024	if (!error) {
1025		ext3_xattr_update_super_block(handle, inode->i_sb);
1026		inode->i_ctime = CURRENT_TIME_SEC;
1027		error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
1028		/*
1029		 * The bh is consumed by ext3_mark_iloc_dirty, even with
1030		 * error != 0.
1031		 */
1032		is.iloc.bh = NULL;
1033		if (IS_SYNC(inode))
1034			handle->h_sync = 1;
1035	}
1036
1037cleanup:
1038	brelse(is.iloc.bh);
1039	brelse(bs.bh);
1040	up_write(&EXT3_I(inode)->xattr_sem);
1041	return error;
1042}
1043
1044/*
1045 * ext3_xattr_set()
1046 *
1047 * Like ext3_xattr_set_handle, but start from an inode. This extended
1048 * attribute modification is a filesystem transaction by itself.
1049 *
1050 * Returns 0, or a negative error number on failure.
1051 */
1052int
1053ext3_xattr_set(struct inode *inode, int name_index, const char *name,
1054	       const void *value, size_t value_len, int flags)
1055{
1056	handle_t *handle;
1057	int error, retries = 0;
1058
1059retry:
1060	handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
1061	if (IS_ERR(handle)) {
1062		error = PTR_ERR(handle);
1063	} else {
1064		int error2;
1065
1066		error = ext3_xattr_set_handle(handle, inode, name_index, name,
1067					      value, value_len, flags);
1068		error2 = ext3_journal_stop(handle);
1069		if (error == -ENOSPC &&
1070		    ext3_should_retry_alloc(inode->i_sb, &retries))
1071			goto retry;
1072		if (error == 0)
1073			error = error2;
1074	}
1075
1076	return error;
1077}
1078
1079/*
1080 * ext3_xattr_delete_inode()
1081 *
1082 * Free extended attribute resources associated with this inode. This
1083 * is called immediately before an inode is freed. We have exclusive
1084 * access to the inode.
1085 */
1086void
1087ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
1088{
1089	struct buffer_head *bh = NULL;
1090
1091	if (!EXT3_I(inode)->i_file_acl)
1092		goto cleanup;
1093	bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
1094	if (!bh) {
1095		ext3_error(inode->i_sb, __func__,
1096			"inode %lu: block "E3FSBLK" read error", inode->i_ino,
1097			EXT3_I(inode)->i_file_acl);
1098		goto cleanup;
1099	}
1100	if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
1101	    BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1102		ext3_error(inode->i_sb, __func__,
1103			"inode %lu: bad block "E3FSBLK, inode->i_ino,
1104			EXT3_I(inode)->i_file_acl);
1105		goto cleanup;
1106	}
1107	ext3_xattr_release_block(handle, inode, bh);
1108	EXT3_I(inode)->i_file_acl = 0;
1109
1110cleanup:
1111	brelse(bh);
1112}
1113
1114/*
1115 * ext3_xattr_put_super()
1116 *
1117 * This is called when a file system is unmounted.
1118 */
1119void
1120ext3_xattr_put_super(struct super_block *sb)
1121{
1122	mb_cache_shrink(sb->s_bdev);
1123}
1124
1125/*
1126 * ext3_xattr_cache_insert()
1127 *
1128 * Create a new entry in the extended attribute cache, and insert
1129 * it unless such an entry is already in the cache.
1130 *
1131 * Returns 0, or a negative error number on failure.
1132 */
1133static void
1134ext3_xattr_cache_insert(struct buffer_head *bh)
1135{
1136	__u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1137	struct mb_cache_entry *ce;
1138	int error;
1139
1140	ce = mb_cache_entry_alloc(ext3_xattr_cache, GFP_NOFS);
1141	if (!ce) {
1142		ea_bdebug(bh, "out of memory");
1143		return;
1144	}
1145	error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1146	if (error) {
1147		mb_cache_entry_free(ce);
1148		if (error == -EBUSY) {
1149			ea_bdebug(bh, "already in cache");
1150			error = 0;
1151		}
1152	} else {
1153		ea_bdebug(bh, "inserting [%x]", (int)hash);
1154		mb_cache_entry_release(ce);
1155	}
1156}
1157
1158/*
1159 * ext3_xattr_cmp()
1160 *
1161 * Compare two extended attribute blocks for equality.
1162 *
1163 * Returns 0 if the blocks are equal, 1 if they differ, and
1164 * a negative error number on errors.
1165 */
1166static int
1167ext3_xattr_cmp(struct ext3_xattr_header *header1,
1168	       struct ext3_xattr_header *header2)
1169{
1170	struct ext3_xattr_entry *entry1, *entry2;
1171
1172	entry1 = ENTRY(header1+1);
1173	entry2 = ENTRY(header2+1);
1174	while (!IS_LAST_ENTRY(entry1)) {
1175		if (IS_LAST_ENTRY(entry2))
1176			return 1;
1177		if (entry1->e_hash != entry2->e_hash ||
1178		    entry1->e_name_index != entry2->e_name_index ||
1179		    entry1->e_name_len != entry2->e_name_len ||
1180		    entry1->e_value_size != entry2->e_value_size ||
1181		    memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1182			return 1;
1183		if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1184			return -EIO;
1185		if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1186			   (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1187			   le32_to_cpu(entry1->e_value_size)))
1188			return 1;
1189
1190		entry1 = EXT3_XATTR_NEXT(entry1);
1191		entry2 = EXT3_XATTR_NEXT(entry2);
1192	}
1193	if (!IS_LAST_ENTRY(entry2))
1194		return 1;
1195	return 0;
1196}
1197
1198/*
1199 * ext3_xattr_cache_find()
1200 *
1201 * Find an identical extended attribute block.
1202 *
1203 * Returns a pointer to the block found, or NULL if such a block was
1204 * not found or an error occurred.
1205 */
1206static struct buffer_head *
1207ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
1208		      struct mb_cache_entry **pce)
1209{
1210	__u32 hash = le32_to_cpu(header->h_hash);
1211	struct mb_cache_entry *ce;
1212
1213	if (!header->h_hash)
1214		return NULL;  /* never share */
1215	ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1216again:
1217	ce = mb_cache_entry_find_first(ext3_xattr_cache, inode->i_sb->s_bdev,
1218				       hash);
1219	while (ce) {
1220		struct buffer_head *bh;
1221
1222		if (IS_ERR(ce)) {
1223			if (PTR_ERR(ce) == -EAGAIN)
1224				goto again;
1225			break;
1226		}
1227		bh = sb_bread(inode->i_sb, ce->e_block);
1228		if (!bh) {
1229			ext3_error(inode->i_sb, __func__,
1230				"inode %lu: block %lu read error",
1231				inode->i_ino, (unsigned long) ce->e_block);
1232		} else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1233				EXT3_XATTR_REFCOUNT_MAX) {
1234			ea_idebug(inode, "block %lu refcount %d>=%d",
1235				  (unsigned long) ce->e_block,
1236				  le32_to_cpu(BHDR(bh)->h_refcount),
1237					  EXT3_XATTR_REFCOUNT_MAX);
1238		} else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
1239			*pce = ce;
1240			return bh;
1241		}
1242		brelse(bh);
1243		ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1244	}
1245	return NULL;
1246}
1247
1248#define NAME_HASH_SHIFT 5
1249#define VALUE_HASH_SHIFT 16
1250
1251/*
1252 * ext3_xattr_hash_entry()
1253 *
1254 * Compute the hash of an extended attribute.
1255 */
1256static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1257					 struct ext3_xattr_entry *entry)
1258{
1259	__u32 hash = 0;
1260	char *name = entry->e_name;
1261	int n;
1262
1263	for (n=0; n < entry->e_name_len; n++) {
1264		hash = (hash << NAME_HASH_SHIFT) ^
1265		       (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1266		       *name++;
1267	}
1268
1269	if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1270		__le32 *value = (__le32 *)((char *)header +
1271			le16_to_cpu(entry->e_value_offs));
1272		for (n = (le32_to_cpu(entry->e_value_size) +
1273		     EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1274			hash = (hash << VALUE_HASH_SHIFT) ^
1275			       (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1276			       le32_to_cpu(*value++);
1277		}
1278	}
1279	entry->e_hash = cpu_to_le32(hash);
1280}
1281
1282#undef NAME_HASH_SHIFT
1283#undef VALUE_HASH_SHIFT
1284
1285#define BLOCK_HASH_SHIFT 16
1286
1287/*
1288 * ext3_xattr_rehash()
1289 *
1290 * Re-compute the extended attribute hash value after an entry has changed.
1291 */
1292static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1293			      struct ext3_xattr_entry *entry)
1294{
1295	struct ext3_xattr_entry *here;
1296	__u32 hash = 0;
1297
1298	ext3_xattr_hash_entry(header, entry);
1299	here = ENTRY(header+1);
1300	while (!IS_LAST_ENTRY(here)) {
1301		if (!here->e_hash) {
1302			/* Block is not shared if an entry's hash value == 0 */
1303			hash = 0;
1304			break;
1305		}
1306		hash = (hash << BLOCK_HASH_SHIFT) ^
1307		       (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1308		       le32_to_cpu(here->e_hash);
1309		here = EXT3_XATTR_NEXT(here);
1310	}
1311	header->h_hash = cpu_to_le32(hash);
1312}
1313
1314#undef BLOCK_HASH_SHIFT
1315
1316int __init
1317init_ext3_xattr(void)
1318{
1319	ext3_xattr_cache = mb_cache_create("ext3_xattr", 6);
1320	if (!ext3_xattr_cache)
1321		return -ENOMEM;
1322	return 0;
1323}
1324
1325void
1326exit_ext3_xattr(void)
1327{
1328	if (ext3_xattr_cache)
1329		mb_cache_destroy(ext3_xattr_cache);
1330	ext3_xattr_cache = NULL;
1331}
v3.1
   1/*
   2 * linux/fs/ext3/xattr.c
   3 *
   4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
   5 *
   6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
   7 * Ext3 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 * EXT3_I(inode)->i_file_acl is protected by EXT3_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/ext3_jbd.h>
  57#include <linux/ext3_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 ext3_xattr_header *)((bh)->b_data))
  65#define ENTRY(ptr) ((struct ext3_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 ext3_xattr_ibody_header *) \
  71		((void *)raw_inode + \
  72		 EXT3_GOOD_OLD_INODE_SIZE + \
  73		 EXT3_I(inode)->i_extra_isize))
  74#define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
  75
  76#ifdef EXT3_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 ext3_xattr_cache_insert(struct buffer_head *);
  97static struct buffer_head *ext3_xattr_cache_find(struct inode *,
  98						 struct ext3_xattr_header *,
  99						 struct mb_cache_entry **);
 100static void ext3_xattr_rehash(struct ext3_xattr_header *,
 101			      struct ext3_xattr_entry *);
 102static int ext3_xattr_list(struct dentry *dentry, char *buffer,
 103			   size_t buffer_size);
 104
 105static struct mb_cache *ext3_xattr_cache;
 106
 107static const struct xattr_handler *ext3_xattr_handler_map[] = {
 108	[EXT3_XATTR_INDEX_USER]		     = &ext3_xattr_user_handler,
 109#ifdef CONFIG_EXT3_FS_POSIX_ACL
 110	[EXT3_XATTR_INDEX_POSIX_ACL_ACCESS]  = &ext3_xattr_acl_access_handler,
 111	[EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_handler,
 112#endif
 113	[EXT3_XATTR_INDEX_TRUSTED]	     = &ext3_xattr_trusted_handler,
 114#ifdef CONFIG_EXT3_FS_SECURITY
 115	[EXT3_XATTR_INDEX_SECURITY]	     = &ext3_xattr_security_handler,
 116#endif
 117};
 118
 119const struct xattr_handler *ext3_xattr_handlers[] = {
 120	&ext3_xattr_user_handler,
 121	&ext3_xattr_trusted_handler,
 122#ifdef CONFIG_EXT3_FS_POSIX_ACL
 123	&ext3_xattr_acl_access_handler,
 124	&ext3_xattr_acl_default_handler,
 125#endif
 126#ifdef CONFIG_EXT3_FS_SECURITY
 127	&ext3_xattr_security_handler,
 128#endif
 129	NULL
 130};
 131
 132static inline const struct xattr_handler *
 133ext3_xattr_handler(int name_index)
 134{
 135	const struct xattr_handler *handler = NULL;
 136
 137	if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
 138		handler = ext3_xattr_handler_map[name_index];
 139	return handler;
 140}
 141
 142/*
 143 * Inode operation listxattr()
 144 *
 145 * dentry->d_inode->i_mutex: don't care
 146 */
 147ssize_t
 148ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
 149{
 150	return ext3_xattr_list(dentry, buffer, size);
 151}
 152
 153static int
 154ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
 155{
 156	while (!IS_LAST_ENTRY(entry)) {
 157		struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
 158		if ((void *)next >= end)
 159			return -EIO;
 160		entry = next;
 161	}
 162	return 0;
 163}
 164
 165static inline int
 166ext3_xattr_check_block(struct buffer_head *bh)
 167{
 168	int error;
 169
 170	if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
 171	    BHDR(bh)->h_blocks != cpu_to_le32(1))
 172		return -EIO;
 173	error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
 174	return error;
 175}
 176
 177static inline int
 178ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
 179{
 180	size_t value_size = le32_to_cpu(entry->e_value_size);
 181
 182	if (entry->e_value_block != 0 || value_size > size ||
 183	    le16_to_cpu(entry->e_value_offs) + value_size > size)
 184		return -EIO;
 185	return 0;
 186}
 187
 188static int
 189ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
 190		      const char *name, size_t size, int sorted)
 191{
 192	struct ext3_xattr_entry *entry;
 193	size_t name_len;
 194	int cmp = 1;
 195
 196	if (name == NULL)
 197		return -EINVAL;
 198	name_len = strlen(name);
 199	entry = *pentry;
 200	for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
 201		cmp = name_index - entry->e_name_index;
 202		if (!cmp)
 203			cmp = name_len - entry->e_name_len;
 204		if (!cmp)
 205			cmp = memcmp(name, entry->e_name, name_len);
 206		if (cmp <= 0 && (sorted || cmp == 0))
 207			break;
 208	}
 209	*pentry = entry;
 210	if (!cmp && ext3_xattr_check_entry(entry, size))
 211			return -EIO;
 212	return cmp ? -ENODATA : 0;
 213}
 214
 215static int
 216ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
 217		     void *buffer, size_t buffer_size)
 218{
 219	struct buffer_head *bh = NULL;
 220	struct ext3_xattr_entry *entry;
 221	size_t size;
 222	int error;
 223
 224	ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
 225		  name_index, name, buffer, (long)buffer_size);
 226
 227	error = -ENODATA;
 228	if (!EXT3_I(inode)->i_file_acl)
 229		goto cleanup;
 230	ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
 231	bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
 232	if (!bh)
 233		goto cleanup;
 234	ea_bdebug(bh, "b_count=%d, refcount=%d",
 235		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
 236	if (ext3_xattr_check_block(bh)) {
 237bad_block:	ext3_error(inode->i_sb, __func__,
 238			   "inode %lu: bad block "E3FSBLK, inode->i_ino,
 239			   EXT3_I(inode)->i_file_acl);
 240		error = -EIO;
 241		goto cleanup;
 242	}
 243	ext3_xattr_cache_insert(bh);
 244	entry = BFIRST(bh);
 245	error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
 246	if (error == -EIO)
 247		goto bad_block;
 248	if (error)
 249		goto cleanup;
 250	size = le32_to_cpu(entry->e_value_size);
 251	if (buffer) {
 252		error = -ERANGE;
 253		if (size > buffer_size)
 254			goto cleanup;
 255		memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
 256		       size);
 257	}
 258	error = size;
 259
 260cleanup:
 261	brelse(bh);
 262	return error;
 263}
 264
 265static int
 266ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
 267		     void *buffer, size_t buffer_size)
 268{
 269	struct ext3_xattr_ibody_header *header;
 270	struct ext3_xattr_entry *entry;
 271	struct ext3_inode *raw_inode;
 272	struct ext3_iloc iloc;
 273	size_t size;
 274	void *end;
 275	int error;
 276
 277	if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
 278		return -ENODATA;
 279	error = ext3_get_inode_loc(inode, &iloc);
 280	if (error)
 281		return error;
 282	raw_inode = ext3_raw_inode(&iloc);
 283	header = IHDR(inode, raw_inode);
 284	entry = IFIRST(header);
 285	end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
 286	error = ext3_xattr_check_names(entry, end);
 287	if (error)
 288		goto cleanup;
 289	error = ext3_xattr_find_entry(&entry, name_index, name,
 290				      end - (void *)entry, 0);
 291	if (error)
 292		goto cleanup;
 293	size = le32_to_cpu(entry->e_value_size);
 294	if (buffer) {
 295		error = -ERANGE;
 296		if (size > buffer_size)
 297			goto cleanup;
 298		memcpy(buffer, (void *)IFIRST(header) +
 299		       le16_to_cpu(entry->e_value_offs), size);
 300	}
 301	error = size;
 302
 303cleanup:
 304	brelse(iloc.bh);
 305	return error;
 306}
 307
 308/*
 309 * ext3_xattr_get()
 310 *
 311 * Copy an extended attribute into the buffer
 312 * provided, or compute the buffer size required.
 313 * Buffer is NULL to compute the size of the buffer required.
 314 *
 315 * Returns a negative error number on failure, or the number of bytes
 316 * used / required on success.
 317 */
 318int
 319ext3_xattr_get(struct inode *inode, int name_index, const char *name,
 320	       void *buffer, size_t buffer_size)
 321{
 322	int error;
 323
 324	down_read(&EXT3_I(inode)->xattr_sem);
 325	error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
 326				     buffer_size);
 327	if (error == -ENODATA)
 328		error = ext3_xattr_block_get(inode, name_index, name, buffer,
 329					     buffer_size);
 330	up_read(&EXT3_I(inode)->xattr_sem);
 331	return error;
 332}
 333
 334static int
 335ext3_xattr_list_entries(struct dentry *dentry, struct ext3_xattr_entry *entry,
 336			char *buffer, size_t buffer_size)
 337{
 338	size_t rest = buffer_size;
 339
 340	for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
 341		const struct xattr_handler *handler =
 342			ext3_xattr_handler(entry->e_name_index);
 343
 344		if (handler) {
 345			size_t size = handler->list(dentry, buffer, rest,
 346						    entry->e_name,
 347						    entry->e_name_len,
 348						    handler->flags);
 349			if (buffer) {
 350				if (size > rest)
 351					return -ERANGE;
 352				buffer += size;
 353			}
 354			rest -= size;
 355		}
 356	}
 357	return buffer_size - rest;
 358}
 359
 360static int
 361ext3_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
 362{
 363	struct inode *inode = dentry->d_inode;
 364	struct buffer_head *bh = NULL;
 365	int error;
 366
 367	ea_idebug(inode, "buffer=%p, buffer_size=%ld",
 368		  buffer, (long)buffer_size);
 369
 370	error = 0;
 371	if (!EXT3_I(inode)->i_file_acl)
 372		goto cleanup;
 373	ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
 374	bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
 375	error = -EIO;
 376	if (!bh)
 377		goto cleanup;
 378	ea_bdebug(bh, "b_count=%d, refcount=%d",
 379		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
 380	if (ext3_xattr_check_block(bh)) {
 381		ext3_error(inode->i_sb, __func__,
 382			   "inode %lu: bad block "E3FSBLK, inode->i_ino,
 383			   EXT3_I(inode)->i_file_acl);
 384		error = -EIO;
 385		goto cleanup;
 386	}
 387	ext3_xattr_cache_insert(bh);
 388	error = ext3_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
 389
 390cleanup:
 391	brelse(bh);
 392
 393	return error;
 394}
 395
 396static int
 397ext3_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
 398{
 399	struct inode *inode = dentry->d_inode;
 400	struct ext3_xattr_ibody_header *header;
 401	struct ext3_inode *raw_inode;
 402	struct ext3_iloc iloc;
 403	void *end;
 404	int error;
 405
 406	if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
 407		return 0;
 408	error = ext3_get_inode_loc(inode, &iloc);
 409	if (error)
 410		return error;
 411	raw_inode = ext3_raw_inode(&iloc);
 412	header = IHDR(inode, raw_inode);
 413	end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
 414	error = ext3_xattr_check_names(IFIRST(header), end);
 415	if (error)
 416		goto cleanup;
 417	error = ext3_xattr_list_entries(dentry, IFIRST(header),
 418					buffer, buffer_size);
 419
 420cleanup:
 421	brelse(iloc.bh);
 422	return error;
 423}
 424
 425/*
 426 * ext3_xattr_list()
 427 *
 428 * Copy a list of attribute names into the buffer
 429 * provided, or compute the buffer size required.
 430 * Buffer is NULL to compute the size of the buffer required.
 431 *
 432 * Returns a negative error number on failure, or the number of bytes
 433 * used / required on success.
 434 */
 435static int
 436ext3_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
 437{
 438	int i_error, b_error;
 439
 440	down_read(&EXT3_I(dentry->d_inode)->xattr_sem);
 441	i_error = ext3_xattr_ibody_list(dentry, buffer, buffer_size);
 442	if (i_error < 0) {
 443		b_error = 0;
 444	} else {
 445		if (buffer) {
 446			buffer += i_error;
 447			buffer_size -= i_error;
 448		}
 449		b_error = ext3_xattr_block_list(dentry, buffer, buffer_size);
 450		if (b_error < 0)
 451			i_error = 0;
 452	}
 453	up_read(&EXT3_I(dentry->d_inode)->xattr_sem);
 454	return i_error + b_error;
 455}
 456
 457/*
 458 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
 459 * not set, set it.
 460 */
 461static void ext3_xattr_update_super_block(handle_t *handle,
 462					  struct super_block *sb)
 463{
 464	if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
 465		return;
 466
 467	if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
 468		EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR);
 469		ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
 470	}
 471}
 472
 473/*
 474 * Release the xattr block BH: If the reference count is > 1, decrement
 475 * it; otherwise free the block.
 476 */
 477static void
 478ext3_xattr_release_block(handle_t *handle, struct inode *inode,
 479			 struct buffer_head *bh)
 480{
 481	struct mb_cache_entry *ce = NULL;
 482	int error = 0;
 483
 484	ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
 485	error = ext3_journal_get_write_access(handle, bh);
 486	if (error)
 487		 goto out;
 488
 489	lock_buffer(bh);
 490
 491	if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
 492		ea_bdebug(bh, "refcount now=0; freeing");
 493		if (ce)
 494			mb_cache_entry_free(ce);
 495		ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
 496		get_bh(bh);
 497		ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
 498	} else {
 499		le32_add_cpu(&BHDR(bh)->h_refcount, -1);
 500		error = ext3_journal_dirty_metadata(handle, bh);
 501		if (IS_SYNC(inode))
 502			handle->h_sync = 1;
 503		dquot_free_block(inode, 1);
 504		ea_bdebug(bh, "refcount now=%d; releasing",
 505			  le32_to_cpu(BHDR(bh)->h_refcount));
 506		if (ce)
 507			mb_cache_entry_release(ce);
 508	}
 509	unlock_buffer(bh);
 510out:
 511	ext3_std_error(inode->i_sb, error);
 512	return;
 513}
 514
 515struct ext3_xattr_info {
 516	int name_index;
 517	const char *name;
 518	const void *value;
 519	size_t value_len;
 520};
 521
 522struct ext3_xattr_search {
 523	struct ext3_xattr_entry *first;
 524	void *base;
 525	void *end;
 526	struct ext3_xattr_entry *here;
 527	int not_found;
 528};
 529
 530static int
 531ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
 532{
 533	struct ext3_xattr_entry *last;
 534	size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
 535
 536	/* Compute min_offs and last. */
 537	last = s->first;
 538	for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
 539		if (!last->e_value_block && last->e_value_size) {
 540			size_t offs = le16_to_cpu(last->e_value_offs);
 541			if (offs < min_offs)
 542				min_offs = offs;
 543		}
 544	}
 545	free = min_offs - ((void *)last - s->base) - sizeof(__u32);
 546	if (!s->not_found) {
 547		if (!s->here->e_value_block && s->here->e_value_size) {
 548			size_t size = le32_to_cpu(s->here->e_value_size);
 549			free += EXT3_XATTR_SIZE(size);
 550		}
 551		free += EXT3_XATTR_LEN(name_len);
 552	}
 553	if (i->value) {
 554		if (free < EXT3_XATTR_SIZE(i->value_len) ||
 555		    free < EXT3_XATTR_LEN(name_len) +
 556			   EXT3_XATTR_SIZE(i->value_len))
 557			return -ENOSPC;
 558	}
 559
 560	if (i->value && s->not_found) {
 561		/* Insert the new name. */
 562		size_t size = EXT3_XATTR_LEN(name_len);
 563		size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
 564		memmove((void *)s->here + size, s->here, rest);
 565		memset(s->here, 0, size);
 566		s->here->e_name_index = i->name_index;
 567		s->here->e_name_len = name_len;
 568		memcpy(s->here->e_name, i->name, name_len);
 569	} else {
 570		if (!s->here->e_value_block && s->here->e_value_size) {
 571			void *first_val = s->base + min_offs;
 572			size_t offs = le16_to_cpu(s->here->e_value_offs);
 573			void *val = s->base + offs;
 574			size_t size = EXT3_XATTR_SIZE(
 575				le32_to_cpu(s->here->e_value_size));
 576
 577			if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
 578				/* The old and the new value have the same
 579				   size. Just replace. */
 580				s->here->e_value_size =
 581					cpu_to_le32(i->value_len);
 582				memset(val + size - EXT3_XATTR_PAD, 0,
 583				       EXT3_XATTR_PAD); /* Clear pad bytes. */
 584				memcpy(val, i->value, i->value_len);
 585				return 0;
 586			}
 587
 588			/* Remove the old value. */
 589			memmove(first_val + size, first_val, val - first_val);
 590			memset(first_val, 0, size);
 591			s->here->e_value_size = 0;
 592			s->here->e_value_offs = 0;
 593			min_offs += size;
 594
 595			/* Adjust all value offsets. */
 596			last = s->first;
 597			while (!IS_LAST_ENTRY(last)) {
 598				size_t o = le16_to_cpu(last->e_value_offs);
 599				if (!last->e_value_block &&
 600				    last->e_value_size && o < offs)
 601					last->e_value_offs =
 602						cpu_to_le16(o + size);
 603				last = EXT3_XATTR_NEXT(last);
 604			}
 605		}
 606		if (!i->value) {
 607			/* Remove the old name. */
 608			size_t size = EXT3_XATTR_LEN(name_len);
 609			last = ENTRY((void *)last - size);
 610			memmove(s->here, (void *)s->here + size,
 611				(void *)last - (void *)s->here + sizeof(__u32));
 612			memset(last, 0, size);
 613		}
 614	}
 615
 616	if (i->value) {
 617		/* Insert the new value. */
 618		s->here->e_value_size = cpu_to_le32(i->value_len);
 619		if (i->value_len) {
 620			size_t size = EXT3_XATTR_SIZE(i->value_len);
 621			void *val = s->base + min_offs - size;
 622			s->here->e_value_offs = cpu_to_le16(min_offs - size);
 623			memset(val + size - EXT3_XATTR_PAD, 0,
 624			       EXT3_XATTR_PAD); /* Clear the pad bytes. */
 625			memcpy(val, i->value, i->value_len);
 626		}
 627	}
 628	return 0;
 629}
 630
 631struct ext3_xattr_block_find {
 632	struct ext3_xattr_search s;
 633	struct buffer_head *bh;
 634};
 635
 636static int
 637ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
 638		      struct ext3_xattr_block_find *bs)
 639{
 640	struct super_block *sb = inode->i_sb;
 641	int error;
 642
 643	ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
 644		  i->name_index, i->name, i->value, (long)i->value_len);
 645
 646	if (EXT3_I(inode)->i_file_acl) {
 647		/* The inode already has an extended attribute block. */
 648		bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
 649		error = -EIO;
 650		if (!bs->bh)
 651			goto cleanup;
 652		ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
 653			atomic_read(&(bs->bh->b_count)),
 654			le32_to_cpu(BHDR(bs->bh)->h_refcount));
 655		if (ext3_xattr_check_block(bs->bh)) {
 656			ext3_error(sb, __func__,
 657				"inode %lu: bad block "E3FSBLK, inode->i_ino,
 658				EXT3_I(inode)->i_file_acl);
 659			error = -EIO;
 660			goto cleanup;
 661		}
 662		/* Find the named attribute. */
 663		bs->s.base = BHDR(bs->bh);
 664		bs->s.first = BFIRST(bs->bh);
 665		bs->s.end = bs->bh->b_data + bs->bh->b_size;
 666		bs->s.here = bs->s.first;
 667		error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
 668					      i->name, bs->bh->b_size, 1);
 669		if (error && error != -ENODATA)
 670			goto cleanup;
 671		bs->s.not_found = error;
 672	}
 673	error = 0;
 674
 675cleanup:
 676	return error;
 677}
 678
 679static int
 680ext3_xattr_block_set(handle_t *handle, struct inode *inode,
 681		     struct ext3_xattr_info *i,
 682		     struct ext3_xattr_block_find *bs)
 683{
 684	struct super_block *sb = inode->i_sb;
 685	struct buffer_head *new_bh = NULL;
 686	struct ext3_xattr_search *s = &bs->s;
 687	struct mb_cache_entry *ce = NULL;
 688	int error = 0;
 689
 690#define header(x) ((struct ext3_xattr_header *)(x))
 691
 692	if (i->value && i->value_len > sb->s_blocksize)
 693		return -ENOSPC;
 694	if (s->base) {
 695		ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
 696					bs->bh->b_blocknr);
 697		error = ext3_journal_get_write_access(handle, bs->bh);
 698		if (error)
 699			goto cleanup;
 700		lock_buffer(bs->bh);
 701
 702		if (header(s->base)->h_refcount == cpu_to_le32(1)) {
 703			if (ce) {
 704				mb_cache_entry_free(ce);
 705				ce = NULL;
 706			}
 707			ea_bdebug(bs->bh, "modifying in-place");
 708			error = ext3_xattr_set_entry(i, s);
 709			if (!error) {
 710				if (!IS_LAST_ENTRY(s->first))
 711					ext3_xattr_rehash(header(s->base),
 712							  s->here);
 713				ext3_xattr_cache_insert(bs->bh);
 714			}
 715			unlock_buffer(bs->bh);
 716			if (error == -EIO)
 717				goto bad_block;
 718			if (!error)
 719				error = ext3_journal_dirty_metadata(handle,
 720								    bs->bh);
 721			if (error)
 722				goto cleanup;
 723			goto inserted;
 724		} else {
 725			int offset = (char *)s->here - bs->bh->b_data;
 726
 727			unlock_buffer(bs->bh);
 728			journal_release_buffer(handle, bs->bh);
 729
 730			if (ce) {
 731				mb_cache_entry_release(ce);
 732				ce = NULL;
 733			}
 734			ea_bdebug(bs->bh, "cloning");
 735			s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
 736			error = -ENOMEM;
 737			if (s->base == NULL)
 738				goto cleanup;
 739			memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
 740			s->first = ENTRY(header(s->base)+1);
 741			header(s->base)->h_refcount = cpu_to_le32(1);
 742			s->here = ENTRY(s->base + offset);
 743			s->end = s->base + bs->bh->b_size;
 744		}
 745	} else {
 746		/* Allocate a buffer where we construct the new block. */
 747		s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
 748		/* assert(header == s->base) */
 749		error = -ENOMEM;
 750		if (s->base == NULL)
 751			goto cleanup;
 752		header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
 753		header(s->base)->h_blocks = cpu_to_le32(1);
 754		header(s->base)->h_refcount = cpu_to_le32(1);
 755		s->first = ENTRY(header(s->base)+1);
 756		s->here = ENTRY(header(s->base)+1);
 757		s->end = s->base + sb->s_blocksize;
 758	}
 759
 760	error = ext3_xattr_set_entry(i, s);
 761	if (error == -EIO)
 762		goto bad_block;
 763	if (error)
 764		goto cleanup;
 765	if (!IS_LAST_ENTRY(s->first))
 766		ext3_xattr_rehash(header(s->base), s->here);
 767
 768inserted:
 769	if (!IS_LAST_ENTRY(s->first)) {
 770		new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
 771		if (new_bh) {
 772			/* We found an identical block in the cache. */
 773			if (new_bh == bs->bh)
 774				ea_bdebug(new_bh, "keeping");
 775			else {
 776				/* The old block is released after updating
 777				   the inode. */
 778				error = dquot_alloc_block(inode, 1);
 779				if (error)
 780					goto cleanup;
 781				error = ext3_journal_get_write_access(handle,
 782								      new_bh);
 783				if (error)
 784					goto cleanup_dquot;
 785				lock_buffer(new_bh);
 786				le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
 787				ea_bdebug(new_bh, "reusing; refcount now=%d",
 788					le32_to_cpu(BHDR(new_bh)->h_refcount));
 789				unlock_buffer(new_bh);
 790				error = ext3_journal_dirty_metadata(handle,
 791								    new_bh);
 792				if (error)
 793					goto cleanup_dquot;
 794			}
 795			mb_cache_entry_release(ce);
 796			ce = NULL;
 797		} else if (bs->bh && s->base == bs->bh->b_data) {
 798			/* We were modifying this block in-place. */
 799			ea_bdebug(bs->bh, "keeping this block");
 800			new_bh = bs->bh;
 801			get_bh(new_bh);
 802		} else {
 803			/* We need to allocate a new block */
 804			ext3_fsblk_t goal = ext3_group_first_block_no(sb,
 805						EXT3_I(inode)->i_block_group);
 806			ext3_fsblk_t block;
 807
 808			/*
 809			 * Protect us agaist concurrent allocations to the
 810			 * same inode from ext3_..._writepage(). Reservation
 811			 * code does not expect racing allocations.
 812			 */
 813			mutex_lock(&EXT3_I(inode)->truncate_mutex);
 814			block = ext3_new_block(handle, inode, goal, &error);
 815			mutex_unlock(&EXT3_I(inode)->truncate_mutex);
 816			if (error)
 817				goto cleanup;
 818			ea_idebug(inode, "creating block %d", block);
 819
 820			new_bh = sb_getblk(sb, block);
 821			if (!new_bh) {
 822getblk_failed:
 823				ext3_free_blocks(handle, inode, block, 1);
 824				error = -EIO;
 825				goto cleanup;
 826			}
 827			lock_buffer(new_bh);
 828			error = ext3_journal_get_create_access(handle, new_bh);
 829			if (error) {
 830				unlock_buffer(new_bh);
 831				goto getblk_failed;
 832			}
 833			memcpy(new_bh->b_data, s->base, new_bh->b_size);
 834			set_buffer_uptodate(new_bh);
 835			unlock_buffer(new_bh);
 836			ext3_xattr_cache_insert(new_bh);
 837			error = ext3_journal_dirty_metadata(handle, new_bh);
 838			if (error)
 839				goto cleanup;
 840		}
 841	}
 842
 843	/* Update the inode. */
 844	EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
 845
 846	/* Drop the previous xattr block. */
 847	if (bs->bh && bs->bh != new_bh)
 848		ext3_xattr_release_block(handle, inode, bs->bh);
 849	error = 0;
 850
 851cleanup:
 852	if (ce)
 853		mb_cache_entry_release(ce);
 854	brelse(new_bh);
 855	if (!(bs->bh && s->base == bs->bh->b_data))
 856		kfree(s->base);
 857
 858	return error;
 859
 860cleanup_dquot:
 861	dquot_free_block(inode, 1);
 862	goto cleanup;
 863
 864bad_block:
 865	ext3_error(inode->i_sb, __func__,
 866		   "inode %lu: bad block "E3FSBLK, inode->i_ino,
 867		   EXT3_I(inode)->i_file_acl);
 868	goto cleanup;
 869
 870#undef header
 871}
 872
 873struct ext3_xattr_ibody_find {
 874	struct ext3_xattr_search s;
 875	struct ext3_iloc iloc;
 876};
 877
 878static int
 879ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
 880		      struct ext3_xattr_ibody_find *is)
 881{
 882	struct ext3_xattr_ibody_header *header;
 883	struct ext3_inode *raw_inode;
 884	int error;
 885
 886	if (EXT3_I(inode)->i_extra_isize == 0)
 887		return 0;
 888	raw_inode = ext3_raw_inode(&is->iloc);
 889	header = IHDR(inode, raw_inode);
 890	is->s.base = is->s.first = IFIRST(header);
 891	is->s.here = is->s.first;
 892	is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
 893	if (ext3_test_inode_state(inode, EXT3_STATE_XATTR)) {
 894		error = ext3_xattr_check_names(IFIRST(header), is->s.end);
 895		if (error)
 896			return error;
 897		/* Find the named attribute. */
 898		error = ext3_xattr_find_entry(&is->s.here, i->name_index,
 899					      i->name, is->s.end -
 900					      (void *)is->s.base, 0);
 901		if (error && error != -ENODATA)
 902			return error;
 903		is->s.not_found = error;
 904	}
 905	return 0;
 906}
 907
 908static int
 909ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
 910		     struct ext3_xattr_info *i,
 911		     struct ext3_xattr_ibody_find *is)
 912{
 913	struct ext3_xattr_ibody_header *header;
 914	struct ext3_xattr_search *s = &is->s;
 915	int error;
 916
 917	if (EXT3_I(inode)->i_extra_isize == 0)
 918		return -ENOSPC;
 919	error = ext3_xattr_set_entry(i, s);
 920	if (error)
 921		return error;
 922	header = IHDR(inode, ext3_raw_inode(&is->iloc));
 923	if (!IS_LAST_ENTRY(s->first)) {
 924		header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
 925		ext3_set_inode_state(inode, EXT3_STATE_XATTR);
 926	} else {
 927		header->h_magic = cpu_to_le32(0);
 928		ext3_clear_inode_state(inode, EXT3_STATE_XATTR);
 929	}
 930	return 0;
 931}
 932
 933/*
 934 * ext3_xattr_set_handle()
 935 *
 936 * Create, replace or remove an extended attribute for this inode.  Value
 937 * is NULL to remove an existing extended attribute, and non-NULL to
 938 * either replace an existing extended attribute, or create a new extended
 939 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
 940 * specify that an extended attribute must exist and must not exist
 941 * previous to the call, respectively.
 942 *
 943 * Returns 0, or a negative error number on failure.
 944 */
 945int
 946ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
 947		      const char *name, const void *value, size_t value_len,
 948		      int flags)
 949{
 950	struct ext3_xattr_info i = {
 951		.name_index = name_index,
 952		.name = name,
 953		.value = value,
 954		.value_len = value_len,
 955
 956	};
 957	struct ext3_xattr_ibody_find is = {
 958		.s = { .not_found = -ENODATA, },
 959	};
 960	struct ext3_xattr_block_find bs = {
 961		.s = { .not_found = -ENODATA, },
 962	};
 963	int error;
 964
 965	if (!name)
 966		return -EINVAL;
 967	if (strlen(name) > 255)
 968		return -ERANGE;
 969	down_write(&EXT3_I(inode)->xattr_sem);
 970	error = ext3_get_inode_loc(inode, &is.iloc);
 971	if (error)
 972		goto cleanup;
 973
 974	error = ext3_journal_get_write_access(handle, is.iloc.bh);
 975	if (error)
 976		goto cleanup;
 977
 978	if (ext3_test_inode_state(inode, EXT3_STATE_NEW)) {
 979		struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
 980		memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
 981		ext3_clear_inode_state(inode, EXT3_STATE_NEW);
 982	}
 983
 984	error = ext3_xattr_ibody_find(inode, &i, &is);
 985	if (error)
 986		goto cleanup;
 987	if (is.s.not_found)
 988		error = ext3_xattr_block_find(inode, &i, &bs);
 989	if (error)
 990		goto cleanup;
 991	if (is.s.not_found && bs.s.not_found) {
 992		error = -ENODATA;
 993		if (flags & XATTR_REPLACE)
 994			goto cleanup;
 995		error = 0;
 996		if (!value)
 997			goto cleanup;
 998	} else {
 999		error = -EEXIST;
1000		if (flags & XATTR_CREATE)
1001			goto cleanup;
1002	}
1003	if (!value) {
1004		if (!is.s.not_found)
1005			error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1006		else if (!bs.s.not_found)
1007			error = ext3_xattr_block_set(handle, inode, &i, &bs);
1008	} else {
1009		error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1010		if (!error && !bs.s.not_found) {
1011			i.value = NULL;
1012			error = ext3_xattr_block_set(handle, inode, &i, &bs);
1013		} else if (error == -ENOSPC) {
1014			if (EXT3_I(inode)->i_file_acl && !bs.s.base) {
1015				error = ext3_xattr_block_find(inode, &i, &bs);
1016				if (error)
1017					goto cleanup;
1018			}
1019			error = ext3_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 = ext3_xattr_ibody_set(handle, inode, &i,
1025							     &is);
1026			}
1027		}
1028	}
1029	if (!error) {
1030		ext3_xattr_update_super_block(handle, inode->i_sb);
1031		inode->i_ctime = CURRENT_TIME_SEC;
1032		error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
1033		/*
1034		 * The bh is consumed by ext3_mark_iloc_dirty, even with
1035		 * error != 0.
1036		 */
1037		is.iloc.bh = NULL;
1038		if (IS_SYNC(inode))
1039			handle->h_sync = 1;
1040	}
1041
1042cleanup:
1043	brelse(is.iloc.bh);
1044	brelse(bs.bh);
1045	up_write(&EXT3_I(inode)->xattr_sem);
1046	return error;
1047}
1048
1049/*
1050 * ext3_xattr_set()
1051 *
1052 * Like ext3_xattr_set_handle, but start from an inode. This extended
1053 * attribute modification is a filesystem transaction by itself.
1054 *
1055 * Returns 0, or a negative error number on failure.
1056 */
1057int
1058ext3_xattr_set(struct inode *inode, int name_index, const char *name,
1059	       const void *value, size_t value_len, int flags)
1060{
1061	handle_t *handle;
1062	int error, retries = 0;
1063
1064retry:
1065	handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
1066	if (IS_ERR(handle)) {
1067		error = PTR_ERR(handle);
1068	} else {
1069		int error2;
1070
1071		error = ext3_xattr_set_handle(handle, inode, name_index, name,
1072					      value, value_len, flags);
1073		error2 = ext3_journal_stop(handle);
1074		if (error == -ENOSPC &&
1075		    ext3_should_retry_alloc(inode->i_sb, &retries))
1076			goto retry;
1077		if (error == 0)
1078			error = error2;
1079	}
1080
1081	return error;
1082}
1083
1084/*
1085 * ext3_xattr_delete_inode()
1086 *
1087 * Free extended attribute resources associated with this inode. This
1088 * is called immediately before an inode is freed. We have exclusive
1089 * access to the inode.
1090 */
1091void
1092ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
1093{
1094	struct buffer_head *bh = NULL;
1095
1096	if (!EXT3_I(inode)->i_file_acl)
1097		goto cleanup;
1098	bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
1099	if (!bh) {
1100		ext3_error(inode->i_sb, __func__,
1101			"inode %lu: block "E3FSBLK" read error", inode->i_ino,
1102			EXT3_I(inode)->i_file_acl);
1103		goto cleanup;
1104	}
1105	if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
1106	    BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1107		ext3_error(inode->i_sb, __func__,
1108			"inode %lu: bad block "E3FSBLK, inode->i_ino,
1109			EXT3_I(inode)->i_file_acl);
1110		goto cleanup;
1111	}
1112	ext3_xattr_release_block(handle, inode, bh);
1113	EXT3_I(inode)->i_file_acl = 0;
1114
1115cleanup:
1116	brelse(bh);
1117}
1118
1119/*
1120 * ext3_xattr_put_super()
1121 *
1122 * This is called when a file system is unmounted.
1123 */
1124void
1125ext3_xattr_put_super(struct super_block *sb)
1126{
1127	mb_cache_shrink(sb->s_bdev);
1128}
1129
1130/*
1131 * ext3_xattr_cache_insert()
1132 *
1133 * Create a new entry in the extended attribute cache, and insert
1134 * it unless such an entry is already in the cache.
1135 *
1136 * Returns 0, or a negative error number on failure.
1137 */
1138static void
1139ext3_xattr_cache_insert(struct buffer_head *bh)
1140{
1141	__u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1142	struct mb_cache_entry *ce;
1143	int error;
1144
1145	ce = mb_cache_entry_alloc(ext3_xattr_cache, GFP_NOFS);
1146	if (!ce) {
1147		ea_bdebug(bh, "out of memory");
1148		return;
1149	}
1150	error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1151	if (error) {
1152		mb_cache_entry_free(ce);
1153		if (error == -EBUSY) {
1154			ea_bdebug(bh, "already in cache");
1155			error = 0;
1156		}
1157	} else {
1158		ea_bdebug(bh, "inserting [%x]", (int)hash);
1159		mb_cache_entry_release(ce);
1160	}
1161}
1162
1163/*
1164 * ext3_xattr_cmp()
1165 *
1166 * Compare two extended attribute blocks for equality.
1167 *
1168 * Returns 0 if the blocks are equal, 1 if they differ, and
1169 * a negative error number on errors.
1170 */
1171static int
1172ext3_xattr_cmp(struct ext3_xattr_header *header1,
1173	       struct ext3_xattr_header *header2)
1174{
1175	struct ext3_xattr_entry *entry1, *entry2;
1176
1177	entry1 = ENTRY(header1+1);
1178	entry2 = ENTRY(header2+1);
1179	while (!IS_LAST_ENTRY(entry1)) {
1180		if (IS_LAST_ENTRY(entry2))
1181			return 1;
1182		if (entry1->e_hash != entry2->e_hash ||
1183		    entry1->e_name_index != entry2->e_name_index ||
1184		    entry1->e_name_len != entry2->e_name_len ||
1185		    entry1->e_value_size != entry2->e_value_size ||
1186		    memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1187			return 1;
1188		if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1189			return -EIO;
1190		if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1191			   (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1192			   le32_to_cpu(entry1->e_value_size)))
1193			return 1;
1194
1195		entry1 = EXT3_XATTR_NEXT(entry1);
1196		entry2 = EXT3_XATTR_NEXT(entry2);
1197	}
1198	if (!IS_LAST_ENTRY(entry2))
1199		return 1;
1200	return 0;
1201}
1202
1203/*
1204 * ext3_xattr_cache_find()
1205 *
1206 * Find an identical extended attribute block.
1207 *
1208 * Returns a pointer to the block found, or NULL if such a block was
1209 * not found or an error occurred.
1210 */
1211static struct buffer_head *
1212ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
1213		      struct mb_cache_entry **pce)
1214{
1215	__u32 hash = le32_to_cpu(header->h_hash);
1216	struct mb_cache_entry *ce;
1217
1218	if (!header->h_hash)
1219		return NULL;  /* never share */
1220	ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1221again:
1222	ce = mb_cache_entry_find_first(ext3_xattr_cache, inode->i_sb->s_bdev,
1223				       hash);
1224	while (ce) {
1225		struct buffer_head *bh;
1226
1227		if (IS_ERR(ce)) {
1228			if (PTR_ERR(ce) == -EAGAIN)
1229				goto again;
1230			break;
1231		}
1232		bh = sb_bread(inode->i_sb, ce->e_block);
1233		if (!bh) {
1234			ext3_error(inode->i_sb, __func__,
1235				"inode %lu: block %lu read error",
1236				inode->i_ino, (unsigned long) ce->e_block);
1237		} else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1238				EXT3_XATTR_REFCOUNT_MAX) {
1239			ea_idebug(inode, "block %lu refcount %d>=%d",
1240				  (unsigned long) ce->e_block,
1241				  le32_to_cpu(BHDR(bh)->h_refcount),
1242					  EXT3_XATTR_REFCOUNT_MAX);
1243		} else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
1244			*pce = ce;
1245			return bh;
1246		}
1247		brelse(bh);
1248		ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1249	}
1250	return NULL;
1251}
1252
1253#define NAME_HASH_SHIFT 5
1254#define VALUE_HASH_SHIFT 16
1255
1256/*
1257 * ext3_xattr_hash_entry()
1258 *
1259 * Compute the hash of an extended attribute.
1260 */
1261static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1262					 struct ext3_xattr_entry *entry)
1263{
1264	__u32 hash = 0;
1265	char *name = entry->e_name;
1266	int n;
1267
1268	for (n=0; n < entry->e_name_len; n++) {
1269		hash = (hash << NAME_HASH_SHIFT) ^
1270		       (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1271		       *name++;
1272	}
1273
1274	if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1275		__le32 *value = (__le32 *)((char *)header +
1276			le16_to_cpu(entry->e_value_offs));
1277		for (n = (le32_to_cpu(entry->e_value_size) +
1278		     EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1279			hash = (hash << VALUE_HASH_SHIFT) ^
1280			       (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1281			       le32_to_cpu(*value++);
1282		}
1283	}
1284	entry->e_hash = cpu_to_le32(hash);
1285}
1286
1287#undef NAME_HASH_SHIFT
1288#undef VALUE_HASH_SHIFT
1289
1290#define BLOCK_HASH_SHIFT 16
1291
1292/*
1293 * ext3_xattr_rehash()
1294 *
1295 * Re-compute the extended attribute hash value after an entry has changed.
1296 */
1297static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1298			      struct ext3_xattr_entry *entry)
1299{
1300	struct ext3_xattr_entry *here;
1301	__u32 hash = 0;
1302
1303	ext3_xattr_hash_entry(header, entry);
1304	here = ENTRY(header+1);
1305	while (!IS_LAST_ENTRY(here)) {
1306		if (!here->e_hash) {
1307			/* Block is not shared if an entry's hash value == 0 */
1308			hash = 0;
1309			break;
1310		}
1311		hash = (hash << BLOCK_HASH_SHIFT) ^
1312		       (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1313		       le32_to_cpu(here->e_hash);
1314		here = EXT3_XATTR_NEXT(here);
1315	}
1316	header->h_hash = cpu_to_le32(hash);
1317}
1318
1319#undef BLOCK_HASH_SHIFT
1320
1321int __init
1322init_ext3_xattr(void)
1323{
1324	ext3_xattr_cache = mb_cache_create("ext3_xattr", 6);
1325	if (!ext3_xattr_cache)
1326		return -ENOMEM;
1327	return 0;
1328}
1329
1330void
1331exit_ext3_xattr(void)
1332{
1333	if (ext3_xattr_cache)
1334		mb_cache_destroy(ext3_xattr_cache);
1335	ext3_xattr_cache = NULL;
1336}