ctree.c 147 KB

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