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