ctree.c 147 KB

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