ctree.c 134 KB

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