ctree.c 151 KB

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