inode-map.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (C) 2007 Oracle. 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. #include "transaction.h"
  21. int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
  22. {
  23. struct btrfs_path *path;
  24. int ret;
  25. struct extent_buffer *l;
  26. struct btrfs_key search_key;
  27. struct btrfs_key found_key;
  28. int slot;
  29. path = btrfs_alloc_path();
  30. BUG_ON(!path);
  31. search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
  32. search_key.type = -1;
  33. search_key.offset = (u64)-1;
  34. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  35. if (ret < 0)
  36. goto error;
  37. BUG_ON(ret == 0);
  38. if (path->slots[0] > 0) {
  39. slot = path->slots[0] - 1;
  40. l = path->nodes[0];
  41. btrfs_item_key_to_cpu(l, &found_key, slot);
  42. *objectid = max_t(u64, found_key.objectid,
  43. BTRFS_FIRST_FREE_OBJECTID - 1);
  44. } else {
  45. *objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
  46. }
  47. ret = 0;
  48. error:
  49. btrfs_free_path(path);
  50. return ret;
  51. }
  52. int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
  53. struct btrfs_root *root,
  54. u64 dirid, u64 *objectid)
  55. {
  56. int ret;
  57. mutex_lock(&root->objectid_mutex);
  58. if (unlikely(root->highest_objectid < BTRFS_FIRST_FREE_OBJECTID)) {
  59. ret = btrfs_find_highest_inode(root, &root->highest_objectid);
  60. if (ret)
  61. goto out;
  62. }
  63. if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
  64. ret = -ENOSPC;
  65. goto out;
  66. }
  67. *objectid = ++root->highest_objectid;
  68. ret = 0;
  69. out:
  70. mutex_unlock(&root->objectid_mutex);
  71. return ret;
  72. }