ctree.c 148 KB

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