HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / ext4 / xattr.c
blob18b9213ce0bd90518a4983ea717071963ca6de0b
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 struct buffer_head *bh)
144 struct ext4_xattr_header *hdr = BHDR(bh);
145 int ret = 1;
147 if (ext4_has_metadata_csum(inode->i_sb)) {
148 lock_buffer(bh);
149 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
150 bh->b_blocknr, hdr));
151 unlock_buffer(bh);
153 return ret;
156 static void ext4_xattr_block_csum_set(struct inode *inode,
157 struct buffer_head *bh)
159 if (ext4_has_metadata_csum(inode->i_sb))
160 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
161 bh->b_blocknr, BHDR(bh));
164 static inline const struct xattr_handler *
165 ext4_xattr_handler(int name_index)
167 const struct xattr_handler *handler = NULL;
169 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
170 handler = ext4_xattr_handler_map[name_index];
171 return handler;
175 * Inode operation listxattr()
177 * d_inode(dentry)->i_mutex: don't care
179 ssize_t
180 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
182 return ext4_xattr_list(dentry, buffer, size);
185 static int
186 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
187 void *value_start)
189 struct ext4_xattr_entry *e = entry;
191 while (!IS_LAST_ENTRY(e)) {
192 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
193 if ((void *)next >= end)
194 return -EFSCORRUPTED;
195 if (strnlen(e->e_name, e->e_name_len) != e->e_name_len)
196 return -EFSCORRUPTED;
197 e = next;
200 while (!IS_LAST_ENTRY(entry)) {
201 if (entry->e_value_size != 0 &&
202 (value_start + le16_to_cpu(entry->e_value_offs) <
203 (void *)e + sizeof(__u32) ||
204 value_start + le16_to_cpu(entry->e_value_offs) +
205 le32_to_cpu(entry->e_value_size) > end))
206 return -EFSCORRUPTED;
207 entry = EXT4_XATTR_NEXT(entry);
210 return 0;
213 static inline int
214 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
216 int error;
218 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
219 BHDR(bh)->h_blocks != cpu_to_le32(1))
220 return -EFSCORRUPTED;
221 if (buffer_verified(bh))
222 return 0;
224 if (!ext4_xattr_block_csum_verify(inode, bh))
225 return -EFSBADCRC;
226 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
227 bh->b_data);
228 if (!error)
229 set_buffer_verified(bh);
230 return error;
233 static int
234 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
235 void *end, const char *function, unsigned int line)
237 struct ext4_xattr_entry *entry = IFIRST(header);
238 int error = -EFSCORRUPTED;
240 if (((void *) header >= end) ||
241 (header->h_magic != le32_to_cpu(EXT4_XATTR_MAGIC)))
242 goto errout;
243 error = ext4_xattr_check_names(entry, end, entry);
244 errout:
245 if (error)
246 __ext4_error_inode(inode, function, line, 0,
247 "corrupted in-inode xattr");
248 return error;
251 #define xattr_check_inode(inode, header, end) \
252 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
254 static inline int
255 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
257 size_t value_size = le32_to_cpu(entry->e_value_size);
259 if (entry->e_value_block != 0 || value_size > size ||
260 le16_to_cpu(entry->e_value_offs) + value_size > size)
261 return -EFSCORRUPTED;
262 return 0;
265 static int
266 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
267 const char *name, size_t size, int sorted)
269 struct ext4_xattr_entry *entry;
270 size_t name_len;
271 int cmp = 1;
273 if (name == NULL)
274 return -EINVAL;
275 name_len = strlen(name);
276 entry = *pentry;
277 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
278 cmp = name_index - entry->e_name_index;
279 if (!cmp)
280 cmp = name_len - entry->e_name_len;
281 if (!cmp)
282 cmp = memcmp(name, entry->e_name, name_len);
283 if (cmp <= 0 && (sorted || cmp == 0))
284 break;
286 *pentry = entry;
287 if (!cmp && ext4_xattr_check_entry(entry, size))
288 return -EFSCORRUPTED;
289 return cmp ? -ENODATA : 0;
292 static int
293 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
294 void *buffer, size_t buffer_size)
296 struct buffer_head *bh = NULL;
297 struct ext4_xattr_entry *entry;
298 size_t size;
299 int error;
300 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
302 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
303 name_index, name, buffer, (long)buffer_size);
305 error = -ENODATA;
306 if (!EXT4_I(inode)->i_file_acl)
307 goto cleanup;
308 ea_idebug(inode, "reading block %llu",
309 (unsigned long long)EXT4_I(inode)->i_file_acl);
310 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
311 if (!bh)
312 goto cleanup;
313 ea_bdebug(bh, "b_count=%d, refcount=%d",
314 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
315 if (ext4_xattr_check_block(inode, bh)) {
316 bad_block:
317 EXT4_ERROR_INODE(inode, "bad block %llu",
318 EXT4_I(inode)->i_file_acl);
319 error = -EFSCORRUPTED;
320 goto cleanup;
322 ext4_xattr_cache_insert(ext4_mb_cache, bh);
323 entry = BFIRST(bh);
324 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
325 if (error == -EFSCORRUPTED)
326 goto bad_block;
327 if (error)
328 goto cleanup;
329 size = le32_to_cpu(entry->e_value_size);
330 if (buffer) {
331 error = -ERANGE;
332 if (size > buffer_size)
333 goto cleanup;
334 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
335 size);
337 error = size;
339 cleanup:
340 brelse(bh);
341 return error;
345 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
346 void *buffer, size_t buffer_size)
348 struct ext4_xattr_ibody_header *header;
349 struct ext4_xattr_entry *entry;
350 struct ext4_inode *raw_inode;
351 struct ext4_iloc iloc;
352 size_t size;
353 void *end;
354 int error;
356 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
357 return -ENODATA;
358 error = ext4_get_inode_loc(inode, &iloc);
359 if (error)
360 return error;
361 raw_inode = ext4_raw_inode(&iloc);
362 header = IHDR(inode, raw_inode);
363 entry = IFIRST(header);
364 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
365 error = xattr_check_inode(inode, header, end);
366 if (error)
367 goto cleanup;
368 error = ext4_xattr_find_entry(&entry, name_index, name,
369 end - (void *)entry, 0);
370 if (error)
371 goto cleanup;
372 size = le32_to_cpu(entry->e_value_size);
373 if (buffer) {
374 error = -ERANGE;
375 if (size > buffer_size)
376 goto cleanup;
377 memcpy(buffer, (void *)IFIRST(header) +
378 le16_to_cpu(entry->e_value_offs), size);
380 error = size;
382 cleanup:
383 brelse(iloc.bh);
384 return error;
388 * ext4_xattr_get()
390 * Copy an extended attribute into the buffer
391 * provided, or compute the buffer size required.
392 * Buffer is NULL to compute the size of the buffer required.
394 * Returns a negative error number on failure, or the number of bytes
395 * used / required on success.
398 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
399 void *buffer, size_t buffer_size)
401 int error;
403 if (strlen(name) > 255)
404 return -ERANGE;
406 down_read(&EXT4_I(inode)->xattr_sem);
407 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
408 buffer_size);
409 if (error == -ENODATA)
410 error = ext4_xattr_block_get(inode, name_index, name, buffer,
411 buffer_size);
412 up_read(&EXT4_I(inode)->xattr_sem);
413 return error;
416 static int
417 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
418 char *buffer, size_t buffer_size)
420 size_t rest = buffer_size;
422 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
423 const struct xattr_handler *handler =
424 ext4_xattr_handler(entry->e_name_index);
426 if (handler) {
427 size_t size = handler->list(handler, dentry, buffer,
428 rest, entry->e_name,
429 entry->e_name_len);
430 if (buffer) {
431 if (size > rest)
432 return -ERANGE;
433 buffer += size;
435 rest -= size;
438 return buffer_size - rest;
441 static int
442 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
444 struct inode *inode = d_inode(dentry);
445 struct buffer_head *bh = NULL;
446 int error;
447 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
449 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
450 buffer, (long)buffer_size);
452 error = 0;
453 if (!EXT4_I(inode)->i_file_acl)
454 goto cleanup;
455 ea_idebug(inode, "reading block %llu",
456 (unsigned long long)EXT4_I(inode)->i_file_acl);
457 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
458 error = -EIO;
459 if (!bh)
460 goto cleanup;
461 ea_bdebug(bh, "b_count=%d, refcount=%d",
462 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
463 if (ext4_xattr_check_block(inode, bh)) {
464 EXT4_ERROR_INODE(inode, "bad block %llu",
465 EXT4_I(inode)->i_file_acl);
466 error = -EFSCORRUPTED;
467 goto cleanup;
469 ext4_xattr_cache_insert(ext4_mb_cache, bh);
470 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
472 cleanup:
473 brelse(bh);
475 return error;
478 static int
479 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
481 struct inode *inode = d_inode(dentry);
482 struct ext4_xattr_ibody_header *header;
483 struct ext4_inode *raw_inode;
484 struct ext4_iloc iloc;
485 void *end;
486 int error;
488 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
489 return 0;
490 error = ext4_get_inode_loc(inode, &iloc);
491 if (error)
492 return error;
493 raw_inode = ext4_raw_inode(&iloc);
494 header = IHDR(inode, raw_inode);
495 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
496 error = xattr_check_inode(inode, header, end);
497 if (error)
498 goto cleanup;
499 error = ext4_xattr_list_entries(dentry, IFIRST(header),
500 buffer, buffer_size);
502 cleanup:
503 brelse(iloc.bh);
504 return error;
508 * ext4_xattr_list()
510 * Copy a list of attribute names into the buffer
511 * provided, or compute the buffer size required.
512 * Buffer is NULL to compute the size of the buffer required.
514 * Returns a negative error number on failure, or the number of bytes
515 * used / required on success.
517 static int
518 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
520 int ret, ret2;
522 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
523 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
524 if (ret < 0)
525 goto errout;
526 if (buffer) {
527 buffer += ret;
528 buffer_size -= ret;
530 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
531 if (ret < 0)
532 goto errout;
533 ret += ret2;
534 errout:
535 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
536 return ret;
540 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
541 * not set, set it.
543 static void ext4_xattr_update_super_block(handle_t *handle,
544 struct super_block *sb)
546 if (ext4_has_feature_xattr(sb))
547 return;
549 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
550 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
551 ext4_set_feature_xattr(sb);
552 ext4_handle_dirty_super(handle, sb);
557 * Release the xattr block BH: If the reference count is > 1, decrement it;
558 * otherwise free the block.
560 static void
561 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
562 struct buffer_head *bh)
564 struct mb_cache_entry *ce = NULL;
565 int error = 0;
566 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
568 ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
569 BUFFER_TRACE(bh, "get_write_access");
570 error = ext4_journal_get_write_access(handle, bh);
571 if (error)
572 goto out;
574 lock_buffer(bh);
575 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
576 ea_bdebug(bh, "refcount now=0; freeing");
577 if (ce)
578 mb_cache_entry_free(ce);
579 get_bh(bh);
580 unlock_buffer(bh);
581 ext4_free_blocks(handle, inode, bh, 0, 1,
582 EXT4_FREE_BLOCKS_METADATA |
583 EXT4_FREE_BLOCKS_FORGET);
584 } else {
585 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
586 if (ce)
587 mb_cache_entry_release(ce);
589 ext4_xattr_block_csum_set(inode, bh);
591 * Beware of this ugliness: Releasing of xattr block references
592 * from different inodes can race and so we have to protect
593 * from a race where someone else frees the block (and releases
594 * its journal_head) before we are done dirtying the buffer. In
595 * nojournal mode this race is harmless and we actually cannot
596 * call ext4_handle_dirty_metadata() with locked buffer as
597 * that function can call sync_dirty_buffer() so for that case
598 * we handle the dirtying after unlocking the buffer.
600 if (ext4_handle_valid(handle))
601 error = ext4_handle_dirty_metadata(handle, inode, bh);
602 unlock_buffer(bh);
603 if (!ext4_handle_valid(handle))
604 error = ext4_handle_dirty_metadata(handle, inode, bh);
605 if (IS_SYNC(inode))
606 ext4_handle_sync(handle);
607 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
608 ea_bdebug(bh, "refcount now=%d; releasing",
609 le32_to_cpu(BHDR(bh)->h_refcount));
611 out:
612 ext4_std_error(inode->i_sb, error);
613 return;
617 * Find the available free space for EAs. This also returns the total number of
618 * bytes used by EA entries.
620 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
621 size_t *min_offs, void *base, int *total)
623 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
624 if (!last->e_value_block && last->e_value_size) {
625 size_t offs = le16_to_cpu(last->e_value_offs);
626 if (offs < *min_offs)
627 *min_offs = offs;
629 if (total)
630 *total += EXT4_XATTR_LEN(last->e_name_len);
632 return (*min_offs - ((void *)last - base) - sizeof(__u32));
635 static int
636 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
637 struct inode *inode)
639 struct ext4_xattr_entry *last, *next;
640 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
642 /* Compute min_offs and last. */
643 last = s->first;
644 for (; !IS_LAST_ENTRY(last); last = next) {
645 next = EXT4_XATTR_NEXT(last);
646 if ((void *)next >= s->end) {
647 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
648 return -EFSCORRUPTED;
650 if (!last->e_value_block && last->e_value_size) {
651 size_t offs = le16_to_cpu(last->e_value_offs);
652 if (offs < min_offs)
653 min_offs = offs;
656 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
657 if (!s->not_found) {
658 if (!s->here->e_value_block && s->here->e_value_size) {
659 size_t size = le32_to_cpu(s->here->e_value_size);
660 free += EXT4_XATTR_SIZE(size);
662 free += EXT4_XATTR_LEN(name_len);
664 if (i->value) {
665 if (free < EXT4_XATTR_LEN(name_len) +
666 EXT4_XATTR_SIZE(i->value_len))
667 return -ENOSPC;
670 if (i->value && s->not_found) {
671 /* Insert the new name. */
672 size_t size = EXT4_XATTR_LEN(name_len);
673 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
674 memmove((void *)s->here + size, s->here, rest);
675 memset(s->here, 0, size);
676 s->here->e_name_index = i->name_index;
677 s->here->e_name_len = name_len;
678 memcpy(s->here->e_name, i->name, name_len);
679 } else {
680 if (!s->here->e_value_block && s->here->e_value_size) {
681 void *first_val = s->base + min_offs;
682 size_t offs = le16_to_cpu(s->here->e_value_offs);
683 void *val = s->base + offs;
684 size_t size = EXT4_XATTR_SIZE(
685 le32_to_cpu(s->here->e_value_size));
687 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
688 /* The old and the new value have the same
689 size. Just replace. */
690 s->here->e_value_size =
691 cpu_to_le32(i->value_len);
692 if (i->value == EXT4_ZERO_XATTR_VALUE) {
693 memset(val, 0, size);
694 } else {
695 /* Clear pad bytes first. */
696 memset(val + size - EXT4_XATTR_PAD, 0,
697 EXT4_XATTR_PAD);
698 memcpy(val, i->value, i->value_len);
700 return 0;
703 /* Remove the old value. */
704 memmove(first_val + size, first_val, val - first_val);
705 memset(first_val, 0, size);
706 s->here->e_value_size = 0;
707 s->here->e_value_offs = 0;
708 min_offs += size;
710 /* Adjust all value offsets. */
711 last = s->first;
712 while (!IS_LAST_ENTRY(last)) {
713 size_t o = le16_to_cpu(last->e_value_offs);
714 if (!last->e_value_block &&
715 last->e_value_size && o < offs)
716 last->e_value_offs =
717 cpu_to_le16(o + size);
718 last = EXT4_XATTR_NEXT(last);
721 if (!i->value) {
722 /* Remove the old name. */
723 size_t size = EXT4_XATTR_LEN(name_len);
724 last = ENTRY((void *)last - size);
725 memmove(s->here, (void *)s->here + size,
726 (void *)last - (void *)s->here + sizeof(__u32));
727 memset(last, 0, size);
731 if (i->value) {
732 /* Insert the new value. */
733 s->here->e_value_size = cpu_to_le32(i->value_len);
734 if (i->value_len) {
735 size_t size = EXT4_XATTR_SIZE(i->value_len);
736 void *val = s->base + min_offs - size;
737 s->here->e_value_offs = cpu_to_le16(min_offs - size);
738 if (i->value == EXT4_ZERO_XATTR_VALUE) {
739 memset(val, 0, size);
740 } else {
741 /* Clear the pad bytes first. */
742 memset(val + size - EXT4_XATTR_PAD, 0,
743 EXT4_XATTR_PAD);
744 memcpy(val, i->value, i->value_len);
748 return 0;
751 struct ext4_xattr_block_find {
752 struct ext4_xattr_search s;
753 struct buffer_head *bh;
756 static int
757 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
758 struct ext4_xattr_block_find *bs)
760 struct super_block *sb = inode->i_sb;
761 int error;
763 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
764 i->name_index, i->name, i->value, (long)i->value_len);
766 if (EXT4_I(inode)->i_file_acl) {
767 /* The inode already has an extended attribute block. */
768 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
769 error = -EIO;
770 if (!bs->bh)
771 goto cleanup;
772 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
773 atomic_read(&(bs->bh->b_count)),
774 le32_to_cpu(BHDR(bs->bh)->h_refcount));
775 if (ext4_xattr_check_block(inode, bs->bh)) {
776 EXT4_ERROR_INODE(inode, "bad block %llu",
777 EXT4_I(inode)->i_file_acl);
778 error = -EFSCORRUPTED;
779 goto cleanup;
781 /* Find the named attribute. */
782 bs->s.base = BHDR(bs->bh);
783 bs->s.first = BFIRST(bs->bh);
784 bs->s.end = bs->bh->b_data + bs->bh->b_size;
785 bs->s.here = bs->s.first;
786 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
787 i->name, bs->bh->b_size, 1);
788 if (error && error != -ENODATA)
789 goto cleanup;
790 bs->s.not_found = error;
792 error = 0;
794 cleanup:
795 return error;
798 static int
799 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
800 struct ext4_xattr_info *i,
801 struct ext4_xattr_block_find *bs)
803 struct super_block *sb = inode->i_sb;
804 struct buffer_head *new_bh = NULL;
805 struct ext4_xattr_search *s = &bs->s;
806 struct mb_cache_entry *ce = NULL;
807 int error = 0;
808 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
810 #define header(x) ((struct ext4_xattr_header *)(x))
812 if (i->value && i->value_len > sb->s_blocksize)
813 return -ENOSPC;
814 if (s->base) {
815 ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
816 bs->bh->b_blocknr);
817 BUFFER_TRACE(bs->bh, "get_write_access");
818 error = ext4_journal_get_write_access(handle, bs->bh);
819 if (error)
820 goto cleanup;
821 lock_buffer(bs->bh);
823 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
824 if (ce) {
825 mb_cache_entry_free(ce);
826 ce = NULL;
828 ea_bdebug(bs->bh, "modifying in-place");
829 error = ext4_xattr_set_entry(i, s, inode);
830 if (!error) {
831 if (!IS_LAST_ENTRY(s->first))
832 ext4_xattr_rehash(header(s->base),
833 s->here);
835 ext4_xattr_block_csum_set(inode, bs->bh);
836 unlock_buffer(bs->bh);
837 if (error == -EFSCORRUPTED)
838 goto bad_block;
839 if (!error)
840 error = ext4_handle_dirty_metadata(handle,
841 inode,
842 bs->bh);
843 if (error)
844 goto cleanup;
845 goto inserted;
846 } else {
847 int offset = (char *)s->here - bs->bh->b_data;
849 unlock_buffer(bs->bh);
850 if (ce) {
851 mb_cache_entry_release(ce);
852 ce = NULL;
854 ea_bdebug(bs->bh, "cloning");
855 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
856 error = -ENOMEM;
857 if (s->base == NULL)
858 goto cleanup;
859 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
860 s->first = ENTRY(header(s->base)+1);
861 header(s->base)->h_refcount = cpu_to_le32(1);
862 s->here = ENTRY(s->base + offset);
863 s->end = s->base + bs->bh->b_size;
865 } else {
866 /* Allocate a buffer where we construct the new block. */
867 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
868 /* assert(header == s->base) */
869 error = -ENOMEM;
870 if (s->base == NULL)
871 goto cleanup;
872 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
873 header(s->base)->h_blocks = cpu_to_le32(1);
874 header(s->base)->h_refcount = cpu_to_le32(1);
875 s->first = ENTRY(header(s->base)+1);
876 s->here = ENTRY(header(s->base)+1);
877 s->end = s->base + sb->s_blocksize;
880 error = ext4_xattr_set_entry(i, s, inode);
881 if (error == -EFSCORRUPTED)
882 goto bad_block;
883 if (error)
884 goto cleanup;
885 if (!IS_LAST_ENTRY(s->first))
886 ext4_xattr_rehash(header(s->base), s->here);
888 inserted:
889 if (!IS_LAST_ENTRY(s->first)) {
890 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
891 if (new_bh) {
892 /* We found an identical block in the cache. */
893 if (new_bh == bs->bh)
894 ea_bdebug(new_bh, "keeping");
895 else {
896 /* The old block is released after updating
897 the inode. */
898 error = dquot_alloc_block(inode,
899 EXT4_C2B(EXT4_SB(sb), 1));
900 if (error)
901 goto cleanup;
902 BUFFER_TRACE(new_bh, "get_write_access");
903 error = ext4_journal_get_write_access(handle,
904 new_bh);
905 if (error)
906 goto cleanup_dquot;
907 lock_buffer(new_bh);
908 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
909 ea_bdebug(new_bh, "reusing; refcount now=%d",
910 le32_to_cpu(BHDR(new_bh)->h_refcount));
911 ext4_xattr_block_csum_set(inode, new_bh);
912 unlock_buffer(new_bh);
913 error = ext4_handle_dirty_metadata(handle,
914 inode,
915 new_bh);
916 if (error)
917 goto cleanup_dquot;
919 mb_cache_entry_release(ce);
920 ce = NULL;
921 } else if (bs->bh && s->base == bs->bh->b_data) {
922 /* We were modifying this block in-place. */
923 ea_bdebug(bs->bh, "keeping this block");
924 ext4_xattr_cache_insert(ext4_mb_cache, bs->bh);
925 new_bh = bs->bh;
926 get_bh(new_bh);
927 } else {
928 /* We need to allocate a new block */
929 ext4_fsblk_t goal, block;
931 goal = ext4_group_first_block_no(sb,
932 EXT4_I(inode)->i_block_group);
934 /* non-extent files can't have physical blocks past 2^32 */
935 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
936 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
938 block = ext4_new_meta_blocks(handle, inode, goal, 0,
939 NULL, &error);
940 if (error)
941 goto cleanup;
943 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
944 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
946 ea_idebug(inode, "creating block %llu",
947 (unsigned long long)block);
949 new_bh = sb_getblk(sb, block);
950 if (unlikely(!new_bh)) {
951 error = -ENOMEM;
952 getblk_failed:
953 ext4_free_blocks(handle, inode, NULL, block, 1,
954 EXT4_FREE_BLOCKS_METADATA);
955 goto cleanup;
957 lock_buffer(new_bh);
958 error = ext4_journal_get_create_access(handle, new_bh);
959 if (error) {
960 unlock_buffer(new_bh);
961 error = -EIO;
962 goto getblk_failed;
964 memcpy(new_bh->b_data, s->base, new_bh->b_size);
965 ext4_xattr_block_csum_set(inode, new_bh);
966 set_buffer_uptodate(new_bh);
967 unlock_buffer(new_bh);
968 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
969 error = ext4_handle_dirty_metadata(handle, inode,
970 new_bh);
971 if (error)
972 goto cleanup;
976 /* Update the inode. */
977 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
979 /* Drop the previous xattr block. */
980 if (bs->bh && bs->bh != new_bh)
981 ext4_xattr_release_block(handle, inode, bs->bh);
982 error = 0;
984 cleanup:
985 if (ce)
986 mb_cache_entry_release(ce);
987 brelse(new_bh);
988 if (!(bs->bh && s->base == bs->bh->b_data))
989 kfree(s->base);
991 return error;
993 cleanup_dquot:
994 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
995 goto cleanup;
997 bad_block:
998 EXT4_ERROR_INODE(inode, "bad block %llu",
999 EXT4_I(inode)->i_file_acl);
1000 goto cleanup;
1002 #undef header
1005 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1006 struct ext4_xattr_ibody_find *is)
1008 struct ext4_xattr_ibody_header *header;
1009 struct ext4_inode *raw_inode;
1010 int error;
1012 if (EXT4_I(inode)->i_extra_isize == 0)
1013 return 0;
1014 raw_inode = ext4_raw_inode(&is->iloc);
1015 header = IHDR(inode, raw_inode);
1016 is->s.base = is->s.first = IFIRST(header);
1017 is->s.here = is->s.first;
1018 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1019 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
1020 error = xattr_check_inode(inode, header, is->s.end);
1021 if (error)
1022 return error;
1023 /* Find the named attribute. */
1024 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
1025 i->name, is->s.end -
1026 (void *)is->s.base, 0);
1027 if (error && error != -ENODATA)
1028 return error;
1029 is->s.not_found = error;
1031 return 0;
1034 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1035 struct ext4_xattr_info *i,
1036 struct ext4_xattr_ibody_find *is)
1038 struct ext4_xattr_ibody_header *header;
1039 struct ext4_xattr_search *s = &is->s;
1040 int error;
1042 if (EXT4_I(inode)->i_extra_isize == 0)
1043 return -ENOSPC;
1044 error = ext4_xattr_set_entry(i, s, inode);
1045 if (error)
1046 return error;
1047 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1048 if (!IS_LAST_ENTRY(s->first)) {
1049 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1050 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1051 } else {
1052 header->h_magic = cpu_to_le32(0);
1053 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1055 return 0;
1058 static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1059 struct ext4_xattr_info *i,
1060 struct ext4_xattr_ibody_find *is)
1062 struct ext4_xattr_ibody_header *header;
1063 struct ext4_xattr_search *s = &is->s;
1064 int error;
1066 if (EXT4_I(inode)->i_extra_isize == 0)
1067 return -ENOSPC;
1068 error = ext4_xattr_set_entry(i, s, inode);
1069 if (error)
1070 return error;
1071 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1072 if (!IS_LAST_ENTRY(s->first)) {
1073 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1074 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1075 } else {
1076 header->h_magic = cpu_to_le32(0);
1077 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1079 return 0;
1083 * ext4_xattr_set_handle()
1085 * Create, replace or remove an extended attribute for this inode. Value
1086 * is NULL to remove an existing extended attribute, and non-NULL to
1087 * either replace an existing extended attribute, or create a new extended
1088 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1089 * specify that an extended attribute must exist and must not exist
1090 * previous to the call, respectively.
1092 * Returns 0, or a negative error number on failure.
1095 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
1096 const char *name, const void *value, size_t value_len,
1097 int flags)
1099 struct ext4_xattr_info i = {
1100 .name_index = name_index,
1101 .name = name,
1102 .value = value,
1103 .value_len = value_len,
1106 struct ext4_xattr_ibody_find is = {
1107 .s = { .not_found = -ENODATA, },
1109 struct ext4_xattr_block_find bs = {
1110 .s = { .not_found = -ENODATA, },
1112 int no_expand;
1113 int error;
1115 if (!name)
1116 return -EINVAL;
1117 if (strlen(name) > 255)
1118 return -ERANGE;
1119 ext4_write_lock_xattr(inode, &no_expand);
1121 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1122 if (error)
1123 goto cleanup;
1125 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1126 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1127 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1128 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1131 error = ext4_xattr_ibody_find(inode, &i, &is);
1132 if (error)
1133 goto cleanup;
1134 if (is.s.not_found)
1135 error = ext4_xattr_block_find(inode, &i, &bs);
1136 if (error)
1137 goto cleanup;
1138 if (is.s.not_found && bs.s.not_found) {
1139 error = -ENODATA;
1140 if (flags & XATTR_REPLACE)
1141 goto cleanup;
1142 error = 0;
1143 if (!value)
1144 goto cleanup;
1145 } else {
1146 error = -EEXIST;
1147 if (flags & XATTR_CREATE)
1148 goto cleanup;
1150 if (!value) {
1151 if (!is.s.not_found)
1152 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1153 else if (!bs.s.not_found)
1154 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1155 } else {
1156 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1157 if (!error && !bs.s.not_found) {
1158 i.value = NULL;
1159 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1160 } else if (error == -ENOSPC) {
1161 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1162 brelse(bs.bh);
1163 bs.bh = NULL;
1164 error = ext4_xattr_block_find(inode, &i, &bs);
1165 if (error)
1166 goto cleanup;
1168 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1169 if (error)
1170 goto cleanup;
1171 if (!is.s.not_found) {
1172 i.value = NULL;
1173 error = ext4_xattr_ibody_set(handle, inode, &i,
1174 &is);
1178 if (!error) {
1179 ext4_xattr_update_super_block(handle, inode->i_sb);
1180 inode->i_ctime = ext4_current_time(inode);
1181 if (!value)
1182 no_expand = 0;
1183 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1185 * The bh is consumed by ext4_mark_iloc_dirty, even with
1186 * error != 0.
1188 is.iloc.bh = NULL;
1189 if (IS_SYNC(inode))
1190 ext4_handle_sync(handle);
1193 cleanup:
1194 brelse(is.iloc.bh);
1195 brelse(bs.bh);
1196 ext4_write_unlock_xattr(inode, &no_expand);
1197 return error;
1201 * ext4_xattr_set()
1203 * Like ext4_xattr_set_handle, but start from an inode. This extended
1204 * attribute modification is a filesystem transaction by itself.
1206 * Returns 0, or a negative error number on failure.
1209 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1210 const void *value, size_t value_len, int flags)
1212 handle_t *handle;
1213 int error, retries = 0;
1214 int credits = ext4_jbd2_credits_xattr(inode);
1216 retry:
1217 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
1218 if (IS_ERR(handle)) {
1219 error = PTR_ERR(handle);
1220 } else {
1221 int error2;
1223 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1224 value, value_len, flags);
1225 error2 = ext4_journal_stop(handle);
1226 if (error == -ENOSPC &&
1227 ext4_should_retry_alloc(inode->i_sb, &retries))
1228 goto retry;
1229 if (error == 0)
1230 error = error2;
1233 return error;
1237 * Shift the EA entries in the inode to create space for the increased
1238 * i_extra_isize.
1240 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1241 int value_offs_shift, void *to,
1242 void *from, size_t n, int blocksize)
1244 struct ext4_xattr_entry *last = entry;
1245 int new_offs;
1247 /* Adjust the value offsets of the entries */
1248 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1249 if (!last->e_value_block && last->e_value_size) {
1250 new_offs = le16_to_cpu(last->e_value_offs) +
1251 value_offs_shift;
1252 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1253 > blocksize);
1254 last->e_value_offs = cpu_to_le16(new_offs);
1257 /* Shift the entries by n bytes */
1258 memmove(to, from, n);
1262 * Expand an inode by new_extra_isize bytes when EAs are present.
1263 * Returns 0 on success or negative error number on failure.
1265 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1266 struct ext4_inode *raw_inode, handle_t *handle)
1268 struct ext4_xattr_ibody_header *header;
1269 struct ext4_xattr_entry *entry, *last, *first;
1270 struct buffer_head *bh = NULL;
1271 struct ext4_xattr_ibody_find *is = NULL;
1272 struct ext4_xattr_block_find *bs = NULL;
1273 char *buffer = NULL, *b_entry_name = NULL;
1274 size_t min_offs, free;
1275 int total_ino;
1276 void *base, *start, *end;
1277 int error = 0, tried_min_extra_isize = 0;
1278 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1279 int isize_diff; /* How much do we need to grow i_extra_isize */
1280 int no_expand;
1282 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1283 return 0;
1285 retry:
1286 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
1287 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1288 goto out;
1290 header = IHDR(inode, raw_inode);
1291 entry = IFIRST(header);
1294 * Check if enough free space is available in the inode to shift the
1295 * entries ahead by new_extra_isize.
1298 base = start = entry;
1299 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1300 min_offs = end - base;
1301 last = entry;
1302 total_ino = sizeof(struct ext4_xattr_ibody_header);
1304 error = xattr_check_inode(inode, header, end);
1305 if (error)
1306 goto cleanup;
1308 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1309 if (free >= isize_diff) {
1310 entry = IFIRST(header);
1311 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1312 - new_extra_isize, (void *)raw_inode +
1313 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1314 (void *)header, total_ino,
1315 inode->i_sb->s_blocksize);
1316 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1317 goto out;
1321 * Enough free space isn't available in the inode, check if
1322 * EA block can hold new_extra_isize bytes.
1324 if (EXT4_I(inode)->i_file_acl) {
1325 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1326 error = -EIO;
1327 if (!bh)
1328 goto cleanup;
1329 if (ext4_xattr_check_block(inode, bh)) {
1330 EXT4_ERROR_INODE(inode, "bad block %llu",
1331 EXT4_I(inode)->i_file_acl);
1332 error = -EFSCORRUPTED;
1333 goto cleanup;
1335 base = BHDR(bh);
1336 first = BFIRST(bh);
1337 end = bh->b_data + bh->b_size;
1338 min_offs = end - base;
1339 free = ext4_xattr_free_space(first, &min_offs, base, NULL);
1340 if (free < isize_diff) {
1341 if (!tried_min_extra_isize && s_min_extra_isize) {
1342 tried_min_extra_isize++;
1343 new_extra_isize = s_min_extra_isize;
1344 brelse(bh);
1345 goto retry;
1347 error = -1;
1348 goto cleanup;
1350 } else {
1351 free = inode->i_sb->s_blocksize;
1354 while (isize_diff > 0) {
1355 size_t offs, size, entry_size;
1356 struct ext4_xattr_entry *small_entry = NULL;
1357 struct ext4_xattr_info i = {
1358 .value = NULL,
1359 .value_len = 0,
1361 unsigned int total_size; /* EA entry size + value size */
1362 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1363 unsigned int min_total_size = ~0U;
1365 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1366 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1367 if (!is || !bs) {
1368 error = -ENOMEM;
1369 goto cleanup;
1372 is->s.not_found = -ENODATA;
1373 bs->s.not_found = -ENODATA;
1374 is->iloc.bh = NULL;
1375 bs->bh = NULL;
1377 last = IFIRST(header);
1378 /* Find the entry best suited to be pushed into EA block */
1379 entry = NULL;
1380 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1381 /* never move system.data out of the inode */
1382 if ((last->e_name_len == 4) &&
1383 (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
1384 !memcmp(last->e_name, "data", 4))
1385 continue;
1386 total_size =
1387 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1388 EXT4_XATTR_LEN(last->e_name_len);
1389 if (total_size <= free && total_size < min_total_size) {
1390 if (total_size < isize_diff) {
1391 small_entry = last;
1392 } else {
1393 entry = last;
1394 min_total_size = total_size;
1399 if (entry == NULL) {
1400 if (small_entry) {
1401 entry = small_entry;
1402 } else {
1403 if (!tried_min_extra_isize &&
1404 s_min_extra_isize) {
1405 tried_min_extra_isize++;
1406 new_extra_isize = s_min_extra_isize;
1407 kfree(is); is = NULL;
1408 kfree(bs); bs = NULL;
1409 brelse(bh);
1410 goto retry;
1412 error = -1;
1413 goto cleanup;
1416 offs = le16_to_cpu(entry->e_value_offs);
1417 size = le32_to_cpu(entry->e_value_size);
1418 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1419 i.name_index = entry->e_name_index,
1420 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1421 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1422 if (!buffer || !b_entry_name) {
1423 error = -ENOMEM;
1424 goto cleanup;
1426 /* Save the entry name and the entry value */
1427 memcpy(buffer, (void *)IFIRST(header) + offs,
1428 EXT4_XATTR_SIZE(size));
1429 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1430 b_entry_name[entry->e_name_len] = '\0';
1431 i.name = b_entry_name;
1433 error = ext4_get_inode_loc(inode, &is->iloc);
1434 if (error)
1435 goto cleanup;
1437 error = ext4_xattr_ibody_find(inode, &i, is);
1438 if (error)
1439 goto cleanup;
1441 /* Remove the chosen entry from the inode */
1442 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1443 if (error)
1444 goto cleanup;
1445 total_ino -= entry_size;
1447 entry = IFIRST(header);
1448 if (entry_size + EXT4_XATTR_SIZE(size) >= isize_diff)
1449 shift_bytes = isize_diff;
1450 else
1451 shift_bytes = entry_size + EXT4_XATTR_SIZE(size);
1452 /* Adjust the offsets and shift the remaining entries ahead */
1453 ext4_xattr_shift_entries(entry, -shift_bytes,
1454 (void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
1455 EXT4_I(inode)->i_extra_isize + shift_bytes,
1456 (void *)header, total_ino, inode->i_sb->s_blocksize);
1458 isize_diff -= shift_bytes;
1459 EXT4_I(inode)->i_extra_isize += shift_bytes;
1460 header = IHDR(inode, raw_inode);
1462 i.name = b_entry_name;
1463 i.value = buffer;
1464 i.value_len = size;
1465 error = ext4_xattr_block_find(inode, &i, bs);
1466 if (error)
1467 goto cleanup;
1469 /* Add entry which was removed from the inode into the block */
1470 error = ext4_xattr_block_set(handle, inode, &i, bs);
1471 if (error)
1472 goto cleanup;
1473 kfree(b_entry_name);
1474 kfree(buffer);
1475 b_entry_name = NULL;
1476 buffer = NULL;
1477 brelse(is->iloc.bh);
1478 kfree(is);
1479 kfree(bs);
1481 brelse(bh);
1482 out:
1483 ext4_write_unlock_xattr(inode, &no_expand);
1484 return 0;
1486 cleanup:
1487 kfree(b_entry_name);
1488 kfree(buffer);
1489 if (is)
1490 brelse(is->iloc.bh);
1491 if (bs)
1492 brelse(bs->bh);
1493 kfree(is);
1494 kfree(bs);
1495 brelse(bh);
1497 * Inode size expansion failed; don't try again
1499 no_expand = 1;
1500 ext4_write_unlock_xattr(inode, &no_expand);
1501 return error;
1507 * ext4_xattr_delete_inode()
1509 * Free extended attribute resources associated with this inode. This
1510 * is called immediately before an inode is freed. We have exclusive
1511 * access to the inode.
1513 void
1514 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1516 struct buffer_head *bh = NULL;
1518 if (!EXT4_I(inode)->i_file_acl)
1519 goto cleanup;
1520 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1521 if (!bh) {
1522 EXT4_ERROR_INODE(inode, "block %llu read error",
1523 EXT4_I(inode)->i_file_acl);
1524 goto cleanup;
1526 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1527 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1528 EXT4_ERROR_INODE(inode, "bad block %llu",
1529 EXT4_I(inode)->i_file_acl);
1530 goto cleanup;
1532 ext4_xattr_release_block(handle, inode, bh);
1533 EXT4_I(inode)->i_file_acl = 0;
1535 cleanup:
1536 brelse(bh);
1540 * ext4_xattr_put_super()
1542 * This is called when a file system is unmounted.
1544 void
1545 ext4_xattr_put_super(struct super_block *sb)
1547 mb_cache_shrink(sb->s_bdev);
1551 * ext4_xattr_cache_insert()
1553 * Create a new entry in the extended attribute cache, and insert
1554 * it unless such an entry is already in the cache.
1556 * Returns 0, or a negative error number on failure.
1558 static void
1559 ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
1561 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1562 struct mb_cache_entry *ce;
1563 int error;
1565 ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
1566 if (!ce) {
1567 ea_bdebug(bh, "out of memory");
1568 return;
1570 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1571 if (error) {
1572 mb_cache_entry_free(ce);
1573 if (error == -EBUSY) {
1574 ea_bdebug(bh, "already in cache");
1575 error = 0;
1577 } else {
1578 ea_bdebug(bh, "inserting [%x]", (int)hash);
1579 mb_cache_entry_release(ce);
1584 * ext4_xattr_cmp()
1586 * Compare two extended attribute blocks for equality.
1588 * Returns 0 if the blocks are equal, 1 if they differ, and
1589 * a negative error number on errors.
1591 static int
1592 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1593 struct ext4_xattr_header *header2)
1595 struct ext4_xattr_entry *entry1, *entry2;
1597 entry1 = ENTRY(header1+1);
1598 entry2 = ENTRY(header2+1);
1599 while (!IS_LAST_ENTRY(entry1)) {
1600 if (IS_LAST_ENTRY(entry2))
1601 return 1;
1602 if (entry1->e_hash != entry2->e_hash ||
1603 entry1->e_name_index != entry2->e_name_index ||
1604 entry1->e_name_len != entry2->e_name_len ||
1605 entry1->e_value_size != entry2->e_value_size ||
1606 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1607 return 1;
1608 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1609 return -EFSCORRUPTED;
1610 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1611 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1612 le32_to_cpu(entry1->e_value_size)))
1613 return 1;
1615 entry1 = EXT4_XATTR_NEXT(entry1);
1616 entry2 = EXT4_XATTR_NEXT(entry2);
1618 if (!IS_LAST_ENTRY(entry2))
1619 return 1;
1620 return 0;
1624 * ext4_xattr_cache_find()
1626 * Find an identical extended attribute block.
1628 * Returns a pointer to the block found, or NULL if such a block was
1629 * not found or an error occurred.
1631 static struct buffer_head *
1632 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1633 struct mb_cache_entry **pce)
1635 __u32 hash = le32_to_cpu(header->h_hash);
1636 struct mb_cache_entry *ce;
1637 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
1639 if (!header->h_hash)
1640 return NULL; /* never share */
1641 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1642 again:
1643 ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
1644 hash);
1645 while (ce) {
1646 struct buffer_head *bh;
1648 if (IS_ERR(ce)) {
1649 if (PTR_ERR(ce) == -EAGAIN)
1650 goto again;
1651 break;
1653 bh = sb_bread(inode->i_sb, ce->e_block);
1654 if (!bh) {
1655 EXT4_ERROR_INODE(inode, "block %lu read error",
1656 (unsigned long) ce->e_block);
1657 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1658 EXT4_XATTR_REFCOUNT_MAX) {
1659 ea_idebug(inode, "block %lu refcount %d>=%d",
1660 (unsigned long) ce->e_block,
1661 le32_to_cpu(BHDR(bh)->h_refcount),
1662 EXT4_XATTR_REFCOUNT_MAX);
1663 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1664 *pce = ce;
1665 return bh;
1667 brelse(bh);
1668 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1670 return NULL;
1673 #define NAME_HASH_SHIFT 5
1674 #define VALUE_HASH_SHIFT 16
1677 * ext4_xattr_hash_entry()
1679 * Compute the hash of an extended attribute.
1681 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1682 struct ext4_xattr_entry *entry)
1684 __u32 hash = 0;
1685 char *name = entry->e_name;
1686 int n;
1688 for (n = 0; n < entry->e_name_len; n++) {
1689 hash = (hash << NAME_HASH_SHIFT) ^
1690 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1691 *name++;
1694 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1695 __le32 *value = (__le32 *)((char *)header +
1696 le16_to_cpu(entry->e_value_offs));
1697 for (n = (le32_to_cpu(entry->e_value_size) +
1698 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1699 hash = (hash << VALUE_HASH_SHIFT) ^
1700 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1701 le32_to_cpu(*value++);
1704 entry->e_hash = cpu_to_le32(hash);
1707 #undef NAME_HASH_SHIFT
1708 #undef VALUE_HASH_SHIFT
1710 #define BLOCK_HASH_SHIFT 16
1713 * ext4_xattr_rehash()
1715 * Re-compute the extended attribute hash value after an entry has changed.
1717 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1718 struct ext4_xattr_entry *entry)
1720 struct ext4_xattr_entry *here;
1721 __u32 hash = 0;
1723 ext4_xattr_hash_entry(header, entry);
1724 here = ENTRY(header+1);
1725 while (!IS_LAST_ENTRY(here)) {
1726 if (!here->e_hash) {
1727 /* Block is not shared if an entry's hash value == 0 */
1728 hash = 0;
1729 break;
1731 hash = (hash << BLOCK_HASH_SHIFT) ^
1732 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1733 le32_to_cpu(here->e_hash);
1734 here = EXT4_XATTR_NEXT(here);
1736 header->h_hash = cpu_to_le32(hash);
1739 #undef BLOCK_HASH_SHIFT
1741 #define HASH_BUCKET_BITS 10
1743 struct mb_cache *
1744 ext4_xattr_create_cache(char *name)
1746 return mb_cache_create(name, HASH_BUCKET_BITS);
1749 void ext4_xattr_destroy_cache(struct mb_cache *cache)
1751 if (cache)
1752 mb_cache_destroy(cache);