xref: /openbmc/linux/fs/btrfs/orphan.c (revision aa5d3003)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
27b128766SJosef Bacik /*
37b128766SJosef Bacik  * Copyright (C) 2008 Red Hat.  All rights reserved.
47b128766SJosef Bacik  */
57b128766SJosef Bacik 
67b128766SJosef Bacik #include "ctree.h"
77b128766SJosef Bacik #include "disk-io.h"
8*aa5d3003SJosef Bacik #include "orphan.h"
97b128766SJosef Bacik 
btrfs_insert_orphan_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 offset)107b128766SJosef Bacik int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
117b128766SJosef Bacik 			     struct btrfs_root *root, u64 offset)
127b128766SJosef Bacik {
137b128766SJosef Bacik 	struct btrfs_path *path;
147b128766SJosef Bacik 	struct btrfs_key key;
157b128766SJosef Bacik 	int ret = 0;
167b128766SJosef Bacik 
177b128766SJosef Bacik 	key.objectid = BTRFS_ORPHAN_OBJECTID;
18962a298fSDavid Sterba 	key.type = BTRFS_ORPHAN_ITEM_KEY;
197b128766SJosef Bacik 	key.offset = offset;
207b128766SJosef Bacik 
217b128766SJosef Bacik 	path = btrfs_alloc_path();
227b128766SJosef Bacik 	if (!path)
237b128766SJosef Bacik 		return -ENOMEM;
247b128766SJosef Bacik 
257b128766SJosef Bacik 	ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
267b128766SJosef Bacik 
277b128766SJosef Bacik 	btrfs_free_path(path);
287b128766SJosef Bacik 	return ret;
297b128766SJosef Bacik }
307b128766SJosef Bacik 
btrfs_del_orphan_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 offset)317b128766SJosef Bacik int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
327b128766SJosef Bacik 			  struct btrfs_root *root, u64 offset)
337b128766SJosef Bacik {
347b128766SJosef Bacik 	struct btrfs_path *path;
357b128766SJosef Bacik 	struct btrfs_key key;
367b128766SJosef Bacik 	int ret = 0;
377b128766SJosef Bacik 
387b128766SJosef Bacik 	key.objectid = BTRFS_ORPHAN_OBJECTID;
39962a298fSDavid Sterba 	key.type = BTRFS_ORPHAN_ITEM_KEY;
407b128766SJosef Bacik 	key.offset = offset;
417b128766SJosef Bacik 
427b128766SJosef Bacik 	path = btrfs_alloc_path();
437b128766SJosef Bacik 	if (!path)
447b128766SJosef Bacik 		return -ENOMEM;
457b128766SJosef Bacik 
467b128766SJosef Bacik 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
477e1fea73SJosef Bacik 	if (ret < 0)
487b128766SJosef Bacik 		goto out;
4979787eaaSJeff Mahoney 	if (ret) { /* JDM: Really? */
507e1fea73SJosef Bacik 		ret = -ENOENT;
517e1fea73SJosef Bacik 		goto out;
527e1fea73SJosef Bacik 	}
537b128766SJosef Bacik 
547b128766SJosef Bacik 	ret = btrfs_del_item(trans, root, path);
557b128766SJosef Bacik 
567b128766SJosef Bacik out:
577b128766SJosef Bacik 	btrfs_free_path(path);
587b128766SJosef Bacik 	return ret;
597b128766SJosef Bacik }
60