ctree.c 147 KB

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