Skip to contentSkip to conversation

e2fsck: update quota inode accounting for ea_inode feature

PATCH Applied Tahsin Erdogan 2 participants 8 messages Jul 7, 2017 – Jul 24, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 e2fsck: update quota inode accounting for ea_inode feature

Extended attribute inodes are charged to all referencing inodes. Update e2fsck so that it can correctly track inode quota charges.

---
 e2fsck/e2fsck.c |  10 +++--
 e2fsck/e2fsck.h |   5 ++-
 e2fsck/pass1.c  | 134 ++++++++++++++++++++++++++++++++++++++------------------
 3 files changed, 101 insertions(+), 48 deletions(-)
e2fsck/e2fsck.c +7 -3
index 63a986b92af9..88088a2578bb 100644
@@ -98,9 +98,13 @@ errcode_t e2fsck_reset_context(e2fsck_t ctx)
 		ea_refcount_free(ctx->refcount_extra);
 		ctx->refcount_extra = 0;
 	}
-	if (ctx->ea_block_quota) {
-		ea_refcount_free(ctx->ea_block_quota);
-		ctx->ea_block_quota = 0;
+	if (ctx->ea_block_quota_blocks) {
+		ea_refcount_free(ctx->ea_block_quota_blocks);
+		ctx->ea_block_quota_blocks = 0;
+	}
+	if (ctx->ea_block_quota_inodes) {
+		ea_refcount_free(ctx->ea_block_quota_inodes);
+		ctx->ea_block_quota_inodes = 0;
 	}
 	if (ctx->ea_inode_refs) {
 		ea_refcount_free(ctx->ea_inode_refs);
e2fsck/e2fsck.h +3 -2
index b25c5eb9179b..ca414ccb662a 100644
@@ -269,9 +269,10 @@ struct e2fsck_struct {
 	ext2_refcount_t refcount_extra;
 
 	/*
-	 * Quota blocks to be charged for each ea block.
+	 * Quota blocks and inodes to be charged for each ea block.
 	 */
-	ext2_refcount_t ea_block_quota;
+	ext2_refcount_t ea_block_quota_blocks;
+	ext2_refcount_t ea_block_quota_inodes;
 
 	/*
 	 * ea_inode references from attr entries.
e2fsck/pass1.c +91 -43

340 lines not on this page · raw message

Signature · 1 line
2.13.2.725.g09c95d1e9-goog
#1 lore raw

PATCH 2/4 libext2fs: rename ext2_xattr_handle->length to capacity

ext2_xattr_handle has two fields 'count' and 'length' which represent number of filled elements vs total element count. They have close meanings so are easy to confuse, thus make code less readable. Rename length to capacity.

---
 lib/ext2fs/ext_attr.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
lib/ext2fs/ext_attr.c +17 -17
index 4357bdd0caae..2e9fc96d114d 100644
@@ -291,7 +291,7 @@ struct ext2_xattr_handle {
 	errcode_t magic;
 	ext2_filsys fs;
 	struct ext2_xattr *attrs;
-	size_t length, count;
+	size_t capacity, count;
 	ext2_ino_t ino;
 	unsigned int flags;
 	int dirty;
@@ -303,14 +303,14 @@ static errcode_t ext2fs_xattrs_expand(struct ext2_xattr_handle *h,
 	struct ext2_xattr *new_attrs;
 	errcode_t err;
 
-	err = ext2fs_get_arrayzero(h->length + expandby,
+	err = ext2fs_get_arrayzero(h->capacity + expandby,
 				   sizeof(struct ext2_xattr), &new_attrs);
 	if (err)
 		return err;
 
-	memcpy(new_attrs, h->attrs, h->length * sizeof(struct ext2_xattr));
+	memcpy(new_attrs, h->attrs, h->capacity * sizeof(struct ext2_xattr));
 	ext2fs_free_mem(&h->attrs);
-	h->length += expandby;
+	h->capacity += expandby;
 	h->attrs = new_attrs;
 
 	return 0;
@@ -675,7 +675,7 @@ static errcode_t write_xattrs_to_buffer(struct ext2_xattr_handle *handle,
 
 	memset(entries_start, 0, storage_size);
 	/* For all remaining x...  */
-	for (; x < handle->attrs + handle->length; x++) {
+	for (; x < handle->attrs + handle->capacity; x++) {
 		if (!x->name)
 			continue;
 
@@ -771,7 +771,7 @@ errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
 	 * to the end.
 	 */
 	x = handle->attrs;
-	qsort(x, handle->length, sizeof(struct ext2_xattr), attr_compare);
+	qsort(x, handle->capacity, sizeof(struct ext2_xattr), attr_compare);
 
 	/* Does the inode have space for EA? */
 	if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
@@ -813,7 +813,7 @@ write_ea_block:
 	if (err)
 		goto out2;
 
-	if (x < handle->attrs + handle->length) {
+	if (x < handle->attrs + handle->capacity) {
 		err = EXT2_ET_EA_NO_SPACE;
 		goto out2;
 	}
@@ -904,11 +904,11 @@ static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
 	       !EXT2_EXT_IS_LAST_ENTRY(entry)) {
 
 		/* Allocate space for more attrs? */
-		if (x == handle->attrs + handle->length) {
+		if (x == handle->attrs + handle->capacity) {
 			err = ext2fs_xattrs_expand(handle, 4);
 			if (err)
 				return err;
-			x = handle->attrs + handle->length - 4;
+			x = handle->attrs + handle->capacity - 4;
 		}
 
 		/* header eats this space */
@@ -1026,7 +1026,7 @@ static void xattrs_free_keys(struct ext2_xattr_handle *h)
 	struct ext2_xattr *a = h->attrs;
 	size_t i;
 
-	for (i = 0; i < h->length; i++) {
+	for (i = 0; i < h->capacity; i++) {
 		if (a[i].name)
 			ext2fs_free_mem(&a[i].name);
 		if (a[i].value)
@@ -1149,7 +1149,7 @@ errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
 	int ret;
 
 	EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
-	for (x = h->attrs; x < h->attrs + h->length; x++) {
+	for (x = h->attrs; x < h->attrs + h->capacity; x++) {
 		if (!x->name)
 			continue;
 
@@ -1171,7 +1171,7 @@ errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
 	errcode_t err;
 
 	EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
-	for (x = h->attrs; x < h->attrs + h->length; x++) {
+	for (x = h->attrs; x < h->attrs + h->capacity; x++) {
 		if (!x->name || strcmp(x->name, key))
 			continue;
 
@@ -1280,7 +1280,7 @@ errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *handle,
 	} else
 		memcpy(new_value, value, value_len);
 
-	for (x = handle->attrs; x < handle->attrs + handle->length; x++) {
+	for (x = handle->attrs; x < handle->attrs + handle->capacity; x++) {
 		if (!x->name) {
 			last_empty = x;
 			continue;
@@ -1314,7 +1314,7 @@ errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *handle,
 	if (err)
 		goto errout;
 
-	x = handle->attrs + handle->length - 4;
+	x = handle->attrs + handle->capacity - 4;
 	err = ext2fs_get_mem(strlen(key) + 1, &x->name);
 	if (err)
 		goto errout;
@@ -1339,7 +1339,7 @@ errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
 	struct ext2_xattr *x;
 
 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
-	for (x = handle->attrs; x < handle->attrs + handle->length; x++) {
+	for (x = handle->attrs; x < handle->attrs + handle->capacity; x++) {
 		if (!x->name)
 			continue;
 
@@ -1372,8 +1372,8 @@ errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
 		return err;
 
 	h->magic = EXT2_ET_MAGIC_EA_HANDLE;
-	h->length = 4;
-	err = ext2fs_get_arrayzero(h->length, sizeof(struct ext2_xattr),
+	h->capacity = 4;
+	err = ext2fs_get_arrayzero(h->capacity, sizeof(struct ext2_xattr),
 				   &h->attrs);
 	if (err) {
 		ext2fs_free_mem(&h);
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
#2 lore raw

Re: [PATCH 2/4] libext2fs: rename ext2_xattr_handle->length to capacity

Quoted 6 lines · Tahsin Erdogan

ext2_xattr_handle has two fields 'count' and 'length' which represent number of filled elements vs total element count. They have close meanings so are easy to confuse, thus make code less readable. Rename length to capacity.

Applied, thanks.

- Ted

#3 lore raw

PATCH 3/4 libext2fs: eliminate empty element holes in ext2_xattr_handle->attrs

In reply to Tahsin Erdogan

When an extended attribute is removed, its array element is emptied. This creates holes in the array so various places that want to walk filled elements have to do an empty element check.

Have remove operation shift remaining filled elements to the left. This allows a simple iteration up to ext2_xattr_handle->count to walk all filled entries, and so empty element checks become unnecessary.

---
 lib/ext2fs/ext_attr.c | 93 ++++++++++++++++-----------------------------------
 1 file changed, 29 insertions(+), 64 deletions(-)
lib/ext2fs/ext_attr.c +26 -62
index 2e9fc96d114d..00ff79ae3890 100644
@@ -335,7 +335,7 @@ static struct ea_name_index ea_names[] = {
 
 static int find_ea_index(char *fullname, char **name, int *index);
 
-/* Push empty attributes to the end and inlinedata to the front. */
+/* Pull inlinedata to the front. */
 static int attr_compare(const void *a, const void *b)
 {
 	const struct ext2_xattr *xa = a, *xb = b;
@@ -343,11 +343,7 @@ static int attr_compare(const void *a, const void *b)
 	int xa_idx, xb_idx;
 	int cmp;
 
-	if (xa->name == NULL)
-		return +1;
-	else if (xb->name == NULL)
-		return -1;
-	else if (!strcmp(xa->name, "system.data"))
+	if (!strcmp(xa->name, "system.data"))
 		return -1;
 	else if (!strcmp(xb->name, "system.data"))
 		return +1;
@@ -675,10 +671,7 @@ static errcode_t write_xattrs_to_buffer(struct ext2_xattr_handle *handle,
 
 	memset(entries_start, 0, storage_size);
 	/* For all remaining x...  */
-	for (; x < handle->attrs + handle->capacity; x++) {
-		if (!x->name)
-			continue;
-
+	for (; x < handle->attrs + handle->count; x++) {
 		/* Calculate index and shortname position */
 		shortname = x->name;
 		ret = find_ea_index(x->name, &shortname, &idx);
@@ -766,12 +759,9 @@ errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
 		goto out;
 	}
 
-	/*
-	 * Force the inlinedata attr to the front and the empty entries
-	 * to the end.
-	 */
+	/* Force the inlinedata attr to the front. */
 	x = handle->attrs;
-	qsort(x, handle->capacity, sizeof(struct ext2_xattr), attr_compare);
+	qsort(x, handle->count, sizeof(struct ext2_xattr), attr_compare);
 
 	/* Does the inode have space for EA? */
 	if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
@@ -813,7 +803,7 @@ write_ea_block:
 	if (err)
 		goto out2;
 
-	if (x < handle->attrs + handle->capacity) {
+	if (x < handle->attrs + handle->count) {
 		err = EXT2_ET_EA_NO_SPACE;
 		goto out2;
 	}
@@ -865,8 +855,7 @@ static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
 					 struct ext2_inode_large *inode,
 					 struct ext2_ext_attr_entry *entries,
 					 unsigned int storage_size,
-					 char *value_start,
-					 size_t *nr_read)
+					 char *value_start)
 {
 	struct ext2_xattr *x;
 	struct ext2_ext_attr_entry *entry, *end;
@@ -876,10 +865,6 @@ static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
 	unsigned int values_size = storage_size +
 			((char *)entries - value_start);
 
-	x = handle->attrs;
-	while (x->name)
-		x++;
-
 	/* find the end */
 	end = entries;
 	remain = storage_size;
@@ -904,13 +889,14 @@ static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
 	       !EXT2_EXT_IS_LAST_ENTRY(entry)) {
 
 		/* Allocate space for more attrs? */
-		if (x == handle->attrs + handle->capacity) {
+		if (handle->count == handle->capacity) {
 			err = ext2fs_xattrs_expand(handle, 4);
 			if (err)
 				return err;
-			x = handle->attrs + handle->capacity - 4;
 		}
 
+		x = handle->attrs + handle->count;
+
 		/* header eats this space */
 		remain -= sizeof(struct ext2_ext_attr_entry);
 
@@ -1013,8 +999,7 @@ static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
 			}
 		}
 
-		x++;
-		(*nr_read)++;
+		handle->count++;
 		entry = EXT2_EXT_ATTR_NEXT(entry);
 	}
 
@@ -1084,8 +1069,8 @@ errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle)
 			inode->i_extra_isize + sizeof(__u32);
 
 		err = read_xattrs_from_buffer(handle, inode,
-			(struct ext2_ext_attr_entry *) start, storage_size,
-					      start, &handle->count);
+					(struct ext2_ext_attr_entry *) start,
+					storage_size, start);
 		if (err)
 			goto out;
 	}
@@ -1121,8 +1106,8 @@ read_ea_block:
 			sizeof(struct ext2_ext_attr_header);
 		start = block_buf + sizeof(struct ext2_ext_attr_header);
 		err = read_xattrs_from_buffer(handle, inode,
-			(struct ext2_ext_attr_entry *) start, storage_size,
-					      block_buf, &handle->count);
+					(struct ext2_ext_attr_entry *) start,
+					storage_size, block_buf);
 		if (err)
 			goto out3;
 
@@ -1149,10 +1134,7 @@ errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
 	int ret;
 
 	EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
-	for (x = h->attrs; x < h->attrs + h->capacity; x++) {
-		if (!x->name)
-			continue;
-
+	for (x = h->attrs; x < h->attrs + h->count; x++) {
 		ret = func(x->name, x->value, x->value_len, data);
 		if (ret & XATTR_CHANGED)
 			h->dirty = 1;
@@ -1171,8 +1153,8 @@ errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
 	errcode_t err;
 
 	EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
-	for (x = h->attrs; x < h->attrs + h->capacity; x++) {
-		if (!x->name || strcmp(x->name, key))
+	for (x = h->attrs; x < h->attrs + h->count; x++) {
+		if (strcmp(x->name, key))
 			continue;
 
 		if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
@@ -1260,12 +1242,11 @@ errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *handle,
 			   const void *value,
 			   size_t value_len)
 {
-	struct ext2_xattr *x, *last_empty;
+	struct ext2_xattr *x;
 	char *new_value;
 	errcode_t err;
 
 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
-	last_empty = NULL;
 
 	err = ext2fs_get_mem(value_len, &new_value);
 	if (err)
@@ -1280,12 +1261,7 @@ errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *handle,
 	} else
 		memcpy(new_value, value, value_len);
 
-	for (x = handle->attrs; x < handle->attrs + handle->capacity; x++) {
-		if (!x->name) {
-			last_empty = x;
-			continue;
-		}
-
+	for (x = handle->attrs; x < handle->attrs + handle->count; x++) {
 		/* Replace xattr */
 		if (strcmp(x->name, key) == 0) {
 			ext2fs_free_mem(&x->value);
@@ -1296,25 +1272,15 @@ errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *handle,
 		}
 	}
 
-	/* Add attr to empty slot */
-	if (last_empty) {
-		err = ext2fs_get_mem(strlen(key) + 1, &last_empty->name);
+	if (handle->count == handle->capacity) {
+		/* Expand array, append slot */
+		err = ext2fs_xattrs_expand(handle, 4);
 		if (err)
 			goto errout;
-		strcpy(last_empty->name, key);
-		last_empty->value = new_value;
-		last_empty->value_len = value_len;
-		handle->dirty = 1;
-		handle->count++;
-		return 0;
-	}
 
-	/* Expand array, append slot */
-	err = ext2fs_xattrs_expand(handle, 4);
-	if (err)
-		goto errout;
+		x = handle->attrs + handle->capacity - 4;
+	}
 
-	x = handle->attrs + handle->capacity - 4;
 	err = ext2fs_get_mem(strlen(key) + 1, &x->name);
 	if (err)
 		goto errout;
@@ -1337,16 +1303,15 @@ errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
 			      const char *key)
 {
 	struct ext2_xattr *x;
+	struct ext2_xattr *end = handle->attrs + handle->count;
 
 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
-	for (x = handle->attrs; x < handle->attrs + handle->capacity; x++) {
-		if (!x->name)
-			continue;
#4 lore raw

Re: [PATCH 3/4] libext2fs: eliminate empty element holes in ext2_xattr_handle->attrs

Quoted 9 lines · Tahsin Erdogan

When an extended attribute is removed, its array element is emptied. This creates holes in the array so various places that want to walk filled elements have to do an empty element check.

Have remove operation shift remaining filled elements to the left. This allows a simple iteration up to ext2_xattr_handle->count to walk all filled entries, and so empty element checks become unnecessary.

Thanks, applied.

- Ted

#5 lore raw

PATCH 4/4 libext2fs: add ea_inode support to set xattr

In reply to Tahsin Erdogan

This patch is a major update to how we decide where to put extended attributes. The main motivation is to enable creating values in extended attribute inodes. While doing this, we want to implement a behavior that is as close to kernel as possible.

Existing set ea code deviates from kernel behavior which makes it harder to implement ea_inode feature:

- kernel only sorts ea entries in xattr block, e2fsprogs implementation sorts all entries on every update.

- e2fsprogs implementation shuffled things on every update so the order of updates does not matter. Kernel does not reshuffle things.

- e2fsprogs could evacuate entries from inode body to xattr block and vice versa. This behavior does not exist in kernel.

Such differences could lead to inconsistent behavior between fuse2fs and a kernel mount.

With ea_inode feature, we also need to decide whether to put a value in an inode or keep it 'inline'. In kernel implementation this depends on current placement of entries.

To close the behavioral gap, ext2fs_xattr_set() now takes over the decision about where to place ea values. This also allows it to raise errors early instead of delaying them to a separate ext2fs_xattrs_write() call later.

---
 contrib/android/perms.c  |   6 -
 debugfs/xattrs.c         |  11 -
 e2fsck/pass4.c           |  20 +-
 lib/ext2fs/ext2fs.h      |   2 +
 lib/ext2fs/ext_attr.c    | 659 +++++++++++++++++++++++++++++++++++------------
 lib/ext2fs/inline_data.c |  10 -
 misc/fuse2fs.c           |  17 --
 7 files changed, 499 insertions(+), 226 deletions(-)
contrib/android/perms.c +0 -6
index 9a7a93f5564b..08fb861486bf 100644
@@ -48,12 +48,6 @@ static errcode_t ino_add_xattr(ext2_filsys fs, ext2_ino_t ino, const char *name,
 			_("while setting xattrs of inode %u"), ino);
 		goto xattrs_close;
 	}
-	retval = ext2fs_xattrs_write(xhandle);
-	if (retval) {
-		com_err(__func__, retval,
-			_("while writting xattrs of inode %u"), ino);
-		goto xattrs_close;
-	}
 xattrs_close:
 	close_retval = ext2fs_xattrs_close(&xhandle);
 	if (close_retval) {
debugfs/xattrs.c +0 -11
index 9b87d14551dc..fdb76e41a230 100644
@@ -306,13 +306,6 @@ void do_set_xattr(int argc, char **argv)
 	}
 
 	err = ext2fs_xattr_set(h, argv[optind + 1], buf, buflen);
-	if (err)
-		goto out;
-
-	err = ext2fs_xattrs_write(h);
-	if (err)
-		goto out;
-
 out:
 	ext2fs_xattrs_close(&h);
 	if (err)
@@ -360,10 +353,6 @@ void do_rm_xattr(int argc, char **argv)
 		if (err)
 			goto out;
 	}
-
-	err = ext2fs_xattrs_write(h);
-	if (err)
-		goto out;
 out:
 	ext2fs_xattrs_close(&h);
 	if (err)
e2fsck/pass4.c +2 -18
index 663f87ab59c0..4125c53b1b97 100644
@@ -87,22 +87,6 @@ static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
 	return 0;
 }
 
-/*
- * Get/set ref functions below could later be moved to somewhere in lib/ext2fs/.
- * Currently the only user is e2fsck so we rather not expose it in common
- * library until there are more users.
- */
-static __u64 ea_inode_get_ref(struct ext2_inode_large *inode)
-{
-	return ((__u64)inode->i_ctime << 32) | inode->osd1.linux1.l_i_version;
-}
-
-static void ea_inode_set_ref(struct ext2_inode_large *inode, __u64 ref_count)
-{
-	inode->i_ctime = (__u32)(ref_count >> 32);
-	inode->osd1.linux1.l_i_version = (__u32)ref_count;
-}
-
 static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i,
 			   struct ext2_inode_large *inode, __u16 *link_counted)
 {
@@ -140,7 +124,7 @@ static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i,
 	 */
 	*link_counted = 1;
 
-	ref_count = ea_inode_get_ref(inode);
+	ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(inode));
 
 	/* Old Lustre-style xattr inodes do not have a stored refcount.
 	 * However, their i_ctime and i_atime should be the same.
@@ -153,7 +137,7 @@ static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i,
 		pctx.num = ref_count;
 		pctx.num2 = actual_refs;
 		if (fix_problem(ctx, PR_4_EA_INODE_REF_COUNT, &pctx)) {
-			ea_inode_set_ref(inode, actual_refs);
+			ext2fs_set_ea_inode_ref(EXT2_INODE(inode), actual_refs);
 			e2fsck_write_inode(ctx, i, EXT2_INODE(inode), "pass4");
 		}
 	}
lib/ext2fs/ext2fs.h +2 -0
index b734f1aa2d5b..a2c8edaa4ac2 100644
@@ -1251,6 +1251,8 @@ extern void ext2fs_ext_attr_block_rehash(struct ext2_ext_attr_header *header,
 					 struct ext2_ext_attr_entry *end);
 extern __u32 ext2fs_get_ea_inode_hash(struct ext2_inode *inode);
 extern void ext2fs_set_ea_inode_hash(struct ext2_inode *inode, __u32 hash);
+extern __u64 ext2fs_get_ea_inode_ref(struct ext2_inode *inode);
+extern void ext2fs_set_ea_inode_ref(struct ext2_inode *inode, __u64 ref_count);
 
 /* extent.c */
 extern errcode_t ext2fs_extent_header_verify(void *ptr, int size);
lib/ext2fs/ext_attr.c +495 -164

876 lines not on this page · raw message

lib/ext2fs/inline_data.c +0 -10
index c0fc4ed1d6c7..0c1eef6c73d2 100644
@@ -42,11 +42,6 @@ static errcode_t ext2fs_inline_data_ea_set(struct ext2_inline_data *data)
 
 	retval = ext2fs_xattr_set(handle, "system.data",
 				  data->ea_data, data->ea_size);
-	if (retval)
-		goto err;
-
-	retval = ext2fs_xattrs_write(handle);
-
 err:
 	(void) ext2fs_xattrs_close(&handle);
 	return retval;
@@ -270,11 +265,6 @@ errcode_t ext2fs_inline_data_ea_remove(ext2_filsys fs, ext2_ino_t ino)
 		goto err;
 
 	retval = ext2fs_xattr_remove(handle, "system.data");
-	if (retval)
-		goto err;
-
-	retval = ext2fs_xattrs_write(handle);
-
 err:
 	(void) ext2fs_xattrs_close(&handle);
 	return retval;
misc/fuse2fs.c +0 -11
index 27c94a62a18d..9435e428c823 100644
@@ -2653,12 +2653,6 @@ static int op_setxattr(const char *path EXT2FS_ATTR((unused)),
 		goto out3;
 	}
 
-	err = ext2fs_xattrs_write(h);
-	if (err) {
-		ret = translate_error(fs, ino, err);
-		goto out3;
-	}
-
 	ret = update_ctime(fs, ino, NULL);
 out3:
 	if (cvalue != value)
@@ -2725,12 +2719,6 @@ static int op_removexattr(const char *path, const char *key)
 		goto out2;
 	}
 
-	err = ext2fs_xattrs_write(h);
-	if (err) {
-		ret = translate_error(fs, ino, err);
-		goto out2;
-	}
#6 lore raw

Re: [PATCH 4/4] libext2fs: add ea_inode support to set xattr

Applied, with some style cleanups. See below for the interdiff.

The cleanups are mostly checkpatch style warnings, plus simplifying complexity by reducing indentation levels. Yes, it makes the code somewhat less parallel, but on the whole I think it makes the code more readable.

- Ted

 lib/ext2fs/ext_attr.c | 112 +++++++++++++++++++++------------------------
 1 file changed, 52 insertions(+), 60 deletions(-)
lib/ext2fs/ext_attr.c +52 -60
index 7cecb1688..f4cc2d056 100644
@@ -1334,7 +1334,7 @@ static errcode_t xattr_update_entry(ext2_filsys fs, struct ext2_xattr *x,
 
 	if (!x->name)
 		x->name = new_name;
-	
+
 	if (x->value)
 		ext2fs_free_mem(&x->value);
 	x->value = new_value;
@@ -1351,7 +1351,8 @@ fail:
 	return ret;
 }
 
-static int xattr_find_position(struct ext2_xattr *attrs, int count, const char *name)
+static int xattr_find_position(struct ext2_xattr *attrs, int count,
+			       const char *name)
 {
 	struct ext2_xattr *x;
 	int i;
@@ -1400,7 +1401,7 @@ errcode_t xattr_array_update(struct ext2_xattr_handle *h, const char *name,
 	if (!in_inode)
 		needed += EXT2_EXT_ATTR_SIZE(value_len);
 
-	if (0 <= old_idx && old_idx < h->ibody_count) {
+	if (old_idx >= 0 && old_idx < h->ibody_count) {
 		ibody_free += EXT2_EXT_ATTR_LEN(name_len);
 		if (!h->attrs[old_idx].ea_ino)
 			ibody_free += EXT2_EXT_ATTR_SIZE(
@@ -1408,75 +1409,66 @@ errcode_t xattr_array_update(struct ext2_xattr_handle *h, const char *name,
 	}
 
 	if (needed <= ibody_free) {
-		if (0 <= old_idx) {
-			/* Update the existing entry. */
-			ret = xattr_update_entry(h->fs, &h->attrs[old_idx],
-						 name, value, value_len,
-						 in_inode);
-			if (ret)
-				return ret;
-			if (h->ibody_count <= old_idx) {
-				/* Move entry from block to the end of ibody. */
-				tmp = h->attrs[old_idx];
-				memmove(h->attrs + h->ibody_count + 1,
-					h->attrs + h->ibody_count,
-					(old_idx - h->ibody_count)
-							* sizeof(*h->attrs));
-				h->attrs[h->ibody_count] = tmp;
-				h->ibody_count++;
-			}
-			return 0;
-		} else {
+		if (old_idx < 0) {
 			new_idx = h->ibody_count;
 			add_to_ibody = 1;
 			goto add_new;
 		}
+
+		/* Update the existing entry. */
+		ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
+					 value, value_len, in_inode);
+		if (ret)
+			return ret;
+		if (h->ibody_count <= old_idx) {
+			/* Move entry from block to the end of ibody. */
+			tmp = h->attrs[old_idx];
+			memmove(h->attrs + h->ibody_count + 1,
+				h->attrs + h->ibody_count,
+				(old_idx - h->ibody_count) * sizeof(*h->attrs));
+			h->attrs[h->ibody_count] = tmp;
+			h->ibody_count++;
+		}
+		return 0;
 	}
 
 	if (h->ibody_count <= old_idx) {
 		block_free += EXT2_EXT_ATTR_LEN(name_len);
 		if (!h->attrs[old_idx].ea_ino)
-			block_free += EXT2_EXT_ATTR_SIZE(
-						h->attrs[old_idx].value_len);
+			block_free +=
+				EXT2_EXT_ATTR_SIZE(h->attrs[old_idx].value_len);
 	}
 
-	if (needed <= block_free) {
-		if (0 <= old_idx) {
-			/* Update the existing entry. */
-			ret = xattr_update_entry(h->fs, &h->attrs[old_idx],
-						 name, value, value_len,
-						 in_inode);
-			if (ret)
-				return ret;
-			if (old_idx < h->ibody_count) {
-				/*
-				 * Move entry from ibody to the block. Note that
-				 * entries in the block are sorted.
-				 */
-				new_idx = xattr_find_position(
-						h->attrs + h->ibody_count,
-						h->count - h->ibody_count,
-						name);
-				new_idx += h->ibody_count - 1;
-				tmp = h->attrs[old_idx];
-				memmove(h->attrs + old_idx,
-					h->attrs + old_idx + 1,
-					(new_idx - old_idx)*sizeof(*h->attrs));
-				h->attrs[new_idx] = tmp;
-				h->ibody_count--;
-			}
-			return 0;
-		} else {
+	if (needed > block_free)
+		return EXT2_ET_EA_NO_SPACE;
+
+	if (old_idx >= 0) {
+		/* Update the existing entry. */
+		ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
+					 value, value_len, in_inode);
+		if (ret)
+			return ret;
+		if (old_idx < h->ibody_count) {
+			/*
+			 * Move entry from ibody to the block. Note that
+			 * entries in the block are sorted.
+			 */
 			new_idx = xattr_find_position(h->attrs + h->ibody_count,
-						      h->count - h->ibody_count,
-						      name);
-			new_idx += h->ibody_count;
-			add_to_ibody = 0;
-			goto add_new;
+				h->count - h->ibody_count, name);
+			new_idx += h->ibody_count - 1;
+			tmp = h->attrs[old_idx];
+			memmove(h->attrs + old_idx, h->attrs + old_idx + 1,
+				(new_idx - old_idx) * sizeof(*h->attrs));
+			h->attrs[new_idx] = tmp;
+			h->ibody_count--;
 		}
+		return 0;
 	}
 
-	return EXT2_ET_EA_NO_SPACE;
+	new_idx = xattr_find_position(h->attrs + h->ibody_count,
+				      h->count - h->ibody_count, name);
+	new_idx += h->ibody_count;
+	add_to_ibody = 0;
 
 add_new:
 	if (h->count == h->capacity) {
@@ -1520,7 +1512,7 @@ int space_used(struct ext2_xattr *attrs, int count)
 /*
  * The minimum size of EA value when you start storing it in an external inode
  * size of block - size of header - size of 1 entry - 4 null bytes
-*/
+ */
 #define EXT4_XATTR_MIN_LARGE_EA_SIZE(b)	\
 	((b) - EXT2_EXT_ATTR_LEN(3) - sizeof(struct ext2_ext_attr_header) - 4)
 
@@ -1579,8 +1571,8 @@ errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *h,
 	if (inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
 		extra_isize = inode->i_extra_isize;
 		if (extra_isize == 0) {
-		    	extra_isize = fs->super->s_want_extra_isize;
-		    	if (extra_isize == 0)
+			extra_isize = fs->super->s_want_extra_isize;
+			if (extra_isize == 0)
 				extra_isize = sizeof(__u32);
 		}
 		ibody_free = inode_size - EXT2_GOOD_OLD_INODE_SIZE;