ctree.c 133 KB

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