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