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