2 * ext_attr.c --- extended attribute blocks
4 * Copyright (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
6 * Copyright (C) 2002 Theodore Ts'o.
9 * This file may be redistributed under the terms of the GNU Library
10 * General Public License, version 2.
22 #if defined HAVE_SYS_XATTR_H
23 #include <sys/xattr.h>
24 #elif defined HAVE_ATTR_XATTR_H
25 #include <attr/xattr.h>
29 #include "ext2_ext_attr.h"
34 static errcode_t read_ea_inode_hash(ext2_filsys fs, ext2_ino_t ino, __u32 *hash)
36 struct ext2_inode inode;
39 retval = ext2fs_read_inode(fs, ino, &inode);
42 *hash = ext2fs_get_ea_inode_hash(&inode);
46 #define NAME_HASH_SHIFT 5
47 #define VALUE_HASH_SHIFT 16
50 * ext2_xattr_hash_entry()
52 * Compute the hash of an extended attribute.
54 __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry, void *data)
57 char *name = ((char *) entry) + sizeof(struct ext2_ext_attr_entry);
60 for (n = 0; n < entry->e_name_len; n++) {
61 hash = (hash << NAME_HASH_SHIFT) ^
62 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
66 /* The hash needs to be calculated on the data in little-endian. */
67 if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
68 __u32 *value = (__u32 *)data;
69 for (n = (entry->e_value_size + EXT2_EXT_ATTR_ROUND) >>
70 EXT2_EXT_ATTR_PAD_BITS; n; n--) {
71 hash = (hash << VALUE_HASH_SHIFT) ^
72 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
73 ext2fs_le32_to_cpu(*value++);
81 * ext2fs_ext_attr_hash_entry2()
83 * Compute the hash of an extended attribute.
84 * This version of the function supports hashing entries that reference
85 * external inodes (ea_inode feature).
87 errcode_t ext2fs_ext_attr_hash_entry2(ext2_filsys fs,
88 struct ext2_ext_attr_entry *entry,
89 void *data, __u32 *hash)
91 *hash = ext2fs_ext_attr_hash_entry(entry, data);
93 if (entry->e_value_inum) {
97 retval = read_ea_inode_hash(fs, entry->e_value_inum,
102 *hash = (*hash << VALUE_HASH_SHIFT) ^
103 (*hash >> (8*sizeof(*hash) - VALUE_HASH_SHIFT)) ^
109 #undef NAME_HASH_SHIFT
110 #undef VALUE_HASH_SHIFT
112 #define BLOCK_HASH_SHIFT 16
114 /* Mirrors ext4_xattr_rehash() implementation in kernel. */
115 void ext2fs_ext_attr_block_rehash(struct ext2_ext_attr_header *header,
116 struct ext2_ext_attr_entry *end)
118 struct ext2_ext_attr_entry *here;
121 here = (struct ext2_ext_attr_entry *)(header+1);
122 while (here < end && !EXT2_EXT_IS_LAST_ENTRY(here)) {
124 /* Block is not shared if an entry's hash value == 0 */
128 hash = (hash << BLOCK_HASH_SHIFT) ^
129 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
131 here = EXT2_EXT_ATTR_NEXT(here);
133 header->h_hash = hash;
137 * Re-compute the extended attribute hash value after an entry has changed.
139 static void ext2fs_attr_rehash(struct ext2_ext_attr_header *header,
140 struct ext2_ext_attr_entry *entry)
142 struct ext2_ext_attr_entry *here;
145 entry->e_hash = ext2fs_ext_attr_hash_entry(entry, (char *)header +
146 entry->e_value_offs);
148 here = ENTRY(header+1);
149 while (!EXT2_EXT_IS_LAST_ENTRY(here)) {
151 /* Block is not shared if an entry's hash value == 0 */
155 hash = (hash << BLOCK_HASH_SHIFT) ^
156 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
158 here = EXT2_EXT_ATTR_NEXT(here);
160 header->h_hash = hash;
163 #undef BLOCK_HASH_SHIFT
165 __u32 ext2fs_get_ea_inode_hash(struct ext2_inode *inode)
167 return inode->i_atime;
170 void ext2fs_set_ea_inode_hash(struct ext2_inode *inode, __u32 hash)
172 inode->i_atime = hash;
175 __u64 ext2fs_get_ea_inode_ref(struct ext2_inode *inode)
177 return ((__u64)inode->i_ctime << 32) | inode->osd1.linux1.l_i_version;
180 void ext2fs_set_ea_inode_ref(struct ext2_inode *inode, __u64 ref_count)
182 inode->i_ctime = (__u32)(ref_count >> 32);
183 inode->osd1.linux1.l_i_version = (__u32)ref_count;
186 static errcode_t check_ext_attr_header(struct ext2_ext_attr_header *header)
188 if ((header->h_magic != EXT2_EXT_ATTR_MAGIC_v1 &&
189 header->h_magic != EXT2_EXT_ATTR_MAGIC) ||
190 header->h_blocks != 1)
191 return EXT2_ET_BAD_EA_HEADER;
196 errcode_t ext2fs_read_ext_attr3(ext2_filsys fs, blk64_t block, void *buf,
202 retval = io_channel_read_blk64(fs->io, block, 1, buf);
206 if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
207 !ext2fs_ext_attr_block_csum_verify(fs, inum, block, buf))
210 #ifdef WORDS_BIGENDIAN
211 ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
214 retval = check_ext_attr_header(buf);
215 if (retval == 0 && csum_failed)
216 retval = EXT2_ET_EXT_ATTR_CSUM_INVALID;
221 errcode_t ext2fs_read_ext_attr2(ext2_filsys fs, blk64_t block, void *buf)
223 return ext2fs_read_ext_attr3(fs, block, buf, 0);
226 errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
228 return ext2fs_read_ext_attr2(fs, block, buf);
231 errcode_t ext2fs_write_ext_attr3(ext2_filsys fs, blk64_t block, void *inbuf,
237 #ifdef WORDS_BIGENDIAN
238 retval = ext2fs_get_mem(fs->blocksize, &write_buf);
241 ext2fs_swap_ext_attr(write_buf, inbuf, fs->blocksize, 1);
243 write_buf = (char *) inbuf;
246 retval = ext2fs_ext_attr_block_csum_set(fs, inum, block,
247 (struct ext2_ext_attr_header *)write_buf);
251 retval = io_channel_write_blk64(fs->io, block, 1, write_buf);
252 #ifdef WORDS_BIGENDIAN
253 ext2fs_free_mem(&write_buf);
256 ext2fs_mark_changed(fs);
260 errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block, void *inbuf)
262 return ext2fs_write_ext_attr3(fs, block, inbuf, 0);
265 errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
267 return ext2fs_write_ext_attr2(fs, block, inbuf);
271 * This function adjusts the reference count of the EA block.
273 errcode_t ext2fs_adjust_ea_refcount3(ext2_filsys fs, blk64_t blk,
274 char *block_buf, int adjust,
275 __u32 *newcount, ext2_ino_t inum)
278 struct ext2_ext_attr_header *header;
281 if ((blk >= ext2fs_blocks_count(fs->super)) ||
282 (blk < fs->super->s_first_data_block))
283 return EXT2_ET_BAD_EA_BLOCK_NUM;
286 retval = ext2fs_get_mem(fs->blocksize, &buf);
292 retval = ext2fs_read_ext_attr3(fs, blk, block_buf, inum);
296 header = BHDR(block_buf);
297 if (header->h_magic != EXT2_EXT_ATTR_MAGIC)
298 return EXT2_ET_EA_BAD_MAGIC;
300 header->h_refcount += adjust;
302 *newcount = header->h_refcount;
304 retval = ext2fs_write_ext_attr3(fs, blk, block_buf, inum);
310 ext2fs_free_mem(&buf);
314 errcode_t ext2fs_adjust_ea_refcount2(ext2_filsys fs, blk64_t blk,
315 char *block_buf, int adjust,
318 return ext2fs_adjust_ea_refcount3(fs, blk, block_buf, adjust,
322 errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
323 char *block_buf, int adjust,
326 return ext2fs_adjust_ea_refcount2(fs, blk, block_buf, adjust,
330 /* Manipulate the contents of extended attribute regions */
336 unsigned int value_len;
340 struct ext2_xattr_handle {
343 struct ext2_xattr *attrs;
351 static errcode_t ext2fs_xattrs_expand(struct ext2_xattr_handle *h,
352 unsigned int expandby)
354 struct ext2_xattr *new_attrs;
357 err = ext2fs_get_arrayzero(h->capacity + expandby,
358 sizeof(struct ext2_xattr), &new_attrs);
362 memcpy(new_attrs, h->attrs, h->capacity * sizeof(struct ext2_xattr));
363 ext2fs_free_mem(&h->attrs);
364 h->capacity += expandby;
365 h->attrs = new_attrs;
370 struct ea_name_index {
375 /* Keep these names sorted in order of decreasing specificity. */
376 static struct ea_name_index ea_names[] = {
378 {3, "system.posix_acl_default"},
379 {2, "system.posix_acl_access"},
380 {8, "system.richacl"},
388 static const char *find_ea_prefix(int index)
390 struct ea_name_index *e;
392 for (e = ea_names; e->name; e++)
393 if (e->index == index)
399 static int find_ea_index(const char *fullname, const char **name, int *index)
401 struct ea_name_index *e;
403 for (e = ea_names; e->name; e++) {
404 if (strncmp(fullname, e->name, strlen(e->name)) == 0) {
405 *name = fullname + strlen(e->name);
413 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
414 struct ext2_inode_large *inode)
416 struct ext2_ext_attr_header *header;
417 void *block_buf = NULL;
420 struct ext2_inode_large i;
424 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&i,
425 sizeof(struct ext2_inode_large));
431 /* Do we already have an EA block? */
432 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
436 /* Find block, zero it, write back */
437 if ((blk < fs->super->s_first_data_block) ||
438 (blk >= ext2fs_blocks_count(fs->super))) {
439 err = EXT2_ET_BAD_EA_BLOCK_NUM;
443 err = ext2fs_get_mem(fs->blocksize, &block_buf);
447 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
451 /* We only know how to deal with v2 EA blocks */
452 header = (struct ext2_ext_attr_header *) block_buf;
453 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
454 err = EXT2_ET_BAD_EA_HEADER;
458 header->h_refcount--;
459 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
463 /* Erase link to block */
464 ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, 0);
465 if (header->h_refcount == 0)
466 ext2fs_block_alloc_stats2(fs, blk, -1);
467 err = ext2fs_iblk_sub_blocks(fs, (struct ext2_inode *)inode, 1);
473 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&i,
474 sizeof(struct ext2_inode_large));
480 ext2fs_free_mem(&block_buf);
485 static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
486 struct ext2_inode_large *inode)
488 struct ext2_ext_attr_header *header;
489 void *block_buf = NULL;
493 /* Do we already have an EA block? */
494 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
496 if ((blk < fs->super->s_first_data_block) ||
497 (blk >= ext2fs_blocks_count(fs->super))) {
498 err = EXT2_ET_BAD_EA_BLOCK_NUM;
502 err = ext2fs_get_mem(fs->blocksize, &block_buf);
506 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
510 /* We only know how to deal with v2 EA blocks */
511 header = (struct ext2_ext_attr_header *) block_buf;
512 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
513 err = EXT2_ET_BAD_EA_HEADER;
517 /* Single-user block. We're done here. */
518 if (header->h_refcount == 1)
521 /* We need to CoW the block. */
522 header->h_refcount--;
523 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
527 /* No block, we must increment i_blocks */
528 err = ext2fs_iblk_add_blocks(fs, (struct ext2_inode *)inode,
534 /* Allocate a block */
535 goal = ext2fs_find_inode_goal(fs, ino, (struct ext2_inode *)inode, 0);
536 err = ext2fs_alloc_block2(fs, goal, NULL, &blk);
539 ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, blk);
542 ext2fs_free_mem(&block_buf);
549 posix_acl_xattr_count(size_t size)
551 if (size < sizeof(posix_acl_xattr_header))
553 size -= sizeof(posix_acl_xattr_header);
554 if (size % sizeof(posix_acl_xattr_entry))
556 return size / sizeof(posix_acl_xattr_entry);
560 * The lgetxattr function returns data formatted in the POSIX extended
561 * attribute format. The on-disk format uses a more compact encoding.
562 * See the ext4_acl_to_disk in fs/ext4/acl.c.
564 static errcode_t convert_posix_acl_to_disk_buffer(const void *value, size_t size,
565 void *out_buf, size_t *size_out)
567 const posix_acl_xattr_header *header =
568 (const posix_acl_xattr_header*) value;
569 const posix_acl_xattr_entry *end, *entry =
570 (const posix_acl_xattr_entry *)(header+1);
571 ext4_acl_header *ext_acl;
579 if (size < sizeof(posix_acl_xattr_header))
581 if (header->a_version != ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION))
584 count = posix_acl_xattr_count(size);
586 ext_acl->a_version = ext2fs_cpu_to_le32(EXT4_ACL_VERSION);
591 e = (char *) out_buf + sizeof(ext4_acl_header);
592 s = sizeof(ext4_acl_header);
593 for (end = entry + count; entry != end;entry++) {
594 ext4_acl_entry *disk_entry = (ext4_acl_entry*) e;
595 disk_entry->e_tag = entry->e_tag;
596 disk_entry->e_perm = entry->e_perm;
598 switch(ext2fs_le16_to_cpu(entry->e_tag)) {
603 e += sizeof(ext4_acl_entry_short);
604 s += sizeof(ext4_acl_entry_short);
608 disk_entry->e_id = entry->e_id;
609 e += sizeof(ext4_acl_entry);
610 s += sizeof(ext4_acl_entry);
620 static errcode_t convert_disk_buffer_to_posix_acl(const void *value, size_t size,
621 void **out_buf, size_t *size_out)
623 posix_acl_xattr_header *header;
624 posix_acl_xattr_entry *entry;
625 const ext4_acl_header *ext_acl = (const ext4_acl_header *) value;
631 (size < sizeof(ext4_acl_header)) ||
632 (ext_acl->a_version != ext2fs_cpu_to_le32(EXT4_ACL_VERSION)))
635 err = ext2fs_get_mem(size * 2, &out);
639 header = (posix_acl_xattr_header *) out;
640 header->a_version = ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION);
641 entry = (posix_acl_xattr_entry *) (out + sizeof(posix_acl_xattr_header));
643 cp = (const char *) value + sizeof(ext4_acl_header);
644 size -= sizeof(ext4_acl_header);
647 const ext4_acl_entry *disk_entry = (const ext4_acl_entry *) cp;
649 entry->e_tag = disk_entry->e_tag;
650 entry->e_perm = disk_entry->e_perm;
652 switch(ext2fs_le16_to_cpu(entry->e_tag)) {
658 cp += sizeof(ext4_acl_entry_short);
659 size -= sizeof(ext4_acl_entry_short);
663 entry->e_id = disk_entry->e_id;
664 cp += sizeof(ext4_acl_entry);
665 size -= sizeof(ext4_acl_entry);
668 ext2fs_free_mem(&out);
674 *size_out = ((char *) entry - out);
679 write_xattrs_to_buffer(ext2_filsys fs, struct ext2_xattr *attrs, int count,
680 void *entries_start, unsigned int storage_size,
681 unsigned int value_offset_correction, int write_hash)
683 struct ext2_xattr *x;
684 struct ext2_ext_attr_entry *e = entries_start;
685 char *end = (char *) entries_start + storage_size;
686 unsigned int value_size;
689 memset(entries_start, 0, storage_size);
690 for (x = attrs; x < attrs + count; x++) {
691 value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
692 EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
694 /* Fill out e appropriately */
695 e->e_name_len = strlen(x->short_name);
696 e->e_name_index = x->name_index;
698 e->e_value_size = x->value_len;
699 e->e_value_inum = x->ea_ino;
702 memcpy((char *)e + sizeof(*e), x->short_name, e->e_name_len);
707 e->e_value_offs = end - (char *) entries_start +
708 value_offset_correction;
709 memcpy(end, x->value, e->e_value_size);
712 if (write_hash || x->ea_ino) {
713 err = ext2fs_ext_attr_hash_entry2(fs, e,
721 e = EXT2_EXT_ATTR_NEXT(e);
727 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
729 ext2_filsys fs = handle->fs;
730 const unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
731 struct ext2_inode_large *inode;
732 char *start, *block_buf = NULL;
733 struct ext2_ext_attr_header *header;
734 __u32 ea_inode_magic;
736 unsigned int storage_size;
740 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
742 if (i < sizeof(*inode))
744 err = ext2fs_get_memzero(i, &inode);
748 err = ext2fs_read_inode_full(fs, handle->ino, EXT2_INODE(inode),
753 /* If extra_isize isn't set, we need to set it now */
754 if (inode->i_extra_isize == 0 &&
755 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
756 char *p = (char *)inode;
757 size_t extra = fs->super->s_want_extra_isize;
760 extra = sizeof(__u32);
761 memset(p + EXT2_GOOD_OLD_INODE_SIZE, 0, extra);
762 inode->i_extra_isize = extra;
764 if (inode->i_extra_isize & 3) {
765 err = EXT2_ET_INODE_CORRUPTED;
769 /* Does the inode have space for EA? */
770 if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
771 inode_size <= EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize +
775 /* Write the inode EA */
776 ea_inode_magic = EXT2_EXT_ATTR_MAGIC;
777 memcpy(((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
778 inode->i_extra_isize, &ea_inode_magic, sizeof(__u32));
779 storage_size = inode_size - EXT2_GOOD_OLD_INODE_SIZE -
780 inode->i_extra_isize - sizeof(__u32);
781 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
782 inode->i_extra_isize + sizeof(__u32);
784 err = write_xattrs_to_buffer(fs, handle->attrs, handle->ibody_count,
785 start, storage_size, 0, 0);
790 if (handle->ibody_count == handle->count &&
791 !ext2fs_file_acl_block(fs, EXT2_INODE(inode)))
794 /* Write the EA block */
795 err = ext2fs_get_memzero(fs->blocksize, &block_buf);
799 storage_size = fs->blocksize - sizeof(struct ext2_ext_attr_header);
800 start = block_buf + sizeof(struct ext2_ext_attr_header);
802 err = write_xattrs_to_buffer(fs, handle->attrs + handle->ibody_count,
803 handle->count - handle->ibody_count, start,
804 storage_size, start - block_buf, 1);
808 /* Write a header on the EA block */
809 header = (struct ext2_ext_attr_header *) block_buf;
810 header->h_magic = EXT2_EXT_ATTR_MAGIC;
811 header->h_refcount = 1;
812 header->h_blocks = 1;
814 /* Get a new block for writing */
815 err = prep_ea_block_for_write(fs, handle->ino, inode);
819 /* Finally, write the new EA block */
820 blk = ext2fs_file_acl_block(fs, EXT2_INODE(inode));
821 err = ext2fs_write_ext_attr3(fs, blk, block_buf, handle->ino);
826 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
827 if (!block_buf && blk) {
828 /* xattrs shrunk, free the block */
829 err = ext2fs_free_ext_attr(fs, handle->ino, inode);
834 /* Write the inode */
835 err = ext2fs_write_inode_full(fs, handle->ino, EXT2_INODE(inode),
841 ext2fs_free_mem(&block_buf);
843 ext2fs_free_mem(&inode);
847 static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
848 struct ext2_inode_large *inode,
849 struct ext2_ext_attr_entry *entries,
850 unsigned int storage_size,
853 struct ext2_xattr *x;
854 struct ext2_ext_attr_entry *entry, *end;
856 unsigned int remain, prefix_len;
858 unsigned int values_size = storage_size +
859 ((char *)entries - value_start);
863 remain = storage_size;
864 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
865 !EXT2_EXT_IS_LAST_ENTRY(end)) {
867 /* header eats this space */
868 remain -= sizeof(struct ext2_ext_attr_entry);
870 /* is attribute name valid? */
871 if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
872 return EXT2_ET_EA_BAD_NAME_LEN;
874 /* attribute len eats this space */
875 remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
876 end = EXT2_EXT_ATTR_NEXT(end);
880 remain = storage_size;
881 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
882 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
884 /* Allocate space for more attrs? */
885 if (handle->count == handle->capacity) {
886 err = ext2fs_xattrs_expand(handle, 4);
891 x = handle->attrs + handle->count;
893 /* header eats this space */
894 remain -= sizeof(struct ext2_ext_attr_entry);
896 /* attribute len eats this space */
897 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
900 prefix = find_ea_prefix(entry->e_name_index);
901 prefix_len = (prefix ? strlen(prefix) : 0);
902 err = ext2fs_get_memzero(entry->e_name_len + prefix_len + 1,
907 memcpy(x->name, prefix, prefix_len);
908 if (entry->e_name_len)
909 memcpy(x->name + prefix_len,
910 (char *)entry + sizeof(*entry),
912 x->short_name = x->name + prefix_len;
913 x->name_index = entry->e_name_index;
915 /* Check & copy value */
916 if (!ext2fs_has_feature_ea_inode(handle->fs->super) &&
917 entry->e_value_inum != 0)
918 return EXT2_ET_BAD_EA_BLOCK_NUM;
920 if (entry->e_value_inum == 0) {
921 if (entry->e_value_size > remain)
922 return EXT2_ET_EA_BAD_VALUE_SIZE;
924 if (entry->e_value_offs + entry->e_value_size > values_size)
925 return EXT2_ET_EA_BAD_VALUE_OFFSET;
927 if (entry->e_value_size > 0 &&
928 value_start + entry->e_value_offs <
929 (char *)end + sizeof(__u32))
930 return EXT2_ET_EA_BAD_VALUE_OFFSET;
932 remain -= entry->e_value_size;
934 err = ext2fs_get_mem(entry->e_value_size, &x->value);
937 memcpy(x->value, value_start + entry->e_value_offs,
938 entry->e_value_size);
940 struct ext2_inode *ea_inode;
943 if (entry->e_value_offs != 0)
944 return EXT2_ET_EA_BAD_VALUE_OFFSET;
946 if (entry->e_value_size > (64 * 1024))
947 return EXT2_ET_EA_BAD_VALUE_SIZE;
949 err = ext2fs_get_mem(entry->e_value_size, &x->value);
953 err = ext2fs_file_open(handle->fs, entry->e_value_inum,
958 ea_inode = ext2fs_file_get_inode(ea_file);
959 if ((ea_inode->i_flags & EXT4_INLINE_DATA_FL) ||
960 !(ea_inode->i_flags & EXT4_EA_INODE_FL) ||
961 ea_inode->i_links_count == 0)
962 err = EXT2_ET_EA_INODE_CORRUPTED;
963 else if ((__u64) ext2fs_file_get_size(ea_file) !=
965 err = EXT2_ET_EA_BAD_VALUE_SIZE;
967 err = ext2fs_file_read(ea_file, x->value,
968 entry->e_value_size, 0);
969 ext2fs_file_close(ea_file);
974 x->ea_ino = entry->e_value_inum;
975 x->value_len = entry->e_value_size;
977 /* e_hash may be 0 in older inode's ea */
978 if (entry->e_hash != 0) {
980 void *data = (entry->e_value_inum != 0) ?
981 0 : value_start + entry->e_value_offs;
983 err = ext2fs_ext_attr_hash_entry2(handle->fs, entry,
987 if (entry->e_hash != hash) {
988 struct ext2_inode child;
990 /* Check whether this is an old Lustre-style
991 * ea_inode reference.
993 err = ext2fs_read_inode(handle->fs,
998 if (child.i_mtime != handle->ino ||
999 child.i_generation != inode->i_generation)
1000 return EXT2_ET_BAD_EA_HASH;
1005 entry = EXT2_EXT_ATTR_NEXT(entry);
1011 static void xattrs_free_keys(struct ext2_xattr_handle *h)
1013 struct ext2_xattr *a = h->attrs;
1016 for (i = 0; i < h->capacity; i++) {
1018 ext2fs_free_mem(&a[i].name);
1020 ext2fs_free_mem(&a[i].value);
1026 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle)
1028 struct ext2_inode_large *inode;
1029 struct ext2_ext_attr_header *header;
1030 __u32 ea_inode_magic;
1031 unsigned int storage_size;
1032 char *start, *block_buf = NULL;
1037 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1038 i = EXT2_INODE_SIZE(handle->fs->super);
1039 if (i < sizeof(*inode))
1041 err = ext2fs_get_memzero(i, &inode);
1045 err = ext2fs_read_inode_full(handle->fs, handle->ino,
1046 (struct ext2_inode *)inode,
1047 EXT2_INODE_SIZE(handle->fs->super));
1051 xattrs_free_keys(handle);
1053 /* Does the inode have space for EA? */
1054 if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
1055 EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1056 inode->i_extra_isize +
1059 if (inode->i_extra_isize & 3) {
1060 err = EXT2_ET_INODE_CORRUPTED;
1064 /* Look for EA in the inode */
1065 memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1066 inode->i_extra_isize, sizeof(__u32));
1067 if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1068 storage_size = EXT2_INODE_SIZE(handle->fs->super) -
1069 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
1071 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1072 inode->i_extra_isize + sizeof(__u32);
1074 err = read_xattrs_from_buffer(handle, inode,
1075 (struct ext2_ext_attr_entry *) start,
1076 storage_size, start);
1080 handle->ibody_count = handle->count;
1084 /* Look for EA in a separate EA block */
1085 blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
1087 if ((blk < handle->fs->super->s_first_data_block) ||
1088 (blk >= ext2fs_blocks_count(handle->fs->super))) {
1089 err = EXT2_ET_BAD_EA_BLOCK_NUM;
1093 err = ext2fs_get_mem(handle->fs->blocksize, &block_buf);
1097 err = ext2fs_read_ext_attr3(handle->fs, blk, block_buf,
1102 /* We only know how to deal with v2 EA blocks */
1103 header = (struct ext2_ext_attr_header *) block_buf;
1104 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1105 err = EXT2_ET_BAD_EA_HEADER;
1110 storage_size = handle->fs->blocksize -
1111 sizeof(struct ext2_ext_attr_header);
1112 start = block_buf + sizeof(struct ext2_ext_attr_header);
1113 err = read_xattrs_from_buffer(handle, inode,
1114 (struct ext2_ext_attr_entry *) start,
1115 storage_size, block_buf);
1119 ext2fs_free_mem(&block_buf);
1122 ext2fs_free_mem(&block_buf);
1123 ext2fs_free_mem(&inode);
1127 ext2fs_free_mem(&block_buf);
1129 ext2fs_free_mem(&inode);
1133 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
1134 int (*func)(char *name, char *value,
1135 size_t value_len, void *data),
1138 struct ext2_xattr *x;
1142 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1143 for (x = h->attrs; x < h->attrs + h->count; x++) {
1144 ret = func(x->name, x->value, x->value_len, data);
1145 if (ret & XATTR_CHANGED)
1147 if (ret & XATTR_ABORT)
1152 return ext2fs_xattrs_write(h);
1156 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
1157 void **value, size_t *value_len)
1159 struct ext2_xattr *x;
1163 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1164 for (x = h->attrs; x < h->attrs + h->count; x++) {
1165 if (strcmp(x->name, key))
1168 if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1169 ((strcmp(key, "system.posix_acl_default") == 0) ||
1170 (strcmp(key, "system.posix_acl_access") == 0))) {
1171 err = convert_disk_buffer_to_posix_acl(x->value, x->value_len,
1175 err = ext2fs_get_mem(x->value_len, &val);
1178 memcpy(val, x->value, x->value_len);
1180 *value_len = x->value_len;
1185 return EXT2_ET_EA_KEY_NOT_FOUND;
1188 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
1191 struct ext2_ext_attr_entry *entry;
1192 struct ext2_inode_large *inode;
1193 __u32 ea_inode_magic;
1194 unsigned int minoff;
1199 i = EXT2_INODE_SIZE(fs->super);
1200 if (i < sizeof(*inode))
1202 err = ext2fs_get_memzero(i, &inode);
1206 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)inode,
1207 EXT2_INODE_SIZE(fs->super));
1211 /* Does the inode have size for EA? */
1212 if (EXT2_INODE_SIZE(fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1213 inode->i_extra_isize +
1215 err = EXT2_ET_INLINE_DATA_NO_SPACE;
1219 minoff = EXT2_INODE_SIZE(fs->super) - sizeof(*inode) - sizeof(__u32);
1220 memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1221 inode->i_extra_isize, sizeof(__u32));
1222 if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1223 /* has xattrs. calculate the size */
1224 start= ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1225 inode->i_extra_isize + sizeof(__u32);
1226 entry = (struct ext2_ext_attr_entry *) start;
1227 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1228 if (!entry->e_value_inum && entry->e_value_size) {
1229 unsigned int offs = entry->e_value_offs;
1233 entry = EXT2_EXT_ATTR_NEXT(entry);
1235 *size = minoff - ((char *)entry - (char *)start) - sizeof(__u32);
1237 /* no xattr. return a maximum size */
1238 *size = EXT2_EXT_ATTR_SIZE(minoff -
1239 EXT2_EXT_ATTR_LEN(strlen("data")) -
1240 EXT2_EXT_ATTR_ROUND - sizeof(__u32));
1244 ext2fs_free_mem(&inode);
1248 static errcode_t xattr_create_ea_inode(ext2_filsys fs, const void *value,
1249 size_t value_len, ext2_ino_t *ea_ino)
1251 struct ext2_inode inode;
1257 ret = ext2fs_new_inode(fs, 0, 0, 0, &ino);
1261 memset(&inode, 0, sizeof(inode));
1262 inode.i_flags |= EXT4_EA_INODE_FL;
1263 if (ext2fs_has_feature_extents(fs->super))
1264 inode.i_flags |= EXT4_EXTENTS_FL;
1266 inode.i_mode = LINUX_S_IFREG | 0600;
1267 inode.i_links_count = 1;
1268 ret = ext2fs_write_new_inode(fs, ino, &inode);
1272 * ref_count and hash utilize inode's i_*time fields.
1273 * ext2fs_write_new_inode() call above initializes these fields with
1274 * current time. That's why ref count and hash updates are done
1277 ext2fs_set_ea_inode_ref(&inode, 1);
1278 hash = ext2fs_crc32c_le(fs->csum_seed, value, value_len);
1279 ext2fs_set_ea_inode_hash(&inode, hash);
1281 ret = ext2fs_write_inode(fs, ino, &inode);
1285 ret = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
1288 ret = ext2fs_file_write(file, value, value_len, NULL);
1289 ext2fs_file_close(file);
1293 ext2fs_inode_alloc_stats2(fs, ino, 1 /* inuse */, 0 /* isdir */);
1299 static errcode_t xattr_inode_dec_ref(ext2_filsys fs, ext2_ino_t ino)
1301 struct ext2_inode_large inode;
1305 ret = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1310 ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(&inode));
1312 ext2fs_set_ea_inode_ref(EXT2_INODE(&inode), ref_count);
1317 inode.i_links_count = 0;
1318 inode.i_dtime = fs->now ? fs->now : time(0);
1320 ret = ext2fs_free_ext_attr(fs, ino, &inode);
1324 if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) {
1325 ret = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL,
1331 ext2fs_inode_alloc_stats2(fs, ino, -1 /* inuse */, 0 /* is_dir */);
1334 ret = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1340 static errcode_t xattr_update_entry(ext2_filsys fs, struct ext2_xattr *x,
1341 const char *name, const char *short_name,
1342 int index, const void *value,
1343 size_t value_len, int in_inode)
1345 ext2_ino_t ea_ino = 0;
1346 void *new_value = NULL;
1347 char *new_name = NULL;
1352 name_len = strlen(name);
1353 ret = ext2fs_get_mem(name_len + 1, &new_name);
1356 memcpy(new_name, name, name_len + 1);
1359 ret = ext2fs_get_mem(value_len, &new_value);
1362 memcpy(new_value, value, value_len);
1365 ret = xattr_create_ea_inode(fs, value, value_len, &ea_ino);
1371 ret = xattr_inode_dec_ref(fs, x->ea_ino);
1378 x->short_name = new_name + (short_name - name);
1380 x->name_index = index;
1383 ext2fs_free_mem(&x->value);
1384 x->value = new_value;
1385 x->value_len = value_len;
1390 ext2fs_free_mem(&new_name);
1392 ext2fs_free_mem(&new_value);
1394 xattr_inode_dec_ref(fs, ea_ino);
1398 static int xattr_find_position(struct ext2_xattr *attrs, int count,
1399 const char *shortname, int name_idx)
1401 struct ext2_xattr *x;
1403 int shortname_len, x_shortname_len;
1405 shortname_len = strlen(shortname);
1407 for (i = 0, x = attrs; i < count; i++, x++) {
1408 if (name_idx < x->name_index)
1410 if (name_idx > x->name_index)
1413 x_shortname_len = strlen(x->short_name);
1414 if (shortname_len < x_shortname_len)
1416 if (shortname_len > x_shortname_len)
1419 if (memcmp(shortname, x->short_name, shortname_len) <= 0)
1425 static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
1427 const void *value, size_t value_len,
1428 int ibody_free, int block_free,
1429 int old_idx, int in_inode)
1431 struct ext2_xattr tmp;
1434 int name_len, name_idx = 0;
1435 const char *shortname = name;
1439 find_ea_index(name, &shortname, &name_idx);
1440 name_len = strlen(shortname);
1442 needed = EXT2_EXT_ATTR_LEN(name_len);
1444 needed += EXT2_EXT_ATTR_SIZE(value_len);
1446 if (old_idx >= 0 && old_idx < h->ibody_count) {
1447 ibody_free += EXT2_EXT_ATTR_LEN(name_len);
1448 if (!h->attrs[old_idx].ea_ino)
1449 ibody_free += EXT2_EXT_ATTR_SIZE(
1450 h->attrs[old_idx].value_len);
1453 if (needed <= ibody_free) {
1455 new_idx = h->ibody_count;
1460 /* Update the existing entry. */
1461 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1462 shortname, name_idx, value,
1463 value_len, in_inode);
1466 if (h->ibody_count <= old_idx) {
1467 /* Move entry from block to the end of ibody. */
1468 tmp = h->attrs[old_idx];
1469 memmove(h->attrs + h->ibody_count + 1,
1470 h->attrs + h->ibody_count,
1471 (old_idx - h->ibody_count) * sizeof(*h->attrs));
1472 h->attrs[h->ibody_count] = tmp;
1478 if (h->ibody_count <= old_idx) {
1479 block_free += EXT2_EXT_ATTR_LEN(name_len);
1480 if (!h->attrs[old_idx].ea_ino)
1482 EXT2_EXT_ATTR_SIZE(h->attrs[old_idx].value_len);
1485 if (needed > block_free)
1486 return EXT2_ET_EA_NO_SPACE;
1489 /* Update the existing entry. */
1490 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1491 shortname, name_idx, value,
1492 value_len, in_inode);
1495 if (old_idx < h->ibody_count) {
1497 * Move entry from ibody to the block. Note that
1498 * entries in the block are sorted.
1500 new_idx = xattr_find_position(h->attrs + h->ibody_count,
1501 h->count - h->ibody_count,
1502 shortname, name_idx);
1503 new_idx += h->ibody_count - 1;
1504 tmp = h->attrs[old_idx];
1505 memmove(h->attrs + old_idx, h->attrs + old_idx + 1,
1506 (new_idx - old_idx) * sizeof(*h->attrs));
1507 h->attrs[new_idx] = tmp;
1513 new_idx = xattr_find_position(h->attrs + h->ibody_count,
1514 h->count - h->ibody_count,
1515 shortname, name_idx);
1516 new_idx += h->ibody_count;
1520 if (h->count == h->capacity) {
1521 ret = ext2fs_xattrs_expand(h, 4);
1526 ret = xattr_update_entry(h->fs, &h->attrs[h->count], name, shortname,
1527 name_idx, value, value_len, in_inode);
1531 tmp = h->attrs[h->count];
1532 memmove(h->attrs + new_idx + 1, h->attrs + new_idx,
1533 (h->count - new_idx)*sizeof(*h->attrs));
1534 h->attrs[new_idx] = tmp;
1541 static int space_used(struct ext2_xattr *attrs, int count)
1544 struct ext2_xattr *x;
1547 for (i = 0, x = attrs; i < count; i++, x++) {
1548 len = strlen(x->short_name);
1549 total += EXT2_EXT_ATTR_LEN(len);
1551 total += EXT2_EXT_ATTR_SIZE(x->value_len);
1557 * The minimum size of EA value when you start storing it in an external inode
1558 * size of block - size of header - size of 1 entry - 4 null bytes
1560 #define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1561 ((b) - EXT2_EXT_ATTR_LEN(3) - sizeof(struct ext2_ext_attr_header) - 4)
1563 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *h,
1568 ext2_filsys fs = h->fs;
1569 const int inode_size = EXT2_INODE_SIZE(fs->super);
1570 struct ext2_inode_large *inode = NULL;
1571 struct ext2_xattr *x;
1573 int ibody_free, block_free;
1579 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1581 ret = ext2fs_get_mem(value_len, &new_value);
1584 if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1585 ((strcmp(name, "system.posix_acl_default") == 0) ||
1586 (strcmp(name, "system.posix_acl_access") == 0))) {
1587 ret = convert_posix_acl_to_disk_buffer(value, value_len,
1588 new_value, &value_len);
1591 } else if (value_len)
1592 memcpy(new_value, value, value_len);
1594 /* Imitate kernel behavior by skipping update if value is the same. */
1595 for (x = h->attrs; x < h->attrs + h->count; x++) {
1596 if (!strcmp(x->name, name)) {
1597 if (!x->ea_ino && x->value_len == value_len &&
1599 !memcmp(x->value, new_value, value_len))) {
1603 old_idx = x - h->attrs;
1608 ret = ext2fs_get_memzero(inode_size, &inode);
1611 ret = ext2fs_read_inode_full(fs, h->ino,
1612 (struct ext2_inode *)inode,
1616 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
1617 extra_isize = inode->i_extra_isize;
1618 if (extra_isize == 0) {
1619 extra_isize = fs->super->s_want_extra_isize;
1620 if (extra_isize == 0)
1621 extra_isize = sizeof(__u32);
1623 ibody_free = inode_size - EXT2_GOOD_OLD_INODE_SIZE;
1624 ibody_free -= extra_isize;
1625 /* Extended attribute magic and final null entry. */
1626 ibody_free -= sizeof(__u32) * 2;
1627 ibody_free -= space_used(h->attrs, h->ibody_count);
1631 /* Inline data can only go to ibody. */
1632 if (strcmp(name, "system.data") == 0) {
1633 if (h->ibody_count <= old_idx) {
1634 ret = EXT2_ET_FILESYSTEM_CORRUPTED;
1637 ret = xattr_array_update(h, name, new_value, value_len,
1639 0 /* block_free */, old_idx,
1646 block_free = fs->blocksize;
1647 block_free -= sizeof(struct ext2_ext_attr_header);
1648 /* Final null entry. */
1649 block_free -= sizeof(__u32);
1650 block_free -= space_used(h->attrs + h->ibody_count,
1651 h->count - h->ibody_count);
1653 if (ext2fs_has_feature_ea_inode(fs->super) &&
1654 value_len > EXT4_XATTR_MIN_LARGE_EA_SIZE(fs->blocksize))
1657 ret = xattr_array_update(h, name, new_value, value_len, ibody_free,
1658 block_free, old_idx, in_inode);
1659 if (ret == EXT2_ET_EA_NO_SPACE && !in_inode &&
1660 ext2fs_has_feature_ea_inode(fs->super))
1661 ret = xattr_array_update(h, name, new_value, value_len,
1662 ibody_free, block_free, old_idx, 1 /* in_inode */);
1667 ret = ext2fs_xattrs_write(h);
1670 ext2fs_free_mem(&inode);
1671 ext2fs_free_mem(&new_value);
1675 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1678 struct ext2_xattr *x;
1679 struct ext2_xattr *end = handle->attrs + handle->count;
1681 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1682 for (x = handle->attrs; x < end; x++) {
1683 if (strcmp(x->name, key) == 0) {
1684 ext2fs_free_mem(&x->name);
1685 ext2fs_free_mem(&x->value);
1687 xattr_inode_dec_ref(handle->fs, x->ea_ino);
1688 memmove(x, x + 1, (end - x - 1)*sizeof(*x));
1689 memset(end - 1, 0, sizeof(*end));
1690 if (x < handle->attrs + handle->ibody_count)
1691 handle->ibody_count--;
1693 return ext2fs_xattrs_write(handle);
1697 /* no key found, success! */
1701 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1702 struct ext2_xattr_handle **handle)
1704 struct ext2_xattr_handle *h;
1707 if (!ext2fs_has_feature_xattr(fs->super) &&
1708 !ext2fs_has_feature_inline_data(fs->super))
1709 return EXT2_ET_MISSING_EA_FEATURE;
1711 err = ext2fs_get_memzero(sizeof(*h), &h);
1715 h->magic = EXT2_ET_MAGIC_EA_HANDLE;
1717 err = ext2fs_get_arrayzero(h->capacity, sizeof(struct ext2_xattr),
1720 ext2fs_free_mem(&h);
1730 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle)
1732 struct ext2_xattr_handle *h = *handle;
1734 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1735 xattrs_free_keys(h);
1736 ext2fs_free_mem(&h->attrs);
1737 ext2fs_free_mem(handle);
1741 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count)
1743 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1744 *count = handle->count;
1748 errcode_t ext2fs_xattrs_flags(struct ext2_xattr_handle *handle,
1749 unsigned int *new_flags, unsigned int *old_flags)
1751 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1753 *old_flags = handle->flags;
1755 handle->flags = *new_flags;
1759 struct ext2_attr_info {
1766 struct ext2_attr_search {
1767 struct ext2_ext_attr_entry *first;
1770 struct ext2_ext_attr_entry *here;
1774 struct ext2_attr_ibody_find {
1776 struct ext2_attr_search s;
1779 struct ext2_attr_block_find {
1780 struct ext2_attr_search s;
1784 void ext2fs_attr_shift_entries(struct ext2_ext_attr_entry *entry,
1785 int value_offs_shift, char *to,
1788 struct ext2_ext_attr_entry *last = entry;
1790 /* Adjust the value offsets of the entries */
1791 for (; !EXT2_EXT_IS_LAST_ENTRY(last); last = EXT2_EXT_ATTR_NEXT(last)) {
1792 if (!last->e_value_inum && last->e_value_size) {
1793 last->e_value_offs = last->e_value_offs +
1797 /* Shift the entries by n bytes and zero freed space in inode */
1798 memmove(to, from, n);
1800 memset(from, 0, to - from);
1804 * This function returns the free space present in the inode or the EA block.
1805 * total is number of bytes taken up by the EA entries and is used to shift
1806 * the EAs in ext2fs_expand_extra_isize().
1808 int ext2fs_attr_free_space(struct ext2_ext_attr_entry *last,
1809 int *min_offs, char *base, int *total)
1811 for (; !EXT2_EXT_IS_LAST_ENTRY(last); last = EXT2_EXT_ATTR_NEXT(last)) {
1812 *total += EXT2_EXT_ATTR_LEN(last->e_name_len);
1813 if (!last->e_value_inum && last->e_value_size) {
1814 int offs = last->e_value_offs;
1815 if (offs < *min_offs)
1820 return *min_offs - ((char *)last - base) - sizeof(__u32);
1823 static errcode_t ext2fs_attr_check_names(struct ext2_ext_attr_entry *entry,
1826 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1827 struct ext2_ext_attr_entry *next = EXT2_EXT_ATTR_NEXT(entry);
1828 if ((char *)next >= end)
1829 return EXT2_ET_EA_BAD_ENTRIES;
1835 /* The unused parameter used to be the blocksize, but with in-inode xattrs
1836 * the xattr storage area size depends on where the xattrs are kept. Keep
1837 * this parameter for API/ABI compatibility, but it is not needed. */
1838 static errcode_t ext2fs_attr_find_entry(struct ext2_ext_attr_entry **pentry,
1839 int name_index, const char *name,
1840 int unused, int sorted)
1842 struct ext2_ext_attr_entry *entry;
1847 return EXT2_ET_EA_BAD_NAME;
1849 name_len = strlen(name);
1851 for (; !EXT2_EXT_IS_LAST_ENTRY(entry);
1852 entry = EXT2_EXT_ATTR_NEXT(entry)) {
1853 cmp = name_index - entry->e_name_index;
1855 cmp = name_len - entry->e_name_len;
1857 cmp = memcmp(name, entry->e_name, name_len);
1858 if (cmp <= 0 && (sorted || cmp == 0))
1863 return cmp ? EXT2_ET_EA_NAME_NOT_FOUND : 0;
1866 static errcode_t ext2fs_attr_block_find(ext2_filsys fs,struct ext2_inode *inode,
1867 struct ext2_attr_info *i,
1868 struct ext2_attr_block_find *bs)
1870 struct ext2_ext_attr_header *header;
1873 if (inode->i_file_acl) {
1874 /* The inode already has an extended attribute block. */
1875 error = ext2fs_get_mem(fs->blocksize, &bs->block);
1878 error = ext2fs_read_ext_attr(fs, inode->i_file_acl, bs->block);
1882 header = BHDR(bs->block);
1883 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1884 error = EXT2_ET_EA_BAD_MAGIC;
1888 /* Find the named attribute. */
1889 bs->s.base = bs->block;
1890 bs->s.first = (struct ext2_ext_attr_entry *)(header + 1);
1891 bs->s.end = bs->block + fs->blocksize;
1892 bs->s.here = bs->s.first;
1893 error = ext2fs_attr_find_entry(&bs->s.here, i->name_index,
1894 i->name, fs->blocksize, 1);
1895 if (error && error != EXT2_ET_EA_NAME_NOT_FOUND)
1897 bs->s.not_found = error;
1902 if (error && bs->block)
1903 ext2fs_free_mem(&bs->block);
1907 static errcode_t ext2fs_attr_ibody_find(ext2_filsys fs,
1908 struct ext2_inode_large *inode,
1909 struct ext2_attr_info *i,
1910 struct ext2_attr_ibody_find *is)
1916 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1919 if (inode->i_extra_isize == 0)
1921 eamagic = IHDR(inode);
1923 start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
1924 inode->i_extra_isize + sizeof(__u32);
1925 is->s.first = (struct ext2_ext_attr_entry *)start;
1927 is->s.here = is->s.first;
1928 is->s.end = (char *)inode + EXT2_INODE_SIZE(fs->super);
1929 if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
1930 error = ext2fs_attr_check_names((struct ext2_ext_attr_entry *)
1934 /* Find the named attribute. */
1935 error = ext2fs_attr_find_entry(&is->s.here, i->name_index,
1936 i->name, is->s.end -
1937 (char *)is->s.base, 0);
1938 if (error && error != EXT2_ET_EA_NAME_NOT_FOUND)
1940 is->s.not_found = error;
1946 static errcode_t ext2fs_attr_set_entry(ext2_filsys fs, struct ext2_attr_info *i,
1947 struct ext2_attr_search *s)
1949 struct ext2_ext_attr_entry *last;
1950 int free, min_offs = s->end - s->base, name_len = strlen(i->name);
1952 /* Compute min_offs and last. */
1953 for (last = s->first; !EXT2_EXT_IS_LAST_ENTRY(last);
1954 last = EXT2_EXT_ATTR_NEXT(last)) {
1955 if (!last->e_value_inum && last->e_value_size) {
1956 int offs = last->e_value_offs;
1958 if (offs < min_offs)
1962 free = min_offs - ((char *)last - s->base) - sizeof(__u32);
1964 if (!s->not_found) {
1965 if (!s->here->e_value_inum && s->here->e_value_size) {
1966 int size = s->here->e_value_size;
1967 free += EXT2_EXT_ATTR_SIZE(size);
1969 free += EXT2_EXT_ATTR_LEN(name_len);
1972 if (free < EXT2_EXT_ATTR_LEN(name_len) +
1973 EXT2_EXT_ATTR_SIZE(i->value_len))
1974 return EXT2_ET_EA_NO_SPACE;
1977 if (i->value && s->not_found) {
1978 /* Insert the new name. */
1979 int size = EXT2_EXT_ATTR_LEN(name_len);
1980 int rest = (char *)last - (char *)s->here + sizeof(__u32);
1982 memmove((char *)s->here + size, s->here, rest);
1983 memset(s->here, 0, size);
1984 s->here->e_name_index = i->name_index;
1985 s->here->e_name_len = name_len;
1986 memcpy(s->here->e_name, i->name, name_len);
1988 if (!s->here->e_value_inum && s->here->e_value_size) {
1989 char *first_val = s->base + min_offs;
1990 int offs = s->here->e_value_offs;
1991 char *val = s->base + offs;
1992 int size = EXT2_EXT_ATTR_SIZE(s->here->e_value_size);
1995 size == EXT2_EXT_ATTR_SIZE(i->value_len)) {
1996 /* The old and the new value have the same
1997 size. Just replace. */
1998 s->here->e_value_size = i->value_len;
1999 memset(val + size - EXT2_EXT_ATTR_PAD, 0,
2000 EXT2_EXT_ATTR_PAD); /* Clear pad bytes */
2001 memcpy(val, i->value, i->value_len);
2005 /* Remove the old value. */
2006 memmove(first_val + size, first_val, val - first_val);
2007 memset(first_val, 0, size);
2008 s->here->e_value_size = 0;
2009 s->here->e_value_offs = 0;
2012 /* Adjust all value offsets. */
2014 while (!EXT2_EXT_IS_LAST_ENTRY(last)) {
2015 int o = last->e_value_offs;
2017 if (!last->e_value_inum &&
2018 last->e_value_size && o < offs)
2019 last->e_value_offs = o + size;
2020 last = EXT2_EXT_ATTR_NEXT(last);
2024 /* Remove the old name. */
2025 int size = EXT2_EXT_ATTR_LEN(name_len);
2027 last = ENTRY((char *)last - size);
2028 memmove((char *)s->here, (char *)s->here + size,
2029 (char *)last - (char *)s->here + sizeof(__u32));
2030 memset(last, 0, size);
2035 /* Insert the new value. */
2036 s->here->e_value_size = i->value_len;
2038 int size = EXT2_EXT_ATTR_SIZE(i->value_len);
2039 char *val = s->base + min_offs - size;
2041 s->here->e_value_offs = min_offs - size;
2042 memset(val + size - EXT2_EXT_ATTR_PAD, 0,
2043 EXT2_EXT_ATTR_PAD); /* Clear the pad bytes. */
2044 memcpy(val, i->value, i->value_len);
2051 static errcode_t ext2fs_attr_block_set(ext2_filsys fs, struct ext2_inode *inode,
2052 struct ext2_attr_info *i,
2053 struct ext2_attr_block_find *bs)
2055 struct ext2_attr_search *s = &bs->s;
2056 char *new_buf = NULL, *old_block = NULL;
2061 if (i->value && i->value_len > fs->blocksize)
2062 return EXT2_ET_EA_NO_SPACE;
2065 if (BHDR(s->base)->h_refcount != 1) {
2066 int offset = (char *)s->here - bs->block;
2068 /* Decrement the refcount of the shared block */
2069 old_block = s->base;
2070 BHDR(s->base)->h_refcount -= 1;
2072 error = ext2fs_get_mem(fs->blocksize, &s->base);
2076 memcpy(s->base, bs->block, fs->blocksize);
2077 s->first = ENTRY(BHDR(s->base)+1);
2078 BHDR(s->base)->h_refcount = 1;
2079 s->here = ENTRY(s->base + offset);
2080 s->end = s->base + fs->blocksize;
2083 error = ext2fs_get_mem(fs->blocksize, &s->base);
2087 memset(s->base, 0, fs->blocksize);
2088 BHDR(s->base)->h_magic = EXT2_EXT_ATTR_MAGIC;
2089 BHDR(s->base)->h_blocks = 1;
2090 BHDR(s->base)->h_refcount = 1;
2091 s->first = ENTRY(BHDR(s->base)+1);
2092 s->here = ENTRY(BHDR(s->base)+1);
2093 s->end = s->base + fs->blocksize;
2096 error = ext2fs_attr_set_entry(fs, i, s);
2100 if (!EXT2_EXT_IS_LAST_ENTRY(s->first))
2101 ext2fs_attr_rehash(BHDR(s->base), s->here);
2103 if (!EXT2_EXT_IS_LAST_ENTRY(s->first)) {
2104 if (bs->block && bs->block == s->base) {
2105 /* We are modifying this block in-place */
2106 new_buf = bs->block;
2107 blk = inode->i_file_acl;
2108 error = ext2fs_write_ext_attr(fs, blk, s->base);
2112 /* We need to allocate a new block */
2113 error = ext2fs_new_block(fs, 0, 0, &blk);
2116 ext2fs_block_alloc_stats(fs, blk, 1);
2117 error = ext2fs_write_ext_attr(fs, blk, s->base);
2122 BHDR(s->base)->h_refcount -= 1;
2123 error = ext2fs_write_ext_attr(fs,
2132 /* Update the i_blocks if we added a new EA block */
2133 if (!inode->i_file_acl && new_buf)
2134 inode->i_blocks += fs->blocksize / 512;
2135 /* Update the inode. */
2136 inode->i_file_acl = new_buf ? blk : 0;
2140 ext2fs_free_mem(&s->base);
2144 static errcode_t ext2fs_attr_ibody_set(ext2_filsys fs,
2145 struct ext2_inode_large *inode,
2146 struct ext2_attr_info *i,
2147 struct ext2_attr_ibody_find *is)
2150 struct ext2_attr_search *s = &is->s;
2153 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
2154 return EXT2_ET_EA_NO_SPACE;
2156 error = ext2fs_attr_set_entry(fs, i, s);
2160 eamagic = IHDR(inode);
2161 if (!EXT2_EXT_IS_LAST_ENTRY(s->first))
2162 *eamagic = EXT2_EXT_ATTR_MAGIC;
2166 return ext2fs_write_inode_full(fs, is->ino, (struct ext2_inode *)inode,
2167 EXT2_INODE_SIZE(fs->super));
2173 } ext2_attr_index_prefix[] = {
2174 [EXT2_ATTR_INDEX_USER] = { EXT2_ATTR_INDEX_USER_PREFIX,
2175 sizeof(EXT2_ATTR_INDEX_USER_PREFIX) },
2176 [EXT2_ATTR_INDEX_POSIX_ACL_ACCESS] = {
2177 EXT2_ATTR_INDEX_POSIX_ACL_ACCESS_PREFIX,
2178 sizeof(EXT2_ATTR_INDEX_POSIX_ACL_ACCESS_PREFIX) },
2179 [EXT2_ATTR_INDEX_POSIX_ACL_DEFAULT] = {
2180 EXT2_ATTR_INDEX_POSIX_ACL_DEFAULT_PREFIX,
2181 sizeof(EXT2_ATTR_INDEX_POSIX_ACL_DEFAULT_PREFIX) },
2182 [EXT2_ATTR_INDEX_TRUSTED] = { EXT2_ATTR_INDEX_TRUSTED_PREFIX,
2183 sizeof(EXT2_ATTR_INDEX_TRUSTED_PREFIX) },
2184 [EXT2_ATTR_INDEX_LUSTRE] = { EXT2_ATTR_INDEX_LUSTRE_PREFIX,
2185 sizeof(EXT2_ATTR_INDEX_LUSTRE_PREFIX) },
2186 [EXT2_ATTR_INDEX_SECURITY] = { EXT2_ATTR_INDEX_SECURITY_PREFIX,
2187 sizeof(EXT2_ATTR_INDEX_SECURITY_PREFIX)},
2191 errcode_t ext2fs_attr_set(ext2_filsys fs, ext2_ino_t ino,
2192 struct ext2_inode *inode,
2193 int name_index, const char *name, const char *value,
2194 int value_len, int flags)
2196 struct ext2_inode_large *inode_large = NULL;
2197 struct ext2_attr_info i = {
2198 .name_index = name_index,
2201 .value_len = value_len,
2203 struct ext2_attr_ibody_find is = {
2205 .s = { .not_found = -ENODATA, },
2207 struct ext2_attr_block_find bs = {
2208 .s = { .not_found = -ENODATA, },
2213 return EXT2_ET_EA_BAD_NAME;
2214 if (strlen(name) > 255)
2215 return EXT2_ET_EA_NAME_TOO_BIG;
2217 /* If the prefix is still present, skip it */
2218 if (strncmp(name, ext2_attr_index_prefix[name_index].str,
2219 ext2_attr_index_prefix[name_index].len) == 0)
2220 i.name += ext2_attr_index_prefix[name_index].len;
2222 if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE) {
2223 inode_large = (struct ext2_inode_large *)inode;
2225 error = ext2fs_attr_ibody_find(fs, inode_large, &i, &is);
2229 if (is.s.not_found) {
2230 error = ext2fs_attr_block_find(fs, inode, &i, &bs);
2235 if (is.s.not_found && bs.s.not_found) {
2236 error = EXT2_ET_EA_NAME_NOT_FOUND;
2237 if (flags & XATTR_REPLACE)
2243 error = EXT2_ET_EA_NAME_EXISTS;
2244 if (flags & XATTR_CREATE)
2249 if (!is.s.not_found &&
2250 (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE))
2251 error = ext2fs_attr_ibody_set(fs, inode_large, &i, &is);
2252 else if (!bs.s.not_found)
2253 error = ext2fs_attr_block_set(fs, inode, &i, &bs);
2255 if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
2256 error = ext2fs_attr_ibody_set(fs, inode_large, &i, &is);
2257 if (!error && !bs.s.not_found) {
2259 error = ext2fs_attr_block_set(fs, inode, &i, &bs);
2260 } else if (error == EXT2_ET_EA_NO_SPACE) {
2261 error = ext2fs_attr_block_set(fs, inode, &i, &bs);
2264 if (!is.s.not_found) {
2266 if (EXT2_INODE_SIZE(fs->super) >
2267 EXT2_GOOD_OLD_INODE_SIZE)
2268 error = ext2fs_attr_ibody_set(fs,
2269 inode_large, &i, &is);
2278 static errcode_t ext2fs_attr_check_block(ext2_filsys fs, char *buffer)
2280 if (BHDR(buffer)->h_magic != (EXT2_EXT_ATTR_MAGIC) ||
2281 BHDR(buffer)->h_blocks != 1)
2282 return EXT2_ET_EA_BAD_MAGIC;
2284 return ext2fs_attr_check_names((struct ext2_ext_attr_entry *)
2286 buffer + fs->blocksize);
2289 static errcode_t ext2fs_attr_block_get(ext2_filsys fs, struct ext2_inode *inode,
2290 int name_index, const char *name,
2291 void *buffer, size_t buffer_size,
2294 struct ext2_ext_attr_header *header = NULL;
2295 struct ext2_ext_attr_entry *entry;
2296 char *block_buf = NULL;
2299 error = EXT2_ET_EA_NAME_NOT_FOUND;
2300 if (!inode->i_file_acl)
2303 error = ext2fs_get_mem(fs->blocksize, &block_buf);
2306 error = ext2fs_read_ext_attr(fs, inode->i_file_acl, block_buf);
2310 error = ext2fs_attr_check_block(fs, block_buf);
2314 header = BHDR(block_buf);
2315 entry = (struct ext2_ext_attr_entry *)(header + 1);
2316 error = ext2fs_attr_find_entry(&entry, name_index, name,
2321 *easize = entry->e_value_size;
2323 if (entry->e_value_size > buffer_size) {
2324 error = EXT2_ET_EA_TOO_BIG;
2327 memcpy(buffer, block_buf + entry->e_value_offs,
2328 entry->e_value_size);
2333 ext2fs_free_mem(&block_buf);
2337 static errcode_t ext2fs_attr_ibody_get(ext2_filsys fs,
2338 struct ext2_inode_large *inode,
2339 int name_index, const char *name,
2340 void *buffer, size_t buffer_size,
2343 struct ext2_ext_attr_entry *entry;
2348 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
2349 return EXT2_ET_EA_NAME_NOT_FOUND;
2351 eamagic = IHDR(inode);
2352 error = ext2fs_attr_check_block(fs, buffer);
2356 start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
2357 inode->i_extra_isize + sizeof(__u32);
2358 entry = (struct ext2_ext_attr_entry *)start;
2359 end = (char *)inode + EXT2_INODE_SIZE(fs->super);
2360 error = ext2fs_attr_check_names(entry, end);
2363 error = ext2fs_attr_find_entry(&entry, name_index, name,
2364 end - (char *)entry, 0);
2368 *easize = entry->e_value_size;
2370 if (entry->e_value_size > buffer_size) {
2371 error = EXT2_ET_EA_TOO_BIG;
2374 memcpy(buffer, start + entry->e_value_offs,entry->e_value_size);
2382 errcode_t ext2fs_attr_get(ext2_filsys fs, struct ext2_inode *inode,
2383 int name_index, const char *name, char *buffer,
2384 size_t buffer_size, int *easize)
2388 error = ext2fs_attr_ibody_get(fs, (struct ext2_inode_large *)inode,
2389 name_index, name, buffer, buffer_size,
2391 if (error == EXT2_ET_EA_NAME_NOT_FOUND || error == EXT2_ET_EA_BAD_MAGIC)
2392 error = ext2fs_attr_block_get(fs, inode, name_index, name,
2393 buffer, buffer_size, easize);
2398 int ext2fs_attr_get_next_attr(struct ext2_ext_attr_entry *entry, int name_index,
2399 char *buffer, int buffer_size, int start)
2401 const int prefix_len = ext2_attr_index_prefix[name_index].len;
2404 if (!start && !EXT2_EXT_IS_LAST_ENTRY(entry))
2405 entry = EXT2_EXT_ATTR_NEXT(entry);
2407 for (; !EXT2_EXT_IS_LAST_ENTRY(entry);
2408 entry = EXT2_EXT_ATTR_NEXT(entry)) {
2411 if (name_index == entry->e_name_index)
2414 if (EXT2_EXT_IS_LAST_ENTRY(entry))
2417 total_len = prefix_len + entry->e_name_len + 1;
2418 if (buffer && total_len <= buffer_size) {
2419 memcpy(buffer, ext2_attr_index_prefix[name_index].str,
2421 memcpy(buffer + prefix_len, entry->e_name, entry->e_name_len);
2422 buffer[prefix_len + entry->e_name_len] = '\0';
2428 errcode_t ext2fs_expand_extra_isize(ext2_filsys fs, ext2_ino_t ino,
2429 struct ext2_inode_large *inode,
2430 int new_extra_isize, int *ret,
2433 struct ext2_inode *inode_buf = NULL;
2434 __u32 *eamagic = NULL;
2435 struct ext2_ext_attr_header *header = NULL;
2436 struct ext2_ext_attr_entry *entry = NULL, *last = NULL;
2437 struct ext2_attr_ibody_find is = {
2439 .s = { .not_found = EXT2_ET_EA_NO_SPACE, },
2441 struct ext2_attr_block_find bs = {
2442 .s = { .not_found = EXT2_ET_EA_NO_SPACE, },
2444 char *start, *end, *block_buf = NULL, *buffer =NULL, *b_entry_name=NULL;
2445 int total_ino = 0, total_blk, free, offs, tried_min_extra_isize = 0;
2446 int s_min_extra_isize = fs->super->s_min_extra_isize;
2447 errcode_t error = 0;
2450 *needed_size = new_extra_isize;
2451 error = ext2fs_get_mem(fs->blocksize, &block_buf);
2455 if (inode == NULL) {
2456 error = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode_buf);
2460 error = ext2fs_read_inode_full(fs, ino, inode_buf,
2461 EXT2_INODE_SIZE(fs->super));
2465 inode = (struct ext2_inode_large *)inode_buf;
2469 if (inode->i_extra_isize >= new_extra_isize)
2472 eamagic = IHDR(inode);
2473 start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2474 /* No extended attributes present */
2475 if (*eamagic != EXT2_EXT_ATTR_MAGIC) {
2477 EXT2_INODE_SIZE(fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
2478 inode->i_extra_isize);
2479 inode->i_extra_isize = new_extra_isize;
2485 start += sizeof(__u32);
2486 end = (char *)inode + EXT2_INODE_SIZE(fs->super);
2487 last = entry = (struct ext2_ext_attr_entry *)start;
2489 /* Consider space takenup by magic number */
2490 total_ino = sizeof(__u32);
2491 free = ext2fs_attr_free_space(last, &offs, start, &total_ino);
2493 /* Enough free space available in the inode for expansion */
2494 if (free >= new_extra_isize) {
2495 ext2fs_attr_shift_entries(entry,
2496 inode->i_extra_isize - new_extra_isize,
2498 EXT2_GOOD_OLD_INODE_SIZE +
2500 start - sizeof(__u32), total_ino);
2501 inode->i_extra_isize = new_extra_isize;
2507 if (inode->i_file_acl) {
2508 error = ext2fs_read_ext_attr(fs, inode->i_file_acl, block_buf);
2512 header = BHDR(block_buf);
2513 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
2514 error = EXT2_ET_EA_BAD_MAGIC;
2517 end = block_buf + fs->blocksize;
2518 last = entry = (struct ext2_ext_attr_entry *)(header+1);
2519 start = (char *)entry;
2521 free = ext2fs_attr_free_space(last, &offs, start, &total_blk);
2522 if (free < new_extra_isize) {
2523 if (!tried_min_extra_isize && s_min_extra_isize) {
2524 tried_min_extra_isize++;
2525 new_extra_isize = s_min_extra_isize;
2529 *ret = EXT2_EXPAND_EISIZE_NOSPC;
2530 error = EXT2_ET_EA_NO_SPACE;
2534 if (ret && *ret == EXT2_EXPAND_EISIZE_UNSAFE) {
2535 *ret = EXT2_EXPAND_EISIZE_NEW_BLOCK;
2539 free = fs->blocksize;
2542 while (new_extra_isize > 0) {
2543 int offs, size, entry_size;
2544 struct ext2_ext_attr_entry *small_entry = NULL;
2545 struct ext2_attr_info i = {
2549 unsigned int total_size, shift_bytes, temp = ~0U, extra_isize=0;
2551 start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
2552 inode->i_extra_isize + sizeof(__u32);
2553 end = (char *)inode + EXT2_INODE_SIZE(fs->super);
2554 last = (struct ext2_ext_attr_entry *)start;
2556 /* Find the entry best suited to be pushed into EA block */
2558 for (; !EXT2_EXT_IS_LAST_ENTRY(last);
2559 last = EXT2_EXT_ATTR_NEXT(last)) {
2560 total_size = EXT2_EXT_ATTR_SIZE(last->e_value_size) +
2561 EXT2_EXT_ATTR_LEN(last->e_name_len);
2562 if (total_size <= free && total_size < temp) {
2563 if (total_size < new_extra_isize) {
2572 if (entry == NULL) {
2574 entry = small_entry;
2576 if (!tried_min_extra_isize &&
2577 s_min_extra_isize) {
2578 tried_min_extra_isize++;
2579 new_extra_isize = s_min_extra_isize;
2583 *ret = EXT2_EXPAND_EISIZE_NOSPC;
2584 error = EXT2_ET_EA_NO_SPACE;
2588 offs = entry->e_value_offs;
2589 size = entry->e_value_size;
2590 entry_size = EXT2_EXT_ATTR_LEN(entry->e_name_len);
2591 i.name_index = entry->e_name_index;
2592 error = ext2fs_get_mem(EXT2_EXT_ATTR_SIZE(size), &buffer);
2595 error = ext2fs_get_mem(entry->e_name_len + 1, &b_entry_name);
2598 /* Save the entry name and the entry value */
2599 memcpy((char *)buffer, (char *)start + offs,
2600 EXT2_EXT_ATTR_SIZE(size));
2601 memcpy((char *)b_entry_name, (char *)entry->e_name,
2603 b_entry_name[entry->e_name_len] = '\0';
2604 i.name = b_entry_name;
2606 error = ext2fs_attr_ibody_find(fs, inode, &i, &is);
2610 error = ext2fs_attr_set_entry(fs, &i, &is.s);
2614 entry = (struct ext2_ext_attr_entry *)start;
2615 if (entry_size + EXT2_EXT_ATTR_SIZE(size) >= new_extra_isize)
2616 shift_bytes = new_extra_isize;
2618 shift_bytes = entry_size + EXT2_EXT_ATTR_SIZE(size);
2619 ext2fs_attr_shift_entries(entry,
2620 inode->i_extra_isize - shift_bytes,
2621 (char *)inode +EXT2_GOOD_OLD_INODE_SIZE+
2622 extra_isize + shift_bytes,
2623 start - sizeof(__u32),
2624 total_ino - entry_size);
2626 extra_isize += shift_bytes;
2627 new_extra_isize -= shift_bytes;
2629 *needed_size = new_extra_isize;
2630 inode->i_extra_isize = extra_isize;
2632 i.name = b_entry_name;
2635 error = ext2fs_attr_block_find(fs, (struct ext2_inode *)inode,
2640 /* Add entry which was removed from the inode into the block */
2641 error = ext2fs_attr_block_set(fs, (struct ext2_inode *)inode,
2648 error = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)inode,
2649 EXT2_INODE_SIZE(fs->super));
2652 ext2fs_free_mem(&inode_buf);
2654 ext2fs_free_mem(&block_buf);
2656 ext2fs_free_mem(&buffer);
2658 ext2fs_free_mem(&b_entry_name);