xref: /openbmc/linux/fs/ext2/xattr.c (revision f4c3fb8c)
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 		/* Find the named attribute. */
440 		here = FIRST_ENTRY(bh);
441 		while (!IS_LAST_ENTRY(here)) {
442 			struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(here);
443 			if ((char *)next >= end)
444 				goto bad_block;
445 			if (!here->e_value_block && here->e_value_size) {
446 				size_t offs = le16_to_cpu(here->e_value_offs);
447 				if (offs < min_offs)
448 					min_offs = offs;
449 			}
450 			not_found = name_index - here->e_name_index;
451 			if (!not_found)
452 				not_found = name_len - here->e_name_len;
453 			if (!not_found)
454 				not_found = memcmp(name, here->e_name,name_len);
455 			if (not_found <= 0)
456 				break;
457 			here = next;
458 		}
459 		last = here;
460 		/* We still need to compute min_offs and last. */
461 		while (!IS_LAST_ENTRY(last)) {
462 			struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last);
463 			if ((char *)next >= end)
464 				goto bad_block;
465 			if (!last->e_value_block && last->e_value_size) {
466 				size_t offs = le16_to_cpu(last->e_value_offs);
467 				if (offs < min_offs)
468 					min_offs = offs;
469 			}
470 			last = next;
471 		}
472 
473 		/* Check whether we have enough space left. */
474 		free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
475 	} else {
476 		/* We will use a new extended attribute block. */
477 		free = sb->s_blocksize -
478 			sizeof(struct ext2_xattr_header) - sizeof(__u32);
479 		here = last = NULL;  /* avoid gcc uninitialized warning. */
480 	}
481 
482 	if (not_found) {
483 		/* Request to remove a nonexistent attribute? */
484 		error = -ENODATA;
485 		if (flags & XATTR_REPLACE)
486 			goto cleanup;
487 		error = 0;
488 		if (value == NULL)
489 			goto cleanup;
490 	} else {
491 		/* Request to create an existing attribute? */
492 		error = -EEXIST;
493 		if (flags & XATTR_CREATE)
494 			goto cleanup;
495 		if (!here->e_value_block && here->e_value_size) {
496 			if (!ext2_xattr_entry_valid(here, sb->s_blocksize))
497 				goto bad_block;
498 			free += EXT2_XATTR_SIZE(
499 					le32_to_cpu(here->e_value_size));
500 		}
501 		free += EXT2_XATTR_LEN(name_len);
502 	}
503 	error = -ENOSPC;
504 	if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
505 		goto cleanup;
506 
507 	/* Here we know that we can set the new attribute. */
508 
509 	if (header) {
510 		/* assert(header == HDR(bh)); */
511 		lock_buffer(bh);
512 		if (header->h_refcount == cpu_to_le32(1)) {
513 			__u32 hash = le32_to_cpu(header->h_hash);
514 
515 			ea_bdebug(bh, "modifying in-place");
516 			/*
517 			 * This must happen under buffer lock for
518 			 * ext2_xattr_set2() to reliably detect modified block
519 			 */
520 			mb_cache_entry_delete(EA_BLOCK_CACHE(inode), hash,
521 					      bh->b_blocknr);
522 
523 			/* keep the buffer locked while modifying it. */
524 		} else {
525 			int offset;
526 
527 			unlock_buffer(bh);
528 			ea_bdebug(bh, "cloning");
529 			header = kmalloc(bh->b_size, GFP_KERNEL);
530 			error = -ENOMEM;
531 			if (header == NULL)
532 				goto cleanup;
533 			memcpy(header, HDR(bh), bh->b_size);
534 			header->h_refcount = cpu_to_le32(1);
535 
536 			offset = (char *)here - bh->b_data;
537 			here = ENTRY((char *)header + offset);
538 			offset = (char *)last - bh->b_data;
539 			last = ENTRY((char *)header + offset);
540 		}
541 	} else {
542 		/* Allocate a buffer where we construct the new block. */
543 		header = kzalloc(sb->s_blocksize, GFP_KERNEL);
544 		error = -ENOMEM;
545 		if (header == NULL)
546 			goto cleanup;
547 		end = (char *)header + sb->s_blocksize;
548 		header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
549 		header->h_blocks = header->h_refcount = cpu_to_le32(1);
550 		last = here = ENTRY(header+1);
551 	}
552 
553 	/* Iff we are modifying the block in-place, bh is locked here. */
554 
555 	if (not_found) {
556 		/* Insert the new name. */
557 		size_t size = EXT2_XATTR_LEN(name_len);
558 		size_t rest = (char *)last - (char *)here;
559 		memmove((char *)here + size, here, rest);
560 		memset(here, 0, size);
561 		here->e_name_index = name_index;
562 		here->e_name_len = name_len;
563 		memcpy(here->e_name, name, name_len);
564 	} else {
565 		if (!here->e_value_block && here->e_value_size) {
566 			char *first_val = (char *)header + min_offs;
567 			size_t offs = le16_to_cpu(here->e_value_offs);
568 			char *val = (char *)header + offs;
569 			size_t size = EXT2_XATTR_SIZE(
570 				le32_to_cpu(here->e_value_size));
571 
572 			if (size == EXT2_XATTR_SIZE(value_len)) {
573 				/* The old and the new value have the same
574 				   size. Just replace. */
575 				here->e_value_size = cpu_to_le32(value_len);
576 				memset(val + size - EXT2_XATTR_PAD, 0,
577 				       EXT2_XATTR_PAD); /* Clear pad bytes. */
578 				memcpy(val, value, value_len);
579 				goto skip_replace;
580 			}
581 
582 			/* Remove the old value. */
583 			memmove(first_val + size, first_val, val - first_val);
584 			memset(first_val, 0, size);
585 			here->e_value_offs = 0;
586 			min_offs += size;
587 
588 			/* Adjust all value offsets. */
589 			last = ENTRY(header+1);
590 			while (!IS_LAST_ENTRY(last)) {
591 				size_t o = le16_to_cpu(last->e_value_offs);
592 				if (!last->e_value_block && o < offs)
593 					last->e_value_offs =
594 						cpu_to_le16(o + size);
595 				last = EXT2_XATTR_NEXT(last);
596 			}
597 		}
598 		if (value == NULL) {
599 			/* Remove the old name. */
600 			size_t size = EXT2_XATTR_LEN(name_len);
601 			last = ENTRY((char *)last - size);
602 			memmove(here, (char*)here + size,
603 				(char*)last - (char*)here);
604 			memset(last, 0, size);
605 		}
606 	}
607 
608 	if (value != NULL) {
609 		/* Insert the new value. */
610 		here->e_value_size = cpu_to_le32(value_len);
611 		if (value_len) {
612 			size_t size = EXT2_XATTR_SIZE(value_len);
613 			char *val = (char *)header + min_offs - size;
614 			here->e_value_offs =
615 				cpu_to_le16((char *)val - (char *)header);
616 			memset(val + size - EXT2_XATTR_PAD, 0,
617 			       EXT2_XATTR_PAD); /* Clear the pad bytes. */
618 			memcpy(val, value, value_len);
619 		}
620 	}
621 
622 skip_replace:
623 	if (IS_LAST_ENTRY(ENTRY(header+1))) {
624 		/* This block is now empty. */
625 		if (bh && header == HDR(bh))
626 			unlock_buffer(bh);  /* we were modifying in-place. */
627 		error = ext2_xattr_set2(inode, bh, NULL);
628 	} else {
629 		ext2_xattr_rehash(header, here);
630 		if (bh && header == HDR(bh))
631 			unlock_buffer(bh);  /* we were modifying in-place. */
632 		error = ext2_xattr_set2(inode, bh, header);
633 	}
634 
635 cleanup:
636 	if (!(bh && header == HDR(bh)))
637 		kfree(header);
638 	brelse(bh);
639 	up_write(&EXT2_I(inode)->xattr_sem);
640 
641 	return error;
642 }
643 
644 /*
645  * Second half of ext2_xattr_set(): Update the file system.
646  */
647 static int
648 ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
649 		struct ext2_xattr_header *header)
650 {
651 	struct super_block *sb = inode->i_sb;
652 	struct buffer_head *new_bh = NULL;
653 	int error;
654 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
655 
656 	if (header) {
657 		new_bh = ext2_xattr_cache_find(inode, header);
658 		if (new_bh) {
659 			/* We found an identical block in the cache. */
660 			if (new_bh == old_bh) {
661 				ea_bdebug(new_bh, "keeping this block");
662 			} else {
663 				/* The old block is released after updating
664 				   the inode.  */
665 				ea_bdebug(new_bh, "reusing block");
666 
667 				error = dquot_alloc_block(inode, 1);
668 				if (error) {
669 					unlock_buffer(new_bh);
670 					goto cleanup;
671 				}
672 				le32_add_cpu(&HDR(new_bh)->h_refcount, 1);
673 				ea_bdebug(new_bh, "refcount now=%d",
674 					le32_to_cpu(HDR(new_bh)->h_refcount));
675 			}
676 			unlock_buffer(new_bh);
677 		} else if (old_bh && header == HDR(old_bh)) {
678 			/* Keep this block. No need to lock the block as we
679 			   don't need to change the reference count. */
680 			new_bh = old_bh;
681 			get_bh(new_bh);
682 			ext2_xattr_cache_insert(ea_block_cache, new_bh);
683 		} else {
684 			/* We need to allocate a new block */
685 			ext2_fsblk_t goal = ext2_group_first_block_no(sb,
686 						EXT2_I(inode)->i_block_group);
687 			int block = ext2_new_block(inode, goal, &error);
688 			if (error)
689 				goto cleanup;
690 			ea_idebug(inode, "creating block %d", block);
691 
692 			new_bh = sb_getblk(sb, block);
693 			if (unlikely(!new_bh)) {
694 				ext2_free_blocks(inode, block, 1);
695 				mark_inode_dirty(inode);
696 				error = -ENOMEM;
697 				goto cleanup;
698 			}
699 			lock_buffer(new_bh);
700 			memcpy(new_bh->b_data, header, new_bh->b_size);
701 			set_buffer_uptodate(new_bh);
702 			unlock_buffer(new_bh);
703 			ext2_xattr_cache_insert(ea_block_cache, new_bh);
704 
705 			ext2_xattr_update_super_block(sb);
706 		}
707 		mark_buffer_dirty(new_bh);
708 		if (IS_SYNC(inode)) {
709 			sync_dirty_buffer(new_bh);
710 			error = -EIO;
711 			if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
712 				goto cleanup;
713 		}
714 	}
715 
716 	/* Update the inode. */
717 	EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
718 	inode->i_ctime = current_time(inode);
719 	if (IS_SYNC(inode)) {
720 		error = sync_inode_metadata(inode, 1);
721 		/* In case sync failed due to ENOSPC the inode was actually
722 		 * written (only some dirty data were not) so we just proceed
723 		 * as if nothing happened and cleanup the unused block */
724 		if (error && error != -ENOSPC) {
725 			if (new_bh && new_bh != old_bh) {
726 				dquot_free_block_nodirty(inode, 1);
727 				mark_inode_dirty(inode);
728 			}
729 			goto cleanup;
730 		}
731 	} else
732 		mark_inode_dirty(inode);
733 
734 	error = 0;
735 	if (old_bh && old_bh != new_bh) {
736 		/*
737 		 * If there was an old block and we are no longer using it,
738 		 * release the old block.
739 		 */
740 		lock_buffer(old_bh);
741 		if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
742 			__u32 hash = le32_to_cpu(HDR(old_bh)->h_hash);
743 
744 			/*
745 			 * This must happen under buffer lock for
746 			 * ext2_xattr_set2() to reliably detect freed block
747 			 */
748 			mb_cache_entry_delete(ea_block_cache, hash,
749 					      old_bh->b_blocknr);
750 			/* Free the old block. */
751 			ea_bdebug(old_bh, "freeing");
752 			ext2_free_blocks(inode, old_bh->b_blocknr, 1);
753 			mark_inode_dirty(inode);
754 			/* We let our caller release old_bh, so we
755 			 * need to duplicate the buffer before. */
756 			get_bh(old_bh);
757 			bforget(old_bh);
758 		} else {
759 			/* Decrement the refcount only. */
760 			le32_add_cpu(&HDR(old_bh)->h_refcount, -1);
761 			dquot_free_block_nodirty(inode, 1);
762 			mark_inode_dirty(inode);
763 			mark_buffer_dirty(old_bh);
764 			ea_bdebug(old_bh, "refcount now=%d",
765 				le32_to_cpu(HDR(old_bh)->h_refcount));
766 		}
767 		unlock_buffer(old_bh);
768 	}
769 
770 cleanup:
771 	brelse(new_bh);
772 
773 	return error;
774 }
775 
776 /*
777  * ext2_xattr_delete_inode()
778  *
779  * Free extended attribute resources associated with this inode. This
780  * is called immediately before an inode is freed.
781  */
782 void
783 ext2_xattr_delete_inode(struct inode *inode)
784 {
785 	struct buffer_head *bh = NULL;
786 	struct ext2_sb_info *sbi = EXT2_SB(inode->i_sb);
787 
788 	down_write(&EXT2_I(inode)->xattr_sem);
789 	if (!EXT2_I(inode)->i_file_acl)
790 		goto cleanup;
791 
792 	if (!ext2_data_block_valid(sbi, EXT2_I(inode)->i_file_acl, 0)) {
793 		ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
794 			"inode %ld: xattr block %d is out of data blocks range",
795 			inode->i_ino, EXT2_I(inode)->i_file_acl);
796 		goto cleanup;
797 	}
798 
799 	bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
800 	if (!bh) {
801 		ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
802 			"inode %ld: block %d read error", inode->i_ino,
803 			EXT2_I(inode)->i_file_acl);
804 		goto cleanup;
805 	}
806 	ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
807 	if (!ext2_xattr_header_valid(HDR(bh))) {
808 		ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
809 			"inode %ld: bad block %d", inode->i_ino,
810 			EXT2_I(inode)->i_file_acl);
811 		goto cleanup;
812 	}
813 	lock_buffer(bh);
814 	if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
815 		__u32 hash = le32_to_cpu(HDR(bh)->h_hash);
816 
817 		/*
818 		 * This must happen under buffer lock for ext2_xattr_set2() to
819 		 * reliably detect freed block
820 		 */
821 		mb_cache_entry_delete(EA_BLOCK_CACHE(inode), hash,
822 				      bh->b_blocknr);
823 		ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
824 		get_bh(bh);
825 		bforget(bh);
826 		unlock_buffer(bh);
827 	} else {
828 		le32_add_cpu(&HDR(bh)->h_refcount, -1);
829 		ea_bdebug(bh, "refcount now=%d",
830 			le32_to_cpu(HDR(bh)->h_refcount));
831 		unlock_buffer(bh);
832 		mark_buffer_dirty(bh);
833 		if (IS_SYNC(inode))
834 			sync_dirty_buffer(bh);
835 		dquot_free_block_nodirty(inode, 1);
836 	}
837 	EXT2_I(inode)->i_file_acl = 0;
838 
839 cleanup:
840 	brelse(bh);
841 	up_write(&EXT2_I(inode)->xattr_sem);
842 }
843 
844 /*
845  * ext2_xattr_cache_insert()
846  *
847  * Create a new entry in the extended attribute cache, and insert
848  * it unless such an entry is already in the cache.
849  *
850  * Returns 0, or a negative error number on failure.
851  */
852 static int
853 ext2_xattr_cache_insert(struct mb_cache *cache, struct buffer_head *bh)
854 {
855 	__u32 hash = le32_to_cpu(HDR(bh)->h_hash);
856 	int error;
857 
858 	error = mb_cache_entry_create(cache, GFP_NOFS, hash, bh->b_blocknr,
859 				      true);
860 	if (error) {
861 		if (error == -EBUSY) {
862 			ea_bdebug(bh, "already in cache (%d cache entries)",
863 				atomic_read(&ext2_xattr_cache->c_entry_count));
864 			error = 0;
865 		}
866 	} else
867 		ea_bdebug(bh, "inserting [%x]", (int)hash);
868 	return error;
869 }
870 
871 /*
872  * ext2_xattr_cmp()
873  *
874  * Compare two extended attribute blocks for equality.
875  *
876  * Returns 0 if the blocks are equal, 1 if they differ, and
877  * a negative error number on errors.
878  */
879 static int
880 ext2_xattr_cmp(struct ext2_xattr_header *header1,
881 	       struct ext2_xattr_header *header2)
882 {
883 	struct ext2_xattr_entry *entry1, *entry2;
884 
885 	entry1 = ENTRY(header1+1);
886 	entry2 = ENTRY(header2+1);
887 	while (!IS_LAST_ENTRY(entry1)) {
888 		if (IS_LAST_ENTRY(entry2))
889 			return 1;
890 		if (entry1->e_hash != entry2->e_hash ||
891 		    entry1->e_name_index != entry2->e_name_index ||
892 		    entry1->e_name_len != entry2->e_name_len ||
893 		    entry1->e_value_size != entry2->e_value_size ||
894 		    memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
895 			return 1;
896 		if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
897 			return -EIO;
898 		if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
899 			   (char *)header2 + le16_to_cpu(entry2->e_value_offs),
900 			   le32_to_cpu(entry1->e_value_size)))
901 			return 1;
902 
903 		entry1 = EXT2_XATTR_NEXT(entry1);
904 		entry2 = EXT2_XATTR_NEXT(entry2);
905 	}
906 	if (!IS_LAST_ENTRY(entry2))
907 		return 1;
908 	return 0;
909 }
910 
911 /*
912  * ext2_xattr_cache_find()
913  *
914  * Find an identical extended attribute block.
915  *
916  * Returns a locked buffer head to the block found, or NULL if such
917  * a block was not found or an error occurred.
918  */
919 static struct buffer_head *
920 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
921 {
922 	__u32 hash = le32_to_cpu(header->h_hash);
923 	struct mb_cache_entry *ce;
924 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
925 
926 	if (!header->h_hash)
927 		return NULL;  /* never share */
928 	ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
929 again:
930 	ce = mb_cache_entry_find_first(ea_block_cache, hash);
931 	while (ce) {
932 		struct buffer_head *bh;
933 
934 		bh = sb_bread(inode->i_sb, ce->e_value);
935 		if (!bh) {
936 			ext2_error(inode->i_sb, "ext2_xattr_cache_find",
937 				"inode %ld: block %ld read error",
938 				inode->i_ino, (unsigned long) ce->e_value);
939 		} else {
940 			lock_buffer(bh);
941 			/*
942 			 * We have to be careful about races with freeing or
943 			 * rehashing of xattr block. Once we hold buffer lock
944 			 * xattr block's state is stable so we can check
945 			 * whether the block got freed / rehashed or not.
946 			 * Since we unhash mbcache entry under buffer lock when
947 			 * freeing / rehashing xattr block, checking whether
948 			 * entry is still hashed is reliable.
949 			 */
950 			if (hlist_bl_unhashed(&ce->e_hash_list)) {
951 				mb_cache_entry_put(ea_block_cache, ce);
952 				unlock_buffer(bh);
953 				brelse(bh);
954 				goto again;
955 			} else if (le32_to_cpu(HDR(bh)->h_refcount) >
956 				   EXT2_XATTR_REFCOUNT_MAX) {
957 				ea_idebug(inode, "block %ld refcount %d>%d",
958 					  (unsigned long) ce->e_value,
959 					  le32_to_cpu(HDR(bh)->h_refcount),
960 					  EXT2_XATTR_REFCOUNT_MAX);
961 			} else if (!ext2_xattr_cmp(header, HDR(bh))) {
962 				ea_bdebug(bh, "b_count=%d",
963 					  atomic_read(&(bh->b_count)));
964 				mb_cache_entry_touch(ea_block_cache, ce);
965 				mb_cache_entry_put(ea_block_cache, ce);
966 				return bh;
967 			}
968 			unlock_buffer(bh);
969 			brelse(bh);
970 		}
971 		ce = mb_cache_entry_find_next(ea_block_cache, ce);
972 	}
973 	return NULL;
974 }
975 
976 #define NAME_HASH_SHIFT 5
977 #define VALUE_HASH_SHIFT 16
978 
979 /*
980  * ext2_xattr_hash_entry()
981  *
982  * Compute the hash of an extended attribute.
983  */
984 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
985 					 struct ext2_xattr_entry *entry)
986 {
987 	__u32 hash = 0;
988 	char *name = entry->e_name;
989 	int n;
990 
991 	for (n=0; n < entry->e_name_len; n++) {
992 		hash = (hash << NAME_HASH_SHIFT) ^
993 		       (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
994 		       *name++;
995 	}
996 
997 	if (entry->e_value_block == 0 && entry->e_value_size != 0) {
998 		__le32 *value = (__le32 *)((char *)header +
999 			le16_to_cpu(entry->e_value_offs));
1000 		for (n = (le32_to_cpu(entry->e_value_size) +
1001 		     EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
1002 			hash = (hash << VALUE_HASH_SHIFT) ^
1003 			       (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1004 			       le32_to_cpu(*value++);
1005 		}
1006 	}
1007 	entry->e_hash = cpu_to_le32(hash);
1008 }
1009 
1010 #undef NAME_HASH_SHIFT
1011 #undef VALUE_HASH_SHIFT
1012 
1013 #define BLOCK_HASH_SHIFT 16
1014 
1015 /*
1016  * ext2_xattr_rehash()
1017  *
1018  * Re-compute the extended attribute hash value after an entry has changed.
1019  */
1020 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
1021 			      struct ext2_xattr_entry *entry)
1022 {
1023 	struct ext2_xattr_entry *here;
1024 	__u32 hash = 0;
1025 
1026 	ext2_xattr_hash_entry(header, entry);
1027 	here = ENTRY(header+1);
1028 	while (!IS_LAST_ENTRY(here)) {
1029 		if (!here->e_hash) {
1030 			/* Block is not shared if an entry's hash value == 0 */
1031 			hash = 0;
1032 			break;
1033 		}
1034 		hash = (hash << BLOCK_HASH_SHIFT) ^
1035 		       (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1036 		       le32_to_cpu(here->e_hash);
1037 		here = EXT2_XATTR_NEXT(here);
1038 	}
1039 	header->h_hash = cpu_to_le32(hash);
1040 }
1041 
1042 #undef BLOCK_HASH_SHIFT
1043 
1044 #define HASH_BUCKET_BITS 10
1045 
1046 struct mb_cache *ext2_xattr_create_cache(void)
1047 {
1048 	return mb_cache_create(HASH_BUCKET_BITS);
1049 }
1050 
1051 void ext2_xattr_destroy_cache(struct mb_cache *cache)
1052 {
1053 	if (cache)
1054 		mb_cache_destroy(cache);
1055 }
1056