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