ctree.c 135 KB

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