backref.c 43 KB

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