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_has_feature_read_only(sb,
125 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
126 uint32_t orig_checksum, 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(~0, sb->uuid, sizeof(sb->uuid));
133 /* Then calculate crc32 checksum block number */
134 checksum = ext4_crc32c(checksum, &le64_blocknr,
135 sizeof(le64_blocknr));
136 /* Finally calculate crc32 checksum against
137 * the entire xattr block */
138 checksum = ext4_crc32c(checksum, header,
139 ext4_sb_get_block_size(sb));
140 header->h_checksum = orig_checksum;
146 ext4_xattr_set_block_checksum(struct ext4_inode_ref *inode_ref,
147 ext4_fsblk_t blocknr,
148 struct ext4_xattr_header *header)
150 struct ext4_sblock *sb = &inode_ref->fs->sb;
151 if (!ext4_sb_has_feature_read_only(sb,
152 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
156 ext4_xattr_block_checksum(inode_ref, blocknr, header);
159 static int ext4_xattr_item_cmp(struct ext4_xattr_item *a,
160 struct ext4_xattr_item *b)
163 result = a->name_index - b->name_index;
167 result = a->name_len - b->name_len;
171 return memcmp(a->name, b->name, a->name_len);
174 RB_GENERATE_INTERNAL(ext4_xattr_tree, ext4_xattr_item, node,
175 ext4_xattr_item_cmp, static inline)
177 static struct ext4_xattr_item *
178 ext4_xattr_item_alloc(uint8_t name_index, const char *name, size_t name_len)
180 struct ext4_xattr_item *item;
181 item = malloc(sizeof(struct ext4_xattr_item) + name_len);
185 item->name_index = name_index;
186 item->name = (char *)(item + 1);
187 item->name_len = name_len;
191 memset(&item->node, 0, sizeof(item->node));
192 memcpy(item->name, name, name_len);
197 static int ext4_xattr_item_alloc_data(struct ext4_xattr_item *item,
198 const void *orig_data, size_t data_size)
201 ext4_assert(!item->data);
202 data = malloc(data_size);
207 memcpy(data, orig_data, data_size);
210 item->data_size = data_size;
214 static void ext4_xattr_item_free_data(struct ext4_xattr_item *item)
216 ext4_assert(item->data);
222 static int ext4_xattr_item_resize_data(struct ext4_xattr_item *item,
223 size_t new_data_size)
225 if (new_data_size != item->data_size) {
227 new_data = realloc(item->data, new_data_size);
231 item->data = new_data;
232 item->data_size = new_data_size;
237 static void ext4_xattr_item_free(struct ext4_xattr_item *item)
240 ext4_xattr_item_free_data(item);
245 static void *ext4_xattr_entry_data(struct ext4_xattr_ref *xattr_ref,
246 struct ext4_xattr_entry *entry,
251 struct ext4_xattr_ibody_header *header;
252 struct ext4_xattr_entry *first_entry;
254 ext4_get16(&xattr_ref->fs->sb, inode_size);
255 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
256 first_entry = EXT4_XATTR_IFIRST(header);
258 ret = ((char *)first_entry + to_le16(entry->e_value_offs));
259 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
260 (char *)xattr_ref->inode_ref->inode > inode_size)
266 int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
267 ret = ((char *)xattr_ref->block.data + to_le16(entry->e_value_offs));
268 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
269 (char *)xattr_ref->block.data > block_size)
274 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
279 struct ext4_xattr_entry *entry = NULL;
281 ext4_assert(xattr_ref->block.data);
282 entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
284 size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
285 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
286 entry = EXT4_XATTR_NEXT(entry),
287 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
288 struct ext4_xattr_item *item;
289 char *e_name = EXT4_XATTR_NAME(entry);
291 data = ext4_xattr_entry_data(xattr_ref, entry, false);
297 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
298 (size_t)entry->e_name_len);
303 if (ext4_xattr_item_alloc_data(
304 item, data, to_le32(entry->e_value_size)) != EOK) {
305 ext4_xattr_item_free(item);
309 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
310 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
311 EXT4_XATTR_LEN(item->name_len);
318 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
323 struct ext4_xattr_ibody_header *header = NULL;
324 struct ext4_xattr_entry *entry = NULL;
325 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
327 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
328 entry = EXT4_XATTR_IFIRST(header);
330 size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
331 xattr_ref->inode_ref->inode->extra_isize;
332 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
333 entry = EXT4_XATTR_NEXT(entry),
334 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
335 struct ext4_xattr_item *item;
336 char *e_name = EXT4_XATTR_NAME(entry);
338 data = ext4_xattr_entry_data(xattr_ref, entry, true);
344 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
345 (size_t)entry->e_name_len);
350 if (ext4_xattr_item_alloc_data(
351 item, data, to_le32(entry->e_value_size)) != EOK) {
352 ext4_xattr_item_free(item);
356 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
357 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
358 EXT4_XATTR_LEN(item->name_len);
365 static size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
367 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
368 uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
369 xattr_ref->inode_ref->inode->extra_isize;
373 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
375 return ext4_sb_get_block_size(&xattr_ref->fs->sb);
378 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
381 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
382 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
383 ret = ext4_xattr_inode_fetch(xattr_ref);
388 if (xattr_ref->block_loaded)
389 ret = ext4_xattr_block_fetch(xattr_ref);
391 xattr_ref->dirty = false;
395 static struct ext4_xattr_item *
396 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
397 const char *name, size_t name_len)
399 struct ext4_xattr_item tmp = {
400 .name_index = name_index,
401 .name = (char *)name, /*RB_FIND - won't touch this string*/
402 .name_len = name_len,
405 return RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
408 static struct ext4_xattr_item *
409 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
410 const char *name, size_t name_len, const void *data,
413 struct ext4_xattr_item *item;
414 item = ext4_xattr_item_alloc(name_index, name, name_len);
418 if ((xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
419 EXT4_XATTR_LEN(item->name_len)
421 ext4_xattr_inode_space(xattr_ref) -
422 sizeof(struct ext4_xattr_ibody_header))
424 (xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
425 EXT4_XATTR_LEN(item->name_len) >
426 ext4_xattr_block_space(xattr_ref) -
427 sizeof(struct ext4_xattr_header))) {
428 ext4_xattr_item_free(item);
432 if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
433 ext4_xattr_item_free(item);
436 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
437 xattr_ref->ea_size +=
438 EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
439 xattr_ref->dirty = true;
443 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
444 uint8_t name_index, const char *name,
448 struct ext4_xattr_item *item =
449 ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
451 if (item == xattr_ref->iter_from)
452 xattr_ref->iter_from =
453 RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
455 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
456 EXT4_XATTR_LEN(item->name_len);
458 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
459 ext4_xattr_item_free(item);
460 xattr_ref->dirty = true;
466 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
467 struct ext4_xattr_item *item,
468 size_t new_data_size)
471 size_t old_data_size = item->data_size;
472 if ((xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
473 EXT4_XATTR_SIZE(new_data_size)
475 ext4_xattr_inode_space(xattr_ref) -
476 sizeof(struct ext4_xattr_ibody_header))
478 (xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
479 EXT4_XATTR_SIZE(new_data_size)
481 ext4_xattr_block_space(xattr_ref) -
482 sizeof(struct ext4_xattr_header))) {
486 ret = ext4_xattr_item_resize_data(item, new_data_size);
492 EXT4_XATTR_SIZE(old_data_size) +
493 EXT4_XATTR_SIZE(new_data_size);
494 xattr_ref->dirty = true;
498 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
500 struct ext4_xattr_item *item, *save_item;
501 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
503 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
504 ext4_xattr_item_free(item);
506 xattr_ref->ea_size = 0;
509 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
513 ext4_fsblk_t xattr_block = 0;
514 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
518 ext4_fs_inode_to_goal_block(xattr_ref->inode_ref);
520 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
526 ret = ext4_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
529 ext4_balloc_free_block(xattr_ref->inode_ref,
534 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
535 &xattr_ref->fs->sb, xattr_block);
536 xattr_ref->inode_ref->dirty = true;
537 xattr_ref->block_loaded = true;
544 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
546 ext4_fsblk_t xattr_block;
547 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
549 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
551 ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
552 ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
553 xattr_ref->inode_ref->dirty = true;
554 xattr_ref->block_loaded = false;
557 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
559 struct ext4_xattr_header *block_header = NULL;
560 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
562 memset(block_header, 0, sizeof(struct ext4_xattr_header));
563 block_header->h_magic = EXT4_XATTR_MAGIC;
564 block_header->h_refcount = to_le32(1);
565 block_header->h_blocks = to_le32(1);
569 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
570 struct ext4_xattr_ibody_header *ibody_header,
571 struct ext4_xattr_entry *entry, void *ibody_data_ptr)
573 entry->e_name_len = to_le32(item->name_len);
574 entry->e_name_index = item->name_index;
575 entry->e_value_offs =
576 (char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header);
577 entry->e_value_block = 0;
578 entry->e_value_size = item->data_size;
581 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
582 struct ext4_xattr_header *block_header,
583 struct ext4_xattr_entry *block_entry,
584 void *block_data_ptr)
586 block_entry->e_name_len = to_le32(item->name_len);
587 block_entry->e_name_index = item->name_index;
588 block_entry->e_value_offs =
589 (char *)block_data_ptr - (char *)block_header;
590 block_entry->e_value_block = 0;
591 block_entry->e_value_size = item->data_size;
594 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
597 bool block_modified = false;
598 void *ibody_data = NULL;
599 void *block_data = NULL;
600 struct ext4_xattr_item *item, *save_item;
601 size_t inode_size_rem, block_size_rem;
602 struct ext4_xattr_ibody_header *ibody_header = NULL;
603 struct ext4_xattr_header *block_header = NULL;
604 struct ext4_xattr_entry *entry = NULL;
605 struct ext4_xattr_entry *block_entry = NULL;
607 inode_size_rem = ext4_xattr_inode_space(xattr_ref);
608 block_size_rem = ext4_xattr_block_space(xattr_ref);
609 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
610 ibody_header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
611 entry = EXT4_XATTR_IFIRST(ibody_header);
614 if (!xattr_ref->dirty)
616 /* If there are enough spaces in the ibody EA table.*/
617 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
618 memset(ibody_header, 0, inode_size_rem);
619 ibody_header->h_magic = EXT4_XATTR_MAGIC;
620 ibody_data = (char *)ibody_header + inode_size_rem;
621 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
623 xattr_ref->inode_ref->dirty = true;
625 /* If we need an extra block to hold the EA entries*/
626 if (xattr_ref->ea_size > inode_size_rem) {
627 if (!xattr_ref->block_loaded) {
628 ret = ext4_xattr_try_alloc_block(xattr_ref);
632 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
633 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
634 ext4_xattr_set_block_header(xattr_ref);
635 block_data = (char *)block_header + block_size_rem;
636 block_size_rem -= sizeof(struct ext4_xattr_header);
638 xattr_ref->block.dirty = true;
640 /* We don't need an extra block.*/
641 if (xattr_ref->block_loaded) {
642 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
643 block_header->h_refcount =
644 to_le32(to_le32(block_header->h_refcount) - 1);
645 if (!block_header->h_refcount) {
646 ext4_xattr_try_free_block(xattr_ref);
650 EXT4_XATTR_BFIRST(&xattr_ref->block);
652 (char *)block_header + block_size_rem;
654 sizeof(struct ext4_xattr_header);
655 ext4_inode_set_file_acl(
656 xattr_ref->inode_ref->inode,
657 &xattr_ref->fs->sb, 0);
659 xattr_ref->inode_ref->dirty = true;
660 xattr_ref->block.dirty = true;
664 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
666 if (EXT4_XATTR_SIZE(item->data_size) +
667 EXT4_XATTR_LEN(item->name_len) <=
669 ibody_data = (char *)ibody_data -
670 EXT4_XATTR_SIZE(item->data_size);
671 ext4_xattr_set_inode_entry(item, ibody_header, entry,
673 memcpy(EXT4_XATTR_NAME(entry), item->name,
675 memcpy(ibody_data, item->data, item->data_size);
676 entry = EXT4_XATTR_NEXT(entry);
677 inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
678 EXT4_XATTR_LEN(item->name_len);
680 xattr_ref->inode_ref->dirty = true;
683 if (EXT4_XATTR_SIZE(item->data_size) +
684 EXT4_XATTR_LEN(item->name_len) >
690 (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
691 ext4_xattr_set_block_entry(item, block_header, block_entry,
693 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
695 memcpy(block_data, item->data, item->data_size);
696 block_entry = EXT4_XATTR_NEXT(block_entry);
697 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
698 EXT4_XATTR_LEN(item->name_len);
700 block_modified = true;
702 xattr_ref->dirty = false;
703 if (block_modified) {
704 ext4_xattr_rehash(block_header,
705 EXT4_XATTR_BFIRST(&xattr_ref->block));
706 ext4_xattr_set_block_checksum(xattr_ref->inode_ref,
707 xattr_ref->block.lb_id,
709 xattr_ref->block.dirty = true;
716 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
717 int (*iter)(struct ext4_xattr_ref *ref,
718 struct ext4_xattr_item *item))
720 struct ext4_xattr_item *item;
722 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
724 RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
726 int ret = EXT4_XATTR_ITERATE_CONT;
730 if (ret != EXT4_XATTR_ITERATE_CONT) {
731 if (ret == EXT4_XATTR_ITERATE_STOP)
732 ref->iter_from = NULL;
739 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
741 ref->iter_from = NULL;
744 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
745 const char *name, size_t name_len, const void *data,
746 size_t data_size, bool replace)
749 struct ext4_xattr_item *item =
750 ext4_xattr_lookup_item(ref, name_index, name, name_len);
756 if (item->data_size != data_size)
757 ret = ext4_xattr_resize_item(ref, item, data_size);
762 memcpy(item->data, data, data_size);
768 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
777 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
778 const char *name, size_t name_len)
780 return ext4_xattr_remove_item(ref, name_index, name, name_len);
783 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
784 const char *name, size_t name_len, void *buf, size_t buf_size,
788 size_t item_size = 0;
789 struct ext4_xattr_item *item =
790 ext4_xattr_lookup_item(ref, name_index, name, name_len);
796 item_size = item->data_size;
797 if (buf_size > item_size)
798 buf_size = item_size;
801 memcpy(buf, item->data, buf_size);
805 *data_size = item_size;
810 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
811 struct ext4_xattr_ref *ref)
814 ext4_fsblk_t xattr_block;
815 xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
818 ref->iter_from = NULL;
820 rc = ext4_block_get(fs->bdev, &ref->block, xattr_block);
824 ref->block_loaded = true;
826 ref->block_loaded = false;
828 ref->inode_ref = inode_ref;
831 rc = ext4_xattr_fetch(ref);
833 ext4_xattr_purge_items(ref);
835 ext4_block_set(fs->bdev, &inode_ref->block);
837 ref->block_loaded = false;
843 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
845 ext4_xattr_write_to_disk(ref);
846 if (ref->block_loaded) {
847 ext4_block_set(ref->fs->bdev, &ref->block);
848 ref->block_loaded = false;
850 ext4_xattr_purge_items(ref);
851 ref->inode_ref = NULL;
855 struct xattr_prefix {
860 static const struct xattr_prefix prefix_tbl[] = {
861 {"user.", EXT4_XATTR_INDEX_USER},
862 {"system.", EXT4_XATTR_INDEX_SYSTEM},
863 {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
864 {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
868 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
869 uint8_t *name_index, size_t *name_len)
872 ext4_assert(name_index);
873 if (!full_name_len) {
880 for (i = 0; prefix_tbl[i].prefix; i++) {
881 size_t prefix_len = strlen(prefix_tbl[i].prefix);
882 if (full_name_len >= prefix_len &&
883 !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
884 *name_index = prefix_tbl[i].name_index;
886 *name_len = full_name_len - prefix_len;
888 return full_name + prefix_len;
897 const char *ext4_get_xattr_name_prefix(uint8_t name_index, size_t *ret_prefix_len)
901 for (i = 0; prefix_tbl[i].prefix; i++) {
902 size_t prefix_len = strlen(prefix_tbl[i].prefix);
903 if (prefix_tbl[i].name_index == name_index) {
905 *ret_prefix_len = prefix_len;
907 return prefix_tbl[i].prefix;