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->is_data && !b->is_data)
173 if (!a->is_data && b->is_data)
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;
203 item->in_inode = false;
205 memset(&item->node, 0, sizeof(item->node));
206 memcpy(item->name, name, name_len);
208 if (name_index == EXT4_XATTR_INDEX_SYSTEM &&
210 !memcmp(name, "data", 4))
211 item->is_data = true;
213 item->is_data = false;
218 static int ext4_xattr_item_alloc_data(struct ext4_xattr_item *item,
219 const void *orig_data, size_t data_size)
222 ext4_assert(!item->data);
223 data = malloc(data_size);
228 memcpy(data, orig_data, data_size);
231 item->data_size = data_size;
235 static void ext4_xattr_item_free_data(struct ext4_xattr_item *item)
237 ext4_assert(item->data);
243 static int ext4_xattr_item_resize_data(struct ext4_xattr_item *item,
244 size_t new_data_size)
246 if (new_data_size != item->data_size) {
248 new_data = realloc(item->data, new_data_size);
252 item->data = new_data;
253 item->data_size = new_data_size;
258 static void ext4_xattr_item_free(struct ext4_xattr_item *item)
261 ext4_xattr_item_free_data(item);
266 static void *ext4_xattr_entry_data(struct ext4_xattr_ref *xattr_ref,
267 struct ext4_xattr_entry *entry,
272 struct ext4_xattr_ibody_header *header;
273 struct ext4_xattr_entry *first_entry;
275 ext4_get16(&xattr_ref->fs->sb, inode_size);
276 header = EXT4_XATTR_IHDR(&xattr_ref->fs->sb,
277 xattr_ref->inode_ref->inode);
278 first_entry = EXT4_XATTR_IFIRST(header);
280 ret = ((char *)first_entry + to_le16(entry->e_value_offs));
281 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
282 (char *)xattr_ref->inode_ref->inode > inode_size)
288 int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
289 ret = ((char *)xattr_ref->block.data + to_le16(entry->e_value_offs));
290 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
291 (char *)xattr_ref->block.data > block_size)
296 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
301 struct ext4_xattr_entry *entry = NULL;
303 ext4_assert(xattr_ref->block.data);
304 entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
306 size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
307 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
308 entry = EXT4_XATTR_NEXT(entry),
309 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
310 struct ext4_xattr_item *item;
311 char *e_name = EXT4_XATTR_NAME(entry);
313 data = ext4_xattr_entry_data(xattr_ref, entry, false);
319 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
320 (size_t)entry->e_name_len);
325 if (ext4_xattr_item_alloc_data(
326 item, data, to_le32(entry->e_value_size)) != EOK) {
327 ext4_xattr_item_free(item);
331 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
332 xattr_ref->block_size_rem -=
333 EXT4_XATTR_SIZE(item->data_size) +
334 EXT4_XATTR_LEN(item->name_len);
335 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
336 EXT4_XATTR_LEN(item->name_len);
343 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
348 struct ext4_xattr_ibody_header *header = NULL;
349 struct ext4_xattr_entry *entry = NULL;
350 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
351 uint16_t extra_isize = ext4_inode_get_extra_isize(&xattr_ref->fs->sb,
352 xattr_ref->inode_ref->inode);
354 header = EXT4_XATTR_IHDR(&xattr_ref->fs->sb,
355 xattr_ref->inode_ref->inode);
356 entry = EXT4_XATTR_IFIRST(header);
358 size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
360 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
361 entry = EXT4_XATTR_NEXT(entry),
362 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
363 struct ext4_xattr_item *item;
364 char *e_name = EXT4_XATTR_NAME(entry);
366 data = ext4_xattr_entry_data(xattr_ref, entry, true);
372 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
373 (size_t)entry->e_name_len);
378 if (ext4_xattr_item_alloc_data(
379 item, data, to_le32(entry->e_value_size)) != EOK) {
380 ext4_xattr_item_free(item);
384 item->in_inode = true;
385 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
386 xattr_ref->inode_size_rem -=
387 EXT4_XATTR_SIZE(item->data_size) +
388 EXT4_XATTR_LEN(item->name_len);
389 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
390 EXT4_XATTR_LEN(item->name_len);
397 static size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
399 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
400 uint16_t extra_isize = ext4_inode_get_extra_isize(&xattr_ref->fs->sb,
401 xattr_ref->inode_ref->inode);
402 uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
407 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
409 return ext4_sb_get_block_size(&xattr_ref->fs->sb);
412 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
415 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
416 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
417 ret = ext4_xattr_inode_fetch(xattr_ref);
422 if (xattr_ref->block_loaded)
423 ret = ext4_xattr_block_fetch(xattr_ref);
425 xattr_ref->dirty = false;
429 static struct ext4_xattr_item *
430 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
431 const char *name, size_t name_len)
433 struct ext4_xattr_item tmp = {
434 .name_index = name_index,
435 .name = (char *)name, /*RB_FIND - won't touch this string*/
436 .name_len = name_len,
438 if (name_index == EXT4_XATTR_INDEX_SYSTEM &&
440 !memcmp(name, "data", 4))
443 return RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
446 static struct ext4_xattr_item *
447 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
448 const char *name, size_t name_len, const void *data,
452 struct ext4_xattr_item *item;
453 item = ext4_xattr_item_alloc(name_index, name, name_len);
461 item->in_inode = true;
462 if (xattr_ref->inode_size_rem <
463 EXT4_XATTR_SIZE(data_size) +
464 EXT4_XATTR_LEN(item->name_len)) {
465 if (xattr_ref->block_size_rem <
466 EXT4_XATTR_SIZE(data_size) +
467 EXT4_XATTR_LEN(item->name_len)) {
474 item->in_inode = false;
476 if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
477 ext4_xattr_item_free(item);
483 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
484 xattr_ref->ea_size +=
485 EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
486 if (item->in_inode) {
487 xattr_ref->inode_size_rem -=
488 EXT4_XATTR_SIZE(item->data_size) +
489 EXT4_XATTR_LEN(item->name_len);
491 xattr_ref->block_size_rem -=
492 EXT4_XATTR_SIZE(item->data_size) +
493 EXT4_XATTR_LEN(item->name_len);
495 xattr_ref->dirty = true;
502 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
503 uint8_t name_index, const char *name,
507 struct ext4_xattr_item *item =
508 ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
510 if (item == xattr_ref->iter_from)
511 xattr_ref->iter_from =
512 RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
514 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
515 EXT4_XATTR_LEN(item->name_len);
517 if (item->in_inode) {
518 xattr_ref->inode_size_rem +=
519 EXT4_XATTR_SIZE(item->data_size) +
520 EXT4_XATTR_LEN(item->name_len);
522 xattr_ref->block_size_rem +=
523 EXT4_XATTR_SIZE(item->data_size) +
524 EXT4_XATTR_LEN(item->name_len);
527 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
528 ext4_xattr_item_free(item);
529 xattr_ref->dirty = true;
535 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
536 struct ext4_xattr_item *item,
537 size_t new_data_size)
540 bool to_inode = false, to_block = false;
541 size_t old_data_size = item->data_size;
542 size_t orig_room_size = item->in_inode ?
543 xattr_ref->inode_size_rem :
544 xattr_ref->block_size_rem;
547 * Check if we can hold this entry in both in-inode and
550 * More complicated case: we do not allow entries stucking in
551 * the middle between in-inode space and on-block space, so
552 * the entry has to stay in either inode space or block space.
554 if (item->in_inode) {
555 if (xattr_ref->inode_size_rem +
556 EXT4_XATTR_SIZE(old_data_size) <
557 EXT4_XATTR_SIZE(new_data_size)) {
558 if (xattr_ref->block_size_rem <
559 EXT4_XATTR_SIZE(new_data_size) +
560 EXT4_XATTR_LEN(item->name_len))
566 if (xattr_ref->block_size_rem +
567 EXT4_XATTR_SIZE(old_data_size) <
568 EXT4_XATTR_SIZE(new_data_size)) {
569 if (xattr_ref->inode_size_rem <
570 EXT4_XATTR_SIZE(new_data_size) +
571 EXT4_XATTR_LEN(item->name_len))
577 ret = ext4_xattr_item_resize_data(item, new_data_size);
583 EXT4_XATTR_SIZE(old_data_size) +
584 EXT4_XATTR_SIZE(new_data_size);
587 * This entry may originally lie in inode space or block space,
588 * and it is going to be transferred to another place.
591 xattr_ref->inode_size_rem +=
592 EXT4_XATTR_SIZE(old_data_size) +
593 EXT4_XATTR_LEN(item->name_len);
594 xattr_ref->block_size_rem -=
595 EXT4_XATTR_SIZE(new_data_size) +
596 EXT4_XATTR_LEN(item->name_len);
597 item->in_inode = false;
598 } else if (to_inode) {
599 xattr_ref->block_size_rem +=
600 EXT4_XATTR_SIZE(old_data_size) +
601 EXT4_XATTR_LEN(item->name_len);
602 xattr_ref->inode_size_rem -=
603 EXT4_XATTR_SIZE(new_data_size) +
604 EXT4_XATTR_LEN(item->name_len);
605 item->in_inode = true;
608 * No need to transfer as there is enough space for the entry
609 * to stay in inode space or block space it used to be.
612 EXT4_XATTR_SIZE(old_data_size);
614 EXT4_XATTR_SIZE(new_data_size);
616 xattr_ref->inode_size_rem = orig_room_size;
618 xattr_ref->block_size_rem = orig_room_size;
621 xattr_ref->dirty = true;
625 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
627 struct ext4_xattr_item *item, *save_item;
628 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item) {
629 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
630 ext4_xattr_item_free(item);
632 xattr_ref->ea_size = 0;
633 if (ext4_xattr_inode_space(xattr_ref) <
634 sizeof(struct ext4_xattr_ibody_header))
635 xattr_ref->inode_size_rem = 0;
637 xattr_ref->inode_size_rem =
638 ext4_xattr_inode_space(xattr_ref) -
639 sizeof(struct ext4_xattr_ibody_header);
641 xattr_ref->block_size_rem =
642 ext4_xattr_block_space(xattr_ref) -
643 sizeof(struct ext4_xattr_header);
646 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
650 ext4_fsblk_t xattr_block = 0;
651 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
655 ext4_fs_inode_to_goal_block(xattr_ref->inode_ref);
657 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
663 ret = ext4_trans_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
666 ext4_balloc_free_block(xattr_ref->inode_ref,
671 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
672 &xattr_ref->fs->sb, xattr_block);
673 xattr_ref->inode_ref->dirty = true;
674 xattr_ref->block_loaded = true;
681 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
683 ext4_fsblk_t xattr_block;
684 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
686 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
688 ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
689 ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
690 xattr_ref->inode_ref->dirty = true;
691 xattr_ref->block_loaded = false;
694 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
696 struct ext4_xattr_header *block_header = NULL;
697 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
699 memset(block_header, 0, sizeof(struct ext4_xattr_header));
700 block_header->h_magic = EXT4_XATTR_MAGIC;
701 block_header->h_refcount = to_le32(1);
702 block_header->h_blocks = to_le32(1);
706 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
707 struct ext4_xattr_ibody_header *ibody_header,
708 struct ext4_xattr_entry *entry, void *ibody_data_ptr)
710 entry->e_name_len = (uint8_t)item->name_len;
711 entry->e_name_index = item->name_index;
712 entry->e_value_offs =
713 to_le16((char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header));
714 entry->e_value_block = 0;
715 entry->e_value_size = to_le32(item->data_size);
718 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
719 struct ext4_xattr_header *block_header,
720 struct ext4_xattr_entry *block_entry,
721 void *block_data_ptr)
723 block_entry->e_name_len = (uint8_t)item->name_len;
724 block_entry->e_name_index = item->name_index;
725 block_entry->e_value_offs =
726 to_le16((char *)block_data_ptr - (char *)block_header);
727 block_entry->e_value_block = 0;
728 block_entry->e_value_size = to_le32(item->data_size);
731 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
734 bool block_modified = false;
735 void *ibody_data = NULL;
736 void *block_data = NULL;
737 struct ext4_xattr_item *item, *save_item;
738 size_t inode_size_rem, block_size_rem;
739 struct ext4_xattr_ibody_header *ibody_header = NULL;
740 struct ext4_xattr_header *block_header = NULL;
741 struct ext4_xattr_entry *entry = NULL;
742 struct ext4_xattr_entry *block_entry = NULL;
744 inode_size_rem = ext4_xattr_inode_space(xattr_ref);
745 block_size_rem = ext4_xattr_block_space(xattr_ref);
746 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
747 ibody_header = EXT4_XATTR_IHDR(&xattr_ref->fs->sb,
748 xattr_ref->inode_ref->inode);
749 entry = EXT4_XATTR_IFIRST(ibody_header);
752 if (!xattr_ref->dirty)
754 /* If there are enough spaces in the ibody EA table.*/
755 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
756 memset(ibody_header, 0, inode_size_rem);
757 ibody_header->h_magic = EXT4_XATTR_MAGIC;
758 ibody_data = (char *)ibody_header + inode_size_rem;
759 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
761 xattr_ref->inode_ref->dirty = true;
763 /* If we need an extra block to hold the EA entries*/
764 if (xattr_ref->ea_size > inode_size_rem) {
765 if (!xattr_ref->block_loaded) {
766 ret = ext4_xattr_try_alloc_block(xattr_ref);
770 memset(xattr_ref->block.data, 0,
771 ext4_sb_get_block_size(&xattr_ref->fs->sb));
772 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
773 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
774 ext4_xattr_set_block_header(xattr_ref);
775 block_data = (char *)block_header + block_size_rem;
776 block_size_rem -= sizeof(struct ext4_xattr_header);
778 ext4_trans_set_block_dirty(xattr_ref->block.buf);
780 /* We don't need an extra block.*/
781 if (xattr_ref->block_loaded) {
782 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
783 block_header->h_refcount =
784 to_le32(to_le32(block_header->h_refcount) - 1);
785 if (!block_header->h_refcount) {
786 ext4_xattr_try_free_block(xattr_ref);
790 EXT4_XATTR_BFIRST(&xattr_ref->block);
792 (char *)block_header + block_size_rem;
794 sizeof(struct ext4_xattr_header);
795 ext4_inode_set_file_acl(
796 xattr_ref->inode_ref->inode,
797 &xattr_ref->fs->sb, 0);
799 xattr_ref->inode_ref->dirty = true;
800 ext4_trans_set_block_dirty(xattr_ref->block.buf);
804 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
806 if (item->in_inode) {
807 ibody_data = (char *)ibody_data -
808 EXT4_XATTR_SIZE(item->data_size);
809 ext4_xattr_set_inode_entry(item, ibody_header, entry,
811 memcpy(EXT4_XATTR_NAME(entry), item->name,
813 memcpy(ibody_data, item->data, item->data_size);
814 entry = EXT4_XATTR_NEXT(entry);
815 inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
816 EXT4_XATTR_LEN(item->name_len);
818 xattr_ref->inode_ref->dirty = true;
821 if (EXT4_XATTR_SIZE(item->data_size) +
822 EXT4_XATTR_LEN(item->name_len) >
825 ext4_dbg(DEBUG_XATTR, "IMPOSSIBLE ENOSPC AS WE DID INSPECTION!\n");
829 (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
830 ext4_xattr_set_block_entry(item, block_header, block_entry,
832 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
834 memcpy(block_data, item->data, item->data_size);
835 ext4_xattr_compute_hash(block_header, block_entry);
836 block_entry = EXT4_XATTR_NEXT(block_entry);
837 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
838 EXT4_XATTR_LEN(item->name_len);
840 block_modified = true;
842 xattr_ref->dirty = false;
843 if (block_modified) {
844 ext4_xattr_rehash(block_header,
845 EXT4_XATTR_BFIRST(&xattr_ref->block));
846 ext4_xattr_set_block_checksum(xattr_ref->inode_ref,
847 xattr_ref->block.lb_id,
849 ext4_trans_set_block_dirty(xattr_ref->block.buf);
856 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
857 int (*iter)(struct ext4_xattr_ref *ref,
858 struct ext4_xattr_item *item))
860 struct ext4_xattr_item *item;
862 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
864 RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
866 int ret = EXT4_XATTR_ITERATE_CONT;
870 if (ret != EXT4_XATTR_ITERATE_CONT) {
871 if (ret == EXT4_XATTR_ITERATE_STOP)
872 ref->iter_from = NULL;
879 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
881 ref->iter_from = NULL;
884 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
885 const char *name, size_t name_len, const void *data,
886 size_t data_size, bool replace)
889 struct ext4_xattr_item *item =
890 ext4_xattr_lookup_item(ref, name_index, name, name_len);
896 if (item->data_size != data_size)
897 ret = ext4_xattr_resize_item(ref, item, data_size);
902 memcpy(item->data, data, data_size);
908 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
909 data, data_size, &ret);
915 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
916 const char *name, size_t name_len)
918 return ext4_xattr_remove_item(ref, name_index, name, name_len);
921 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
922 const char *name, size_t name_len, void *buf,
923 size_t buf_size, size_t *data_size)
926 size_t item_size = 0;
927 struct ext4_xattr_item *item =
928 ext4_xattr_lookup_item(ref, name_index, name, name_len);
934 item_size = item->data_size;
935 if (buf_size > item_size)
936 buf_size = item_size;
939 memcpy(buf, item->data, buf_size);
943 *data_size = item_size;
948 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
949 struct ext4_xattr_ref *ref)
952 ext4_fsblk_t xattr_block;
953 xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
956 ref->iter_from = NULL;
958 rc = ext4_trans_block_get(fs->bdev, &ref->block, xattr_block);
962 ref->block_loaded = true;
964 ref->block_loaded = false;
966 ref->inode_ref = inode_ref;
969 if (ext4_xattr_inode_space(ref) <
970 sizeof(struct ext4_xattr_ibody_header))
971 ref->inode_size_rem = 0;
973 ref->inode_size_rem =
974 ext4_xattr_inode_space(ref) -
975 sizeof(struct ext4_xattr_ibody_header);
977 ref->block_size_rem =
978 ext4_xattr_block_space(ref) -
979 sizeof(struct ext4_xattr_header);
981 rc = ext4_xattr_fetch(ref);
983 ext4_xattr_purge_items(ref);
985 ext4_block_set(fs->bdev, &ref->block);
987 ref->block_loaded = false;
993 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
995 int rc = ext4_xattr_write_to_disk(ref);
996 if (ref->block_loaded) {
998 ext4_bcache_clear_dirty(ref->block.buf);
1000 ext4_block_set(ref->fs->bdev, &ref->block);
1001 ref->block_loaded = false;
1003 ext4_xattr_purge_items(ref);
1004 ref->inode_ref = NULL;
1008 struct xattr_prefix {
1013 static const struct xattr_prefix prefix_tbl[] = {
1014 {"user.", EXT4_XATTR_INDEX_USER},
1015 {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
1016 {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
1017 {"trusted.", EXT4_XATTR_INDEX_TRUSTED},
1018 {"security.", EXT4_XATTR_INDEX_SECURITY},
1019 {"system.", EXT4_XATTR_INDEX_SYSTEM},
1020 {"system.richacl", EXT4_XATTR_INDEX_RICHACL},
1024 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
1025 uint8_t *name_index, size_t *name_len,
1029 ext4_assert(name_index);
1034 if (!full_name_len) {
1041 for (i = 0; prefix_tbl[i].prefix; i++) {
1042 size_t prefix_len = strlen(prefix_tbl[i].prefix);
1043 if (full_name_len >= prefix_len &&
1044 !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
1046 prefix_tbl[i].prefix[prefix_len - 1] == '.';
1047 *name_index = prefix_tbl[i].name_index;
1049 *name_len = full_name_len - prefix_len;
1051 if (!(full_name_len - prefix_len) && require_name)
1056 return full_name + prefix_len;
1067 const char *ext4_get_xattr_name_prefix(uint8_t name_index,
1068 size_t *ret_prefix_len)
1072 for (i = 0; prefix_tbl[i].prefix; i++) {
1073 size_t prefix_len = strlen(prefix_tbl[i].prefix);
1074 if (prefix_tbl[i].name_index == name_index) {
1076 *ret_prefix_len = prefix_len;
1078 return prefix_tbl[i].prefix;
1082 *ret_prefix_len = 0;