inode-item.c 13 KB

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