ctree.c 153 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963
  1. /*
  2. * Copyright (C) 2007,2008 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/rbtree.h>
  21. #include <linux/mm.h>
  22. #include "ctree.h"
  23. #include "disk-io.h"
  24. #include "transaction.h"
  25. #include "print-tree.h"
  26. #include "locking.h"
  27. static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
  28. *root, struct btrfs_path *path, int level);
  29. static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  30. const struct btrfs_key *ins_key, struct btrfs_path *path,
  31. int data_size, int extend);
  32. static int push_node_left(struct btrfs_trans_handle *trans,
  33. struct btrfs_fs_info *fs_info,
  34. struct extent_buffer *dst,
  35. struct extent_buffer *src, int empty);
  36. static int balance_node_right(struct btrfs_trans_handle *trans,
  37. struct btrfs_fs_info *fs_info,
  38. struct extent_buffer *dst_buf,
  39. struct extent_buffer *src_buf);
  40. static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
  41. int level, int slot);
  42. static int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
  43. struct extent_buffer *eb);
  44. struct btrfs_path *btrfs_alloc_path(void)
  45. {
  46. return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
  47. }
  48. /*
  49. * set all locked nodes in the path to blocking locks. This should
  50. * be done before scheduling
  51. */
  52. noinline void btrfs_set_path_blocking(struct btrfs_path *p)
  53. {
  54. int i;
  55. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  56. if (!p->nodes[i] || !p->locks[i])
  57. continue;
  58. btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
  59. if (p->locks[i] == BTRFS_READ_LOCK)
  60. p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
  61. else if (p->locks[i] == BTRFS_WRITE_LOCK)
  62. p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
  63. }
  64. }
  65. /*
  66. * reset all the locked nodes in the patch to spinning locks.
  67. *
  68. * held is used to keep lockdep happy, when lockdep is enabled
  69. * we set held to a blocking lock before we go around and
  70. * retake all the spinlocks in the path. You can safely use NULL
  71. * for held
  72. */
  73. noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
  74. struct extent_buffer *held, int held_rw)
  75. {
  76. int i;
  77. if (held) {
  78. btrfs_set_lock_blocking_rw(held, held_rw);
  79. if (held_rw == BTRFS_WRITE_LOCK)
  80. held_rw = BTRFS_WRITE_LOCK_BLOCKING;
  81. else if (held_rw == BTRFS_READ_LOCK)
  82. held_rw = BTRFS_READ_LOCK_BLOCKING;
  83. }
  84. btrfs_set_path_blocking(p);
  85. for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
  86. if (p->nodes[i] && p->locks[i]) {
  87. btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
  88. if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
  89. p->locks[i] = BTRFS_WRITE_LOCK;
  90. else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
  91. p->locks[i] = BTRFS_READ_LOCK;
  92. }
  93. }
  94. if (held)
  95. btrfs_clear_lock_blocking_rw(held, held_rw);
  96. }
  97. /* this also releases the path */
  98. void btrfs_free_path(struct btrfs_path *p)
  99. {
  100. if (!p)
  101. return;
  102. btrfs_release_path(p);
  103. kmem_cache_free(btrfs_path_cachep, p);
  104. }
  105. /*
  106. * path release drops references on the extent buffers in the path
  107. * and it drops any locks held by this path
  108. *
  109. * It is safe to call this on paths that no locks or extent buffers held.
  110. */
  111. noinline void btrfs_release_path(struct btrfs_path *p)
  112. {
  113. int i;
  114. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  115. p->slots[i] = 0;
  116. if (!p->nodes[i])
  117. continue;
  118. if (p->locks[i]) {
  119. btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
  120. p->locks[i] = 0;
  121. }
  122. free_extent_buffer(p->nodes[i]);
  123. p->nodes[i] = NULL;
  124. }
  125. }
  126. /*
  127. * safely gets a reference on the root node of a tree. A lock
  128. * is not taken, so a concurrent writer may put a different node
  129. * at the root of the tree. See btrfs_lock_root_node for the
  130. * looping required.
  131. *
  132. * The extent buffer returned by this has a reference taken, so
  133. * it won't disappear. It may stop being the root of the tree
  134. * at any time because there are no locks held.
  135. */
  136. struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
  137. {
  138. struct extent_buffer *eb;
  139. while (1) {
  140. rcu_read_lock();
  141. eb = rcu_dereference(root->node);
  142. /*
  143. * RCU really hurts here, we could free up the root node because
  144. * it was COWed but we may not get the new root node yet so do
  145. * the inc_not_zero dance and if it doesn't work then
  146. * synchronize_rcu and try again.
  147. */
  148. if (atomic_inc_not_zero(&eb->refs)) {
  149. rcu_read_unlock();
  150. break;
  151. }
  152. rcu_read_unlock();
  153. synchronize_rcu();
  154. }
  155. return eb;
  156. }
  157. /* loop around taking references on and locking the root node of the
  158. * tree until you end up with a lock on the root. A locked buffer
  159. * is returned, with a reference held.
  160. */
  161. struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
  162. {
  163. struct extent_buffer *eb;
  164. while (1) {
  165. eb = btrfs_root_node(root);
  166. btrfs_tree_lock(eb);
  167. if (eb == root->node)
  168. break;
  169. btrfs_tree_unlock(eb);
  170. free_extent_buffer(eb);
  171. }
  172. return eb;
  173. }
  174. /* loop around taking references on and locking the root node of the
  175. * tree until you end up with a lock on the root. A locked buffer
  176. * is returned, with a reference held.
  177. */
  178. struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
  179. {
  180. struct extent_buffer *eb;
  181. while (1) {
  182. eb = btrfs_root_node(root);
  183. btrfs_tree_read_lock(eb);
  184. if (eb == root->node)
  185. break;
  186. btrfs_tree_read_unlock(eb);
  187. free_extent_buffer(eb);
  188. }
  189. return eb;
  190. }
  191. /* cowonly root (everything not a reference counted cow subvolume), just get
  192. * put onto a simple dirty list. transaction.c walks this to make sure they
  193. * get properly updated on disk.
  194. */
  195. static void add_root_to_dirty_list(struct btrfs_root *root)
  196. {
  197. struct btrfs_fs_info *fs_info = root->fs_info;
  198. if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
  199. !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
  200. return;
  201. spin_lock(&fs_info->trans_lock);
  202. if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
  203. /* Want the extent tree to be the last on the list */
  204. if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  205. list_move_tail(&root->dirty_list,
  206. &fs_info->dirty_cowonly_roots);
  207. else
  208. list_move(&root->dirty_list,
  209. &fs_info->dirty_cowonly_roots);
  210. }
  211. spin_unlock(&fs_info->trans_lock);
  212. }
  213. /*
  214. * used by snapshot creation to make a copy of a root for a tree with
  215. * a given objectid. The buffer with the new root node is returned in
  216. * cow_ret, and this func returns zero on success or a negative error code.
  217. */
  218. int btrfs_copy_root(struct btrfs_trans_handle *trans,
  219. struct btrfs_root *root,
  220. struct extent_buffer *buf,
  221. struct extent_buffer **cow_ret, u64 new_root_objectid)
  222. {
  223. struct btrfs_fs_info *fs_info = root->fs_info;
  224. struct extent_buffer *cow;
  225. int ret = 0;
  226. int level;
  227. struct btrfs_disk_key disk_key;
  228. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  229. trans->transid != fs_info->running_transaction->transid);
  230. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  231. trans->transid != root->last_trans);
  232. level = btrfs_header_level(buf);
  233. if (level == 0)
  234. btrfs_item_key(buf, &disk_key, 0);
  235. else
  236. btrfs_node_key(buf, &disk_key, 0);
  237. cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
  238. &disk_key, level, buf->start, 0);
  239. if (IS_ERR(cow))
  240. return PTR_ERR(cow);
  241. copy_extent_buffer_full(cow, buf);
  242. btrfs_set_header_bytenr(cow, cow->start);
  243. btrfs_set_header_generation(cow, trans->transid);
  244. btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
  245. btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
  246. BTRFS_HEADER_FLAG_RELOC);
  247. if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  248. btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
  249. else
  250. btrfs_set_header_owner(cow, new_root_objectid);
  251. write_extent_buffer_fsid(cow, fs_info->fsid);
  252. WARN_ON(btrfs_header_generation(buf) > trans->transid);
  253. if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  254. ret = btrfs_inc_ref(trans, root, cow, 1);
  255. else
  256. ret = btrfs_inc_ref(trans, root, cow, 0);
  257. if (ret)
  258. return ret;
  259. btrfs_mark_buffer_dirty(cow);
  260. *cow_ret = cow;
  261. return 0;
  262. }
  263. enum mod_log_op {
  264. MOD_LOG_KEY_REPLACE,
  265. MOD_LOG_KEY_ADD,
  266. MOD_LOG_KEY_REMOVE,
  267. MOD_LOG_KEY_REMOVE_WHILE_FREEING,
  268. MOD_LOG_KEY_REMOVE_WHILE_MOVING,
  269. MOD_LOG_MOVE_KEYS,
  270. MOD_LOG_ROOT_REPLACE,
  271. };
  272. struct tree_mod_move {
  273. int dst_slot;
  274. int nr_items;
  275. };
  276. struct tree_mod_root {
  277. u64 logical;
  278. u8 level;
  279. };
  280. struct tree_mod_elem {
  281. struct rb_node node;
  282. u64 logical;
  283. u64 seq;
  284. enum mod_log_op op;
  285. /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
  286. int slot;
  287. /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
  288. u64 generation;
  289. /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
  290. struct btrfs_disk_key key;
  291. u64 blockptr;
  292. /* this is used for op == MOD_LOG_MOVE_KEYS */
  293. struct tree_mod_move move;
  294. /* this is used for op == MOD_LOG_ROOT_REPLACE */
  295. struct tree_mod_root old_root;
  296. };
  297. static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
  298. {
  299. read_lock(&fs_info->tree_mod_log_lock);
  300. }
  301. static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
  302. {
  303. read_unlock(&fs_info->tree_mod_log_lock);
  304. }
  305. static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
  306. {
  307. write_lock(&fs_info->tree_mod_log_lock);
  308. }
  309. static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
  310. {
  311. write_unlock(&fs_info->tree_mod_log_lock);
  312. }
  313. /*
  314. * Pull a new tree mod seq number for our operation.
  315. */
  316. static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
  317. {
  318. return atomic64_inc_return(&fs_info->tree_mod_seq);
  319. }
  320. /*
  321. * This adds a new blocker to the tree mod log's blocker list if the @elem
  322. * passed does not already have a sequence number set. So when a caller expects
  323. * to record tree modifications, it should ensure to set elem->seq to zero
  324. * before calling btrfs_get_tree_mod_seq.
  325. * Returns a fresh, unused tree log modification sequence number, even if no new
  326. * blocker was added.
  327. */
  328. u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
  329. struct seq_list *elem)
  330. {
  331. tree_mod_log_write_lock(fs_info);
  332. spin_lock(&fs_info->tree_mod_seq_lock);
  333. if (!elem->seq) {
  334. elem->seq = btrfs_inc_tree_mod_seq(fs_info);
  335. list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
  336. }
  337. spin_unlock(&fs_info->tree_mod_seq_lock);
  338. tree_mod_log_write_unlock(fs_info);
  339. return elem->seq;
  340. }
  341. void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
  342. struct seq_list *elem)
  343. {
  344. struct rb_root *tm_root;
  345. struct rb_node *node;
  346. struct rb_node *next;
  347. struct seq_list *cur_elem;
  348. struct tree_mod_elem *tm;
  349. u64 min_seq = (u64)-1;
  350. u64 seq_putting = elem->seq;
  351. if (!seq_putting)
  352. return;
  353. spin_lock(&fs_info->tree_mod_seq_lock);
  354. list_del(&elem->list);
  355. elem->seq = 0;
  356. list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
  357. if (cur_elem->seq < min_seq) {
  358. if (seq_putting > cur_elem->seq) {
  359. /*
  360. * blocker with lower sequence number exists, we
  361. * cannot remove anything from the log
  362. */
  363. spin_unlock(&fs_info->tree_mod_seq_lock);
  364. return;
  365. }
  366. min_seq = cur_elem->seq;
  367. }
  368. }
  369. spin_unlock(&fs_info->tree_mod_seq_lock);
  370. /*
  371. * anything that's lower than the lowest existing (read: blocked)
  372. * sequence number can be removed from the tree.
  373. */
  374. tree_mod_log_write_lock(fs_info);
  375. tm_root = &fs_info->tree_mod_log;
  376. for (node = rb_first(tm_root); node; node = next) {
  377. next = rb_next(node);
  378. tm = rb_entry(node, struct tree_mod_elem, node);
  379. if (tm->seq > min_seq)
  380. continue;
  381. rb_erase(node, tm_root);
  382. kfree(tm);
  383. }
  384. tree_mod_log_write_unlock(fs_info);
  385. }
  386. /*
  387. * key order of the log:
  388. * node/leaf start address -> sequence
  389. *
  390. * The 'start address' is the logical address of the *new* root node
  391. * for root replace operations, or the logical address of the affected
  392. * block for all other operations.
  393. *
  394. * Note: must be called with write lock (tree_mod_log_write_lock).
  395. */
  396. static noinline int
  397. __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
  398. {
  399. struct rb_root *tm_root;
  400. struct rb_node **new;
  401. struct rb_node *parent = NULL;
  402. struct tree_mod_elem *cur;
  403. tm->seq = btrfs_inc_tree_mod_seq(fs_info);
  404. tm_root = &fs_info->tree_mod_log;
  405. new = &tm_root->rb_node;
  406. while (*new) {
  407. cur = rb_entry(*new, struct tree_mod_elem, node);
  408. parent = *new;
  409. if (cur->logical < tm->logical)
  410. new = &((*new)->rb_left);
  411. else if (cur->logical > tm->logical)
  412. new = &((*new)->rb_right);
  413. else if (cur->seq < tm->seq)
  414. new = &((*new)->rb_left);
  415. else if (cur->seq > tm->seq)
  416. new = &((*new)->rb_right);
  417. else
  418. return -EEXIST;
  419. }
  420. rb_link_node(&tm->node, parent, new);
  421. rb_insert_color(&tm->node, tm_root);
  422. return 0;
  423. }
  424. /*
  425. * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
  426. * returns zero with the tree_mod_log_lock acquired. The caller must hold
  427. * this until all tree mod log insertions are recorded in the rb tree and then
  428. * call tree_mod_log_write_unlock() to release.
  429. */
  430. static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
  431. struct extent_buffer *eb) {
  432. smp_mb();
  433. if (list_empty(&(fs_info)->tree_mod_seq_list))
  434. return 1;
  435. if (eb && btrfs_header_level(eb) == 0)
  436. return 1;
  437. tree_mod_log_write_lock(fs_info);
  438. if (list_empty(&(fs_info)->tree_mod_seq_list)) {
  439. tree_mod_log_write_unlock(fs_info);
  440. return 1;
  441. }
  442. return 0;
  443. }
  444. /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
  445. static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
  446. struct extent_buffer *eb)
  447. {
  448. smp_mb();
  449. if (list_empty(&(fs_info)->tree_mod_seq_list))
  450. return 0;
  451. if (eb && btrfs_header_level(eb) == 0)
  452. return 0;
  453. return 1;
  454. }
  455. static struct tree_mod_elem *
  456. alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
  457. enum mod_log_op op, gfp_t flags)
  458. {
  459. struct tree_mod_elem *tm;
  460. tm = kzalloc(sizeof(*tm), flags);
  461. if (!tm)
  462. return NULL;
  463. tm->logical = eb->start;
  464. if (op != MOD_LOG_KEY_ADD) {
  465. btrfs_node_key(eb, &tm->key, slot);
  466. tm->blockptr = btrfs_node_blockptr(eb, slot);
  467. }
  468. tm->op = op;
  469. tm->slot = slot;
  470. tm->generation = btrfs_node_ptr_generation(eb, slot);
  471. RB_CLEAR_NODE(&tm->node);
  472. return tm;
  473. }
  474. static noinline int
  475. tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
  476. struct extent_buffer *eb, int slot,
  477. enum mod_log_op op, gfp_t flags)
  478. {
  479. struct tree_mod_elem *tm;
  480. int ret;
  481. if (!tree_mod_need_log(fs_info, eb))
  482. return 0;
  483. tm = alloc_tree_mod_elem(eb, slot, op, flags);
  484. if (!tm)
  485. return -ENOMEM;
  486. if (tree_mod_dont_log(fs_info, eb)) {
  487. kfree(tm);
  488. return 0;
  489. }
  490. ret = __tree_mod_log_insert(fs_info, tm);
  491. tree_mod_log_write_unlock(fs_info);
  492. if (ret)
  493. kfree(tm);
  494. return ret;
  495. }
  496. static noinline int
  497. tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
  498. struct extent_buffer *eb, int dst_slot, int src_slot,
  499. int nr_items)
  500. {
  501. struct tree_mod_elem *tm = NULL;
  502. struct tree_mod_elem **tm_list = NULL;
  503. int ret = 0;
  504. int i;
  505. int locked = 0;
  506. if (!tree_mod_need_log(fs_info, eb))
  507. return 0;
  508. tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
  509. if (!tm_list)
  510. return -ENOMEM;
  511. tm = kzalloc(sizeof(*tm), GFP_NOFS);
  512. if (!tm) {
  513. ret = -ENOMEM;
  514. goto free_tms;
  515. }
  516. tm->logical = eb->start;
  517. tm->slot = src_slot;
  518. tm->move.dst_slot = dst_slot;
  519. tm->move.nr_items = nr_items;
  520. tm->op = MOD_LOG_MOVE_KEYS;
  521. for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
  522. tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
  523. MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
  524. if (!tm_list[i]) {
  525. ret = -ENOMEM;
  526. goto free_tms;
  527. }
  528. }
  529. if (tree_mod_dont_log(fs_info, eb))
  530. goto free_tms;
  531. locked = 1;
  532. /*
  533. * When we override something during the move, we log these removals.
  534. * This can only happen when we move towards the beginning of the
  535. * buffer, i.e. dst_slot < src_slot.
  536. */
  537. for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
  538. ret = __tree_mod_log_insert(fs_info, tm_list[i]);
  539. if (ret)
  540. goto free_tms;
  541. }
  542. ret = __tree_mod_log_insert(fs_info, tm);
  543. if (ret)
  544. goto free_tms;
  545. tree_mod_log_write_unlock(fs_info);
  546. kfree(tm_list);
  547. return 0;
  548. free_tms:
  549. for (i = 0; i < nr_items; i++) {
  550. if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
  551. rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
  552. kfree(tm_list[i]);
  553. }
  554. if (locked)
  555. tree_mod_log_write_unlock(fs_info);
  556. kfree(tm_list);
  557. kfree(tm);
  558. return ret;
  559. }
  560. static inline int
  561. __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
  562. struct tree_mod_elem **tm_list,
  563. int nritems)
  564. {
  565. int i, j;
  566. int ret;
  567. for (i = nritems - 1; i >= 0; i--) {
  568. ret = __tree_mod_log_insert(fs_info, tm_list[i]);
  569. if (ret) {
  570. for (j = nritems - 1; j > i; j--)
  571. rb_erase(&tm_list[j]->node,
  572. &fs_info->tree_mod_log);
  573. return ret;
  574. }
  575. }
  576. return 0;
  577. }
  578. static noinline int
  579. tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
  580. struct extent_buffer *old_root,
  581. struct extent_buffer *new_root,
  582. int log_removal)
  583. {
  584. struct tree_mod_elem *tm = NULL;
  585. struct tree_mod_elem **tm_list = NULL;
  586. int nritems = 0;
  587. int ret = 0;
  588. int i;
  589. if (!tree_mod_need_log(fs_info, NULL))
  590. return 0;
  591. if (log_removal && btrfs_header_level(old_root) > 0) {
  592. nritems = btrfs_header_nritems(old_root);
  593. tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
  594. GFP_NOFS);
  595. if (!tm_list) {
  596. ret = -ENOMEM;
  597. goto free_tms;
  598. }
  599. for (i = 0; i < nritems; i++) {
  600. tm_list[i] = alloc_tree_mod_elem(old_root, i,
  601. MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
  602. if (!tm_list[i]) {
  603. ret = -ENOMEM;
  604. goto free_tms;
  605. }
  606. }
  607. }
  608. tm = kzalloc(sizeof(*tm), GFP_NOFS);
  609. if (!tm) {
  610. ret = -ENOMEM;
  611. goto free_tms;
  612. }
  613. tm->logical = new_root->start;
  614. tm->old_root.logical = old_root->start;
  615. tm->old_root.level = btrfs_header_level(old_root);
  616. tm->generation = btrfs_header_generation(old_root);
  617. tm->op = MOD_LOG_ROOT_REPLACE;
  618. if (tree_mod_dont_log(fs_info, NULL))
  619. goto free_tms;
  620. if (tm_list)
  621. ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
  622. if (!ret)
  623. ret = __tree_mod_log_insert(fs_info, tm);
  624. tree_mod_log_write_unlock(fs_info);
  625. if (ret)
  626. goto free_tms;
  627. kfree(tm_list);
  628. return ret;
  629. free_tms:
  630. if (tm_list) {
  631. for (i = 0; i < nritems; i++)
  632. kfree(tm_list[i]);
  633. kfree(tm_list);
  634. }
  635. kfree(tm);
  636. return ret;
  637. }
  638. static struct tree_mod_elem *
  639. __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
  640. int smallest)
  641. {
  642. struct rb_root *tm_root;
  643. struct rb_node *node;
  644. struct tree_mod_elem *cur = NULL;
  645. struct tree_mod_elem *found = NULL;
  646. tree_mod_log_read_lock(fs_info);
  647. tm_root = &fs_info->tree_mod_log;
  648. node = tm_root->rb_node;
  649. while (node) {
  650. cur = rb_entry(node, struct tree_mod_elem, node);
  651. if (cur->logical < start) {
  652. node = node->rb_left;
  653. } else if (cur->logical > start) {
  654. node = node->rb_right;
  655. } else if (cur->seq < min_seq) {
  656. node = node->rb_left;
  657. } else if (!smallest) {
  658. /* we want the node with the highest seq */
  659. if (found)
  660. BUG_ON(found->seq > cur->seq);
  661. found = cur;
  662. node = node->rb_left;
  663. } else if (cur->seq > min_seq) {
  664. /* we want the node with the smallest seq */
  665. if (found)
  666. BUG_ON(found->seq < cur->seq);
  667. found = cur;
  668. node = node->rb_right;
  669. } else {
  670. found = cur;
  671. break;
  672. }
  673. }
  674. tree_mod_log_read_unlock(fs_info);
  675. return found;
  676. }
  677. /*
  678. * this returns the element from the log with the smallest time sequence
  679. * value that's in the log (the oldest log item). any element with a time
  680. * sequence lower than min_seq will be ignored.
  681. */
  682. static struct tree_mod_elem *
  683. tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
  684. u64 min_seq)
  685. {
  686. return __tree_mod_log_search(fs_info, start, min_seq, 1);
  687. }
  688. /*
  689. * this returns the element from the log with the largest time sequence
  690. * value that's in the log (the most recent log item). any element with
  691. * a time sequence lower than min_seq will be ignored.
  692. */
  693. static struct tree_mod_elem *
  694. tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
  695. {
  696. return __tree_mod_log_search(fs_info, start, min_seq, 0);
  697. }
  698. static noinline int
  699. tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
  700. struct extent_buffer *src, unsigned long dst_offset,
  701. unsigned long src_offset, int nr_items)
  702. {
  703. int ret = 0;
  704. struct tree_mod_elem **tm_list = NULL;
  705. struct tree_mod_elem **tm_list_add, **tm_list_rem;
  706. int i;
  707. int locked = 0;
  708. if (!tree_mod_need_log(fs_info, NULL))
  709. return 0;
  710. if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
  711. return 0;
  712. tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
  713. GFP_NOFS);
  714. if (!tm_list)
  715. return -ENOMEM;
  716. tm_list_add = tm_list;
  717. tm_list_rem = tm_list + nr_items;
  718. for (i = 0; i < nr_items; i++) {
  719. tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
  720. MOD_LOG_KEY_REMOVE, GFP_NOFS);
  721. if (!tm_list_rem[i]) {
  722. ret = -ENOMEM;
  723. goto free_tms;
  724. }
  725. tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
  726. MOD_LOG_KEY_ADD, GFP_NOFS);
  727. if (!tm_list_add[i]) {
  728. ret = -ENOMEM;
  729. goto free_tms;
  730. }
  731. }
  732. if (tree_mod_dont_log(fs_info, NULL))
  733. goto free_tms;
  734. locked = 1;
  735. for (i = 0; i < nr_items; i++) {
  736. ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
  737. if (ret)
  738. goto free_tms;
  739. ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
  740. if (ret)
  741. goto free_tms;
  742. }
  743. tree_mod_log_write_unlock(fs_info);
  744. kfree(tm_list);
  745. return 0;
  746. free_tms:
  747. for (i = 0; i < nr_items * 2; i++) {
  748. if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
  749. rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
  750. kfree(tm_list[i]);
  751. }
  752. if (locked)
  753. tree_mod_log_write_unlock(fs_info);
  754. kfree(tm_list);
  755. return ret;
  756. }
  757. static inline void
  758. tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
  759. int dst_offset, int src_offset, int nr_items)
  760. {
  761. int ret;
  762. ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
  763. nr_items);
  764. BUG_ON(ret < 0);
  765. }
  766. static noinline void
  767. tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
  768. struct extent_buffer *eb, int slot, int atomic)
  769. {
  770. int ret;
  771. ret = tree_mod_log_insert_key(fs_info, eb, slot,
  772. MOD_LOG_KEY_REPLACE,
  773. atomic ? GFP_ATOMIC : GFP_NOFS);
  774. BUG_ON(ret < 0);
  775. }
  776. static noinline int
  777. tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
  778. {
  779. struct tree_mod_elem **tm_list = NULL;
  780. int nritems = 0;
  781. int i;
  782. int ret = 0;
  783. if (btrfs_header_level(eb) == 0)
  784. return 0;
  785. if (!tree_mod_need_log(fs_info, NULL))
  786. return 0;
  787. nritems = btrfs_header_nritems(eb);
  788. tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
  789. if (!tm_list)
  790. return -ENOMEM;
  791. for (i = 0; i < nritems; i++) {
  792. tm_list[i] = alloc_tree_mod_elem(eb, i,
  793. MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
  794. if (!tm_list[i]) {
  795. ret = -ENOMEM;
  796. goto free_tms;
  797. }
  798. }
  799. if (tree_mod_dont_log(fs_info, eb))
  800. goto free_tms;
  801. ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
  802. tree_mod_log_write_unlock(fs_info);
  803. if (ret)
  804. goto free_tms;
  805. kfree(tm_list);
  806. return 0;
  807. free_tms:
  808. for (i = 0; i < nritems; i++)
  809. kfree(tm_list[i]);
  810. kfree(tm_list);
  811. return ret;
  812. }
  813. static noinline void
  814. tree_mod_log_set_root_pointer(struct btrfs_root *root,
  815. struct extent_buffer *new_root_node,
  816. int log_removal)
  817. {
  818. int ret;
  819. ret = tree_mod_log_insert_root(root->fs_info, root->node,
  820. new_root_node, log_removal);
  821. BUG_ON(ret < 0);
  822. }
  823. /*
  824. * check if the tree block can be shared by multiple trees
  825. */
  826. int btrfs_block_can_be_shared(struct btrfs_root *root,
  827. struct extent_buffer *buf)
  828. {
  829. /*
  830. * Tree blocks not in reference counted trees and tree roots
  831. * are never shared. If a block was allocated after the last
  832. * snapshot and the block was not allocated by tree relocation,
  833. * we know the block is not shared.
  834. */
  835. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  836. buf != root->node && buf != root->commit_root &&
  837. (btrfs_header_generation(buf) <=
  838. btrfs_root_last_snapshot(&root->root_item) ||
  839. btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
  840. return 1;
  841. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  842. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  843. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  844. return 1;
  845. #endif
  846. return 0;
  847. }
  848. static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
  849. struct btrfs_root *root,
  850. struct extent_buffer *buf,
  851. struct extent_buffer *cow,
  852. int *last_ref)
  853. {
  854. struct btrfs_fs_info *fs_info = root->fs_info;
  855. u64 refs;
  856. u64 owner;
  857. u64 flags;
  858. u64 new_flags = 0;
  859. int ret;
  860. /*
  861. * Backrefs update rules:
  862. *
  863. * Always use full backrefs for extent pointers in tree block
  864. * allocated by tree relocation.
  865. *
  866. * If a shared tree block is no longer referenced by its owner
  867. * tree (btrfs_header_owner(buf) == root->root_key.objectid),
  868. * use full backrefs for extent pointers in tree block.
  869. *
  870. * If a tree block is been relocating
  871. * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
  872. * use full backrefs for extent pointers in tree block.
  873. * The reason for this is some operations (such as drop tree)
  874. * are only allowed for blocks use full backrefs.
  875. */
  876. if (btrfs_block_can_be_shared(root, buf)) {
  877. ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
  878. btrfs_header_level(buf), 1,
  879. &refs, &flags);
  880. if (ret)
  881. return ret;
  882. if (refs == 0) {
  883. ret = -EROFS;
  884. btrfs_handle_fs_error(fs_info, ret, NULL);
  885. return ret;
  886. }
  887. } else {
  888. refs = 1;
  889. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  890. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  891. flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  892. else
  893. flags = 0;
  894. }
  895. owner = btrfs_header_owner(buf);
  896. BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
  897. !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  898. if (refs > 1) {
  899. if ((owner == root->root_key.objectid ||
  900. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
  901. !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
  902. ret = btrfs_inc_ref(trans, root, buf, 1);
  903. if (ret)
  904. return ret;
  905. if (root->root_key.objectid ==
  906. BTRFS_TREE_RELOC_OBJECTID) {
  907. ret = btrfs_dec_ref(trans, root, buf, 0);
  908. if (ret)
  909. return ret;
  910. ret = btrfs_inc_ref(trans, root, cow, 1);
  911. if (ret)
  912. return ret;
  913. }
  914. new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  915. } else {
  916. if (root->root_key.objectid ==
  917. BTRFS_TREE_RELOC_OBJECTID)
  918. ret = btrfs_inc_ref(trans, root, cow, 1);
  919. else
  920. ret = btrfs_inc_ref(trans, root, cow, 0);
  921. if (ret)
  922. return ret;
  923. }
  924. if (new_flags != 0) {
  925. int level = btrfs_header_level(buf);
  926. ret = btrfs_set_disk_extent_flags(trans, fs_info,
  927. buf->start,
  928. buf->len,
  929. new_flags, level, 0);
  930. if (ret)
  931. return ret;
  932. }
  933. } else {
  934. if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  935. if (root->root_key.objectid ==
  936. BTRFS_TREE_RELOC_OBJECTID)
  937. ret = btrfs_inc_ref(trans, root, cow, 1);
  938. else
  939. ret = btrfs_inc_ref(trans, root, cow, 0);
  940. if (ret)
  941. return ret;
  942. ret = btrfs_dec_ref(trans, root, buf, 1);
  943. if (ret)
  944. return ret;
  945. }
  946. clean_tree_block(fs_info, buf);
  947. *last_ref = 1;
  948. }
  949. return 0;
  950. }
  951. /*
  952. * does the dirty work in cow of a single block. The parent block (if
  953. * supplied) is updated to point to the new cow copy. The new buffer is marked
  954. * dirty and returned locked. If you modify the block it needs to be marked
  955. * dirty again.
  956. *
  957. * search_start -- an allocation hint for the new block
  958. *
  959. * empty_size -- a hint that you plan on doing more cow. This is the size in
  960. * bytes the allocator should try to find free next to the block it returns.
  961. * This is just a hint and may be ignored by the allocator.
  962. */
  963. static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
  964. struct btrfs_root *root,
  965. struct extent_buffer *buf,
  966. struct extent_buffer *parent, int parent_slot,
  967. struct extent_buffer **cow_ret,
  968. u64 search_start, u64 empty_size)
  969. {
  970. struct btrfs_fs_info *fs_info = root->fs_info;
  971. struct btrfs_disk_key disk_key;
  972. struct extent_buffer *cow;
  973. int level, ret;
  974. int last_ref = 0;
  975. int unlock_orig = 0;
  976. u64 parent_start = 0;
  977. if (*cow_ret == buf)
  978. unlock_orig = 1;
  979. btrfs_assert_tree_locked(buf);
  980. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  981. trans->transid != fs_info->running_transaction->transid);
  982. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  983. trans->transid != root->last_trans);
  984. level = btrfs_header_level(buf);
  985. if (level == 0)
  986. btrfs_item_key(buf, &disk_key, 0);
  987. else
  988. btrfs_node_key(buf, &disk_key, 0);
  989. if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
  990. parent_start = parent->start;
  991. cow = btrfs_alloc_tree_block(trans, root, parent_start,
  992. root->root_key.objectid, &disk_key, level,
  993. search_start, empty_size);
  994. if (IS_ERR(cow))
  995. return PTR_ERR(cow);
  996. /* cow is set to blocking by btrfs_init_new_buffer */
  997. copy_extent_buffer_full(cow, buf);
  998. btrfs_set_header_bytenr(cow, cow->start);
  999. btrfs_set_header_generation(cow, trans->transid);
  1000. btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
  1001. btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
  1002. BTRFS_HEADER_FLAG_RELOC);
  1003. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  1004. btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
  1005. else
  1006. btrfs_set_header_owner(cow, root->root_key.objectid);
  1007. write_extent_buffer_fsid(cow, fs_info->fsid);
  1008. ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
  1009. if (ret) {
  1010. btrfs_abort_transaction(trans, ret);
  1011. return ret;
  1012. }
  1013. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
  1014. ret = btrfs_reloc_cow_block(trans, root, buf, cow);
  1015. if (ret) {
  1016. btrfs_abort_transaction(trans, ret);
  1017. return ret;
  1018. }
  1019. }
  1020. if (buf == root->node) {
  1021. WARN_ON(parent && parent != buf);
  1022. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  1023. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  1024. parent_start = buf->start;
  1025. extent_buffer_get(cow);
  1026. tree_mod_log_set_root_pointer(root, cow, 1);
  1027. rcu_assign_pointer(root->node, cow);
  1028. btrfs_free_tree_block(trans, root, buf, parent_start,
  1029. last_ref);
  1030. free_extent_buffer(buf);
  1031. add_root_to_dirty_list(root);
  1032. } else {
  1033. WARN_ON(trans->transid != btrfs_header_generation(parent));
  1034. tree_mod_log_insert_key(fs_info, parent, parent_slot,
  1035. MOD_LOG_KEY_REPLACE, GFP_NOFS);
  1036. btrfs_set_node_blockptr(parent, parent_slot,
  1037. cow->start);
  1038. btrfs_set_node_ptr_generation(parent, parent_slot,
  1039. trans->transid);
  1040. btrfs_mark_buffer_dirty(parent);
  1041. if (last_ref) {
  1042. ret = tree_mod_log_free_eb(fs_info, buf);
  1043. if (ret) {
  1044. btrfs_abort_transaction(trans, ret);
  1045. return ret;
  1046. }
  1047. }
  1048. btrfs_free_tree_block(trans, root, buf, parent_start,
  1049. last_ref);
  1050. }
  1051. if (unlock_orig)
  1052. btrfs_tree_unlock(buf);
  1053. free_extent_buffer_stale(buf);
  1054. btrfs_mark_buffer_dirty(cow);
  1055. *cow_ret = cow;
  1056. return 0;
  1057. }
  1058. /*
  1059. * returns the logical address of the oldest predecessor of the given root.
  1060. * entries older than time_seq are ignored.
  1061. */
  1062. static struct tree_mod_elem *
  1063. __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
  1064. struct extent_buffer *eb_root, u64 time_seq)
  1065. {
  1066. struct tree_mod_elem *tm;
  1067. struct tree_mod_elem *found = NULL;
  1068. u64 root_logical = eb_root->start;
  1069. int looped = 0;
  1070. if (!time_seq)
  1071. return NULL;
  1072. /*
  1073. * the very last operation that's logged for a root is the
  1074. * replacement operation (if it is replaced at all). this has
  1075. * the logical address of the *new* root, making it the very
  1076. * first operation that's logged for this root.
  1077. */
  1078. while (1) {
  1079. tm = tree_mod_log_search_oldest(fs_info, root_logical,
  1080. time_seq);
  1081. if (!looped && !tm)
  1082. return NULL;
  1083. /*
  1084. * if there are no tree operation for the oldest root, we simply
  1085. * return it. this should only happen if that (old) root is at
  1086. * level 0.
  1087. */
  1088. if (!tm)
  1089. break;
  1090. /*
  1091. * if there's an operation that's not a root replacement, we
  1092. * found the oldest version of our root. normally, we'll find a
  1093. * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
  1094. */
  1095. if (tm->op != MOD_LOG_ROOT_REPLACE)
  1096. break;
  1097. found = tm;
  1098. root_logical = tm->old_root.logical;
  1099. looped = 1;
  1100. }
  1101. /* if there's no old root to return, return what we found instead */
  1102. if (!found)
  1103. found = tm;
  1104. return found;
  1105. }
  1106. /*
  1107. * tm is a pointer to the first operation to rewind within eb. then, all
  1108. * previous operations will be rewound (until we reach something older than
  1109. * time_seq).
  1110. */
  1111. static void
  1112. __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
  1113. u64 time_seq, struct tree_mod_elem *first_tm)
  1114. {
  1115. u32 n;
  1116. struct rb_node *next;
  1117. struct tree_mod_elem *tm = first_tm;
  1118. unsigned long o_dst;
  1119. unsigned long o_src;
  1120. unsigned long p_size = sizeof(struct btrfs_key_ptr);
  1121. n = btrfs_header_nritems(eb);
  1122. tree_mod_log_read_lock(fs_info);
  1123. while (tm && tm->seq >= time_seq) {
  1124. /*
  1125. * all the operations are recorded with the operator used for
  1126. * the modification. as we're going backwards, we do the
  1127. * opposite of each operation here.
  1128. */
  1129. switch (tm->op) {
  1130. case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
  1131. BUG_ON(tm->slot < n);
  1132. /* Fallthrough */
  1133. case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
  1134. case MOD_LOG_KEY_REMOVE:
  1135. btrfs_set_node_key(eb, &tm->key, tm->slot);
  1136. btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
  1137. btrfs_set_node_ptr_generation(eb, tm->slot,
  1138. tm->generation);
  1139. n++;
  1140. break;
  1141. case MOD_LOG_KEY_REPLACE:
  1142. BUG_ON(tm->slot >= n);
  1143. btrfs_set_node_key(eb, &tm->key, tm->slot);
  1144. btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
  1145. btrfs_set_node_ptr_generation(eb, tm->slot,
  1146. tm->generation);
  1147. break;
  1148. case MOD_LOG_KEY_ADD:
  1149. /* if a move operation is needed it's in the log */
  1150. n--;
  1151. break;
  1152. case MOD_LOG_MOVE_KEYS:
  1153. o_dst = btrfs_node_key_ptr_offset(tm->slot);
  1154. o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
  1155. memmove_extent_buffer(eb, o_dst, o_src,
  1156. tm->move.nr_items * p_size);
  1157. break;
  1158. case MOD_LOG_ROOT_REPLACE:
  1159. /*
  1160. * this operation is special. for roots, this must be
  1161. * handled explicitly before rewinding.
  1162. * for non-roots, this operation may exist if the node
  1163. * was a root: root A -> child B; then A gets empty and
  1164. * B is promoted to the new root. in the mod log, we'll
  1165. * have a root-replace operation for B, a tree block
  1166. * that is no root. we simply ignore that operation.
  1167. */
  1168. break;
  1169. }
  1170. next = rb_next(&tm->node);
  1171. if (!next)
  1172. break;
  1173. tm = rb_entry(next, struct tree_mod_elem, node);
  1174. if (tm->logical != first_tm->logical)
  1175. break;
  1176. }
  1177. tree_mod_log_read_unlock(fs_info);
  1178. btrfs_set_header_nritems(eb, n);
  1179. }
  1180. /*
  1181. * Called with eb read locked. If the buffer cannot be rewound, the same buffer
  1182. * is returned. If rewind operations happen, a fresh buffer is returned. The
  1183. * returned buffer is always read-locked. If the returned buffer is not the
  1184. * input buffer, the lock on the input buffer is released and the input buffer
  1185. * is freed (its refcount is decremented).
  1186. */
  1187. static struct extent_buffer *
  1188. tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  1189. struct extent_buffer *eb, u64 time_seq)
  1190. {
  1191. struct extent_buffer *eb_rewin;
  1192. struct tree_mod_elem *tm;
  1193. if (!time_seq)
  1194. return eb;
  1195. if (btrfs_header_level(eb) == 0)
  1196. return eb;
  1197. tm = tree_mod_log_search(fs_info, eb->start, time_seq);
  1198. if (!tm)
  1199. return eb;
  1200. btrfs_set_path_blocking(path);
  1201. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1202. if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
  1203. BUG_ON(tm->slot != 0);
  1204. eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
  1205. if (!eb_rewin) {
  1206. btrfs_tree_read_unlock_blocking(eb);
  1207. free_extent_buffer(eb);
  1208. return NULL;
  1209. }
  1210. btrfs_set_header_bytenr(eb_rewin, eb->start);
  1211. btrfs_set_header_backref_rev(eb_rewin,
  1212. btrfs_header_backref_rev(eb));
  1213. btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
  1214. btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
  1215. } else {
  1216. eb_rewin = btrfs_clone_extent_buffer(eb);
  1217. if (!eb_rewin) {
  1218. btrfs_tree_read_unlock_blocking(eb);
  1219. free_extent_buffer(eb);
  1220. return NULL;
  1221. }
  1222. }
  1223. btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
  1224. btrfs_tree_read_unlock_blocking(eb);
  1225. free_extent_buffer(eb);
  1226. extent_buffer_get(eb_rewin);
  1227. btrfs_tree_read_lock(eb_rewin);
  1228. __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
  1229. WARN_ON(btrfs_header_nritems(eb_rewin) >
  1230. BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  1231. return eb_rewin;
  1232. }
  1233. /*
  1234. * get_old_root() rewinds the state of @root's root node to the given @time_seq
  1235. * value. If there are no changes, the current root->root_node is returned. If
  1236. * anything changed in between, there's a fresh buffer allocated on which the
  1237. * rewind operations are done. In any case, the returned buffer is read locked.
  1238. * Returns NULL on error (with no locks held).
  1239. */
  1240. static inline struct extent_buffer *
  1241. get_old_root(struct btrfs_root *root, u64 time_seq)
  1242. {
  1243. struct btrfs_fs_info *fs_info = root->fs_info;
  1244. struct tree_mod_elem *tm;
  1245. struct extent_buffer *eb = NULL;
  1246. struct extent_buffer *eb_root;
  1247. struct extent_buffer *old;
  1248. struct tree_mod_root *old_root = NULL;
  1249. u64 old_generation = 0;
  1250. u64 logical;
  1251. eb_root = btrfs_read_lock_root_node(root);
  1252. tm = __tree_mod_log_oldest_root(fs_info, eb_root, time_seq);
  1253. if (!tm)
  1254. return eb_root;
  1255. if (tm->op == MOD_LOG_ROOT_REPLACE) {
  1256. old_root = &tm->old_root;
  1257. old_generation = tm->generation;
  1258. logical = old_root->logical;
  1259. } else {
  1260. logical = eb_root->start;
  1261. }
  1262. tm = tree_mod_log_search(fs_info, logical, time_seq);
  1263. if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
  1264. btrfs_tree_read_unlock(eb_root);
  1265. free_extent_buffer(eb_root);
  1266. old = read_tree_block(fs_info, logical, 0);
  1267. if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
  1268. if (!IS_ERR(old))
  1269. free_extent_buffer(old);
  1270. btrfs_warn(fs_info,
  1271. "failed to read tree block %llu from get_old_root",
  1272. logical);
  1273. } else {
  1274. eb = btrfs_clone_extent_buffer(old);
  1275. free_extent_buffer(old);
  1276. }
  1277. } else if (old_root) {
  1278. btrfs_tree_read_unlock(eb_root);
  1279. free_extent_buffer(eb_root);
  1280. eb = alloc_dummy_extent_buffer(fs_info, logical);
  1281. } else {
  1282. btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
  1283. eb = btrfs_clone_extent_buffer(eb_root);
  1284. btrfs_tree_read_unlock_blocking(eb_root);
  1285. free_extent_buffer(eb_root);
  1286. }
  1287. if (!eb)
  1288. return NULL;
  1289. extent_buffer_get(eb);
  1290. btrfs_tree_read_lock(eb);
  1291. if (old_root) {
  1292. btrfs_set_header_bytenr(eb, eb->start);
  1293. btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
  1294. btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
  1295. btrfs_set_header_level(eb, old_root->level);
  1296. btrfs_set_header_generation(eb, old_generation);
  1297. }
  1298. if (tm)
  1299. __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
  1300. else
  1301. WARN_ON(btrfs_header_level(eb) != 0);
  1302. WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  1303. return eb;
  1304. }
  1305. int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
  1306. {
  1307. struct tree_mod_elem *tm;
  1308. int level;
  1309. struct extent_buffer *eb_root = btrfs_root_node(root);
  1310. tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
  1311. if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
  1312. level = tm->old_root.level;
  1313. } else {
  1314. level = btrfs_header_level(eb_root);
  1315. }
  1316. free_extent_buffer(eb_root);
  1317. return level;
  1318. }
  1319. static inline int should_cow_block(struct btrfs_trans_handle *trans,
  1320. struct btrfs_root *root,
  1321. struct extent_buffer *buf)
  1322. {
  1323. if (btrfs_is_testing(root->fs_info))
  1324. return 0;
  1325. /* ensure we can see the force_cow */
  1326. smp_rmb();
  1327. /*
  1328. * We do not need to cow a block if
  1329. * 1) this block is not created or changed in this transaction;
  1330. * 2) this block does not belong to TREE_RELOC tree;
  1331. * 3) the root is not forced COW.
  1332. *
  1333. * What is forced COW:
  1334. * when we create snapshot during committing the transaction,
  1335. * after we've finished coping src root, we must COW the shared
  1336. * block to ensure the metadata consistency.
  1337. */
  1338. if (btrfs_header_generation(buf) == trans->transid &&
  1339. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
  1340. !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
  1341. btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
  1342. !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
  1343. return 0;
  1344. return 1;
  1345. }
  1346. /*
  1347. * cows a single block, see __btrfs_cow_block for the real work.
  1348. * This version of it has extra checks so that a block isn't COWed more than
  1349. * once per transaction, as long as it hasn't been written yet
  1350. */
  1351. noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
  1352. struct btrfs_root *root, struct extent_buffer *buf,
  1353. struct extent_buffer *parent, int parent_slot,
  1354. struct extent_buffer **cow_ret)
  1355. {
  1356. struct btrfs_fs_info *fs_info = root->fs_info;
  1357. u64 search_start;
  1358. int ret;
  1359. if (trans->transaction != fs_info->running_transaction)
  1360. WARN(1, KERN_CRIT "trans %llu running %llu\n",
  1361. trans->transid,
  1362. fs_info->running_transaction->transid);
  1363. if (trans->transid != fs_info->generation)
  1364. WARN(1, KERN_CRIT "trans %llu running %llu\n",
  1365. trans->transid, fs_info->generation);
  1366. if (!should_cow_block(trans, root, buf)) {
  1367. trans->dirty = true;
  1368. *cow_ret = buf;
  1369. return 0;
  1370. }
  1371. search_start = buf->start & ~((u64)SZ_1G - 1);
  1372. if (parent)
  1373. btrfs_set_lock_blocking(parent);
  1374. btrfs_set_lock_blocking(buf);
  1375. ret = __btrfs_cow_block(trans, root, buf, parent,
  1376. parent_slot, cow_ret, search_start, 0);
  1377. trace_btrfs_cow_block(root, buf, *cow_ret);
  1378. return ret;
  1379. }
  1380. /*
  1381. * helper function for defrag to decide if two blocks pointed to by a
  1382. * node are actually close by
  1383. */
  1384. static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
  1385. {
  1386. if (blocknr < other && other - (blocknr + blocksize) < 32768)
  1387. return 1;
  1388. if (blocknr > other && blocknr - (other + blocksize) < 32768)
  1389. return 1;
  1390. return 0;
  1391. }
  1392. /*
  1393. * compare two keys in a memcmp fashion
  1394. */
  1395. static int comp_keys(const struct btrfs_disk_key *disk,
  1396. const struct btrfs_key *k2)
  1397. {
  1398. struct btrfs_key k1;
  1399. btrfs_disk_key_to_cpu(&k1, disk);
  1400. return btrfs_comp_cpu_keys(&k1, k2);
  1401. }
  1402. /*
  1403. * same as comp_keys only with two btrfs_key's
  1404. */
  1405. int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
  1406. {
  1407. if (k1->objectid > k2->objectid)
  1408. return 1;
  1409. if (k1->objectid < k2->objectid)
  1410. return -1;
  1411. if (k1->type > k2->type)
  1412. return 1;
  1413. if (k1->type < k2->type)
  1414. return -1;
  1415. if (k1->offset > k2->offset)
  1416. return 1;
  1417. if (k1->offset < k2->offset)
  1418. return -1;
  1419. return 0;
  1420. }
  1421. /*
  1422. * this is used by the defrag code to go through all the
  1423. * leaves pointed to by a node and reallocate them so that
  1424. * disk order is close to key order
  1425. */
  1426. int btrfs_realloc_node(struct btrfs_trans_handle *trans,
  1427. struct btrfs_root *root, struct extent_buffer *parent,
  1428. int start_slot, u64 *last_ret,
  1429. struct btrfs_key *progress)
  1430. {
  1431. struct btrfs_fs_info *fs_info = root->fs_info;
  1432. struct extent_buffer *cur;
  1433. u64 blocknr;
  1434. u64 gen;
  1435. u64 search_start = *last_ret;
  1436. u64 last_block = 0;
  1437. u64 other;
  1438. u32 parent_nritems;
  1439. int end_slot;
  1440. int i;
  1441. int err = 0;
  1442. int parent_level;
  1443. int uptodate;
  1444. u32 blocksize;
  1445. int progress_passed = 0;
  1446. struct btrfs_disk_key disk_key;
  1447. parent_level = btrfs_header_level(parent);
  1448. WARN_ON(trans->transaction != fs_info->running_transaction);
  1449. WARN_ON(trans->transid != fs_info->generation);
  1450. parent_nritems = btrfs_header_nritems(parent);
  1451. blocksize = fs_info->nodesize;
  1452. end_slot = parent_nritems - 1;
  1453. if (parent_nritems <= 1)
  1454. return 0;
  1455. btrfs_set_lock_blocking(parent);
  1456. for (i = start_slot; i <= end_slot; i++) {
  1457. int close = 1;
  1458. btrfs_node_key(parent, &disk_key, i);
  1459. if (!progress_passed && comp_keys(&disk_key, progress) < 0)
  1460. continue;
  1461. progress_passed = 1;
  1462. blocknr = btrfs_node_blockptr(parent, i);
  1463. gen = btrfs_node_ptr_generation(parent, i);
  1464. if (last_block == 0)
  1465. last_block = blocknr;
  1466. if (i > 0) {
  1467. other = btrfs_node_blockptr(parent, i - 1);
  1468. close = close_blocks(blocknr, other, blocksize);
  1469. }
  1470. if (!close && i < end_slot) {
  1471. other = btrfs_node_blockptr(parent, i + 1);
  1472. close = close_blocks(blocknr, other, blocksize);
  1473. }
  1474. if (close) {
  1475. last_block = blocknr;
  1476. continue;
  1477. }
  1478. cur = find_extent_buffer(fs_info, blocknr);
  1479. if (cur)
  1480. uptodate = btrfs_buffer_uptodate(cur, gen, 0);
  1481. else
  1482. uptodate = 0;
  1483. if (!cur || !uptodate) {
  1484. if (!cur) {
  1485. cur = read_tree_block(fs_info, blocknr, gen);
  1486. if (IS_ERR(cur)) {
  1487. return PTR_ERR(cur);
  1488. } else if (!extent_buffer_uptodate(cur)) {
  1489. free_extent_buffer(cur);
  1490. return -EIO;
  1491. }
  1492. } else if (!uptodate) {
  1493. err = btrfs_read_buffer(cur, gen);
  1494. if (err) {
  1495. free_extent_buffer(cur);
  1496. return err;
  1497. }
  1498. }
  1499. }
  1500. if (search_start == 0)
  1501. search_start = last_block;
  1502. btrfs_tree_lock(cur);
  1503. btrfs_set_lock_blocking(cur);
  1504. err = __btrfs_cow_block(trans, root, cur, parent, i,
  1505. &cur, search_start,
  1506. min(16 * blocksize,
  1507. (end_slot - i) * blocksize));
  1508. if (err) {
  1509. btrfs_tree_unlock(cur);
  1510. free_extent_buffer(cur);
  1511. break;
  1512. }
  1513. search_start = cur->start;
  1514. last_block = cur->start;
  1515. *last_ret = search_start;
  1516. btrfs_tree_unlock(cur);
  1517. free_extent_buffer(cur);
  1518. }
  1519. return err;
  1520. }
  1521. /*
  1522. * search for key in the extent_buffer. The items start at offset p,
  1523. * and they are item_size apart. There are 'max' items in p.
  1524. *
  1525. * the slot in the array is returned via slot, and it points to
  1526. * the place where you would insert key if it is not found in
  1527. * the array.
  1528. *
  1529. * slot may point to max if the key is bigger than all of the keys
  1530. */
  1531. static noinline int generic_bin_search(struct extent_buffer *eb,
  1532. unsigned long p, int item_size,
  1533. const struct btrfs_key *key,
  1534. int max, int *slot)
  1535. {
  1536. int low = 0;
  1537. int high = max;
  1538. int mid;
  1539. int ret;
  1540. struct btrfs_disk_key *tmp = NULL;
  1541. struct btrfs_disk_key unaligned;
  1542. unsigned long offset;
  1543. char *kaddr = NULL;
  1544. unsigned long map_start = 0;
  1545. unsigned long map_len = 0;
  1546. int err;
  1547. if (low > high) {
  1548. btrfs_err(eb->fs_info,
  1549. "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
  1550. __func__, low, high, eb->start,
  1551. btrfs_header_owner(eb), btrfs_header_level(eb));
  1552. return -EINVAL;
  1553. }
  1554. while (low < high) {
  1555. mid = (low + high) / 2;
  1556. offset = p + mid * item_size;
  1557. if (!kaddr || offset < map_start ||
  1558. (offset + sizeof(struct btrfs_disk_key)) >
  1559. map_start + map_len) {
  1560. err = map_private_extent_buffer(eb, offset,
  1561. sizeof(struct btrfs_disk_key),
  1562. &kaddr, &map_start, &map_len);
  1563. if (!err) {
  1564. tmp = (struct btrfs_disk_key *)(kaddr + offset -
  1565. map_start);
  1566. } else if (err == 1) {
  1567. read_extent_buffer(eb, &unaligned,
  1568. offset, sizeof(unaligned));
  1569. tmp = &unaligned;
  1570. } else {
  1571. return err;
  1572. }
  1573. } else {
  1574. tmp = (struct btrfs_disk_key *)(kaddr + offset -
  1575. map_start);
  1576. }
  1577. ret = comp_keys(tmp, key);
  1578. if (ret < 0)
  1579. low = mid + 1;
  1580. else if (ret > 0)
  1581. high = mid;
  1582. else {
  1583. *slot = mid;
  1584. return 0;
  1585. }
  1586. }
  1587. *slot = low;
  1588. return 1;
  1589. }
  1590. /*
  1591. * simple bin_search frontend that does the right thing for
  1592. * leaves vs nodes
  1593. */
  1594. int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
  1595. int level, int *slot)
  1596. {
  1597. if (level == 0)
  1598. return generic_bin_search(eb,
  1599. offsetof(struct btrfs_leaf, items),
  1600. sizeof(struct btrfs_item),
  1601. key, btrfs_header_nritems(eb),
  1602. slot);
  1603. else
  1604. return generic_bin_search(eb,
  1605. offsetof(struct btrfs_node, ptrs),
  1606. sizeof(struct btrfs_key_ptr),
  1607. key, btrfs_header_nritems(eb),
  1608. slot);
  1609. }
  1610. static void root_add_used(struct btrfs_root *root, u32 size)
  1611. {
  1612. spin_lock(&root->accounting_lock);
  1613. btrfs_set_root_used(&root->root_item,
  1614. btrfs_root_used(&root->root_item) + size);
  1615. spin_unlock(&root->accounting_lock);
  1616. }
  1617. static void root_sub_used(struct btrfs_root *root, u32 size)
  1618. {
  1619. spin_lock(&root->accounting_lock);
  1620. btrfs_set_root_used(&root->root_item,
  1621. btrfs_root_used(&root->root_item) - size);
  1622. spin_unlock(&root->accounting_lock);
  1623. }
  1624. /* given a node and slot number, this reads the blocks it points to. The
  1625. * extent buffer is returned with a reference taken (but unlocked).
  1626. */
  1627. static noinline struct extent_buffer *
  1628. read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent,
  1629. int slot)
  1630. {
  1631. int level = btrfs_header_level(parent);
  1632. struct extent_buffer *eb;
  1633. if (slot < 0 || slot >= btrfs_header_nritems(parent))
  1634. return ERR_PTR(-ENOENT);
  1635. BUG_ON(level == 0);
  1636. eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
  1637. btrfs_node_ptr_generation(parent, slot));
  1638. if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
  1639. free_extent_buffer(eb);
  1640. eb = ERR_PTR(-EIO);
  1641. }
  1642. return eb;
  1643. }
  1644. /*
  1645. * node level balancing, used to make sure nodes are in proper order for
  1646. * item deletion. We balance from the top down, so we have to make sure
  1647. * that a deletion won't leave an node completely empty later on.
  1648. */
  1649. static noinline int balance_level(struct btrfs_trans_handle *trans,
  1650. struct btrfs_root *root,
  1651. struct btrfs_path *path, int level)
  1652. {
  1653. struct btrfs_fs_info *fs_info = root->fs_info;
  1654. struct extent_buffer *right = NULL;
  1655. struct extent_buffer *mid;
  1656. struct extent_buffer *left = NULL;
  1657. struct extent_buffer *parent = NULL;
  1658. int ret = 0;
  1659. int wret;
  1660. int pslot;
  1661. int orig_slot = path->slots[level];
  1662. u64 orig_ptr;
  1663. if (level == 0)
  1664. return 0;
  1665. mid = path->nodes[level];
  1666. WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
  1667. path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
  1668. WARN_ON(btrfs_header_generation(mid) != trans->transid);
  1669. orig_ptr = btrfs_node_blockptr(mid, orig_slot);
  1670. if (level < BTRFS_MAX_LEVEL - 1) {
  1671. parent = path->nodes[level + 1];
  1672. pslot = path->slots[level + 1];
  1673. }
  1674. /*
  1675. * deal with the case where there is only one pointer in the root
  1676. * by promoting the node below to a root
  1677. */
  1678. if (!parent) {
  1679. struct extent_buffer *child;
  1680. if (btrfs_header_nritems(mid) != 1)
  1681. return 0;
  1682. /* promote the child to a root */
  1683. child = read_node_slot(fs_info, mid, 0);
  1684. if (IS_ERR(child)) {
  1685. ret = PTR_ERR(child);
  1686. btrfs_handle_fs_error(fs_info, ret, NULL);
  1687. goto enospc;
  1688. }
  1689. btrfs_tree_lock(child);
  1690. btrfs_set_lock_blocking(child);
  1691. ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
  1692. if (ret) {
  1693. btrfs_tree_unlock(child);
  1694. free_extent_buffer(child);
  1695. goto enospc;
  1696. }
  1697. tree_mod_log_set_root_pointer(root, child, 1);
  1698. rcu_assign_pointer(root->node, child);
  1699. add_root_to_dirty_list(root);
  1700. btrfs_tree_unlock(child);
  1701. path->locks[level] = 0;
  1702. path->nodes[level] = NULL;
  1703. clean_tree_block(fs_info, mid);
  1704. btrfs_tree_unlock(mid);
  1705. /* once for the path */
  1706. free_extent_buffer(mid);
  1707. root_sub_used(root, mid->len);
  1708. btrfs_free_tree_block(trans, root, mid, 0, 1);
  1709. /* once for the root ptr */
  1710. free_extent_buffer_stale(mid);
  1711. return 0;
  1712. }
  1713. if (btrfs_header_nritems(mid) >
  1714. BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
  1715. return 0;
  1716. left = read_node_slot(fs_info, parent, pslot - 1);
  1717. if (IS_ERR(left))
  1718. left = NULL;
  1719. if (left) {
  1720. btrfs_tree_lock(left);
  1721. btrfs_set_lock_blocking(left);
  1722. wret = btrfs_cow_block(trans, root, left,
  1723. parent, pslot - 1, &left);
  1724. if (wret) {
  1725. ret = wret;
  1726. goto enospc;
  1727. }
  1728. }
  1729. right = read_node_slot(fs_info, parent, pslot + 1);
  1730. if (IS_ERR(right))
  1731. right = NULL;
  1732. if (right) {
  1733. btrfs_tree_lock(right);
  1734. btrfs_set_lock_blocking(right);
  1735. wret = btrfs_cow_block(trans, root, right,
  1736. parent, pslot + 1, &right);
  1737. if (wret) {
  1738. ret = wret;
  1739. goto enospc;
  1740. }
  1741. }
  1742. /* first, try to make some room in the middle buffer */
  1743. if (left) {
  1744. orig_slot += btrfs_header_nritems(left);
  1745. wret = push_node_left(trans, fs_info, left, mid, 1);
  1746. if (wret < 0)
  1747. ret = wret;
  1748. }
  1749. /*
  1750. * then try to empty the right most buffer into the middle
  1751. */
  1752. if (right) {
  1753. wret = push_node_left(trans, fs_info, mid, right, 1);
  1754. if (wret < 0 && wret != -ENOSPC)
  1755. ret = wret;
  1756. if (btrfs_header_nritems(right) == 0) {
  1757. clean_tree_block(fs_info, right);
  1758. btrfs_tree_unlock(right);
  1759. del_ptr(root, path, level + 1, pslot + 1);
  1760. root_sub_used(root, right->len);
  1761. btrfs_free_tree_block(trans, root, right, 0, 1);
  1762. free_extent_buffer_stale(right);
  1763. right = NULL;
  1764. } else {
  1765. struct btrfs_disk_key right_key;
  1766. btrfs_node_key(right, &right_key, 0);
  1767. tree_mod_log_set_node_key(fs_info, parent,
  1768. pslot + 1, 0);
  1769. btrfs_set_node_key(parent, &right_key, pslot + 1);
  1770. btrfs_mark_buffer_dirty(parent);
  1771. }
  1772. }
  1773. if (btrfs_header_nritems(mid) == 1) {
  1774. /*
  1775. * we're not allowed to leave a node with one item in the
  1776. * tree during a delete. A deletion from lower in the tree
  1777. * could try to delete the only pointer in this node.
  1778. * So, pull some keys from the left.
  1779. * There has to be a left pointer at this point because
  1780. * otherwise we would have pulled some pointers from the
  1781. * right
  1782. */
  1783. if (!left) {
  1784. ret = -EROFS;
  1785. btrfs_handle_fs_error(fs_info, ret, NULL);
  1786. goto enospc;
  1787. }
  1788. wret = balance_node_right(trans, fs_info, mid, left);
  1789. if (wret < 0) {
  1790. ret = wret;
  1791. goto enospc;
  1792. }
  1793. if (wret == 1) {
  1794. wret = push_node_left(trans, fs_info, left, mid, 1);
  1795. if (wret < 0)
  1796. ret = wret;
  1797. }
  1798. BUG_ON(wret == 1);
  1799. }
  1800. if (btrfs_header_nritems(mid) == 0) {
  1801. clean_tree_block(fs_info, mid);
  1802. btrfs_tree_unlock(mid);
  1803. del_ptr(root, path, level + 1, pslot);
  1804. root_sub_used(root, mid->len);
  1805. btrfs_free_tree_block(trans, root, mid, 0, 1);
  1806. free_extent_buffer_stale(mid);
  1807. mid = NULL;
  1808. } else {
  1809. /* update the parent key to reflect our changes */
  1810. struct btrfs_disk_key mid_key;
  1811. btrfs_node_key(mid, &mid_key, 0);
  1812. tree_mod_log_set_node_key(fs_info, parent, pslot, 0);
  1813. btrfs_set_node_key(parent, &mid_key, pslot);
  1814. btrfs_mark_buffer_dirty(parent);
  1815. }
  1816. /* update the path */
  1817. if (left) {
  1818. if (btrfs_header_nritems(left) > orig_slot) {
  1819. extent_buffer_get(left);
  1820. /* left was locked after cow */
  1821. path->nodes[level] = left;
  1822. path->slots[level + 1] -= 1;
  1823. path->slots[level] = orig_slot;
  1824. if (mid) {
  1825. btrfs_tree_unlock(mid);
  1826. free_extent_buffer(mid);
  1827. }
  1828. } else {
  1829. orig_slot -= btrfs_header_nritems(left);
  1830. path->slots[level] = orig_slot;
  1831. }
  1832. }
  1833. /* double check we haven't messed things up */
  1834. if (orig_ptr !=
  1835. btrfs_node_blockptr(path->nodes[level], path->slots[level]))
  1836. BUG();
  1837. enospc:
  1838. if (right) {
  1839. btrfs_tree_unlock(right);
  1840. free_extent_buffer(right);
  1841. }
  1842. if (left) {
  1843. if (path->nodes[level] != left)
  1844. btrfs_tree_unlock(left);
  1845. free_extent_buffer(left);
  1846. }
  1847. return ret;
  1848. }
  1849. /* Node balancing for insertion. Here we only split or push nodes around
  1850. * when they are completely full. This is also done top down, so we
  1851. * have to be pessimistic.
  1852. */
  1853. static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
  1854. struct btrfs_root *root,
  1855. struct btrfs_path *path, int level)
  1856. {
  1857. struct btrfs_fs_info *fs_info = root->fs_info;
  1858. struct extent_buffer *right = NULL;
  1859. struct extent_buffer *mid;
  1860. struct extent_buffer *left = NULL;
  1861. struct extent_buffer *parent = NULL;
  1862. int ret = 0;
  1863. int wret;
  1864. int pslot;
  1865. int orig_slot = path->slots[level];
  1866. if (level == 0)
  1867. return 1;
  1868. mid = path->nodes[level];
  1869. WARN_ON(btrfs_header_generation(mid) != trans->transid);
  1870. if (level < BTRFS_MAX_LEVEL - 1) {
  1871. parent = path->nodes[level + 1];
  1872. pslot = path->slots[level + 1];
  1873. }
  1874. if (!parent)
  1875. return 1;
  1876. left = read_node_slot(fs_info, parent, pslot - 1);
  1877. if (IS_ERR(left))
  1878. left = NULL;
  1879. /* first, try to make some room in the middle buffer */
  1880. if (left) {
  1881. u32 left_nr;
  1882. btrfs_tree_lock(left);
  1883. btrfs_set_lock_blocking(left);
  1884. left_nr = btrfs_header_nritems(left);
  1885. if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
  1886. wret = 1;
  1887. } else {
  1888. ret = btrfs_cow_block(trans, root, left, parent,
  1889. pslot - 1, &left);
  1890. if (ret)
  1891. wret = 1;
  1892. else {
  1893. wret = push_node_left(trans, fs_info,
  1894. left, mid, 0);
  1895. }
  1896. }
  1897. if (wret < 0)
  1898. ret = wret;
  1899. if (wret == 0) {
  1900. struct btrfs_disk_key disk_key;
  1901. orig_slot += left_nr;
  1902. btrfs_node_key(mid, &disk_key, 0);
  1903. tree_mod_log_set_node_key(fs_info, parent, pslot, 0);
  1904. btrfs_set_node_key(parent, &disk_key, pslot);
  1905. btrfs_mark_buffer_dirty(parent);
  1906. if (btrfs_header_nritems(left) > orig_slot) {
  1907. path->nodes[level] = left;
  1908. path->slots[level + 1] -= 1;
  1909. path->slots[level] = orig_slot;
  1910. btrfs_tree_unlock(mid);
  1911. free_extent_buffer(mid);
  1912. } else {
  1913. orig_slot -=
  1914. btrfs_header_nritems(left);
  1915. path->slots[level] = orig_slot;
  1916. btrfs_tree_unlock(left);
  1917. free_extent_buffer(left);
  1918. }
  1919. return 0;
  1920. }
  1921. btrfs_tree_unlock(left);
  1922. free_extent_buffer(left);
  1923. }
  1924. right = read_node_slot(fs_info, parent, pslot + 1);
  1925. if (IS_ERR(right))
  1926. right = NULL;
  1927. /*
  1928. * then try to empty the right most buffer into the middle
  1929. */
  1930. if (right) {
  1931. u32 right_nr;
  1932. btrfs_tree_lock(right);
  1933. btrfs_set_lock_blocking(right);
  1934. right_nr = btrfs_header_nritems(right);
  1935. if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
  1936. wret = 1;
  1937. } else {
  1938. ret = btrfs_cow_block(trans, root, right,
  1939. parent, pslot + 1,
  1940. &right);
  1941. if (ret)
  1942. wret = 1;
  1943. else {
  1944. wret = balance_node_right(trans, fs_info,
  1945. right, mid);
  1946. }
  1947. }
  1948. if (wret < 0)
  1949. ret = wret;
  1950. if (wret == 0) {
  1951. struct btrfs_disk_key disk_key;
  1952. btrfs_node_key(right, &disk_key, 0);
  1953. tree_mod_log_set_node_key(fs_info, parent,
  1954. pslot + 1, 0);
  1955. btrfs_set_node_key(parent, &disk_key, pslot + 1);
  1956. btrfs_mark_buffer_dirty(parent);
  1957. if (btrfs_header_nritems(mid) <= orig_slot) {
  1958. path->nodes[level] = right;
  1959. path->slots[level + 1] += 1;
  1960. path->slots[level] = orig_slot -
  1961. btrfs_header_nritems(mid);
  1962. btrfs_tree_unlock(mid);
  1963. free_extent_buffer(mid);
  1964. } else {
  1965. btrfs_tree_unlock(right);
  1966. free_extent_buffer(right);
  1967. }
  1968. return 0;
  1969. }
  1970. btrfs_tree_unlock(right);
  1971. free_extent_buffer(right);
  1972. }
  1973. return 1;
  1974. }
  1975. /*
  1976. * readahead one full node of leaves, finding things that are close
  1977. * to the block in 'slot', and triggering ra on them.
  1978. */
  1979. static void reada_for_search(struct btrfs_fs_info *fs_info,
  1980. struct btrfs_path *path,
  1981. int level, int slot, u64 objectid)
  1982. {
  1983. struct extent_buffer *node;
  1984. struct btrfs_disk_key disk_key;
  1985. u32 nritems;
  1986. u64 search;
  1987. u64 target;
  1988. u64 nread = 0;
  1989. struct extent_buffer *eb;
  1990. u32 nr;
  1991. u32 blocksize;
  1992. u32 nscan = 0;
  1993. if (level != 1)
  1994. return;
  1995. if (!path->nodes[level])
  1996. return;
  1997. node = path->nodes[level];
  1998. search = btrfs_node_blockptr(node, slot);
  1999. blocksize = fs_info->nodesize;
  2000. eb = find_extent_buffer(fs_info, search);
  2001. if (eb) {
  2002. free_extent_buffer(eb);
  2003. return;
  2004. }
  2005. target = search;
  2006. nritems = btrfs_header_nritems(node);
  2007. nr = slot;
  2008. while (1) {
  2009. if (path->reada == READA_BACK) {
  2010. if (nr == 0)
  2011. break;
  2012. nr--;
  2013. } else if (path->reada == READA_FORWARD) {
  2014. nr++;
  2015. if (nr >= nritems)
  2016. break;
  2017. }
  2018. if (path->reada == READA_BACK && objectid) {
  2019. btrfs_node_key(node, &disk_key, nr);
  2020. if (btrfs_disk_key_objectid(&disk_key) != objectid)
  2021. break;
  2022. }
  2023. search = btrfs_node_blockptr(node, nr);
  2024. if ((search <= target && target - search <= 65536) ||
  2025. (search > target && search - target <= 65536)) {
  2026. readahead_tree_block(fs_info, search);
  2027. nread += blocksize;
  2028. }
  2029. nscan++;
  2030. if ((nread > 65536 || nscan > 32))
  2031. break;
  2032. }
  2033. }
  2034. static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
  2035. struct btrfs_path *path, int level)
  2036. {
  2037. int slot;
  2038. int nritems;
  2039. struct extent_buffer *parent;
  2040. struct extent_buffer *eb;
  2041. u64 gen;
  2042. u64 block1 = 0;
  2043. u64 block2 = 0;
  2044. parent = path->nodes[level + 1];
  2045. if (!parent)
  2046. return;
  2047. nritems = btrfs_header_nritems(parent);
  2048. slot = path->slots[level + 1];
  2049. if (slot > 0) {
  2050. block1 = btrfs_node_blockptr(parent, slot - 1);
  2051. gen = btrfs_node_ptr_generation(parent, slot - 1);
  2052. eb = find_extent_buffer(fs_info, block1);
  2053. /*
  2054. * if we get -eagain from btrfs_buffer_uptodate, we
  2055. * don't want to return eagain here. That will loop
  2056. * forever
  2057. */
  2058. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  2059. block1 = 0;
  2060. free_extent_buffer(eb);
  2061. }
  2062. if (slot + 1 < nritems) {
  2063. block2 = btrfs_node_blockptr(parent, slot + 1);
  2064. gen = btrfs_node_ptr_generation(parent, slot + 1);
  2065. eb = find_extent_buffer(fs_info, block2);
  2066. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  2067. block2 = 0;
  2068. free_extent_buffer(eb);
  2069. }
  2070. if (block1)
  2071. readahead_tree_block(fs_info, block1);
  2072. if (block2)
  2073. readahead_tree_block(fs_info, block2);
  2074. }
  2075. /*
  2076. * when we walk down the tree, it is usually safe to unlock the higher layers
  2077. * in the tree. The exceptions are when our path goes through slot 0, because
  2078. * operations on the tree might require changing key pointers higher up in the
  2079. * tree.
  2080. *
  2081. * callers might also have set path->keep_locks, which tells this code to keep
  2082. * the lock if the path points to the last slot in the block. This is part of
  2083. * walking through the tree, and selecting the next slot in the higher block.
  2084. *
  2085. * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
  2086. * if lowest_unlock is 1, level 0 won't be unlocked
  2087. */
  2088. static noinline void unlock_up(struct btrfs_path *path, int level,
  2089. int lowest_unlock, int min_write_lock_level,
  2090. int *write_lock_level)
  2091. {
  2092. int i;
  2093. int skip_level = level;
  2094. int no_skips = 0;
  2095. struct extent_buffer *t;
  2096. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2097. if (!path->nodes[i])
  2098. break;
  2099. if (!path->locks[i])
  2100. break;
  2101. if (!no_skips && path->slots[i] == 0) {
  2102. skip_level = i + 1;
  2103. continue;
  2104. }
  2105. if (!no_skips && path->keep_locks) {
  2106. u32 nritems;
  2107. t = path->nodes[i];
  2108. nritems = btrfs_header_nritems(t);
  2109. if (nritems < 1 || path->slots[i] >= nritems - 1) {
  2110. skip_level = i + 1;
  2111. continue;
  2112. }
  2113. }
  2114. if (skip_level < i && i >= lowest_unlock)
  2115. no_skips = 1;
  2116. t = path->nodes[i];
  2117. if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
  2118. btrfs_tree_unlock_rw(t, path->locks[i]);
  2119. path->locks[i] = 0;
  2120. if (write_lock_level &&
  2121. i > min_write_lock_level &&
  2122. i <= *write_lock_level) {
  2123. *write_lock_level = i - 1;
  2124. }
  2125. }
  2126. }
  2127. }
  2128. /*
  2129. * This releases any locks held in the path starting at level and
  2130. * going all the way up to the root.
  2131. *
  2132. * btrfs_search_slot will keep the lock held on higher nodes in a few
  2133. * corner cases, such as COW of the block at slot zero in the node. This
  2134. * ignores those rules, and it should only be called when there are no
  2135. * more updates to be done higher up in the tree.
  2136. */
  2137. noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
  2138. {
  2139. int i;
  2140. if (path->keep_locks)
  2141. return;
  2142. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2143. if (!path->nodes[i])
  2144. continue;
  2145. if (!path->locks[i])
  2146. continue;
  2147. btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
  2148. path->locks[i] = 0;
  2149. }
  2150. }
  2151. /*
  2152. * helper function for btrfs_search_slot. The goal is to find a block
  2153. * in cache without setting the path to blocking. If we find the block
  2154. * we return zero and the path is unchanged.
  2155. *
  2156. * If we can't find the block, we set the path blocking and do some
  2157. * reada. -EAGAIN is returned and the search must be repeated.
  2158. */
  2159. static int
  2160. read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
  2161. struct extent_buffer **eb_ret, int level, int slot,
  2162. const struct btrfs_key *key)
  2163. {
  2164. struct btrfs_fs_info *fs_info = root->fs_info;
  2165. u64 blocknr;
  2166. u64 gen;
  2167. struct extent_buffer *b = *eb_ret;
  2168. struct extent_buffer *tmp;
  2169. int ret;
  2170. blocknr = btrfs_node_blockptr(b, slot);
  2171. gen = btrfs_node_ptr_generation(b, slot);
  2172. tmp = find_extent_buffer(fs_info, blocknr);
  2173. if (tmp) {
  2174. /* first we do an atomic uptodate check */
  2175. if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
  2176. *eb_ret = tmp;
  2177. return 0;
  2178. }
  2179. /* the pages were up to date, but we failed
  2180. * the generation number check. Do a full
  2181. * read for the generation number that is correct.
  2182. * We must do this without dropping locks so
  2183. * we can trust our generation number
  2184. */
  2185. btrfs_set_path_blocking(p);
  2186. /* now we're allowed to do a blocking uptodate check */
  2187. ret = btrfs_read_buffer(tmp, gen);
  2188. if (!ret) {
  2189. *eb_ret = tmp;
  2190. return 0;
  2191. }
  2192. free_extent_buffer(tmp);
  2193. btrfs_release_path(p);
  2194. return -EIO;
  2195. }
  2196. /*
  2197. * reduce lock contention at high levels
  2198. * of the btree by dropping locks before
  2199. * we read. Don't release the lock on the current
  2200. * level because we need to walk this node to figure
  2201. * out which blocks to read.
  2202. */
  2203. btrfs_unlock_up_safe(p, level + 1);
  2204. btrfs_set_path_blocking(p);
  2205. free_extent_buffer(tmp);
  2206. if (p->reada != READA_NONE)
  2207. reada_for_search(fs_info, p, level, slot, key->objectid);
  2208. btrfs_release_path(p);
  2209. ret = -EAGAIN;
  2210. tmp = read_tree_block(fs_info, blocknr, 0);
  2211. if (!IS_ERR(tmp)) {
  2212. /*
  2213. * If the read above didn't mark this buffer up to date,
  2214. * it will never end up being up to date. Set ret to EIO now
  2215. * and give up so that our caller doesn't loop forever
  2216. * on our EAGAINs.
  2217. */
  2218. if (!btrfs_buffer_uptodate(tmp, 0, 0))
  2219. ret = -EIO;
  2220. free_extent_buffer(tmp);
  2221. } else {
  2222. ret = PTR_ERR(tmp);
  2223. }
  2224. return ret;
  2225. }
  2226. /*
  2227. * helper function for btrfs_search_slot. This does all of the checks
  2228. * for node-level blocks and does any balancing required based on
  2229. * the ins_len.
  2230. *
  2231. * If no extra work was required, zero is returned. If we had to
  2232. * drop the path, -EAGAIN is returned and btrfs_search_slot must
  2233. * start over
  2234. */
  2235. static int
  2236. setup_nodes_for_search(struct btrfs_trans_handle *trans,
  2237. struct btrfs_root *root, struct btrfs_path *p,
  2238. struct extent_buffer *b, int level, int ins_len,
  2239. int *write_lock_level)
  2240. {
  2241. struct btrfs_fs_info *fs_info = root->fs_info;
  2242. int ret;
  2243. if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
  2244. BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
  2245. int sret;
  2246. if (*write_lock_level < level + 1) {
  2247. *write_lock_level = level + 1;
  2248. btrfs_release_path(p);
  2249. goto again;
  2250. }
  2251. btrfs_set_path_blocking(p);
  2252. reada_for_balance(fs_info, p, level);
  2253. sret = split_node(trans, root, p, level);
  2254. btrfs_clear_path_blocking(p, NULL, 0);
  2255. BUG_ON(sret > 0);
  2256. if (sret) {
  2257. ret = sret;
  2258. goto done;
  2259. }
  2260. b = p->nodes[level];
  2261. } else if (ins_len < 0 && btrfs_header_nritems(b) <
  2262. BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
  2263. int sret;
  2264. if (*write_lock_level < level + 1) {
  2265. *write_lock_level = level + 1;
  2266. btrfs_release_path(p);
  2267. goto again;
  2268. }
  2269. btrfs_set_path_blocking(p);
  2270. reada_for_balance(fs_info, p, level);
  2271. sret = balance_level(trans, root, p, level);
  2272. btrfs_clear_path_blocking(p, NULL, 0);
  2273. if (sret) {
  2274. ret = sret;
  2275. goto done;
  2276. }
  2277. b = p->nodes[level];
  2278. if (!b) {
  2279. btrfs_release_path(p);
  2280. goto again;
  2281. }
  2282. BUG_ON(btrfs_header_nritems(b) == 1);
  2283. }
  2284. return 0;
  2285. again:
  2286. ret = -EAGAIN;
  2287. done:
  2288. return ret;
  2289. }
  2290. static void key_search_validate(struct extent_buffer *b,
  2291. const struct btrfs_key *key,
  2292. int level)
  2293. {
  2294. #ifdef CONFIG_BTRFS_ASSERT
  2295. struct btrfs_disk_key disk_key;
  2296. btrfs_cpu_key_to_disk(&disk_key, key);
  2297. if (level == 0)
  2298. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2299. offsetof(struct btrfs_leaf, items[0].key),
  2300. sizeof(disk_key)));
  2301. else
  2302. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2303. offsetof(struct btrfs_node, ptrs[0].key),
  2304. sizeof(disk_key)));
  2305. #endif
  2306. }
  2307. static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
  2308. int level, int *prev_cmp, int *slot)
  2309. {
  2310. if (*prev_cmp != 0) {
  2311. *prev_cmp = btrfs_bin_search(b, key, level, slot);
  2312. return *prev_cmp;
  2313. }
  2314. key_search_validate(b, key, level);
  2315. *slot = 0;
  2316. return 0;
  2317. }
  2318. int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
  2319. u64 iobjectid, u64 ioff, u8 key_type,
  2320. struct btrfs_key *found_key)
  2321. {
  2322. int ret;
  2323. struct btrfs_key key;
  2324. struct extent_buffer *eb;
  2325. ASSERT(path);
  2326. ASSERT(found_key);
  2327. key.type = key_type;
  2328. key.objectid = iobjectid;
  2329. key.offset = ioff;
  2330. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  2331. if (ret < 0)
  2332. return ret;
  2333. eb = path->nodes[0];
  2334. if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
  2335. ret = btrfs_next_leaf(fs_root, path);
  2336. if (ret)
  2337. return ret;
  2338. eb = path->nodes[0];
  2339. }
  2340. btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
  2341. if (found_key->type != key.type ||
  2342. found_key->objectid != key.objectid)
  2343. return 1;
  2344. return 0;
  2345. }
  2346. /*
  2347. * btrfs_search_slot - look for a key in a tree and perform necessary
  2348. * modifications to preserve tree invariants.
  2349. *
  2350. * @trans: Handle of transaction, used when modifying the tree
  2351. * @p: Holds all btree nodes along the search path
  2352. * @root: The root node of the tree
  2353. * @key: The key we are looking for
  2354. * @ins_len: Indicates purpose of search, for inserts it is 1, for
  2355. * deletions it's -1. 0 for plain searches
  2356. * @cow: boolean should CoW operations be performed. Must always be 1
  2357. * when modifying the tree.
  2358. *
  2359. * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
  2360. * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
  2361. *
  2362. * If @key is found, 0 is returned and you can find the item in the leaf level
  2363. * of the path (level 0)
  2364. *
  2365. * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
  2366. * points to the slot where it should be inserted
  2367. *
  2368. * If an error is encountered while searching the tree a negative error number
  2369. * is returned
  2370. */
  2371. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2372. const struct btrfs_key *key, struct btrfs_path *p,
  2373. int ins_len, int cow)
  2374. {
  2375. struct btrfs_fs_info *fs_info = root->fs_info;
  2376. struct extent_buffer *b;
  2377. int slot;
  2378. int ret;
  2379. int err;
  2380. int level;
  2381. int lowest_unlock = 1;
  2382. int root_lock;
  2383. /* everything at write_lock_level or lower must be write locked */
  2384. int write_lock_level = 0;
  2385. u8 lowest_level = 0;
  2386. int min_write_lock_level;
  2387. int prev_cmp;
  2388. lowest_level = p->lowest_level;
  2389. WARN_ON(lowest_level && ins_len > 0);
  2390. WARN_ON(p->nodes[0] != NULL);
  2391. BUG_ON(!cow && ins_len);
  2392. if (ins_len < 0) {
  2393. lowest_unlock = 2;
  2394. /* when we are removing items, we might have to go up to level
  2395. * two as we update tree pointers Make sure we keep write
  2396. * for those levels as well
  2397. */
  2398. write_lock_level = 2;
  2399. } else if (ins_len > 0) {
  2400. /*
  2401. * for inserting items, make sure we have a write lock on
  2402. * level 1 so we can update keys
  2403. */
  2404. write_lock_level = 1;
  2405. }
  2406. if (!cow)
  2407. write_lock_level = -1;
  2408. if (cow && (p->keep_locks || p->lowest_level))
  2409. write_lock_level = BTRFS_MAX_LEVEL;
  2410. min_write_lock_level = write_lock_level;
  2411. again:
  2412. prev_cmp = -1;
  2413. /*
  2414. * we try very hard to do read locks on the root
  2415. */
  2416. root_lock = BTRFS_READ_LOCK;
  2417. level = 0;
  2418. if (p->search_commit_root) {
  2419. /*
  2420. * the commit roots are read only
  2421. * so we always do read locks
  2422. */
  2423. if (p->need_commit_sem)
  2424. down_read(&fs_info->commit_root_sem);
  2425. b = root->commit_root;
  2426. extent_buffer_get(b);
  2427. level = btrfs_header_level(b);
  2428. if (p->need_commit_sem)
  2429. up_read(&fs_info->commit_root_sem);
  2430. if (!p->skip_locking)
  2431. btrfs_tree_read_lock(b);
  2432. } else {
  2433. if (p->skip_locking) {
  2434. b = btrfs_root_node(root);
  2435. level = btrfs_header_level(b);
  2436. } else {
  2437. /* we don't know the level of the root node
  2438. * until we actually have it read locked
  2439. */
  2440. b = btrfs_read_lock_root_node(root);
  2441. level = btrfs_header_level(b);
  2442. if (level <= write_lock_level) {
  2443. /* whoops, must trade for write lock */
  2444. btrfs_tree_read_unlock(b);
  2445. free_extent_buffer(b);
  2446. b = btrfs_lock_root_node(root);
  2447. root_lock = BTRFS_WRITE_LOCK;
  2448. /* the level might have changed, check again */
  2449. level = btrfs_header_level(b);
  2450. }
  2451. }
  2452. }
  2453. p->nodes[level] = b;
  2454. if (!p->skip_locking)
  2455. p->locks[level] = root_lock;
  2456. while (b) {
  2457. level = btrfs_header_level(b);
  2458. /*
  2459. * setup the path here so we can release it under lock
  2460. * contention with the cow code
  2461. */
  2462. if (cow) {
  2463. bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
  2464. /*
  2465. * if we don't really need to cow this block
  2466. * then we don't want to set the path blocking,
  2467. * so we test it here
  2468. */
  2469. if (!should_cow_block(trans, root, b)) {
  2470. trans->dirty = true;
  2471. goto cow_done;
  2472. }
  2473. /*
  2474. * must have write locks on this node and the
  2475. * parent
  2476. */
  2477. if (level > write_lock_level ||
  2478. (level + 1 > write_lock_level &&
  2479. level + 1 < BTRFS_MAX_LEVEL &&
  2480. p->nodes[level + 1])) {
  2481. write_lock_level = level + 1;
  2482. btrfs_release_path(p);
  2483. goto again;
  2484. }
  2485. btrfs_set_path_blocking(p);
  2486. if (last_level)
  2487. err = btrfs_cow_block(trans, root, b, NULL, 0,
  2488. &b);
  2489. else
  2490. err = btrfs_cow_block(trans, root, b,
  2491. p->nodes[level + 1],
  2492. p->slots[level + 1], &b);
  2493. if (err) {
  2494. ret = err;
  2495. goto done;
  2496. }
  2497. }
  2498. cow_done:
  2499. p->nodes[level] = b;
  2500. btrfs_clear_path_blocking(p, NULL, 0);
  2501. /*
  2502. * we have a lock on b and as long as we aren't changing
  2503. * the tree, there is no way to for the items in b to change.
  2504. * It is safe to drop the lock on our parent before we
  2505. * go through the expensive btree search on b.
  2506. *
  2507. * If we're inserting or deleting (ins_len != 0), then we might
  2508. * be changing slot zero, which may require changing the parent.
  2509. * So, we can't drop the lock until after we know which slot
  2510. * we're operating on.
  2511. */
  2512. if (!ins_len && !p->keep_locks) {
  2513. int u = level + 1;
  2514. if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
  2515. btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
  2516. p->locks[u] = 0;
  2517. }
  2518. }
  2519. ret = key_search(b, key, level, &prev_cmp, &slot);
  2520. if (ret < 0)
  2521. goto done;
  2522. if (level != 0) {
  2523. int dec = 0;
  2524. if (ret && slot > 0) {
  2525. dec = 1;
  2526. slot -= 1;
  2527. }
  2528. p->slots[level] = slot;
  2529. err = setup_nodes_for_search(trans, root, p, b, level,
  2530. ins_len, &write_lock_level);
  2531. if (err == -EAGAIN)
  2532. goto again;
  2533. if (err) {
  2534. ret = err;
  2535. goto done;
  2536. }
  2537. b = p->nodes[level];
  2538. slot = p->slots[level];
  2539. /*
  2540. * slot 0 is special, if we change the key
  2541. * we have to update the parent pointer
  2542. * which means we must have a write lock
  2543. * on the parent
  2544. */
  2545. if (slot == 0 && ins_len &&
  2546. write_lock_level < level + 1) {
  2547. write_lock_level = level + 1;
  2548. btrfs_release_path(p);
  2549. goto again;
  2550. }
  2551. unlock_up(p, level, lowest_unlock,
  2552. min_write_lock_level, &write_lock_level);
  2553. if (level == lowest_level) {
  2554. if (dec)
  2555. p->slots[level]++;
  2556. goto done;
  2557. }
  2558. err = read_block_for_search(root, p, &b, level,
  2559. slot, key);
  2560. if (err == -EAGAIN)
  2561. goto again;
  2562. if (err) {
  2563. ret = err;
  2564. goto done;
  2565. }
  2566. if (!p->skip_locking) {
  2567. level = btrfs_header_level(b);
  2568. if (level <= write_lock_level) {
  2569. err = btrfs_try_tree_write_lock(b);
  2570. if (!err) {
  2571. btrfs_set_path_blocking(p);
  2572. btrfs_tree_lock(b);
  2573. btrfs_clear_path_blocking(p, b,
  2574. BTRFS_WRITE_LOCK);
  2575. }
  2576. p->locks[level] = BTRFS_WRITE_LOCK;
  2577. } else {
  2578. err = btrfs_tree_read_lock_atomic(b);
  2579. if (!err) {
  2580. btrfs_set_path_blocking(p);
  2581. btrfs_tree_read_lock(b);
  2582. btrfs_clear_path_blocking(p, b,
  2583. BTRFS_READ_LOCK);
  2584. }
  2585. p->locks[level] = BTRFS_READ_LOCK;
  2586. }
  2587. p->nodes[level] = b;
  2588. }
  2589. } else {
  2590. p->slots[level] = slot;
  2591. if (ins_len > 0 &&
  2592. btrfs_leaf_free_space(fs_info, b) < ins_len) {
  2593. if (write_lock_level < 1) {
  2594. write_lock_level = 1;
  2595. btrfs_release_path(p);
  2596. goto again;
  2597. }
  2598. btrfs_set_path_blocking(p);
  2599. err = split_leaf(trans, root, key,
  2600. p, ins_len, ret == 0);
  2601. btrfs_clear_path_blocking(p, NULL, 0);
  2602. BUG_ON(err > 0);
  2603. if (err) {
  2604. ret = err;
  2605. goto done;
  2606. }
  2607. }
  2608. if (!p->search_for_split)
  2609. unlock_up(p, level, lowest_unlock,
  2610. min_write_lock_level, &write_lock_level);
  2611. goto done;
  2612. }
  2613. }
  2614. ret = 1;
  2615. done:
  2616. /*
  2617. * we don't really know what they plan on doing with the path
  2618. * from here on, so for now just mark it as blocking
  2619. */
  2620. if (!p->leave_spinning)
  2621. btrfs_set_path_blocking(p);
  2622. if (ret < 0 && !p->skip_release_on_error)
  2623. btrfs_release_path(p);
  2624. return ret;
  2625. }
  2626. /*
  2627. * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
  2628. * current state of the tree together with the operations recorded in the tree
  2629. * modification log to search for the key in a previous version of this tree, as
  2630. * denoted by the time_seq parameter.
  2631. *
  2632. * Naturally, there is no support for insert, delete or cow operations.
  2633. *
  2634. * The resulting path and return value will be set up as if we called
  2635. * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
  2636. */
  2637. int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
  2638. struct btrfs_path *p, u64 time_seq)
  2639. {
  2640. struct btrfs_fs_info *fs_info = root->fs_info;
  2641. struct extent_buffer *b;
  2642. int slot;
  2643. int ret;
  2644. int err;
  2645. int level;
  2646. int lowest_unlock = 1;
  2647. u8 lowest_level = 0;
  2648. int prev_cmp = -1;
  2649. lowest_level = p->lowest_level;
  2650. WARN_ON(p->nodes[0] != NULL);
  2651. if (p->search_commit_root) {
  2652. BUG_ON(time_seq);
  2653. return btrfs_search_slot(NULL, root, key, p, 0, 0);
  2654. }
  2655. again:
  2656. b = get_old_root(root, time_seq);
  2657. level = btrfs_header_level(b);
  2658. p->locks[level] = BTRFS_READ_LOCK;
  2659. while (b) {
  2660. level = btrfs_header_level(b);
  2661. p->nodes[level] = b;
  2662. btrfs_clear_path_blocking(p, NULL, 0);
  2663. /*
  2664. * we have a lock on b and as long as we aren't changing
  2665. * the tree, there is no way to for the items in b to change.
  2666. * It is safe to drop the lock on our parent before we
  2667. * go through the expensive btree search on b.
  2668. */
  2669. btrfs_unlock_up_safe(p, level + 1);
  2670. /*
  2671. * Since we can unwind ebs we want to do a real search every
  2672. * time.
  2673. */
  2674. prev_cmp = -1;
  2675. ret = key_search(b, key, level, &prev_cmp, &slot);
  2676. if (level != 0) {
  2677. int dec = 0;
  2678. if (ret && slot > 0) {
  2679. dec = 1;
  2680. slot -= 1;
  2681. }
  2682. p->slots[level] = slot;
  2683. unlock_up(p, level, lowest_unlock, 0, NULL);
  2684. if (level == lowest_level) {
  2685. if (dec)
  2686. p->slots[level]++;
  2687. goto done;
  2688. }
  2689. err = read_block_for_search(root, p, &b, level,
  2690. slot, key);
  2691. if (err == -EAGAIN)
  2692. goto again;
  2693. if (err) {
  2694. ret = err;
  2695. goto done;
  2696. }
  2697. level = btrfs_header_level(b);
  2698. err = btrfs_tree_read_lock_atomic(b);
  2699. if (!err) {
  2700. btrfs_set_path_blocking(p);
  2701. btrfs_tree_read_lock(b);
  2702. btrfs_clear_path_blocking(p, b,
  2703. BTRFS_READ_LOCK);
  2704. }
  2705. b = tree_mod_log_rewind(fs_info, p, b, time_seq);
  2706. if (!b) {
  2707. ret = -ENOMEM;
  2708. goto done;
  2709. }
  2710. p->locks[level] = BTRFS_READ_LOCK;
  2711. p->nodes[level] = b;
  2712. } else {
  2713. p->slots[level] = slot;
  2714. unlock_up(p, level, lowest_unlock, 0, NULL);
  2715. goto done;
  2716. }
  2717. }
  2718. ret = 1;
  2719. done:
  2720. if (!p->leave_spinning)
  2721. btrfs_set_path_blocking(p);
  2722. if (ret < 0)
  2723. btrfs_release_path(p);
  2724. return ret;
  2725. }
  2726. /*
  2727. * helper to use instead of search slot if no exact match is needed but
  2728. * instead the next or previous item should be returned.
  2729. * When find_higher is true, the next higher item is returned, the next lower
  2730. * otherwise.
  2731. * When return_any and find_higher are both true, and no higher item is found,
  2732. * return the next lower instead.
  2733. * When return_any is true and find_higher is false, and no lower item is found,
  2734. * return the next higher instead.
  2735. * It returns 0 if any item is found, 1 if none is found (tree empty), and
  2736. * < 0 on error
  2737. */
  2738. int btrfs_search_slot_for_read(struct btrfs_root *root,
  2739. const struct btrfs_key *key,
  2740. struct btrfs_path *p, int find_higher,
  2741. int return_any)
  2742. {
  2743. int ret;
  2744. struct extent_buffer *leaf;
  2745. again:
  2746. ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
  2747. if (ret <= 0)
  2748. return ret;
  2749. /*
  2750. * a return value of 1 means the path is at the position where the
  2751. * item should be inserted. Normally this is the next bigger item,
  2752. * but in case the previous item is the last in a leaf, path points
  2753. * to the first free slot in the previous leaf, i.e. at an invalid
  2754. * item.
  2755. */
  2756. leaf = p->nodes[0];
  2757. if (find_higher) {
  2758. if (p->slots[0] >= btrfs_header_nritems(leaf)) {
  2759. ret = btrfs_next_leaf(root, p);
  2760. if (ret <= 0)
  2761. return ret;
  2762. if (!return_any)
  2763. return 1;
  2764. /*
  2765. * no higher item found, return the next
  2766. * lower instead
  2767. */
  2768. return_any = 0;
  2769. find_higher = 0;
  2770. btrfs_release_path(p);
  2771. goto again;
  2772. }
  2773. } else {
  2774. if (p->slots[0] == 0) {
  2775. ret = btrfs_prev_leaf(root, p);
  2776. if (ret < 0)
  2777. return ret;
  2778. if (!ret) {
  2779. leaf = p->nodes[0];
  2780. if (p->slots[0] == btrfs_header_nritems(leaf))
  2781. p->slots[0]--;
  2782. return 0;
  2783. }
  2784. if (!return_any)
  2785. return 1;
  2786. /*
  2787. * no lower item found, return the next
  2788. * higher instead
  2789. */
  2790. return_any = 0;
  2791. find_higher = 1;
  2792. btrfs_release_path(p);
  2793. goto again;
  2794. } else {
  2795. --p->slots[0];
  2796. }
  2797. }
  2798. return 0;
  2799. }
  2800. /*
  2801. * adjust the pointers going up the tree, starting at level
  2802. * making sure the right key of each node is points to 'key'.
  2803. * This is used after shifting pointers to the left, so it stops
  2804. * fixing up pointers when a given leaf/node is not in slot 0 of the
  2805. * higher levels
  2806. *
  2807. */
  2808. static void fixup_low_keys(struct btrfs_fs_info *fs_info,
  2809. struct btrfs_path *path,
  2810. struct btrfs_disk_key *key, int level)
  2811. {
  2812. int i;
  2813. struct extent_buffer *t;
  2814. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2815. int tslot = path->slots[i];
  2816. if (!path->nodes[i])
  2817. break;
  2818. t = path->nodes[i];
  2819. tree_mod_log_set_node_key(fs_info, t, tslot, 1);
  2820. btrfs_set_node_key(t, key, tslot);
  2821. btrfs_mark_buffer_dirty(path->nodes[i]);
  2822. if (tslot != 0)
  2823. break;
  2824. }
  2825. }
  2826. /*
  2827. * update item key.
  2828. *
  2829. * This function isn't completely safe. It's the caller's responsibility
  2830. * that the new key won't break the order
  2831. */
  2832. void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
  2833. struct btrfs_path *path,
  2834. const struct btrfs_key *new_key)
  2835. {
  2836. struct btrfs_disk_key disk_key;
  2837. struct extent_buffer *eb;
  2838. int slot;
  2839. eb = path->nodes[0];
  2840. slot = path->slots[0];
  2841. if (slot > 0) {
  2842. btrfs_item_key(eb, &disk_key, slot - 1);
  2843. BUG_ON(comp_keys(&disk_key, new_key) >= 0);
  2844. }
  2845. if (slot < btrfs_header_nritems(eb) - 1) {
  2846. btrfs_item_key(eb, &disk_key, slot + 1);
  2847. BUG_ON(comp_keys(&disk_key, new_key) <= 0);
  2848. }
  2849. btrfs_cpu_key_to_disk(&disk_key, new_key);
  2850. btrfs_set_item_key(eb, &disk_key, slot);
  2851. btrfs_mark_buffer_dirty(eb);
  2852. if (slot == 0)
  2853. fixup_low_keys(fs_info, path, &disk_key, 1);
  2854. }
  2855. /*
  2856. * try to push data from one node into the next node left in the
  2857. * tree.
  2858. *
  2859. * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
  2860. * error, and > 0 if there was no room in the left hand block.
  2861. */
  2862. static int push_node_left(struct btrfs_trans_handle *trans,
  2863. struct btrfs_fs_info *fs_info,
  2864. struct extent_buffer *dst,
  2865. struct extent_buffer *src, int empty)
  2866. {
  2867. int push_items = 0;
  2868. int src_nritems;
  2869. int dst_nritems;
  2870. int ret = 0;
  2871. src_nritems = btrfs_header_nritems(src);
  2872. dst_nritems = btrfs_header_nritems(dst);
  2873. push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
  2874. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2875. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2876. if (!empty && src_nritems <= 8)
  2877. return 1;
  2878. if (push_items <= 0)
  2879. return 1;
  2880. if (empty) {
  2881. push_items = min(src_nritems, push_items);
  2882. if (push_items < src_nritems) {
  2883. /* leave at least 8 pointers in the node if
  2884. * we aren't going to empty it
  2885. */
  2886. if (src_nritems - push_items < 8) {
  2887. if (push_items <= 8)
  2888. return 1;
  2889. push_items -= 8;
  2890. }
  2891. }
  2892. } else
  2893. push_items = min(src_nritems - 8, push_items);
  2894. ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
  2895. push_items);
  2896. if (ret) {
  2897. btrfs_abort_transaction(trans, ret);
  2898. return ret;
  2899. }
  2900. copy_extent_buffer(dst, src,
  2901. btrfs_node_key_ptr_offset(dst_nritems),
  2902. btrfs_node_key_ptr_offset(0),
  2903. push_items * sizeof(struct btrfs_key_ptr));
  2904. if (push_items < src_nritems) {
  2905. /*
  2906. * don't call tree_mod_log_eb_move here, key removal was already
  2907. * fully logged by tree_mod_log_eb_copy above.
  2908. */
  2909. memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
  2910. btrfs_node_key_ptr_offset(push_items),
  2911. (src_nritems - push_items) *
  2912. sizeof(struct btrfs_key_ptr));
  2913. }
  2914. btrfs_set_header_nritems(src, src_nritems - push_items);
  2915. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2916. btrfs_mark_buffer_dirty(src);
  2917. btrfs_mark_buffer_dirty(dst);
  2918. return ret;
  2919. }
  2920. /*
  2921. * try to push data from one node into the next node right in the
  2922. * tree.
  2923. *
  2924. * returns 0 if some ptrs were pushed, < 0 if there was some horrible
  2925. * error, and > 0 if there was no room in the right hand block.
  2926. *
  2927. * this will only push up to 1/2 the contents of the left node over
  2928. */
  2929. static int balance_node_right(struct btrfs_trans_handle *trans,
  2930. struct btrfs_fs_info *fs_info,
  2931. struct extent_buffer *dst,
  2932. struct extent_buffer *src)
  2933. {
  2934. int push_items = 0;
  2935. int max_push;
  2936. int src_nritems;
  2937. int dst_nritems;
  2938. int ret = 0;
  2939. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2940. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2941. src_nritems = btrfs_header_nritems(src);
  2942. dst_nritems = btrfs_header_nritems(dst);
  2943. push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
  2944. if (push_items <= 0)
  2945. return 1;
  2946. if (src_nritems < 4)
  2947. return 1;
  2948. max_push = src_nritems / 2 + 1;
  2949. /* don't try to empty the node */
  2950. if (max_push >= src_nritems)
  2951. return 1;
  2952. if (max_push < push_items)
  2953. push_items = max_push;
  2954. tree_mod_log_eb_move(fs_info, dst, push_items, 0, dst_nritems);
  2955. memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
  2956. btrfs_node_key_ptr_offset(0),
  2957. (dst_nritems) *
  2958. sizeof(struct btrfs_key_ptr));
  2959. ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
  2960. src_nritems - push_items, push_items);
  2961. if (ret) {
  2962. btrfs_abort_transaction(trans, ret);
  2963. return ret;
  2964. }
  2965. copy_extent_buffer(dst, src,
  2966. btrfs_node_key_ptr_offset(0),
  2967. btrfs_node_key_ptr_offset(src_nritems - push_items),
  2968. push_items * sizeof(struct btrfs_key_ptr));
  2969. btrfs_set_header_nritems(src, src_nritems - push_items);
  2970. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2971. btrfs_mark_buffer_dirty(src);
  2972. btrfs_mark_buffer_dirty(dst);
  2973. return ret;
  2974. }
  2975. /*
  2976. * helper function to insert a new root level in the tree.
  2977. * A new node is allocated, and a single item is inserted to
  2978. * point to the existing root
  2979. *
  2980. * returns zero on success or < 0 on failure.
  2981. */
  2982. static noinline int insert_new_root(struct btrfs_trans_handle *trans,
  2983. struct btrfs_root *root,
  2984. struct btrfs_path *path, int level)
  2985. {
  2986. struct btrfs_fs_info *fs_info = root->fs_info;
  2987. u64 lower_gen;
  2988. struct extent_buffer *lower;
  2989. struct extent_buffer *c;
  2990. struct extent_buffer *old;
  2991. struct btrfs_disk_key lower_key;
  2992. BUG_ON(path->nodes[level]);
  2993. BUG_ON(path->nodes[level-1] != root->node);
  2994. lower = path->nodes[level-1];
  2995. if (level == 1)
  2996. btrfs_item_key(lower, &lower_key, 0);
  2997. else
  2998. btrfs_node_key(lower, &lower_key, 0);
  2999. c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3000. &lower_key, level, root->node->start, 0);
  3001. if (IS_ERR(c))
  3002. return PTR_ERR(c);
  3003. root_add_used(root, fs_info->nodesize);
  3004. memzero_extent_buffer(c, 0, sizeof(struct btrfs_header));
  3005. btrfs_set_header_nritems(c, 1);
  3006. btrfs_set_header_level(c, level);
  3007. btrfs_set_header_bytenr(c, c->start);
  3008. btrfs_set_header_generation(c, trans->transid);
  3009. btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
  3010. btrfs_set_header_owner(c, root->root_key.objectid);
  3011. write_extent_buffer_fsid(c, fs_info->fsid);
  3012. write_extent_buffer_chunk_tree_uuid(c, fs_info->chunk_tree_uuid);
  3013. btrfs_set_node_key(c, &lower_key, 0);
  3014. btrfs_set_node_blockptr(c, 0, lower->start);
  3015. lower_gen = btrfs_header_generation(lower);
  3016. WARN_ON(lower_gen != trans->transid);
  3017. btrfs_set_node_ptr_generation(c, 0, lower_gen);
  3018. btrfs_mark_buffer_dirty(c);
  3019. old = root->node;
  3020. tree_mod_log_set_root_pointer(root, c, 0);
  3021. rcu_assign_pointer(root->node, c);
  3022. /* the super has an extra ref to root->node */
  3023. free_extent_buffer(old);
  3024. add_root_to_dirty_list(root);
  3025. extent_buffer_get(c);
  3026. path->nodes[level] = c;
  3027. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  3028. path->slots[level] = 0;
  3029. return 0;
  3030. }
  3031. /*
  3032. * worker function to insert a single pointer in a node.
  3033. * the node should have enough room for the pointer already
  3034. *
  3035. * slot and level indicate where you want the key to go, and
  3036. * blocknr is the block the key points to.
  3037. */
  3038. static void insert_ptr(struct btrfs_trans_handle *trans,
  3039. struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  3040. struct btrfs_disk_key *key, u64 bytenr,
  3041. int slot, int level)
  3042. {
  3043. struct extent_buffer *lower;
  3044. int nritems;
  3045. int ret;
  3046. BUG_ON(!path->nodes[level]);
  3047. btrfs_assert_tree_locked(path->nodes[level]);
  3048. lower = path->nodes[level];
  3049. nritems = btrfs_header_nritems(lower);
  3050. BUG_ON(slot > nritems);
  3051. BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  3052. if (slot != nritems) {
  3053. if (level)
  3054. tree_mod_log_eb_move(fs_info, lower, slot + 1,
  3055. slot, nritems - slot);
  3056. memmove_extent_buffer(lower,
  3057. btrfs_node_key_ptr_offset(slot + 1),
  3058. btrfs_node_key_ptr_offset(slot),
  3059. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  3060. }
  3061. if (level) {
  3062. ret = tree_mod_log_insert_key(fs_info, lower, slot,
  3063. MOD_LOG_KEY_ADD, GFP_NOFS);
  3064. BUG_ON(ret < 0);
  3065. }
  3066. btrfs_set_node_key(lower, key, slot);
  3067. btrfs_set_node_blockptr(lower, slot, bytenr);
  3068. WARN_ON(trans->transid == 0);
  3069. btrfs_set_node_ptr_generation(lower, slot, trans->transid);
  3070. btrfs_set_header_nritems(lower, nritems + 1);
  3071. btrfs_mark_buffer_dirty(lower);
  3072. }
  3073. /*
  3074. * split the node at the specified level in path in two.
  3075. * The path is corrected to point to the appropriate node after the split
  3076. *
  3077. * Before splitting this tries to make some room in the node by pushing
  3078. * left and right, if either one works, it returns right away.
  3079. *
  3080. * returns 0 on success and < 0 on failure
  3081. */
  3082. static noinline int split_node(struct btrfs_trans_handle *trans,
  3083. struct btrfs_root *root,
  3084. struct btrfs_path *path, int level)
  3085. {
  3086. struct btrfs_fs_info *fs_info = root->fs_info;
  3087. struct extent_buffer *c;
  3088. struct extent_buffer *split;
  3089. struct btrfs_disk_key disk_key;
  3090. int mid;
  3091. int ret;
  3092. u32 c_nritems;
  3093. c = path->nodes[level];
  3094. WARN_ON(btrfs_header_generation(c) != trans->transid);
  3095. if (c == root->node) {
  3096. /*
  3097. * trying to split the root, lets make a new one
  3098. *
  3099. * tree mod log: We don't log_removal old root in
  3100. * insert_new_root, because that root buffer will be kept as a
  3101. * normal node. We are going to log removal of half of the
  3102. * elements below with tree_mod_log_eb_copy. We're holding a
  3103. * tree lock on the buffer, which is why we cannot race with
  3104. * other tree_mod_log users.
  3105. */
  3106. ret = insert_new_root(trans, root, path, level + 1);
  3107. if (ret)
  3108. return ret;
  3109. } else {
  3110. ret = push_nodes_for_insert(trans, root, path, level);
  3111. c = path->nodes[level];
  3112. if (!ret && btrfs_header_nritems(c) <
  3113. BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
  3114. return 0;
  3115. if (ret < 0)
  3116. return ret;
  3117. }
  3118. c_nritems = btrfs_header_nritems(c);
  3119. mid = (c_nritems + 1) / 2;
  3120. btrfs_node_key(c, &disk_key, mid);
  3121. split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3122. &disk_key, level, c->start, 0);
  3123. if (IS_ERR(split))
  3124. return PTR_ERR(split);
  3125. root_add_used(root, fs_info->nodesize);
  3126. memzero_extent_buffer(split, 0, sizeof(struct btrfs_header));
  3127. btrfs_set_header_level(split, btrfs_header_level(c));
  3128. btrfs_set_header_bytenr(split, split->start);
  3129. btrfs_set_header_generation(split, trans->transid);
  3130. btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
  3131. btrfs_set_header_owner(split, root->root_key.objectid);
  3132. write_extent_buffer_fsid(split, fs_info->fsid);
  3133. write_extent_buffer_chunk_tree_uuid(split, fs_info->chunk_tree_uuid);
  3134. ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
  3135. if (ret) {
  3136. btrfs_abort_transaction(trans, ret);
  3137. return ret;
  3138. }
  3139. copy_extent_buffer(split, c,
  3140. btrfs_node_key_ptr_offset(0),
  3141. btrfs_node_key_ptr_offset(mid),
  3142. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  3143. btrfs_set_header_nritems(split, c_nritems - mid);
  3144. btrfs_set_header_nritems(c, mid);
  3145. ret = 0;
  3146. btrfs_mark_buffer_dirty(c);
  3147. btrfs_mark_buffer_dirty(split);
  3148. insert_ptr(trans, fs_info, path, &disk_key, split->start,
  3149. path->slots[level + 1] + 1, level + 1);
  3150. if (path->slots[level] >= mid) {
  3151. path->slots[level] -= mid;
  3152. btrfs_tree_unlock(c);
  3153. free_extent_buffer(c);
  3154. path->nodes[level] = split;
  3155. path->slots[level + 1] += 1;
  3156. } else {
  3157. btrfs_tree_unlock(split);
  3158. free_extent_buffer(split);
  3159. }
  3160. return ret;
  3161. }
  3162. /*
  3163. * how many bytes are required to store the items in a leaf. start
  3164. * and nr indicate which items in the leaf to check. This totals up the
  3165. * space used both by the item structs and the item data
  3166. */
  3167. static int leaf_space_used(struct extent_buffer *l, int start, int nr)
  3168. {
  3169. struct btrfs_item *start_item;
  3170. struct btrfs_item *end_item;
  3171. struct btrfs_map_token token;
  3172. int data_len;
  3173. int nritems = btrfs_header_nritems(l);
  3174. int end = min(nritems, start + nr) - 1;
  3175. if (!nr)
  3176. return 0;
  3177. btrfs_init_map_token(&token);
  3178. start_item = btrfs_item_nr(start);
  3179. end_item = btrfs_item_nr(end);
  3180. data_len = btrfs_token_item_offset(l, start_item, &token) +
  3181. btrfs_token_item_size(l, start_item, &token);
  3182. data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
  3183. data_len += sizeof(struct btrfs_item) * nr;
  3184. WARN_ON(data_len < 0);
  3185. return data_len;
  3186. }
  3187. /*
  3188. * The space between the end of the leaf items and
  3189. * the start of the leaf data. IOW, how much room
  3190. * the leaf has left for both items and data
  3191. */
  3192. noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
  3193. struct extent_buffer *leaf)
  3194. {
  3195. int nritems = btrfs_header_nritems(leaf);
  3196. int ret;
  3197. ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
  3198. if (ret < 0) {
  3199. btrfs_crit(fs_info,
  3200. "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
  3201. ret,
  3202. (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
  3203. leaf_space_used(leaf, 0, nritems), nritems);
  3204. }
  3205. return ret;
  3206. }
  3207. /*
  3208. * min slot controls the lowest index we're willing to push to the
  3209. * right. We'll push up to and including min_slot, but no lower
  3210. */
  3211. static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
  3212. struct btrfs_path *path,
  3213. int data_size, int empty,
  3214. struct extent_buffer *right,
  3215. int free_space, u32 left_nritems,
  3216. u32 min_slot)
  3217. {
  3218. struct extent_buffer *left = path->nodes[0];
  3219. struct extent_buffer *upper = path->nodes[1];
  3220. struct btrfs_map_token token;
  3221. struct btrfs_disk_key disk_key;
  3222. int slot;
  3223. u32 i;
  3224. int push_space = 0;
  3225. int push_items = 0;
  3226. struct btrfs_item *item;
  3227. u32 nr;
  3228. u32 right_nritems;
  3229. u32 data_end;
  3230. u32 this_item_size;
  3231. btrfs_init_map_token(&token);
  3232. if (empty)
  3233. nr = 0;
  3234. else
  3235. nr = max_t(u32, 1, min_slot);
  3236. if (path->slots[0] >= left_nritems)
  3237. push_space += data_size;
  3238. slot = path->slots[1];
  3239. i = left_nritems - 1;
  3240. while (i >= nr) {
  3241. item = btrfs_item_nr(i);
  3242. if (!empty && push_items > 0) {
  3243. if (path->slots[0] > i)
  3244. break;
  3245. if (path->slots[0] == i) {
  3246. int space = btrfs_leaf_free_space(fs_info, left);
  3247. if (space + push_space * 2 > free_space)
  3248. break;
  3249. }
  3250. }
  3251. if (path->slots[0] == i)
  3252. push_space += data_size;
  3253. this_item_size = btrfs_item_size(left, item);
  3254. if (this_item_size + sizeof(*item) + push_space > free_space)
  3255. break;
  3256. push_items++;
  3257. push_space += this_item_size + sizeof(*item);
  3258. if (i == 0)
  3259. break;
  3260. i--;
  3261. }
  3262. if (push_items == 0)
  3263. goto out_unlock;
  3264. WARN_ON(!empty && push_items == left_nritems);
  3265. /* push left to right */
  3266. right_nritems = btrfs_header_nritems(right);
  3267. push_space = btrfs_item_end_nr(left, left_nritems - push_items);
  3268. push_space -= leaf_data_end(fs_info, left);
  3269. /* make room in the right data area */
  3270. data_end = leaf_data_end(fs_info, right);
  3271. memmove_extent_buffer(right,
  3272. BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
  3273. BTRFS_LEAF_DATA_OFFSET + data_end,
  3274. BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
  3275. /* copy from the left data area */
  3276. copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
  3277. BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
  3278. BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
  3279. push_space);
  3280. memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
  3281. btrfs_item_nr_offset(0),
  3282. right_nritems * sizeof(struct btrfs_item));
  3283. /* copy the items from left to right */
  3284. copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
  3285. btrfs_item_nr_offset(left_nritems - push_items),
  3286. push_items * sizeof(struct btrfs_item));
  3287. /* update the item pointers */
  3288. right_nritems += push_items;
  3289. btrfs_set_header_nritems(right, right_nritems);
  3290. push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
  3291. for (i = 0; i < right_nritems; i++) {
  3292. item = btrfs_item_nr(i);
  3293. push_space -= btrfs_token_item_size(right, item, &token);
  3294. btrfs_set_token_item_offset(right, item, push_space, &token);
  3295. }
  3296. left_nritems -= push_items;
  3297. btrfs_set_header_nritems(left, left_nritems);
  3298. if (left_nritems)
  3299. btrfs_mark_buffer_dirty(left);
  3300. else
  3301. clean_tree_block(fs_info, left);
  3302. btrfs_mark_buffer_dirty(right);
  3303. btrfs_item_key(right, &disk_key, 0);
  3304. btrfs_set_node_key(upper, &disk_key, slot + 1);
  3305. btrfs_mark_buffer_dirty(upper);
  3306. /* then fixup the leaf pointer in the path */
  3307. if (path->slots[0] >= left_nritems) {
  3308. path->slots[0] -= left_nritems;
  3309. if (btrfs_header_nritems(path->nodes[0]) == 0)
  3310. clean_tree_block(fs_info, path->nodes[0]);
  3311. btrfs_tree_unlock(path->nodes[0]);
  3312. free_extent_buffer(path->nodes[0]);
  3313. path->nodes[0] = right;
  3314. path->slots[1] += 1;
  3315. } else {
  3316. btrfs_tree_unlock(right);
  3317. free_extent_buffer(right);
  3318. }
  3319. return 0;
  3320. out_unlock:
  3321. btrfs_tree_unlock(right);
  3322. free_extent_buffer(right);
  3323. return 1;
  3324. }
  3325. /*
  3326. * push some data in the path leaf to the right, trying to free up at
  3327. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3328. *
  3329. * returns 1 if the push failed because the other node didn't have enough
  3330. * room, 0 if everything worked out and < 0 if there were major errors.
  3331. *
  3332. * this will push starting from min_slot to the end of the leaf. It won't
  3333. * push any slot lower than min_slot
  3334. */
  3335. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  3336. *root, struct btrfs_path *path,
  3337. int min_data_size, int data_size,
  3338. int empty, u32 min_slot)
  3339. {
  3340. struct btrfs_fs_info *fs_info = root->fs_info;
  3341. struct extent_buffer *left = path->nodes[0];
  3342. struct extent_buffer *right;
  3343. struct extent_buffer *upper;
  3344. int slot;
  3345. int free_space;
  3346. u32 left_nritems;
  3347. int ret;
  3348. if (!path->nodes[1])
  3349. return 1;
  3350. slot = path->slots[1];
  3351. upper = path->nodes[1];
  3352. if (slot >= btrfs_header_nritems(upper) - 1)
  3353. return 1;
  3354. btrfs_assert_tree_locked(path->nodes[1]);
  3355. right = read_node_slot(fs_info, upper, slot + 1);
  3356. /*
  3357. * slot + 1 is not valid or we fail to read the right node,
  3358. * no big deal, just return.
  3359. */
  3360. if (IS_ERR(right))
  3361. return 1;
  3362. btrfs_tree_lock(right);
  3363. btrfs_set_lock_blocking(right);
  3364. free_space = btrfs_leaf_free_space(fs_info, right);
  3365. if (free_space < data_size)
  3366. goto out_unlock;
  3367. /* cow and double check */
  3368. ret = btrfs_cow_block(trans, root, right, upper,
  3369. slot + 1, &right);
  3370. if (ret)
  3371. goto out_unlock;
  3372. free_space = btrfs_leaf_free_space(fs_info, right);
  3373. if (free_space < data_size)
  3374. goto out_unlock;
  3375. left_nritems = btrfs_header_nritems(left);
  3376. if (left_nritems == 0)
  3377. goto out_unlock;
  3378. if (path->slots[0] == left_nritems && !empty) {
  3379. /* Key greater than all keys in the leaf, right neighbor has
  3380. * enough room for it and we're not emptying our leaf to delete
  3381. * it, therefore use right neighbor to insert the new item and
  3382. * no need to touch/dirty our left leaft. */
  3383. btrfs_tree_unlock(left);
  3384. free_extent_buffer(left);
  3385. path->nodes[0] = right;
  3386. path->slots[0] = 0;
  3387. path->slots[1]++;
  3388. return 0;
  3389. }
  3390. return __push_leaf_right(fs_info, path, min_data_size, empty,
  3391. right, free_space, left_nritems, min_slot);
  3392. out_unlock:
  3393. btrfs_tree_unlock(right);
  3394. free_extent_buffer(right);
  3395. return 1;
  3396. }
  3397. /*
  3398. * push some data in the path leaf to the left, trying to free up at
  3399. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3400. *
  3401. * max_slot can put a limit on how far into the leaf we'll push items. The
  3402. * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
  3403. * items
  3404. */
  3405. static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
  3406. struct btrfs_path *path, int data_size,
  3407. int empty, struct extent_buffer *left,
  3408. int free_space, u32 right_nritems,
  3409. u32 max_slot)
  3410. {
  3411. struct btrfs_disk_key disk_key;
  3412. struct extent_buffer *right = path->nodes[0];
  3413. int i;
  3414. int push_space = 0;
  3415. int push_items = 0;
  3416. struct btrfs_item *item;
  3417. u32 old_left_nritems;
  3418. u32 nr;
  3419. int ret = 0;
  3420. u32 this_item_size;
  3421. u32 old_left_item_size;
  3422. struct btrfs_map_token token;
  3423. btrfs_init_map_token(&token);
  3424. if (empty)
  3425. nr = min(right_nritems, max_slot);
  3426. else
  3427. nr = min(right_nritems - 1, max_slot);
  3428. for (i = 0; i < nr; i++) {
  3429. item = btrfs_item_nr(i);
  3430. if (!empty && push_items > 0) {
  3431. if (path->slots[0] < i)
  3432. break;
  3433. if (path->slots[0] == i) {
  3434. int space = btrfs_leaf_free_space(fs_info, right);
  3435. if (space + push_space * 2 > free_space)
  3436. break;
  3437. }
  3438. }
  3439. if (path->slots[0] == i)
  3440. push_space += data_size;
  3441. this_item_size = btrfs_item_size(right, item);
  3442. if (this_item_size + sizeof(*item) + push_space > free_space)
  3443. break;
  3444. push_items++;
  3445. push_space += this_item_size + sizeof(*item);
  3446. }
  3447. if (push_items == 0) {
  3448. ret = 1;
  3449. goto out;
  3450. }
  3451. WARN_ON(!empty && push_items == btrfs_header_nritems(right));
  3452. /* push data from right to left */
  3453. copy_extent_buffer(left, right,
  3454. btrfs_item_nr_offset(btrfs_header_nritems(left)),
  3455. btrfs_item_nr_offset(0),
  3456. push_items * sizeof(struct btrfs_item));
  3457. push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
  3458. btrfs_item_offset_nr(right, push_items - 1);
  3459. copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
  3460. leaf_data_end(fs_info, left) - push_space,
  3461. BTRFS_LEAF_DATA_OFFSET +
  3462. btrfs_item_offset_nr(right, push_items - 1),
  3463. push_space);
  3464. old_left_nritems = btrfs_header_nritems(left);
  3465. BUG_ON(old_left_nritems <= 0);
  3466. old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
  3467. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  3468. u32 ioff;
  3469. item = btrfs_item_nr(i);
  3470. ioff = btrfs_token_item_offset(left, item, &token);
  3471. btrfs_set_token_item_offset(left, item,
  3472. ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
  3473. &token);
  3474. }
  3475. btrfs_set_header_nritems(left, old_left_nritems + push_items);
  3476. /* fixup right node */
  3477. if (push_items > right_nritems)
  3478. WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
  3479. right_nritems);
  3480. if (push_items < right_nritems) {
  3481. push_space = btrfs_item_offset_nr(right, push_items - 1) -
  3482. leaf_data_end(fs_info, right);
  3483. memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
  3484. BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
  3485. BTRFS_LEAF_DATA_OFFSET +
  3486. leaf_data_end(fs_info, right), push_space);
  3487. memmove_extent_buffer(right, btrfs_item_nr_offset(0),
  3488. btrfs_item_nr_offset(push_items),
  3489. (btrfs_header_nritems(right) - push_items) *
  3490. sizeof(struct btrfs_item));
  3491. }
  3492. right_nritems -= push_items;
  3493. btrfs_set_header_nritems(right, right_nritems);
  3494. push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
  3495. for (i = 0; i < right_nritems; i++) {
  3496. item = btrfs_item_nr(i);
  3497. push_space = push_space - btrfs_token_item_size(right,
  3498. item, &token);
  3499. btrfs_set_token_item_offset(right, item, push_space, &token);
  3500. }
  3501. btrfs_mark_buffer_dirty(left);
  3502. if (right_nritems)
  3503. btrfs_mark_buffer_dirty(right);
  3504. else
  3505. clean_tree_block(fs_info, right);
  3506. btrfs_item_key(right, &disk_key, 0);
  3507. fixup_low_keys(fs_info, path, &disk_key, 1);
  3508. /* then fixup the leaf pointer in the path */
  3509. if (path->slots[0] < push_items) {
  3510. path->slots[0] += old_left_nritems;
  3511. btrfs_tree_unlock(path->nodes[0]);
  3512. free_extent_buffer(path->nodes[0]);
  3513. path->nodes[0] = left;
  3514. path->slots[1] -= 1;
  3515. } else {
  3516. btrfs_tree_unlock(left);
  3517. free_extent_buffer(left);
  3518. path->slots[0] -= push_items;
  3519. }
  3520. BUG_ON(path->slots[0] < 0);
  3521. return ret;
  3522. out:
  3523. btrfs_tree_unlock(left);
  3524. free_extent_buffer(left);
  3525. return ret;
  3526. }
  3527. /*
  3528. * push some data in the path leaf to the left, trying to free up at
  3529. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3530. *
  3531. * max_slot can put a limit on how far into the leaf we'll push items. The
  3532. * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
  3533. * items
  3534. */
  3535. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  3536. *root, struct btrfs_path *path, int min_data_size,
  3537. int data_size, int empty, u32 max_slot)
  3538. {
  3539. struct btrfs_fs_info *fs_info = root->fs_info;
  3540. struct extent_buffer *right = path->nodes[0];
  3541. struct extent_buffer *left;
  3542. int slot;
  3543. int free_space;
  3544. u32 right_nritems;
  3545. int ret = 0;
  3546. slot = path->slots[1];
  3547. if (slot == 0)
  3548. return 1;
  3549. if (!path->nodes[1])
  3550. return 1;
  3551. right_nritems = btrfs_header_nritems(right);
  3552. if (right_nritems == 0)
  3553. return 1;
  3554. btrfs_assert_tree_locked(path->nodes[1]);
  3555. left = read_node_slot(fs_info, path->nodes[1], slot - 1);
  3556. /*
  3557. * slot - 1 is not valid or we fail to read the left node,
  3558. * no big deal, just return.
  3559. */
  3560. if (IS_ERR(left))
  3561. return 1;
  3562. btrfs_tree_lock(left);
  3563. btrfs_set_lock_blocking(left);
  3564. free_space = btrfs_leaf_free_space(fs_info, left);
  3565. if (free_space < data_size) {
  3566. ret = 1;
  3567. goto out;
  3568. }
  3569. /* cow and double check */
  3570. ret = btrfs_cow_block(trans, root, left,
  3571. path->nodes[1], slot - 1, &left);
  3572. if (ret) {
  3573. /* we hit -ENOSPC, but it isn't fatal here */
  3574. if (ret == -ENOSPC)
  3575. ret = 1;
  3576. goto out;
  3577. }
  3578. free_space = btrfs_leaf_free_space(fs_info, left);
  3579. if (free_space < data_size) {
  3580. ret = 1;
  3581. goto out;
  3582. }
  3583. return __push_leaf_left(fs_info, path, min_data_size,
  3584. empty, left, free_space, right_nritems,
  3585. max_slot);
  3586. out:
  3587. btrfs_tree_unlock(left);
  3588. free_extent_buffer(left);
  3589. return ret;
  3590. }
  3591. /*
  3592. * split the path's leaf in two, making sure there is at least data_size
  3593. * available for the resulting leaf level of the path.
  3594. */
  3595. static noinline void copy_for_split(struct btrfs_trans_handle *trans,
  3596. struct btrfs_fs_info *fs_info,
  3597. struct btrfs_path *path,
  3598. struct extent_buffer *l,
  3599. struct extent_buffer *right,
  3600. int slot, int mid, int nritems)
  3601. {
  3602. int data_copy_size;
  3603. int rt_data_off;
  3604. int i;
  3605. struct btrfs_disk_key disk_key;
  3606. struct btrfs_map_token token;
  3607. btrfs_init_map_token(&token);
  3608. nritems = nritems - mid;
  3609. btrfs_set_header_nritems(right, nritems);
  3610. data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
  3611. copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
  3612. btrfs_item_nr_offset(mid),
  3613. nritems * sizeof(struct btrfs_item));
  3614. copy_extent_buffer(right, l,
  3615. BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
  3616. data_copy_size, BTRFS_LEAF_DATA_OFFSET +
  3617. leaf_data_end(fs_info, l), data_copy_size);
  3618. rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
  3619. for (i = 0; i < nritems; i++) {
  3620. struct btrfs_item *item = btrfs_item_nr(i);
  3621. u32 ioff;
  3622. ioff = btrfs_token_item_offset(right, item, &token);
  3623. btrfs_set_token_item_offset(right, item,
  3624. ioff + rt_data_off, &token);
  3625. }
  3626. btrfs_set_header_nritems(l, mid);
  3627. btrfs_item_key(right, &disk_key, 0);
  3628. insert_ptr(trans, fs_info, path, &disk_key, right->start,
  3629. path->slots[1] + 1, 1);
  3630. btrfs_mark_buffer_dirty(right);
  3631. btrfs_mark_buffer_dirty(l);
  3632. BUG_ON(path->slots[0] != slot);
  3633. if (mid <= slot) {
  3634. btrfs_tree_unlock(path->nodes[0]);
  3635. free_extent_buffer(path->nodes[0]);
  3636. path->nodes[0] = right;
  3637. path->slots[0] -= mid;
  3638. path->slots[1] += 1;
  3639. } else {
  3640. btrfs_tree_unlock(right);
  3641. free_extent_buffer(right);
  3642. }
  3643. BUG_ON(path->slots[0] < 0);
  3644. }
  3645. /*
  3646. * double splits happen when we need to insert a big item in the middle
  3647. * of a leaf. A double split can leave us with 3 mostly empty leaves:
  3648. * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
  3649. * A B C
  3650. *
  3651. * We avoid this by trying to push the items on either side of our target
  3652. * into the adjacent leaves. If all goes well we can avoid the double split
  3653. * completely.
  3654. */
  3655. static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
  3656. struct btrfs_root *root,
  3657. struct btrfs_path *path,
  3658. int data_size)
  3659. {
  3660. struct btrfs_fs_info *fs_info = root->fs_info;
  3661. int ret;
  3662. int progress = 0;
  3663. int slot;
  3664. u32 nritems;
  3665. int space_needed = data_size;
  3666. slot = path->slots[0];
  3667. if (slot < btrfs_header_nritems(path->nodes[0]))
  3668. space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
  3669. /*
  3670. * try to push all the items after our slot into the
  3671. * right leaf
  3672. */
  3673. ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
  3674. if (ret < 0)
  3675. return ret;
  3676. if (ret == 0)
  3677. progress++;
  3678. nritems = btrfs_header_nritems(path->nodes[0]);
  3679. /*
  3680. * our goal is to get our slot at the start or end of a leaf. If
  3681. * we've done so we're done
  3682. */
  3683. if (path->slots[0] == 0 || path->slots[0] == nritems)
  3684. return 0;
  3685. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
  3686. return 0;
  3687. /* try to push all the items before our slot into the next leaf */
  3688. slot = path->slots[0];
  3689. space_needed = data_size;
  3690. if (slot > 0)
  3691. space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
  3692. ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
  3693. if (ret < 0)
  3694. return ret;
  3695. if (ret == 0)
  3696. progress++;
  3697. if (progress)
  3698. return 0;
  3699. return 1;
  3700. }
  3701. /*
  3702. * split the path's leaf in two, making sure there is at least data_size
  3703. * available for the resulting leaf level of the path.
  3704. *
  3705. * returns 0 if all went well and < 0 on failure.
  3706. */
  3707. static noinline int split_leaf(struct btrfs_trans_handle *trans,
  3708. struct btrfs_root *root,
  3709. const struct btrfs_key *ins_key,
  3710. struct btrfs_path *path, int data_size,
  3711. int extend)
  3712. {
  3713. struct btrfs_disk_key disk_key;
  3714. struct extent_buffer *l;
  3715. u32 nritems;
  3716. int mid;
  3717. int slot;
  3718. struct extent_buffer *right;
  3719. struct btrfs_fs_info *fs_info = root->fs_info;
  3720. int ret = 0;
  3721. int wret;
  3722. int split;
  3723. int num_doubles = 0;
  3724. int tried_avoid_double = 0;
  3725. l = path->nodes[0];
  3726. slot = path->slots[0];
  3727. if (extend && data_size + btrfs_item_size_nr(l, slot) +
  3728. sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
  3729. return -EOVERFLOW;
  3730. /* first try to make some room by pushing left and right */
  3731. if (data_size && path->nodes[1]) {
  3732. int space_needed = data_size;
  3733. if (slot < btrfs_header_nritems(l))
  3734. space_needed -= btrfs_leaf_free_space(fs_info, l);
  3735. wret = push_leaf_right(trans, root, path, space_needed,
  3736. space_needed, 0, 0);
  3737. if (wret < 0)
  3738. return wret;
  3739. if (wret) {
  3740. space_needed = data_size;
  3741. if (slot > 0)
  3742. space_needed -= btrfs_leaf_free_space(fs_info,
  3743. l);
  3744. wret = push_leaf_left(trans, root, path, space_needed,
  3745. space_needed, 0, (u32)-1);
  3746. if (wret < 0)
  3747. return wret;
  3748. }
  3749. l = path->nodes[0];
  3750. /* did the pushes work? */
  3751. if (btrfs_leaf_free_space(fs_info, l) >= data_size)
  3752. return 0;
  3753. }
  3754. if (!path->nodes[1]) {
  3755. ret = insert_new_root(trans, root, path, 1);
  3756. if (ret)
  3757. return ret;
  3758. }
  3759. again:
  3760. split = 1;
  3761. l = path->nodes[0];
  3762. slot = path->slots[0];
  3763. nritems = btrfs_header_nritems(l);
  3764. mid = (nritems + 1) / 2;
  3765. if (mid <= slot) {
  3766. if (nritems == 1 ||
  3767. leaf_space_used(l, mid, nritems - mid) + data_size >
  3768. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3769. if (slot >= nritems) {
  3770. split = 0;
  3771. } else {
  3772. mid = slot;
  3773. if (mid != nritems &&
  3774. leaf_space_used(l, mid, nritems - mid) +
  3775. data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3776. if (data_size && !tried_avoid_double)
  3777. goto push_for_double;
  3778. split = 2;
  3779. }
  3780. }
  3781. }
  3782. } else {
  3783. if (leaf_space_used(l, 0, mid) + data_size >
  3784. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3785. if (!extend && data_size && slot == 0) {
  3786. split = 0;
  3787. } else if ((extend || !data_size) && slot == 0) {
  3788. mid = 1;
  3789. } else {
  3790. mid = slot;
  3791. if (mid != nritems &&
  3792. leaf_space_used(l, mid, nritems - mid) +
  3793. data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3794. if (data_size && !tried_avoid_double)
  3795. goto push_for_double;
  3796. split = 2;
  3797. }
  3798. }
  3799. }
  3800. }
  3801. if (split == 0)
  3802. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  3803. else
  3804. btrfs_item_key(l, &disk_key, mid);
  3805. right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3806. &disk_key, 0, l->start, 0);
  3807. if (IS_ERR(right))
  3808. return PTR_ERR(right);
  3809. root_add_used(root, fs_info->nodesize);
  3810. memzero_extent_buffer(right, 0, sizeof(struct btrfs_header));
  3811. btrfs_set_header_bytenr(right, right->start);
  3812. btrfs_set_header_generation(right, trans->transid);
  3813. btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
  3814. btrfs_set_header_owner(right, root->root_key.objectid);
  3815. btrfs_set_header_level(right, 0);
  3816. write_extent_buffer_fsid(right, fs_info->fsid);
  3817. write_extent_buffer_chunk_tree_uuid(right, fs_info->chunk_tree_uuid);
  3818. if (split == 0) {
  3819. if (mid <= slot) {
  3820. btrfs_set_header_nritems(right, 0);
  3821. insert_ptr(trans, fs_info, path, &disk_key,
  3822. right->start, path->slots[1] + 1, 1);
  3823. btrfs_tree_unlock(path->nodes[0]);
  3824. free_extent_buffer(path->nodes[0]);
  3825. path->nodes[0] = right;
  3826. path->slots[0] = 0;
  3827. path->slots[1] += 1;
  3828. } else {
  3829. btrfs_set_header_nritems(right, 0);
  3830. insert_ptr(trans, fs_info, path, &disk_key,
  3831. right->start, path->slots[1], 1);
  3832. btrfs_tree_unlock(path->nodes[0]);
  3833. free_extent_buffer(path->nodes[0]);
  3834. path->nodes[0] = right;
  3835. path->slots[0] = 0;
  3836. if (path->slots[1] == 0)
  3837. fixup_low_keys(fs_info, path, &disk_key, 1);
  3838. }
  3839. /*
  3840. * We create a new leaf 'right' for the required ins_len and
  3841. * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
  3842. * the content of ins_len to 'right'.
  3843. */
  3844. return ret;
  3845. }
  3846. copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
  3847. if (split == 2) {
  3848. BUG_ON(num_doubles != 0);
  3849. num_doubles++;
  3850. goto again;
  3851. }
  3852. return 0;
  3853. push_for_double:
  3854. push_for_double_split(trans, root, path, data_size);
  3855. tried_avoid_double = 1;
  3856. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
  3857. return 0;
  3858. goto again;
  3859. }
  3860. static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
  3861. struct btrfs_root *root,
  3862. struct btrfs_path *path, int ins_len)
  3863. {
  3864. struct btrfs_fs_info *fs_info = root->fs_info;
  3865. struct btrfs_key key;
  3866. struct extent_buffer *leaf;
  3867. struct btrfs_file_extent_item *fi;
  3868. u64 extent_len = 0;
  3869. u32 item_size;
  3870. int ret;
  3871. leaf = path->nodes[0];
  3872. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3873. BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
  3874. key.type != BTRFS_EXTENT_CSUM_KEY);
  3875. if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
  3876. return 0;
  3877. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3878. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3879. fi = btrfs_item_ptr(leaf, path->slots[0],
  3880. struct btrfs_file_extent_item);
  3881. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  3882. }
  3883. btrfs_release_path(path);
  3884. path->keep_locks = 1;
  3885. path->search_for_split = 1;
  3886. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3887. path->search_for_split = 0;
  3888. if (ret > 0)
  3889. ret = -EAGAIN;
  3890. if (ret < 0)
  3891. goto err;
  3892. ret = -EAGAIN;
  3893. leaf = path->nodes[0];
  3894. /* if our item isn't there, return now */
  3895. if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
  3896. goto err;
  3897. /* the leaf has changed, it now has room. return now */
  3898. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
  3899. goto err;
  3900. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3901. fi = btrfs_item_ptr(leaf, path->slots[0],
  3902. struct btrfs_file_extent_item);
  3903. if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
  3904. goto err;
  3905. }
  3906. btrfs_set_path_blocking(path);
  3907. ret = split_leaf(trans, root, &key, path, ins_len, 1);
  3908. if (ret)
  3909. goto err;
  3910. path->keep_locks = 0;
  3911. btrfs_unlock_up_safe(path, 1);
  3912. return 0;
  3913. err:
  3914. path->keep_locks = 0;
  3915. return ret;
  3916. }
  3917. static noinline int split_item(struct btrfs_fs_info *fs_info,
  3918. struct btrfs_path *path,
  3919. const struct btrfs_key *new_key,
  3920. unsigned long split_offset)
  3921. {
  3922. struct extent_buffer *leaf;
  3923. struct btrfs_item *item;
  3924. struct btrfs_item *new_item;
  3925. int slot;
  3926. char *buf;
  3927. u32 nritems;
  3928. u32 item_size;
  3929. u32 orig_offset;
  3930. struct btrfs_disk_key disk_key;
  3931. leaf = path->nodes[0];
  3932. BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
  3933. btrfs_set_path_blocking(path);
  3934. item = btrfs_item_nr(path->slots[0]);
  3935. orig_offset = btrfs_item_offset(leaf, item);
  3936. item_size = btrfs_item_size(leaf, item);
  3937. buf = kmalloc(item_size, GFP_NOFS);
  3938. if (!buf)
  3939. return -ENOMEM;
  3940. read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
  3941. path->slots[0]), item_size);
  3942. slot = path->slots[0] + 1;
  3943. nritems = btrfs_header_nritems(leaf);
  3944. if (slot != nritems) {
  3945. /* shift the items */
  3946. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
  3947. btrfs_item_nr_offset(slot),
  3948. (nritems - slot) * sizeof(struct btrfs_item));
  3949. }
  3950. btrfs_cpu_key_to_disk(&disk_key, new_key);
  3951. btrfs_set_item_key(leaf, &disk_key, slot);
  3952. new_item = btrfs_item_nr(slot);
  3953. btrfs_set_item_offset(leaf, new_item, orig_offset);
  3954. btrfs_set_item_size(leaf, new_item, item_size - split_offset);
  3955. btrfs_set_item_offset(leaf, item,
  3956. orig_offset + item_size - split_offset);
  3957. btrfs_set_item_size(leaf, item, split_offset);
  3958. btrfs_set_header_nritems(leaf, nritems + 1);
  3959. /* write the data for the start of the original item */
  3960. write_extent_buffer(leaf, buf,
  3961. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3962. split_offset);
  3963. /* write the data for the new item */
  3964. write_extent_buffer(leaf, buf + split_offset,
  3965. btrfs_item_ptr_offset(leaf, slot),
  3966. item_size - split_offset);
  3967. btrfs_mark_buffer_dirty(leaf);
  3968. BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
  3969. kfree(buf);
  3970. return 0;
  3971. }
  3972. /*
  3973. * This function splits a single item into two items,
  3974. * giving 'new_key' to the new item and splitting the
  3975. * old one at split_offset (from the start of the item).
  3976. *
  3977. * The path may be released by this operation. After
  3978. * the split, the path is pointing to the old item. The
  3979. * new item is going to be in the same node as the old one.
  3980. *
  3981. * Note, the item being split must be smaller enough to live alone on
  3982. * a tree block with room for one extra struct btrfs_item
  3983. *
  3984. * This allows us to split the item in place, keeping a lock on the
  3985. * leaf the entire time.
  3986. */
  3987. int btrfs_split_item(struct btrfs_trans_handle *trans,
  3988. struct btrfs_root *root,
  3989. struct btrfs_path *path,
  3990. const struct btrfs_key *new_key,
  3991. unsigned long split_offset)
  3992. {
  3993. int ret;
  3994. ret = setup_leaf_for_split(trans, root, path,
  3995. sizeof(struct btrfs_item));
  3996. if (ret)
  3997. return ret;
  3998. ret = split_item(root->fs_info, path, new_key, split_offset);
  3999. return ret;
  4000. }
  4001. /*
  4002. * This function duplicate a item, giving 'new_key' to the new item.
  4003. * It guarantees both items live in the same tree leaf and the new item
  4004. * is contiguous with the original item.
  4005. *
  4006. * This allows us to split file extent in place, keeping a lock on the
  4007. * leaf the entire time.
  4008. */
  4009. int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
  4010. struct btrfs_root *root,
  4011. struct btrfs_path *path,
  4012. const struct btrfs_key *new_key)
  4013. {
  4014. struct extent_buffer *leaf;
  4015. int ret;
  4016. u32 item_size;
  4017. leaf = path->nodes[0];
  4018. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  4019. ret = setup_leaf_for_split(trans, root, path,
  4020. item_size + sizeof(struct btrfs_item));
  4021. if (ret)
  4022. return ret;
  4023. path->slots[0]++;
  4024. setup_items_for_insert(root, path, new_key, &item_size,
  4025. item_size, item_size +
  4026. sizeof(struct btrfs_item), 1);
  4027. leaf = path->nodes[0];
  4028. memcpy_extent_buffer(leaf,
  4029. btrfs_item_ptr_offset(leaf, path->slots[0]),
  4030. btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
  4031. item_size);
  4032. return 0;
  4033. }
  4034. /*
  4035. * make the item pointed to by the path smaller. new_size indicates
  4036. * how small to make it, and from_end tells us if we just chop bytes
  4037. * off the end of the item or if we shift the item to chop bytes off
  4038. * the front.
  4039. */
  4040. void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
  4041. struct btrfs_path *path, u32 new_size, int from_end)
  4042. {
  4043. int slot;
  4044. struct extent_buffer *leaf;
  4045. struct btrfs_item *item;
  4046. u32 nritems;
  4047. unsigned int data_end;
  4048. unsigned int old_data_start;
  4049. unsigned int old_size;
  4050. unsigned int size_diff;
  4051. int i;
  4052. struct btrfs_map_token token;
  4053. btrfs_init_map_token(&token);
  4054. leaf = path->nodes[0];
  4055. slot = path->slots[0];
  4056. old_size = btrfs_item_size_nr(leaf, slot);
  4057. if (old_size == new_size)
  4058. return;
  4059. nritems = btrfs_header_nritems(leaf);
  4060. data_end = leaf_data_end(fs_info, leaf);
  4061. old_data_start = btrfs_item_offset_nr(leaf, slot);
  4062. size_diff = old_size - new_size;
  4063. BUG_ON(slot < 0);
  4064. BUG_ON(slot >= nritems);
  4065. /*
  4066. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4067. */
  4068. /* first correct the data pointers */
  4069. for (i = slot; i < nritems; i++) {
  4070. u32 ioff;
  4071. item = btrfs_item_nr(i);
  4072. ioff = btrfs_token_item_offset(leaf, item, &token);
  4073. btrfs_set_token_item_offset(leaf, item,
  4074. ioff + size_diff, &token);
  4075. }
  4076. /* shift the data */
  4077. if (from_end) {
  4078. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4079. data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
  4080. data_end, old_data_start + new_size - data_end);
  4081. } else {
  4082. struct btrfs_disk_key disk_key;
  4083. u64 offset;
  4084. btrfs_item_key(leaf, &disk_key, slot);
  4085. if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
  4086. unsigned long ptr;
  4087. struct btrfs_file_extent_item *fi;
  4088. fi = btrfs_item_ptr(leaf, slot,
  4089. struct btrfs_file_extent_item);
  4090. fi = (struct btrfs_file_extent_item *)(
  4091. (unsigned long)fi - size_diff);
  4092. if (btrfs_file_extent_type(leaf, fi) ==
  4093. BTRFS_FILE_EXTENT_INLINE) {
  4094. ptr = btrfs_item_ptr_offset(leaf, slot);
  4095. memmove_extent_buffer(leaf, ptr,
  4096. (unsigned long)fi,
  4097. BTRFS_FILE_EXTENT_INLINE_DATA_START);
  4098. }
  4099. }
  4100. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4101. data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
  4102. data_end, old_data_start - data_end);
  4103. offset = btrfs_disk_key_offset(&disk_key);
  4104. btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
  4105. btrfs_set_item_key(leaf, &disk_key, slot);
  4106. if (slot == 0)
  4107. fixup_low_keys(fs_info, path, &disk_key, 1);
  4108. }
  4109. item = btrfs_item_nr(slot);
  4110. btrfs_set_item_size(leaf, item, new_size);
  4111. btrfs_mark_buffer_dirty(leaf);
  4112. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4113. btrfs_print_leaf(leaf);
  4114. BUG();
  4115. }
  4116. }
  4117. /*
  4118. * make the item pointed to by the path bigger, data_size is the added size.
  4119. */
  4120. void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  4121. u32 data_size)
  4122. {
  4123. int slot;
  4124. struct extent_buffer *leaf;
  4125. struct btrfs_item *item;
  4126. u32 nritems;
  4127. unsigned int data_end;
  4128. unsigned int old_data;
  4129. unsigned int old_size;
  4130. int i;
  4131. struct btrfs_map_token token;
  4132. btrfs_init_map_token(&token);
  4133. leaf = path->nodes[0];
  4134. nritems = btrfs_header_nritems(leaf);
  4135. data_end = leaf_data_end(fs_info, leaf);
  4136. if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
  4137. btrfs_print_leaf(leaf);
  4138. BUG();
  4139. }
  4140. slot = path->slots[0];
  4141. old_data = btrfs_item_end_nr(leaf, slot);
  4142. BUG_ON(slot < 0);
  4143. if (slot >= nritems) {
  4144. btrfs_print_leaf(leaf);
  4145. btrfs_crit(fs_info, "slot %d too large, nritems %d",
  4146. slot, nritems);
  4147. BUG_ON(1);
  4148. }
  4149. /*
  4150. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4151. */
  4152. /* first correct the data pointers */
  4153. for (i = slot; i < nritems; i++) {
  4154. u32 ioff;
  4155. item = btrfs_item_nr(i);
  4156. ioff = btrfs_token_item_offset(leaf, item, &token);
  4157. btrfs_set_token_item_offset(leaf, item,
  4158. ioff - data_size, &token);
  4159. }
  4160. /* shift the data */
  4161. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4162. data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
  4163. data_end, old_data - data_end);
  4164. data_end = old_data;
  4165. old_size = btrfs_item_size_nr(leaf, slot);
  4166. item = btrfs_item_nr(slot);
  4167. btrfs_set_item_size(leaf, item, old_size + data_size);
  4168. btrfs_mark_buffer_dirty(leaf);
  4169. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4170. btrfs_print_leaf(leaf);
  4171. BUG();
  4172. }
  4173. }
  4174. /*
  4175. * this is a helper for btrfs_insert_empty_items, the main goal here is
  4176. * to save stack depth by doing the bulk of the work in a function
  4177. * that doesn't call btrfs_search_slot
  4178. */
  4179. void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
  4180. const struct btrfs_key *cpu_key, u32 *data_size,
  4181. u32 total_data, u32 total_size, int nr)
  4182. {
  4183. struct btrfs_fs_info *fs_info = root->fs_info;
  4184. struct btrfs_item *item;
  4185. int i;
  4186. u32 nritems;
  4187. unsigned int data_end;
  4188. struct btrfs_disk_key disk_key;
  4189. struct extent_buffer *leaf;
  4190. int slot;
  4191. struct btrfs_map_token token;
  4192. if (path->slots[0] == 0) {
  4193. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  4194. fixup_low_keys(fs_info, path, &disk_key, 1);
  4195. }
  4196. btrfs_unlock_up_safe(path, 1);
  4197. btrfs_init_map_token(&token);
  4198. leaf = path->nodes[0];
  4199. slot = path->slots[0];
  4200. nritems = btrfs_header_nritems(leaf);
  4201. data_end = leaf_data_end(fs_info, leaf);
  4202. if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
  4203. btrfs_print_leaf(leaf);
  4204. btrfs_crit(fs_info, "not enough freespace need %u have %d",
  4205. total_size, btrfs_leaf_free_space(fs_info, leaf));
  4206. BUG();
  4207. }
  4208. if (slot != nritems) {
  4209. unsigned int old_data = btrfs_item_end_nr(leaf, slot);
  4210. if (old_data < data_end) {
  4211. btrfs_print_leaf(leaf);
  4212. btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
  4213. slot, old_data, data_end);
  4214. BUG_ON(1);
  4215. }
  4216. /*
  4217. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4218. */
  4219. /* first correct the data pointers */
  4220. for (i = slot; i < nritems; i++) {
  4221. u32 ioff;
  4222. item = btrfs_item_nr(i);
  4223. ioff = btrfs_token_item_offset(leaf, item, &token);
  4224. btrfs_set_token_item_offset(leaf, item,
  4225. ioff - total_data, &token);
  4226. }
  4227. /* shift the items */
  4228. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
  4229. btrfs_item_nr_offset(slot),
  4230. (nritems - slot) * sizeof(struct btrfs_item));
  4231. /* shift the data */
  4232. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4233. data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
  4234. data_end, old_data - data_end);
  4235. data_end = old_data;
  4236. }
  4237. /* setup the item for the new data */
  4238. for (i = 0; i < nr; i++) {
  4239. btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
  4240. btrfs_set_item_key(leaf, &disk_key, slot + i);
  4241. item = btrfs_item_nr(slot + i);
  4242. btrfs_set_token_item_offset(leaf, item,
  4243. data_end - data_size[i], &token);
  4244. data_end -= data_size[i];
  4245. btrfs_set_token_item_size(leaf, item, data_size[i], &token);
  4246. }
  4247. btrfs_set_header_nritems(leaf, nritems + nr);
  4248. btrfs_mark_buffer_dirty(leaf);
  4249. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4250. btrfs_print_leaf(leaf);
  4251. BUG();
  4252. }
  4253. }
  4254. /*
  4255. * Given a key and some data, insert items into the tree.
  4256. * This does all the path init required, making room in the tree if needed.
  4257. */
  4258. int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
  4259. struct btrfs_root *root,
  4260. struct btrfs_path *path,
  4261. const struct btrfs_key *cpu_key, u32 *data_size,
  4262. int nr)
  4263. {
  4264. int ret = 0;
  4265. int slot;
  4266. int i;
  4267. u32 total_size = 0;
  4268. u32 total_data = 0;
  4269. for (i = 0; i < nr; i++)
  4270. total_data += data_size[i];
  4271. total_size = total_data + (nr * sizeof(struct btrfs_item));
  4272. ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
  4273. if (ret == 0)
  4274. return -EEXIST;
  4275. if (ret < 0)
  4276. return ret;
  4277. slot = path->slots[0];
  4278. BUG_ON(slot < 0);
  4279. setup_items_for_insert(root, path, cpu_key, data_size,
  4280. total_data, total_size, nr);
  4281. return 0;
  4282. }
  4283. /*
  4284. * Given a key and some data, insert an item into the tree.
  4285. * This does all the path init required, making room in the tree if needed.
  4286. */
  4287. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4288. const struct btrfs_key *cpu_key, void *data,
  4289. u32 data_size)
  4290. {
  4291. int ret = 0;
  4292. struct btrfs_path *path;
  4293. struct extent_buffer *leaf;
  4294. unsigned long ptr;
  4295. path = btrfs_alloc_path();
  4296. if (!path)
  4297. return -ENOMEM;
  4298. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  4299. if (!ret) {
  4300. leaf = path->nodes[0];
  4301. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  4302. write_extent_buffer(leaf, data, ptr, data_size);
  4303. btrfs_mark_buffer_dirty(leaf);
  4304. }
  4305. btrfs_free_path(path);
  4306. return ret;
  4307. }
  4308. /*
  4309. * delete the pointer from a given node.
  4310. *
  4311. * the tree should have been previously balanced so the deletion does not
  4312. * empty a node.
  4313. */
  4314. static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
  4315. int level, int slot)
  4316. {
  4317. struct btrfs_fs_info *fs_info = root->fs_info;
  4318. struct extent_buffer *parent = path->nodes[level];
  4319. u32 nritems;
  4320. int ret;
  4321. nritems = btrfs_header_nritems(parent);
  4322. if (slot != nritems - 1) {
  4323. if (level)
  4324. tree_mod_log_eb_move(fs_info, parent, slot,
  4325. slot + 1, nritems - slot - 1);
  4326. memmove_extent_buffer(parent,
  4327. btrfs_node_key_ptr_offset(slot),
  4328. btrfs_node_key_ptr_offset(slot + 1),
  4329. sizeof(struct btrfs_key_ptr) *
  4330. (nritems - slot - 1));
  4331. } else if (level) {
  4332. ret = tree_mod_log_insert_key(fs_info, parent, slot,
  4333. MOD_LOG_KEY_REMOVE, GFP_NOFS);
  4334. BUG_ON(ret < 0);
  4335. }
  4336. nritems--;
  4337. btrfs_set_header_nritems(parent, nritems);
  4338. if (nritems == 0 && parent == root->node) {
  4339. BUG_ON(btrfs_header_level(root->node) != 1);
  4340. /* just turn the root into a leaf and break */
  4341. btrfs_set_header_level(root->node, 0);
  4342. } else if (slot == 0) {
  4343. struct btrfs_disk_key disk_key;
  4344. btrfs_node_key(parent, &disk_key, 0);
  4345. fixup_low_keys(fs_info, path, &disk_key, level + 1);
  4346. }
  4347. btrfs_mark_buffer_dirty(parent);
  4348. }
  4349. /*
  4350. * a helper function to delete the leaf pointed to by path->slots[1] and
  4351. * path->nodes[1].
  4352. *
  4353. * This deletes the pointer in path->nodes[1] and frees the leaf
  4354. * block extent. zero is returned if it all worked out, < 0 otherwise.
  4355. *
  4356. * The path must have already been setup for deleting the leaf, including
  4357. * all the proper balancing. path->nodes[1] must be locked.
  4358. */
  4359. static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
  4360. struct btrfs_root *root,
  4361. struct btrfs_path *path,
  4362. struct extent_buffer *leaf)
  4363. {
  4364. WARN_ON(btrfs_header_generation(leaf) != trans->transid);
  4365. del_ptr(root, path, 1, path->slots[1]);
  4366. /*
  4367. * btrfs_free_extent is expensive, we want to make sure we
  4368. * aren't holding any locks when we call it
  4369. */
  4370. btrfs_unlock_up_safe(path, 0);
  4371. root_sub_used(root, leaf->len);
  4372. extent_buffer_get(leaf);
  4373. btrfs_free_tree_block(trans, root, leaf, 0, 1);
  4374. free_extent_buffer_stale(leaf);
  4375. }
  4376. /*
  4377. * delete the item at the leaf level in path. If that empties
  4378. * the leaf, remove it from the tree
  4379. */
  4380. int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4381. struct btrfs_path *path, int slot, int nr)
  4382. {
  4383. struct btrfs_fs_info *fs_info = root->fs_info;
  4384. struct extent_buffer *leaf;
  4385. struct btrfs_item *item;
  4386. u32 last_off;
  4387. u32 dsize = 0;
  4388. int ret = 0;
  4389. int wret;
  4390. int i;
  4391. u32 nritems;
  4392. struct btrfs_map_token token;
  4393. btrfs_init_map_token(&token);
  4394. leaf = path->nodes[0];
  4395. last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
  4396. for (i = 0; i < nr; i++)
  4397. dsize += btrfs_item_size_nr(leaf, slot + i);
  4398. nritems = btrfs_header_nritems(leaf);
  4399. if (slot + nr != nritems) {
  4400. int data_end = leaf_data_end(fs_info, leaf);
  4401. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4402. data_end + dsize,
  4403. BTRFS_LEAF_DATA_OFFSET + data_end,
  4404. last_off - data_end);
  4405. for (i = slot + nr; i < nritems; i++) {
  4406. u32 ioff;
  4407. item = btrfs_item_nr(i);
  4408. ioff = btrfs_token_item_offset(leaf, item, &token);
  4409. btrfs_set_token_item_offset(leaf, item,
  4410. ioff + dsize, &token);
  4411. }
  4412. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
  4413. btrfs_item_nr_offset(slot + nr),
  4414. sizeof(struct btrfs_item) *
  4415. (nritems - slot - nr));
  4416. }
  4417. btrfs_set_header_nritems(leaf, nritems - nr);
  4418. nritems -= nr;
  4419. /* delete the leaf if we've emptied it */
  4420. if (nritems == 0) {
  4421. if (leaf == root->node) {
  4422. btrfs_set_header_level(leaf, 0);
  4423. } else {
  4424. btrfs_set_path_blocking(path);
  4425. clean_tree_block(fs_info, leaf);
  4426. btrfs_del_leaf(trans, root, path, leaf);
  4427. }
  4428. } else {
  4429. int used = leaf_space_used(leaf, 0, nritems);
  4430. if (slot == 0) {
  4431. struct btrfs_disk_key disk_key;
  4432. btrfs_item_key(leaf, &disk_key, 0);
  4433. fixup_low_keys(fs_info, path, &disk_key, 1);
  4434. }
  4435. /* delete the leaf if it is mostly empty */
  4436. if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
  4437. /* push_leaf_left fixes the path.
  4438. * make sure the path still points to our leaf
  4439. * for possible call to del_ptr below
  4440. */
  4441. slot = path->slots[1];
  4442. extent_buffer_get(leaf);
  4443. btrfs_set_path_blocking(path);
  4444. wret = push_leaf_left(trans, root, path, 1, 1,
  4445. 1, (u32)-1);
  4446. if (wret < 0 && wret != -ENOSPC)
  4447. ret = wret;
  4448. if (path->nodes[0] == leaf &&
  4449. btrfs_header_nritems(leaf)) {
  4450. wret = push_leaf_right(trans, root, path, 1,
  4451. 1, 1, 0);
  4452. if (wret < 0 && wret != -ENOSPC)
  4453. ret = wret;
  4454. }
  4455. if (btrfs_header_nritems(leaf) == 0) {
  4456. path->slots[1] = slot;
  4457. btrfs_del_leaf(trans, root, path, leaf);
  4458. free_extent_buffer(leaf);
  4459. ret = 0;
  4460. } else {
  4461. /* if we're still in the path, make sure
  4462. * we're dirty. Otherwise, one of the
  4463. * push_leaf functions must have already
  4464. * dirtied this buffer
  4465. */
  4466. if (path->nodes[0] == leaf)
  4467. btrfs_mark_buffer_dirty(leaf);
  4468. free_extent_buffer(leaf);
  4469. }
  4470. } else {
  4471. btrfs_mark_buffer_dirty(leaf);
  4472. }
  4473. }
  4474. return ret;
  4475. }
  4476. /*
  4477. * search the tree again to find a leaf with lesser keys
  4478. * returns 0 if it found something or 1 if there are no lesser leaves.
  4479. * returns < 0 on io errors.
  4480. *
  4481. * This may release the path, and so you may lose any locks held at the
  4482. * time you call it.
  4483. */
  4484. int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
  4485. {
  4486. struct btrfs_key key;
  4487. struct btrfs_disk_key found_key;
  4488. int ret;
  4489. btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
  4490. if (key.offset > 0) {
  4491. key.offset--;
  4492. } else if (key.type > 0) {
  4493. key.type--;
  4494. key.offset = (u64)-1;
  4495. } else if (key.objectid > 0) {
  4496. key.objectid--;
  4497. key.type = (u8)-1;
  4498. key.offset = (u64)-1;
  4499. } else {
  4500. return 1;
  4501. }
  4502. btrfs_release_path(path);
  4503. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4504. if (ret < 0)
  4505. return ret;
  4506. btrfs_item_key(path->nodes[0], &found_key, 0);
  4507. ret = comp_keys(&found_key, &key);
  4508. /*
  4509. * We might have had an item with the previous key in the tree right
  4510. * before we released our path. And after we released our path, that
  4511. * item might have been pushed to the first slot (0) of the leaf we
  4512. * were holding due to a tree balance. Alternatively, an item with the
  4513. * previous key can exist as the only element of a leaf (big fat item).
  4514. * Therefore account for these 2 cases, so that our callers (like
  4515. * btrfs_previous_item) don't miss an existing item with a key matching
  4516. * the previous key we computed above.
  4517. */
  4518. if (ret <= 0)
  4519. return 0;
  4520. return 1;
  4521. }
  4522. /*
  4523. * A helper function to walk down the tree starting at min_key, and looking
  4524. * for nodes or leaves that are have a minimum transaction id.
  4525. * This is used by the btree defrag code, and tree logging
  4526. *
  4527. * This does not cow, but it does stuff the starting key it finds back
  4528. * into min_key, so you can call btrfs_search_slot with cow=1 on the
  4529. * key and get a writable path.
  4530. *
  4531. * This does lock as it descends, and path->keep_locks should be set
  4532. * to 1 by the caller.
  4533. *
  4534. * This honors path->lowest_level to prevent descent past a given level
  4535. * of the tree.
  4536. *
  4537. * min_trans indicates the oldest transaction that you are interested
  4538. * in walking through. Any nodes or leaves older than min_trans are
  4539. * skipped over (without reading them).
  4540. *
  4541. * returns zero if something useful was found, < 0 on error and 1 if there
  4542. * was nothing in the tree that matched the search criteria.
  4543. */
  4544. int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
  4545. struct btrfs_path *path,
  4546. u64 min_trans)
  4547. {
  4548. struct btrfs_fs_info *fs_info = root->fs_info;
  4549. struct extent_buffer *cur;
  4550. struct btrfs_key found_key;
  4551. int slot;
  4552. int sret;
  4553. u32 nritems;
  4554. int level;
  4555. int ret = 1;
  4556. int keep_locks = path->keep_locks;
  4557. path->keep_locks = 1;
  4558. again:
  4559. cur = btrfs_read_lock_root_node(root);
  4560. level = btrfs_header_level(cur);
  4561. WARN_ON(path->nodes[level]);
  4562. path->nodes[level] = cur;
  4563. path->locks[level] = BTRFS_READ_LOCK;
  4564. if (btrfs_header_generation(cur) < min_trans) {
  4565. ret = 1;
  4566. goto out;
  4567. }
  4568. while (1) {
  4569. nritems = btrfs_header_nritems(cur);
  4570. level = btrfs_header_level(cur);
  4571. sret = btrfs_bin_search(cur, min_key, level, &slot);
  4572. /* at the lowest level, we're done, setup the path and exit */
  4573. if (level == path->lowest_level) {
  4574. if (slot >= nritems)
  4575. goto find_next_key;
  4576. ret = 0;
  4577. path->slots[level] = slot;
  4578. btrfs_item_key_to_cpu(cur, &found_key, slot);
  4579. goto out;
  4580. }
  4581. if (sret && slot > 0)
  4582. slot--;
  4583. /*
  4584. * check this node pointer against the min_trans parameters.
  4585. * If it is too old, old, skip to the next one.
  4586. */
  4587. while (slot < nritems) {
  4588. u64 gen;
  4589. gen = btrfs_node_ptr_generation(cur, slot);
  4590. if (gen < min_trans) {
  4591. slot++;
  4592. continue;
  4593. }
  4594. break;
  4595. }
  4596. find_next_key:
  4597. /*
  4598. * we didn't find a candidate key in this node, walk forward
  4599. * and find another one
  4600. */
  4601. if (slot >= nritems) {
  4602. path->slots[level] = slot;
  4603. btrfs_set_path_blocking(path);
  4604. sret = btrfs_find_next_key(root, path, min_key, level,
  4605. min_trans);
  4606. if (sret == 0) {
  4607. btrfs_release_path(path);
  4608. goto again;
  4609. } else {
  4610. goto out;
  4611. }
  4612. }
  4613. /* save our key for returning back */
  4614. btrfs_node_key_to_cpu(cur, &found_key, slot);
  4615. path->slots[level] = slot;
  4616. if (level == path->lowest_level) {
  4617. ret = 0;
  4618. goto out;
  4619. }
  4620. btrfs_set_path_blocking(path);
  4621. cur = read_node_slot(fs_info, cur, slot);
  4622. if (IS_ERR(cur)) {
  4623. ret = PTR_ERR(cur);
  4624. goto out;
  4625. }
  4626. btrfs_tree_read_lock(cur);
  4627. path->locks[level - 1] = BTRFS_READ_LOCK;
  4628. path->nodes[level - 1] = cur;
  4629. unlock_up(path, level, 1, 0, NULL);
  4630. btrfs_clear_path_blocking(path, NULL, 0);
  4631. }
  4632. out:
  4633. path->keep_locks = keep_locks;
  4634. if (ret == 0) {
  4635. btrfs_unlock_up_safe(path, path->lowest_level + 1);
  4636. btrfs_set_path_blocking(path);
  4637. memcpy(min_key, &found_key, sizeof(found_key));
  4638. }
  4639. return ret;
  4640. }
  4641. static int tree_move_down(struct btrfs_fs_info *fs_info,
  4642. struct btrfs_path *path,
  4643. int *level)
  4644. {
  4645. struct extent_buffer *eb;
  4646. BUG_ON(*level == 0);
  4647. eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
  4648. if (IS_ERR(eb))
  4649. return PTR_ERR(eb);
  4650. path->nodes[*level - 1] = eb;
  4651. path->slots[*level - 1] = 0;
  4652. (*level)--;
  4653. return 0;
  4654. }
  4655. static int tree_move_next_or_upnext(struct btrfs_path *path,
  4656. int *level, int root_level)
  4657. {
  4658. int ret = 0;
  4659. int nritems;
  4660. nritems = btrfs_header_nritems(path->nodes[*level]);
  4661. path->slots[*level]++;
  4662. while (path->slots[*level] >= nritems) {
  4663. if (*level == root_level)
  4664. return -1;
  4665. /* move upnext */
  4666. path->slots[*level] = 0;
  4667. free_extent_buffer(path->nodes[*level]);
  4668. path->nodes[*level] = NULL;
  4669. (*level)++;
  4670. path->slots[*level]++;
  4671. nritems = btrfs_header_nritems(path->nodes[*level]);
  4672. ret = 1;
  4673. }
  4674. return ret;
  4675. }
  4676. /*
  4677. * Returns 1 if it had to move up and next. 0 is returned if it moved only next
  4678. * or down.
  4679. */
  4680. static int tree_advance(struct btrfs_fs_info *fs_info,
  4681. struct btrfs_path *path,
  4682. int *level, int root_level,
  4683. int allow_down,
  4684. struct btrfs_key *key)
  4685. {
  4686. int ret;
  4687. if (*level == 0 || !allow_down) {
  4688. ret = tree_move_next_or_upnext(path, level, root_level);
  4689. } else {
  4690. ret = tree_move_down(fs_info, path, level);
  4691. }
  4692. if (ret >= 0) {
  4693. if (*level == 0)
  4694. btrfs_item_key_to_cpu(path->nodes[*level], key,
  4695. path->slots[*level]);
  4696. else
  4697. btrfs_node_key_to_cpu(path->nodes[*level], key,
  4698. path->slots[*level]);
  4699. }
  4700. return ret;
  4701. }
  4702. static int tree_compare_item(struct btrfs_path *left_path,
  4703. struct btrfs_path *right_path,
  4704. char *tmp_buf)
  4705. {
  4706. int cmp;
  4707. int len1, len2;
  4708. unsigned long off1, off2;
  4709. len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
  4710. len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
  4711. if (len1 != len2)
  4712. return 1;
  4713. off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
  4714. off2 = btrfs_item_ptr_offset(right_path->nodes[0],
  4715. right_path->slots[0]);
  4716. read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
  4717. cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
  4718. if (cmp)
  4719. return 1;
  4720. return 0;
  4721. }
  4722. #define ADVANCE 1
  4723. #define ADVANCE_ONLY_NEXT -1
  4724. /*
  4725. * This function compares two trees and calls the provided callback for
  4726. * every changed/new/deleted item it finds.
  4727. * If shared tree blocks are encountered, whole subtrees are skipped, making
  4728. * the compare pretty fast on snapshotted subvolumes.
  4729. *
  4730. * This currently works on commit roots only. As commit roots are read only,
  4731. * we don't do any locking. The commit roots are protected with transactions.
  4732. * Transactions are ended and rejoined when a commit is tried in between.
  4733. *
  4734. * This function checks for modifications done to the trees while comparing.
  4735. * If it detects a change, it aborts immediately.
  4736. */
  4737. int btrfs_compare_trees(struct btrfs_root *left_root,
  4738. struct btrfs_root *right_root,
  4739. btrfs_changed_cb_t changed_cb, void *ctx)
  4740. {
  4741. struct btrfs_fs_info *fs_info = left_root->fs_info;
  4742. int ret;
  4743. int cmp;
  4744. struct btrfs_path *left_path = NULL;
  4745. struct btrfs_path *right_path = NULL;
  4746. struct btrfs_key left_key;
  4747. struct btrfs_key right_key;
  4748. char *tmp_buf = NULL;
  4749. int left_root_level;
  4750. int right_root_level;
  4751. int left_level;
  4752. int right_level;
  4753. int left_end_reached;
  4754. int right_end_reached;
  4755. int advance_left;
  4756. int advance_right;
  4757. u64 left_blockptr;
  4758. u64 right_blockptr;
  4759. u64 left_gen;
  4760. u64 right_gen;
  4761. left_path = btrfs_alloc_path();
  4762. if (!left_path) {
  4763. ret = -ENOMEM;
  4764. goto out;
  4765. }
  4766. right_path = btrfs_alloc_path();
  4767. if (!right_path) {
  4768. ret = -ENOMEM;
  4769. goto out;
  4770. }
  4771. tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
  4772. if (!tmp_buf) {
  4773. ret = -ENOMEM;
  4774. goto out;
  4775. }
  4776. left_path->search_commit_root = 1;
  4777. left_path->skip_locking = 1;
  4778. right_path->search_commit_root = 1;
  4779. right_path->skip_locking = 1;
  4780. /*
  4781. * Strategy: Go to the first items of both trees. Then do
  4782. *
  4783. * If both trees are at level 0
  4784. * Compare keys of current items
  4785. * If left < right treat left item as new, advance left tree
  4786. * and repeat
  4787. * If left > right treat right item as deleted, advance right tree
  4788. * and repeat
  4789. * If left == right do deep compare of items, treat as changed if
  4790. * needed, advance both trees and repeat
  4791. * If both trees are at the same level but not at level 0
  4792. * Compare keys of current nodes/leafs
  4793. * If left < right advance left tree and repeat
  4794. * If left > right advance right tree and repeat
  4795. * If left == right compare blockptrs of the next nodes/leafs
  4796. * If they match advance both trees but stay at the same level
  4797. * and repeat
  4798. * If they don't match advance both trees while allowing to go
  4799. * deeper and repeat
  4800. * If tree levels are different
  4801. * Advance the tree that needs it and repeat
  4802. *
  4803. * Advancing a tree means:
  4804. * If we are at level 0, try to go to the next slot. If that's not
  4805. * possible, go one level up and repeat. Stop when we found a level
  4806. * where we could go to the next slot. We may at this point be on a
  4807. * node or a leaf.
  4808. *
  4809. * If we are not at level 0 and not on shared tree blocks, go one
  4810. * level deeper.
  4811. *
  4812. * If we are not at level 0 and on shared tree blocks, go one slot to
  4813. * the right if possible or go up and right.
  4814. */
  4815. down_read(&fs_info->commit_root_sem);
  4816. left_level = btrfs_header_level(left_root->commit_root);
  4817. left_root_level = left_level;
  4818. left_path->nodes[left_level] = left_root->commit_root;
  4819. extent_buffer_get(left_path->nodes[left_level]);
  4820. right_level = btrfs_header_level(right_root->commit_root);
  4821. right_root_level = right_level;
  4822. right_path->nodes[right_level] = right_root->commit_root;
  4823. extent_buffer_get(right_path->nodes[right_level]);
  4824. up_read(&fs_info->commit_root_sem);
  4825. if (left_level == 0)
  4826. btrfs_item_key_to_cpu(left_path->nodes[left_level],
  4827. &left_key, left_path->slots[left_level]);
  4828. else
  4829. btrfs_node_key_to_cpu(left_path->nodes[left_level],
  4830. &left_key, left_path->slots[left_level]);
  4831. if (right_level == 0)
  4832. btrfs_item_key_to_cpu(right_path->nodes[right_level],
  4833. &right_key, right_path->slots[right_level]);
  4834. else
  4835. btrfs_node_key_to_cpu(right_path->nodes[right_level],
  4836. &right_key, right_path->slots[right_level]);
  4837. left_end_reached = right_end_reached = 0;
  4838. advance_left = advance_right = 0;
  4839. while (1) {
  4840. if (advance_left && !left_end_reached) {
  4841. ret = tree_advance(fs_info, left_path, &left_level,
  4842. left_root_level,
  4843. advance_left != ADVANCE_ONLY_NEXT,
  4844. &left_key);
  4845. if (ret == -1)
  4846. left_end_reached = ADVANCE;
  4847. else if (ret < 0)
  4848. goto out;
  4849. advance_left = 0;
  4850. }
  4851. if (advance_right && !right_end_reached) {
  4852. ret = tree_advance(fs_info, right_path, &right_level,
  4853. right_root_level,
  4854. advance_right != ADVANCE_ONLY_NEXT,
  4855. &right_key);
  4856. if (ret == -1)
  4857. right_end_reached = ADVANCE;
  4858. else if (ret < 0)
  4859. goto out;
  4860. advance_right = 0;
  4861. }
  4862. if (left_end_reached && right_end_reached) {
  4863. ret = 0;
  4864. goto out;
  4865. } else if (left_end_reached) {
  4866. if (right_level == 0) {
  4867. ret = changed_cb(left_path, right_path,
  4868. &right_key,
  4869. BTRFS_COMPARE_TREE_DELETED,
  4870. ctx);
  4871. if (ret < 0)
  4872. goto out;
  4873. }
  4874. advance_right = ADVANCE;
  4875. continue;
  4876. } else if (right_end_reached) {
  4877. if (left_level == 0) {
  4878. ret = changed_cb(left_path, right_path,
  4879. &left_key,
  4880. BTRFS_COMPARE_TREE_NEW,
  4881. ctx);
  4882. if (ret < 0)
  4883. goto out;
  4884. }
  4885. advance_left = ADVANCE;
  4886. continue;
  4887. }
  4888. if (left_level == 0 && right_level == 0) {
  4889. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4890. if (cmp < 0) {
  4891. ret = changed_cb(left_path, right_path,
  4892. &left_key,
  4893. BTRFS_COMPARE_TREE_NEW,
  4894. ctx);
  4895. if (ret < 0)
  4896. goto out;
  4897. advance_left = ADVANCE;
  4898. } else if (cmp > 0) {
  4899. ret = changed_cb(left_path, right_path,
  4900. &right_key,
  4901. BTRFS_COMPARE_TREE_DELETED,
  4902. ctx);
  4903. if (ret < 0)
  4904. goto out;
  4905. advance_right = ADVANCE;
  4906. } else {
  4907. enum btrfs_compare_tree_result result;
  4908. WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
  4909. ret = tree_compare_item(left_path, right_path,
  4910. tmp_buf);
  4911. if (ret)
  4912. result = BTRFS_COMPARE_TREE_CHANGED;
  4913. else
  4914. result = BTRFS_COMPARE_TREE_SAME;
  4915. ret = changed_cb(left_path, right_path,
  4916. &left_key, result, ctx);
  4917. if (ret < 0)
  4918. goto out;
  4919. advance_left = ADVANCE;
  4920. advance_right = ADVANCE;
  4921. }
  4922. } else if (left_level == right_level) {
  4923. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4924. if (cmp < 0) {
  4925. advance_left = ADVANCE;
  4926. } else if (cmp > 0) {
  4927. advance_right = ADVANCE;
  4928. } else {
  4929. left_blockptr = btrfs_node_blockptr(
  4930. left_path->nodes[left_level],
  4931. left_path->slots[left_level]);
  4932. right_blockptr = btrfs_node_blockptr(
  4933. right_path->nodes[right_level],
  4934. right_path->slots[right_level]);
  4935. left_gen = btrfs_node_ptr_generation(
  4936. left_path->nodes[left_level],
  4937. left_path->slots[left_level]);
  4938. right_gen = btrfs_node_ptr_generation(
  4939. right_path->nodes[right_level],
  4940. right_path->slots[right_level]);
  4941. if (left_blockptr == right_blockptr &&
  4942. left_gen == right_gen) {
  4943. /*
  4944. * As we're on a shared block, don't
  4945. * allow to go deeper.
  4946. */
  4947. advance_left = ADVANCE_ONLY_NEXT;
  4948. advance_right = ADVANCE_ONLY_NEXT;
  4949. } else {
  4950. advance_left = ADVANCE;
  4951. advance_right = ADVANCE;
  4952. }
  4953. }
  4954. } else if (left_level < right_level) {
  4955. advance_right = ADVANCE;
  4956. } else {
  4957. advance_left = ADVANCE;
  4958. }
  4959. }
  4960. out:
  4961. btrfs_free_path(left_path);
  4962. btrfs_free_path(right_path);
  4963. kvfree(tmp_buf);
  4964. return ret;
  4965. }
  4966. /*
  4967. * this is similar to btrfs_next_leaf, but does not try to preserve
  4968. * and fixup the path. It looks for and returns the next key in the
  4969. * tree based on the current path and the min_trans parameters.
  4970. *
  4971. * 0 is returned if another key is found, < 0 if there are any errors
  4972. * and 1 is returned if there are no higher keys in the tree
  4973. *
  4974. * path->keep_locks should be set to 1 on the search made before
  4975. * calling this function.
  4976. */
  4977. int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
  4978. struct btrfs_key *key, int level, u64 min_trans)
  4979. {
  4980. int slot;
  4981. struct extent_buffer *c;
  4982. WARN_ON(!path->keep_locks);
  4983. while (level < BTRFS_MAX_LEVEL) {
  4984. if (!path->nodes[level])
  4985. return 1;
  4986. slot = path->slots[level] + 1;
  4987. c = path->nodes[level];
  4988. next:
  4989. if (slot >= btrfs_header_nritems(c)) {
  4990. int ret;
  4991. int orig_lowest;
  4992. struct btrfs_key cur_key;
  4993. if (level + 1 >= BTRFS_MAX_LEVEL ||
  4994. !path->nodes[level + 1])
  4995. return 1;
  4996. if (path->locks[level + 1]) {
  4997. level++;
  4998. continue;
  4999. }
  5000. slot = btrfs_header_nritems(c) - 1;
  5001. if (level == 0)
  5002. btrfs_item_key_to_cpu(c, &cur_key, slot);
  5003. else
  5004. btrfs_node_key_to_cpu(c, &cur_key, slot);
  5005. orig_lowest = path->lowest_level;
  5006. btrfs_release_path(path);
  5007. path->lowest_level = level;
  5008. ret = btrfs_search_slot(NULL, root, &cur_key, path,
  5009. 0, 0);
  5010. path->lowest_level = orig_lowest;
  5011. if (ret < 0)
  5012. return ret;
  5013. c = path->nodes[level];
  5014. slot = path->slots[level];
  5015. if (ret == 0)
  5016. slot++;
  5017. goto next;
  5018. }
  5019. if (level == 0)
  5020. btrfs_item_key_to_cpu(c, key, slot);
  5021. else {
  5022. u64 gen = btrfs_node_ptr_generation(c, slot);
  5023. if (gen < min_trans) {
  5024. slot++;
  5025. goto next;
  5026. }
  5027. btrfs_node_key_to_cpu(c, key, slot);
  5028. }
  5029. return 0;
  5030. }
  5031. return 1;
  5032. }
  5033. /*
  5034. * search the tree again to find a leaf with greater keys
  5035. * returns 0 if it found something or 1 if there are no greater leaves.
  5036. * returns < 0 on io errors.
  5037. */
  5038. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  5039. {
  5040. return btrfs_next_old_leaf(root, path, 0);
  5041. }
  5042. int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
  5043. u64 time_seq)
  5044. {
  5045. int slot;
  5046. int level;
  5047. struct extent_buffer *c;
  5048. struct extent_buffer *next;
  5049. struct btrfs_key key;
  5050. u32 nritems;
  5051. int ret;
  5052. int old_spinning = path->leave_spinning;
  5053. int next_rw_lock = 0;
  5054. nritems = btrfs_header_nritems(path->nodes[0]);
  5055. if (nritems == 0)
  5056. return 1;
  5057. btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
  5058. again:
  5059. level = 1;
  5060. next = NULL;
  5061. next_rw_lock = 0;
  5062. btrfs_release_path(path);
  5063. path->keep_locks = 1;
  5064. path->leave_spinning = 1;
  5065. if (time_seq)
  5066. ret = btrfs_search_old_slot(root, &key, path, time_seq);
  5067. else
  5068. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5069. path->keep_locks = 0;
  5070. if (ret < 0)
  5071. return ret;
  5072. nritems = btrfs_header_nritems(path->nodes[0]);
  5073. /*
  5074. * by releasing the path above we dropped all our locks. A balance
  5075. * could have added more items next to the key that used to be
  5076. * at the very end of the block. So, check again here and
  5077. * advance the path if there are now more items available.
  5078. */
  5079. if (nritems > 0 && path->slots[0] < nritems - 1) {
  5080. if (ret == 0)
  5081. path->slots[0]++;
  5082. ret = 0;
  5083. goto done;
  5084. }
  5085. /*
  5086. * So the above check misses one case:
  5087. * - after releasing the path above, someone has removed the item that
  5088. * used to be at the very end of the block, and balance between leafs
  5089. * gets another one with bigger key.offset to replace it.
  5090. *
  5091. * This one should be returned as well, or we can get leaf corruption
  5092. * later(esp. in __btrfs_drop_extents()).
  5093. *
  5094. * And a bit more explanation about this check,
  5095. * with ret > 0, the key isn't found, the path points to the slot
  5096. * where it should be inserted, so the path->slots[0] item must be the
  5097. * bigger one.
  5098. */
  5099. if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
  5100. ret = 0;
  5101. goto done;
  5102. }
  5103. while (level < BTRFS_MAX_LEVEL) {
  5104. if (!path->nodes[level]) {
  5105. ret = 1;
  5106. goto done;
  5107. }
  5108. slot = path->slots[level] + 1;
  5109. c = path->nodes[level];
  5110. if (slot >= btrfs_header_nritems(c)) {
  5111. level++;
  5112. if (level == BTRFS_MAX_LEVEL) {
  5113. ret = 1;
  5114. goto done;
  5115. }
  5116. continue;
  5117. }
  5118. if (next) {
  5119. btrfs_tree_unlock_rw(next, next_rw_lock);
  5120. free_extent_buffer(next);
  5121. }
  5122. next = c;
  5123. next_rw_lock = path->locks[level];
  5124. ret = read_block_for_search(root, path, &next, level,
  5125. slot, &key);
  5126. if (ret == -EAGAIN)
  5127. goto again;
  5128. if (ret < 0) {
  5129. btrfs_release_path(path);
  5130. goto done;
  5131. }
  5132. if (!path->skip_locking) {
  5133. ret = btrfs_try_tree_read_lock(next);
  5134. if (!ret && time_seq) {
  5135. /*
  5136. * If we don't get the lock, we may be racing
  5137. * with push_leaf_left, holding that lock while
  5138. * itself waiting for the leaf we've currently
  5139. * locked. To solve this situation, we give up
  5140. * on our lock and cycle.
  5141. */
  5142. free_extent_buffer(next);
  5143. btrfs_release_path(path);
  5144. cond_resched();
  5145. goto again;
  5146. }
  5147. if (!ret) {
  5148. btrfs_set_path_blocking(path);
  5149. btrfs_tree_read_lock(next);
  5150. btrfs_clear_path_blocking(path, next,
  5151. BTRFS_READ_LOCK);
  5152. }
  5153. next_rw_lock = BTRFS_READ_LOCK;
  5154. }
  5155. break;
  5156. }
  5157. path->slots[level] = slot;
  5158. while (1) {
  5159. level--;
  5160. c = path->nodes[level];
  5161. if (path->locks[level])
  5162. btrfs_tree_unlock_rw(c, path->locks[level]);
  5163. free_extent_buffer(c);
  5164. path->nodes[level] = next;
  5165. path->slots[level] = 0;
  5166. if (!path->skip_locking)
  5167. path->locks[level] = next_rw_lock;
  5168. if (!level)
  5169. break;
  5170. ret = read_block_for_search(root, path, &next, level,
  5171. 0, &key);
  5172. if (ret == -EAGAIN)
  5173. goto again;
  5174. if (ret < 0) {
  5175. btrfs_release_path(path);
  5176. goto done;
  5177. }
  5178. if (!path->skip_locking) {
  5179. ret = btrfs_try_tree_read_lock(next);
  5180. if (!ret) {
  5181. btrfs_set_path_blocking(path);
  5182. btrfs_tree_read_lock(next);
  5183. btrfs_clear_path_blocking(path, next,
  5184. BTRFS_READ_LOCK);
  5185. }
  5186. next_rw_lock = BTRFS_READ_LOCK;
  5187. }
  5188. }
  5189. ret = 0;
  5190. done:
  5191. unlock_up(path, 0, 1, 0, NULL);
  5192. path->leave_spinning = old_spinning;
  5193. if (!old_spinning)
  5194. btrfs_set_path_blocking(path);
  5195. return ret;
  5196. }
  5197. /*
  5198. * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
  5199. * searching until it gets past min_objectid or finds an item of 'type'
  5200. *
  5201. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5202. */
  5203. int btrfs_previous_item(struct btrfs_root *root,
  5204. struct btrfs_path *path, u64 min_objectid,
  5205. int type)
  5206. {
  5207. struct btrfs_key found_key;
  5208. struct extent_buffer *leaf;
  5209. u32 nritems;
  5210. int ret;
  5211. while (1) {
  5212. if (path->slots[0] == 0) {
  5213. btrfs_set_path_blocking(path);
  5214. ret = btrfs_prev_leaf(root, path);
  5215. if (ret != 0)
  5216. return ret;
  5217. } else {
  5218. path->slots[0]--;
  5219. }
  5220. leaf = path->nodes[0];
  5221. nritems = btrfs_header_nritems(leaf);
  5222. if (nritems == 0)
  5223. return 1;
  5224. if (path->slots[0] == nritems)
  5225. path->slots[0]--;
  5226. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5227. if (found_key.objectid < min_objectid)
  5228. break;
  5229. if (found_key.type == type)
  5230. return 0;
  5231. if (found_key.objectid == min_objectid &&
  5232. found_key.type < type)
  5233. break;
  5234. }
  5235. return 1;
  5236. }
  5237. /*
  5238. * search in extent tree to find a previous Metadata/Data extent item with
  5239. * min objecitd.
  5240. *
  5241. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5242. */
  5243. int btrfs_previous_extent_item(struct btrfs_root *root,
  5244. struct btrfs_path *path, u64 min_objectid)
  5245. {
  5246. struct btrfs_key found_key;
  5247. struct extent_buffer *leaf;
  5248. u32 nritems;
  5249. int ret;
  5250. while (1) {
  5251. if (path->slots[0] == 0) {
  5252. btrfs_set_path_blocking(path);
  5253. ret = btrfs_prev_leaf(root, path);
  5254. if (ret != 0)
  5255. return ret;
  5256. } else {
  5257. path->slots[0]--;
  5258. }
  5259. leaf = path->nodes[0];
  5260. nritems = btrfs_header_nritems(leaf);
  5261. if (nritems == 0)
  5262. return 1;
  5263. if (path->slots[0] == nritems)
  5264. path->slots[0]--;
  5265. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5266. if (found_key.objectid < min_objectid)
  5267. break;
  5268. if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
  5269. found_key.type == BTRFS_METADATA_ITEM_KEY)
  5270. return 0;
  5271. if (found_key.objectid == min_objectid &&
  5272. found_key.type < BTRFS_EXTENT_ITEM_KEY)
  5273. break;
  5274. }
  5275. return 1;
  5276. }