backref.c 59 KB

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