xref: /openbmc/linux/fs/ext2/xattr.c (revision 8cd0f2ba)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/ext2/xattr.c
4  *
5  * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
6  *
7  * Fix by Harrison Xing <harrison@mountainviewdata.com>.
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  *
13  */
14 
15 /*
16  * Extended attributes are stored on disk blocks allocated outside of
17  * any inode. The i_file_acl field is then made to point to this allocated
18  * block. If all extended attributes of an inode are identical, these
19  * inodes may share the same extended attribute block. Such situations
20  * are automatically detected by keeping a cache of recent attribute block
21  * numbers and hashes over the block's contents in memory.
22  *
23  *
24  * Extended attribute block layout:
25  *
26  *   +------------------+
27  *   | header           |
28  *   | entry 1          | |
29  *   | entry 2          | | growing downwards
30  *   | entry 3          | v
31  *   | four null bytes  |
32  *   | . . .            |
33  *   | value 1          | ^
34  *   | value 3          | | growing upwards
35  *   | value 2          | |
36  *   +------------------+
37  *
38  * The block header is followed by multiple entry descriptors. These entry
39  * descriptors are variable in size, and aligned to EXT2_XATTR_PAD
40  * byte boundaries. The entry descriptors are sorted by attribute name,
41  * so that two extended attribute blocks can be compared efficiently.
42  *
43  * Attribute values are aligned to the end of the block, stored in
44  * no specific order. They are also padded to EXT2_XATTR_PAD byte
45  * boundaries. No additional gaps are left between them.
46  *
47  * Locking strategy
48  * ----------------
49  * EXT2_I(inode)->i_file_acl is protected by EXT2_I(inode)->xattr_sem.
50  * EA blocks are only changed if they are exclusive to an inode, so
51  * holding xattr_sem also means that nothing but the EA block's reference
52  * count will change. Multiple writers to an EA block are synchronized
53  * by the bh lock. No more than a single bh lock is held at any time
54  * to avoid deadlocks.
55  */
56 
57 #include <linux/buffer_head.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
60 #include <linux/mbcache.h>
61 #include <linux/quotaops.h>
62 #include <linux/rwsem.h>
63 #include <linux/security.h>
64 #include "ext2.h"
65 #include "xattr.h"
66 #include "acl.h"
67 
68 #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
69 #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
70 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
71 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
72 
73 #ifdef EXT2_XATTR_DEBUG
74 # define ea_idebug(inode, f...) do { \
75 		printk(KERN_DEBUG "inode %s:%ld: ", \
76 			inode->i_sb->s_id, inode->i_ino); \
77 		printk(f); \
78 		printk("\n"); \
79 	} while (0)
80 # define ea_bdebug(bh, f...) do { \
81 		printk(KERN_DEBUG "block %pg:%lu: ", \
82 			bh->b_bdev, (unsigned long) bh->b_blocknr); \
83 		printk(f); \
84 		printk("\n"); \
85 	} while (0)
86 #else
87 # define ea_idebug(f...)
88 # define ea_bdebug(f...)
89 #endif
90 
91 static int ext2_xattr_set2(struct inode *, struct buffer_head *,
92 			   struct ext2_xattr_header *);
93 
94 static int ext2_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
95 static struct buffer_head *ext2_xattr_cache_find(struct inode *,
96 						 struct ext2_xattr_header *);
97 static void ext2_xattr_rehash(struct ext2_xattr_header *,
98 			      struct ext2_xattr_entry *);
99 
100 static const struct xattr_handler *ext2_xattr_handler_map[] = {
101 	[EXT2_XATTR_INDEX_USER]		     = &ext2_xattr_user_handler,
102 #ifdef CONFIG_EXT2_FS_POSIX_ACL
103 	[EXT2_XATTR_INDEX_POSIX_ACL_ACCESS]  = &posix_acl_access_xattr_handler,
104 	[EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
105 #endif
106 	[EXT2_XATTR_INDEX_TRUSTED]	     = &ext2_xattr_trusted_handler,
107 #ifdef CONFIG_EXT2_FS_SECURITY
108 	[EXT2_XATTR_INDEX_SECURITY]	     = &ext2_xattr_security_handler,
109 #endif
110 };
111 
112 const struct xattr_handler *ext2_xattr_handlers[] = {
113 	&ext2_xattr_user_handler,
114 	&ext2_xattr_trusted_handler,
115 #ifdef CONFIG_EXT2_FS_POSIX_ACL
116 	&posix_acl_access_xattr_handler,
117 	&posix_acl_default_xattr_handler,
118 #endif
119 #ifdef CONFIG_EXT2_FS_SECURITY
120 	&ext2_xattr_security_handler,
121 #endif
122 	NULL
123 };
124 
125 #define EA_BLOCK_CACHE(inode)	(EXT2_SB(inode->i_sb)->s_ea_block_cache)
126 
127 static inline const struct xattr_handler *
128 ext2_xattr_handler(int name_index)
129 {
130 	const struct xattr_handler *handler = NULL;
131 
132 	if (name_index > 0 && name_index < ARRAY_SIZE(ext2_xattr_handler_map))
133 		handler = ext2_xattr_handler_map[name_index];
134 	return handler;
135 }
136 
137 static bool
138 ext2_xattr_header_valid(struct ext2_xattr_header *header)
139 {
140 	if (header->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
141 	    header->h_blocks != cpu_to_le32(1))
142 		return false;
143 
144 	return true;
145 }
146 
147 static bool
148 ext2_xattr_entry_valid(struct ext2_xattr_entry *entry, size_t end_offs)
149 {
150 	size_t size;
151 
152 	if (entry->e_value_block != 0)
153 		return false;
154 
155 	size = le32_to_cpu(entry->e_value_size);
156 	if (size > end_offs ||
157 	    le16_to_cpu(entry->e_value_offs) + size > end_offs)
158 		return false;
159 
160 	return true;
161 }
162 
163 /*
164  * ext2_xattr_get()
165  *
166  * Copy an extended attribute into the buffer
167  * provided, or compute the buffer size required.
168  * Buffer is NULL to compute the size of the buffer required.
169  *
170  * Returns a negative error number on failure, or the number of bytes
171  * used / required on success.
172  */
173 int
174 ext2_xattr_get(struct inode *inode, int name_index, const char *name,
175 	       void *buffer, size_t buffer_size)
176 {
177 	struct buffer_head *bh = NULL;
178 	struct ext2_xattr_entry *entry;
179 	size_t name_len, size;
180 	char *end;
181 	int error;
182 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
183 
184 	ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
185 		  name_index, name, buffer, (long)buffer_size);
186 
187 	if (name == NULL)
188 		return -EINVAL;
189 	name_len = strlen(name);
190 	if (name_len > 255)
191 		return -ERANGE;
192 
193 	down_read(&EXT2_I(inode)->xattr_sem);
194 	error = -ENODATA;
195 	if (!EXT2_I(inode)->i_file_acl)
196 		goto cleanup;
197 	ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
198 	bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
199 	error = -EIO;
200 	if (!bh)
201 		goto cleanup;
202 	ea_bdebug(bh, "b_count=%d, refcount=%d",
203 		atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
204 	end = bh->b_data + bh->b_size;
205 	if (!ext2_xattr_header_valid(HDR(bh))) {
206 bad_block:
207 		ext2_error(inode->i_sb, "ext2_xattr_get",
208 			"inode %ld: bad block %d", inode->i_ino,
209 			EXT2_I(inode)->i_file_acl);
210 		error = -EIO;
211 		goto cleanup;
212 	}
213 
214 	/* find named attribute */
215 	entry = FIRST_ENTRY(bh);
216 	while (!IS_LAST_ENTRY(entry)) {
217 		struct ext2_xattr_entry *next =
218 			EXT2_XATTR_NEXT(entry);
219 		if ((char *)next >= end)
220 			goto bad_block;
221 		if (name_index == entry->e_name_index &&
222 		    name_len == entry->e_name_len &&
223 		    memcmp(name, entry->e_name, name_len) == 0)
224 			goto found;
225 		entry = next;
226 	}
227 	if (ext2_xattr_cache_insert(ea_block_cache, bh))
228 		ea_idebug(inode, "cache insert failed");
229 	error = -ENODATA;
230 	goto cleanup;
231 found:
232 	if (!ext2_xattr_entry_valid(entry, inode->i_sb->s_blocksize))
233 		goto bad_block;
234 
235 	size = le32_to_cpu(entry->e_value_size);
236 	if (ext2_xattr_cache_insert(ea_block_cache, bh))
237 		ea_idebug(inode, "cache insert failed");
238 	if (buffer) {
239 		error = -ERANGE;
240 		if (size > buffer_size)
241 			goto cleanup;
242 		/* return value of attribute */
243 		memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
244 			size);
245 	}
246 	error = size;
247 
248 cleanup:
249 	brelse(bh);
250 	up_read(&EXT2_I(inode)->xattr_sem);
251 
252 	return error;
253 }
254 
255 /*
256  * ext2_xattr_list()
257  *
258  * Copy a list of attribute names into the buffer
259  * provided, or compute the buffer size required.
260  * Buffer is NULL to compute the size of the buffer required.
261  *
262  * Returns a negative error number on failure, or the number of bytes
263  * used / required on success.
264  */
265 static int
266 ext2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
267 {
268 	struct inode *inode = d_inode(dentry);
269 	struct buffer_head *bh = NULL;
270 	struct ext2_xattr_entry *entry;
271 	char *end;
272 	size_t rest = buffer_size;
273 	int error;
274 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
275 
276 	ea_idebug(inode, "buffer=%p, buffer_size=%ld",
277 		  buffer, (long)buffer_size);
278 
279 	down_read(&EXT2_I(inode)->xattr_sem);
280 	error = 0;
281 	if (!EXT2_I(inode)->i_file_acl)
282 		goto cleanup;
283 	ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
284 	bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
285 	error = -EIO;
286 	if (!bh)
287 		goto cleanup;
288 	ea_bdebug(bh, "b_count=%d, refcount=%d",
289 		atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
290 	end = bh->b_data + bh->b_size;
291 	if (!ext2_xattr_header_valid(HDR(bh))) {
292 bad_block:
293 		ext2_error(inode->i_sb, "ext2_xattr_list",
294 			"inode %ld: bad block %d", inode->i_ino,
295 			EXT2_I(inode)->i_file_acl);
296 		error = -EIO;
297 		goto cleanup;
298 	}
299 
300 	/* check the on-disk data structure */
301 	entry = FIRST_ENTRY(bh);
302 	while (!IS_LAST_ENTRY(entry)) {
303 		struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(entry);
304 
305 		if ((char *)next >= end)
306 			goto bad_block;
307 		entry = next;
308 	}
309 	if (ext2_xattr_cache_insert(ea_block_cache, bh))
310 		ea_idebug(inode, "cache insert failed");
311 
312 	/* list the attribute names */
313 	for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
314 	     entry = EXT2_XATTR_NEXT(entry)) {
315 		const struct xattr_handler *handler =
316 			ext2_xattr_handler(entry->e_name_index);
317 
318 		if (handler && (!handler->list || handler->list(dentry))) {
319 			const char *prefix = handler->prefix ?: handler->name;
320 			size_t prefix_len = strlen(prefix);
321 			size_t size = prefix_len + entry->e_name_len + 1;
322 
323 			if (buffer) {
324 				if (size > rest) {
325 					error = -ERANGE;
326 					goto cleanup;
327 				}
328 				memcpy(buffer, prefix, prefix_len);
329 				buffer += prefix_len;
330 				memcpy(buffer, entry->e_name, entry->e_name_len);
331 				buffer += entry->e_name_len;
332 				*buffer++ = 0;
333 			}
334 			rest -= size;
335 		}
336 	}
337 	error = buffer_size - rest;  /* total size */
338 
339 cleanup:
340 	brelse(bh);
341 	up_read(&EXT2_I(inode)->xattr_sem);
342 
343 	return error;
344 }
345 
346 /*
347  * Inode operation listxattr()
348  *
349  * d_inode(dentry)->i_mutex: don't care
350  */
351 ssize_t
352 ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
353 {
354 	return ext2_xattr_list(dentry, buffer, size);
355 }
356 
357 /*
358  * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
359  * not set, set it.
360  */
361 static void ext2_xattr_update_super_block(struct super_block *sb)
362 {
363 	if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
364 		return;
365 
366 	spin_lock(&EXT2_SB(sb)->s_lock);
367 	ext2_update_dynamic_rev(sb);
368 	EXT2_SET_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR);
369 	spin_unlock(&EXT2_SB(sb)->s_lock);
370 	mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
371 }
372 
373 /*
374  * ext2_xattr_set()
375  *
376  * Create, replace or remove an extended attribute for this inode.  Value
377  * is NULL to remove an existing extended attribute, and non-NULL to
378  * either replace an existing extended attribute, or create a new extended
379  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
380  * specify that an extended attribute must exist and must not exist
381  * previous to the call, respectively.
382  *
383  * Returns 0, or a negative error number on failure.
384  */
385 int
386 ext2_xattr_set(struct inode *inode, int name_index, const char *name,
387 	       const void *value, size_t value_len, int flags)
388 {
389 	struct super_block *sb = inode->i_sb;
390 	struct buffer_head *bh = NULL;
391 	struct ext2_xattr_header *header = NULL;
392 	struct ext2_xattr_entry *here, *last;
393 	size_t name_len, free, min_offs = sb->s_blocksize;
394 	int not_found = 1, error;
395 	char *end;
396 
397 	/*
398 	 * header -- Points either into bh, or to a temporarily
399 	 *           allocated buffer.
400 	 * here -- The named entry found, or the place for inserting, within
401 	 *         the block pointed to by header.
402 	 * last -- Points right after the last named entry within the block
403 	 *         pointed to by header.
404 	 * min_offs -- The offset of the first value (values are aligned
405 	 *             towards the end of the block).
406 	 * end -- Points right after the block pointed to by header.
407 	 */
408 
409 	ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
410 		  name_index, name, value, (long)value_len);
411 
412 	if (value == NULL)
413 		value_len = 0;
414 	if (name == NULL)
415 		return -EINVAL;
416 	name_len = strlen(name);
417 	if (name_len > 255 || value_len > sb->s_blocksize)
418 		return -ERANGE;
419 	down_write(&EXT2_I(inode)->xattr_sem);
420 	if (EXT2_I(inode)->i_file_acl) {
421 		/* The inode already has an extended attribute block. */
422 		bh = sb_bread(sb, EXT2_I(inode)->i_file_acl);
423 		error = -EIO;
424 		if (!bh)
425 			goto cleanup;
426 		ea_bdebug(bh, "b_count=%d, refcount=%d",
427 			atomic_read(&(bh->b_count)),
428 			le32_to_cpu(HDR(bh)->h_refcount));
429 		header = HDR(bh);
430 		end = bh->b_data + bh->b_size;
431 		if (!ext2_xattr_header_valid(header)) {
432 bad_block:
433 			ext2_error(sb, "ext2_xattr_set",
434 				"inode %ld: bad block %d", inode->i_ino,
435 				   EXT2_I(inode)->i_file_acl);
436 			error = -EIO;
437 			goto cleanup;
438 		}
439 		/*
440 		 * Find the named attribute. If not found, 'here' will point
441 		 * to entry where the new attribute should be inserted to
442 		 * maintain sorting.
443 		 */
444 		last = FIRST_ENTRY(bh);
445 		while (!IS_LAST_ENTRY(last)) {
446 			struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last);
447 			if ((char *)next >= end)
448 				goto bad_block;
449 			if (!last->e_value_block && last->e_value_size) {
450 				size_t offs = le16_to_cpu(last->e_value_offs);
451 				if (offs < min_offs)
452 					min_offs = offs;
453 			}
454 			if (not_found > 0) {
455 				not_found = name_index - last->e_name_index;
456 				if (!not_found)
457 					not_found = name_len - last->e_name_len;
458 				if (!not_found) {
459 					not_found = memcmp(name, last->e_name,
460 							   name_len);
461 				}
462 				if (not_found <= 0)
463 					here = last;
464 			}
465 			last = next;
466 		}
467 		if (not_found > 0)
468 			here = last;
469 
470 		/* Check whether we have enough space left. */
471 		free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
472 	} else {
473 		/* We will use a new extended attribute block. */
474 		free = sb->s_blocksize -
475 			sizeof(struct ext2_xattr_header) - sizeof(__u32);
476 		here = last = NULL;  /* avoid gcc uninitialized warning. */
477 	}
478 
479 	if (not_found) {
480 		/* Request to remove a nonexistent attribute? */
481 		error = -ENODATA;
482 		if (flags & XATTR_REPLACE)
483 			goto cleanup;
484 		error = 0;
485 		if (value == NULL)
486 			goto cleanup;
487 	} else {
488 		/* Request to create an existing attribute? */
489 		error = -EEXIST;
490 		if (flags & XATTR_CREATE)
491 			goto cleanup;
492 		if (!here->e_value_block && here->e_value_size) {
493 			if (!ext2_xattr_entry_valid(here, sb->s_blocksize))
494 				goto bad_block;
495 			free += EXT2_XATTR_SIZE(
496 					le32_to_cpu(here->e_value_size));
497 		}
498 		free += EXT2_XATTR_LEN(name_len);
499 	}
500 	error = -ENOSPC;
501 	if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
502 		goto cleanup;
503 
504 	/* Here we know that we can set the new attribute. */
505 
506 	if (header) {
507 		/* assert(header == HDR(bh)); */
508 		lock_buffer(bh);
509 		if (header->h_refcount == cpu_to_le32(1)) {
510 			__u32 hash = le32_to_cpu(header->h_hash);
511 
512 			ea_bdebug(bh, "modifying in-place");
513 			/*
514 			 * This must happen under buffer lock for
515 			 * ext2_xattr_set2() to reliably detect modified block
516 			 */
517 			mb_cache_entry_delete(EA_BLOCK_CACHE(inode), hash,
518 					      bh->b_blocknr);
519 
520 			/* keep the buffer locked while modifying it. */
521 		} else {
522 			int offset;
523 
524 			unlock_buffer(bh);
525 			ea_bdebug(bh, "cloning");
526 			header = kmalloc(bh->b_size, GFP_KERNEL);
527 			error = -ENOMEM;
528 			if (header == NULL)
529 				goto cleanup;
530 			memcpy(header, HDR(bh), bh->b_size);
531 			header->h_refcount = cpu_to_le32(1);
532 
533 			offset = (char *)here - bh->b_data;
534 			here = ENTRY((char *)header + offset);
535 			offset = (char *)last - bh->b_data;
536 			last = ENTRY((char *)header + offset);
537 		}
538 	} else {
539 		/* Allocate a buffer where we construct the new block. */
540 		header = kzalloc(sb->s_blocksize, GFP_KERNEL);
541 		error = -ENOMEM;
542 		if (header == NULL)
543 			goto cleanup;
544 		end = (char *)header + sb->s_blocksize;
545 		header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
546 		header->h_blocks = header->h_refcount = cpu_to_le32(1);
547 		last = here = ENTRY(header+1);
548 	}
549 
550 	/* Iff we are modifying the block in-place, bh is locked here. */
551 
552 	if (not_found) {
553 		/* Insert the new name. */
554 		size_t size = EXT2_XATTR_LEN(name_len);
555 		size_t rest = (char *)last - (char *)here;
556 		memmove((char *)here + size, here, rest);
557 		memset(here, 0, size);
558 		here->e_name_index = name_index;
559 		here->e_name_len = name_len;
560 		memcpy(here->e_name, name, name_len);
561 	} else {
562 		if (!here->e_value_block && here->e_value_size) {
563 			char *first_val = (char *)header + min_offs;
564 			size_t offs = le16_to_cpu(here->e_value_offs);
565 			char *val = (char *)header + offs;
566 			size_t size = EXT2_XATTR_SIZE(
567 				le32_to_cpu(here->e_value_size));
568 
569 			if (size == EXT2_XATTR_SIZE(value_len)) {
570 				/* The old and the new value have the same
571 				   size. Just replace. */
572 				here->e_value_size = cpu_to_le32(value_len);
573 				memset(val + size - EXT2_XATTR_PAD, 0,
574 				       EXT2_XATTR_PAD); /* Clear pad bytes. */
575 				memcpy(val, value, value_len);
576 				goto skip_replace;
577 			}
578 
579 			/* Remove the old value. */
580 			memmove(first_val + size, first_val, val - first_val);
581 			memset(first_val, 0, size);
582 			here->e_value_offs = 0;
583 			min_offs += size;
584 
585 			/* Adjust all value offsets. */
586 			last = ENTRY(header+1);
587 			while (!IS_LAST_ENTRY(last)) {
588 				size_t o = le16_to_cpu(last->e_value_offs);
589 				if (!last->e_value_block && o < offs)
590 					last->e_value_offs =
591 						cpu_to_le16(o + size);
592 				last = EXT2_XATTR_NEXT(last);
593 			}
594 		}
595 		if (value == NULL) {
596 			/* Remove the old name. */
597 			size_t size = EXT2_XATTR_LEN(name_len);
598 			last = ENTRY((char *)last - size);
599 			memmove(here, (char*)here + size,
600 				(char*)last - (char*)here);
601 			memset(last, 0, size);
602 		}
603 	}
604 
605 	if (value != NULL) {
606 		/* Insert the new value. */
607 		here->e_value_size = cpu_to_le32(value_len);
608 		if (value_len) {
609 			size_t size = EXT2_XATTR_SIZE(value_len);
610 			char *val = (char *)header + min_offs - size;
611 			here->e_value_offs =
612 				cpu_to_le16((char *)val - (char *)header);
613 			memset(val + size - EXT2_XATTR_PAD, 0,
614 			       EXT2_XATTR_PAD); /* Clear the pad bytes. */
615 			memcpy(val, value, value_len);
616 		}
617 	}
618 
619 skip_replace:
620 	if (IS_LAST_ENTRY(ENTRY(header+1))) {
621 		/* This block is now empty. */
622 		if (bh && header == HDR(bh))
623 			unlock_buffer(bh);  /* we were modifying in-place. */
624 		error = ext2_xattr_set2(inode, bh, NULL);
625 	} else {
626 		ext2_xattr_rehash(header, here);
627 		if (bh && header == HDR(bh))
628 			unlock_buffer(bh);  /* we were modifying in-place. */
629 		error = ext2_xattr_set2(inode, bh, header);
630 	}
631 
632 cleanup:
633 	if (!(bh && header == HDR(bh)))
634 		kfree(header);
635 	brelse(bh);
636 	up_write(&EXT2_I(inode)->xattr_sem);
637 
638 	return error;
639 }
640 
641 /*
642  * Second half of ext2_xattr_set(): Update the file system.
643  */
644 static int
645 ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
646 		struct ext2_xattr_header *header)
647 {
648 	struct super_block *sb = inode->i_sb;
649 	struct buffer_head *new_bh = NULL;
650 	int error;
651 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
652 
653 	if (header) {
654 		new_bh = ext2_xattr_cache_find(inode, header);
655 		if (new_bh) {
656 			/* We found an identical block in the cache. */
657 			if (new_bh == old_bh) {
658 				ea_bdebug(new_bh, "keeping this block");
659 			} else {
660 				/* The old block is released after updating
661 				   the inode.  */
662 				ea_bdebug(new_bh, "reusing block");
663 
664 				error = dquot_alloc_block(inode, 1);
665 				if (error) {
666 					unlock_buffer(new_bh);
667 					goto cleanup;
668 				}
669 				le32_add_cpu(&HDR(new_bh)->h_refcount, 1);
670 				ea_bdebug(new_bh, "refcount now=%d",
671 					le32_to_cpu(HDR(new_bh)->h_refcount));
672 			}
673 			unlock_buffer(new_bh);
674 		} else if (old_bh && header == HDR(old_bh)) {
675 			/* Keep this block. No need to lock the block as we
676 			   don't need to change the reference count. */
677 			new_bh = old_bh;
678 			get_bh(new_bh);
679 			ext2_xattr_cache_insert(ea_block_cache, new_bh);
680 		} else {
681 			/* We need to allocate a new block */
682 			ext2_fsblk_t goal = ext2_group_first_block_no(sb,
683 						EXT2_I(inode)->i_block_group);
684 			int block = ext2_new_block(inode, goal, &error);
685 			if (error)
686 				goto cleanup;
687 			ea_idebug(inode, "creating block %d", block);
688 
689 			new_bh = sb_getblk(sb, block);
690 			if (unlikely(!new_bh)) {
691 				ext2_free_blocks(inode, block, 1);
692 				mark_inode_dirty(inode);
693 				error = -ENOMEM;
694 				goto cleanup;
695 			}
696 			lock_buffer(new_bh);
697 			memcpy(new_bh->b_data, header, new_bh->b_size);
698 			set_buffer_uptodate(new_bh);
699 			unlock_buffer(new_bh);
700 			ext2_xattr_cache_insert(ea_block_cache, new_bh);
701 
702 			ext2_xattr_update_super_block(sb);
703 		}
704 		mark_buffer_dirty(new_bh);
705 		if (IS_SYNC(inode)) {
706 			sync_dirty_buffer(new_bh);
707 			error = -EIO;
708 			if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
709 				goto cleanup;
710 		}
711 	}
712 
713 	/* Update the inode. */
714 	EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
715 	inode->i_ctime = current_time(inode);
716 	if (IS_SYNC(inode)) {
717 		error = sync_inode_metadata(inode, 1);
718 		/* In case sync failed due to ENOSPC the inode was actually
719 		 * written (only some dirty data were not) so we just proceed
720 		 * as if nothing happened and cleanup the unused block */
721 		if (error && error != -ENOSPC) {
722 			if (new_bh && new_bh != old_bh) {
723 				dquot_free_block_nodirty(inode, 1);
724 				mark_inode_dirty(inode);
725 			}
726 			goto cleanup;
727 		}
728 	} else
729 		mark_inode_dirty(inode);
730 
731 	error = 0;
732 	if (old_bh && old_bh != new_bh) {
733 		/*
734 		 * If there was an old block and we are no longer using it,
735 		 * release the old block.
736 		 */
737 		lock_buffer(old_bh);
738 		if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
739 			__u32 hash = le32_to_cpu(HDR(old_bh)->h_hash);
740 
741 			/*
742 			 * This must happen under buffer lock for
743 			 * ext2_xattr_set2() to reliably detect freed block
744 			 */
745 			mb_cache_entry_delete(ea_block_cache, hash,
746 					      old_bh->b_blocknr);
747 			/* Free the old block. */
748 			ea_bdebug(old_bh, "freeing");
749 			ext2_free_blocks(inode, old_bh->b_blocknr, 1);
750 			mark_inode_dirty(inode);
751 			/* We let our caller release old_bh, so we
752 			 * need to duplicate the buffer before. */
753 			get_bh(old_bh);
754 			bforget(old_bh);
755 		} else {
756 			/* Decrement the refcount only. */
757 			le32_add_cpu(&HDR(old_bh)->h_refcount, -1);
758 			dquot_free_block_nodirty(inode, 1);
759 			mark_inode_dirty(inode);
760 			mark_buffer_dirty(old_bh);
761 			ea_bdebug(old_bh, "refcount now=%d",
762 				le32_to_cpu(HDR(old_bh)->h_refcount));
763 		}
764 		unlock_buffer(old_bh);
765 	}
766 
767 cleanup:
768 	brelse(new_bh);
769 
770 	return error;
771 }
772 
773 /*
774  * ext2_xattr_delete_inode()
775  *
776  * Free extended attribute resources associated with this inode. This
777  * is called immediately before an inode is freed.
778  */
779 void
780 ext2_xattr_delete_inode(struct inode *inode)
781 {
782 	struct buffer_head *bh = NULL;
783 	struct ext2_sb_info *sbi = EXT2_SB(inode->i_sb);
784 
785 	down_write(&EXT2_I(inode)->xattr_sem);
786 	if (!EXT2_I(inode)->i_file_acl)
787 		goto cleanup;
788 
789 	if (!ext2_data_block_valid(sbi, EXT2_I(inode)->i_file_acl, 0)) {
790 		ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
791 			"inode %ld: xattr block %d is out of data blocks range",
792 			inode->i_ino, EXT2_I(inode)->i_file_acl);
793 		goto cleanup;
794 	}
795 
796 	bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
797 	if (!bh) {
798 		ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
799 			"inode %ld: block %d read error", inode->i_ino,
800 			EXT2_I(inode)->i_file_acl);
801 		goto cleanup;
802 	}
803 	ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
804 	if (!ext2_xattr_header_valid(HDR(bh))) {
805 		ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
806 			"inode %ld: bad block %d", inode->i_ino,
807 			EXT2_I(inode)->i_file_acl);
808 		goto cleanup;
809 	}
810 	lock_buffer(bh);
811 	if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
812 		__u32 hash = le32_to_cpu(HDR(bh)->h_hash);
813 
814 		/*
815 		 * This must happen under buffer lock for ext2_xattr_set2() to
816 		 * reliably detect freed block
817 		 */
818 		mb_cache_entry_delete(EA_BLOCK_CACHE(inode), hash,
819 				      bh->b_blocknr);
820 		ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
821 		get_bh(bh);
822 		bforget(bh);
823 		unlock_buffer(bh);
824 	} else {
825 		le32_add_cpu(&HDR(bh)->h_refcount, -1);
826 		ea_bdebug(bh, "refcount now=%d",
827 			le32_to_cpu(HDR(bh)->h_refcount));
828 		unlock_buffer(bh);
829 		mark_buffer_dirty(bh);
830 		if (IS_SYNC(inode))
831 			sync_dirty_buffer(bh);
832 		dquot_free_block_nodirty(inode, 1);
833 	}
834 	EXT2_I(inode)->i_file_acl = 0;
835 
836 cleanup:
837 	brelse(bh);
838 	up_write(&EXT2_I(inode)->xattr_sem);
839 }
840 
841 /*
842  * ext2_xattr_cache_insert()
843  *
844  * Create a new entry in the extended attribute cache, and insert
845  * it unless such an entry is already in the cache.
846  *
847  * Returns 0, or a negative error number on failure.
848  */
849 static int
850 ext2_xattr_cache_insert(struct mb_cache *cache, struct buffer_head *bh)
851 {
852 	__u32 hash = le32_to_cpu(HDR(bh)->h_hash);
853 	int error;
854 
855 	error = mb_cache_entry_create(cache, GFP_NOFS, hash, bh->b_blocknr,
856 				      true);
857 	if (error) {
858 		if (error == -EBUSY) {
859 			ea_bdebug(bh, "already in cache (%d cache entries)",
860 				atomic_read(&ext2_xattr_cache->c_entry_count));
861 			error = 0;
862 		}
863 	} else
864 		ea_bdebug(bh, "inserting [%x]", (int)hash);
865 	return error;
866 }
867 
868 /*
869  * ext2_xattr_cmp()
870  *
871  * Compare two extended attribute blocks for equality.
872  *
873  * Returns 0 if the blocks are equal, 1 if they differ, and
874  * a negative error number on errors.
875  */
876 static int
877 ext2_xattr_cmp(struct ext2_xattr_header *header1,
878 	       struct ext2_xattr_header *header2)
879 {
880 	struct ext2_xattr_entry *entry1, *entry2;
881 
882 	entry1 = ENTRY(header1+1);
883 	entry2 = ENTRY(header2+1);
884 	while (!IS_LAST_ENTRY(entry1)) {
885 		if (IS_LAST_ENTRY(entry2))
886 			return 1;
887 		if (entry1->e_hash != entry2->e_hash ||
888 		    entry1->e_name_index != entry2->e_name_index ||
889 		    entry1->e_name_len != entry2->e_name_len ||
890 		    entry1->e_value_size != entry2->e_value_size ||
891 		    memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
892 			return 1;
893 		if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
894 			return -EIO;
895 		if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
896 			   (char *)header2 + le16_to_cpu(entry2->e_value_offs),
897 			   le32_to_cpu(entry1->e_value_size)))
898 			return 1;
899 
900 		entry1 = EXT2_XATTR_NEXT(entry1);
901 		entry2 = EXT2_XATTR_NEXT(entry2);
902 	}
903 	if (!IS_LAST_ENTRY(entry2))
904 		return 1;
905 	return 0;
906 }
907 
908 /*
909  * ext2_xattr_cache_find()
910  *
911  * Find an identical extended attribute block.
912  *
913  * Returns a locked buffer head to the block found, or NULL if such
914  * a block was not found or an error occurred.
915  */
916 static struct buffer_head *
917 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
918 {
919 	__u32 hash = le32_to_cpu(header->h_hash);
920 	struct mb_cache_entry *ce;
921 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
922 
923 	if (!header->h_hash)
924 		return NULL;  /* never share */
925 	ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
926 again:
927 	ce = mb_cache_entry_find_first(ea_block_cache, hash);
928 	while (ce) {
929 		struct buffer_head *bh;
930 
931 		bh = sb_bread(inode->i_sb, ce->e_value);
932 		if (!bh) {
933 			ext2_error(inode->i_sb, "ext2_xattr_cache_find",
934 				"inode %ld: block %ld read error",
935 				inode->i_ino, (unsigned long) ce->e_value);
936 		} else {
937 			lock_buffer(bh);
938 			/*
939 			 * We have to be careful about races with freeing or
940 			 * rehashing of xattr block. Once we hold buffer lock
941 			 * xattr block's state is stable so we can check
942 			 * whether the block got freed / rehashed or not.
943 			 * Since we unhash mbcache entry under buffer lock when
944 			 * freeing / rehashing xattr block, checking whether
945 			 * entry is still hashed is reliable.
946 			 */
947 			if (hlist_bl_unhashed(&ce->e_hash_list)) {
948 				mb_cache_entry_put(ea_block_cache, ce);
949 				unlock_buffer(bh);
950 				brelse(bh);
951 				goto again;
952 			} else if (le32_to_cpu(HDR(bh)->h_refcount) >
953 				   EXT2_XATTR_REFCOUNT_MAX) {
954 				ea_idebug(inode, "block %ld refcount %d>%d",
955 					  (unsigned long) ce->e_value,
956 					  le32_to_cpu(HDR(bh)->h_refcount),
957 					  EXT2_XATTR_REFCOUNT_MAX);
958 			} else if (!ext2_xattr_cmp(header, HDR(bh))) {
959 				ea_bdebug(bh, "b_count=%d",
960 					  atomic_read(&(bh->b_count)));
961 				mb_cache_entry_touch(ea_block_cache, ce);
962 				mb_cache_entry_put(ea_block_cache, ce);
963 				return bh;
964 			}
965 			unlock_buffer(bh);
966 			brelse(bh);
967 		}
968 		ce = mb_cache_entry_find_next(ea_block_cache, ce);
969 	}
970 	return NULL;
971 }
972 
973 #define NAME_HASH_SHIFT 5
974 #define VALUE_HASH_SHIFT 16
975 
976 /*
977  * ext2_xattr_hash_entry()
978  *
979  * Compute the hash of an extended attribute.
980  */
981 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
982 					 struct ext2_xattr_entry *entry)
983 {
984 	__u32 hash = 0;
985 	char *name = entry->e_name;
986 	int n;
987 
988 	for (n=0; n < entry->e_name_len; n++) {
989 		hash = (hash << NAME_HASH_SHIFT) ^
990 		       (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
991 		       *name++;
992 	}
993 
994 	if (entry->e_value_block == 0 && entry->e_value_size != 0) {
995 		__le32 *value = (__le32 *)((char *)header +
996 			le16_to_cpu(entry->e_value_offs));
997 		for (n = (le32_to_cpu(entry->e_value_size) +
998 		     EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
999 			hash = (hash << VALUE_HASH_SHIFT) ^
1000 			       (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1001 			       le32_to_cpu(*value++);
1002 		}
1003 	}
1004 	entry->e_hash = cpu_to_le32(hash);
1005 }
1006 
1007 #undef NAME_HASH_SHIFT
1008 #undef VALUE_HASH_SHIFT
1009 
1010 #define BLOCK_HASH_SHIFT 16
1011 
1012 /*
1013  * ext2_xattr_rehash()
1014  *
1015  * Re-compute the extended attribute hash value after an entry has changed.
1016  */
1017 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
1018 			      struct ext2_xattr_entry *entry)
1019 {
1020 	struct ext2_xattr_entry *here;
1021 	__u32 hash = 0;
1022 
1023 	ext2_xattr_hash_entry(header, entry);
1024 	here = ENTRY(header+1);
1025 	while (!IS_LAST_ENTRY(here)) {
1026 		if (!here->e_hash) {
1027 			/* Block is not shared if an entry's hash value == 0 */
1028 			hash = 0;
1029 			break;
1030 		}
1031 		hash = (hash << BLOCK_HASH_SHIFT) ^
1032 		       (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1033 		       le32_to_cpu(here->e_hash);
1034 		here = EXT2_XATTR_NEXT(here);
1035 	}
1036 	header->h_hash = cpu_to_le32(hash);
1037 }
1038 
1039 #undef BLOCK_HASH_SHIFT
1040 
1041 #define HASH_BUCKET_BITS 10
1042 
1043 struct mb_cache *ext2_xattr_create_cache(void)
1044 {
1045 	return mb_cache_create(HASH_BUCKET_BITS);
1046 }
1047 
1048 void ext2_xattr_destroy_cache(struct mb_cache *cache)
1049 {
1050 	if (cache)
1051 		mb_cache_destroy(cache);
1052 }
1053