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;
211 static int ext4_xattr_item_alloc_data(struct ext4_xattr_item *item,
212 const void *orig_data, size_t data_size)
215 ext4_assert(!item->data);
216 data = malloc(data_size);
221 memcpy(data, orig_data, data_size);
224 item->data_size = data_size;
228 static void ext4_xattr_item_free_data(struct ext4_xattr_item *item)
230 ext4_assert(item->data);
236 static int ext4_xattr_item_resize_data(struct ext4_xattr_item *item,
237 size_t new_data_size)
239 if (new_data_size != item->data_size) {
241 new_data = realloc(item->data, new_data_size);
245 item->data = new_data;
246 item->data_size = new_data_size;
251 static void ext4_xattr_item_free(struct ext4_xattr_item *item)
254 ext4_xattr_item_free_data(item);
259 static void *ext4_xattr_entry_data(struct ext4_xattr_ref *xattr_ref,
260 struct ext4_xattr_entry *entry,
265 struct ext4_xattr_ibody_header *header;
266 struct ext4_xattr_entry *first_entry;
268 ext4_get16(&xattr_ref->fs->sb, inode_size);
269 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
270 first_entry = EXT4_XATTR_IFIRST(header);
272 ret = ((char *)first_entry + to_le16(entry->e_value_offs));
273 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
274 (char *)xattr_ref->inode_ref->inode > inode_size)
280 int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
281 ret = ((char *)xattr_ref->block.data + to_le16(entry->e_value_offs));
282 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
283 (char *)xattr_ref->block.data > block_size)
288 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
293 struct ext4_xattr_entry *entry = NULL;
295 ext4_assert(xattr_ref->block.data);
296 entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
298 size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
299 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
300 entry = EXT4_XATTR_NEXT(entry),
301 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
302 struct ext4_xattr_item *item;
303 char *e_name = EXT4_XATTR_NAME(entry);
305 data = ext4_xattr_entry_data(xattr_ref, entry, false);
311 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
312 (size_t)entry->e_name_len);
317 if (ext4_xattr_item_alloc_data(
318 item, data, to_le32(entry->e_value_size)) != EOK) {
319 ext4_xattr_item_free(item);
323 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
324 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
325 EXT4_XATTR_LEN(item->name_len);
332 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
337 struct ext4_xattr_ibody_header *header = NULL;
338 struct ext4_xattr_entry *entry = NULL;
339 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
341 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
342 entry = EXT4_XATTR_IFIRST(header);
344 size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
345 xattr_ref->inode_ref->inode->extra_isize;
346 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
347 entry = EXT4_XATTR_NEXT(entry),
348 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
349 struct ext4_xattr_item *item;
350 char *e_name = EXT4_XATTR_NAME(entry);
352 data = ext4_xattr_entry_data(xattr_ref, entry, true);
358 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
359 (size_t)entry->e_name_len);
364 if (ext4_xattr_item_alloc_data(
365 item, data, to_le32(entry->e_value_size)) != EOK) {
366 ext4_xattr_item_free(item);
370 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
371 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
372 EXT4_XATTR_LEN(item->name_len);
379 static size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
381 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
382 uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
383 xattr_ref->inode_ref->inode->extra_isize;
387 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
389 return ext4_sb_get_block_size(&xattr_ref->fs->sb);
392 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
395 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
396 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
397 ret = ext4_xattr_inode_fetch(xattr_ref);
402 if (xattr_ref->block_loaded)
403 ret = ext4_xattr_block_fetch(xattr_ref);
405 xattr_ref->dirty = false;
409 static struct ext4_xattr_item *
410 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
411 const char *name, size_t name_len)
413 struct ext4_xattr_item tmp = {
414 .name_index = name_index,
415 .name = (char *)name, /*RB_FIND - won't touch this string*/
416 .name_len = name_len,
418 if (name_index == EXT4_XATTR_INDEX_SYSTEM &&
420 !memcmp(name, "data", 4))
423 return RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
426 static struct ext4_xattr_item *
427 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
428 const char *name, size_t name_len, const void *data,
431 struct ext4_xattr_item *item;
432 item = ext4_xattr_item_alloc(name_index, name, name_len);
436 if ((xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
437 EXT4_XATTR_LEN(item->name_len)
439 ext4_xattr_inode_space(xattr_ref) -
440 sizeof(struct ext4_xattr_ibody_header))
442 (xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
443 EXT4_XATTR_LEN(item->name_len) >
444 ext4_xattr_block_space(xattr_ref) -
445 sizeof(struct ext4_xattr_header))) {
446 ext4_xattr_item_free(item);
450 if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
451 ext4_xattr_item_free(item);
454 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
455 xattr_ref->ea_size +=
456 EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
457 xattr_ref->dirty = true;
461 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
462 uint8_t name_index, const char *name,
466 struct ext4_xattr_item *item =
467 ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
469 if (item == xattr_ref->iter_from)
470 xattr_ref->iter_from =
471 RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
473 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
474 EXT4_XATTR_LEN(item->name_len);
476 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
477 ext4_xattr_item_free(item);
478 xattr_ref->dirty = true;
484 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
485 struct ext4_xattr_item *item,
486 size_t new_data_size)
489 size_t old_data_size = item->data_size;
490 if ((xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
491 EXT4_XATTR_SIZE(new_data_size)
493 ext4_xattr_inode_space(xattr_ref) -
494 sizeof(struct ext4_xattr_ibody_header))
496 (xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
497 EXT4_XATTR_SIZE(new_data_size)
499 ext4_xattr_block_space(xattr_ref) -
500 sizeof(struct ext4_xattr_header))) {
504 ret = ext4_xattr_item_resize_data(item, new_data_size);
510 EXT4_XATTR_SIZE(old_data_size) +
511 EXT4_XATTR_SIZE(new_data_size);
512 xattr_ref->dirty = true;
516 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
518 struct ext4_xattr_item *item, *save_item;
519 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
521 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
522 ext4_xattr_item_free(item);
524 xattr_ref->ea_size = 0;
527 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
531 ext4_fsblk_t xattr_block = 0;
532 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
536 ext4_fs_inode_to_goal_block(xattr_ref->inode_ref);
538 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
544 ret = ext4_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
547 ext4_balloc_free_block(xattr_ref->inode_ref,
552 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
553 &xattr_ref->fs->sb, xattr_block);
554 xattr_ref->inode_ref->dirty = true;
555 xattr_ref->block_loaded = true;
562 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
564 ext4_fsblk_t xattr_block;
565 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
567 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
569 ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
570 ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
571 xattr_ref->inode_ref->dirty = true;
572 xattr_ref->block_loaded = false;
575 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
577 struct ext4_xattr_header *block_header = NULL;
578 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
580 memset(block_header, 0, sizeof(struct ext4_xattr_header));
581 block_header->h_magic = EXT4_XATTR_MAGIC;
582 block_header->h_refcount = to_le32(1);
583 block_header->h_blocks = to_le32(1);
587 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
588 struct ext4_xattr_ibody_header *ibody_header,
589 struct ext4_xattr_entry *entry, void *ibody_data_ptr)
591 entry->e_name_len = to_le32(item->name_len);
592 entry->e_name_index = item->name_index;
593 entry->e_value_offs =
594 (char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header);
595 entry->e_value_block = 0;
596 entry->e_value_size = item->data_size;
599 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
600 struct ext4_xattr_header *block_header,
601 struct ext4_xattr_entry *block_entry,
602 void *block_data_ptr)
604 block_entry->e_name_len = to_le32(item->name_len);
605 block_entry->e_name_index = item->name_index;
606 block_entry->e_value_offs =
607 (char *)block_data_ptr - (char *)block_header;
608 block_entry->e_value_block = 0;
609 block_entry->e_value_size = item->data_size;
612 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
615 bool block_modified = false;
616 void *ibody_data = NULL;
617 void *block_data = NULL;
618 struct ext4_xattr_item *item, *save_item;
619 size_t inode_size_rem, block_size_rem;
620 struct ext4_xattr_ibody_header *ibody_header = NULL;
621 struct ext4_xattr_header *block_header = NULL;
622 struct ext4_xattr_entry *entry = NULL;
623 struct ext4_xattr_entry *block_entry = NULL;
625 inode_size_rem = ext4_xattr_inode_space(xattr_ref);
626 block_size_rem = ext4_xattr_block_space(xattr_ref);
627 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
628 ibody_header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
629 entry = EXT4_XATTR_IFIRST(ibody_header);
632 if (!xattr_ref->dirty)
634 /* If there are enough spaces in the ibody EA table.*/
635 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
636 memset(ibody_header, 0, inode_size_rem);
637 ibody_header->h_magic = EXT4_XATTR_MAGIC;
638 ibody_data = (char *)ibody_header + inode_size_rem;
639 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
641 xattr_ref->inode_ref->dirty = true;
643 /* If we need an extra block to hold the EA entries*/
644 if (xattr_ref->ea_size > inode_size_rem) {
645 if (!xattr_ref->block_loaded) {
646 ret = ext4_xattr_try_alloc_block(xattr_ref);
650 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
651 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
652 ext4_xattr_set_block_header(xattr_ref);
653 block_data = (char *)block_header + block_size_rem;
654 block_size_rem -= sizeof(struct ext4_xattr_header);
656 xattr_ref->block.dirty = true;
658 /* We don't need an extra block.*/
659 if (xattr_ref->block_loaded) {
660 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
661 block_header->h_refcount =
662 to_le32(to_le32(block_header->h_refcount) - 1);
663 if (!block_header->h_refcount) {
664 ext4_xattr_try_free_block(xattr_ref);
668 EXT4_XATTR_BFIRST(&xattr_ref->block);
670 (char *)block_header + block_size_rem;
672 sizeof(struct ext4_xattr_header);
673 ext4_inode_set_file_acl(
674 xattr_ref->inode_ref->inode,
675 &xattr_ref->fs->sb, 0);
677 xattr_ref->inode_ref->dirty = true;
678 xattr_ref->block.dirty = true;
682 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
684 if (EXT4_XATTR_SIZE(item->data_size) +
685 EXT4_XATTR_LEN(item->name_len) <=
687 ibody_data = (char *)ibody_data -
688 EXT4_XATTR_SIZE(item->data_size);
689 ext4_xattr_set_inode_entry(item, ibody_header, entry,
691 memcpy(EXT4_XATTR_NAME(entry), item->name,
693 memcpy(ibody_data, item->data, item->data_size);
694 entry = EXT4_XATTR_NEXT(entry);
695 inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
696 EXT4_XATTR_LEN(item->name_len);
698 xattr_ref->inode_ref->dirty = true;
701 if (EXT4_XATTR_SIZE(item->data_size) +
702 EXT4_XATTR_LEN(item->name_len) >
708 (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
709 ext4_xattr_set_block_entry(item, block_header, block_entry,
711 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
713 memcpy(block_data, item->data, item->data_size);
714 block_entry = EXT4_XATTR_NEXT(block_entry);
715 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
716 EXT4_XATTR_LEN(item->name_len);
718 block_modified = true;
720 xattr_ref->dirty = false;
721 if (block_modified) {
722 ext4_xattr_rehash(block_header,
723 EXT4_XATTR_BFIRST(&xattr_ref->block));
724 ext4_xattr_set_block_checksum(xattr_ref->inode_ref,
725 xattr_ref->block.lb_id,
727 xattr_ref->block.dirty = true;
734 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
735 int (*iter)(struct ext4_xattr_ref *ref,
736 struct ext4_xattr_item *item))
738 struct ext4_xattr_item *item;
740 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
742 RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
744 int ret = EXT4_XATTR_ITERATE_CONT;
748 if (ret != EXT4_XATTR_ITERATE_CONT) {
749 if (ret == EXT4_XATTR_ITERATE_STOP)
750 ref->iter_from = NULL;
757 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
759 ref->iter_from = NULL;
762 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
763 const char *name, size_t name_len, const void *data,
764 size_t data_size, bool replace)
767 struct ext4_xattr_item *item =
768 ext4_xattr_lookup_item(ref, name_index, name, name_len);
774 if (item->data_size != data_size)
775 ret = ext4_xattr_resize_item(ref, item, data_size);
780 memcpy(item->data, data, data_size);
786 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
795 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
796 const char *name, size_t name_len)
798 return ext4_xattr_remove_item(ref, name_index, name, name_len);
801 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
802 const char *name, size_t name_len, void *buf, size_t buf_size,
806 size_t item_size = 0;
807 struct ext4_xattr_item *item =
808 ext4_xattr_lookup_item(ref, name_index, name, name_len);
814 item_size = item->data_size;
815 if (buf_size > item_size)
816 buf_size = item_size;
819 memcpy(buf, item->data, buf_size);
823 *data_size = item_size;
828 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
829 struct ext4_xattr_ref *ref)
832 ext4_fsblk_t xattr_block;
833 xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
836 ref->iter_from = NULL;
838 rc = ext4_block_get(fs->bdev, &ref->block, xattr_block);
842 ref->block_loaded = true;
844 ref->block_loaded = false;
846 ref->inode_ref = inode_ref;
849 rc = ext4_xattr_fetch(ref);
851 ext4_xattr_purge_items(ref);
853 ext4_block_set(fs->bdev, &inode_ref->block);
855 ref->block_loaded = false;
861 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
863 ext4_xattr_write_to_disk(ref);
864 if (ref->block_loaded) {
865 ext4_block_set(ref->fs->bdev, &ref->block);
866 ref->block_loaded = false;
868 ext4_xattr_purge_items(ref);
869 ref->inode_ref = NULL;
873 struct xattr_prefix {
878 static const struct xattr_prefix prefix_tbl[] = {
879 {"user.", EXT4_XATTR_INDEX_USER},
880 {"system.", EXT4_XATTR_INDEX_SYSTEM},
881 {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
882 {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
886 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
887 uint8_t *name_index, size_t *name_len)
890 ext4_assert(name_index);
891 if (!full_name_len) {
898 for (i = 0; prefix_tbl[i].prefix; i++) {
899 size_t prefix_len = strlen(prefix_tbl[i].prefix);
900 if (full_name_len >= prefix_len &&
901 !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
902 *name_index = prefix_tbl[i].name_index;
904 *name_len = full_name_len - prefix_len;
906 return full_name + prefix_len;
915 const char *ext4_get_xattr_name_prefix(uint8_t name_index, size_t *ret_prefix_len)
919 for (i = 0; prefix_tbl[i].prefix; i++) {
920 size_t prefix_len = strlen(prefix_tbl[i].prefix);
921 if (prefix_tbl[i].name_index == name_index) {
923 *ret_prefix_len = prefix_len;
925 return prefix_tbl[i].prefix;