backref.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740
  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. #include "misc.h"
  16. #include "tree-mod-log.h"
  17. #include "fs.h"
  18. #include "accessors.h"
  19. #include "extent-tree.h"
  20. #include "relocation.h"
  21. #include "tree-checker.h"
  22. /* Just arbitrary numbers so we can be sure one of these happened. */
  23. #define BACKREF_FOUND_SHARED 6
  24. #define BACKREF_FOUND_NOT_SHARED 7
  25. struct extent_inode_elem {
  26. u64 inum;
  27. u64 offset;
  28. u64 num_bytes;
  29. struct extent_inode_elem *next;
  30. };
  31. static int check_extent_in_eb(struct btrfs_backref_walk_ctx *ctx,
  32. const struct btrfs_key *key,
  33. const struct extent_buffer *eb,
  34. const struct btrfs_file_extent_item *fi,
  35. struct extent_inode_elem **eie)
  36. {
  37. const u64 data_len = btrfs_file_extent_num_bytes(eb, fi);
  38. u64 offset = key->offset;
  39. struct extent_inode_elem *e;
  40. const u64 *root_ids;
  41. int root_count;
  42. bool cached;
  43. if (!ctx->ignore_extent_item_pos &&
  44. !btrfs_file_extent_compression(eb, fi) &&
  45. !btrfs_file_extent_encryption(eb, fi) &&
  46. !btrfs_file_extent_other_encoding(eb, fi)) {
  47. u64 data_offset;
  48. data_offset = btrfs_file_extent_offset(eb, fi);
  49. if (ctx->extent_item_pos < data_offset ||
  50. ctx->extent_item_pos >= data_offset + data_len)
  51. return 1;
  52. offset += ctx->extent_item_pos - data_offset;
  53. }
  54. if (!ctx->indirect_ref_iterator || !ctx->cache_lookup)
  55. goto add_inode_elem;
  56. cached = ctx->cache_lookup(eb->start, ctx->user_ctx, &root_ids,
  57. &root_count);
  58. if (!cached)
  59. goto add_inode_elem;
  60. for (int i = 0; i < root_count; i++) {
  61. int ret;
  62. ret = ctx->indirect_ref_iterator(key->objectid, offset,
  63. data_len, root_ids[i],
  64. ctx->user_ctx);
  65. if (ret)
  66. return ret;
  67. }
  68. add_inode_elem:
  69. e = kmalloc(sizeof(*e), GFP_NOFS);
  70. if (!e)
  71. return -ENOMEM;
  72. e->next = *eie;
  73. e->inum = key->objectid;
  74. e->offset = offset;
  75. e->num_bytes = data_len;
  76. *eie = e;
  77. return 0;
  78. }
  79. static void free_inode_elem_list(struct extent_inode_elem *eie)
  80. {
  81. struct extent_inode_elem *eie_next;
  82. for (; eie; eie = eie_next) {
  83. eie_next = eie->next;
  84. kfree(eie);
  85. }
  86. }
  87. static int find_extent_in_eb(struct btrfs_backref_walk_ctx *ctx,
  88. const struct extent_buffer *eb,
  89. struct extent_inode_elem **eie)
  90. {
  91. u64 disk_byte;
  92. struct btrfs_key key;
  93. struct btrfs_file_extent_item *fi;
  94. int slot;
  95. int nritems;
  96. int extent_type;
  97. int ret;
  98. /*
  99. * from the shared data ref, we only have the leaf but we need
  100. * the key. thus, we must look into all items and see that we
  101. * find one (some) with a reference to our extent item.
  102. */
  103. nritems = btrfs_header_nritems(eb);
  104. for (slot = 0; slot < nritems; ++slot) {
  105. btrfs_item_key_to_cpu(eb, &key, slot);
  106. if (key.type != BTRFS_EXTENT_DATA_KEY)
  107. continue;
  108. fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  109. extent_type = btrfs_file_extent_type(eb, fi);
  110. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  111. continue;
  112. /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
  113. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  114. if (disk_byte != ctx->bytenr)
  115. continue;
  116. ret = check_extent_in_eb(ctx, &key, eb, fi, eie);
  117. if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || ret < 0)
  118. return ret;
  119. }
  120. return 0;
  121. }
  122. struct preftree {
  123. struct rb_root_cached root;
  124. unsigned int count;
  125. };
  126. #define PREFTREE_INIT { .root = RB_ROOT_CACHED, .count = 0 }
  127. struct preftrees {
  128. struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */
  129. struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */
  130. struct preftree indirect_missing_keys;
  131. };
  132. /*
  133. * Checks for a shared extent during backref search.
  134. *
  135. * The share_count tracks prelim_refs (direct and indirect) having a
  136. * ref->count >0:
  137. * - incremented when a ref->count transitions to >0
  138. * - decremented when a ref->count transitions to <1
  139. */
  140. struct share_check {
  141. struct btrfs_backref_share_check_ctx *ctx;
  142. struct btrfs_root *root;
  143. u64 inum;
  144. u64 data_bytenr;
  145. u64 data_extent_gen;
  146. /*
  147. * Counts number of inodes that refer to an extent (different inodes in
  148. * the same root or different roots) that we could find. The sharedness
  149. * check typically stops once this counter gets greater than 1, so it
  150. * may not reflect the total number of inodes.
  151. */
  152. int share_count;
  153. /*
  154. * The number of times we found our inode refers to the data extent we
  155. * are determining the sharedness. In other words, how many file extent
  156. * items we could find for our inode that point to our target data
  157. * extent. The value we get here after finishing the extent sharedness
  158. * check may be smaller than reality, but if it ends up being greater
  159. * than 1, then we know for sure the inode has multiple file extent
  160. * items that point to our inode, and we can safely assume it's useful
  161. * to cache the sharedness check result.
  162. */
  163. int self_ref_count;
  164. bool have_delayed_delete_refs;
  165. };
  166. static inline int extent_is_shared(struct share_check *sc)
  167. {
  168. return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0;
  169. }
  170. static struct kmem_cache *btrfs_prelim_ref_cache;
  171. int __init btrfs_prelim_ref_init(void)
  172. {
  173. btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
  174. sizeof(struct prelim_ref), 0, 0, NULL);
  175. if (!btrfs_prelim_ref_cache)
  176. return -ENOMEM;
  177. return 0;
  178. }
  179. void __cold btrfs_prelim_ref_exit(void)
  180. {
  181. kmem_cache_destroy(btrfs_prelim_ref_cache);
  182. }
  183. static void free_pref(struct prelim_ref *ref)
  184. {
  185. kmem_cache_free(btrfs_prelim_ref_cache, ref);
  186. }
  187. /*
  188. * Return 0 when both refs are for the same block (and can be merged).
  189. * A -1 return indicates ref1 is a 'lower' block than ref2, while 1
  190. * indicates a 'higher' block.
  191. */
  192. static int prelim_ref_compare(const struct prelim_ref *ref1,
  193. const struct prelim_ref *ref2)
  194. {
  195. if (ref1->level < ref2->level)
  196. return -1;
  197. if (ref1->level > ref2->level)
  198. return 1;
  199. if (ref1->root_id < ref2->root_id)
  200. return -1;
  201. if (ref1->root_id > ref2->root_id)
  202. return 1;
  203. if (ref1->key_for_search.type < ref2->key_for_search.type)
  204. return -1;
  205. if (ref1->key_for_search.type > ref2->key_for_search.type)
  206. return 1;
  207. if (ref1->key_for_search.objectid < ref2->key_for_search.objectid)
  208. return -1;
  209. if (ref1->key_for_search.objectid > ref2->key_for_search.objectid)
  210. return 1;
  211. if (ref1->key_for_search.offset < ref2->key_for_search.offset)
  212. return -1;
  213. if (ref1->key_for_search.offset > ref2->key_for_search.offset)
  214. return 1;
  215. if (ref1->parent < ref2->parent)
  216. return -1;
  217. if (ref1->parent > ref2->parent)
  218. return 1;
  219. return 0;
  220. }
  221. static void update_share_count(struct share_check *sc, int oldcount,
  222. int newcount, const struct prelim_ref *newref)
  223. {
  224. if ((!sc) || (oldcount == 0 && newcount < 1))
  225. return;
  226. if (oldcount > 0 && newcount < 1)
  227. sc->share_count--;
  228. else if (oldcount < 1 && newcount > 0)
  229. sc->share_count++;
  230. if (newref->root_id == btrfs_root_id(sc->root) &&
  231. newref->wanted_disk_byte == sc->data_bytenr &&
  232. newref->key_for_search.objectid == sc->inum)
  233. sc->self_ref_count += newref->count;
  234. }
  235. /*
  236. * Add @newref to the @root rbtree, merging identical refs.
  237. *
  238. * Callers should assume that newref has been freed after calling.
  239. */
  240. static void prelim_ref_insert(const struct btrfs_fs_info *fs_info,
  241. struct preftree *preftree,
  242. struct prelim_ref *newref,
  243. struct share_check *sc)
  244. {
  245. struct rb_root_cached *root;
  246. struct rb_node **p;
  247. struct rb_node *parent = NULL;
  248. struct prelim_ref *ref;
  249. int result;
  250. bool leftmost = true;
  251. root = &preftree->root;
  252. p = &root->rb_root.rb_node;
  253. while (*p) {
  254. parent = *p;
  255. ref = rb_entry(parent, struct prelim_ref, rbnode);
  256. result = prelim_ref_compare(ref, newref);
  257. if (result < 0) {
  258. p = &(*p)->rb_left;
  259. } else if (result > 0) {
  260. p = &(*p)->rb_right;
  261. leftmost = false;
  262. } else {
  263. /* Identical refs, merge them and free @newref */
  264. struct extent_inode_elem *eie = ref->inode_list;
  265. while (eie && eie->next)
  266. eie = eie->next;
  267. if (!eie)
  268. ref->inode_list = newref->inode_list;
  269. else
  270. eie->next = newref->inode_list;
  271. trace_btrfs_prelim_ref_merge(fs_info, ref, newref,
  272. preftree->count);
  273. /*
  274. * A delayed ref can have newref->count < 0.
  275. * The ref->count is updated to follow any
  276. * BTRFS_[ADD|DROP]_DELAYED_REF actions.
  277. */
  278. update_share_count(sc, ref->count,
  279. ref->count + newref->count, newref);
  280. ref->count += newref->count;
  281. free_pref(newref);
  282. return;
  283. }
  284. }
  285. update_share_count(sc, 0, newref->count, newref);
  286. preftree->count++;
  287. trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count);
  288. rb_link_node(&newref->rbnode, parent, p);
  289. rb_insert_color_cached(&newref->rbnode, root, leftmost);
  290. }
  291. /*
  292. * Release the entire tree. We don't care about internal consistency so
  293. * just free everything and then reset the tree root.
  294. */
  295. static void prelim_release(struct preftree *preftree)
  296. {
  297. struct prelim_ref *ref, *next_ref;
  298. rbtree_postorder_for_each_entry_safe(ref, next_ref,
  299. &preftree->root.rb_root, rbnode) {
  300. free_inode_elem_list(ref->inode_list);
  301. free_pref(ref);
  302. }
  303. preftree->root = RB_ROOT_CACHED;
  304. preftree->count = 0;
  305. }
  306. /*
  307. * the rules for all callers of this function are:
  308. * - obtaining the parent is the goal
  309. * - if you add a key, you must know that it is a correct key
  310. * - if you cannot add the parent or a correct key, then we will look into the
  311. * block later to set a correct key
  312. *
  313. * delayed refs
  314. * ============
  315. * backref type | shared | indirect | shared | indirect
  316. * information | tree | tree | data | data
  317. * --------------------+--------+----------+--------+----------
  318. * parent logical | y | - | - | -
  319. * key to resolve | - | y | y | y
  320. * tree block logical | - | - | - | -
  321. * root for resolving | y | y | y | y
  322. *
  323. * - column 1: we've the parent -> done
  324. * - column 2, 3, 4: we use the key to find the parent
  325. *
  326. * on disk refs (inline or keyed)
  327. * ==============================
  328. * backref type | shared | indirect | shared | indirect
  329. * information | tree | tree | data | data
  330. * --------------------+--------+----------+--------+----------
  331. * parent logical | y | - | y | -
  332. * key to resolve | - | - | - | y
  333. * tree block logical | y | y | y | y
  334. * root for resolving | - | y | y | y
  335. *
  336. * - column 1, 3: we've the parent -> done
  337. * - column 2: we take the first key from the block to find the parent
  338. * (see add_missing_keys)
  339. * - column 4: we use the key to find the parent
  340. *
  341. * additional information that's available but not required to find the parent
  342. * block might help in merging entries to gain some speed.
  343. */
  344. static int add_prelim_ref(const struct btrfs_fs_info *fs_info,
  345. struct preftree *preftree, u64 root_id,
  346. const struct btrfs_key *key, int level, u64 parent,
  347. u64 wanted_disk_byte, int count,
  348. struct share_check *sc, gfp_t gfp_mask)
  349. {
  350. struct prelim_ref *ref;
  351. if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID)
  352. return 0;
  353. ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
  354. if (!ref)
  355. return -ENOMEM;
  356. ref->root_id = root_id;
  357. if (key)
  358. ref->key_for_search = *key;
  359. else
  360. memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
  361. ref->inode_list = NULL;
  362. ref->level = level;
  363. ref->count = count;
  364. ref->parent = parent;
  365. ref->wanted_disk_byte = wanted_disk_byte;
  366. prelim_ref_insert(fs_info, preftree, ref, sc);
  367. return extent_is_shared(sc);
  368. }
  369. /* direct refs use root == 0, key == NULL */
  370. static int add_direct_ref(const struct btrfs_fs_info *fs_info,
  371. struct preftrees *preftrees, int level, u64 parent,
  372. u64 wanted_disk_byte, int count,
  373. struct share_check *sc, gfp_t gfp_mask)
  374. {
  375. return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level,
  376. parent, wanted_disk_byte, count, sc, gfp_mask);
  377. }
  378. /* indirect refs use parent == 0 */
  379. static int add_indirect_ref(const struct btrfs_fs_info *fs_info,
  380. struct preftrees *preftrees, u64 root_id,
  381. const struct btrfs_key *key, int level,
  382. u64 wanted_disk_byte, int count,
  383. struct share_check *sc, gfp_t gfp_mask)
  384. {
  385. struct preftree *tree = &preftrees->indirect;
  386. if (!key)
  387. tree = &preftrees->indirect_missing_keys;
  388. return add_prelim_ref(fs_info, tree, root_id, key, level, 0,
  389. wanted_disk_byte, count, sc, gfp_mask);
  390. }
  391. static int is_shared_data_backref(struct preftrees *preftrees, u64 bytenr)
  392. {
  393. struct rb_node **p = &preftrees->direct.root.rb_root.rb_node;
  394. struct rb_node *parent = NULL;
  395. struct prelim_ref *ref = NULL;
  396. struct prelim_ref target = {};
  397. int result;
  398. target.parent = bytenr;
  399. while (*p) {
  400. parent = *p;
  401. ref = rb_entry(parent, struct prelim_ref, rbnode);
  402. result = prelim_ref_compare(ref, &target);
  403. if (result < 0)
  404. p = &(*p)->rb_left;
  405. else if (result > 0)
  406. p = &(*p)->rb_right;
  407. else
  408. return 1;
  409. }
  410. return 0;
  411. }
  412. static int add_all_parents(struct btrfs_backref_walk_ctx *ctx,
  413. struct btrfs_root *root, struct btrfs_path *path,
  414. struct ulist *parents,
  415. struct preftrees *preftrees, struct prelim_ref *ref,
  416. int level)
  417. {
  418. int ret = 0;
  419. int slot;
  420. struct extent_buffer *eb;
  421. struct btrfs_key key;
  422. struct btrfs_key *key_for_search = &ref->key_for_search;
  423. struct btrfs_file_extent_item *fi;
  424. struct extent_inode_elem *eie = NULL, *old = NULL;
  425. u64 disk_byte;
  426. u64 wanted_disk_byte = ref->wanted_disk_byte;
  427. u64 count = 0;
  428. u64 data_offset;
  429. u8 type;
  430. if (level != 0) {
  431. eb = path->nodes[level];
  432. ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
  433. if (ret < 0)
  434. return ret;
  435. return 0;
  436. }
  437. /*
  438. * 1. We normally enter this function with the path already pointing to
  439. * the first item to check. But sometimes, we may enter it with
  440. * slot == nritems.
  441. * 2. We are searching for normal backref but bytenr of this leaf
  442. * matches shared data backref
  443. * 3. The leaf owner is not equal to the root we are searching
  444. *
  445. * For these cases, go to the next leaf before we continue.
  446. */
  447. eb = path->nodes[0];
  448. if (path->slots[0] >= btrfs_header_nritems(eb) ||
  449. is_shared_data_backref(preftrees, eb->start) ||
  450. ref->root_id != btrfs_header_owner(eb)) {
  451. if (ctx->time_seq == BTRFS_SEQ_LAST)
  452. ret = btrfs_next_leaf(root, path);
  453. else
  454. ret = btrfs_next_old_leaf(root, path, ctx->time_seq);
  455. }
  456. while (!ret && count < ref->count) {
  457. eb = path->nodes[0];
  458. slot = path->slots[0];
  459. btrfs_item_key_to_cpu(eb, &key, slot);
  460. if (key.objectid != key_for_search->objectid ||
  461. key.type != BTRFS_EXTENT_DATA_KEY)
  462. break;
  463. /*
  464. * We are searching for normal backref but bytenr of this leaf
  465. * matches shared data backref, OR
  466. * the leaf owner is not equal to the root we are searching for
  467. */
  468. if (slot == 0 &&
  469. (is_shared_data_backref(preftrees, eb->start) ||
  470. ref->root_id != btrfs_header_owner(eb))) {
  471. if (ctx->time_seq == BTRFS_SEQ_LAST)
  472. ret = btrfs_next_leaf(root, path);
  473. else
  474. ret = btrfs_next_old_leaf(root, path, ctx->time_seq);
  475. continue;
  476. }
  477. fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  478. type = btrfs_file_extent_type(eb, fi);
  479. if (type == BTRFS_FILE_EXTENT_INLINE)
  480. goto next;
  481. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  482. data_offset = btrfs_file_extent_offset(eb, fi);
  483. if (disk_byte == wanted_disk_byte) {
  484. eie = NULL;
  485. old = NULL;
  486. if (ref->key_for_search.offset == key.offset - data_offset)
  487. count++;
  488. else
  489. goto next;
  490. if (!ctx->skip_inode_ref_list) {
  491. ret = check_extent_in_eb(ctx, &key, eb, fi, &eie);
  492. if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP ||
  493. ret < 0)
  494. break;
  495. }
  496. if (ret > 0)
  497. goto next;
  498. ret = ulist_add_merge_ptr(parents, eb->start,
  499. eie, (void **)&old, GFP_NOFS);
  500. if (ret < 0)
  501. break;
  502. if (!ret && !ctx->skip_inode_ref_list) {
  503. while (old->next)
  504. old = old->next;
  505. old->next = eie;
  506. }
  507. eie = NULL;
  508. }
  509. next:
  510. if (ctx->time_seq == BTRFS_SEQ_LAST)
  511. ret = btrfs_next_item(root, path);
  512. else
  513. ret = btrfs_next_old_item(root, path, ctx->time_seq);
  514. }
  515. if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || ret < 0)
  516. free_inode_elem_list(eie);
  517. else if (ret > 0)
  518. ret = 0;
  519. return ret;
  520. }
  521. /*
  522. * resolve an indirect backref in the form (root_id, key, level)
  523. * to a logical address
  524. */
  525. static int resolve_indirect_ref(struct btrfs_backref_walk_ctx *ctx,
  526. struct btrfs_path *path,
  527. struct preftrees *preftrees,
  528. struct prelim_ref *ref, struct ulist *parents)
  529. {
  530. struct btrfs_root *root;
  531. struct extent_buffer *eb;
  532. int ret = 0;
  533. int root_level;
  534. int level = ref->level;
  535. struct btrfs_key search_key = ref->key_for_search;
  536. /*
  537. * If we're search_commit_root we could possibly be holding locks on
  538. * other tree nodes. This happens when qgroups does backref walks when
  539. * adding new delayed refs. To deal with this we need to look in cache
  540. * for the root, and if we don't find it then we need to search the
  541. * tree_root's commit root, thus the btrfs_get_fs_root_commit_root usage
  542. * here.
  543. */
  544. if (path->search_commit_root)
  545. root = btrfs_get_fs_root_commit_root(ctx->fs_info, path, ref->root_id);
  546. else
  547. root = btrfs_get_fs_root(ctx->fs_info, ref->root_id, false);
  548. if (IS_ERR(root)) {
  549. ret = PTR_ERR(root);
  550. goto out_free;
  551. }
  552. if (!path->search_commit_root &&
  553. test_bit(BTRFS_ROOT_DELETING, &root->state)) {
  554. ret = -ENOENT;
  555. goto out;
  556. }
  557. if (btrfs_is_testing(ctx->fs_info)) {
  558. ret = -ENOENT;
  559. goto out;
  560. }
  561. if (path->search_commit_root)
  562. root_level = btrfs_header_level(root->commit_root);
  563. else if (ctx->time_seq == BTRFS_SEQ_LAST)
  564. root_level = btrfs_header_level(root->node);
  565. else
  566. root_level = btrfs_old_root_level(root, ctx->time_seq);
  567. if (root_level + 1 == level)
  568. goto out;
  569. /*
  570. * We can often find data backrefs with an offset that is too large
  571. * (>= LLONG_MAX, maximum allowed file offset) due to underflows when
  572. * subtracting a file's offset with the data offset of its
  573. * corresponding extent data item. This can happen for example in the
  574. * clone ioctl.
  575. *
  576. * So if we detect such case we set the search key's offset to zero to
  577. * make sure we will find the matching file extent item at
  578. * add_all_parents(), otherwise we will miss it because the offset
  579. * taken form the backref is much larger then the offset of the file
  580. * extent item. This can make us scan a very large number of file
  581. * extent items, but at least it will not make us miss any.
  582. *
  583. * This is an ugly workaround for a behaviour that should have never
  584. * existed, but it does and a fix for the clone ioctl would touch a lot
  585. * of places, cause backwards incompatibility and would not fix the
  586. * problem for extents cloned with older kernels.
  587. */
  588. if (search_key.type == BTRFS_EXTENT_DATA_KEY &&
  589. search_key.offset >= LLONG_MAX)
  590. search_key.offset = 0;
  591. path->lowest_level = level;
  592. if (ctx->time_seq == BTRFS_SEQ_LAST)
  593. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  594. else
  595. ret = btrfs_search_old_slot(root, &search_key, path, ctx->time_seq);
  596. btrfs_debug(ctx->fs_info,
  597. "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)",
  598. ref->root_id, level, ref->count, ret,
  599. ref->key_for_search.objectid, ref->key_for_search.type,
  600. ref->key_for_search.offset);
  601. if (ret < 0)
  602. goto out;
  603. eb = path->nodes[level];
  604. while (!eb) {
  605. if (WARN_ON(!level)) {
  606. ret = 1;
  607. goto out;
  608. }
  609. level--;
  610. eb = path->nodes[level];
  611. }
  612. ret = add_all_parents(ctx, root, path, parents, preftrees, ref, level);
  613. out:
  614. btrfs_put_root(root);
  615. out_free:
  616. path->lowest_level = 0;
  617. btrfs_release_path(path);
  618. return ret;
  619. }
  620. static struct extent_inode_elem *
  621. unode_aux_to_inode_list(struct ulist_node *node)
  622. {
  623. if (!node)
  624. return NULL;
  625. return (struct extent_inode_elem *)(uintptr_t)node->aux;
  626. }
  627. static void free_leaf_list(struct ulist *ulist)
  628. {
  629. struct ulist_node *node;
  630. struct ulist_iterator uiter;
  631. ULIST_ITER_INIT(&uiter);
  632. while ((node = ulist_next(ulist, &uiter)))
  633. free_inode_elem_list(unode_aux_to_inode_list(node));
  634. ulist_free(ulist);
  635. }
  636. /*
  637. * We maintain three separate rbtrees: one for direct refs, one for
  638. * indirect refs which have a key, and one for indirect refs which do not
  639. * have a key. Each tree does merge on insertion.
  640. *
  641. * Once all of the references are located, we iterate over the tree of
  642. * indirect refs with missing keys. An appropriate key is located and
  643. * the ref is moved onto the tree for indirect refs. After all missing
  644. * keys are thus located, we iterate over the indirect ref tree, resolve
  645. * each reference, and then insert the resolved reference onto the
  646. * direct tree (merging there too).
  647. *
  648. * New backrefs (i.e., for parent nodes) are added to the appropriate
  649. * rbtree as they are encountered. The new backrefs are subsequently
  650. * resolved as above.
  651. */
  652. static int resolve_indirect_refs(struct btrfs_backref_walk_ctx *ctx,
  653. struct btrfs_path *path,
  654. struct preftrees *preftrees,
  655. struct share_check *sc)
  656. {
  657. int err;
  658. int ret = 0;
  659. struct ulist *parents;
  660. struct ulist_node *node;
  661. struct ulist_iterator uiter;
  662. struct rb_node *rnode;
  663. parents = ulist_alloc(GFP_NOFS);
  664. if (!parents)
  665. return -ENOMEM;
  666. /*
  667. * We could trade memory usage for performance here by iterating
  668. * the tree, allocating new refs for each insertion, and then
  669. * freeing the entire indirect tree when we're done. In some test
  670. * cases, the tree can grow quite large (~200k objects).
  671. */
  672. while ((rnode = rb_first_cached(&preftrees->indirect.root))) {
  673. struct prelim_ref *ref;
  674. ref = rb_entry(rnode, struct prelim_ref, rbnode);
  675. if (WARN(ref->parent,
  676. "BUG: direct ref found in indirect tree")) {
  677. ret = -EINVAL;
  678. goto out;
  679. }
  680. rb_erase_cached(&ref->rbnode, &preftrees->indirect.root);
  681. preftrees->indirect.count--;
  682. if (ref->count == 0) {
  683. free_pref(ref);
  684. continue;
  685. }
  686. if (sc && ref->root_id != btrfs_root_id(sc->root)) {
  687. free_pref(ref);
  688. ret = BACKREF_FOUND_SHARED;
  689. goto out;
  690. }
  691. err = resolve_indirect_ref(ctx, path, preftrees, ref, parents);
  692. /*
  693. * we can only tolerate ENOENT,otherwise,we should catch error
  694. * and return directly.
  695. */
  696. if (err == -ENOENT) {
  697. prelim_ref_insert(ctx->fs_info, &preftrees->direct, ref,
  698. NULL);
  699. continue;
  700. } else if (err) {
  701. free_pref(ref);
  702. ret = err;
  703. goto out;
  704. }
  705. /* we put the first parent into the ref at hand */
  706. ULIST_ITER_INIT(&uiter);
  707. node = ulist_next(parents, &uiter);
  708. ref->parent = node ? node->val : 0;
  709. ref->inode_list = unode_aux_to_inode_list(node);
  710. /* Add a prelim_ref(s) for any other parent(s). */
  711. while ((node = ulist_next(parents, &uiter))) {
  712. struct prelim_ref *new_ref;
  713. new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
  714. GFP_NOFS);
  715. if (!new_ref) {
  716. free_pref(ref);
  717. ret = -ENOMEM;
  718. goto out;
  719. }
  720. memcpy(new_ref, ref, sizeof(*ref));
  721. new_ref->parent = node->val;
  722. new_ref->inode_list = unode_aux_to_inode_list(node);
  723. prelim_ref_insert(ctx->fs_info, &preftrees->direct,
  724. new_ref, NULL);
  725. }
  726. /*
  727. * Now it's a direct ref, put it in the direct tree. We must
  728. * do this last because the ref could be merged/freed here.
  729. */
  730. prelim_ref_insert(ctx->fs_info, &preftrees->direct, ref, NULL);
  731. ulist_reinit(parents);
  732. cond_resched();
  733. }
  734. out:
  735. /*
  736. * We may have inode lists attached to refs in the parents ulist, so we
  737. * must free them before freeing the ulist and its refs.
  738. */
  739. free_leaf_list(parents);
  740. return ret;
  741. }
  742. /*
  743. * read tree blocks and add keys where required.
  744. */
  745. static int add_missing_keys(struct btrfs_fs_info *fs_info,
  746. struct preftrees *preftrees, bool lock)
  747. {
  748. struct prelim_ref *ref;
  749. struct extent_buffer *eb;
  750. struct preftree *tree = &preftrees->indirect_missing_keys;
  751. struct rb_node *node;
  752. while ((node = rb_first_cached(&tree->root))) {
  753. struct btrfs_tree_parent_check check = { 0 };
  754. ref = rb_entry(node, struct prelim_ref, rbnode);
  755. rb_erase_cached(node, &tree->root);
  756. BUG_ON(ref->parent); /* should not be a direct ref */
  757. BUG_ON(ref->key_for_search.type);
  758. BUG_ON(!ref->wanted_disk_byte);
  759. check.level = ref->level - 1;
  760. check.owner_root = ref->root_id;
  761. eb = read_tree_block(fs_info, ref->wanted_disk_byte, &check);
  762. if (IS_ERR(eb)) {
  763. free_pref(ref);
  764. return PTR_ERR(eb);
  765. }
  766. if (!extent_buffer_uptodate(eb)) {
  767. free_pref(ref);
  768. free_extent_buffer(eb);
  769. return -EIO;
  770. }
  771. if (lock)
  772. btrfs_tree_read_lock(eb);
  773. if (btrfs_header_level(eb) == 0)
  774. btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
  775. else
  776. btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
  777. if (lock)
  778. btrfs_tree_read_unlock(eb);
  779. free_extent_buffer(eb);
  780. prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL);
  781. cond_resched();
  782. }
  783. return 0;
  784. }
  785. /*
  786. * add all currently queued delayed refs from this head whose seq nr is
  787. * smaller or equal that seq to the list
  788. */
  789. static int add_delayed_refs(const struct btrfs_fs_info *fs_info,
  790. struct btrfs_delayed_ref_head *head, u64 seq,
  791. struct preftrees *preftrees, struct share_check *sc)
  792. {
  793. struct btrfs_delayed_ref_node *node;
  794. struct btrfs_key key;
  795. struct rb_node *n;
  796. int count;
  797. int ret = 0;
  798. spin_lock(&head->lock);
  799. for (n = rb_first_cached(&head->ref_tree); n; n = rb_next(n)) {
  800. node = rb_entry(n, struct btrfs_delayed_ref_node,
  801. ref_node);
  802. if (node->seq > seq)
  803. continue;
  804. switch (node->action) {
  805. case BTRFS_ADD_DELAYED_EXTENT:
  806. case BTRFS_UPDATE_DELAYED_HEAD:
  807. WARN_ON(1);
  808. continue;
  809. case BTRFS_ADD_DELAYED_REF:
  810. count = node->ref_mod;
  811. break;
  812. case BTRFS_DROP_DELAYED_REF:
  813. count = node->ref_mod * -1;
  814. break;
  815. default:
  816. BUG();
  817. }
  818. switch (node->type) {
  819. case BTRFS_TREE_BLOCK_REF_KEY: {
  820. /* NORMAL INDIRECT METADATA backref */
  821. struct btrfs_key *key_ptr = NULL;
  822. /* The owner of a tree block ref is the level. */
  823. int level = btrfs_delayed_ref_owner(node);
  824. if (head->extent_op && head->extent_op->update_key) {
  825. btrfs_disk_key_to_cpu(&key, &head->extent_op->key);
  826. key_ptr = &key;
  827. }
  828. ret = add_indirect_ref(fs_info, preftrees, node->ref_root,
  829. key_ptr, level + 1, node->bytenr,
  830. count, sc, GFP_ATOMIC);
  831. break;
  832. }
  833. case BTRFS_SHARED_BLOCK_REF_KEY: {
  834. /*
  835. * SHARED DIRECT METADATA backref
  836. *
  837. * The owner of a tree block ref is the level.
  838. */
  839. int level = btrfs_delayed_ref_owner(node);
  840. ret = add_direct_ref(fs_info, preftrees, level + 1,
  841. node->parent, node->bytenr, count,
  842. sc, GFP_ATOMIC);
  843. break;
  844. }
  845. case BTRFS_EXTENT_DATA_REF_KEY: {
  846. /* NORMAL INDIRECT DATA backref */
  847. key.objectid = btrfs_delayed_ref_owner(node);
  848. key.type = BTRFS_EXTENT_DATA_KEY;
  849. key.offset = btrfs_delayed_ref_offset(node);
  850. /*
  851. * If we have a share check context and a reference for
  852. * another inode, we can't exit immediately. This is
  853. * because even if this is a BTRFS_ADD_DELAYED_REF
  854. * reference we may find next a BTRFS_DROP_DELAYED_REF
  855. * which cancels out this ADD reference.
  856. *
  857. * If this is a DROP reference and there was no previous
  858. * ADD reference, then we need to signal that when we
  859. * process references from the extent tree (through
  860. * add_inline_refs() and add_keyed_refs()), we should
  861. * not exit early if we find a reference for another
  862. * inode, because one of the delayed DROP references
  863. * may cancel that reference in the extent tree.
  864. */
  865. if (sc && count < 0)
  866. sc->have_delayed_delete_refs = true;
  867. ret = add_indirect_ref(fs_info, preftrees, node->ref_root,
  868. &key, 0, node->bytenr, count, sc,
  869. GFP_ATOMIC);
  870. break;
  871. }
  872. case BTRFS_SHARED_DATA_REF_KEY: {
  873. /* SHARED DIRECT FULL backref */
  874. ret = add_direct_ref(fs_info, preftrees, 0, node->parent,
  875. node->bytenr, count, sc,
  876. GFP_ATOMIC);
  877. break;
  878. }
  879. default:
  880. WARN_ON(1);
  881. }
  882. /*
  883. * We must ignore BACKREF_FOUND_SHARED until all delayed
  884. * refs have been checked.
  885. */
  886. if (ret && (ret != BACKREF_FOUND_SHARED))
  887. break;
  888. }
  889. if (!ret)
  890. ret = extent_is_shared(sc);
  891. spin_unlock(&head->lock);
  892. return ret;
  893. }
  894. /*
  895. * add all inline backrefs for bytenr to the list
  896. *
  897. * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED.
  898. */
  899. static int add_inline_refs(struct btrfs_backref_walk_ctx *ctx,
  900. struct btrfs_path *path,
  901. int *info_level, struct preftrees *preftrees,
  902. struct share_check *sc)
  903. {
  904. int ret = 0;
  905. int slot;
  906. struct extent_buffer *leaf;
  907. struct btrfs_key key;
  908. struct btrfs_key found_key;
  909. unsigned long ptr;
  910. unsigned long end;
  911. struct btrfs_extent_item *ei;
  912. u64 flags;
  913. u64 item_size;
  914. /*
  915. * enumerate all inline refs
  916. */
  917. leaf = path->nodes[0];
  918. slot = path->slots[0];
  919. item_size = btrfs_item_size(leaf, slot);
  920. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  921. if (ctx->check_extent_item) {
  922. ret = ctx->check_extent_item(ctx->bytenr, ei, leaf, ctx->user_ctx);
  923. if (ret)
  924. return ret;
  925. }
  926. flags = btrfs_extent_flags(leaf, ei);
  927. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  928. ptr = (unsigned long)(ei + 1);
  929. end = (unsigned long)ei + item_size;
  930. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  931. flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  932. struct btrfs_tree_block_info *info;
  933. info = (struct btrfs_tree_block_info *)ptr;
  934. *info_level = btrfs_tree_block_level(leaf, info);
  935. ptr += sizeof(struct btrfs_tree_block_info);
  936. BUG_ON(ptr > end);
  937. } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
  938. *info_level = found_key.offset;
  939. } else {
  940. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
  941. }
  942. while (ptr < end) {
  943. struct btrfs_extent_inline_ref *iref;
  944. u64 offset;
  945. int type;
  946. iref = (struct btrfs_extent_inline_ref *)ptr;
  947. type = btrfs_get_extent_inline_ref_type(leaf, iref,
  948. BTRFS_REF_TYPE_ANY);
  949. if (type == BTRFS_REF_TYPE_INVALID)
  950. return -EUCLEAN;
  951. offset = btrfs_extent_inline_ref_offset(leaf, iref);
  952. switch (type) {
  953. case BTRFS_SHARED_BLOCK_REF_KEY:
  954. ret = add_direct_ref(ctx->fs_info, preftrees,
  955. *info_level + 1, offset,
  956. ctx->bytenr, 1, NULL, GFP_NOFS);
  957. break;
  958. case BTRFS_SHARED_DATA_REF_KEY: {
  959. struct btrfs_shared_data_ref *sdref;
  960. int count;
  961. sdref = (struct btrfs_shared_data_ref *)(iref + 1);
  962. count = btrfs_shared_data_ref_count(leaf, sdref);
  963. ret = add_direct_ref(ctx->fs_info, preftrees, 0, offset,
  964. ctx->bytenr, count, sc, GFP_NOFS);
  965. break;
  966. }
  967. case BTRFS_TREE_BLOCK_REF_KEY:
  968. ret = add_indirect_ref(ctx->fs_info, preftrees, offset,
  969. NULL, *info_level + 1,
  970. ctx->bytenr, 1, NULL, GFP_NOFS);
  971. break;
  972. case BTRFS_EXTENT_DATA_REF_KEY: {
  973. struct btrfs_extent_data_ref *dref;
  974. int count;
  975. u64 root;
  976. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  977. count = btrfs_extent_data_ref_count(leaf, dref);
  978. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  979. dref);
  980. key.type = BTRFS_EXTENT_DATA_KEY;
  981. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  982. if (sc && key.objectid != sc->inum &&
  983. !sc->have_delayed_delete_refs) {
  984. ret = BACKREF_FOUND_SHARED;
  985. break;
  986. }
  987. root = btrfs_extent_data_ref_root(leaf, dref);
  988. if (!ctx->skip_data_ref ||
  989. !ctx->skip_data_ref(root, key.objectid, key.offset,
  990. ctx->user_ctx))
  991. ret = add_indirect_ref(ctx->fs_info, preftrees,
  992. root, &key, 0, ctx->bytenr,
  993. count, sc, GFP_NOFS);
  994. break;
  995. }
  996. case BTRFS_EXTENT_OWNER_REF_KEY:
  997. ASSERT(btrfs_fs_incompat(ctx->fs_info, SIMPLE_QUOTA));
  998. break;
  999. default:
  1000. WARN_ON(1);
  1001. }
  1002. if (ret)
  1003. return ret;
  1004. ptr += btrfs_extent_inline_ref_size(type);
  1005. }
  1006. return 0;
  1007. }
  1008. /*
  1009. * add all non-inline backrefs for bytenr to the list
  1010. *
  1011. * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED.
  1012. */
  1013. static int add_keyed_refs(struct btrfs_backref_walk_ctx *ctx,
  1014. struct btrfs_root *extent_root,
  1015. struct btrfs_path *path,
  1016. int info_level, struct preftrees *preftrees,
  1017. struct share_check *sc)
  1018. {
  1019. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  1020. int ret;
  1021. int slot;
  1022. struct extent_buffer *leaf;
  1023. struct btrfs_key key;
  1024. while (1) {
  1025. ret = btrfs_next_item(extent_root, path);
  1026. if (ret < 0)
  1027. break;
  1028. if (ret) {
  1029. ret = 0;
  1030. break;
  1031. }
  1032. slot = path->slots[0];
  1033. leaf = path->nodes[0];
  1034. btrfs_item_key_to_cpu(leaf, &key, slot);
  1035. if (key.objectid != ctx->bytenr)
  1036. break;
  1037. if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
  1038. continue;
  1039. if (key.type > BTRFS_SHARED_DATA_REF_KEY)
  1040. break;
  1041. switch (key.type) {
  1042. case BTRFS_SHARED_BLOCK_REF_KEY:
  1043. /* SHARED DIRECT METADATA backref */
  1044. ret = add_direct_ref(fs_info, preftrees,
  1045. info_level + 1, key.offset,
  1046. ctx->bytenr, 1, NULL, GFP_NOFS);
  1047. break;
  1048. case BTRFS_SHARED_DATA_REF_KEY: {
  1049. /* SHARED DIRECT FULL backref */
  1050. struct btrfs_shared_data_ref *sdref;
  1051. int count;
  1052. sdref = btrfs_item_ptr(leaf, slot,
  1053. struct btrfs_shared_data_ref);
  1054. count = btrfs_shared_data_ref_count(leaf, sdref);
  1055. ret = add_direct_ref(fs_info, preftrees, 0,
  1056. key.offset, ctx->bytenr, count,
  1057. sc, GFP_NOFS);
  1058. break;
  1059. }
  1060. case BTRFS_TREE_BLOCK_REF_KEY:
  1061. /* NORMAL INDIRECT METADATA backref */
  1062. ret = add_indirect_ref(fs_info, preftrees, key.offset,
  1063. NULL, info_level + 1, ctx->bytenr,
  1064. 1, NULL, GFP_NOFS);
  1065. break;
  1066. case BTRFS_EXTENT_DATA_REF_KEY: {
  1067. /* NORMAL INDIRECT DATA backref */
  1068. struct btrfs_extent_data_ref *dref;
  1069. int count;
  1070. u64 root;
  1071. dref = btrfs_item_ptr(leaf, slot,
  1072. struct btrfs_extent_data_ref);
  1073. count = btrfs_extent_data_ref_count(leaf, dref);
  1074. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  1075. dref);
  1076. key.type = BTRFS_EXTENT_DATA_KEY;
  1077. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  1078. if (sc && key.objectid != sc->inum &&
  1079. !sc->have_delayed_delete_refs) {
  1080. ret = BACKREF_FOUND_SHARED;
  1081. break;
  1082. }
  1083. root = btrfs_extent_data_ref_root(leaf, dref);
  1084. if (!ctx->skip_data_ref ||
  1085. !ctx->skip_data_ref(root, key.objectid, key.offset,
  1086. ctx->user_ctx))
  1087. ret = add_indirect_ref(fs_info, preftrees, root,
  1088. &key, 0, ctx->bytenr,
  1089. count, sc, GFP_NOFS);
  1090. break;
  1091. }
  1092. default:
  1093. WARN_ON(1);
  1094. }
  1095. if (ret)
  1096. return ret;
  1097. }
  1098. return ret;
  1099. }
  1100. /*
  1101. * The caller has joined a transaction or is holding a read lock on the
  1102. * fs_info->commit_root_sem semaphore, so no need to worry about the root's last
  1103. * snapshot field changing while updating or checking the cache.
  1104. */
  1105. static bool lookup_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx,
  1106. struct btrfs_root *root,
  1107. u64 bytenr, int level, bool *is_shared)
  1108. {
  1109. const struct btrfs_fs_info *fs_info = root->fs_info;
  1110. struct btrfs_backref_shared_cache_entry *entry;
  1111. if (!current->journal_info)
  1112. lockdep_assert_held(&fs_info->commit_root_sem);
  1113. if (!ctx->use_path_cache)
  1114. return false;
  1115. if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL))
  1116. return false;
  1117. /*
  1118. * Level -1 is used for the data extent, which is not reliable to cache
  1119. * because its reference count can increase or decrease without us
  1120. * realizing. We cache results only for extent buffers that lead from
  1121. * the root node down to the leaf with the file extent item.
  1122. */
  1123. ASSERT(level >= 0);
  1124. entry = &ctx->path_cache_entries[level];
  1125. /* Unused cache entry or being used for some other extent buffer. */
  1126. if (entry->bytenr != bytenr)
  1127. return false;
  1128. /*
  1129. * We cached a false result, but the last snapshot generation of the
  1130. * root changed, so we now have a snapshot. Don't trust the result.
  1131. */
  1132. if (!entry->is_shared &&
  1133. entry->gen != btrfs_root_last_snapshot(&root->root_item))
  1134. return false;
  1135. /*
  1136. * If we cached a true result and the last generation used for dropping
  1137. * a root changed, we can not trust the result, because the dropped root
  1138. * could be a snapshot sharing this extent buffer.
  1139. */
  1140. if (entry->is_shared &&
  1141. entry->gen != btrfs_get_last_root_drop_gen(fs_info))
  1142. return false;
  1143. *is_shared = entry->is_shared;
  1144. /*
  1145. * If the node at this level is shared, than all nodes below are also
  1146. * shared. Currently some of the nodes below may be marked as not shared
  1147. * because we have just switched from one leaf to another, and switched
  1148. * also other nodes above the leaf and below the current level, so mark
  1149. * them as shared.
  1150. */
  1151. if (*is_shared) {
  1152. for (int i = 0; i < level; i++) {
  1153. ctx->path_cache_entries[i].is_shared = true;
  1154. ctx->path_cache_entries[i].gen = entry->gen;
  1155. }
  1156. }
  1157. return true;
  1158. }
  1159. /*
  1160. * The caller has joined a transaction or is holding a read lock on the
  1161. * fs_info->commit_root_sem semaphore, so no need to worry about the root's last
  1162. * snapshot field changing while updating or checking the cache.
  1163. */
  1164. static void store_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx,
  1165. struct btrfs_root *root,
  1166. u64 bytenr, int level, bool is_shared)
  1167. {
  1168. const struct btrfs_fs_info *fs_info = root->fs_info;
  1169. struct btrfs_backref_shared_cache_entry *entry;
  1170. u64 gen;
  1171. if (!current->journal_info)
  1172. lockdep_assert_held(&fs_info->commit_root_sem);
  1173. if (!ctx->use_path_cache)
  1174. return;
  1175. if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL))
  1176. return;
  1177. /*
  1178. * Level -1 is used for the data extent, which is not reliable to cache
  1179. * because its reference count can increase or decrease without us
  1180. * realizing. We cache results only for extent buffers that lead from
  1181. * the root node down to the leaf with the file extent item.
  1182. */
  1183. ASSERT(level >= 0);
  1184. if (is_shared)
  1185. gen = btrfs_get_last_root_drop_gen(fs_info);
  1186. else
  1187. gen = btrfs_root_last_snapshot(&root->root_item);
  1188. entry = &ctx->path_cache_entries[level];
  1189. entry->bytenr = bytenr;
  1190. entry->is_shared = is_shared;
  1191. entry->gen = gen;
  1192. /*
  1193. * If we found an extent buffer is shared, set the cache result for all
  1194. * extent buffers below it to true. As nodes in the path are COWed,
  1195. * their sharedness is moved to their children, and if a leaf is COWed,
  1196. * then the sharedness of a data extent becomes direct, the refcount of
  1197. * data extent is increased in the extent item at the extent tree.
  1198. */
  1199. if (is_shared) {
  1200. for (int i = 0; i < level; i++) {
  1201. entry = &ctx->path_cache_entries[i];
  1202. entry->is_shared = is_shared;
  1203. entry->gen = gen;
  1204. }
  1205. }
  1206. }
  1207. /*
  1208. * this adds all existing backrefs (inline backrefs, backrefs and delayed
  1209. * refs) for the given bytenr to the refs list, merges duplicates and resolves
  1210. * indirect refs to their parent bytenr.
  1211. * When roots are found, they're added to the roots list
  1212. *
  1213. * @ctx: Backref walking context object, must be not NULL.
  1214. * @sc: If !NULL, then immediately return BACKREF_FOUND_SHARED when a
  1215. * shared extent is detected.
  1216. *
  1217. * Otherwise this returns 0 for success and <0 for an error.
  1218. *
  1219. * FIXME some caching might speed things up
  1220. */
  1221. static int find_parent_nodes(struct btrfs_backref_walk_ctx *ctx,
  1222. struct share_check *sc)
  1223. {
  1224. struct btrfs_root *root = btrfs_extent_root(ctx->fs_info, ctx->bytenr);
  1225. struct btrfs_key key;
  1226. struct btrfs_path *path;
  1227. struct btrfs_delayed_ref_root *delayed_refs = NULL;
  1228. struct btrfs_delayed_ref_head *head;
  1229. int info_level = 0;
  1230. int ret;
  1231. struct prelim_ref *ref;
  1232. struct rb_node *node;
  1233. struct extent_inode_elem *eie = NULL;
  1234. struct preftrees preftrees = {
  1235. .direct = PREFTREE_INIT,
  1236. .indirect = PREFTREE_INIT,
  1237. .indirect_missing_keys = PREFTREE_INIT
  1238. };
  1239. /* Roots ulist is not needed when using a sharedness check context. */
  1240. if (sc)
  1241. ASSERT(ctx->roots == NULL);
  1242. key.objectid = ctx->bytenr;
  1243. key.offset = (u64)-1;
  1244. if (btrfs_fs_incompat(ctx->fs_info, SKINNY_METADATA))
  1245. key.type = BTRFS_METADATA_ITEM_KEY;
  1246. else
  1247. key.type = BTRFS_EXTENT_ITEM_KEY;
  1248. path = btrfs_alloc_path();
  1249. if (!path)
  1250. return -ENOMEM;
  1251. if (!ctx->trans) {
  1252. path->search_commit_root = 1;
  1253. path->skip_locking = 1;
  1254. }
  1255. if (ctx->time_seq == BTRFS_SEQ_LAST)
  1256. path->skip_locking = 1;
  1257. again:
  1258. head = NULL;
  1259. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1260. if (ret < 0)
  1261. goto out;
  1262. if (ret == 0) {
  1263. /*
  1264. * Key with offset -1 found, there would have to exist an extent
  1265. * item with such offset, but this is out of the valid range.
  1266. */
  1267. ret = -EUCLEAN;
  1268. goto out;
  1269. }
  1270. if (ctx->trans && likely(ctx->trans->type != __TRANS_DUMMY) &&
  1271. ctx->time_seq != BTRFS_SEQ_LAST) {
  1272. /*
  1273. * We have a specific time_seq we care about and trans which
  1274. * means we have the path lock, we need to grab the ref head and
  1275. * lock it so we have a consistent view of the refs at the given
  1276. * time.
  1277. */
  1278. delayed_refs = &ctx->trans->transaction->delayed_refs;
  1279. spin_lock(&delayed_refs->lock);
  1280. head = btrfs_find_delayed_ref_head(delayed_refs, ctx->bytenr);
  1281. if (head) {
  1282. if (!mutex_trylock(&head->mutex)) {
  1283. refcount_inc(&head->refs);
  1284. spin_unlock(&delayed_refs->lock);
  1285. btrfs_release_path(path);
  1286. /*
  1287. * Mutex was contended, block until it's
  1288. * released and try again
  1289. */
  1290. mutex_lock(&head->mutex);
  1291. mutex_unlock(&head->mutex);
  1292. btrfs_put_delayed_ref_head(head);
  1293. goto again;
  1294. }
  1295. spin_unlock(&delayed_refs->lock);
  1296. ret = add_delayed_refs(ctx->fs_info, head, ctx->time_seq,
  1297. &preftrees, sc);
  1298. mutex_unlock(&head->mutex);
  1299. if (ret)
  1300. goto out;
  1301. } else {
  1302. spin_unlock(&delayed_refs->lock);
  1303. }
  1304. }
  1305. if (path->slots[0]) {
  1306. struct extent_buffer *leaf;
  1307. int slot;
  1308. path->slots[0]--;
  1309. leaf = path->nodes[0];
  1310. slot = path->slots[0];
  1311. btrfs_item_key_to_cpu(leaf, &key, slot);
  1312. if (key.objectid == ctx->bytenr &&
  1313. (key.type == BTRFS_EXTENT_ITEM_KEY ||
  1314. key.type == BTRFS_METADATA_ITEM_KEY)) {
  1315. ret = add_inline_refs(ctx, path, &info_level,
  1316. &preftrees, sc);
  1317. if (ret)
  1318. goto out;
  1319. ret = add_keyed_refs(ctx, root, path, info_level,
  1320. &preftrees, sc);
  1321. if (ret)
  1322. goto out;
  1323. }
  1324. }
  1325. /*
  1326. * If we have a share context and we reached here, it means the extent
  1327. * is not directly shared (no multiple reference items for it),
  1328. * otherwise we would have exited earlier with a return value of
  1329. * BACKREF_FOUND_SHARED after processing delayed references or while
  1330. * processing inline or keyed references from the extent tree.
  1331. * The extent may however be indirectly shared through shared subtrees
  1332. * as a result from creating snapshots, so we determine below what is
  1333. * its parent node, in case we are dealing with a metadata extent, or
  1334. * what's the leaf (or leaves), from a fs tree, that has a file extent
  1335. * item pointing to it in case we are dealing with a data extent.
  1336. */
  1337. ASSERT(extent_is_shared(sc) == 0);
  1338. /*
  1339. * If we are here for a data extent and we have a share_check structure
  1340. * it means the data extent is not directly shared (does not have
  1341. * multiple reference items), so we have to check if a path in the fs
  1342. * tree (going from the root node down to the leaf that has the file
  1343. * extent item pointing to the data extent) is shared, that is, if any
  1344. * of the extent buffers in the path is referenced by other trees.
  1345. */
  1346. if (sc && ctx->bytenr == sc->data_bytenr) {
  1347. /*
  1348. * If our data extent is from a generation more recent than the
  1349. * last generation used to snapshot the root, then we know that
  1350. * it can not be shared through subtrees, so we can skip
  1351. * resolving indirect references, there's no point in
  1352. * determining the extent buffers for the path from the fs tree
  1353. * root node down to the leaf that has the file extent item that
  1354. * points to the data extent.
  1355. */
  1356. if (sc->data_extent_gen >
  1357. btrfs_root_last_snapshot(&sc->root->root_item)) {
  1358. ret = BACKREF_FOUND_NOT_SHARED;
  1359. goto out;
  1360. }
  1361. /*
  1362. * If we are only determining if a data extent is shared or not
  1363. * and the corresponding file extent item is located in the same
  1364. * leaf as the previous file extent item, we can skip resolving
  1365. * indirect references for a data extent, since the fs tree path
  1366. * is the same (same leaf, so same path). We skip as long as the
  1367. * cached result for the leaf is valid and only if there's only
  1368. * one file extent item pointing to the data extent, because in
  1369. * the case of multiple file extent items, they may be located
  1370. * in different leaves and therefore we have multiple paths.
  1371. */
  1372. if (sc->ctx->curr_leaf_bytenr == sc->ctx->prev_leaf_bytenr &&
  1373. sc->self_ref_count == 1) {
  1374. bool cached;
  1375. bool is_shared;
  1376. cached = lookup_backref_shared_cache(sc->ctx, sc->root,
  1377. sc->ctx->curr_leaf_bytenr,
  1378. 0, &is_shared);
  1379. if (cached) {
  1380. if (is_shared)
  1381. ret = BACKREF_FOUND_SHARED;
  1382. else
  1383. ret = BACKREF_FOUND_NOT_SHARED;
  1384. goto out;
  1385. }
  1386. }
  1387. }
  1388. btrfs_release_path(path);
  1389. ret = add_missing_keys(ctx->fs_info, &preftrees, path->skip_locking == 0);
  1390. if (ret)
  1391. goto out;
  1392. WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root.rb_root));
  1393. ret = resolve_indirect_refs(ctx, path, &preftrees, sc);
  1394. if (ret)
  1395. goto out;
  1396. WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root.rb_root));
  1397. /*
  1398. * This walks the tree of merged and resolved refs. Tree blocks are
  1399. * read in as needed. Unique entries are added to the ulist, and
  1400. * the list of found roots is updated.
  1401. *
  1402. * We release the entire tree in one go before returning.
  1403. */
  1404. node = rb_first_cached(&preftrees.direct.root);
  1405. while (node) {
  1406. ref = rb_entry(node, struct prelim_ref, rbnode);
  1407. node = rb_next(&ref->rbnode);
  1408. /*
  1409. * ref->count < 0 can happen here if there are delayed
  1410. * refs with a node->action of BTRFS_DROP_DELAYED_REF.
  1411. * prelim_ref_insert() relies on this when merging
  1412. * identical refs to keep the overall count correct.
  1413. * prelim_ref_insert() will merge only those refs
  1414. * which compare identically. Any refs having
  1415. * e.g. different offsets would not be merged,
  1416. * and would retain their original ref->count < 0.
  1417. */
  1418. if (ctx->roots && ref->count && ref->root_id && ref->parent == 0) {
  1419. /* no parent == root of tree */
  1420. ret = ulist_add(ctx->roots, ref->root_id, 0, GFP_NOFS);
  1421. if (ret < 0)
  1422. goto out;
  1423. }
  1424. if (ref->count && ref->parent) {
  1425. if (!ctx->skip_inode_ref_list && !ref->inode_list &&
  1426. ref->level == 0) {
  1427. struct btrfs_tree_parent_check check = { 0 };
  1428. struct extent_buffer *eb;
  1429. check.level = ref->level;
  1430. eb = read_tree_block(ctx->fs_info, ref->parent,
  1431. &check);
  1432. if (IS_ERR(eb)) {
  1433. ret = PTR_ERR(eb);
  1434. goto out;
  1435. }
  1436. if (!extent_buffer_uptodate(eb)) {
  1437. free_extent_buffer(eb);
  1438. ret = -EIO;
  1439. goto out;
  1440. }
  1441. if (!path->skip_locking)
  1442. btrfs_tree_read_lock(eb);
  1443. ret = find_extent_in_eb(ctx, eb, &eie);
  1444. if (!path->skip_locking)
  1445. btrfs_tree_read_unlock(eb);
  1446. free_extent_buffer(eb);
  1447. if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP ||
  1448. ret < 0)
  1449. goto out;
  1450. ref->inode_list = eie;
  1451. /*
  1452. * We transferred the list ownership to the ref,
  1453. * so set to NULL to avoid a double free in case
  1454. * an error happens after this.
  1455. */
  1456. eie = NULL;
  1457. }
  1458. ret = ulist_add_merge_ptr(ctx->refs, ref->parent,
  1459. ref->inode_list,
  1460. (void **)&eie, GFP_NOFS);
  1461. if (ret < 0)
  1462. goto out;
  1463. if (!ret && !ctx->skip_inode_ref_list) {
  1464. /*
  1465. * We've recorded that parent, so we must extend
  1466. * its inode list here.
  1467. *
  1468. * However if there was corruption we may not
  1469. * have found an eie, return an error in this
  1470. * case.
  1471. */
  1472. ASSERT(eie);
  1473. if (!eie) {
  1474. ret = -EUCLEAN;
  1475. goto out;
  1476. }
  1477. while (eie->next)
  1478. eie = eie->next;
  1479. eie->next = ref->inode_list;
  1480. }
  1481. eie = NULL;
  1482. /*
  1483. * We have transferred the inode list ownership from
  1484. * this ref to the ref we added to the 'refs' ulist.
  1485. * So set this ref's inode list to NULL to avoid
  1486. * use-after-free when our caller uses it or double
  1487. * frees in case an error happens before we return.
  1488. */
  1489. ref->inode_list = NULL;
  1490. }
  1491. cond_resched();
  1492. }
  1493. out:
  1494. btrfs_free_path(path);
  1495. prelim_release(&preftrees.direct);
  1496. prelim_release(&preftrees.indirect);
  1497. prelim_release(&preftrees.indirect_missing_keys);
  1498. if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || ret < 0)
  1499. free_inode_elem_list(eie);
  1500. return ret;
  1501. }
  1502. /*
  1503. * Finds all leaves with a reference to the specified combination of
  1504. * @ctx->bytenr and @ctx->extent_item_pos. The bytenr of the found leaves are
  1505. * added to the ulist at @ctx->refs, and that ulist is allocated by this
  1506. * function. The caller should free the ulist with free_leaf_list() if
  1507. * @ctx->ignore_extent_item_pos is false, otherwise a fimple ulist_free() is
  1508. * enough.
  1509. *
  1510. * Returns 0 on success and < 0 on error. On error @ctx->refs is not allocated.
  1511. */
  1512. int btrfs_find_all_leafs(struct btrfs_backref_walk_ctx *ctx)
  1513. {
  1514. int ret;
  1515. ASSERT(ctx->refs == NULL);
  1516. ctx->refs = ulist_alloc(GFP_NOFS);
  1517. if (!ctx->refs)
  1518. return -ENOMEM;
  1519. ret = find_parent_nodes(ctx, NULL);
  1520. if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP ||
  1521. (ret < 0 && ret != -ENOENT)) {
  1522. free_leaf_list(ctx->refs);
  1523. ctx->refs = NULL;
  1524. return ret;
  1525. }
  1526. return 0;
  1527. }
  1528. /*
  1529. * Walk all backrefs for a given extent to find all roots that reference this
  1530. * extent. Walking a backref means finding all extents that reference this
  1531. * extent and in turn walk the backrefs of those, too. Naturally this is a
  1532. * recursive process, but here it is implemented in an iterative fashion: We
  1533. * find all referencing extents for the extent in question and put them on a
  1534. * list. In turn, we find all referencing extents for those, further appending
  1535. * to the list. The way we iterate the list allows adding more elements after
  1536. * the current while iterating. The process stops when we reach the end of the
  1537. * list.
  1538. *
  1539. * Found roots are added to @ctx->roots, which is allocated by this function if
  1540. * it points to NULL, in which case the caller is responsible for freeing it
  1541. * after it's not needed anymore.
  1542. * This function requires @ctx->refs to be NULL, as it uses it for allocating a
  1543. * ulist to do temporary work, and frees it before returning.
  1544. *
  1545. * Returns 0 on success, < 0 on error.
  1546. */
  1547. static int btrfs_find_all_roots_safe(struct btrfs_backref_walk_ctx *ctx)
  1548. {
  1549. const u64 orig_bytenr = ctx->bytenr;
  1550. const bool orig_skip_inode_ref_list = ctx->skip_inode_ref_list;
  1551. bool roots_ulist_allocated = false;
  1552. struct ulist_iterator uiter;
  1553. int ret = 0;
  1554. ASSERT(ctx->refs == NULL);
  1555. ctx->refs = ulist_alloc(GFP_NOFS);
  1556. if (!ctx->refs)
  1557. return -ENOMEM;
  1558. if (!ctx->roots) {
  1559. ctx->roots = ulist_alloc(GFP_NOFS);
  1560. if (!ctx->roots) {
  1561. ulist_free(ctx->refs);
  1562. ctx->refs = NULL;
  1563. return -ENOMEM;
  1564. }
  1565. roots_ulist_allocated = true;
  1566. }
  1567. ctx->skip_inode_ref_list = true;
  1568. ULIST_ITER_INIT(&uiter);
  1569. while (1) {
  1570. struct ulist_node *node;
  1571. ret = find_parent_nodes(ctx, NULL);
  1572. if (ret < 0 && ret != -ENOENT) {
  1573. if (roots_ulist_allocated) {
  1574. ulist_free(ctx->roots);
  1575. ctx->roots = NULL;
  1576. }
  1577. break;
  1578. }
  1579. ret = 0;
  1580. node = ulist_next(ctx->refs, &uiter);
  1581. if (!node)
  1582. break;
  1583. ctx->bytenr = node->val;
  1584. cond_resched();
  1585. }
  1586. ulist_free(ctx->refs);
  1587. ctx->refs = NULL;
  1588. ctx->bytenr = orig_bytenr;
  1589. ctx->skip_inode_ref_list = orig_skip_inode_ref_list;
  1590. return ret;
  1591. }
  1592. int btrfs_find_all_roots(struct btrfs_backref_walk_ctx *ctx,
  1593. bool skip_commit_root_sem)
  1594. {
  1595. int ret;
  1596. if (!ctx->trans && !skip_commit_root_sem)
  1597. down_read(&ctx->fs_info->commit_root_sem);
  1598. ret = btrfs_find_all_roots_safe(ctx);
  1599. if (!ctx->trans && !skip_commit_root_sem)
  1600. up_read(&ctx->fs_info->commit_root_sem);
  1601. return ret;
  1602. }
  1603. struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void)
  1604. {
  1605. struct btrfs_backref_share_check_ctx *ctx;
  1606. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1607. if (!ctx)
  1608. return NULL;
  1609. ulist_init(&ctx->refs);
  1610. return ctx;
  1611. }
  1612. void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx)
  1613. {
  1614. if (!ctx)
  1615. return;
  1616. ulist_release(&ctx->refs);
  1617. kfree(ctx);
  1618. }
  1619. /*
  1620. * Check if a data extent is shared or not.
  1621. *
  1622. * @inode: The inode whose extent we are checking.
  1623. * @bytenr: Logical bytenr of the extent we are checking.
  1624. * @extent_gen: Generation of the extent (file extent item) or 0 if it is
  1625. * not known.
  1626. * @ctx: A backref sharedness check context.
  1627. *
  1628. * btrfs_is_data_extent_shared uses the backref walking code but will short
  1629. * circuit as soon as it finds a root or inode that doesn't match the
  1630. * one passed in. This provides a significant performance benefit for
  1631. * callers (such as fiemap) which want to know whether the extent is
  1632. * shared but do not need a ref count.
  1633. *
  1634. * This attempts to attach to the running transaction in order to account for
  1635. * delayed refs, but continues on even when no running transaction exists.
  1636. *
  1637. * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
  1638. */
  1639. int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr,
  1640. u64 extent_gen,
  1641. struct btrfs_backref_share_check_ctx *ctx)
  1642. {
  1643. struct btrfs_backref_walk_ctx walk_ctx = { 0 };
  1644. struct btrfs_root *root = inode->root;
  1645. struct btrfs_fs_info *fs_info = root->fs_info;
  1646. struct btrfs_trans_handle *trans;
  1647. struct ulist_iterator uiter;
  1648. struct ulist_node *node;
  1649. struct btrfs_seq_list elem = BTRFS_SEQ_LIST_INIT(elem);
  1650. int ret = 0;
  1651. struct share_check shared = {
  1652. .ctx = ctx,
  1653. .root = root,
  1654. .inum = btrfs_ino(inode),
  1655. .data_bytenr = bytenr,
  1656. .data_extent_gen = extent_gen,
  1657. .share_count = 0,
  1658. .self_ref_count = 0,
  1659. .have_delayed_delete_refs = false,
  1660. };
  1661. int level;
  1662. bool leaf_cached;
  1663. bool leaf_is_shared;
  1664. for (int i = 0; i < BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE; i++) {
  1665. if (ctx->prev_extents_cache[i].bytenr == bytenr)
  1666. return ctx->prev_extents_cache[i].is_shared;
  1667. }
  1668. ulist_init(&ctx->refs);
  1669. trans = btrfs_join_transaction_nostart(root);
  1670. if (IS_ERR(trans)) {
  1671. if (PTR_ERR(trans) != -ENOENT && PTR_ERR(trans) != -EROFS) {
  1672. ret = PTR_ERR(trans);
  1673. goto out;
  1674. }
  1675. trans = NULL;
  1676. down_read(&fs_info->commit_root_sem);
  1677. } else {
  1678. btrfs_get_tree_mod_seq(fs_info, &elem);
  1679. walk_ctx.time_seq = elem.seq;
  1680. }
  1681. ctx->use_path_cache = true;
  1682. /*
  1683. * We may have previously determined that the current leaf is shared.
  1684. * If it is, then we have a data extent that is shared due to a shared
  1685. * subtree (caused by snapshotting) and we don't need to check for data
  1686. * backrefs. If the leaf is not shared, then we must do backref walking
  1687. * to determine if the data extent is shared through reflinks.
  1688. */
  1689. leaf_cached = lookup_backref_shared_cache(ctx, root,
  1690. ctx->curr_leaf_bytenr, 0,
  1691. &leaf_is_shared);
  1692. if (leaf_cached && leaf_is_shared) {
  1693. ret = 1;
  1694. goto out_trans;
  1695. }
  1696. walk_ctx.skip_inode_ref_list = true;
  1697. walk_ctx.trans = trans;
  1698. walk_ctx.fs_info = fs_info;
  1699. walk_ctx.refs = &ctx->refs;
  1700. /* -1 means we are in the bytenr of the data extent. */
  1701. level = -1;
  1702. ULIST_ITER_INIT(&uiter);
  1703. while (1) {
  1704. const unsigned long prev_ref_count = ctx->refs.nnodes;
  1705. walk_ctx.bytenr = bytenr;
  1706. ret = find_parent_nodes(&walk_ctx, &shared);
  1707. if (ret == BACKREF_FOUND_SHARED ||
  1708. ret == BACKREF_FOUND_NOT_SHARED) {
  1709. /* If shared must return 1, otherwise return 0. */
  1710. ret = (ret == BACKREF_FOUND_SHARED) ? 1 : 0;
  1711. if (level >= 0)
  1712. store_backref_shared_cache(ctx, root, bytenr,
  1713. level, ret == 1);
  1714. break;
  1715. }
  1716. if (ret < 0 && ret != -ENOENT)
  1717. break;
  1718. ret = 0;
  1719. /*
  1720. * More than one extent buffer (bytenr) may have been added to
  1721. * the ctx->refs ulist, in which case we have to check multiple
  1722. * tree paths in case the first one is not shared, so we can not
  1723. * use the path cache which is made for a single path. Multiple
  1724. * extent buffers at the current level happen when:
  1725. *
  1726. * 1) level -1, the data extent: If our data extent was not
  1727. * directly shared (without multiple reference items), then
  1728. * it might have a single reference item with a count > 1 for
  1729. * the same offset, which means there are 2 (or more) file
  1730. * extent items that point to the data extent - this happens
  1731. * when a file extent item needs to be split and then one
  1732. * item gets moved to another leaf due to a b+tree leaf split
  1733. * when inserting some item. In this case the file extent
  1734. * items may be located in different leaves and therefore
  1735. * some of the leaves may be referenced through shared
  1736. * subtrees while others are not. Since our extent buffer
  1737. * cache only works for a single path (by far the most common
  1738. * case and simpler to deal with), we can not use it if we
  1739. * have multiple leaves (which implies multiple paths).
  1740. *
  1741. * 2) level >= 0, a tree node/leaf: We can have a mix of direct
  1742. * and indirect references on a b+tree node/leaf, so we have
  1743. * to check multiple paths, and the extent buffer (the
  1744. * current bytenr) may be shared or not. One example is
  1745. * during relocation as we may get a shared tree block ref
  1746. * (direct ref) and a non-shared tree block ref (indirect
  1747. * ref) for the same node/leaf.
  1748. */
  1749. if ((ctx->refs.nnodes - prev_ref_count) > 1)
  1750. ctx->use_path_cache = false;
  1751. if (level >= 0)
  1752. store_backref_shared_cache(ctx, root, bytenr,
  1753. level, false);
  1754. node = ulist_next(&ctx->refs, &uiter);
  1755. if (!node)
  1756. break;
  1757. bytenr = node->val;
  1758. if (ctx->use_path_cache) {
  1759. bool is_shared;
  1760. bool cached;
  1761. level++;
  1762. cached = lookup_backref_shared_cache(ctx, root, bytenr,
  1763. level, &is_shared);
  1764. if (cached) {
  1765. ret = (is_shared ? 1 : 0);
  1766. break;
  1767. }
  1768. }
  1769. shared.share_count = 0;
  1770. shared.have_delayed_delete_refs = false;
  1771. cond_resched();
  1772. }
  1773. /*
  1774. * If the path cache is disabled, then it means at some tree level we
  1775. * got multiple parents due to a mix of direct and indirect backrefs or
  1776. * multiple leaves with file extent items pointing to the same data
  1777. * extent. We have to invalidate the cache and cache only the sharedness
  1778. * result for the levels where we got only one node/reference.
  1779. */
  1780. if (!ctx->use_path_cache) {
  1781. int i = 0;
  1782. level--;
  1783. if (ret >= 0 && level >= 0) {
  1784. bytenr = ctx->path_cache_entries[level].bytenr;
  1785. ctx->use_path_cache = true;
  1786. store_backref_shared_cache(ctx, root, bytenr, level, ret);
  1787. i = level + 1;
  1788. }
  1789. for ( ; i < BTRFS_MAX_LEVEL; i++)
  1790. ctx->path_cache_entries[i].bytenr = 0;
  1791. }
  1792. /*
  1793. * Cache the sharedness result for the data extent if we know our inode
  1794. * has more than 1 file extent item that refers to the data extent.
  1795. */
  1796. if (ret >= 0 && shared.self_ref_count > 1) {
  1797. int slot = ctx->prev_extents_cache_slot;
  1798. ctx->prev_extents_cache[slot].bytenr = shared.data_bytenr;
  1799. ctx->prev_extents_cache[slot].is_shared = (ret == 1);
  1800. slot = (slot + 1) % BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE;
  1801. ctx->prev_extents_cache_slot = slot;
  1802. }
  1803. out_trans:
  1804. if (trans) {
  1805. btrfs_put_tree_mod_seq(fs_info, &elem);
  1806. btrfs_end_transaction(trans);
  1807. } else {
  1808. up_read(&fs_info->commit_root_sem);
  1809. }
  1810. out:
  1811. ulist_release(&ctx->refs);
  1812. ctx->prev_leaf_bytenr = ctx->curr_leaf_bytenr;
  1813. return ret;
  1814. }
  1815. int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
  1816. u64 start_off, struct btrfs_path *path,
  1817. struct btrfs_inode_extref **ret_extref,
  1818. u64 *found_off)
  1819. {
  1820. int ret, slot;
  1821. struct btrfs_key key;
  1822. struct btrfs_key found_key;
  1823. struct btrfs_inode_extref *extref;
  1824. const struct extent_buffer *leaf;
  1825. unsigned long ptr;
  1826. key.objectid = inode_objectid;
  1827. key.type = BTRFS_INODE_EXTREF_KEY;
  1828. key.offset = start_off;
  1829. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1830. if (ret < 0)
  1831. return ret;
  1832. while (1) {
  1833. leaf = path->nodes[0];
  1834. slot = path->slots[0];
  1835. if (slot >= btrfs_header_nritems(leaf)) {
  1836. /*
  1837. * If the item at offset is not found,
  1838. * btrfs_search_slot will point us to the slot
  1839. * where it should be inserted. In our case
  1840. * that will be the slot directly before the
  1841. * next INODE_REF_KEY_V2 item. In the case
  1842. * that we're pointing to the last slot in a
  1843. * leaf, we must move one leaf over.
  1844. */
  1845. ret = btrfs_next_leaf(root, path);
  1846. if (ret) {
  1847. if (ret >= 1)
  1848. ret = -ENOENT;
  1849. break;
  1850. }
  1851. continue;
  1852. }
  1853. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1854. /*
  1855. * Check that we're still looking at an extended ref key for
  1856. * this particular objectid. If we have different
  1857. * objectid or type then there are no more to be found
  1858. * in the tree and we can exit.
  1859. */
  1860. ret = -ENOENT;
  1861. if (found_key.objectid != inode_objectid)
  1862. break;
  1863. if (found_key.type != BTRFS_INODE_EXTREF_KEY)
  1864. break;
  1865. ret = 0;
  1866. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1867. extref = (struct btrfs_inode_extref *)ptr;
  1868. *ret_extref = extref;
  1869. if (found_off)
  1870. *found_off = found_key.offset;
  1871. break;
  1872. }
  1873. return ret;
  1874. }
  1875. /*
  1876. * this iterates to turn a name (from iref/extref) into a full filesystem path.
  1877. * Elements of the path are separated by '/' and the path is guaranteed to be
  1878. * 0-terminated. the path is only given within the current file system.
  1879. * Therefore, it never starts with a '/'. the caller is responsible to provide
  1880. * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
  1881. * the start point of the resulting string is returned. this pointer is within
  1882. * dest, normally.
  1883. * in case the path buffer would overflow, the pointer is decremented further
  1884. * as if output was written to the buffer, though no more output is actually
  1885. * generated. that way, the caller can determine how much space would be
  1886. * required for the path to fit into the buffer. in that case, the returned
  1887. * value will be smaller than dest. callers must check this!
  1888. */
  1889. char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
  1890. u32 name_len, unsigned long name_off,
  1891. struct extent_buffer *eb_in, u64 parent,
  1892. char *dest, u32 size)
  1893. {
  1894. int slot;
  1895. u64 next_inum;
  1896. int ret;
  1897. s64 bytes_left = ((s64)size) - 1;
  1898. struct extent_buffer *eb = eb_in;
  1899. struct btrfs_key found_key;
  1900. struct btrfs_inode_ref *iref;
  1901. if (bytes_left >= 0)
  1902. dest[bytes_left] = '\0';
  1903. while (1) {
  1904. bytes_left -= name_len;
  1905. if (bytes_left >= 0)
  1906. read_extent_buffer(eb, dest + bytes_left,
  1907. name_off, name_len);
  1908. if (eb != eb_in) {
  1909. if (!path->skip_locking)
  1910. btrfs_tree_read_unlock(eb);
  1911. free_extent_buffer(eb);
  1912. }
  1913. ret = btrfs_find_item(fs_root, path, parent, 0,
  1914. BTRFS_INODE_REF_KEY, &found_key);
  1915. if (ret > 0)
  1916. ret = -ENOENT;
  1917. if (ret)
  1918. break;
  1919. next_inum = found_key.offset;
  1920. /* regular exit ahead */
  1921. if (parent == next_inum)
  1922. break;
  1923. slot = path->slots[0];
  1924. eb = path->nodes[0];
  1925. /* make sure we can use eb after releasing the path */
  1926. if (eb != eb_in) {
  1927. path->nodes[0] = NULL;
  1928. path->locks[0] = 0;
  1929. }
  1930. btrfs_release_path(path);
  1931. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1932. name_len = btrfs_inode_ref_name_len(eb, iref);
  1933. name_off = (unsigned long)(iref + 1);
  1934. parent = next_inum;
  1935. --bytes_left;
  1936. if (bytes_left >= 0)
  1937. dest[bytes_left] = '/';
  1938. }
  1939. btrfs_release_path(path);
  1940. if (ret)
  1941. return ERR_PTR(ret);
  1942. return dest + bytes_left;
  1943. }
  1944. /*
  1945. * this makes the path point to (logical EXTENT_ITEM *)
  1946. * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
  1947. * tree blocks and <0 on error.
  1948. */
  1949. int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
  1950. struct btrfs_path *path, struct btrfs_key *found_key,
  1951. u64 *flags_ret)
  1952. {
  1953. struct btrfs_root *extent_root = btrfs_extent_root(fs_info, logical);
  1954. int ret;
  1955. u64 flags;
  1956. u64 size = 0;
  1957. u32 item_size;
  1958. const struct extent_buffer *eb;
  1959. struct btrfs_extent_item *ei;
  1960. struct btrfs_key key;
  1961. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  1962. key.type = BTRFS_METADATA_ITEM_KEY;
  1963. else
  1964. key.type = BTRFS_EXTENT_ITEM_KEY;
  1965. key.objectid = logical;
  1966. key.offset = (u64)-1;
  1967. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  1968. if (ret < 0)
  1969. return ret;
  1970. if (ret == 0) {
  1971. /*
  1972. * Key with offset -1 found, there would have to exist an extent
  1973. * item with such offset, but this is out of the valid range.
  1974. */
  1975. return -EUCLEAN;
  1976. }
  1977. ret = btrfs_previous_extent_item(extent_root, path, 0);
  1978. if (ret) {
  1979. if (ret > 0)
  1980. ret = -ENOENT;
  1981. return ret;
  1982. }
  1983. btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
  1984. if (found_key->type == BTRFS_METADATA_ITEM_KEY)
  1985. size = fs_info->nodesize;
  1986. else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
  1987. size = found_key->offset;
  1988. if (found_key->objectid > logical ||
  1989. found_key->objectid + size <= logical) {
  1990. btrfs_debug(fs_info,
  1991. "logical %llu is not within any extent", logical);
  1992. return -ENOENT;
  1993. }
  1994. eb = path->nodes[0];
  1995. item_size = btrfs_item_size(eb, path->slots[0]);
  1996. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  1997. flags = btrfs_extent_flags(eb, ei);
  1998. btrfs_debug(fs_info,
  1999. "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
  2000. logical, logical - found_key->objectid, found_key->objectid,
  2001. found_key->offset, flags, item_size);
  2002. WARN_ON(!flags_ret);
  2003. if (flags_ret) {
  2004. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  2005. *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
  2006. else if (flags & BTRFS_EXTENT_FLAG_DATA)
  2007. *flags_ret = BTRFS_EXTENT_FLAG_DATA;
  2008. else
  2009. BUG();
  2010. return 0;
  2011. }
  2012. return -EIO;
  2013. }
  2014. /*
  2015. * helper function to iterate extent inline refs. ptr must point to a 0 value
  2016. * for the first call and may be modified. it is used to track state.
  2017. * if more refs exist, 0 is returned and the next call to
  2018. * get_extent_inline_ref must pass the modified ptr parameter to get the
  2019. * next ref. after the last ref was processed, 1 is returned.
  2020. * returns <0 on error
  2021. */
  2022. static int get_extent_inline_ref(unsigned long *ptr,
  2023. const struct extent_buffer *eb,
  2024. const struct btrfs_key *key,
  2025. const struct btrfs_extent_item *ei,
  2026. u32 item_size,
  2027. struct btrfs_extent_inline_ref **out_eiref,
  2028. int *out_type)
  2029. {
  2030. unsigned long end;
  2031. u64 flags;
  2032. struct btrfs_tree_block_info *info;
  2033. if (!*ptr) {
  2034. /* first call */
  2035. flags = btrfs_extent_flags(eb, ei);
  2036. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  2037. if (key->type == BTRFS_METADATA_ITEM_KEY) {
  2038. /* a skinny metadata extent */
  2039. *out_eiref =
  2040. (struct btrfs_extent_inline_ref *)(ei + 1);
  2041. } else {
  2042. WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
  2043. info = (struct btrfs_tree_block_info *)(ei + 1);
  2044. *out_eiref =
  2045. (struct btrfs_extent_inline_ref *)(info + 1);
  2046. }
  2047. } else {
  2048. *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
  2049. }
  2050. *ptr = (unsigned long)*out_eiref;
  2051. if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
  2052. return -ENOENT;
  2053. }
  2054. end = (unsigned long)ei + item_size;
  2055. *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
  2056. *out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref,
  2057. BTRFS_REF_TYPE_ANY);
  2058. if (*out_type == BTRFS_REF_TYPE_INVALID)
  2059. return -EUCLEAN;
  2060. *ptr += btrfs_extent_inline_ref_size(*out_type);
  2061. WARN_ON(*ptr > end);
  2062. if (*ptr == end)
  2063. return 1; /* last */
  2064. return 0;
  2065. }
  2066. /*
  2067. * reads the tree block backref for an extent. tree level and root are returned
  2068. * through out_level and out_root. ptr must point to a 0 value for the first
  2069. * call and may be modified (see get_extent_inline_ref comment).
  2070. * returns 0 if data was provided, 1 if there was no more data to provide or
  2071. * <0 on error.
  2072. */
  2073. int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
  2074. struct btrfs_key *key, struct btrfs_extent_item *ei,
  2075. u32 item_size, u64 *out_root, u8 *out_level)
  2076. {
  2077. int ret;
  2078. int type;
  2079. struct btrfs_extent_inline_ref *eiref;
  2080. if (*ptr == (unsigned long)-1)
  2081. return 1;
  2082. while (1) {
  2083. ret = get_extent_inline_ref(ptr, eb, key, ei, item_size,
  2084. &eiref, &type);
  2085. if (ret < 0)
  2086. return ret;
  2087. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  2088. type == BTRFS_SHARED_BLOCK_REF_KEY)
  2089. break;
  2090. if (ret == 1)
  2091. return 1;
  2092. }
  2093. /* we can treat both ref types equally here */
  2094. *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
  2095. if (key->type == BTRFS_EXTENT_ITEM_KEY) {
  2096. struct btrfs_tree_block_info *info;
  2097. info = (struct btrfs_tree_block_info *)(ei + 1);
  2098. *out_level = btrfs_tree_block_level(eb, info);
  2099. } else {
  2100. ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
  2101. *out_level = (u8)key->offset;
  2102. }
  2103. if (ret == 1)
  2104. *ptr = (unsigned long)-1;
  2105. return 0;
  2106. }
  2107. static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
  2108. struct extent_inode_elem *inode_list,
  2109. u64 root, u64 extent_item_objectid,
  2110. iterate_extent_inodes_t *iterate, void *ctx)
  2111. {
  2112. struct extent_inode_elem *eie;
  2113. int ret = 0;
  2114. for (eie = inode_list; eie; eie = eie->next) {
  2115. btrfs_debug(fs_info,
  2116. "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu",
  2117. extent_item_objectid, eie->inum,
  2118. eie->offset, root);
  2119. ret = iterate(eie->inum, eie->offset, eie->num_bytes, root, ctx);
  2120. if (ret) {
  2121. btrfs_debug(fs_info,
  2122. "stopping iteration for %llu due to ret=%d",
  2123. extent_item_objectid, ret);
  2124. break;
  2125. }
  2126. }
  2127. return ret;
  2128. }
  2129. /*
  2130. * calls iterate() for every inode that references the extent identified by
  2131. * the given parameters.
  2132. * when the iterator function returns a non-zero value, iteration stops.
  2133. */
  2134. int iterate_extent_inodes(struct btrfs_backref_walk_ctx *ctx,
  2135. bool search_commit_root,
  2136. iterate_extent_inodes_t *iterate, void *user_ctx)
  2137. {
  2138. int ret;
  2139. struct ulist *refs;
  2140. struct ulist_node *ref_node;
  2141. struct btrfs_seq_list seq_elem = BTRFS_SEQ_LIST_INIT(seq_elem);
  2142. struct ulist_iterator ref_uiter;
  2143. btrfs_debug(ctx->fs_info, "resolving all inodes for extent %llu",
  2144. ctx->bytenr);
  2145. ASSERT(ctx->trans == NULL);
  2146. ASSERT(ctx->roots == NULL);
  2147. if (!search_commit_root) {
  2148. struct btrfs_trans_handle *trans;
  2149. trans = btrfs_attach_transaction(ctx->fs_info->tree_root);
  2150. if (IS_ERR(trans)) {
  2151. if (PTR_ERR(trans) != -ENOENT &&
  2152. PTR_ERR(trans) != -EROFS)
  2153. return PTR_ERR(trans);
  2154. trans = NULL;
  2155. }
  2156. ctx->trans = trans;
  2157. }
  2158. if (ctx->trans) {
  2159. btrfs_get_tree_mod_seq(ctx->fs_info, &seq_elem);
  2160. ctx->time_seq = seq_elem.seq;
  2161. } else {
  2162. down_read(&ctx->fs_info->commit_root_sem);
  2163. }
  2164. ret = btrfs_find_all_leafs(ctx);
  2165. if (ret)
  2166. goto out;
  2167. refs = ctx->refs;
  2168. ctx->refs = NULL;
  2169. ULIST_ITER_INIT(&ref_uiter);
  2170. while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
  2171. const u64 leaf_bytenr = ref_node->val;
  2172. struct ulist_node *root_node;
  2173. struct ulist_iterator root_uiter;
  2174. struct extent_inode_elem *inode_list;
  2175. inode_list = (struct extent_inode_elem *)(uintptr_t)ref_node->aux;
  2176. if (ctx->cache_lookup) {
  2177. const u64 *root_ids;
  2178. int root_count;
  2179. bool cached;
  2180. cached = ctx->cache_lookup(leaf_bytenr, ctx->user_ctx,
  2181. &root_ids, &root_count);
  2182. if (cached) {
  2183. for (int i = 0; i < root_count; i++) {
  2184. ret = iterate_leaf_refs(ctx->fs_info,
  2185. inode_list,
  2186. root_ids[i],
  2187. leaf_bytenr,
  2188. iterate,
  2189. user_ctx);
  2190. if (ret)
  2191. break;
  2192. }
  2193. continue;
  2194. }
  2195. }
  2196. if (!ctx->roots) {
  2197. ctx->roots = ulist_alloc(GFP_NOFS);
  2198. if (!ctx->roots) {
  2199. ret = -ENOMEM;
  2200. break;
  2201. }
  2202. }
  2203. ctx->bytenr = leaf_bytenr;
  2204. ret = btrfs_find_all_roots_safe(ctx);
  2205. if (ret)
  2206. break;
  2207. if (ctx->cache_store)
  2208. ctx->cache_store(leaf_bytenr, ctx->roots, ctx->user_ctx);
  2209. ULIST_ITER_INIT(&root_uiter);
  2210. while (!ret && (root_node = ulist_next(ctx->roots, &root_uiter))) {
  2211. btrfs_debug(ctx->fs_info,
  2212. "root %llu references leaf %llu, data list %#llx",
  2213. root_node->val, ref_node->val,
  2214. ref_node->aux);
  2215. ret = iterate_leaf_refs(ctx->fs_info, inode_list,
  2216. root_node->val, ctx->bytenr,
  2217. iterate, user_ctx);
  2218. }
  2219. ulist_reinit(ctx->roots);
  2220. }
  2221. free_leaf_list(refs);
  2222. out:
  2223. if (ctx->trans) {
  2224. btrfs_put_tree_mod_seq(ctx->fs_info, &seq_elem);
  2225. btrfs_end_transaction(ctx->trans);
  2226. ctx->trans = NULL;
  2227. } else {
  2228. up_read(&ctx->fs_info->commit_root_sem);
  2229. }
  2230. ulist_free(ctx->roots);
  2231. ctx->roots = NULL;
  2232. if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP)
  2233. ret = 0;
  2234. return ret;
  2235. }
  2236. static int build_ino_list(u64 inum, u64 offset, u64 num_bytes, u64 root, void *ctx)
  2237. {
  2238. struct btrfs_data_container *inodes = ctx;
  2239. const size_t c = 3 * sizeof(u64);
  2240. if (inodes->bytes_left >= c) {
  2241. inodes->bytes_left -= c;
  2242. inodes->val[inodes->elem_cnt] = inum;
  2243. inodes->val[inodes->elem_cnt + 1] = offset;
  2244. inodes->val[inodes->elem_cnt + 2] = root;
  2245. inodes->elem_cnt += 3;
  2246. } else {
  2247. inodes->bytes_missing += c - inodes->bytes_left;
  2248. inodes->bytes_left = 0;
  2249. inodes->elem_missed += 3;
  2250. }
  2251. return 0;
  2252. }
  2253. int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
  2254. struct btrfs_path *path,
  2255. void *ctx, bool ignore_offset)
  2256. {
  2257. struct btrfs_backref_walk_ctx walk_ctx = { 0 };
  2258. int ret;
  2259. u64 flags = 0;
  2260. struct btrfs_key found_key;
  2261. int search_commit_root = path->search_commit_root;
  2262. ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
  2263. btrfs_release_path(path);
  2264. if (ret < 0)
  2265. return ret;
  2266. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  2267. return -EINVAL;
  2268. walk_ctx.bytenr = found_key.objectid;
  2269. if (ignore_offset)
  2270. walk_ctx.ignore_extent_item_pos = true;
  2271. else
  2272. walk_ctx.extent_item_pos = logical - found_key.objectid;
  2273. walk_ctx.fs_info = fs_info;
  2274. return iterate_extent_inodes(&walk_ctx, search_commit_root,
  2275. build_ino_list, ctx);
  2276. }
  2277. static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
  2278. struct extent_buffer *eb, struct inode_fs_paths *ipath);
  2279. static int iterate_inode_refs(u64 inum, struct inode_fs_paths *ipath)
  2280. {
  2281. int ret = 0;
  2282. int slot;
  2283. u32 cur;
  2284. u32 len;
  2285. u32 name_len;
  2286. u64 parent = 0;
  2287. int found = 0;
  2288. struct btrfs_root *fs_root = ipath->fs_root;
  2289. struct btrfs_path *path = ipath->btrfs_path;
  2290. struct extent_buffer *eb;
  2291. struct btrfs_inode_ref *iref;
  2292. struct btrfs_key found_key;
  2293. while (!ret) {
  2294. ret = btrfs_find_item(fs_root, path, inum,
  2295. parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
  2296. &found_key);
  2297. if (ret < 0)
  2298. break;
  2299. if (ret) {
  2300. ret = found ? 0 : -ENOENT;
  2301. break;
  2302. }
  2303. ++found;
  2304. parent = found_key.offset;
  2305. slot = path->slots[0];
  2306. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  2307. if (!eb) {
  2308. ret = -ENOMEM;
  2309. break;
  2310. }
  2311. btrfs_release_path(path);
  2312. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  2313. for (cur = 0; cur < btrfs_item_size(eb, slot); cur += len) {
  2314. name_len = btrfs_inode_ref_name_len(eb, iref);
  2315. /* path must be released before calling iterate()! */
  2316. btrfs_debug(fs_root->fs_info,
  2317. "following ref at offset %u for inode %llu in tree %llu",
  2318. cur, found_key.objectid,
  2319. btrfs_root_id(fs_root));
  2320. ret = inode_to_path(parent, name_len,
  2321. (unsigned long)(iref + 1), eb, ipath);
  2322. if (ret)
  2323. break;
  2324. len = sizeof(*iref) + name_len;
  2325. iref = (struct btrfs_inode_ref *)((char *)iref + len);
  2326. }
  2327. free_extent_buffer(eb);
  2328. }
  2329. btrfs_release_path(path);
  2330. return ret;
  2331. }
  2332. static int iterate_inode_extrefs(u64 inum, struct inode_fs_paths *ipath)
  2333. {
  2334. int ret;
  2335. int slot;
  2336. u64 offset = 0;
  2337. u64 parent;
  2338. int found = 0;
  2339. struct btrfs_root *fs_root = ipath->fs_root;
  2340. struct btrfs_path *path = ipath->btrfs_path;
  2341. struct extent_buffer *eb;
  2342. struct btrfs_inode_extref *extref;
  2343. u32 item_size;
  2344. u32 cur_offset;
  2345. unsigned long ptr;
  2346. while (1) {
  2347. ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
  2348. &offset);
  2349. if (ret < 0)
  2350. break;
  2351. if (ret) {
  2352. ret = found ? 0 : -ENOENT;
  2353. break;
  2354. }
  2355. ++found;
  2356. slot = path->slots[0];
  2357. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  2358. if (!eb) {
  2359. ret = -ENOMEM;
  2360. break;
  2361. }
  2362. btrfs_release_path(path);
  2363. item_size = btrfs_item_size(eb, slot);
  2364. ptr = btrfs_item_ptr_offset(eb, slot);
  2365. cur_offset = 0;
  2366. while (cur_offset < item_size) {
  2367. u32 name_len;
  2368. extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
  2369. parent = btrfs_inode_extref_parent(eb, extref);
  2370. name_len = btrfs_inode_extref_name_len(eb, extref);
  2371. ret = inode_to_path(parent, name_len,
  2372. (unsigned long)&extref->name, eb, ipath);
  2373. if (ret)
  2374. break;
  2375. cur_offset += btrfs_inode_extref_name_len(eb, extref);
  2376. cur_offset += sizeof(*extref);
  2377. }
  2378. free_extent_buffer(eb);
  2379. offset++;
  2380. }
  2381. btrfs_release_path(path);
  2382. return ret;
  2383. }
  2384. /*
  2385. * returns 0 if the path could be dumped (probably truncated)
  2386. * returns <0 in case of an error
  2387. */
  2388. static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
  2389. struct extent_buffer *eb, struct inode_fs_paths *ipath)
  2390. {
  2391. char *fspath;
  2392. char *fspath_min;
  2393. int i = ipath->fspath->elem_cnt;
  2394. const int s_ptr = sizeof(char *);
  2395. u32 bytes_left;
  2396. bytes_left = ipath->fspath->bytes_left > s_ptr ?
  2397. ipath->fspath->bytes_left - s_ptr : 0;
  2398. fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
  2399. fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
  2400. name_off, eb, inum, fspath_min, bytes_left);
  2401. if (IS_ERR(fspath))
  2402. return PTR_ERR(fspath);
  2403. if (fspath > fspath_min) {
  2404. ipath->fspath->val[i] = (u64)(unsigned long)fspath;
  2405. ++ipath->fspath->elem_cnt;
  2406. ipath->fspath->bytes_left = fspath - fspath_min;
  2407. } else {
  2408. ++ipath->fspath->elem_missed;
  2409. ipath->fspath->bytes_missing += fspath_min - fspath;
  2410. ipath->fspath->bytes_left = 0;
  2411. }
  2412. return 0;
  2413. }
  2414. /*
  2415. * this dumps all file system paths to the inode into the ipath struct, provided
  2416. * is has been created large enough. each path is zero-terminated and accessed
  2417. * from ipath->fspath->val[i].
  2418. * when it returns, there are ipath->fspath->elem_cnt number of paths available
  2419. * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
  2420. * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
  2421. * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
  2422. * have been needed to return all paths.
  2423. */
  2424. int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
  2425. {
  2426. int ret;
  2427. int found_refs = 0;
  2428. ret = iterate_inode_refs(inum, ipath);
  2429. if (!ret)
  2430. ++found_refs;
  2431. else if (ret != -ENOENT)
  2432. return ret;
  2433. ret = iterate_inode_extrefs(inum, ipath);
  2434. if (ret == -ENOENT && found_refs)
  2435. return 0;
  2436. return ret;
  2437. }
  2438. struct btrfs_data_container *init_data_container(u32 total_bytes)
  2439. {
  2440. struct btrfs_data_container *data;
  2441. size_t alloc_bytes;
  2442. alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
  2443. data = kvzalloc(alloc_bytes, GFP_KERNEL);
  2444. if (!data)
  2445. return ERR_PTR(-ENOMEM);
  2446. if (total_bytes >= sizeof(*data))
  2447. data->bytes_left = total_bytes - sizeof(*data);
  2448. else
  2449. data->bytes_missing = sizeof(*data) - total_bytes;
  2450. return data;
  2451. }
  2452. /*
  2453. * allocates space to return multiple file system paths for an inode.
  2454. * total_bytes to allocate are passed, note that space usable for actual path
  2455. * information will be total_bytes - sizeof(struct inode_fs_paths).
  2456. * the returned pointer must be freed with free_ipath() in the end.
  2457. */
  2458. struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
  2459. struct btrfs_path *path)
  2460. {
  2461. struct inode_fs_paths *ifp;
  2462. struct btrfs_data_container *fspath;
  2463. fspath = init_data_container(total_bytes);
  2464. if (IS_ERR(fspath))
  2465. return ERR_CAST(fspath);
  2466. ifp = kmalloc(sizeof(*ifp), GFP_KERNEL);
  2467. if (!ifp) {
  2468. kvfree(fspath);
  2469. return ERR_PTR(-ENOMEM);
  2470. }
  2471. ifp->btrfs_path = path;
  2472. ifp->fspath = fspath;
  2473. ifp->fs_root = fs_root;
  2474. return ifp;
  2475. }
  2476. void free_ipath(struct inode_fs_paths *ipath)
  2477. {
  2478. if (!ipath)
  2479. return;
  2480. kvfree(ipath->fspath);
  2481. kfree(ipath);
  2482. }
  2483. struct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info)
  2484. {
  2485. struct btrfs_backref_iter *ret;
  2486. ret = kzalloc(sizeof(*ret), GFP_NOFS);
  2487. if (!ret)
  2488. return NULL;
  2489. ret->path = btrfs_alloc_path();
  2490. if (!ret->path) {
  2491. kfree(ret);
  2492. return NULL;
  2493. }
  2494. /* Current backref iterator only supports iteration in commit root */
  2495. ret->path->search_commit_root = 1;
  2496. ret->path->skip_locking = 1;
  2497. ret->fs_info = fs_info;
  2498. return ret;
  2499. }
  2500. static void btrfs_backref_iter_release(struct btrfs_backref_iter *iter)
  2501. {
  2502. iter->bytenr = 0;
  2503. iter->item_ptr = 0;
  2504. iter->cur_ptr = 0;
  2505. iter->end_ptr = 0;
  2506. btrfs_release_path(iter->path);
  2507. memset(&iter->cur_key, 0, sizeof(iter->cur_key));
  2508. }
  2509. int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr)
  2510. {
  2511. struct btrfs_fs_info *fs_info = iter->fs_info;
  2512. struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr);
  2513. struct btrfs_path *path = iter->path;
  2514. struct btrfs_extent_item *ei;
  2515. struct btrfs_key key;
  2516. int ret;
  2517. key.objectid = bytenr;
  2518. key.type = BTRFS_METADATA_ITEM_KEY;
  2519. key.offset = (u64)-1;
  2520. iter->bytenr = bytenr;
  2521. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2522. if (ret < 0)
  2523. return ret;
  2524. if (ret == 0) {
  2525. /*
  2526. * Key with offset -1 found, there would have to exist an extent
  2527. * item with such offset, but this is out of the valid range.
  2528. */
  2529. ret = -EUCLEAN;
  2530. goto release;
  2531. }
  2532. if (path->slots[0] == 0) {
  2533. WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
  2534. ret = -EUCLEAN;
  2535. goto release;
  2536. }
  2537. path->slots[0]--;
  2538. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  2539. if ((key.type != BTRFS_EXTENT_ITEM_KEY &&
  2540. key.type != BTRFS_METADATA_ITEM_KEY) || key.objectid != bytenr) {
  2541. ret = -ENOENT;
  2542. goto release;
  2543. }
  2544. memcpy(&iter->cur_key, &key, sizeof(key));
  2545. iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0],
  2546. path->slots[0]);
  2547. iter->end_ptr = (u32)(iter->item_ptr +
  2548. btrfs_item_size(path->nodes[0], path->slots[0]));
  2549. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2550. struct btrfs_extent_item);
  2551. /*
  2552. * Only support iteration on tree backref yet.
  2553. *
  2554. * This is an extra precaution for non skinny-metadata, where
  2555. * EXTENT_ITEM is also used for tree blocks, that we can only use
  2556. * extent flags to determine if it's a tree block.
  2557. */
  2558. if (btrfs_extent_flags(path->nodes[0], ei) & BTRFS_EXTENT_FLAG_DATA) {
  2559. ret = -ENOTSUPP;
  2560. goto release;
  2561. }
  2562. iter->cur_ptr = (u32)(iter->item_ptr + sizeof(*ei));
  2563. /* If there is no inline backref, go search for keyed backref */
  2564. if (iter->cur_ptr >= iter->end_ptr) {
  2565. ret = btrfs_next_item(extent_root, path);
  2566. /* No inline nor keyed ref */
  2567. if (ret > 0) {
  2568. ret = -ENOENT;
  2569. goto release;
  2570. }
  2571. if (ret < 0)
  2572. goto release;
  2573. btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key,
  2574. path->slots[0]);
  2575. if (iter->cur_key.objectid != bytenr ||
  2576. (iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
  2577. iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY)) {
  2578. ret = -ENOENT;
  2579. goto release;
  2580. }
  2581. iter->cur_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0],
  2582. path->slots[0]);
  2583. iter->item_ptr = iter->cur_ptr;
  2584. iter->end_ptr = (u32)(iter->item_ptr + btrfs_item_size(
  2585. path->nodes[0], path->slots[0]));
  2586. }
  2587. return 0;
  2588. release:
  2589. btrfs_backref_iter_release(iter);
  2590. return ret;
  2591. }
  2592. static bool btrfs_backref_iter_is_inline_ref(struct btrfs_backref_iter *iter)
  2593. {
  2594. if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY ||
  2595. iter->cur_key.type == BTRFS_METADATA_ITEM_KEY)
  2596. return true;
  2597. return false;
  2598. }
  2599. /*
  2600. * Go to the next backref item of current bytenr, can be either inlined or
  2601. * keyed.
  2602. *
  2603. * Caller needs to check whether it's inline ref or not by iter->cur_key.
  2604. *
  2605. * Return 0 if we get next backref without problem.
  2606. * Return >0 if there is no extra backref for this bytenr.
  2607. * Return <0 if there is something wrong happened.
  2608. */
  2609. int btrfs_backref_iter_next(struct btrfs_backref_iter *iter)
  2610. {
  2611. struct extent_buffer *eb = iter->path->nodes[0];
  2612. struct btrfs_root *extent_root;
  2613. struct btrfs_path *path = iter->path;
  2614. struct btrfs_extent_inline_ref *iref;
  2615. int ret;
  2616. u32 size;
  2617. if (btrfs_backref_iter_is_inline_ref(iter)) {
  2618. /* We're still inside the inline refs */
  2619. ASSERT(iter->cur_ptr < iter->end_ptr);
  2620. if (btrfs_backref_has_tree_block_info(iter)) {
  2621. /* First tree block info */
  2622. size = sizeof(struct btrfs_tree_block_info);
  2623. } else {
  2624. /* Use inline ref type to determine the size */
  2625. int type;
  2626. iref = (struct btrfs_extent_inline_ref *)
  2627. ((unsigned long)iter->cur_ptr);
  2628. type = btrfs_extent_inline_ref_type(eb, iref);
  2629. size = btrfs_extent_inline_ref_size(type);
  2630. }
  2631. iter->cur_ptr += size;
  2632. if (iter->cur_ptr < iter->end_ptr)
  2633. return 0;
  2634. /* All inline items iterated, fall through */
  2635. }
  2636. /* We're at keyed items, there is no inline item, go to the next one */
  2637. extent_root = btrfs_extent_root(iter->fs_info, iter->bytenr);
  2638. ret = btrfs_next_item(extent_root, iter->path);
  2639. if (ret)
  2640. return ret;
  2641. btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, path->slots[0]);
  2642. if (iter->cur_key.objectid != iter->bytenr ||
  2643. (iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
  2644. iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY))
  2645. return 1;
  2646. iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0],
  2647. path->slots[0]);
  2648. iter->cur_ptr = iter->item_ptr;
  2649. iter->end_ptr = iter->item_ptr + (u32)btrfs_item_size(path->nodes[0],
  2650. path->slots[0]);
  2651. return 0;
  2652. }
  2653. void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
  2654. struct btrfs_backref_cache *cache, bool is_reloc)
  2655. {
  2656. int i;
  2657. cache->rb_root = RB_ROOT;
  2658. for (i = 0; i < BTRFS_MAX_LEVEL; i++)
  2659. INIT_LIST_HEAD(&cache->pending[i]);
  2660. INIT_LIST_HEAD(&cache->changed);
  2661. INIT_LIST_HEAD(&cache->detached);
  2662. INIT_LIST_HEAD(&cache->leaves);
  2663. INIT_LIST_HEAD(&cache->pending_edge);
  2664. INIT_LIST_HEAD(&cache->useless_node);
  2665. cache->fs_info = fs_info;
  2666. cache->is_reloc = is_reloc;
  2667. }
  2668. struct btrfs_backref_node *btrfs_backref_alloc_node(
  2669. struct btrfs_backref_cache *cache, u64 bytenr, int level)
  2670. {
  2671. struct btrfs_backref_node *node;
  2672. ASSERT(level >= 0 && level < BTRFS_MAX_LEVEL);
  2673. node = kzalloc(sizeof(*node), GFP_NOFS);
  2674. if (!node)
  2675. return node;
  2676. INIT_LIST_HEAD(&node->list);
  2677. INIT_LIST_HEAD(&node->upper);
  2678. INIT_LIST_HEAD(&node->lower);
  2679. RB_CLEAR_NODE(&node->rb_node);
  2680. cache->nr_nodes++;
  2681. node->level = level;
  2682. node->bytenr = bytenr;
  2683. return node;
  2684. }
  2685. void btrfs_backref_free_node(struct btrfs_backref_cache *cache,
  2686. struct btrfs_backref_node *node)
  2687. {
  2688. if (node) {
  2689. ASSERT(list_empty(&node->list));
  2690. ASSERT(list_empty(&node->lower));
  2691. ASSERT(node->eb == NULL);
  2692. cache->nr_nodes--;
  2693. btrfs_put_root(node->root);
  2694. kfree(node);
  2695. }
  2696. }
  2697. struct btrfs_backref_edge *btrfs_backref_alloc_edge(
  2698. struct btrfs_backref_cache *cache)
  2699. {
  2700. struct btrfs_backref_edge *edge;
  2701. edge = kzalloc(sizeof(*edge), GFP_NOFS);
  2702. if (edge)
  2703. cache->nr_edges++;
  2704. return edge;
  2705. }
  2706. void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,
  2707. struct btrfs_backref_edge *edge)
  2708. {
  2709. if (edge) {
  2710. cache->nr_edges--;
  2711. kfree(edge);
  2712. }
  2713. }
  2714. void btrfs_backref_unlock_node_buffer(struct btrfs_backref_node *node)
  2715. {
  2716. if (node->locked) {
  2717. btrfs_tree_unlock(node->eb);
  2718. node->locked = 0;
  2719. }
  2720. }
  2721. void btrfs_backref_drop_node_buffer(struct btrfs_backref_node *node)
  2722. {
  2723. if (node->eb) {
  2724. btrfs_backref_unlock_node_buffer(node);
  2725. free_extent_buffer(node->eb);
  2726. node->eb = NULL;
  2727. }
  2728. }
  2729. /*
  2730. * Drop the backref node from cache without cleaning up its children
  2731. * edges.
  2732. *
  2733. * This can only be called on node without parent edges.
  2734. * The children edges are still kept as is.
  2735. */
  2736. void btrfs_backref_drop_node(struct btrfs_backref_cache *tree,
  2737. struct btrfs_backref_node *node)
  2738. {
  2739. ASSERT(list_empty(&node->upper));
  2740. btrfs_backref_drop_node_buffer(node);
  2741. list_del_init(&node->list);
  2742. list_del_init(&node->lower);
  2743. if (!RB_EMPTY_NODE(&node->rb_node))
  2744. rb_erase(&node->rb_node, &tree->rb_root);
  2745. btrfs_backref_free_node(tree, node);
  2746. }
  2747. /*
  2748. * Drop the backref node from cache, also cleaning up all its
  2749. * upper edges and any uncached nodes in the path.
  2750. *
  2751. * This cleanup happens bottom up, thus the node should either
  2752. * be the lowest node in the cache or a detached node.
  2753. */
  2754. void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
  2755. struct btrfs_backref_node *node)
  2756. {
  2757. struct btrfs_backref_node *upper;
  2758. struct btrfs_backref_edge *edge;
  2759. if (!node)
  2760. return;
  2761. BUG_ON(!node->lowest && !node->detached);
  2762. while (!list_empty(&node->upper)) {
  2763. edge = list_entry(node->upper.next, struct btrfs_backref_edge,
  2764. list[LOWER]);
  2765. upper = edge->node[UPPER];
  2766. list_del(&edge->list[LOWER]);
  2767. list_del(&edge->list[UPPER]);
  2768. btrfs_backref_free_edge(cache, edge);
  2769. /*
  2770. * Add the node to leaf node list if no other child block
  2771. * cached.
  2772. */
  2773. if (list_empty(&upper->lower)) {
  2774. list_add_tail(&upper->lower, &cache->leaves);
  2775. upper->lowest = 1;
  2776. }
  2777. }
  2778. btrfs_backref_drop_node(cache, node);
  2779. }
  2780. /*
  2781. * Release all nodes/edges from current cache
  2782. */
  2783. void btrfs_backref_release_cache(struct btrfs_backref_cache *cache)
  2784. {
  2785. struct btrfs_backref_node *node;
  2786. int i;
  2787. while (!list_empty(&cache->detached)) {
  2788. node = list_entry(cache->detached.next,
  2789. struct btrfs_backref_node, list);
  2790. btrfs_backref_cleanup_node(cache, node);
  2791. }
  2792. while (!list_empty(&cache->leaves)) {
  2793. node = list_entry(cache->leaves.next,
  2794. struct btrfs_backref_node, lower);
  2795. btrfs_backref_cleanup_node(cache, node);
  2796. }
  2797. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  2798. while (!list_empty(&cache->pending[i])) {
  2799. node = list_first_entry(&cache->pending[i],
  2800. struct btrfs_backref_node,
  2801. list);
  2802. btrfs_backref_cleanup_node(cache, node);
  2803. }
  2804. }
  2805. ASSERT(list_empty(&cache->pending_edge));
  2806. ASSERT(list_empty(&cache->useless_node));
  2807. ASSERT(list_empty(&cache->changed));
  2808. ASSERT(list_empty(&cache->detached));
  2809. ASSERT(RB_EMPTY_ROOT(&cache->rb_root));
  2810. ASSERT(!cache->nr_nodes);
  2811. ASSERT(!cache->nr_edges);
  2812. }
  2813. void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
  2814. struct btrfs_backref_node *lower,
  2815. struct btrfs_backref_node *upper,
  2816. int link_which)
  2817. {
  2818. ASSERT(upper && lower && upper->level == lower->level + 1);
  2819. edge->node[LOWER] = lower;
  2820. edge->node[UPPER] = upper;
  2821. if (link_which & LINK_LOWER)
  2822. list_add_tail(&edge->list[LOWER], &lower->upper);
  2823. if (link_which & LINK_UPPER)
  2824. list_add_tail(&edge->list[UPPER], &upper->lower);
  2825. }
  2826. /*
  2827. * Handle direct tree backref
  2828. *
  2829. * Direct tree backref means, the backref item shows its parent bytenr
  2830. * directly. This is for SHARED_BLOCK_REF backref (keyed or inlined).
  2831. *
  2832. * @ref_key: The converted backref key.
  2833. * For keyed backref, it's the item key.
  2834. * For inlined backref, objectid is the bytenr,
  2835. * type is btrfs_inline_ref_type, offset is
  2836. * btrfs_inline_ref_offset.
  2837. */
  2838. static int handle_direct_tree_backref(struct btrfs_backref_cache *cache,
  2839. struct btrfs_key *ref_key,
  2840. struct btrfs_backref_node *cur)
  2841. {
  2842. struct btrfs_backref_edge *edge;
  2843. struct btrfs_backref_node *upper;
  2844. struct rb_node *rb_node;
  2845. ASSERT(ref_key->type == BTRFS_SHARED_BLOCK_REF_KEY);
  2846. /* Only reloc root uses backref pointing to itself */
  2847. if (ref_key->objectid == ref_key->offset) {
  2848. struct btrfs_root *root;
  2849. cur->is_reloc_root = 1;
  2850. /* Only reloc backref cache cares about a specific root */
  2851. if (cache->is_reloc) {
  2852. root = find_reloc_root(cache->fs_info, cur->bytenr);
  2853. if (!root)
  2854. return -ENOENT;
  2855. cur->root = root;
  2856. } else {
  2857. /*
  2858. * For generic purpose backref cache, reloc root node
  2859. * is useless.
  2860. */
  2861. list_add(&cur->list, &cache->useless_node);
  2862. }
  2863. return 0;
  2864. }
  2865. edge = btrfs_backref_alloc_edge(cache);
  2866. if (!edge)
  2867. return -ENOMEM;
  2868. rb_node = rb_simple_search(&cache->rb_root, ref_key->offset);
  2869. if (!rb_node) {
  2870. /* Parent node not yet cached */
  2871. upper = btrfs_backref_alloc_node(cache, ref_key->offset,
  2872. cur->level + 1);
  2873. if (!upper) {
  2874. btrfs_backref_free_edge(cache, edge);
  2875. return -ENOMEM;
  2876. }
  2877. /*
  2878. * Backrefs for the upper level block isn't cached, add the
  2879. * block to pending list
  2880. */
  2881. list_add_tail(&edge->list[UPPER], &cache->pending_edge);
  2882. } else {
  2883. /* Parent node already cached */
  2884. upper = rb_entry(rb_node, struct btrfs_backref_node, rb_node);
  2885. ASSERT(upper->checked);
  2886. INIT_LIST_HEAD(&edge->list[UPPER]);
  2887. }
  2888. btrfs_backref_link_edge(edge, cur, upper, LINK_LOWER);
  2889. return 0;
  2890. }
  2891. /*
  2892. * Handle indirect tree backref
  2893. *
  2894. * Indirect tree backref means, we only know which tree the node belongs to.
  2895. * We still need to do a tree search to find out the parents. This is for
  2896. * TREE_BLOCK_REF backref (keyed or inlined).
  2897. *
  2898. * @trans: Transaction handle.
  2899. * @ref_key: The same as @ref_key in handle_direct_tree_backref()
  2900. * @tree_key: The first key of this tree block.
  2901. * @path: A clean (released) path, to avoid allocating path every time
  2902. * the function get called.
  2903. */
  2904. static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
  2905. struct btrfs_backref_cache *cache,
  2906. struct btrfs_path *path,
  2907. struct btrfs_key *ref_key,
  2908. struct btrfs_key *tree_key,
  2909. struct btrfs_backref_node *cur)
  2910. {
  2911. struct btrfs_fs_info *fs_info = cache->fs_info;
  2912. struct btrfs_backref_node *upper;
  2913. struct btrfs_backref_node *lower;
  2914. struct btrfs_backref_edge *edge;
  2915. struct extent_buffer *eb;
  2916. struct btrfs_root *root;
  2917. struct rb_node *rb_node;
  2918. int level;
  2919. bool need_check = true;
  2920. int ret;
  2921. root = btrfs_get_fs_root(fs_info, ref_key->offset, false);
  2922. if (IS_ERR(root))
  2923. return PTR_ERR(root);
  2924. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
  2925. cur->cowonly = 1;
  2926. if (btrfs_root_level(&root->root_item) == cur->level) {
  2927. /* Tree root */
  2928. ASSERT(btrfs_root_bytenr(&root->root_item) == cur->bytenr);
  2929. /*
  2930. * For reloc backref cache, we may ignore reloc root. But for
  2931. * general purpose backref cache, we can't rely on
  2932. * btrfs_should_ignore_reloc_root() as it may conflict with
  2933. * current running relocation and lead to missing root.
  2934. *
  2935. * For general purpose backref cache, reloc root detection is
  2936. * completely relying on direct backref (key->offset is parent
  2937. * bytenr), thus only do such check for reloc cache.
  2938. */
  2939. if (btrfs_should_ignore_reloc_root(root) && cache->is_reloc) {
  2940. btrfs_put_root(root);
  2941. list_add(&cur->list, &cache->useless_node);
  2942. } else {
  2943. cur->root = root;
  2944. }
  2945. return 0;
  2946. }
  2947. level = cur->level + 1;
  2948. /* Search the tree to find parent blocks referring to the block */
  2949. path->search_commit_root = 1;
  2950. path->skip_locking = 1;
  2951. path->lowest_level = level;
  2952. ret = btrfs_search_slot(NULL, root, tree_key, path, 0, 0);
  2953. path->lowest_level = 0;
  2954. if (ret < 0) {
  2955. btrfs_put_root(root);
  2956. return ret;
  2957. }
  2958. if (ret > 0 && path->slots[level] > 0)
  2959. path->slots[level]--;
  2960. eb = path->nodes[level];
  2961. if (btrfs_node_blockptr(eb, path->slots[level]) != cur->bytenr) {
  2962. btrfs_err(fs_info,
  2963. "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)",
  2964. cur->bytenr, level - 1, btrfs_root_id(root),
  2965. tree_key->objectid, tree_key->type, tree_key->offset);
  2966. btrfs_put_root(root);
  2967. ret = -ENOENT;
  2968. goto out;
  2969. }
  2970. lower = cur;
  2971. /* Add all nodes and edges in the path */
  2972. for (; level < BTRFS_MAX_LEVEL; level++) {
  2973. if (!path->nodes[level]) {
  2974. ASSERT(btrfs_root_bytenr(&root->root_item) ==
  2975. lower->bytenr);
  2976. /* Same as previous should_ignore_reloc_root() call */
  2977. if (btrfs_should_ignore_reloc_root(root) &&
  2978. cache->is_reloc) {
  2979. btrfs_put_root(root);
  2980. list_add(&lower->list, &cache->useless_node);
  2981. } else {
  2982. lower->root = root;
  2983. }
  2984. break;
  2985. }
  2986. edge = btrfs_backref_alloc_edge(cache);
  2987. if (!edge) {
  2988. btrfs_put_root(root);
  2989. ret = -ENOMEM;
  2990. goto out;
  2991. }
  2992. eb = path->nodes[level];
  2993. rb_node = rb_simple_search(&cache->rb_root, eb->start);
  2994. if (!rb_node) {
  2995. upper = btrfs_backref_alloc_node(cache, eb->start,
  2996. lower->level + 1);
  2997. if (!upper) {
  2998. btrfs_put_root(root);
  2999. btrfs_backref_free_edge(cache, edge);
  3000. ret = -ENOMEM;
  3001. goto out;
  3002. }
  3003. upper->owner = btrfs_header_owner(eb);
  3004. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
  3005. upper->cowonly = 1;
  3006. /*
  3007. * If we know the block isn't shared we can avoid
  3008. * checking its backrefs.
  3009. */
  3010. if (btrfs_block_can_be_shared(trans, root, eb))
  3011. upper->checked = 0;
  3012. else
  3013. upper->checked = 1;
  3014. /*
  3015. * Add the block to pending list if we need to check its
  3016. * backrefs, we only do this once while walking up a
  3017. * tree as we will catch anything else later on.
  3018. */
  3019. if (!upper->checked && need_check) {
  3020. need_check = false;
  3021. list_add_tail(&edge->list[UPPER],
  3022. &cache->pending_edge);
  3023. } else {
  3024. if (upper->checked)
  3025. need_check = true;
  3026. INIT_LIST_HEAD(&edge->list[UPPER]);
  3027. }
  3028. } else {
  3029. upper = rb_entry(rb_node, struct btrfs_backref_node,
  3030. rb_node);
  3031. ASSERT(upper->checked);
  3032. INIT_LIST_HEAD(&edge->list[UPPER]);
  3033. if (!upper->owner)
  3034. upper->owner = btrfs_header_owner(eb);
  3035. }
  3036. btrfs_backref_link_edge(edge, lower, upper, LINK_LOWER);
  3037. if (rb_node) {
  3038. btrfs_put_root(root);
  3039. break;
  3040. }
  3041. lower = upper;
  3042. upper = NULL;
  3043. }
  3044. out:
  3045. btrfs_release_path(path);
  3046. return ret;
  3047. }
  3048. /*
  3049. * Add backref node @cur into @cache.
  3050. *
  3051. * NOTE: Even if the function returned 0, @cur is not yet cached as its upper
  3052. * links aren't yet bi-directional. Needs to finish such links.
  3053. * Use btrfs_backref_finish_upper_links() to finish such linkage.
  3054. *
  3055. * @trans: Transaction handle.
  3056. * @path: Released path for indirect tree backref lookup
  3057. * @iter: Released backref iter for extent tree search
  3058. * @node_key: The first key of the tree block
  3059. */
  3060. int btrfs_backref_add_tree_node(struct btrfs_trans_handle *trans,
  3061. struct btrfs_backref_cache *cache,
  3062. struct btrfs_path *path,
  3063. struct btrfs_backref_iter *iter,
  3064. struct btrfs_key *node_key,
  3065. struct btrfs_backref_node *cur)
  3066. {
  3067. struct btrfs_backref_edge *edge;
  3068. struct btrfs_backref_node *exist;
  3069. int ret;
  3070. ret = btrfs_backref_iter_start(iter, cur->bytenr);
  3071. if (ret < 0)
  3072. return ret;
  3073. /*
  3074. * We skip the first btrfs_tree_block_info, as we don't use the key
  3075. * stored in it, but fetch it from the tree block
  3076. */
  3077. if (btrfs_backref_has_tree_block_info(iter)) {
  3078. ret = btrfs_backref_iter_next(iter);
  3079. if (ret < 0)
  3080. goto out;
  3081. /* No extra backref? This means the tree block is corrupted */
  3082. if (ret > 0) {
  3083. ret = -EUCLEAN;
  3084. goto out;
  3085. }
  3086. }
  3087. WARN_ON(cur->checked);
  3088. if (!list_empty(&cur->upper)) {
  3089. /*
  3090. * The backref was added previously when processing backref of
  3091. * type BTRFS_TREE_BLOCK_REF_KEY
  3092. */
  3093. ASSERT(list_is_singular(&cur->upper));
  3094. edge = list_entry(cur->upper.next, struct btrfs_backref_edge,
  3095. list[LOWER]);
  3096. ASSERT(list_empty(&edge->list[UPPER]));
  3097. exist = edge->node[UPPER];
  3098. /*
  3099. * Add the upper level block to pending list if we need check
  3100. * its backrefs
  3101. */
  3102. if (!exist->checked)
  3103. list_add_tail(&edge->list[UPPER], &cache->pending_edge);
  3104. } else {
  3105. exist = NULL;
  3106. }
  3107. for (; ret == 0; ret = btrfs_backref_iter_next(iter)) {
  3108. struct extent_buffer *eb;
  3109. struct btrfs_key key;
  3110. int type;
  3111. cond_resched();
  3112. eb = iter->path->nodes[0];
  3113. key.objectid = iter->bytenr;
  3114. if (btrfs_backref_iter_is_inline_ref(iter)) {
  3115. struct btrfs_extent_inline_ref *iref;
  3116. /* Update key for inline backref */
  3117. iref = (struct btrfs_extent_inline_ref *)
  3118. ((unsigned long)iter->cur_ptr);
  3119. type = btrfs_get_extent_inline_ref_type(eb, iref,
  3120. BTRFS_REF_TYPE_BLOCK);
  3121. if (type == BTRFS_REF_TYPE_INVALID) {
  3122. ret = -EUCLEAN;
  3123. goto out;
  3124. }
  3125. key.type = type;
  3126. key.offset = btrfs_extent_inline_ref_offset(eb, iref);
  3127. } else {
  3128. key.type = iter->cur_key.type;
  3129. key.offset = iter->cur_key.offset;
  3130. }
  3131. /*
  3132. * Parent node found and matches current inline ref, no need to
  3133. * rebuild this node for this inline ref
  3134. */
  3135. if (exist &&
  3136. ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
  3137. exist->owner == key.offset) ||
  3138. (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
  3139. exist->bytenr == key.offset))) {
  3140. exist = NULL;
  3141. continue;
  3142. }
  3143. /* SHARED_BLOCK_REF means key.offset is the parent bytenr */
  3144. if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
  3145. ret = handle_direct_tree_backref(cache, &key, cur);
  3146. if (ret < 0)
  3147. goto out;
  3148. } else if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
  3149. /*
  3150. * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref
  3151. * offset means the root objectid. We need to search
  3152. * the tree to get its parent bytenr.
  3153. */
  3154. ret = handle_indirect_tree_backref(trans, cache, path,
  3155. &key, node_key, cur);
  3156. if (ret < 0)
  3157. goto out;
  3158. }
  3159. /*
  3160. * Unrecognized tree backref items (if it can pass tree-checker)
  3161. * would be ignored.
  3162. */
  3163. }
  3164. ret = 0;
  3165. cur->checked = 1;
  3166. WARN_ON(exist);
  3167. out:
  3168. btrfs_backref_iter_release(iter);
  3169. return ret;
  3170. }
  3171. /*
  3172. * Finish the upwards linkage created by btrfs_backref_add_tree_node()
  3173. */
  3174. int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
  3175. struct btrfs_backref_node *start)
  3176. {
  3177. struct list_head *useless_node = &cache->useless_node;
  3178. struct btrfs_backref_edge *edge;
  3179. struct rb_node *rb_node;
  3180. LIST_HEAD(pending_edge);
  3181. ASSERT(start->checked);
  3182. /* Insert this node to cache if it's not COW-only */
  3183. if (!start->cowonly) {
  3184. rb_node = rb_simple_insert(&cache->rb_root, start->bytenr,
  3185. &start->rb_node);
  3186. if (rb_node)
  3187. btrfs_backref_panic(cache->fs_info, start->bytenr,
  3188. -EEXIST);
  3189. list_add_tail(&start->lower, &cache->leaves);
  3190. }
  3191. /*
  3192. * Use breadth first search to iterate all related edges.
  3193. *
  3194. * The starting points are all the edges of this node
  3195. */
  3196. list_for_each_entry(edge, &start->upper, list[LOWER])
  3197. list_add_tail(&edge->list[UPPER], &pending_edge);
  3198. while (!list_empty(&pending_edge)) {
  3199. struct btrfs_backref_node *upper;
  3200. struct btrfs_backref_node *lower;
  3201. edge = list_first_entry(&pending_edge,
  3202. struct btrfs_backref_edge, list[UPPER]);
  3203. list_del_init(&edge->list[UPPER]);
  3204. upper = edge->node[UPPER];
  3205. lower = edge->node[LOWER];
  3206. /* Parent is detached, no need to keep any edges */
  3207. if (upper->detached) {
  3208. list_del(&edge->list[LOWER]);
  3209. btrfs_backref_free_edge(cache, edge);
  3210. /* Lower node is orphan, queue for cleanup */
  3211. if (list_empty(&lower->upper))
  3212. list_add(&lower->list, useless_node);
  3213. continue;
  3214. }
  3215. /*
  3216. * All new nodes added in current build_backref_tree() haven't
  3217. * been linked to the cache rb tree.
  3218. * So if we have upper->rb_node populated, this means a cache
  3219. * hit. We only need to link the edge, as @upper and all its
  3220. * parents have already been linked.
  3221. */
  3222. if (!RB_EMPTY_NODE(&upper->rb_node)) {
  3223. if (upper->lowest) {
  3224. list_del_init(&upper->lower);
  3225. upper->lowest = 0;
  3226. }
  3227. list_add_tail(&edge->list[UPPER], &upper->lower);
  3228. continue;
  3229. }
  3230. /* Sanity check, we shouldn't have any unchecked nodes */
  3231. if (!upper->checked) {
  3232. ASSERT(0);
  3233. return -EUCLEAN;
  3234. }
  3235. /* Sanity check, COW-only node has non-COW-only parent */
  3236. if (start->cowonly != upper->cowonly) {
  3237. ASSERT(0);
  3238. return -EUCLEAN;
  3239. }
  3240. /* Only cache non-COW-only (subvolume trees) tree blocks */
  3241. if (!upper->cowonly) {
  3242. rb_node = rb_simple_insert(&cache->rb_root, upper->bytenr,
  3243. &upper->rb_node);
  3244. if (rb_node) {
  3245. btrfs_backref_panic(cache->fs_info,
  3246. upper->bytenr, -EEXIST);
  3247. return -EUCLEAN;
  3248. }
  3249. }
  3250. list_add_tail(&edge->list[UPPER], &upper->lower);
  3251. /*
  3252. * Also queue all the parent edges of this uncached node
  3253. * to finish the upper linkage
  3254. */
  3255. list_for_each_entry(edge, &upper->upper, list[LOWER])
  3256. list_add_tail(&edge->list[UPPER], &pending_edge);
  3257. }
  3258. return 0;
  3259. }
  3260. void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
  3261. struct btrfs_backref_node *node)
  3262. {
  3263. struct btrfs_backref_node *lower;
  3264. struct btrfs_backref_node *upper;
  3265. struct btrfs_backref_edge *edge;
  3266. while (!list_empty(&cache->useless_node)) {
  3267. lower = list_first_entry(&cache->useless_node,
  3268. struct btrfs_backref_node, list);
  3269. list_del_init(&lower->list);
  3270. }
  3271. while (!list_empty(&cache->pending_edge)) {
  3272. edge = list_first_entry(&cache->pending_edge,
  3273. struct btrfs_backref_edge, list[UPPER]);
  3274. list_del(&edge->list[UPPER]);
  3275. list_del(&edge->list[LOWER]);
  3276. lower = edge->node[LOWER];
  3277. upper = edge->node[UPPER];
  3278. btrfs_backref_free_edge(cache, edge);
  3279. /*
  3280. * Lower is no longer linked to any upper backref nodes and
  3281. * isn't in the cache, we can free it ourselves.
  3282. */
  3283. if (list_empty(&lower->upper) &&
  3284. RB_EMPTY_NODE(&lower->rb_node))
  3285. list_add(&lower->list, &cache->useless_node);
  3286. if (!RB_EMPTY_NODE(&upper->rb_node))
  3287. continue;
  3288. /* Add this guy's upper edges to the list to process */
  3289. list_for_each_entry(edge, &upper->upper, list[LOWER])
  3290. list_add_tail(&edge->list[UPPER],
  3291. &cache->pending_edge);
  3292. if (list_empty(&upper->upper))
  3293. list_add(&upper->list, &cache->useless_node);
  3294. }
  3295. while (!list_empty(&cache->useless_node)) {
  3296. lower = list_first_entry(&cache->useless_node,
  3297. struct btrfs_backref_node, list);
  3298. list_del_init(&lower->list);
  3299. if (lower == node)
  3300. node = NULL;
  3301. btrfs_backref_drop_node(cache, lower);
  3302. }
  3303. btrfs_backref_cleanup_node(cache, node);
  3304. ASSERT(list_empty(&cache->useless_node) &&
  3305. list_empty(&cache->pending_edge));
  3306. }