inode-item.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. static struct btrfs_inode_ref *
  86. btrfs_lookup_inode_ref(struct btrfs_trans_handle *trans,
  87. struct btrfs_root *root,
  88. struct btrfs_path *path,
  89. const char *name, int name_len,
  90. u64 inode_objectid, u64 ref_objectid, int ins_len,
  91. int cow)
  92. {
  93. int ret;
  94. struct btrfs_key key;
  95. struct btrfs_inode_ref *ref;
  96. key.objectid = inode_objectid;
  97. key.type = BTRFS_INODE_REF_KEY;
  98. key.offset = ref_objectid;
  99. ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
  100. if (ret < 0)
  101. return ERR_PTR(ret);
  102. if (ret > 0)
  103. return NULL;
  104. if (!find_name_in_backref(path, name, name_len, &ref))
  105. return NULL;
  106. return ref;
  107. }
  108. /* Returns NULL if no extref found */
  109. struct btrfs_inode_extref *
  110. btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
  111. struct btrfs_root *root,
  112. struct btrfs_path *path,
  113. const char *name, int name_len,
  114. u64 inode_objectid, u64 ref_objectid, int ins_len,
  115. int cow)
  116. {
  117. int ret;
  118. struct btrfs_key key;
  119. struct btrfs_inode_extref *extref;
  120. key.objectid = inode_objectid;
  121. key.type = BTRFS_INODE_EXTREF_KEY;
  122. key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
  123. ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
  124. if (ret < 0)
  125. return ERR_PTR(ret);
  126. if (ret > 0)
  127. return NULL;
  128. if (!btrfs_find_name_in_ext_backref(path, ref_objectid, name, name_len, &extref))
  129. return NULL;
  130. return extref;
  131. }
  132. int btrfs_get_inode_ref_index(struct btrfs_trans_handle *trans,
  133. struct btrfs_root *root,
  134. struct btrfs_path *path,
  135. const char *name, int name_len,
  136. u64 inode_objectid, u64 ref_objectid, int mod,
  137. u64 *ret_index)
  138. {
  139. struct btrfs_inode_ref *ref;
  140. struct btrfs_inode_extref *extref;
  141. int ins_len = mod < 0 ? -1 : 0;
  142. int cow = mod != 0;
  143. ref = btrfs_lookup_inode_ref(trans, root, path, name, name_len,
  144. inode_objectid, ref_objectid, ins_len,
  145. cow);
  146. if (IS_ERR(ref))
  147. return PTR_ERR(ref);
  148. if (ref != NULL) {
  149. *ret_index = btrfs_inode_ref_index(path->nodes[0], ref);
  150. return 0;
  151. }
  152. btrfs_release_path(path);
  153. extref = btrfs_lookup_inode_extref(trans, root, path, name,
  154. name_len, inode_objectid,
  155. ref_objectid, ins_len, cow);
  156. if (IS_ERR(extref))
  157. return PTR_ERR(extref);
  158. if (extref) {
  159. *ret_index = btrfs_inode_extref_index(path->nodes[0], extref);
  160. return 0;
  161. }
  162. return -ENOENT;
  163. }
  164. int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
  165. struct btrfs_root *root,
  166. const char *name, int name_len,
  167. u64 inode_objectid, u64 ref_objectid, u64 *index)
  168. {
  169. struct btrfs_path *path;
  170. struct btrfs_key key;
  171. struct btrfs_inode_extref *extref;
  172. struct extent_buffer *leaf;
  173. int ret;
  174. int del_len = name_len + sizeof(*extref);
  175. unsigned long ptr;
  176. unsigned long item_start;
  177. u32 item_size;
  178. key.objectid = inode_objectid;
  179. btrfs_set_key_type(&key, BTRFS_INODE_EXTREF_KEY);
  180. key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
  181. path = btrfs_alloc_path();
  182. if (!path)
  183. return -ENOMEM;
  184. path->leave_spinning = 1;
  185. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  186. if (ret > 0)
  187. ret = -ENOENT;
  188. if (ret < 0)
  189. goto out;
  190. /*
  191. * Sanity check - did we find the right item for this name?
  192. * This should always succeed so error here will make the FS
  193. * readonly.
  194. */
  195. if (!btrfs_find_name_in_ext_backref(path, ref_objectid,
  196. name, name_len, &extref)) {
  197. btrfs_std_error(root->fs_info, -ENOENT);
  198. ret = -EROFS;
  199. goto out;
  200. }
  201. leaf = path->nodes[0];
  202. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  203. if (index)
  204. *index = btrfs_inode_extref_index(leaf, extref);
  205. if (del_len == item_size) {
  206. /*
  207. * Common case only one ref in the item, remove the
  208. * whole item.
  209. */
  210. ret = btrfs_del_item(trans, root, path);
  211. goto out;
  212. }
  213. ptr = (unsigned long)extref;
  214. item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
  215. memmove_extent_buffer(leaf, ptr, ptr + del_len,
  216. item_size - (ptr + del_len - item_start));
  217. btrfs_truncate_item(trans, root, path, item_size - del_len, 1);
  218. out:
  219. btrfs_free_path(path);
  220. return ret;
  221. }
  222. int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
  223. struct btrfs_root *root,
  224. const char *name, int name_len,
  225. u64 inode_objectid, u64 ref_objectid, u64 *index)
  226. {
  227. struct btrfs_path *path;
  228. struct btrfs_key key;
  229. struct btrfs_inode_ref *ref;
  230. struct extent_buffer *leaf;
  231. unsigned long ptr;
  232. unsigned long item_start;
  233. u32 item_size;
  234. u32 sub_item_len;
  235. int ret;
  236. int search_ext_refs = 0;
  237. int del_len = name_len + sizeof(*ref);
  238. key.objectid = inode_objectid;
  239. key.offset = ref_objectid;
  240. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  241. path = btrfs_alloc_path();
  242. if (!path)
  243. return -ENOMEM;
  244. path->leave_spinning = 1;
  245. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  246. if (ret > 0) {
  247. ret = -ENOENT;
  248. search_ext_refs = 1;
  249. goto out;
  250. } else if (ret < 0) {
  251. goto out;
  252. }
  253. if (!find_name_in_backref(path, name, name_len, &ref)) {
  254. ret = -ENOENT;
  255. search_ext_refs = 1;
  256. goto out;
  257. }
  258. leaf = path->nodes[0];
  259. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  260. if (index)
  261. *index = btrfs_inode_ref_index(leaf, ref);
  262. if (del_len == item_size) {
  263. ret = btrfs_del_item(trans, root, path);
  264. goto out;
  265. }
  266. ptr = (unsigned long)ref;
  267. sub_item_len = name_len + sizeof(*ref);
  268. item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
  269. memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
  270. item_size - (ptr + sub_item_len - item_start));
  271. btrfs_truncate_item(trans, root, path, item_size - sub_item_len, 1);
  272. out:
  273. btrfs_free_path(path);
  274. if (search_ext_refs) {
  275. /*
  276. * No refs were found, or we could not find the
  277. * name in our ref array. Find and remove the extended
  278. * inode ref then.
  279. */
  280. return btrfs_del_inode_extref(trans, root, name, name_len,
  281. inode_objectid, ref_objectid, index);
  282. }
  283. return ret;
  284. }
  285. /*
  286. * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree.
  287. *
  288. * The caller must have checked against BTRFS_LINK_MAX already.
  289. */
  290. static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
  291. struct btrfs_root *root,
  292. const char *name, int name_len,
  293. u64 inode_objectid, u64 ref_objectid, u64 index)
  294. {
  295. struct btrfs_inode_extref *extref;
  296. int ret;
  297. int ins_len = name_len + sizeof(*extref);
  298. unsigned long ptr;
  299. struct btrfs_path *path;
  300. struct btrfs_key key;
  301. struct extent_buffer *leaf;
  302. struct btrfs_item *item;
  303. key.objectid = inode_objectid;
  304. key.type = BTRFS_INODE_EXTREF_KEY;
  305. key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
  306. path = btrfs_alloc_path();
  307. if (!path)
  308. return -ENOMEM;
  309. path->leave_spinning = 1;
  310. ret = btrfs_insert_empty_item(trans, root, path, &key,
  311. ins_len);
  312. if (ret == -EEXIST) {
  313. if (btrfs_find_name_in_ext_backref(path, ref_objectid,
  314. name, name_len, NULL))
  315. goto out;
  316. btrfs_extend_item(trans, root, path, ins_len);
  317. ret = 0;
  318. }
  319. if (ret < 0)
  320. goto out;
  321. leaf = path->nodes[0];
  322. item = btrfs_item_nr(leaf, path->slots[0]);
  323. ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char);
  324. ptr += btrfs_item_size(leaf, item) - ins_len;
  325. extref = (struct btrfs_inode_extref *)ptr;
  326. btrfs_set_inode_extref_name_len(path->nodes[0], extref, name_len);
  327. btrfs_set_inode_extref_index(path->nodes[0], extref, index);
  328. btrfs_set_inode_extref_parent(path->nodes[0], extref, ref_objectid);
  329. ptr = (unsigned long)&extref->name;
  330. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  331. btrfs_mark_buffer_dirty(path->nodes[0]);
  332. out:
  333. btrfs_free_path(path);
  334. return ret;
  335. }
  336. /* Will return 0, -ENOMEM, -EMLINK, or -EEXIST or anything from the CoW path */
  337. int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
  338. struct btrfs_root *root,
  339. const char *name, int name_len,
  340. u64 inode_objectid, u64 ref_objectid, u64 index)
  341. {
  342. struct btrfs_path *path;
  343. struct btrfs_key key;
  344. struct btrfs_inode_ref *ref;
  345. unsigned long ptr;
  346. int ret;
  347. int ins_len = name_len + sizeof(*ref);
  348. key.objectid = inode_objectid;
  349. key.offset = ref_objectid;
  350. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  351. path = btrfs_alloc_path();
  352. if (!path)
  353. return -ENOMEM;
  354. path->leave_spinning = 1;
  355. ret = btrfs_insert_empty_item(trans, root, path, &key,
  356. ins_len);
  357. if (ret == -EEXIST) {
  358. u32 old_size;
  359. if (find_name_in_backref(path, name, name_len, &ref))
  360. goto out;
  361. old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  362. btrfs_extend_item(trans, root, path, ins_len);
  363. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  364. struct btrfs_inode_ref);
  365. ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
  366. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  367. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  368. ptr = (unsigned long)(ref + 1);
  369. ret = 0;
  370. } else if (ret < 0) {
  371. if (ret == -EOVERFLOW)
  372. ret = -EMLINK;
  373. goto out;
  374. } else {
  375. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  376. struct btrfs_inode_ref);
  377. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  378. btrfs_set_inode_ref_index(path->nodes[0], ref, index);
  379. ptr = (unsigned long)(ref + 1);
  380. }
  381. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  382. btrfs_mark_buffer_dirty(path->nodes[0]);
  383. out:
  384. btrfs_free_path(path);
  385. if (ret == -EMLINK) {
  386. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  387. /* We ran out of space in the ref array. Need to
  388. * add an extended ref. */
  389. if (btrfs_super_incompat_flags(disk_super)
  390. & BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
  391. ret = btrfs_insert_inode_extref(trans, root, name,
  392. name_len,
  393. inode_objectid,
  394. ref_objectid, index);
  395. }
  396. return ret;
  397. }
  398. int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
  399. struct btrfs_root *root,
  400. struct btrfs_path *path, u64 objectid)
  401. {
  402. struct btrfs_key key;
  403. int ret;
  404. key.objectid = objectid;
  405. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  406. key.offset = 0;
  407. ret = btrfs_insert_empty_item(trans, root, path, &key,
  408. sizeof(struct btrfs_inode_item));
  409. return ret;
  410. }
  411. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  412. *root, struct btrfs_path *path,
  413. struct btrfs_key *location, int mod)
  414. {
  415. int ins_len = mod < 0 ? -1 : 0;
  416. int cow = mod != 0;
  417. int ret;
  418. int slot;
  419. struct extent_buffer *leaf;
  420. struct btrfs_key found_key;
  421. ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
  422. if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY &&
  423. location->offset == (u64)-1 && path->slots[0] != 0) {
  424. slot = path->slots[0] - 1;
  425. leaf = path->nodes[0];
  426. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  427. if (found_key.objectid == location->objectid &&
  428. btrfs_key_type(&found_key) == btrfs_key_type(location)) {
  429. path->slots[0]--;
  430. return 0;
  431. }
  432. }
  433. return ret;
  434. }