ctree.c 150 KB

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