ctree.c 152 KB

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