ctree.c 153 KB

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