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);
116 ext4_xattr_block_checksum(struct ext4_inode_ref *inode_ref,
117 ext4_fsblk_t blocknr,
118 struct ext4_xattr_header *header)
120 uint32_t checksum = 0;
121 uint64_t le64_blocknr = blocknr;
122 struct ext4_sblock *sb = &inode_ref->fs->sb;
124 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
125 uint32_t orig_checksum;
127 /* Preparation: temporarily set bg checksum to 0 */
128 orig_checksum = header->h_checksum;
129 header->h_checksum = 0;
130 /* First calculate crc32 checksum against fs uuid */
131 checksum = ext4_crc32c(~0, sb->uuid, sizeof(sb->uuid));
132 /* Then calculate crc32 checksum block number */
133 checksum = ext4_crc32c(checksum, &le64_blocknr,
134 sizeof(le64_blocknr));
135 /* Finally calculate crc32 checksum against
136 * the entire xattr block */
137 checksum = ext4_crc32c(checksum, header,
138 ext4_sb_get_block_size(sb));
139 header->h_checksum = orig_checksum;
145 ext4_xattr_set_block_checksum(struct ext4_inode_ref *inode_ref,
146 ext4_fsblk_t blocknr,
147 struct ext4_xattr_header *header)
149 struct ext4_sblock *sb = &inode_ref->fs->sb;
150 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
154 ext4_xattr_block_checksum(inode_ref, blocknr, header);
157 static int ext4_xattr_item_cmp(struct ext4_xattr_item *a,
158 struct ext4_xattr_item *b)
161 result = a->name_index - b->name_index;
165 result = a->name_len - b->name_len;
169 return memcmp(a->name, b->name, a->name_len);
172 RB_GENERATE_INTERNAL(ext4_xattr_tree, ext4_xattr_item, node,
173 ext4_xattr_item_cmp, static inline)
175 static struct ext4_xattr_item *
176 ext4_xattr_item_alloc(uint8_t name_index, const char *name, size_t name_len)
178 struct ext4_xattr_item *item;
179 item = malloc(sizeof(struct ext4_xattr_item) + name_len);
183 item->name_index = name_index;
184 item->name = (char *)(item + 1);
185 item->name_len = name_len;
189 memset(&item->node, 0, sizeof(item->node));
190 memcpy(item->name, name, name_len);
195 static int ext4_xattr_item_alloc_data(struct ext4_xattr_item *item,
196 const void *orig_data, size_t data_size)
199 ext4_assert(!item->data);
200 data = malloc(data_size);
205 memcpy(data, orig_data, data_size);
208 item->data_size = data_size;
212 static void ext4_xattr_item_free_data(struct ext4_xattr_item *item)
214 ext4_assert(item->data);
220 static int ext4_xattr_item_resize_data(struct ext4_xattr_item *item,
221 size_t new_data_size)
223 if (new_data_size != item->data_size) {
225 new_data = realloc(item->data, new_data_size);
229 item->data = new_data;
230 item->data_size = new_data_size;
235 static void ext4_xattr_item_free(struct ext4_xattr_item *item)
238 ext4_xattr_item_free_data(item);
243 static void *ext4_xattr_entry_data(struct ext4_xattr_ref *xattr_ref,
244 struct ext4_xattr_entry *entry,
249 struct ext4_xattr_ibody_header *header;
250 struct ext4_xattr_entry *first_entry;
252 ext4_get16(&xattr_ref->fs->sb, inode_size);
253 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
254 first_entry = EXT4_XATTR_IFIRST(header);
256 ret = ((char *)first_entry + to_le16(entry->e_value_offs));
257 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
258 (char *)xattr_ref->inode_ref->inode > inode_size)
264 int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
265 ret = ((char *)xattr_ref->block.data + to_le16(entry->e_value_offs));
266 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
267 (char *)xattr_ref->block.data > block_size)
272 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
277 struct ext4_xattr_entry *entry = NULL;
279 ext4_assert(xattr_ref->block.data);
280 entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
282 size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
283 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
284 entry = EXT4_XATTR_NEXT(entry),
285 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
286 struct ext4_xattr_item *item;
287 char *e_name = EXT4_XATTR_NAME(entry);
289 data = ext4_xattr_entry_data(xattr_ref, entry, false);
295 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
296 (size_t)entry->e_name_len);
301 if (ext4_xattr_item_alloc_data(
302 item, data, to_le32(entry->e_value_size)) != EOK) {
303 ext4_xattr_item_free(item);
307 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
308 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
309 EXT4_XATTR_LEN(item->name_len);
316 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
321 struct ext4_xattr_ibody_header *header = NULL;
322 struct ext4_xattr_entry *entry = NULL;
323 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
325 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
326 entry = EXT4_XATTR_IFIRST(header);
328 size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
329 xattr_ref->inode_ref->inode->extra_isize;
330 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
331 entry = EXT4_XATTR_NEXT(entry),
332 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
333 struct ext4_xattr_item *item;
334 char *e_name = EXT4_XATTR_NAME(entry);
336 data = ext4_xattr_entry_data(xattr_ref, entry, true);
342 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
343 (size_t)entry->e_name_len);
348 if (ext4_xattr_item_alloc_data(
349 item, data, to_le32(entry->e_value_size)) != EOK) {
350 ext4_xattr_item_free(item);
354 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
355 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
356 EXT4_XATTR_LEN(item->name_len);
363 static size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
365 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
366 uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
367 xattr_ref->inode_ref->inode->extra_isize;
371 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
373 return ext4_sb_get_block_size(&xattr_ref->fs->sb);
376 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
379 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
380 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
381 ret = ext4_xattr_inode_fetch(xattr_ref);
386 if (xattr_ref->block_loaded)
387 ret = ext4_xattr_block_fetch(xattr_ref);
389 xattr_ref->dirty = false;
393 static struct ext4_xattr_item *
394 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
395 const char *name, size_t name_len)
397 struct ext4_xattr_item tmp = {
398 .name_index = name_index,
399 .name = (char *)name, /*RB_FIND - won't touch this string*/
400 .name_len = name_len,
403 return RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
406 static struct ext4_xattr_item *
407 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
408 const char *name, size_t name_len, const void *data,
411 struct ext4_xattr_item *item;
412 item = ext4_xattr_item_alloc(name_index, name, name_len);
416 if ((xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
417 EXT4_XATTR_LEN(item->name_len)
419 ext4_xattr_inode_space(xattr_ref) -
420 sizeof(struct ext4_xattr_ibody_header))
422 (xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
423 EXT4_XATTR_LEN(item->name_len) >
424 ext4_xattr_block_space(xattr_ref) -
425 sizeof(struct ext4_xattr_header))) {
426 ext4_xattr_item_free(item);
430 if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
431 ext4_xattr_item_free(item);
434 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
435 xattr_ref->ea_size +=
436 EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
437 xattr_ref->dirty = true;
441 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
442 uint8_t name_index, const char *name,
446 struct ext4_xattr_item *item =
447 ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
449 if (item == xattr_ref->iter_from)
450 xattr_ref->iter_from =
451 RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
453 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
454 EXT4_XATTR_LEN(item->name_len);
456 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
457 ext4_xattr_item_free(item);
458 xattr_ref->dirty = true;
464 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
465 struct ext4_xattr_item *item,
466 size_t new_data_size)
469 size_t old_data_size = item->data_size;
470 if ((xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
471 EXT4_XATTR_SIZE(new_data_size)
473 ext4_xattr_inode_space(xattr_ref) -
474 sizeof(struct ext4_xattr_ibody_header))
476 (xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
477 EXT4_XATTR_SIZE(new_data_size)
479 ext4_xattr_block_space(xattr_ref) -
480 sizeof(struct ext4_xattr_header))) {
484 ret = ext4_xattr_item_resize_data(item, new_data_size);
490 EXT4_XATTR_SIZE(old_data_size) +
491 EXT4_XATTR_SIZE(new_data_size);
492 xattr_ref->dirty = true;
496 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
498 struct ext4_xattr_item *item, *save_item;
499 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
501 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
502 ext4_xattr_item_free(item);
504 xattr_ref->ea_size = 0;
507 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
511 ext4_fsblk_t xattr_block = 0;
512 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
516 ext4_fs_inode_to_goal_block(xattr_ref->inode_ref);
518 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
524 ret = ext4_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
527 ext4_balloc_free_block(xattr_ref->inode_ref,
532 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
533 &xattr_ref->fs->sb, xattr_block);
534 xattr_ref->inode_ref->dirty = true;
535 xattr_ref->block_loaded = true;
542 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
544 ext4_fsblk_t xattr_block;
545 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
547 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
549 ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
550 ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
551 xattr_ref->inode_ref->dirty = true;
552 xattr_ref->block_loaded = false;
555 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
557 struct ext4_xattr_header *block_header = NULL;
558 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
560 memset(block_header, 0, sizeof(struct ext4_xattr_header));
561 block_header->h_magic = EXT4_XATTR_MAGIC;
562 block_header->h_refcount = to_le32(1);
563 block_header->h_blocks = to_le32(1);
567 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
568 struct ext4_xattr_ibody_header *ibody_header,
569 struct ext4_xattr_entry *entry, void *ibody_data_ptr)
571 entry->e_name_len = to_le32(item->name_len);
572 entry->e_name_index = item->name_index;
573 entry->e_value_offs =
574 (char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header);
575 entry->e_value_block = 0;
576 entry->e_value_size = item->data_size;
579 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
580 struct ext4_xattr_header *block_header,
581 struct ext4_xattr_entry *block_entry,
582 void *block_data_ptr)
584 block_entry->e_name_len = to_le32(item->name_len);
585 block_entry->e_name_index = item->name_index;
586 block_entry->e_value_offs =
587 (char *)block_data_ptr - (char *)block_header;
588 block_entry->e_value_block = 0;
589 block_entry->e_value_size = item->data_size;
592 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
595 bool block_modified = false;
596 void *ibody_data = NULL;
597 void *block_data = NULL;
598 struct ext4_xattr_item *item, *save_item;
599 size_t inode_size_rem, block_size_rem;
600 struct ext4_xattr_ibody_header *ibody_header = NULL;
601 struct ext4_xattr_header *block_header = NULL;
602 struct ext4_xattr_entry *entry = NULL;
603 struct ext4_xattr_entry *block_entry = NULL;
605 inode_size_rem = ext4_xattr_inode_space(xattr_ref);
606 block_size_rem = ext4_xattr_block_space(xattr_ref);
607 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
608 ibody_header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
609 entry = EXT4_XATTR_IFIRST(ibody_header);
612 if (!xattr_ref->dirty)
614 /* If there are enough spaces in the ibody EA table.*/
615 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
616 memset(ibody_header, 0, inode_size_rem);
617 ibody_header->h_magic = EXT4_XATTR_MAGIC;
618 ibody_data = (char *)ibody_header + inode_size_rem;
619 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
621 xattr_ref->inode_ref->dirty = true;
623 /* If we need an extra block to hold the EA entries*/
624 if (xattr_ref->ea_size > inode_size_rem) {
625 if (!xattr_ref->block_loaded) {
626 ret = ext4_xattr_try_alloc_block(xattr_ref);
630 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
631 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
632 ext4_xattr_set_block_header(xattr_ref);
633 block_data = (char *)block_header + block_size_rem;
634 block_size_rem -= sizeof(struct ext4_xattr_header);
636 xattr_ref->block.dirty = true;
638 /* We don't need an extra block.*/
639 if (xattr_ref->block_loaded) {
640 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
641 block_header->h_refcount =
642 to_le32(to_le32(block_header->h_refcount) - 1);
643 if (!block_header->h_refcount) {
644 ext4_xattr_try_free_block(xattr_ref);
648 EXT4_XATTR_BFIRST(&xattr_ref->block);
650 (char *)block_header + block_size_rem;
652 sizeof(struct ext4_xattr_header);
653 ext4_inode_set_file_acl(
654 xattr_ref->inode_ref->inode,
655 &xattr_ref->fs->sb, 0);
657 xattr_ref->inode_ref->dirty = true;
658 xattr_ref->block.dirty = true;
662 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
664 if (EXT4_XATTR_SIZE(item->data_size) +
665 EXT4_XATTR_LEN(item->name_len) <=
667 ibody_data = (char *)ibody_data -
668 EXT4_XATTR_SIZE(item->data_size);
669 ext4_xattr_set_inode_entry(item, ibody_header, entry,
671 memcpy(EXT4_XATTR_NAME(entry), item->name,
673 memcpy(ibody_data, item->data, item->data_size);
674 entry = EXT4_XATTR_NEXT(entry);
675 inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
676 EXT4_XATTR_LEN(item->name_len);
678 xattr_ref->inode_ref->dirty = true;
681 if (EXT4_XATTR_SIZE(item->data_size) +
682 EXT4_XATTR_LEN(item->name_len) >
688 (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
689 ext4_xattr_set_block_entry(item, block_header, block_entry,
691 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
693 memcpy(block_data, item->data, item->data_size);
694 block_entry = EXT4_XATTR_NEXT(block_entry);
695 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
696 EXT4_XATTR_LEN(item->name_len);
698 block_modified = true;
700 xattr_ref->dirty = false;
701 if (block_modified) {
702 ext4_xattr_rehash(block_header,
703 EXT4_XATTR_BFIRST(&xattr_ref->block));
704 ext4_xattr_set_block_checksum(xattr_ref->inode_ref,
705 xattr_ref->block.lb_id,
707 xattr_ref->block.dirty = true;
714 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
715 int (*iter)(struct ext4_xattr_ref *ref,
716 struct ext4_xattr_item *item))
718 struct ext4_xattr_item *item;
720 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
722 RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
724 int ret = EXT4_XATTR_ITERATE_CONT;
728 if (ret != EXT4_XATTR_ITERATE_CONT) {
729 if (ret == EXT4_XATTR_ITERATE_STOP)
730 ref->iter_from = NULL;
737 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
739 ref->iter_from = NULL;
742 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
743 const char *name, size_t name_len, const void *data,
744 size_t data_size, bool replace)
747 struct ext4_xattr_item *item =
748 ext4_xattr_lookup_item(ref, name_index, name, name_len);
754 if (item->data_size != data_size)
755 ret = ext4_xattr_resize_item(ref, item, data_size);
760 memcpy(item->data, data, data_size);
766 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
775 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
776 const char *name, size_t name_len)
778 return ext4_xattr_remove_item(ref, name_index, name, name_len);
781 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
782 const char *name, size_t name_len, void *buf, size_t buf_size,
786 size_t item_size = 0;
787 struct ext4_xattr_item *item =
788 ext4_xattr_lookup_item(ref, name_index, name, name_len);
794 item_size = item->data_size;
795 if (buf_size > item_size)
796 buf_size = item_size;
799 memcpy(buf, item->data, buf_size);
803 *data_size = item_size;
808 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
809 struct ext4_xattr_ref *ref)
812 ext4_fsblk_t xattr_block;
813 xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
816 ref->iter_from = NULL;
818 rc = ext4_block_get(fs->bdev, &ref->block, xattr_block);
822 ref->block_loaded = true;
824 ref->block_loaded = false;
826 ref->inode_ref = inode_ref;
829 rc = ext4_xattr_fetch(ref);
831 ext4_xattr_purge_items(ref);
833 ext4_block_set(fs->bdev, &inode_ref->block);
835 ref->block_loaded = false;
841 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
843 ext4_xattr_write_to_disk(ref);
844 if (ref->block_loaded) {
845 ext4_block_set(ref->fs->bdev, &ref->block);
846 ref->block_loaded = false;
848 ext4_xattr_purge_items(ref);
849 ref->inode_ref = NULL;
853 struct xattr_prefix {
858 static const struct xattr_prefix prefix_tbl[] = {
859 {"user.", EXT4_XATTR_INDEX_USER},
860 {"system.", EXT4_XATTR_INDEX_SYSTEM},
861 {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
862 {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
866 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
867 uint8_t *name_index, size_t *name_len)
870 ext4_assert(name_index);
871 if (!full_name_len) {
878 for (i = 0; prefix_tbl[i].prefix; i++) {
879 size_t prefix_len = strlen(prefix_tbl[i].prefix);
880 if (full_name_len >= prefix_len &&
881 !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
882 *name_index = prefix_tbl[i].name_index;
884 *name_len = full_name_len - prefix_len;
886 return full_name + prefix_len;
895 const char *ext4_get_xattr_name_prefix(uint8_t name_index, size_t *ret_prefix_len)
899 for (i = 0; prefix_tbl[i].prefix; i++) {
900 size_t prefix_len = strlen(prefix_tbl[i].prefix);
901 if (prefix_tbl[i].name_index == name_index) {
903 *ret_prefix_len = prefix_len;
905 return prefix_tbl[i].prefix;