ctree.c 154 KB

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