ctree.c 147 KB

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