wlcore: Add RX_BA_WIN_SIZE_CHANGE_EVENT event
[linux/fpc-iii.git] / fs / ext4 / xattr.c
blob7c23363ecf19b7e609a410411e99c4da33b0f989
1 /*
2 * linux/fs/ext4/xattr.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
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>.
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.
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
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 * +------------------+
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.
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.
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"
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
83 static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
84 static struct buffer_head *ext4_xattr_cache_find(struct inode *,
85 struct ext4_xattr_header *,
86 struct mb_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);
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
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
117 #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
118 inode->i_sb->s_fs_info)->s_mb_cache)
120 static __le32 ext4_xattr_block_csum(struct inode *inode,
121 sector_t block_nr,
122 struct ext4_xattr_header *hdr)
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);
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);
138 return cpu_to_le32(csum);
141 static int ext4_xattr_block_csum_verify(struct inode *inode,
142 sector_t block_nr,
143 struct ext4_xattr_header *hdr)
145 if (ext4_has_metadata_csum(inode->i_sb) &&
146 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
147 return 0;
148 return 1;
151 static void ext4_xattr_block_csum_set(struct inode *inode,
152 sector_t block_nr,
153 struct ext4_xattr_header *hdr)
155 if (!ext4_has_metadata_csum(inode->i_sb))
156 return;
158 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
161 static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
162 struct inode *inode,
163 struct buffer_head *bh)
165 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
166 return ext4_handle_dirty_metadata(handle, inode, bh);
169 static inline const struct xattr_handler *
170 ext4_xattr_handler(int name_index)
172 const struct xattr_handler *handler = NULL;
174 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
175 handler = ext4_xattr_handler_map[name_index];
176 return handler;
180 * Inode operation listxattr()
182 * d_inode(dentry)->i_mutex: don't care
184 ssize_t
185 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
187 return ext4_xattr_list(dentry, buffer, size);
190 static int
191 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
192 void *value_start)
194 struct ext4_xattr_entry *e = entry;
196 while (!IS_LAST_ENTRY(e)) {
197 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
198 if ((void *)next >= end)
199 return -EFSCORRUPTED;
200 e = next;
203 while (!IS_LAST_ENTRY(entry)) {
204 if (entry->e_value_size != 0 &&
205 (value_start + le16_to_cpu(entry->e_value_offs) <
206 (void *)e + sizeof(__u32) ||
207 value_start + le16_to_cpu(entry->e_value_offs) +
208 le32_to_cpu(entry->e_value_size) > end))
209 return -EFSCORRUPTED;
210 entry = EXT4_XATTR_NEXT(entry);
213 return 0;
216 static inline int
217 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
219 int error;
221 if (buffer_verified(bh))
222 return 0;
224 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
225 BHDR(bh)->h_blocks != cpu_to_le32(1))
226 return -EFSCORRUPTED;
227 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
228 return -EFSBADCRC;
229 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
230 bh->b_data);
231 if (!error)
232 set_buffer_verified(bh);
233 return error;
236 static int
237 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
238 void *end, const char *function, unsigned int line)
240 struct ext4_xattr_entry *entry = IFIRST(header);
241 int error = -EFSCORRUPTED;
243 if (((void *) header >= end) ||
244 (header->h_magic != le32_to_cpu(EXT4_XATTR_MAGIC)))
245 goto errout;
246 error = ext4_xattr_check_names(entry, end, entry);
247 errout:
248 if (error)
249 __ext4_error_inode(inode, function, line, 0,
250 "corrupted in-inode xattr");
251 return error;
254 #define xattr_check_inode(inode, header, end) \
255 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
257 static inline int
258 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
260 size_t value_size = le32_to_cpu(entry->e_value_size);
262 if (entry->e_value_block != 0 || value_size > size ||
263 le16_to_cpu(entry->e_value_offs) + value_size > size)
264 return -EFSCORRUPTED;
265 return 0;
268 static int
269 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
270 const char *name, size_t size, int sorted)
272 struct ext4_xattr_entry *entry;
273 size_t name_len;
274 int cmp = 1;
276 if (name == NULL)
277 return -EINVAL;
278 name_len = strlen(name);
279 entry = *pentry;
280 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
281 cmp = name_index - entry->e_name_index;
282 if (!cmp)
283 cmp = name_len - entry->e_name_len;
284 if (!cmp)
285 cmp = memcmp(name, entry->e_name, name_len);
286 if (cmp <= 0 && (sorted || cmp == 0))
287 break;
289 *pentry = entry;
290 if (!cmp && ext4_xattr_check_entry(entry, size))
291 return -EFSCORRUPTED;
292 return cmp ? -ENODATA : 0;
295 static int
296 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
297 void *buffer, size_t buffer_size)
299 struct buffer_head *bh = NULL;
300 struct ext4_xattr_entry *entry;
301 size_t size;
302 int error;
303 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
305 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
306 name_index, name, buffer, (long)buffer_size);
308 error = -ENODATA;
309 if (!EXT4_I(inode)->i_file_acl)
310 goto cleanup;
311 ea_idebug(inode, "reading block %llu",
312 (unsigned long long)EXT4_I(inode)->i_file_acl);
313 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
314 if (!bh)
315 goto cleanup;
316 ea_bdebug(bh, "b_count=%d, refcount=%d",
317 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
318 if (ext4_xattr_check_block(inode, bh)) {
319 bad_block:
320 EXT4_ERROR_INODE(inode, "bad block %llu",
321 EXT4_I(inode)->i_file_acl);
322 error = -EFSCORRUPTED;
323 goto cleanup;
325 ext4_xattr_cache_insert(ext4_mb_cache, bh);
326 entry = BFIRST(bh);
327 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
328 if (error == -EFSCORRUPTED)
329 goto bad_block;
330 if (error)
331 goto cleanup;
332 size = le32_to_cpu(entry->e_value_size);
333 if (buffer) {
334 error = -ERANGE;
335 if (size > buffer_size)
336 goto cleanup;
337 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
338 size);
340 error = size;
342 cleanup:
343 brelse(bh);
344 return error;
348 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
349 void *buffer, size_t buffer_size)
351 struct ext4_xattr_ibody_header *header;
352 struct ext4_xattr_entry *entry;
353 struct ext4_inode *raw_inode;
354 struct ext4_iloc iloc;
355 size_t size;
356 void *end;
357 int error;
359 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
360 return -ENODATA;
361 error = ext4_get_inode_loc(inode, &iloc);
362 if (error)
363 return error;
364 raw_inode = ext4_raw_inode(&iloc);
365 header = IHDR(inode, raw_inode);
366 entry = IFIRST(header);
367 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
368 error = xattr_check_inode(inode, header, end);
369 if (error)
370 goto cleanup;
371 error = ext4_xattr_find_entry(&entry, name_index, name,
372 end - (void *)entry, 0);
373 if (error)
374 goto cleanup;
375 size = le32_to_cpu(entry->e_value_size);
376 if (buffer) {
377 error = -ERANGE;
378 if (size > buffer_size)
379 goto cleanup;
380 memcpy(buffer, (void *)IFIRST(header) +
381 le16_to_cpu(entry->e_value_offs), size);
383 error = size;
385 cleanup:
386 brelse(iloc.bh);
387 return error;
391 * ext4_xattr_get()
393 * Copy an extended attribute into the buffer
394 * provided, or compute the buffer size required.
395 * Buffer is NULL to compute the size of the buffer required.
397 * Returns a negative error number on failure, or the number of bytes
398 * used / required on success.
401 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
402 void *buffer, size_t buffer_size)
404 int error;
406 if (strlen(name) > 255)
407 return -ERANGE;
409 down_read(&EXT4_I(inode)->xattr_sem);
410 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
411 buffer_size);
412 if (error == -ENODATA)
413 error = ext4_xattr_block_get(inode, name_index, name, buffer,
414 buffer_size);
415 up_read(&EXT4_I(inode)->xattr_sem);
416 return error;
419 static int
420 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
421 char *buffer, size_t buffer_size)
423 size_t rest = buffer_size;
425 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
426 const struct xattr_handler *handler =
427 ext4_xattr_handler(entry->e_name_index);
429 if (handler) {
430 size_t size = handler->list(handler, dentry, buffer,
431 rest, entry->e_name,
432 entry->e_name_len);
433 if (buffer) {
434 if (size > rest)
435 return -ERANGE;
436 buffer += size;
438 rest -= size;
441 return buffer_size - rest;
444 static int
445 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
447 struct inode *inode = d_inode(dentry);
448 struct buffer_head *bh = NULL;
449 int error;
450 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
452 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
453 buffer, (long)buffer_size);
455 error = 0;
456 if (!EXT4_I(inode)->i_file_acl)
457 goto cleanup;
458 ea_idebug(inode, "reading block %llu",
459 (unsigned long long)EXT4_I(inode)->i_file_acl);
460 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
461 error = -EIO;
462 if (!bh)
463 goto cleanup;
464 ea_bdebug(bh, "b_count=%d, refcount=%d",
465 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
466 if (ext4_xattr_check_block(inode, bh)) {
467 EXT4_ERROR_INODE(inode, "bad block %llu",
468 EXT4_I(inode)->i_file_acl);
469 error = -EFSCORRUPTED;
470 goto cleanup;
472 ext4_xattr_cache_insert(ext4_mb_cache, bh);
473 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
475 cleanup:
476 brelse(bh);
478 return error;
481 static int
482 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
484 struct inode *inode = d_inode(dentry);
485 struct ext4_xattr_ibody_header *header;
486 struct ext4_inode *raw_inode;
487 struct ext4_iloc iloc;
488 void *end;
489 int error;
491 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
492 return 0;
493 error = ext4_get_inode_loc(inode, &iloc);
494 if (error)
495 return error;
496 raw_inode = ext4_raw_inode(&iloc);
497 header = IHDR(inode, raw_inode);
498 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
499 error = xattr_check_inode(inode, header, end);
500 if (error)
501 goto cleanup;
502 error = ext4_xattr_list_entries(dentry, IFIRST(header),
503 buffer, buffer_size);
505 cleanup:
506 brelse(iloc.bh);
507 return error;
511 * ext4_xattr_list()
513 * Copy a list of attribute names into the buffer
514 * provided, or compute the buffer size required.
515 * Buffer is NULL to compute the size of the buffer required.
517 * Returns a negative error number on failure, or the number of bytes
518 * used / required on success.
520 static int
521 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
523 int ret, ret2;
525 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
526 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
527 if (ret < 0)
528 goto errout;
529 if (buffer) {
530 buffer += ret;
531 buffer_size -= ret;
533 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
534 if (ret < 0)
535 goto errout;
536 ret += ret2;
537 errout:
538 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
539 return ret;
543 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
544 * not set, set it.
546 static void ext4_xattr_update_super_block(handle_t *handle,
547 struct super_block *sb)
549 if (ext4_has_feature_xattr(sb))
550 return;
552 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
553 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
554 ext4_set_feature_xattr(sb);
555 ext4_handle_dirty_super(handle, sb);
560 * Release the xattr block BH: If the reference count is > 1, decrement it;
561 * otherwise free the block.
563 static void
564 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
565 struct buffer_head *bh)
567 struct mb_cache_entry *ce = NULL;
568 int error = 0;
569 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
571 ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
572 BUFFER_TRACE(bh, "get_write_access");
573 error = ext4_journal_get_write_access(handle, bh);
574 if (error)
575 goto out;
577 lock_buffer(bh);
578 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
579 ea_bdebug(bh, "refcount now=0; freeing");
580 if (ce)
581 mb_cache_entry_free(ce);
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 if (ce)
590 mb_cache_entry_release(ce);
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_xattr_block() 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.
601 if (ext4_handle_valid(handle))
602 error = ext4_handle_dirty_xattr_block(handle, inode,
603 bh);
604 unlock_buffer(bh);
605 if (!ext4_handle_valid(handle))
606 error = ext4_handle_dirty_xattr_block(handle, inode,
607 bh);
608 if (IS_SYNC(inode))
609 ext4_handle_sync(handle);
610 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
611 ea_bdebug(bh, "refcount now=%d; releasing",
612 le32_to_cpu(BHDR(bh)->h_refcount));
614 out:
615 ext4_std_error(inode->i_sb, error);
616 return;
620 * Find the available free space for EAs. This also returns the total number of
621 * bytes used by EA entries.
623 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
624 size_t *min_offs, void *base, int *total)
626 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
627 if (!last->e_value_block && last->e_value_size) {
628 size_t offs = le16_to_cpu(last->e_value_offs);
629 if (offs < *min_offs)
630 *min_offs = offs;
632 if (total)
633 *total += EXT4_XATTR_LEN(last->e_name_len);
635 return (*min_offs - ((void *)last - base) - sizeof(__u32));
638 static int
639 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
641 struct ext4_xattr_entry *last;
642 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
644 /* Compute min_offs and last. */
645 last = s->first;
646 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
647 if (!last->e_value_block && last->e_value_size) {
648 size_t offs = le16_to_cpu(last->e_value_offs);
649 if (offs < min_offs)
650 min_offs = offs;
653 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
654 if (!s->not_found) {
655 if (!s->here->e_value_block && s->here->e_value_size) {
656 size_t size = le32_to_cpu(s->here->e_value_size);
657 free += EXT4_XATTR_SIZE(size);
659 free += EXT4_XATTR_LEN(name_len);
661 if (i->value) {
662 if (free < EXT4_XATTR_LEN(name_len) +
663 EXT4_XATTR_SIZE(i->value_len))
664 return -ENOSPC;
667 if (i->value && s->not_found) {
668 /* Insert the new name. */
669 size_t size = EXT4_XATTR_LEN(name_len);
670 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
671 memmove((void *)s->here + size, s->here, rest);
672 memset(s->here, 0, size);
673 s->here->e_name_index = i->name_index;
674 s->here->e_name_len = name_len;
675 memcpy(s->here->e_name, i->name, name_len);
676 } else {
677 if (!s->here->e_value_block && s->here->e_value_size) {
678 void *first_val = s->base + min_offs;
679 size_t offs = le16_to_cpu(s->here->e_value_offs);
680 void *val = s->base + offs;
681 size_t size = EXT4_XATTR_SIZE(
682 le32_to_cpu(s->here->e_value_size));
684 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
685 /* The old and the new value have the same
686 size. Just replace. */
687 s->here->e_value_size =
688 cpu_to_le32(i->value_len);
689 if (i->value == EXT4_ZERO_XATTR_VALUE) {
690 memset(val, 0, size);
691 } else {
692 /* Clear pad bytes first. */
693 memset(val + size - EXT4_XATTR_PAD, 0,
694 EXT4_XATTR_PAD);
695 memcpy(val, i->value, i->value_len);
697 return 0;
700 /* Remove the old value. */
701 memmove(first_val + size, first_val, val - first_val);
702 memset(first_val, 0, size);
703 s->here->e_value_size = 0;
704 s->here->e_value_offs = 0;
705 min_offs += size;
707 /* Adjust all value offsets. */
708 last = s->first;
709 while (!IS_LAST_ENTRY(last)) {
710 size_t o = le16_to_cpu(last->e_value_offs);
711 if (!last->e_value_block &&
712 last->e_value_size && o < offs)
713 last->e_value_offs =
714 cpu_to_le16(o + size);
715 last = EXT4_XATTR_NEXT(last);
718 if (!i->value) {
719 /* Remove the old name. */
720 size_t size = EXT4_XATTR_LEN(name_len);
721 last = ENTRY((void *)last - size);
722 memmove(s->here, (void *)s->here + size,
723 (void *)last - (void *)s->here + sizeof(__u32));
724 memset(last, 0, size);
728 if (i->value) {
729 /* Insert the new value. */
730 s->here->e_value_size = cpu_to_le32(i->value_len);
731 if (i->value_len) {
732 size_t size = EXT4_XATTR_SIZE(i->value_len);
733 void *val = s->base + min_offs - size;
734 s->here->e_value_offs = cpu_to_le16(min_offs - size);
735 if (i->value == EXT4_ZERO_XATTR_VALUE) {
736 memset(val, 0, size);
737 } else {
738 /* Clear the pad bytes first. */
739 memset(val + size - EXT4_XATTR_PAD, 0,
740 EXT4_XATTR_PAD);
741 memcpy(val, i->value, i->value_len);
745 return 0;
748 struct ext4_xattr_block_find {
749 struct ext4_xattr_search s;
750 struct buffer_head *bh;
753 static int
754 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
755 struct ext4_xattr_block_find *bs)
757 struct super_block *sb = inode->i_sb;
758 int error;
760 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
761 i->name_index, i->name, i->value, (long)i->value_len);
763 if (EXT4_I(inode)->i_file_acl) {
764 /* The inode already has an extended attribute block. */
765 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
766 error = -EIO;
767 if (!bs->bh)
768 goto cleanup;
769 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
770 atomic_read(&(bs->bh->b_count)),
771 le32_to_cpu(BHDR(bs->bh)->h_refcount));
772 if (ext4_xattr_check_block(inode, bs->bh)) {
773 EXT4_ERROR_INODE(inode, "bad block %llu",
774 EXT4_I(inode)->i_file_acl);
775 error = -EFSCORRUPTED;
776 goto cleanup;
778 /* Find the named attribute. */
779 bs->s.base = BHDR(bs->bh);
780 bs->s.first = BFIRST(bs->bh);
781 bs->s.end = bs->bh->b_data + bs->bh->b_size;
782 bs->s.here = bs->s.first;
783 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
784 i->name, bs->bh->b_size, 1);
785 if (error && error != -ENODATA)
786 goto cleanup;
787 bs->s.not_found = error;
789 error = 0;
791 cleanup:
792 return error;
795 static int
796 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
797 struct ext4_xattr_info *i,
798 struct ext4_xattr_block_find *bs)
800 struct super_block *sb = inode->i_sb;
801 struct buffer_head *new_bh = NULL;
802 struct ext4_xattr_search *s = &bs->s;
803 struct mb_cache_entry *ce = NULL;
804 int error = 0;
805 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
807 #define header(x) ((struct ext4_xattr_header *)(x))
809 if (i->value && i->value_len > sb->s_blocksize)
810 return -ENOSPC;
811 if (s->base) {
812 ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
813 bs->bh->b_blocknr);
814 BUFFER_TRACE(bs->bh, "get_write_access");
815 error = ext4_journal_get_write_access(handle, bs->bh);
816 if (error)
817 goto cleanup;
818 lock_buffer(bs->bh);
820 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
821 if (ce) {
822 mb_cache_entry_free(ce);
823 ce = NULL;
825 ea_bdebug(bs->bh, "modifying in-place");
826 error = ext4_xattr_set_entry(i, s);
827 if (!error) {
828 if (!IS_LAST_ENTRY(s->first))
829 ext4_xattr_rehash(header(s->base),
830 s->here);
831 ext4_xattr_cache_insert(ext4_mb_cache,
832 bs->bh);
834 unlock_buffer(bs->bh);
835 if (error == -EFSCORRUPTED)
836 goto bad_block;
837 if (!error)
838 error = ext4_handle_dirty_xattr_block(handle,
839 inode,
840 bs->bh);
841 if (error)
842 goto cleanup;
843 goto inserted;
844 } else {
845 int offset = (char *)s->here - bs->bh->b_data;
847 unlock_buffer(bs->bh);
848 if (ce) {
849 mb_cache_entry_release(ce);
850 ce = NULL;
852 ea_bdebug(bs->bh, "cloning");
853 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
854 error = -ENOMEM;
855 if (s->base == NULL)
856 goto cleanup;
857 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
858 s->first = ENTRY(header(s->base)+1);
859 header(s->base)->h_refcount = cpu_to_le32(1);
860 s->here = ENTRY(s->base + offset);
861 s->end = s->base + bs->bh->b_size;
863 } else {
864 /* Allocate a buffer where we construct the new block. */
865 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
866 /* assert(header == s->base) */
867 error = -ENOMEM;
868 if (s->base == NULL)
869 goto cleanup;
870 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
871 header(s->base)->h_blocks = cpu_to_le32(1);
872 header(s->base)->h_refcount = cpu_to_le32(1);
873 s->first = ENTRY(header(s->base)+1);
874 s->here = ENTRY(header(s->base)+1);
875 s->end = s->base + sb->s_blocksize;
878 error = ext4_xattr_set_entry(i, s);
879 if (error == -EFSCORRUPTED)
880 goto bad_block;
881 if (error)
882 goto cleanup;
883 if (!IS_LAST_ENTRY(s->first))
884 ext4_xattr_rehash(header(s->base), s->here);
886 inserted:
887 if (!IS_LAST_ENTRY(s->first)) {
888 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
889 if (new_bh) {
890 /* We found an identical block in the cache. */
891 if (new_bh == bs->bh)
892 ea_bdebug(new_bh, "keeping");
893 else {
894 /* The old block is released after updating
895 the inode. */
896 error = dquot_alloc_block(inode,
897 EXT4_C2B(EXT4_SB(sb), 1));
898 if (error)
899 goto cleanup;
900 BUFFER_TRACE(new_bh, "get_write_access");
901 error = ext4_journal_get_write_access(handle,
902 new_bh);
903 if (error)
904 goto cleanup_dquot;
905 lock_buffer(new_bh);
906 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
907 ea_bdebug(new_bh, "reusing; refcount now=%d",
908 le32_to_cpu(BHDR(new_bh)->h_refcount));
909 unlock_buffer(new_bh);
910 error = ext4_handle_dirty_xattr_block(handle,
911 inode,
912 new_bh);
913 if (error)
914 goto cleanup_dquot;
916 mb_cache_entry_release(ce);
917 ce = NULL;
918 } else if (bs->bh && s->base == bs->bh->b_data) {
919 /* We were modifying this block in-place. */
920 ea_bdebug(bs->bh, "keeping this block");
921 new_bh = bs->bh;
922 get_bh(new_bh);
923 } else {
924 /* We need to allocate a new block */
925 ext4_fsblk_t goal, block;
927 goal = ext4_group_first_block_no(sb,
928 EXT4_I(inode)->i_block_group);
930 /* non-extent files can't have physical blocks past 2^32 */
931 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
932 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
934 block = ext4_new_meta_blocks(handle, inode, goal, 0,
935 NULL, &error);
936 if (error)
937 goto cleanup;
939 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
940 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
942 ea_idebug(inode, "creating block %llu",
943 (unsigned long long)block);
945 new_bh = sb_getblk(sb, block);
946 if (unlikely(!new_bh)) {
947 error = -ENOMEM;
948 getblk_failed:
949 ext4_free_blocks(handle, inode, NULL, block, 1,
950 EXT4_FREE_BLOCKS_METADATA);
951 goto cleanup;
953 lock_buffer(new_bh);
954 error = ext4_journal_get_create_access(handle, new_bh);
955 if (error) {
956 unlock_buffer(new_bh);
957 error = -EIO;
958 goto getblk_failed;
960 memcpy(new_bh->b_data, s->base, new_bh->b_size);
961 set_buffer_uptodate(new_bh);
962 unlock_buffer(new_bh);
963 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
964 error = ext4_handle_dirty_xattr_block(handle,
965 inode, new_bh);
966 if (error)
967 goto cleanup;
971 /* Update the inode. */
972 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
974 /* Drop the previous xattr block. */
975 if (bs->bh && bs->bh != new_bh)
976 ext4_xattr_release_block(handle, inode, bs->bh);
977 error = 0;
979 cleanup:
980 if (ce)
981 mb_cache_entry_release(ce);
982 brelse(new_bh);
983 if (!(bs->bh && s->base == bs->bh->b_data))
984 kfree(s->base);
986 return error;
988 cleanup_dquot:
989 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
990 goto cleanup;
992 bad_block:
993 EXT4_ERROR_INODE(inode, "bad block %llu",
994 EXT4_I(inode)->i_file_acl);
995 goto cleanup;
997 #undef header
1000 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1001 struct ext4_xattr_ibody_find *is)
1003 struct ext4_xattr_ibody_header *header;
1004 struct ext4_inode *raw_inode;
1005 int error;
1007 if (EXT4_I(inode)->i_extra_isize == 0)
1008 return 0;
1009 raw_inode = ext4_raw_inode(&is->iloc);
1010 header = IHDR(inode, raw_inode);
1011 is->s.base = is->s.first = IFIRST(header);
1012 is->s.here = is->s.first;
1013 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1014 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
1015 error = xattr_check_inode(inode, header, is->s.end);
1016 if (error)
1017 return error;
1018 /* Find the named attribute. */
1019 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
1020 i->name, is->s.end -
1021 (void *)is->s.base, 0);
1022 if (error && error != -ENODATA)
1023 return error;
1024 is->s.not_found = error;
1026 return 0;
1029 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1030 struct ext4_xattr_info *i,
1031 struct ext4_xattr_ibody_find *is)
1033 struct ext4_xattr_ibody_header *header;
1034 struct ext4_xattr_search *s = &is->s;
1035 int error;
1037 if (EXT4_I(inode)->i_extra_isize == 0)
1038 return -ENOSPC;
1039 error = ext4_xattr_set_entry(i, s);
1040 if (error) {
1041 if (error == -ENOSPC &&
1042 ext4_has_inline_data(inode)) {
1043 error = ext4_try_to_evict_inline_data(handle, inode,
1044 EXT4_XATTR_LEN(strlen(i->name) +
1045 EXT4_XATTR_SIZE(i->value_len)));
1046 if (error)
1047 return error;
1048 error = ext4_xattr_ibody_find(inode, i, is);
1049 if (error)
1050 return error;
1051 error = ext4_xattr_set_entry(i, s);
1053 if (error)
1054 return error;
1056 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1057 if (!IS_LAST_ENTRY(s->first)) {
1058 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1059 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1060 } else {
1061 header->h_magic = cpu_to_le32(0);
1062 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1064 return 0;
1067 static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1068 struct ext4_xattr_info *i,
1069 struct ext4_xattr_ibody_find *is)
1071 struct ext4_xattr_ibody_header *header;
1072 struct ext4_xattr_search *s = &is->s;
1073 int error;
1075 if (EXT4_I(inode)->i_extra_isize == 0)
1076 return -ENOSPC;
1077 error = ext4_xattr_set_entry(i, s);
1078 if (error)
1079 return error;
1080 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1081 if (!IS_LAST_ENTRY(s->first)) {
1082 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1083 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1084 } else {
1085 header->h_magic = cpu_to_le32(0);
1086 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1088 return 0;
1092 * ext4_xattr_set_handle()
1094 * Create, replace or remove an extended attribute for this inode. Value
1095 * is NULL to remove an existing extended attribute, and non-NULL to
1096 * either replace an existing extended attribute, or create a new extended
1097 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1098 * specify that an extended attribute must exist and must not exist
1099 * previous to the call, respectively.
1101 * Returns 0, or a negative error number on failure.
1104 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
1105 const char *name, const void *value, size_t value_len,
1106 int flags)
1108 struct ext4_xattr_info i = {
1109 .name_index = name_index,
1110 .name = name,
1111 .value = value,
1112 .value_len = value_len,
1115 struct ext4_xattr_ibody_find is = {
1116 .s = { .not_found = -ENODATA, },
1118 struct ext4_xattr_block_find bs = {
1119 .s = { .not_found = -ENODATA, },
1121 unsigned long no_expand;
1122 int error;
1124 if (!name)
1125 return -EINVAL;
1126 if (strlen(name) > 255)
1127 return -ERANGE;
1128 down_write(&EXT4_I(inode)->xattr_sem);
1129 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1130 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1132 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1133 if (error)
1134 goto cleanup;
1136 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1137 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1138 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1139 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1142 error = ext4_xattr_ibody_find(inode, &i, &is);
1143 if (error)
1144 goto cleanup;
1145 if (is.s.not_found)
1146 error = ext4_xattr_block_find(inode, &i, &bs);
1147 if (error)
1148 goto cleanup;
1149 if (is.s.not_found && bs.s.not_found) {
1150 error = -ENODATA;
1151 if (flags & XATTR_REPLACE)
1152 goto cleanup;
1153 error = 0;
1154 if (!value)
1155 goto cleanup;
1156 } else {
1157 error = -EEXIST;
1158 if (flags & XATTR_CREATE)
1159 goto cleanup;
1161 if (!value) {
1162 if (!is.s.not_found)
1163 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1164 else if (!bs.s.not_found)
1165 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1166 } else {
1167 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1168 if (!error && !bs.s.not_found) {
1169 i.value = NULL;
1170 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1171 } else if (error == -ENOSPC) {
1172 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1173 error = ext4_xattr_block_find(inode, &i, &bs);
1174 if (error)
1175 goto cleanup;
1177 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1178 if (error)
1179 goto cleanup;
1180 if (!is.s.not_found) {
1181 i.value = NULL;
1182 error = ext4_xattr_ibody_set(handle, inode, &i,
1183 &is);
1187 if (!error) {
1188 ext4_xattr_update_super_block(handle, inode->i_sb);
1189 inode->i_ctime = ext4_current_time(inode);
1190 if (!value)
1191 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1192 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1194 * The bh is consumed by ext4_mark_iloc_dirty, even with
1195 * error != 0.
1197 is.iloc.bh = NULL;
1198 if (IS_SYNC(inode))
1199 ext4_handle_sync(handle);
1202 cleanup:
1203 brelse(is.iloc.bh);
1204 brelse(bs.bh);
1205 if (no_expand == 0)
1206 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1207 up_write(&EXT4_I(inode)->xattr_sem);
1208 return error;
1212 * ext4_xattr_set()
1214 * Like ext4_xattr_set_handle, but start from an inode. This extended
1215 * attribute modification is a filesystem transaction by itself.
1217 * Returns 0, or a negative error number on failure.
1220 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1221 const void *value, size_t value_len, int flags)
1223 handle_t *handle;
1224 int error, retries = 0;
1225 int credits = ext4_jbd2_credits_xattr(inode);
1227 retry:
1228 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
1229 if (IS_ERR(handle)) {
1230 error = PTR_ERR(handle);
1231 } else {
1232 int error2;
1234 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1235 value, value_len, flags);
1236 error2 = ext4_journal_stop(handle);
1237 if (error == -ENOSPC &&
1238 ext4_should_retry_alloc(inode->i_sb, &retries))
1239 goto retry;
1240 if (error == 0)
1241 error = error2;
1244 return error;
1248 * Shift the EA entries in the inode to create space for the increased
1249 * i_extra_isize.
1251 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1252 int value_offs_shift, void *to,
1253 void *from, size_t n, int blocksize)
1255 struct ext4_xattr_entry *last = entry;
1256 int new_offs;
1258 /* Adjust the value offsets of the entries */
1259 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1260 if (!last->e_value_block && last->e_value_size) {
1261 new_offs = le16_to_cpu(last->e_value_offs) +
1262 value_offs_shift;
1263 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1264 > blocksize);
1265 last->e_value_offs = cpu_to_le16(new_offs);
1268 /* Shift the entries by n bytes */
1269 memmove(to, from, n);
1273 * Expand an inode by new_extra_isize bytes when EAs are present.
1274 * Returns 0 on success or negative error number on failure.
1276 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1277 struct ext4_inode *raw_inode, handle_t *handle)
1279 struct ext4_xattr_ibody_header *header;
1280 struct ext4_xattr_entry *entry, *last, *first;
1281 struct buffer_head *bh = NULL;
1282 struct ext4_xattr_ibody_find *is = NULL;
1283 struct ext4_xattr_block_find *bs = NULL;
1284 char *buffer = NULL, *b_entry_name = NULL;
1285 size_t min_offs, free;
1286 int total_ino;
1287 void *base, *start, *end;
1288 int error = 0, tried_min_extra_isize = 0;
1289 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1290 int isize_diff; /* How much do we need to grow i_extra_isize */
1292 down_write(&EXT4_I(inode)->xattr_sem);
1294 * Set EXT4_STATE_NO_EXPAND to avoid recursion when marking inode dirty
1296 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1297 retry:
1298 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
1299 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1300 goto out;
1302 header = IHDR(inode, raw_inode);
1303 entry = IFIRST(header);
1306 * Check if enough free space is available in the inode to shift the
1307 * entries ahead by new_extra_isize.
1310 base = start = entry;
1311 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1312 min_offs = end - base;
1313 last = entry;
1314 total_ino = sizeof(struct ext4_xattr_ibody_header);
1316 error = xattr_check_inode(inode, header, end);
1317 if (error)
1318 goto cleanup;
1320 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1321 if (free >= isize_diff) {
1322 entry = IFIRST(header);
1323 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1324 - new_extra_isize, (void *)raw_inode +
1325 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1326 (void *)header, total_ino,
1327 inode->i_sb->s_blocksize);
1328 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1329 goto out;
1333 * Enough free space isn't available in the inode, check if
1334 * EA block can hold new_extra_isize bytes.
1336 if (EXT4_I(inode)->i_file_acl) {
1337 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1338 error = -EIO;
1339 if (!bh)
1340 goto cleanup;
1341 if (ext4_xattr_check_block(inode, bh)) {
1342 EXT4_ERROR_INODE(inode, "bad block %llu",
1343 EXT4_I(inode)->i_file_acl);
1344 error = -EFSCORRUPTED;
1345 goto cleanup;
1347 base = BHDR(bh);
1348 first = BFIRST(bh);
1349 end = bh->b_data + bh->b_size;
1350 min_offs = end - base;
1351 free = ext4_xattr_free_space(first, &min_offs, base, NULL);
1352 if (free < isize_diff) {
1353 if (!tried_min_extra_isize && s_min_extra_isize) {
1354 tried_min_extra_isize++;
1355 new_extra_isize = s_min_extra_isize;
1356 brelse(bh);
1357 goto retry;
1359 error = -1;
1360 goto cleanup;
1362 } else {
1363 free = inode->i_sb->s_blocksize;
1366 while (isize_diff > 0) {
1367 size_t offs, size, entry_size;
1368 struct ext4_xattr_entry *small_entry = NULL;
1369 struct ext4_xattr_info i = {
1370 .value = NULL,
1371 .value_len = 0,
1373 unsigned int total_size; /* EA entry size + value size */
1374 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1375 unsigned int min_total_size = ~0U;
1377 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1378 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1379 if (!is || !bs) {
1380 error = -ENOMEM;
1381 goto cleanup;
1384 is->s.not_found = -ENODATA;
1385 bs->s.not_found = -ENODATA;
1386 is->iloc.bh = NULL;
1387 bs->bh = NULL;
1389 last = IFIRST(header);
1390 /* Find the entry best suited to be pushed into EA block */
1391 entry = NULL;
1392 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1393 total_size =
1394 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1395 EXT4_XATTR_LEN(last->e_name_len);
1396 if (total_size <= free && total_size < min_total_size) {
1397 if (total_size < isize_diff) {
1398 small_entry = last;
1399 } else {
1400 entry = last;
1401 min_total_size = total_size;
1406 if (entry == NULL) {
1407 if (small_entry) {
1408 entry = small_entry;
1409 } else {
1410 if (!tried_min_extra_isize &&
1411 s_min_extra_isize) {
1412 tried_min_extra_isize++;
1413 new_extra_isize = s_min_extra_isize;
1414 kfree(is); is = NULL;
1415 kfree(bs); bs = NULL;
1416 brelse(bh);
1417 goto retry;
1419 error = -1;
1420 goto cleanup;
1423 offs = le16_to_cpu(entry->e_value_offs);
1424 size = le32_to_cpu(entry->e_value_size);
1425 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1426 i.name_index = entry->e_name_index,
1427 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1428 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1429 if (!buffer || !b_entry_name) {
1430 error = -ENOMEM;
1431 goto cleanup;
1433 /* Save the entry name and the entry value */
1434 memcpy(buffer, (void *)IFIRST(header) + offs,
1435 EXT4_XATTR_SIZE(size));
1436 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1437 b_entry_name[entry->e_name_len] = '\0';
1438 i.name = b_entry_name;
1440 error = ext4_get_inode_loc(inode, &is->iloc);
1441 if (error)
1442 goto cleanup;
1444 error = ext4_xattr_ibody_find(inode, &i, is);
1445 if (error)
1446 goto cleanup;
1448 /* Remove the chosen entry from the inode */
1449 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1450 if (error)
1451 goto cleanup;
1452 total_ino -= entry_size;
1454 entry = IFIRST(header);
1455 if (entry_size + EXT4_XATTR_SIZE(size) >= isize_diff)
1456 shift_bytes = isize_diff;
1457 else
1458 shift_bytes = entry_size + EXT4_XATTR_SIZE(size);
1459 /* Adjust the offsets and shift the remaining entries ahead */
1460 ext4_xattr_shift_entries(entry, -shift_bytes,
1461 (void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
1462 EXT4_I(inode)->i_extra_isize + shift_bytes,
1463 (void *)header, total_ino, inode->i_sb->s_blocksize);
1465 isize_diff -= shift_bytes;
1466 EXT4_I(inode)->i_extra_isize += shift_bytes;
1467 header = IHDR(inode, raw_inode);
1469 i.name = b_entry_name;
1470 i.value = buffer;
1471 i.value_len = size;
1472 error = ext4_xattr_block_find(inode, &i, bs);
1473 if (error)
1474 goto cleanup;
1476 /* Add entry which was removed from the inode into the block */
1477 error = ext4_xattr_block_set(handle, inode, &i, bs);
1478 if (error)
1479 goto cleanup;
1480 kfree(b_entry_name);
1481 kfree(buffer);
1482 b_entry_name = NULL;
1483 buffer = NULL;
1484 brelse(is->iloc.bh);
1485 kfree(is);
1486 kfree(bs);
1488 brelse(bh);
1489 out:
1490 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1491 up_write(&EXT4_I(inode)->xattr_sem);
1492 return 0;
1494 cleanup:
1495 kfree(b_entry_name);
1496 kfree(buffer);
1497 if (is)
1498 brelse(is->iloc.bh);
1499 kfree(is);
1500 kfree(bs);
1501 brelse(bh);
1503 * We deliberately leave EXT4_STATE_NO_EXPAND set here since inode
1504 * size expansion failed.
1506 up_write(&EXT4_I(inode)->xattr_sem);
1507 return error;
1513 * ext4_xattr_delete_inode()
1515 * Free extended attribute resources associated with this inode. This
1516 * is called immediately before an inode is freed. We have exclusive
1517 * access to the inode.
1519 void
1520 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1522 struct buffer_head *bh = NULL;
1524 if (!EXT4_I(inode)->i_file_acl)
1525 goto cleanup;
1526 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1527 if (!bh) {
1528 EXT4_ERROR_INODE(inode, "block %llu read error",
1529 EXT4_I(inode)->i_file_acl);
1530 goto cleanup;
1532 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1533 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1534 EXT4_ERROR_INODE(inode, "bad block %llu",
1535 EXT4_I(inode)->i_file_acl);
1536 goto cleanup;
1538 ext4_xattr_release_block(handle, inode, bh);
1539 EXT4_I(inode)->i_file_acl = 0;
1541 cleanup:
1542 brelse(bh);
1546 * ext4_xattr_put_super()
1548 * This is called when a file system is unmounted.
1550 void
1551 ext4_xattr_put_super(struct super_block *sb)
1553 mb_cache_shrink(sb->s_bdev);
1557 * ext4_xattr_cache_insert()
1559 * Create a new entry in the extended attribute cache, and insert
1560 * it unless such an entry is already in the cache.
1562 * Returns 0, or a negative error number on failure.
1564 static void
1565 ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
1567 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1568 struct mb_cache_entry *ce;
1569 int error;
1571 ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
1572 if (!ce) {
1573 ea_bdebug(bh, "out of memory");
1574 return;
1576 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1577 if (error) {
1578 mb_cache_entry_free(ce);
1579 if (error == -EBUSY) {
1580 ea_bdebug(bh, "already in cache");
1581 error = 0;
1583 } else {
1584 ea_bdebug(bh, "inserting [%x]", (int)hash);
1585 mb_cache_entry_release(ce);
1590 * ext4_xattr_cmp()
1592 * Compare two extended attribute blocks for equality.
1594 * Returns 0 if the blocks are equal, 1 if they differ, and
1595 * a negative error number on errors.
1597 static int
1598 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1599 struct ext4_xattr_header *header2)
1601 struct ext4_xattr_entry *entry1, *entry2;
1603 entry1 = ENTRY(header1+1);
1604 entry2 = ENTRY(header2+1);
1605 while (!IS_LAST_ENTRY(entry1)) {
1606 if (IS_LAST_ENTRY(entry2))
1607 return 1;
1608 if (entry1->e_hash != entry2->e_hash ||
1609 entry1->e_name_index != entry2->e_name_index ||
1610 entry1->e_name_len != entry2->e_name_len ||
1611 entry1->e_value_size != entry2->e_value_size ||
1612 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1613 return 1;
1614 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1615 return -EFSCORRUPTED;
1616 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1617 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1618 le32_to_cpu(entry1->e_value_size)))
1619 return 1;
1621 entry1 = EXT4_XATTR_NEXT(entry1);
1622 entry2 = EXT4_XATTR_NEXT(entry2);
1624 if (!IS_LAST_ENTRY(entry2))
1625 return 1;
1626 return 0;
1630 * ext4_xattr_cache_find()
1632 * Find an identical extended attribute block.
1634 * Returns a pointer to the block found, or NULL if such a block was
1635 * not found or an error occurred.
1637 static struct buffer_head *
1638 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1639 struct mb_cache_entry **pce)
1641 __u32 hash = le32_to_cpu(header->h_hash);
1642 struct mb_cache_entry *ce;
1643 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
1645 if (!header->h_hash)
1646 return NULL; /* never share */
1647 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1648 again:
1649 ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
1650 hash);
1651 while (ce) {
1652 struct buffer_head *bh;
1654 if (IS_ERR(ce)) {
1655 if (PTR_ERR(ce) == -EAGAIN)
1656 goto again;
1657 break;
1659 bh = sb_bread(inode->i_sb, ce->e_block);
1660 if (!bh) {
1661 EXT4_ERROR_INODE(inode, "block %lu read error",
1662 (unsigned long) ce->e_block);
1663 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1664 EXT4_XATTR_REFCOUNT_MAX) {
1665 ea_idebug(inode, "block %lu refcount %d>=%d",
1666 (unsigned long) ce->e_block,
1667 le32_to_cpu(BHDR(bh)->h_refcount),
1668 EXT4_XATTR_REFCOUNT_MAX);
1669 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1670 *pce = ce;
1671 return bh;
1673 brelse(bh);
1674 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1676 return NULL;
1679 #define NAME_HASH_SHIFT 5
1680 #define VALUE_HASH_SHIFT 16
1683 * ext4_xattr_hash_entry()
1685 * Compute the hash of an extended attribute.
1687 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1688 struct ext4_xattr_entry *entry)
1690 __u32 hash = 0;
1691 char *name = entry->e_name;
1692 int n;
1694 for (n = 0; n < entry->e_name_len; n++) {
1695 hash = (hash << NAME_HASH_SHIFT) ^
1696 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1697 *name++;
1700 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1701 __le32 *value = (__le32 *)((char *)header +
1702 le16_to_cpu(entry->e_value_offs));
1703 for (n = (le32_to_cpu(entry->e_value_size) +
1704 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1705 hash = (hash << VALUE_HASH_SHIFT) ^
1706 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1707 le32_to_cpu(*value++);
1710 entry->e_hash = cpu_to_le32(hash);
1713 #undef NAME_HASH_SHIFT
1714 #undef VALUE_HASH_SHIFT
1716 #define BLOCK_HASH_SHIFT 16
1719 * ext4_xattr_rehash()
1721 * Re-compute the extended attribute hash value after an entry has changed.
1723 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1724 struct ext4_xattr_entry *entry)
1726 struct ext4_xattr_entry *here;
1727 __u32 hash = 0;
1729 ext4_xattr_hash_entry(header, entry);
1730 here = ENTRY(header+1);
1731 while (!IS_LAST_ENTRY(here)) {
1732 if (!here->e_hash) {
1733 /* Block is not shared if an entry's hash value == 0 */
1734 hash = 0;
1735 break;
1737 hash = (hash << BLOCK_HASH_SHIFT) ^
1738 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1739 le32_to_cpu(here->e_hash);
1740 here = EXT4_XATTR_NEXT(here);
1742 header->h_hash = cpu_to_le32(hash);
1745 #undef BLOCK_HASH_SHIFT
1747 #define HASH_BUCKET_BITS 10
1749 struct mb_cache *
1750 ext4_xattr_create_cache(char *name)
1752 return mb_cache_create(name, HASH_BUCKET_BITS);
1755 void ext4_xattr_destroy_cache(struct mb_cache *cache)
1757 if (cache)
1758 mb_cache_destroy(cache);