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
  475. tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
  476. struct extent_buffer *eb, int slot,
  477. enum mod_log_op op, gfp_t flags)
  478. {
  479. struct tree_mod_elem *tm;
  480. int ret;
  481. if (!tree_mod_need_log(fs_info, eb))
  482. return 0;
  483. tm = alloc_tree_mod_elem(eb, slot, op, flags);
  484. if (!tm)
  485. return -ENOMEM;
  486. if (tree_mod_dont_log(fs_info, eb)) {
  487. kfree(tm);
  488. return 0;
  489. }
  490. ret = __tree_mod_log_insert(fs_info, tm);
  491. tree_mod_log_write_unlock(fs_info);
  492. if (ret)
  493. kfree(tm);
  494. return ret;
  495. }
  496. static noinline int
  497. tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
  498. struct extent_buffer *eb, int dst_slot, int src_slot,
  499. int nr_items)
  500. {
  501. struct tree_mod_elem *tm = NULL;
  502. struct tree_mod_elem **tm_list = NULL;
  503. int ret = 0;
  504. int i;
  505. int locked = 0;
  506. if (!tree_mod_need_log(fs_info, eb))
  507. return 0;
  508. tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
  509. if (!tm_list)
  510. return -ENOMEM;
  511. tm = kzalloc(sizeof(*tm), GFP_NOFS);
  512. if (!tm) {
  513. ret = -ENOMEM;
  514. goto free_tms;
  515. }
  516. tm->logical = eb->start;
  517. tm->slot = src_slot;
  518. tm->move.dst_slot = dst_slot;
  519. tm->move.nr_items = nr_items;
  520. tm->op = MOD_LOG_MOVE_KEYS;
  521. for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
  522. tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
  523. MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
  524. if (!tm_list[i]) {
  525. ret = -ENOMEM;
  526. goto free_tms;
  527. }
  528. }
  529. if (tree_mod_dont_log(fs_info, eb))
  530. goto free_tms;
  531. locked = 1;
  532. /*
  533. * When we override something during the move, we log these removals.
  534. * This can only happen when we move towards the beginning of the
  535. * buffer, i.e. dst_slot < src_slot.
  536. */
  537. for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
  538. ret = __tree_mod_log_insert(fs_info, tm_list[i]);
  539. if (ret)
  540. goto free_tms;
  541. }
  542. ret = __tree_mod_log_insert(fs_info, tm);
  543. if (ret)
  544. goto free_tms;
  545. tree_mod_log_write_unlock(fs_info);
  546. kfree(tm_list);
  547. return 0;
  548. free_tms:
  549. for (i = 0; i < nr_items; i++) {
  550. if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
  551. rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
  552. kfree(tm_list[i]);
  553. }
  554. if (locked)
  555. tree_mod_log_write_unlock(fs_info);
  556. kfree(tm_list);
  557. kfree(tm);
  558. return ret;
  559. }
  560. static inline int
  561. __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
  562. struct tree_mod_elem **tm_list,
  563. int nritems)
  564. {
  565. int i, j;
  566. int ret;
  567. for (i = nritems - 1; i >= 0; i--) {
  568. ret = __tree_mod_log_insert(fs_info, tm_list[i]);
  569. if (ret) {
  570. for (j = nritems - 1; j > i; j--)
  571. rb_erase(&tm_list[j]->node,
  572. &fs_info->tree_mod_log);
  573. return ret;
  574. }
  575. }
  576. return 0;
  577. }
  578. static noinline int
  579. tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
  580. struct extent_buffer *old_root,
  581. struct extent_buffer *new_root,
  582. int log_removal)
  583. {
  584. struct tree_mod_elem *tm = NULL;
  585. struct tree_mod_elem **tm_list = NULL;
  586. int nritems = 0;
  587. int ret = 0;
  588. int i;
  589. if (!tree_mod_need_log(fs_info, NULL))
  590. return 0;
  591. if (log_removal && btrfs_header_level(old_root) > 0) {
  592. nritems = btrfs_header_nritems(old_root);
  593. tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
  594. GFP_NOFS);
  595. if (!tm_list) {
  596. ret = -ENOMEM;
  597. goto free_tms;
  598. }
  599. for (i = 0; i < nritems; i++) {
  600. tm_list[i] = alloc_tree_mod_elem(old_root, i,
  601. MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
  602. if (!tm_list[i]) {
  603. ret = -ENOMEM;
  604. goto free_tms;
  605. }
  606. }
  607. }
  608. tm = kzalloc(sizeof(*tm), GFP_NOFS);
  609. if (!tm) {
  610. ret = -ENOMEM;
  611. goto free_tms;
  612. }
  613. tm->logical = new_root->start;
  614. tm->old_root.logical = old_root->start;
  615. tm->old_root.level = btrfs_header_level(old_root);
  616. tm->generation = btrfs_header_generation(old_root);
  617. tm->op = MOD_LOG_ROOT_REPLACE;
  618. if (tree_mod_dont_log(fs_info, NULL))
  619. goto free_tms;
  620. if (tm_list)
  621. ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
  622. if (!ret)
  623. ret = __tree_mod_log_insert(fs_info, tm);
  624. tree_mod_log_write_unlock(fs_info);
  625. if (ret)
  626. goto free_tms;
  627. kfree(tm_list);
  628. return ret;
  629. free_tms:
  630. if (tm_list) {
  631. for (i = 0; i < nritems; i++)
  632. kfree(tm_list[i]);
  633. kfree(tm_list);
  634. }
  635. kfree(tm);
  636. return ret;
  637. }
  638. static struct tree_mod_elem *
  639. __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
  640. int smallest)
  641. {
  642. struct rb_root *tm_root;
  643. struct rb_node *node;
  644. struct tree_mod_elem *cur = NULL;
  645. struct tree_mod_elem *found = NULL;
  646. tree_mod_log_read_lock(fs_info);
  647. tm_root = &fs_info->tree_mod_log;
  648. node = tm_root->rb_node;
  649. while (node) {
  650. cur = rb_entry(node, struct tree_mod_elem, node);
  651. if (cur->logical < start) {
  652. node = node->rb_left;
  653. } else if (cur->logical > start) {
  654. node = node->rb_right;
  655. } else if (cur->seq < min_seq) {
  656. node = node->rb_left;
  657. } else if (!smallest) {
  658. /* we want the node with the highest seq */
  659. if (found)
  660. BUG_ON(found->seq > cur->seq);
  661. found = cur;
  662. node = node->rb_left;
  663. } else if (cur->seq > min_seq) {
  664. /* we want the node with the smallest seq */
  665. if (found)
  666. BUG_ON(found->seq < cur->seq);
  667. found = cur;
  668. node = node->rb_right;
  669. } else {
  670. found = cur;
  671. break;
  672. }
  673. }
  674. tree_mod_log_read_unlock(fs_info);
  675. return found;
  676. }
  677. /*
  678. * this returns the element from the log with the smallest time sequence
  679. * value that's in the log (the oldest log item). any element with a time
  680. * sequence lower than min_seq will be ignored.
  681. */
  682. static struct tree_mod_elem *
  683. tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
  684. u64 min_seq)
  685. {
  686. return __tree_mod_log_search(fs_info, start, min_seq, 1);
  687. }
  688. /*
  689. * this returns the element from the log with the largest time sequence
  690. * value that's in the log (the most recent log item). any element with
  691. * a time sequence lower than min_seq will be ignored.
  692. */
  693. static struct tree_mod_elem *
  694. tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
  695. {
  696. return __tree_mod_log_search(fs_info, start, min_seq, 0);
  697. }
  698. static noinline int
  699. tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
  700. struct extent_buffer *src, unsigned long dst_offset,
  701. unsigned long src_offset, int nr_items)
  702. {
  703. int ret = 0;
  704. struct tree_mod_elem **tm_list = NULL;
  705. struct tree_mod_elem **tm_list_add, **tm_list_rem;
  706. int i;
  707. int locked = 0;
  708. if (!tree_mod_need_log(fs_info, NULL))
  709. return 0;
  710. if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
  711. return 0;
  712. tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
  713. GFP_NOFS);
  714. if (!tm_list)
  715. return -ENOMEM;
  716. tm_list_add = tm_list;
  717. tm_list_rem = tm_list + nr_items;
  718. for (i = 0; i < nr_items; i++) {
  719. tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
  720. MOD_LOG_KEY_REMOVE, GFP_NOFS);
  721. if (!tm_list_rem[i]) {
  722. ret = -ENOMEM;
  723. goto free_tms;
  724. }
  725. tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
  726. MOD_LOG_KEY_ADD, GFP_NOFS);
  727. if (!tm_list_add[i]) {
  728. ret = -ENOMEM;
  729. goto free_tms;
  730. }
  731. }
  732. if (tree_mod_dont_log(fs_info, NULL))
  733. goto free_tms;
  734. locked = 1;
  735. for (i = 0; i < nr_items; i++) {
  736. ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
  737. if (ret)
  738. goto free_tms;
  739. ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
  740. if (ret)
  741. goto free_tms;
  742. }
  743. tree_mod_log_write_unlock(fs_info);
  744. kfree(tm_list);
  745. return 0;
  746. free_tms:
  747. for (i = 0; i < nr_items * 2; i++) {
  748. if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
  749. rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
  750. kfree(tm_list[i]);
  751. }
  752. if (locked)
  753. tree_mod_log_write_unlock(fs_info);
  754. kfree(tm_list);
  755. return ret;
  756. }
  757. static inline void
  758. tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
  759. int dst_offset, int src_offset, int nr_items)
  760. {
  761. int ret;
  762. ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
  763. nr_items);
  764. BUG_ON(ret < 0);
  765. }
  766. static noinline void
  767. tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
  768. struct extent_buffer *eb, int slot, int atomic)
  769. {
  770. int ret;
  771. ret = tree_mod_log_insert_key(fs_info, eb, slot,
  772. MOD_LOG_KEY_REPLACE,
  773. atomic ? GFP_ATOMIC : GFP_NOFS);
  774. BUG_ON(ret < 0);
  775. }
  776. static noinline int
  777. tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
  778. {
  779. struct tree_mod_elem **tm_list = NULL;
  780. int nritems = 0;
  781. int i;
  782. int ret = 0;
  783. if (btrfs_header_level(eb) == 0)
  784. return 0;
  785. if (!tree_mod_need_log(fs_info, NULL))
  786. return 0;
  787. nritems = btrfs_header_nritems(eb);
  788. tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
  789. if (!tm_list)
  790. return -ENOMEM;
  791. for (i = 0; i < nritems; i++) {
  792. tm_list[i] = alloc_tree_mod_elem(eb, i,
  793. MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
  794. if (!tm_list[i]) {
  795. ret = -ENOMEM;
  796. goto free_tms;
  797. }
  798. }
  799. if (tree_mod_dont_log(fs_info, eb))
  800. goto free_tms;
  801. ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
  802. tree_mod_log_write_unlock(fs_info);
  803. if (ret)
  804. goto free_tms;
  805. kfree(tm_list);
  806. return 0;
  807. free_tms:
  808. for (i = 0; i < nritems; i++)
  809. kfree(tm_list[i]);
  810. kfree(tm_list);
  811. return ret;
  812. }
  813. static noinline void
  814. tree_mod_log_set_root_pointer(struct btrfs_root *root,
  815. struct extent_buffer *new_root_node,
  816. int log_removal)
  817. {
  818. int ret;
  819. ret = tree_mod_log_insert_root(root->fs_info, root->node,
  820. new_root_node, log_removal);
  821. BUG_ON(ret < 0);
  822. }
  823. /*
  824. * check if the tree block can be shared by multiple trees
  825. */
  826. int btrfs_block_can_be_shared(struct btrfs_root *root,
  827. struct extent_buffer *buf)
  828. {
  829. /*
  830. * Tree blocks not in reference counted trees and tree roots
  831. * are never shared. If a block was allocated after the last
  832. * snapshot and the block was not allocated by tree relocation,
  833. * we know the block is not shared.
  834. */
  835. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  836. buf != root->node && buf != root->commit_root &&
  837. (btrfs_header_generation(buf) <=
  838. btrfs_root_last_snapshot(&root->root_item) ||
  839. btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
  840. return 1;
  841. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  842. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  843. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  844. return 1;
  845. #endif
  846. return 0;
  847. }
  848. static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
  849. struct btrfs_root *root,
  850. struct extent_buffer *buf,
  851. struct extent_buffer *cow,
  852. int *last_ref)
  853. {
  854. struct btrfs_fs_info *fs_info = root->fs_info;
  855. u64 refs;
  856. u64 owner;
  857. u64 flags;
  858. u64 new_flags = 0;
  859. int ret;
  860. /*
  861. * Backrefs update rules:
  862. *
  863. * Always use full backrefs for extent pointers in tree block
  864. * allocated by tree relocation.
  865. *
  866. * If a shared tree block is no longer referenced by its owner
  867. * tree (btrfs_header_owner(buf) == root->root_key.objectid),
  868. * use full backrefs for extent pointers in tree block.
  869. *
  870. * If a tree block is been relocating
  871. * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
  872. * use full backrefs for extent pointers in tree block.
  873. * The reason for this is some operations (such as drop tree)
  874. * are only allowed for blocks use full backrefs.
  875. */
  876. if (btrfs_block_can_be_shared(root, buf)) {
  877. ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
  878. btrfs_header_level(buf), 1,
  879. &refs, &flags);
  880. if (ret)
  881. return ret;
  882. if (refs == 0) {
  883. ret = -EROFS;
  884. btrfs_handle_fs_error(fs_info, ret, NULL);
  885. return ret;
  886. }
  887. } else {
  888. refs = 1;
  889. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  890. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  891. flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  892. else
  893. flags = 0;
  894. }
  895. owner = btrfs_header_owner(buf);
  896. BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
  897. !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  898. if (refs > 1) {
  899. if ((owner == root->root_key.objectid ||
  900. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
  901. !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
  902. ret = btrfs_inc_ref(trans, root, buf, 1);
  903. if (ret)
  904. return ret;
  905. if (root->root_key.objectid ==
  906. BTRFS_TREE_RELOC_OBJECTID) {
  907. ret = btrfs_dec_ref(trans, root, buf, 0);
  908. if (ret)
  909. return ret;
  910. ret = btrfs_inc_ref(trans, root, cow, 1);
  911. if (ret)
  912. return ret;
  913. }
  914. new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  915. } else {
  916. if (root->root_key.objectid ==
  917. BTRFS_TREE_RELOC_OBJECTID)
  918. ret = btrfs_inc_ref(trans, root, cow, 1);
  919. else
  920. ret = btrfs_inc_ref(trans, root, cow, 0);
  921. if (ret)
  922. return ret;
  923. }
  924. if (new_flags != 0) {
  925. int level = btrfs_header_level(buf);
  926. ret = btrfs_set_disk_extent_flags(trans, fs_info,
  927. buf->start,
  928. buf->len,
  929. new_flags, level, 0);
  930. if (ret)
  931. return ret;
  932. }
  933. } else {
  934. if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  935. if (root->root_key.objectid ==
  936. BTRFS_TREE_RELOC_OBJECTID)
  937. ret = btrfs_inc_ref(trans, root, cow, 1);
  938. else
  939. ret = btrfs_inc_ref(trans, root, cow, 0);
  940. if (ret)
  941. return ret;
  942. ret = btrfs_dec_ref(trans, root, buf, 1);
  943. if (ret)
  944. return ret;
  945. }
  946. clean_tree_block(fs_info, buf);
  947. *last_ref = 1;
  948. }
  949. return 0;
  950. }
  951. /*
  952. * does the dirty work in cow of a single block. The parent block (if
  953. * supplied) is updated to point to the new cow copy. The new buffer is marked
  954. * dirty and returned locked. If you modify the block it needs to be marked
  955. * dirty again.
  956. *
  957. * search_start -- an allocation hint for the new block
  958. *
  959. * empty_size -- a hint that you plan on doing more cow. This is the size in
  960. * bytes the allocator should try to find free next to the block it returns.
  961. * This is just a hint and may be ignored by the allocator.
  962. */
  963. static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
  964. struct btrfs_root *root,
  965. struct extent_buffer *buf,
  966. struct extent_buffer *parent, int parent_slot,
  967. struct extent_buffer **cow_ret,
  968. u64 search_start, u64 empty_size)
  969. {
  970. struct btrfs_fs_info *fs_info = root->fs_info;
  971. struct btrfs_disk_key disk_key;
  972. struct extent_buffer *cow;
  973. int level, ret;
  974. int last_ref = 0;
  975. int unlock_orig = 0;
  976. u64 parent_start = 0;
  977. if (*cow_ret == buf)
  978. unlock_orig = 1;
  979. btrfs_assert_tree_locked(buf);
  980. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  981. trans->transid != fs_info->running_transaction->transid);
  982. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  983. trans->transid != root->last_trans);
  984. level = btrfs_header_level(buf);
  985. if (level == 0)
  986. btrfs_item_key(buf, &disk_key, 0);
  987. else
  988. btrfs_node_key(buf, &disk_key, 0);
  989. if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
  990. parent_start = parent->start;
  991. cow = btrfs_alloc_tree_block(trans, root, parent_start,
  992. root->root_key.objectid, &disk_key, level,
  993. search_start, empty_size);
  994. if (IS_ERR(cow))
  995. return PTR_ERR(cow);
  996. /* cow is set to blocking by btrfs_init_new_buffer */
  997. copy_extent_buffer_full(cow, buf);
  998. btrfs_set_header_bytenr(cow, cow->start);
  999. btrfs_set_header_generation(cow, trans->transid);
  1000. btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
  1001. btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
  1002. BTRFS_HEADER_FLAG_RELOC);
  1003. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  1004. btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
  1005. else
  1006. btrfs_set_header_owner(cow, root->root_key.objectid);
  1007. write_extent_buffer_fsid(cow, fs_info->fsid);
  1008. ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
  1009. if (ret) {
  1010. btrfs_abort_transaction(trans, ret);
  1011. return ret;
  1012. }
  1013. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
  1014. ret = btrfs_reloc_cow_block(trans, root, buf, cow);
  1015. if (ret) {
  1016. btrfs_abort_transaction(trans, ret);
  1017. return ret;
  1018. }
  1019. }
  1020. if (buf == root->node) {
  1021. WARN_ON(parent && parent != buf);
  1022. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  1023. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  1024. parent_start = buf->start;
  1025. extent_buffer_get(cow);
  1026. tree_mod_log_set_root_pointer(root, cow, 1);
  1027. rcu_assign_pointer(root->node, cow);
  1028. btrfs_free_tree_block(trans, root, buf, parent_start,
  1029. last_ref);
  1030. free_extent_buffer(buf);
  1031. add_root_to_dirty_list(root);
  1032. } else {
  1033. WARN_ON(trans->transid != btrfs_header_generation(parent));
  1034. tree_mod_log_insert_key(fs_info, parent, parent_slot,
  1035. MOD_LOG_KEY_REPLACE, GFP_NOFS);
  1036. btrfs_set_node_blockptr(parent, parent_slot,
  1037. cow->start);
  1038. btrfs_set_node_ptr_generation(parent, parent_slot,
  1039. trans->transid);
  1040. btrfs_mark_buffer_dirty(parent);
  1041. if (last_ref) {
  1042. ret = tree_mod_log_free_eb(fs_info, buf);
  1043. if (ret) {
  1044. btrfs_abort_transaction(trans, ret);
  1045. return ret;
  1046. }
  1047. }
  1048. btrfs_free_tree_block(trans, root, buf, parent_start,
  1049. last_ref);
  1050. }
  1051. if (unlock_orig)
  1052. btrfs_tree_unlock(buf);
  1053. free_extent_buffer_stale(buf);
  1054. btrfs_mark_buffer_dirty(cow);
  1055. *cow_ret = cow;
  1056. return 0;
  1057. }
  1058. /*
  1059. * returns the logical address of the oldest predecessor of the given root.
  1060. * entries older than time_seq are ignored.
  1061. */
  1062. static struct tree_mod_elem *
  1063. __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
  1064. struct extent_buffer *eb_root, u64 time_seq)
  1065. {
  1066. struct tree_mod_elem *tm;
  1067. struct tree_mod_elem *found = NULL;
  1068. u64 root_logical = eb_root->start;
  1069. int looped = 0;
  1070. if (!time_seq)
  1071. return NULL;
  1072. /*
  1073. * the very last operation that's logged for a root is the
  1074. * replacement operation (if it is replaced at all). this has
  1075. * the logical address of the *new* root, making it the very
  1076. * first operation that's logged for this root.
  1077. */
  1078. while (1) {
  1079. tm = tree_mod_log_search_oldest(fs_info, root_logical,
  1080. time_seq);
  1081. if (!looped && !tm)
  1082. return NULL;
  1083. /*
  1084. * if there are no tree operation for the oldest root, we simply
  1085. * return it. this should only happen if that (old) root is at
  1086. * level 0.
  1087. */
  1088. if (!tm)
  1089. break;
  1090. /*
  1091. * if there's an operation that's not a root replacement, we
  1092. * found the oldest version of our root. normally, we'll find a
  1093. * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
  1094. */
  1095. if (tm->op != MOD_LOG_ROOT_REPLACE)
  1096. break;
  1097. found = tm;
  1098. root_logical = tm->old_root.logical;
  1099. looped = 1;
  1100. }
  1101. /* if there's no old root to return, return what we found instead */
  1102. if (!found)
  1103. found = tm;
  1104. return found;
  1105. }
  1106. /*
  1107. * tm is a pointer to the first operation to rewind within eb. then, all
  1108. * previous operations will be rewound (until we reach something older than
  1109. * time_seq).
  1110. */
  1111. static void
  1112. __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
  1113. u64 time_seq, struct tree_mod_elem *first_tm)
  1114. {
  1115. u32 n;
  1116. struct rb_node *next;
  1117. struct tree_mod_elem *tm = first_tm;
  1118. unsigned long o_dst;
  1119. unsigned long o_src;
  1120. unsigned long p_size = sizeof(struct btrfs_key_ptr);
  1121. n = btrfs_header_nritems(eb);
  1122. tree_mod_log_read_lock(fs_info);
  1123. while (tm && tm->seq >= time_seq) {
  1124. /*
  1125. * all the operations are recorded with the operator used for
  1126. * the modification. as we're going backwards, we do the
  1127. * opposite of each operation here.
  1128. */
  1129. switch (tm->op) {
  1130. case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
  1131. BUG_ON(tm->slot < n);
  1132. /* Fallthrough */
  1133. case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
  1134. case MOD_LOG_KEY_REMOVE:
  1135. btrfs_set_node_key(eb, &tm->key, tm->slot);
  1136. btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
  1137. btrfs_set_node_ptr_generation(eb, tm->slot,
  1138. tm->generation);
  1139. n++;
  1140. break;
  1141. case MOD_LOG_KEY_REPLACE:
  1142. BUG_ON(tm->slot >= n);
  1143. btrfs_set_node_key(eb, &tm->key, tm->slot);
  1144. btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
  1145. btrfs_set_node_ptr_generation(eb, tm->slot,
  1146. tm->generation);
  1147. break;
  1148. case MOD_LOG_KEY_ADD:
  1149. /* if a move operation is needed it's in the log */
  1150. n--;
  1151. break;
  1152. case MOD_LOG_MOVE_KEYS:
  1153. o_dst = btrfs_node_key_ptr_offset(tm->slot);
  1154. o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
  1155. memmove_extent_buffer(eb, o_dst, o_src,
  1156. tm->move.nr_items * p_size);
  1157. break;
  1158. case MOD_LOG_ROOT_REPLACE:
  1159. /*
  1160. * this operation is special. for roots, this must be
  1161. * handled explicitly before rewinding.
  1162. * for non-roots, this operation may exist if the node
  1163. * was a root: root A -> child B; then A gets empty and
  1164. * B is promoted to the new root. in the mod log, we'll
  1165. * have a root-replace operation for B, a tree block
  1166. * that is no root. we simply ignore that operation.
  1167. */
  1168. break;
  1169. }
  1170. next = rb_next(&tm->node);
  1171. if (!next)
  1172. break;
  1173. tm = rb_entry(next, struct tree_mod_elem, node);
  1174. if (tm->logical != first_tm->logical)
  1175. break;
  1176. }
  1177. tree_mod_log_read_unlock(fs_info);
  1178. btrfs_set_header_nritems(eb, n);
  1179. }
  1180. /*
  1181. * Called with eb read locked. If the buffer cannot be rewound, the same buffer
  1182. * is returned. If rewind operations happen, a fresh buffer is returned. The
  1183. * returned buffer is always read-locked. If the returned buffer is not the
  1184. * input buffer, the lock on the input buffer is released and the input buffer
  1185. * is freed (its refcount is decremented).
  1186. */
  1187. static struct extent_buffer *
  1188. tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  1189. struct extent_buffer *eb, u64 time_seq)
  1190. {
  1191. struct extent_buffer *eb_rewin;
  1192. struct tree_mod_elem *tm;
  1193. if (!time_seq)
  1194. return eb;
  1195. if (btrfs_header_level(eb) == 0)
  1196. return eb;
  1197. tm = tree_mod_log_search(fs_info, eb->start, time_seq);
  1198. if (!tm)
  1199. return eb;
  1200. btrfs_set_path_blocking(path);
  1201. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1202. if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
  1203. BUG_ON(tm->slot != 0);
  1204. eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
  1205. if (!eb_rewin) {
  1206. btrfs_tree_read_unlock_blocking(eb);
  1207. free_extent_buffer(eb);
  1208. return NULL;
  1209. }
  1210. btrfs_set_header_bytenr(eb_rewin, eb->start);
  1211. btrfs_set_header_backref_rev(eb_rewin,
  1212. btrfs_header_backref_rev(eb));
  1213. btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
  1214. btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
  1215. } else {
  1216. eb_rewin = btrfs_clone_extent_buffer(eb);
  1217. if (!eb_rewin) {
  1218. btrfs_tree_read_unlock_blocking(eb);
  1219. free_extent_buffer(eb);
  1220. return NULL;
  1221. }
  1222. }
  1223. btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
  1224. btrfs_tree_read_unlock_blocking(eb);
  1225. free_extent_buffer(eb);
  1226. extent_buffer_get(eb_rewin);
  1227. btrfs_tree_read_lock(eb_rewin);
  1228. __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
  1229. WARN_ON(btrfs_header_nritems(eb_rewin) >
  1230. BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  1231. return eb_rewin;
  1232. }
  1233. /*
  1234. * get_old_root() rewinds the state of @root's root node to the given @time_seq
  1235. * value. If there are no changes, the current root->root_node is returned. If
  1236. * anything changed in between, there's a fresh buffer allocated on which the
  1237. * rewind operations are done. In any case, the returned buffer is read locked.
  1238. * Returns NULL on error (with no locks held).
  1239. */
  1240. static inline struct extent_buffer *
  1241. get_old_root(struct btrfs_root *root, u64 time_seq)
  1242. {
  1243. struct btrfs_fs_info *fs_info = root->fs_info;
  1244. struct tree_mod_elem *tm;
  1245. struct extent_buffer *eb = NULL;
  1246. struct extent_buffer *eb_root;
  1247. struct extent_buffer *old;
  1248. struct tree_mod_root *old_root = NULL;
  1249. u64 old_generation = 0;
  1250. u64 logical;
  1251. eb_root = btrfs_read_lock_root_node(root);
  1252. tm = __tree_mod_log_oldest_root(fs_info, eb_root, time_seq);
  1253. if (!tm)
  1254. return eb_root;
  1255. if (tm->op == MOD_LOG_ROOT_REPLACE) {
  1256. old_root = &tm->old_root;
  1257. old_generation = tm->generation;
  1258. logical = old_root->logical;
  1259. } else {
  1260. logical = eb_root->start;
  1261. }
  1262. tm = tree_mod_log_search(fs_info, logical, time_seq);
  1263. if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
  1264. btrfs_tree_read_unlock(eb_root);
  1265. free_extent_buffer(eb_root);
  1266. old = read_tree_block(fs_info, logical, 0);
  1267. if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
  1268. if (!IS_ERR(old))
  1269. free_extent_buffer(old);
  1270. btrfs_warn(fs_info,
  1271. "failed to read tree block %llu from get_old_root",
  1272. logical);
  1273. } else {
  1274. eb = btrfs_clone_extent_buffer(old);
  1275. free_extent_buffer(old);
  1276. }
  1277. } else if (old_root) {
  1278. btrfs_tree_read_unlock(eb_root);
  1279. free_extent_buffer(eb_root);
  1280. eb = alloc_dummy_extent_buffer(fs_info, logical);
  1281. } else {
  1282. btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
  1283. eb = btrfs_clone_extent_buffer(eb_root);
  1284. btrfs_tree_read_unlock_blocking(eb_root);
  1285. free_extent_buffer(eb_root);
  1286. }
  1287. if (!eb)
  1288. return NULL;
  1289. extent_buffer_get(eb);
  1290. btrfs_tree_read_lock(eb);
  1291. if (old_root) {
  1292. btrfs_set_header_bytenr(eb, eb->start);
  1293. btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
  1294. btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
  1295. btrfs_set_header_level(eb, old_root->level);
  1296. btrfs_set_header_generation(eb, old_generation);
  1297. }
  1298. if (tm)
  1299. __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
  1300. else
  1301. WARN_ON(btrfs_header_level(eb) != 0);
  1302. WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  1303. return eb;
  1304. }
  1305. int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
  1306. {
  1307. struct tree_mod_elem *tm;
  1308. int level;
  1309. struct extent_buffer *eb_root = btrfs_root_node(root);
  1310. tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
  1311. if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
  1312. level = tm->old_root.level;
  1313. } else {
  1314. level = btrfs_header_level(eb_root);
  1315. }
  1316. free_extent_buffer(eb_root);
  1317. return level;
  1318. }
  1319. static inline int should_cow_block(struct btrfs_trans_handle *trans,
  1320. struct btrfs_root *root,
  1321. struct extent_buffer *buf)
  1322. {
  1323. if (btrfs_is_testing(root->fs_info))
  1324. return 0;
  1325. /* ensure we can see the force_cow */
  1326. smp_rmb();
  1327. /*
  1328. * We do not need to cow a block if
  1329. * 1) this block is not created or changed in this transaction;
  1330. * 2) this block does not belong to TREE_RELOC tree;
  1331. * 3) the root is not forced COW.
  1332. *
  1333. * What is forced COW:
  1334. * when we create snapshot during committing the transaction,
  1335. * after we've finished coping src root, we must COW the shared
  1336. * block to ensure the metadata consistency.
  1337. */
  1338. if (btrfs_header_generation(buf) == trans->transid &&
  1339. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
  1340. !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
  1341. btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
  1342. !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
  1343. return 0;
  1344. return 1;
  1345. }
  1346. /*
  1347. * cows a single block, see __btrfs_cow_block for the real work.
  1348. * This version of it has extra checks so that a block isn't COWed more than
  1349. * once per transaction, as long as it hasn't been written yet
  1350. */
  1351. noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
  1352. struct btrfs_root *root, struct extent_buffer *buf,
  1353. struct extent_buffer *parent, int parent_slot,
  1354. struct extent_buffer **cow_ret)
  1355. {
  1356. struct btrfs_fs_info *fs_info = root->fs_info;
  1357. u64 search_start;
  1358. int ret;
  1359. if (trans->transaction != fs_info->running_transaction)
  1360. WARN(1, KERN_CRIT "trans %llu running %llu\n",
  1361. trans->transid,
  1362. fs_info->running_transaction->transid);
  1363. if (trans->transid != fs_info->generation)
  1364. WARN(1, KERN_CRIT "trans %llu running %llu\n",
  1365. trans->transid, fs_info->generation);
  1366. if (!should_cow_block(trans, root, buf)) {
  1367. trans->dirty = true;
  1368. *cow_ret = buf;
  1369. return 0;
  1370. }
  1371. search_start = buf->start & ~((u64)SZ_1G - 1);
  1372. if (parent)
  1373. btrfs_set_lock_blocking(parent);
  1374. btrfs_set_lock_blocking(buf);
  1375. ret = __btrfs_cow_block(trans, root, buf, parent,
  1376. parent_slot, cow_ret, search_start, 0);
  1377. trace_btrfs_cow_block(root, buf, *cow_ret);
  1378. return ret;
  1379. }
  1380. /*
  1381. * helper function for defrag to decide if two blocks pointed to by a
  1382. * node are actually close by
  1383. */
  1384. static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
  1385. {
  1386. if (blocknr < other && other - (blocknr + blocksize) < 32768)
  1387. return 1;
  1388. if (blocknr > other && blocknr - (other + blocksize) < 32768)
  1389. return 1;
  1390. return 0;
  1391. }
  1392. /*
  1393. * compare two keys in a memcmp fashion
  1394. */
  1395. static int comp_keys(const struct btrfs_disk_key *disk,
  1396. const struct btrfs_key *k2)
  1397. {
  1398. struct btrfs_key k1;
  1399. btrfs_disk_key_to_cpu(&k1, disk);
  1400. return btrfs_comp_cpu_keys(&k1, k2);
  1401. }
  1402. /*
  1403. * same as comp_keys only with two btrfs_key's
  1404. */
  1405. int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
  1406. {
  1407. if (k1->objectid > k2->objectid)
  1408. return 1;
  1409. if (k1->objectid < k2->objectid)
  1410. return -1;
  1411. if (k1->type > k2->type)
  1412. return 1;
  1413. if (k1->type < k2->type)
  1414. return -1;
  1415. if (k1->offset > k2->offset)
  1416. return 1;
  1417. if (k1->offset < k2->offset)
  1418. return -1;
  1419. return 0;
  1420. }
  1421. /*
  1422. * this is used by the defrag code to go through all the
  1423. * leaves pointed to by a node and reallocate them so that
  1424. * disk order is close to key order
  1425. */
  1426. int btrfs_realloc_node(struct btrfs_trans_handle *trans,
  1427. struct btrfs_root *root, struct extent_buffer *parent,
  1428. int start_slot, u64 *last_ret,
  1429. struct btrfs_key *progress)
  1430. {
  1431. struct btrfs_fs_info *fs_info = root->fs_info;
  1432. struct extent_buffer *cur;
  1433. u64 blocknr;
  1434. u64 gen;
  1435. u64 search_start = *last_ret;
  1436. u64 last_block = 0;
  1437. u64 other;
  1438. u32 parent_nritems;
  1439. int end_slot;
  1440. int i;
  1441. int err = 0;
  1442. int parent_level;
  1443. int uptodate;
  1444. u32 blocksize;
  1445. int progress_passed = 0;
  1446. struct btrfs_disk_key disk_key;
  1447. parent_level = btrfs_header_level(parent);
  1448. WARN_ON(trans->transaction != fs_info->running_transaction);
  1449. WARN_ON(trans->transid != fs_info->generation);
  1450. parent_nritems = btrfs_header_nritems(parent);
  1451. blocksize = fs_info->nodesize;
  1452. end_slot = parent_nritems - 1;
  1453. if (parent_nritems <= 1)
  1454. return 0;
  1455. btrfs_set_lock_blocking(parent);
  1456. for (i = start_slot; i <= end_slot; i++) {
  1457. int close = 1;
  1458. btrfs_node_key(parent, &disk_key, i);
  1459. if (!progress_passed && comp_keys(&disk_key, progress) < 0)
  1460. continue;
  1461. progress_passed = 1;
  1462. blocknr = btrfs_node_blockptr(parent, i);
  1463. gen = btrfs_node_ptr_generation(parent, i);
  1464. if (last_block == 0)
  1465. last_block = blocknr;
  1466. if (i > 0) {
  1467. other = btrfs_node_blockptr(parent, i - 1);
  1468. close = close_blocks(blocknr, other, blocksize);
  1469. }
  1470. if (!close && i < end_slot) {
  1471. other = btrfs_node_blockptr(parent, i + 1);
  1472. close = close_blocks(blocknr, other, blocksize);
  1473. }
  1474. if (close) {
  1475. last_block = blocknr;
  1476. continue;
  1477. }
  1478. cur = find_extent_buffer(fs_info, blocknr);
  1479. if (cur)
  1480. uptodate = btrfs_buffer_uptodate(cur, gen, 0);
  1481. else
  1482. uptodate = 0;
  1483. if (!cur || !uptodate) {
  1484. if (!cur) {
  1485. cur = read_tree_block(fs_info, blocknr, gen);
  1486. if (IS_ERR(cur)) {
  1487. return PTR_ERR(cur);
  1488. } else if (!extent_buffer_uptodate(cur)) {
  1489. free_extent_buffer(cur);
  1490. return -EIO;
  1491. }
  1492. } else if (!uptodate) {
  1493. err = btrfs_read_buffer(cur, gen);
  1494. if (err) {
  1495. free_extent_buffer(cur);
  1496. return err;
  1497. }
  1498. }
  1499. }
  1500. if (search_start == 0)
  1501. search_start = last_block;
  1502. btrfs_tree_lock(cur);
  1503. btrfs_set_lock_blocking(cur);
  1504. err = __btrfs_cow_block(trans, root, cur, parent, i,
  1505. &cur, search_start,
  1506. min(16 * blocksize,
  1507. (end_slot - i) * blocksize));
  1508. if (err) {
  1509. btrfs_tree_unlock(cur);
  1510. free_extent_buffer(cur);
  1511. break;
  1512. }
  1513. search_start = cur->start;
  1514. last_block = cur->start;
  1515. *last_ret = search_start;
  1516. btrfs_tree_unlock(cur);
  1517. free_extent_buffer(cur);
  1518. }
  1519. return err;
  1520. }
  1521. /*
  1522. * search for key in the extent_buffer. The items start at offset p,
  1523. * and they are item_size apart. There are 'max' items in p.
  1524. *
  1525. * the slot in the array is returned via slot, and it points to
  1526. * the place where you would insert key if it is not found in
  1527. * the array.
  1528. *
  1529. * slot may point to max if the key is bigger than all of the keys
  1530. */
  1531. static noinline int generic_bin_search(struct extent_buffer *eb,
  1532. unsigned long p, int item_size,
  1533. const struct btrfs_key *key,
  1534. int max, int *slot)
  1535. {
  1536. int low = 0;
  1537. int high = max;
  1538. int mid;
  1539. int ret;
  1540. struct btrfs_disk_key *tmp = NULL;
  1541. struct btrfs_disk_key unaligned;
  1542. unsigned long offset;
  1543. char *kaddr = NULL;
  1544. unsigned long map_start = 0;
  1545. unsigned long map_len = 0;
  1546. int err;
  1547. if (low > high) {
  1548. btrfs_err(eb->fs_info,
  1549. "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
  1550. __func__, low, high, eb->start,
  1551. btrfs_header_owner(eb), btrfs_header_level(eb));
  1552. return -EINVAL;
  1553. }
  1554. while (low < high) {
  1555. mid = (low + high) / 2;
  1556. offset = p + mid * item_size;
  1557. if (!kaddr || offset < map_start ||
  1558. (offset + sizeof(struct btrfs_disk_key)) >
  1559. map_start + map_len) {
  1560. err = map_private_extent_buffer(eb, offset,
  1561. sizeof(struct btrfs_disk_key),
  1562. &kaddr, &map_start, &map_len);
  1563. if (!err) {
  1564. tmp = (struct btrfs_disk_key *)(kaddr + offset -
  1565. map_start);
  1566. } else if (err == 1) {
  1567. read_extent_buffer(eb, &unaligned,
  1568. offset, sizeof(unaligned));
  1569. tmp = &unaligned;
  1570. } else {
  1571. return err;
  1572. }
  1573. } else {
  1574. tmp = (struct btrfs_disk_key *)(kaddr + offset -
  1575. map_start);
  1576. }
  1577. ret = comp_keys(tmp, key);
  1578. if (ret < 0)
  1579. low = mid + 1;
  1580. else if (ret > 0)
  1581. high = mid;
  1582. else {
  1583. *slot = mid;
  1584. return 0;
  1585. }
  1586. }
  1587. *slot = low;
  1588. return 1;
  1589. }
  1590. /*
  1591. * simple bin_search frontend that does the right thing for
  1592. * leaves vs nodes
  1593. */
  1594. static int bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
  1595. int level, int *slot)
  1596. {
  1597. if (level == 0)
  1598. return generic_bin_search(eb,
  1599. offsetof(struct btrfs_leaf, items),
  1600. sizeof(struct btrfs_item),
  1601. key, btrfs_header_nritems(eb),
  1602. slot);
  1603. else
  1604. return generic_bin_search(eb,
  1605. offsetof(struct btrfs_node, ptrs),
  1606. sizeof(struct btrfs_key_ptr),
  1607. key, btrfs_header_nritems(eb),
  1608. slot);
  1609. }
  1610. int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
  1611. int level, int *slot)
  1612. {
  1613. return bin_search(eb, key, level, slot);
  1614. }
  1615. static void root_add_used(struct btrfs_root *root, u32 size)
  1616. {
  1617. spin_lock(&root->accounting_lock);
  1618. btrfs_set_root_used(&root->root_item,
  1619. btrfs_root_used(&root->root_item) + size);
  1620. spin_unlock(&root->accounting_lock);
  1621. }
  1622. static void root_sub_used(struct btrfs_root *root, u32 size)
  1623. {
  1624. spin_lock(&root->accounting_lock);
  1625. btrfs_set_root_used(&root->root_item,
  1626. btrfs_root_used(&root->root_item) - size);
  1627. spin_unlock(&root->accounting_lock);
  1628. }
  1629. /* given a node and slot number, this reads the blocks it points to. The
  1630. * extent buffer is returned with a reference taken (but unlocked).
  1631. */
  1632. static noinline struct extent_buffer *
  1633. read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent,
  1634. int slot)
  1635. {
  1636. int level = btrfs_header_level(parent);
  1637. struct extent_buffer *eb;
  1638. if (slot < 0 || slot >= btrfs_header_nritems(parent))
  1639. return ERR_PTR(-ENOENT);
  1640. BUG_ON(level == 0);
  1641. eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
  1642. btrfs_node_ptr_generation(parent, slot));
  1643. if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
  1644. free_extent_buffer(eb);
  1645. eb = ERR_PTR(-EIO);
  1646. }
  1647. return eb;
  1648. }
  1649. /*
  1650. * node level balancing, used to make sure nodes are in proper order for
  1651. * item deletion. We balance from the top down, so we have to make sure
  1652. * that a deletion won't leave an node completely empty later on.
  1653. */
  1654. static noinline int balance_level(struct btrfs_trans_handle *trans,
  1655. struct btrfs_root *root,
  1656. struct btrfs_path *path, int level)
  1657. {
  1658. struct btrfs_fs_info *fs_info = root->fs_info;
  1659. struct extent_buffer *right = NULL;
  1660. struct extent_buffer *mid;
  1661. struct extent_buffer *left = NULL;
  1662. struct extent_buffer *parent = NULL;
  1663. int ret = 0;
  1664. int wret;
  1665. int pslot;
  1666. int orig_slot = path->slots[level];
  1667. u64 orig_ptr;
  1668. if (level == 0)
  1669. return 0;
  1670. mid = path->nodes[level];
  1671. WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
  1672. path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
  1673. WARN_ON(btrfs_header_generation(mid) != trans->transid);
  1674. orig_ptr = btrfs_node_blockptr(mid, orig_slot);
  1675. if (level < BTRFS_MAX_LEVEL - 1) {
  1676. parent = path->nodes[level + 1];
  1677. pslot = path->slots[level + 1];
  1678. }
  1679. /*
  1680. * deal with the case where there is only one pointer in the root
  1681. * by promoting the node below to a root
  1682. */
  1683. if (!parent) {
  1684. struct extent_buffer *child;
  1685. if (btrfs_header_nritems(mid) != 1)
  1686. return 0;
  1687. /* promote the child to a root */
  1688. child = read_node_slot(fs_info, mid, 0);
  1689. if (IS_ERR(child)) {
  1690. ret = PTR_ERR(child);
  1691. btrfs_handle_fs_error(fs_info, ret, NULL);
  1692. goto enospc;
  1693. }
  1694. btrfs_tree_lock(child);
  1695. btrfs_set_lock_blocking(child);
  1696. ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
  1697. if (ret) {
  1698. btrfs_tree_unlock(child);
  1699. free_extent_buffer(child);
  1700. goto enospc;
  1701. }
  1702. tree_mod_log_set_root_pointer(root, child, 1);
  1703. rcu_assign_pointer(root->node, child);
  1704. add_root_to_dirty_list(root);
  1705. btrfs_tree_unlock(child);
  1706. path->locks[level] = 0;
  1707. path->nodes[level] = NULL;
  1708. clean_tree_block(fs_info, mid);
  1709. btrfs_tree_unlock(mid);
  1710. /* once for the path */
  1711. free_extent_buffer(mid);
  1712. root_sub_used(root, mid->len);
  1713. btrfs_free_tree_block(trans, root, mid, 0, 1);
  1714. /* once for the root ptr */
  1715. free_extent_buffer_stale(mid);
  1716. return 0;
  1717. }
  1718. if (btrfs_header_nritems(mid) >
  1719. BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
  1720. return 0;
  1721. left = read_node_slot(fs_info, parent, pslot - 1);
  1722. if (IS_ERR(left))
  1723. left = NULL;
  1724. if (left) {
  1725. btrfs_tree_lock(left);
  1726. btrfs_set_lock_blocking(left);
  1727. wret = btrfs_cow_block(trans, root, left,
  1728. parent, pslot - 1, &left);
  1729. if (wret) {
  1730. ret = wret;
  1731. goto enospc;
  1732. }
  1733. }
  1734. right = read_node_slot(fs_info, parent, pslot + 1);
  1735. if (IS_ERR(right))
  1736. right = NULL;
  1737. if (right) {
  1738. btrfs_tree_lock(right);
  1739. btrfs_set_lock_blocking(right);
  1740. wret = btrfs_cow_block(trans, root, right,
  1741. parent, pslot + 1, &right);
  1742. if (wret) {
  1743. ret = wret;
  1744. goto enospc;
  1745. }
  1746. }
  1747. /* first, try to make some room in the middle buffer */
  1748. if (left) {
  1749. orig_slot += btrfs_header_nritems(left);
  1750. wret = push_node_left(trans, fs_info, left, mid, 1);
  1751. if (wret < 0)
  1752. ret = wret;
  1753. }
  1754. /*
  1755. * then try to empty the right most buffer into the middle
  1756. */
  1757. if (right) {
  1758. wret = push_node_left(trans, fs_info, mid, right, 1);
  1759. if (wret < 0 && wret != -ENOSPC)
  1760. ret = wret;
  1761. if (btrfs_header_nritems(right) == 0) {
  1762. clean_tree_block(fs_info, right);
  1763. btrfs_tree_unlock(right);
  1764. del_ptr(root, path, level + 1, pslot + 1);
  1765. root_sub_used(root, right->len);
  1766. btrfs_free_tree_block(trans, root, right, 0, 1);
  1767. free_extent_buffer_stale(right);
  1768. right = NULL;
  1769. } else {
  1770. struct btrfs_disk_key right_key;
  1771. btrfs_node_key(right, &right_key, 0);
  1772. tree_mod_log_set_node_key(fs_info, parent,
  1773. pslot + 1, 0);
  1774. btrfs_set_node_key(parent, &right_key, pslot + 1);
  1775. btrfs_mark_buffer_dirty(parent);
  1776. }
  1777. }
  1778. if (btrfs_header_nritems(mid) == 1) {
  1779. /*
  1780. * we're not allowed to leave a node with one item in the
  1781. * tree during a delete. A deletion from lower in the tree
  1782. * could try to delete the only pointer in this node.
  1783. * So, pull some keys from the left.
  1784. * There has to be a left pointer at this point because
  1785. * otherwise we would have pulled some pointers from the
  1786. * right
  1787. */
  1788. if (!left) {
  1789. ret = -EROFS;
  1790. btrfs_handle_fs_error(fs_info, ret, NULL);
  1791. goto enospc;
  1792. }
  1793. wret = balance_node_right(trans, fs_info, mid, left);
  1794. if (wret < 0) {
  1795. ret = wret;
  1796. goto enospc;
  1797. }
  1798. if (wret == 1) {
  1799. wret = push_node_left(trans, fs_info, left, mid, 1);
  1800. if (wret < 0)
  1801. ret = wret;
  1802. }
  1803. BUG_ON(wret == 1);
  1804. }
  1805. if (btrfs_header_nritems(mid) == 0) {
  1806. clean_tree_block(fs_info, mid);
  1807. btrfs_tree_unlock(mid);
  1808. del_ptr(root, path, level + 1, pslot);
  1809. root_sub_used(root, mid->len);
  1810. btrfs_free_tree_block(trans, root, mid, 0, 1);
  1811. free_extent_buffer_stale(mid);
  1812. mid = NULL;
  1813. } else {
  1814. /* update the parent key to reflect our changes */
  1815. struct btrfs_disk_key mid_key;
  1816. btrfs_node_key(mid, &mid_key, 0);
  1817. tree_mod_log_set_node_key(fs_info, parent, pslot, 0);
  1818. btrfs_set_node_key(parent, &mid_key, pslot);
  1819. btrfs_mark_buffer_dirty(parent);
  1820. }
  1821. /* update the path */
  1822. if (left) {
  1823. if (btrfs_header_nritems(left) > orig_slot) {
  1824. extent_buffer_get(left);
  1825. /* left was locked after cow */
  1826. path->nodes[level] = left;
  1827. path->slots[level + 1] -= 1;
  1828. path->slots[level] = orig_slot;
  1829. if (mid) {
  1830. btrfs_tree_unlock(mid);
  1831. free_extent_buffer(mid);
  1832. }
  1833. } else {
  1834. orig_slot -= btrfs_header_nritems(left);
  1835. path->slots[level] = orig_slot;
  1836. }
  1837. }
  1838. /* double check we haven't messed things up */
  1839. if (orig_ptr !=
  1840. btrfs_node_blockptr(path->nodes[level], path->slots[level]))
  1841. BUG();
  1842. enospc:
  1843. if (right) {
  1844. btrfs_tree_unlock(right);
  1845. free_extent_buffer(right);
  1846. }
  1847. if (left) {
  1848. if (path->nodes[level] != left)
  1849. btrfs_tree_unlock(left);
  1850. free_extent_buffer(left);
  1851. }
  1852. return ret;
  1853. }
  1854. /* Node balancing for insertion. Here we only split or push nodes around
  1855. * when they are completely full. This is also done top down, so we
  1856. * have to be pessimistic.
  1857. */
  1858. static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
  1859. struct btrfs_root *root,
  1860. struct btrfs_path *path, int level)
  1861. {
  1862. struct btrfs_fs_info *fs_info = root->fs_info;
  1863. struct extent_buffer *right = NULL;
  1864. struct extent_buffer *mid;
  1865. struct extent_buffer *left = NULL;
  1866. struct extent_buffer *parent = NULL;
  1867. int ret = 0;
  1868. int wret;
  1869. int pslot;
  1870. int orig_slot = path->slots[level];
  1871. if (level == 0)
  1872. return 1;
  1873. mid = path->nodes[level];
  1874. WARN_ON(btrfs_header_generation(mid) != trans->transid);
  1875. if (level < BTRFS_MAX_LEVEL - 1) {
  1876. parent = path->nodes[level + 1];
  1877. pslot = path->slots[level + 1];
  1878. }
  1879. if (!parent)
  1880. return 1;
  1881. left = read_node_slot(fs_info, parent, pslot - 1);
  1882. if (IS_ERR(left))
  1883. left = NULL;
  1884. /* first, try to make some room in the middle buffer */
  1885. if (left) {
  1886. u32 left_nr;
  1887. btrfs_tree_lock(left);
  1888. btrfs_set_lock_blocking(left);
  1889. left_nr = btrfs_header_nritems(left);
  1890. if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
  1891. wret = 1;
  1892. } else {
  1893. ret = btrfs_cow_block(trans, root, left, parent,
  1894. pslot - 1, &left);
  1895. if (ret)
  1896. wret = 1;
  1897. else {
  1898. wret = push_node_left(trans, fs_info,
  1899. left, mid, 0);
  1900. }
  1901. }
  1902. if (wret < 0)
  1903. ret = wret;
  1904. if (wret == 0) {
  1905. struct btrfs_disk_key disk_key;
  1906. orig_slot += left_nr;
  1907. btrfs_node_key(mid, &disk_key, 0);
  1908. tree_mod_log_set_node_key(fs_info, parent, pslot, 0);
  1909. btrfs_set_node_key(parent, &disk_key, pslot);
  1910. btrfs_mark_buffer_dirty(parent);
  1911. if (btrfs_header_nritems(left) > orig_slot) {
  1912. path->nodes[level] = left;
  1913. path->slots[level + 1] -= 1;
  1914. path->slots[level] = orig_slot;
  1915. btrfs_tree_unlock(mid);
  1916. free_extent_buffer(mid);
  1917. } else {
  1918. orig_slot -=
  1919. btrfs_header_nritems(left);
  1920. path->slots[level] = orig_slot;
  1921. btrfs_tree_unlock(left);
  1922. free_extent_buffer(left);
  1923. }
  1924. return 0;
  1925. }
  1926. btrfs_tree_unlock(left);
  1927. free_extent_buffer(left);
  1928. }
  1929. right = read_node_slot(fs_info, parent, pslot + 1);
  1930. if (IS_ERR(right))
  1931. right = NULL;
  1932. /*
  1933. * then try to empty the right most buffer into the middle
  1934. */
  1935. if (right) {
  1936. u32 right_nr;
  1937. btrfs_tree_lock(right);
  1938. btrfs_set_lock_blocking(right);
  1939. right_nr = btrfs_header_nritems(right);
  1940. if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
  1941. wret = 1;
  1942. } else {
  1943. ret = btrfs_cow_block(trans, root, right,
  1944. parent, pslot + 1,
  1945. &right);
  1946. if (ret)
  1947. wret = 1;
  1948. else {
  1949. wret = balance_node_right(trans, fs_info,
  1950. right, mid);
  1951. }
  1952. }
  1953. if (wret < 0)
  1954. ret = wret;
  1955. if (wret == 0) {
  1956. struct btrfs_disk_key disk_key;
  1957. btrfs_node_key(right, &disk_key, 0);
  1958. tree_mod_log_set_node_key(fs_info, parent,
  1959. pslot + 1, 0);
  1960. btrfs_set_node_key(parent, &disk_key, pslot + 1);
  1961. btrfs_mark_buffer_dirty(parent);
  1962. if (btrfs_header_nritems(mid) <= orig_slot) {
  1963. path->nodes[level] = right;
  1964. path->slots[level + 1] += 1;
  1965. path->slots[level] = orig_slot -
  1966. btrfs_header_nritems(mid);
  1967. btrfs_tree_unlock(mid);
  1968. free_extent_buffer(mid);
  1969. } else {
  1970. btrfs_tree_unlock(right);
  1971. free_extent_buffer(right);
  1972. }
  1973. return 0;
  1974. }
  1975. btrfs_tree_unlock(right);
  1976. free_extent_buffer(right);
  1977. }
  1978. return 1;
  1979. }
  1980. /*
  1981. * readahead one full node of leaves, finding things that are close
  1982. * to the block in 'slot', and triggering ra on them.
  1983. */
  1984. static void reada_for_search(struct btrfs_fs_info *fs_info,
  1985. struct btrfs_path *path,
  1986. int level, int slot, u64 objectid)
  1987. {
  1988. struct extent_buffer *node;
  1989. struct btrfs_disk_key disk_key;
  1990. u32 nritems;
  1991. u64 search;
  1992. u64 target;
  1993. u64 nread = 0;
  1994. struct extent_buffer *eb;
  1995. u32 nr;
  1996. u32 blocksize;
  1997. u32 nscan = 0;
  1998. if (level != 1)
  1999. return;
  2000. if (!path->nodes[level])
  2001. return;
  2002. node = path->nodes[level];
  2003. search = btrfs_node_blockptr(node, slot);
  2004. blocksize = fs_info->nodesize;
  2005. eb = find_extent_buffer(fs_info, search);
  2006. if (eb) {
  2007. free_extent_buffer(eb);
  2008. return;
  2009. }
  2010. target = search;
  2011. nritems = btrfs_header_nritems(node);
  2012. nr = slot;
  2013. while (1) {
  2014. if (path->reada == READA_BACK) {
  2015. if (nr == 0)
  2016. break;
  2017. nr--;
  2018. } else if (path->reada == READA_FORWARD) {
  2019. nr++;
  2020. if (nr >= nritems)
  2021. break;
  2022. }
  2023. if (path->reada == READA_BACK && objectid) {
  2024. btrfs_node_key(node, &disk_key, nr);
  2025. if (btrfs_disk_key_objectid(&disk_key) != objectid)
  2026. break;
  2027. }
  2028. search = btrfs_node_blockptr(node, nr);
  2029. if ((search <= target && target - search <= 65536) ||
  2030. (search > target && search - target <= 65536)) {
  2031. readahead_tree_block(fs_info, search);
  2032. nread += blocksize;
  2033. }
  2034. nscan++;
  2035. if ((nread > 65536 || nscan > 32))
  2036. break;
  2037. }
  2038. }
  2039. static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
  2040. struct btrfs_path *path, int level)
  2041. {
  2042. int slot;
  2043. int nritems;
  2044. struct extent_buffer *parent;
  2045. struct extent_buffer *eb;
  2046. u64 gen;
  2047. u64 block1 = 0;
  2048. u64 block2 = 0;
  2049. parent = path->nodes[level + 1];
  2050. if (!parent)
  2051. return;
  2052. nritems = btrfs_header_nritems(parent);
  2053. slot = path->slots[level + 1];
  2054. if (slot > 0) {
  2055. block1 = btrfs_node_blockptr(parent, slot - 1);
  2056. gen = btrfs_node_ptr_generation(parent, slot - 1);
  2057. eb = find_extent_buffer(fs_info, block1);
  2058. /*
  2059. * if we get -eagain from btrfs_buffer_uptodate, we
  2060. * don't want to return eagain here. That will loop
  2061. * forever
  2062. */
  2063. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  2064. block1 = 0;
  2065. free_extent_buffer(eb);
  2066. }
  2067. if (slot + 1 < nritems) {
  2068. block2 = btrfs_node_blockptr(parent, slot + 1);
  2069. gen = btrfs_node_ptr_generation(parent, slot + 1);
  2070. eb = find_extent_buffer(fs_info, block2);
  2071. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  2072. block2 = 0;
  2073. free_extent_buffer(eb);
  2074. }
  2075. if (block1)
  2076. readahead_tree_block(fs_info, block1);
  2077. if (block2)
  2078. readahead_tree_block(fs_info, block2);
  2079. }
  2080. /*
  2081. * when we walk down the tree, it is usually safe to unlock the higher layers
  2082. * in the tree. The exceptions are when our path goes through slot 0, because
  2083. * operations on the tree might require changing key pointers higher up in the
  2084. * tree.
  2085. *
  2086. * callers might also have set path->keep_locks, which tells this code to keep
  2087. * the lock if the path points to the last slot in the block. This is part of
  2088. * walking through the tree, and selecting the next slot in the higher block.
  2089. *
  2090. * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
  2091. * if lowest_unlock is 1, level 0 won't be unlocked
  2092. */
  2093. static noinline void unlock_up(struct btrfs_path *path, int level,
  2094. int lowest_unlock, int min_write_lock_level,
  2095. int *write_lock_level)
  2096. {
  2097. int i;
  2098. int skip_level = level;
  2099. int no_skips = 0;
  2100. struct extent_buffer *t;
  2101. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2102. if (!path->nodes[i])
  2103. break;
  2104. if (!path->locks[i])
  2105. break;
  2106. if (!no_skips && path->slots[i] == 0) {
  2107. skip_level = i + 1;
  2108. continue;
  2109. }
  2110. if (!no_skips && path->keep_locks) {
  2111. u32 nritems;
  2112. t = path->nodes[i];
  2113. nritems = btrfs_header_nritems(t);
  2114. if (nritems < 1 || path->slots[i] >= nritems - 1) {
  2115. skip_level = i + 1;
  2116. continue;
  2117. }
  2118. }
  2119. if (skip_level < i && i >= lowest_unlock)
  2120. no_skips = 1;
  2121. t = path->nodes[i];
  2122. if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
  2123. btrfs_tree_unlock_rw(t, path->locks[i]);
  2124. path->locks[i] = 0;
  2125. if (write_lock_level &&
  2126. i > min_write_lock_level &&
  2127. i <= *write_lock_level) {
  2128. *write_lock_level = i - 1;
  2129. }
  2130. }
  2131. }
  2132. }
  2133. /*
  2134. * This releases any locks held in the path starting at level and
  2135. * going all the way up to the root.
  2136. *
  2137. * btrfs_search_slot will keep the lock held on higher nodes in a few
  2138. * corner cases, such as COW of the block at slot zero in the node. This
  2139. * ignores those rules, and it should only be called when there are no
  2140. * more updates to be done higher up in the tree.
  2141. */
  2142. noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
  2143. {
  2144. int i;
  2145. if (path->keep_locks)
  2146. return;
  2147. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2148. if (!path->nodes[i])
  2149. continue;
  2150. if (!path->locks[i])
  2151. continue;
  2152. btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
  2153. path->locks[i] = 0;
  2154. }
  2155. }
  2156. /*
  2157. * helper function for btrfs_search_slot. The goal is to find a block
  2158. * in cache without setting the path to blocking. If we find the block
  2159. * we return zero and the path is unchanged.
  2160. *
  2161. * If we can't find the block, we set the path blocking and do some
  2162. * reada. -EAGAIN is returned and the search must be repeated.
  2163. */
  2164. static int
  2165. read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
  2166. struct extent_buffer **eb_ret, int level, int slot,
  2167. const struct btrfs_key *key)
  2168. {
  2169. struct btrfs_fs_info *fs_info = root->fs_info;
  2170. u64 blocknr;
  2171. u64 gen;
  2172. struct extent_buffer *b = *eb_ret;
  2173. struct extent_buffer *tmp;
  2174. int ret;
  2175. blocknr = btrfs_node_blockptr(b, slot);
  2176. gen = btrfs_node_ptr_generation(b, slot);
  2177. tmp = find_extent_buffer(fs_info, blocknr);
  2178. if (tmp) {
  2179. /* first we do an atomic uptodate check */
  2180. if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
  2181. *eb_ret = tmp;
  2182. return 0;
  2183. }
  2184. /* the pages were up to date, but we failed
  2185. * the generation number check. Do a full
  2186. * read for the generation number that is correct.
  2187. * We must do this without dropping locks so
  2188. * we can trust our generation number
  2189. */
  2190. btrfs_set_path_blocking(p);
  2191. /* now we're allowed to do a blocking uptodate check */
  2192. ret = btrfs_read_buffer(tmp, gen);
  2193. if (!ret) {
  2194. *eb_ret = tmp;
  2195. return 0;
  2196. }
  2197. free_extent_buffer(tmp);
  2198. btrfs_release_path(p);
  2199. return -EIO;
  2200. }
  2201. /*
  2202. * reduce lock contention at high levels
  2203. * of the btree by dropping locks before
  2204. * we read. Don't release the lock on the current
  2205. * level because we need to walk this node to figure
  2206. * out which blocks to read.
  2207. */
  2208. btrfs_unlock_up_safe(p, level + 1);
  2209. btrfs_set_path_blocking(p);
  2210. free_extent_buffer(tmp);
  2211. if (p->reada != READA_NONE)
  2212. reada_for_search(fs_info, p, level, slot, key->objectid);
  2213. btrfs_release_path(p);
  2214. ret = -EAGAIN;
  2215. tmp = read_tree_block(fs_info, blocknr, 0);
  2216. if (!IS_ERR(tmp)) {
  2217. /*
  2218. * If the read above didn't mark this buffer up to date,
  2219. * it will never end up being up to date. Set ret to EIO now
  2220. * and give up so that our caller doesn't loop forever
  2221. * on our EAGAINs.
  2222. */
  2223. if (!btrfs_buffer_uptodate(tmp, 0, 0))
  2224. ret = -EIO;
  2225. free_extent_buffer(tmp);
  2226. } else {
  2227. ret = PTR_ERR(tmp);
  2228. }
  2229. return ret;
  2230. }
  2231. /*
  2232. * helper function for btrfs_search_slot. This does all of the checks
  2233. * for node-level blocks and does any balancing required based on
  2234. * the ins_len.
  2235. *
  2236. * If no extra work was required, zero is returned. If we had to
  2237. * drop the path, -EAGAIN is returned and btrfs_search_slot must
  2238. * start over
  2239. */
  2240. static int
  2241. setup_nodes_for_search(struct btrfs_trans_handle *trans,
  2242. struct btrfs_root *root, struct btrfs_path *p,
  2243. struct extent_buffer *b, int level, int ins_len,
  2244. int *write_lock_level)
  2245. {
  2246. struct btrfs_fs_info *fs_info = root->fs_info;
  2247. int ret;
  2248. if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
  2249. BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
  2250. int sret;
  2251. if (*write_lock_level < level + 1) {
  2252. *write_lock_level = level + 1;
  2253. btrfs_release_path(p);
  2254. goto again;
  2255. }
  2256. btrfs_set_path_blocking(p);
  2257. reada_for_balance(fs_info, p, level);
  2258. sret = split_node(trans, root, p, level);
  2259. btrfs_clear_path_blocking(p, NULL, 0);
  2260. BUG_ON(sret > 0);
  2261. if (sret) {
  2262. ret = sret;
  2263. goto done;
  2264. }
  2265. b = p->nodes[level];
  2266. } else if (ins_len < 0 && btrfs_header_nritems(b) <
  2267. BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
  2268. int sret;
  2269. if (*write_lock_level < level + 1) {
  2270. *write_lock_level = level + 1;
  2271. btrfs_release_path(p);
  2272. goto again;
  2273. }
  2274. btrfs_set_path_blocking(p);
  2275. reada_for_balance(fs_info, p, level);
  2276. sret = balance_level(trans, root, p, level);
  2277. btrfs_clear_path_blocking(p, NULL, 0);
  2278. if (sret) {
  2279. ret = sret;
  2280. goto done;
  2281. }
  2282. b = p->nodes[level];
  2283. if (!b) {
  2284. btrfs_release_path(p);
  2285. goto again;
  2286. }
  2287. BUG_ON(btrfs_header_nritems(b) == 1);
  2288. }
  2289. return 0;
  2290. again:
  2291. ret = -EAGAIN;
  2292. done:
  2293. return ret;
  2294. }
  2295. static void key_search_validate(struct extent_buffer *b,
  2296. const struct btrfs_key *key,
  2297. int level)
  2298. {
  2299. #ifdef CONFIG_BTRFS_ASSERT
  2300. struct btrfs_disk_key disk_key;
  2301. btrfs_cpu_key_to_disk(&disk_key, key);
  2302. if (level == 0)
  2303. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2304. offsetof(struct btrfs_leaf, items[0].key),
  2305. sizeof(disk_key)));
  2306. else
  2307. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2308. offsetof(struct btrfs_node, ptrs[0].key),
  2309. sizeof(disk_key)));
  2310. #endif
  2311. }
  2312. static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
  2313. int level, int *prev_cmp, int *slot)
  2314. {
  2315. if (*prev_cmp != 0) {
  2316. *prev_cmp = bin_search(b, key, level, slot);
  2317. return *prev_cmp;
  2318. }
  2319. key_search_validate(b, key, level);
  2320. *slot = 0;
  2321. return 0;
  2322. }
  2323. int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
  2324. u64 iobjectid, u64 ioff, u8 key_type,
  2325. struct btrfs_key *found_key)
  2326. {
  2327. int ret;
  2328. struct btrfs_key key;
  2329. struct extent_buffer *eb;
  2330. ASSERT(path);
  2331. ASSERT(found_key);
  2332. key.type = key_type;
  2333. key.objectid = iobjectid;
  2334. key.offset = ioff;
  2335. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  2336. if (ret < 0)
  2337. return ret;
  2338. eb = path->nodes[0];
  2339. if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
  2340. ret = btrfs_next_leaf(fs_root, path);
  2341. if (ret)
  2342. return ret;
  2343. eb = path->nodes[0];
  2344. }
  2345. btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
  2346. if (found_key->type != key.type ||
  2347. found_key->objectid != key.objectid)
  2348. return 1;
  2349. return 0;
  2350. }
  2351. /*
  2352. * look for key in the tree. path is filled in with nodes along the way
  2353. * if key is found, we return zero and you can find the item in the leaf
  2354. * level of the path (level 0)
  2355. *
  2356. * If the key isn't found, the path points to the slot where it should
  2357. * be inserted, and 1 is returned. If there are other errors during the
  2358. * search a negative error number is returned.
  2359. *
  2360. * if ins_len > 0, nodes and leaves will be split as we walk down the
  2361. * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
  2362. * possible)
  2363. */
  2364. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2365. const struct btrfs_key *key, struct btrfs_path *p,
  2366. int ins_len, int cow)
  2367. {
  2368. struct btrfs_fs_info *fs_info = root->fs_info;
  2369. struct extent_buffer *b;
  2370. int slot;
  2371. int ret;
  2372. int err;
  2373. int level;
  2374. int lowest_unlock = 1;
  2375. int root_lock;
  2376. /* everything at write_lock_level or lower must be write locked */
  2377. int write_lock_level = 0;
  2378. u8 lowest_level = 0;
  2379. int min_write_lock_level;
  2380. int prev_cmp;
  2381. lowest_level = p->lowest_level;
  2382. WARN_ON(lowest_level && ins_len > 0);
  2383. WARN_ON(p->nodes[0] != NULL);
  2384. BUG_ON(!cow && ins_len);
  2385. if (ins_len < 0) {
  2386. lowest_unlock = 2;
  2387. /* when we are removing items, we might have to go up to level
  2388. * two as we update tree pointers Make sure we keep write
  2389. * for those levels as well
  2390. */
  2391. write_lock_level = 2;
  2392. } else if (ins_len > 0) {
  2393. /*
  2394. * for inserting items, make sure we have a write lock on
  2395. * level 1 so we can update keys
  2396. */
  2397. write_lock_level = 1;
  2398. }
  2399. if (!cow)
  2400. write_lock_level = -1;
  2401. if (cow && (p->keep_locks || p->lowest_level))
  2402. write_lock_level = BTRFS_MAX_LEVEL;
  2403. min_write_lock_level = write_lock_level;
  2404. again:
  2405. prev_cmp = -1;
  2406. /*
  2407. * we try very hard to do read locks on the root
  2408. */
  2409. root_lock = BTRFS_READ_LOCK;
  2410. level = 0;
  2411. if (p->search_commit_root) {
  2412. /*
  2413. * the commit roots are read only
  2414. * so we always do read locks
  2415. */
  2416. if (p->need_commit_sem)
  2417. down_read(&fs_info->commit_root_sem);
  2418. b = root->commit_root;
  2419. extent_buffer_get(b);
  2420. level = btrfs_header_level(b);
  2421. if (p->need_commit_sem)
  2422. up_read(&fs_info->commit_root_sem);
  2423. if (!p->skip_locking)
  2424. btrfs_tree_read_lock(b);
  2425. } else {
  2426. if (p->skip_locking) {
  2427. b = btrfs_root_node(root);
  2428. level = btrfs_header_level(b);
  2429. } else {
  2430. /* we don't know the level of the root node
  2431. * until we actually have it read locked
  2432. */
  2433. b = btrfs_read_lock_root_node(root);
  2434. level = btrfs_header_level(b);
  2435. if (level <= write_lock_level) {
  2436. /* whoops, must trade for write lock */
  2437. btrfs_tree_read_unlock(b);
  2438. free_extent_buffer(b);
  2439. b = btrfs_lock_root_node(root);
  2440. root_lock = BTRFS_WRITE_LOCK;
  2441. /* the level might have changed, check again */
  2442. level = btrfs_header_level(b);
  2443. }
  2444. }
  2445. }
  2446. p->nodes[level] = b;
  2447. if (!p->skip_locking)
  2448. p->locks[level] = root_lock;
  2449. while (b) {
  2450. level = btrfs_header_level(b);
  2451. /*
  2452. * setup the path here so we can release it under lock
  2453. * contention with the cow code
  2454. */
  2455. if (cow) {
  2456. /*
  2457. * if we don't really need to cow this block
  2458. * then we don't want to set the path blocking,
  2459. * so we test it here
  2460. */
  2461. if (!should_cow_block(trans, root, b)) {
  2462. trans->dirty = true;
  2463. goto cow_done;
  2464. }
  2465. /*
  2466. * must have write locks on this node and the
  2467. * parent
  2468. */
  2469. if (level > write_lock_level ||
  2470. (level + 1 > write_lock_level &&
  2471. level + 1 < BTRFS_MAX_LEVEL &&
  2472. p->nodes[level + 1])) {
  2473. write_lock_level = level + 1;
  2474. btrfs_release_path(p);
  2475. goto again;
  2476. }
  2477. btrfs_set_path_blocking(p);
  2478. err = btrfs_cow_block(trans, root, b,
  2479. p->nodes[level + 1],
  2480. p->slots[level + 1], &b);
  2481. if (err) {
  2482. ret = err;
  2483. goto done;
  2484. }
  2485. }
  2486. cow_done:
  2487. p->nodes[level] = b;
  2488. btrfs_clear_path_blocking(p, NULL, 0);
  2489. /*
  2490. * we have a lock on b and as long as we aren't changing
  2491. * the tree, there is no way to for the items in b to change.
  2492. * It is safe to drop the lock on our parent before we
  2493. * go through the expensive btree search on b.
  2494. *
  2495. * If we're inserting or deleting (ins_len != 0), then we might
  2496. * be changing slot zero, which may require changing the parent.
  2497. * So, we can't drop the lock until after we know which slot
  2498. * we're operating on.
  2499. */
  2500. if (!ins_len && !p->keep_locks) {
  2501. int u = level + 1;
  2502. if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
  2503. btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
  2504. p->locks[u] = 0;
  2505. }
  2506. }
  2507. ret = key_search(b, key, level, &prev_cmp, &slot);
  2508. if (ret < 0)
  2509. goto done;
  2510. if (level != 0) {
  2511. int dec = 0;
  2512. if (ret && slot > 0) {
  2513. dec = 1;
  2514. slot -= 1;
  2515. }
  2516. p->slots[level] = slot;
  2517. err = setup_nodes_for_search(trans, root, p, b, level,
  2518. ins_len, &write_lock_level);
  2519. if (err == -EAGAIN)
  2520. goto again;
  2521. if (err) {
  2522. ret = err;
  2523. goto done;
  2524. }
  2525. b = p->nodes[level];
  2526. slot = p->slots[level];
  2527. /*
  2528. * slot 0 is special, if we change the key
  2529. * we have to update the parent pointer
  2530. * which means we must have a write lock
  2531. * on the parent
  2532. */
  2533. if (slot == 0 && ins_len &&
  2534. write_lock_level < level + 1) {
  2535. write_lock_level = level + 1;
  2536. btrfs_release_path(p);
  2537. goto again;
  2538. }
  2539. unlock_up(p, level, lowest_unlock,
  2540. min_write_lock_level, &write_lock_level);
  2541. if (level == lowest_level) {
  2542. if (dec)
  2543. p->slots[level]++;
  2544. goto done;
  2545. }
  2546. err = read_block_for_search(root, p, &b, level,
  2547. slot, key);
  2548. if (err == -EAGAIN)
  2549. goto again;
  2550. if (err) {
  2551. ret = err;
  2552. goto done;
  2553. }
  2554. if (!p->skip_locking) {
  2555. level = btrfs_header_level(b);
  2556. if (level <= write_lock_level) {
  2557. err = btrfs_try_tree_write_lock(b);
  2558. if (!err) {
  2559. btrfs_set_path_blocking(p);
  2560. btrfs_tree_lock(b);
  2561. btrfs_clear_path_blocking(p, b,
  2562. BTRFS_WRITE_LOCK);
  2563. }
  2564. p->locks[level] = BTRFS_WRITE_LOCK;
  2565. } else {
  2566. err = btrfs_tree_read_lock_atomic(b);
  2567. if (!err) {
  2568. btrfs_set_path_blocking(p);
  2569. btrfs_tree_read_lock(b);
  2570. btrfs_clear_path_blocking(p, b,
  2571. BTRFS_READ_LOCK);
  2572. }
  2573. p->locks[level] = BTRFS_READ_LOCK;
  2574. }
  2575. p->nodes[level] = b;
  2576. }
  2577. } else {
  2578. p->slots[level] = slot;
  2579. if (ins_len > 0 &&
  2580. btrfs_leaf_free_space(fs_info, b) < ins_len) {
  2581. if (write_lock_level < 1) {
  2582. write_lock_level = 1;
  2583. btrfs_release_path(p);
  2584. goto again;
  2585. }
  2586. btrfs_set_path_blocking(p);
  2587. err = split_leaf(trans, root, key,
  2588. p, ins_len, ret == 0);
  2589. btrfs_clear_path_blocking(p, NULL, 0);
  2590. BUG_ON(err > 0);
  2591. if (err) {
  2592. ret = err;
  2593. goto done;
  2594. }
  2595. }
  2596. if (!p->search_for_split)
  2597. unlock_up(p, level, lowest_unlock,
  2598. min_write_lock_level, &write_lock_level);
  2599. goto done;
  2600. }
  2601. }
  2602. ret = 1;
  2603. done:
  2604. /*
  2605. * we don't really know what they plan on doing with the path
  2606. * from here on, so for now just mark it as blocking
  2607. */
  2608. if (!p->leave_spinning)
  2609. btrfs_set_path_blocking(p);
  2610. if (ret < 0 && !p->skip_release_on_error)
  2611. btrfs_release_path(p);
  2612. return ret;
  2613. }
  2614. /*
  2615. * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
  2616. * current state of the tree together with the operations recorded in the tree
  2617. * modification log to search for the key in a previous version of this tree, as
  2618. * denoted by the time_seq parameter.
  2619. *
  2620. * Naturally, there is no support for insert, delete or cow operations.
  2621. *
  2622. * The resulting path and return value will be set up as if we called
  2623. * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
  2624. */
  2625. int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
  2626. struct btrfs_path *p, u64 time_seq)
  2627. {
  2628. struct btrfs_fs_info *fs_info = root->fs_info;
  2629. struct extent_buffer *b;
  2630. int slot;
  2631. int ret;
  2632. int err;
  2633. int level;
  2634. int lowest_unlock = 1;
  2635. u8 lowest_level = 0;
  2636. int prev_cmp = -1;
  2637. lowest_level = p->lowest_level;
  2638. WARN_ON(p->nodes[0] != NULL);
  2639. if (p->search_commit_root) {
  2640. BUG_ON(time_seq);
  2641. return btrfs_search_slot(NULL, root, key, p, 0, 0);
  2642. }
  2643. again:
  2644. b = get_old_root(root, time_seq);
  2645. level = btrfs_header_level(b);
  2646. p->locks[level] = BTRFS_READ_LOCK;
  2647. while (b) {
  2648. level = btrfs_header_level(b);
  2649. p->nodes[level] = b;
  2650. btrfs_clear_path_blocking(p, NULL, 0);
  2651. /*
  2652. * we have a lock on b and as long as we aren't changing
  2653. * the tree, there is no way to for the items in b to change.
  2654. * It is safe to drop the lock on our parent before we
  2655. * go through the expensive btree search on b.
  2656. */
  2657. btrfs_unlock_up_safe(p, level + 1);
  2658. /*
  2659. * Since we can unwind ebs we want to do a real search every
  2660. * time.
  2661. */
  2662. prev_cmp = -1;
  2663. ret = key_search(b, key, level, &prev_cmp, &slot);
  2664. if (level != 0) {
  2665. int dec = 0;
  2666. if (ret && slot > 0) {
  2667. dec = 1;
  2668. slot -= 1;
  2669. }
  2670. p->slots[level] = slot;
  2671. unlock_up(p, level, lowest_unlock, 0, NULL);
  2672. if (level == lowest_level) {
  2673. if (dec)
  2674. p->slots[level]++;
  2675. goto done;
  2676. }
  2677. err = read_block_for_search(root, p, &b, level,
  2678. slot, key);
  2679. if (err == -EAGAIN)
  2680. goto again;
  2681. if (err) {
  2682. ret = err;
  2683. goto done;
  2684. }
  2685. level = btrfs_header_level(b);
  2686. err = btrfs_tree_read_lock_atomic(b);
  2687. if (!err) {
  2688. btrfs_set_path_blocking(p);
  2689. btrfs_tree_read_lock(b);
  2690. btrfs_clear_path_blocking(p, b,
  2691. BTRFS_READ_LOCK);
  2692. }
  2693. b = tree_mod_log_rewind(fs_info, p, b, time_seq);
  2694. if (!b) {
  2695. ret = -ENOMEM;
  2696. goto done;
  2697. }
  2698. p->locks[level] = BTRFS_READ_LOCK;
  2699. p->nodes[level] = b;
  2700. } else {
  2701. p->slots[level] = slot;
  2702. unlock_up(p, level, lowest_unlock, 0, NULL);
  2703. goto done;
  2704. }
  2705. }
  2706. ret = 1;
  2707. done:
  2708. if (!p->leave_spinning)
  2709. btrfs_set_path_blocking(p);
  2710. if (ret < 0)
  2711. btrfs_release_path(p);
  2712. return ret;
  2713. }
  2714. /*
  2715. * helper to use instead of search slot if no exact match is needed but
  2716. * instead the next or previous item should be returned.
  2717. * When find_higher is true, the next higher item is returned, the next lower
  2718. * otherwise.
  2719. * When return_any and find_higher are both true, and no higher item is found,
  2720. * return the next lower instead.
  2721. * When return_any is true and find_higher is false, and no lower item is found,
  2722. * return the next higher instead.
  2723. * It returns 0 if any item is found, 1 if none is found (tree empty), and
  2724. * < 0 on error
  2725. */
  2726. int btrfs_search_slot_for_read(struct btrfs_root *root,
  2727. const struct btrfs_key *key,
  2728. struct btrfs_path *p, int find_higher,
  2729. int return_any)
  2730. {
  2731. int ret;
  2732. struct extent_buffer *leaf;
  2733. again:
  2734. ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
  2735. if (ret <= 0)
  2736. return ret;
  2737. /*
  2738. * a return value of 1 means the path is at the position where the
  2739. * item should be inserted. Normally this is the next bigger item,
  2740. * but in case the previous item is the last in a leaf, path points
  2741. * to the first free slot in the previous leaf, i.e. at an invalid
  2742. * item.
  2743. */
  2744. leaf = p->nodes[0];
  2745. if (find_higher) {
  2746. if (p->slots[0] >= btrfs_header_nritems(leaf)) {
  2747. ret = btrfs_next_leaf(root, p);
  2748. if (ret <= 0)
  2749. return ret;
  2750. if (!return_any)
  2751. return 1;
  2752. /*
  2753. * no higher item found, return the next
  2754. * lower instead
  2755. */
  2756. return_any = 0;
  2757. find_higher = 0;
  2758. btrfs_release_path(p);
  2759. goto again;
  2760. }
  2761. } else {
  2762. if (p->slots[0] == 0) {
  2763. ret = btrfs_prev_leaf(root, p);
  2764. if (ret < 0)
  2765. return ret;
  2766. if (!ret) {
  2767. leaf = p->nodes[0];
  2768. if (p->slots[0] == btrfs_header_nritems(leaf))
  2769. p->slots[0]--;
  2770. return 0;
  2771. }
  2772. if (!return_any)
  2773. return 1;
  2774. /*
  2775. * no lower item found, return the next
  2776. * higher instead
  2777. */
  2778. return_any = 0;
  2779. find_higher = 1;
  2780. btrfs_release_path(p);
  2781. goto again;
  2782. } else {
  2783. --p->slots[0];
  2784. }
  2785. }
  2786. return 0;
  2787. }
  2788. /*
  2789. * adjust the pointers going up the tree, starting at level
  2790. * making sure the right key of each node is points to 'key'.
  2791. * This is used after shifting pointers to the left, so it stops
  2792. * fixing up pointers when a given leaf/node is not in slot 0 of the
  2793. * higher levels
  2794. *
  2795. */
  2796. static void fixup_low_keys(struct btrfs_fs_info *fs_info,
  2797. struct btrfs_path *path,
  2798. struct btrfs_disk_key *key, int level)
  2799. {
  2800. int i;
  2801. struct extent_buffer *t;
  2802. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2803. int tslot = path->slots[i];
  2804. if (!path->nodes[i])
  2805. break;
  2806. t = path->nodes[i];
  2807. tree_mod_log_set_node_key(fs_info, t, tslot, 1);
  2808. btrfs_set_node_key(t, key, tslot);
  2809. btrfs_mark_buffer_dirty(path->nodes[i]);
  2810. if (tslot != 0)
  2811. break;
  2812. }
  2813. }
  2814. /*
  2815. * update item key.
  2816. *
  2817. * This function isn't completely safe. It's the caller's responsibility
  2818. * that the new key won't break the order
  2819. */
  2820. void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
  2821. struct btrfs_path *path,
  2822. const struct btrfs_key *new_key)
  2823. {
  2824. struct btrfs_disk_key disk_key;
  2825. struct extent_buffer *eb;
  2826. int slot;
  2827. eb = path->nodes[0];
  2828. slot = path->slots[0];
  2829. if (slot > 0) {
  2830. btrfs_item_key(eb, &disk_key, slot - 1);
  2831. BUG_ON(comp_keys(&disk_key, new_key) >= 0);
  2832. }
  2833. if (slot < btrfs_header_nritems(eb) - 1) {
  2834. btrfs_item_key(eb, &disk_key, slot + 1);
  2835. BUG_ON(comp_keys(&disk_key, new_key) <= 0);
  2836. }
  2837. btrfs_cpu_key_to_disk(&disk_key, new_key);
  2838. btrfs_set_item_key(eb, &disk_key, slot);
  2839. btrfs_mark_buffer_dirty(eb);
  2840. if (slot == 0)
  2841. fixup_low_keys(fs_info, path, &disk_key, 1);
  2842. }
  2843. /*
  2844. * try to push data from one node into the next node left in the
  2845. * tree.
  2846. *
  2847. * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
  2848. * error, and > 0 if there was no room in the left hand block.
  2849. */
  2850. static int push_node_left(struct btrfs_trans_handle *trans,
  2851. struct btrfs_fs_info *fs_info,
  2852. struct extent_buffer *dst,
  2853. struct extent_buffer *src, int empty)
  2854. {
  2855. int push_items = 0;
  2856. int src_nritems;
  2857. int dst_nritems;
  2858. int ret = 0;
  2859. src_nritems = btrfs_header_nritems(src);
  2860. dst_nritems = btrfs_header_nritems(dst);
  2861. push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
  2862. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2863. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2864. if (!empty && src_nritems <= 8)
  2865. return 1;
  2866. if (push_items <= 0)
  2867. return 1;
  2868. if (empty) {
  2869. push_items = min(src_nritems, push_items);
  2870. if (push_items < src_nritems) {
  2871. /* leave at least 8 pointers in the node if
  2872. * we aren't going to empty it
  2873. */
  2874. if (src_nritems - push_items < 8) {
  2875. if (push_items <= 8)
  2876. return 1;
  2877. push_items -= 8;
  2878. }
  2879. }
  2880. } else
  2881. push_items = min(src_nritems - 8, push_items);
  2882. ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
  2883. push_items);
  2884. if (ret) {
  2885. btrfs_abort_transaction(trans, ret);
  2886. return ret;
  2887. }
  2888. copy_extent_buffer(dst, src,
  2889. btrfs_node_key_ptr_offset(dst_nritems),
  2890. btrfs_node_key_ptr_offset(0),
  2891. push_items * sizeof(struct btrfs_key_ptr));
  2892. if (push_items < src_nritems) {
  2893. /*
  2894. * don't call tree_mod_log_eb_move here, key removal was already
  2895. * fully logged by tree_mod_log_eb_copy above.
  2896. */
  2897. memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
  2898. btrfs_node_key_ptr_offset(push_items),
  2899. (src_nritems - push_items) *
  2900. sizeof(struct btrfs_key_ptr));
  2901. }
  2902. btrfs_set_header_nritems(src, src_nritems - push_items);
  2903. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2904. btrfs_mark_buffer_dirty(src);
  2905. btrfs_mark_buffer_dirty(dst);
  2906. return ret;
  2907. }
  2908. /*
  2909. * try to push data from one node into the next node right in the
  2910. * tree.
  2911. *
  2912. * returns 0 if some ptrs were pushed, < 0 if there was some horrible
  2913. * error, and > 0 if there was no room in the right hand block.
  2914. *
  2915. * this will only push up to 1/2 the contents of the left node over
  2916. */
  2917. static int balance_node_right(struct btrfs_trans_handle *trans,
  2918. struct btrfs_fs_info *fs_info,
  2919. struct extent_buffer *dst,
  2920. struct extent_buffer *src)
  2921. {
  2922. int push_items = 0;
  2923. int max_push;
  2924. int src_nritems;
  2925. int dst_nritems;
  2926. int ret = 0;
  2927. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2928. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2929. src_nritems = btrfs_header_nritems(src);
  2930. dst_nritems = btrfs_header_nritems(dst);
  2931. push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
  2932. if (push_items <= 0)
  2933. return 1;
  2934. if (src_nritems < 4)
  2935. return 1;
  2936. max_push = src_nritems / 2 + 1;
  2937. /* don't try to empty the node */
  2938. if (max_push >= src_nritems)
  2939. return 1;
  2940. if (max_push < push_items)
  2941. push_items = max_push;
  2942. tree_mod_log_eb_move(fs_info, dst, push_items, 0, dst_nritems);
  2943. memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
  2944. btrfs_node_key_ptr_offset(0),
  2945. (dst_nritems) *
  2946. sizeof(struct btrfs_key_ptr));
  2947. ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
  2948. src_nritems - push_items, push_items);
  2949. if (ret) {
  2950. btrfs_abort_transaction(trans, ret);
  2951. return ret;
  2952. }
  2953. copy_extent_buffer(dst, src,
  2954. btrfs_node_key_ptr_offset(0),
  2955. btrfs_node_key_ptr_offset(src_nritems - push_items),
  2956. push_items * sizeof(struct btrfs_key_ptr));
  2957. btrfs_set_header_nritems(src, src_nritems - push_items);
  2958. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2959. btrfs_mark_buffer_dirty(src);
  2960. btrfs_mark_buffer_dirty(dst);
  2961. return ret;
  2962. }
  2963. /*
  2964. * helper function to insert a new root level in the tree.
  2965. * A new node is allocated, and a single item is inserted to
  2966. * point to the existing root
  2967. *
  2968. * returns zero on success or < 0 on failure.
  2969. */
  2970. static noinline int insert_new_root(struct btrfs_trans_handle *trans,
  2971. struct btrfs_root *root,
  2972. struct btrfs_path *path, int level)
  2973. {
  2974. struct btrfs_fs_info *fs_info = root->fs_info;
  2975. u64 lower_gen;
  2976. struct extent_buffer *lower;
  2977. struct extent_buffer *c;
  2978. struct extent_buffer *old;
  2979. struct btrfs_disk_key lower_key;
  2980. BUG_ON(path->nodes[level]);
  2981. BUG_ON(path->nodes[level-1] != root->node);
  2982. lower = path->nodes[level-1];
  2983. if (level == 1)
  2984. btrfs_item_key(lower, &lower_key, 0);
  2985. else
  2986. btrfs_node_key(lower, &lower_key, 0);
  2987. c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  2988. &lower_key, level, root->node->start, 0);
  2989. if (IS_ERR(c))
  2990. return PTR_ERR(c);
  2991. root_add_used(root, fs_info->nodesize);
  2992. memzero_extent_buffer(c, 0, sizeof(struct btrfs_header));
  2993. btrfs_set_header_nritems(c, 1);
  2994. btrfs_set_header_level(c, level);
  2995. btrfs_set_header_bytenr(c, c->start);
  2996. btrfs_set_header_generation(c, trans->transid);
  2997. btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
  2998. btrfs_set_header_owner(c, root->root_key.objectid);
  2999. write_extent_buffer_fsid(c, fs_info->fsid);
  3000. write_extent_buffer_chunk_tree_uuid(c, fs_info->chunk_tree_uuid);
  3001. btrfs_set_node_key(c, &lower_key, 0);
  3002. btrfs_set_node_blockptr(c, 0, lower->start);
  3003. lower_gen = btrfs_header_generation(lower);
  3004. WARN_ON(lower_gen != trans->transid);
  3005. btrfs_set_node_ptr_generation(c, 0, lower_gen);
  3006. btrfs_mark_buffer_dirty(c);
  3007. old = root->node;
  3008. tree_mod_log_set_root_pointer(root, c, 0);
  3009. rcu_assign_pointer(root->node, c);
  3010. /* the super has an extra ref to root->node */
  3011. free_extent_buffer(old);
  3012. add_root_to_dirty_list(root);
  3013. extent_buffer_get(c);
  3014. path->nodes[level] = c;
  3015. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  3016. path->slots[level] = 0;
  3017. return 0;
  3018. }
  3019. /*
  3020. * worker function to insert a single pointer in a node.
  3021. * the node should have enough room for the pointer already
  3022. *
  3023. * slot and level indicate where you want the key to go, and
  3024. * blocknr is the block the key points to.
  3025. */
  3026. static void insert_ptr(struct btrfs_trans_handle *trans,
  3027. struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  3028. struct btrfs_disk_key *key, u64 bytenr,
  3029. int slot, int level)
  3030. {
  3031. struct extent_buffer *lower;
  3032. int nritems;
  3033. int ret;
  3034. BUG_ON(!path->nodes[level]);
  3035. btrfs_assert_tree_locked(path->nodes[level]);
  3036. lower = path->nodes[level];
  3037. nritems = btrfs_header_nritems(lower);
  3038. BUG_ON(slot > nritems);
  3039. BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  3040. if (slot != nritems) {
  3041. if (level)
  3042. tree_mod_log_eb_move(fs_info, lower, slot + 1,
  3043. slot, nritems - slot);
  3044. memmove_extent_buffer(lower,
  3045. btrfs_node_key_ptr_offset(slot + 1),
  3046. btrfs_node_key_ptr_offset(slot),
  3047. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  3048. }
  3049. if (level) {
  3050. ret = tree_mod_log_insert_key(fs_info, lower, slot,
  3051. MOD_LOG_KEY_ADD, GFP_NOFS);
  3052. BUG_ON(ret < 0);
  3053. }
  3054. btrfs_set_node_key(lower, key, slot);
  3055. btrfs_set_node_blockptr(lower, slot, bytenr);
  3056. WARN_ON(trans->transid == 0);
  3057. btrfs_set_node_ptr_generation(lower, slot, trans->transid);
  3058. btrfs_set_header_nritems(lower, nritems + 1);
  3059. btrfs_mark_buffer_dirty(lower);
  3060. }
  3061. /*
  3062. * split the node at the specified level in path in two.
  3063. * The path is corrected to point to the appropriate node after the split
  3064. *
  3065. * Before splitting this tries to make some room in the node by pushing
  3066. * left and right, if either one works, it returns right away.
  3067. *
  3068. * returns 0 on success and < 0 on failure
  3069. */
  3070. static noinline int split_node(struct btrfs_trans_handle *trans,
  3071. struct btrfs_root *root,
  3072. struct btrfs_path *path, int level)
  3073. {
  3074. struct btrfs_fs_info *fs_info = root->fs_info;
  3075. struct extent_buffer *c;
  3076. struct extent_buffer *split;
  3077. struct btrfs_disk_key disk_key;
  3078. int mid;
  3079. int ret;
  3080. u32 c_nritems;
  3081. c = path->nodes[level];
  3082. WARN_ON(btrfs_header_generation(c) != trans->transid);
  3083. if (c == root->node) {
  3084. /*
  3085. * trying to split the root, lets make a new one
  3086. *
  3087. * tree mod log: We don't log_removal old root in
  3088. * insert_new_root, because that root buffer will be kept as a
  3089. * normal node. We are going to log removal of half of the
  3090. * elements below with tree_mod_log_eb_copy. We're holding a
  3091. * tree lock on the buffer, which is why we cannot race with
  3092. * other tree_mod_log users.
  3093. */
  3094. ret = insert_new_root(trans, root, path, level + 1);
  3095. if (ret)
  3096. return ret;
  3097. } else {
  3098. ret = push_nodes_for_insert(trans, root, path, level);
  3099. c = path->nodes[level];
  3100. if (!ret && btrfs_header_nritems(c) <
  3101. BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
  3102. return 0;
  3103. if (ret < 0)
  3104. return ret;
  3105. }
  3106. c_nritems = btrfs_header_nritems(c);
  3107. mid = (c_nritems + 1) / 2;
  3108. btrfs_node_key(c, &disk_key, mid);
  3109. split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3110. &disk_key, level, c->start, 0);
  3111. if (IS_ERR(split))
  3112. return PTR_ERR(split);
  3113. root_add_used(root, fs_info->nodesize);
  3114. memzero_extent_buffer(split, 0, sizeof(struct btrfs_header));
  3115. btrfs_set_header_level(split, btrfs_header_level(c));
  3116. btrfs_set_header_bytenr(split, split->start);
  3117. btrfs_set_header_generation(split, trans->transid);
  3118. btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
  3119. btrfs_set_header_owner(split, root->root_key.objectid);
  3120. write_extent_buffer_fsid(split, fs_info->fsid);
  3121. write_extent_buffer_chunk_tree_uuid(split, fs_info->chunk_tree_uuid);
  3122. ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
  3123. if (ret) {
  3124. btrfs_abort_transaction(trans, ret);
  3125. return ret;
  3126. }
  3127. copy_extent_buffer(split, c,
  3128. btrfs_node_key_ptr_offset(0),
  3129. btrfs_node_key_ptr_offset(mid),
  3130. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  3131. btrfs_set_header_nritems(split, c_nritems - mid);
  3132. btrfs_set_header_nritems(c, mid);
  3133. ret = 0;
  3134. btrfs_mark_buffer_dirty(c);
  3135. btrfs_mark_buffer_dirty(split);
  3136. insert_ptr(trans, fs_info, path, &disk_key, split->start,
  3137. path->slots[level + 1] + 1, level + 1);
  3138. if (path->slots[level] >= mid) {
  3139. path->slots[level] -= mid;
  3140. btrfs_tree_unlock(c);
  3141. free_extent_buffer(c);
  3142. path->nodes[level] = split;
  3143. path->slots[level + 1] += 1;
  3144. } else {
  3145. btrfs_tree_unlock(split);
  3146. free_extent_buffer(split);
  3147. }
  3148. return ret;
  3149. }
  3150. /*
  3151. * how many bytes are required to store the items in a leaf. start
  3152. * and nr indicate which items in the leaf to check. This totals up the
  3153. * space used both by the item structs and the item data
  3154. */
  3155. static int leaf_space_used(struct extent_buffer *l, int start, int nr)
  3156. {
  3157. struct btrfs_item *start_item;
  3158. struct btrfs_item *end_item;
  3159. struct btrfs_map_token token;
  3160. int data_len;
  3161. int nritems = btrfs_header_nritems(l);
  3162. int end = min(nritems, start + nr) - 1;
  3163. if (!nr)
  3164. return 0;
  3165. btrfs_init_map_token(&token);
  3166. start_item = btrfs_item_nr(start);
  3167. end_item = btrfs_item_nr(end);
  3168. data_len = btrfs_token_item_offset(l, start_item, &token) +
  3169. btrfs_token_item_size(l, start_item, &token);
  3170. data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
  3171. data_len += sizeof(struct btrfs_item) * nr;
  3172. WARN_ON(data_len < 0);
  3173. return data_len;
  3174. }
  3175. /*
  3176. * The space between the end of the leaf items and
  3177. * the start of the leaf data. IOW, how much room
  3178. * the leaf has left for both items and data
  3179. */
  3180. noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
  3181. struct extent_buffer *leaf)
  3182. {
  3183. int nritems = btrfs_header_nritems(leaf);
  3184. int ret;
  3185. ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
  3186. if (ret < 0) {
  3187. btrfs_crit(fs_info,
  3188. "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
  3189. ret,
  3190. (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
  3191. leaf_space_used(leaf, 0, nritems), nritems);
  3192. }
  3193. return ret;
  3194. }
  3195. /*
  3196. * min slot controls the lowest index we're willing to push to the
  3197. * right. We'll push up to and including min_slot, but no lower
  3198. */
  3199. static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
  3200. struct btrfs_path *path,
  3201. int data_size, int empty,
  3202. struct extent_buffer *right,
  3203. int free_space, u32 left_nritems,
  3204. u32 min_slot)
  3205. {
  3206. struct extent_buffer *left = path->nodes[0];
  3207. struct extent_buffer *upper = path->nodes[1];
  3208. struct btrfs_map_token token;
  3209. struct btrfs_disk_key disk_key;
  3210. int slot;
  3211. u32 i;
  3212. int push_space = 0;
  3213. int push_items = 0;
  3214. struct btrfs_item *item;
  3215. u32 nr;
  3216. u32 right_nritems;
  3217. u32 data_end;
  3218. u32 this_item_size;
  3219. btrfs_init_map_token(&token);
  3220. if (empty)
  3221. nr = 0;
  3222. else
  3223. nr = max_t(u32, 1, min_slot);
  3224. if (path->slots[0] >= left_nritems)
  3225. push_space += data_size;
  3226. slot = path->slots[1];
  3227. i = left_nritems - 1;
  3228. while (i >= nr) {
  3229. item = btrfs_item_nr(i);
  3230. if (!empty && push_items > 0) {
  3231. if (path->slots[0] > i)
  3232. break;
  3233. if (path->slots[0] == i) {
  3234. int space = btrfs_leaf_free_space(fs_info, left);
  3235. if (space + push_space * 2 > free_space)
  3236. break;
  3237. }
  3238. }
  3239. if (path->slots[0] == i)
  3240. push_space += data_size;
  3241. this_item_size = btrfs_item_size(left, item);
  3242. if (this_item_size + sizeof(*item) + push_space > free_space)
  3243. break;
  3244. push_items++;
  3245. push_space += this_item_size + sizeof(*item);
  3246. if (i == 0)
  3247. break;
  3248. i--;
  3249. }
  3250. if (push_items == 0)
  3251. goto out_unlock;
  3252. WARN_ON(!empty && push_items == left_nritems);
  3253. /* push left to right */
  3254. right_nritems = btrfs_header_nritems(right);
  3255. push_space = btrfs_item_end_nr(left, left_nritems - push_items);
  3256. push_space -= leaf_data_end(fs_info, left);
  3257. /* make room in the right data area */
  3258. data_end = leaf_data_end(fs_info, right);
  3259. memmove_extent_buffer(right,
  3260. BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
  3261. BTRFS_LEAF_DATA_OFFSET + data_end,
  3262. BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
  3263. /* copy from the left data area */
  3264. copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
  3265. BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
  3266. BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
  3267. push_space);
  3268. memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
  3269. btrfs_item_nr_offset(0),
  3270. right_nritems * sizeof(struct btrfs_item));
  3271. /* copy the items from left to right */
  3272. copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
  3273. btrfs_item_nr_offset(left_nritems - push_items),
  3274. push_items * sizeof(struct btrfs_item));
  3275. /* update the item pointers */
  3276. right_nritems += push_items;
  3277. btrfs_set_header_nritems(right, right_nritems);
  3278. push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
  3279. for (i = 0; i < right_nritems; i++) {
  3280. item = btrfs_item_nr(i);
  3281. push_space -= btrfs_token_item_size(right, item, &token);
  3282. btrfs_set_token_item_offset(right, item, push_space, &token);
  3283. }
  3284. left_nritems -= push_items;
  3285. btrfs_set_header_nritems(left, left_nritems);
  3286. if (left_nritems)
  3287. btrfs_mark_buffer_dirty(left);
  3288. else
  3289. clean_tree_block(fs_info, left);
  3290. btrfs_mark_buffer_dirty(right);
  3291. btrfs_item_key(right, &disk_key, 0);
  3292. btrfs_set_node_key(upper, &disk_key, slot + 1);
  3293. btrfs_mark_buffer_dirty(upper);
  3294. /* then fixup the leaf pointer in the path */
  3295. if (path->slots[0] >= left_nritems) {
  3296. path->slots[0] -= left_nritems;
  3297. if (btrfs_header_nritems(path->nodes[0]) == 0)
  3298. clean_tree_block(fs_info, path->nodes[0]);
  3299. btrfs_tree_unlock(path->nodes[0]);
  3300. free_extent_buffer(path->nodes[0]);
  3301. path->nodes[0] = right;
  3302. path->slots[1] += 1;
  3303. } else {
  3304. btrfs_tree_unlock(right);
  3305. free_extent_buffer(right);
  3306. }
  3307. return 0;
  3308. out_unlock:
  3309. btrfs_tree_unlock(right);
  3310. free_extent_buffer(right);
  3311. return 1;
  3312. }
  3313. /*
  3314. * push some data in the path leaf to the right, trying to free up at
  3315. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3316. *
  3317. * returns 1 if the push failed because the other node didn't have enough
  3318. * room, 0 if everything worked out and < 0 if there were major errors.
  3319. *
  3320. * this will push starting from min_slot to the end of the leaf. It won't
  3321. * push any slot lower than min_slot
  3322. */
  3323. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  3324. *root, struct btrfs_path *path,
  3325. int min_data_size, int data_size,
  3326. int empty, u32 min_slot)
  3327. {
  3328. struct btrfs_fs_info *fs_info = root->fs_info;
  3329. struct extent_buffer *left = path->nodes[0];
  3330. struct extent_buffer *right;
  3331. struct extent_buffer *upper;
  3332. int slot;
  3333. int free_space;
  3334. u32 left_nritems;
  3335. int ret;
  3336. if (!path->nodes[1])
  3337. return 1;
  3338. slot = path->slots[1];
  3339. upper = path->nodes[1];
  3340. if (slot >= btrfs_header_nritems(upper) - 1)
  3341. return 1;
  3342. btrfs_assert_tree_locked(path->nodes[1]);
  3343. right = read_node_slot(fs_info, upper, slot + 1);
  3344. /*
  3345. * slot + 1 is not valid or we fail to read the right node,
  3346. * no big deal, just return.
  3347. */
  3348. if (IS_ERR(right))
  3349. return 1;
  3350. btrfs_tree_lock(right);
  3351. btrfs_set_lock_blocking(right);
  3352. free_space = btrfs_leaf_free_space(fs_info, right);
  3353. if (free_space < data_size)
  3354. goto out_unlock;
  3355. /* cow and double check */
  3356. ret = btrfs_cow_block(trans, root, right, upper,
  3357. slot + 1, &right);
  3358. if (ret)
  3359. goto out_unlock;
  3360. free_space = btrfs_leaf_free_space(fs_info, right);
  3361. if (free_space < data_size)
  3362. goto out_unlock;
  3363. left_nritems = btrfs_header_nritems(left);
  3364. if (left_nritems == 0)
  3365. goto out_unlock;
  3366. if (path->slots[0] == left_nritems && !empty) {
  3367. /* Key greater than all keys in the leaf, right neighbor has
  3368. * enough room for it and we're not emptying our leaf to delete
  3369. * it, therefore use right neighbor to insert the new item and
  3370. * no need to touch/dirty our left leaft. */
  3371. btrfs_tree_unlock(left);
  3372. free_extent_buffer(left);
  3373. path->nodes[0] = right;
  3374. path->slots[0] = 0;
  3375. path->slots[1]++;
  3376. return 0;
  3377. }
  3378. return __push_leaf_right(fs_info, path, min_data_size, empty,
  3379. right, free_space, left_nritems, min_slot);
  3380. out_unlock:
  3381. btrfs_tree_unlock(right);
  3382. free_extent_buffer(right);
  3383. return 1;
  3384. }
  3385. /*
  3386. * push some data in the path leaf to the left, trying to free up at
  3387. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3388. *
  3389. * max_slot can put a limit on how far into the leaf we'll push items. The
  3390. * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
  3391. * items
  3392. */
  3393. static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
  3394. struct btrfs_path *path, int data_size,
  3395. int empty, struct extent_buffer *left,
  3396. int free_space, u32 right_nritems,
  3397. u32 max_slot)
  3398. {
  3399. struct btrfs_disk_key disk_key;
  3400. struct extent_buffer *right = path->nodes[0];
  3401. int i;
  3402. int push_space = 0;
  3403. int push_items = 0;
  3404. struct btrfs_item *item;
  3405. u32 old_left_nritems;
  3406. u32 nr;
  3407. int ret = 0;
  3408. u32 this_item_size;
  3409. u32 old_left_item_size;
  3410. struct btrfs_map_token token;
  3411. btrfs_init_map_token(&token);
  3412. if (empty)
  3413. nr = min(right_nritems, max_slot);
  3414. else
  3415. nr = min(right_nritems - 1, max_slot);
  3416. for (i = 0; i < nr; i++) {
  3417. item = btrfs_item_nr(i);
  3418. if (!empty && push_items > 0) {
  3419. if (path->slots[0] < i)
  3420. break;
  3421. if (path->slots[0] == i) {
  3422. int space = btrfs_leaf_free_space(fs_info, right);
  3423. if (space + push_space * 2 > free_space)
  3424. break;
  3425. }
  3426. }
  3427. if (path->slots[0] == i)
  3428. push_space += data_size;
  3429. this_item_size = btrfs_item_size(right, item);
  3430. if (this_item_size + sizeof(*item) + push_space > free_space)
  3431. break;
  3432. push_items++;
  3433. push_space += this_item_size + sizeof(*item);
  3434. }
  3435. if (push_items == 0) {
  3436. ret = 1;
  3437. goto out;
  3438. }
  3439. WARN_ON(!empty && push_items == btrfs_header_nritems(right));
  3440. /* push data from right to left */
  3441. copy_extent_buffer(left, right,
  3442. btrfs_item_nr_offset(btrfs_header_nritems(left)),
  3443. btrfs_item_nr_offset(0),
  3444. push_items * sizeof(struct btrfs_item));
  3445. push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
  3446. btrfs_item_offset_nr(right, push_items - 1);
  3447. copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
  3448. leaf_data_end(fs_info, left) - push_space,
  3449. BTRFS_LEAF_DATA_OFFSET +
  3450. btrfs_item_offset_nr(right, push_items - 1),
  3451. push_space);
  3452. old_left_nritems = btrfs_header_nritems(left);
  3453. BUG_ON(old_left_nritems <= 0);
  3454. old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
  3455. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  3456. u32 ioff;
  3457. item = btrfs_item_nr(i);
  3458. ioff = btrfs_token_item_offset(left, item, &token);
  3459. btrfs_set_token_item_offset(left, item,
  3460. ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
  3461. &token);
  3462. }
  3463. btrfs_set_header_nritems(left, old_left_nritems + push_items);
  3464. /* fixup right node */
  3465. if (push_items > right_nritems)
  3466. WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
  3467. right_nritems);
  3468. if (push_items < right_nritems) {
  3469. push_space = btrfs_item_offset_nr(right, push_items - 1) -
  3470. leaf_data_end(fs_info, right);
  3471. memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
  3472. BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
  3473. BTRFS_LEAF_DATA_OFFSET +
  3474. leaf_data_end(fs_info, right), push_space);
  3475. memmove_extent_buffer(right, btrfs_item_nr_offset(0),
  3476. btrfs_item_nr_offset(push_items),
  3477. (btrfs_header_nritems(right) - push_items) *
  3478. sizeof(struct btrfs_item));
  3479. }
  3480. right_nritems -= push_items;
  3481. btrfs_set_header_nritems(right, right_nritems);
  3482. push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
  3483. for (i = 0; i < right_nritems; i++) {
  3484. item = btrfs_item_nr(i);
  3485. push_space = push_space - btrfs_token_item_size(right,
  3486. item, &token);
  3487. btrfs_set_token_item_offset(right, item, push_space, &token);
  3488. }
  3489. btrfs_mark_buffer_dirty(left);
  3490. if (right_nritems)
  3491. btrfs_mark_buffer_dirty(right);
  3492. else
  3493. clean_tree_block(fs_info, right);
  3494. btrfs_item_key(right, &disk_key, 0);
  3495. fixup_low_keys(fs_info, path, &disk_key, 1);
  3496. /* then fixup the leaf pointer in the path */
  3497. if (path->slots[0] < push_items) {
  3498. path->slots[0] += old_left_nritems;
  3499. btrfs_tree_unlock(path->nodes[0]);
  3500. free_extent_buffer(path->nodes[0]);
  3501. path->nodes[0] = left;
  3502. path->slots[1] -= 1;
  3503. } else {
  3504. btrfs_tree_unlock(left);
  3505. free_extent_buffer(left);
  3506. path->slots[0] -= push_items;
  3507. }
  3508. BUG_ON(path->slots[0] < 0);
  3509. return ret;
  3510. out:
  3511. btrfs_tree_unlock(left);
  3512. free_extent_buffer(left);
  3513. return ret;
  3514. }
  3515. /*
  3516. * push some data in the path leaf to the left, trying to free up at
  3517. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3518. *
  3519. * max_slot can put a limit on how far into the leaf we'll push items. The
  3520. * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
  3521. * items
  3522. */
  3523. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  3524. *root, struct btrfs_path *path, int min_data_size,
  3525. int data_size, int empty, u32 max_slot)
  3526. {
  3527. struct btrfs_fs_info *fs_info = root->fs_info;
  3528. struct extent_buffer *right = path->nodes[0];
  3529. struct extent_buffer *left;
  3530. int slot;
  3531. int free_space;
  3532. u32 right_nritems;
  3533. int ret = 0;
  3534. slot = path->slots[1];
  3535. if (slot == 0)
  3536. return 1;
  3537. if (!path->nodes[1])
  3538. return 1;
  3539. right_nritems = btrfs_header_nritems(right);
  3540. if (right_nritems == 0)
  3541. return 1;
  3542. btrfs_assert_tree_locked(path->nodes[1]);
  3543. left = read_node_slot(fs_info, path->nodes[1], slot - 1);
  3544. /*
  3545. * slot - 1 is not valid or we fail to read the left node,
  3546. * no big deal, just return.
  3547. */
  3548. if (IS_ERR(left))
  3549. return 1;
  3550. btrfs_tree_lock(left);
  3551. btrfs_set_lock_blocking(left);
  3552. free_space = btrfs_leaf_free_space(fs_info, left);
  3553. if (free_space < data_size) {
  3554. ret = 1;
  3555. goto out;
  3556. }
  3557. /* cow and double check */
  3558. ret = btrfs_cow_block(trans, root, left,
  3559. path->nodes[1], slot - 1, &left);
  3560. if (ret) {
  3561. /* we hit -ENOSPC, but it isn't fatal here */
  3562. if (ret == -ENOSPC)
  3563. ret = 1;
  3564. goto out;
  3565. }
  3566. free_space = btrfs_leaf_free_space(fs_info, left);
  3567. if (free_space < data_size) {
  3568. ret = 1;
  3569. goto out;
  3570. }
  3571. return __push_leaf_left(fs_info, path, min_data_size,
  3572. empty, left, free_space, right_nritems,
  3573. max_slot);
  3574. out:
  3575. btrfs_tree_unlock(left);
  3576. free_extent_buffer(left);
  3577. return ret;
  3578. }
  3579. /*
  3580. * split the path's leaf in two, making sure there is at least data_size
  3581. * available for the resulting leaf level of the path.
  3582. */
  3583. static noinline void copy_for_split(struct btrfs_trans_handle *trans,
  3584. struct btrfs_fs_info *fs_info,
  3585. struct btrfs_path *path,
  3586. struct extent_buffer *l,
  3587. struct extent_buffer *right,
  3588. int slot, int mid, int nritems)
  3589. {
  3590. int data_copy_size;
  3591. int rt_data_off;
  3592. int i;
  3593. struct btrfs_disk_key disk_key;
  3594. struct btrfs_map_token token;
  3595. btrfs_init_map_token(&token);
  3596. nritems = nritems - mid;
  3597. btrfs_set_header_nritems(right, nritems);
  3598. data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
  3599. copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
  3600. btrfs_item_nr_offset(mid),
  3601. nritems * sizeof(struct btrfs_item));
  3602. copy_extent_buffer(right, l,
  3603. BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
  3604. data_copy_size, BTRFS_LEAF_DATA_OFFSET +
  3605. leaf_data_end(fs_info, l), data_copy_size);
  3606. rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
  3607. for (i = 0; i < nritems; i++) {
  3608. struct btrfs_item *item = btrfs_item_nr(i);
  3609. u32 ioff;
  3610. ioff = btrfs_token_item_offset(right, item, &token);
  3611. btrfs_set_token_item_offset(right, item,
  3612. ioff + rt_data_off, &token);
  3613. }
  3614. btrfs_set_header_nritems(l, mid);
  3615. btrfs_item_key(right, &disk_key, 0);
  3616. insert_ptr(trans, fs_info, path, &disk_key, right->start,
  3617. path->slots[1] + 1, 1);
  3618. btrfs_mark_buffer_dirty(right);
  3619. btrfs_mark_buffer_dirty(l);
  3620. BUG_ON(path->slots[0] != slot);
  3621. if (mid <= slot) {
  3622. btrfs_tree_unlock(path->nodes[0]);
  3623. free_extent_buffer(path->nodes[0]);
  3624. path->nodes[0] = right;
  3625. path->slots[0] -= mid;
  3626. path->slots[1] += 1;
  3627. } else {
  3628. btrfs_tree_unlock(right);
  3629. free_extent_buffer(right);
  3630. }
  3631. BUG_ON(path->slots[0] < 0);
  3632. }
  3633. /*
  3634. * double splits happen when we need to insert a big item in the middle
  3635. * of a leaf. A double split can leave us with 3 mostly empty leaves:
  3636. * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
  3637. * A B C
  3638. *
  3639. * We avoid this by trying to push the items on either side of our target
  3640. * into the adjacent leaves. If all goes well we can avoid the double split
  3641. * completely.
  3642. */
  3643. static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
  3644. struct btrfs_root *root,
  3645. struct btrfs_path *path,
  3646. int data_size)
  3647. {
  3648. struct btrfs_fs_info *fs_info = root->fs_info;
  3649. int ret;
  3650. int progress = 0;
  3651. int slot;
  3652. u32 nritems;
  3653. int space_needed = data_size;
  3654. slot = path->slots[0];
  3655. if (slot < btrfs_header_nritems(path->nodes[0]))
  3656. space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
  3657. /*
  3658. * try to push all the items after our slot into the
  3659. * right leaf
  3660. */
  3661. ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
  3662. if (ret < 0)
  3663. return ret;
  3664. if (ret == 0)
  3665. progress++;
  3666. nritems = btrfs_header_nritems(path->nodes[0]);
  3667. /*
  3668. * our goal is to get our slot at the start or end of a leaf. If
  3669. * we've done so we're done
  3670. */
  3671. if (path->slots[0] == 0 || path->slots[0] == nritems)
  3672. return 0;
  3673. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
  3674. return 0;
  3675. /* try to push all the items before our slot into the next leaf */
  3676. slot = path->slots[0];
  3677. space_needed = data_size;
  3678. if (slot > 0)
  3679. space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
  3680. ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
  3681. if (ret < 0)
  3682. return ret;
  3683. if (ret == 0)
  3684. progress++;
  3685. if (progress)
  3686. return 0;
  3687. return 1;
  3688. }
  3689. /*
  3690. * split the path's leaf in two, making sure there is at least data_size
  3691. * available for the resulting leaf level of the path.
  3692. *
  3693. * returns 0 if all went well and < 0 on failure.
  3694. */
  3695. static noinline int split_leaf(struct btrfs_trans_handle *trans,
  3696. struct btrfs_root *root,
  3697. const struct btrfs_key *ins_key,
  3698. struct btrfs_path *path, int data_size,
  3699. int extend)
  3700. {
  3701. struct btrfs_disk_key disk_key;
  3702. struct extent_buffer *l;
  3703. u32 nritems;
  3704. int mid;
  3705. int slot;
  3706. struct extent_buffer *right;
  3707. struct btrfs_fs_info *fs_info = root->fs_info;
  3708. int ret = 0;
  3709. int wret;
  3710. int split;
  3711. int num_doubles = 0;
  3712. int tried_avoid_double = 0;
  3713. l = path->nodes[0];
  3714. slot = path->slots[0];
  3715. if (extend && data_size + btrfs_item_size_nr(l, slot) +
  3716. sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
  3717. return -EOVERFLOW;
  3718. /* first try to make some room by pushing left and right */
  3719. if (data_size && path->nodes[1]) {
  3720. int space_needed = data_size;
  3721. if (slot < btrfs_header_nritems(l))
  3722. space_needed -= btrfs_leaf_free_space(fs_info, l);
  3723. wret = push_leaf_right(trans, root, path, space_needed,
  3724. space_needed, 0, 0);
  3725. if (wret < 0)
  3726. return wret;
  3727. if (wret) {
  3728. space_needed = data_size;
  3729. if (slot > 0)
  3730. space_needed -= btrfs_leaf_free_space(fs_info,
  3731. l);
  3732. wret = push_leaf_left(trans, root, path, space_needed,
  3733. space_needed, 0, (u32)-1);
  3734. if (wret < 0)
  3735. return wret;
  3736. }
  3737. l = path->nodes[0];
  3738. /* did the pushes work? */
  3739. if (btrfs_leaf_free_space(fs_info, l) >= data_size)
  3740. return 0;
  3741. }
  3742. if (!path->nodes[1]) {
  3743. ret = insert_new_root(trans, root, path, 1);
  3744. if (ret)
  3745. return ret;
  3746. }
  3747. again:
  3748. split = 1;
  3749. l = path->nodes[0];
  3750. slot = path->slots[0];
  3751. nritems = btrfs_header_nritems(l);
  3752. mid = (nritems + 1) / 2;
  3753. if (mid <= slot) {
  3754. if (nritems == 1 ||
  3755. leaf_space_used(l, mid, nritems - mid) + data_size >
  3756. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3757. if (slot >= nritems) {
  3758. split = 0;
  3759. } else {
  3760. mid = slot;
  3761. if (mid != nritems &&
  3762. leaf_space_used(l, mid, nritems - mid) +
  3763. data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3764. if (data_size && !tried_avoid_double)
  3765. goto push_for_double;
  3766. split = 2;
  3767. }
  3768. }
  3769. }
  3770. } else {
  3771. if (leaf_space_used(l, 0, mid) + data_size >
  3772. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3773. if (!extend && data_size && slot == 0) {
  3774. split = 0;
  3775. } else if ((extend || !data_size) && slot == 0) {
  3776. mid = 1;
  3777. } else {
  3778. mid = slot;
  3779. if (mid != nritems &&
  3780. leaf_space_used(l, mid, nritems - mid) +
  3781. data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3782. if (data_size && !tried_avoid_double)
  3783. goto push_for_double;
  3784. split = 2;
  3785. }
  3786. }
  3787. }
  3788. }
  3789. if (split == 0)
  3790. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  3791. else
  3792. btrfs_item_key(l, &disk_key, mid);
  3793. right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3794. &disk_key, 0, l->start, 0);
  3795. if (IS_ERR(right))
  3796. return PTR_ERR(right);
  3797. root_add_used(root, fs_info->nodesize);
  3798. memzero_extent_buffer(right, 0, sizeof(struct btrfs_header));
  3799. btrfs_set_header_bytenr(right, right->start);
  3800. btrfs_set_header_generation(right, trans->transid);
  3801. btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
  3802. btrfs_set_header_owner(right, root->root_key.objectid);
  3803. btrfs_set_header_level(right, 0);
  3804. write_extent_buffer_fsid(right, fs_info->fsid);
  3805. write_extent_buffer_chunk_tree_uuid(right, fs_info->chunk_tree_uuid);
  3806. if (split == 0) {
  3807. if (mid <= slot) {
  3808. btrfs_set_header_nritems(right, 0);
  3809. insert_ptr(trans, fs_info, path, &disk_key,
  3810. right->start, path->slots[1] + 1, 1);
  3811. btrfs_tree_unlock(path->nodes[0]);
  3812. free_extent_buffer(path->nodes[0]);
  3813. path->nodes[0] = right;
  3814. path->slots[0] = 0;
  3815. path->slots[1] += 1;
  3816. } else {
  3817. btrfs_set_header_nritems(right, 0);
  3818. insert_ptr(trans, fs_info, path, &disk_key,
  3819. right->start, path->slots[1], 1);
  3820. btrfs_tree_unlock(path->nodes[0]);
  3821. free_extent_buffer(path->nodes[0]);
  3822. path->nodes[0] = right;
  3823. path->slots[0] = 0;
  3824. if (path->slots[1] == 0)
  3825. fixup_low_keys(fs_info, path, &disk_key, 1);
  3826. }
  3827. /*
  3828. * We create a new leaf 'right' for the required ins_len and
  3829. * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
  3830. * the content of ins_len to 'right'.
  3831. */
  3832. return ret;
  3833. }
  3834. copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
  3835. if (split == 2) {
  3836. BUG_ON(num_doubles != 0);
  3837. num_doubles++;
  3838. goto again;
  3839. }
  3840. return 0;
  3841. push_for_double:
  3842. push_for_double_split(trans, root, path, data_size);
  3843. tried_avoid_double = 1;
  3844. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
  3845. return 0;
  3846. goto again;
  3847. }
  3848. static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
  3849. struct btrfs_root *root,
  3850. struct btrfs_path *path, int ins_len)
  3851. {
  3852. struct btrfs_fs_info *fs_info = root->fs_info;
  3853. struct btrfs_key key;
  3854. struct extent_buffer *leaf;
  3855. struct btrfs_file_extent_item *fi;
  3856. u64 extent_len = 0;
  3857. u32 item_size;
  3858. int ret;
  3859. leaf = path->nodes[0];
  3860. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3861. BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
  3862. key.type != BTRFS_EXTENT_CSUM_KEY);
  3863. if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
  3864. return 0;
  3865. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3866. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3867. fi = btrfs_item_ptr(leaf, path->slots[0],
  3868. struct btrfs_file_extent_item);
  3869. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  3870. }
  3871. btrfs_release_path(path);
  3872. path->keep_locks = 1;
  3873. path->search_for_split = 1;
  3874. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3875. path->search_for_split = 0;
  3876. if (ret > 0)
  3877. ret = -EAGAIN;
  3878. if (ret < 0)
  3879. goto err;
  3880. ret = -EAGAIN;
  3881. leaf = path->nodes[0];
  3882. /* if our item isn't there, return now */
  3883. if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
  3884. goto err;
  3885. /* the leaf has changed, it now has room. return now */
  3886. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
  3887. goto err;
  3888. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3889. fi = btrfs_item_ptr(leaf, path->slots[0],
  3890. struct btrfs_file_extent_item);
  3891. if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
  3892. goto err;
  3893. }
  3894. btrfs_set_path_blocking(path);
  3895. ret = split_leaf(trans, root, &key, path, ins_len, 1);
  3896. if (ret)
  3897. goto err;
  3898. path->keep_locks = 0;
  3899. btrfs_unlock_up_safe(path, 1);
  3900. return 0;
  3901. err:
  3902. path->keep_locks = 0;
  3903. return ret;
  3904. }
  3905. static noinline int split_item(struct btrfs_fs_info *fs_info,
  3906. struct btrfs_path *path,
  3907. const struct btrfs_key *new_key,
  3908. unsigned long split_offset)
  3909. {
  3910. struct extent_buffer *leaf;
  3911. struct btrfs_item *item;
  3912. struct btrfs_item *new_item;
  3913. int slot;
  3914. char *buf;
  3915. u32 nritems;
  3916. u32 item_size;
  3917. u32 orig_offset;
  3918. struct btrfs_disk_key disk_key;
  3919. leaf = path->nodes[0];
  3920. BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
  3921. btrfs_set_path_blocking(path);
  3922. item = btrfs_item_nr(path->slots[0]);
  3923. orig_offset = btrfs_item_offset(leaf, item);
  3924. item_size = btrfs_item_size(leaf, item);
  3925. buf = kmalloc(item_size, GFP_NOFS);
  3926. if (!buf)
  3927. return -ENOMEM;
  3928. read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
  3929. path->slots[0]), item_size);
  3930. slot = path->slots[0] + 1;
  3931. nritems = btrfs_header_nritems(leaf);
  3932. if (slot != nritems) {
  3933. /* shift the items */
  3934. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
  3935. btrfs_item_nr_offset(slot),
  3936. (nritems - slot) * sizeof(struct btrfs_item));
  3937. }
  3938. btrfs_cpu_key_to_disk(&disk_key, new_key);
  3939. btrfs_set_item_key(leaf, &disk_key, slot);
  3940. new_item = btrfs_item_nr(slot);
  3941. btrfs_set_item_offset(leaf, new_item, orig_offset);
  3942. btrfs_set_item_size(leaf, new_item, item_size - split_offset);
  3943. btrfs_set_item_offset(leaf, item,
  3944. orig_offset + item_size - split_offset);
  3945. btrfs_set_item_size(leaf, item, split_offset);
  3946. btrfs_set_header_nritems(leaf, nritems + 1);
  3947. /* write the data for the start of the original item */
  3948. write_extent_buffer(leaf, buf,
  3949. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3950. split_offset);
  3951. /* write the data for the new item */
  3952. write_extent_buffer(leaf, buf + split_offset,
  3953. btrfs_item_ptr_offset(leaf, slot),
  3954. item_size - split_offset);
  3955. btrfs_mark_buffer_dirty(leaf);
  3956. BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
  3957. kfree(buf);
  3958. return 0;
  3959. }
  3960. /*
  3961. * This function splits a single item into two items,
  3962. * giving 'new_key' to the new item and splitting the
  3963. * old one at split_offset (from the start of the item).
  3964. *
  3965. * The path may be released by this operation. After
  3966. * the split, the path is pointing to the old item. The
  3967. * new item is going to be in the same node as the old one.
  3968. *
  3969. * Note, the item being split must be smaller enough to live alone on
  3970. * a tree block with room for one extra struct btrfs_item
  3971. *
  3972. * This allows us to split the item in place, keeping a lock on the
  3973. * leaf the entire time.
  3974. */
  3975. int btrfs_split_item(struct btrfs_trans_handle *trans,
  3976. struct btrfs_root *root,
  3977. struct btrfs_path *path,
  3978. const struct btrfs_key *new_key,
  3979. unsigned long split_offset)
  3980. {
  3981. int ret;
  3982. ret = setup_leaf_for_split(trans, root, path,
  3983. sizeof(struct btrfs_item));
  3984. if (ret)
  3985. return ret;
  3986. ret = split_item(root->fs_info, path, new_key, split_offset);
  3987. return ret;
  3988. }
  3989. /*
  3990. * This function duplicate a item, giving 'new_key' to the new item.
  3991. * It guarantees both items live in the same tree leaf and the new item
  3992. * is contiguous with the original item.
  3993. *
  3994. * This allows us to split file extent in place, keeping a lock on the
  3995. * leaf the entire time.
  3996. */
  3997. int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
  3998. struct btrfs_root *root,
  3999. struct btrfs_path *path,
  4000. const struct btrfs_key *new_key)
  4001. {
  4002. struct extent_buffer *leaf;
  4003. int ret;
  4004. u32 item_size;
  4005. leaf = path->nodes[0];
  4006. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  4007. ret = setup_leaf_for_split(trans, root, path,
  4008. item_size + sizeof(struct btrfs_item));
  4009. if (ret)
  4010. return ret;
  4011. path->slots[0]++;
  4012. setup_items_for_insert(root, path, new_key, &item_size,
  4013. item_size, item_size +
  4014. sizeof(struct btrfs_item), 1);
  4015. leaf = path->nodes[0];
  4016. memcpy_extent_buffer(leaf,
  4017. btrfs_item_ptr_offset(leaf, path->slots[0]),
  4018. btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
  4019. item_size);
  4020. return 0;
  4021. }
  4022. /*
  4023. * make the item pointed to by the path smaller. new_size indicates
  4024. * how small to make it, and from_end tells us if we just chop bytes
  4025. * off the end of the item or if we shift the item to chop bytes off
  4026. * the front.
  4027. */
  4028. void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
  4029. struct btrfs_path *path, u32 new_size, int from_end)
  4030. {
  4031. int slot;
  4032. struct extent_buffer *leaf;
  4033. struct btrfs_item *item;
  4034. u32 nritems;
  4035. unsigned int data_end;
  4036. unsigned int old_data_start;
  4037. unsigned int old_size;
  4038. unsigned int size_diff;
  4039. int i;
  4040. struct btrfs_map_token token;
  4041. btrfs_init_map_token(&token);
  4042. leaf = path->nodes[0];
  4043. slot = path->slots[0];
  4044. old_size = btrfs_item_size_nr(leaf, slot);
  4045. if (old_size == new_size)
  4046. return;
  4047. nritems = btrfs_header_nritems(leaf);
  4048. data_end = leaf_data_end(fs_info, leaf);
  4049. old_data_start = btrfs_item_offset_nr(leaf, slot);
  4050. size_diff = old_size - new_size;
  4051. BUG_ON(slot < 0);
  4052. BUG_ON(slot >= nritems);
  4053. /*
  4054. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4055. */
  4056. /* first correct the data pointers */
  4057. for (i = slot; i < nritems; i++) {
  4058. u32 ioff;
  4059. item = btrfs_item_nr(i);
  4060. ioff = btrfs_token_item_offset(leaf, item, &token);
  4061. btrfs_set_token_item_offset(leaf, item,
  4062. ioff + size_diff, &token);
  4063. }
  4064. /* shift the data */
  4065. if (from_end) {
  4066. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4067. data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
  4068. data_end, old_data_start + new_size - data_end);
  4069. } else {
  4070. struct btrfs_disk_key disk_key;
  4071. u64 offset;
  4072. btrfs_item_key(leaf, &disk_key, slot);
  4073. if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
  4074. unsigned long ptr;
  4075. struct btrfs_file_extent_item *fi;
  4076. fi = btrfs_item_ptr(leaf, slot,
  4077. struct btrfs_file_extent_item);
  4078. fi = (struct btrfs_file_extent_item *)(
  4079. (unsigned long)fi - size_diff);
  4080. if (btrfs_file_extent_type(leaf, fi) ==
  4081. BTRFS_FILE_EXTENT_INLINE) {
  4082. ptr = btrfs_item_ptr_offset(leaf, slot);
  4083. memmove_extent_buffer(leaf, ptr,
  4084. (unsigned long)fi,
  4085. BTRFS_FILE_EXTENT_INLINE_DATA_START);
  4086. }
  4087. }
  4088. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4089. data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
  4090. data_end, old_data_start - data_end);
  4091. offset = btrfs_disk_key_offset(&disk_key);
  4092. btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
  4093. btrfs_set_item_key(leaf, &disk_key, slot);
  4094. if (slot == 0)
  4095. fixup_low_keys(fs_info, path, &disk_key, 1);
  4096. }
  4097. item = btrfs_item_nr(slot);
  4098. btrfs_set_item_size(leaf, item, new_size);
  4099. btrfs_mark_buffer_dirty(leaf);
  4100. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4101. btrfs_print_leaf(leaf);
  4102. BUG();
  4103. }
  4104. }
  4105. /*
  4106. * make the item pointed to by the path bigger, data_size is the added size.
  4107. */
  4108. void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  4109. u32 data_size)
  4110. {
  4111. int slot;
  4112. struct extent_buffer *leaf;
  4113. struct btrfs_item *item;
  4114. u32 nritems;
  4115. unsigned int data_end;
  4116. unsigned int old_data;
  4117. unsigned int old_size;
  4118. int i;
  4119. struct btrfs_map_token token;
  4120. btrfs_init_map_token(&token);
  4121. leaf = path->nodes[0];
  4122. nritems = btrfs_header_nritems(leaf);
  4123. data_end = leaf_data_end(fs_info, leaf);
  4124. if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
  4125. btrfs_print_leaf(leaf);
  4126. BUG();
  4127. }
  4128. slot = path->slots[0];
  4129. old_data = btrfs_item_end_nr(leaf, slot);
  4130. BUG_ON(slot < 0);
  4131. if (slot >= nritems) {
  4132. btrfs_print_leaf(leaf);
  4133. btrfs_crit(fs_info, "slot %d too large, nritems %d",
  4134. slot, nritems);
  4135. BUG_ON(1);
  4136. }
  4137. /*
  4138. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4139. */
  4140. /* first correct the data pointers */
  4141. for (i = slot; i < nritems; i++) {
  4142. u32 ioff;
  4143. item = btrfs_item_nr(i);
  4144. ioff = btrfs_token_item_offset(leaf, item, &token);
  4145. btrfs_set_token_item_offset(leaf, item,
  4146. ioff - data_size, &token);
  4147. }
  4148. /* shift the data */
  4149. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4150. data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
  4151. data_end, old_data - data_end);
  4152. data_end = old_data;
  4153. old_size = btrfs_item_size_nr(leaf, slot);
  4154. item = btrfs_item_nr(slot);
  4155. btrfs_set_item_size(leaf, item, old_size + data_size);
  4156. btrfs_mark_buffer_dirty(leaf);
  4157. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4158. btrfs_print_leaf(leaf);
  4159. BUG();
  4160. }
  4161. }
  4162. /*
  4163. * this is a helper for btrfs_insert_empty_items, the main goal here is
  4164. * to save stack depth by doing the bulk of the work in a function
  4165. * that doesn't call btrfs_search_slot
  4166. */
  4167. void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
  4168. const struct btrfs_key *cpu_key, u32 *data_size,
  4169. u32 total_data, u32 total_size, int nr)
  4170. {
  4171. struct btrfs_fs_info *fs_info = root->fs_info;
  4172. struct btrfs_item *item;
  4173. int i;
  4174. u32 nritems;
  4175. unsigned int data_end;
  4176. struct btrfs_disk_key disk_key;
  4177. struct extent_buffer *leaf;
  4178. int slot;
  4179. struct btrfs_map_token token;
  4180. if (path->slots[0] == 0) {
  4181. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  4182. fixup_low_keys(fs_info, path, &disk_key, 1);
  4183. }
  4184. btrfs_unlock_up_safe(path, 1);
  4185. btrfs_init_map_token(&token);
  4186. leaf = path->nodes[0];
  4187. slot = path->slots[0];
  4188. nritems = btrfs_header_nritems(leaf);
  4189. data_end = leaf_data_end(fs_info, leaf);
  4190. if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
  4191. btrfs_print_leaf(leaf);
  4192. btrfs_crit(fs_info, "not enough freespace need %u have %d",
  4193. total_size, btrfs_leaf_free_space(fs_info, leaf));
  4194. BUG();
  4195. }
  4196. if (slot != nritems) {
  4197. unsigned int old_data = btrfs_item_end_nr(leaf, slot);
  4198. if (old_data < data_end) {
  4199. btrfs_print_leaf(leaf);
  4200. btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
  4201. slot, old_data, data_end);
  4202. BUG_ON(1);
  4203. }
  4204. /*
  4205. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4206. */
  4207. /* first correct the data pointers */
  4208. for (i = slot; i < nritems; i++) {
  4209. u32 ioff;
  4210. item = btrfs_item_nr(i);
  4211. ioff = btrfs_token_item_offset(leaf, item, &token);
  4212. btrfs_set_token_item_offset(leaf, item,
  4213. ioff - total_data, &token);
  4214. }
  4215. /* shift the items */
  4216. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
  4217. btrfs_item_nr_offset(slot),
  4218. (nritems - slot) * sizeof(struct btrfs_item));
  4219. /* shift the data */
  4220. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4221. data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
  4222. data_end, old_data - data_end);
  4223. data_end = old_data;
  4224. }
  4225. /* setup the item for the new data */
  4226. for (i = 0; i < nr; i++) {
  4227. btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
  4228. btrfs_set_item_key(leaf, &disk_key, slot + i);
  4229. item = btrfs_item_nr(slot + i);
  4230. btrfs_set_token_item_offset(leaf, item,
  4231. data_end - data_size[i], &token);
  4232. data_end -= data_size[i];
  4233. btrfs_set_token_item_size(leaf, item, data_size[i], &token);
  4234. }
  4235. btrfs_set_header_nritems(leaf, nritems + nr);
  4236. btrfs_mark_buffer_dirty(leaf);
  4237. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4238. btrfs_print_leaf(leaf);
  4239. BUG();
  4240. }
  4241. }
  4242. /*
  4243. * Given a key and some data, insert items into the tree.
  4244. * This does all the path init required, making room in the tree if needed.
  4245. */
  4246. int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
  4247. struct btrfs_root *root,
  4248. struct btrfs_path *path,
  4249. const struct btrfs_key *cpu_key, u32 *data_size,
  4250. int nr)
  4251. {
  4252. int ret = 0;
  4253. int slot;
  4254. int i;
  4255. u32 total_size = 0;
  4256. u32 total_data = 0;
  4257. for (i = 0; i < nr; i++)
  4258. total_data += data_size[i];
  4259. total_size = total_data + (nr * sizeof(struct btrfs_item));
  4260. ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
  4261. if (ret == 0)
  4262. return -EEXIST;
  4263. if (ret < 0)
  4264. return ret;
  4265. slot = path->slots[0];
  4266. BUG_ON(slot < 0);
  4267. setup_items_for_insert(root, path, cpu_key, data_size,
  4268. total_data, total_size, nr);
  4269. return 0;
  4270. }
  4271. /*
  4272. * Given a key and some data, insert an item into the tree.
  4273. * This does all the path init required, making room in the tree if needed.
  4274. */
  4275. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4276. const struct btrfs_key *cpu_key, void *data,
  4277. u32 data_size)
  4278. {
  4279. int ret = 0;
  4280. struct btrfs_path *path;
  4281. struct extent_buffer *leaf;
  4282. unsigned long ptr;
  4283. path = btrfs_alloc_path();
  4284. if (!path)
  4285. return -ENOMEM;
  4286. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  4287. if (!ret) {
  4288. leaf = path->nodes[0];
  4289. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  4290. write_extent_buffer(leaf, data, ptr, data_size);
  4291. btrfs_mark_buffer_dirty(leaf);
  4292. }
  4293. btrfs_free_path(path);
  4294. return ret;
  4295. }
  4296. /*
  4297. * delete the pointer from a given node.
  4298. *
  4299. * the tree should have been previously balanced so the deletion does not
  4300. * empty a node.
  4301. */
  4302. static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
  4303. int level, int slot)
  4304. {
  4305. struct btrfs_fs_info *fs_info = root->fs_info;
  4306. struct extent_buffer *parent = path->nodes[level];
  4307. u32 nritems;
  4308. int ret;
  4309. nritems = btrfs_header_nritems(parent);
  4310. if (slot != nritems - 1) {
  4311. if (level)
  4312. tree_mod_log_eb_move(fs_info, parent, slot,
  4313. slot + 1, nritems - slot - 1);
  4314. memmove_extent_buffer(parent,
  4315. btrfs_node_key_ptr_offset(slot),
  4316. btrfs_node_key_ptr_offset(slot + 1),
  4317. sizeof(struct btrfs_key_ptr) *
  4318. (nritems - slot - 1));
  4319. } else if (level) {
  4320. ret = tree_mod_log_insert_key(fs_info, parent, slot,
  4321. MOD_LOG_KEY_REMOVE, GFP_NOFS);
  4322. BUG_ON(ret < 0);
  4323. }
  4324. nritems--;
  4325. btrfs_set_header_nritems(parent, nritems);
  4326. if (nritems == 0 && parent == root->node) {
  4327. BUG_ON(btrfs_header_level(root->node) != 1);
  4328. /* just turn the root into a leaf and break */
  4329. btrfs_set_header_level(root->node, 0);
  4330. } else if (slot == 0) {
  4331. struct btrfs_disk_key disk_key;
  4332. btrfs_node_key(parent, &disk_key, 0);
  4333. fixup_low_keys(fs_info, path, &disk_key, level + 1);
  4334. }
  4335. btrfs_mark_buffer_dirty(parent);
  4336. }
  4337. /*
  4338. * a helper function to delete the leaf pointed to by path->slots[1] and
  4339. * path->nodes[1].
  4340. *
  4341. * This deletes the pointer in path->nodes[1] and frees the leaf
  4342. * block extent. zero is returned if it all worked out, < 0 otherwise.
  4343. *
  4344. * The path must have already been setup for deleting the leaf, including
  4345. * all the proper balancing. path->nodes[1] must be locked.
  4346. */
  4347. static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
  4348. struct btrfs_root *root,
  4349. struct btrfs_path *path,
  4350. struct extent_buffer *leaf)
  4351. {
  4352. WARN_ON(btrfs_header_generation(leaf) != trans->transid);
  4353. del_ptr(root, path, 1, path->slots[1]);
  4354. /*
  4355. * btrfs_free_extent is expensive, we want to make sure we
  4356. * aren't holding any locks when we call it
  4357. */
  4358. btrfs_unlock_up_safe(path, 0);
  4359. root_sub_used(root, leaf->len);
  4360. extent_buffer_get(leaf);
  4361. btrfs_free_tree_block(trans, root, leaf, 0, 1);
  4362. free_extent_buffer_stale(leaf);
  4363. }
  4364. /*
  4365. * delete the item at the leaf level in path. If that empties
  4366. * the leaf, remove it from the tree
  4367. */
  4368. int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4369. struct btrfs_path *path, int slot, int nr)
  4370. {
  4371. struct btrfs_fs_info *fs_info = root->fs_info;
  4372. struct extent_buffer *leaf;
  4373. struct btrfs_item *item;
  4374. u32 last_off;
  4375. u32 dsize = 0;
  4376. int ret = 0;
  4377. int wret;
  4378. int i;
  4379. u32 nritems;
  4380. struct btrfs_map_token token;
  4381. btrfs_init_map_token(&token);
  4382. leaf = path->nodes[0];
  4383. last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
  4384. for (i = 0; i < nr; i++)
  4385. dsize += btrfs_item_size_nr(leaf, slot + i);
  4386. nritems = btrfs_header_nritems(leaf);
  4387. if (slot + nr != nritems) {
  4388. int data_end = leaf_data_end(fs_info, leaf);
  4389. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4390. data_end + dsize,
  4391. BTRFS_LEAF_DATA_OFFSET + data_end,
  4392. last_off - data_end);
  4393. for (i = slot + nr; i < nritems; i++) {
  4394. u32 ioff;
  4395. item = btrfs_item_nr(i);
  4396. ioff = btrfs_token_item_offset(leaf, item, &token);
  4397. btrfs_set_token_item_offset(leaf, item,
  4398. ioff + dsize, &token);
  4399. }
  4400. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
  4401. btrfs_item_nr_offset(slot + nr),
  4402. sizeof(struct btrfs_item) *
  4403. (nritems - slot - nr));
  4404. }
  4405. btrfs_set_header_nritems(leaf, nritems - nr);
  4406. nritems -= nr;
  4407. /* delete the leaf if we've emptied it */
  4408. if (nritems == 0) {
  4409. if (leaf == root->node) {
  4410. btrfs_set_header_level(leaf, 0);
  4411. } else {
  4412. btrfs_set_path_blocking(path);
  4413. clean_tree_block(fs_info, leaf);
  4414. btrfs_del_leaf(trans, root, path, leaf);
  4415. }
  4416. } else {
  4417. int used = leaf_space_used(leaf, 0, nritems);
  4418. if (slot == 0) {
  4419. struct btrfs_disk_key disk_key;
  4420. btrfs_item_key(leaf, &disk_key, 0);
  4421. fixup_low_keys(fs_info, path, &disk_key, 1);
  4422. }
  4423. /* delete the leaf if it is mostly empty */
  4424. if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
  4425. /* push_leaf_left fixes the path.
  4426. * make sure the path still points to our leaf
  4427. * for possible call to del_ptr below
  4428. */
  4429. slot = path->slots[1];
  4430. extent_buffer_get(leaf);
  4431. btrfs_set_path_blocking(path);
  4432. wret = push_leaf_left(trans, root, path, 1, 1,
  4433. 1, (u32)-1);
  4434. if (wret < 0 && wret != -ENOSPC)
  4435. ret = wret;
  4436. if (path->nodes[0] == leaf &&
  4437. btrfs_header_nritems(leaf)) {
  4438. wret = push_leaf_right(trans, root, path, 1,
  4439. 1, 1, 0);
  4440. if (wret < 0 && wret != -ENOSPC)
  4441. ret = wret;
  4442. }
  4443. if (btrfs_header_nritems(leaf) == 0) {
  4444. path->slots[1] = slot;
  4445. btrfs_del_leaf(trans, root, path, leaf);
  4446. free_extent_buffer(leaf);
  4447. ret = 0;
  4448. } else {
  4449. /* if we're still in the path, make sure
  4450. * we're dirty. Otherwise, one of the
  4451. * push_leaf functions must have already
  4452. * dirtied this buffer
  4453. */
  4454. if (path->nodes[0] == leaf)
  4455. btrfs_mark_buffer_dirty(leaf);
  4456. free_extent_buffer(leaf);
  4457. }
  4458. } else {
  4459. btrfs_mark_buffer_dirty(leaf);
  4460. }
  4461. }
  4462. return ret;
  4463. }
  4464. /*
  4465. * search the tree again to find a leaf with lesser keys
  4466. * returns 0 if it found something or 1 if there are no lesser leaves.
  4467. * returns < 0 on io errors.
  4468. *
  4469. * This may release the path, and so you may lose any locks held at the
  4470. * time you call it.
  4471. */
  4472. int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
  4473. {
  4474. struct btrfs_key key;
  4475. struct btrfs_disk_key found_key;
  4476. int ret;
  4477. btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
  4478. if (key.offset > 0) {
  4479. key.offset--;
  4480. } else if (key.type > 0) {
  4481. key.type--;
  4482. key.offset = (u64)-1;
  4483. } else if (key.objectid > 0) {
  4484. key.objectid--;
  4485. key.type = (u8)-1;
  4486. key.offset = (u64)-1;
  4487. } else {
  4488. return 1;
  4489. }
  4490. btrfs_release_path(path);
  4491. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4492. if (ret < 0)
  4493. return ret;
  4494. btrfs_item_key(path->nodes[0], &found_key, 0);
  4495. ret = comp_keys(&found_key, &key);
  4496. /*
  4497. * We might have had an item with the previous key in the tree right
  4498. * before we released our path. And after we released our path, that
  4499. * item might have been pushed to the first slot (0) of the leaf we
  4500. * were holding due to a tree balance. Alternatively, an item with the
  4501. * previous key can exist as the only element of a leaf (big fat item).
  4502. * Therefore account for these 2 cases, so that our callers (like
  4503. * btrfs_previous_item) don't miss an existing item with a key matching
  4504. * the previous key we computed above.
  4505. */
  4506. if (ret <= 0)
  4507. return 0;
  4508. return 1;
  4509. }
  4510. /*
  4511. * A helper function to walk down the tree starting at min_key, and looking
  4512. * for nodes or leaves that are have a minimum transaction id.
  4513. * This is used by the btree defrag code, and tree logging
  4514. *
  4515. * This does not cow, but it does stuff the starting key it finds back
  4516. * into min_key, so you can call btrfs_search_slot with cow=1 on the
  4517. * key and get a writable path.
  4518. *
  4519. * This does lock as it descends, and path->keep_locks should be set
  4520. * to 1 by the caller.
  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 = 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. }