inode-item.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include "ctree.h"
  6. #include "disk-io.h"
  7. #include "transaction.h"
  8. #include "print-tree.h"
  9. struct btrfs_inode_ref *btrfs_find_name_in_backref(struct extent_buffer *leaf,
  10. int slot, const char *name,
  11. int name_len)
  12. {
  13. struct btrfs_inode_ref *ref;
  14. unsigned long ptr;
  15. unsigned long name_ptr;
  16. u32 item_size;
  17. u32 cur_offset = 0;
  18. int len;
  19. item_size = btrfs_item_size_nr(leaf, slot);
  20. ptr = btrfs_item_ptr_offset(leaf, slot);
  21. while (cur_offset < item_size) {
  22. ref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  23. len = btrfs_inode_ref_name_len(leaf, ref);
  24. name_ptr = (unsigned long)(ref + 1);
  25. cur_offset += len + sizeof(*ref);
  26. if (len != name_len)
  27. continue;
  28. if (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
  29. return ref;
  30. }
  31. return NULL;
  32. }
  33. struct btrfs_inode_extref *btrfs_find_name_in_ext_backref(
  34. struct extent_buffer *leaf, int slot, u64 ref_objectid,
  35. const char *name, int name_len)
  36. {
  37. struct btrfs_inode_extref *extref;
  38. unsigned long ptr;
  39. unsigned long name_ptr;
  40. u32 item_size;
  41. u32 cur_offset = 0;
  42. int ref_name_len;
  43. item_size = btrfs_item_size_nr(leaf, slot);
  44. ptr = btrfs_item_ptr_offset(leaf, slot);
  45. /*
  46. * Search all extended backrefs in this item. We're only
  47. * looking through any collisions so most of the time this is
  48. * just going to compare against one buffer. If all is well,
  49. * we'll return success and the inode ref object.
  50. */
  51. while (cur_offset < item_size) {
  52. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  53. name_ptr = (unsigned long)(&extref->name);
  54. ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
  55. if (ref_name_len == name_len &&
  56. btrfs_inode_extref_parent(leaf, extref) == ref_objectid &&
  57. (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0))
  58. return extref;
  59. cur_offset += ref_name_len + sizeof(*extref);
  60. }
  61. return NULL;
  62. }
  63. /* Returns NULL if no extref found */
  64. struct btrfs_inode_extref *
  65. btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
  66. struct btrfs_root *root,
  67. struct btrfs_path *path,
  68. const char *name, int name_len,
  69. u64 inode_objectid, u64 ref_objectid, int ins_len,
  70. int cow)
  71. {
  72. int ret;
  73. struct btrfs_key key;
  74. key.objectid = inode_objectid;
  75. key.type = BTRFS_INODE_EXTREF_KEY;
  76. key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
  77. ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
  78. if (ret < 0)
  79. return ERR_PTR(ret);
  80. if (ret > 0)
  81. return NULL;
  82. return btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
  83. ref_objectid, name, name_len);
  84. }
  85. static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
  86. struct btrfs_root *root,
  87. const char *name, int name_len,
  88. u64 inode_objectid, u64 ref_objectid,
  89. u64 *index)
  90. {
  91. struct btrfs_path *path;
  92. struct btrfs_key key;
  93. struct btrfs_inode_extref *extref;
  94. struct extent_buffer *leaf;
  95. int ret;
  96. int del_len = name_len + sizeof(*extref);
  97. unsigned long ptr;
  98. unsigned long item_start;
  99. u32 item_size;
  100. key.objectid = inode_objectid;
  101. key.type = BTRFS_INODE_EXTREF_KEY;
  102. key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
  103. path = btrfs_alloc_path();
  104. if (!path)
  105. return -ENOMEM;
  106. path->leave_spinning = 1;
  107. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  108. if (ret > 0)
  109. ret = -ENOENT;
  110. if (ret < 0)
  111. goto out;
  112. /*
  113. * Sanity check - did we find the right item for this name?
  114. * This should always succeed so error here will make the FS
  115. * readonly.
  116. */
  117. extref = btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0],
  118. ref_objectid, name, name_len);
  119. if (!extref) {
  120. btrfs_handle_fs_error(root->fs_info, -ENOENT, NULL);
  121. ret = -EROFS;
  122. goto out;
  123. }
  124. leaf = path->nodes[0];
  125. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  126. if (index)
  127. *index = btrfs_inode_extref_index(leaf, extref);
  128. if (del_len == item_size) {
  129. /*
  130. * Common case only one ref in the item, remove the
  131. * whole item.
  132. */
  133. ret = btrfs_del_item(trans, root, path);
  134. goto out;
  135. }
  136. ptr = (unsigned long)extref;
  137. item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
  138. memmove_extent_buffer(leaf, ptr, ptr + del_len,
  139. item_size - (ptr + del_len - item_start));
  140. btrfs_truncate_item(path, item_size - del_len, 1);
  141. out:
  142. btrfs_free_path(path);
  143. return ret;
  144. }
  145. int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
  146. struct btrfs_root *root,
  147. const char *name, int name_len,
  148. u64 inode_objectid, u64 ref_objectid, u64 *index)
  149. {
  150. struct btrfs_path *path;
  151. struct btrfs_key key;
  152. struct btrfs_inode_ref *ref;
  153. struct extent_buffer *leaf;
  154. unsigned long ptr;
  155. unsigned long item_start;
  156. u32 item_size;
  157. u32 sub_item_len;
  158. int ret;
  159. int search_ext_refs = 0;
  160. int del_len = name_len + sizeof(*ref);
  161. key.objectid = inode_objectid;
  162. key.offset = ref_objectid;
  163. key.type = BTRFS_INODE_REF_KEY;
  164. path = btrfs_alloc_path();
  165. if (!path)
  166. return -ENOMEM;
  167. path->leave_spinning = 1;
  168. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  169. if (ret > 0) {
  170. ret = -ENOENT;
  171. search_ext_refs = 1;
  172. goto out;
  173. } else if (ret < 0) {
  174. goto out;
  175. }
  176. ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], name,
  177. name_len);
  178. if (!ref) {
  179. ret = -ENOENT;
  180. search_ext_refs = 1;
  181. goto out;
  182. }
  183. leaf = path->nodes[0];
  184. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  185. if (index)
  186. *index = btrfs_inode_ref_index(leaf, ref);
  187. if (del_len == item_size) {
  188. ret = btrfs_del_item(trans, root, path);
  189. goto out;
  190. }
  191. ptr = (unsigned long)ref;
  192. sub_item_len = name_len + sizeof(*ref);
  193. item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
  194. memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
  195. item_size - (ptr + sub_item_len - item_start));
  196. btrfs_truncate_item(path, item_size - sub_item_len, 1);
  197. out:
  198. btrfs_free_path(path);
  199. if (search_ext_refs) {
  200. /*
  201. * No refs were found, or we could not find the
  202. * name in our ref array. Find and remove the extended
  203. * inode ref then.
  204. */
  205. return btrfs_del_inode_extref(trans, root, name, name_len,
  206. inode_objectid, ref_objectid, index);
  207. }
  208. return ret;
  209. }
  210. /*
  211. * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree.
  212. *
  213. * The caller must have checked against BTRFS_LINK_MAX already.
  214. */
  215. static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
  216. struct btrfs_root *root,
  217. const char *name, int name_len,
  218. u64 inode_objectid, u64 ref_objectid, u64 index)
  219. {
  220. struct btrfs_inode_extref *extref;
  221. int ret;
  222. int ins_len = name_len + sizeof(*extref);
  223. unsigned long ptr;
  224. struct btrfs_path *path;
  225. struct btrfs_key key;
  226. struct extent_buffer *leaf;
  227. struct btrfs_item *item;
  228. key.objectid = inode_objectid;
  229. key.type = BTRFS_INODE_EXTREF_KEY;
  230. key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
  231. path = btrfs_alloc_path();
  232. if (!path)
  233. return -ENOMEM;
  234. path->leave_spinning = 1;
  235. ret = btrfs_insert_empty_item(trans, root, path, &key,
  236. ins_len);
  237. if (ret == -EEXIST) {
  238. if (btrfs_find_name_in_ext_backref(path->nodes[0],
  239. path->slots[0],
  240. ref_objectid,
  241. name, name_len))
  242. goto out;
  243. btrfs_extend_item(path, ins_len);
  244. ret = 0;
  245. }
  246. if (ret < 0)
  247. goto out;
  248. leaf = path->nodes[0];
  249. item = btrfs_item_nr(path->slots[0]);
  250. ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char);
  251. ptr += btrfs_item_size(leaf, item) - ins_len;
  252. extref = (struct btrfs_inode_extref *)ptr;
  253. btrfs_set_inode_extref_name_len(path->nodes[0], extref, name_len);
  254. btrfs_set_inode_extref_index(path->nodes[0], extref, index);
  255. btrfs_set_inode_extref_parent(path->nodes[0], extref, ref_objectid);
  256. ptr = (unsigned long)&extref->name;
  257. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  258. btrfs_mark_buffer_dirty(path->nodes[0]);
  259. out:
  260. btrfs_free_path(path);
  261. return ret;
  262. }
  263. /* Will return 0, -ENOMEM, -EMLINK, or -EEXIST or anything from the CoW path */
  264. int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
  265. struct btrfs_root *root,
  266. const char *name, int name_len,
  267. u64 inode_objectid, u64 ref_objectid, u64 index)
  268. {
  269. struct btrfs_fs_info *fs_info = root->fs_info;
  270. struct btrfs_path *path;
  271. struct btrfs_key key;
  272. struct btrfs_inode_ref *ref;
  273. unsigned long ptr;
  274. int ret;
  275. int ins_len = name_len + sizeof(*ref);
  276. key.objectid = inode_objectid;
  277. key.offset = ref_objectid;
  278. key.type = BTRFS_INODE_REF_KEY;
  279. path = btrfs_alloc_path();
  280. if (!path)
  281. return -ENOMEM;
  282. path->leave_spinning = 1;
  283. path->skip_release_on_error = 1;
  284. ret = btrfs_insert_empty_item(trans, root, path, &key,
  285. ins_len);
  286. if (ret == -EEXIST) {
  287. u32 old_size;
  288. ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
  289. name, name_len);
  290. if (ref)
  291. goto out;
  292. old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  293. btrfs_extend_item(path, ins_len);
  294. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  295. struct btrfs_inode_ref);
  296. ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
  297. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  298. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  299. ptr = (unsigned long)(ref + 1);
  300. ret = 0;
  301. } else if (ret < 0) {
  302. if (ret == -EOVERFLOW) {
  303. if (btrfs_find_name_in_backref(path->nodes[0],
  304. path->slots[0],
  305. name, name_len))
  306. ret = -EEXIST;
  307. else
  308. ret = -EMLINK;
  309. }
  310. goto out;
  311. } else {
  312. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  313. struct btrfs_inode_ref);
  314. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  315. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  316. ptr = (unsigned long)(ref + 1);
  317. }
  318. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  319. btrfs_mark_buffer_dirty(path->nodes[0]);
  320. out:
  321. btrfs_free_path(path);
  322. if (ret == -EMLINK) {
  323. struct btrfs_super_block *disk_super = fs_info->super_copy;
  324. /* We ran out of space in the ref array. Need to
  325. * add an extended ref. */
  326. if (btrfs_super_incompat_flags(disk_super)
  327. & BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
  328. ret = btrfs_insert_inode_extref(trans, root, name,
  329. name_len,
  330. inode_objectid,
  331. ref_objectid, index);
  332. }
  333. return ret;
  334. }
  335. int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
  336. struct btrfs_root *root,
  337. struct btrfs_path *path, u64 objectid)
  338. {
  339. struct btrfs_key key;
  340. int ret;
  341. key.objectid = objectid;
  342. key.type = BTRFS_INODE_ITEM_KEY;
  343. key.offset = 0;
  344. ret = btrfs_insert_empty_item(trans, root, path, &key,
  345. sizeof(struct btrfs_inode_item));
  346. return ret;
  347. }
  348. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  349. *root, struct btrfs_path *path,
  350. struct btrfs_key *location, int mod)
  351. {
  352. int ins_len = mod < 0 ? -1 : 0;
  353. int cow = mod != 0;
  354. int ret;
  355. int slot;
  356. struct extent_buffer *leaf;
  357. struct btrfs_key found_key;
  358. ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
  359. if (ret > 0 && location->type == BTRFS_ROOT_ITEM_KEY &&
  360. location->offset == (u64)-1 && path->slots[0] != 0) {
  361. slot = path->slots[0] - 1;
  362. leaf = path->nodes[0];
  363. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  364. if (found_key.objectid == location->objectid &&
  365. found_key.type == location->type) {
  366. path->slots[0]--;
  367. return 0;
  368. }
  369. }
  370. return ret;
  371. }