scoutfs: add scoutfs_btree_force

Add a btree_update variant which will insert the item if a previous
wasn't found instead of returning -ENOENT.  This saves callers from
having to lookup befure updating to discover if they should call _create
or _update.

Signed-off-by: Zach Brown <zab@versity.com>
This commit is contained in:
Zach Brown
2020-01-17 11:21:36 -08:00
committed by Zach Brown
parent 2fab3b4377
commit 43d416003a
2 changed files with 40 additions and 0 deletions
+34
View File
@@ -1115,6 +1115,40 @@ int scoutfs_btree_update(struct super_block *sb,
return ret;
}
/*
* Create an item, overwriting any item that might exist. It's _update
* which will insert instead of returning -ENOENT.
*/
int scoutfs_btree_force(struct super_block *sb,
struct scoutfs_balloc_allocator *alloc,
struct scoutfs_block_writer *wri,
struct scoutfs_btree_root *root,
void *key, unsigned key_len,
void *val, unsigned val_len)
{
struct scoutfs_btree_block *bt;
struct scoutfs_block *bl;
int pos;
int cmp;
int ret;
if (invalid_item(key, key_len, val_len))
return -EINVAL;
ret = btree_walk(sb, alloc, wri, root, BTW_DIRTY | BTW_INSERT,
key, key_len, val_len, &bl, NULL, NULL);
if (ret == 0) {
bt = bl->data;
pos = find_pos(bt, key, key_len, &cmp);
if (cmp == 0)
delete_item(bt, pos);
create_item(bt, pos, key, key_len, val, val_len);
scoutfs_block_put(sb, bl);
}
return ret;
}
/*
* Delete an item from the tree. -ENOENT is returned if the key isn't
* found.
+6
View File
@@ -35,6 +35,12 @@ int scoutfs_btree_update(struct super_block *sb,
struct scoutfs_btree_root *root,
void *key, unsigned key_len,
void *val, unsigned val_len);
int scoutfs_btree_force(struct super_block *sb,
struct scoutfs_balloc_allocator *alloc,
struct scoutfs_block_writer *wri,
struct scoutfs_btree_root *root,
void *key, unsigned key_len,
void *val, unsigned val_len);
int scoutfs_btree_delete(struct super_block *sb,
struct scoutfs_balloc_allocator *alloc,
struct scoutfs_block_writer *wri,