xref: /linux/fs/btrfs/orphan.c (revision 7a40974fd0efa3698de4c6d1d0ee0436bcc4445d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2008 Red Hat.  All rights reserved.
4  */
5 
6 #include "ctree.h"
7 #include "orphan.h"
8 
btrfs_insert_orphan_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 offset)9 int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
10 			     struct btrfs_root *root, u64 offset)
11 {
12 	BTRFS_PATH_AUTO_FREE(path);
13 	struct btrfs_key key;
14 
15 	key.objectid = BTRFS_ORPHAN_OBJECTID;
16 	key.type = BTRFS_ORPHAN_ITEM_KEY;
17 	key.offset = offset;
18 
19 	path = btrfs_alloc_path();
20 	if (!path)
21 		return -ENOMEM;
22 
23 	return btrfs_insert_empty_item(trans, root, path, &key, 0);
24 }
25 
btrfs_del_orphan_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 offset)26 int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
27 			  struct btrfs_root *root, u64 offset)
28 {
29 	BTRFS_PATH_AUTO_FREE(path);
30 	struct btrfs_key key;
31 	int ret = 0;
32 
33 	key.objectid = BTRFS_ORPHAN_OBJECTID;
34 	key.type = BTRFS_ORPHAN_ITEM_KEY;
35 	key.offset = offset;
36 
37 	path = btrfs_alloc_path();
38 	if (!path)
39 		return -ENOMEM;
40 
41 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
42 	if (ret < 0)
43 		return ret;
44 	if (ret)
45 		return -ENOENT;
46 
47 	return btrfs_del_item(trans, root, path);
48 }
49