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