2 * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3 * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com)
7 * Copyright (c) 2012 Martin Sucha
8 * Copyright (c) 2012 Frantisek Princ
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * - Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * - The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /** @addtogroup lwext4
40 * @brief Extended Attribute manipulation.
43 #include "ext4_config.h"
44 #include "ext4_types.h"
46 #include "ext4_errno.h"
47 #include "ext4_blockdev.h"
48 #include "ext4_super.h"
49 #include "ext4_debug.h"
50 #include "ext4_block_group.h"
51 #include "ext4_balloc.h"
52 #include "ext4_inode.h"
53 #include "ext4_extent.h"
60 * @brief Extended Attribute Manipulation
63 #define NAME_HASH_SHIFT 5
64 #define VALUE_HASH_SHIFT 16
66 static inline void ext4_xattr_compute_hash(struct ext4_xattr_header *header,
67 struct ext4_xattr_entry *entry)
70 char *name = EXT4_XATTR_NAME(entry);
73 for (n = 0; n < entry->e_name_len; n++) {
74 hash = (hash << NAME_HASH_SHIFT) ^
75 (hash >> (8 * sizeof(hash) - NAME_HASH_SHIFT)) ^ *name++;
78 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
80 (uint32_t *)((char *)header + to_le16(entry->e_value_offs));
81 for (n = (to_le32(entry->e_value_size) + EXT4_XATTR_ROUND) >>
84 hash = (hash << VALUE_HASH_SHIFT) ^
85 (hash >> (8 * sizeof(hash) - VALUE_HASH_SHIFT)) ^
89 entry->e_hash = to_le32(hash);
92 #define BLOCK_HASH_SHIFT 16
97 * Re-compute the extended attribute hash value after an entry has changed.
99 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
100 struct ext4_xattr_entry *entry)
102 struct ext4_xattr_entry *here;
105 ext4_xattr_compute_hash(header, entry);
106 here = EXT4_XATTR_ENTRY(header + 1);
107 while (!EXT4_XATTR_IS_LAST_ENTRY(here)) {
109 /* Block is not shared if an entry's hash value == 0 */
113 hash = (hash << BLOCK_HASH_SHIFT) ^
114 (hash >> (8 * sizeof(hash) - BLOCK_HASH_SHIFT)) ^
115 to_le32(here->e_hash);
116 here = EXT4_XATTR_NEXT(here);
118 header->h_hash = to_le32(hash);
121 static int ext4_xattr_item_cmp(struct ext4_xattr_item *a,
122 struct ext4_xattr_item *b)
125 result = a->name_index - b->name_index;
129 result = a->name_len - b->name_len;
133 return memcmp(a->name, b->name, a->name_len);
136 RB_GENERATE_INTERNAL(ext4_xattr_tree, ext4_xattr_item, node,
137 ext4_xattr_item_cmp, static inline)
139 static struct ext4_xattr_item *
140 ext4_xattr_item_alloc(uint8_t name_index, char *name, size_t name_len)
142 struct ext4_xattr_item *item;
143 item = malloc(sizeof(struct ext4_xattr_item) + name_len);
147 item->name_index = name_index;
148 item->name = (char *)(item + 1);
149 item->name_len = name_len;
153 memset(&item->node, 0, sizeof(item->node));
154 memcpy(item->name, name, name_len);
159 static int ext4_xattr_item_alloc_data(struct ext4_xattr_item *item,
160 void *orig_data, size_t data_size)
163 ext4_assert(!item->data);
164 data = malloc(data_size);
169 memcpy(data, orig_data, data_size);
172 item->data_size = data_size;
176 static void ext4_xattr_item_free_data(struct ext4_xattr_item *item)
178 ext4_assert(item->data);
184 static int ext4_xattr_item_resize_data(struct ext4_xattr_item *item,
185 size_t new_data_size)
187 if (new_data_size != item->data_size) {
189 new_data = realloc(item->data, new_data_size);
193 item->data = new_data;
194 item->data_size = new_data_size;
199 static void ext4_xattr_item_free(struct ext4_xattr_item *item)
202 ext4_xattr_item_free_data(item);
207 static void *ext4_xattr_entry_data(struct ext4_xattr_ref *xattr_ref,
208 struct ext4_xattr_entry *entry,
213 struct ext4_xattr_ibody_header *header;
214 struct ext4_xattr_entry *first_entry;
215 uint16_t inode_size =
216 ext4_get16(&xattr_ref->fs->sb, inode_size);
217 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
218 first_entry = EXT4_XATTR_IFIRST(header);
220 ret = (void *)((char *)first_entry +
221 to_le16(entry->e_value_offs));
223 EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
224 (char *)xattr_ref->inode_ref->inode >
229 int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
230 ret = (void *)((char *)xattr_ref->block.data +
231 to_le16(entry->e_value_offs));
233 EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
234 (char *)xattr_ref->block.data >
241 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
246 struct ext4_xattr_entry *entry = NULL;
248 ext4_assert(xattr_ref->block.data);
249 entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
251 size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
252 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
253 entry = EXT4_XATTR_NEXT(entry),
254 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
255 struct ext4_xattr_item *item;
256 char *e_name = EXT4_XATTR_NAME(entry);
258 data = ext4_xattr_entry_data(xattr_ref, entry, false);
264 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
265 (size_t)entry->e_name_len);
270 if (ext4_xattr_item_alloc_data(
271 item, data, to_le32(entry->e_value_size)) != EOK) {
272 ext4_xattr_item_free(item);
276 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
277 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
278 EXT4_XATTR_LEN(item->name_len);
285 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
290 struct ext4_xattr_ibody_header *header = NULL;
291 struct ext4_xattr_entry *entry = NULL;
292 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
294 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
295 entry = EXT4_XATTR_IFIRST(header);
297 size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
298 xattr_ref->inode_ref->inode->extra_isize;
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, true);
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 size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
334 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
335 uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
336 xattr_ref->inode_ref->inode->extra_isize;
340 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
342 return ext4_sb_get_block_size(&xattr_ref->fs->sb);
345 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
348 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
349 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
350 ret = ext4_xattr_inode_fetch(xattr_ref);
355 if (xattr_ref->block_loaded)
356 ret = ext4_xattr_block_fetch(xattr_ref);
358 xattr_ref->dirty = false;
362 static struct ext4_xattr_item *
363 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
364 char *name, size_t name_len)
366 struct ext4_xattr_item tmp, *ret;
367 tmp.name_index = name_index;
369 tmp.name_len = name_len;
370 ret = RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
374 static struct ext4_xattr_item *
375 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
376 char *name, size_t name_len, void *data,
379 struct ext4_xattr_item *item;
380 item = ext4_xattr_item_alloc(name_index, name, name_len);
384 if (xattr_ref->ea_size + EXT4_XATTR_SIZE(item->data_size) +
385 EXT4_XATTR_LEN(item->name_len) >
386 ext4_xattr_inode_space(xattr_ref) +
387 ext4_xattr_block_space(xattr_ref)) {
388 ext4_xattr_item_free(item);
391 if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
392 ext4_xattr_item_free(item);
395 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
396 xattr_ref->ea_size +=
397 EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
398 xattr_ref->dirty = true;
402 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
403 uint8_t name_index, char *name,
407 struct ext4_xattr_item *item =
408 ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
410 if (item == xattr_ref->iter_from)
411 xattr_ref->iter_from =
412 RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
414 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
415 ext4_xattr_item_free(item);
416 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
417 EXT4_XATTR_LEN(item->name_len);
418 xattr_ref->dirty = true;
424 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
425 struct ext4_xattr_item *item,
426 size_t new_data_size)
429 if (xattr_ref->ea_size - EXT4_XATTR_SIZE(item->data_size) +
430 EXT4_XATTR_SIZE(new_data_size) >
431 ext4_xattr_inode_space(xattr_ref) +
432 ext4_xattr_block_space(xattr_ref)) {
436 ret = ext4_xattr_item_resize_data(item, new_data_size);
440 xattr_ref->ea_size -=
441 EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_SIZE(new_data_size);
442 xattr_ref->dirty = true;
446 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
448 struct ext4_xattr_item *item, *save_item;
449 uint64_t xattr_block = ext4_inode_get_file_acl(
450 xattr_ref->inode_ref->inode, &xattr_ref->fs->sb);
451 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
453 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
454 ext4_xattr_item_free(item);
456 xattr_ref->ea_size = 0;
458 xattr_ref->ea_size += sizeof(struct ext4_xattr_header);
460 if (ext4_xattr_inode_space(xattr_ref) >
461 sizeof(struct ext4_xattr_ibody_header))
462 xattr_ref->ea_size += sizeof(struct ext4_xattr_ibody_header);
465 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
469 uint64_t xattr_block = 0;
470 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
473 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
474 (uint32_t *)&xattr_block);
478 ret = ext4_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
481 ext4_balloc_free_block(xattr_ref->inode_ref,
486 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
487 &xattr_ref->fs->sb, xattr_block);
488 xattr_ref->inode_ref->dirty = true;
489 xattr_ref->block_loaded = true;
490 xattr_ref->ea_size += sizeof(struct ext4_xattr_header);
497 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
499 uint64_t xattr_block;
500 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
502 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
504 ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
505 ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
506 xattr_ref->inode_ref->dirty = true;
507 xattr_ref->block_loaded = false;
508 xattr_ref->ea_size -= sizeof(struct ext4_xattr_header);
511 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
513 struct ext4_xattr_header *block_header = NULL;
514 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
516 memset(block_header, 0, sizeof(struct ext4_xattr_header));
517 block_header->h_magic = EXT4_XATTR_MAGIC;
518 block_header->h_refcount = to_le32(1);
519 block_header->h_blocks = to_le32(1);
523 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
524 struct ext4_xattr_ibody_header *ibody_header,
525 struct ext4_xattr_entry *entry, void *ibody_data_ptr)
527 entry->e_name_len = to_le32(item->name_len);
528 entry->e_name_index = item->name_index;
529 entry->e_value_offs =
530 (char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header);
531 entry->e_value_block = 0;
532 entry->e_value_size = item->data_size;
535 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
536 struct ext4_xattr_header *block_header,
537 struct ext4_xattr_entry *block_entry,
538 void *block_data_ptr)
540 block_entry->e_name_len = to_le32(item->name_len);
541 block_entry->e_name_index = item->name_index;
542 block_entry->e_value_offs =
543 (char *)block_data_ptr - (char *)block_header;
544 block_entry->e_value_block = 0;
545 block_entry->e_value_size = item->data_size;
548 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
551 bool block_modified = false;
552 void *ibody_data, *block_data;
553 struct ext4_xattr_item *item, *save_item;
554 size_t inode_size_rem, block_size_rem;
555 struct ext4_xattr_ibody_header *ibody_header = NULL;
556 struct ext4_xattr_header *block_header = NULL;
557 struct ext4_xattr_entry *entry = NULL;
558 struct ext4_xattr_entry *block_entry = NULL;
560 inode_size_rem = ext4_xattr_inode_space(xattr_ref);
561 block_size_rem = ext4_xattr_block_space(xattr_ref);
562 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
563 ibody_header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
564 entry = EXT4_XATTR_IFIRST(ibody_header);
567 if (!xattr_ref->dirty)
569 /* If there are enough spaces in the ibody EA table.*/
570 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
571 memset(ibody_header, 0, inode_size_rem);
572 ibody_header->h_magic = EXT4_XATTR_MAGIC;
573 ibody_data = (char *)ibody_header + inode_size_rem;
574 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
576 xattr_ref->inode_ref->dirty = true;
578 /* If we need an extra block to hold the EA entries*/
579 if (xattr_ref->ea_size > inode_size_rem) {
580 if (!xattr_ref->block_loaded) {
581 ret = ext4_xattr_try_alloc_block(xattr_ref);
585 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
586 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
587 ext4_xattr_set_block_header(xattr_ref);
588 block_data = (char *)block_header + block_size_rem;
589 block_size_rem -= sizeof(struct ext4_xattr_header);
591 xattr_ref->block.dirty = true;
593 /* We don't need an extra block.*/
594 if (xattr_ref->block_loaded) {
595 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
596 block_header->h_refcount =
597 to_le32(to_le32(block_header->h_refcount) - 1);
598 if (!block_header->h_refcount) {
599 ext4_xattr_try_free_block(xattr_ref);
603 EXT4_XATTR_BFIRST(&xattr_ref->block);
605 (char *)block_header + block_size_rem;
607 sizeof(struct ext4_xattr_header);
608 ext4_inode_set_file_acl(
609 xattr_ref->inode_ref->inode,
610 &xattr_ref->fs->sb, 0);
612 xattr_ref->inode_ref->dirty = true;
613 xattr_ref->block.dirty = true;
617 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
619 if (EXT4_XATTR_SIZE(item->data_size) +
620 EXT4_XATTR_LEN(item->name_len) <=
622 ibody_data = (char *)ibody_data -
623 EXT4_XATTR_SIZE(item->data_size);
624 ext4_xattr_set_inode_entry(item, ibody_header, entry,
626 memcpy(EXT4_XATTR_NAME(entry), item->name,
628 memcpy(ibody_data, item->data, item->data_size);
629 entry = EXT4_XATTR_NEXT(entry);
630 inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
631 EXT4_XATTR_LEN(item->name_len);
633 xattr_ref->inode_ref->dirty = true;
636 if (EXT4_XATTR_SIZE(item->data_size) +
637 EXT4_XATTR_LEN(item->name_len) >
643 (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
644 ext4_xattr_set_block_entry(item, block_header, block_entry,
646 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
648 memcpy(block_data, item->data, item->data_size);
649 block_entry = EXT4_XATTR_NEXT(block_entry);
650 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
651 EXT4_XATTR_LEN(item->name_len);
653 block_modified = true;
655 xattr_ref->dirty = false;
656 if (block_modified) {
657 ext4_xattr_rehash(block_header,
658 EXT4_XATTR_BFIRST(&xattr_ref->block));
659 xattr_ref->block.dirty = true;
666 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
667 int(iter)(struct ext4_xattr_ref *ref,
668 struct ext4_xattr_item *item))
670 struct ext4_xattr_item *item;
672 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
674 RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
676 int ret = EXT4_XATTR_ITERATE_CONT;
680 if (ret != EXT4_XATTR_ITERATE_CONT) {
681 if (ret == EXT4_XATTR_ITERATE_STOP)
682 ref->iter_from = NULL;
689 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
691 ref->iter_from = NULL;
694 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
695 char *name, size_t name_len, void *data, size_t data_size,
699 struct ext4_xattr_item *item =
700 ext4_xattr_lookup_item(ref, name_index, name, name_len);
706 if (item->data_size != data_size)
707 ret = ext4_xattr_resize_item(ref, item, data_size);
712 memcpy(item->data, data, data_size);
718 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
727 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
728 char *name, size_t name_len)
730 return ext4_xattr_remove_item(ref, name_index, name, name_len);
733 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
734 char *name, size_t name_len, void *buf, size_t buf_size,
738 size_t item_size = 0;
739 struct ext4_xattr_item *item =
740 ext4_xattr_lookup_item(ref, name_index, name, name_len);
746 item_size = item->data_size;
747 if (buf_size > item_size)
748 buf_size = item_size;
751 memcpy(buf, item->data, buf_size);
755 *size_got = buf_size;
760 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
761 struct ext4_xattr_ref *ref)
764 uint64_t xattr_block;
765 xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
768 ref->iter_from = NULL;
770 rc = ext4_block_get(fs->bdev, &ref->block, xattr_block);
774 ref->ea_size += sizeof(struct ext4_xattr_header);
775 ref->block_loaded = true;
777 ref->block_loaded = false;
779 ref->inode_ref = inode_ref;
782 if (ext4_xattr_inode_space(ref) >
783 sizeof(struct ext4_xattr_ibody_header))
784 ref->ea_size += sizeof(struct ext4_xattr_ibody_header);
786 rc = ext4_xattr_fetch(ref);
788 ext4_xattr_purge_items(ref);
790 ext4_block_set(fs->bdev, &inode_ref->block);
792 ref->block_loaded = false;
798 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
800 ext4_xattr_write_to_disk(ref);
801 if (ref->block_loaded) {
802 ext4_block_set(ref->fs->bdev, &ref->block);
803 ref->block_loaded = false;
805 ext4_xattr_purge_items(ref);
806 ref->inode_ref = NULL;
810 struct xattr_prefix {
815 static const struct xattr_prefix prefix_tbl[] = {
816 {"user.", EXT4_XATTR_INDEX_USER},
817 {"system.", EXT4_XATTR_INDEX_SYSTEM},
818 {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
819 {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
823 char *ext4_extract_xattr_name(char *full_name, size_t full_name_len,
824 uint8_t *name_index, size_t *name_len)
827 ext4_assert(name_index);
831 for (i = 0; prefix_tbl[i].prefix; i++) {
832 size_t prefix_len = strlen(prefix_tbl[i].prefix);
833 if (full_name_len >= prefix_len &&
834 !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
835 *name_index = prefix_tbl[i].name_index;
837 *name_len = full_name_len - prefix_len;
839 return full_name + prefix_len;