ctree.c 149 KB

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