inode-item.c 12 KB

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