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"
40 #include "ext4_errno.h"
41 #include "ext4_blockdev.h"
42 #include "ext4_super.h"
43 #include "ext4_crc32c.h"
44 #include "ext4_debug.h"
45 #include "ext4_block_group.h"
46 #include "ext4_balloc.h"
47 #include "ext4_inode.h"
54 * @brief Extended Attribute Manipulation
57 #define NAME_HASH_SHIFT 5
58 #define VALUE_HASH_SHIFT 16
60 static inline void ext4_xattr_compute_hash(struct ext4_xattr_header *header,
61 struct ext4_xattr_entry *entry)
64 char *name = EXT4_XATTR_NAME(entry);
67 for (n = 0; n < entry->e_name_len; n++) {
68 hash = (hash << NAME_HASH_SHIFT) ^
69 (hash >> (8 * sizeof(hash) - NAME_HASH_SHIFT)) ^ *name++;
72 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
74 (uint32_t *)((char *)header + to_le16(entry->e_value_offs));
75 for (n = (to_le32(entry->e_value_size) + EXT4_XATTR_ROUND) >>
78 hash = (hash << VALUE_HASH_SHIFT) ^
79 (hash >> (8 * sizeof(hash) - VALUE_HASH_SHIFT)) ^
83 entry->e_hash = to_le32(hash);
86 #define BLOCK_HASH_SHIFT 16
91 * Re-compute the extended attribute hash value after an entry has changed.
93 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
94 struct ext4_xattr_entry *entry)
96 struct ext4_xattr_entry *here;
99 ext4_xattr_compute_hash(header, entry);
100 here = EXT4_XATTR_ENTRY(header + 1);
101 while (!EXT4_XATTR_IS_LAST_ENTRY(here)) {
103 /* Block is not shared if an entry's hash value == 0 */
107 hash = (hash << BLOCK_HASH_SHIFT) ^
108 (hash >> (8 * sizeof(hash) - BLOCK_HASH_SHIFT)) ^
109 to_le32(here->e_hash);
110 here = EXT4_XATTR_NEXT(here);
112 header->h_hash = to_le32(hash);
115 #if CONFIG_META_CSUM_ENABLE
117 ext4_xattr_block_checksum(struct ext4_inode_ref *inode_ref,
118 ext4_fsblk_t blocknr,
119 struct ext4_xattr_header *header)
121 uint32_t checksum = 0;
122 uint64_t le64_blocknr = blocknr;
123 struct ext4_sblock *sb = &inode_ref->fs->sb;
125 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
126 uint32_t orig_checksum;
128 /* Preparation: temporarily set bg checksum to 0 */
129 orig_checksum = header->h_checksum;
130 header->h_checksum = 0;
131 /* First calculate crc32 checksum against fs uuid */
132 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
134 /* Then calculate crc32 checksum block number */
135 checksum = ext4_crc32c(checksum, &le64_blocknr,
136 sizeof(le64_blocknr));
137 /* Finally calculate crc32 checksum against
138 * the entire xattr block */
139 checksum = ext4_crc32c(checksum, header,
140 ext4_sb_get_block_size(sb));
141 header->h_checksum = orig_checksum;
146 #define ext4_xattr_block_checksum(...) 0
150 ext4_xattr_set_block_checksum(struct ext4_inode_ref *inode_ref,
151 ext4_fsblk_t blocknr __unused,
152 struct ext4_xattr_header *header)
154 struct ext4_sblock *sb = &inode_ref->fs->sb;
155 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
159 ext4_xattr_block_checksum(inode_ref, blocknr, header);
162 static int ext4_xattr_item_cmp(struct ext4_xattr_item *a,
163 struct ext4_xattr_item *b)
166 if (a->in_inode && !b->in_inode)
169 if (!a->in_inode && b->in_inode)
172 result = a->name_index - b->name_index;
176 result = a->name_len - b->name_len;
180 return memcmp(a->name, b->name, a->name_len);
183 RB_GENERATE_INTERNAL(ext4_xattr_tree, ext4_xattr_item, node,
184 ext4_xattr_item_cmp, static inline)
186 static struct ext4_xattr_item *
187 ext4_xattr_item_alloc(uint8_t name_index, const char *name, size_t name_len)
189 struct ext4_xattr_item *item;
190 item = malloc(sizeof(struct ext4_xattr_item) + name_len);
194 item->name_index = name_index;
195 item->name = (char *)(item + 1);
196 item->name_len = name_len;
200 memset(&item->node, 0, sizeof(item->node));
201 memcpy(item->name, name, name_len);
203 if (name_index == EXT4_XATTR_INDEX_SYSTEM &&
205 !memcmp(name, "data", 4))
206 item->in_inode = true;
208 item->in_inode = false;
213 static int ext4_xattr_item_alloc_data(struct ext4_xattr_item *item,
214 const void *orig_data, size_t data_size)
217 ext4_assert(!item->data);
218 data = malloc(data_size);
223 memcpy(data, orig_data, data_size);
226 item->data_size = data_size;
230 static void ext4_xattr_item_free_data(struct ext4_xattr_item *item)
232 ext4_assert(item->data);
238 static int ext4_xattr_item_resize_data(struct ext4_xattr_item *item,
239 size_t new_data_size)
241 if (new_data_size != item->data_size) {
243 new_data = realloc(item->data, new_data_size);
247 item->data = new_data;
248 item->data_size = new_data_size;
253 static void ext4_xattr_item_free(struct ext4_xattr_item *item)
256 ext4_xattr_item_free_data(item);
261 static void *ext4_xattr_entry_data(struct ext4_xattr_ref *xattr_ref,
262 struct ext4_xattr_entry *entry,
267 struct ext4_xattr_ibody_header *header;
268 struct ext4_xattr_entry *first_entry;
270 ext4_get16(&xattr_ref->fs->sb, inode_size);
271 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
272 first_entry = EXT4_XATTR_IFIRST(header);
274 ret = ((char *)first_entry + to_le16(entry->e_value_offs));
275 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
276 (char *)xattr_ref->inode_ref->inode > inode_size)
282 int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
283 ret = ((char *)xattr_ref->block.data + to_le16(entry->e_value_offs));
284 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
285 (char *)xattr_ref->block.data > block_size)
290 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
295 struct ext4_xattr_entry *entry = NULL;
297 ext4_assert(xattr_ref->block.data);
298 entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
300 size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
301 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
302 entry = EXT4_XATTR_NEXT(entry),
303 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
304 struct ext4_xattr_item *item;
305 char *e_name = EXT4_XATTR_NAME(entry);
307 data = ext4_xattr_entry_data(xattr_ref, entry, false);
313 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
314 (size_t)entry->e_name_len);
319 if (ext4_xattr_item_alloc_data(
320 item, data, to_le32(entry->e_value_size)) != EOK) {
321 ext4_xattr_item_free(item);
325 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
326 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
327 EXT4_XATTR_LEN(item->name_len);
334 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
339 struct ext4_xattr_ibody_header *header = NULL;
340 struct ext4_xattr_entry *entry = NULL;
341 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
343 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
344 entry = EXT4_XATTR_IFIRST(header);
346 size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
347 xattr_ref->inode_ref->inode->extra_isize;
348 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
349 entry = EXT4_XATTR_NEXT(entry),
350 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
351 struct ext4_xattr_item *item;
352 char *e_name = EXT4_XATTR_NAME(entry);
354 data = ext4_xattr_entry_data(xattr_ref, entry, true);
360 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
361 (size_t)entry->e_name_len);
366 if (ext4_xattr_item_alloc_data(
367 item, data, to_le32(entry->e_value_size)) != EOK) {
368 ext4_xattr_item_free(item);
372 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
373 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
374 EXT4_XATTR_LEN(item->name_len);
381 static size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
383 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
384 uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
385 xattr_ref->inode_ref->inode->extra_isize;
389 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
391 return ext4_sb_get_block_size(&xattr_ref->fs->sb);
394 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
397 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
398 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
399 ret = ext4_xattr_inode_fetch(xattr_ref);
404 if (xattr_ref->block_loaded)
405 ret = ext4_xattr_block_fetch(xattr_ref);
407 xattr_ref->dirty = false;
411 static struct ext4_xattr_item *
412 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
413 const char *name, size_t name_len)
415 struct ext4_xattr_item tmp = {
416 .name_index = name_index,
417 .name = (char *)name, /*RB_FIND - won't touch this string*/
418 .name_len = name_len,
420 if (name_index == EXT4_XATTR_INDEX_SYSTEM &&
422 !memcmp(name, "data", 4))
425 return RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
428 static struct ext4_xattr_item *
429 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
430 const char *name, size_t name_len, const void *data,
433 struct ext4_xattr_item *item;
434 item = ext4_xattr_item_alloc(name_index, name, name_len);
438 if ((xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
439 EXT4_XATTR_LEN(item->name_len)
441 ext4_xattr_inode_space(xattr_ref) -
442 sizeof(struct ext4_xattr_ibody_header))
444 (xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
445 EXT4_XATTR_LEN(item->name_len) >
446 ext4_xattr_block_space(xattr_ref) -
447 sizeof(struct ext4_xattr_header))) {
448 ext4_xattr_item_free(item);
452 if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
453 ext4_xattr_item_free(item);
456 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
457 xattr_ref->ea_size +=
458 EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
459 xattr_ref->dirty = true;
463 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
464 uint8_t name_index, const char *name,
468 struct ext4_xattr_item *item =
469 ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
471 if (item == xattr_ref->iter_from)
472 xattr_ref->iter_from =
473 RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
475 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
476 EXT4_XATTR_LEN(item->name_len);
478 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
479 ext4_xattr_item_free(item);
480 xattr_ref->dirty = true;
486 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
487 struct ext4_xattr_item *item,
488 size_t new_data_size)
491 size_t old_data_size = item->data_size;
492 if ((xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
493 EXT4_XATTR_SIZE(new_data_size)
495 ext4_xattr_inode_space(xattr_ref) -
496 sizeof(struct ext4_xattr_ibody_header))
498 (xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
499 EXT4_XATTR_SIZE(new_data_size)
501 ext4_xattr_block_space(xattr_ref) -
502 sizeof(struct ext4_xattr_header))) {
506 ret = ext4_xattr_item_resize_data(item, new_data_size);
512 EXT4_XATTR_SIZE(old_data_size) +
513 EXT4_XATTR_SIZE(new_data_size);
514 xattr_ref->dirty = true;
518 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
520 struct ext4_xattr_item *item, *save_item;
521 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
523 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
524 ext4_xattr_item_free(item);
526 xattr_ref->ea_size = 0;
529 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
533 ext4_fsblk_t xattr_block = 0;
534 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
538 ext4_fs_inode_to_goal_block(xattr_ref->inode_ref);
540 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
546 ret = ext4_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
549 ext4_balloc_free_block(xattr_ref->inode_ref,
554 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
555 &xattr_ref->fs->sb, xattr_block);
556 xattr_ref->inode_ref->dirty = true;
557 xattr_ref->block_loaded = true;
564 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
566 ext4_fsblk_t xattr_block;
567 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
569 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
571 ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
572 ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
573 xattr_ref->inode_ref->dirty = true;
574 xattr_ref->block_loaded = false;
577 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
579 struct ext4_xattr_header *block_header = NULL;
580 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
582 memset(block_header, 0, sizeof(struct ext4_xattr_header));
583 block_header->h_magic = EXT4_XATTR_MAGIC;
584 block_header->h_refcount = to_le32(1);
585 block_header->h_blocks = to_le32(1);
589 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
590 struct ext4_xattr_ibody_header *ibody_header,
591 struct ext4_xattr_entry *entry, void *ibody_data_ptr)
593 entry->e_name_len = to_le32(item->name_len);
594 entry->e_name_index = item->name_index;
595 entry->e_value_offs =
596 (char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header);
597 entry->e_value_block = 0;
598 entry->e_value_size = item->data_size;
601 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
602 struct ext4_xattr_header *block_header,
603 struct ext4_xattr_entry *block_entry,
604 void *block_data_ptr)
606 block_entry->e_name_len = to_le32(item->name_len);
607 block_entry->e_name_index = item->name_index;
608 block_entry->e_value_offs =
609 (char *)block_data_ptr - (char *)block_header;
610 block_entry->e_value_block = 0;
611 block_entry->e_value_size = item->data_size;
614 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
617 bool block_modified = false;
618 void *ibody_data = NULL;
619 void *block_data = NULL;
620 struct ext4_xattr_item *item, *save_item;
621 size_t inode_size_rem, block_size_rem;
622 struct ext4_xattr_ibody_header *ibody_header = NULL;
623 struct ext4_xattr_header *block_header = NULL;
624 struct ext4_xattr_entry *entry = NULL;
625 struct ext4_xattr_entry *block_entry = NULL;
627 inode_size_rem = ext4_xattr_inode_space(xattr_ref);
628 block_size_rem = ext4_xattr_block_space(xattr_ref);
629 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
630 ibody_header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
631 entry = EXT4_XATTR_IFIRST(ibody_header);
634 if (!xattr_ref->dirty)
636 /* If there are enough spaces in the ibody EA table.*/
637 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
638 memset(ibody_header, 0, inode_size_rem);
639 ibody_header->h_magic = EXT4_XATTR_MAGIC;
640 ibody_data = (char *)ibody_header + inode_size_rem;
641 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
643 xattr_ref->inode_ref->dirty = true;
645 /* If we need an extra block to hold the EA entries*/
646 if (xattr_ref->ea_size > inode_size_rem) {
647 if (!xattr_ref->block_loaded) {
648 ret = ext4_xattr_try_alloc_block(xattr_ref);
652 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
653 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
654 ext4_xattr_set_block_header(xattr_ref);
655 block_data = (char *)block_header + block_size_rem;
656 block_size_rem -= sizeof(struct ext4_xattr_header);
658 ext4_bcache_set_dirty(xattr_ref->block.buf);
660 /* We don't need an extra block.*/
661 if (xattr_ref->block_loaded) {
662 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
663 block_header->h_refcount =
664 to_le32(to_le32(block_header->h_refcount) - 1);
665 if (!block_header->h_refcount) {
666 ext4_xattr_try_free_block(xattr_ref);
670 EXT4_XATTR_BFIRST(&xattr_ref->block);
672 (char *)block_header + block_size_rem;
674 sizeof(struct ext4_xattr_header);
675 ext4_inode_set_file_acl(
676 xattr_ref->inode_ref->inode,
677 &xattr_ref->fs->sb, 0);
679 xattr_ref->inode_ref->dirty = true;
680 ext4_bcache_set_dirty(xattr_ref->block.buf);
684 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
686 if (EXT4_XATTR_SIZE(item->data_size) +
687 EXT4_XATTR_LEN(item->name_len) <=
689 ibody_data = (char *)ibody_data -
690 EXT4_XATTR_SIZE(item->data_size);
691 ext4_xattr_set_inode_entry(item, ibody_header, entry,
693 memcpy(EXT4_XATTR_NAME(entry), item->name,
695 memcpy(ibody_data, item->data, item->data_size);
696 entry = EXT4_XATTR_NEXT(entry);
697 inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
698 EXT4_XATTR_LEN(item->name_len);
700 xattr_ref->inode_ref->dirty = true;
703 if (EXT4_XATTR_SIZE(item->data_size) +
704 EXT4_XATTR_LEN(item->name_len) >
710 (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
711 ext4_xattr_set_block_entry(item, block_header, block_entry,
713 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
715 memcpy(block_data, item->data, item->data_size);
716 block_entry = EXT4_XATTR_NEXT(block_entry);
717 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
718 EXT4_XATTR_LEN(item->name_len);
720 block_modified = true;
722 xattr_ref->dirty = false;
723 if (block_modified) {
724 ext4_xattr_rehash(block_header,
725 EXT4_XATTR_BFIRST(&xattr_ref->block));
726 ext4_xattr_set_block_checksum(xattr_ref->inode_ref,
727 xattr_ref->block.lb_id,
729 ext4_bcache_set_dirty(xattr_ref->block.buf);
736 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
737 int (*iter)(struct ext4_xattr_ref *ref,
738 struct ext4_xattr_item *item))
740 struct ext4_xattr_item *item;
742 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
744 RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
746 int ret = EXT4_XATTR_ITERATE_CONT;
750 if (ret != EXT4_XATTR_ITERATE_CONT) {
751 if (ret == EXT4_XATTR_ITERATE_STOP)
752 ref->iter_from = NULL;
759 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
761 ref->iter_from = NULL;
764 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
765 const char *name, size_t name_len, const void *data,
766 size_t data_size, bool replace)
769 struct ext4_xattr_item *item =
770 ext4_xattr_lookup_item(ref, name_index, name, name_len);
776 if (item->data_size != data_size)
777 ret = ext4_xattr_resize_item(ref, item, data_size);
782 memcpy(item->data, data, data_size);
788 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
797 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
798 const char *name, size_t name_len)
800 return ext4_xattr_remove_item(ref, name_index, name, name_len);
803 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
804 const char *name, size_t name_len, void *buf, size_t buf_size,
808 size_t item_size = 0;
809 struct ext4_xattr_item *item =
810 ext4_xattr_lookup_item(ref, name_index, name, name_len);
816 item_size = item->data_size;
817 if (buf_size > item_size)
818 buf_size = item_size;
821 memcpy(buf, item->data, buf_size);
825 *data_size = item_size;
830 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
831 struct ext4_xattr_ref *ref)
834 ext4_fsblk_t xattr_block;
835 xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
838 ref->iter_from = NULL;
840 rc = ext4_block_get(fs->bdev, &ref->block, xattr_block);
844 ref->block_loaded = true;
846 ref->block_loaded = false;
848 ref->inode_ref = inode_ref;
851 rc = ext4_xattr_fetch(ref);
853 ext4_xattr_purge_items(ref);
855 ext4_block_set(fs->bdev, &inode_ref->block);
857 ref->block_loaded = false;
863 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
865 ext4_xattr_write_to_disk(ref);
866 if (ref->block_loaded) {
867 ext4_block_set(ref->fs->bdev, &ref->block);
868 ref->block_loaded = false;
870 ext4_xattr_purge_items(ref);
871 ref->inode_ref = NULL;
875 struct xattr_prefix {
880 static const struct xattr_prefix prefix_tbl[] = {
881 {"user.", EXT4_XATTR_INDEX_USER},
882 {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
883 {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
884 {"trusted.", EXT4_XATTR_INDEX_TRUSTED},
885 {"security.", EXT4_XATTR_INDEX_SECURITY},
886 {"system.", EXT4_XATTR_INDEX_SYSTEM},
887 {"system.richacl", EXT4_XATTR_INDEX_RICHACL},
891 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
892 uint8_t *name_index, size_t *name_len)
895 ext4_assert(name_index);
896 if (!full_name_len) {
903 for (i = 0; prefix_tbl[i].prefix; i++) {
904 size_t prefix_len = strlen(prefix_tbl[i].prefix);
905 if (full_name_len >= prefix_len &&
906 !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
907 *name_index = prefix_tbl[i].name_index;
909 *name_len = full_name_len - prefix_len;
911 return full_name + prefix_len;
920 const char *ext4_get_xattr_name_prefix(uint8_t name_index, size_t *ret_prefix_len)
924 for (i = 0; prefix_tbl[i].prefix; i++) {
925 size_t prefix_len = strlen(prefix_tbl[i].prefix);
926 if (prefix_tbl[i].name_index == name_index) {
928 *ret_prefix_len = prefix_len;
930 return prefix_tbl[i].prefix;