David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 Red Hat. All rights reserved. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include "ctree.h" |
| 7 | #include "disk-io.h" |
| 8 | |
| 9 | int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans, |
| 10 | struct btrfs_root *root, u64 offset) |
| 11 | { |
| 12 | struct btrfs_path *path; |
| 13 | struct btrfs_key key; |
| 14 | int ret = 0; |
| 15 | |
| 16 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 17 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 18 | key.offset = offset; |
| 19 | |
| 20 | path = btrfs_alloc_path(); |
| 21 | if (!path) |
| 22 | return -ENOMEM; |
| 23 | |
| 24 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
| 25 | |
| 26 | btrfs_free_path(path); |
| 27 | return ret; |
| 28 | } |
| 29 | |
| 30 | int btrfs_del_orphan_item(struct btrfs_trans_handle *trans, |
| 31 | struct btrfs_root *root, u64 offset) |
| 32 | { |
| 33 | struct btrfs_path *path; |
| 34 | struct btrfs_key key; |
| 35 | int ret = 0; |
| 36 | |
| 37 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 38 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 39 | key.offset = offset; |
| 40 | |
| 41 | path = btrfs_alloc_path(); |
| 42 | if (!path) |
| 43 | return -ENOMEM; |
| 44 | |
| 45 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Josef Bacik | 7e1fea7 | 2010-12-08 12:22:34 -0500 | [diff] [blame] | 46 | if (ret < 0) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 47 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 48 | if (ret) { /* JDM: Really? */ |
Josef Bacik | 7e1fea7 | 2010-12-08 12:22:34 -0500 | [diff] [blame] | 49 | ret = -ENOENT; |
| 50 | goto out; |
| 51 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 52 | |
| 53 | ret = btrfs_del_item(trans, root, path); |
| 54 | |
| 55 | out: |
| 56 | btrfs_free_path(path); |
| 57 | return ret; |
| 58 | } |