backref.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. /*
  2. * Copyright (C) 2011 STRATO. 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 "backref.h"
  21. #include "ulist.h"
  22. #include "transaction.h"
  23. #include "delayed-ref.h"
  24. #include "locking.h"
  25. /*
  26. * this structure records all encountered refs on the way up to the root
  27. */
  28. struct __prelim_ref {
  29. struct list_head list;
  30. u64 root_id;
  31. struct btrfs_key key_for_search;
  32. int level;
  33. int count;
  34. u64 parent;
  35. u64 wanted_disk_byte;
  36. };
  37. /*
  38. * the rules for all callers of this function are:
  39. * - obtaining the parent is the goal
  40. * - if you add a key, you must know that it is a correct key
  41. * - if you cannot add the parent or a correct key, then we will look into the
  42. * block later to set a correct key
  43. *
  44. * delayed refs
  45. * ============
  46. * backref type | shared | indirect | shared | indirect
  47. * information | tree | tree | data | data
  48. * --------------------+--------+----------+--------+----------
  49. * parent logical | y | - | - | -
  50. * key to resolve | - | y | y | y
  51. * tree block logical | - | - | - | -
  52. * root for resolving | y | y | y | y
  53. *
  54. * - column 1: we've the parent -> done
  55. * - column 2, 3, 4: we use the key to find the parent
  56. *
  57. * on disk refs (inline or keyed)
  58. * ==============================
  59. * backref type | shared | indirect | shared | indirect
  60. * information | tree | tree | data | data
  61. * --------------------+--------+----------+--------+----------
  62. * parent logical | y | - | y | -
  63. * key to resolve | - | - | - | y
  64. * tree block logical | y | y | y | y
  65. * root for resolving | - | y | y | y
  66. *
  67. * - column 1, 3: we've the parent -> done
  68. * - column 2: we take the first key from the block to find the parent
  69. * (see __add_missing_keys)
  70. * - column 4: we use the key to find the parent
  71. *
  72. * additional information that's available but not required to find the parent
  73. * block might help in merging entries to gain some speed.
  74. */
  75. static int __add_prelim_ref(struct list_head *head, u64 root_id,
  76. struct btrfs_key *key, int level,
  77. u64 parent, u64 wanted_disk_byte, int count)
  78. {
  79. struct __prelim_ref *ref;
  80. /* in case we're adding delayed refs, we're holding the refs spinlock */
  81. ref = kmalloc(sizeof(*ref), GFP_ATOMIC);
  82. if (!ref)
  83. return -ENOMEM;
  84. ref->root_id = root_id;
  85. if (key)
  86. ref->key_for_search = *key;
  87. else
  88. memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
  89. ref->level = level;
  90. ref->count = count;
  91. ref->parent = parent;
  92. ref->wanted_disk_byte = wanted_disk_byte;
  93. list_add_tail(&ref->list, head);
  94. return 0;
  95. }
  96. static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
  97. struct ulist *parents,
  98. struct extent_buffer *eb, int level,
  99. u64 wanted_objectid, u64 wanted_disk_byte)
  100. {
  101. int ret;
  102. int slot;
  103. struct btrfs_file_extent_item *fi;
  104. struct btrfs_key key;
  105. u64 disk_byte;
  106. add_parent:
  107. ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
  108. if (ret < 0)
  109. return ret;
  110. if (level != 0)
  111. return 0;
  112. /*
  113. * if the current leaf is full with EXTENT_DATA items, we must
  114. * check the next one if that holds a reference as well.
  115. * ref->count cannot be used to skip this check.
  116. * repeat this until we don't find any additional EXTENT_DATA items.
  117. */
  118. while (1) {
  119. ret = btrfs_next_leaf(root, path);
  120. if (ret < 0)
  121. return ret;
  122. if (ret)
  123. return 0;
  124. eb = path->nodes[0];
  125. for (slot = 0; slot < btrfs_header_nritems(eb); ++slot) {
  126. btrfs_item_key_to_cpu(eb, &key, slot);
  127. if (key.objectid != wanted_objectid ||
  128. key.type != BTRFS_EXTENT_DATA_KEY)
  129. return 0;
  130. fi = btrfs_item_ptr(eb, slot,
  131. struct btrfs_file_extent_item);
  132. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  133. if (disk_byte == wanted_disk_byte)
  134. goto add_parent;
  135. }
  136. }
  137. return 0;
  138. }
  139. /*
  140. * resolve an indirect backref in the form (root_id, key, level)
  141. * to a logical address
  142. */
  143. static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info,
  144. int search_commit_root,
  145. struct __prelim_ref *ref,
  146. struct ulist *parents)
  147. {
  148. struct btrfs_path *path;
  149. struct btrfs_root *root;
  150. struct btrfs_key root_key;
  151. struct btrfs_key key = {0};
  152. struct extent_buffer *eb;
  153. int ret = 0;
  154. int root_level;
  155. int level = ref->level;
  156. path = btrfs_alloc_path();
  157. if (!path)
  158. return -ENOMEM;
  159. path->search_commit_root = !!search_commit_root;
  160. root_key.objectid = ref->root_id;
  161. root_key.type = BTRFS_ROOT_ITEM_KEY;
  162. root_key.offset = (u64)-1;
  163. root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  164. if (IS_ERR(root)) {
  165. ret = PTR_ERR(root);
  166. goto out;
  167. }
  168. rcu_read_lock();
  169. root_level = btrfs_header_level(root->node);
  170. rcu_read_unlock();
  171. if (root_level + 1 == level)
  172. goto out;
  173. path->lowest_level = level;
  174. ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path, 0, 0);
  175. pr_debug("search slot in root %llu (level %d, ref count %d) returned "
  176. "%d for key (%llu %u %llu)\n",
  177. (unsigned long long)ref->root_id, level, ref->count, ret,
  178. (unsigned long long)ref->key_for_search.objectid,
  179. ref->key_for_search.type,
  180. (unsigned long long)ref->key_for_search.offset);
  181. if (ret < 0)
  182. goto out;
  183. eb = path->nodes[level];
  184. if (!eb) {
  185. WARN_ON(1);
  186. ret = 1;
  187. goto out;
  188. }
  189. if (level == 0) {
  190. if (ret == 1 && path->slots[0] >= btrfs_header_nritems(eb)) {
  191. ret = btrfs_next_leaf(root, path);
  192. if (ret)
  193. goto out;
  194. eb = path->nodes[0];
  195. }
  196. btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
  197. }
  198. /* the last two parameters will only be used for level == 0 */
  199. ret = add_all_parents(root, path, parents, eb, level, key.objectid,
  200. ref->wanted_disk_byte);
  201. out:
  202. btrfs_free_path(path);
  203. return ret;
  204. }
  205. /*
  206. * resolve all indirect backrefs from the list
  207. */
  208. static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
  209. int search_commit_root,
  210. struct list_head *head)
  211. {
  212. int err;
  213. int ret = 0;
  214. struct __prelim_ref *ref;
  215. struct __prelim_ref *ref_safe;
  216. struct __prelim_ref *new_ref;
  217. struct ulist *parents;
  218. struct ulist_node *node;
  219. struct ulist_iterator uiter;
  220. parents = ulist_alloc(GFP_NOFS);
  221. if (!parents)
  222. return -ENOMEM;
  223. /*
  224. * _safe allows us to insert directly after the current item without
  225. * iterating over the newly inserted items.
  226. * we're also allowed to re-assign ref during iteration.
  227. */
  228. list_for_each_entry_safe(ref, ref_safe, head, list) {
  229. if (ref->parent) /* already direct */
  230. continue;
  231. if (ref->count == 0)
  232. continue;
  233. err = __resolve_indirect_ref(fs_info, search_commit_root,
  234. ref, parents);
  235. if (err) {
  236. if (ret == 0)
  237. ret = err;
  238. continue;
  239. }
  240. /* we put the first parent into the ref at hand */
  241. ULIST_ITER_INIT(&uiter);
  242. node = ulist_next(parents, &uiter);
  243. ref->parent = node ? node->val : 0;
  244. /* additional parents require new refs being added here */
  245. while ((node = ulist_next(parents, &uiter))) {
  246. new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS);
  247. if (!new_ref) {
  248. ret = -ENOMEM;
  249. break;
  250. }
  251. memcpy(new_ref, ref, sizeof(*ref));
  252. new_ref->parent = node->val;
  253. list_add(&new_ref->list, &ref->list);
  254. }
  255. ulist_reinit(parents);
  256. }
  257. ulist_free(parents);
  258. return ret;
  259. }
  260. static inline int ref_for_same_block(struct __prelim_ref *ref1,
  261. struct __prelim_ref *ref2)
  262. {
  263. if (ref1->level != ref2->level)
  264. return 0;
  265. if (ref1->root_id != ref2->root_id)
  266. return 0;
  267. if (ref1->key_for_search.type != ref2->key_for_search.type)
  268. return 0;
  269. if (ref1->key_for_search.objectid != ref2->key_for_search.objectid)
  270. return 0;
  271. if (ref1->key_for_search.offset != ref2->key_for_search.offset)
  272. return 0;
  273. if (ref1->parent != ref2->parent)
  274. return 0;
  275. return 1;
  276. }
  277. /*
  278. * read tree blocks and add keys where required.
  279. */
  280. static int __add_missing_keys(struct btrfs_fs_info *fs_info,
  281. struct list_head *head)
  282. {
  283. struct list_head *pos;
  284. struct extent_buffer *eb;
  285. list_for_each(pos, head) {
  286. struct __prelim_ref *ref;
  287. ref = list_entry(pos, struct __prelim_ref, list);
  288. if (ref->parent)
  289. continue;
  290. if (ref->key_for_search.type)
  291. continue;
  292. BUG_ON(!ref->wanted_disk_byte);
  293. eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte,
  294. fs_info->tree_root->leafsize, 0);
  295. BUG_ON(!eb);
  296. btrfs_tree_read_lock(eb);
  297. if (btrfs_header_level(eb) == 0)
  298. btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
  299. else
  300. btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
  301. btrfs_tree_read_unlock(eb);
  302. free_extent_buffer(eb);
  303. }
  304. return 0;
  305. }
  306. /*
  307. * merge two lists of backrefs and adjust counts accordingly
  308. *
  309. * mode = 1: merge identical keys, if key is set
  310. * FIXME: if we add more keys in __add_prelim_ref, we can merge more here.
  311. * additionally, we could even add a key range for the blocks we
  312. * looked into to merge even more (-> replace unresolved refs by those
  313. * having a parent).
  314. * mode = 2: merge identical parents
  315. */
  316. static int __merge_refs(struct list_head *head, int mode)
  317. {
  318. struct list_head *pos1;
  319. list_for_each(pos1, head) {
  320. struct list_head *n2;
  321. struct list_head *pos2;
  322. struct __prelim_ref *ref1;
  323. ref1 = list_entry(pos1, struct __prelim_ref, list);
  324. for (pos2 = pos1->next, n2 = pos2->next; pos2 != head;
  325. pos2 = n2, n2 = pos2->next) {
  326. struct __prelim_ref *ref2;
  327. struct __prelim_ref *xchg;
  328. ref2 = list_entry(pos2, struct __prelim_ref, list);
  329. if (mode == 1) {
  330. if (!ref_for_same_block(ref1, ref2))
  331. continue;
  332. if (!ref1->parent && ref2->parent) {
  333. xchg = ref1;
  334. ref1 = ref2;
  335. ref2 = xchg;
  336. }
  337. ref1->count += ref2->count;
  338. } else {
  339. if (ref1->parent != ref2->parent)
  340. continue;
  341. ref1->count += ref2->count;
  342. }
  343. list_del(&ref2->list);
  344. kfree(ref2);
  345. }
  346. }
  347. return 0;
  348. }
  349. /*
  350. * add all currently queued delayed refs from this head whose seq nr is
  351. * smaller or equal that seq to the list
  352. */
  353. static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
  354. struct list_head *prefs)
  355. {
  356. struct btrfs_delayed_extent_op *extent_op = head->extent_op;
  357. struct rb_node *n = &head->node.rb_node;
  358. struct btrfs_key key;
  359. struct btrfs_key op_key = {0};
  360. int sgn;
  361. int ret = 0;
  362. if (extent_op && extent_op->update_key)
  363. btrfs_disk_key_to_cpu(&op_key, &extent_op->key);
  364. while ((n = rb_prev(n))) {
  365. struct btrfs_delayed_ref_node *node;
  366. node = rb_entry(n, struct btrfs_delayed_ref_node,
  367. rb_node);
  368. if (node->bytenr != head->node.bytenr)
  369. break;
  370. WARN_ON(node->is_head);
  371. if (node->seq > seq)
  372. continue;
  373. switch (node->action) {
  374. case BTRFS_ADD_DELAYED_EXTENT:
  375. case BTRFS_UPDATE_DELAYED_HEAD:
  376. WARN_ON(1);
  377. continue;
  378. case BTRFS_ADD_DELAYED_REF:
  379. sgn = 1;
  380. break;
  381. case BTRFS_DROP_DELAYED_REF:
  382. sgn = -1;
  383. break;
  384. default:
  385. BUG_ON(1);
  386. }
  387. switch (node->type) {
  388. case BTRFS_TREE_BLOCK_REF_KEY: {
  389. struct btrfs_delayed_tree_ref *ref;
  390. ref = btrfs_delayed_node_to_tree_ref(node);
  391. ret = __add_prelim_ref(prefs, ref->root, &op_key,
  392. ref->level + 1, 0, node->bytenr,
  393. node->ref_mod * sgn);
  394. break;
  395. }
  396. case BTRFS_SHARED_BLOCK_REF_KEY: {
  397. struct btrfs_delayed_tree_ref *ref;
  398. ref = btrfs_delayed_node_to_tree_ref(node);
  399. ret = __add_prelim_ref(prefs, ref->root, NULL,
  400. ref->level + 1, ref->parent,
  401. node->bytenr,
  402. node->ref_mod * sgn);
  403. break;
  404. }
  405. case BTRFS_EXTENT_DATA_REF_KEY: {
  406. struct btrfs_delayed_data_ref *ref;
  407. ref = btrfs_delayed_node_to_data_ref(node);
  408. key.objectid = ref->objectid;
  409. key.type = BTRFS_EXTENT_DATA_KEY;
  410. key.offset = ref->offset;
  411. ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0,
  412. node->bytenr,
  413. node->ref_mod * sgn);
  414. break;
  415. }
  416. case BTRFS_SHARED_DATA_REF_KEY: {
  417. struct btrfs_delayed_data_ref *ref;
  418. ref = btrfs_delayed_node_to_data_ref(node);
  419. key.objectid = ref->objectid;
  420. key.type = BTRFS_EXTENT_DATA_KEY;
  421. key.offset = ref->offset;
  422. ret = __add_prelim_ref(prefs, ref->root, &key, 0,
  423. ref->parent, node->bytenr,
  424. node->ref_mod * sgn);
  425. break;
  426. }
  427. default:
  428. WARN_ON(1);
  429. }
  430. BUG_ON(ret);
  431. }
  432. return 0;
  433. }
  434. /*
  435. * add all inline backrefs for bytenr to the list
  436. */
  437. static int __add_inline_refs(struct btrfs_fs_info *fs_info,
  438. struct btrfs_path *path, u64 bytenr,
  439. int *info_level, struct list_head *prefs)
  440. {
  441. int ret = 0;
  442. int slot;
  443. struct extent_buffer *leaf;
  444. struct btrfs_key key;
  445. unsigned long ptr;
  446. unsigned long end;
  447. struct btrfs_extent_item *ei;
  448. u64 flags;
  449. u64 item_size;
  450. /*
  451. * enumerate all inline refs
  452. */
  453. leaf = path->nodes[0];
  454. slot = path->slots[0];
  455. item_size = btrfs_item_size_nr(leaf, slot);
  456. BUG_ON(item_size < sizeof(*ei));
  457. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  458. flags = btrfs_extent_flags(leaf, ei);
  459. ptr = (unsigned long)(ei + 1);
  460. end = (unsigned long)ei + item_size;
  461. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  462. struct btrfs_tree_block_info *info;
  463. info = (struct btrfs_tree_block_info *)ptr;
  464. *info_level = btrfs_tree_block_level(leaf, info);
  465. ptr += sizeof(struct btrfs_tree_block_info);
  466. BUG_ON(ptr > end);
  467. } else {
  468. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
  469. }
  470. while (ptr < end) {
  471. struct btrfs_extent_inline_ref *iref;
  472. u64 offset;
  473. int type;
  474. iref = (struct btrfs_extent_inline_ref *)ptr;
  475. type = btrfs_extent_inline_ref_type(leaf, iref);
  476. offset = btrfs_extent_inline_ref_offset(leaf, iref);
  477. switch (type) {
  478. case BTRFS_SHARED_BLOCK_REF_KEY:
  479. ret = __add_prelim_ref(prefs, 0, NULL,
  480. *info_level + 1, offset,
  481. bytenr, 1);
  482. break;
  483. case BTRFS_SHARED_DATA_REF_KEY: {
  484. struct btrfs_shared_data_ref *sdref;
  485. int count;
  486. sdref = (struct btrfs_shared_data_ref *)(iref + 1);
  487. count = btrfs_shared_data_ref_count(leaf, sdref);
  488. ret = __add_prelim_ref(prefs, 0, NULL, 0, offset,
  489. bytenr, count);
  490. break;
  491. }
  492. case BTRFS_TREE_BLOCK_REF_KEY:
  493. ret = __add_prelim_ref(prefs, offset, NULL,
  494. *info_level + 1, 0,
  495. bytenr, 1);
  496. break;
  497. case BTRFS_EXTENT_DATA_REF_KEY: {
  498. struct btrfs_extent_data_ref *dref;
  499. int count;
  500. u64 root;
  501. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  502. count = btrfs_extent_data_ref_count(leaf, dref);
  503. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  504. dref);
  505. key.type = BTRFS_EXTENT_DATA_KEY;
  506. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  507. root = btrfs_extent_data_ref_root(leaf, dref);
  508. ret = __add_prelim_ref(prefs, root, &key, 0, 0,
  509. bytenr, count);
  510. break;
  511. }
  512. default:
  513. WARN_ON(1);
  514. }
  515. BUG_ON(ret);
  516. ptr += btrfs_extent_inline_ref_size(type);
  517. }
  518. return 0;
  519. }
  520. /*
  521. * add all non-inline backrefs for bytenr to the list
  522. */
  523. static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
  524. struct btrfs_path *path, u64 bytenr,
  525. int info_level, struct list_head *prefs)
  526. {
  527. struct btrfs_root *extent_root = fs_info->extent_root;
  528. int ret;
  529. int slot;
  530. struct extent_buffer *leaf;
  531. struct btrfs_key key;
  532. while (1) {
  533. ret = btrfs_next_item(extent_root, path);
  534. if (ret < 0)
  535. break;
  536. if (ret) {
  537. ret = 0;
  538. break;
  539. }
  540. slot = path->slots[0];
  541. leaf = path->nodes[0];
  542. btrfs_item_key_to_cpu(leaf, &key, slot);
  543. if (key.objectid != bytenr)
  544. break;
  545. if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
  546. continue;
  547. if (key.type > BTRFS_SHARED_DATA_REF_KEY)
  548. break;
  549. switch (key.type) {
  550. case BTRFS_SHARED_BLOCK_REF_KEY:
  551. ret = __add_prelim_ref(prefs, 0, NULL,
  552. info_level + 1, key.offset,
  553. bytenr, 1);
  554. break;
  555. case BTRFS_SHARED_DATA_REF_KEY: {
  556. struct btrfs_shared_data_ref *sdref;
  557. int count;
  558. sdref = btrfs_item_ptr(leaf, slot,
  559. struct btrfs_shared_data_ref);
  560. count = btrfs_shared_data_ref_count(leaf, sdref);
  561. ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset,
  562. bytenr, count);
  563. break;
  564. }
  565. case BTRFS_TREE_BLOCK_REF_KEY:
  566. ret = __add_prelim_ref(prefs, key.offset, NULL,
  567. info_level + 1, 0,
  568. bytenr, 1);
  569. break;
  570. case BTRFS_EXTENT_DATA_REF_KEY: {
  571. struct btrfs_extent_data_ref *dref;
  572. int count;
  573. u64 root;
  574. dref = btrfs_item_ptr(leaf, slot,
  575. struct btrfs_extent_data_ref);
  576. count = btrfs_extent_data_ref_count(leaf, dref);
  577. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  578. dref);
  579. key.type = BTRFS_EXTENT_DATA_KEY;
  580. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  581. root = btrfs_extent_data_ref_root(leaf, dref);
  582. ret = __add_prelim_ref(prefs, root, &key, 0, 0,
  583. bytenr, count);
  584. break;
  585. }
  586. default:
  587. WARN_ON(1);
  588. }
  589. BUG_ON(ret);
  590. }
  591. return ret;
  592. }
  593. /*
  594. * this adds all existing backrefs (inline backrefs, backrefs and delayed
  595. * refs) for the given bytenr to the refs list, merges duplicates and resolves
  596. * indirect refs to their parent bytenr.
  597. * When roots are found, they're added to the roots list
  598. *
  599. * FIXME some caching might speed things up
  600. */
  601. static int find_parent_nodes(struct btrfs_trans_handle *trans,
  602. struct btrfs_fs_info *fs_info, u64 bytenr,
  603. u64 seq, struct ulist *refs, struct ulist *roots)
  604. {
  605. struct btrfs_key key;
  606. struct btrfs_path *path;
  607. struct btrfs_delayed_ref_root *delayed_refs = NULL;
  608. struct btrfs_delayed_ref_head *head;
  609. int info_level = 0;
  610. int ret;
  611. int search_commit_root = (trans == BTRFS_BACKREF_SEARCH_COMMIT_ROOT);
  612. struct list_head prefs_delayed;
  613. struct list_head prefs;
  614. struct __prelim_ref *ref;
  615. INIT_LIST_HEAD(&prefs);
  616. INIT_LIST_HEAD(&prefs_delayed);
  617. key.objectid = bytenr;
  618. key.type = BTRFS_EXTENT_ITEM_KEY;
  619. key.offset = (u64)-1;
  620. path = btrfs_alloc_path();
  621. if (!path)
  622. return -ENOMEM;
  623. path->search_commit_root = !!search_commit_root;
  624. /*
  625. * grab both a lock on the path and a lock on the delayed ref head.
  626. * We need both to get a consistent picture of how the refs look
  627. * at a specified point in time
  628. */
  629. again:
  630. head = NULL;
  631. ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
  632. if (ret < 0)
  633. goto out;
  634. BUG_ON(ret == 0);
  635. if (trans != BTRFS_BACKREF_SEARCH_COMMIT_ROOT) {
  636. /*
  637. * look if there are updates for this ref queued and lock the
  638. * head
  639. */
  640. delayed_refs = &trans->transaction->delayed_refs;
  641. spin_lock(&delayed_refs->lock);
  642. head = btrfs_find_delayed_ref_head(trans, bytenr);
  643. if (head) {
  644. if (!mutex_trylock(&head->mutex)) {
  645. atomic_inc(&head->node.refs);
  646. spin_unlock(&delayed_refs->lock);
  647. btrfs_release_path(path);
  648. /*
  649. * Mutex was contended, block until it's
  650. * released and try again
  651. */
  652. mutex_lock(&head->mutex);
  653. mutex_unlock(&head->mutex);
  654. btrfs_put_delayed_ref(&head->node);
  655. goto again;
  656. }
  657. ret = __add_delayed_refs(head, seq, &prefs_delayed);
  658. if (ret) {
  659. spin_unlock(&delayed_refs->lock);
  660. goto out;
  661. }
  662. }
  663. spin_unlock(&delayed_refs->lock);
  664. }
  665. if (path->slots[0]) {
  666. struct extent_buffer *leaf;
  667. int slot;
  668. path->slots[0]--;
  669. leaf = path->nodes[0];
  670. slot = path->slots[0];
  671. btrfs_item_key_to_cpu(leaf, &key, slot);
  672. if (key.objectid == bytenr &&
  673. key.type == BTRFS_EXTENT_ITEM_KEY) {
  674. ret = __add_inline_refs(fs_info, path, bytenr,
  675. &info_level, &prefs);
  676. if (ret)
  677. goto out;
  678. ret = __add_keyed_refs(fs_info, path, bytenr,
  679. info_level, &prefs);
  680. if (ret)
  681. goto out;
  682. }
  683. }
  684. btrfs_release_path(path);
  685. list_splice_init(&prefs_delayed, &prefs);
  686. ret = __add_missing_keys(fs_info, &prefs);
  687. if (ret)
  688. goto out;
  689. ret = __merge_refs(&prefs, 1);
  690. if (ret)
  691. goto out;
  692. ret = __resolve_indirect_refs(fs_info, search_commit_root, &prefs);
  693. if (ret)
  694. goto out;
  695. ret = __merge_refs(&prefs, 2);
  696. if (ret)
  697. goto out;
  698. while (!list_empty(&prefs)) {
  699. ref = list_first_entry(&prefs, struct __prelim_ref, list);
  700. list_del(&ref->list);
  701. if (ref->count < 0)
  702. WARN_ON(1);
  703. if (ref->count && ref->root_id && ref->parent == 0) {
  704. /* no parent == root of tree */
  705. ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
  706. BUG_ON(ret < 0);
  707. }
  708. if (ref->count && ref->parent) {
  709. ret = ulist_add(refs, ref->parent, 0, GFP_NOFS);
  710. BUG_ON(ret < 0);
  711. }
  712. kfree(ref);
  713. }
  714. out:
  715. if (head)
  716. mutex_unlock(&head->mutex);
  717. btrfs_free_path(path);
  718. while (!list_empty(&prefs)) {
  719. ref = list_first_entry(&prefs, struct __prelim_ref, list);
  720. list_del(&ref->list);
  721. kfree(ref);
  722. }
  723. while (!list_empty(&prefs_delayed)) {
  724. ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
  725. list);
  726. list_del(&ref->list);
  727. kfree(ref);
  728. }
  729. return ret;
  730. }
  731. /*
  732. * Finds all leafs with a reference to the specified combination of bytenr and
  733. * offset. key_list_head will point to a list of corresponding keys (caller must
  734. * free each list element). The leafs will be stored in the leafs ulist, which
  735. * must be freed with ulist_free.
  736. *
  737. * returns 0 on success, <0 on error
  738. */
  739. static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
  740. struct btrfs_fs_info *fs_info, u64 bytenr,
  741. u64 num_bytes, u64 seq, struct ulist **leafs)
  742. {
  743. struct ulist *tmp;
  744. int ret;
  745. tmp = ulist_alloc(GFP_NOFS);
  746. if (!tmp)
  747. return -ENOMEM;
  748. *leafs = ulist_alloc(GFP_NOFS);
  749. if (!*leafs) {
  750. ulist_free(tmp);
  751. return -ENOMEM;
  752. }
  753. ret = find_parent_nodes(trans, fs_info, bytenr, seq, *leafs, tmp);
  754. ulist_free(tmp);
  755. if (ret < 0 && ret != -ENOENT) {
  756. ulist_free(*leafs);
  757. return ret;
  758. }
  759. return 0;
  760. }
  761. /*
  762. * walk all backrefs for a given extent to find all roots that reference this
  763. * extent. Walking a backref means finding all extents that reference this
  764. * extent and in turn walk the backrefs of those, too. Naturally this is a
  765. * recursive process, but here it is implemented in an iterative fashion: We
  766. * find all referencing extents for the extent in question and put them on a
  767. * list. In turn, we find all referencing extents for those, further appending
  768. * to the list. The way we iterate the list allows adding more elements after
  769. * the current while iterating. The process stops when we reach the end of the
  770. * list. Found roots are added to the roots list.
  771. *
  772. * returns 0 on success, < 0 on error.
  773. */
  774. int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
  775. struct btrfs_fs_info *fs_info, u64 bytenr,
  776. u64 num_bytes, u64 seq, struct ulist **roots)
  777. {
  778. struct ulist *tmp;
  779. struct ulist_node *node = NULL;
  780. struct ulist_iterator uiter;
  781. int ret;
  782. tmp = ulist_alloc(GFP_NOFS);
  783. if (!tmp)
  784. return -ENOMEM;
  785. *roots = ulist_alloc(GFP_NOFS);
  786. if (!*roots) {
  787. ulist_free(tmp);
  788. return -ENOMEM;
  789. }
  790. ULIST_ITER_INIT(&uiter);
  791. while (1) {
  792. ret = find_parent_nodes(trans, fs_info, bytenr, seq,
  793. tmp, *roots);
  794. if (ret < 0 && ret != -ENOENT) {
  795. ulist_free(tmp);
  796. ulist_free(*roots);
  797. return ret;
  798. }
  799. node = ulist_next(tmp, &uiter);
  800. if (!node)
  801. break;
  802. bytenr = node->val;
  803. }
  804. ulist_free(tmp);
  805. return 0;
  806. }
  807. static int __inode_info(u64 inum, u64 ioff, u8 key_type,
  808. struct btrfs_root *fs_root, struct btrfs_path *path,
  809. struct btrfs_key *found_key)
  810. {
  811. int ret;
  812. struct btrfs_key key;
  813. struct extent_buffer *eb;
  814. key.type = key_type;
  815. key.objectid = inum;
  816. key.offset = ioff;
  817. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  818. if (ret < 0)
  819. return ret;
  820. eb = path->nodes[0];
  821. if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
  822. ret = btrfs_next_leaf(fs_root, path);
  823. if (ret)
  824. return ret;
  825. eb = path->nodes[0];
  826. }
  827. btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
  828. if (found_key->type != key.type || found_key->objectid != key.objectid)
  829. return 1;
  830. return 0;
  831. }
  832. /*
  833. * this makes the path point to (inum INODE_ITEM ioff)
  834. */
  835. int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
  836. struct btrfs_path *path)
  837. {
  838. struct btrfs_key key;
  839. return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path,
  840. &key);
  841. }
  842. static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
  843. struct btrfs_path *path,
  844. struct btrfs_key *found_key)
  845. {
  846. return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path,
  847. found_key);
  848. }
  849. /*
  850. * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements
  851. * of the path are separated by '/' and the path is guaranteed to be
  852. * 0-terminated. the path is only given within the current file system.
  853. * Therefore, it never starts with a '/'. the caller is responsible to provide
  854. * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
  855. * the start point of the resulting string is returned. this pointer is within
  856. * dest, normally.
  857. * in case the path buffer would overflow, the pointer is decremented further
  858. * as if output was written to the buffer, though no more output is actually
  859. * generated. that way, the caller can determine how much space would be
  860. * required for the path to fit into the buffer. in that case, the returned
  861. * value will be smaller than dest. callers must check this!
  862. */
  863. static char *iref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
  864. struct btrfs_inode_ref *iref,
  865. struct extent_buffer *eb_in, u64 parent,
  866. char *dest, u32 size)
  867. {
  868. u32 len;
  869. int slot;
  870. u64 next_inum;
  871. int ret;
  872. s64 bytes_left = size - 1;
  873. struct extent_buffer *eb = eb_in;
  874. struct btrfs_key found_key;
  875. int leave_spinning = path->leave_spinning;
  876. if (bytes_left >= 0)
  877. dest[bytes_left] = '\0';
  878. path->leave_spinning = 1;
  879. while (1) {
  880. len = btrfs_inode_ref_name_len(eb, iref);
  881. bytes_left -= len;
  882. if (bytes_left >= 0)
  883. read_extent_buffer(eb, dest + bytes_left,
  884. (unsigned long)(iref + 1), len);
  885. if (eb != eb_in) {
  886. btrfs_tree_read_unlock_blocking(eb);
  887. free_extent_buffer(eb);
  888. }
  889. ret = inode_ref_info(parent, 0, fs_root, path, &found_key);
  890. if (ret > 0)
  891. ret = -ENOENT;
  892. if (ret)
  893. break;
  894. next_inum = found_key.offset;
  895. /* regular exit ahead */
  896. if (parent == next_inum)
  897. break;
  898. slot = path->slots[0];
  899. eb = path->nodes[0];
  900. /* make sure we can use eb after releasing the path */
  901. if (eb != eb_in) {
  902. atomic_inc(&eb->refs);
  903. btrfs_tree_read_lock(eb);
  904. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  905. }
  906. btrfs_release_path(path);
  907. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  908. parent = next_inum;
  909. --bytes_left;
  910. if (bytes_left >= 0)
  911. dest[bytes_left] = '/';
  912. }
  913. btrfs_release_path(path);
  914. path->leave_spinning = leave_spinning;
  915. if (ret)
  916. return ERR_PTR(ret);
  917. return dest + bytes_left;
  918. }
  919. /*
  920. * this makes the path point to (logical EXTENT_ITEM *)
  921. * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
  922. * tree blocks and <0 on error.
  923. */
  924. int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
  925. struct btrfs_path *path, struct btrfs_key *found_key)
  926. {
  927. int ret;
  928. u64 flags;
  929. u32 item_size;
  930. struct extent_buffer *eb;
  931. struct btrfs_extent_item *ei;
  932. struct btrfs_key key;
  933. key.type = BTRFS_EXTENT_ITEM_KEY;
  934. key.objectid = logical;
  935. key.offset = (u64)-1;
  936. ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
  937. if (ret < 0)
  938. return ret;
  939. ret = btrfs_previous_item(fs_info->extent_root, path,
  940. 0, BTRFS_EXTENT_ITEM_KEY);
  941. if (ret < 0)
  942. return ret;
  943. btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
  944. if (found_key->type != BTRFS_EXTENT_ITEM_KEY ||
  945. found_key->objectid > logical ||
  946. found_key->objectid + found_key->offset <= logical) {
  947. pr_debug("logical %llu is not within any extent\n",
  948. (unsigned long long)logical);
  949. return -ENOENT;
  950. }
  951. eb = path->nodes[0];
  952. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  953. BUG_ON(item_size < sizeof(*ei));
  954. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  955. flags = btrfs_extent_flags(eb, ei);
  956. pr_debug("logical %llu is at position %llu within the extent (%llu "
  957. "EXTENT_ITEM %llu) flags %#llx size %u\n",
  958. (unsigned long long)logical,
  959. (unsigned long long)(logical - found_key->objectid),
  960. (unsigned long long)found_key->objectid,
  961. (unsigned long long)found_key->offset,
  962. (unsigned long long)flags, item_size);
  963. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  964. return BTRFS_EXTENT_FLAG_TREE_BLOCK;
  965. if (flags & BTRFS_EXTENT_FLAG_DATA)
  966. return BTRFS_EXTENT_FLAG_DATA;
  967. return -EIO;
  968. }
  969. /*
  970. * helper function to iterate extent inline refs. ptr must point to a 0 value
  971. * for the first call and may be modified. it is used to track state.
  972. * if more refs exist, 0 is returned and the next call to
  973. * __get_extent_inline_ref must pass the modified ptr parameter to get the
  974. * next ref. after the last ref was processed, 1 is returned.
  975. * returns <0 on error
  976. */
  977. static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
  978. struct btrfs_extent_item *ei, u32 item_size,
  979. struct btrfs_extent_inline_ref **out_eiref,
  980. int *out_type)
  981. {
  982. unsigned long end;
  983. u64 flags;
  984. struct btrfs_tree_block_info *info;
  985. if (!*ptr) {
  986. /* first call */
  987. flags = btrfs_extent_flags(eb, ei);
  988. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  989. info = (struct btrfs_tree_block_info *)(ei + 1);
  990. *out_eiref =
  991. (struct btrfs_extent_inline_ref *)(info + 1);
  992. } else {
  993. *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
  994. }
  995. *ptr = (unsigned long)*out_eiref;
  996. if ((void *)*ptr >= (void *)ei + item_size)
  997. return -ENOENT;
  998. }
  999. end = (unsigned long)ei + item_size;
  1000. *out_eiref = (struct btrfs_extent_inline_ref *)*ptr;
  1001. *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
  1002. *ptr += btrfs_extent_inline_ref_size(*out_type);
  1003. WARN_ON(*ptr > end);
  1004. if (*ptr == end)
  1005. return 1; /* last */
  1006. return 0;
  1007. }
  1008. /*
  1009. * reads the tree block backref for an extent. tree level and root are returned
  1010. * through out_level and out_root. ptr must point to a 0 value for the first
  1011. * call and may be modified (see __get_extent_inline_ref comment).
  1012. * returns 0 if data was provided, 1 if there was no more data to provide or
  1013. * <0 on error.
  1014. */
  1015. int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
  1016. struct btrfs_extent_item *ei, u32 item_size,
  1017. u64 *out_root, u8 *out_level)
  1018. {
  1019. int ret;
  1020. int type;
  1021. struct btrfs_tree_block_info *info;
  1022. struct btrfs_extent_inline_ref *eiref;
  1023. if (*ptr == (unsigned long)-1)
  1024. return 1;
  1025. while (1) {
  1026. ret = __get_extent_inline_ref(ptr, eb, ei, item_size,
  1027. &eiref, &type);
  1028. if (ret < 0)
  1029. return ret;
  1030. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  1031. type == BTRFS_SHARED_BLOCK_REF_KEY)
  1032. break;
  1033. if (ret == 1)
  1034. return 1;
  1035. }
  1036. /* we can treat both ref types equally here */
  1037. info = (struct btrfs_tree_block_info *)(ei + 1);
  1038. *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
  1039. *out_level = btrfs_tree_block_level(eb, info);
  1040. if (ret == 1)
  1041. *ptr = (unsigned long)-1;
  1042. return 0;
  1043. }
  1044. static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, u64 logical,
  1045. u64 orig_extent_item_objectid,
  1046. u64 extent_item_pos, u64 root,
  1047. iterate_extent_inodes_t *iterate, void *ctx)
  1048. {
  1049. u64 disk_byte;
  1050. struct btrfs_key key;
  1051. struct btrfs_file_extent_item *fi;
  1052. struct extent_buffer *eb;
  1053. int slot;
  1054. int nritems;
  1055. int ret = 0;
  1056. int extent_type;
  1057. u64 data_offset;
  1058. u64 data_len;
  1059. eb = read_tree_block(fs_info->tree_root, logical,
  1060. fs_info->tree_root->leafsize, 0);
  1061. if (!eb)
  1062. return -EIO;
  1063. /*
  1064. * from the shared data ref, we only have the leaf but we need
  1065. * the key. thus, we must look into all items and see that we
  1066. * find one (some) with a reference to our extent item.
  1067. */
  1068. nritems = btrfs_header_nritems(eb);
  1069. for (slot = 0; slot < nritems; ++slot) {
  1070. btrfs_item_key_to_cpu(eb, &key, slot);
  1071. if (key.type != BTRFS_EXTENT_DATA_KEY)
  1072. continue;
  1073. fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  1074. extent_type = btrfs_file_extent_type(eb, fi);
  1075. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  1076. continue;
  1077. /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
  1078. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  1079. if (disk_byte != orig_extent_item_objectid)
  1080. continue;
  1081. data_offset = btrfs_file_extent_offset(eb, fi);
  1082. data_len = btrfs_file_extent_num_bytes(eb, fi);
  1083. if (extent_item_pos < data_offset ||
  1084. extent_item_pos >= data_offset + data_len)
  1085. continue;
  1086. pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
  1087. "root %llu\n", orig_extent_item_objectid,
  1088. key.objectid, key.offset, root);
  1089. ret = iterate(key.objectid,
  1090. key.offset + (extent_item_pos - data_offset),
  1091. root, ctx);
  1092. if (ret) {
  1093. pr_debug("stopping iteration because ret=%d\n", ret);
  1094. break;
  1095. }
  1096. }
  1097. free_extent_buffer(eb);
  1098. return ret;
  1099. }
  1100. /*
  1101. * calls iterate() for every inode that references the extent identified by
  1102. * the given parameters.
  1103. * when the iterator function returns a non-zero value, iteration stops.
  1104. */
  1105. int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
  1106. u64 extent_item_objectid, u64 extent_item_pos,
  1107. int search_commit_root,
  1108. iterate_extent_inodes_t *iterate, void *ctx)
  1109. {
  1110. int ret;
  1111. struct list_head data_refs = LIST_HEAD_INIT(data_refs);
  1112. struct list_head shared_refs = LIST_HEAD_INIT(shared_refs);
  1113. struct btrfs_trans_handle *trans;
  1114. struct ulist *refs = NULL;
  1115. struct ulist *roots = NULL;
  1116. struct ulist_node *ref_node = NULL;
  1117. struct ulist_node *root_node = NULL;
  1118. struct seq_list seq_elem;
  1119. struct ulist_iterator ref_uiter;
  1120. struct ulist_iterator root_uiter;
  1121. struct btrfs_delayed_ref_root *delayed_refs = NULL;
  1122. pr_debug("resolving all inodes for extent %llu\n",
  1123. extent_item_objectid);
  1124. if (search_commit_root) {
  1125. trans = BTRFS_BACKREF_SEARCH_COMMIT_ROOT;
  1126. } else {
  1127. trans = btrfs_join_transaction(fs_info->extent_root);
  1128. if (IS_ERR(trans))
  1129. return PTR_ERR(trans);
  1130. delayed_refs = &trans->transaction->delayed_refs;
  1131. spin_lock(&delayed_refs->lock);
  1132. btrfs_get_delayed_seq(delayed_refs, &seq_elem);
  1133. spin_unlock(&delayed_refs->lock);
  1134. }
  1135. ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
  1136. extent_item_pos, seq_elem.seq,
  1137. &refs);
  1138. if (ret)
  1139. goto out;
  1140. ULIST_ITER_INIT(&ref_uiter);
  1141. while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
  1142. ret = btrfs_find_all_roots(trans, fs_info, ref_node->val, -1,
  1143. seq_elem.seq, &roots);
  1144. if (ret)
  1145. break;
  1146. ULIST_ITER_INIT(&root_uiter);
  1147. while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
  1148. pr_debug("root %llu references leaf %llu\n",
  1149. root_node->val, ref_node->val);
  1150. ret = iterate_leaf_refs(fs_info, ref_node->val,
  1151. extent_item_objectid,
  1152. extent_item_pos, root_node->val,
  1153. iterate, ctx);
  1154. }
  1155. }
  1156. ulist_free(refs);
  1157. ulist_free(roots);
  1158. out:
  1159. if (!search_commit_root) {
  1160. btrfs_put_delayed_seq(delayed_refs, &seq_elem);
  1161. btrfs_end_transaction(trans, fs_info->extent_root);
  1162. }
  1163. return ret;
  1164. }
  1165. int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
  1166. struct btrfs_path *path,
  1167. iterate_extent_inodes_t *iterate, void *ctx)
  1168. {
  1169. int ret;
  1170. u64 extent_item_pos;
  1171. struct btrfs_key found_key;
  1172. int search_commit_root = path->search_commit_root;
  1173. ret = extent_from_logical(fs_info, logical, path,
  1174. &found_key);
  1175. btrfs_release_path(path);
  1176. if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1177. ret = -EINVAL;
  1178. if (ret < 0)
  1179. return ret;
  1180. extent_item_pos = logical - found_key.objectid;
  1181. ret = iterate_extent_inodes(fs_info, found_key.objectid,
  1182. extent_item_pos, search_commit_root,
  1183. iterate, ctx);
  1184. return ret;
  1185. }
  1186. static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
  1187. struct btrfs_path *path,
  1188. iterate_irefs_t *iterate, void *ctx)
  1189. {
  1190. int ret = 0;
  1191. int slot;
  1192. u32 cur;
  1193. u32 len;
  1194. u32 name_len;
  1195. u64 parent = 0;
  1196. int found = 0;
  1197. struct extent_buffer *eb;
  1198. struct btrfs_item *item;
  1199. struct btrfs_inode_ref *iref;
  1200. struct btrfs_key found_key;
  1201. while (!ret) {
  1202. path->leave_spinning = 1;
  1203. ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path,
  1204. &found_key);
  1205. if (ret < 0)
  1206. break;
  1207. if (ret) {
  1208. ret = found ? 0 : -ENOENT;
  1209. break;
  1210. }
  1211. ++found;
  1212. parent = found_key.offset;
  1213. slot = path->slots[0];
  1214. eb = path->nodes[0];
  1215. /* make sure we can use eb after releasing the path */
  1216. atomic_inc(&eb->refs);
  1217. btrfs_tree_read_lock(eb);
  1218. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1219. btrfs_release_path(path);
  1220. item = btrfs_item_nr(eb, slot);
  1221. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1222. for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
  1223. name_len = btrfs_inode_ref_name_len(eb, iref);
  1224. /* path must be released before calling iterate()! */
  1225. pr_debug("following ref at offset %u for inode %llu in "
  1226. "tree %llu\n", cur,
  1227. (unsigned long long)found_key.objectid,
  1228. (unsigned long long)fs_root->objectid);
  1229. ret = iterate(parent, iref, eb, ctx);
  1230. if (ret)
  1231. break;
  1232. len = sizeof(*iref) + name_len;
  1233. iref = (struct btrfs_inode_ref *)((char *)iref + len);
  1234. }
  1235. btrfs_tree_read_unlock_blocking(eb);
  1236. free_extent_buffer(eb);
  1237. }
  1238. btrfs_release_path(path);
  1239. return ret;
  1240. }
  1241. /*
  1242. * returns 0 if the path could be dumped (probably truncated)
  1243. * returns <0 in case of an error
  1244. */
  1245. static int inode_to_path(u64 inum, struct btrfs_inode_ref *iref,
  1246. struct extent_buffer *eb, void *ctx)
  1247. {
  1248. struct inode_fs_paths *ipath = ctx;
  1249. char *fspath;
  1250. char *fspath_min;
  1251. int i = ipath->fspath->elem_cnt;
  1252. const int s_ptr = sizeof(char *);
  1253. u32 bytes_left;
  1254. bytes_left = ipath->fspath->bytes_left > s_ptr ?
  1255. ipath->fspath->bytes_left - s_ptr : 0;
  1256. fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
  1257. fspath = iref_to_path(ipath->fs_root, ipath->btrfs_path, iref, eb,
  1258. inum, fspath_min, bytes_left);
  1259. if (IS_ERR(fspath))
  1260. return PTR_ERR(fspath);
  1261. if (fspath > fspath_min) {
  1262. pr_debug("path resolved: %s\n", fspath);
  1263. ipath->fspath->val[i] = (u64)(unsigned long)fspath;
  1264. ++ipath->fspath->elem_cnt;
  1265. ipath->fspath->bytes_left = fspath - fspath_min;
  1266. } else {
  1267. pr_debug("missed path, not enough space. missing bytes: %lu, "
  1268. "constructed so far: %s\n",
  1269. (unsigned long)(fspath_min - fspath), fspath_min);
  1270. ++ipath->fspath->elem_missed;
  1271. ipath->fspath->bytes_missing += fspath_min - fspath;
  1272. ipath->fspath->bytes_left = 0;
  1273. }
  1274. return 0;
  1275. }
  1276. /*
  1277. * this dumps all file system paths to the inode into the ipath struct, provided
  1278. * is has been created large enough. each path is zero-terminated and accessed
  1279. * from ipath->fspath->val[i].
  1280. * when it returns, there are ipath->fspath->elem_cnt number of paths available
  1281. * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
  1282. * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
  1283. * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
  1284. * have been needed to return all paths.
  1285. */
  1286. int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
  1287. {
  1288. return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
  1289. inode_to_path, ipath);
  1290. }
  1291. struct btrfs_data_container *init_data_container(u32 total_bytes)
  1292. {
  1293. struct btrfs_data_container *data;
  1294. size_t alloc_bytes;
  1295. alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
  1296. data = kmalloc(alloc_bytes, GFP_NOFS);
  1297. if (!data)
  1298. return ERR_PTR(-ENOMEM);
  1299. if (total_bytes >= sizeof(*data)) {
  1300. data->bytes_left = total_bytes - sizeof(*data);
  1301. data->bytes_missing = 0;
  1302. } else {
  1303. data->bytes_missing = sizeof(*data) - total_bytes;
  1304. data->bytes_left = 0;
  1305. }
  1306. data->elem_cnt = 0;
  1307. data->elem_missed = 0;
  1308. return data;
  1309. }
  1310. /*
  1311. * allocates space to return multiple file system paths for an inode.
  1312. * total_bytes to allocate are passed, note that space usable for actual path
  1313. * information will be total_bytes - sizeof(struct inode_fs_paths).
  1314. * the returned pointer must be freed with free_ipath() in the end.
  1315. */
  1316. struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
  1317. struct btrfs_path *path)
  1318. {
  1319. struct inode_fs_paths *ifp;
  1320. struct btrfs_data_container *fspath;
  1321. fspath = init_data_container(total_bytes);
  1322. if (IS_ERR(fspath))
  1323. return (void *)fspath;
  1324. ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
  1325. if (!ifp) {
  1326. kfree(fspath);
  1327. return ERR_PTR(-ENOMEM);
  1328. }
  1329. ifp->btrfs_path = path;
  1330. ifp->fspath = fspath;
  1331. ifp->fs_root = fs_root;
  1332. return ifp;
  1333. }
  1334. void free_ipath(struct inode_fs_paths *ipath)
  1335. {
  1336. if (!ipath)
  1337. return;
  1338. kfree(ipath->fspath);
  1339. kfree(ipath);
  1340. }