Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa09200b authored by Josef Bacik's avatar Josef Bacik
Browse files

Btrfs: try to only do one btrfs_search_slot in do_setxattr



I've been watching how many btrfs_search_slot()'s we do and I noticed that when
we create a file with selinux enabled we were doing 2 each time we initialize
the security context.  That's because we lookup the xattr first so we can delete
it if we're setting a new value to an existing xattr.  But in the create case we
don't have any xattrs, so it is completely useless to have the extra lookup.  So
re-arrange things so that we only lookup first if we specifically have
XATTR_REPLACE.  That way in the basic case we only do 1 search, and in the more
complicated case we do the normal 2 lookups.  Thanks,

Signed-off-by: default avatarJosef Bacik <josef@redhat.com>
parent 149e2d76
Loading
Loading
Loading
Loading
+2 −7
Original line number Original line Diff line number Diff line
@@ -89,13 +89,8 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
	data_size = sizeof(*dir_item) + name_len + data_len;
	data_size = sizeof(*dir_item) + name_len + data_len;
	dir_item = insert_with_overflow(trans, root, path, &key, data_size,
	dir_item = insert_with_overflow(trans, root, path, &key, data_size,
					name, name_len);
					name, name_len);
	/*
	if (IS_ERR(dir_item))
	 * FIXME: at some point we should handle xattr's that are larger than
		return PTR_ERR(dir_item);
	 * what we can fit in our leaf.  We set location to NULL b/c we arent
	 * pointing at anything else, that will change if we store the xattr
	 * data in a separate inode.
	 */
	BUG_ON(IS_ERR(dir_item));
	memset(&location, 0, sizeof(location));
	memset(&location, 0, sizeof(location));


	leaf = path->nodes[0];
	leaf = path->nodes[0];
+40 −26
Original line number Original line Diff line number Diff line
@@ -102,43 +102,57 @@ static int do_setxattr(struct btrfs_trans_handle *trans,
	if (!path)
	if (!path)
		return -ENOMEM;
		return -ENOMEM;


	/* first lets see if we already have this xattr */
	if (flags & XATTR_REPLACE) {
		di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), name,
		di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), name,
				strlen(name), -1);
					name_len, -1);
		if (IS_ERR(di)) {
		if (IS_ERR(di)) {
			ret = PTR_ERR(di);
			ret = PTR_ERR(di);
			goto out;
			goto out;
	}
		} else if (!di) {

			ret = -ENODATA;
	/* ok we already have this xattr, lets remove it */
	if (di) {
		/* if we want create only exit */
		if (flags & XATTR_CREATE) {
			ret = -EEXIST;
			goto out;
			goto out;
		}
		}

		ret = btrfs_delete_one_dir_name(trans, root, path, di);
		ret = btrfs_delete_one_dir_name(trans, root, path, di);
		BUG_ON(ret);
		if (ret)
			goto out;
		btrfs_release_path(path);
		btrfs_release_path(path);
	}


		/* if we don't have a value then we are removing the xattr */
again:
		if (!value)
	ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
				      name, name_len, value, size);
	if (ret == -EEXIST) {
		if (flags & XATTR_CREATE)
			goto out;
			goto out;
	} else {
		/*
		 * We can't use the path we already have since we won't have the
		 * proper locking for a delete, so release the path and
		 * re-lookup to delete the thing.
		 */
		btrfs_release_path(path);
		btrfs_release_path(path);
		di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode),
					name, name_len, -1);
		if (IS_ERR(di)) {
			ret = PTR_ERR(di);
			goto out;
		} else if (!di) {
			/* Shouldn't happen but just in case... */
			btrfs_release_path(path);
			goto again;
		}


		if (flags & XATTR_REPLACE) {
		ret = btrfs_delete_one_dir_name(trans, root, path, di);
			/* we couldn't find the attr to replace */
		if (ret)
			ret = -ENODATA;
			goto out;
			goto out;

		/*
		 * We have a value to set, so go back and try to insert it now.
		 */
		if (value) {
			btrfs_release_path(path);
			goto again;
		}
		}
	}
	}

	/* ok we have to create a completely new xattr */
	ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
				      name, name_len, value, size);
	BUG_ON(ret);
out:
out:
	btrfs_free_path(path);
	btrfs_free_path(path);
	return ret;
	return ret;