xref: /openbmc/linux/fs/ext4/xattr.c (revision 3478c83c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/ext4/xattr.c
4  *
5  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6  *
7  * Fix by Harrison Xing <harrison@mountainviewdata.com>.
8  * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
9  * Extended attributes for symlinks and special files added per
10  *  suggestion of Luka Renko <luka.renko@hermes.si>.
11  * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
12  *  Red Hat Inc.
13  * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
14  *  and Andreas Gruenbacher <agruen@suse.de>.
15  */
16 
17 /*
18  * Extended attributes are stored directly in inodes (on file systems with
19  * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
20  * field contains the block number if an inode uses an additional block. All
21  * attributes must fit in the inode and one additional block. Blocks that
22  * contain the identical set of attributes may be shared among several inodes.
23  * Identical blocks are detected by keeping a cache of blocks that have
24  * recently been accessed.
25  *
26  * The attributes in inodes and on blocks have a different header; the entries
27  * are stored in the same format:
28  *
29  *   +------------------+
30  *   | header           |
31  *   | entry 1          | |
32  *   | entry 2          | | growing downwards
33  *   | entry 3          | v
34  *   | four null bytes  |
35  *   | . . .            |
36  *   | value 1          | ^
37  *   | value 3          | | growing upwards
38  *   | value 2          | |
39  *   +------------------+
40  *
41  * The header is followed by multiple entry descriptors. In disk blocks, the
42  * entry descriptors are kept sorted. In inodes, they are unsorted. The
43  * attribute values are aligned to the end of the block in no specific order.
44  *
45  * Locking strategy
46  * ----------------
47  * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
48  * EA blocks are only changed if they are exclusive to an inode, so
49  * holding xattr_sem also means that nothing but the EA block's reference
50  * count can change. Multiple writers to the same block are synchronized
51  * by the buffer lock.
52  */
53 
54 #include <linux/init.h>
55 #include <linux/fs.h>
56 #include <linux/slab.h>
57 #include <linux/mbcache.h>
58 #include <linux/quotaops.h>
59 #include <linux/iversion.h>
60 #include "ext4_jbd2.h"
61 #include "ext4.h"
62 #include "xattr.h"
63 #include "acl.h"
64 
65 #ifdef EXT4_XATTR_DEBUG
66 # define ea_idebug(inode, fmt, ...)					\
67 	printk(KERN_DEBUG "inode %s:%lu: " fmt "\n",			\
68 	       inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
69 # define ea_bdebug(bh, fmt, ...)					\
70 	printk(KERN_DEBUG "block %pg:%lu: " fmt "\n",			\
71 	       bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
72 #else
73 # define ea_idebug(inode, fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
74 # define ea_bdebug(bh, fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
75 #endif
76 
77 static void ext4_xattr_block_cache_insert(struct mb_cache *,
78 					  struct buffer_head *);
79 static struct buffer_head *
80 ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
81 			    struct mb_cache_entry **);
82 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
83 				    size_t value_count);
84 static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value,
85 				    size_t value_count);
86 static void ext4_xattr_rehash(struct ext4_xattr_header *);
87 
88 static const struct xattr_handler * const ext4_xattr_handler_map[] = {
89 	[EXT4_XATTR_INDEX_USER]		     = &ext4_xattr_user_handler,
90 #ifdef CONFIG_EXT4_FS_POSIX_ACL
91 	[EXT4_XATTR_INDEX_POSIX_ACL_ACCESS]  = &posix_acl_access_xattr_handler,
92 	[EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
93 #endif
94 	[EXT4_XATTR_INDEX_TRUSTED]	     = &ext4_xattr_trusted_handler,
95 #ifdef CONFIG_EXT4_FS_SECURITY
96 	[EXT4_XATTR_INDEX_SECURITY]	     = &ext4_xattr_security_handler,
97 #endif
98 	[EXT4_XATTR_INDEX_HURD]		     = &ext4_xattr_hurd_handler,
99 };
100 
101 const struct xattr_handler *ext4_xattr_handlers[] = {
102 	&ext4_xattr_user_handler,
103 	&ext4_xattr_trusted_handler,
104 #ifdef CONFIG_EXT4_FS_POSIX_ACL
105 	&posix_acl_access_xattr_handler,
106 	&posix_acl_default_xattr_handler,
107 #endif
108 #ifdef CONFIG_EXT4_FS_SECURITY
109 	&ext4_xattr_security_handler,
110 #endif
111 	&ext4_xattr_hurd_handler,
112 	NULL
113 };
114 
115 #define EA_BLOCK_CACHE(inode)	(((struct ext4_sb_info *) \
116 				inode->i_sb->s_fs_info)->s_ea_block_cache)
117 
118 #define EA_INODE_CACHE(inode)	(((struct ext4_sb_info *) \
119 				inode->i_sb->s_fs_info)->s_ea_inode_cache)
120 
121 static int
122 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
123 			struct inode *inode);
124 
125 #ifdef CONFIG_LOCKDEP
126 void ext4_xattr_inode_set_class(struct inode *ea_inode)
127 {
128 	lockdep_set_subclass(&ea_inode->i_rwsem, 1);
129 }
130 #endif
131 
132 static __le32 ext4_xattr_block_csum(struct inode *inode,
133 				    sector_t block_nr,
134 				    struct ext4_xattr_header *hdr)
135 {
136 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
137 	__u32 csum;
138 	__le64 dsk_block_nr = cpu_to_le64(block_nr);
139 	__u32 dummy_csum = 0;
140 	int offset = offsetof(struct ext4_xattr_header, h_checksum);
141 
142 	csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
143 			   sizeof(dsk_block_nr));
144 	csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
145 	csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
146 	offset += sizeof(dummy_csum);
147 	csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
148 			   EXT4_BLOCK_SIZE(inode->i_sb) - offset);
149 
150 	return cpu_to_le32(csum);
151 }
152 
153 static int ext4_xattr_block_csum_verify(struct inode *inode,
154 					struct buffer_head *bh)
155 {
156 	struct ext4_xattr_header *hdr = BHDR(bh);
157 	int ret = 1;
158 
159 	if (ext4_has_metadata_csum(inode->i_sb)) {
160 		lock_buffer(bh);
161 		ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
162 							bh->b_blocknr, hdr));
163 		unlock_buffer(bh);
164 	}
165 	return ret;
166 }
167 
168 static void ext4_xattr_block_csum_set(struct inode *inode,
169 				      struct buffer_head *bh)
170 {
171 	if (ext4_has_metadata_csum(inode->i_sb))
172 		BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
173 						bh->b_blocknr, BHDR(bh));
174 }
175 
176 static inline const struct xattr_handler *
177 ext4_xattr_handler(int name_index)
178 {
179 	const struct xattr_handler *handler = NULL;
180 
181 	if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
182 		handler = ext4_xattr_handler_map[name_index];
183 	return handler;
184 }
185 
186 static int
187 check_xattrs(struct inode *inode, struct buffer_head *bh,
188 	     struct ext4_xattr_entry *entry, void *end, void *value_start,
189 	     const char *function, unsigned int line)
190 {
191 	struct ext4_xattr_entry *e = entry;
192 	int err = -EFSCORRUPTED;
193 	char *err_str;
194 
195 	if (bh) {
196 		if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
197 		    BHDR(bh)->h_blocks != cpu_to_le32(1)) {
198 			err_str = "invalid header";
199 			goto errout;
200 		}
201 		if (buffer_verified(bh))
202 			return 0;
203 		if (!ext4_xattr_block_csum_verify(inode, bh)) {
204 			err = -EFSBADCRC;
205 			err_str = "invalid checksum";
206 			goto errout;
207 		}
208 	} else {
209 		struct ext4_xattr_ibody_header *header = value_start;
210 
211 		header -= 1;
212 		if (end - (void *)header < sizeof(*header) + sizeof(u32)) {
213 			err_str = "in-inode xattr block too small";
214 			goto errout;
215 		}
216 		if (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
217 			err_str = "bad magic number in in-inode xattr";
218 			goto errout;
219 		}
220 	}
221 
222 	/* Find the end of the names list */
223 	while (!IS_LAST_ENTRY(e)) {
224 		struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
225 		if ((void *)next >= end) {
226 			err_str = "e_name out of bounds";
227 			goto errout;
228 		}
229 		if (strnlen(e->e_name, e->e_name_len) != e->e_name_len) {
230 			err_str = "bad e_name length";
231 			goto errout;
232 		}
233 		e = next;
234 	}
235 
236 	/* Check the values */
237 	while (!IS_LAST_ENTRY(entry)) {
238 		u32 size = le32_to_cpu(entry->e_value_size);
239 		unsigned long ea_ino = le32_to_cpu(entry->e_value_inum);
240 
241 		if (!ext4_has_feature_ea_inode(inode->i_sb) && ea_ino) {
242 			err_str = "ea_inode specified without ea_inode feature enabled";
243 			goto errout;
244 		}
245 		if (ea_ino && ((ea_ino == EXT4_ROOT_INO) ||
246 			       !ext4_valid_inum(inode->i_sb, ea_ino))) {
247 			err_str = "invalid ea_ino";
248 			goto errout;
249 		}
250 		if (size > EXT4_XATTR_SIZE_MAX) {
251 			err_str = "e_value size too large";
252 			goto errout;
253 		}
254 
255 		if (size != 0 && entry->e_value_inum == 0) {
256 			u16 offs = le16_to_cpu(entry->e_value_offs);
257 			void *value;
258 
259 			/*
260 			 * The value cannot overlap the names, and the value
261 			 * with padding cannot extend beyond 'end'.  Check both
262 			 * the padded and unpadded sizes, since the size may
263 			 * overflow to 0 when adding padding.
264 			 */
265 			if (offs > end - value_start) {
266 				err_str = "e_value out of bounds";
267 				goto errout;
268 			}
269 			value = value_start + offs;
270 			if (value < (void *)e + sizeof(u32) ||
271 			    size > end - value ||
272 			    EXT4_XATTR_SIZE(size) > end - value) {
273 				err_str = "overlapping e_value ";
274 				goto errout;
275 			}
276 		}
277 		entry = EXT4_XATTR_NEXT(entry);
278 	}
279 	if (bh)
280 		set_buffer_verified(bh);
281 	return 0;
282 
283 errout:
284 	if (bh)
285 		__ext4_error_inode(inode, function, line, 0, -err,
286 				   "corrupted xattr block %llu: %s",
287 				   (unsigned long long) bh->b_blocknr,
288 				   err_str);
289 	else
290 		__ext4_error_inode(inode, function, line, 0, -err,
291 				   "corrupted in-inode xattr: %s", err_str);
292 	return err;
293 }
294 
295 static inline int
296 __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
297 			 const char *function, unsigned int line)
298 {
299 	return check_xattrs(inode, bh, BFIRST(bh), bh->b_data + bh->b_size,
300 			    bh->b_data, function, line);
301 }
302 
303 #define ext4_xattr_check_block(inode, bh) \
304 	__ext4_xattr_check_block((inode), (bh),  __func__, __LINE__)
305 
306 
307 static inline int
308 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
309 			 void *end, const char *function, unsigned int line)
310 {
311 	return check_xattrs(inode, NULL, IFIRST(header), end, IFIRST(header),
312 			    function, line);
313 }
314 
315 #define xattr_check_inode(inode, header, end) \
316 	__xattr_check_inode((inode), (header), (end), __func__, __LINE__)
317 
318 static int
319 xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
320 		 void *end, int name_index, const char *name, int sorted)
321 {
322 	struct ext4_xattr_entry *entry, *next;
323 	size_t name_len;
324 	int cmp = 1;
325 
326 	if (name == NULL)
327 		return -EINVAL;
328 	name_len = strlen(name);
329 	for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
330 		next = EXT4_XATTR_NEXT(entry);
331 		if ((void *) next >= end) {
332 			EXT4_ERROR_INODE(inode, "corrupted xattr entries");
333 			return -EFSCORRUPTED;
334 		}
335 		cmp = name_index - entry->e_name_index;
336 		if (!cmp)
337 			cmp = name_len - entry->e_name_len;
338 		if (!cmp)
339 			cmp = memcmp(name, entry->e_name, name_len);
340 		if (cmp <= 0 && (sorted || cmp == 0))
341 			break;
342 	}
343 	*pentry = entry;
344 	return cmp ? -ENODATA : 0;
345 }
346 
347 static u32
348 ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
349 {
350 	return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
351 }
352 
353 static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
354 {
355 	return ((u64)ea_inode->i_ctime.tv_sec << 32) |
356 		(u32) inode_peek_iversion_raw(ea_inode);
357 }
358 
359 static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
360 {
361 	ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
362 	inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
363 }
364 
365 static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
366 {
367 	return (u32)ea_inode->i_atime.tv_sec;
368 }
369 
370 static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
371 {
372 	ea_inode->i_atime.tv_sec = hash;
373 }
374 
375 /*
376  * Read the EA value from an inode.
377  */
378 static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
379 {
380 	int blocksize = 1 << ea_inode->i_blkbits;
381 	int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
382 	int tail_size = (size % blocksize) ?: blocksize;
383 	struct buffer_head *bhs_inline[8];
384 	struct buffer_head **bhs = bhs_inline;
385 	int i, ret;
386 
387 	if (bh_count > ARRAY_SIZE(bhs_inline)) {
388 		bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
389 		if (!bhs)
390 			return -ENOMEM;
391 	}
392 
393 	ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
394 			       true /* wait */, bhs);
395 	if (ret)
396 		goto free_bhs;
397 
398 	for (i = 0; i < bh_count; i++) {
399 		/* There shouldn't be any holes in ea_inode. */
400 		if (!bhs[i]) {
401 			ret = -EFSCORRUPTED;
402 			goto put_bhs;
403 		}
404 		memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
405 		       i < bh_count - 1 ? blocksize : tail_size);
406 	}
407 	ret = 0;
408 put_bhs:
409 	for (i = 0; i < bh_count; i++)
410 		brelse(bhs[i]);
411 free_bhs:
412 	if (bhs != bhs_inline)
413 		kfree(bhs);
414 	return ret;
415 }
416 
417 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
418 
419 static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
420 				 u32 ea_inode_hash, struct inode **ea_inode)
421 {
422 	struct inode *inode;
423 	int err;
424 
425 	inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_NORMAL);
426 	if (IS_ERR(inode)) {
427 		err = PTR_ERR(inode);
428 		ext4_error(parent->i_sb,
429 			   "error while reading EA inode %lu err=%d", ea_ino,
430 			   err);
431 		return err;
432 	}
433 
434 	if (is_bad_inode(inode)) {
435 		ext4_error(parent->i_sb,
436 			   "error while reading EA inode %lu is_bad_inode",
437 			   ea_ino);
438 		err = -EIO;
439 		goto error;
440 	}
441 
442 	if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
443 		ext4_error(parent->i_sb,
444 			   "EA inode %lu does not have EXT4_EA_INODE_FL flag",
445 			    ea_ino);
446 		err = -EINVAL;
447 		goto error;
448 	}
449 
450 	ext4_xattr_inode_set_class(inode);
451 
452 	/*
453 	 * Check whether this is an old Lustre-style xattr inode. Lustre
454 	 * implementation does not have hash validation, rather it has a
455 	 * backpointer from ea_inode to the parent inode.
456 	 */
457 	if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
458 	    EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
459 	    inode->i_generation == parent->i_generation) {
460 		ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
461 		ext4_xattr_inode_set_ref(inode, 1);
462 	} else {
463 		inode_lock(inode);
464 		inode->i_flags |= S_NOQUOTA;
465 		inode_unlock(inode);
466 	}
467 
468 	*ea_inode = inode;
469 	return 0;
470 error:
471 	iput(inode);
472 	return err;
473 }
474 
475 /* Remove entry from mbcache when EA inode is getting evicted */
476 void ext4_evict_ea_inode(struct inode *inode)
477 {
478 	struct mb_cache_entry *oe;
479 
480 	if (!EA_INODE_CACHE(inode))
481 		return;
482 	/* Wait for entry to get unused so that we can remove it */
483 	while ((oe = mb_cache_entry_delete_or_get(EA_INODE_CACHE(inode),
484 			ext4_xattr_inode_get_hash(inode), inode->i_ino))) {
485 		mb_cache_entry_wait_unused(oe);
486 		mb_cache_entry_put(EA_INODE_CACHE(inode), oe);
487 	}
488 }
489 
490 static int
491 ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
492 			       struct ext4_xattr_entry *entry, void *buffer,
493 			       size_t size)
494 {
495 	u32 hash;
496 
497 	/* Verify stored hash matches calculated hash. */
498 	hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
499 	if (hash != ext4_xattr_inode_get_hash(ea_inode))
500 		return -EFSCORRUPTED;
501 
502 	if (entry) {
503 		__le32 e_hash, tmp_data;
504 
505 		/* Verify entry hash. */
506 		tmp_data = cpu_to_le32(hash);
507 		e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
508 					       &tmp_data, 1);
509 		/* All good? */
510 		if (e_hash == entry->e_hash)
511 			return 0;
512 
513 		/*
514 		 * Not good. Maybe the entry hash was calculated
515 		 * using the buggy signed char version?
516 		 */
517 		e_hash = ext4_xattr_hash_entry_signed(entry->e_name, entry->e_name_len,
518 							&tmp_data, 1);
519 		if (e_hash == entry->e_hash)
520 			return 0;
521 
522 		/* Still no match - bad */
523 		return -EFSCORRUPTED;
524 	}
525 	return 0;
526 }
527 
528 /*
529  * Read xattr value from the EA inode.
530  */
531 static int
532 ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
533 		     void *buffer, size_t size)
534 {
535 	struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
536 	struct inode *ea_inode;
537 	int err;
538 
539 	err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
540 				    le32_to_cpu(entry->e_hash), &ea_inode);
541 	if (err) {
542 		ea_inode = NULL;
543 		goto out;
544 	}
545 
546 	if (i_size_read(ea_inode) != size) {
547 		ext4_warning_inode(ea_inode,
548 				   "ea_inode file size=%llu entry size=%zu",
549 				   i_size_read(ea_inode), size);
550 		err = -EFSCORRUPTED;
551 		goto out;
552 	}
553 
554 	err = ext4_xattr_inode_read(ea_inode, buffer, size);
555 	if (err)
556 		goto out;
557 
558 	if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
559 		err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
560 						     size);
561 		if (err) {
562 			ext4_warning_inode(ea_inode,
563 					   "EA inode hash validation failed");
564 			goto out;
565 		}
566 
567 		if (ea_inode_cache)
568 			mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
569 					ext4_xattr_inode_get_hash(ea_inode),
570 					ea_inode->i_ino, true /* reusable */);
571 	}
572 out:
573 	iput(ea_inode);
574 	return err;
575 }
576 
577 static int
578 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
579 		     void *buffer, size_t buffer_size)
580 {
581 	struct buffer_head *bh = NULL;
582 	struct ext4_xattr_entry *entry;
583 	size_t size;
584 	void *end;
585 	int error;
586 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
587 
588 	ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
589 		  name_index, name, buffer, (long)buffer_size);
590 
591 	if (!EXT4_I(inode)->i_file_acl)
592 		return -ENODATA;
593 	ea_idebug(inode, "reading block %llu",
594 		  (unsigned long long)EXT4_I(inode)->i_file_acl);
595 	bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
596 	if (IS_ERR(bh))
597 		return PTR_ERR(bh);
598 	ea_bdebug(bh, "b_count=%d, refcount=%d",
599 		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
600 	error = ext4_xattr_check_block(inode, bh);
601 	if (error)
602 		goto cleanup;
603 	ext4_xattr_block_cache_insert(ea_block_cache, bh);
604 	entry = BFIRST(bh);
605 	end = bh->b_data + bh->b_size;
606 	error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
607 	if (error)
608 		goto cleanup;
609 	size = le32_to_cpu(entry->e_value_size);
610 	error = -ERANGE;
611 	if (unlikely(size > EXT4_XATTR_SIZE_MAX))
612 		goto cleanup;
613 	if (buffer) {
614 		if (size > buffer_size)
615 			goto cleanup;
616 		if (entry->e_value_inum) {
617 			error = ext4_xattr_inode_get(inode, entry, buffer,
618 						     size);
619 			if (error)
620 				goto cleanup;
621 		} else {
622 			u16 offset = le16_to_cpu(entry->e_value_offs);
623 			void *p = bh->b_data + offset;
624 
625 			if (unlikely(p + size > end))
626 				goto cleanup;
627 			memcpy(buffer, p, size);
628 		}
629 	}
630 	error = size;
631 
632 cleanup:
633 	brelse(bh);
634 	return error;
635 }
636 
637 int
638 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
639 		     void *buffer, size_t buffer_size)
640 {
641 	struct ext4_xattr_ibody_header *header;
642 	struct ext4_xattr_entry *entry;
643 	struct ext4_inode *raw_inode;
644 	struct ext4_iloc iloc;
645 	size_t size;
646 	void *end;
647 	int error;
648 
649 	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
650 		return -ENODATA;
651 	error = ext4_get_inode_loc(inode, &iloc);
652 	if (error)
653 		return error;
654 	raw_inode = ext4_raw_inode(&iloc);
655 	header = IHDR(inode, raw_inode);
656 	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
657 	error = xattr_check_inode(inode, header, end);
658 	if (error)
659 		goto cleanup;
660 	entry = IFIRST(header);
661 	error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
662 	if (error)
663 		goto cleanup;
664 	size = le32_to_cpu(entry->e_value_size);
665 	error = -ERANGE;
666 	if (unlikely(size > EXT4_XATTR_SIZE_MAX))
667 		goto cleanup;
668 	if (buffer) {
669 		if (size > buffer_size)
670 			goto cleanup;
671 		if (entry->e_value_inum) {
672 			error = ext4_xattr_inode_get(inode, entry, buffer,
673 						     size);
674 			if (error)
675 				goto cleanup;
676 		} else {
677 			u16 offset = le16_to_cpu(entry->e_value_offs);
678 			void *p = (void *)IFIRST(header) + offset;
679 
680 			if (unlikely(p + size > end))
681 				goto cleanup;
682 			memcpy(buffer, p, size);
683 		}
684 	}
685 	error = size;
686 
687 cleanup:
688 	brelse(iloc.bh);
689 	return error;
690 }
691 
692 /*
693  * ext4_xattr_get()
694  *
695  * Copy an extended attribute into the buffer
696  * provided, or compute the buffer size required.
697  * Buffer is NULL to compute the size of the buffer required.
698  *
699  * Returns a negative error number on failure, or the number of bytes
700  * used / required on success.
701  */
702 int
703 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
704 	       void *buffer, size_t buffer_size)
705 {
706 	int error;
707 
708 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
709 		return -EIO;
710 
711 	if (strlen(name) > 255)
712 		return -ERANGE;
713 
714 	down_read(&EXT4_I(inode)->xattr_sem);
715 	error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
716 				     buffer_size);
717 	if (error == -ENODATA)
718 		error = ext4_xattr_block_get(inode, name_index, name, buffer,
719 					     buffer_size);
720 	up_read(&EXT4_I(inode)->xattr_sem);
721 	return error;
722 }
723 
724 static int
725 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
726 			char *buffer, size_t buffer_size)
727 {
728 	size_t rest = buffer_size;
729 
730 	for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
731 		const struct xattr_handler *handler =
732 			ext4_xattr_handler(entry->e_name_index);
733 
734 		if (handler && (!handler->list || handler->list(dentry))) {
735 			const char *prefix = handler->prefix ?: handler->name;
736 			size_t prefix_len = strlen(prefix);
737 			size_t size = prefix_len + entry->e_name_len + 1;
738 
739 			if (buffer) {
740 				if (size > rest)
741 					return -ERANGE;
742 				memcpy(buffer, prefix, prefix_len);
743 				buffer += prefix_len;
744 				memcpy(buffer, entry->e_name, entry->e_name_len);
745 				buffer += entry->e_name_len;
746 				*buffer++ = 0;
747 			}
748 			rest -= size;
749 		}
750 	}
751 	return buffer_size - rest;  /* total size */
752 }
753 
754 static int
755 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
756 {
757 	struct inode *inode = d_inode(dentry);
758 	struct buffer_head *bh = NULL;
759 	int error;
760 
761 	ea_idebug(inode, "buffer=%p, buffer_size=%ld",
762 		  buffer, (long)buffer_size);
763 
764 	if (!EXT4_I(inode)->i_file_acl)
765 		return 0;
766 	ea_idebug(inode, "reading block %llu",
767 		  (unsigned long long)EXT4_I(inode)->i_file_acl);
768 	bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
769 	if (IS_ERR(bh))
770 		return PTR_ERR(bh);
771 	ea_bdebug(bh, "b_count=%d, refcount=%d",
772 		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
773 	error = ext4_xattr_check_block(inode, bh);
774 	if (error)
775 		goto cleanup;
776 	ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
777 	error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer,
778 					buffer_size);
779 cleanup:
780 	brelse(bh);
781 	return error;
782 }
783 
784 static int
785 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
786 {
787 	struct inode *inode = d_inode(dentry);
788 	struct ext4_xattr_ibody_header *header;
789 	struct ext4_inode *raw_inode;
790 	struct ext4_iloc iloc;
791 	void *end;
792 	int error;
793 
794 	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
795 		return 0;
796 	error = ext4_get_inode_loc(inode, &iloc);
797 	if (error)
798 		return error;
799 	raw_inode = ext4_raw_inode(&iloc);
800 	header = IHDR(inode, raw_inode);
801 	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
802 	error = xattr_check_inode(inode, header, end);
803 	if (error)
804 		goto cleanup;
805 	error = ext4_xattr_list_entries(dentry, IFIRST(header),
806 					buffer, buffer_size);
807 
808 cleanup:
809 	brelse(iloc.bh);
810 	return error;
811 }
812 
813 /*
814  * Inode operation listxattr()
815  *
816  * d_inode(dentry)->i_rwsem: don't care
817  *
818  * Copy a list of attribute names into the buffer
819  * provided, or compute the buffer size required.
820  * Buffer is NULL to compute the size of the buffer required.
821  *
822  * Returns a negative error number on failure, or the number of bytes
823  * used / required on success.
824  */
825 ssize_t
826 ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
827 {
828 	int ret, ret2;
829 
830 	down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
831 	ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
832 	if (ret < 0)
833 		goto errout;
834 	if (buffer) {
835 		buffer += ret;
836 		buffer_size -= ret;
837 	}
838 	ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
839 	if (ret < 0)
840 		goto errout;
841 	ret += ret2;
842 errout:
843 	up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
844 	return ret;
845 }
846 
847 /*
848  * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
849  * not set, set it.
850  */
851 static void ext4_xattr_update_super_block(handle_t *handle,
852 					  struct super_block *sb)
853 {
854 	if (ext4_has_feature_xattr(sb))
855 		return;
856 
857 	BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
858 	if (ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh,
859 					  EXT4_JTR_NONE) == 0) {
860 		lock_buffer(EXT4_SB(sb)->s_sbh);
861 		ext4_set_feature_xattr(sb);
862 		ext4_superblock_csum_set(sb);
863 		unlock_buffer(EXT4_SB(sb)->s_sbh);
864 		ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
865 	}
866 }
867 
868 int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
869 {
870 	struct ext4_iloc iloc = { .bh = NULL };
871 	struct buffer_head *bh = NULL;
872 	struct ext4_inode *raw_inode;
873 	struct ext4_xattr_ibody_header *header;
874 	struct ext4_xattr_entry *entry;
875 	qsize_t ea_inode_refs = 0;
876 	void *end;
877 	int ret;
878 
879 	lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
880 
881 	if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
882 		ret = ext4_get_inode_loc(inode, &iloc);
883 		if (ret)
884 			goto out;
885 		raw_inode = ext4_raw_inode(&iloc);
886 		header = IHDR(inode, raw_inode);
887 		end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
888 		ret = xattr_check_inode(inode, header, end);
889 		if (ret)
890 			goto out;
891 
892 		for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
893 		     entry = EXT4_XATTR_NEXT(entry))
894 			if (entry->e_value_inum)
895 				ea_inode_refs++;
896 	}
897 
898 	if (EXT4_I(inode)->i_file_acl) {
899 		bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
900 		if (IS_ERR(bh)) {
901 			ret = PTR_ERR(bh);
902 			bh = NULL;
903 			goto out;
904 		}
905 
906 		ret = ext4_xattr_check_block(inode, bh);
907 		if (ret)
908 			goto out;
909 
910 		for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
911 		     entry = EXT4_XATTR_NEXT(entry))
912 			if (entry->e_value_inum)
913 				ea_inode_refs++;
914 	}
915 	*usage = ea_inode_refs + 1;
916 	ret = 0;
917 out:
918 	brelse(iloc.bh);
919 	brelse(bh);
920 	return ret;
921 }
922 
923 static inline size_t round_up_cluster(struct inode *inode, size_t length)
924 {
925 	struct super_block *sb = inode->i_sb;
926 	size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
927 				    inode->i_blkbits);
928 	size_t mask = ~(cluster_size - 1);
929 
930 	return (length + cluster_size - 1) & mask;
931 }
932 
933 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
934 {
935 	int err;
936 
937 	err = dquot_alloc_inode(inode);
938 	if (err)
939 		return err;
940 	err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
941 	if (err)
942 		dquot_free_inode(inode);
943 	return err;
944 }
945 
946 static void ext4_xattr_inode_free_quota(struct inode *parent,
947 					struct inode *ea_inode,
948 					size_t len)
949 {
950 	if (ea_inode &&
951 	    ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
952 		return;
953 	dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
954 	dquot_free_inode(parent);
955 }
956 
957 int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
958 			     struct buffer_head *block_bh, size_t value_len,
959 			     bool is_create)
960 {
961 	int credits;
962 	int blocks;
963 
964 	/*
965 	 * 1) Owner inode update
966 	 * 2) Ref count update on old xattr block
967 	 * 3) new xattr block
968 	 * 4) block bitmap update for new xattr block
969 	 * 5) group descriptor for new xattr block
970 	 * 6) block bitmap update for old xattr block
971 	 * 7) group descriptor for old block
972 	 *
973 	 * 6 & 7 can happen if we have two racing threads T_a and T_b
974 	 * which are each trying to set an xattr on inodes I_a and I_b
975 	 * which were both initially sharing an xattr block.
976 	 */
977 	credits = 7;
978 
979 	/* Quota updates. */
980 	credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
981 
982 	/*
983 	 * In case of inline data, we may push out the data to a block,
984 	 * so we need to reserve credits for this eventuality
985 	 */
986 	if (inode && ext4_has_inline_data(inode))
987 		credits += ext4_writepage_trans_blocks(inode) + 1;
988 
989 	/* We are done if ea_inode feature is not enabled. */
990 	if (!ext4_has_feature_ea_inode(sb))
991 		return credits;
992 
993 	/* New ea_inode, inode map, block bitmap, group descriptor. */
994 	credits += 4;
995 
996 	/* Data blocks. */
997 	blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
998 
999 	/* Indirection block or one level of extent tree. */
1000 	blocks += 1;
1001 
1002 	/* Block bitmap and group descriptor updates for each block. */
1003 	credits += blocks * 2;
1004 
1005 	/* Blocks themselves. */
1006 	credits += blocks;
1007 
1008 	if (!is_create) {
1009 		/* Dereference ea_inode holding old xattr value.
1010 		 * Old ea_inode, inode map, block bitmap, group descriptor.
1011 		 */
1012 		credits += 4;
1013 
1014 		/* Data blocks for old ea_inode. */
1015 		blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
1016 
1017 		/* Indirection block or one level of extent tree for old
1018 		 * ea_inode.
1019 		 */
1020 		blocks += 1;
1021 
1022 		/* Block bitmap and group descriptor updates for each block. */
1023 		credits += blocks * 2;
1024 	}
1025 
1026 	/* We may need to clone the existing xattr block in which case we need
1027 	 * to increment ref counts for existing ea_inodes referenced by it.
1028 	 */
1029 	if (block_bh) {
1030 		struct ext4_xattr_entry *entry = BFIRST(block_bh);
1031 
1032 		for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
1033 			if (entry->e_value_inum)
1034 				/* Ref count update on ea_inode. */
1035 				credits += 1;
1036 	}
1037 	return credits;
1038 }
1039 
1040 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
1041 				       int ref_change)
1042 {
1043 	struct ext4_iloc iloc;
1044 	s64 ref_count;
1045 	int ret;
1046 
1047 	inode_lock(ea_inode);
1048 
1049 	ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1050 	if (ret)
1051 		goto out;
1052 
1053 	ref_count = ext4_xattr_inode_get_ref(ea_inode);
1054 	ref_count += ref_change;
1055 	ext4_xattr_inode_set_ref(ea_inode, ref_count);
1056 
1057 	if (ref_change > 0) {
1058 		WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1059 			  ea_inode->i_ino, ref_count);
1060 
1061 		if (ref_count == 1) {
1062 			WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1063 				  ea_inode->i_ino, ea_inode->i_nlink);
1064 
1065 			set_nlink(ea_inode, 1);
1066 			ext4_orphan_del(handle, ea_inode);
1067 		}
1068 	} else {
1069 		WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1070 			  ea_inode->i_ino, ref_count);
1071 
1072 		if (ref_count == 0) {
1073 			WARN_ONCE(ea_inode->i_nlink != 1,
1074 				  "EA inode %lu i_nlink=%u",
1075 				  ea_inode->i_ino, ea_inode->i_nlink);
1076 
1077 			clear_nlink(ea_inode);
1078 			ext4_orphan_add(handle, ea_inode);
1079 		}
1080 	}
1081 
1082 	ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1083 	if (ret)
1084 		ext4_warning_inode(ea_inode,
1085 				   "ext4_mark_iloc_dirty() failed ret=%d", ret);
1086 out:
1087 	inode_unlock(ea_inode);
1088 	return ret;
1089 }
1090 
1091 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1092 {
1093 	return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1094 }
1095 
1096 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1097 {
1098 	return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1099 }
1100 
1101 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1102 					struct ext4_xattr_entry *first)
1103 {
1104 	struct inode *ea_inode;
1105 	struct ext4_xattr_entry *entry;
1106 	struct ext4_xattr_entry *failed_entry;
1107 	unsigned int ea_ino;
1108 	int err, saved_err;
1109 
1110 	for (entry = first; !IS_LAST_ENTRY(entry);
1111 	     entry = EXT4_XATTR_NEXT(entry)) {
1112 		if (!entry->e_value_inum)
1113 			continue;
1114 		ea_ino = le32_to_cpu(entry->e_value_inum);
1115 		err = ext4_xattr_inode_iget(parent, ea_ino,
1116 					    le32_to_cpu(entry->e_hash),
1117 					    &ea_inode);
1118 		if (err)
1119 			goto cleanup;
1120 		err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1121 		if (err) {
1122 			ext4_warning_inode(ea_inode, "inc ref error %d", err);
1123 			iput(ea_inode);
1124 			goto cleanup;
1125 		}
1126 		iput(ea_inode);
1127 	}
1128 	return 0;
1129 
1130 cleanup:
1131 	saved_err = err;
1132 	failed_entry = entry;
1133 
1134 	for (entry = first; entry != failed_entry;
1135 	     entry = EXT4_XATTR_NEXT(entry)) {
1136 		if (!entry->e_value_inum)
1137 			continue;
1138 		ea_ino = le32_to_cpu(entry->e_value_inum);
1139 		err = ext4_xattr_inode_iget(parent, ea_ino,
1140 					    le32_to_cpu(entry->e_hash),
1141 					    &ea_inode);
1142 		if (err) {
1143 			ext4_warning(parent->i_sb,
1144 				     "cleanup ea_ino %u iget error %d", ea_ino,
1145 				     err);
1146 			continue;
1147 		}
1148 		err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1149 		if (err)
1150 			ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1151 					   err);
1152 		iput(ea_inode);
1153 	}
1154 	return saved_err;
1155 }
1156 
1157 static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode,
1158 			struct buffer_head *bh, bool block_csum, bool dirty)
1159 {
1160 	int error;
1161 
1162 	if (bh && dirty) {
1163 		if (block_csum)
1164 			ext4_xattr_block_csum_set(inode, bh);
1165 		error = ext4_handle_dirty_metadata(handle, NULL, bh);
1166 		if (error) {
1167 			ext4_warning(inode->i_sb, "Handle metadata (error %d)",
1168 				     error);
1169 			return error;
1170 		}
1171 	}
1172 	return 0;
1173 }
1174 
1175 static void
1176 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1177 			     struct buffer_head *bh,
1178 			     struct ext4_xattr_entry *first, bool block_csum,
1179 			     struct ext4_xattr_inode_array **ea_inode_array,
1180 			     int extra_credits, bool skip_quota)
1181 {
1182 	struct inode *ea_inode;
1183 	struct ext4_xattr_entry *entry;
1184 	bool dirty = false;
1185 	unsigned int ea_ino;
1186 	int err;
1187 	int credits;
1188 
1189 	/* One credit for dec ref on ea_inode, one for orphan list addition, */
1190 	credits = 2 + extra_credits;
1191 
1192 	for (entry = first; !IS_LAST_ENTRY(entry);
1193 	     entry = EXT4_XATTR_NEXT(entry)) {
1194 		if (!entry->e_value_inum)
1195 			continue;
1196 		ea_ino = le32_to_cpu(entry->e_value_inum);
1197 		err = ext4_xattr_inode_iget(parent, ea_ino,
1198 					    le32_to_cpu(entry->e_hash),
1199 					    &ea_inode);
1200 		if (err)
1201 			continue;
1202 
1203 		err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1204 		if (err) {
1205 			ext4_warning_inode(ea_inode,
1206 					   "Expand inode array err=%d", err);
1207 			iput(ea_inode);
1208 			continue;
1209 		}
1210 
1211 		err = ext4_journal_ensure_credits_fn(handle, credits, credits,
1212 			ext4_free_metadata_revoke_credits(parent->i_sb, 1),
1213 			ext4_xattr_restart_fn(handle, parent, bh, block_csum,
1214 					      dirty));
1215 		if (err < 0) {
1216 			ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1217 					   err);
1218 			continue;
1219 		}
1220 		if (err > 0) {
1221 			err = ext4_journal_get_write_access(handle,
1222 					parent->i_sb, bh, EXT4_JTR_NONE);
1223 			if (err) {
1224 				ext4_warning_inode(ea_inode,
1225 						"Re-get write access err=%d",
1226 						err);
1227 				continue;
1228 			}
1229 		}
1230 
1231 		err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1232 		if (err) {
1233 			ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1234 					   err);
1235 			continue;
1236 		}
1237 
1238 		if (!skip_quota)
1239 			ext4_xattr_inode_free_quota(parent, ea_inode,
1240 					      le32_to_cpu(entry->e_value_size));
1241 
1242 		/*
1243 		 * Forget about ea_inode within the same transaction that
1244 		 * decrements the ref count. This avoids duplicate decrements in
1245 		 * case the rest of the work spills over to subsequent
1246 		 * transactions.
1247 		 */
1248 		entry->e_value_inum = 0;
1249 		entry->e_value_size = 0;
1250 
1251 		dirty = true;
1252 	}
1253 
1254 	if (dirty) {
1255 		/*
1256 		 * Note that we are deliberately skipping csum calculation for
1257 		 * the final update because we do not expect any journal
1258 		 * restarts until xattr block is freed.
1259 		 */
1260 
1261 		err = ext4_handle_dirty_metadata(handle, NULL, bh);
1262 		if (err)
1263 			ext4_warning_inode(parent,
1264 					   "handle dirty metadata err=%d", err);
1265 	}
1266 }
1267 
1268 /*
1269  * Release the xattr block BH: If the reference count is > 1, decrement it;
1270  * otherwise free the block.
1271  */
1272 static void
1273 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1274 			 struct buffer_head *bh,
1275 			 struct ext4_xattr_inode_array **ea_inode_array,
1276 			 int extra_credits)
1277 {
1278 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1279 	u32 hash, ref;
1280 	int error = 0;
1281 
1282 	BUFFER_TRACE(bh, "get_write_access");
1283 	error = ext4_journal_get_write_access(handle, inode->i_sb, bh,
1284 					      EXT4_JTR_NONE);
1285 	if (error)
1286 		goto out;
1287 
1288 retry_ref:
1289 	lock_buffer(bh);
1290 	hash = le32_to_cpu(BHDR(bh)->h_hash);
1291 	ref = le32_to_cpu(BHDR(bh)->h_refcount);
1292 	if (ref == 1) {
1293 		ea_bdebug(bh, "refcount now=0; freeing");
1294 		/*
1295 		 * This must happen under buffer lock for
1296 		 * ext4_xattr_block_set() to reliably detect freed block
1297 		 */
1298 		if (ea_block_cache) {
1299 			struct mb_cache_entry *oe;
1300 
1301 			oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
1302 							  bh->b_blocknr);
1303 			if (oe) {
1304 				unlock_buffer(bh);
1305 				mb_cache_entry_wait_unused(oe);
1306 				mb_cache_entry_put(ea_block_cache, oe);
1307 				goto retry_ref;
1308 			}
1309 		}
1310 		get_bh(bh);
1311 		unlock_buffer(bh);
1312 
1313 		if (ext4_has_feature_ea_inode(inode->i_sb))
1314 			ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1315 						     BFIRST(bh),
1316 						     true /* block_csum */,
1317 						     ea_inode_array,
1318 						     extra_credits,
1319 						     true /* skip_quota */);
1320 		ext4_free_blocks(handle, inode, bh, 0, 1,
1321 				 EXT4_FREE_BLOCKS_METADATA |
1322 				 EXT4_FREE_BLOCKS_FORGET);
1323 	} else {
1324 		ref--;
1325 		BHDR(bh)->h_refcount = cpu_to_le32(ref);
1326 		if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1327 			struct mb_cache_entry *ce;
1328 
1329 			if (ea_block_cache) {
1330 				ce = mb_cache_entry_get(ea_block_cache, hash,
1331 							bh->b_blocknr);
1332 				if (ce) {
1333 					set_bit(MBE_REUSABLE_B, &ce->e_flags);
1334 					mb_cache_entry_put(ea_block_cache, ce);
1335 				}
1336 			}
1337 		}
1338 
1339 		ext4_xattr_block_csum_set(inode, bh);
1340 		/*
1341 		 * Beware of this ugliness: Releasing of xattr block references
1342 		 * from different inodes can race and so we have to protect
1343 		 * from a race where someone else frees the block (and releases
1344 		 * its journal_head) before we are done dirtying the buffer. In
1345 		 * nojournal mode this race is harmless and we actually cannot
1346 		 * call ext4_handle_dirty_metadata() with locked buffer as
1347 		 * that function can call sync_dirty_buffer() so for that case
1348 		 * we handle the dirtying after unlocking the buffer.
1349 		 */
1350 		if (ext4_handle_valid(handle))
1351 			error = ext4_handle_dirty_metadata(handle, inode, bh);
1352 		unlock_buffer(bh);
1353 		if (!ext4_handle_valid(handle))
1354 			error = ext4_handle_dirty_metadata(handle, inode, bh);
1355 		if (IS_SYNC(inode))
1356 			ext4_handle_sync(handle);
1357 		dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
1358 		ea_bdebug(bh, "refcount now=%d; releasing",
1359 			  le32_to_cpu(BHDR(bh)->h_refcount));
1360 	}
1361 out:
1362 	ext4_std_error(inode->i_sb, error);
1363 	return;
1364 }
1365 
1366 /*
1367  * Find the available free space for EAs. This also returns the total number of
1368  * bytes used by EA entries.
1369  */
1370 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1371 				    size_t *min_offs, void *base, int *total)
1372 {
1373 	for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1374 		if (!last->e_value_inum && last->e_value_size) {
1375 			size_t offs = le16_to_cpu(last->e_value_offs);
1376 			if (offs < *min_offs)
1377 				*min_offs = offs;
1378 		}
1379 		if (total)
1380 			*total += EXT4_XATTR_LEN(last->e_name_len);
1381 	}
1382 	return (*min_offs - ((void *)last - base) - sizeof(__u32));
1383 }
1384 
1385 /*
1386  * Write the value of the EA in an inode.
1387  */
1388 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1389 				  const void *buf, int bufsize)
1390 {
1391 	struct buffer_head *bh = NULL;
1392 	unsigned long block = 0;
1393 	int blocksize = ea_inode->i_sb->s_blocksize;
1394 	int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
1395 	int csize, wsize = 0;
1396 	int ret = 0, ret2 = 0;
1397 	int retries = 0;
1398 
1399 retry:
1400 	while (ret >= 0 && ret < max_blocks) {
1401 		struct ext4_map_blocks map;
1402 		map.m_lblk = block += ret;
1403 		map.m_len = max_blocks -= ret;
1404 
1405 		ret = ext4_map_blocks(handle, ea_inode, &map,
1406 				      EXT4_GET_BLOCKS_CREATE);
1407 		if (ret <= 0) {
1408 			ext4_mark_inode_dirty(handle, ea_inode);
1409 			if (ret == -ENOSPC &&
1410 			    ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1411 				ret = 0;
1412 				goto retry;
1413 			}
1414 			break;
1415 		}
1416 	}
1417 
1418 	if (ret < 0)
1419 		return ret;
1420 
1421 	block = 0;
1422 	while (wsize < bufsize) {
1423 		brelse(bh);
1424 		csize = (bufsize - wsize) > blocksize ? blocksize :
1425 								bufsize - wsize;
1426 		bh = ext4_getblk(handle, ea_inode, block, 0);
1427 		if (IS_ERR(bh))
1428 			return PTR_ERR(bh);
1429 		if (!bh) {
1430 			WARN_ON_ONCE(1);
1431 			EXT4_ERROR_INODE(ea_inode,
1432 					 "ext4_getblk() return bh = NULL");
1433 			return -EFSCORRUPTED;
1434 		}
1435 		ret = ext4_journal_get_write_access(handle, ea_inode->i_sb, bh,
1436 						   EXT4_JTR_NONE);
1437 		if (ret)
1438 			goto out;
1439 
1440 		memcpy(bh->b_data, buf, csize);
1441 		set_buffer_uptodate(bh);
1442 		ext4_handle_dirty_metadata(handle, ea_inode, bh);
1443 
1444 		buf += csize;
1445 		wsize += csize;
1446 		block += 1;
1447 	}
1448 
1449 	inode_lock(ea_inode);
1450 	i_size_write(ea_inode, wsize);
1451 	ext4_update_i_disksize(ea_inode, wsize);
1452 	inode_unlock(ea_inode);
1453 
1454 	ret2 = ext4_mark_inode_dirty(handle, ea_inode);
1455 	if (unlikely(ret2 && !ret))
1456 		ret = ret2;
1457 
1458 out:
1459 	brelse(bh);
1460 
1461 	return ret;
1462 }
1463 
1464 /*
1465  * Create an inode to store the value of a large EA.
1466  */
1467 static struct inode *ext4_xattr_inode_create(handle_t *handle,
1468 					     struct inode *inode, u32 hash)
1469 {
1470 	struct inode *ea_inode = NULL;
1471 	uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
1472 	int err;
1473 
1474 	/*
1475 	 * Let the next inode be the goal, so we try and allocate the EA inode
1476 	 * in the same group, or nearby one.
1477 	 */
1478 	ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1479 				  S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
1480 				  EXT4_EA_INODE_FL);
1481 	if (!IS_ERR(ea_inode)) {
1482 		ea_inode->i_op = &ext4_file_inode_operations;
1483 		ea_inode->i_fop = &ext4_file_operations;
1484 		ext4_set_aops(ea_inode);
1485 		ext4_xattr_inode_set_class(ea_inode);
1486 		unlock_new_inode(ea_inode);
1487 		ext4_xattr_inode_set_ref(ea_inode, 1);
1488 		ext4_xattr_inode_set_hash(ea_inode, hash);
1489 		err = ext4_mark_inode_dirty(handle, ea_inode);
1490 		if (!err)
1491 			err = ext4_inode_attach_jinode(ea_inode);
1492 		if (err) {
1493 			if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1494 				ext4_warning_inode(ea_inode,
1495 					"cleanup dec ref error %d", err);
1496 			iput(ea_inode);
1497 			return ERR_PTR(err);
1498 		}
1499 
1500 		/*
1501 		 * Xattr inodes are shared therefore quota charging is performed
1502 		 * at a higher level.
1503 		 */
1504 		dquot_free_inode(ea_inode);
1505 		dquot_drop(ea_inode);
1506 		inode_lock(ea_inode);
1507 		ea_inode->i_flags |= S_NOQUOTA;
1508 		inode_unlock(ea_inode);
1509 	}
1510 
1511 	return ea_inode;
1512 }
1513 
1514 static struct inode *
1515 ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1516 			    size_t value_len, u32 hash)
1517 {
1518 	struct inode *ea_inode;
1519 	struct mb_cache_entry *ce;
1520 	struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1521 	void *ea_data;
1522 
1523 	if (!ea_inode_cache)
1524 		return NULL;
1525 
1526 	ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1527 	if (!ce)
1528 		return NULL;
1529 
1530 	WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) &&
1531 		     !(current->flags & PF_MEMALLOC_NOFS));
1532 
1533 	ea_data = kvmalloc(value_len, GFP_KERNEL);
1534 	if (!ea_data) {
1535 		mb_cache_entry_put(ea_inode_cache, ce);
1536 		return NULL;
1537 	}
1538 
1539 	while (ce) {
1540 		ea_inode = ext4_iget(inode->i_sb, ce->e_value,
1541 				     EXT4_IGET_NORMAL);
1542 		if (!IS_ERR(ea_inode) &&
1543 		    !is_bad_inode(ea_inode) &&
1544 		    (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1545 		    i_size_read(ea_inode) == value_len &&
1546 		    !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1547 		    !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1548 						    value_len) &&
1549 		    !memcmp(value, ea_data, value_len)) {
1550 			mb_cache_entry_touch(ea_inode_cache, ce);
1551 			mb_cache_entry_put(ea_inode_cache, ce);
1552 			kvfree(ea_data);
1553 			return ea_inode;
1554 		}
1555 
1556 		if (!IS_ERR(ea_inode))
1557 			iput(ea_inode);
1558 		ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1559 	}
1560 	kvfree(ea_data);
1561 	return NULL;
1562 }
1563 
1564 /*
1565  * Add value of the EA in an inode.
1566  */
1567 static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1568 					  const void *value, size_t value_len,
1569 					  struct inode **ret_inode)
1570 {
1571 	struct inode *ea_inode;
1572 	u32 hash;
1573 	int err;
1574 
1575 	hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1576 	ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1577 	if (ea_inode) {
1578 		err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1579 		if (err) {
1580 			iput(ea_inode);
1581 			return err;
1582 		}
1583 
1584 		*ret_inode = ea_inode;
1585 		return 0;
1586 	}
1587 
1588 	/* Create an inode for the EA value */
1589 	ea_inode = ext4_xattr_inode_create(handle, inode, hash);
1590 	if (IS_ERR(ea_inode))
1591 		return PTR_ERR(ea_inode);
1592 
1593 	err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
1594 	if (err) {
1595 		if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1596 			ext4_warning_inode(ea_inode, "cleanup dec ref error %d", err);
1597 		iput(ea_inode);
1598 		return err;
1599 	}
1600 
1601 	if (EA_INODE_CACHE(inode))
1602 		mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1603 				      ea_inode->i_ino, true /* reusable */);
1604 
1605 	*ret_inode = ea_inode;
1606 	return 0;
1607 }
1608 
1609 /*
1610  * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1611  * feature is enabled.
1612  */
1613 #define EXT4_XATTR_BLOCK_RESERVE(inode)	min(i_blocksize(inode)/8, 1024U)
1614 
1615 static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1616 				struct ext4_xattr_search *s,
1617 				handle_t *handle, struct inode *inode,
1618 				bool is_block)
1619 {
1620 	struct ext4_xattr_entry *last, *next;
1621 	struct ext4_xattr_entry *here = s->here;
1622 	size_t min_offs = s->end - s->base, name_len = strlen(i->name);
1623 	int in_inode = i->in_inode;
1624 	struct inode *old_ea_inode = NULL;
1625 	struct inode *new_ea_inode = NULL;
1626 	size_t old_size, new_size;
1627 	int ret;
1628 
1629 	/* Space used by old and new values. */
1630 	old_size = (!s->not_found && !here->e_value_inum) ?
1631 			EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1632 	new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1633 
1634 	/*
1635 	 * Optimization for the simple case when old and new values have the
1636 	 * same padded sizes. Not applicable if external inodes are involved.
1637 	 */
1638 	if (new_size && new_size == old_size) {
1639 		size_t offs = le16_to_cpu(here->e_value_offs);
1640 		void *val = s->base + offs;
1641 
1642 		here->e_value_size = cpu_to_le32(i->value_len);
1643 		if (i->value == EXT4_ZERO_XATTR_VALUE) {
1644 			memset(val, 0, new_size);
1645 		} else {
1646 			memcpy(val, i->value, i->value_len);
1647 			/* Clear padding bytes. */
1648 			memset(val + i->value_len, 0, new_size - i->value_len);
1649 		}
1650 		goto update_hash;
1651 	}
1652 
1653 	/* Compute min_offs and last. */
1654 	last = s->first;
1655 	for (; !IS_LAST_ENTRY(last); last = next) {
1656 		next = EXT4_XATTR_NEXT(last);
1657 		if ((void *)next >= s->end) {
1658 			EXT4_ERROR_INODE(inode, "corrupted xattr entries");
1659 			ret = -EFSCORRUPTED;
1660 			goto out;
1661 		}
1662 		if (!last->e_value_inum && last->e_value_size) {
1663 			size_t offs = le16_to_cpu(last->e_value_offs);
1664 			if (offs < min_offs)
1665 				min_offs = offs;
1666 		}
1667 	}
1668 
1669 	/* Check whether we have enough space. */
1670 	if (i->value) {
1671 		size_t free;
1672 
1673 		free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1674 		if (!s->not_found)
1675 			free += EXT4_XATTR_LEN(name_len) + old_size;
1676 
1677 		if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1678 			ret = -ENOSPC;
1679 			goto out;
1680 		}
1681 
1682 		/*
1683 		 * If storing the value in an external inode is an option,
1684 		 * reserve space for xattr entries/names in the external
1685 		 * attribute block so that a long value does not occupy the
1686 		 * whole space and prevent further entries being added.
1687 		 */
1688 		if (ext4_has_feature_ea_inode(inode->i_sb) &&
1689 		    new_size && is_block &&
1690 		    (min_offs + old_size - new_size) <
1691 					EXT4_XATTR_BLOCK_RESERVE(inode)) {
1692 			ret = -ENOSPC;
1693 			goto out;
1694 		}
1695 	}
1696 
1697 	/*
1698 	 * Getting access to old and new ea inodes is subject to failures.
1699 	 * Finish that work before doing any modifications to the xattr data.
1700 	 */
1701 	if (!s->not_found && here->e_value_inum) {
1702 		ret = ext4_xattr_inode_iget(inode,
1703 					    le32_to_cpu(here->e_value_inum),
1704 					    le32_to_cpu(here->e_hash),
1705 					    &old_ea_inode);
1706 		if (ret) {
1707 			old_ea_inode = NULL;
1708 			goto out;
1709 		}
1710 	}
1711 	if (i->value && in_inode) {
1712 		WARN_ON_ONCE(!i->value_len);
1713 
1714 		ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1715 		if (ret)
1716 			goto out;
1717 
1718 		ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1719 						     i->value_len,
1720 						     &new_ea_inode);
1721 		if (ret) {
1722 			new_ea_inode = NULL;
1723 			ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
1724 			goto out;
1725 		}
1726 	}
1727 
1728 	if (old_ea_inode) {
1729 		/* We are ready to release ref count on the old_ea_inode. */
1730 		ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1731 		if (ret) {
1732 			/* Release newly required ref count on new_ea_inode. */
1733 			if (new_ea_inode) {
1734 				int err;
1735 
1736 				err = ext4_xattr_inode_dec_ref(handle,
1737 							       new_ea_inode);
1738 				if (err)
1739 					ext4_warning_inode(new_ea_inode,
1740 						  "dec ref new_ea_inode err=%d",
1741 						  err);
1742 				ext4_xattr_inode_free_quota(inode, new_ea_inode,
1743 							    i->value_len);
1744 			}
1745 			goto out;
1746 		}
1747 
1748 		ext4_xattr_inode_free_quota(inode, old_ea_inode,
1749 					    le32_to_cpu(here->e_value_size));
1750 	}
1751 
1752 	/* No failures allowed past this point. */
1753 
1754 	if (!s->not_found && here->e_value_size && !here->e_value_inum) {
1755 		/* Remove the old value. */
1756 		void *first_val = s->base + min_offs;
1757 		size_t offs = le16_to_cpu(here->e_value_offs);
1758 		void *val = s->base + offs;
1759 
1760 		memmove(first_val + old_size, first_val, val - first_val);
1761 		memset(first_val, 0, old_size);
1762 		min_offs += old_size;
1763 
1764 		/* Adjust all value offsets. */
1765 		last = s->first;
1766 		while (!IS_LAST_ENTRY(last)) {
1767 			size_t o = le16_to_cpu(last->e_value_offs);
1768 
1769 			if (!last->e_value_inum &&
1770 			    last->e_value_size && o < offs)
1771 				last->e_value_offs = cpu_to_le16(o + old_size);
1772 			last = EXT4_XATTR_NEXT(last);
1773 		}
1774 	}
1775 
1776 	if (!i->value) {
1777 		/* Remove old name. */
1778 		size_t size = EXT4_XATTR_LEN(name_len);
1779 
1780 		last = ENTRY((void *)last - size);
1781 		memmove(here, (void *)here + size,
1782 			(void *)last - (void *)here + sizeof(__u32));
1783 		memset(last, 0, size);
1784 	} else if (s->not_found) {
1785 		/* Insert new name. */
1786 		size_t size = EXT4_XATTR_LEN(name_len);
1787 		size_t rest = (void *)last - (void *)here + sizeof(__u32);
1788 
1789 		memmove((void *)here + size, here, rest);
1790 		memset(here, 0, size);
1791 		here->e_name_index = i->name_index;
1792 		here->e_name_len = name_len;
1793 		memcpy(here->e_name, i->name, name_len);
1794 	} else {
1795 		/* This is an update, reset value info. */
1796 		here->e_value_inum = 0;
1797 		here->e_value_offs = 0;
1798 		here->e_value_size = 0;
1799 	}
1800 
1801 	if (i->value) {
1802 		/* Insert new value. */
1803 		if (in_inode) {
1804 			here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
1805 		} else if (i->value_len) {
1806 			void *val = s->base + min_offs - new_size;
1807 
1808 			here->e_value_offs = cpu_to_le16(min_offs - new_size);
1809 			if (i->value == EXT4_ZERO_XATTR_VALUE) {
1810 				memset(val, 0, new_size);
1811 			} else {
1812 				memcpy(val, i->value, i->value_len);
1813 				/* Clear padding bytes. */
1814 				memset(val + i->value_len, 0,
1815 				       new_size - i->value_len);
1816 			}
1817 		}
1818 		here->e_value_size = cpu_to_le32(i->value_len);
1819 	}
1820 
1821 update_hash:
1822 	if (i->value) {
1823 		__le32 hash = 0;
1824 
1825 		/* Entry hash calculation. */
1826 		if (in_inode) {
1827 			__le32 crc32c_hash;
1828 
1829 			/*
1830 			 * Feed crc32c hash instead of the raw value for entry
1831 			 * hash calculation. This is to avoid walking
1832 			 * potentially long value buffer again.
1833 			 */
1834 			crc32c_hash = cpu_to_le32(
1835 				       ext4_xattr_inode_get_hash(new_ea_inode));
1836 			hash = ext4_xattr_hash_entry(here->e_name,
1837 						     here->e_name_len,
1838 						     &crc32c_hash, 1);
1839 		} else if (is_block) {
1840 			__le32 *value = s->base + le16_to_cpu(
1841 							here->e_value_offs);
1842 
1843 			hash = ext4_xattr_hash_entry(here->e_name,
1844 						     here->e_name_len, value,
1845 						     new_size >> 2);
1846 		}
1847 		here->e_hash = hash;
1848 	}
1849 
1850 	if (is_block)
1851 		ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1852 
1853 	ret = 0;
1854 out:
1855 	iput(old_ea_inode);
1856 	iput(new_ea_inode);
1857 	return ret;
1858 }
1859 
1860 struct ext4_xattr_block_find {
1861 	struct ext4_xattr_search s;
1862 	struct buffer_head *bh;
1863 };
1864 
1865 static int
1866 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1867 		      struct ext4_xattr_block_find *bs)
1868 {
1869 	struct super_block *sb = inode->i_sb;
1870 	int error;
1871 
1872 	ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1873 		  i->name_index, i->name, i->value, (long)i->value_len);
1874 
1875 	if (EXT4_I(inode)->i_file_acl) {
1876 		/* The inode already has an extended attribute block. */
1877 		bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
1878 		if (IS_ERR(bs->bh)) {
1879 			error = PTR_ERR(bs->bh);
1880 			bs->bh = NULL;
1881 			return error;
1882 		}
1883 		ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1884 			atomic_read(&(bs->bh->b_count)),
1885 			le32_to_cpu(BHDR(bs->bh)->h_refcount));
1886 		error = ext4_xattr_check_block(inode, bs->bh);
1887 		if (error)
1888 			return error;
1889 		/* Find the named attribute. */
1890 		bs->s.base = BHDR(bs->bh);
1891 		bs->s.first = BFIRST(bs->bh);
1892 		bs->s.end = bs->bh->b_data + bs->bh->b_size;
1893 		bs->s.here = bs->s.first;
1894 		error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
1895 					 i->name_index, i->name, 1);
1896 		if (error && error != -ENODATA)
1897 			return error;
1898 		bs->s.not_found = error;
1899 	}
1900 	return 0;
1901 }
1902 
1903 static int
1904 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1905 		     struct ext4_xattr_info *i,
1906 		     struct ext4_xattr_block_find *bs)
1907 {
1908 	struct super_block *sb = inode->i_sb;
1909 	struct buffer_head *new_bh = NULL;
1910 	struct ext4_xattr_search s_copy = bs->s;
1911 	struct ext4_xattr_search *s = &s_copy;
1912 	struct mb_cache_entry *ce = NULL;
1913 	int error = 0;
1914 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1915 	struct inode *ea_inode = NULL, *tmp_inode;
1916 	size_t old_ea_inode_quota = 0;
1917 	unsigned int ea_ino;
1918 
1919 
1920 #define header(x) ((struct ext4_xattr_header *)(x))
1921 
1922 	if (s->base) {
1923 		int offset = (char *)s->here - bs->bh->b_data;
1924 
1925 		BUFFER_TRACE(bs->bh, "get_write_access");
1926 		error = ext4_journal_get_write_access(handle, sb, bs->bh,
1927 						      EXT4_JTR_NONE);
1928 		if (error)
1929 			goto cleanup;
1930 		lock_buffer(bs->bh);
1931 
1932 		if (header(s->base)->h_refcount == cpu_to_le32(1)) {
1933 			__u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1934 
1935 			/*
1936 			 * This must happen under buffer lock for
1937 			 * ext4_xattr_block_set() to reliably detect modified
1938 			 * block
1939 			 */
1940 			if (ea_block_cache) {
1941 				struct mb_cache_entry *oe;
1942 
1943 				oe = mb_cache_entry_delete_or_get(ea_block_cache,
1944 					hash, bs->bh->b_blocknr);
1945 				if (oe) {
1946 					/*
1947 					 * Xattr block is getting reused. Leave
1948 					 * it alone.
1949 					 */
1950 					mb_cache_entry_put(ea_block_cache, oe);
1951 					goto clone_block;
1952 				}
1953 			}
1954 			ea_bdebug(bs->bh, "modifying in-place");
1955 			error = ext4_xattr_set_entry(i, s, handle, inode,
1956 						     true /* is_block */);
1957 			ext4_xattr_block_csum_set(inode, bs->bh);
1958 			unlock_buffer(bs->bh);
1959 			if (error == -EFSCORRUPTED)
1960 				goto bad_block;
1961 			if (!error)
1962 				error = ext4_handle_dirty_metadata(handle,
1963 								   inode,
1964 								   bs->bh);
1965 			if (error)
1966 				goto cleanup;
1967 			goto inserted;
1968 		}
1969 clone_block:
1970 		unlock_buffer(bs->bh);
1971 		ea_bdebug(bs->bh, "cloning");
1972 		s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS);
1973 		error = -ENOMEM;
1974 		if (s->base == NULL)
1975 			goto cleanup;
1976 		s->first = ENTRY(header(s->base)+1);
1977 		header(s->base)->h_refcount = cpu_to_le32(1);
1978 		s->here = ENTRY(s->base + offset);
1979 		s->end = s->base + bs->bh->b_size;
1980 
1981 		/*
1982 		 * If existing entry points to an xattr inode, we need
1983 		 * to prevent ext4_xattr_set_entry() from decrementing
1984 		 * ref count on it because the reference belongs to the
1985 		 * original block. In this case, make the entry look
1986 		 * like it has an empty value.
1987 		 */
1988 		if (!s->not_found && s->here->e_value_inum) {
1989 			ea_ino = le32_to_cpu(s->here->e_value_inum);
1990 			error = ext4_xattr_inode_iget(inode, ea_ino,
1991 				      le32_to_cpu(s->here->e_hash),
1992 				      &tmp_inode);
1993 			if (error)
1994 				goto cleanup;
1995 
1996 			if (!ext4_test_inode_state(tmp_inode,
1997 					EXT4_STATE_LUSTRE_EA_INODE)) {
1998 				/*
1999 				 * Defer quota free call for previous
2000 				 * inode until success is guaranteed.
2001 				 */
2002 				old_ea_inode_quota = le32_to_cpu(
2003 						s->here->e_value_size);
2004 			}
2005 			iput(tmp_inode);
2006 
2007 			s->here->e_value_inum = 0;
2008 			s->here->e_value_size = 0;
2009 		}
2010 	} else {
2011 		/* Allocate a buffer where we construct the new block. */
2012 		s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
2013 		error = -ENOMEM;
2014 		if (s->base == NULL)
2015 			goto cleanup;
2016 		header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2017 		header(s->base)->h_blocks = cpu_to_le32(1);
2018 		header(s->base)->h_refcount = cpu_to_le32(1);
2019 		s->first = ENTRY(header(s->base)+1);
2020 		s->here = ENTRY(header(s->base)+1);
2021 		s->end = s->base + sb->s_blocksize;
2022 	}
2023 
2024 	error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
2025 	if (error == -EFSCORRUPTED)
2026 		goto bad_block;
2027 	if (error)
2028 		goto cleanup;
2029 
2030 	if (i->value && s->here->e_value_inum) {
2031 		/*
2032 		 * A ref count on ea_inode has been taken as part of the call to
2033 		 * ext4_xattr_set_entry() above. We would like to drop this
2034 		 * extra ref but we have to wait until the xattr block is
2035 		 * initialized and has its own ref count on the ea_inode.
2036 		 */
2037 		ea_ino = le32_to_cpu(s->here->e_value_inum);
2038 		error = ext4_xattr_inode_iget(inode, ea_ino,
2039 					      le32_to_cpu(s->here->e_hash),
2040 					      &ea_inode);
2041 		if (error) {
2042 			ea_inode = NULL;
2043 			goto cleanup;
2044 		}
2045 	}
2046 
2047 inserted:
2048 	if (!IS_LAST_ENTRY(s->first)) {
2049 		new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
2050 						     &ce);
2051 		if (new_bh) {
2052 			/* We found an identical block in the cache. */
2053 			if (new_bh == bs->bh)
2054 				ea_bdebug(new_bh, "keeping");
2055 			else {
2056 				u32 ref;
2057 
2058 				WARN_ON_ONCE(dquot_initialize_needed(inode));
2059 
2060 				/* The old block is released after updating
2061 				   the inode. */
2062 				error = dquot_alloc_block(inode,
2063 						EXT4_C2B(EXT4_SB(sb), 1));
2064 				if (error)
2065 					goto cleanup;
2066 				BUFFER_TRACE(new_bh, "get_write_access");
2067 				error = ext4_journal_get_write_access(
2068 						handle, sb, new_bh,
2069 						EXT4_JTR_NONE);
2070 				if (error)
2071 					goto cleanup_dquot;
2072 				lock_buffer(new_bh);
2073 				/*
2074 				 * We have to be careful about races with
2075 				 * adding references to xattr block. Once we
2076 				 * hold buffer lock xattr block's state is
2077 				 * stable so we can check the additional
2078 				 * reference fits.
2079 				 */
2080 				ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2081 				if (ref > EXT4_XATTR_REFCOUNT_MAX) {
2082 					/*
2083 					 * Undo everything and check mbcache
2084 					 * again.
2085 					 */
2086 					unlock_buffer(new_bh);
2087 					dquot_free_block(inode,
2088 							 EXT4_C2B(EXT4_SB(sb),
2089 								  1));
2090 					brelse(new_bh);
2091 					mb_cache_entry_put(ea_block_cache, ce);
2092 					ce = NULL;
2093 					new_bh = NULL;
2094 					goto inserted;
2095 				}
2096 				BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2097 				if (ref == EXT4_XATTR_REFCOUNT_MAX)
2098 					clear_bit(MBE_REUSABLE_B, &ce->e_flags);
2099 				ea_bdebug(new_bh, "reusing; refcount now=%d",
2100 					  ref);
2101 				ext4_xattr_block_csum_set(inode, new_bh);
2102 				unlock_buffer(new_bh);
2103 				error = ext4_handle_dirty_metadata(handle,
2104 								   inode,
2105 								   new_bh);
2106 				if (error)
2107 					goto cleanup_dquot;
2108 			}
2109 			mb_cache_entry_touch(ea_block_cache, ce);
2110 			mb_cache_entry_put(ea_block_cache, ce);
2111 			ce = NULL;
2112 		} else if (bs->bh && s->base == bs->bh->b_data) {
2113 			/* We were modifying this block in-place. */
2114 			ea_bdebug(bs->bh, "keeping this block");
2115 			ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
2116 			new_bh = bs->bh;
2117 			get_bh(new_bh);
2118 		} else {
2119 			/* We need to allocate a new block */
2120 			ext4_fsblk_t goal, block;
2121 
2122 			WARN_ON_ONCE(dquot_initialize_needed(inode));
2123 
2124 			goal = ext4_group_first_block_no(sb,
2125 						EXT4_I(inode)->i_block_group);
2126 			block = ext4_new_meta_blocks(handle, inode, goal, 0,
2127 						     NULL, &error);
2128 			if (error)
2129 				goto cleanup;
2130 
2131 			ea_idebug(inode, "creating block %llu",
2132 				  (unsigned long long)block);
2133 
2134 			new_bh = sb_getblk(sb, block);
2135 			if (unlikely(!new_bh)) {
2136 				error = -ENOMEM;
2137 getblk_failed:
2138 				ext4_free_blocks(handle, inode, NULL, block, 1,
2139 						 EXT4_FREE_BLOCKS_METADATA);
2140 				goto cleanup;
2141 			}
2142 			error = ext4_xattr_inode_inc_ref_all(handle, inode,
2143 						      ENTRY(header(s->base)+1));
2144 			if (error)
2145 				goto getblk_failed;
2146 			if (ea_inode) {
2147 				/* Drop the extra ref on ea_inode. */
2148 				error = ext4_xattr_inode_dec_ref(handle,
2149 								 ea_inode);
2150 				if (error)
2151 					ext4_warning_inode(ea_inode,
2152 							   "dec ref error=%d",
2153 							   error);
2154 				iput(ea_inode);
2155 				ea_inode = NULL;
2156 			}
2157 
2158 			lock_buffer(new_bh);
2159 			error = ext4_journal_get_create_access(handle, sb,
2160 							new_bh, EXT4_JTR_NONE);
2161 			if (error) {
2162 				unlock_buffer(new_bh);
2163 				error = -EIO;
2164 				goto getblk_failed;
2165 			}
2166 			memcpy(new_bh->b_data, s->base, new_bh->b_size);
2167 			ext4_xattr_block_csum_set(inode, new_bh);
2168 			set_buffer_uptodate(new_bh);
2169 			unlock_buffer(new_bh);
2170 			ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
2171 			error = ext4_handle_dirty_metadata(handle, inode,
2172 							   new_bh);
2173 			if (error)
2174 				goto cleanup;
2175 		}
2176 	}
2177 
2178 	if (old_ea_inode_quota)
2179 		ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
2180 
2181 	/* Update the inode. */
2182 	EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
2183 
2184 	/* Drop the previous xattr block. */
2185 	if (bs->bh && bs->bh != new_bh) {
2186 		struct ext4_xattr_inode_array *ea_inode_array = NULL;
2187 
2188 		ext4_xattr_release_block(handle, inode, bs->bh,
2189 					 &ea_inode_array,
2190 					 0 /* extra_credits */);
2191 		ext4_xattr_inode_array_free(ea_inode_array);
2192 	}
2193 	error = 0;
2194 
2195 cleanup:
2196 	if (ea_inode) {
2197 		int error2;
2198 
2199 		error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2200 		if (error2)
2201 			ext4_warning_inode(ea_inode, "dec ref error=%d",
2202 					   error2);
2203 
2204 		/* If there was an error, revert the quota charge. */
2205 		if (error)
2206 			ext4_xattr_inode_free_quota(inode, ea_inode,
2207 						    i_size_read(ea_inode));
2208 		iput(ea_inode);
2209 	}
2210 	if (ce)
2211 		mb_cache_entry_put(ea_block_cache, ce);
2212 	brelse(new_bh);
2213 	if (!(bs->bh && s->base == bs->bh->b_data))
2214 		kfree(s->base);
2215 
2216 	return error;
2217 
2218 cleanup_dquot:
2219 	dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
2220 	goto cleanup;
2221 
2222 bad_block:
2223 	EXT4_ERROR_INODE(inode, "bad block %llu",
2224 			 EXT4_I(inode)->i_file_acl);
2225 	goto cleanup;
2226 
2227 #undef header
2228 }
2229 
2230 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2231 			  struct ext4_xattr_ibody_find *is)
2232 {
2233 	struct ext4_xattr_ibody_header *header;
2234 	struct ext4_inode *raw_inode;
2235 	int error;
2236 
2237 	if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2238 		return 0;
2239 
2240 	raw_inode = ext4_raw_inode(&is->iloc);
2241 	header = IHDR(inode, raw_inode);
2242 	is->s.base = is->s.first = IFIRST(header);
2243 	is->s.here = is->s.first;
2244 	is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2245 	if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2246 		error = xattr_check_inode(inode, header, is->s.end);
2247 		if (error)
2248 			return error;
2249 		/* Find the named attribute. */
2250 		error = xattr_find_entry(inode, &is->s.here, is->s.end,
2251 					 i->name_index, i->name, 0);
2252 		if (error && error != -ENODATA)
2253 			return error;
2254 		is->s.not_found = error;
2255 	}
2256 	return 0;
2257 }
2258 
2259 int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2260 				struct ext4_xattr_info *i,
2261 				struct ext4_xattr_ibody_find *is)
2262 {
2263 	struct ext4_xattr_ibody_header *header;
2264 	struct ext4_xattr_search *s = &is->s;
2265 	int error;
2266 
2267 	if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2268 		return -ENOSPC;
2269 
2270 	error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
2271 	if (error)
2272 		return error;
2273 	header = IHDR(inode, ext4_raw_inode(&is->iloc));
2274 	if (!IS_LAST_ENTRY(s->first)) {
2275 		header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2276 		ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2277 	} else {
2278 		header->h_magic = cpu_to_le32(0);
2279 		ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2280 	}
2281 	return 0;
2282 }
2283 
2284 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2285 				 struct ext4_xattr_info *i)
2286 {
2287 	void *value;
2288 
2289 	/* When e_value_inum is set the value is stored externally. */
2290 	if (s->here->e_value_inum)
2291 		return 0;
2292 	if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2293 		return 0;
2294 	value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2295 	return !memcmp(value, i->value, i->value_len);
2296 }
2297 
2298 static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2299 {
2300 	struct buffer_head *bh;
2301 	int error;
2302 
2303 	if (!EXT4_I(inode)->i_file_acl)
2304 		return NULL;
2305 	bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2306 	if (IS_ERR(bh))
2307 		return bh;
2308 	error = ext4_xattr_check_block(inode, bh);
2309 	if (error) {
2310 		brelse(bh);
2311 		return ERR_PTR(error);
2312 	}
2313 	return bh;
2314 }
2315 
2316 /*
2317  * ext4_xattr_set_handle()
2318  *
2319  * Create, replace or remove an extended attribute for this inode.  Value
2320  * is NULL to remove an existing extended attribute, and non-NULL to
2321  * either replace an existing extended attribute, or create a new extended
2322  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2323  * specify that an extended attribute must exist and must not exist
2324  * previous to the call, respectively.
2325  *
2326  * Returns 0, or a negative error number on failure.
2327  */
2328 int
2329 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
2330 		      const char *name, const void *value, size_t value_len,
2331 		      int flags)
2332 {
2333 	struct ext4_xattr_info i = {
2334 		.name_index = name_index,
2335 		.name = name,
2336 		.value = value,
2337 		.value_len = value_len,
2338 		.in_inode = 0,
2339 	};
2340 	struct ext4_xattr_ibody_find is = {
2341 		.s = { .not_found = -ENODATA, },
2342 	};
2343 	struct ext4_xattr_block_find bs = {
2344 		.s = { .not_found = -ENODATA, },
2345 	};
2346 	int no_expand;
2347 	int error;
2348 
2349 	if (!name)
2350 		return -EINVAL;
2351 	if (strlen(name) > 255)
2352 		return -ERANGE;
2353 
2354 	ext4_write_lock_xattr(inode, &no_expand);
2355 
2356 	/* Check journal credits under write lock. */
2357 	if (ext4_handle_valid(handle)) {
2358 		struct buffer_head *bh;
2359 		int credits;
2360 
2361 		bh = ext4_xattr_get_block(inode);
2362 		if (IS_ERR(bh)) {
2363 			error = PTR_ERR(bh);
2364 			goto cleanup;
2365 		}
2366 
2367 		credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2368 						   value_len,
2369 						   flags & XATTR_CREATE);
2370 		brelse(bh);
2371 
2372 		if (jbd2_handle_buffer_credits(handle) < credits) {
2373 			error = -ENOSPC;
2374 			goto cleanup;
2375 		}
2376 		WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
2377 	}
2378 
2379 	error = ext4_reserve_inode_write(handle, inode, &is.iloc);
2380 	if (error)
2381 		goto cleanup;
2382 
2383 	if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
2384 		struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2385 		memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
2386 		ext4_clear_inode_state(inode, EXT4_STATE_NEW);
2387 	}
2388 
2389 	error = ext4_xattr_ibody_find(inode, &i, &is);
2390 	if (error)
2391 		goto cleanup;
2392 	if (is.s.not_found)
2393 		error = ext4_xattr_block_find(inode, &i, &bs);
2394 	if (error)
2395 		goto cleanup;
2396 	if (is.s.not_found && bs.s.not_found) {
2397 		error = -ENODATA;
2398 		if (flags & XATTR_REPLACE)
2399 			goto cleanup;
2400 		error = 0;
2401 		if (!value)
2402 			goto cleanup;
2403 	} else {
2404 		error = -EEXIST;
2405 		if (flags & XATTR_CREATE)
2406 			goto cleanup;
2407 	}
2408 
2409 	if (!value) {
2410 		if (!is.s.not_found)
2411 			error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2412 		else if (!bs.s.not_found)
2413 			error = ext4_xattr_block_set(handle, inode, &i, &bs);
2414 	} else {
2415 		error = 0;
2416 		/* Xattr value did not change? Save us some work and bail out */
2417 		if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2418 			goto cleanup;
2419 		if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2420 			goto cleanup;
2421 
2422 		if (ext4_has_feature_ea_inode(inode->i_sb) &&
2423 		    (EXT4_XATTR_SIZE(i.value_len) >
2424 			EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2425 			i.in_inode = 1;
2426 retry_inode:
2427 		error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2428 		if (!error && !bs.s.not_found) {
2429 			i.value = NULL;
2430 			error = ext4_xattr_block_set(handle, inode, &i, &bs);
2431 		} else if (error == -ENOSPC) {
2432 			if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2433 				brelse(bs.bh);
2434 				bs.bh = NULL;
2435 				error = ext4_xattr_block_find(inode, &i, &bs);
2436 				if (error)
2437 					goto cleanup;
2438 			}
2439 			error = ext4_xattr_block_set(handle, inode, &i, &bs);
2440 			if (!error && !is.s.not_found) {
2441 				i.value = NULL;
2442 				error = ext4_xattr_ibody_set(handle, inode, &i,
2443 							     &is);
2444 			} else if (error == -ENOSPC) {
2445 				/*
2446 				 * Xattr does not fit in the block, store at
2447 				 * external inode if possible.
2448 				 */
2449 				if (ext4_has_feature_ea_inode(inode->i_sb) &&
2450 				    i.value_len && !i.in_inode) {
2451 					i.in_inode = 1;
2452 					goto retry_inode;
2453 				}
2454 			}
2455 		}
2456 	}
2457 	if (!error) {
2458 		ext4_xattr_update_super_block(handle, inode->i_sb);
2459 		inode->i_ctime = current_time(inode);
2460 		inode_inc_iversion(inode);
2461 		if (!value)
2462 			no_expand = 0;
2463 		error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
2464 		/*
2465 		 * The bh is consumed by ext4_mark_iloc_dirty, even with
2466 		 * error != 0.
2467 		 */
2468 		is.iloc.bh = NULL;
2469 		if (IS_SYNC(inode))
2470 			ext4_handle_sync(handle);
2471 	}
2472 	ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
2473 
2474 cleanup:
2475 	brelse(is.iloc.bh);
2476 	brelse(bs.bh);
2477 	ext4_write_unlock_xattr(inode, &no_expand);
2478 	return error;
2479 }
2480 
2481 int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2482 			   bool is_create, int *credits)
2483 {
2484 	struct buffer_head *bh;
2485 	int err;
2486 
2487 	*credits = 0;
2488 
2489 	if (!EXT4_SB(inode->i_sb)->s_journal)
2490 		return 0;
2491 
2492 	down_read(&EXT4_I(inode)->xattr_sem);
2493 
2494 	bh = ext4_xattr_get_block(inode);
2495 	if (IS_ERR(bh)) {
2496 		err = PTR_ERR(bh);
2497 	} else {
2498 		*credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2499 						    value_len, is_create);
2500 		brelse(bh);
2501 		err = 0;
2502 	}
2503 
2504 	up_read(&EXT4_I(inode)->xattr_sem);
2505 	return err;
2506 }
2507 
2508 /*
2509  * ext4_xattr_set()
2510  *
2511  * Like ext4_xattr_set_handle, but start from an inode. This extended
2512  * attribute modification is a filesystem transaction by itself.
2513  *
2514  * Returns 0, or a negative error number on failure.
2515  */
2516 int
2517 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
2518 	       const void *value, size_t value_len, int flags)
2519 {
2520 	handle_t *handle;
2521 	struct super_block *sb = inode->i_sb;
2522 	int error, retries = 0;
2523 	int credits;
2524 
2525 	error = dquot_initialize(inode);
2526 	if (error)
2527 		return error;
2528 
2529 retry:
2530 	error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2531 				       &credits);
2532 	if (error)
2533 		return error;
2534 
2535 	handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
2536 	if (IS_ERR(handle)) {
2537 		error = PTR_ERR(handle);
2538 	} else {
2539 		int error2;
2540 
2541 		error = ext4_xattr_set_handle(handle, inode, name_index, name,
2542 					      value, value_len, flags);
2543 		error2 = ext4_journal_stop(handle);
2544 		if (error == -ENOSPC &&
2545 		    ext4_should_retry_alloc(sb, &retries))
2546 			goto retry;
2547 		if (error == 0)
2548 			error = error2;
2549 	}
2550 	ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, NULL);
2551 
2552 	return error;
2553 }
2554 
2555 /*
2556  * Shift the EA entries in the inode to create space for the increased
2557  * i_extra_isize.
2558  */
2559 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2560 				     int value_offs_shift, void *to,
2561 				     void *from, size_t n)
2562 {
2563 	struct ext4_xattr_entry *last = entry;
2564 	int new_offs;
2565 
2566 	/* We always shift xattr headers further thus offsets get lower */
2567 	BUG_ON(value_offs_shift > 0);
2568 
2569 	/* Adjust the value offsets of the entries */
2570 	for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2571 		if (!last->e_value_inum && last->e_value_size) {
2572 			new_offs = le16_to_cpu(last->e_value_offs) +
2573 							value_offs_shift;
2574 			last->e_value_offs = cpu_to_le16(new_offs);
2575 		}
2576 	}
2577 	/* Shift the entries by n bytes */
2578 	memmove(to, from, n);
2579 }
2580 
2581 /*
2582  * Move xattr pointed to by 'entry' from inode into external xattr block
2583  */
2584 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2585 				    struct ext4_inode *raw_inode,
2586 				    struct ext4_xattr_entry *entry)
2587 {
2588 	struct ext4_xattr_ibody_find *is = NULL;
2589 	struct ext4_xattr_block_find *bs = NULL;
2590 	char *buffer = NULL, *b_entry_name = NULL;
2591 	size_t value_size = le32_to_cpu(entry->e_value_size);
2592 	struct ext4_xattr_info i = {
2593 		.value = NULL,
2594 		.value_len = 0,
2595 		.name_index = entry->e_name_index,
2596 		.in_inode = !!entry->e_value_inum,
2597 	};
2598 	struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2599 	int error;
2600 
2601 	is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2602 	bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2603 	buffer = kvmalloc(value_size, GFP_NOFS);
2604 	b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2605 	if (!is || !bs || !buffer || !b_entry_name) {
2606 		error = -ENOMEM;
2607 		goto out;
2608 	}
2609 
2610 	is->s.not_found = -ENODATA;
2611 	bs->s.not_found = -ENODATA;
2612 	is->iloc.bh = NULL;
2613 	bs->bh = NULL;
2614 
2615 	/* Save the entry name and the entry value */
2616 	if (entry->e_value_inum) {
2617 		error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
2618 		if (error)
2619 			goto out;
2620 	} else {
2621 		size_t value_offs = le16_to_cpu(entry->e_value_offs);
2622 		memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2623 	}
2624 
2625 	memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2626 	b_entry_name[entry->e_name_len] = '\0';
2627 	i.name = b_entry_name;
2628 
2629 	error = ext4_get_inode_loc(inode, &is->iloc);
2630 	if (error)
2631 		goto out;
2632 
2633 	error = ext4_xattr_ibody_find(inode, &i, is);
2634 	if (error)
2635 		goto out;
2636 
2637 	/* Remove the chosen entry from the inode */
2638 	error = ext4_xattr_ibody_set(handle, inode, &i, is);
2639 	if (error)
2640 		goto out;
2641 
2642 	i.value = buffer;
2643 	i.value_len = value_size;
2644 	error = ext4_xattr_block_find(inode, &i, bs);
2645 	if (error)
2646 		goto out;
2647 
2648 	/* Add entry which was removed from the inode into the block */
2649 	error = ext4_xattr_block_set(handle, inode, &i, bs);
2650 	if (error)
2651 		goto out;
2652 	error = 0;
2653 out:
2654 	kfree(b_entry_name);
2655 	kvfree(buffer);
2656 	if (is)
2657 		brelse(is->iloc.bh);
2658 	if (bs)
2659 		brelse(bs->bh);
2660 	kfree(is);
2661 	kfree(bs);
2662 
2663 	return error;
2664 }
2665 
2666 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2667 				       struct ext4_inode *raw_inode,
2668 				       int isize_diff, size_t ifree,
2669 				       size_t bfree, int *total_ino)
2670 {
2671 	struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2672 	struct ext4_xattr_entry *small_entry;
2673 	struct ext4_xattr_entry *entry;
2674 	struct ext4_xattr_entry *last;
2675 	unsigned int entry_size;	/* EA entry size */
2676 	unsigned int total_size;	/* EA entry size + value size */
2677 	unsigned int min_total_size;
2678 	int error;
2679 
2680 	while (isize_diff > ifree) {
2681 		entry = NULL;
2682 		small_entry = NULL;
2683 		min_total_size = ~0U;
2684 		last = IFIRST(header);
2685 		/* Find the entry best suited to be pushed into EA block */
2686 		for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2687 			/* never move system.data out of the inode */
2688 			if ((last->e_name_len == 4) &&
2689 			    (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
2690 			    !memcmp(last->e_name, "data", 4))
2691 				continue;
2692 			total_size = EXT4_XATTR_LEN(last->e_name_len);
2693 			if (!last->e_value_inum)
2694 				total_size += EXT4_XATTR_SIZE(
2695 					       le32_to_cpu(last->e_value_size));
2696 			if (total_size <= bfree &&
2697 			    total_size < min_total_size) {
2698 				if (total_size + ifree < isize_diff) {
2699 					small_entry = last;
2700 				} else {
2701 					entry = last;
2702 					min_total_size = total_size;
2703 				}
2704 			}
2705 		}
2706 
2707 		if (entry == NULL) {
2708 			if (small_entry == NULL)
2709 				return -ENOSPC;
2710 			entry = small_entry;
2711 		}
2712 
2713 		entry_size = EXT4_XATTR_LEN(entry->e_name_len);
2714 		total_size = entry_size;
2715 		if (!entry->e_value_inum)
2716 			total_size += EXT4_XATTR_SIZE(
2717 					      le32_to_cpu(entry->e_value_size));
2718 		error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2719 						 entry);
2720 		if (error)
2721 			return error;
2722 
2723 		*total_ino -= entry_size;
2724 		ifree += total_size;
2725 		bfree -= total_size;
2726 	}
2727 
2728 	return 0;
2729 }
2730 
2731 /*
2732  * Expand an inode by new_extra_isize bytes when EAs are present.
2733  * Returns 0 on success or negative error number on failure.
2734  */
2735 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2736 			       struct ext4_inode *raw_inode, handle_t *handle)
2737 {
2738 	struct ext4_xattr_ibody_header *header;
2739 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2740 	static unsigned int mnt_count;
2741 	size_t min_offs;
2742 	size_t ifree, bfree;
2743 	int total_ino;
2744 	void *base, *end;
2745 	int error = 0, tried_min_extra_isize = 0;
2746 	int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
2747 	int isize_diff;	/* How much do we need to grow i_extra_isize */
2748 
2749 retry:
2750 	isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2751 	if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2752 		return 0;
2753 
2754 	header = IHDR(inode, raw_inode);
2755 
2756 	/*
2757 	 * Check if enough free space is available in the inode to shift the
2758 	 * entries ahead by new_extra_isize.
2759 	 */
2760 
2761 	base = IFIRST(header);
2762 	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2763 	min_offs = end - base;
2764 	total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32);
2765 
2766 	error = xattr_check_inode(inode, header, end);
2767 	if (error)
2768 		goto cleanup;
2769 
2770 	ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
2771 	if (ifree >= isize_diff)
2772 		goto shift;
2773 
2774 	/*
2775 	 * Enough free space isn't available in the inode, check if
2776 	 * EA block can hold new_extra_isize bytes.
2777 	 */
2778 	if (EXT4_I(inode)->i_file_acl) {
2779 		struct buffer_head *bh;
2780 
2781 		bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2782 		if (IS_ERR(bh)) {
2783 			error = PTR_ERR(bh);
2784 			goto cleanup;
2785 		}
2786 		error = ext4_xattr_check_block(inode, bh);
2787 		if (error) {
2788 			brelse(bh);
2789 			goto cleanup;
2790 		}
2791 		base = BHDR(bh);
2792 		end = bh->b_data + bh->b_size;
2793 		min_offs = end - base;
2794 		bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2795 					      NULL);
2796 		brelse(bh);
2797 		if (bfree + ifree < isize_diff) {
2798 			if (!tried_min_extra_isize && s_min_extra_isize) {
2799 				tried_min_extra_isize++;
2800 				new_extra_isize = s_min_extra_isize;
2801 				goto retry;
2802 			}
2803 			error = -ENOSPC;
2804 			goto cleanup;
2805 		}
2806 	} else {
2807 		bfree = inode->i_sb->s_blocksize;
2808 	}
2809 
2810 	error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2811 					    isize_diff, ifree, bfree,
2812 					    &total_ino);
2813 	if (error) {
2814 		if (error == -ENOSPC && !tried_min_extra_isize &&
2815 		    s_min_extra_isize) {
2816 			tried_min_extra_isize++;
2817 			new_extra_isize = s_min_extra_isize;
2818 			goto retry;
2819 		}
2820 		goto cleanup;
2821 	}
2822 shift:
2823 	/* Adjust the offsets and shift the remaining entries ahead */
2824 	ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
2825 			- new_extra_isize, (void *)raw_inode +
2826 			EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
2827 			(void *)header, total_ino);
2828 	EXT4_I(inode)->i_extra_isize = new_extra_isize;
2829 
2830 cleanup:
2831 	if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
2832 		ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2833 			     inode->i_ino);
2834 		mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2835 	}
2836 	return error;
2837 }
2838 
2839 #define EIA_INCR 16 /* must be 2^n */
2840 #define EIA_MASK (EIA_INCR - 1)
2841 
2842 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2843  * If @ea_inode_array is new or full it will be grown and the old
2844  * contents copied over.
2845  */
2846 static int
2847 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2848 			struct inode *inode)
2849 {
2850 	if (*ea_inode_array == NULL) {
2851 		/*
2852 		 * Start with 15 inodes, so it fits into a power-of-two size.
2853 		 * If *ea_inode_array is NULL, this is essentially offsetof()
2854 		 */
2855 		(*ea_inode_array) =
2856 			kmalloc(offsetof(struct ext4_xattr_inode_array,
2857 					 inodes[EIA_MASK]),
2858 				GFP_NOFS);
2859 		if (*ea_inode_array == NULL)
2860 			return -ENOMEM;
2861 		(*ea_inode_array)->count = 0;
2862 	} else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
2863 		/* expand the array once all 15 + n * 16 slots are full */
2864 		struct ext4_xattr_inode_array *new_array = NULL;
2865 		int count = (*ea_inode_array)->count;
2866 
2867 		/* if new_array is NULL, this is essentially offsetof() */
2868 		new_array = kmalloc(
2869 				offsetof(struct ext4_xattr_inode_array,
2870 					 inodes[count + EIA_INCR]),
2871 				GFP_NOFS);
2872 		if (new_array == NULL)
2873 			return -ENOMEM;
2874 		memcpy(new_array, *ea_inode_array,
2875 		       offsetof(struct ext4_xattr_inode_array, inodes[count]));
2876 		kfree(*ea_inode_array);
2877 		*ea_inode_array = new_array;
2878 	}
2879 	(*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
2880 	return 0;
2881 }
2882 
2883 /*
2884  * ext4_xattr_delete_inode()
2885  *
2886  * Free extended attribute resources associated with this inode. Traverse
2887  * all entries and decrement reference on any xattr inodes associated with this
2888  * inode. This is called immediately before an inode is freed. We have exclusive
2889  * access to the inode. If an orphan inode is deleted it will also release its
2890  * references on xattr block and xattr inodes.
2891  */
2892 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2893 			    struct ext4_xattr_inode_array **ea_inode_array,
2894 			    int extra_credits)
2895 {
2896 	struct buffer_head *bh = NULL;
2897 	struct ext4_xattr_ibody_header *header;
2898 	struct ext4_iloc iloc = { .bh = NULL };
2899 	struct ext4_xattr_entry *entry;
2900 	struct inode *ea_inode;
2901 	int error;
2902 
2903 	error = ext4_journal_ensure_credits(handle, extra_credits,
2904 			ext4_free_metadata_revoke_credits(inode->i_sb, 1));
2905 	if (error < 0) {
2906 		EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2907 		goto cleanup;
2908 	}
2909 
2910 	if (ext4_has_feature_ea_inode(inode->i_sb) &&
2911 	    ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2912 
2913 		error = ext4_get_inode_loc(inode, &iloc);
2914 		if (error) {
2915 			EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
2916 			goto cleanup;
2917 		}
2918 
2919 		error = ext4_journal_get_write_access(handle, inode->i_sb,
2920 						iloc.bh, EXT4_JTR_NONE);
2921 		if (error) {
2922 			EXT4_ERROR_INODE(inode, "write access (error %d)",
2923 					 error);
2924 			goto cleanup;
2925 		}
2926 
2927 		header = IHDR(inode, ext4_raw_inode(&iloc));
2928 		if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2929 			ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2930 						     IFIRST(header),
2931 						     false /* block_csum */,
2932 						     ea_inode_array,
2933 						     extra_credits,
2934 						     false /* skip_quota */);
2935 	}
2936 
2937 	if (EXT4_I(inode)->i_file_acl) {
2938 		bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2939 		if (IS_ERR(bh)) {
2940 			error = PTR_ERR(bh);
2941 			if (error == -EIO) {
2942 				EXT4_ERROR_INODE_ERR(inode, EIO,
2943 						     "block %llu read error",
2944 						     EXT4_I(inode)->i_file_acl);
2945 			}
2946 			bh = NULL;
2947 			goto cleanup;
2948 		}
2949 		error = ext4_xattr_check_block(inode, bh);
2950 		if (error)
2951 			goto cleanup;
2952 
2953 		if (ext4_has_feature_ea_inode(inode->i_sb)) {
2954 			for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2955 			     entry = EXT4_XATTR_NEXT(entry)) {
2956 				if (!entry->e_value_inum)
2957 					continue;
2958 				error = ext4_xattr_inode_iget(inode,
2959 					      le32_to_cpu(entry->e_value_inum),
2960 					      le32_to_cpu(entry->e_hash),
2961 					      &ea_inode);
2962 				if (error)
2963 					continue;
2964 				ext4_xattr_inode_free_quota(inode, ea_inode,
2965 					      le32_to_cpu(entry->e_value_size));
2966 				iput(ea_inode);
2967 			}
2968 
2969 		}
2970 
2971 		ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2972 					 extra_credits);
2973 		/*
2974 		 * Update i_file_acl value in the same transaction that releases
2975 		 * block.
2976 		 */
2977 		EXT4_I(inode)->i_file_acl = 0;
2978 		error = ext4_mark_inode_dirty(handle, inode);
2979 		if (error) {
2980 			EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2981 					 error);
2982 			goto cleanup;
2983 		}
2984 		ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
2985 	}
2986 	error = 0;
2987 cleanup:
2988 	brelse(iloc.bh);
2989 	brelse(bh);
2990 	return error;
2991 }
2992 
2993 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
2994 {
2995 	int idx;
2996 
2997 	if (ea_inode_array == NULL)
2998 		return;
2999 
3000 	for (idx = 0; idx < ea_inode_array->count; ++idx)
3001 		iput(ea_inode_array->inodes[idx]);
3002 	kfree(ea_inode_array);
3003 }
3004 
3005 /*
3006  * ext4_xattr_block_cache_insert()
3007  *
3008  * Create a new entry in the extended attribute block cache, and insert
3009  * it unless such an entry is already in the cache.
3010  *
3011  * Returns 0, or a negative error number on failure.
3012  */
3013 static void
3014 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
3015 			      struct buffer_head *bh)
3016 {
3017 	struct ext4_xattr_header *header = BHDR(bh);
3018 	__u32 hash = le32_to_cpu(header->h_hash);
3019 	int reusable = le32_to_cpu(header->h_refcount) <
3020 		       EXT4_XATTR_REFCOUNT_MAX;
3021 	int error;
3022 
3023 	if (!ea_block_cache)
3024 		return;
3025 	error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
3026 				      bh->b_blocknr, reusable);
3027 	if (error) {
3028 		if (error == -EBUSY)
3029 			ea_bdebug(bh, "already in cache");
3030 	} else
3031 		ea_bdebug(bh, "inserting [%x]", (int)hash);
3032 }
3033 
3034 /*
3035  * ext4_xattr_cmp()
3036  *
3037  * Compare two extended attribute blocks for equality.
3038  *
3039  * Returns 0 if the blocks are equal, 1 if they differ, and
3040  * a negative error number on errors.
3041  */
3042 static int
3043 ext4_xattr_cmp(struct ext4_xattr_header *header1,
3044 	       struct ext4_xattr_header *header2)
3045 {
3046 	struct ext4_xattr_entry *entry1, *entry2;
3047 
3048 	entry1 = ENTRY(header1+1);
3049 	entry2 = ENTRY(header2+1);
3050 	while (!IS_LAST_ENTRY(entry1)) {
3051 		if (IS_LAST_ENTRY(entry2))
3052 			return 1;
3053 		if (entry1->e_hash != entry2->e_hash ||
3054 		    entry1->e_name_index != entry2->e_name_index ||
3055 		    entry1->e_name_len != entry2->e_name_len ||
3056 		    entry1->e_value_size != entry2->e_value_size ||
3057 		    entry1->e_value_inum != entry2->e_value_inum ||
3058 		    memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
3059 			return 1;
3060 		if (!entry1->e_value_inum &&
3061 		    memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
3062 			   (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3063 			   le32_to_cpu(entry1->e_value_size)))
3064 			return 1;
3065 
3066 		entry1 = EXT4_XATTR_NEXT(entry1);
3067 		entry2 = EXT4_XATTR_NEXT(entry2);
3068 	}
3069 	if (!IS_LAST_ENTRY(entry2))
3070 		return 1;
3071 	return 0;
3072 }
3073 
3074 /*
3075  * ext4_xattr_block_cache_find()
3076  *
3077  * Find an identical extended attribute block.
3078  *
3079  * Returns a pointer to the block found, or NULL if such a block was
3080  * not found or an error occurred.
3081  */
3082 static struct buffer_head *
3083 ext4_xattr_block_cache_find(struct inode *inode,
3084 			    struct ext4_xattr_header *header,
3085 			    struct mb_cache_entry **pce)
3086 {
3087 	__u32 hash = le32_to_cpu(header->h_hash);
3088 	struct mb_cache_entry *ce;
3089 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
3090 
3091 	if (!ea_block_cache)
3092 		return NULL;
3093 	if (!header->h_hash)
3094 		return NULL;  /* never share */
3095 	ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
3096 	ce = mb_cache_entry_find_first(ea_block_cache, hash);
3097 	while (ce) {
3098 		struct buffer_head *bh;
3099 
3100 		bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
3101 		if (IS_ERR(bh)) {
3102 			if (PTR_ERR(bh) == -ENOMEM)
3103 				return NULL;
3104 			bh = NULL;
3105 			EXT4_ERROR_INODE(inode, "block %lu read error",
3106 					 (unsigned long)ce->e_value);
3107 		} else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
3108 			*pce = ce;
3109 			return bh;
3110 		}
3111 		brelse(bh);
3112 		ce = mb_cache_entry_find_next(ea_block_cache, ce);
3113 	}
3114 	return NULL;
3115 }
3116 
3117 #define NAME_HASH_SHIFT 5
3118 #define VALUE_HASH_SHIFT 16
3119 
3120 /*
3121  * ext4_xattr_hash_entry()
3122  *
3123  * Compute the hash of an extended attribute.
3124  */
3125 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3126 				    size_t value_count)
3127 {
3128 	__u32 hash = 0;
3129 
3130 	while (name_len--) {
3131 		hash = (hash << NAME_HASH_SHIFT) ^
3132 		       (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3133 		       *name++;
3134 	}
3135 	while (value_count--) {
3136 		hash = (hash << VALUE_HASH_SHIFT) ^
3137 		       (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3138 		       le32_to_cpu(*value++);
3139 	}
3140 	return cpu_to_le32(hash);
3141 }
3142 
3143 /*
3144  * ext4_xattr_hash_entry_signed()
3145  *
3146  * Compute the hash of an extended attribute incorrectly.
3147  */
3148 static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value, size_t value_count)
3149 {
3150 	__u32 hash = 0;
3151 
3152 	while (name_len--) {
3153 		hash = (hash << NAME_HASH_SHIFT) ^
3154 		       (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3155 		       (signed char)*name++;
3156 	}
3157 	while (value_count--) {
3158 		hash = (hash << VALUE_HASH_SHIFT) ^
3159 		       (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3160 		       le32_to_cpu(*value++);
3161 	}
3162 	return cpu_to_le32(hash);
3163 }
3164 
3165 #undef NAME_HASH_SHIFT
3166 #undef VALUE_HASH_SHIFT
3167 
3168 #define BLOCK_HASH_SHIFT 16
3169 
3170 /*
3171  * ext4_xattr_rehash()
3172  *
3173  * Re-compute the extended attribute hash value after an entry has changed.
3174  */
3175 static void ext4_xattr_rehash(struct ext4_xattr_header *header)
3176 {
3177 	struct ext4_xattr_entry *here;
3178 	__u32 hash = 0;
3179 
3180 	here = ENTRY(header+1);
3181 	while (!IS_LAST_ENTRY(here)) {
3182 		if (!here->e_hash) {
3183 			/* Block is not shared if an entry's hash value == 0 */
3184 			hash = 0;
3185 			break;
3186 		}
3187 		hash = (hash << BLOCK_HASH_SHIFT) ^
3188 		       (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3189 		       le32_to_cpu(here->e_hash);
3190 		here = EXT4_XATTR_NEXT(here);
3191 	}
3192 	header->h_hash = cpu_to_le32(hash);
3193 }
3194 
3195 #undef BLOCK_HASH_SHIFT
3196 
3197 #define	HASH_BUCKET_BITS	10
3198 
3199 struct mb_cache *
3200 ext4_xattr_create_cache(void)
3201 {
3202 	return mb_cache_create(HASH_BUCKET_BITS);
3203 }
3204 
3205 void ext4_xattr_destroy_cache(struct mb_cache *cache)
3206 {
3207 	if (cache)
3208 		mb_cache_destroy(cache);
3209 }
3210 
3211