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"},
389 static const char *find_ea_prefix(int index)
391 struct ea_name_index *e;
393 for (e = ea_names; e->name; e++)
394 if (e->index == index)
400 static int find_ea_index(const char *fullname, const char **name, int *index)
402 struct ea_name_index *e;
404 for (e = ea_names; e->name; e++) {
405 if (strncmp(fullname, e->name, strlen(e->name)) == 0) {
406 *name = fullname + strlen(e->name);
414 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
415 struct ext2_inode_large *inode)
417 struct ext2_ext_attr_header *header;
418 void *block_buf = NULL;
421 struct ext2_inode_large i;
425 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&i,
426 sizeof(struct ext2_inode_large));
432 /* Do we already have an EA block? */
433 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
437 /* Find block, zero it, write back */
438 if ((blk < fs->super->s_first_data_block) ||
439 (blk >= ext2fs_blocks_count(fs->super))) {
440 err = EXT2_ET_BAD_EA_BLOCK_NUM;
444 err = ext2fs_get_mem(fs->blocksize, &block_buf);
448 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
452 /* We only know how to deal with v2 EA blocks */
453 header = (struct ext2_ext_attr_header *) block_buf;
454 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
455 err = EXT2_ET_BAD_EA_HEADER;
459 header->h_refcount--;
460 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
464 /* Erase link to block */
465 ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, 0);
466 if (header->h_refcount == 0)
467 ext2fs_block_alloc_stats2(fs, blk, -1);
468 err = ext2fs_iblk_sub_blocks(fs, (struct ext2_inode *)inode, 1);
474 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&i,
475 sizeof(struct ext2_inode_large));
481 ext2fs_free_mem(&block_buf);
486 static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
487 struct ext2_inode_large *inode)
489 struct ext2_ext_attr_header *header;
490 void *block_buf = NULL;
494 /* Do we already have an EA block? */
495 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
497 if ((blk < fs->super->s_first_data_block) ||
498 (blk >= ext2fs_blocks_count(fs->super))) {
499 err = EXT2_ET_BAD_EA_BLOCK_NUM;
503 err = ext2fs_get_mem(fs->blocksize, &block_buf);
507 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
511 /* We only know how to deal with v2 EA blocks */
512 header = (struct ext2_ext_attr_header *) block_buf;
513 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
514 err = EXT2_ET_BAD_EA_HEADER;
518 /* Single-user block. We're done here. */
519 if (header->h_refcount == 1)
522 /* We need to CoW the block. */
523 header->h_refcount--;
524 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
528 /* No block, we must increment i_blocks */
529 err = ext2fs_iblk_add_blocks(fs, (struct ext2_inode *)inode,
535 /* Allocate a block */
536 goal = ext2fs_find_inode_goal(fs, ino, (struct ext2_inode *)inode, 0);
537 err = ext2fs_alloc_block2(fs, goal, NULL, &blk);
540 ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, blk);
543 ext2fs_free_mem(&block_buf);
550 posix_acl_xattr_count(size_t size)
552 if (size < sizeof(posix_acl_xattr_header))
554 size -= sizeof(posix_acl_xattr_header);
555 if (size % sizeof(posix_acl_xattr_entry))
557 return size / sizeof(posix_acl_xattr_entry);
561 * The lgetxattr function returns data formatted in the POSIX extended
562 * attribute format. The on-disk format uses a more compact encoding.
563 * See the ext4_acl_to_disk in fs/ext4/acl.c.
565 static errcode_t convert_posix_acl_to_disk_buffer(const void *value, size_t size,
566 void *out_buf, size_t *size_out)
568 const posix_acl_xattr_header *header =
569 (const posix_acl_xattr_header*) value;
570 const posix_acl_xattr_entry *end, *entry =
571 (const posix_acl_xattr_entry *)(header+1);
572 ext4_acl_header *ext_acl;
580 if (size < sizeof(posix_acl_xattr_header))
582 if (header->a_version != ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION))
585 count = posix_acl_xattr_count(size);
587 ext_acl->a_version = ext2fs_cpu_to_le32(EXT4_ACL_VERSION);
592 e = (char *) out_buf + sizeof(ext4_acl_header);
593 s = sizeof(ext4_acl_header);
594 for (end = entry + count; entry != end;entry++) {
595 ext4_acl_entry *disk_entry = (ext4_acl_entry*) e;
596 disk_entry->e_tag = ext2fs_cpu_to_le16(entry->e_tag);
597 disk_entry->e_perm = ext2fs_cpu_to_le16(entry->e_perm);
599 switch(entry->e_tag) {
604 e += sizeof(ext4_acl_entry_short);
605 s += sizeof(ext4_acl_entry_short);
609 disk_entry->e_id = ext2fs_cpu_to_le32(entry->e_id);
610 e += sizeof(ext4_acl_entry);
611 s += sizeof(ext4_acl_entry);
619 static errcode_t convert_disk_buffer_to_posix_acl(const void *value, size_t size,
620 void **out_buf, size_t *size_out)
622 posix_acl_xattr_header *header;
623 posix_acl_xattr_entry *entry;
624 const ext4_acl_header *ext_acl = (const ext4_acl_header *) value;
630 (size < sizeof(ext4_acl_header)) ||
631 (ext_acl->a_version != ext2fs_cpu_to_le32(EXT4_ACL_VERSION)))
634 err = ext2fs_get_mem(size * 2, &out);
638 header = (posix_acl_xattr_header *) out;
639 header->a_version = ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION);
640 entry = (posix_acl_xattr_entry *) (out + sizeof(posix_acl_xattr_header));
642 cp = (const char *) value + sizeof(ext4_acl_header);
643 size -= sizeof(ext4_acl_header);
646 const ext4_acl_entry *disk_entry = (const ext4_acl_entry *) cp;
648 entry->e_tag = ext2fs_le16_to_cpu(disk_entry->e_tag);
649 entry->e_perm = ext2fs_le16_to_cpu(disk_entry->e_perm);
651 switch(entry->e_tag) {
657 cp += sizeof(ext4_acl_entry_short);
658 size -= sizeof(ext4_acl_entry_short);
662 entry->e_id = ext2fs_le32_to_cpu(disk_entry->e_id);
663 cp += sizeof(ext4_acl_entry);
664 size -= sizeof(ext4_acl_entry);
667 ext2fs_free_mem(&out);
673 *size_out = ((char *) entry - out);
678 write_xattrs_to_buffer(ext2_filsys fs, struct ext2_xattr *attrs, int count,
679 void *entries_start, unsigned int storage_size,
680 unsigned int value_offset_correction, int write_hash)
682 struct ext2_xattr *x;
683 struct ext2_ext_attr_entry *e = entries_start;
684 char *end = (char *) entries_start + storage_size;
685 unsigned int value_size;
688 memset(entries_start, 0, storage_size);
689 for (x = attrs; x < attrs + count; x++) {
690 value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
691 EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
693 /* Fill out e appropriately */
694 e->e_name_len = strlen(x->short_name);
695 e->e_name_index = x->name_index;
697 e->e_value_size = x->value_len;
698 e->e_value_inum = x->ea_ino;
701 memcpy((char *)e + sizeof(*e), x->short_name, e->e_name_len);
706 e->e_value_offs = end - (char *) entries_start +
707 value_offset_correction;
708 memcpy(end, x->value, e->e_value_size);
711 if (write_hash || x->ea_ino) {
712 err = ext2fs_ext_attr_hash_entry2(fs, e,
720 e = EXT2_EXT_ATTR_NEXT(e);
726 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
728 ext2_filsys fs = handle->fs;
729 const unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
730 struct ext2_inode_large *inode;
731 char *start, *block_buf = NULL;
732 struct ext2_ext_attr_header *header;
733 __u32 ea_inode_magic;
735 unsigned int storage_size;
739 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
741 if (i < sizeof(*inode))
743 err = ext2fs_get_memzero(i, &inode);
747 err = ext2fs_read_inode_full(fs, handle->ino, EXT2_INODE(inode),
752 /* If extra_isize isn't set, we need to set it now */
753 if (inode->i_extra_isize == 0 &&
754 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
755 char *p = (char *)inode;
756 size_t extra = fs->super->s_want_extra_isize;
759 extra = sizeof(__u32);
760 memset(p + EXT2_GOOD_OLD_INODE_SIZE, 0, extra);
761 inode->i_extra_isize = extra;
763 if (inode->i_extra_isize & 3) {
764 err = EXT2_ET_INODE_CORRUPTED;
768 /* Does the inode have space for EA? */
769 if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
770 inode_size <= EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize +
774 /* Write the inode EA */
775 ea_inode_magic = EXT2_EXT_ATTR_MAGIC;
776 memcpy(((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
777 inode->i_extra_isize, &ea_inode_magic, sizeof(__u32));
778 storage_size = inode_size - EXT2_GOOD_OLD_INODE_SIZE -
779 inode->i_extra_isize - sizeof(__u32);
780 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
781 inode->i_extra_isize + sizeof(__u32);
783 err = write_xattrs_to_buffer(fs, handle->attrs, handle->ibody_count,
784 start, storage_size, 0, 0);
789 if (handle->ibody_count == handle->count &&
790 !ext2fs_file_acl_block(fs, EXT2_INODE(inode)))
793 /* Write the EA block */
794 err = ext2fs_get_memzero(fs->blocksize, &block_buf);
798 storage_size = fs->blocksize - sizeof(struct ext2_ext_attr_header);
799 start = block_buf + sizeof(struct ext2_ext_attr_header);
801 err = write_xattrs_to_buffer(fs, handle->attrs + handle->ibody_count,
802 handle->count - handle->ibody_count, start,
803 storage_size, start - block_buf, 1);
807 /* Write a header on the EA block */
808 header = (struct ext2_ext_attr_header *) block_buf;
809 header->h_magic = EXT2_EXT_ATTR_MAGIC;
810 header->h_refcount = 1;
811 header->h_blocks = 1;
813 /* Get a new block for writing */
814 err = prep_ea_block_for_write(fs, handle->ino, inode);
818 /* Finally, write the new EA block */
819 blk = ext2fs_file_acl_block(fs, EXT2_INODE(inode));
820 err = ext2fs_write_ext_attr3(fs, blk, block_buf, handle->ino);
825 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
826 if (!block_buf && blk) {
827 /* xattrs shrunk, free the block */
828 err = ext2fs_free_ext_attr(fs, handle->ino, inode);
833 /* Write the inode */
834 err = ext2fs_write_inode_full(fs, handle->ino, EXT2_INODE(inode),
840 ext2fs_free_mem(&block_buf);
842 ext2fs_free_mem(&inode);
846 static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
847 struct ext2_inode_large *inode,
848 struct ext2_ext_attr_entry *entries,
849 unsigned int storage_size,
852 struct ext2_xattr *x;
853 struct ext2_ext_attr_entry *entry, *end;
855 unsigned int remain, prefix_len;
857 unsigned int values_size = storage_size +
858 ((char *)entries - value_start);
862 remain = storage_size;
863 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
864 !EXT2_EXT_IS_LAST_ENTRY(end)) {
866 /* header eats this space */
867 remain -= sizeof(struct ext2_ext_attr_entry);
869 /* is attribute name valid? */
870 if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
871 return EXT2_ET_EA_BAD_NAME_LEN;
873 /* attribute len eats this space */
874 remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
875 end = EXT2_EXT_ATTR_NEXT(end);
879 remain = storage_size;
880 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
881 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
883 /* Allocate space for more attrs? */
884 if (handle->count == handle->capacity) {
885 err = ext2fs_xattrs_expand(handle, 4);
890 x = handle->attrs + handle->count;
892 /* header eats this space */
893 remain -= sizeof(struct ext2_ext_attr_entry);
895 /* attribute len eats this space */
896 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
899 prefix = find_ea_prefix(entry->e_name_index);
900 prefix_len = (prefix ? strlen(prefix) : 0);
901 err = ext2fs_get_memzero(entry->e_name_len + prefix_len + 1,
906 memcpy(x->name, prefix, prefix_len);
907 if (entry->e_name_len)
908 memcpy(x->name + prefix_len,
909 (char *)entry + sizeof(*entry),
911 x->short_name = x->name + prefix_len;
912 x->name_index = entry->e_name_index;
914 /* Check & copy value */
915 if (!ext2fs_has_feature_ea_inode(handle->fs->super) &&
916 entry->e_value_inum != 0)
917 return EXT2_ET_BAD_EA_BLOCK_NUM;
919 if (entry->e_value_inum == 0) {
920 if (entry->e_value_size > remain)
921 return EXT2_ET_EA_BAD_VALUE_SIZE;
923 if (entry->e_value_offs + entry->e_value_size > values_size)
924 return EXT2_ET_EA_BAD_VALUE_OFFSET;
926 if (entry->e_value_size > 0 &&
927 value_start + entry->e_value_offs <
928 (char *)end + sizeof(__u32))
929 return EXT2_ET_EA_BAD_VALUE_OFFSET;
931 remain -= entry->e_value_size;
933 err = ext2fs_get_mem(entry->e_value_size, &x->value);
936 memcpy(x->value, value_start + entry->e_value_offs,
937 entry->e_value_size);
939 struct ext2_inode *ea_inode;
942 if (entry->e_value_offs != 0)
943 return EXT2_ET_EA_BAD_VALUE_OFFSET;
945 if (entry->e_value_size > (64 * 1024))
946 return EXT2_ET_EA_BAD_VALUE_SIZE;
948 err = ext2fs_get_mem(entry->e_value_size, &x->value);
952 err = ext2fs_file_open(handle->fs, entry->e_value_inum,
957 ea_inode = ext2fs_file_get_inode(ea_file);
958 if ((ea_inode->i_flags & EXT4_INLINE_DATA_FL) ||
959 !(ea_inode->i_flags & EXT4_EA_INODE_FL) ||
960 ea_inode->i_links_count == 0)
961 err = EXT2_ET_EA_INODE_CORRUPTED;
962 else if ((__u64) ext2fs_file_get_size(ea_file) !=
964 err = EXT2_ET_EA_BAD_VALUE_SIZE;
966 err = ext2fs_file_read(ea_file, x->value,
967 entry->e_value_size, 0);
968 ext2fs_file_close(ea_file);
973 x->ea_ino = entry->e_value_inum;
974 x->value_len = entry->e_value_size;
976 /* e_hash may be 0 in older inode's ea */
977 if (entry->e_hash != 0) {
979 void *data = (entry->e_value_inum != 0) ?
980 0 : value_start + entry->e_value_offs;
982 err = ext2fs_ext_attr_hash_entry2(handle->fs, entry,
986 if (entry->e_hash != hash) {
987 struct ext2_inode child;
989 /* Check whether this is an old Lustre-style
990 * ea_inode reference.
992 err = ext2fs_read_inode(handle->fs,
997 if (child.i_mtime != handle->ino ||
998 child.i_generation != inode->i_generation)
999 return EXT2_ET_BAD_EA_HASH;
1004 entry = EXT2_EXT_ATTR_NEXT(entry);
1010 static void xattrs_free_keys(struct ext2_xattr_handle *h)
1012 struct ext2_xattr *a = h->attrs;
1015 for (i = 0; i < h->capacity; i++) {
1017 ext2fs_free_mem(&a[i].name);
1019 ext2fs_free_mem(&a[i].value);
1025 /* fetch xattrs from an already-loaded inode */
1026 errcode_t ext2fs_xattrs_read_inode(struct ext2_xattr_handle *handle,
1027 struct ext2_inode_large *inode)
1030 struct ext2_ext_attr_header *header;
1031 __u32 ea_inode_magic;
1032 unsigned int storage_size;
1033 char *start, *block_buf = NULL;
1037 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1039 xattrs_free_keys(handle);
1041 /* Does the inode have space for EA? */
1042 if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
1043 EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1044 inode->i_extra_isize +
1047 if (inode->i_extra_isize & 3) {
1048 err = EXT2_ET_INODE_CORRUPTED;
1052 /* Look for EA in the inode */
1053 memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1054 inode->i_extra_isize, sizeof(__u32));
1055 if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1056 storage_size = EXT2_INODE_SIZE(handle->fs->super) -
1057 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
1059 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1060 inode->i_extra_isize + sizeof(__u32);
1062 err = read_xattrs_from_buffer(handle, inode,
1063 (struct ext2_ext_attr_entry *) start,
1064 storage_size, start);
1068 handle->ibody_count = handle->count;
1072 /* Look for EA in a separate EA block */
1073 blk = ext2fs_file_acl_block(handle->fs, EXT2_INODE(inode));
1075 if ((blk < handle->fs->super->s_first_data_block) ||
1076 (blk >= ext2fs_blocks_count(handle->fs->super))) {
1077 err = EXT2_ET_BAD_EA_BLOCK_NUM;
1081 err = ext2fs_get_mem(handle->fs->blocksize, &block_buf);
1085 err = ext2fs_read_ext_attr3(handle->fs, blk, block_buf,
1090 /* We only know how to deal with v2 EA blocks */
1091 header = (struct ext2_ext_attr_header *) block_buf;
1092 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1093 err = EXT2_ET_BAD_EA_HEADER;
1098 storage_size = handle->fs->blocksize -
1099 sizeof(struct ext2_ext_attr_header);
1100 start = block_buf + sizeof(struct ext2_ext_attr_header);
1101 err = read_xattrs_from_buffer(handle, inode,
1102 (struct ext2_ext_attr_entry *) start,
1103 storage_size, block_buf);
1108 ext2fs_free_mem(&block_buf);
1113 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle)
1115 struct ext2_inode_large *inode;
1116 size_t inode_size = EXT2_INODE_SIZE(handle->fs->super);
1119 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1121 if (inode_size < sizeof(*inode))
1122 inode_size = sizeof(*inode);
1123 err = ext2fs_get_memzero(inode_size, &inode);
1127 err = ext2fs_read_inode_full(handle->fs, handle->ino, EXT2_INODE(inode),
1128 EXT2_INODE_SIZE(handle->fs->super));
1132 err = ext2fs_xattrs_read_inode(handle, inode);
1135 ext2fs_free_mem(&inode);
1140 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
1141 int (*func)(char *name, char *value,
1143 ext2_ino_t inode_num, void *data),
1146 struct ext2_xattr *x;
1150 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1151 for (x = h->attrs; x < h->attrs + h->count; x++) {
1152 ret = func(x->name, x->value, x->value_len, x->ea_ino, data);
1153 if (ret & XATTR_CHANGED)
1155 if (ret & XATTR_ABORT)
1160 return ext2fs_xattrs_write(h);
1164 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
1165 void **value, size_t *value_len)
1167 struct ext2_xattr *x;
1171 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1172 for (x = h->attrs; x < h->attrs + h->count; x++) {
1173 if (strcmp(x->name, key))
1176 if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1177 ((strcmp(key, "system.posix_acl_default") == 0) ||
1178 (strcmp(key, "system.posix_acl_access") == 0))) {
1179 err = convert_disk_buffer_to_posix_acl(x->value, x->value_len,
1183 err = ext2fs_get_mem(x->value_len, &val);
1186 memcpy(val, x->value, x->value_len);
1188 *value_len = x->value_len;
1193 return EXT2_ET_EA_KEY_NOT_FOUND;
1196 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
1199 struct ext2_ext_attr_entry *entry;
1200 struct ext2_inode_large *inode;
1201 __u32 ea_inode_magic;
1202 unsigned int minoff;
1207 i = EXT2_INODE_SIZE(fs->super);
1208 if (i < sizeof(*inode))
1210 err = ext2fs_get_memzero(i, &inode);
1214 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)inode,
1215 EXT2_INODE_SIZE(fs->super));
1219 /* Does the inode have size for EA? */
1220 if (EXT2_INODE_SIZE(fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1221 inode->i_extra_isize +
1223 err = EXT2_ET_INLINE_DATA_NO_SPACE;
1227 minoff = EXT2_INODE_SIZE(fs->super) - sizeof(*inode) - sizeof(__u32);
1228 memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1229 inode->i_extra_isize, sizeof(__u32));
1230 if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1231 /* has xattrs. calculate the size */
1232 start= ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1233 inode->i_extra_isize + sizeof(__u32);
1234 entry = (struct ext2_ext_attr_entry *) start;
1235 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1236 if (!entry->e_value_inum && entry->e_value_size) {
1237 unsigned int offs = entry->e_value_offs;
1241 entry = EXT2_EXT_ATTR_NEXT(entry);
1243 *size = minoff - ((char *)entry - (char *)start) - sizeof(__u32);
1245 /* no xattr. return a maximum size */
1246 *size = EXT2_EXT_ATTR_SIZE(minoff -
1247 EXT2_EXT_ATTR_LEN(strlen("data")) -
1248 EXT2_EXT_ATTR_ROUND - sizeof(__u32));
1252 ext2fs_free_mem(&inode);
1256 static errcode_t xattr_create_ea_inode(ext2_filsys fs, const void *value,
1257 size_t value_len, ext2_ino_t *ea_ino)
1259 struct ext2_inode inode;
1265 ret = ext2fs_new_inode(fs, 0, 0, 0, &ino);
1269 memset(&inode, 0, sizeof(inode));
1270 inode.i_flags |= EXT4_EA_INODE_FL;
1271 if (ext2fs_has_feature_extents(fs->super))
1272 inode.i_flags |= EXT4_EXTENTS_FL;
1274 inode.i_mode = LINUX_S_IFREG | 0600;
1275 inode.i_links_count = 1;
1276 ret = ext2fs_write_new_inode(fs, ino, &inode);
1280 * ref_count and hash utilize inode's i_*time fields.
1281 * ext2fs_write_new_inode() call above initializes these fields with
1282 * current time. That's why ref count and hash updates are done
1285 ext2fs_set_ea_inode_ref(&inode, 1);
1286 hash = ext2fs_crc32c_le(fs->csum_seed, value, value_len);
1287 ext2fs_set_ea_inode_hash(&inode, hash);
1289 ret = ext2fs_write_inode(fs, ino, &inode);
1293 ret = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
1296 ret = ext2fs_file_write(file, value, value_len, NULL);
1297 ext2fs_file_close(file);
1301 ext2fs_inode_alloc_stats2(fs, ino, 1 /* inuse */, 0 /* isdir */);
1307 static errcode_t xattr_inode_dec_ref(ext2_filsys fs, ext2_ino_t ino)
1309 struct ext2_inode_large inode;
1313 ret = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1318 ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(&inode));
1320 ext2fs_set_ea_inode_ref(EXT2_INODE(&inode), ref_count);
1325 inode.i_links_count = 0;
1326 inode.i_dtime = fs->now ? fs->now : time(0);
1328 ret = ext2fs_free_ext_attr(fs, ino, &inode);
1332 if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) {
1333 ret = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL,
1339 ext2fs_inode_alloc_stats2(fs, ino, -1 /* inuse */, 0 /* is_dir */);
1342 ret = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1348 static errcode_t xattr_update_entry(ext2_filsys fs, struct ext2_xattr *x,
1349 const char *name, const char *short_name,
1350 int index, const void *value,
1351 size_t value_len, int in_inode)
1353 ext2_ino_t ea_ino = 0;
1354 void *new_value = NULL;
1355 char *new_name = NULL;
1360 name_len = strlen(name);
1361 ret = ext2fs_get_mem(name_len + 1, &new_name);
1364 memcpy(new_name, name, name_len + 1);
1367 ret = ext2fs_get_mem(value_len, &new_value);
1370 memcpy(new_value, value, value_len);
1373 ret = xattr_create_ea_inode(fs, value, value_len, &ea_ino);
1379 ret = xattr_inode_dec_ref(fs, x->ea_ino);
1386 x->short_name = new_name + (short_name - name);
1388 x->name_index = index;
1391 ext2fs_free_mem(&x->value);
1392 x->value = new_value;
1393 x->value_len = value_len;
1398 ext2fs_free_mem(&new_name);
1400 ext2fs_free_mem(&new_value);
1402 xattr_inode_dec_ref(fs, ea_ino);
1406 static int xattr_find_position(struct ext2_xattr *attrs, int count,
1407 const char *shortname, int name_idx)
1409 struct ext2_xattr *x;
1411 int shortname_len, x_shortname_len;
1413 shortname_len = strlen(shortname);
1415 for (i = 0, x = attrs; i < count; i++, x++) {
1416 if (name_idx < x->name_index)
1418 if (name_idx > x->name_index)
1421 x_shortname_len = strlen(x->short_name);
1422 if (shortname_len < x_shortname_len)
1424 if (shortname_len > x_shortname_len)
1427 if (memcmp(shortname, x->short_name, shortname_len) <= 0)
1433 static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
1435 const void *value, size_t value_len,
1436 int ibody_free, int block_free,
1437 int old_idx, int in_inode)
1439 struct ext2_xattr tmp;
1442 int name_len, name_idx = 0;
1443 const char *shortname = name;
1447 find_ea_index(name, &shortname, &name_idx);
1448 name_len = strlen(shortname);
1450 needed = EXT2_EXT_ATTR_LEN(name_len);
1452 needed += EXT2_EXT_ATTR_SIZE(value_len);
1454 if (old_idx >= 0 && old_idx < h->ibody_count) {
1455 ibody_free += EXT2_EXT_ATTR_LEN(name_len);
1456 if (!h->attrs[old_idx].ea_ino)
1457 ibody_free += EXT2_EXT_ATTR_SIZE(
1458 h->attrs[old_idx].value_len);
1461 if (needed <= ibody_free) {
1463 new_idx = h->ibody_count;
1468 /* Update the existing entry. */
1469 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1470 shortname, name_idx, value,
1471 value_len, in_inode);
1474 if (h->ibody_count <= old_idx) {
1475 /* Move entry from block to the end of ibody. */
1476 tmp = h->attrs[old_idx];
1477 memmove(h->attrs + h->ibody_count + 1,
1478 h->attrs + h->ibody_count,
1479 (old_idx - h->ibody_count) * sizeof(*h->attrs));
1480 h->attrs[h->ibody_count] = tmp;
1486 if (h->ibody_count <= old_idx) {
1487 block_free += EXT2_EXT_ATTR_LEN(name_len);
1488 if (!h->attrs[old_idx].ea_ino)
1490 EXT2_EXT_ATTR_SIZE(h->attrs[old_idx].value_len);
1493 if (needed > block_free)
1494 return EXT2_ET_EA_NO_SPACE;
1497 /* Update the existing entry. */
1498 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1499 shortname, name_idx, value,
1500 value_len, in_inode);
1503 if (old_idx < h->ibody_count) {
1505 * Move entry from ibody to the block. Note that
1506 * entries in the block are sorted.
1508 new_idx = xattr_find_position(h->attrs + h->ibody_count,
1509 h->count - h->ibody_count,
1510 shortname, name_idx);
1511 new_idx += h->ibody_count - 1;
1512 tmp = h->attrs[old_idx];
1513 memmove(h->attrs + old_idx, h->attrs + old_idx + 1,
1514 (new_idx - old_idx) * sizeof(*h->attrs));
1515 h->attrs[new_idx] = tmp;
1521 new_idx = xattr_find_position(h->attrs + h->ibody_count,
1522 h->count - h->ibody_count,
1523 shortname, name_idx);
1524 new_idx += h->ibody_count;
1528 if (h->count == h->capacity) {
1529 ret = ext2fs_xattrs_expand(h, 4);
1534 ret = xattr_update_entry(h->fs, &h->attrs[h->count], name, shortname,
1535 name_idx, value, value_len, in_inode);
1539 tmp = h->attrs[h->count];
1540 memmove(h->attrs + new_idx + 1, h->attrs + new_idx,
1541 (h->count - new_idx)*sizeof(*h->attrs));
1542 h->attrs[new_idx] = tmp;
1549 static int space_used(struct ext2_xattr *attrs, int count)
1552 struct ext2_xattr *x;
1555 for (i = 0, x = attrs; i < count; i++, x++) {
1556 len = strlen(x->short_name);
1557 total += EXT2_EXT_ATTR_LEN(len);
1559 total += EXT2_EXT_ATTR_SIZE(x->value_len);
1565 * The minimum size of EA value when you start storing it in an external inode
1566 * size of block - size of header - size of 1 entry - 4 null bytes
1568 #define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1569 ((b) - EXT2_EXT_ATTR_LEN(3) - sizeof(struct ext2_ext_attr_header) - 4)
1571 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *h,
1576 ext2_filsys fs = h->fs;
1577 const int inode_size = EXT2_INODE_SIZE(fs->super);
1578 struct ext2_inode_large *inode = NULL;
1579 struct ext2_xattr *x;
1581 int ibody_free, block_free;
1587 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1589 ret = ext2fs_get_mem(value_len, &new_value);
1592 if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1593 ((strcmp(name, "system.posix_acl_default") == 0) ||
1594 (strcmp(name, "system.posix_acl_access") == 0))) {
1595 ret = convert_posix_acl_to_disk_buffer(value, value_len,
1596 new_value, &value_len);
1599 } else if (value_len)
1600 memcpy(new_value, value, value_len);
1602 /* Imitate kernel behavior by skipping update if value is the same. */
1603 for (x = h->attrs; x < h->attrs + h->count; x++) {
1604 if (!strcmp(x->name, name)) {
1605 if (!x->ea_ino && x->value_len == value_len &&
1607 !memcmp(x->value, new_value, value_len))) {
1611 old_idx = x - h->attrs;
1616 ret = ext2fs_get_memzero(inode_size, &inode);
1619 ret = ext2fs_read_inode_full(fs, h->ino,
1620 (struct ext2_inode *)inode,
1624 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
1625 extra_isize = inode->i_extra_isize;
1626 if (extra_isize == 0) {
1627 extra_isize = fs->super->s_want_extra_isize;
1628 if (extra_isize == 0)
1629 extra_isize = sizeof(__u32);
1631 ibody_free = inode_size - EXT2_GOOD_OLD_INODE_SIZE;
1632 ibody_free -= extra_isize;
1633 /* Extended attribute magic and final null entry. */
1634 ibody_free -= sizeof(__u32) * 2;
1635 ibody_free -= space_used(h->attrs, h->ibody_count);
1639 /* Inline data can only go to ibody. */
1640 if (strcmp(name, "system.data") == 0) {
1641 if (h->ibody_count <= old_idx) {
1642 ret = EXT2_ET_FILESYSTEM_CORRUPTED;
1645 ret = xattr_array_update(h, name, new_value, value_len,
1647 0 /* block_free */, old_idx,
1654 block_free = fs->blocksize;
1655 block_free -= sizeof(struct ext2_ext_attr_header);
1656 /* Final null entry. */
1657 block_free -= sizeof(__u32);
1658 block_free -= space_used(h->attrs + h->ibody_count,
1659 h->count - h->ibody_count);
1661 if (ext2fs_has_feature_ea_inode(fs->super) &&
1662 value_len > EXT4_XATTR_MIN_LARGE_EA_SIZE(fs->blocksize))
1665 ret = xattr_array_update(h, name, new_value, value_len, ibody_free,
1666 block_free, old_idx, in_inode);
1667 if (ret == EXT2_ET_EA_NO_SPACE && !in_inode &&
1668 ext2fs_has_feature_ea_inode(fs->super))
1669 ret = xattr_array_update(h, name, new_value, value_len,
1670 ibody_free, block_free, old_idx, 1 /* in_inode */);
1675 ret = ext2fs_xattrs_write(h);
1678 ext2fs_free_mem(&inode);
1679 ext2fs_free_mem(&new_value);
1683 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1686 struct ext2_xattr *x;
1687 struct ext2_xattr *end = handle->attrs + handle->count;
1689 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1690 for (x = handle->attrs; x < end; x++) {
1691 if (strcmp(x->name, key) == 0) {
1692 ext2fs_free_mem(&x->name);
1693 ext2fs_free_mem(&x->value);
1695 xattr_inode_dec_ref(handle->fs, x->ea_ino);
1696 memmove(x, x + 1, (end - x - 1)*sizeof(*x));
1697 memset(end - 1, 0, sizeof(*end));
1698 if (x < handle->attrs + handle->ibody_count)
1699 handle->ibody_count--;
1701 return ext2fs_xattrs_write(handle);
1705 /* no key found, success! */
1709 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1710 struct ext2_xattr_handle **handle)
1712 struct ext2_xattr_handle *h;
1715 if (!ext2fs_has_feature_xattr(fs->super) &&
1716 !ext2fs_has_feature_inline_data(fs->super))
1717 return EXT2_ET_MISSING_EA_FEATURE;
1719 err = ext2fs_get_memzero(sizeof(*h), &h);
1723 h->magic = EXT2_ET_MAGIC_EA_HANDLE;
1725 err = ext2fs_get_arrayzero(h->capacity, sizeof(struct ext2_xattr),
1728 ext2fs_free_mem(&h);
1738 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle)
1740 struct ext2_xattr_handle *h = *handle;
1742 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1743 xattrs_free_keys(h);
1744 ext2fs_free_mem(&h->attrs);
1745 ext2fs_free_mem(handle);
1749 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count)
1751 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1752 *count = handle->count;
1756 errcode_t ext2fs_xattrs_flags(struct ext2_xattr_handle *handle,
1757 unsigned int *new_flags, unsigned int *old_flags)
1759 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1761 *old_flags = handle->flags;
1763 handle->flags = *new_flags;
1767 struct ext2_attr_info {
1774 struct ext2_attr_search {
1775 struct ext2_ext_attr_entry *first;
1778 struct ext2_ext_attr_entry *here;
1782 struct ext2_attr_ibody_find {
1784 struct ext2_attr_search s;
1787 struct ext2_attr_block_find {
1788 struct ext2_attr_search s;
1792 void ext2fs_attr_shift_entries(struct ext2_ext_attr_entry *entry,
1793 int value_offs_shift, char *to,
1796 struct ext2_ext_attr_entry *last = entry;
1798 /* Adjust the value offsets of the entries */
1799 for (; !EXT2_EXT_IS_LAST_ENTRY(last); last = EXT2_EXT_ATTR_NEXT(last)) {
1800 if (!last->e_value_inum && last->e_value_size) {
1801 last->e_value_offs = last->e_value_offs +
1805 /* Shift the entries by n bytes and zero freed space in inode */
1806 memmove(to, from, n);
1808 memset(from, 0, to - from);
1812 * This function returns the free space present in the inode or the EA block.
1813 * total is number of bytes taken up by the EA entries and is used to shift
1814 * the EAs in ext2fs_expand_extra_isize().
1816 int ext2fs_attr_free_space(struct ext2_ext_attr_entry *last,
1817 int *min_offs, char *base, int *total)
1819 for (; !EXT2_EXT_IS_LAST_ENTRY(last); last = EXT2_EXT_ATTR_NEXT(last)) {
1820 *total += EXT2_EXT_ATTR_LEN(last->e_name_len);
1821 if (!last->e_value_inum && last->e_value_size) {
1822 int offs = last->e_value_offs;
1823 if (offs < *min_offs)
1828 return *min_offs - ((char *)last - base) - sizeof(__u32);
1831 static errcode_t ext2fs_attr_check_names(struct ext2_ext_attr_entry *entry,
1834 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1835 struct ext2_ext_attr_entry *next = EXT2_EXT_ATTR_NEXT(entry);
1836 if ((char *)next >= end)
1837 return EXT2_ET_EA_BAD_ENTRIES;
1843 /* The unused parameter used to be the blocksize, but with in-inode xattrs
1844 * the xattr storage area size depends on where the xattrs are kept. Keep
1845 * this parameter for API/ABI compatibility, but it is not needed. */
1846 static errcode_t ext2fs_attr_find_entry(struct ext2_ext_attr_entry **pentry,
1847 int name_index, const char *name,
1848 int unused, int sorted)
1850 struct ext2_ext_attr_entry *entry;
1855 return EXT2_ET_EA_BAD_NAME;
1857 name_len = strlen(name);
1859 for (; !EXT2_EXT_IS_LAST_ENTRY(entry);
1860 entry = EXT2_EXT_ATTR_NEXT(entry)) {
1861 cmp = name_index - entry->e_name_index;
1863 cmp = name_len - entry->e_name_len;
1865 cmp = memcmp(name, entry->e_name, name_len);
1866 if (cmp <= 0 && (sorted || cmp == 0))
1871 return cmp ? EXT2_ET_EA_NAME_NOT_FOUND : 0;
1874 static errcode_t ext2fs_attr_block_find(ext2_filsys fs,struct ext2_inode *inode,
1875 struct ext2_attr_info *i,
1876 struct ext2_attr_block_find *bs)
1878 struct ext2_ext_attr_header *header;
1881 if (inode->i_file_acl) {
1882 /* The inode already has an extended attribute block. */
1883 error = ext2fs_get_mem(fs->blocksize, &bs->block);
1886 error = ext2fs_read_ext_attr(fs, inode->i_file_acl, bs->block);
1890 header = BHDR(bs->block);
1891 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1892 error = EXT2_ET_EA_BAD_MAGIC;
1896 /* Find the named attribute. */
1897 bs->s.base = bs->block;
1898 bs->s.first = (struct ext2_ext_attr_entry *)(header + 1);
1899 bs->s.end = bs->block + fs->blocksize;
1900 bs->s.here = bs->s.first;
1901 error = ext2fs_attr_find_entry(&bs->s.here, i->name_index,
1902 i->name, fs->blocksize, 1);
1903 if (error && error != EXT2_ET_EA_NAME_NOT_FOUND)
1905 bs->s.not_found = error;
1910 if (error && bs->block)
1911 ext2fs_free_mem(&bs->block);
1915 static errcode_t ext2fs_attr_ibody_find(ext2_filsys fs,
1916 struct ext2_inode_large *inode,
1917 struct ext2_attr_info *i,
1918 struct ext2_attr_ibody_find *is)
1922 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1925 if (inode->i_extra_isize == 0)
1928 is->s.first = &IHDR(inode)->h_first_entry[0];
1929 is->s.base = (char *)is->s.first;
1930 is->s.here = is->s.first;
1931 is->s.end = (char *)inode + EXT2_INODE_SIZE(fs->super);
1932 if (IHDR(inode)->h_magic == EXT2_EXT_ATTR_MAGIC) {
1933 error = ext2fs_attr_check_names(is->s.first, is->s.end);
1936 /* Find the named attribute. */
1937 error = ext2fs_attr_find_entry(&is->s.here, i->name_index,
1938 i->name, is->s.end -
1939 (char *)is->s.base, 0);
1940 if (error && error != EXT2_ET_EA_NAME_NOT_FOUND)
1942 is->s.not_found = error;
1948 static errcode_t ext2fs_attr_set_entry(ext2_filsys fs, struct ext2_attr_info *i,
1949 struct ext2_attr_search *s)
1951 struct ext2_ext_attr_entry *last;
1952 int free, min_offs = s->end - s->base, name_len = strlen(i->name);
1954 /* Compute min_offs and last. */
1955 for (last = s->first; !EXT2_EXT_IS_LAST_ENTRY(last);
1956 last = EXT2_EXT_ATTR_NEXT(last)) {
1957 if (!last->e_value_inum && last->e_value_size) {
1958 int offs = last->e_value_offs;
1960 if (offs < min_offs)
1964 free = min_offs - ((char *)last - s->base) - sizeof(__u32);
1966 if (!s->not_found) {
1967 if (!s->here->e_value_inum && s->here->e_value_size) {
1968 int size = s->here->e_value_size;
1969 free += EXT2_EXT_ATTR_SIZE(size);
1971 free += EXT2_EXT_ATTR_LEN(name_len);
1974 if (free < EXT2_EXT_ATTR_LEN(name_len) +
1975 EXT2_EXT_ATTR_SIZE(i->value_len))
1976 return EXT2_ET_EA_NO_SPACE;
1979 if (i->value && s->not_found) {
1980 /* Insert the new name. */
1981 int size = EXT2_EXT_ATTR_LEN(name_len);
1982 int rest = (char *)last - (char *)s->here + sizeof(__u32);
1984 memmove((char *)s->here + size, s->here, rest);
1985 memset(s->here, 0, size);
1986 s->here->e_name_index = i->name_index;
1987 s->here->e_name_len = name_len;
1988 memcpy(s->here->e_name, i->name, name_len);
1990 if (!s->here->e_value_inum && s->here->e_value_size) {
1991 char *first_val = s->base + min_offs;
1992 int offs = s->here->e_value_offs;
1993 char *val = s->base + offs;
1994 int size = EXT2_EXT_ATTR_SIZE(s->here->e_value_size);
1997 size == EXT2_EXT_ATTR_SIZE(i->value_len)) {
1998 /* The old and the new value have the same
1999 size. Just replace. */
2000 s->here->e_value_size = i->value_len;
2001 memset(val + size - EXT2_EXT_ATTR_PAD, 0,
2002 EXT2_EXT_ATTR_PAD); /* Clear pad bytes */
2003 memcpy(val, i->value, i->value_len);
2007 /* Remove the old value. */
2008 memmove(first_val + size, first_val, val - first_val);
2009 memset(first_val, 0, size);
2010 s->here->e_value_size = 0;
2011 s->here->e_value_offs = 0;
2014 /* Adjust all value offsets. */
2016 while (!EXT2_EXT_IS_LAST_ENTRY(last)) {
2017 int o = last->e_value_offs;
2019 if (!last->e_value_inum &&
2020 last->e_value_size && o < offs)
2021 last->e_value_offs = o + size;
2022 last = EXT2_EXT_ATTR_NEXT(last);
2026 /* Remove the old name. */
2027 int size = EXT2_EXT_ATTR_LEN(name_len);
2029 last = ENTRY((char *)last - size);
2030 memmove((char *)s->here, (char *)s->here + size,
2031 (char *)last - (char *)s->here + sizeof(__u32));
2032 memset(last, 0, size);
2037 /* Insert the new value. */
2038 s->here->e_value_size = i->value_len;
2040 int size = EXT2_EXT_ATTR_SIZE(i->value_len);
2041 char *val = s->base + min_offs - size;
2043 s->here->e_value_offs = min_offs - size;
2044 memset(val + size - EXT2_EXT_ATTR_PAD, 0,
2045 EXT2_EXT_ATTR_PAD); /* Clear the pad bytes. */
2046 memcpy(val, i->value, i->value_len);
2053 static errcode_t ext2fs_attr_block_set(ext2_filsys fs, struct ext2_inode *inode,
2054 struct ext2_attr_info *i,
2055 struct ext2_attr_block_find *bs)
2057 struct ext2_attr_search *s = &bs->s;
2058 char *new_buf = NULL, *old_block = NULL;
2063 if (i->value && i->value_len > fs->blocksize)
2064 return EXT2_ET_EA_NO_SPACE;
2067 if (BHDR(s->base)->h_refcount != 1) {
2068 int offset = (char *)s->here - bs->block;
2070 /* Decrement the refcount of the shared block */
2071 old_block = s->base;
2072 BHDR(s->base)->h_refcount -= 1;
2074 error = ext2fs_get_mem(fs->blocksize, &s->base);
2078 memcpy(s->base, bs->block, fs->blocksize);
2079 s->first = ENTRY(BHDR(s->base)+1);
2080 BHDR(s->base)->h_refcount = 1;
2081 s->here = ENTRY(s->base + offset);
2082 s->end = s->base + fs->blocksize;
2085 error = ext2fs_get_mem(fs->blocksize, &s->base);
2089 memset(s->base, 0, fs->blocksize);
2090 BHDR(s->base)->h_magic = EXT2_EXT_ATTR_MAGIC;
2091 BHDR(s->base)->h_blocks = 1;
2092 BHDR(s->base)->h_refcount = 1;
2093 s->first = ENTRY(BHDR(s->base)+1);
2094 s->here = ENTRY(BHDR(s->base)+1);
2095 s->end = s->base + fs->blocksize;
2098 error = ext2fs_attr_set_entry(fs, i, s);
2102 if (!EXT2_EXT_IS_LAST_ENTRY(s->first))
2103 ext2fs_attr_rehash(BHDR(s->base), s->here);
2105 if (!EXT2_EXT_IS_LAST_ENTRY(s->first)) {
2106 if (bs->block && bs->block == s->base) {
2107 /* We are modifying this block in-place */
2108 new_buf = bs->block;
2109 blk = inode->i_file_acl;
2110 error = ext2fs_write_ext_attr(fs, blk, s->base);
2114 /* We need to allocate a new block */
2115 error = ext2fs_new_block(fs, 0, 0, &blk);
2118 ext2fs_block_alloc_stats(fs, blk, 1);
2119 error = ext2fs_write_ext_attr(fs, blk, s->base);
2124 BHDR(s->base)->h_refcount -= 1;
2125 error = ext2fs_write_ext_attr(fs,
2134 /* Update the i_blocks if we added a new EA block */
2135 if (!inode->i_file_acl && new_buf)
2136 inode->i_blocks += fs->blocksize / 512;
2137 /* Update the inode. */
2138 inode->i_file_acl = new_buf ? blk : 0;
2142 ext2fs_free_mem(&s->base);
2146 static errcode_t ext2fs_attr_ibody_set(ext2_filsys fs,
2147 struct ext2_inode_large *inode,
2148 struct ext2_attr_info *i,
2149 struct ext2_attr_ibody_find *is)
2151 struct ext2_attr_search *s = &is->s;
2154 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
2155 return EXT2_ET_EA_NO_SPACE;
2157 error = ext2fs_attr_set_entry(fs, i, s);
2161 if (!EXT2_EXT_IS_LAST_ENTRY(s->first))
2162 IHDR(inode)->h_magic = EXT2_EXT_ATTR_MAGIC;
2164 IHDR(inode)->h_magic = 0;
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);
2334 ext2fs_free_mem(&block_buf);
2338 static errcode_t ext2fs_attr_check_ibody(ext2_filsys fs,
2339 struct ext2_inode_large *inode)
2341 const int inode_size = EXT2_INODE_SIZE(fs->super);
2343 if (inode_size == EXT2_GOOD_OLD_INODE_SIZE)
2344 return EXT2_ET_EA_NAME_NOT_FOUND;
2346 if (IHDR(inode)->h_magic != EXT2_EXT_ATTR_MAGIC)
2347 return EXT2_ET_EA_BAD_MAGIC;
2349 return ext2fs_attr_check_names(&IHDR(inode)->h_first_entry[0],
2350 (char *)inode + inode_size);
2354 static errcode_t ext2fs_attr_ibody_get(ext2_filsys fs,
2355 struct ext2_inode_large *inode,
2356 int name_index, const char *name,
2357 void *buffer, size_t buffer_size,
2360 struct ext2_ext_attr_entry *entry;
2363 error = ext2fs_attr_check_ibody(fs, inode);
2367 entry = &IHDR(inode)->h_first_entry[0];
2369 error = ext2fs_attr_find_entry(&entry, name_index, name,
2370 (char *)inode + EXT2_INODE_SIZE(fs->super) -
2375 *easize = entry->e_value_size;
2377 if (entry->e_value_size > buffer_size) {
2378 error = EXT2_ET_EA_TOO_BIG;
2381 memcpy(buffer, (char *)&IHDR(inode)->h_first_entry[0] +
2382 entry->e_value_offs, entry->e_value_size);
2390 errcode_t ext2fs_attr_get(ext2_filsys fs, struct ext2_inode *inode,
2391 int name_index, const char *name, char *buffer,
2392 size_t buffer_size, int *easize)
2396 error = ext2fs_attr_ibody_get(fs, (struct ext2_inode_large *)inode,
2397 name_index, name, buffer, buffer_size,
2399 if (error == EXT2_ET_EA_NAME_NOT_FOUND || error == EXT2_ET_EA_BAD_MAGIC)
2400 error = ext2fs_attr_block_get(fs, inode, name_index, name,
2401 buffer, buffer_size, easize);
2406 int ext2fs_attr_get_next_attr(struct ext2_ext_attr_entry *entry, int name_index,
2407 char *buffer, int buffer_size, int start)
2409 const int prefix_len = ext2_attr_index_prefix[name_index].len;
2412 if (!start && !EXT2_EXT_IS_LAST_ENTRY(entry))
2413 entry = EXT2_EXT_ATTR_NEXT(entry);
2415 for (; !EXT2_EXT_IS_LAST_ENTRY(entry);
2416 entry = EXT2_EXT_ATTR_NEXT(entry)) {
2419 if (name_index == entry->e_name_index)
2422 if (EXT2_EXT_IS_LAST_ENTRY(entry))
2425 total_len = prefix_len + entry->e_name_len + 1;
2426 if (buffer && total_len <= buffer_size) {
2427 memcpy(buffer, ext2_attr_index_prefix[name_index].str,
2429 memcpy(buffer + prefix_len, entry->e_name, entry->e_name_len);
2430 buffer[prefix_len + entry->e_name_len] = '\0';
2436 errcode_t ext2fs_expand_extra_isize(ext2_filsys fs, ext2_ino_t ino,
2437 struct ext2_inode_large *inode,
2438 int new_extra_isize, int *ret,
2441 struct ext2_inode *inode_buf = NULL;
2442 struct ext2_ext_attr_header *header = NULL;
2443 struct ext2_ext_attr_entry *entry = NULL, *last = NULL;
2444 struct ext2_attr_ibody_find is = {
2446 .s = { .not_found = EXT2_ET_EA_NO_SPACE, },
2448 struct ext2_attr_block_find bs = {
2449 .s = { .not_found = EXT2_ET_EA_NO_SPACE, },
2451 char *start, *end, *block_buf = NULL, *buffer =NULL, *b_entry_name=NULL;
2452 int total_ino = 0, total_blk, free, offs, tried_min_extra_isize = 0;
2453 int s_min_extra_isize = fs->super->s_min_extra_isize;
2454 errcode_t error = 0;
2457 *needed_size = new_extra_isize;
2458 error = ext2fs_get_mem(fs->blocksize, &block_buf);
2462 if (inode == NULL) {
2463 error = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode_buf);
2467 error = ext2fs_read_inode_full(fs, ino, inode_buf,
2468 EXT2_INODE_SIZE(fs->super));
2472 inode = (struct ext2_inode_large *)inode_buf;
2476 if (inode->i_extra_isize >= new_extra_isize)
2479 start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2480 /* No extended attributes present */
2481 if (IHDR(inode)->h_magic != EXT2_EXT_ATTR_MAGIC) {
2483 EXT2_INODE_SIZE(fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
2484 inode->i_extra_isize);
2485 inode->i_extra_isize = new_extra_isize;
2491 start += sizeof(__u32);
2492 end = (char *)inode + EXT2_INODE_SIZE(fs->super);
2493 last = entry = (struct ext2_ext_attr_entry *)start;
2495 /* Consider space takenup by magic number */
2496 total_ino = sizeof(__u32);
2497 free = ext2fs_attr_free_space(last, &offs, start, &total_ino);
2499 /* Enough free space available in the inode for expansion */
2500 if (free >= new_extra_isize) {
2501 ext2fs_attr_shift_entries(entry,
2502 inode->i_extra_isize - new_extra_isize,
2504 EXT2_GOOD_OLD_INODE_SIZE +
2506 start - sizeof(__u32), total_ino);
2507 inode->i_extra_isize = new_extra_isize;
2513 if (inode->i_file_acl) {
2514 error = ext2fs_read_ext_attr(fs, inode->i_file_acl, block_buf);
2518 header = BHDR(block_buf);
2519 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
2520 error = EXT2_ET_EA_BAD_MAGIC;
2523 end = block_buf + fs->blocksize;
2524 last = entry = (struct ext2_ext_attr_entry *)(header+1);
2525 start = (char *)entry;
2527 free = ext2fs_attr_free_space(last, &offs, start, &total_blk);
2528 if (free < new_extra_isize) {
2529 if (!tried_min_extra_isize && s_min_extra_isize) {
2530 tried_min_extra_isize++;
2531 new_extra_isize = s_min_extra_isize;
2535 *ret = EXT2_EXPAND_EISIZE_NOSPC;
2536 error = EXT2_ET_EA_NO_SPACE;
2540 if (ret && *ret == EXT2_EXPAND_EISIZE_UNSAFE) {
2541 *ret = EXT2_EXPAND_EISIZE_NEW_BLOCK;
2545 free = fs->blocksize;
2548 while (new_extra_isize > 0) {
2549 int offs, size, entry_size;
2550 struct ext2_ext_attr_entry *small_entry = NULL;
2551 struct ext2_attr_info i = {
2555 unsigned int total_size, shift_bytes, temp = ~0U, extra_isize=0;
2557 start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
2558 inode->i_extra_isize + sizeof(__u32);
2559 end = (char *)inode + EXT2_INODE_SIZE(fs->super);
2560 last = (struct ext2_ext_attr_entry *)start;
2562 /* Find the entry best suited to be pushed into EA block */
2564 for (; !EXT2_EXT_IS_LAST_ENTRY(last);
2565 last = EXT2_EXT_ATTR_NEXT(last)) {
2566 total_size = EXT2_EXT_ATTR_SIZE(last->e_value_size) +
2567 EXT2_EXT_ATTR_LEN(last->e_name_len);
2568 if (total_size <= free && total_size < temp) {
2569 if (total_size < new_extra_isize) {
2578 if (entry == NULL) {
2580 entry = small_entry;
2582 if (!tried_min_extra_isize &&
2583 s_min_extra_isize) {
2584 tried_min_extra_isize++;
2585 new_extra_isize = s_min_extra_isize;
2589 *ret = EXT2_EXPAND_EISIZE_NOSPC;
2590 error = EXT2_ET_EA_NO_SPACE;
2594 offs = entry->e_value_offs;
2595 size = entry->e_value_size;
2596 entry_size = EXT2_EXT_ATTR_LEN(entry->e_name_len);
2597 i.name_index = entry->e_name_index;
2598 error = ext2fs_get_mem(EXT2_EXT_ATTR_SIZE(size), &buffer);
2601 error = ext2fs_get_mem(entry->e_name_len + 1, &b_entry_name);
2604 /* Save the entry name and the entry value */
2605 memcpy((char *)buffer, (char *)start + offs,
2606 EXT2_EXT_ATTR_SIZE(size));
2607 memcpy((char *)b_entry_name, (char *)entry->e_name,
2609 b_entry_name[entry->e_name_len] = '\0';
2610 i.name = b_entry_name;
2612 error = ext2fs_attr_ibody_find(fs, inode, &i, &is);
2616 error = ext2fs_attr_set_entry(fs, &i, &is.s);
2620 entry = (struct ext2_ext_attr_entry *)start;
2621 if (entry_size + EXT2_EXT_ATTR_SIZE(size) >= new_extra_isize)
2622 shift_bytes = new_extra_isize;
2624 shift_bytes = entry_size + EXT2_EXT_ATTR_SIZE(size);
2625 ext2fs_attr_shift_entries(entry,
2626 inode->i_extra_isize - shift_bytes,
2627 (char *)inode +EXT2_GOOD_OLD_INODE_SIZE+
2628 extra_isize + shift_bytes,
2629 start - sizeof(__u32),
2630 total_ino - entry_size);
2632 extra_isize += shift_bytes;
2633 new_extra_isize -= shift_bytes;
2635 *needed_size = new_extra_isize;
2636 inode->i_extra_isize = extra_isize;
2638 i.name = b_entry_name;
2641 error = ext2fs_attr_block_find(fs, (struct ext2_inode *)inode,
2646 /* Add entry which was removed from the inode into the block */
2647 error = ext2fs_attr_block_set(fs, (struct ext2_inode *)inode,
2654 error = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)inode,
2655 EXT2_INODE_SIZE(fs->super));
2658 ext2fs_free_mem(&inode_buf);
2660 ext2fs_free_mem(&block_buf);
2662 ext2fs_free_mem(&buffer);
2664 ext2fs_free_mem(&b_entry_name);