inode-item.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <linux/module.h>
  19. #include "ctree.h"
  20. #include "disk-io.h"
  21. #include "transaction.h"
  22. int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  23. *root, u64 objectid, struct btrfs_inode_item
  24. *inode_item)
  25. {
  26. struct btrfs_path *path;
  27. struct btrfs_key key;
  28. int ret;
  29. key.objectid = objectid;
  30. key.flags = 0;
  31. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  32. key.offset = 0;
  33. path = btrfs_alloc_path();
  34. BUG_ON(!path);
  35. btrfs_init_path(path);
  36. ret = btrfs_insert_item(trans, root, &key, inode_item,
  37. sizeof(*inode_item));
  38. btrfs_release_path(root, path);
  39. btrfs_free_path(path);
  40. if (ret == 0 && objectid > root->highest_inode)
  41. root->highest_inode = objectid;
  42. return ret;
  43. }
  44. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  45. *root, struct btrfs_path *path,
  46. struct btrfs_key *location, int mod)
  47. {
  48. int ins_len = mod < 0 ? -1 : 0;
  49. int cow = mod != 0;
  50. int ret;
  51. int slot;
  52. struct btrfs_leaf *leaf;
  53. struct btrfs_key found_key;
  54. ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
  55. if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY &&
  56. location->offset == (u64)-1 && path->slots[0] != 0) {
  57. slot = path->slots[0] - 1;
  58. leaf = btrfs_buffer_leaf(path->nodes[0]);
  59. btrfs_disk_key_to_cpu(&found_key, &leaf->items[slot].key);
  60. if (found_key.objectid == location->objectid &&
  61. btrfs_key_type(&found_key) == btrfs_key_type(location)) {
  62. path->slots[0]--;
  63. return 0;
  64. }
  65. }
  66. return ret;
  67. }