ctree.c 147 KB

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