ctree.c 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900
  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 *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. ASSERT(path);
  2319. key.type = key_type;
  2320. key.objectid = iobjectid;
  2321. key.offset = ioff;
  2322. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  2323. if ((ret < 0) || (found_key == NULL))
  2324. return ret;
  2325. eb = path->nodes[0];
  2326. if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
  2327. ret = btrfs_next_leaf(fs_root, path);
  2328. if (ret)
  2329. return ret;
  2330. eb = path->nodes[0];
  2331. }
  2332. btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
  2333. if (found_key->type != key.type ||
  2334. found_key->objectid != key.objectid)
  2335. return 1;
  2336. return 0;
  2337. }
  2338. /*
  2339. * look for key in the tree. path is filled in with nodes along the way
  2340. * if key is found, we return zero and you can find the item in the leaf
  2341. * level of the path (level 0)
  2342. *
  2343. * If the key isn't found, the path points to the slot where it should
  2344. * be inserted, and 1 is returned. If there are other errors during the
  2345. * search a negative error number is returned.
  2346. *
  2347. * if ins_len > 0, nodes and leaves will be split as we walk down the
  2348. * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
  2349. * possible)
  2350. */
  2351. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  2352. *root, struct btrfs_key *key, struct btrfs_path *p, int
  2353. ins_len, int cow)
  2354. {
  2355. struct extent_buffer *b;
  2356. int slot;
  2357. int ret;
  2358. int err;
  2359. int level;
  2360. int lowest_unlock = 1;
  2361. int root_lock;
  2362. /* everything at write_lock_level or lower must be write locked */
  2363. int write_lock_level = 0;
  2364. u8 lowest_level = 0;
  2365. int min_write_lock_level;
  2366. int prev_cmp;
  2367. lowest_level = p->lowest_level;
  2368. WARN_ON(lowest_level && ins_len > 0);
  2369. WARN_ON(p->nodes[0] != NULL);
  2370. BUG_ON(!cow && ins_len);
  2371. if (ins_len < 0) {
  2372. lowest_unlock = 2;
  2373. /* when we are removing items, we might have to go up to level
  2374. * two as we update tree pointers Make sure we keep write
  2375. * for those levels as well
  2376. */
  2377. write_lock_level = 2;
  2378. } else if (ins_len > 0) {
  2379. /*
  2380. * for inserting items, make sure we have a write lock on
  2381. * level 1 so we can update keys
  2382. */
  2383. write_lock_level = 1;
  2384. }
  2385. if (!cow)
  2386. write_lock_level = -1;
  2387. if (cow && (p->keep_locks || p->lowest_level))
  2388. write_lock_level = BTRFS_MAX_LEVEL;
  2389. min_write_lock_level = write_lock_level;
  2390. again:
  2391. prev_cmp = -1;
  2392. /*
  2393. * we try very hard to do read locks on the root
  2394. */
  2395. root_lock = BTRFS_READ_LOCK;
  2396. level = 0;
  2397. if (p->search_commit_root) {
  2398. /*
  2399. * the commit roots are read only
  2400. * so we always do read locks
  2401. */
  2402. if (p->need_commit_sem)
  2403. down_read(&root->fs_info->commit_root_sem);
  2404. b = root->commit_root;
  2405. extent_buffer_get(b);
  2406. level = btrfs_header_level(b);
  2407. if (p->need_commit_sem)
  2408. up_read(&root->fs_info->commit_root_sem);
  2409. if (!p->skip_locking)
  2410. btrfs_tree_read_lock(b);
  2411. } else {
  2412. if (p->skip_locking) {
  2413. b = btrfs_root_node(root);
  2414. level = btrfs_header_level(b);
  2415. } else {
  2416. /* we don't know the level of the root node
  2417. * until we actually have it read locked
  2418. */
  2419. b = btrfs_read_lock_root_node(root);
  2420. level = btrfs_header_level(b);
  2421. if (level <= write_lock_level) {
  2422. /* whoops, must trade for write lock */
  2423. btrfs_tree_read_unlock(b);
  2424. free_extent_buffer(b);
  2425. b = btrfs_lock_root_node(root);
  2426. root_lock = BTRFS_WRITE_LOCK;
  2427. /* the level might have changed, check again */
  2428. level = btrfs_header_level(b);
  2429. }
  2430. }
  2431. }
  2432. p->nodes[level] = b;
  2433. if (!p->skip_locking)
  2434. p->locks[level] = root_lock;
  2435. while (b) {
  2436. level = btrfs_header_level(b);
  2437. /*
  2438. * setup the path here so we can release it under lock
  2439. * contention with the cow code
  2440. */
  2441. if (cow) {
  2442. /*
  2443. * if we don't really need to cow this block
  2444. * then we don't want to set the path blocking,
  2445. * so we test it here
  2446. */
  2447. if (!should_cow_block(trans, root, b))
  2448. goto cow_done;
  2449. /*
  2450. * must have write locks on this node and the
  2451. * parent
  2452. */
  2453. if (level > write_lock_level ||
  2454. (level + 1 > write_lock_level &&
  2455. level + 1 < BTRFS_MAX_LEVEL &&
  2456. p->nodes[level + 1])) {
  2457. write_lock_level = level + 1;
  2458. btrfs_release_path(p);
  2459. goto again;
  2460. }
  2461. btrfs_set_path_blocking(p);
  2462. err = btrfs_cow_block(trans, root, b,
  2463. p->nodes[level + 1],
  2464. p->slots[level + 1], &b);
  2465. if (err) {
  2466. ret = err;
  2467. goto done;
  2468. }
  2469. }
  2470. cow_done:
  2471. p->nodes[level] = b;
  2472. btrfs_clear_path_blocking(p, NULL, 0);
  2473. /*
  2474. * we have a lock on b and as long as we aren't changing
  2475. * the tree, there is no way to for the items in b to change.
  2476. * It is safe to drop the lock on our parent before we
  2477. * go through the expensive btree search on b.
  2478. *
  2479. * If we're inserting or deleting (ins_len != 0), then we might
  2480. * be changing slot zero, which may require changing the parent.
  2481. * So, we can't drop the lock until after we know which slot
  2482. * we're operating on.
  2483. */
  2484. if (!ins_len && !p->keep_locks) {
  2485. int u = level + 1;
  2486. if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
  2487. btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
  2488. p->locks[u] = 0;
  2489. }
  2490. }
  2491. ret = key_search(b, key, level, &prev_cmp, &slot);
  2492. if (level != 0) {
  2493. int dec = 0;
  2494. if (ret && slot > 0) {
  2495. dec = 1;
  2496. slot -= 1;
  2497. }
  2498. p->slots[level] = slot;
  2499. err = setup_nodes_for_search(trans, root, p, b, level,
  2500. ins_len, &write_lock_level);
  2501. if (err == -EAGAIN)
  2502. goto again;
  2503. if (err) {
  2504. ret = err;
  2505. goto done;
  2506. }
  2507. b = p->nodes[level];
  2508. slot = p->slots[level];
  2509. /*
  2510. * slot 0 is special, if we change the key
  2511. * we have to update the parent pointer
  2512. * which means we must have a write lock
  2513. * on the parent
  2514. */
  2515. if (slot == 0 && ins_len &&
  2516. write_lock_level < level + 1) {
  2517. write_lock_level = level + 1;
  2518. btrfs_release_path(p);
  2519. goto again;
  2520. }
  2521. unlock_up(p, level, lowest_unlock,
  2522. min_write_lock_level, &write_lock_level);
  2523. if (level == lowest_level) {
  2524. if (dec)
  2525. p->slots[level]++;
  2526. goto done;
  2527. }
  2528. err = read_block_for_search(trans, root, p,
  2529. &b, level, slot, key, 0);
  2530. if (err == -EAGAIN)
  2531. goto again;
  2532. if (err) {
  2533. ret = err;
  2534. goto done;
  2535. }
  2536. if (!p->skip_locking) {
  2537. level = btrfs_header_level(b);
  2538. if (level <= write_lock_level) {
  2539. err = btrfs_try_tree_write_lock(b);
  2540. if (!err) {
  2541. btrfs_set_path_blocking(p);
  2542. btrfs_tree_lock(b);
  2543. btrfs_clear_path_blocking(p, b,
  2544. BTRFS_WRITE_LOCK);
  2545. }
  2546. p->locks[level] = BTRFS_WRITE_LOCK;
  2547. } else {
  2548. err = btrfs_tree_read_lock_atomic(b);
  2549. if (!err) {
  2550. btrfs_set_path_blocking(p);
  2551. btrfs_tree_read_lock(b);
  2552. btrfs_clear_path_blocking(p, b,
  2553. BTRFS_READ_LOCK);
  2554. }
  2555. p->locks[level] = BTRFS_READ_LOCK;
  2556. }
  2557. p->nodes[level] = b;
  2558. }
  2559. } else {
  2560. p->slots[level] = slot;
  2561. if (ins_len > 0 &&
  2562. btrfs_leaf_free_space(root, b) < ins_len) {
  2563. if (write_lock_level < 1) {
  2564. write_lock_level = 1;
  2565. btrfs_release_path(p);
  2566. goto again;
  2567. }
  2568. btrfs_set_path_blocking(p);
  2569. err = split_leaf(trans, root, key,
  2570. p, ins_len, ret == 0);
  2571. btrfs_clear_path_blocking(p, NULL, 0);
  2572. BUG_ON(err > 0);
  2573. if (err) {
  2574. ret = err;
  2575. goto done;
  2576. }
  2577. }
  2578. if (!p->search_for_split)
  2579. unlock_up(p, level, lowest_unlock,
  2580. min_write_lock_level, &write_lock_level);
  2581. goto done;
  2582. }
  2583. }
  2584. ret = 1;
  2585. done:
  2586. /*
  2587. * we don't really know what they plan on doing with the path
  2588. * from here on, so for now just mark it as blocking
  2589. */
  2590. if (!p->leave_spinning)
  2591. btrfs_set_path_blocking(p);
  2592. if (ret < 0 && !p->skip_release_on_error)
  2593. btrfs_release_path(p);
  2594. return ret;
  2595. }
  2596. /*
  2597. * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
  2598. * current state of the tree together with the operations recorded in the tree
  2599. * modification log to search for the key in a previous version of this tree, as
  2600. * denoted by the time_seq parameter.
  2601. *
  2602. * Naturally, there is no support for insert, delete or cow operations.
  2603. *
  2604. * The resulting path and return value will be set up as if we called
  2605. * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
  2606. */
  2607. int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
  2608. struct btrfs_path *p, u64 time_seq)
  2609. {
  2610. struct extent_buffer *b;
  2611. int slot;
  2612. int ret;
  2613. int err;
  2614. int level;
  2615. int lowest_unlock = 1;
  2616. u8 lowest_level = 0;
  2617. int prev_cmp = -1;
  2618. lowest_level = p->lowest_level;
  2619. WARN_ON(p->nodes[0] != NULL);
  2620. if (p->search_commit_root) {
  2621. BUG_ON(time_seq);
  2622. return btrfs_search_slot(NULL, root, key, p, 0, 0);
  2623. }
  2624. again:
  2625. b = get_old_root(root, time_seq);
  2626. level = btrfs_header_level(b);
  2627. p->locks[level] = BTRFS_READ_LOCK;
  2628. while (b) {
  2629. level = btrfs_header_level(b);
  2630. p->nodes[level] = b;
  2631. btrfs_clear_path_blocking(p, NULL, 0);
  2632. /*
  2633. * we have a lock on b and as long as we aren't changing
  2634. * the tree, there is no way to for the items in b to change.
  2635. * It is safe to drop the lock on our parent before we
  2636. * go through the expensive btree search on b.
  2637. */
  2638. btrfs_unlock_up_safe(p, level + 1);
  2639. /*
  2640. * Since we can unwind eb's we want to do a real search every
  2641. * time.
  2642. */
  2643. prev_cmp = -1;
  2644. ret = key_search(b, key, level, &prev_cmp, &slot);
  2645. if (level != 0) {
  2646. int dec = 0;
  2647. if (ret && slot > 0) {
  2648. dec = 1;
  2649. slot -= 1;
  2650. }
  2651. p->slots[level] = slot;
  2652. unlock_up(p, level, lowest_unlock, 0, NULL);
  2653. if (level == lowest_level) {
  2654. if (dec)
  2655. p->slots[level]++;
  2656. goto done;
  2657. }
  2658. err = read_block_for_search(NULL, root, p, &b, level,
  2659. slot, key, time_seq);
  2660. if (err == -EAGAIN)
  2661. goto again;
  2662. if (err) {
  2663. ret = err;
  2664. goto done;
  2665. }
  2666. level = btrfs_header_level(b);
  2667. err = btrfs_tree_read_lock_atomic(b);
  2668. if (!err) {
  2669. btrfs_set_path_blocking(p);
  2670. btrfs_tree_read_lock(b);
  2671. btrfs_clear_path_blocking(p, b,
  2672. BTRFS_READ_LOCK);
  2673. }
  2674. b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
  2675. if (!b) {
  2676. ret = -ENOMEM;
  2677. goto done;
  2678. }
  2679. p->locks[level] = BTRFS_READ_LOCK;
  2680. p->nodes[level] = b;
  2681. } else {
  2682. p->slots[level] = slot;
  2683. unlock_up(p, level, lowest_unlock, 0, NULL);
  2684. goto done;
  2685. }
  2686. }
  2687. ret = 1;
  2688. done:
  2689. if (!p->leave_spinning)
  2690. btrfs_set_path_blocking(p);
  2691. if (ret < 0)
  2692. btrfs_release_path(p);
  2693. return ret;
  2694. }
  2695. /*
  2696. * helper to use instead of search slot if no exact match is needed but
  2697. * instead the next or previous item should be returned.
  2698. * When find_higher is true, the next higher item is returned, the next lower
  2699. * otherwise.
  2700. * When return_any and find_higher are both true, and no higher item is found,
  2701. * return the next lower instead.
  2702. * When return_any is true and find_higher is false, and no lower item is found,
  2703. * return the next higher instead.
  2704. * It returns 0 if any item is found, 1 if none is found (tree empty), and
  2705. * < 0 on error
  2706. */
  2707. int btrfs_search_slot_for_read(struct btrfs_root *root,
  2708. struct btrfs_key *key, struct btrfs_path *p,
  2709. int find_higher, int return_any)
  2710. {
  2711. int ret;
  2712. struct extent_buffer *leaf;
  2713. again:
  2714. ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
  2715. if (ret <= 0)
  2716. return ret;
  2717. /*
  2718. * a return value of 1 means the path is at the position where the
  2719. * item should be inserted. Normally this is the next bigger item,
  2720. * but in case the previous item is the last in a leaf, path points
  2721. * to the first free slot in the previous leaf, i.e. at an invalid
  2722. * item.
  2723. */
  2724. leaf = p->nodes[0];
  2725. if (find_higher) {
  2726. if (p->slots[0] >= btrfs_header_nritems(leaf)) {
  2727. ret = btrfs_next_leaf(root, p);
  2728. if (ret <= 0)
  2729. return ret;
  2730. if (!return_any)
  2731. return 1;
  2732. /*
  2733. * no higher item found, return the next
  2734. * lower instead
  2735. */
  2736. return_any = 0;
  2737. find_higher = 0;
  2738. btrfs_release_path(p);
  2739. goto again;
  2740. }
  2741. } else {
  2742. if (p->slots[0] == 0) {
  2743. ret = btrfs_prev_leaf(root, p);
  2744. if (ret < 0)
  2745. return ret;
  2746. if (!ret) {
  2747. leaf = p->nodes[0];
  2748. if (p->slots[0] == btrfs_header_nritems(leaf))
  2749. p->slots[0]--;
  2750. return 0;
  2751. }
  2752. if (!return_any)
  2753. return 1;
  2754. /*
  2755. * no lower item found, return the next
  2756. * higher instead
  2757. */
  2758. return_any = 0;
  2759. find_higher = 1;
  2760. btrfs_release_path(p);
  2761. goto again;
  2762. } else {
  2763. --p->slots[0];
  2764. }
  2765. }
  2766. return 0;
  2767. }
  2768. /*
  2769. * adjust the pointers going up the tree, starting at level
  2770. * making sure the right key of each node is points to 'key'.
  2771. * This is used after shifting pointers to the left, so it stops
  2772. * fixing up pointers when a given leaf/node is not in slot 0 of the
  2773. * higher levels
  2774. *
  2775. */
  2776. static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
  2777. struct btrfs_disk_key *key, int level)
  2778. {
  2779. int i;
  2780. struct extent_buffer *t;
  2781. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2782. int tslot = path->slots[i];
  2783. if (!path->nodes[i])
  2784. break;
  2785. t = path->nodes[i];
  2786. tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
  2787. btrfs_set_node_key(t, key, tslot);
  2788. btrfs_mark_buffer_dirty(path->nodes[i]);
  2789. if (tslot != 0)
  2790. break;
  2791. }
  2792. }
  2793. /*
  2794. * update item key.
  2795. *
  2796. * This function isn't completely safe. It's the caller's responsibility
  2797. * that the new key won't break the order
  2798. */
  2799. void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
  2800. struct btrfs_key *new_key)
  2801. {
  2802. struct btrfs_disk_key disk_key;
  2803. struct extent_buffer *eb;
  2804. int slot;
  2805. eb = path->nodes[0];
  2806. slot = path->slots[0];
  2807. if (slot > 0) {
  2808. btrfs_item_key(eb, &disk_key, slot - 1);
  2809. BUG_ON(comp_keys(&disk_key, new_key) >= 0);
  2810. }
  2811. if (slot < btrfs_header_nritems(eb) - 1) {
  2812. btrfs_item_key(eb, &disk_key, slot + 1);
  2813. BUG_ON(comp_keys(&disk_key, new_key) <= 0);
  2814. }
  2815. btrfs_cpu_key_to_disk(&disk_key, new_key);
  2816. btrfs_set_item_key(eb, &disk_key, slot);
  2817. btrfs_mark_buffer_dirty(eb);
  2818. if (slot == 0)
  2819. fixup_low_keys(root, path, &disk_key, 1);
  2820. }
  2821. /*
  2822. * try to push data from one node into the next node left in the
  2823. * tree.
  2824. *
  2825. * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
  2826. * error, and > 0 if there was no room in the left hand block.
  2827. */
  2828. static int push_node_left(struct btrfs_trans_handle *trans,
  2829. struct btrfs_root *root, struct extent_buffer *dst,
  2830. struct extent_buffer *src, int empty)
  2831. {
  2832. int push_items = 0;
  2833. int src_nritems;
  2834. int dst_nritems;
  2835. int ret = 0;
  2836. src_nritems = btrfs_header_nritems(src);
  2837. dst_nritems = btrfs_header_nritems(dst);
  2838. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  2839. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2840. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2841. if (!empty && src_nritems <= 8)
  2842. return 1;
  2843. if (push_items <= 0)
  2844. return 1;
  2845. if (empty) {
  2846. push_items = min(src_nritems, push_items);
  2847. if (push_items < src_nritems) {
  2848. /* leave at least 8 pointers in the node if
  2849. * we aren't going to empty it
  2850. */
  2851. if (src_nritems - push_items < 8) {
  2852. if (push_items <= 8)
  2853. return 1;
  2854. push_items -= 8;
  2855. }
  2856. }
  2857. } else
  2858. push_items = min(src_nritems - 8, push_items);
  2859. ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
  2860. push_items);
  2861. if (ret) {
  2862. btrfs_abort_transaction(trans, root, ret);
  2863. return ret;
  2864. }
  2865. copy_extent_buffer(dst, src,
  2866. btrfs_node_key_ptr_offset(dst_nritems),
  2867. btrfs_node_key_ptr_offset(0),
  2868. push_items * sizeof(struct btrfs_key_ptr));
  2869. if (push_items < src_nritems) {
  2870. /*
  2871. * don't call tree_mod_log_eb_move here, key removal was already
  2872. * fully logged by tree_mod_log_eb_copy above.
  2873. */
  2874. memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
  2875. btrfs_node_key_ptr_offset(push_items),
  2876. (src_nritems - push_items) *
  2877. sizeof(struct btrfs_key_ptr));
  2878. }
  2879. btrfs_set_header_nritems(src, src_nritems - push_items);
  2880. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2881. btrfs_mark_buffer_dirty(src);
  2882. btrfs_mark_buffer_dirty(dst);
  2883. return ret;
  2884. }
  2885. /*
  2886. * try to push data from one node into the next node right in the
  2887. * tree.
  2888. *
  2889. * returns 0 if some ptrs were pushed, < 0 if there was some horrible
  2890. * error, and > 0 if there was no room in the right hand block.
  2891. *
  2892. * this will only push up to 1/2 the contents of the left node over
  2893. */
  2894. static int balance_node_right(struct btrfs_trans_handle *trans,
  2895. struct btrfs_root *root,
  2896. struct extent_buffer *dst,
  2897. struct extent_buffer *src)
  2898. {
  2899. int push_items = 0;
  2900. int max_push;
  2901. int src_nritems;
  2902. int dst_nritems;
  2903. int ret = 0;
  2904. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2905. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2906. src_nritems = btrfs_header_nritems(src);
  2907. dst_nritems = btrfs_header_nritems(dst);
  2908. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  2909. if (push_items <= 0)
  2910. return 1;
  2911. if (src_nritems < 4)
  2912. return 1;
  2913. max_push = src_nritems / 2 + 1;
  2914. /* don't try to empty the node */
  2915. if (max_push >= src_nritems)
  2916. return 1;
  2917. if (max_push < push_items)
  2918. push_items = max_push;
  2919. tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
  2920. memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
  2921. btrfs_node_key_ptr_offset(0),
  2922. (dst_nritems) *
  2923. sizeof(struct btrfs_key_ptr));
  2924. ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
  2925. src_nritems - push_items, push_items);
  2926. if (ret) {
  2927. btrfs_abort_transaction(trans, root, ret);
  2928. return ret;
  2929. }
  2930. copy_extent_buffer(dst, src,
  2931. btrfs_node_key_ptr_offset(0),
  2932. btrfs_node_key_ptr_offset(src_nritems - push_items),
  2933. push_items * sizeof(struct btrfs_key_ptr));
  2934. btrfs_set_header_nritems(src, src_nritems - push_items);
  2935. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2936. btrfs_mark_buffer_dirty(src);
  2937. btrfs_mark_buffer_dirty(dst);
  2938. return ret;
  2939. }
  2940. /*
  2941. * helper function to insert a new root level in the tree.
  2942. * A new node is allocated, and a single item is inserted to
  2943. * point to the existing root
  2944. *
  2945. * returns zero on success or < 0 on failure.
  2946. */
  2947. static noinline int insert_new_root(struct btrfs_trans_handle *trans,
  2948. struct btrfs_root *root,
  2949. struct btrfs_path *path, int level)
  2950. {
  2951. u64 lower_gen;
  2952. struct extent_buffer *lower;
  2953. struct extent_buffer *c;
  2954. struct extent_buffer *old;
  2955. struct btrfs_disk_key lower_key;
  2956. BUG_ON(path->nodes[level]);
  2957. BUG_ON(path->nodes[level-1] != root->node);
  2958. lower = path->nodes[level-1];
  2959. if (level == 1)
  2960. btrfs_item_key(lower, &lower_key, 0);
  2961. else
  2962. btrfs_node_key(lower, &lower_key, 0);
  2963. c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  2964. &lower_key, level, root->node->start, 0);
  2965. if (IS_ERR(c))
  2966. return PTR_ERR(c);
  2967. root_add_used(root, root->nodesize);
  2968. memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
  2969. btrfs_set_header_nritems(c, 1);
  2970. btrfs_set_header_level(c, level);
  2971. btrfs_set_header_bytenr(c, c->start);
  2972. btrfs_set_header_generation(c, trans->transid);
  2973. btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
  2974. btrfs_set_header_owner(c, root->root_key.objectid);
  2975. write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
  2976. BTRFS_FSID_SIZE);
  2977. write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
  2978. btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
  2979. btrfs_set_node_key(c, &lower_key, 0);
  2980. btrfs_set_node_blockptr(c, 0, lower->start);
  2981. lower_gen = btrfs_header_generation(lower);
  2982. WARN_ON(lower_gen != trans->transid);
  2983. btrfs_set_node_ptr_generation(c, 0, lower_gen);
  2984. btrfs_mark_buffer_dirty(c);
  2985. old = root->node;
  2986. tree_mod_log_set_root_pointer(root, c, 0);
  2987. rcu_assign_pointer(root->node, c);
  2988. /* the super has an extra ref to root->node */
  2989. free_extent_buffer(old);
  2990. add_root_to_dirty_list(root);
  2991. extent_buffer_get(c);
  2992. path->nodes[level] = c;
  2993. path->locks[level] = BTRFS_WRITE_LOCK;
  2994. path->slots[level] = 0;
  2995. return 0;
  2996. }
  2997. /*
  2998. * worker function to insert a single pointer in a node.
  2999. * the node should have enough room for the pointer already
  3000. *
  3001. * slot and level indicate where you want the key to go, and
  3002. * blocknr is the block the key points to.
  3003. */
  3004. static void insert_ptr(struct btrfs_trans_handle *trans,
  3005. struct btrfs_root *root, struct btrfs_path *path,
  3006. struct btrfs_disk_key *key, u64 bytenr,
  3007. int slot, int level)
  3008. {
  3009. struct extent_buffer *lower;
  3010. int nritems;
  3011. int ret;
  3012. BUG_ON(!path->nodes[level]);
  3013. btrfs_assert_tree_locked(path->nodes[level]);
  3014. lower = path->nodes[level];
  3015. nritems = btrfs_header_nritems(lower);
  3016. BUG_ON(slot > nritems);
  3017. BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
  3018. if (slot != nritems) {
  3019. if (level)
  3020. tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
  3021. slot, nritems - slot);
  3022. memmove_extent_buffer(lower,
  3023. btrfs_node_key_ptr_offset(slot + 1),
  3024. btrfs_node_key_ptr_offset(slot),
  3025. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  3026. }
  3027. if (level) {
  3028. ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
  3029. MOD_LOG_KEY_ADD, GFP_NOFS);
  3030. BUG_ON(ret < 0);
  3031. }
  3032. btrfs_set_node_key(lower, key, slot);
  3033. btrfs_set_node_blockptr(lower, slot, bytenr);
  3034. WARN_ON(trans->transid == 0);
  3035. btrfs_set_node_ptr_generation(lower, slot, trans->transid);
  3036. btrfs_set_header_nritems(lower, nritems + 1);
  3037. btrfs_mark_buffer_dirty(lower);
  3038. }
  3039. /*
  3040. * split the node at the specified level in path in two.
  3041. * The path is corrected to point to the appropriate node after the split
  3042. *
  3043. * Before splitting this tries to make some room in the node by pushing
  3044. * left and right, if either one works, it returns right away.
  3045. *
  3046. * returns 0 on success and < 0 on failure
  3047. */
  3048. static noinline int split_node(struct btrfs_trans_handle *trans,
  3049. struct btrfs_root *root,
  3050. struct btrfs_path *path, int level)
  3051. {
  3052. struct extent_buffer *c;
  3053. struct extent_buffer *split;
  3054. struct btrfs_disk_key disk_key;
  3055. int mid;
  3056. int ret;
  3057. u32 c_nritems;
  3058. c = path->nodes[level];
  3059. WARN_ON(btrfs_header_generation(c) != trans->transid);
  3060. if (c == root->node) {
  3061. /*
  3062. * trying to split the root, lets make a new one
  3063. *
  3064. * tree mod log: We don't log_removal old root in
  3065. * insert_new_root, because that root buffer will be kept as a
  3066. * normal node. We are going to log removal of half of the
  3067. * elements below with tree_mod_log_eb_copy. We're holding a
  3068. * tree lock on the buffer, which is why we cannot race with
  3069. * other tree_mod_log users.
  3070. */
  3071. ret = insert_new_root(trans, root, path, level + 1);
  3072. if (ret)
  3073. return ret;
  3074. } else {
  3075. ret = push_nodes_for_insert(trans, root, path, level);
  3076. c = path->nodes[level];
  3077. if (!ret && btrfs_header_nritems(c) <
  3078. BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
  3079. return 0;
  3080. if (ret < 0)
  3081. return ret;
  3082. }
  3083. c_nritems = btrfs_header_nritems(c);
  3084. mid = (c_nritems + 1) / 2;
  3085. btrfs_node_key(c, &disk_key, mid);
  3086. split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3087. &disk_key, level, c->start, 0);
  3088. if (IS_ERR(split))
  3089. return PTR_ERR(split);
  3090. root_add_used(root, root->nodesize);
  3091. memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
  3092. btrfs_set_header_level(split, btrfs_header_level(c));
  3093. btrfs_set_header_bytenr(split, split->start);
  3094. btrfs_set_header_generation(split, trans->transid);
  3095. btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
  3096. btrfs_set_header_owner(split, root->root_key.objectid);
  3097. write_extent_buffer(split, root->fs_info->fsid,
  3098. btrfs_header_fsid(), BTRFS_FSID_SIZE);
  3099. write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
  3100. btrfs_header_chunk_tree_uuid(split),
  3101. BTRFS_UUID_SIZE);
  3102. ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
  3103. mid, c_nritems - mid);
  3104. if (ret) {
  3105. btrfs_abort_transaction(trans, root, ret);
  3106. return ret;
  3107. }
  3108. copy_extent_buffer(split, c,
  3109. btrfs_node_key_ptr_offset(0),
  3110. btrfs_node_key_ptr_offset(mid),
  3111. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  3112. btrfs_set_header_nritems(split, c_nritems - mid);
  3113. btrfs_set_header_nritems(c, mid);
  3114. ret = 0;
  3115. btrfs_mark_buffer_dirty(c);
  3116. btrfs_mark_buffer_dirty(split);
  3117. insert_ptr(trans, root, path, &disk_key, split->start,
  3118. path->slots[level + 1] + 1, level + 1);
  3119. if (path->slots[level] >= mid) {
  3120. path->slots[level] -= mid;
  3121. btrfs_tree_unlock(c);
  3122. free_extent_buffer(c);
  3123. path->nodes[level] = split;
  3124. path->slots[level + 1] += 1;
  3125. } else {
  3126. btrfs_tree_unlock(split);
  3127. free_extent_buffer(split);
  3128. }
  3129. return ret;
  3130. }
  3131. /*
  3132. * how many bytes are required to store the items in a leaf. start
  3133. * and nr indicate which items in the leaf to check. This totals up the
  3134. * space used both by the item structs and the item data
  3135. */
  3136. static int leaf_space_used(struct extent_buffer *l, int start, int nr)
  3137. {
  3138. struct btrfs_item *start_item;
  3139. struct btrfs_item *end_item;
  3140. struct btrfs_map_token token;
  3141. int data_len;
  3142. int nritems = btrfs_header_nritems(l);
  3143. int end = min(nritems, start + nr) - 1;
  3144. if (!nr)
  3145. return 0;
  3146. btrfs_init_map_token(&token);
  3147. start_item = btrfs_item_nr(start);
  3148. end_item = btrfs_item_nr(end);
  3149. data_len = btrfs_token_item_offset(l, start_item, &token) +
  3150. btrfs_token_item_size(l, start_item, &token);
  3151. data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
  3152. data_len += sizeof(struct btrfs_item) * nr;
  3153. WARN_ON(data_len < 0);
  3154. return data_len;
  3155. }
  3156. /*
  3157. * The space between the end of the leaf items and
  3158. * the start of the leaf data. IOW, how much room
  3159. * the leaf has left for both items and data
  3160. */
  3161. noinline int btrfs_leaf_free_space(struct btrfs_root *root,
  3162. struct extent_buffer *leaf)
  3163. {
  3164. int nritems = btrfs_header_nritems(leaf);
  3165. int ret;
  3166. ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
  3167. if (ret < 0) {
  3168. btrfs_crit(root->fs_info,
  3169. "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
  3170. ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
  3171. leaf_space_used(leaf, 0, nritems), nritems);
  3172. }
  3173. return ret;
  3174. }
  3175. /*
  3176. * min slot controls the lowest index we're willing to push to the
  3177. * right. We'll push up to and including min_slot, but no lower
  3178. */
  3179. static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
  3180. struct btrfs_root *root,
  3181. struct btrfs_path *path,
  3182. int data_size, int empty,
  3183. struct extent_buffer *right,
  3184. int free_space, u32 left_nritems,
  3185. u32 min_slot)
  3186. {
  3187. struct extent_buffer *left = path->nodes[0];
  3188. struct extent_buffer *upper = path->nodes[1];
  3189. struct btrfs_map_token token;
  3190. struct btrfs_disk_key disk_key;
  3191. int slot;
  3192. u32 i;
  3193. int push_space = 0;
  3194. int push_items = 0;
  3195. struct btrfs_item *item;
  3196. u32 nr;
  3197. u32 right_nritems;
  3198. u32 data_end;
  3199. u32 this_item_size;
  3200. btrfs_init_map_token(&token);
  3201. if (empty)
  3202. nr = 0;
  3203. else
  3204. nr = max_t(u32, 1, min_slot);
  3205. if (path->slots[0] >= left_nritems)
  3206. push_space += data_size;
  3207. slot = path->slots[1];
  3208. i = left_nritems - 1;
  3209. while (i >= nr) {
  3210. item = btrfs_item_nr(i);
  3211. if (!empty && push_items > 0) {
  3212. if (path->slots[0] > i)
  3213. break;
  3214. if (path->slots[0] == i) {
  3215. int space = btrfs_leaf_free_space(root, left);
  3216. if (space + push_space * 2 > free_space)
  3217. break;
  3218. }
  3219. }
  3220. if (path->slots[0] == i)
  3221. push_space += data_size;
  3222. this_item_size = btrfs_item_size(left, item);
  3223. if (this_item_size + sizeof(*item) + push_space > free_space)
  3224. break;
  3225. push_items++;
  3226. push_space += this_item_size + sizeof(*item);
  3227. if (i == 0)
  3228. break;
  3229. i--;
  3230. }
  3231. if (push_items == 0)
  3232. goto out_unlock;
  3233. WARN_ON(!empty && push_items == left_nritems);
  3234. /* push left to right */
  3235. right_nritems = btrfs_header_nritems(right);
  3236. push_space = btrfs_item_end_nr(left, left_nritems - push_items);
  3237. push_space -= leaf_data_end(root, left);
  3238. /* make room in the right data area */
  3239. data_end = leaf_data_end(root, right);
  3240. memmove_extent_buffer(right,
  3241. btrfs_leaf_data(right) + data_end - push_space,
  3242. btrfs_leaf_data(right) + data_end,
  3243. BTRFS_LEAF_DATA_SIZE(root) - data_end);
  3244. /* copy from the left data area */
  3245. copy_extent_buffer(right, left, btrfs_leaf_data(right) +
  3246. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  3247. btrfs_leaf_data(left) + leaf_data_end(root, left),
  3248. push_space);
  3249. memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
  3250. btrfs_item_nr_offset(0),
  3251. right_nritems * sizeof(struct btrfs_item));
  3252. /* copy the items from left to right */
  3253. copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
  3254. btrfs_item_nr_offset(left_nritems - push_items),
  3255. push_items * sizeof(struct btrfs_item));
  3256. /* update the item pointers */
  3257. right_nritems += push_items;
  3258. btrfs_set_header_nritems(right, right_nritems);
  3259. push_space = BTRFS_LEAF_DATA_SIZE(root);
  3260. for (i = 0; i < right_nritems; i++) {
  3261. item = btrfs_item_nr(i);
  3262. push_space -= btrfs_token_item_size(right, item, &token);
  3263. btrfs_set_token_item_offset(right, item, push_space, &token);
  3264. }
  3265. left_nritems -= push_items;
  3266. btrfs_set_header_nritems(left, left_nritems);
  3267. if (left_nritems)
  3268. btrfs_mark_buffer_dirty(left);
  3269. else
  3270. clean_tree_block(trans, root, left);
  3271. btrfs_mark_buffer_dirty(right);
  3272. btrfs_item_key(right, &disk_key, 0);
  3273. btrfs_set_node_key(upper, &disk_key, slot + 1);
  3274. btrfs_mark_buffer_dirty(upper);
  3275. /* then fixup the leaf pointer in the path */
  3276. if (path->slots[0] >= left_nritems) {
  3277. path->slots[0] -= left_nritems;
  3278. if (btrfs_header_nritems(path->nodes[0]) == 0)
  3279. clean_tree_block(trans, root, path->nodes[0]);
  3280. btrfs_tree_unlock(path->nodes[0]);
  3281. free_extent_buffer(path->nodes[0]);
  3282. path->nodes[0] = right;
  3283. path->slots[1] += 1;
  3284. } else {
  3285. btrfs_tree_unlock(right);
  3286. free_extent_buffer(right);
  3287. }
  3288. return 0;
  3289. out_unlock:
  3290. btrfs_tree_unlock(right);
  3291. free_extent_buffer(right);
  3292. return 1;
  3293. }
  3294. /*
  3295. * push some data in the path leaf to the right, trying to free up at
  3296. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3297. *
  3298. * returns 1 if the push failed because the other node didn't have enough
  3299. * room, 0 if everything worked out and < 0 if there were major errors.
  3300. *
  3301. * this will push starting from min_slot to the end of the leaf. It won't
  3302. * push any slot lower than min_slot
  3303. */
  3304. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  3305. *root, struct btrfs_path *path,
  3306. int min_data_size, int data_size,
  3307. int empty, u32 min_slot)
  3308. {
  3309. struct extent_buffer *left = path->nodes[0];
  3310. struct extent_buffer *right;
  3311. struct extent_buffer *upper;
  3312. int slot;
  3313. int free_space;
  3314. u32 left_nritems;
  3315. int ret;
  3316. if (!path->nodes[1])
  3317. return 1;
  3318. slot = path->slots[1];
  3319. upper = path->nodes[1];
  3320. if (slot >= btrfs_header_nritems(upper) - 1)
  3321. return 1;
  3322. btrfs_assert_tree_locked(path->nodes[1]);
  3323. right = read_node_slot(root, upper, slot + 1);
  3324. if (right == NULL)
  3325. return 1;
  3326. btrfs_tree_lock(right);
  3327. btrfs_set_lock_blocking(right);
  3328. free_space = btrfs_leaf_free_space(root, right);
  3329. if (free_space < data_size)
  3330. goto out_unlock;
  3331. /* cow and double check */
  3332. ret = btrfs_cow_block(trans, root, right, upper,
  3333. slot + 1, &right);
  3334. if (ret)
  3335. goto out_unlock;
  3336. free_space = btrfs_leaf_free_space(root, right);
  3337. if (free_space < data_size)
  3338. goto out_unlock;
  3339. left_nritems = btrfs_header_nritems(left);
  3340. if (left_nritems == 0)
  3341. goto out_unlock;
  3342. if (path->slots[0] == left_nritems && !empty) {
  3343. /* Key greater than all keys in the leaf, right neighbor has
  3344. * enough room for it and we're not emptying our leaf to delete
  3345. * it, therefore use right neighbor to insert the new item and
  3346. * no need to touch/dirty our left leaft. */
  3347. btrfs_tree_unlock(left);
  3348. free_extent_buffer(left);
  3349. path->nodes[0] = right;
  3350. path->slots[0] = 0;
  3351. path->slots[1]++;
  3352. return 0;
  3353. }
  3354. return __push_leaf_right(trans, root, path, min_data_size, empty,
  3355. right, free_space, left_nritems, min_slot);
  3356. out_unlock:
  3357. btrfs_tree_unlock(right);
  3358. free_extent_buffer(right);
  3359. return 1;
  3360. }
  3361. /*
  3362. * push some data in the path leaf to the left, trying to free up at
  3363. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3364. *
  3365. * max_slot can put a limit on how far into the leaf we'll push items. The
  3366. * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
  3367. * items
  3368. */
  3369. static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
  3370. struct btrfs_root *root,
  3371. struct btrfs_path *path, int data_size,
  3372. int empty, struct extent_buffer *left,
  3373. int free_space, u32 right_nritems,
  3374. u32 max_slot)
  3375. {
  3376. struct btrfs_disk_key disk_key;
  3377. struct extent_buffer *right = path->nodes[0];
  3378. int i;
  3379. int push_space = 0;
  3380. int push_items = 0;
  3381. struct btrfs_item *item;
  3382. u32 old_left_nritems;
  3383. u32 nr;
  3384. int ret = 0;
  3385. u32 this_item_size;
  3386. u32 old_left_item_size;
  3387. struct btrfs_map_token token;
  3388. btrfs_init_map_token(&token);
  3389. if (empty)
  3390. nr = min(right_nritems, max_slot);
  3391. else
  3392. nr = min(right_nritems - 1, max_slot);
  3393. for (i = 0; i < nr; i++) {
  3394. item = btrfs_item_nr(i);
  3395. if (!empty && push_items > 0) {
  3396. if (path->slots[0] < i)
  3397. break;
  3398. if (path->slots[0] == i) {
  3399. int space = btrfs_leaf_free_space(root, right);
  3400. if (space + push_space * 2 > free_space)
  3401. break;
  3402. }
  3403. }
  3404. if (path->slots[0] == i)
  3405. push_space += data_size;
  3406. this_item_size = btrfs_item_size(right, item);
  3407. if (this_item_size + sizeof(*item) + push_space > free_space)
  3408. break;
  3409. push_items++;
  3410. push_space += this_item_size + sizeof(*item);
  3411. }
  3412. if (push_items == 0) {
  3413. ret = 1;
  3414. goto out;
  3415. }
  3416. WARN_ON(!empty && push_items == btrfs_header_nritems(right));
  3417. /* push data from right to left */
  3418. copy_extent_buffer(left, right,
  3419. btrfs_item_nr_offset(btrfs_header_nritems(left)),
  3420. btrfs_item_nr_offset(0),
  3421. push_items * sizeof(struct btrfs_item));
  3422. push_space = BTRFS_LEAF_DATA_SIZE(root) -
  3423. btrfs_item_offset_nr(right, push_items - 1);
  3424. copy_extent_buffer(left, right, btrfs_leaf_data(left) +
  3425. leaf_data_end(root, left) - push_space,
  3426. btrfs_leaf_data(right) +
  3427. btrfs_item_offset_nr(right, push_items - 1),
  3428. push_space);
  3429. old_left_nritems = btrfs_header_nritems(left);
  3430. BUG_ON(old_left_nritems <= 0);
  3431. old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
  3432. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  3433. u32 ioff;
  3434. item = btrfs_item_nr(i);
  3435. ioff = btrfs_token_item_offset(left, item, &token);
  3436. btrfs_set_token_item_offset(left, item,
  3437. ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
  3438. &token);
  3439. }
  3440. btrfs_set_header_nritems(left, old_left_nritems + push_items);
  3441. /* fixup right node */
  3442. if (push_items > right_nritems)
  3443. WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
  3444. right_nritems);
  3445. if (push_items < right_nritems) {
  3446. push_space = btrfs_item_offset_nr(right, push_items - 1) -
  3447. leaf_data_end(root, right);
  3448. memmove_extent_buffer(right, btrfs_leaf_data(right) +
  3449. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  3450. btrfs_leaf_data(right) +
  3451. leaf_data_end(root, right), push_space);
  3452. memmove_extent_buffer(right, btrfs_item_nr_offset(0),
  3453. btrfs_item_nr_offset(push_items),
  3454. (btrfs_header_nritems(right) - push_items) *
  3455. sizeof(struct btrfs_item));
  3456. }
  3457. right_nritems -= push_items;
  3458. btrfs_set_header_nritems(right, right_nritems);
  3459. push_space = BTRFS_LEAF_DATA_SIZE(root);
  3460. for (i = 0; i < right_nritems; i++) {
  3461. item = btrfs_item_nr(i);
  3462. push_space = push_space - btrfs_token_item_size(right,
  3463. item, &token);
  3464. btrfs_set_token_item_offset(right, item, push_space, &token);
  3465. }
  3466. btrfs_mark_buffer_dirty(left);
  3467. if (right_nritems)
  3468. btrfs_mark_buffer_dirty(right);
  3469. else
  3470. clean_tree_block(trans, root, right);
  3471. btrfs_item_key(right, &disk_key, 0);
  3472. fixup_low_keys(root, path, &disk_key, 1);
  3473. /* then fixup the leaf pointer in the path */
  3474. if (path->slots[0] < push_items) {
  3475. path->slots[0] += old_left_nritems;
  3476. btrfs_tree_unlock(path->nodes[0]);
  3477. free_extent_buffer(path->nodes[0]);
  3478. path->nodes[0] = left;
  3479. path->slots[1] -= 1;
  3480. } else {
  3481. btrfs_tree_unlock(left);
  3482. free_extent_buffer(left);
  3483. path->slots[0] -= push_items;
  3484. }
  3485. BUG_ON(path->slots[0] < 0);
  3486. return ret;
  3487. out:
  3488. btrfs_tree_unlock(left);
  3489. free_extent_buffer(left);
  3490. return ret;
  3491. }
  3492. /*
  3493. * push some data in the path leaf to the left, trying to free up at
  3494. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3495. *
  3496. * max_slot can put a limit on how far into the leaf we'll push items. The
  3497. * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
  3498. * items
  3499. */
  3500. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  3501. *root, struct btrfs_path *path, int min_data_size,
  3502. int data_size, int empty, u32 max_slot)
  3503. {
  3504. struct extent_buffer *right = path->nodes[0];
  3505. struct extent_buffer *left;
  3506. int slot;
  3507. int free_space;
  3508. u32 right_nritems;
  3509. int ret = 0;
  3510. slot = path->slots[1];
  3511. if (slot == 0)
  3512. return 1;
  3513. if (!path->nodes[1])
  3514. return 1;
  3515. right_nritems = btrfs_header_nritems(right);
  3516. if (right_nritems == 0)
  3517. return 1;
  3518. btrfs_assert_tree_locked(path->nodes[1]);
  3519. left = read_node_slot(root, path->nodes[1], slot - 1);
  3520. if (left == NULL)
  3521. return 1;
  3522. btrfs_tree_lock(left);
  3523. btrfs_set_lock_blocking(left);
  3524. free_space = btrfs_leaf_free_space(root, left);
  3525. if (free_space < data_size) {
  3526. ret = 1;
  3527. goto out;
  3528. }
  3529. /* cow and double check */
  3530. ret = btrfs_cow_block(trans, root, left,
  3531. path->nodes[1], slot - 1, &left);
  3532. if (ret) {
  3533. /* we hit -ENOSPC, but it isn't fatal here */
  3534. if (ret == -ENOSPC)
  3535. ret = 1;
  3536. goto out;
  3537. }
  3538. free_space = btrfs_leaf_free_space(root, left);
  3539. if (free_space < data_size) {
  3540. ret = 1;
  3541. goto out;
  3542. }
  3543. return __push_leaf_left(trans, root, path, min_data_size,
  3544. empty, left, free_space, right_nritems,
  3545. max_slot);
  3546. out:
  3547. btrfs_tree_unlock(left);
  3548. free_extent_buffer(left);
  3549. return ret;
  3550. }
  3551. /*
  3552. * split the path's leaf in two, making sure there is at least data_size
  3553. * available for the resulting leaf level of the path.
  3554. */
  3555. static noinline void copy_for_split(struct btrfs_trans_handle *trans,
  3556. struct btrfs_root *root,
  3557. struct btrfs_path *path,
  3558. struct extent_buffer *l,
  3559. struct extent_buffer *right,
  3560. int slot, int mid, int nritems)
  3561. {
  3562. int data_copy_size;
  3563. int rt_data_off;
  3564. int i;
  3565. struct btrfs_disk_key disk_key;
  3566. struct btrfs_map_token token;
  3567. btrfs_init_map_token(&token);
  3568. nritems = nritems - mid;
  3569. btrfs_set_header_nritems(right, nritems);
  3570. data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
  3571. copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
  3572. btrfs_item_nr_offset(mid),
  3573. nritems * sizeof(struct btrfs_item));
  3574. copy_extent_buffer(right, l,
  3575. btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
  3576. data_copy_size, btrfs_leaf_data(l) +
  3577. leaf_data_end(root, l), data_copy_size);
  3578. rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
  3579. btrfs_item_end_nr(l, mid);
  3580. for (i = 0; i < nritems; i++) {
  3581. struct btrfs_item *item = btrfs_item_nr(i);
  3582. u32 ioff;
  3583. ioff = btrfs_token_item_offset(right, item, &token);
  3584. btrfs_set_token_item_offset(right, item,
  3585. ioff + rt_data_off, &token);
  3586. }
  3587. btrfs_set_header_nritems(l, mid);
  3588. btrfs_item_key(right, &disk_key, 0);
  3589. insert_ptr(trans, root, path, &disk_key, right->start,
  3590. path->slots[1] + 1, 1);
  3591. btrfs_mark_buffer_dirty(right);
  3592. btrfs_mark_buffer_dirty(l);
  3593. BUG_ON(path->slots[0] != slot);
  3594. if (mid <= slot) {
  3595. btrfs_tree_unlock(path->nodes[0]);
  3596. free_extent_buffer(path->nodes[0]);
  3597. path->nodes[0] = right;
  3598. path->slots[0] -= mid;
  3599. path->slots[1] += 1;
  3600. } else {
  3601. btrfs_tree_unlock(right);
  3602. free_extent_buffer(right);
  3603. }
  3604. BUG_ON(path->slots[0] < 0);
  3605. }
  3606. /*
  3607. * double splits happen when we need to insert a big item in the middle
  3608. * of a leaf. A double split can leave us with 3 mostly empty leaves:
  3609. * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
  3610. * A B C
  3611. *
  3612. * We avoid this by trying to push the items on either side of our target
  3613. * into the adjacent leaves. If all goes well we can avoid the double split
  3614. * completely.
  3615. */
  3616. static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
  3617. struct btrfs_root *root,
  3618. struct btrfs_path *path,
  3619. int data_size)
  3620. {
  3621. int ret;
  3622. int progress = 0;
  3623. int slot;
  3624. u32 nritems;
  3625. int space_needed = data_size;
  3626. slot = path->slots[0];
  3627. if (slot < btrfs_header_nritems(path->nodes[0]))
  3628. space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
  3629. /*
  3630. * try to push all the items after our slot into the
  3631. * right leaf
  3632. */
  3633. ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
  3634. if (ret < 0)
  3635. return ret;
  3636. if (ret == 0)
  3637. progress++;
  3638. nritems = btrfs_header_nritems(path->nodes[0]);
  3639. /*
  3640. * our goal is to get our slot at the start or end of a leaf. If
  3641. * we've done so we're done
  3642. */
  3643. if (path->slots[0] == 0 || path->slots[0] == nritems)
  3644. return 0;
  3645. if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
  3646. return 0;
  3647. /* try to push all the items before our slot into the next leaf */
  3648. slot = path->slots[0];
  3649. ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
  3650. if (ret < 0)
  3651. return ret;
  3652. if (ret == 0)
  3653. progress++;
  3654. if (progress)
  3655. return 0;
  3656. return 1;
  3657. }
  3658. /*
  3659. * split the path's leaf in two, making sure there is at least data_size
  3660. * available for the resulting leaf level of the path.
  3661. *
  3662. * returns 0 if all went well and < 0 on failure.
  3663. */
  3664. static noinline int split_leaf(struct btrfs_trans_handle *trans,
  3665. struct btrfs_root *root,
  3666. struct btrfs_key *ins_key,
  3667. struct btrfs_path *path, int data_size,
  3668. int extend)
  3669. {
  3670. struct btrfs_disk_key disk_key;
  3671. struct extent_buffer *l;
  3672. u32 nritems;
  3673. int mid;
  3674. int slot;
  3675. struct extent_buffer *right;
  3676. int ret = 0;
  3677. int wret;
  3678. int split;
  3679. int num_doubles = 0;
  3680. int tried_avoid_double = 0;
  3681. l = path->nodes[0];
  3682. slot = path->slots[0];
  3683. if (extend && data_size + btrfs_item_size_nr(l, slot) +
  3684. sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
  3685. return -EOVERFLOW;
  3686. /* first try to make some room by pushing left and right */
  3687. if (data_size && path->nodes[1]) {
  3688. int space_needed = data_size;
  3689. if (slot < btrfs_header_nritems(l))
  3690. space_needed -= btrfs_leaf_free_space(root, l);
  3691. wret = push_leaf_right(trans, root, path, space_needed,
  3692. space_needed, 0, 0);
  3693. if (wret < 0)
  3694. return wret;
  3695. if (wret) {
  3696. wret = push_leaf_left(trans, root, path, space_needed,
  3697. space_needed, 0, (u32)-1);
  3698. if (wret < 0)
  3699. return wret;
  3700. }
  3701. l = path->nodes[0];
  3702. /* did the pushes work? */
  3703. if (btrfs_leaf_free_space(root, l) >= data_size)
  3704. return 0;
  3705. }
  3706. if (!path->nodes[1]) {
  3707. ret = insert_new_root(trans, root, path, 1);
  3708. if (ret)
  3709. return ret;
  3710. }
  3711. again:
  3712. split = 1;
  3713. l = path->nodes[0];
  3714. slot = path->slots[0];
  3715. nritems = btrfs_header_nritems(l);
  3716. mid = (nritems + 1) / 2;
  3717. if (mid <= slot) {
  3718. if (nritems == 1 ||
  3719. leaf_space_used(l, mid, nritems - mid) + data_size >
  3720. BTRFS_LEAF_DATA_SIZE(root)) {
  3721. if (slot >= nritems) {
  3722. split = 0;
  3723. } else {
  3724. mid = slot;
  3725. if (mid != nritems &&
  3726. leaf_space_used(l, mid, nritems - mid) +
  3727. data_size > BTRFS_LEAF_DATA_SIZE(root)) {
  3728. if (data_size && !tried_avoid_double)
  3729. goto push_for_double;
  3730. split = 2;
  3731. }
  3732. }
  3733. }
  3734. } else {
  3735. if (leaf_space_used(l, 0, mid) + data_size >
  3736. BTRFS_LEAF_DATA_SIZE(root)) {
  3737. if (!extend && data_size && slot == 0) {
  3738. split = 0;
  3739. } else if ((extend || !data_size) && slot == 0) {
  3740. mid = 1;
  3741. } else {
  3742. mid = slot;
  3743. if (mid != nritems &&
  3744. leaf_space_used(l, mid, nritems - mid) +
  3745. data_size > BTRFS_LEAF_DATA_SIZE(root)) {
  3746. if (data_size && !tried_avoid_double)
  3747. goto push_for_double;
  3748. split = 2;
  3749. }
  3750. }
  3751. }
  3752. }
  3753. if (split == 0)
  3754. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  3755. else
  3756. btrfs_item_key(l, &disk_key, mid);
  3757. right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3758. &disk_key, 0, l->start, 0);
  3759. if (IS_ERR(right))
  3760. return PTR_ERR(right);
  3761. root_add_used(root, root->nodesize);
  3762. memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
  3763. btrfs_set_header_bytenr(right, right->start);
  3764. btrfs_set_header_generation(right, trans->transid);
  3765. btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
  3766. btrfs_set_header_owner(right, root->root_key.objectid);
  3767. btrfs_set_header_level(right, 0);
  3768. write_extent_buffer(right, root->fs_info->fsid,
  3769. btrfs_header_fsid(), BTRFS_FSID_SIZE);
  3770. write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
  3771. btrfs_header_chunk_tree_uuid(right),
  3772. BTRFS_UUID_SIZE);
  3773. if (split == 0) {
  3774. if (mid <= slot) {
  3775. btrfs_set_header_nritems(right, 0);
  3776. insert_ptr(trans, root, path, &disk_key, right->start,
  3777. path->slots[1] + 1, 1);
  3778. btrfs_tree_unlock(path->nodes[0]);
  3779. free_extent_buffer(path->nodes[0]);
  3780. path->nodes[0] = right;
  3781. path->slots[0] = 0;
  3782. path->slots[1] += 1;
  3783. } else {
  3784. btrfs_set_header_nritems(right, 0);
  3785. insert_ptr(trans, root, path, &disk_key, right->start,
  3786. path->slots[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. if (path->slots[1] == 0)
  3792. fixup_low_keys(root, path, &disk_key, 1);
  3793. }
  3794. btrfs_mark_buffer_dirty(right);
  3795. return ret;
  3796. }
  3797. copy_for_split(trans, root, path, l, right, slot, mid, nritems);
  3798. if (split == 2) {
  3799. BUG_ON(num_doubles != 0);
  3800. num_doubles++;
  3801. goto again;
  3802. }
  3803. return 0;
  3804. push_for_double:
  3805. push_for_double_split(trans, root, path, data_size);
  3806. tried_avoid_double = 1;
  3807. if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
  3808. return 0;
  3809. goto again;
  3810. }
  3811. static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
  3812. struct btrfs_root *root,
  3813. struct btrfs_path *path, int ins_len)
  3814. {
  3815. struct btrfs_key key;
  3816. struct extent_buffer *leaf;
  3817. struct btrfs_file_extent_item *fi;
  3818. u64 extent_len = 0;
  3819. u32 item_size;
  3820. int ret;
  3821. leaf = path->nodes[0];
  3822. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3823. BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
  3824. key.type != BTRFS_EXTENT_CSUM_KEY);
  3825. if (btrfs_leaf_free_space(root, leaf) >= ins_len)
  3826. return 0;
  3827. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3828. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3829. fi = btrfs_item_ptr(leaf, path->slots[0],
  3830. struct btrfs_file_extent_item);
  3831. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  3832. }
  3833. btrfs_release_path(path);
  3834. path->keep_locks = 1;
  3835. path->search_for_split = 1;
  3836. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3837. path->search_for_split = 0;
  3838. if (ret < 0)
  3839. goto err;
  3840. ret = -EAGAIN;
  3841. leaf = path->nodes[0];
  3842. /* if our item isn't there or got smaller, return now */
  3843. if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
  3844. goto err;
  3845. /* the leaf has changed, it now has room. return now */
  3846. if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
  3847. goto err;
  3848. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3849. fi = btrfs_item_ptr(leaf, path->slots[0],
  3850. struct btrfs_file_extent_item);
  3851. if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
  3852. goto err;
  3853. }
  3854. btrfs_set_path_blocking(path);
  3855. ret = split_leaf(trans, root, &key, path, ins_len, 1);
  3856. if (ret)
  3857. goto err;
  3858. path->keep_locks = 0;
  3859. btrfs_unlock_up_safe(path, 1);
  3860. return 0;
  3861. err:
  3862. path->keep_locks = 0;
  3863. return ret;
  3864. }
  3865. static noinline int split_item(struct btrfs_trans_handle *trans,
  3866. struct btrfs_root *root,
  3867. struct btrfs_path *path,
  3868. struct btrfs_key *new_key,
  3869. unsigned long split_offset)
  3870. {
  3871. struct extent_buffer *leaf;
  3872. struct btrfs_item *item;
  3873. struct btrfs_item *new_item;
  3874. int slot;
  3875. char *buf;
  3876. u32 nritems;
  3877. u32 item_size;
  3878. u32 orig_offset;
  3879. struct btrfs_disk_key disk_key;
  3880. leaf = path->nodes[0];
  3881. BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
  3882. btrfs_set_path_blocking(path);
  3883. item = btrfs_item_nr(path->slots[0]);
  3884. orig_offset = btrfs_item_offset(leaf, item);
  3885. item_size = btrfs_item_size(leaf, item);
  3886. buf = kmalloc(item_size, GFP_NOFS);
  3887. if (!buf)
  3888. return -ENOMEM;
  3889. read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
  3890. path->slots[0]), item_size);
  3891. slot = path->slots[0] + 1;
  3892. nritems = btrfs_header_nritems(leaf);
  3893. if (slot != nritems) {
  3894. /* shift the items */
  3895. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
  3896. btrfs_item_nr_offset(slot),
  3897. (nritems - slot) * sizeof(struct btrfs_item));
  3898. }
  3899. btrfs_cpu_key_to_disk(&disk_key, new_key);
  3900. btrfs_set_item_key(leaf, &disk_key, slot);
  3901. new_item = btrfs_item_nr(slot);
  3902. btrfs_set_item_offset(leaf, new_item, orig_offset);
  3903. btrfs_set_item_size(leaf, new_item, item_size - split_offset);
  3904. btrfs_set_item_offset(leaf, item,
  3905. orig_offset + item_size - split_offset);
  3906. btrfs_set_item_size(leaf, item, split_offset);
  3907. btrfs_set_header_nritems(leaf, nritems + 1);
  3908. /* write the data for the start of the original item */
  3909. write_extent_buffer(leaf, buf,
  3910. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3911. split_offset);
  3912. /* write the data for the new item */
  3913. write_extent_buffer(leaf, buf + split_offset,
  3914. btrfs_item_ptr_offset(leaf, slot),
  3915. item_size - split_offset);
  3916. btrfs_mark_buffer_dirty(leaf);
  3917. BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
  3918. kfree(buf);
  3919. return 0;
  3920. }
  3921. /*
  3922. * This function splits a single item into two items,
  3923. * giving 'new_key' to the new item and splitting the
  3924. * old one at split_offset (from the start of the item).
  3925. *
  3926. * The path may be released by this operation. After
  3927. * the split, the path is pointing to the old item. The
  3928. * new item is going to be in the same node as the old one.
  3929. *
  3930. * Note, the item being split must be smaller enough to live alone on
  3931. * a tree block with room for one extra struct btrfs_item
  3932. *
  3933. * This allows us to split the item in place, keeping a lock on the
  3934. * leaf the entire time.
  3935. */
  3936. int btrfs_split_item(struct btrfs_trans_handle *trans,
  3937. struct btrfs_root *root,
  3938. struct btrfs_path *path,
  3939. struct btrfs_key *new_key,
  3940. unsigned long split_offset)
  3941. {
  3942. int ret;
  3943. ret = setup_leaf_for_split(trans, root, path,
  3944. sizeof(struct btrfs_item));
  3945. if (ret)
  3946. return ret;
  3947. ret = split_item(trans, root, path, new_key, split_offset);
  3948. return ret;
  3949. }
  3950. /*
  3951. * This function duplicate a item, giving 'new_key' to the new item.
  3952. * It guarantees both items live in the same tree leaf and the new item
  3953. * is contiguous with the original item.
  3954. *
  3955. * This allows us to split file extent in place, keeping a lock on the
  3956. * leaf the entire time.
  3957. */
  3958. int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
  3959. struct btrfs_root *root,
  3960. struct btrfs_path *path,
  3961. struct btrfs_key *new_key)
  3962. {
  3963. struct extent_buffer *leaf;
  3964. int ret;
  3965. u32 item_size;
  3966. leaf = path->nodes[0];
  3967. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3968. ret = setup_leaf_for_split(trans, root, path,
  3969. item_size + sizeof(struct btrfs_item));
  3970. if (ret)
  3971. return ret;
  3972. path->slots[0]++;
  3973. setup_items_for_insert(root, path, new_key, &item_size,
  3974. item_size, item_size +
  3975. sizeof(struct btrfs_item), 1);
  3976. leaf = path->nodes[0];
  3977. memcpy_extent_buffer(leaf,
  3978. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3979. btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
  3980. item_size);
  3981. return 0;
  3982. }
  3983. /*
  3984. * make the item pointed to by the path smaller. new_size indicates
  3985. * how small to make it, and from_end tells us if we just chop bytes
  3986. * off the end of the item or if we shift the item to chop bytes off
  3987. * the front.
  3988. */
  3989. void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
  3990. u32 new_size, int from_end)
  3991. {
  3992. int slot;
  3993. struct extent_buffer *leaf;
  3994. struct btrfs_item *item;
  3995. u32 nritems;
  3996. unsigned int data_end;
  3997. unsigned int old_data_start;
  3998. unsigned int old_size;
  3999. unsigned int size_diff;
  4000. int i;
  4001. struct btrfs_map_token token;
  4002. btrfs_init_map_token(&token);
  4003. leaf = path->nodes[0];
  4004. slot = path->slots[0];
  4005. old_size = btrfs_item_size_nr(leaf, slot);
  4006. if (old_size == new_size)
  4007. return;
  4008. nritems = btrfs_header_nritems(leaf);
  4009. data_end = leaf_data_end(root, leaf);
  4010. old_data_start = btrfs_item_offset_nr(leaf, slot);
  4011. size_diff = old_size - new_size;
  4012. BUG_ON(slot < 0);
  4013. BUG_ON(slot >= nritems);
  4014. /*
  4015. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4016. */
  4017. /* first correct the data pointers */
  4018. for (i = slot; i < nritems; i++) {
  4019. u32 ioff;
  4020. item = btrfs_item_nr(i);
  4021. ioff = btrfs_token_item_offset(leaf, item, &token);
  4022. btrfs_set_token_item_offset(leaf, item,
  4023. ioff + size_diff, &token);
  4024. }
  4025. /* shift the data */
  4026. if (from_end) {
  4027. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4028. data_end + size_diff, btrfs_leaf_data(leaf) +
  4029. data_end, old_data_start + new_size - data_end);
  4030. } else {
  4031. struct btrfs_disk_key disk_key;
  4032. u64 offset;
  4033. btrfs_item_key(leaf, &disk_key, slot);
  4034. if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
  4035. unsigned long ptr;
  4036. struct btrfs_file_extent_item *fi;
  4037. fi = btrfs_item_ptr(leaf, slot,
  4038. struct btrfs_file_extent_item);
  4039. fi = (struct btrfs_file_extent_item *)(
  4040. (unsigned long)fi - size_diff);
  4041. if (btrfs_file_extent_type(leaf, fi) ==
  4042. BTRFS_FILE_EXTENT_INLINE) {
  4043. ptr = btrfs_item_ptr_offset(leaf, slot);
  4044. memmove_extent_buffer(leaf, ptr,
  4045. (unsigned long)fi,
  4046. BTRFS_FILE_EXTENT_INLINE_DATA_START);
  4047. }
  4048. }
  4049. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4050. data_end + size_diff, btrfs_leaf_data(leaf) +
  4051. data_end, old_data_start - data_end);
  4052. offset = btrfs_disk_key_offset(&disk_key);
  4053. btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
  4054. btrfs_set_item_key(leaf, &disk_key, slot);
  4055. if (slot == 0)
  4056. fixup_low_keys(root, path, &disk_key, 1);
  4057. }
  4058. item = btrfs_item_nr(slot);
  4059. btrfs_set_item_size(leaf, item, new_size);
  4060. btrfs_mark_buffer_dirty(leaf);
  4061. if (btrfs_leaf_free_space(root, leaf) < 0) {
  4062. btrfs_print_leaf(root, leaf);
  4063. BUG();
  4064. }
  4065. }
  4066. /*
  4067. * make the item pointed to by the path bigger, data_size is the added size.
  4068. */
  4069. void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
  4070. u32 data_size)
  4071. {
  4072. int slot;
  4073. struct extent_buffer *leaf;
  4074. struct btrfs_item *item;
  4075. u32 nritems;
  4076. unsigned int data_end;
  4077. unsigned int old_data;
  4078. unsigned int old_size;
  4079. int i;
  4080. struct btrfs_map_token token;
  4081. btrfs_init_map_token(&token);
  4082. leaf = path->nodes[0];
  4083. nritems = btrfs_header_nritems(leaf);
  4084. data_end = leaf_data_end(root, leaf);
  4085. if (btrfs_leaf_free_space(root, leaf) < data_size) {
  4086. btrfs_print_leaf(root, leaf);
  4087. BUG();
  4088. }
  4089. slot = path->slots[0];
  4090. old_data = btrfs_item_end_nr(leaf, slot);
  4091. BUG_ON(slot < 0);
  4092. if (slot >= nritems) {
  4093. btrfs_print_leaf(root, leaf);
  4094. btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
  4095. slot, nritems);
  4096. BUG_ON(1);
  4097. }
  4098. /*
  4099. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4100. */
  4101. /* first correct the data pointers */
  4102. for (i = slot; i < nritems; i++) {
  4103. u32 ioff;
  4104. item = btrfs_item_nr(i);
  4105. ioff = btrfs_token_item_offset(leaf, item, &token);
  4106. btrfs_set_token_item_offset(leaf, item,
  4107. ioff - data_size, &token);
  4108. }
  4109. /* shift the data */
  4110. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4111. data_end - data_size, btrfs_leaf_data(leaf) +
  4112. data_end, old_data - data_end);
  4113. data_end = old_data;
  4114. old_size = btrfs_item_size_nr(leaf, slot);
  4115. item = btrfs_item_nr(slot);
  4116. btrfs_set_item_size(leaf, item, old_size + data_size);
  4117. btrfs_mark_buffer_dirty(leaf);
  4118. if (btrfs_leaf_free_space(root, leaf) < 0) {
  4119. btrfs_print_leaf(root, leaf);
  4120. BUG();
  4121. }
  4122. }
  4123. /*
  4124. * this is a helper for btrfs_insert_empty_items, the main goal here is
  4125. * to save stack depth by doing the bulk of the work in a function
  4126. * that doesn't call btrfs_search_slot
  4127. */
  4128. void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
  4129. struct btrfs_key *cpu_key, u32 *data_size,
  4130. u32 total_data, u32 total_size, int nr)
  4131. {
  4132. struct btrfs_item *item;
  4133. int i;
  4134. u32 nritems;
  4135. unsigned int data_end;
  4136. struct btrfs_disk_key disk_key;
  4137. struct extent_buffer *leaf;
  4138. int slot;
  4139. struct btrfs_map_token token;
  4140. if (path->slots[0] == 0) {
  4141. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  4142. fixup_low_keys(root, path, &disk_key, 1);
  4143. }
  4144. btrfs_unlock_up_safe(path, 1);
  4145. btrfs_init_map_token(&token);
  4146. leaf = path->nodes[0];
  4147. slot = path->slots[0];
  4148. nritems = btrfs_header_nritems(leaf);
  4149. data_end = leaf_data_end(root, leaf);
  4150. if (btrfs_leaf_free_space(root, leaf) < total_size) {
  4151. btrfs_print_leaf(root, leaf);
  4152. btrfs_crit(root->fs_info, "not enough freespace need %u have %d",
  4153. total_size, btrfs_leaf_free_space(root, leaf));
  4154. BUG();
  4155. }
  4156. if (slot != nritems) {
  4157. unsigned int old_data = btrfs_item_end_nr(leaf, slot);
  4158. if (old_data < data_end) {
  4159. btrfs_print_leaf(root, leaf);
  4160. btrfs_crit(root->fs_info, "slot %d old_data %d data_end %d",
  4161. slot, old_data, data_end);
  4162. BUG_ON(1);
  4163. }
  4164. /*
  4165. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4166. */
  4167. /* first correct the data pointers */
  4168. for (i = slot; i < nritems; i++) {
  4169. u32 ioff;
  4170. item = btrfs_item_nr( i);
  4171. ioff = btrfs_token_item_offset(leaf, item, &token);
  4172. btrfs_set_token_item_offset(leaf, item,
  4173. ioff - total_data, &token);
  4174. }
  4175. /* shift the items */
  4176. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
  4177. btrfs_item_nr_offset(slot),
  4178. (nritems - slot) * sizeof(struct btrfs_item));
  4179. /* shift the data */
  4180. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4181. data_end - total_data, btrfs_leaf_data(leaf) +
  4182. data_end, old_data - data_end);
  4183. data_end = old_data;
  4184. }
  4185. /* setup the item for the new data */
  4186. for (i = 0; i < nr; i++) {
  4187. btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
  4188. btrfs_set_item_key(leaf, &disk_key, slot + i);
  4189. item = btrfs_item_nr(slot + i);
  4190. btrfs_set_token_item_offset(leaf, item,
  4191. data_end - data_size[i], &token);
  4192. data_end -= data_size[i];
  4193. btrfs_set_token_item_size(leaf, item, data_size[i], &token);
  4194. }
  4195. btrfs_set_header_nritems(leaf, nritems + nr);
  4196. btrfs_mark_buffer_dirty(leaf);
  4197. if (btrfs_leaf_free_space(root, leaf) < 0) {
  4198. btrfs_print_leaf(root, leaf);
  4199. BUG();
  4200. }
  4201. }
  4202. /*
  4203. * Given a key and some data, insert items into the tree.
  4204. * This does all the path init required, making room in the tree if needed.
  4205. */
  4206. int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
  4207. struct btrfs_root *root,
  4208. struct btrfs_path *path,
  4209. struct btrfs_key *cpu_key, u32 *data_size,
  4210. int nr)
  4211. {
  4212. int ret = 0;
  4213. int slot;
  4214. int i;
  4215. u32 total_size = 0;
  4216. u32 total_data = 0;
  4217. for (i = 0; i < nr; i++)
  4218. total_data += data_size[i];
  4219. total_size = total_data + (nr * sizeof(struct btrfs_item));
  4220. ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
  4221. if (ret == 0)
  4222. return -EEXIST;
  4223. if (ret < 0)
  4224. return ret;
  4225. slot = path->slots[0];
  4226. BUG_ON(slot < 0);
  4227. setup_items_for_insert(root, path, cpu_key, data_size,
  4228. total_data, total_size, nr);
  4229. return 0;
  4230. }
  4231. /*
  4232. * Given a key and some data, insert an item into the tree.
  4233. * This does all the path init required, making room in the tree if needed.
  4234. */
  4235. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  4236. *root, struct btrfs_key *cpu_key, void *data, u32
  4237. data_size)
  4238. {
  4239. int ret = 0;
  4240. struct btrfs_path *path;
  4241. struct extent_buffer *leaf;
  4242. unsigned long ptr;
  4243. path = btrfs_alloc_path();
  4244. if (!path)
  4245. return -ENOMEM;
  4246. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  4247. if (!ret) {
  4248. leaf = path->nodes[0];
  4249. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  4250. write_extent_buffer(leaf, data, ptr, data_size);
  4251. btrfs_mark_buffer_dirty(leaf);
  4252. }
  4253. btrfs_free_path(path);
  4254. return ret;
  4255. }
  4256. /*
  4257. * delete the pointer from a given node.
  4258. *
  4259. * the tree should have been previously balanced so the deletion does not
  4260. * empty a node.
  4261. */
  4262. static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
  4263. int level, int slot)
  4264. {
  4265. struct extent_buffer *parent = path->nodes[level];
  4266. u32 nritems;
  4267. int ret;
  4268. nritems = btrfs_header_nritems(parent);
  4269. if (slot != nritems - 1) {
  4270. if (level)
  4271. tree_mod_log_eb_move(root->fs_info, parent, slot,
  4272. slot + 1, nritems - slot - 1);
  4273. memmove_extent_buffer(parent,
  4274. btrfs_node_key_ptr_offset(slot),
  4275. btrfs_node_key_ptr_offset(slot + 1),
  4276. sizeof(struct btrfs_key_ptr) *
  4277. (nritems - slot - 1));
  4278. } else if (level) {
  4279. ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
  4280. MOD_LOG_KEY_REMOVE, GFP_NOFS);
  4281. BUG_ON(ret < 0);
  4282. }
  4283. nritems--;
  4284. btrfs_set_header_nritems(parent, nritems);
  4285. if (nritems == 0 && parent == root->node) {
  4286. BUG_ON(btrfs_header_level(root->node) != 1);
  4287. /* just turn the root into a leaf and break */
  4288. btrfs_set_header_level(root->node, 0);
  4289. } else if (slot == 0) {
  4290. struct btrfs_disk_key disk_key;
  4291. btrfs_node_key(parent, &disk_key, 0);
  4292. fixup_low_keys(root, path, &disk_key, level + 1);
  4293. }
  4294. btrfs_mark_buffer_dirty(parent);
  4295. }
  4296. /*
  4297. * a helper function to delete the leaf pointed to by path->slots[1] and
  4298. * path->nodes[1].
  4299. *
  4300. * This deletes the pointer in path->nodes[1] and frees the leaf
  4301. * block extent. zero is returned if it all worked out, < 0 otherwise.
  4302. *
  4303. * The path must have already been setup for deleting the leaf, including
  4304. * all the proper balancing. path->nodes[1] must be locked.
  4305. */
  4306. static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
  4307. struct btrfs_root *root,
  4308. struct btrfs_path *path,
  4309. struct extent_buffer *leaf)
  4310. {
  4311. WARN_ON(btrfs_header_generation(leaf) != trans->transid);
  4312. del_ptr(root, path, 1, path->slots[1]);
  4313. /*
  4314. * btrfs_free_extent is expensive, we want to make sure we
  4315. * aren't holding any locks when we call it
  4316. */
  4317. btrfs_unlock_up_safe(path, 0);
  4318. root_sub_used(root, leaf->len);
  4319. extent_buffer_get(leaf);
  4320. btrfs_free_tree_block(trans, root, leaf, 0, 1);
  4321. free_extent_buffer_stale(leaf);
  4322. }
  4323. /*
  4324. * delete the item at the leaf level in path. If that empties
  4325. * the leaf, remove it from the tree
  4326. */
  4327. int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4328. struct btrfs_path *path, int slot, int nr)
  4329. {
  4330. struct extent_buffer *leaf;
  4331. struct btrfs_item *item;
  4332. int last_off;
  4333. int dsize = 0;
  4334. int ret = 0;
  4335. int wret;
  4336. int i;
  4337. u32 nritems;
  4338. struct btrfs_map_token token;
  4339. btrfs_init_map_token(&token);
  4340. leaf = path->nodes[0];
  4341. last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
  4342. for (i = 0; i < nr; i++)
  4343. dsize += btrfs_item_size_nr(leaf, slot + i);
  4344. nritems = btrfs_header_nritems(leaf);
  4345. if (slot + nr != nritems) {
  4346. int data_end = leaf_data_end(root, leaf);
  4347. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4348. data_end + dsize,
  4349. btrfs_leaf_data(leaf) + data_end,
  4350. last_off - data_end);
  4351. for (i = slot + nr; i < nritems; i++) {
  4352. u32 ioff;
  4353. item = btrfs_item_nr(i);
  4354. ioff = btrfs_token_item_offset(leaf, item, &token);
  4355. btrfs_set_token_item_offset(leaf, item,
  4356. ioff + dsize, &token);
  4357. }
  4358. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
  4359. btrfs_item_nr_offset(slot + nr),
  4360. sizeof(struct btrfs_item) *
  4361. (nritems - slot - nr));
  4362. }
  4363. btrfs_set_header_nritems(leaf, nritems - nr);
  4364. nritems -= nr;
  4365. /* delete the leaf if we've emptied it */
  4366. if (nritems == 0) {
  4367. if (leaf == root->node) {
  4368. btrfs_set_header_level(leaf, 0);
  4369. } else {
  4370. btrfs_set_path_blocking(path);
  4371. clean_tree_block(trans, root, leaf);
  4372. btrfs_del_leaf(trans, root, path, leaf);
  4373. }
  4374. } else {
  4375. int used = leaf_space_used(leaf, 0, nritems);
  4376. if (slot == 0) {
  4377. struct btrfs_disk_key disk_key;
  4378. btrfs_item_key(leaf, &disk_key, 0);
  4379. fixup_low_keys(root, path, &disk_key, 1);
  4380. }
  4381. /* delete the leaf if it is mostly empty */
  4382. if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
  4383. /* push_leaf_left fixes the path.
  4384. * make sure the path still points to our leaf
  4385. * for possible call to del_ptr below
  4386. */
  4387. slot = path->slots[1];
  4388. extent_buffer_get(leaf);
  4389. btrfs_set_path_blocking(path);
  4390. wret = push_leaf_left(trans, root, path, 1, 1,
  4391. 1, (u32)-1);
  4392. if (wret < 0 && wret != -ENOSPC)
  4393. ret = wret;
  4394. if (path->nodes[0] == leaf &&
  4395. btrfs_header_nritems(leaf)) {
  4396. wret = push_leaf_right(trans, root, path, 1,
  4397. 1, 1, 0);
  4398. if (wret < 0 && wret != -ENOSPC)
  4399. ret = wret;
  4400. }
  4401. if (btrfs_header_nritems(leaf) == 0) {
  4402. path->slots[1] = slot;
  4403. btrfs_del_leaf(trans, root, path, leaf);
  4404. free_extent_buffer(leaf);
  4405. ret = 0;
  4406. } else {
  4407. /* if we're still in the path, make sure
  4408. * we're dirty. Otherwise, one of the
  4409. * push_leaf functions must have already
  4410. * dirtied this buffer
  4411. */
  4412. if (path->nodes[0] == leaf)
  4413. btrfs_mark_buffer_dirty(leaf);
  4414. free_extent_buffer(leaf);
  4415. }
  4416. } else {
  4417. btrfs_mark_buffer_dirty(leaf);
  4418. }
  4419. }
  4420. return ret;
  4421. }
  4422. /*
  4423. * search the tree again to find a leaf with lesser keys
  4424. * returns 0 if it found something or 1 if there are no lesser leaves.
  4425. * returns < 0 on io errors.
  4426. *
  4427. * This may release the path, and so you may lose any locks held at the
  4428. * time you call it.
  4429. */
  4430. int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
  4431. {
  4432. struct btrfs_key key;
  4433. struct btrfs_disk_key found_key;
  4434. int ret;
  4435. btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
  4436. if (key.offset > 0) {
  4437. key.offset--;
  4438. } else if (key.type > 0) {
  4439. key.type--;
  4440. key.offset = (u64)-1;
  4441. } else if (key.objectid > 0) {
  4442. key.objectid--;
  4443. key.type = (u8)-1;
  4444. key.offset = (u64)-1;
  4445. } else {
  4446. return 1;
  4447. }
  4448. btrfs_release_path(path);
  4449. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4450. if (ret < 0)
  4451. return ret;
  4452. btrfs_item_key(path->nodes[0], &found_key, 0);
  4453. ret = comp_keys(&found_key, &key);
  4454. /*
  4455. * We might have had an item with the previous key in the tree right
  4456. * before we released our path. And after we released our path, that
  4457. * item might have been pushed to the first slot (0) of the leaf we
  4458. * were holding due to a tree balance. Alternatively, an item with the
  4459. * previous key can exist as the only element of a leaf (big fat item).
  4460. * Therefore account for these 2 cases, so that our callers (like
  4461. * btrfs_previous_item) don't miss an existing item with a key matching
  4462. * the previous key we computed above.
  4463. */
  4464. if (ret <= 0)
  4465. return 0;
  4466. return 1;
  4467. }
  4468. /*
  4469. * A helper function to walk down the tree starting at min_key, and looking
  4470. * for nodes or leaves that are have a minimum transaction id.
  4471. * This is used by the btree defrag code, and tree logging
  4472. *
  4473. * This does not cow, but it does stuff the starting key it finds back
  4474. * into min_key, so you can call btrfs_search_slot with cow=1 on the
  4475. * key and get a writable path.
  4476. *
  4477. * This does lock as it descends, and path->keep_locks should be set
  4478. * to 1 by the caller.
  4479. *
  4480. * This honors path->lowest_level to prevent descent past a given level
  4481. * of the tree.
  4482. *
  4483. * min_trans indicates the oldest transaction that you are interested
  4484. * in walking through. Any nodes or leaves older than min_trans are
  4485. * skipped over (without reading them).
  4486. *
  4487. * returns zero if something useful was found, < 0 on error and 1 if there
  4488. * was nothing in the tree that matched the search criteria.
  4489. */
  4490. int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
  4491. struct btrfs_path *path,
  4492. u64 min_trans)
  4493. {
  4494. struct extent_buffer *cur;
  4495. struct btrfs_key found_key;
  4496. int slot;
  4497. int sret;
  4498. u32 nritems;
  4499. int level;
  4500. int ret = 1;
  4501. int keep_locks = path->keep_locks;
  4502. path->keep_locks = 1;
  4503. again:
  4504. cur = btrfs_read_lock_root_node(root);
  4505. level = btrfs_header_level(cur);
  4506. WARN_ON(path->nodes[level]);
  4507. path->nodes[level] = cur;
  4508. path->locks[level] = BTRFS_READ_LOCK;
  4509. if (btrfs_header_generation(cur) < min_trans) {
  4510. ret = 1;
  4511. goto out;
  4512. }
  4513. while (1) {
  4514. nritems = btrfs_header_nritems(cur);
  4515. level = btrfs_header_level(cur);
  4516. sret = bin_search(cur, min_key, level, &slot);
  4517. /* at the lowest level, we're done, setup the path and exit */
  4518. if (level == path->lowest_level) {
  4519. if (slot >= nritems)
  4520. goto find_next_key;
  4521. ret = 0;
  4522. path->slots[level] = slot;
  4523. btrfs_item_key_to_cpu(cur, &found_key, slot);
  4524. goto out;
  4525. }
  4526. if (sret && slot > 0)
  4527. slot--;
  4528. /*
  4529. * check this node pointer against the min_trans parameters.
  4530. * If it is too old, old, skip to the next one.
  4531. */
  4532. while (slot < nritems) {
  4533. u64 gen;
  4534. gen = btrfs_node_ptr_generation(cur, slot);
  4535. if (gen < min_trans) {
  4536. slot++;
  4537. continue;
  4538. }
  4539. break;
  4540. }
  4541. find_next_key:
  4542. /*
  4543. * we didn't find a candidate key in this node, walk forward
  4544. * and find another one
  4545. */
  4546. if (slot >= nritems) {
  4547. path->slots[level] = slot;
  4548. btrfs_set_path_blocking(path);
  4549. sret = btrfs_find_next_key(root, path, min_key, level,
  4550. min_trans);
  4551. if (sret == 0) {
  4552. btrfs_release_path(path);
  4553. goto again;
  4554. } else {
  4555. goto out;
  4556. }
  4557. }
  4558. /* save our key for returning back */
  4559. btrfs_node_key_to_cpu(cur, &found_key, slot);
  4560. path->slots[level] = slot;
  4561. if (level == path->lowest_level) {
  4562. ret = 0;
  4563. goto out;
  4564. }
  4565. btrfs_set_path_blocking(path);
  4566. cur = read_node_slot(root, cur, slot);
  4567. BUG_ON(!cur); /* -ENOMEM */
  4568. btrfs_tree_read_lock(cur);
  4569. path->locks[level - 1] = BTRFS_READ_LOCK;
  4570. path->nodes[level - 1] = cur;
  4571. unlock_up(path, level, 1, 0, NULL);
  4572. btrfs_clear_path_blocking(path, NULL, 0);
  4573. }
  4574. out:
  4575. path->keep_locks = keep_locks;
  4576. if (ret == 0) {
  4577. btrfs_unlock_up_safe(path, path->lowest_level + 1);
  4578. btrfs_set_path_blocking(path);
  4579. memcpy(min_key, &found_key, sizeof(found_key));
  4580. }
  4581. return ret;
  4582. }
  4583. static void tree_move_down(struct btrfs_root *root,
  4584. struct btrfs_path *path,
  4585. int *level, int root_level)
  4586. {
  4587. BUG_ON(*level == 0);
  4588. path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
  4589. path->slots[*level]);
  4590. path->slots[*level - 1] = 0;
  4591. (*level)--;
  4592. }
  4593. static int tree_move_next_or_upnext(struct btrfs_root *root,
  4594. struct btrfs_path *path,
  4595. int *level, int root_level)
  4596. {
  4597. int ret = 0;
  4598. int nritems;
  4599. nritems = btrfs_header_nritems(path->nodes[*level]);
  4600. path->slots[*level]++;
  4601. while (path->slots[*level] >= nritems) {
  4602. if (*level == root_level)
  4603. return -1;
  4604. /* move upnext */
  4605. path->slots[*level] = 0;
  4606. free_extent_buffer(path->nodes[*level]);
  4607. path->nodes[*level] = NULL;
  4608. (*level)++;
  4609. path->slots[*level]++;
  4610. nritems = btrfs_header_nritems(path->nodes[*level]);
  4611. ret = 1;
  4612. }
  4613. return ret;
  4614. }
  4615. /*
  4616. * Returns 1 if it had to move up and next. 0 is returned if it moved only next
  4617. * or down.
  4618. */
  4619. static int tree_advance(struct btrfs_root *root,
  4620. struct btrfs_path *path,
  4621. int *level, int root_level,
  4622. int allow_down,
  4623. struct btrfs_key *key)
  4624. {
  4625. int ret;
  4626. if (*level == 0 || !allow_down) {
  4627. ret = tree_move_next_or_upnext(root, path, level, root_level);
  4628. } else {
  4629. tree_move_down(root, path, level, root_level);
  4630. ret = 0;
  4631. }
  4632. if (ret >= 0) {
  4633. if (*level == 0)
  4634. btrfs_item_key_to_cpu(path->nodes[*level], key,
  4635. path->slots[*level]);
  4636. else
  4637. btrfs_node_key_to_cpu(path->nodes[*level], key,
  4638. path->slots[*level]);
  4639. }
  4640. return ret;
  4641. }
  4642. static int tree_compare_item(struct btrfs_root *left_root,
  4643. struct btrfs_path *left_path,
  4644. struct btrfs_path *right_path,
  4645. char *tmp_buf)
  4646. {
  4647. int cmp;
  4648. int len1, len2;
  4649. unsigned long off1, off2;
  4650. len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
  4651. len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
  4652. if (len1 != len2)
  4653. return 1;
  4654. off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
  4655. off2 = btrfs_item_ptr_offset(right_path->nodes[0],
  4656. right_path->slots[0]);
  4657. read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
  4658. cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
  4659. if (cmp)
  4660. return 1;
  4661. return 0;
  4662. }
  4663. #define ADVANCE 1
  4664. #define ADVANCE_ONLY_NEXT -1
  4665. /*
  4666. * This function compares two trees and calls the provided callback for
  4667. * every changed/new/deleted item it finds.
  4668. * If shared tree blocks are encountered, whole subtrees are skipped, making
  4669. * the compare pretty fast on snapshotted subvolumes.
  4670. *
  4671. * This currently works on commit roots only. As commit roots are read only,
  4672. * we don't do any locking. The commit roots are protected with transactions.
  4673. * Transactions are ended and rejoined when a commit is tried in between.
  4674. *
  4675. * This function checks for modifications done to the trees while comparing.
  4676. * If it detects a change, it aborts immediately.
  4677. */
  4678. int btrfs_compare_trees(struct btrfs_root *left_root,
  4679. struct btrfs_root *right_root,
  4680. btrfs_changed_cb_t changed_cb, void *ctx)
  4681. {
  4682. int ret;
  4683. int cmp;
  4684. struct btrfs_path *left_path = NULL;
  4685. struct btrfs_path *right_path = NULL;
  4686. struct btrfs_key left_key;
  4687. struct btrfs_key right_key;
  4688. char *tmp_buf = NULL;
  4689. int left_root_level;
  4690. int right_root_level;
  4691. int left_level;
  4692. int right_level;
  4693. int left_end_reached;
  4694. int right_end_reached;
  4695. int advance_left;
  4696. int advance_right;
  4697. u64 left_blockptr;
  4698. u64 right_blockptr;
  4699. u64 left_gen;
  4700. u64 right_gen;
  4701. left_path = btrfs_alloc_path();
  4702. if (!left_path) {
  4703. ret = -ENOMEM;
  4704. goto out;
  4705. }
  4706. right_path = btrfs_alloc_path();
  4707. if (!right_path) {
  4708. ret = -ENOMEM;
  4709. goto out;
  4710. }
  4711. tmp_buf = kmalloc(left_root->nodesize, GFP_NOFS);
  4712. if (!tmp_buf) {
  4713. ret = -ENOMEM;
  4714. goto out;
  4715. }
  4716. left_path->search_commit_root = 1;
  4717. left_path->skip_locking = 1;
  4718. right_path->search_commit_root = 1;
  4719. right_path->skip_locking = 1;
  4720. /*
  4721. * Strategy: Go to the first items of both trees. Then do
  4722. *
  4723. * If both trees are at level 0
  4724. * Compare keys of current items
  4725. * If left < right treat left item as new, advance left tree
  4726. * and repeat
  4727. * If left > right treat right item as deleted, advance right tree
  4728. * and repeat
  4729. * If left == right do deep compare of items, treat as changed if
  4730. * needed, advance both trees and repeat
  4731. * If both trees are at the same level but not at level 0
  4732. * Compare keys of current nodes/leafs
  4733. * If left < right advance left tree and repeat
  4734. * If left > right advance right tree and repeat
  4735. * If left == right compare blockptrs of the next nodes/leafs
  4736. * If they match advance both trees but stay at the same level
  4737. * and repeat
  4738. * If they don't match advance both trees while allowing to go
  4739. * deeper and repeat
  4740. * If tree levels are different
  4741. * Advance the tree that needs it and repeat
  4742. *
  4743. * Advancing a tree means:
  4744. * If we are at level 0, try to go to the next slot. If that's not
  4745. * possible, go one level up and repeat. Stop when we found a level
  4746. * where we could go to the next slot. We may at this point be on a
  4747. * node or a leaf.
  4748. *
  4749. * If we are not at level 0 and not on shared tree blocks, go one
  4750. * level deeper.
  4751. *
  4752. * If we are not at level 0 and on shared tree blocks, go one slot to
  4753. * the right if possible or go up and right.
  4754. */
  4755. down_read(&left_root->fs_info->commit_root_sem);
  4756. left_level = btrfs_header_level(left_root->commit_root);
  4757. left_root_level = left_level;
  4758. left_path->nodes[left_level] = left_root->commit_root;
  4759. extent_buffer_get(left_path->nodes[left_level]);
  4760. right_level = btrfs_header_level(right_root->commit_root);
  4761. right_root_level = right_level;
  4762. right_path->nodes[right_level] = right_root->commit_root;
  4763. extent_buffer_get(right_path->nodes[right_level]);
  4764. up_read(&left_root->fs_info->commit_root_sem);
  4765. if (left_level == 0)
  4766. btrfs_item_key_to_cpu(left_path->nodes[left_level],
  4767. &left_key, left_path->slots[left_level]);
  4768. else
  4769. btrfs_node_key_to_cpu(left_path->nodes[left_level],
  4770. &left_key, left_path->slots[left_level]);
  4771. if (right_level == 0)
  4772. btrfs_item_key_to_cpu(right_path->nodes[right_level],
  4773. &right_key, right_path->slots[right_level]);
  4774. else
  4775. btrfs_node_key_to_cpu(right_path->nodes[right_level],
  4776. &right_key, right_path->slots[right_level]);
  4777. left_end_reached = right_end_reached = 0;
  4778. advance_left = advance_right = 0;
  4779. while (1) {
  4780. if (advance_left && !left_end_reached) {
  4781. ret = tree_advance(left_root, left_path, &left_level,
  4782. left_root_level,
  4783. advance_left != ADVANCE_ONLY_NEXT,
  4784. &left_key);
  4785. if (ret < 0)
  4786. left_end_reached = ADVANCE;
  4787. advance_left = 0;
  4788. }
  4789. if (advance_right && !right_end_reached) {
  4790. ret = tree_advance(right_root, right_path, &right_level,
  4791. right_root_level,
  4792. advance_right != ADVANCE_ONLY_NEXT,
  4793. &right_key);
  4794. if (ret < 0)
  4795. right_end_reached = ADVANCE;
  4796. advance_right = 0;
  4797. }
  4798. if (left_end_reached && right_end_reached) {
  4799. ret = 0;
  4800. goto out;
  4801. } else if (left_end_reached) {
  4802. if (right_level == 0) {
  4803. ret = changed_cb(left_root, right_root,
  4804. left_path, right_path,
  4805. &right_key,
  4806. BTRFS_COMPARE_TREE_DELETED,
  4807. ctx);
  4808. if (ret < 0)
  4809. goto out;
  4810. }
  4811. advance_right = ADVANCE;
  4812. continue;
  4813. } else if (right_end_reached) {
  4814. if (left_level == 0) {
  4815. ret = changed_cb(left_root, right_root,
  4816. left_path, right_path,
  4817. &left_key,
  4818. BTRFS_COMPARE_TREE_NEW,
  4819. ctx);
  4820. if (ret < 0)
  4821. goto out;
  4822. }
  4823. advance_left = ADVANCE;
  4824. continue;
  4825. }
  4826. if (left_level == 0 && right_level == 0) {
  4827. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4828. if (cmp < 0) {
  4829. ret = changed_cb(left_root, right_root,
  4830. left_path, right_path,
  4831. &left_key,
  4832. BTRFS_COMPARE_TREE_NEW,
  4833. ctx);
  4834. if (ret < 0)
  4835. goto out;
  4836. advance_left = ADVANCE;
  4837. } else if (cmp > 0) {
  4838. ret = changed_cb(left_root, right_root,
  4839. left_path, right_path,
  4840. &right_key,
  4841. BTRFS_COMPARE_TREE_DELETED,
  4842. ctx);
  4843. if (ret < 0)
  4844. goto out;
  4845. advance_right = ADVANCE;
  4846. } else {
  4847. enum btrfs_compare_tree_result result;
  4848. WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
  4849. ret = tree_compare_item(left_root, left_path,
  4850. right_path, tmp_buf);
  4851. if (ret)
  4852. result = BTRFS_COMPARE_TREE_CHANGED;
  4853. else
  4854. result = BTRFS_COMPARE_TREE_SAME;
  4855. ret = changed_cb(left_root, right_root,
  4856. left_path, right_path,
  4857. &left_key, result, ctx);
  4858. if (ret < 0)
  4859. goto out;
  4860. advance_left = ADVANCE;
  4861. advance_right = ADVANCE;
  4862. }
  4863. } else if (left_level == right_level) {
  4864. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4865. if (cmp < 0) {
  4866. advance_left = ADVANCE;
  4867. } else if (cmp > 0) {
  4868. advance_right = ADVANCE;
  4869. } else {
  4870. left_blockptr = btrfs_node_blockptr(
  4871. left_path->nodes[left_level],
  4872. left_path->slots[left_level]);
  4873. right_blockptr = btrfs_node_blockptr(
  4874. right_path->nodes[right_level],
  4875. right_path->slots[right_level]);
  4876. left_gen = btrfs_node_ptr_generation(
  4877. left_path->nodes[left_level],
  4878. left_path->slots[left_level]);
  4879. right_gen = btrfs_node_ptr_generation(
  4880. right_path->nodes[right_level],
  4881. right_path->slots[right_level]);
  4882. if (left_blockptr == right_blockptr &&
  4883. left_gen == right_gen) {
  4884. /*
  4885. * As we're on a shared block, don't
  4886. * allow to go deeper.
  4887. */
  4888. advance_left = ADVANCE_ONLY_NEXT;
  4889. advance_right = ADVANCE_ONLY_NEXT;
  4890. } else {
  4891. advance_left = ADVANCE;
  4892. advance_right = ADVANCE;
  4893. }
  4894. }
  4895. } else if (left_level < right_level) {
  4896. advance_right = ADVANCE;
  4897. } else {
  4898. advance_left = ADVANCE;
  4899. }
  4900. }
  4901. out:
  4902. btrfs_free_path(left_path);
  4903. btrfs_free_path(right_path);
  4904. kfree(tmp_buf);
  4905. return ret;
  4906. }
  4907. /*
  4908. * this is similar to btrfs_next_leaf, but does not try to preserve
  4909. * and fixup the path. It looks for and returns the next key in the
  4910. * tree based on the current path and the min_trans parameters.
  4911. *
  4912. * 0 is returned if another key is found, < 0 if there are any errors
  4913. * and 1 is returned if there are no higher keys in the tree
  4914. *
  4915. * path->keep_locks should be set to 1 on the search made before
  4916. * calling this function.
  4917. */
  4918. int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
  4919. struct btrfs_key *key, int level, u64 min_trans)
  4920. {
  4921. int slot;
  4922. struct extent_buffer *c;
  4923. WARN_ON(!path->keep_locks);
  4924. while (level < BTRFS_MAX_LEVEL) {
  4925. if (!path->nodes[level])
  4926. return 1;
  4927. slot = path->slots[level] + 1;
  4928. c = path->nodes[level];
  4929. next:
  4930. if (slot >= btrfs_header_nritems(c)) {
  4931. int ret;
  4932. int orig_lowest;
  4933. struct btrfs_key cur_key;
  4934. if (level + 1 >= BTRFS_MAX_LEVEL ||
  4935. !path->nodes[level + 1])
  4936. return 1;
  4937. if (path->locks[level + 1]) {
  4938. level++;
  4939. continue;
  4940. }
  4941. slot = btrfs_header_nritems(c) - 1;
  4942. if (level == 0)
  4943. btrfs_item_key_to_cpu(c, &cur_key, slot);
  4944. else
  4945. btrfs_node_key_to_cpu(c, &cur_key, slot);
  4946. orig_lowest = path->lowest_level;
  4947. btrfs_release_path(path);
  4948. path->lowest_level = level;
  4949. ret = btrfs_search_slot(NULL, root, &cur_key, path,
  4950. 0, 0);
  4951. path->lowest_level = orig_lowest;
  4952. if (ret < 0)
  4953. return ret;
  4954. c = path->nodes[level];
  4955. slot = path->slots[level];
  4956. if (ret == 0)
  4957. slot++;
  4958. goto next;
  4959. }
  4960. if (level == 0)
  4961. btrfs_item_key_to_cpu(c, key, slot);
  4962. else {
  4963. u64 gen = btrfs_node_ptr_generation(c, slot);
  4964. if (gen < min_trans) {
  4965. slot++;
  4966. goto next;
  4967. }
  4968. btrfs_node_key_to_cpu(c, key, slot);
  4969. }
  4970. return 0;
  4971. }
  4972. return 1;
  4973. }
  4974. /*
  4975. * search the tree again to find a leaf with greater keys
  4976. * returns 0 if it found something or 1 if there are no greater leaves.
  4977. * returns < 0 on io errors.
  4978. */
  4979. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  4980. {
  4981. return btrfs_next_old_leaf(root, path, 0);
  4982. }
  4983. int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
  4984. u64 time_seq)
  4985. {
  4986. int slot;
  4987. int level;
  4988. struct extent_buffer *c;
  4989. struct extent_buffer *next;
  4990. struct btrfs_key key;
  4991. u32 nritems;
  4992. int ret;
  4993. int old_spinning = path->leave_spinning;
  4994. int next_rw_lock = 0;
  4995. nritems = btrfs_header_nritems(path->nodes[0]);
  4996. if (nritems == 0)
  4997. return 1;
  4998. btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
  4999. again:
  5000. level = 1;
  5001. next = NULL;
  5002. next_rw_lock = 0;
  5003. btrfs_release_path(path);
  5004. path->keep_locks = 1;
  5005. path->leave_spinning = 1;
  5006. if (time_seq)
  5007. ret = btrfs_search_old_slot(root, &key, path, time_seq);
  5008. else
  5009. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5010. path->keep_locks = 0;
  5011. if (ret < 0)
  5012. return ret;
  5013. nritems = btrfs_header_nritems(path->nodes[0]);
  5014. /*
  5015. * by releasing the path above we dropped all our locks. A balance
  5016. * could have added more items next to the key that used to be
  5017. * at the very end of the block. So, check again here and
  5018. * advance the path if there are now more items available.
  5019. */
  5020. if (nritems > 0 && path->slots[0] < nritems - 1) {
  5021. if (ret == 0)
  5022. path->slots[0]++;
  5023. ret = 0;
  5024. goto done;
  5025. }
  5026. /*
  5027. * So the above check misses one case:
  5028. * - after releasing the path above, someone has removed the item that
  5029. * used to be at the very end of the block, and balance between leafs
  5030. * gets another one with bigger key.offset to replace it.
  5031. *
  5032. * This one should be returned as well, or we can get leaf corruption
  5033. * later(esp. in __btrfs_drop_extents()).
  5034. *
  5035. * And a bit more explanation about this check,
  5036. * with ret > 0, the key isn't found, the path points to the slot
  5037. * where it should be inserted, so the path->slots[0] item must be the
  5038. * bigger one.
  5039. */
  5040. if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
  5041. ret = 0;
  5042. goto done;
  5043. }
  5044. while (level < BTRFS_MAX_LEVEL) {
  5045. if (!path->nodes[level]) {
  5046. ret = 1;
  5047. goto done;
  5048. }
  5049. slot = path->slots[level] + 1;
  5050. c = path->nodes[level];
  5051. if (slot >= btrfs_header_nritems(c)) {
  5052. level++;
  5053. if (level == BTRFS_MAX_LEVEL) {
  5054. ret = 1;
  5055. goto done;
  5056. }
  5057. continue;
  5058. }
  5059. if (next) {
  5060. btrfs_tree_unlock_rw(next, next_rw_lock);
  5061. free_extent_buffer(next);
  5062. }
  5063. next = c;
  5064. next_rw_lock = path->locks[level];
  5065. ret = read_block_for_search(NULL, root, path, &next, level,
  5066. slot, &key, 0);
  5067. if (ret == -EAGAIN)
  5068. goto again;
  5069. if (ret < 0) {
  5070. btrfs_release_path(path);
  5071. goto done;
  5072. }
  5073. if (!path->skip_locking) {
  5074. ret = btrfs_try_tree_read_lock(next);
  5075. if (!ret && time_seq) {
  5076. /*
  5077. * If we don't get the lock, we may be racing
  5078. * with push_leaf_left, holding that lock while
  5079. * itself waiting for the leaf we've currently
  5080. * locked. To solve this situation, we give up
  5081. * on our lock and cycle.
  5082. */
  5083. free_extent_buffer(next);
  5084. btrfs_release_path(path);
  5085. cond_resched();
  5086. goto again;
  5087. }
  5088. if (!ret) {
  5089. btrfs_set_path_blocking(path);
  5090. btrfs_tree_read_lock(next);
  5091. btrfs_clear_path_blocking(path, next,
  5092. BTRFS_READ_LOCK);
  5093. }
  5094. next_rw_lock = BTRFS_READ_LOCK;
  5095. }
  5096. break;
  5097. }
  5098. path->slots[level] = slot;
  5099. while (1) {
  5100. level--;
  5101. c = path->nodes[level];
  5102. if (path->locks[level])
  5103. btrfs_tree_unlock_rw(c, path->locks[level]);
  5104. free_extent_buffer(c);
  5105. path->nodes[level] = next;
  5106. path->slots[level] = 0;
  5107. if (!path->skip_locking)
  5108. path->locks[level] = next_rw_lock;
  5109. if (!level)
  5110. break;
  5111. ret = read_block_for_search(NULL, root, path, &next, level,
  5112. 0, &key, 0);
  5113. if (ret == -EAGAIN)
  5114. goto again;
  5115. if (ret < 0) {
  5116. btrfs_release_path(path);
  5117. goto done;
  5118. }
  5119. if (!path->skip_locking) {
  5120. ret = btrfs_try_tree_read_lock(next);
  5121. if (!ret) {
  5122. btrfs_set_path_blocking(path);
  5123. btrfs_tree_read_lock(next);
  5124. btrfs_clear_path_blocking(path, next,
  5125. BTRFS_READ_LOCK);
  5126. }
  5127. next_rw_lock = BTRFS_READ_LOCK;
  5128. }
  5129. }
  5130. ret = 0;
  5131. done:
  5132. unlock_up(path, 0, 1, 0, NULL);
  5133. path->leave_spinning = old_spinning;
  5134. if (!old_spinning)
  5135. btrfs_set_path_blocking(path);
  5136. return ret;
  5137. }
  5138. /*
  5139. * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
  5140. * searching until it gets past min_objectid or finds an item of 'type'
  5141. *
  5142. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5143. */
  5144. int btrfs_previous_item(struct btrfs_root *root,
  5145. struct btrfs_path *path, u64 min_objectid,
  5146. int type)
  5147. {
  5148. struct btrfs_key found_key;
  5149. struct extent_buffer *leaf;
  5150. u32 nritems;
  5151. int ret;
  5152. while (1) {
  5153. if (path->slots[0] == 0) {
  5154. btrfs_set_path_blocking(path);
  5155. ret = btrfs_prev_leaf(root, path);
  5156. if (ret != 0)
  5157. return ret;
  5158. } else {
  5159. path->slots[0]--;
  5160. }
  5161. leaf = path->nodes[0];
  5162. nritems = btrfs_header_nritems(leaf);
  5163. if (nritems == 0)
  5164. return 1;
  5165. if (path->slots[0] == nritems)
  5166. path->slots[0]--;
  5167. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5168. if (found_key.objectid < min_objectid)
  5169. break;
  5170. if (found_key.type == type)
  5171. return 0;
  5172. if (found_key.objectid == min_objectid &&
  5173. found_key.type < type)
  5174. break;
  5175. }
  5176. return 1;
  5177. }
  5178. /*
  5179. * search in extent tree to find a previous Metadata/Data extent item with
  5180. * min objecitd.
  5181. *
  5182. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5183. */
  5184. int btrfs_previous_extent_item(struct btrfs_root *root,
  5185. struct btrfs_path *path, u64 min_objectid)
  5186. {
  5187. struct btrfs_key found_key;
  5188. struct extent_buffer *leaf;
  5189. u32 nritems;
  5190. int ret;
  5191. while (1) {
  5192. if (path->slots[0] == 0) {
  5193. btrfs_set_path_blocking(path);
  5194. ret = btrfs_prev_leaf(root, path);
  5195. if (ret != 0)
  5196. return ret;
  5197. } else {
  5198. path->slots[0]--;
  5199. }
  5200. leaf = path->nodes[0];
  5201. nritems = btrfs_header_nritems(leaf);
  5202. if (nritems == 0)
  5203. return 1;
  5204. if (path->slots[0] == nritems)
  5205. path->slots[0]--;
  5206. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5207. if (found_key.objectid < min_objectid)
  5208. break;
  5209. if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
  5210. found_key.type == BTRFS_METADATA_ITEM_KEY)
  5211. return 0;
  5212. if (found_key.objectid == min_objectid &&
  5213. found_key.type < BTRFS_EXTENT_ITEM_KEY)
  5214. break;
  5215. }
  5216. return 1;
  5217. }