dir-item.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <linux/module.h>
  2. #include "ctree.h"
  3. #include "disk-io.h"
  4. #include "hash.h"
  5. #include "transaction.h"
  6. int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
  7. *root, const char *name, int name_len, u64 dir, u64
  8. objectid, u8 type)
  9. {
  10. int ret = 0;
  11. struct btrfs_path path;
  12. struct btrfs_dir_item *dir_item;
  13. char *name_ptr;
  14. struct btrfs_key key;
  15. u32 data_size;
  16. key.objectid = dir;
  17. key.flags = 0;
  18. btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
  19. ret = btrfs_name_hash(name, name_len, &key.offset);
  20. BUG_ON(ret);
  21. btrfs_init_path(&path);
  22. data_size = sizeof(*dir_item) + name_len;
  23. ret = btrfs_insert_empty_item(trans, root, &path, &key, data_size);
  24. if (ret)
  25. goto out;
  26. dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
  27. path.slots[0],
  28. struct btrfs_dir_item);
  29. btrfs_set_dir_objectid(dir_item, objectid);
  30. btrfs_set_dir_type(dir_item, type);
  31. btrfs_set_dir_flags(dir_item, 0);
  32. btrfs_set_dir_name_len(dir_item, name_len);
  33. name_ptr = (char *)(dir_item + 1);
  34. memcpy(name_ptr, name, name_len);
  35. if (name_ptr + name_len > path.nodes[0]->b_data + 4096)
  36. WARN_ON(1);
  37. mark_buffer_dirty(path.nodes[0]);
  38. out:
  39. btrfs_release_path(root, &path);
  40. return ret;
  41. }
  42. int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
  43. *root, struct btrfs_path *path, u64 dir,
  44. const char *name, int name_len, int mod)
  45. {
  46. int ret;
  47. struct btrfs_key key;
  48. int ins_len = mod < 0 ? -1 : 0;
  49. int cow = mod != 0;
  50. key.objectid = dir;
  51. key.flags = 0;
  52. btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
  53. ret = btrfs_name_hash(name, name_len, &key.offset);
  54. BUG_ON(ret);
  55. ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
  56. return ret;
  57. }
  58. int btrfs_match_dir_item_name(struct btrfs_root *root,
  59. struct btrfs_path *path,
  60. const char *name, int name_len)
  61. {
  62. struct btrfs_dir_item *dir_item;
  63. char *name_ptr;
  64. dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  65. path->slots[0],
  66. struct btrfs_dir_item);
  67. if (btrfs_dir_name_len(dir_item) != name_len)
  68. return 0;
  69. name_ptr = (char *)(dir_item + 1);
  70. if (memcmp(name_ptr, name, name_len))
  71. return 0;
  72. return 1;
  73. }