ctree.c 147 KB

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