backref.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011 STRATO. All rights reserved.
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/rbtree.h>
  7. #include <trace/events/btrfs.h>
  8. #include "ctree.h"
  9. #include "disk-io.h"
  10. #include "backref.h"
  11. #include "ulist.h"
  12. #include "transaction.h"
  13. #include "delayed-ref.h"
  14. #include "locking.h"
  15. /* Just an arbitrary number so we can be sure this happened */
  16. #define BACKREF_FOUND_SHARED 6
  17. struct extent_inode_elem {
  18. u64 inum;
  19. u64 offset;
  20. struct extent_inode_elem *next;
  21. };
  22. static int check_extent_in_eb(const struct btrfs_key *key,
  23. const struct extent_buffer *eb,
  24. const struct btrfs_file_extent_item *fi,
  25. u64 extent_item_pos,
  26. struct extent_inode_elem **eie,
  27. bool ignore_offset)
  28. {
  29. u64 offset = 0;
  30. struct extent_inode_elem *e;
  31. if (!ignore_offset &&
  32. !btrfs_file_extent_compression(eb, fi) &&
  33. !btrfs_file_extent_encryption(eb, fi) &&
  34. !btrfs_file_extent_other_encoding(eb, fi)) {
  35. u64 data_offset;
  36. u64 data_len;
  37. data_offset = btrfs_file_extent_offset(eb, fi);
  38. data_len = btrfs_file_extent_num_bytes(eb, fi);
  39. if (extent_item_pos < data_offset ||
  40. extent_item_pos >= data_offset + data_len)
  41. return 1;
  42. offset = extent_item_pos - data_offset;
  43. }
  44. e = kmalloc(sizeof(*e), GFP_NOFS);
  45. if (!e)
  46. return -ENOMEM;
  47. e->next = *eie;
  48. e->inum = key->objectid;
  49. e->offset = key->offset + offset;
  50. *eie = e;
  51. return 0;
  52. }
  53. static void free_inode_elem_list(struct extent_inode_elem *eie)
  54. {
  55. struct extent_inode_elem *eie_next;
  56. for (; eie; eie = eie_next) {
  57. eie_next = eie->next;
  58. kfree(eie);
  59. }
  60. }
  61. static int find_extent_in_eb(const struct extent_buffer *eb,
  62. u64 wanted_disk_byte, u64 extent_item_pos,
  63. struct extent_inode_elem **eie,
  64. bool ignore_offset)
  65. {
  66. u64 disk_byte;
  67. struct btrfs_key key;
  68. struct btrfs_file_extent_item *fi;
  69. int slot;
  70. int nritems;
  71. int extent_type;
  72. int ret;
  73. /*
  74. * from the shared data ref, we only have the leaf but we need
  75. * the key. thus, we must look into all items and see that we
  76. * find one (some) with a reference to our extent item.
  77. */
  78. nritems = btrfs_header_nritems(eb);
  79. for (slot = 0; slot < nritems; ++slot) {
  80. btrfs_item_key_to_cpu(eb, &key, slot);
  81. if (key.type != BTRFS_EXTENT_DATA_KEY)
  82. continue;
  83. fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  84. extent_type = btrfs_file_extent_type(eb, fi);
  85. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  86. continue;
  87. /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
  88. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  89. if (disk_byte != wanted_disk_byte)
  90. continue;
  91. ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie, ignore_offset);
  92. if (ret < 0)
  93. return ret;
  94. }
  95. return 0;
  96. }
  97. struct preftree {
  98. struct rb_root_cached root;
  99. unsigned int count;
  100. };
  101. #define PREFTREE_INIT { .root = RB_ROOT_CACHED, .count = 0 }
  102. struct preftrees {
  103. struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */
  104. struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */
  105. struct preftree indirect_missing_keys;
  106. };
  107. /*
  108. * Checks for a shared extent during backref search.
  109. *
  110. * The share_count tracks prelim_refs (direct and indirect) having a
  111. * ref->count >0:
  112. * - incremented when a ref->count transitions to >0
  113. * - decremented when a ref->count transitions to <1
  114. */
  115. struct share_check {
  116. u64 root_objectid;
  117. u64 inum;
  118. int share_count;
  119. };
  120. static inline int extent_is_shared(struct share_check *sc)
  121. {
  122. return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0;
  123. }
  124. static struct kmem_cache *btrfs_prelim_ref_cache;
  125. int __init btrfs_prelim_ref_init(void)
  126. {
  127. btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
  128. sizeof(struct prelim_ref),
  129. 0,
  130. SLAB_MEM_SPREAD,
  131. NULL);
  132. if (!btrfs_prelim_ref_cache)
  133. return -ENOMEM;
  134. return 0;
  135. }
  136. void __cold btrfs_prelim_ref_exit(void)
  137. {
  138. kmem_cache_destroy(btrfs_prelim_ref_cache);
  139. }
  140. static void free_pref(struct prelim_ref *ref)
  141. {
  142. kmem_cache_free(btrfs_prelim_ref_cache, ref);
  143. }
  144. /*
  145. * Return 0 when both refs are for the same block (and can be merged).
  146. * A -1 return indicates ref1 is a 'lower' block than ref2, while 1
  147. * indicates a 'higher' block.
  148. */
  149. static int prelim_ref_compare(struct prelim_ref *ref1,
  150. struct prelim_ref *ref2)
  151. {
  152. if (ref1->level < ref2->level)
  153. return -1;
  154. if (ref1->level > ref2->level)
  155. return 1;
  156. if (ref1->root_id < ref2->root_id)
  157. return -1;
  158. if (ref1->root_id > ref2->root_id)
  159. return 1;
  160. if (ref1->key_for_search.type < ref2->key_for_search.type)
  161. return -1;
  162. if (ref1->key_for_search.type > ref2->key_for_search.type)
  163. return 1;
  164. if (ref1->key_for_search.objectid < ref2->key_for_search.objectid)
  165. return -1;
  166. if (ref1->key_for_search.objectid > ref2->key_for_search.objectid)
  167. return 1;
  168. if (ref1->key_for_search.offset < ref2->key_for_search.offset)
  169. return -1;
  170. if (ref1->key_for_search.offset > ref2->key_for_search.offset)
  171. return 1;
  172. if (ref1->parent < ref2->parent)
  173. return -1;
  174. if (ref1->parent > ref2->parent)
  175. return 1;
  176. return 0;
  177. }
  178. static void update_share_count(struct share_check *sc, int oldcount,
  179. int newcount)
  180. {
  181. if ((!sc) || (oldcount == 0 && newcount < 1))
  182. return;
  183. if (oldcount > 0 && newcount < 1)
  184. sc->share_count--;
  185. else if (oldcount < 1 && newcount > 0)
  186. sc->share_count++;
  187. }
  188. /*
  189. * Add @newref to the @root rbtree, merging identical refs.
  190. *
  191. * Callers should assume that newref has been freed after calling.
  192. */
  193. static void prelim_ref_insert(const struct btrfs_fs_info *fs_info,
  194. struct preftree *preftree,
  195. struct prelim_ref *newref,
  196. struct share_check *sc)
  197. {
  198. struct rb_root_cached *root;
  199. struct rb_node **p;
  200. struct rb_node *parent = NULL;
  201. struct prelim_ref *ref;
  202. int result;
  203. bool leftmost = true;
  204. root = &preftree->root;
  205. p = &root->rb_root.rb_node;
  206. while (*p) {
  207. parent = *p;
  208. ref = rb_entry(parent, struct prelim_ref, rbnode);
  209. result = prelim_ref_compare(ref, newref);
  210. if (result < 0) {
  211. p = &(*p)->rb_left;
  212. } else if (result > 0) {
  213. p = &(*p)->rb_right;
  214. leftmost = false;
  215. } else {
  216. /* Identical refs, merge them and free @newref */
  217. struct extent_inode_elem *eie = ref->inode_list;
  218. while (eie && eie->next)
  219. eie = eie->next;
  220. if (!eie)
  221. ref->inode_list = newref->inode_list;
  222. else
  223. eie->next = newref->inode_list;
  224. trace_btrfs_prelim_ref_merge(fs_info, ref, newref,
  225. preftree->count);
  226. /*
  227. * A delayed ref can have newref->count < 0.
  228. * The ref->count is updated to follow any
  229. * BTRFS_[ADD|DROP]_DELAYED_REF actions.
  230. */
  231. update_share_count(sc, ref->count,
  232. ref->count + newref->count);
  233. ref->count += newref->count;
  234. free_pref(newref);
  235. return;
  236. }
  237. }
  238. update_share_count(sc, 0, newref->count);
  239. preftree->count++;
  240. trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count);
  241. rb_link_node(&newref->rbnode, parent, p);
  242. rb_insert_color_cached(&newref->rbnode, root, leftmost);
  243. }
  244. /*
  245. * Release the entire tree. We don't care about internal consistency so
  246. * just free everything and then reset the tree root.
  247. */
  248. static void prelim_release(struct preftree *preftree)
  249. {
  250. struct prelim_ref *ref, *next_ref;
  251. rbtree_postorder_for_each_entry_safe(ref, next_ref,
  252. &preftree->root.rb_root, rbnode)
  253. free_pref(ref);
  254. preftree->root = RB_ROOT_CACHED;
  255. preftree->count = 0;
  256. }
  257. /*
  258. * the rules for all callers of this function are:
  259. * - obtaining the parent is the goal
  260. * - if you add a key, you must know that it is a correct key
  261. * - if you cannot add the parent or a correct key, then we will look into the
  262. * block later to set a correct key
  263. *
  264. * delayed refs
  265. * ============
  266. * backref type | shared | indirect | shared | indirect
  267. * information | tree | tree | data | data
  268. * --------------------+--------+----------+--------+----------
  269. * parent logical | y | - | - | -
  270. * key to resolve | - | y | y | y
  271. * tree block logical | - | - | - | -
  272. * root for resolving | y | y | y | y
  273. *
  274. * - column 1: we've the parent -> done
  275. * - column 2, 3, 4: we use the key to find the parent
  276. *
  277. * on disk refs (inline or keyed)
  278. * ==============================
  279. * backref type | shared | indirect | shared | indirect
  280. * information | tree | tree | data | data
  281. * --------------------+--------+----------+--------+----------
  282. * parent logical | y | - | y | -
  283. * key to resolve | - | - | - | y
  284. * tree block logical | y | y | y | y
  285. * root for resolving | - | y | y | y
  286. *
  287. * - column 1, 3: we've the parent -> done
  288. * - column 2: we take the first key from the block to find the parent
  289. * (see add_missing_keys)
  290. * - column 4: we use the key to find the parent
  291. *
  292. * additional information that's available but not required to find the parent
  293. * block might help in merging entries to gain some speed.
  294. */
  295. static int add_prelim_ref(const struct btrfs_fs_info *fs_info,
  296. struct preftree *preftree, u64 root_id,
  297. const struct btrfs_key *key, int level, u64 parent,
  298. u64 wanted_disk_byte, int count,
  299. struct share_check *sc, gfp_t gfp_mask)
  300. {
  301. struct prelim_ref *ref;
  302. if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID)
  303. return 0;
  304. ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
  305. if (!ref)
  306. return -ENOMEM;
  307. ref->root_id = root_id;
  308. if (key) {
  309. ref->key_for_search = *key;
  310. /*
  311. * We can often find data backrefs with an offset that is too
  312. * large (>= LLONG_MAX, maximum allowed file offset) due to
  313. * underflows when subtracting a file's offset with the data
  314. * offset of its corresponding extent data item. This can
  315. * happen for example in the clone ioctl.
  316. * So if we detect such case we set the search key's offset to
  317. * zero to make sure we will find the matching file extent item
  318. * at add_all_parents(), otherwise we will miss it because the
  319. * offset taken form the backref is much larger then the offset
  320. * of the file extent item. This can make us scan a very large
  321. * number of file extent items, but at least it will not make
  322. * us miss any.
  323. * This is an ugly workaround for a behaviour that should have
  324. * never existed, but it does and a fix for the clone ioctl
  325. * would touch a lot of places, cause backwards incompatibility
  326. * and would not fix the problem for extents cloned with older
  327. * kernels.
  328. */
  329. if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY &&
  330. ref->key_for_search.offset >= LLONG_MAX)
  331. ref->key_for_search.offset = 0;
  332. } else {
  333. memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
  334. }
  335. ref->inode_list = NULL;
  336. ref->level = level;
  337. ref->count = count;
  338. ref->parent = parent;
  339. ref->wanted_disk_byte = wanted_disk_byte;
  340. prelim_ref_insert(fs_info, preftree, ref, sc);
  341. return extent_is_shared(sc);
  342. }
  343. /* direct refs use root == 0, key == NULL */
  344. static int add_direct_ref(const struct btrfs_fs_info *fs_info,
  345. struct preftrees *preftrees, int level, u64 parent,
  346. u64 wanted_disk_byte, int count,
  347. struct share_check *sc, gfp_t gfp_mask)
  348. {
  349. return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level,
  350. parent, wanted_disk_byte, count, sc, gfp_mask);
  351. }
  352. /* indirect refs use parent == 0 */
  353. static int add_indirect_ref(const struct btrfs_fs_info *fs_info,
  354. struct preftrees *preftrees, u64 root_id,
  355. const struct btrfs_key *key, int level,
  356. u64 wanted_disk_byte, int count,
  357. struct share_check *sc, gfp_t gfp_mask)
  358. {
  359. struct preftree *tree = &preftrees->indirect;
  360. if (!key)
  361. tree = &preftrees->indirect_missing_keys;
  362. return add_prelim_ref(fs_info, tree, root_id, key, level, 0,
  363. wanted_disk_byte, count, sc, gfp_mask);
  364. }
  365. static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
  366. struct ulist *parents, struct prelim_ref *ref,
  367. int level, u64 time_seq, const u64 *extent_item_pos,
  368. u64 total_refs, bool ignore_offset)
  369. {
  370. int ret = 0;
  371. int slot;
  372. struct extent_buffer *eb;
  373. struct btrfs_key key;
  374. struct btrfs_key *key_for_search = &ref->key_for_search;
  375. struct btrfs_file_extent_item *fi;
  376. struct extent_inode_elem *eie = NULL, *old = NULL;
  377. u64 disk_byte;
  378. u64 wanted_disk_byte = ref->wanted_disk_byte;
  379. u64 count = 0;
  380. if (level != 0) {
  381. eb = path->nodes[level];
  382. ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
  383. if (ret < 0)
  384. return ret;
  385. return 0;
  386. }
  387. /*
  388. * We normally enter this function with the path already pointing to
  389. * the first item to check. But sometimes, we may enter it with
  390. * slot==nritems. In that case, go to the next leaf before we continue.
  391. */
  392. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  393. if (time_seq == SEQ_LAST)
  394. ret = btrfs_next_leaf(root, path);
  395. else
  396. ret = btrfs_next_old_leaf(root, path, time_seq);
  397. }
  398. while (!ret && count < total_refs) {
  399. eb = path->nodes[0];
  400. slot = path->slots[0];
  401. btrfs_item_key_to_cpu(eb, &key, slot);
  402. if (key.objectid != key_for_search->objectid ||
  403. key.type != BTRFS_EXTENT_DATA_KEY)
  404. break;
  405. fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  406. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  407. if (disk_byte == wanted_disk_byte) {
  408. eie = NULL;
  409. old = NULL;
  410. count++;
  411. if (extent_item_pos) {
  412. ret = check_extent_in_eb(&key, eb, fi,
  413. *extent_item_pos,
  414. &eie, ignore_offset);
  415. if (ret < 0)
  416. break;
  417. }
  418. if (ret > 0)
  419. goto next;
  420. ret = ulist_add_merge_ptr(parents, eb->start,
  421. eie, (void **)&old, GFP_NOFS);
  422. if (ret < 0)
  423. break;
  424. if (!ret && extent_item_pos) {
  425. while (old->next)
  426. old = old->next;
  427. old->next = eie;
  428. }
  429. eie = NULL;
  430. }
  431. next:
  432. if (time_seq == SEQ_LAST)
  433. ret = btrfs_next_item(root, path);
  434. else
  435. ret = btrfs_next_old_item(root, path, time_seq);
  436. }
  437. if (ret > 0)
  438. ret = 0;
  439. else if (ret < 0)
  440. free_inode_elem_list(eie);
  441. return ret;
  442. }
  443. /*
  444. * resolve an indirect backref in the form (root_id, key, level)
  445. * to a logical address
  446. */
  447. static int resolve_indirect_ref(struct btrfs_fs_info *fs_info,
  448. struct btrfs_path *path, u64 time_seq,
  449. struct prelim_ref *ref, struct ulist *parents,
  450. const u64 *extent_item_pos, u64 total_refs,
  451. bool ignore_offset)
  452. {
  453. struct btrfs_root *root;
  454. struct btrfs_key root_key;
  455. struct extent_buffer *eb;
  456. int ret = 0;
  457. int root_level;
  458. int level = ref->level;
  459. int index;
  460. root_key.objectid = ref->root_id;
  461. root_key.type = BTRFS_ROOT_ITEM_KEY;
  462. root_key.offset = (u64)-1;
  463. index = srcu_read_lock(&fs_info->subvol_srcu);
  464. root = btrfs_get_fs_root(fs_info, &root_key, false);
  465. if (IS_ERR(root)) {
  466. srcu_read_unlock(&fs_info->subvol_srcu, index);
  467. ret = PTR_ERR(root);
  468. goto out;
  469. }
  470. if (btrfs_is_testing(fs_info)) {
  471. srcu_read_unlock(&fs_info->subvol_srcu, index);
  472. ret = -ENOENT;
  473. goto out;
  474. }
  475. if (path->search_commit_root)
  476. root_level = btrfs_header_level(root->commit_root);
  477. else if (time_seq == SEQ_LAST)
  478. root_level = btrfs_header_level(root->node);
  479. else
  480. root_level = btrfs_old_root_level(root, time_seq);
  481. if (root_level + 1 == level) {
  482. srcu_read_unlock(&fs_info->subvol_srcu, index);
  483. goto out;
  484. }
  485. path->lowest_level = level;
  486. if (time_seq == SEQ_LAST)
  487. ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path,
  488. 0, 0);
  489. else
  490. ret = btrfs_search_old_slot(root, &ref->key_for_search, path,
  491. time_seq);
  492. /* root node has been locked, we can release @subvol_srcu safely here */
  493. srcu_read_unlock(&fs_info->subvol_srcu, index);
  494. btrfs_debug(fs_info,
  495. "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)",
  496. ref->root_id, level, ref->count, ret,
  497. ref->key_for_search.objectid, ref->key_for_search.type,
  498. ref->key_for_search.offset);
  499. if (ret < 0)
  500. goto out;
  501. eb = path->nodes[level];
  502. while (!eb) {
  503. if (WARN_ON(!level)) {
  504. ret = 1;
  505. goto out;
  506. }
  507. level--;
  508. eb = path->nodes[level];
  509. }
  510. ret = add_all_parents(root, path, parents, ref, level, time_seq,
  511. extent_item_pos, total_refs, ignore_offset);
  512. out:
  513. path->lowest_level = 0;
  514. btrfs_release_path(path);
  515. return ret;
  516. }
  517. static struct extent_inode_elem *
  518. unode_aux_to_inode_list(struct ulist_node *node)
  519. {
  520. if (!node)
  521. return NULL;
  522. return (struct extent_inode_elem *)(uintptr_t)node->aux;
  523. }
  524. /*
  525. * We maintain three seperate rbtrees: one for direct refs, one for
  526. * indirect refs which have a key, and one for indirect refs which do not
  527. * have a key. Each tree does merge on insertion.
  528. *
  529. * Once all of the references are located, we iterate over the tree of
  530. * indirect refs with missing keys. An appropriate key is located and
  531. * the ref is moved onto the tree for indirect refs. After all missing
  532. * keys are thus located, we iterate over the indirect ref tree, resolve
  533. * each reference, and then insert the resolved reference onto the
  534. * direct tree (merging there too).
  535. *
  536. * New backrefs (i.e., for parent nodes) are added to the appropriate
  537. * rbtree as they are encountered. The new backrefs are subsequently
  538. * resolved as above.
  539. */
  540. static int resolve_indirect_refs(struct btrfs_fs_info *fs_info,
  541. struct btrfs_path *path, u64 time_seq,
  542. struct preftrees *preftrees,
  543. const u64 *extent_item_pos, u64 total_refs,
  544. struct share_check *sc, bool ignore_offset)
  545. {
  546. int err;
  547. int ret = 0;
  548. struct ulist *parents;
  549. struct ulist_node *node;
  550. struct ulist_iterator uiter;
  551. struct rb_node *rnode;
  552. parents = ulist_alloc(GFP_NOFS);
  553. if (!parents)
  554. return -ENOMEM;
  555. /*
  556. * We could trade memory usage for performance here by iterating
  557. * the tree, allocating new refs for each insertion, and then
  558. * freeing the entire indirect tree when we're done. In some test
  559. * cases, the tree can grow quite large (~200k objects).
  560. */
  561. while ((rnode = rb_first_cached(&preftrees->indirect.root))) {
  562. struct prelim_ref *ref;
  563. ref = rb_entry(rnode, struct prelim_ref, rbnode);
  564. if (WARN(ref->parent,
  565. "BUG: direct ref found in indirect tree")) {
  566. ret = -EINVAL;
  567. goto out;
  568. }
  569. rb_erase_cached(&ref->rbnode, &preftrees->indirect.root);
  570. preftrees->indirect.count--;
  571. if (ref->count == 0) {
  572. free_pref(ref);
  573. continue;
  574. }
  575. if (sc && sc->root_objectid &&
  576. ref->root_id != sc->root_objectid) {
  577. free_pref(ref);
  578. ret = BACKREF_FOUND_SHARED;
  579. goto out;
  580. }
  581. err = resolve_indirect_ref(fs_info, path, time_seq, ref,
  582. parents, extent_item_pos,
  583. total_refs, ignore_offset);
  584. /*
  585. * we can only tolerate ENOENT,otherwise,we should catch error
  586. * and return directly.
  587. */
  588. if (err == -ENOENT) {
  589. prelim_ref_insert(fs_info, &preftrees->direct, ref,
  590. NULL);
  591. continue;
  592. } else if (err) {
  593. free_pref(ref);
  594. ret = err;
  595. goto out;
  596. }
  597. /* we put the first parent into the ref at hand */
  598. ULIST_ITER_INIT(&uiter);
  599. node = ulist_next(parents, &uiter);
  600. ref->parent = node ? node->val : 0;
  601. ref->inode_list = unode_aux_to_inode_list(node);
  602. /* Add a prelim_ref(s) for any other parent(s). */
  603. while ((node = ulist_next(parents, &uiter))) {
  604. struct prelim_ref *new_ref;
  605. new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
  606. GFP_NOFS);
  607. if (!new_ref) {
  608. free_pref(ref);
  609. ret = -ENOMEM;
  610. goto out;
  611. }
  612. memcpy(new_ref, ref, sizeof(*ref));
  613. new_ref->parent = node->val;
  614. new_ref->inode_list = unode_aux_to_inode_list(node);
  615. prelim_ref_insert(fs_info, &preftrees->direct,
  616. new_ref, NULL);
  617. }
  618. /*
  619. * Now it's a direct ref, put it in the the direct tree. We must
  620. * do this last because the ref could be merged/freed here.
  621. */
  622. prelim_ref_insert(fs_info, &preftrees->direct, ref, NULL);
  623. ulist_reinit(parents);
  624. cond_resched();
  625. }
  626. out:
  627. ulist_free(parents);
  628. return ret;
  629. }
  630. /*
  631. * read tree blocks and add keys where required.
  632. */
  633. static int add_missing_keys(struct btrfs_fs_info *fs_info,
  634. struct preftrees *preftrees)
  635. {
  636. struct prelim_ref *ref;
  637. struct extent_buffer *eb;
  638. struct preftree *tree = &preftrees->indirect_missing_keys;
  639. struct rb_node *node;
  640. while ((node = rb_first_cached(&tree->root))) {
  641. ref = rb_entry(node, struct prelim_ref, rbnode);
  642. rb_erase_cached(node, &tree->root);
  643. BUG_ON(ref->parent); /* should not be a direct ref */
  644. BUG_ON(ref->key_for_search.type);
  645. BUG_ON(!ref->wanted_disk_byte);
  646. eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0,
  647. ref->level - 1, NULL);
  648. if (IS_ERR(eb)) {
  649. free_pref(ref);
  650. return PTR_ERR(eb);
  651. } else if (!extent_buffer_uptodate(eb)) {
  652. free_pref(ref);
  653. free_extent_buffer(eb);
  654. return -EIO;
  655. }
  656. btrfs_tree_read_lock(eb);
  657. if (btrfs_header_level(eb) == 0)
  658. btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
  659. else
  660. btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
  661. btrfs_tree_read_unlock(eb);
  662. free_extent_buffer(eb);
  663. prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL);
  664. cond_resched();
  665. }
  666. return 0;
  667. }
  668. /*
  669. * add all currently queued delayed refs from this head whose seq nr is
  670. * smaller or equal that seq to the list
  671. */
  672. static int add_delayed_refs(const struct btrfs_fs_info *fs_info,
  673. struct btrfs_delayed_ref_head *head, u64 seq,
  674. struct preftrees *preftrees, u64 *total_refs,
  675. struct share_check *sc)
  676. {
  677. struct btrfs_delayed_ref_node *node;
  678. struct btrfs_delayed_extent_op *extent_op = head->extent_op;
  679. struct btrfs_key key;
  680. struct btrfs_key tmp_op_key;
  681. struct rb_node *n;
  682. int count;
  683. int ret = 0;
  684. if (extent_op && extent_op->update_key)
  685. btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key);
  686. spin_lock(&head->lock);
  687. for (n = rb_first_cached(&head->ref_tree); n; n = rb_next(n)) {
  688. node = rb_entry(n, struct btrfs_delayed_ref_node,
  689. ref_node);
  690. if (node->seq > seq)
  691. continue;
  692. switch (node->action) {
  693. case BTRFS_ADD_DELAYED_EXTENT:
  694. case BTRFS_UPDATE_DELAYED_HEAD:
  695. WARN_ON(1);
  696. continue;
  697. case BTRFS_ADD_DELAYED_REF:
  698. count = node->ref_mod;
  699. break;
  700. case BTRFS_DROP_DELAYED_REF:
  701. count = node->ref_mod * -1;
  702. break;
  703. default:
  704. BUG_ON(1);
  705. }
  706. *total_refs += count;
  707. switch (node->type) {
  708. case BTRFS_TREE_BLOCK_REF_KEY: {
  709. /* NORMAL INDIRECT METADATA backref */
  710. struct btrfs_delayed_tree_ref *ref;
  711. ref = btrfs_delayed_node_to_tree_ref(node);
  712. ret = add_indirect_ref(fs_info, preftrees, ref->root,
  713. &tmp_op_key, ref->level + 1,
  714. node->bytenr, count, sc,
  715. GFP_ATOMIC);
  716. break;
  717. }
  718. case BTRFS_SHARED_BLOCK_REF_KEY: {
  719. /* SHARED DIRECT METADATA backref */
  720. struct btrfs_delayed_tree_ref *ref;
  721. ref = btrfs_delayed_node_to_tree_ref(node);
  722. ret = add_direct_ref(fs_info, preftrees, ref->level + 1,
  723. ref->parent, node->bytenr, count,
  724. sc, GFP_ATOMIC);
  725. break;
  726. }
  727. case BTRFS_EXTENT_DATA_REF_KEY: {
  728. /* NORMAL INDIRECT DATA backref */
  729. struct btrfs_delayed_data_ref *ref;
  730. ref = btrfs_delayed_node_to_data_ref(node);
  731. key.objectid = ref->objectid;
  732. key.type = BTRFS_EXTENT_DATA_KEY;
  733. key.offset = ref->offset;
  734. /*
  735. * Found a inum that doesn't match our known inum, we
  736. * know it's shared.
  737. */
  738. if (sc && sc->inum && ref->objectid != sc->inum) {
  739. ret = BACKREF_FOUND_SHARED;
  740. goto out;
  741. }
  742. ret = add_indirect_ref(fs_info, preftrees, ref->root,
  743. &key, 0, node->bytenr, count, sc,
  744. GFP_ATOMIC);
  745. break;
  746. }
  747. case BTRFS_SHARED_DATA_REF_KEY: {
  748. /* SHARED DIRECT FULL backref */
  749. struct btrfs_delayed_data_ref *ref;
  750. ref = btrfs_delayed_node_to_data_ref(node);
  751. ret = add_direct_ref(fs_info, preftrees, 0, ref->parent,
  752. node->bytenr, count, sc,
  753. GFP_ATOMIC);
  754. break;
  755. }
  756. default:
  757. WARN_ON(1);
  758. }
  759. /*
  760. * We must ignore BACKREF_FOUND_SHARED until all delayed
  761. * refs have been checked.
  762. */
  763. if (ret && (ret != BACKREF_FOUND_SHARED))
  764. break;
  765. }
  766. if (!ret)
  767. ret = extent_is_shared(sc);
  768. out:
  769. spin_unlock(&head->lock);
  770. return ret;
  771. }
  772. /*
  773. * add all inline backrefs for bytenr to the list
  774. *
  775. * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED.
  776. */
  777. static int add_inline_refs(const struct btrfs_fs_info *fs_info,
  778. struct btrfs_path *path, u64 bytenr,
  779. int *info_level, struct preftrees *preftrees,
  780. u64 *total_refs, struct share_check *sc)
  781. {
  782. int ret = 0;
  783. int slot;
  784. struct extent_buffer *leaf;
  785. struct btrfs_key key;
  786. struct btrfs_key found_key;
  787. unsigned long ptr;
  788. unsigned long end;
  789. struct btrfs_extent_item *ei;
  790. u64 flags;
  791. u64 item_size;
  792. /*
  793. * enumerate all inline refs
  794. */
  795. leaf = path->nodes[0];
  796. slot = path->slots[0];
  797. item_size = btrfs_item_size_nr(leaf, slot);
  798. BUG_ON(item_size < sizeof(*ei));
  799. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  800. flags = btrfs_extent_flags(leaf, ei);
  801. *total_refs += btrfs_extent_refs(leaf, ei);
  802. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  803. ptr = (unsigned long)(ei + 1);
  804. end = (unsigned long)ei + item_size;
  805. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  806. flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  807. struct btrfs_tree_block_info *info;
  808. info = (struct btrfs_tree_block_info *)ptr;
  809. *info_level = btrfs_tree_block_level(leaf, info);
  810. ptr += sizeof(struct btrfs_tree_block_info);
  811. BUG_ON(ptr > end);
  812. } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
  813. *info_level = found_key.offset;
  814. } else {
  815. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
  816. }
  817. while (ptr < end) {
  818. struct btrfs_extent_inline_ref *iref;
  819. u64 offset;
  820. int type;
  821. iref = (struct btrfs_extent_inline_ref *)ptr;
  822. type = btrfs_get_extent_inline_ref_type(leaf, iref,
  823. BTRFS_REF_TYPE_ANY);
  824. if (type == BTRFS_REF_TYPE_INVALID)
  825. return -EUCLEAN;
  826. offset = btrfs_extent_inline_ref_offset(leaf, iref);
  827. switch (type) {
  828. case BTRFS_SHARED_BLOCK_REF_KEY:
  829. ret = add_direct_ref(fs_info, preftrees,
  830. *info_level + 1, offset,
  831. bytenr, 1, NULL, GFP_NOFS);
  832. break;
  833. case BTRFS_SHARED_DATA_REF_KEY: {
  834. struct btrfs_shared_data_ref *sdref;
  835. int count;
  836. sdref = (struct btrfs_shared_data_ref *)(iref + 1);
  837. count = btrfs_shared_data_ref_count(leaf, sdref);
  838. ret = add_direct_ref(fs_info, preftrees, 0, offset,
  839. bytenr, count, sc, GFP_NOFS);
  840. break;
  841. }
  842. case BTRFS_TREE_BLOCK_REF_KEY:
  843. ret = add_indirect_ref(fs_info, preftrees, offset,
  844. NULL, *info_level + 1,
  845. bytenr, 1, NULL, GFP_NOFS);
  846. break;
  847. case BTRFS_EXTENT_DATA_REF_KEY: {
  848. struct btrfs_extent_data_ref *dref;
  849. int count;
  850. u64 root;
  851. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  852. count = btrfs_extent_data_ref_count(leaf, dref);
  853. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  854. dref);
  855. key.type = BTRFS_EXTENT_DATA_KEY;
  856. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  857. if (sc && sc->inum && key.objectid != sc->inum) {
  858. ret = BACKREF_FOUND_SHARED;
  859. break;
  860. }
  861. root = btrfs_extent_data_ref_root(leaf, dref);
  862. ret = add_indirect_ref(fs_info, preftrees, root,
  863. &key, 0, bytenr, count,
  864. sc, GFP_NOFS);
  865. break;
  866. }
  867. default:
  868. WARN_ON(1);
  869. }
  870. if (ret)
  871. return ret;
  872. ptr += btrfs_extent_inline_ref_size(type);
  873. }
  874. return 0;
  875. }
  876. /*
  877. * add all non-inline backrefs for bytenr to the list
  878. *
  879. * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED.
  880. */
  881. static int add_keyed_refs(struct btrfs_fs_info *fs_info,
  882. struct btrfs_path *path, u64 bytenr,
  883. int info_level, struct preftrees *preftrees,
  884. struct share_check *sc)
  885. {
  886. struct btrfs_root *extent_root = fs_info->extent_root;
  887. int ret;
  888. int slot;
  889. struct extent_buffer *leaf;
  890. struct btrfs_key key;
  891. while (1) {
  892. ret = btrfs_next_item(extent_root, path);
  893. if (ret < 0)
  894. break;
  895. if (ret) {
  896. ret = 0;
  897. break;
  898. }
  899. slot = path->slots[0];
  900. leaf = path->nodes[0];
  901. btrfs_item_key_to_cpu(leaf, &key, slot);
  902. if (key.objectid != bytenr)
  903. break;
  904. if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
  905. continue;
  906. if (key.type > BTRFS_SHARED_DATA_REF_KEY)
  907. break;
  908. switch (key.type) {
  909. case BTRFS_SHARED_BLOCK_REF_KEY:
  910. /* SHARED DIRECT METADATA backref */
  911. ret = add_direct_ref(fs_info, preftrees,
  912. info_level + 1, key.offset,
  913. bytenr, 1, NULL, GFP_NOFS);
  914. break;
  915. case BTRFS_SHARED_DATA_REF_KEY: {
  916. /* SHARED DIRECT FULL backref */
  917. struct btrfs_shared_data_ref *sdref;
  918. int count;
  919. sdref = btrfs_item_ptr(leaf, slot,
  920. struct btrfs_shared_data_ref);
  921. count = btrfs_shared_data_ref_count(leaf, sdref);
  922. ret = add_direct_ref(fs_info, preftrees, 0,
  923. key.offset, bytenr, count,
  924. sc, GFP_NOFS);
  925. break;
  926. }
  927. case BTRFS_TREE_BLOCK_REF_KEY:
  928. /* NORMAL INDIRECT METADATA backref */
  929. ret = add_indirect_ref(fs_info, preftrees, key.offset,
  930. NULL, info_level + 1, bytenr,
  931. 1, NULL, GFP_NOFS);
  932. break;
  933. case BTRFS_EXTENT_DATA_REF_KEY: {
  934. /* NORMAL INDIRECT DATA backref */
  935. struct btrfs_extent_data_ref *dref;
  936. int count;
  937. u64 root;
  938. dref = btrfs_item_ptr(leaf, slot,
  939. struct btrfs_extent_data_ref);
  940. count = btrfs_extent_data_ref_count(leaf, dref);
  941. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  942. dref);
  943. key.type = BTRFS_EXTENT_DATA_KEY;
  944. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  945. if (sc && sc->inum && key.objectid != sc->inum) {
  946. ret = BACKREF_FOUND_SHARED;
  947. break;
  948. }
  949. root = btrfs_extent_data_ref_root(leaf, dref);
  950. ret = add_indirect_ref(fs_info, preftrees, root,
  951. &key, 0, bytenr, count,
  952. sc, GFP_NOFS);
  953. break;
  954. }
  955. default:
  956. WARN_ON(1);
  957. }
  958. if (ret)
  959. return ret;
  960. }
  961. return ret;
  962. }
  963. /*
  964. * this adds all existing backrefs (inline backrefs, backrefs and delayed
  965. * refs) for the given bytenr to the refs list, merges duplicates and resolves
  966. * indirect refs to their parent bytenr.
  967. * When roots are found, they're added to the roots list
  968. *
  969. * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave
  970. * much like trans == NULL case, the difference only lies in it will not
  971. * commit root.
  972. * The special case is for qgroup to search roots in commit_transaction().
  973. *
  974. * @sc - if !NULL, then immediately return BACKREF_FOUND_SHARED when a
  975. * shared extent is detected.
  976. *
  977. * Otherwise this returns 0 for success and <0 for an error.
  978. *
  979. * If ignore_offset is set to false, only extent refs whose offsets match
  980. * extent_item_pos are returned. If true, every extent ref is returned
  981. * and extent_item_pos is ignored.
  982. *
  983. * FIXME some caching might speed things up
  984. */
  985. static int find_parent_nodes(struct btrfs_trans_handle *trans,
  986. struct btrfs_fs_info *fs_info, u64 bytenr,
  987. u64 time_seq, struct ulist *refs,
  988. struct ulist *roots, const u64 *extent_item_pos,
  989. struct share_check *sc, bool ignore_offset)
  990. {
  991. struct btrfs_key key;
  992. struct btrfs_path *path;
  993. struct btrfs_delayed_ref_root *delayed_refs = NULL;
  994. struct btrfs_delayed_ref_head *head;
  995. int info_level = 0;
  996. int ret;
  997. struct prelim_ref *ref;
  998. struct rb_node *node;
  999. struct extent_inode_elem *eie = NULL;
  1000. /* total of both direct AND indirect refs! */
  1001. u64 total_refs = 0;
  1002. struct preftrees preftrees = {
  1003. .direct = PREFTREE_INIT,
  1004. .indirect = PREFTREE_INIT,
  1005. .indirect_missing_keys = PREFTREE_INIT
  1006. };
  1007. key.objectid = bytenr;
  1008. key.offset = (u64)-1;
  1009. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  1010. key.type = BTRFS_METADATA_ITEM_KEY;
  1011. else
  1012. key.type = BTRFS_EXTENT_ITEM_KEY;
  1013. path = btrfs_alloc_path();
  1014. if (!path)
  1015. return -ENOMEM;
  1016. if (!trans) {
  1017. path->search_commit_root = 1;
  1018. path->skip_locking = 1;
  1019. }
  1020. if (time_seq == SEQ_LAST)
  1021. path->skip_locking = 1;
  1022. /*
  1023. * grab both a lock on the path and a lock on the delayed ref head.
  1024. * We need both to get a consistent picture of how the refs look
  1025. * at a specified point in time
  1026. */
  1027. again:
  1028. head = NULL;
  1029. ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
  1030. if (ret < 0)
  1031. goto out;
  1032. BUG_ON(ret == 0);
  1033. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  1034. if (trans && likely(trans->type != __TRANS_DUMMY) &&
  1035. time_seq != SEQ_LAST) {
  1036. #else
  1037. if (trans && time_seq != SEQ_LAST) {
  1038. #endif
  1039. /*
  1040. * look if there are updates for this ref queued and lock the
  1041. * head
  1042. */
  1043. delayed_refs = &trans->transaction->delayed_refs;
  1044. spin_lock(&delayed_refs->lock);
  1045. head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
  1046. if (head) {
  1047. if (!mutex_trylock(&head->mutex)) {
  1048. refcount_inc(&head->refs);
  1049. spin_unlock(&delayed_refs->lock);
  1050. btrfs_release_path(path);
  1051. /*
  1052. * Mutex was contended, block until it's
  1053. * released and try again
  1054. */
  1055. mutex_lock(&head->mutex);
  1056. mutex_unlock(&head->mutex);
  1057. btrfs_put_delayed_ref_head(head);
  1058. goto again;
  1059. }
  1060. spin_unlock(&delayed_refs->lock);
  1061. ret = add_delayed_refs(fs_info, head, time_seq,
  1062. &preftrees, &total_refs, sc);
  1063. mutex_unlock(&head->mutex);
  1064. if (ret)
  1065. goto out;
  1066. } else {
  1067. spin_unlock(&delayed_refs->lock);
  1068. }
  1069. }
  1070. if (path->slots[0]) {
  1071. struct extent_buffer *leaf;
  1072. int slot;
  1073. path->slots[0]--;
  1074. leaf = path->nodes[0];
  1075. slot = path->slots[0];
  1076. btrfs_item_key_to_cpu(leaf, &key, slot);
  1077. if (key.objectid == bytenr &&
  1078. (key.type == BTRFS_EXTENT_ITEM_KEY ||
  1079. key.type == BTRFS_METADATA_ITEM_KEY)) {
  1080. ret = add_inline_refs(fs_info, path, bytenr,
  1081. &info_level, &preftrees,
  1082. &total_refs, sc);
  1083. if (ret)
  1084. goto out;
  1085. ret = add_keyed_refs(fs_info, path, bytenr, info_level,
  1086. &preftrees, sc);
  1087. if (ret)
  1088. goto out;
  1089. }
  1090. }
  1091. btrfs_release_path(path);
  1092. ret = add_missing_keys(fs_info, &preftrees);
  1093. if (ret)
  1094. goto out;
  1095. WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root.rb_root));
  1096. ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees,
  1097. extent_item_pos, total_refs, sc, ignore_offset);
  1098. if (ret)
  1099. goto out;
  1100. WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root.rb_root));
  1101. /*
  1102. * This walks the tree of merged and resolved refs. Tree blocks are
  1103. * read in as needed. Unique entries are added to the ulist, and
  1104. * the list of found roots is updated.
  1105. *
  1106. * We release the entire tree in one go before returning.
  1107. */
  1108. node = rb_first_cached(&preftrees.direct.root);
  1109. while (node) {
  1110. ref = rb_entry(node, struct prelim_ref, rbnode);
  1111. node = rb_next(&ref->rbnode);
  1112. /*
  1113. * ref->count < 0 can happen here if there are delayed
  1114. * refs with a node->action of BTRFS_DROP_DELAYED_REF.
  1115. * prelim_ref_insert() relies on this when merging
  1116. * identical refs to keep the overall count correct.
  1117. * prelim_ref_insert() will merge only those refs
  1118. * which compare identically. Any refs having
  1119. * e.g. different offsets would not be merged,
  1120. * and would retain their original ref->count < 0.
  1121. */
  1122. if (roots && ref->count && ref->root_id && ref->parent == 0) {
  1123. if (sc && sc->root_objectid &&
  1124. ref->root_id != sc->root_objectid) {
  1125. ret = BACKREF_FOUND_SHARED;
  1126. goto out;
  1127. }
  1128. /* no parent == root of tree */
  1129. ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
  1130. if (ret < 0)
  1131. goto out;
  1132. }
  1133. if (ref->count && ref->parent) {
  1134. if (extent_item_pos && !ref->inode_list &&
  1135. ref->level == 0) {
  1136. struct extent_buffer *eb;
  1137. eb = read_tree_block(fs_info, ref->parent, 0,
  1138. ref->level, NULL);
  1139. if (IS_ERR(eb)) {
  1140. ret = PTR_ERR(eb);
  1141. goto out;
  1142. } else if (!extent_buffer_uptodate(eb)) {
  1143. free_extent_buffer(eb);
  1144. ret = -EIO;
  1145. goto out;
  1146. }
  1147. btrfs_tree_read_lock(eb);
  1148. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1149. ret = find_extent_in_eb(eb, bytenr,
  1150. *extent_item_pos, &eie, ignore_offset);
  1151. btrfs_tree_read_unlock_blocking(eb);
  1152. free_extent_buffer(eb);
  1153. if (ret < 0)
  1154. goto out;
  1155. ref->inode_list = eie;
  1156. }
  1157. ret = ulist_add_merge_ptr(refs, ref->parent,
  1158. ref->inode_list,
  1159. (void **)&eie, GFP_NOFS);
  1160. if (ret < 0)
  1161. goto out;
  1162. if (!ret && extent_item_pos) {
  1163. /*
  1164. * we've recorded that parent, so we must extend
  1165. * its inode list here
  1166. */
  1167. BUG_ON(!eie);
  1168. while (eie->next)
  1169. eie = eie->next;
  1170. eie->next = ref->inode_list;
  1171. }
  1172. eie = NULL;
  1173. }
  1174. cond_resched();
  1175. }
  1176. out:
  1177. btrfs_free_path(path);
  1178. prelim_release(&preftrees.direct);
  1179. prelim_release(&preftrees.indirect);
  1180. prelim_release(&preftrees.indirect_missing_keys);
  1181. if (ret < 0)
  1182. free_inode_elem_list(eie);
  1183. return ret;
  1184. }
  1185. static void free_leaf_list(struct ulist *blocks)
  1186. {
  1187. struct ulist_node *node = NULL;
  1188. struct extent_inode_elem *eie;
  1189. struct ulist_iterator uiter;
  1190. ULIST_ITER_INIT(&uiter);
  1191. while ((node = ulist_next(blocks, &uiter))) {
  1192. if (!node->aux)
  1193. continue;
  1194. eie = unode_aux_to_inode_list(node);
  1195. free_inode_elem_list(eie);
  1196. node->aux = 0;
  1197. }
  1198. ulist_free(blocks);
  1199. }
  1200. /*
  1201. * Finds all leafs with a reference to the specified combination of bytenr and
  1202. * offset. key_list_head will point to a list of corresponding keys (caller must
  1203. * free each list element). The leafs will be stored in the leafs ulist, which
  1204. * must be freed with ulist_free.
  1205. *
  1206. * returns 0 on success, <0 on error
  1207. */
  1208. static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
  1209. struct btrfs_fs_info *fs_info, u64 bytenr,
  1210. u64 time_seq, struct ulist **leafs,
  1211. const u64 *extent_item_pos, bool ignore_offset)
  1212. {
  1213. int ret;
  1214. *leafs = ulist_alloc(GFP_NOFS);
  1215. if (!*leafs)
  1216. return -ENOMEM;
  1217. ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
  1218. *leafs, NULL, extent_item_pos, NULL, ignore_offset);
  1219. if (ret < 0 && ret != -ENOENT) {
  1220. free_leaf_list(*leafs);
  1221. return ret;
  1222. }
  1223. return 0;
  1224. }
  1225. /*
  1226. * walk all backrefs for a given extent to find all roots that reference this
  1227. * extent. Walking a backref means finding all extents that reference this
  1228. * extent and in turn walk the backrefs of those, too. Naturally this is a
  1229. * recursive process, but here it is implemented in an iterative fashion: We
  1230. * find all referencing extents for the extent in question and put them on a
  1231. * list. In turn, we find all referencing extents for those, further appending
  1232. * to the list. The way we iterate the list allows adding more elements after
  1233. * the current while iterating. The process stops when we reach the end of the
  1234. * list. Found roots are added to the roots list.
  1235. *
  1236. * returns 0 on success, < 0 on error.
  1237. */
  1238. static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans,
  1239. struct btrfs_fs_info *fs_info, u64 bytenr,
  1240. u64 time_seq, struct ulist **roots,
  1241. bool ignore_offset)
  1242. {
  1243. struct ulist *tmp;
  1244. struct ulist_node *node = NULL;
  1245. struct ulist_iterator uiter;
  1246. int ret;
  1247. tmp = ulist_alloc(GFP_NOFS);
  1248. if (!tmp)
  1249. return -ENOMEM;
  1250. *roots = ulist_alloc(GFP_NOFS);
  1251. if (!*roots) {
  1252. ulist_free(tmp);
  1253. return -ENOMEM;
  1254. }
  1255. ULIST_ITER_INIT(&uiter);
  1256. while (1) {
  1257. ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
  1258. tmp, *roots, NULL, NULL, ignore_offset);
  1259. if (ret < 0 && ret != -ENOENT) {
  1260. ulist_free(tmp);
  1261. ulist_free(*roots);
  1262. return ret;
  1263. }
  1264. node = ulist_next(tmp, &uiter);
  1265. if (!node)
  1266. break;
  1267. bytenr = node->val;
  1268. cond_resched();
  1269. }
  1270. ulist_free(tmp);
  1271. return 0;
  1272. }
  1273. int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
  1274. struct btrfs_fs_info *fs_info, u64 bytenr,
  1275. u64 time_seq, struct ulist **roots,
  1276. bool ignore_offset)
  1277. {
  1278. int ret;
  1279. if (!trans)
  1280. down_read(&fs_info->commit_root_sem);
  1281. ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr,
  1282. time_seq, roots, ignore_offset);
  1283. if (!trans)
  1284. up_read(&fs_info->commit_root_sem);
  1285. return ret;
  1286. }
  1287. /**
  1288. * btrfs_check_shared - tell us whether an extent is shared
  1289. *
  1290. * btrfs_check_shared uses the backref walking code but will short
  1291. * circuit as soon as it finds a root or inode that doesn't match the
  1292. * one passed in. This provides a significant performance benefit for
  1293. * callers (such as fiemap) which want to know whether the extent is
  1294. * shared but do not need a ref count.
  1295. *
  1296. * This attempts to allocate a transaction in order to account for
  1297. * delayed refs, but continues on even when the alloc fails.
  1298. *
  1299. * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
  1300. */
  1301. int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr)
  1302. {
  1303. struct btrfs_fs_info *fs_info = root->fs_info;
  1304. struct btrfs_trans_handle *trans;
  1305. struct ulist *tmp = NULL;
  1306. struct ulist *roots = NULL;
  1307. struct ulist_iterator uiter;
  1308. struct ulist_node *node;
  1309. struct seq_list elem = SEQ_LIST_INIT(elem);
  1310. int ret = 0;
  1311. struct share_check shared = {
  1312. .root_objectid = root->root_key.objectid,
  1313. .inum = inum,
  1314. .share_count = 0,
  1315. };
  1316. tmp = ulist_alloc(GFP_NOFS);
  1317. roots = ulist_alloc(GFP_NOFS);
  1318. if (!tmp || !roots) {
  1319. ulist_free(tmp);
  1320. ulist_free(roots);
  1321. return -ENOMEM;
  1322. }
  1323. trans = btrfs_join_transaction(root);
  1324. if (IS_ERR(trans)) {
  1325. trans = NULL;
  1326. down_read(&fs_info->commit_root_sem);
  1327. } else {
  1328. btrfs_get_tree_mod_seq(fs_info, &elem);
  1329. }
  1330. ULIST_ITER_INIT(&uiter);
  1331. while (1) {
  1332. ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
  1333. roots, NULL, &shared, false);
  1334. if (ret == BACKREF_FOUND_SHARED) {
  1335. /* this is the only condition under which we return 1 */
  1336. ret = 1;
  1337. break;
  1338. }
  1339. if (ret < 0 && ret != -ENOENT)
  1340. break;
  1341. ret = 0;
  1342. node = ulist_next(tmp, &uiter);
  1343. if (!node)
  1344. break;
  1345. bytenr = node->val;
  1346. shared.share_count = 0;
  1347. cond_resched();
  1348. }
  1349. if (trans) {
  1350. btrfs_put_tree_mod_seq(fs_info, &elem);
  1351. btrfs_end_transaction(trans);
  1352. } else {
  1353. up_read(&fs_info->commit_root_sem);
  1354. }
  1355. ulist_free(tmp);
  1356. ulist_free(roots);
  1357. return ret;
  1358. }
  1359. int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
  1360. u64 start_off, struct btrfs_path *path,
  1361. struct btrfs_inode_extref **ret_extref,
  1362. u64 *found_off)
  1363. {
  1364. int ret, slot;
  1365. struct btrfs_key key;
  1366. struct btrfs_key found_key;
  1367. struct btrfs_inode_extref *extref;
  1368. const struct extent_buffer *leaf;
  1369. unsigned long ptr;
  1370. key.objectid = inode_objectid;
  1371. key.type = BTRFS_INODE_EXTREF_KEY;
  1372. key.offset = start_off;
  1373. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1374. if (ret < 0)
  1375. return ret;
  1376. while (1) {
  1377. leaf = path->nodes[0];
  1378. slot = path->slots[0];
  1379. if (slot >= btrfs_header_nritems(leaf)) {
  1380. /*
  1381. * If the item at offset is not found,
  1382. * btrfs_search_slot will point us to the slot
  1383. * where it should be inserted. In our case
  1384. * that will be the slot directly before the
  1385. * next INODE_REF_KEY_V2 item. In the case
  1386. * that we're pointing to the last slot in a
  1387. * leaf, we must move one leaf over.
  1388. */
  1389. ret = btrfs_next_leaf(root, path);
  1390. if (ret) {
  1391. if (ret >= 1)
  1392. ret = -ENOENT;
  1393. break;
  1394. }
  1395. continue;
  1396. }
  1397. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1398. /*
  1399. * Check that we're still looking at an extended ref key for
  1400. * this particular objectid. If we have different
  1401. * objectid or type then there are no more to be found
  1402. * in the tree and we can exit.
  1403. */
  1404. ret = -ENOENT;
  1405. if (found_key.objectid != inode_objectid)
  1406. break;
  1407. if (found_key.type != BTRFS_INODE_EXTREF_KEY)
  1408. break;
  1409. ret = 0;
  1410. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1411. extref = (struct btrfs_inode_extref *)ptr;
  1412. *ret_extref = extref;
  1413. if (found_off)
  1414. *found_off = found_key.offset;
  1415. break;
  1416. }
  1417. return ret;
  1418. }
  1419. /*
  1420. * this iterates to turn a name (from iref/extref) into a full filesystem path.
  1421. * Elements of the path are separated by '/' and the path is guaranteed to be
  1422. * 0-terminated. the path is only given within the current file system.
  1423. * Therefore, it never starts with a '/'. the caller is responsible to provide
  1424. * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
  1425. * the start point of the resulting string is returned. this pointer is within
  1426. * dest, normally.
  1427. * in case the path buffer would overflow, the pointer is decremented further
  1428. * as if output was written to the buffer, though no more output is actually
  1429. * generated. that way, the caller can determine how much space would be
  1430. * required for the path to fit into the buffer. in that case, the returned
  1431. * value will be smaller than dest. callers must check this!
  1432. */
  1433. char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
  1434. u32 name_len, unsigned long name_off,
  1435. struct extent_buffer *eb_in, u64 parent,
  1436. char *dest, u32 size)
  1437. {
  1438. int slot;
  1439. u64 next_inum;
  1440. int ret;
  1441. s64 bytes_left = ((s64)size) - 1;
  1442. struct extent_buffer *eb = eb_in;
  1443. struct btrfs_key found_key;
  1444. int leave_spinning = path->leave_spinning;
  1445. struct btrfs_inode_ref *iref;
  1446. if (bytes_left >= 0)
  1447. dest[bytes_left] = '\0';
  1448. path->leave_spinning = 1;
  1449. while (1) {
  1450. bytes_left -= name_len;
  1451. if (bytes_left >= 0)
  1452. read_extent_buffer(eb, dest + bytes_left,
  1453. name_off, name_len);
  1454. if (eb != eb_in) {
  1455. if (!path->skip_locking)
  1456. btrfs_tree_read_unlock_blocking(eb);
  1457. free_extent_buffer(eb);
  1458. }
  1459. ret = btrfs_find_item(fs_root, path, parent, 0,
  1460. BTRFS_INODE_REF_KEY, &found_key);
  1461. if (ret > 0)
  1462. ret = -ENOENT;
  1463. if (ret)
  1464. break;
  1465. next_inum = found_key.offset;
  1466. /* regular exit ahead */
  1467. if (parent == next_inum)
  1468. break;
  1469. slot = path->slots[0];
  1470. eb = path->nodes[0];
  1471. /* make sure we can use eb after releasing the path */
  1472. if (eb != eb_in) {
  1473. if (!path->skip_locking)
  1474. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1475. path->nodes[0] = NULL;
  1476. path->locks[0] = 0;
  1477. }
  1478. btrfs_release_path(path);
  1479. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1480. name_len = btrfs_inode_ref_name_len(eb, iref);
  1481. name_off = (unsigned long)(iref + 1);
  1482. parent = next_inum;
  1483. --bytes_left;
  1484. if (bytes_left >= 0)
  1485. dest[bytes_left] = '/';
  1486. }
  1487. btrfs_release_path(path);
  1488. path->leave_spinning = leave_spinning;
  1489. if (ret)
  1490. return ERR_PTR(ret);
  1491. return dest + bytes_left;
  1492. }
  1493. /*
  1494. * this makes the path point to (logical EXTENT_ITEM *)
  1495. * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
  1496. * tree blocks and <0 on error.
  1497. */
  1498. int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
  1499. struct btrfs_path *path, struct btrfs_key *found_key,
  1500. u64 *flags_ret)
  1501. {
  1502. int ret;
  1503. u64 flags;
  1504. u64 size = 0;
  1505. u32 item_size;
  1506. const struct extent_buffer *eb;
  1507. struct btrfs_extent_item *ei;
  1508. struct btrfs_key key;
  1509. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  1510. key.type = BTRFS_METADATA_ITEM_KEY;
  1511. else
  1512. key.type = BTRFS_EXTENT_ITEM_KEY;
  1513. key.objectid = logical;
  1514. key.offset = (u64)-1;
  1515. ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
  1516. if (ret < 0)
  1517. return ret;
  1518. ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
  1519. if (ret) {
  1520. if (ret > 0)
  1521. ret = -ENOENT;
  1522. return ret;
  1523. }
  1524. btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
  1525. if (found_key->type == BTRFS_METADATA_ITEM_KEY)
  1526. size = fs_info->nodesize;
  1527. else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
  1528. size = found_key->offset;
  1529. if (found_key->objectid > logical ||
  1530. found_key->objectid + size <= logical) {
  1531. btrfs_debug(fs_info,
  1532. "logical %llu is not within any extent", logical);
  1533. return -ENOENT;
  1534. }
  1535. eb = path->nodes[0];
  1536. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  1537. BUG_ON(item_size < sizeof(*ei));
  1538. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  1539. flags = btrfs_extent_flags(eb, ei);
  1540. btrfs_debug(fs_info,
  1541. "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
  1542. logical, logical - found_key->objectid, found_key->objectid,
  1543. found_key->offset, flags, item_size);
  1544. WARN_ON(!flags_ret);
  1545. if (flags_ret) {
  1546. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1547. *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
  1548. else if (flags & BTRFS_EXTENT_FLAG_DATA)
  1549. *flags_ret = BTRFS_EXTENT_FLAG_DATA;
  1550. else
  1551. BUG_ON(1);
  1552. return 0;
  1553. }
  1554. return -EIO;
  1555. }
  1556. /*
  1557. * helper function to iterate extent inline refs. ptr must point to a 0 value
  1558. * for the first call and may be modified. it is used to track state.
  1559. * if more refs exist, 0 is returned and the next call to
  1560. * get_extent_inline_ref must pass the modified ptr parameter to get the
  1561. * next ref. after the last ref was processed, 1 is returned.
  1562. * returns <0 on error
  1563. */
  1564. static int get_extent_inline_ref(unsigned long *ptr,
  1565. const struct extent_buffer *eb,
  1566. const struct btrfs_key *key,
  1567. const struct btrfs_extent_item *ei,
  1568. u32 item_size,
  1569. struct btrfs_extent_inline_ref **out_eiref,
  1570. int *out_type)
  1571. {
  1572. unsigned long end;
  1573. u64 flags;
  1574. struct btrfs_tree_block_info *info;
  1575. if (!*ptr) {
  1576. /* first call */
  1577. flags = btrfs_extent_flags(eb, ei);
  1578. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1579. if (key->type == BTRFS_METADATA_ITEM_KEY) {
  1580. /* a skinny metadata extent */
  1581. *out_eiref =
  1582. (struct btrfs_extent_inline_ref *)(ei + 1);
  1583. } else {
  1584. WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
  1585. info = (struct btrfs_tree_block_info *)(ei + 1);
  1586. *out_eiref =
  1587. (struct btrfs_extent_inline_ref *)(info + 1);
  1588. }
  1589. } else {
  1590. *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
  1591. }
  1592. *ptr = (unsigned long)*out_eiref;
  1593. if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
  1594. return -ENOENT;
  1595. }
  1596. end = (unsigned long)ei + item_size;
  1597. *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
  1598. *out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref,
  1599. BTRFS_REF_TYPE_ANY);
  1600. if (*out_type == BTRFS_REF_TYPE_INVALID)
  1601. return -EUCLEAN;
  1602. *ptr += btrfs_extent_inline_ref_size(*out_type);
  1603. WARN_ON(*ptr > end);
  1604. if (*ptr == end)
  1605. return 1; /* last */
  1606. return 0;
  1607. }
  1608. /*
  1609. * reads the tree block backref for an extent. tree level and root are returned
  1610. * through out_level and out_root. ptr must point to a 0 value for the first
  1611. * call and may be modified (see get_extent_inline_ref comment).
  1612. * returns 0 if data was provided, 1 if there was no more data to provide or
  1613. * <0 on error.
  1614. */
  1615. int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
  1616. struct btrfs_key *key, struct btrfs_extent_item *ei,
  1617. u32 item_size, u64 *out_root, u8 *out_level)
  1618. {
  1619. int ret;
  1620. int type;
  1621. struct btrfs_extent_inline_ref *eiref;
  1622. if (*ptr == (unsigned long)-1)
  1623. return 1;
  1624. while (1) {
  1625. ret = get_extent_inline_ref(ptr, eb, key, ei, item_size,
  1626. &eiref, &type);
  1627. if (ret < 0)
  1628. return ret;
  1629. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  1630. type == BTRFS_SHARED_BLOCK_REF_KEY)
  1631. break;
  1632. if (ret == 1)
  1633. return 1;
  1634. }
  1635. /* we can treat both ref types equally here */
  1636. *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
  1637. if (key->type == BTRFS_EXTENT_ITEM_KEY) {
  1638. struct btrfs_tree_block_info *info;
  1639. info = (struct btrfs_tree_block_info *)(ei + 1);
  1640. *out_level = btrfs_tree_block_level(eb, info);
  1641. } else {
  1642. ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
  1643. *out_level = (u8)key->offset;
  1644. }
  1645. if (ret == 1)
  1646. *ptr = (unsigned long)-1;
  1647. return 0;
  1648. }
  1649. static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
  1650. struct extent_inode_elem *inode_list,
  1651. u64 root, u64 extent_item_objectid,
  1652. iterate_extent_inodes_t *iterate, void *ctx)
  1653. {
  1654. struct extent_inode_elem *eie;
  1655. int ret = 0;
  1656. for (eie = inode_list; eie; eie = eie->next) {
  1657. btrfs_debug(fs_info,
  1658. "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu",
  1659. extent_item_objectid, eie->inum,
  1660. eie->offset, root);
  1661. ret = iterate(eie->inum, eie->offset, root, ctx);
  1662. if (ret) {
  1663. btrfs_debug(fs_info,
  1664. "stopping iteration for %llu due to ret=%d",
  1665. extent_item_objectid, ret);
  1666. break;
  1667. }
  1668. }
  1669. return ret;
  1670. }
  1671. /*
  1672. * calls iterate() for every inode that references the extent identified by
  1673. * the given parameters.
  1674. * when the iterator function returns a non-zero value, iteration stops.
  1675. */
  1676. int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
  1677. u64 extent_item_objectid, u64 extent_item_pos,
  1678. int search_commit_root,
  1679. iterate_extent_inodes_t *iterate, void *ctx,
  1680. bool ignore_offset)
  1681. {
  1682. int ret;
  1683. struct btrfs_trans_handle *trans = NULL;
  1684. struct ulist *refs = NULL;
  1685. struct ulist *roots = NULL;
  1686. struct ulist_node *ref_node = NULL;
  1687. struct ulist_node *root_node = NULL;
  1688. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  1689. struct ulist_iterator ref_uiter;
  1690. struct ulist_iterator root_uiter;
  1691. btrfs_debug(fs_info, "resolving all inodes for extent %llu",
  1692. extent_item_objectid);
  1693. if (!search_commit_root) {
  1694. trans = btrfs_join_transaction(fs_info->extent_root);
  1695. if (IS_ERR(trans))
  1696. return PTR_ERR(trans);
  1697. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1698. } else {
  1699. down_read(&fs_info->commit_root_sem);
  1700. }
  1701. ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
  1702. tree_mod_seq_elem.seq, &refs,
  1703. &extent_item_pos, ignore_offset);
  1704. if (ret)
  1705. goto out;
  1706. ULIST_ITER_INIT(&ref_uiter);
  1707. while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
  1708. ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val,
  1709. tree_mod_seq_elem.seq, &roots,
  1710. ignore_offset);
  1711. if (ret)
  1712. break;
  1713. ULIST_ITER_INIT(&root_uiter);
  1714. while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
  1715. btrfs_debug(fs_info,
  1716. "root %llu references leaf %llu, data list %#llx",
  1717. root_node->val, ref_node->val,
  1718. ref_node->aux);
  1719. ret = iterate_leaf_refs(fs_info,
  1720. (struct extent_inode_elem *)
  1721. (uintptr_t)ref_node->aux,
  1722. root_node->val,
  1723. extent_item_objectid,
  1724. iterate, ctx);
  1725. }
  1726. ulist_free(roots);
  1727. }
  1728. free_leaf_list(refs);
  1729. out:
  1730. if (!search_commit_root) {
  1731. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1732. btrfs_end_transaction(trans);
  1733. } else {
  1734. up_read(&fs_info->commit_root_sem);
  1735. }
  1736. return ret;
  1737. }
  1738. int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
  1739. struct btrfs_path *path,
  1740. iterate_extent_inodes_t *iterate, void *ctx,
  1741. bool ignore_offset)
  1742. {
  1743. int ret;
  1744. u64 extent_item_pos;
  1745. u64 flags = 0;
  1746. struct btrfs_key found_key;
  1747. int search_commit_root = path->search_commit_root;
  1748. ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
  1749. btrfs_release_path(path);
  1750. if (ret < 0)
  1751. return ret;
  1752. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1753. return -EINVAL;
  1754. extent_item_pos = logical - found_key.objectid;
  1755. ret = iterate_extent_inodes(fs_info, found_key.objectid,
  1756. extent_item_pos, search_commit_root,
  1757. iterate, ctx, ignore_offset);
  1758. return ret;
  1759. }
  1760. typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
  1761. struct extent_buffer *eb, void *ctx);
  1762. static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
  1763. struct btrfs_path *path,
  1764. iterate_irefs_t *iterate, void *ctx)
  1765. {
  1766. int ret = 0;
  1767. int slot;
  1768. u32 cur;
  1769. u32 len;
  1770. u32 name_len;
  1771. u64 parent = 0;
  1772. int found = 0;
  1773. struct extent_buffer *eb;
  1774. struct btrfs_item *item;
  1775. struct btrfs_inode_ref *iref;
  1776. struct btrfs_key found_key;
  1777. while (!ret) {
  1778. ret = btrfs_find_item(fs_root, path, inum,
  1779. parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
  1780. &found_key);
  1781. if (ret < 0)
  1782. break;
  1783. if (ret) {
  1784. ret = found ? 0 : -ENOENT;
  1785. break;
  1786. }
  1787. ++found;
  1788. parent = found_key.offset;
  1789. slot = path->slots[0];
  1790. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  1791. if (!eb) {
  1792. ret = -ENOMEM;
  1793. break;
  1794. }
  1795. extent_buffer_get(eb);
  1796. btrfs_tree_read_lock(eb);
  1797. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1798. btrfs_release_path(path);
  1799. item = btrfs_item_nr(slot);
  1800. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1801. for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
  1802. name_len = btrfs_inode_ref_name_len(eb, iref);
  1803. /* path must be released before calling iterate()! */
  1804. btrfs_debug(fs_root->fs_info,
  1805. "following ref at offset %u for inode %llu in tree %llu",
  1806. cur, found_key.objectid,
  1807. fs_root->root_key.objectid);
  1808. ret = iterate(parent, name_len,
  1809. (unsigned long)(iref + 1), eb, ctx);
  1810. if (ret)
  1811. break;
  1812. len = sizeof(*iref) + name_len;
  1813. iref = (struct btrfs_inode_ref *)((char *)iref + len);
  1814. }
  1815. btrfs_tree_read_unlock_blocking(eb);
  1816. free_extent_buffer(eb);
  1817. }
  1818. btrfs_release_path(path);
  1819. return ret;
  1820. }
  1821. static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
  1822. struct btrfs_path *path,
  1823. iterate_irefs_t *iterate, void *ctx)
  1824. {
  1825. int ret;
  1826. int slot;
  1827. u64 offset = 0;
  1828. u64 parent;
  1829. int found = 0;
  1830. struct extent_buffer *eb;
  1831. struct btrfs_inode_extref *extref;
  1832. u32 item_size;
  1833. u32 cur_offset;
  1834. unsigned long ptr;
  1835. while (1) {
  1836. ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
  1837. &offset);
  1838. if (ret < 0)
  1839. break;
  1840. if (ret) {
  1841. ret = found ? 0 : -ENOENT;
  1842. break;
  1843. }
  1844. ++found;
  1845. slot = path->slots[0];
  1846. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  1847. if (!eb) {
  1848. ret = -ENOMEM;
  1849. break;
  1850. }
  1851. extent_buffer_get(eb);
  1852. btrfs_tree_read_lock(eb);
  1853. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1854. btrfs_release_path(path);
  1855. item_size = btrfs_item_size_nr(eb, slot);
  1856. ptr = btrfs_item_ptr_offset(eb, slot);
  1857. cur_offset = 0;
  1858. while (cur_offset < item_size) {
  1859. u32 name_len;
  1860. extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
  1861. parent = btrfs_inode_extref_parent(eb, extref);
  1862. name_len = btrfs_inode_extref_name_len(eb, extref);
  1863. ret = iterate(parent, name_len,
  1864. (unsigned long)&extref->name, eb, ctx);
  1865. if (ret)
  1866. break;
  1867. cur_offset += btrfs_inode_extref_name_len(eb, extref);
  1868. cur_offset += sizeof(*extref);
  1869. }
  1870. btrfs_tree_read_unlock_blocking(eb);
  1871. free_extent_buffer(eb);
  1872. offset++;
  1873. }
  1874. btrfs_release_path(path);
  1875. return ret;
  1876. }
  1877. static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
  1878. struct btrfs_path *path, iterate_irefs_t *iterate,
  1879. void *ctx)
  1880. {
  1881. int ret;
  1882. int found_refs = 0;
  1883. ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
  1884. if (!ret)
  1885. ++found_refs;
  1886. else if (ret != -ENOENT)
  1887. return ret;
  1888. ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
  1889. if (ret == -ENOENT && found_refs)
  1890. return 0;
  1891. return ret;
  1892. }
  1893. /*
  1894. * returns 0 if the path could be dumped (probably truncated)
  1895. * returns <0 in case of an error
  1896. */
  1897. static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
  1898. struct extent_buffer *eb, void *ctx)
  1899. {
  1900. struct inode_fs_paths *ipath = ctx;
  1901. char *fspath;
  1902. char *fspath_min;
  1903. int i = ipath->fspath->elem_cnt;
  1904. const int s_ptr = sizeof(char *);
  1905. u32 bytes_left;
  1906. bytes_left = ipath->fspath->bytes_left > s_ptr ?
  1907. ipath->fspath->bytes_left - s_ptr : 0;
  1908. fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
  1909. fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
  1910. name_off, eb, inum, fspath_min, bytes_left);
  1911. if (IS_ERR(fspath))
  1912. return PTR_ERR(fspath);
  1913. if (fspath > fspath_min) {
  1914. ipath->fspath->val[i] = (u64)(unsigned long)fspath;
  1915. ++ipath->fspath->elem_cnt;
  1916. ipath->fspath->bytes_left = fspath - fspath_min;
  1917. } else {
  1918. ++ipath->fspath->elem_missed;
  1919. ipath->fspath->bytes_missing += fspath_min - fspath;
  1920. ipath->fspath->bytes_left = 0;
  1921. }
  1922. return 0;
  1923. }
  1924. /*
  1925. * this dumps all file system paths to the inode into the ipath struct, provided
  1926. * is has been created large enough. each path is zero-terminated and accessed
  1927. * from ipath->fspath->val[i].
  1928. * when it returns, there are ipath->fspath->elem_cnt number of paths available
  1929. * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
  1930. * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
  1931. * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
  1932. * have been needed to return all paths.
  1933. */
  1934. int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
  1935. {
  1936. return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
  1937. inode_to_path, ipath);
  1938. }
  1939. struct btrfs_data_container *init_data_container(u32 total_bytes)
  1940. {
  1941. struct btrfs_data_container *data;
  1942. size_t alloc_bytes;
  1943. alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
  1944. data = kvmalloc(alloc_bytes, GFP_KERNEL);
  1945. if (!data)
  1946. return ERR_PTR(-ENOMEM);
  1947. if (total_bytes >= sizeof(*data)) {
  1948. data->bytes_left = total_bytes - sizeof(*data);
  1949. data->bytes_missing = 0;
  1950. } else {
  1951. data->bytes_missing = sizeof(*data) - total_bytes;
  1952. data->bytes_left = 0;
  1953. }
  1954. data->elem_cnt = 0;
  1955. data->elem_missed = 0;
  1956. return data;
  1957. }
  1958. /*
  1959. * allocates space to return multiple file system paths for an inode.
  1960. * total_bytes to allocate are passed, note that space usable for actual path
  1961. * information will be total_bytes - sizeof(struct inode_fs_paths).
  1962. * the returned pointer must be freed with free_ipath() in the end.
  1963. */
  1964. struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
  1965. struct btrfs_path *path)
  1966. {
  1967. struct inode_fs_paths *ifp;
  1968. struct btrfs_data_container *fspath;
  1969. fspath = init_data_container(total_bytes);
  1970. if (IS_ERR(fspath))
  1971. return ERR_CAST(fspath);
  1972. ifp = kmalloc(sizeof(*ifp), GFP_KERNEL);
  1973. if (!ifp) {
  1974. kvfree(fspath);
  1975. return ERR_PTR(-ENOMEM);
  1976. }
  1977. ifp->btrfs_path = path;
  1978. ifp->fspath = fspath;
  1979. ifp->fs_root = fs_root;
  1980. return ifp;
  1981. }
  1982. void free_ipath(struct inode_fs_paths *ipath)
  1983. {
  1984. if (!ipath)
  1985. return;
  1986. kvfree(ipath->fspath);
  1987. kfree(ipath);
  1988. }