inode-item.c 11 KB

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