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_debug.h"
44 #include "ext4_block_group.h"
45 #include "ext4_balloc.h"
46 #include "ext4_inode.h"
47 #include "ext4_extent.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 static int ext4_xattr_item_cmp(struct ext4_xattr_item *a,
116 struct ext4_xattr_item *b)
119 result = a->name_index - b->name_index;
123 result = a->name_len - b->name_len;
127 return memcmp(a->name, b->name, a->name_len);
130 RB_GENERATE_INTERNAL(ext4_xattr_tree, ext4_xattr_item, node,
131 ext4_xattr_item_cmp, static inline)
133 static struct ext4_xattr_item *
134 ext4_xattr_item_alloc(uint8_t name_index, char *name, size_t name_len)
136 struct ext4_xattr_item *item;
137 item = malloc(sizeof(struct ext4_xattr_item) + name_len);
141 item->name_index = name_index;
142 item->name = (char *)(item + 1);
143 item->name_len = name_len;
147 memset(&item->node, 0, sizeof(item->node));
148 memcpy(item->name, name, name_len);
153 static int ext4_xattr_item_alloc_data(struct ext4_xattr_item *item,
154 void *orig_data, size_t data_size)
157 ext4_assert(!item->data);
158 data = malloc(data_size);
163 memcpy(data, orig_data, data_size);
166 item->data_size = data_size;
170 static void ext4_xattr_item_free_data(struct ext4_xattr_item *item)
172 ext4_assert(item->data);
178 static int ext4_xattr_item_resize_data(struct ext4_xattr_item *item,
179 size_t new_data_size)
181 if (new_data_size != item->data_size) {
183 new_data = realloc(item->data, new_data_size);
187 item->data = new_data;
188 item->data_size = new_data_size;
193 static void ext4_xattr_item_free(struct ext4_xattr_item *item)
196 ext4_xattr_item_free_data(item);
201 static void *ext4_xattr_entry_data(struct ext4_xattr_ref *xattr_ref,
202 struct ext4_xattr_entry *entry,
207 struct ext4_xattr_ibody_header *header;
208 struct ext4_xattr_entry *first_entry;
210 ext4_get16(&xattr_ref->fs->sb, inode_size);
211 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
212 first_entry = EXT4_XATTR_IFIRST(header);
214 ret = (void *)((char *)first_entry +
215 to_le16(entry->e_value_offs));
217 EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
218 (char *)xattr_ref->inode_ref->inode >
223 int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
224 ret = (void *)((char *)xattr_ref->block.data +
225 to_le16(entry->e_value_offs));
227 EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
228 (char *)xattr_ref->block.data >
235 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
240 struct ext4_xattr_entry *entry = NULL;
242 ext4_assert(xattr_ref->block.data);
243 entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
245 size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
246 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
247 entry = EXT4_XATTR_NEXT(entry),
248 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
249 struct ext4_xattr_item *item;
250 char *e_name = EXT4_XATTR_NAME(entry);
252 data = ext4_xattr_entry_data(xattr_ref, entry, false);
258 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
259 (size_t)entry->e_name_len);
264 if (ext4_xattr_item_alloc_data(
265 item, data, to_le32(entry->e_value_size)) != EOK) {
266 ext4_xattr_item_free(item);
270 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
271 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
272 EXT4_XATTR_LEN(item->name_len);
279 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
284 struct ext4_xattr_ibody_header *header = NULL;
285 struct ext4_xattr_entry *entry = NULL;
286 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
288 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
289 entry = EXT4_XATTR_IFIRST(header);
291 size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
292 xattr_ref->inode_ref->inode->extra_isize;
293 for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
294 entry = EXT4_XATTR_NEXT(entry),
295 size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
296 struct ext4_xattr_item *item;
297 char *e_name = EXT4_XATTR_NAME(entry);
299 data = ext4_xattr_entry_data(xattr_ref, entry, true);
305 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
306 (size_t)entry->e_name_len);
311 if (ext4_xattr_item_alloc_data(
312 item, data, to_le32(entry->e_value_size)) != EOK) {
313 ext4_xattr_item_free(item);
317 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
318 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
319 EXT4_XATTR_LEN(item->name_len);
326 static size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
328 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
329 uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
330 xattr_ref->inode_ref->inode->extra_isize;
334 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
336 return ext4_sb_get_block_size(&xattr_ref->fs->sb);
339 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
342 uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
343 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
344 ret = ext4_xattr_inode_fetch(xattr_ref);
349 if (xattr_ref->block_loaded)
350 ret = ext4_xattr_block_fetch(xattr_ref);
352 xattr_ref->dirty = false;
356 static struct ext4_xattr_item *
357 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
358 char *name, size_t name_len)
360 struct ext4_xattr_item tmp, *ret;
361 tmp.name_index = name_index;
363 tmp.name_len = name_len;
364 ret = RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
368 static struct ext4_xattr_item *
369 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
370 char *name, size_t name_len, void *data,
373 struct ext4_xattr_item *item;
374 item = ext4_xattr_item_alloc(name_index, name, name_len);
378 if (xattr_ref->ea_size + EXT4_XATTR_SIZE(item->data_size) +
379 EXT4_XATTR_LEN(item->name_len) >
380 ext4_xattr_inode_space(xattr_ref) +
381 ext4_xattr_block_space(xattr_ref)) {
382 ext4_xattr_item_free(item);
385 if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
386 ext4_xattr_item_free(item);
389 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
390 xattr_ref->ea_size +=
391 EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
392 xattr_ref->dirty = true;
396 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
397 uint8_t name_index, char *name,
401 struct ext4_xattr_item *item =
402 ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
404 if (item == xattr_ref->iter_from)
405 xattr_ref->iter_from =
406 RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
408 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
409 ext4_xattr_item_free(item);
410 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
411 EXT4_XATTR_LEN(item->name_len);
412 xattr_ref->dirty = true;
418 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
419 struct ext4_xattr_item *item,
420 size_t new_data_size)
423 if (xattr_ref->ea_size - EXT4_XATTR_SIZE(item->data_size) +
424 EXT4_XATTR_SIZE(new_data_size) >
425 ext4_xattr_inode_space(xattr_ref) +
426 ext4_xattr_block_space(xattr_ref)) {
430 ret = ext4_xattr_item_resize_data(item, new_data_size);
434 xattr_ref->ea_size -=
435 EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_SIZE(new_data_size);
436 xattr_ref->dirty = true;
440 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
442 struct ext4_xattr_item *item, *save_item;
443 uint64_t xattr_block = ext4_inode_get_file_acl(
444 xattr_ref->inode_ref->inode, &xattr_ref->fs->sb);
445 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
447 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
448 ext4_xattr_item_free(item);
450 xattr_ref->ea_size = 0;
452 xattr_ref->ea_size += sizeof(struct ext4_xattr_header);
454 if (ext4_xattr_inode_space(xattr_ref) >
455 sizeof(struct ext4_xattr_ibody_header))
456 xattr_ref->ea_size += sizeof(struct ext4_xattr_ibody_header);
459 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
463 uint64_t xattr_block = 0;
464 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
467 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
468 (uint32_t *)&xattr_block);
472 ret = ext4_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
475 ext4_balloc_free_block(xattr_ref->inode_ref,
480 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
481 &xattr_ref->fs->sb, xattr_block);
482 xattr_ref->inode_ref->dirty = true;
483 xattr_ref->block_loaded = true;
484 xattr_ref->ea_size += sizeof(struct ext4_xattr_header);
491 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
493 uint64_t xattr_block;
494 xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
496 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
498 ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
499 ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
500 xattr_ref->inode_ref->dirty = true;
501 xattr_ref->block_loaded = false;
502 xattr_ref->ea_size -= sizeof(struct ext4_xattr_header);
505 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
507 struct ext4_xattr_header *block_header = NULL;
508 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
510 memset(block_header, 0, sizeof(struct ext4_xattr_header));
511 block_header->h_magic = EXT4_XATTR_MAGIC;
512 block_header->h_refcount = to_le32(1);
513 block_header->h_blocks = to_le32(1);
517 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
518 struct ext4_xattr_ibody_header *ibody_header,
519 struct ext4_xattr_entry *entry, void *ibody_data_ptr)
521 entry->e_name_len = to_le32(item->name_len);
522 entry->e_name_index = item->name_index;
523 entry->e_value_offs =
524 (char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header);
525 entry->e_value_block = 0;
526 entry->e_value_size = item->data_size;
529 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
530 struct ext4_xattr_header *block_header,
531 struct ext4_xattr_entry *block_entry,
532 void *block_data_ptr)
534 block_entry->e_name_len = to_le32(item->name_len);
535 block_entry->e_name_index = item->name_index;
536 block_entry->e_value_offs =
537 (char *)block_data_ptr - (char *)block_header;
538 block_entry->e_value_block = 0;
539 block_entry->e_value_size = item->data_size;
542 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
545 bool block_modified = false;
546 void *ibody_data = 0;
547 void *block_data = 0;
548 struct ext4_xattr_item *item, *save_item;
549 size_t inode_size_rem, block_size_rem;
550 struct ext4_xattr_ibody_header *ibody_header = NULL;
551 struct ext4_xattr_header *block_header = NULL;
552 struct ext4_xattr_entry *entry = NULL;
553 struct ext4_xattr_entry *block_entry = NULL;
555 inode_size_rem = ext4_xattr_inode_space(xattr_ref);
556 block_size_rem = ext4_xattr_block_space(xattr_ref);
557 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
558 ibody_header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
559 entry = EXT4_XATTR_IFIRST(ibody_header);
562 if (!xattr_ref->dirty)
564 /* If there are enough spaces in the ibody EA table.*/
565 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
566 memset(ibody_header, 0, inode_size_rem);
567 ibody_header->h_magic = EXT4_XATTR_MAGIC;
568 ibody_data = (char *)ibody_header + inode_size_rem;
569 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
571 xattr_ref->inode_ref->dirty = true;
573 /* If we need an extra block to hold the EA entries*/
574 if (xattr_ref->ea_size > inode_size_rem) {
575 if (!xattr_ref->block_loaded) {
576 ret = ext4_xattr_try_alloc_block(xattr_ref);
580 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
581 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
582 ext4_xattr_set_block_header(xattr_ref);
583 block_data = (char *)block_header + block_size_rem;
584 block_size_rem -= sizeof(struct ext4_xattr_header);
586 xattr_ref->block.dirty = true;
588 /* We don't need an extra block.*/
589 if (xattr_ref->block_loaded) {
590 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
591 block_header->h_refcount =
592 to_le32(to_le32(block_header->h_refcount) - 1);
593 if (!block_header->h_refcount) {
594 ext4_xattr_try_free_block(xattr_ref);
598 EXT4_XATTR_BFIRST(&xattr_ref->block);
600 (char *)block_header + block_size_rem;
602 sizeof(struct ext4_xattr_header);
603 ext4_inode_set_file_acl(
604 xattr_ref->inode_ref->inode,
605 &xattr_ref->fs->sb, 0);
607 xattr_ref->inode_ref->dirty = true;
608 xattr_ref->block.dirty = true;
612 RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
614 if (EXT4_XATTR_SIZE(item->data_size) +
615 EXT4_XATTR_LEN(item->name_len) <=
617 ibody_data = (char *)ibody_data -
618 EXT4_XATTR_SIZE(item->data_size);
619 ext4_xattr_set_inode_entry(item, ibody_header, entry,
621 memcpy(EXT4_XATTR_NAME(entry), item->name,
623 memcpy(ibody_data, item->data, item->data_size);
624 entry = EXT4_XATTR_NEXT(entry);
625 inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
626 EXT4_XATTR_LEN(item->name_len);
628 xattr_ref->inode_ref->dirty = true;
631 if (EXT4_XATTR_SIZE(item->data_size) +
632 EXT4_XATTR_LEN(item->name_len) >
638 (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
639 ext4_xattr_set_block_entry(item, block_header, block_entry,
641 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
643 memcpy(block_data, item->data, item->data_size);
644 block_entry = EXT4_XATTR_NEXT(block_entry);
645 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
646 EXT4_XATTR_LEN(item->name_len);
648 block_modified = true;
650 xattr_ref->dirty = false;
651 if (block_modified) {
652 ext4_xattr_rehash(block_header,
653 EXT4_XATTR_BFIRST(&xattr_ref->block));
654 xattr_ref->block.dirty = true;
661 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
662 int(iter)(struct ext4_xattr_ref *ref,
663 struct ext4_xattr_item *item))
665 struct ext4_xattr_item *item;
667 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
669 RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
671 int ret = EXT4_XATTR_ITERATE_CONT;
675 if (ret != EXT4_XATTR_ITERATE_CONT) {
676 if (ret == EXT4_XATTR_ITERATE_STOP)
677 ref->iter_from = NULL;
684 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
686 ref->iter_from = NULL;
689 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
690 char *name, size_t name_len, void *data, size_t data_size,
694 struct ext4_xattr_item *item =
695 ext4_xattr_lookup_item(ref, name_index, name, name_len);
701 if (item->data_size != data_size)
702 ret = ext4_xattr_resize_item(ref, item, data_size);
707 memcpy(item->data, data, data_size);
713 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
722 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
723 char *name, size_t name_len)
725 return ext4_xattr_remove_item(ref, name_index, name, name_len);
728 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
729 char *name, size_t name_len, void *buf, size_t buf_size,
733 size_t item_size = 0;
734 struct ext4_xattr_item *item =
735 ext4_xattr_lookup_item(ref, name_index, name, name_len);
741 item_size = item->data_size;
742 if (buf_size > item_size)
743 buf_size = item_size;
746 memcpy(buf, item->data, buf_size);
750 *size_got = buf_size;
755 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
756 struct ext4_xattr_ref *ref)
759 uint64_t xattr_block;
760 xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
763 ref->iter_from = NULL;
765 rc = ext4_block_get(fs->bdev, &ref->block, xattr_block);
769 ref->ea_size += sizeof(struct ext4_xattr_header);
770 ref->block_loaded = true;
772 ref->block_loaded = false;
774 ref->inode_ref = inode_ref;
777 if (ext4_xattr_inode_space(ref) >
778 sizeof(struct ext4_xattr_ibody_header))
779 ref->ea_size += sizeof(struct ext4_xattr_ibody_header);
781 rc = ext4_xattr_fetch(ref);
783 ext4_xattr_purge_items(ref);
785 ext4_block_set(fs->bdev, &inode_ref->block);
787 ref->block_loaded = false;
793 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
795 ext4_xattr_write_to_disk(ref);
796 if (ref->block_loaded) {
797 ext4_block_set(ref->fs->bdev, &ref->block);
798 ref->block_loaded = false;
800 ext4_xattr_purge_items(ref);
801 ref->inode_ref = NULL;
805 struct xattr_prefix {
810 static const struct xattr_prefix prefix_tbl[] = {
811 {"user.", EXT4_XATTR_INDEX_USER},
812 {"system.", EXT4_XATTR_INDEX_SYSTEM},
813 {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
814 {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
818 char *ext4_extract_xattr_name(char *full_name, size_t full_name_len,
819 uint8_t *name_index, size_t *name_len)
822 ext4_assert(name_index);
826 for (i = 0; prefix_tbl[i].prefix; i++) {
827 size_t prefix_len = strlen(prefix_tbl[i].prefix);
828 if (full_name_len >= prefix_len &&
829 !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
830 *name_index = prefix_tbl[i].name_index;
832 *name_len = full_name_len - prefix_len;
834 return full_name + prefix_len;