ctree.c 153 KB

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