2 * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3 * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup lwext4
34 * @brief Extended Attribute manipulation.
37 #include "ext4_config.h"
38 #include "ext4_types.h"
39 #include "ext4_misc.h"
40 #include "ext4_errno.h"
41 #include "ext4_debug.h"
44 #include "ext4_trans.h"
45 #include "ext4_xattr.h"
46 #include "ext4_blockdev.h"
47 #include "ext4_super.h"
48 #include "ext4_crc32.h"
49 #include "ext4_block_group.h"
50 #include "ext4_balloc.h"
51 #include "ext4_inode.h"
58 * @brief Extended Attribute Manipulation
61 #define NAME_HASH_SHIFT 5
62 #define VALUE_HASH_SHIFT 16
64 static inline void ext4_xattr_compute_hash(struct ext4_xattr_header *header,
65 struct ext4_xattr_entry *entry)
68 char *name = EXT4_XATTR_NAME(entry);
71 for (n = 0; n < entry->e_name_len; n++) {
72 hash = (hash << NAME_HASH_SHIFT) ^
73 (hash >> (8 * sizeof(hash) - NAME_HASH_SHIFT)) ^ *name++;
76 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
78 (uint32_t *)((char *)header + to_le16(entry->e_value_offs));
79 for (n = (to_le32(entry->e_value_size) + EXT4_XATTR_ROUND) >>
82 hash = (hash << VALUE_HASH_SHIFT) ^
83 (hash >> (8 * sizeof(hash) - VALUE_HASH_SHIFT)) ^
87 entry->e_hash = to_le32(hash);
90 #define BLOCK_HASH_SHIFT 16
95 * Re-compute the extended attribute hash value after an entry has changed.
97 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
98 struct ext4_xattr_entry *entry)
100 struct ext4_xattr_entry *here;
103 ext4_xattr_compute_hash(header, entry);
104 here = EXT4_XATTR_ENTRY(header + 1);
105 while (!EXT4_XATTR_IS_LAST_ENTRY(here)) {
107 /* Block is not shared if an entry's hash value == 0 */
111 hash = (hash << BLOCK_HASH_SHIFT) ^
112 (hash >> (8 * sizeof(hash) - BLOCK_HASH_SHIFT)) ^
113 to_le32(here->e_hash);
114 here = EXT4_XATTR_NEXT(here);
116 header->h_hash = to_le32(hash);
119 #if CONFIG_META_CSUM_ENABLE
121 ext4_xattr_block_checksum(struct ext4_inode_ref *inode_ref,
122 ext4_fsblk_t blocknr,
123 struct ext4_xattr_header *header)
125 uint32_t checksum = 0;
126 uint64_t le64_blocknr = blocknr;
127 struct ext4_sblock *sb = &inode_ref->fs->sb;
129 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
130 uint32_t orig_checksum;
132 /* Preparation: temporarily set bg checksum to 0 */
133 orig_checksum = header->h_checksum;
134 header->h_checksum = 0;
135 /* First calculate crc32 checksum against fs uuid */
136 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
138 /* Then calculate crc32 checksum block number */
139 checksum = ext4_crc32c(checksum, &le64_blocknr,
140 sizeof(le64_blocknr));
141 /* Finally calculate crc32 checksum against
142 * the entire xattr block */
143 checksum = ext4_crc32c(checksum, header,
144 ext4_sb_get_block_size(sb));
145 header->h_checksum = orig_checksum;
150 #define ext4_xattr_block_checksum(...) 0
154 ext4_xattr_set_block_checksum(struct ext4_inode_ref *inode_ref,
155 ext4_fsblk_t blocknr __unused,
156 struct ext4_xattr_header *header)
158 struct ext4_sblock *sb = &inode_ref->fs->sb;
159 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
163 ext4_xattr_block_checksum(inode_ref, blocknr, header);
166 static int ext4_xattr_item_cmp(struct ext4_xattr_item *a,
167 struct ext4_xattr_item *b)
170 if (a->in_inode && !b->in_inode)
173 if (!a->in_inode && b->in_inode)
176 result = a->name_index - b->name_index;
180 result = a->name_len - b->name_len;
184 return memcmp(a->name, b->name, a->name_len);
187 RB_GENERATE_INTERNAL(ext4_xattr_tree, ext4_xattr_item, node,
188 ext4_xattr_item_cmp, static inline)
190 static struct ext4_xattr_item *
191 ext4_xattr_item_alloc(uint8_t name_index, const char *name, size_t name_len)
193 struct ext4_xattr_item *item;
194 item = malloc(sizeof(struct ext4_xattr_item) + name_len);
198 item->name_index = name_index;
199 item->name = (char *)(item + 1);
200 item->name_len = name_len;
204 memset(&item->node, 0, sizeof(item->node));
205 memcpy(item->name, name, name_len);
207 if (name_index == EXT4_XATTR_INDEX_SYSTEM &&
209 !memcmp(name, "data", 4))
210 item->in_inode = true;
212 item->in_inode = false;
217 static int ext4_xattr_item_alloc_data(struct ext4_xattr_item *item,
218 const void *orig_data, size_t data_size)
221 ext4_assert(!item->data);
222 data = malloc(data_size);
227 memcpy(data, orig_data, data_size);
230 item->data_size = data_size;
234 static void ext4_xattr_item_free_data(struct ext4_xattr_item *item)
236 ext4_assert(item->data);
242 static int ext4_xattr_item_resize_data(struct ext4_xattr_item *item,
243 size_t new_data_size)
245 if (new_data_size != item->data_size) {
247 new_data = realloc(item->data, new_data_size);
251 item->data = new_data;
252 item->data_size = new_data_size;
257 static void ext4_xattr_item_free(struct ext4_xattr_item *item)
260 ext4_xattr_item_free_data(item);
265 static void *ext4_xattr_entry_data(struct ext4_xattr_ref *xattr_ref,
266 struct ext4_xattr_entry *entry,
271 struct ext4_xattr_ibody_header *header;
272 struct ext4_xattr_entry *first_entry;
274 ext4_get16(&xattr_ref->fs->sb, inode_size);
275 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
276 first_entry = EXT4_XATTR_IFIRST(header);
278 ret = ((char *)first_entry + to_le16(entry->e_value_offs));
279 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
280 (char *)xattr_ref->inode_ref->inode > inode_size)
286 int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
287 ret = ((char *)xattr_ref->block.data + to_le16(entry->e_value_offs));
288 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
289 (char *)xattr_ref->block.data > block_size)
294 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
299 struct ext4_xattr_entry *entry = NULL;
301 ext4_assert(xattr_ref->block.data);
302 entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
304 size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
305 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
306 entry = EXT4_XATTR_NEXT(entry),
307 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
308 struct ext4_xattr_item *item;
309 char *e_name = EXT4_XATTR_NAME(entry);
311 data = ext4_xattr_entry_data(xattr_ref, entry, false);
317 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
318 (size_t)entry->e_name_len);
323 if (ext4_xattr_item_alloc_data(
324 item, data, to_le32(entry->e_value_size)) != EOK) {
325 ext4_xattr_item_free(item);
329 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
330 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
331 EXT4_XATTR_LEN(item->name_len);
338 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
343 struct ext4_xattr_ibody_header *header = NULL;
344 struct ext4_xattr_entry *entry = NULL;
345 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
347 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
348 entry = EXT4_XATTR_IFIRST(header);
350 size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
351 xattr_ref->inode_ref->inode->extra_isize;
352 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
353 entry = EXT4_XATTR_NEXT(entry),
354 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
355 struct ext4_xattr_item *item;
356 char *e_name = EXT4_XATTR_NAME(entry);
358 data = ext4_xattr_entry_data(xattr_ref, entry, true);
364 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
365 (size_t)entry->e_name_len);
370 if (ext4_xattr_item_alloc_data(
371 item, data, to_le32(entry->e_value_size)) != EOK) {
372 ext4_xattr_item_free(item);
376 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
377 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
378 EXT4_XATTR_LEN(item->name_len);
385 static size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
387 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
388 uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
389 xattr_ref->inode_ref->inode->extra_isize;
393 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
395 return ext4_sb_get_block_size(&xattr_ref->fs->sb);
398 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
401 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
402 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
403 ret = ext4_xattr_inode_fetch(xattr_ref);
408 if (xattr_ref->block_loaded)
409 ret = ext4_xattr_block_fetch(xattr_ref);
411 xattr_ref->dirty = false;
415 static struct ext4_xattr_item *
416 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
417 const char *name, size_t name_len)
419 struct ext4_xattr_item tmp = {
420 .name_index = name_index,
421 .name = (char *)name, /*RB_FIND - won't touch this string*/
422 .name_len = name_len,
424 if (name_index == EXT4_XATTR_INDEX_SYSTEM &&
426 !memcmp(name, "data", 4))
429 return RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
432 static struct ext4_xattr_item *
433 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
434 const char *name, size_t name_len, const void *data,
437 struct ext4_xattr_item *item;
438 item = ext4_xattr_item_alloc(name_index, name, name_len);
442 if ((xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
443 EXT4_XATTR_LEN(item->name_len)
445 ext4_xattr_inode_space(xattr_ref) -
446 sizeof(struct ext4_xattr_ibody_header))
448 (xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
449 EXT4_XATTR_LEN(item->name_len) >
450 ext4_xattr_block_space(xattr_ref) -
451 sizeof(struct ext4_xattr_header))) {
452 ext4_xattr_item_free(item);
456 if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
457 ext4_xattr_item_free(item);
460 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
461 xattr_ref->ea_size +=
462 EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
463 xattr_ref->dirty = true;
467 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
468 uint8_t name_index, const char *name,
472 struct ext4_xattr_item *item =
473 ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
475 if (item == xattr_ref->iter_from)
476 xattr_ref->iter_from =
477 RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
479 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
480 EXT4_XATTR_LEN(item->name_len);
482 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
483 ext4_xattr_item_free(item);
484 xattr_ref->dirty = true;
490 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
491 struct ext4_xattr_item *item,
492 size_t new_data_size)
495 size_t old_data_size = item->data_size;
496 if ((xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
497 EXT4_XATTR_SIZE(new_data_size)
499 ext4_xattr_inode_space(xattr_ref) -
500 sizeof(struct ext4_xattr_ibody_header))
502 (xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
503 EXT4_XATTR_SIZE(new_data_size)
505 ext4_xattr_block_space(xattr_ref) -
506 sizeof(struct ext4_xattr_header))) {
510 ret = ext4_xattr_item_resize_data(item, new_data_size);
516 EXT4_XATTR_SIZE(old_data_size) +
517 EXT4_XATTR_SIZE(new_data_size);
518 xattr_ref->dirty = true;
522 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
524 struct ext4_xattr_item *item, *save_item;
525 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
527 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
528 ext4_xattr_item_free(item);
530 xattr_ref->ea_size = 0;
533 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
537 ext4_fsblk_t xattr_block = 0;
538 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
542 ext4_fs_inode_to_goal_block(xattr_ref->inode_ref);
544 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
550 ret = ext4_trans_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
553 ext4_balloc_free_block(xattr_ref->inode_ref,
558 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
559 &xattr_ref->fs->sb, xattr_block);
560 xattr_ref->inode_ref->dirty = true;
561 xattr_ref->block_loaded = true;
568 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
570 ext4_fsblk_t xattr_block;
571 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
573 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
575 ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
576 ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
577 xattr_ref->inode_ref->dirty = true;
578 xattr_ref->block_loaded = false;
581 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
583 struct ext4_xattr_header *block_header = NULL;
584 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
586 memset(block_header, 0, sizeof(struct ext4_xattr_header));
587 block_header->h_magic = EXT4_XATTR_MAGIC;
588 block_header->h_refcount = to_le32(1);
589 block_header->h_blocks = to_le32(1);
593 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
594 struct ext4_xattr_ibody_header *ibody_header,
595 struct ext4_xattr_entry *entry, void *ibody_data_ptr)
597 entry->e_name_len = (uint8_t)item->name_len;
598 entry->e_name_index = item->name_index;
599 entry->e_value_offs =
600 (char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header);
601 entry->e_value_block = 0;
602 entry->e_value_size = item->data_size;
605 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
606 struct ext4_xattr_header *block_header,
607 struct ext4_xattr_entry *block_entry,
608 void *block_data_ptr)
610 block_entry->e_name_len = (uint8_t)item->name_len;
611 block_entry->e_name_index = item->name_index;
612 block_entry->e_value_offs =
613 (char *)block_data_ptr - (char *)block_header;
614 block_entry->e_value_block = 0;
615 block_entry->e_value_size = item->data_size;
618 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
621 bool block_modified = false;
622 void *ibody_data = NULL;
623 void *block_data = NULL;
624 struct ext4_xattr_item *item, *save_item;
625 size_t inode_size_rem, block_size_rem;
626 struct ext4_xattr_ibody_header *ibody_header = NULL;
627 struct ext4_xattr_header *block_header = NULL;
628 struct ext4_xattr_entry *entry = NULL;
629 struct ext4_xattr_entry *block_entry = NULL;
631 inode_size_rem = ext4_xattr_inode_space(xattr_ref);
632 block_size_rem = ext4_xattr_block_space(xattr_ref);
633 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
634 ibody_header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
635 entry = EXT4_XATTR_IFIRST(ibody_header);
638 if (!xattr_ref->dirty)
640 /* If there are enough spaces in the ibody EA table.*/
641 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
642 memset(ibody_header, 0, inode_size_rem);
643 ibody_header->h_magic = EXT4_XATTR_MAGIC;
644 ibody_data = (char *)ibody_header + inode_size_rem;
645 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
647 xattr_ref->inode_ref->dirty = true;
649 /* If we need an extra block to hold the EA entries*/
650 if (xattr_ref->ea_size > inode_size_rem) {
651 if (!xattr_ref->block_loaded) {
652 ret = ext4_xattr_try_alloc_block(xattr_ref);
656 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
657 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
658 ext4_xattr_set_block_header(xattr_ref);
659 block_data = (char *)block_header + block_size_rem;
660 block_size_rem -= sizeof(struct ext4_xattr_header);
662 ext4_trans_set_block_dirty(xattr_ref->block.buf);
664 /* We don't need an extra block.*/
665 if (xattr_ref->block_loaded) {
666 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
667 block_header->h_refcount =
668 to_le32(to_le32(block_header->h_refcount) - 1);
669 if (!block_header->h_refcount) {
670 ext4_xattr_try_free_block(xattr_ref);
674 EXT4_XATTR_BFIRST(&xattr_ref->block);
676 (char *)block_header + block_size_rem;
678 sizeof(struct ext4_xattr_header);
679 ext4_inode_set_file_acl(
680 xattr_ref->inode_ref->inode,
681 &xattr_ref->fs->sb, 0);
683 xattr_ref->inode_ref->dirty = true;
684 ext4_trans_set_block_dirty(xattr_ref->block.buf);
688 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
690 if (EXT4_XATTR_SIZE(item->data_size) +
691 EXT4_XATTR_LEN(item->name_len) <=
693 ibody_data = (char *)ibody_data -
694 EXT4_XATTR_SIZE(item->data_size);
695 ext4_xattr_set_inode_entry(item, ibody_header, entry,
697 memcpy(EXT4_XATTR_NAME(entry), item->name,
699 memcpy(ibody_data, item->data, item->data_size);
700 entry = EXT4_XATTR_NEXT(entry);
701 inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
702 EXT4_XATTR_LEN(item->name_len);
704 xattr_ref->inode_ref->dirty = true;
707 if (EXT4_XATTR_SIZE(item->data_size) +
708 EXT4_XATTR_LEN(item->name_len) >
714 (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
715 ext4_xattr_set_block_entry(item, block_header, block_entry,
717 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
719 memcpy(block_data, item->data, item->data_size);
720 block_entry = EXT4_XATTR_NEXT(block_entry);
721 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
722 EXT4_XATTR_LEN(item->name_len);
724 block_modified = true;
726 xattr_ref->dirty = false;
727 if (block_modified) {
728 ext4_xattr_rehash(block_header,
729 EXT4_XATTR_BFIRST(&xattr_ref->block));
730 ext4_xattr_set_block_checksum(xattr_ref->inode_ref,
731 xattr_ref->block.lb_id,
733 ext4_trans_set_block_dirty(xattr_ref->block.buf);
740 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
741 int (*iter)(struct ext4_xattr_ref *ref,
742 struct ext4_xattr_item *item))
744 struct ext4_xattr_item *item;
746 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
748 RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
750 int ret = EXT4_XATTR_ITERATE_CONT;
754 if (ret != EXT4_XATTR_ITERATE_CONT) {
755 if (ret == EXT4_XATTR_ITERATE_STOP)
756 ref->iter_from = NULL;
763 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
765 ref->iter_from = NULL;
768 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
769 const char *name, size_t name_len, const void *data,
770 size_t data_size, bool replace)
773 struct ext4_xattr_item *item =
774 ext4_xattr_lookup_item(ref, name_index, name, name_len);
780 if (item->data_size != data_size)
781 ret = ext4_xattr_resize_item(ref, item, data_size);
786 memcpy(item->data, data, data_size);
792 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
801 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
802 const char *name, size_t name_len)
804 return ext4_xattr_remove_item(ref, name_index, name, name_len);
807 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
808 const char *name, size_t name_len, void *buf,
809 size_t buf_size, size_t *data_size)
812 size_t item_size = 0;
813 struct ext4_xattr_item *item =
814 ext4_xattr_lookup_item(ref, name_index, name, name_len);
820 item_size = item->data_size;
821 if (buf_size > item_size)
822 buf_size = item_size;
825 memcpy(buf, item->data, buf_size);
829 *data_size = item_size;
834 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
835 struct ext4_xattr_ref *ref)
838 ext4_fsblk_t xattr_block;
839 xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
842 ref->iter_from = NULL;
844 rc = ext4_trans_block_get(fs->bdev, &ref->block, xattr_block);
848 ref->block_loaded = true;
850 ref->block_loaded = false;
852 ref->inode_ref = inode_ref;
855 rc = ext4_xattr_fetch(ref);
857 ext4_xattr_purge_items(ref);
859 ext4_block_set(fs->bdev, &ref->block);
861 ref->block_loaded = false;
867 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
869 ext4_xattr_write_to_disk(ref);
870 if (ref->block_loaded) {
871 ext4_block_set(ref->fs->bdev, &ref->block);
872 ref->block_loaded = false;
874 ext4_xattr_purge_items(ref);
875 ref->inode_ref = NULL;
879 struct xattr_prefix {
884 static const struct xattr_prefix prefix_tbl[] = {
885 {"user.", EXT4_XATTR_INDEX_USER},
886 {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
887 {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
888 {"trusted.", EXT4_XATTR_INDEX_TRUSTED},
889 {"security.", EXT4_XATTR_INDEX_SECURITY},
890 {"system.", EXT4_XATTR_INDEX_SYSTEM},
891 {"system.richacl", EXT4_XATTR_INDEX_RICHACL},
895 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
896 uint8_t *name_index, size_t *name_len,
900 ext4_assert(name_index);
905 if (!full_name_len) {
912 for (i = 0; prefix_tbl[i].prefix; i++) {
913 size_t prefix_len = strlen(prefix_tbl[i].prefix);
914 if (full_name_len >= prefix_len &&
915 !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
917 prefix_tbl[i].prefix[prefix_len - 1] == '.';
918 *name_index = prefix_tbl[i].name_index;
920 *name_len = full_name_len - prefix_len;
922 if (!(full_name_len - prefix_len) && require_name)
927 return full_name + prefix_len;
938 const char *ext4_get_xattr_name_prefix(uint8_t name_index,
939 size_t *ret_prefix_len)
943 for (i = 0; prefix_tbl[i].prefix; i++) {
944 size_t prefix_len = strlen(prefix_tbl[i].prefix);
945 if (prefix_tbl[i].name_index == name_index) {
947 *ret_prefix_len = prefix_len;
949 return prefix_tbl[i].prefix;