orphan.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) 2008 Red Hat. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include "ctree.h"
  19. #include "disk-io.h"
  20. int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
  21. struct btrfs_root *root, u64 offset)
  22. {
  23. struct btrfs_path *path;
  24. struct btrfs_key key;
  25. int ret = 0;
  26. key.objectid = BTRFS_ORPHAN_OBJECTID;
  27. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  28. key.offset = offset;
  29. path = btrfs_alloc_path();
  30. if (!path)
  31. return -ENOMEM;
  32. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  33. btrfs_free_path(path);
  34. return ret;
  35. }
  36. int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
  37. struct btrfs_root *root, u64 offset)
  38. {
  39. struct btrfs_path *path;
  40. struct btrfs_key key;
  41. int ret = 0;
  42. key.objectid = BTRFS_ORPHAN_OBJECTID;
  43. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  44. key.offset = offset;
  45. path = btrfs_alloc_path();
  46. if (!path)
  47. return -ENOMEM;
  48. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  49. if (ret)
  50. goto out;
  51. ret = btrfs_del_item(trans, root, path);
  52. out:
  53. btrfs_free_path(path);
  54. return ret;
  55. }
  56. int btrfs_find_orphan_item(struct btrfs_root *root, u64 offset)
  57. {
  58. struct btrfs_path *path;
  59. struct btrfs_key key;
  60. int ret;
  61. key.objectid = BTRFS_ORPHAN_OBJECTID;
  62. key.type = BTRFS_ORPHAN_ITEM_KEY;
  63. key.offset = offset;
  64. path = btrfs_alloc_path();
  65. if (!path)
  66. return -ENOMEM;
  67. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  68. btrfs_free_path(path);
  69. return ret;
  70. }